hyph: replace current escape character to backslash
[neatroff.git] / wb.c
blobf93078e076dbd64c52f42f244636ee384a10f2c9
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 (wb_pendingfont(wb) || wb->sub_n == LEN(wb->sub_c))
255 wb_flush(wb);
256 if (wb->sub_collect) {
257 if (font_find(dev_font(wb->f), c) || c_hymark(c))
258 strcpy(wb->sub_c[wb->sub_n++], c);
259 else
260 wb_putraw(wb, c);
261 } else {
262 wb_putbuf(wb, c);
266 /* just like wb_put() but disable subword collection */
267 void wb_putraw(struct wb *wb, char *c)
269 int collect;
270 wb_flushsub(wb);
271 collect = wb_collect(wb, 0);
272 wb_put(wb, c);
273 wb_collect(wb, collect);
276 /* just like wb_put(), but call cdef_expand() if c is defined */
277 void wb_putexpand(struct wb *wb, char *c)
279 if (cdef_expand(wb, c, R_F(wb)))
280 wb_put(wb, c);
283 int wb_part(struct wb *wb)
285 return wb->part;
288 void wb_setpart(struct wb *wb)
290 wb->part = 1;
293 void wb_drawl(struct wb *wb, int c, int h, int v)
295 wb_flush(wb);
296 sbuf_printf(&wb->sbuf, "%cD'%c %du %du'", c_ec, c, h, v);
297 wb->h += h;
298 wb->v += v;
299 wb_stsb(wb);
302 void wb_drawc(struct wb *wb, int c, int r)
304 wb_flush(wb);
305 sbuf_printf(&wb->sbuf, "%cD'%c %du'", c_ec, c, r);
306 wb->h += r;
309 void wb_drawe(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;
316 void wb_drawa(struct wb *wb, int c, int h1, int v1, int h2, int v2)
318 wb_flush(wb);
319 sbuf_printf(&wb->sbuf, "%cD'%c %du %du %du %du'",
320 c_ec, c, h1, v1, h2, v2);
321 wb->h += h1 + h2;
322 wb->v += v1 + v2;
323 wb_stsb(wb);
326 void wb_drawxbeg(struct wb *wb, int c)
328 wb_flush(wb);
329 sbuf_printf(&wb->sbuf, "%cD'%c", c_ec, c);
332 void wb_drawxdot(struct wb *wb, int h, int v)
334 sbuf_printf(&wb->sbuf, " %du %du", h, v);
335 wb->h += h;
336 wb->v += v;
337 wb_stsb(wb);
340 void wb_drawxend(struct wb *wb)
342 sbuf_printf(&wb->sbuf, "'");
345 void wb_reset(struct wb *wb)
347 wb_done(wb);
348 wb_init(wb);
351 char *wb_buf(struct wb *wb)
353 wb_flushsub(wb);
354 return sbuf_buf(&wb->sbuf);
357 static void wb_putc(struct wb *wb, int t, char *s)
359 if (t && t != 'C')
360 wb_flushsub(wb);
361 switch (t) {
362 case 0:
363 case 'C':
364 wb_put(wb, s);
365 break;
366 case 'D':
367 ren_dcmd(wb, s);
368 break;
369 case 'f':
370 wb->r_f = atoi(s);
371 break;
372 case 'h':
373 wb_hmov(wb, atoi(s));
374 break;
375 case 'm':
376 wb->r_m = clr_get(s);
377 break;
378 case 's':
379 wb->r_s = atoi(s);
380 break;
381 case 'v':
382 wb_vmov(wb, atoi(s));
383 break;
384 case 'x':
385 wb_els(wb, atoi(s));
386 break;
387 case 'X':
388 wb_etc(wb, s);
389 break;
393 void wb_cat(struct wb *wb, struct wb *src)
395 char *s;
396 char d[ILNLEN];
397 int c, part;
398 int collect;
399 wb_flushsub(src);
400 wb_flushsub(wb);
401 collect = wb_collect(wb, 0);
402 s = sbuf_buf(&src->sbuf);
403 while ((c = escread(&s, d)) >= 0)
404 wb_putc(wb, c, d);
405 part = src->part;
406 wb->r_s = -1;
407 wb->r_f = -1;
408 wb->r_m = -1;
409 wb_reset(src);
410 src->part = part;
411 wb_collect(wb, collect);
414 int wb_wid(struct wb *wb)
416 wb_flushsub(wb);
417 return wb->h;
420 int wb_hpos(struct wb *wb)
422 wb_flushsub(wb);
423 return wb->h;
426 int wb_vpos(struct wb *wb)
428 wb_flushsub(wb);
429 return wb->v;
432 int wb_empty(struct wb *wb)
434 return !wb->sub_n && sbuf_empty(&wb->sbuf);
437 /* return 1 if wb ends a sentence (.?!) */
438 int wb_eos(struct wb *wb)
440 int i = wb->sub_n - 1;
441 while (i > 0 && c_eostran(wb->sub_c[i]))
442 i--;
443 return i >= 0 && c_eossent(wb->sub_c[i]);
446 void wb_wconf(struct wb *wb, int *ct, int *st, int *sb,
447 int *llx, int *lly, int *urx, int *ury)
449 wb_flushsub(wb);
450 *ct = wb->ct;
451 *st = -wb->st;
452 *sb = -wb->sb;
453 *llx = wb->llx < BBMAX ? wb->llx : 0;
454 *lly = wb->lly < BBMAX ? -wb->lly : 0;
455 *urx = wb->urx > BBMIN ? wb->urx : 0;
456 *ury = wb->ury > BBMIN ? -wb->ury : 0;
459 static struct glyph *wb_prevglyph(struct wb *wb)
461 return wb->sub_n ? dev_glyph(wb->sub_c[wb->sub_n - 1], wb->f) : NULL;
464 void wb_italiccorrection(struct wb *wb)
466 struct glyph *g = wb_prevglyph(wb);
467 if (g && glyph_ic(g))
468 wb_hmov(wb, font_wid(g->font, wb->s, glyph_ic(g)));
471 void wb_italiccorrectionleft(struct wb *wb)
473 wb_flushsub(wb);
474 wb->icleft = 1;
477 void wb_fnszget(struct wb *wb, int *fn, int *sz, int *m)
479 wb_flushsub(wb);
480 *fn = wb->r_f;
481 *sz = wb->r_s;
482 *m = wb->r_m;
485 void wb_fnszset(struct wb *wb, int fn, int sz, int m)
487 wb->r_f = fn;
488 wb->r_s = sz;
489 wb->r_m = m;
492 void wb_catstr(struct wb *wb, char *s, char *end)
494 char d[ILNLEN];
495 int collect, c;
496 wb_flushsub(wb);
497 collect = wb_collect(wb, 0);
498 while (s < end && (c = escread(&s, d)) >= 0)
499 wb_putc(wb, c, d);
500 wb_collect(wb, collect);
503 /* return the size of \(hy if appended to wb */
504 int wb_hywid(struct wb *wb)
506 struct glyph *g = dev_glyph("hy", wb->f);
507 return g ? font_gwid(g->font, dev_font(wb->f), wb->s, g->wid) : 0;