font: specify font script with .ffsc request
[neatroff.git] / wb.c
blob1ea95b4dccc3ac2c599cc6c140546a38405df74c
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 = c_hymark(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 /* return nonzero if it cannot be hyphenated */
163 static int wb_hyph(char src[][GNLEN], int src_n, char *src_hyph, int flg)
165 char word[WORDLEN * GNLEN]; /* word to pass to hyphenate() */
166 char hyph[WORDLEN * GNLEN]; /* hyphenation points of word */
167 int smap[WORDLEN]; /* the mapping from src[] to word[] */
168 char *s, *d;
169 int i;
170 d = word;
171 *d = '\0';
172 for (i = 0; i < src_n; i++) {
173 s = src[i];
174 smap[i] = d - word;
175 if (c_hystop(s))
176 return 1;
177 if (c_hymark(s))
178 continue;
179 d += hy_cput(d, s);
181 memset(hyph, 0, (d - word) * sizeof(hyph[0]));
182 hyphenate(hyph, word, flg);
183 for (i = 0; i < src_n; i++)
184 src_hyph[i] = hyph[smap[i]];
185 return 0;
188 static int wb_collect(struct wb *wb, int val)
190 int old = wb->sub_collect;
191 wb->sub_collect = val;
192 return old;
195 /* output the collected characters; only for those present in wb->f font */
196 static void wb_flushsub(struct wb *wb)
198 struct font *fn;
199 struct glyph *gsrc[WORDLEN];
200 struct glyph *gdst[WORDLEN];
201 int x[WORDLEN], y[WORDLEN], xadv[WORDLEN], yadv[WORDLEN];
202 int dmap[WORDLEN];
203 char src_hyph[WORDLEN];
204 int dst_n, i;
205 int sidx = 0;
206 if (!wb->sub_n || !wb->sub_collect)
207 return;
208 wb->sub_collect = 0;
209 fn = dev_font(wb->f);
210 if (!n_hy || wb_hyph(wb->sub_c, wb->sub_n, src_hyph, n_hy))
211 memset(src_hyph, 0, sizeof(src_hyph));
212 /* call font_layout() for collected glyphs; skip hyphenation marks */
213 while (sidx < wb->sub_n) {
214 int beg = sidx;
215 for (; sidx < wb->sub_n && !c_hymark(wb->sub_c[sidx]); sidx++)
216 gsrc[sidx - beg] = font_find(fn, wb->sub_c[sidx]);
217 dst_n = font_layout(fn, gsrc, sidx - beg, wb->s,
218 gdst, dmap, x, y, xadv, yadv, n_lg, n_kn);
219 for (i = 0; i < dst_n; i++) {
220 if (x[i])
221 wb_hmov(wb, font_wid(fn, wb->s, x[i]));
222 if (y[i])
223 wb_vmov(wb, font_wid(fn, wb->s, y[i]));
224 if (src_hyph[beg + dmap[i]])
225 wb_putbuf(wb, c_hc);
226 if (gdst[i] == gsrc[dmap[i]])
227 wb_putbuf(wb, wb->sub_c[beg + dmap[i]]);
228 else
229 wb_putbuf(wb, gdst[i]->name);
230 if (x[i] || xadv[i])
231 wb_hmov(wb, font_wid(fn, wb->s, xadv[i] - x[i]));
232 if (y[i] || yadv[i])
233 wb_vmov(wb, font_wid(fn, wb->s, yadv[i] - y[i]));
235 for (; sidx < wb->sub_n && c_hymark(wb->sub_c[sidx]); sidx++)
236 wb_putbuf(wb, wb->sub_c[sidx]);
238 wb->sub_n = 0;
239 wb->icleft = 0;
240 wb->sub_collect = 1;
243 void wb_put(struct wb *wb, char *c)
245 if (c[0] == '\n') {
246 wb->part = 0;
247 return;
249 if (c[0] == ' ') {
250 wb_flushsub(wb);
251 wb_hmov(wb, font_swid(dev_font(R_F(wb)), R_S(wb), n_ss));
252 return;
254 if (!strcmp(c_nb, c)) {
255 wb_flushsub(wb);
256 sbuf_append(&wb->sbuf, c);
257 wb->h += font_swid(dev_font(R_F(wb)), R_S(wb), n_ss);
258 return;
260 if (wb_pendingfont(wb) || wb->sub_n == LEN(wb->sub_c))
261 wb_flush(wb);
262 if (wb->sub_collect) {
263 if (font_find(dev_font(wb->f), c) || c_hymark(c))
264 strcpy(wb->sub_c[wb->sub_n++], c);
265 else
266 wb_putraw(wb, c);
267 } else {
268 wb_putbuf(wb, c);
272 /* just like wb_put() but disable subword collection */
273 void wb_putraw(struct wb *wb, char *c)
275 int collect;
276 wb_flushsub(wb);
277 collect = wb_collect(wb, 0);
278 wb_put(wb, c);
279 wb_collect(wb, collect);
282 /* just like wb_put(), but call cdef_expand() if c is defined */
283 void wb_putexpand(struct wb *wb, char *c)
285 if (cdef_expand(wb, c, R_F(wb)))
286 wb_put(wb, c);
289 int wb_part(struct wb *wb)
291 return wb->part;
294 void wb_setpart(struct wb *wb)
296 wb->part = 1;
299 int wb_cost(struct wb *wb)
301 return wb->cost;
304 void wb_setcost(struct wb *wb, int cost)
306 wb->cost = cost;
309 void wb_drawl(struct wb *wb, int c, int h, int v)
311 wb_flush(wb);
312 sbuf_printf(&wb->sbuf, "%cD'%c %du %du'", c_ec, c, h, v);
313 wb->h += h;
314 wb->v += v;
315 wb_stsb(wb);
318 void wb_drawc(struct wb *wb, int c, int r)
320 wb_flush(wb);
321 sbuf_printf(&wb->sbuf, "%cD'%c %du'", c_ec, c, r);
322 wb->h += r;
325 void wb_drawe(struct wb *wb, int c, int h, int v)
327 wb_flush(wb);
328 sbuf_printf(&wb->sbuf, "%cD'%c %du %du'", c_ec, c, h, v);
329 wb->h += h;
332 void wb_drawa(struct wb *wb, int c, int h1, int v1, int h2, int v2)
334 wb_flush(wb);
335 sbuf_printf(&wb->sbuf, "%cD'%c %du %du %du %du'",
336 c_ec, c, h1, v1, h2, v2);
337 wb->h += h1 + h2;
338 wb->v += v1 + v2;
339 wb_stsb(wb);
342 void wb_drawxbeg(struct wb *wb, int c)
344 wb_flush(wb);
345 sbuf_printf(&wb->sbuf, "%cD'%c", c_ec, c);
348 void wb_drawxdot(struct wb *wb, int h, int v)
350 sbuf_printf(&wb->sbuf, " %du %du", h, v);
351 wb->h += h;
352 wb->v += v;
353 wb_stsb(wb);
356 void wb_drawxcmd(struct wb *wb, char *cmd)
358 sbuf_printf(&wb->sbuf, " %s", cmd);
361 void wb_drawxend(struct wb *wb)
363 sbuf_printf(&wb->sbuf, "'");
366 void wb_reset(struct wb *wb)
368 wb_done(wb);
369 wb_init(wb);
372 char *wb_buf(struct wb *wb)
374 wb_flushsub(wb);
375 return sbuf_buf(&wb->sbuf);
378 static void wb_putc(struct wb *wb, int t, char *s)
380 if (t && t != 'C')
381 wb_flushsub(wb);
382 switch (t) {
383 case 0:
384 case 'C':
385 wb_put(wb, s);
386 break;
387 case 'D':
388 ren_dcmd(wb, s);
389 break;
390 case 'f':
391 wb->r_f = atoi(s);
392 break;
393 case 'h':
394 wb_hmov(wb, atoi(s));
395 break;
396 case 'm':
397 wb->r_m = clr_get(s);
398 break;
399 case 's':
400 wb->r_s = atoi(s);
401 break;
402 case 'v':
403 wb_vmov(wb, atoi(s));
404 break;
405 case 'x':
406 wb_els(wb, atoi(s));
407 break;
408 case 'X':
409 wb_etc(wb, s);
410 break;
414 void wb_cat(struct wb *wb, struct wb *src)
416 char *s, *d;
417 int c, part;
418 int collect;
419 wb_flushsub(src);
420 wb_flushsub(wb);
421 collect = wb_collect(wb, 0);
422 s = sbuf_buf(&src->sbuf);
423 while ((c = escread(&s, &d)) >= 0)
424 wb_putc(wb, c, d);
425 part = src->part;
426 wb->r_s = -1;
427 wb->r_f = -1;
428 wb->r_m = -1;
429 wb_reset(src);
430 src->part = part;
431 wb_collect(wb, collect);
434 int wb_wid(struct wb *wb)
436 wb_flushsub(wb);
437 return wb->h;
440 int wb_hpos(struct wb *wb)
442 wb_flushsub(wb);
443 return wb->h;
446 int wb_vpos(struct wb *wb)
448 wb_flushsub(wb);
449 return wb->v;
452 int wb_empty(struct wb *wb)
454 return !wb->sub_n && sbuf_empty(&wb->sbuf);
457 /* return 1 if wb ends a sentence (.?!) */
458 int wb_eos(struct wb *wb)
460 int i = wb->sub_n - 1;
461 while (i > 0 && c_eostran(wb->sub_c[i]))
462 i--;
463 return i >= 0 && c_eossent(wb->sub_c[i]);
466 void wb_wconf(struct wb *wb, int *ct, int *st, int *sb,
467 int *llx, int *lly, int *urx, int *ury)
469 wb_flushsub(wb);
470 *ct = wb->ct;
471 *st = -wb->st;
472 *sb = -wb->sb;
473 *llx = wb->llx < BBMAX ? wb->llx : 0;
474 *lly = wb->lly < BBMAX ? -wb->lly : 0;
475 *urx = wb->urx > BBMIN ? wb->urx : 0;
476 *ury = wb->ury > BBMIN ? -wb->ury : 0;
479 static struct glyph *wb_prevglyph(struct wb *wb)
481 return wb->sub_n ? dev_glyph(wb->sub_c[wb->sub_n - 1], wb->f) : NULL;
484 void wb_italiccorrection(struct wb *wb)
486 struct glyph *g = wb_prevglyph(wb);
487 if (g && glyph_ic(g))
488 wb_hmov(wb, font_wid(g->font, wb->s, glyph_ic(g)));
491 void wb_italiccorrectionleft(struct wb *wb)
493 wb_flushsub(wb);
494 wb->icleft = 1;
497 void wb_fnszget(struct wb *wb, int *fn, int *sz, int *m)
499 wb_flushsub(wb);
500 *fn = wb->r_f;
501 *sz = wb->r_s;
502 *m = wb->r_m;
505 void wb_fnszset(struct wb *wb, int fn, int sz, int m)
507 wb->r_f = fn;
508 wb->r_s = sz;
509 wb->r_m = m;
512 void wb_catstr(struct wb *wb, char *s, char *end)
514 int collect, c;
515 char *d;
516 wb_flushsub(wb);
517 collect = wb_collect(wb, 0);
518 while (s < end && (c = escread(&s, &d)) >= 0)
519 wb_putc(wb, c, d);
520 wb_collect(wb, collect);
523 /* return the size of \(hy if appended to wb */
524 int wb_hywid(struct wb *wb)
526 struct glyph *g = dev_glyph("hy", wb->f);
527 return g ? font_gwid(g->font, dev_font(wb->f), wb->s, g->wid) : 0;