roff.h: describe the meaning of function prefixes
[neatroff.git] / wb.c
blob4f6bb8799df9619a91b6322749450dd25c546d05
1 /* word buffer */
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "roff.h"
7 #define R_F(wb) ((wb)->r_f >= 0 ? (wb)->r_f : n_f) /* current font */
8 #define R_S(wb) ((wb)->r_s >= 0 ? (wb)->r_s : n_s) /* current size */
9 #define R_M(wb) ((wb)->r_m >= 0 ? (wb)->r_m : n_m) /* current color */
11 void wb_init(struct wb *wb)
13 memset(wb, 0, sizeof(*wb));
14 sbuf_init(&wb->sbuf);
15 wb->f = -1;
16 wb->s = -1;
17 wb->m = -1;
18 wb->r_f = -1;
19 wb->r_s = -1;
20 wb->r_m = -1;
23 void wb_done(struct wb *wb)
25 sbuf_done(&wb->sbuf);
28 /* update wb->st and wb->sb */
29 static void wb_stsb(struct wb *wb)
31 wb->st = MIN(wb->st, wb->v - SC_HT);
32 wb->sb = MAX(wb->sb, wb->v);
35 /* append font and size to the buffer if needed */
36 static void wb_font(struct wb *wb)
38 if (wb->f != R_F(wb)) {
39 sbuf_printf(&wb->sbuf, "%cf(%02d", c_ec, R_F(wb));
40 wb->f = R_F(wb);
42 if (wb->s != R_S(wb)) {
43 sbuf_printf(&wb->sbuf, "%cs(%02d", c_ec, R_S(wb));
44 wb->s = R_S(wb);
46 if (!n_cp && wb->m != R_M(wb)) {
47 sbuf_printf(&wb->sbuf, "%cm[%s]", c_ec, clr_str(R_M(wb)));
48 wb->m = R_M(wb);
50 wb_stsb(wb);
53 void wb_hmov(struct wb *wb, int n)
55 wb->h += n;
56 sbuf_printf(&wb->sbuf, "%ch'%du'", c_ec, n);
59 void wb_vmov(struct wb *wb, int n)
61 wb->v += n;
62 sbuf_printf(&wb->sbuf, "%cv'%du'", c_ec, n);
65 void wb_els(struct wb *wb, int els)
67 if (els > wb->els_pos)
68 wb->els_pos = els;
69 if (els < wb->els_neg)
70 wb->els_neg = els;
71 sbuf_printf(&wb->sbuf, "%cx'%du'", c_ec, els);
74 void wb_etc(struct wb *wb, char *x)
76 wb_font(wb);
77 sbuf_printf(&wb->sbuf, "%cX\x02%s\x02", c_ec, x);
80 /* make sure nothing is appended to wb after the last wb_put() */
81 static void wb_prevcheck(struct wb *wb)
83 if (wb->prev_ll != sbuf_len(&wb->sbuf))
84 wb->prev_n = 0;
87 /* mark wb->prev_c[] as valid */
88 static void wb_prevok(struct wb *wb)
90 wb->prev_ll = sbuf_len(&wb->sbuf);
93 /* append c to wb->prev_c[] */
94 static void wb_prevput(struct wb *wb, char *c, int ll)
96 if (wb->prev_n == LEN(wb->prev_c))
97 wb->prev_n--;
98 memmove(wb->prev_l + 1, wb->prev_l, wb->prev_n * sizeof(wb->prev_l[0]));
99 memmove(wb->prev_h + 1, wb->prev_h, wb->prev_n * sizeof(wb->prev_h[0]));
100 memmove(wb->prev_c + 1, wb->prev_c, wb->prev_n * sizeof(wb->prev_c[0]));
101 wb->prev_l[0] = ll;
102 wb->prev_h[0] = wb->h;
103 strcpy(wb->prev_c[0], c);
104 wb->prev_n++;
105 wb_prevok(wb);
108 /* strip the last i characters from wb */
109 static void wb_prevpop(struct wb *wb, int i)
111 int n = wb->prev_n - i;
112 sbuf_cut(&wb->sbuf, wb->prev_l[i - 1]);
113 wb->h = wb->prev_h[i - 1];
114 memmove(wb->prev_l, wb->prev_l + i, n * sizeof(wb->prev_l[0]));
115 memmove(wb->prev_h, wb->prev_h + i, n * sizeof(wb->prev_h[0]));
116 memmove(wb->prev_c, wb->prev_c + i, n * sizeof(wb->prev_c[0]));
117 wb->prev_n = n;
118 wb->prev_ll = sbuf_len(&wb->sbuf);
121 /* return the i-th last character inserted via wb_put() */
122 static char *wb_prev(struct wb *wb, int i)
124 wb_prevcheck(wb);
125 return i < wb->prev_n ? wb->prev_c[i] : NULL;
128 void wb_put(struct wb *wb, char *c)
130 struct glyph *g;
131 int ll;
132 if (c[0] == '\n') {
133 wb->part = 0;
134 return;
136 if (c[0] == ' ') {
137 wb_hmov(wb, spacewid(R_F(wb), R_S(wb)));
138 return;
140 if (c[0] == '\t' || c[0] == '\x01' ||
141 (c[0] == c_ni && (c[1] == '\t' || c[1] == '\x01'))) {
142 sbuf_append(&wb->sbuf, c);
143 return;
145 g = dev_glyph(c, R_F(wb));
146 wb_font(wb);
147 wb_prevcheck(wb); /* make sure wb->prev_c[] is valid */
148 ll = sbuf_len(&wb->sbuf); /* sbuf length before inserting c */
149 if (!c[1] || c[0] == c_ec || c[0] == c_ni || utf8one(c)) {
150 if (c[0] == c_ni && c[1] == c_ec)
151 sbuf_printf(&wb->sbuf, "%c%c", c_ec, c_ec);
152 else
153 sbuf_append(&wb->sbuf, c);
154 } else {
155 if (c[1] && !c[2])
156 sbuf_printf(&wb->sbuf, "%c(%s", c_ec, c);
157 else
158 sbuf_printf(&wb->sbuf, "%cC'%s'", c_ec, c);
160 if (strcmp(c_hc, c)) {
161 wb_prevput(wb, c, ll);
162 wb->h += charwid(R_F(wb), R_S(wb), g ? g->wid : SC_DW);
163 wb->ct |= g ? g->type : 0;
164 wb_stsb(wb);
168 /* just like wb_put(), but call cdef_expand() if c is defined */
169 void wb_putexpand(struct wb *wb, char *c)
171 if (cdef_expand(wb, c, R_F(wb)))
172 wb_put(wb, c);
175 /* return zero if c formed a ligature with its previous character */
176 int wb_lig(struct wb *wb, char *c)
178 char lig[GNLEN] = "";
179 char *cs[LIGLEN + 2];
180 int i = -1;
181 int ligpos;
182 cs[0] = c;
183 while (wb_prev(wb, ++i))
184 cs[i + 1] = wb_prev(wb, i);
185 ligpos = font_lig(dev_font(R_F(wb)), cs, i + 1);
186 if (ligpos > 1) {
187 for (i = 0; i < ligpos - 1; i++)
188 strcat(lig, wb_prev(wb, ligpos - i - 2));
189 strcat(lig, c);
190 wb_prevpop(wb, ligpos - 1);
191 wb_put(wb, lig);
192 return 0;
194 return 1;
197 /* return 0 if pairwise kerning was done */
198 int wb_kern(struct wb *wb, char *c)
200 int val;
201 if (!wb_prev(wb, 0))
202 return 1;
203 val = font_kern(dev_font(R_F(wb)), wb_prev(wb, 0), c);
204 if (val)
205 wb_hmov(wb, charwid(R_F(wb), R_S(wb), val));
206 wb_prevok(wb); /* kerning should not prevent ligatures */
207 return !val;
210 int wb_part(struct wb *wb)
212 return wb->part;
215 void wb_setpart(struct wb *wb)
217 wb->part = 1;
220 void wb_drawl(struct wb *wb, int c, int h, int v)
222 wb_font(wb);
223 sbuf_printf(&wb->sbuf, "%cD'%c %du %du'", c_ec, c, h, v);
224 wb->h += h;
225 wb->v += v;
226 wb_stsb(wb);
229 void wb_drawc(struct wb *wb, int c, int r)
231 wb_font(wb);
232 sbuf_printf(&wb->sbuf, "%cD'%c %du'", c_ec, c, r);
233 wb->h += r;
236 void wb_drawe(struct wb *wb, int c, int h, int v)
238 wb_font(wb);
239 sbuf_printf(&wb->sbuf, "%cD'%c %du %du'", c_ec, c, h, v);
240 wb->h += h;
243 void wb_drawa(struct wb *wb, int c, int h1, int v1, int h2, int v2)
245 wb_font(wb);
246 sbuf_printf(&wb->sbuf, "%cD'%c %du %du %du %du'", c_ec, c, h1, v1, h2, v2);
247 wb->h += h1 + h2;
248 wb->v += v1 + v2;
249 wb_stsb(wb);
252 void wb_drawxbeg(struct wb *wb, int c)
254 wb_font(wb);
255 sbuf_printf(&wb->sbuf, "%cD'%c", c_ec, c);
258 void wb_drawxdot(struct wb *wb, int h, int v)
260 sbuf_printf(&wb->sbuf, " %du %du", h, v);
261 wb->h += h;
262 wb->v += v;
263 wb_stsb(wb);
266 void wb_drawxend(struct wb *wb)
268 sbuf_printf(&wb->sbuf, "'");
271 static void wb_reset(struct wb *wb)
273 wb_done(wb);
274 wb_init(wb);
277 static void wb_putc(struct wb *wb, int t, char *s)
279 switch (t) {
280 case 0:
281 case 'C':
282 wb_put(wb, s);
283 break;
284 case 'D':
285 ren_dcmd(wb, s);
286 break;
287 case 'f':
288 wb->r_f = atoi(s);
289 break;
290 case 'h':
291 wb_hmov(wb, atoi(s));
292 break;
293 case 'm':
294 wb->r_m = clr_get(s);
295 break;
296 case 's':
297 wb->r_s = atoi(s);
298 break;
299 case 'v':
300 wb_vmov(wb, atoi(s));
301 break;
302 case 'x':
303 wb_els(wb, atoi(s));
304 break;
305 case 'X':
306 wb_etc(wb, s);
307 break;
311 void wb_cat(struct wb *wb, struct wb *src)
313 char *s = sbuf_buf(&src->sbuf);
314 char d[ILNLEN];
315 int c, part;
316 while ((c = escread(&s, d)) >= 0)
317 wb_putc(wb, c, d);
318 part = src->part;
319 wb->r_s = -1;
320 wb->r_f = -1;
321 wb->r_m = -1;
322 wb_reset(src);
323 src->part = part;
326 int wb_wid(struct wb *wb)
328 return wb->h;
331 int wb_empty(struct wb *wb)
333 return sbuf_empty(&wb->sbuf);
336 /* return 1 if wb ends a sentence (.?!) */
337 int wb_eos(struct wb *wb)
339 int i = 0;
340 while (wb_prev(wb, i) && strchr("'\")]*", wb_prev(wb, i)[0]))
341 i++;
342 return wb_prev(wb, i) && strchr(".?!", wb_prev(wb, i)[0]);
345 void wb_wconf(struct wb *wb, int *ct, int *st, int *sb)
347 *ct = wb->ct;
348 *st = -wb->st;
349 *sb = -wb->sb;
352 /* skip troff requests; return 1 if read c_hc */
353 static int skipreqs(char **s, struct wb *w1)
355 char d[ILNLEN];
356 char *r = *s;
357 int c;
358 wb_reset(w1);
359 while ((c = escread(s, d)) > 0) {
360 wb_putc(w1, c, d);
361 r = *s;
363 if (c < 0 || !strcmp(c_hc, d))
364 return 1;
365 *s = r;
366 return 0;
369 static char *dashpos(char *s, int w, struct wb *w1, int flg)
371 char d[ILNLEN];
372 char *r = NULL;
373 int c;
374 skipreqs(&s, w1);
375 while ((c = escread(&s, d)) >= 0) {
376 wb_putc(w1, c, d);
377 if (wb_wid(w1) > w && (!(flg & HY_ANY) || r))
378 continue;
379 if (!c && (!strcmp("-", d) || (!strcmp("em", d) || !strcmp("hy", d))))
380 r = s;
382 return r;
385 static int wb_dashwid(struct wb *wb)
387 struct glyph *g = dev_glyph("hy", R_F(wb));
388 return charwid(R_F(wb), R_S(wb), g ? g->wid : SC_DW);
391 static char *indicatorpos(char *s, int w, struct wb *w1, int flg)
393 char d[ILNLEN];
394 char *r = NULL;
395 int c;
396 skipreqs(&s, w1);
397 while ((c = escread(&s, d)) >= 0) {
398 wb_putc(w1, c, d);
399 if (wb_wid(w1) + wb_dashwid(w1) > w && (!(flg & HY_ANY) || r))
400 continue;
401 if (!c && !strcmp(c_hc, d))
402 r = s;
404 return r;
407 static char *hyphpos(char *s, int w, struct wb *w1, int flg)
409 char word[ILNLEN]; /* word to pass to hyphenate() */
410 char hyph[ILNLEN]; /* hyphenation points returned from hyphenate() */
411 char *iw[ILNLEN]; /* beginning of i-th char in word */
412 char *is[ILNLEN]; /* beginning of i-th char in s */
413 int fits[ILNLEN]; /* fits[i] is 1, if the first i chars fit w */
414 int n = 0; /* the number of characters in word */
415 char d[ILNLEN];
416 char *prev_s = s;
417 char *r = NULL;
418 char *wp = word, *we = word + sizeof(word);
419 int i, c;
420 skipreqs(&s, w1);
421 while ((c = escread(&s, d)) >= 0 && (c > 0 || strlen(d) + 1 < we - wp)) {
422 fits[n] = wb_wid(w1) + wb_dashwid(w1) <= w;
423 wb_putc(w1, c, d);
424 if (c == 0) {
425 iw[n] = wp;
426 is[n] = prev_s;
427 /* ignore multi-char aliases except for ligatures */
428 if (!utf8one(d) && !font_islig(dev_font(R_F(w1)), d))
429 strcpy(d, ".");
430 strcpy(wp, d);
431 wp = strchr(wp, '\0');
432 n++;
434 prev_s = s;
436 if (n < 3)
437 return NULL;
438 hyphenate(hyph, word, flg);
439 for (i = 1; i < n - 1; i++)
440 if (hyph[iw[i] - word] && (fits[i] || ((flg & HY_ANY) && !r)))
441 r = is[i];
442 return r;
445 static void dohyph(char *s, char *pos, int dash, struct wb *w1, struct wb *w2)
447 char d[ILNLEN];
448 int c = -1;
449 wb_reset(w1);
450 wb_reset(w2);
451 while (s != pos && (c = escread(&s, d)) >= 0)
452 wb_putc(w1, c, d);
453 if (dash)
454 wb_putc(w1, 0, "hy");
455 w2->r_s = w1->r_s;
456 w2->r_f = w1->r_f;
457 w2->r_m = w1->r_m;
458 while ((c = escread(&s, d)) >= 0)
459 wb_putc(w2, c, d);
462 /* hyphenate wb into w1 and w2; return zero on success */
463 int wb_hyph(struct wb *wb, int w, struct wb *w1, struct wb *w2, int flg)
465 char *s = sbuf_buf(&wb->sbuf);
466 char *dp, *hp, *p;
467 if (skipreqs(&s, w1))
468 return 1;
469 dp = dashpos(sbuf_buf(&wb->sbuf), w, w1, flg);
470 hp = indicatorpos(sbuf_buf(&wb->sbuf), w, w1, flg);
471 if (hp && dp)
472 p = flg & HY_ANY ? MIN(dp, hp) : MAX(dp, hp);
473 else
474 p = dp ? dp : hp;
475 if (!p && flg & HY_MASK)
476 p = hyphpos(sbuf_buf(&wb->sbuf), w, w1, flg);
477 if (p)
478 dohyph(sbuf_buf(&wb->sbuf), p, p != dp, w1, w2);
479 return !p;