tr: escaped spaces inside macro arguments
[neatroff.git] / wb.c
blob44cc7c5f7c22eb84aba342b28b143d3194f5ab26
1 /* word buffer */
2 #include <ctype.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include "roff.h"
8 /* the current font, size and color */
9 #define R_F(wb) ((wb)->r_f >= 0 ? (wb)->r_f : n_f) /* current font */
10 #define R_S(wb) ((wb)->r_s >= 0 ? (wb)->r_s : n_s) /* current size */
11 #define R_M(wb) ((wb)->r_m >= 0 ? (wb)->r_m : n_m) /* current color */
12 /* italic correction */
13 #define glyph_ic(g) (MAX(0, (g)->urx - (g)->wid))
14 #define glyph_icleft(g) (MAX(0, -(g)->llx))
15 /* the maximum and minimum values of bounding box coordinates */
16 #define BBMAX (1 << 29)
17 #define BBMIN -BBMAX
19 static void wb_flushsub(struct wb *wb);
21 void wb_init(struct wb *wb)
23 memset(wb, 0, sizeof(*wb));
24 sbuf_init(&wb->sbuf);
25 wb->sub_collect = 1;
26 wb->f = -1;
27 wb->s = -1;
28 wb->m = -1;
29 wb->r_f = -1;
30 wb->r_s = -1;
31 wb->r_m = -1;
32 wb->llx = BBMAX;
33 wb->lly = BBMAX;
34 wb->urx = BBMIN;
35 wb->ury = BBMIN;
38 void wb_done(struct wb *wb)
40 sbuf_done(&wb->sbuf);
43 /* update wb->st and wb->sb */
44 static void wb_stsb(struct wb *wb)
46 wb->st = MIN(wb->st, wb->v - (wb->s * SC_IN / 72));
47 wb->sb = MAX(wb->sb, wb->v);
50 /* update bounding box */
51 static void wb_bbox(struct wb *wb, int llx, int lly, int urx, int ury)
53 wb->llx = MIN(wb->llx, wb->h + llx);
54 wb->lly = MIN(wb->lly, -wb->v + lly);
55 wb->urx = MAX(wb->urx, wb->h + urx);
56 wb->ury = MAX(wb->ury, -wb->v + ury);
59 /* pending font, size or color changes */
60 static int wb_pendingfont(struct wb *wb)
62 return wb->f != R_F(wb) || wb->s != R_S(wb) ||
63 (!n_cp && wb->m != R_M(wb));
66 /* append font and size to the buffer if needed */
67 static void wb_flushfont(struct wb *wb)
69 if (wb->f != R_F(wb)) {
70 sbuf_printf(&wb->sbuf, "%cf(%02d", c_ec, R_F(wb));
71 wb->f = R_F(wb);
73 if (wb->s != R_S(wb)) {
74 sbuf_printf(&wb->sbuf, "%cs(%02d", c_ec, R_S(wb));
75 wb->s = R_S(wb);
77 if (!n_cp && wb->m != R_M(wb)) {
78 sbuf_printf(&wb->sbuf, "%cm[%s]", c_ec, clr_str(R_M(wb)));
79 wb->m = R_M(wb);
81 wb_stsb(wb);
84 /* apply font and size changes and flush the collected subword */
85 static void wb_flush(struct wb *wb)
87 wb_flushsub(wb);
88 wb_flushfont(wb);
91 void wb_hmov(struct wb *wb, int n)
93 wb_flushsub(wb);
94 wb->h += n;
95 sbuf_printf(&wb->sbuf, "%ch'%du'", c_ec, n);
98 void wb_vmov(struct wb *wb, int n)
100 wb_flushsub(wb);
101 wb->v += n;
102 sbuf_printf(&wb->sbuf, "%cv'%du'", c_ec, n);
105 void wb_els(struct wb *wb, int els)
107 wb_flushsub(wb);
108 if (els > wb->els_pos)
109 wb->els_pos = els;
110 if (els < wb->els_neg)
111 wb->els_neg = els;
112 sbuf_printf(&wb->sbuf, "%cx'%du'", c_ec, els);
115 void wb_etc(struct wb *wb, char *x)
117 wb_flush(wb);
118 sbuf_printf(&wb->sbuf, "%cX\x02%s\x02", c_ec, x);
121 static void wb_putbuf(struct wb *wb, char *c)
123 struct glyph *g;
124 int zerowidth;
125 if (c[0] == '\t' || c[0] == '\x01' ||
126 (c[0] == c_ni && (c[1] == '\t' || c[1] == '\x01'))) {
127 sbuf_append(&wb->sbuf, c);
128 return;
130 g = dev_glyph(c, wb->f);
131 zerowidth = !strcmp(c_hc, c) || !strcmp(c_bp, c);
132 if (!g && c[0] == c_ec && !zerowidth) { /* unknown escape */
133 memmove(c, c + 1, strlen(c));
134 g = dev_glyph(c, wb->f);
136 if (g && !zerowidth && wb->icleft && glyph_icleft(g))
137 wb_hmov(wb, font_wid(g->font, wb->s, glyph_icleft(g)));
138 wb->icleft = 0;
139 if (!c[1] || c[0] == c_ec || c[0] == c_ni || utf8one(c)) {
140 if (c[0] == c_ni && c[1] == c_ec)
141 sbuf_printf(&wb->sbuf, "%c%c", c_ec, c_ec);
142 else
143 sbuf_append(&wb->sbuf, c);
144 } else {
145 if (c[1] && !c[2])
146 sbuf_printf(&wb->sbuf, "%c(%s", c_ec, c);
147 else
148 sbuf_printf(&wb->sbuf, "%cC'%s'", c_ec, c);
150 if (!zerowidth) {
151 if (!n_cp && g)
152 wb_bbox(wb, font_wid(g->font, wb->s, g->llx),
153 font_wid(g->font, wb->s, g->lly),
154 font_wid(g->font, wb->s, g->urx),
155 font_wid(g->font, wb->s, g->ury));
156 wb->h += g ? font_gwid(g->font, dev_font(wb->f), wb->s, g->wid) : 0;
157 wb->ct |= g ? g->type : 0;
158 wb_stsb(wb);
162 int c_isdash(char *c)
164 return !strcmp("-", c) || !strcmp("em", c) || !strcmp("hy", c);
167 /* return nonzero if it cannot be hyphenated */
168 static int wb_hyph(char src[][GNLEN], int src_n, char *src_hyph, int flg)
170 char word[WORDLEN * GNLEN]; /* word to pass to hyphenate() */
171 char hyph[WORDLEN * GNLEN]; /* hyphenation points of word */
172 int smap[WORDLEN]; /* the mapping from src[] to word[] */
173 char *s, *d;
174 int i;
175 d = word;
176 *d = '\0';
177 for (i = 0; i < src_n; i++) {
178 s = src[i];
179 smap[i] = d - word;
180 if (c_isdash(s) || !strcmp(c_hc, s))
181 return 1;
182 if (!strcmp(c_bp, s))
183 continue;
184 if (!utf8one(s) || (!s[1] && !isalpha((unsigned char) s[0])))
185 strcpy(d, ".");
186 else
187 strcpy(d, s);
188 d = strchr(d, '\0');
190 memset(hyph, 0, (d - word) * sizeof(hyph[0]));
191 hyphenate(hyph, word, flg);
192 for (i = 0; i < src_n; i++)
193 src_hyph[i] = hyph[smap[i]];
194 return 0;
197 static int wb_collect(struct wb *wb, int val)
199 int old = wb->sub_collect;
200 wb->sub_collect = val;
201 return old;
204 static void wb_flushsub(struct wb *wb)
206 struct font *fn;
207 struct glyph *gsrc[WORDLEN];
208 struct glyph *gdst[WORDLEN];
209 int x[WORDLEN], y[WORDLEN], xadv[WORDLEN], yadv[WORDLEN];
210 int dmap[WORDLEN];
211 char src_hyph[WORDLEN];
212 int dst_n, i;
213 if (!wb->sub_n || !wb->sub_collect)
214 return;
215 wb->sub_collect = 0;
216 fn = dev_font(wb->f);
217 if (!n_hy || wb_hyph(wb->sub_c, wb->sub_n, src_hyph, n_hy))
218 memset(src_hyph, 0, sizeof(src_hyph));
219 for (i = 0; i < wb->sub_n; i++)
220 gsrc[i] = font_find(fn, wb->sub_c[i]);
221 dst_n = font_layout(fn, gsrc, wb->sub_n, wb->s,
222 gdst, dmap, x, y, xadv, yadv, n_lg, n_kn);
223 for (i = 0; i < dst_n; i++) {
224 if (x[i])
225 wb_hmov(wb, font_wid(fn, wb->s, x[i]));
226 if (y[i])
227 wb_vmov(wb, font_wid(fn, wb->s, y[i]));
228 if (src_hyph[dmap[i]])
229 wb_putbuf(wb, c_hc);
230 if (gdst[i] == gsrc[dmap[i]])
231 wb_putbuf(wb, wb->sub_c[dmap[i]]);
232 else
233 wb_putbuf(wb, gdst[i]->name);
234 if (x[i] || xadv[i])
235 wb_hmov(wb, font_wid(fn, wb->s, xadv[i] - x[i]));
236 if (y[i] || yadv[i])
237 wb_vmov(wb, font_wid(fn, wb->s, yadv[i] - y[i]));
239 wb->sub_n = 0;
240 wb->icleft = 0;
241 wb->sub_collect = 1;
244 void wb_put(struct wb *wb, char *c)
246 if (c[0] == '\n') {
247 wb->part = 0;
248 return;
250 if (c[0] == ' ') {
251 wb_flushsub(wb);
252 wb_hmov(wb, font_swid(dev_font(R_F(wb)), R_S(wb), n_ss));
253 return;
255 if (wb_pendingfont(wb) || wb->sub_n == LEN(wb->sub_c))
256 wb_flush(wb);
257 if (wb->sub_collect) {
258 if (font_find(dev_font(wb->f), c))
259 strcpy(wb->sub_c[wb->sub_n++], c);
260 else
261 wb_putraw(wb, c);
262 } else {
263 wb_putbuf(wb, c);
267 /* just like wb_put() but disable subword collection */
268 void wb_putraw(struct wb *wb, char *c)
270 int collect;
271 wb_flushsub(wb);
272 collect = wb_collect(wb, 0);
273 wb_put(wb, c);
274 wb_collect(wb, collect);
277 /* just like wb_put(), but call cdef_expand() if c is defined */
278 void wb_putexpand(struct wb *wb, char *c)
280 if (cdef_expand(wb, c, R_F(wb)))
281 wb_put(wb, c);
284 int wb_part(struct wb *wb)
286 return wb->part;
289 void wb_setpart(struct wb *wb)
291 wb->part = 1;
294 void wb_drawl(struct wb *wb, int c, int h, int v)
296 wb_flush(wb);
297 sbuf_printf(&wb->sbuf, "%cD'%c %du %du'", c_ec, c, h, v);
298 wb->h += h;
299 wb->v += v;
300 wb_stsb(wb);
303 void wb_drawc(struct wb *wb, int c, int r)
305 wb_flush(wb);
306 sbuf_printf(&wb->sbuf, "%cD'%c %du'", c_ec, c, r);
307 wb->h += r;
310 void wb_drawe(struct wb *wb, int c, int h, int v)
312 wb_flush(wb);
313 sbuf_printf(&wb->sbuf, "%cD'%c %du %du'", c_ec, c, h, v);
314 wb->h += h;
317 void wb_drawa(struct wb *wb, int c, int h1, int v1, int h2, int v2)
319 wb_flush(wb);
320 sbuf_printf(&wb->sbuf, "%cD'%c %du %du %du %du'",
321 c_ec, c, h1, v1, h2, v2);
322 wb->h += h1 + h2;
323 wb->v += v1 + v2;
324 wb_stsb(wb);
327 void wb_drawxbeg(struct wb *wb, int c)
329 wb_flush(wb);
330 sbuf_printf(&wb->sbuf, "%cD'%c", c_ec, c);
333 void wb_drawxdot(struct wb *wb, int h, int v)
335 sbuf_printf(&wb->sbuf, " %du %du", h, v);
336 wb->h += h;
337 wb->v += v;
338 wb_stsb(wb);
341 void wb_drawxend(struct wb *wb)
343 sbuf_printf(&wb->sbuf, "'");
346 void wb_reset(struct wb *wb)
348 wb_done(wb);
349 wb_init(wb);
352 char *wb_buf(struct wb *wb)
354 wb_flushsub(wb);
355 return sbuf_buf(&wb->sbuf);
358 static void wb_putc(struct wb *wb, int t, char *s)
360 if (t && t != 'C')
361 wb_flushsub(wb);
362 switch (t) {
363 case 0:
364 case 'C':
365 wb_put(wb, s);
366 break;
367 case 'D':
368 ren_dcmd(wb, s);
369 break;
370 case 'f':
371 wb->r_f = atoi(s);
372 break;
373 case 'h':
374 wb_hmov(wb, atoi(s));
375 break;
376 case 'm':
377 wb->r_m = clr_get(s);
378 break;
379 case 's':
380 wb->r_s = atoi(s);
381 break;
382 case 'v':
383 wb_vmov(wb, atoi(s));
384 break;
385 case 'x':
386 wb_els(wb, atoi(s));
387 break;
388 case 'X':
389 wb_etc(wb, s);
390 break;
394 void wb_cat(struct wb *wb, struct wb *src)
396 char *s;
397 char d[ILNLEN];
398 int c, part;
399 int collect;
400 wb_flushsub(src);
401 wb_flushsub(wb);
402 collect = wb_collect(wb, 0);
403 s = sbuf_buf(&src->sbuf);
404 while ((c = escread(&s, d)) >= 0)
405 wb_putc(wb, c, d);
406 part = src->part;
407 wb->r_s = -1;
408 wb->r_f = -1;
409 wb->r_m = -1;
410 wb_reset(src);
411 src->part = part;
412 wb_collect(wb, collect);
415 int wb_wid(struct wb *wb)
417 wb_flushsub(wb);
418 return wb->h;
421 int wb_hpos(struct wb *wb)
423 wb_flushsub(wb);
424 return wb->h;
427 int wb_vpos(struct wb *wb)
429 wb_flushsub(wb);
430 return wb->v;
433 int wb_empty(struct wb *wb)
435 return !wb->sub_n && sbuf_empty(&wb->sbuf);
438 /* return 1 if wb ends a sentence (.?!) */
439 int wb_eos(struct wb *wb)
441 int i = wb->sub_n - 1;
442 while (i > 0 && strchr("'\")]*", wb->sub_c[i][0]))
443 i--;
444 return i >= 0 && strchr(".?!", wb->sub_c[i][0]);
447 void wb_wconf(struct wb *wb, int *ct, int *st, int *sb,
448 int *llx, int *lly, int *urx, int *ury)
450 wb_flushsub(wb);
451 *ct = wb->ct;
452 *st = -wb->st;
453 *sb = -wb->sb;
454 *llx = wb->llx < BBMAX ? wb->llx : 0;
455 *lly = wb->lly < BBMAX ? -wb->lly : 0;
456 *urx = wb->urx > BBMIN ? wb->urx : 0;
457 *ury = wb->ury > BBMIN ? -wb->ury : 0;
460 static struct glyph *wb_prevglyph(struct wb *wb)
462 return wb->sub_n ? dev_glyph(wb->sub_c[wb->sub_n - 1], wb->f) : NULL;
465 void wb_italiccorrection(struct wb *wb)
467 struct glyph *g = wb_prevglyph(wb);
468 if (g && glyph_ic(g))
469 wb_hmov(wb, font_wid(g->font, wb->s, glyph_ic(g)));
472 void wb_italiccorrectionleft(struct wb *wb)
474 wb_flushsub(wb);
475 wb->icleft = 1;
478 void wb_fnszget(struct wb *wb, int *fn, int *sz, int *m)
480 wb_flushsub(wb);
481 *fn = wb->r_f;
482 *sz = wb->r_s;
483 *m = wb->r_m;
486 void wb_fnszset(struct wb *wb, int fn, int sz, int m)
488 wb->r_f = fn;
489 wb->r_s = sz;
490 wb->r_m = m;
493 void wb_catstr(struct wb *wb, char *s, char *end)
495 char d[ILNLEN];
496 int collect, c;
497 wb_flushsub(wb);
498 collect = wb_collect(wb, 0);
499 while (s < end && (c = escread(&s, d)) >= 0)
500 wb_putc(wb, c, d);
501 wb_collect(wb, collect);
504 /* return the size of \(hy if appended to wb */
505 int wb_dashwid(struct wb *wb)
507 struct glyph *g = dev_glyph("hy", wb->f);
508 return g ? font_gwid(g->font, dev_font(wb->f), wb->s, g->wid) : 0;