hyph: .hcode should behave like .tr
[neatroff.git] / wb.c
bloba47bd3ae53812a8e2211fe26ca276487d7c4be11
1 /* word buffer */
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "roff.h"
7 /* the current font, size and color */
8 #define R_F(wb) ((wb)->r_f >= 0 ? (wb)->r_f : n_f) /* current font */
9 #define R_S(wb) ((wb)->r_s >= 0 ? (wb)->r_s : n_s) /* current size */
10 #define R_M(wb) ((wb)->r_m >= 0 ? (wb)->r_m : n_m) /* current color */
11 /* italic correction */
12 #define glyph_ic(g) (MAX(0, (g)->urx - (g)->wid))
13 #define glyph_icleft(g) (MAX(0, -(g)->llx))
14 /* like DEVWID() but handles negative w */
15 #define SDEVWID(sz, w) ((w) >= 0 ? DEVWID((sz), (w)) : -DEVWID((sz), -(w)))
16 /* the maximum and minimum values of bounding box coordinates */
17 #define BBMAX (1 << 29)
18 #define BBMIN -BBMAX
20 void wb_init(struct wb *wb)
22 memset(wb, 0, sizeof(*wb));
23 sbuf_init(&wb->sbuf);
24 wb->f = -1;
25 wb->s = -1;
26 wb->m = -1;
27 wb->r_f = -1;
28 wb->r_s = -1;
29 wb->r_m = -1;
30 wb->icleft_ll = -1;
31 wb->llx = BBMAX;
32 wb->lly = BBMAX;
33 wb->urx = BBMIN;
34 wb->ury = BBMIN;
37 void wb_done(struct wb *wb)
39 sbuf_done(&wb->sbuf);
42 /* update wb->st and wb->sb */
43 static void wb_stsb(struct wb *wb)
45 wb->st = MIN(wb->st, wb->v - (wb->s * SC_IN / 72));
46 wb->sb = MAX(wb->sb, wb->v);
49 /* update bounding box */
50 static void wb_bbox(struct wb *wb, int llx, int lly, int urx, int ury)
52 wb->llx = MIN(wb->llx, wb->h + llx);
53 wb->lly = MIN(wb->lly, -wb->v + lly);
54 wb->urx = MAX(wb->urx, wb->h + urx);
55 wb->ury = MAX(wb->ury, -wb->v + ury);
58 /* append font and size to the buffer if needed */
59 static void wb_font(struct wb *wb)
61 if (wb->f != R_F(wb)) {
62 sbuf_printf(&wb->sbuf, "%cf(%02d", c_ec, R_F(wb));
63 wb->f = R_F(wb);
65 if (wb->s != R_S(wb)) {
66 sbuf_printf(&wb->sbuf, "%cs(%02d", c_ec, R_S(wb));
67 wb->s = R_S(wb);
69 if (!n_cp && wb->m != R_M(wb)) {
70 sbuf_printf(&wb->sbuf, "%cm[%s]", c_ec, clr_str(R_M(wb)));
71 wb->m = R_M(wb);
73 wb_stsb(wb);
76 /* pending font, size or color changes */
77 static int wb_pendingfont(struct wb *wb)
79 return wb->f != R_F(wb) || wb->s != R_S(wb) ||
80 (!n_cp && wb->m != R_M(wb));
83 void wb_hmov(struct wb *wb, int n)
85 wb->h += n;
86 sbuf_printf(&wb->sbuf, "%ch'%du'", c_ec, n);
89 void wb_vmov(struct wb *wb, int n)
91 wb->v += n;
92 sbuf_printf(&wb->sbuf, "%cv'%du'", c_ec, n);
95 void wb_els(struct wb *wb, int els)
97 if (els > wb->els_pos)
98 wb->els_pos = els;
99 if (els < wb->els_neg)
100 wb->els_neg = els;
101 sbuf_printf(&wb->sbuf, "%cx'%du'", c_ec, els);
104 void wb_etc(struct wb *wb, char *x)
106 wb_font(wb);
107 sbuf_printf(&wb->sbuf, "%cX\x02%s\x02", c_ec, x);
110 /* make sure nothing is appended to wb after the last wb_put() */
111 static void wb_prevcheck(struct wb *wb)
113 if (wb->prev_ll != sbuf_len(&wb->sbuf))
114 wb->prev_n = 0;
117 /* mark wb->prev_c[] as valid */
118 static void wb_prevok(struct wb *wb)
120 wb->prev_ll = sbuf_len(&wb->sbuf);
123 /* append c to wb->prev_c[] */
124 static void wb_prevput(struct wb *wb, char *c, int ll)
126 if (wb->prev_n == LEN(wb->prev_c))
127 wb->prev_n--;
128 memmove(wb->prev_l + 1, wb->prev_l, wb->prev_n * sizeof(wb->prev_l[0]));
129 memmove(wb->prev_h + 1, wb->prev_h, wb->prev_n * sizeof(wb->prev_h[0]));
130 memmove(wb->prev_c + 1, wb->prev_c, wb->prev_n * sizeof(wb->prev_c[0]));
131 wb->prev_l[0] = ll;
132 wb->prev_h[0] = wb->h;
133 strcpy(wb->prev_c[0], c);
134 wb->prev_n++;
135 wb_prevok(wb);
138 /* strip the last i characters from wb */
139 static void wb_prevpop(struct wb *wb, int i)
141 int n = wb->prev_n - i;
142 sbuf_cut(&wb->sbuf, wb->prev_l[i - 1]);
143 wb->h = wb->prev_h[i - 1];
144 memmove(wb->prev_l, wb->prev_l + i, n * sizeof(wb->prev_l[0]));
145 memmove(wb->prev_h, wb->prev_h + i, n * sizeof(wb->prev_h[0]));
146 memmove(wb->prev_c, wb->prev_c + i, n * sizeof(wb->prev_c[0]));
147 wb->prev_n = n;
148 wb->prev_ll = sbuf_len(&wb->sbuf);
151 /* return the i-th last character inserted via wb_put() */
152 static char *wb_prev(struct wb *wb, int i)
154 wb_prevcheck(wb);
155 return i < wb->prev_n ? wb->prev_c[i] : NULL;
158 static struct glyph *wb_prevglyph(struct wb *wb)
160 return wb_prev(wb, 0) ? dev_glyph(wb_prev(wb, 0), wb->f) : NULL;
163 void wb_put(struct wb *wb, char *c)
165 struct glyph *g;
166 int ll, zerowidth;
167 if (c[0] == '\n') {
168 wb->part = 0;
169 return;
171 if (c[0] == ' ') {
172 wb_hmov(wb, N_SS(R_F(wb), R_S(wb)));
173 return;
175 if (c[0] == '\t' || c[0] == '\x01' ||
176 (c[0] == c_ni && (c[1] == '\t' || c[1] == '\x01'))) {
177 sbuf_append(&wb->sbuf, c);
178 return;
180 g = dev_glyph(c, R_F(wb));
181 zerowidth = !strcmp(c_hc, c) || !strcmp(c_bp, c);
182 if (!g && c[0] == c_ec && !zerowidth) { /* unknown escape */
183 memmove(c, c + 1, strlen(c));
184 g = dev_glyph(c, R_F(wb));
186 if (g && !zerowidth && wb->icleft_ll == sbuf_len(&wb->sbuf))
187 if (glyph_icleft(g))
188 wb_hmov(wb, SDEVWID(R_S(wb), glyph_icleft(g)));
189 wb->icleft_ll = -1;
190 wb_font(wb);
191 wb_prevcheck(wb); /* make sure wb->prev_c[] is valid */
192 ll = sbuf_len(&wb->sbuf); /* sbuf length before inserting c */
193 if (!c[1] || c[0] == c_ec || c[0] == c_ni || utf8one(c)) {
194 if (c[0] == c_ni && c[1] == c_ec)
195 sbuf_printf(&wb->sbuf, "%c%c", c_ec, c_ec);
196 else
197 sbuf_append(&wb->sbuf, c);
198 } else {
199 if (c[1] && !c[2])
200 sbuf_printf(&wb->sbuf, "%c(%s", c_ec, c);
201 else
202 sbuf_printf(&wb->sbuf, "%cC'%s'", c_ec, c);
204 if (!zerowidth) {
205 wb_prevput(wb, c, ll);
206 if (!n_cp && g)
207 wb_bbox(wb, SDEVWID(wb->s, g->llx),
208 SDEVWID(wb->s, g->lly),
209 SDEVWID(wb->s, g->urx),
210 SDEVWID(wb->s, g->ury));
211 wb->h += charwid(wb->f, wb->s, g ? g->wid : 0);
212 wb->ct |= g ? g->type : 0;
213 wb_stsb(wb);
217 /* just like wb_put(), but call cdef_expand() if c is defined */
218 void wb_putexpand(struct wb *wb, char *c)
220 if (cdef_expand(wb, c, R_F(wb)))
221 wb_put(wb, c);
224 /* return zero if c formed a ligature with its previous character */
225 int wb_lig(struct wb *wb, char *c)
227 char lig[GNLEN] = "";
228 char *cs[LIGLEN + 2];
229 int i = -1;
230 int ligpos;
231 if (wb_pendingfont(wb)) /* font changes disable ligatures */
232 return 1;
233 cs[0] = c;
234 while (wb_prev(wb, ++i))
235 cs[i + 1] = wb_prev(wb, i);
236 ligpos = font_lig(dev_font(R_F(wb)), cs, i + 1);
237 if (ligpos > 1) {
238 for (i = 0; i < ligpos - 1; i++)
239 strcat(lig, wb_prev(wb, ligpos - i - 2));
240 strcat(lig, c);
241 wb_prevpop(wb, ligpos - 1);
242 wb_put(wb, lig);
243 return 0;
245 return 1;
248 /* return 0 if pairwise kerning was done */
249 int wb_kern(struct wb *wb, char *c)
251 int val;
252 if (wb_pendingfont(wb) || !wb_prev(wb, 0))
253 return 1;
254 val = font_kern(dev_font(R_F(wb)), wb_prev(wb, 0), c);
255 if (val)
256 wb_hmov(wb, charwid(R_F(wb), R_S(wb), val));
257 wb_prevok(wb); /* kerning should not prevent ligatures */
258 return !val;
261 int wb_part(struct wb *wb)
263 return wb->part;
266 void wb_setpart(struct wb *wb)
268 wb->part = 1;
271 void wb_drawl(struct wb *wb, int c, int h, int v)
273 wb_font(wb);
274 sbuf_printf(&wb->sbuf, "%cD'%c %du %du'", c_ec, c, h, v);
275 wb->h += h;
276 wb->v += v;
277 wb_stsb(wb);
280 void wb_drawc(struct wb *wb, int c, int r)
282 wb_font(wb);
283 sbuf_printf(&wb->sbuf, "%cD'%c %du'", c_ec, c, r);
284 wb->h += r;
287 void wb_drawe(struct wb *wb, int c, int h, int v)
289 wb_font(wb);
290 sbuf_printf(&wb->sbuf, "%cD'%c %du %du'", c_ec, c, h, v);
291 wb->h += h;
294 void wb_drawa(struct wb *wb, int c, int h1, int v1, int h2, int v2)
296 wb_font(wb);
297 sbuf_printf(&wb->sbuf, "%cD'%c %du %du %du %du'", c_ec, c, h1, v1, h2, v2);
298 wb->h += h1 + h2;
299 wb->v += v1 + v2;
300 wb_stsb(wb);
303 void wb_drawxbeg(struct wb *wb, int c)
305 wb_font(wb);
306 sbuf_printf(&wb->sbuf, "%cD'%c", c_ec, c);
309 void wb_drawxdot(struct wb *wb, int h, int v)
311 sbuf_printf(&wb->sbuf, " %du %du", h, v);
312 wb->h += h;
313 wb->v += v;
314 wb_stsb(wb);
317 void wb_drawxend(struct wb *wb)
319 sbuf_printf(&wb->sbuf, "'");
322 void wb_reset(struct wb *wb)
324 wb_done(wb);
325 wb_init(wb);
328 char *wb_buf(struct wb *wb)
330 return sbuf_buf(&wb->sbuf);
333 static void wb_putc(struct wb *wb, int t, char *s)
335 switch (t) {
336 case 0:
337 case 'C':
338 wb_put(wb, s);
339 break;
340 case 'D':
341 ren_dcmd(wb, s);
342 break;
343 case 'f':
344 wb->r_f = atoi(s);
345 break;
346 case 'h':
347 wb_hmov(wb, atoi(s));
348 break;
349 case 'm':
350 wb->r_m = clr_get(s);
351 break;
352 case 's':
353 wb->r_s = atoi(s);
354 break;
355 case 'v':
356 wb_vmov(wb, atoi(s));
357 break;
358 case 'x':
359 wb_els(wb, atoi(s));
360 break;
361 case 'X':
362 wb_etc(wb, s);
363 break;
367 void wb_cat(struct wb *wb, struct wb *src)
369 char *s = sbuf_buf(&src->sbuf);
370 char d[ILNLEN];
371 int c, part;
372 while ((c = escread(&s, d)) >= 0)
373 wb_putc(wb, c, d);
374 part = src->part;
375 wb->r_s = -1;
376 wb->r_f = -1;
377 wb->r_m = -1;
378 wb_reset(src);
379 src->part = part;
382 int wb_wid(struct wb *wb)
384 return wb->h;
387 int wb_empty(struct wb *wb)
389 return sbuf_empty(&wb->sbuf);
392 /* return 1 if wb ends a sentence (.?!) */
393 int wb_eos(struct wb *wb)
395 int i = 0;
396 while (wb_prev(wb, i) && strchr("'\")]*", wb_prev(wb, i)[0]))
397 i++;
398 return wb_prev(wb, i) && strchr(".?!", wb_prev(wb, i)[0]);
401 void wb_wconf(struct wb *wb, int *ct, int *st, int *sb,
402 int *llx, int *lly, int *urx, int *ury)
404 *ct = wb->ct;
405 *st = -wb->st;
406 *sb = -wb->sb;
407 *llx = wb->llx < BBMAX ? wb->llx : 0;
408 *lly = wb->lly < BBMAX ? -wb->lly : 0;
409 *urx = wb->urx > BBMIN ? wb->urx : 0;
410 *ury = wb->ury > BBMIN ? -wb->ury : 0;
413 /* skip troff requests; return 1 if read c_hc */
414 static int skipreqs(char **s, struct wb *w1)
416 char d[ILNLEN];
417 char *r = *s;
418 int c;
419 if (w1)
420 wb_reset(w1);
421 while ((c = escread(s, d)) > 0) {
422 if (w1)
423 wb_putc(w1, c, d);
424 r = *s;
426 if (c < 0 || !strcmp(c_hc, d))
427 return 1;
428 *s = r;
429 return 0;
432 /* return the size of \(hy if appended to wb */
433 int wb_dashwid(struct wb *wb)
435 struct glyph *g = dev_glyph("hy", R_F(wb));
436 return charwid(R_F(wb), R_S(wb), g ? g->wid : 0);
439 /* find explicit hyphenation positions: dashes, \: and \% */
440 int wb_hyphmark(char *word, int *hyidx, int *hyins)
442 char d[ILNLEN];
443 char *s = word;
444 int c, n = 0;
445 if (skipreqs(&s, NULL))
446 return -1;
447 while ((c = escread(&s, d)) >= 0 && n < NHYPHSWORD) {
448 if (!c && !strcmp(c_hc, d)) {
449 hyins[n] = 1;
450 hyidx[n++] = s - word;
452 if (!c && (!strcmp(c_bp, d) || !strcmp("-", d) ||
453 (!strcmp("em", d) || !strcmp("hy", d)))) {
454 hyins[n] = 0;
455 hyidx[n++] = s - word;
458 return n;
461 /* find the hyphenation positions of the given word */
462 int wb_hyph(char *src, int *hyidx, int flg)
464 char word[WORDLEN]; /* word to pass to hyphenate() */
465 char hyph[WORDLEN]; /* hyphenation points returned from hyphenate() */
466 char *iw[WORDLEN]; /* beginning of i-th char in word */
467 char *is[WORDLEN]; /* beginning of i-th char in s */
468 int n = 0; /* the number of characters in word */
469 int nhy = 0; /* number of hyphenations found */
470 char d[ILNLEN];
471 struct wb wb;
472 char *s = src;
473 char *prev_s = s;
474 char *wp = word, *we = word + sizeof(word);
475 int i, c;
476 wb_init(&wb);
477 skipreqs(&s, &wb);
478 while ((c = escread(&s, d)) >= 0 && (c > 0 || strlen(d) + 1 < we - wp)) {
479 wb_putc(&wb, c, d);
480 if (c == 0) {
481 iw[n] = wp;
482 is[n] = prev_s;
483 /* ignore multi-char aliases except for ligatures */
484 if (!utf8one(d) && !font_islig(dev_font(R_F(&wb)), d))
485 strcpy(d, ".");
486 strcpy(wp, d);
487 wp = strchr(wp, '\0');
488 n++;
490 prev_s = s;
492 wb_done(&wb);
493 if (n < 3)
494 return 0;
495 memset(hyph, 0, (wp - word) * sizeof(hyph[0]));
496 hyphenate(hyph, word, flg);
497 for (i = 1; i < n - 1 && nhy < NHYPHSWORD; i++)
498 if (hyph[iw[i] - word])
499 hyidx[nhy++] = is[i] - src;
500 return nhy;
503 void wb_italiccorrection(struct wb *wb)
505 struct glyph *g = wb_prevglyph(wb);
506 if (g && glyph_ic(g))
507 wb_hmov(wb, SDEVWID(wb->s, glyph_ic(g)));
510 void wb_italiccorrectionleft(struct wb *wb)
512 wb->icleft_ll = sbuf_len(&wb->sbuf);
515 void wb_fnszget(struct wb *wb, int *fn, int *sz, int *m)
517 *fn = wb->r_f;
518 *sz = wb->r_s;
519 *m = wb->r_m;
522 void wb_fnszset(struct wb *wb, int fn, int sz, int m)
524 wb->r_f = fn;
525 wb->r_s = sz;
526 wb->r_m = m;
529 void wb_catstr(struct wb *wb, char *s, char *end)
531 char d[ILNLEN];
532 int c;
533 while (s < end && (c = escread(&s, d)) >= 0)
534 wb_putc(wb, c, d);