Annotation of src/usr.bin/mandoc/term.c, Revision 1.148
1.148 ! schwarze 1: /* $OpenBSD: term.c,v 1.147 2022/08/15 10:21:01 schwarze Exp $ */
1.1 kristaps 2: /*
1.145 schwarze 3: * Copyright (c) 2010-2022 Ingo Schwarze <schwarze@openbsd.org>
1.59 schwarze 4: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
1.2 schwarze 7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 9: *
1.106 schwarze 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.2 schwarze 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.106 schwarze 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.2 schwarze 13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 17: */
1.20 schwarze 18: #include <sys/types.h>
19:
1.1 kristaps 20: #include <assert.h>
1.20 schwarze 21: #include <ctype.h>
1.138 schwarze 22: #include <stdint.h>
1.1 kristaps 23: #include <stdio.h>
24: #include <stdlib.h>
25: #include <string.h>
26:
1.34 schwarze 27: #include "mandoc.h"
1.79 schwarze 28: #include "mandoc_aux.h"
1.16 schwarze 29: #include "out.h"
1.1 kristaps 30: #include "term.h"
1.16 schwarze 31: #include "main.h"
1.1 kristaps 32:
1.64 schwarze 33: static size_t cond_width(const struct termp *, int, int *);
1.126 schwarze 34: static void adjbuf(struct termp_col *, size_t);
1.59 schwarze 35: static void bufferc(struct termp *, char);
36: static void encode(struct termp *, const char *, size_t);
37: static void encode1(struct termp *, int);
1.124 schwarze 38: static void endline(struct termp *);
1.142 schwarze 39: static void term_field(struct termp *, size_t, size_t);
1.138 schwarze 40: static void term_fill(struct termp *, size_t *, size_t *,
41: size_t);
1.1 kristaps 42:
1.83 schwarze 43:
1.37 schwarze 44: void
1.129 schwarze 45: term_setcol(struct termp *p, size_t maxtcol)
46: {
47: if (maxtcol > p->maxtcol) {
48: p->tcols = mandoc_recallocarray(p->tcols,
49: p->maxtcol, maxtcol, sizeof(*p->tcols));
50: p->maxtcol = maxtcol;
51: }
52: p->lasttcol = maxtcol - 1;
53: p->tcol = p->tcols;
54: }
55:
56: void
1.37 schwarze 57: term_free(struct termp *p)
1.1 kristaps 58: {
1.144 schwarze 59: term_tab_free();
1.126 schwarze 60: for (p->tcol = p->tcols; p->tcol < p->tcols + p->maxtcol; p->tcol++)
61: free(p->tcol->buf);
62: free(p->tcols);
1.98 schwarze 63: free(p->fontq);
1.37 schwarze 64: free(p);
1.1 kristaps 65: }
66:
1.13 schwarze 67: void
1.83 schwarze 68: term_begin(struct termp *p, term_margin head,
1.106 schwarze 69: term_margin foot, const struct roff_meta *arg)
1.1 kristaps 70: {
71:
1.37 schwarze 72: p->headf = head;
73: p->footf = foot;
74: p->argf = arg;
75: (*p->begin)(p);
1.1 kristaps 76: }
77:
1.37 schwarze 78: void
79: term_end(struct termp *p)
1.1 kristaps 80: {
81:
1.37 schwarze 82: (*p->end)(p);
1.1 kristaps 83: }
84:
85: /*
1.82 schwarze 86: * Flush a chunk of text. By default, break the output line each time
87: * the right margin is reached, and continue output on the next line
88: * at the same offset as the chunk itself. By default, also break the
1.138 schwarze 89: * output line at the end of the chunk. There are many flags modifying
90: * this behaviour, see the comments in the body of the function.
1.1 kristaps 91: */
92: void
93: term_flushln(struct termp *p)
94: {
1.138 schwarze 95: size_t vbl; /* Number of blanks to prepend to the output. */
96: size_t vbr; /* Actual visual position of the end of field. */
97: size_t vfield; /* Desired visual field width. */
98: size_t vtarget; /* Desired visual position of the right margin. */
99: size_t ic; /* Character position in the input buffer. */
100: size_t nbr; /* Number of characters to print in this field. */
101:
102: /*
103: * Normally, start writing at the left margin, but with the
104: * NOPAD flag, start writing at the current position instead.
105: */
1.1 kristaps 106:
1.126 schwarze 107: vbl = (p->flags & TERMP_NOPAD) || p->tcol->offset < p->viscol ?
108: 0 : p->tcol->offset - p->viscol;
1.123 schwarze 109: if (p->minbl && vbl < p->minbl)
110: vbl = p->minbl;
1.19 schwarze 111:
1.134 florian 112: if ((p->flags & TERMP_MULTICOL) == 0)
1.127 schwarze 113: p->tcol->col = 0;
1.138 schwarze 114:
115: /* Loop over output lines. */
116:
117: for (;;) {
118: vfield = p->tcol->rmargin > p->viscol + vbl ?
119: p->tcol->rmargin - p->viscol - vbl : 0;
1.127 schwarze 120:
1.22 schwarze 121: /*
1.138 schwarze 122: * Normally, break the line at the the right margin
123: * of the field, but with the NOBREAK flag, only
124: * break it at the max right margin of the screen,
125: * and with the BRNEVER flag, never break it at all.
1.30 schwarze 126: */
1.127 schwarze 127:
1.142 schwarze 128: vtarget = (p->flags & TERMP_NOBREAK) == 0 ? vfield :
1.138 schwarze 129: p->maxrmargin > p->viscol + vbl ?
130: p->maxrmargin - p->viscol - vbl : 0;
131:
132: /*
133: * Figure out how much text will fit in the field.
134: * If there is whitespace only, print nothing.
135: */
136:
1.142 schwarze 137: term_fill(p, &nbr, &vbr,
138: p->flags & TERMP_BRNEVER ? SIZE_MAX : vtarget);
1.138 schwarze 139: if (nbr == 0)
140: break;
1.139 schwarze 141:
142: /*
143: * With the CENTER or RIGHT flag, increase the indentation
144: * to center the text between the left and right margins
145: * or to adjust it to the right margin, respectively.
146: */
147:
148: if (vbr < vtarget) {
149: if (p->flags & TERMP_CENTER)
150: vbl += (vtarget - vbr) / 2;
151: else if (p->flags & TERMP_RIGHT)
152: vbl += vtarget - vbr;
153: }
154:
155: /* Finally, print the field content. */
1.138 schwarze 156:
1.142 schwarze 157: term_field(p, vbl, nbr);
1.148 ! schwarze 158: p->tcol->taboff += vbr + (*p->width)(p, ' ');
1.22 schwarze 159:
1.1 kristaps 160: /*
1.138 schwarze 161: * If there is no text left in the field, exit the loop.
162: * If the BRTRSP flag is set, consider trailing
163: * whitespace significant when deciding whether
164: * the field fits or not.
1.1 kristaps 165: */
166:
1.138 schwarze 167: for (ic = p->tcol->col; ic < p->tcol->lastcol; ic++) {
168: switch (p->tcol->buf[ic]) {
169: case '\t':
170: if (p->flags & TERMP_BRTRSP)
171: vbr = term_tab_next(vbr);
172: continue;
173: case ' ':
174: if (p->flags & TERMP_BRTRSP)
175: vbr += (*p->width)(p, ' ');
1.130 schwarze 176: continue;
1.138 schwarze 177: case '\n':
178: case ASCII_BREAK:
179: continue;
180: default:
181: break;
1.130 schwarze 182: }
1.138 schwarze 183: break;
184: }
185: if (ic == p->tcol->lastcol)
186: break;
1.42 schwarze 187:
1.138 schwarze 188: /*
189: * At the location of an automtic line break, input
190: * space characters are consumed by the line break.
191: */
1.42 schwarze 192:
1.138 schwarze 193: while (p->tcol->col < p->tcol->lastcol &&
194: p->tcol->buf[p->tcol->col] == ' ')
195: p->tcol->col++;
1.42 schwarze 196:
1.138 schwarze 197: /*
198: * In multi-column mode, leave the rest of the text
199: * in the buffer to be handled by a subsequent
200: * invocation, such that the other columns of the
201: * table can be handled first.
202: * In single-column mode, simply break the line.
203: */
204:
205: if (p->flags & TERMP_MULTICOL)
206: return;
1.78 schwarze 207:
1.138 schwarze 208: endline(p);
209: p->viscol = 0;
1.1 kristaps 210:
211: /*
1.138 schwarze 212: * Normally, start the next line at the same indentation
213: * as this one, but with the BRIND flag, start it at the
214: * right margin instead. This is used together with
215: * NOBREAK for the tags in various kinds of tagged lists.
1.5 schwarze 216: */
1.127 schwarze 217:
1.138 schwarze 218: vbl = p->flags & TERMP_BRIND ?
219: p->tcol->rmargin : p->tcol->offset;
220: }
1.127 schwarze 221:
1.138 schwarze 222: /* Reset output state in preparation for the next field. */
1.66 schwarze 223:
1.138 schwarze 224: p->col = p->tcol->col = p->tcol->lastcol = 0;
225: p->minbl = p->trailspace;
226: p->flags &= ~(TERMP_BACKAFTER | TERMP_BACKBEFORE | TERMP_NOPAD);
1.120 schwarze 227:
1.138 schwarze 228: if (p->flags & TERMP_MULTICOL)
229: return;
1.120 schwarze 230:
1.138 schwarze 231: /*
232: * The HANG flag means that the next field
233: * always follows on the same line.
234: * The NOBREAK flag means that the next field
235: * follows on the same line unless the field was overrun.
236: * Normally, break the line at the end of each field.
237: */
1.66 schwarze 238:
1.138 schwarze 239: if ((p->flags & TERMP_HANG) == 0 &&
240: ((p->flags & TERMP_NOBREAK) == 0 ||
241: vbr + term_len(p, p->trailspace) > vfield))
242: endline(p);
243: }
1.30 schwarze 244:
1.138 schwarze 245: /*
246: * Store the number of input characters to print in this field in *nbr
247: * and their total visual width to print in *vbr.
248: * If there is only whitespace in the field, both remain zero.
249: * The desired visual width of the field is provided by vtarget.
250: * If the first word is longer, the field will be overrun.
251: */
252: static void
253: term_fill(struct termp *p, size_t *nbr, size_t *vbr, size_t vtarget)
254: {
255: size_t ic; /* Character position in the input buffer. */
256: size_t vis; /* Visual position of the current character. */
257: size_t vn; /* Visual position of the next character. */
258: int breakline; /* Break at the end of this word. */
259: int graph; /* Last character was non-blank. */
260:
261: *nbr = *vbr = vis = 0;
262: breakline = graph = 0;
263: for (ic = p->tcol->col; ic < p->tcol->lastcol; ic++) {
264: switch (p->tcol->buf[ic]) {
265: case '\b': /* Escape \o (overstrike) or backspace markup. */
266: assert(ic > 0);
267: vis -= (*p->width)(p, p->tcol->buf[ic - 1]);
268: continue;
1.127 schwarze 269:
1.138 schwarze 270: case ' ':
271: case ASCII_BREAK: /* Escape \: (breakpoint). */
1.147 schwarze 272: vn = vis;
273: if (p->tcol->buf[ic] == ' ')
274: vn += (*p->width)(p, ' ');
1.138 schwarze 275: /* Can break at the end of a word. */
276: if (breakline || vn > vtarget)
277: break;
278: if (graph) {
279: *nbr = ic;
280: *vbr = vis;
281: graph = 0;
1.33 schwarze 282: }
1.138 schwarze 283: vis = vn;
284: continue;
285:
286: case '\n': /* Escape \p (break at the end of the word). */
287: breakline = 1;
288: continue;
1.33 schwarze 289:
1.138 schwarze 290: case ASCII_HYPH: /* Breakable hyphen. */
291: graph = 1;
1.33 schwarze 292: /*
1.138 schwarze 293: * We are about to decide whether to break the
294: * line or not, so we no longer need this hyphen
295: * to be marked as breakable. Put back a real
296: * hyphen such that we get the correct width.
1.33 schwarze 297: */
1.138 schwarze 298: p->tcol->buf[ic] = '-';
299: vis += (*p->width)(p, '-');
300: if (vis > vtarget) {
301: ic++;
302: break;
1.61 schwarze 303: }
1.138 schwarze 304: *nbr = ic + 1;
305: *vbr = vis;
306: continue;
1.61 schwarze 307:
1.147 schwarze 308: default:
309: switch (p->tcol->buf[ic]) {
310: case '\t':
1.148 ! schwarze 311: vis += p->tcol->taboff;
1.147 schwarze 312: vis = term_tab_next(vis);
1.148 ! schwarze 313: vis -= p->tcol->taboff;
1.147 schwarze 314: break;
315: case ASCII_NBRSP: /* Non-breakable space. */
316: p->tcol->buf[ic] = ' ';
317: /* FALLTHROUGH */
318: default: /* Printable character. */
319: vis += (*p->width)(p, p->tcol->buf[ic]);
320: break;
321: }
1.138 schwarze 322: graph = 1;
323: if (vis > vtarget && *nbr > 0)
324: return;
325: continue;
1.1 kristaps 326: }
1.138 schwarze 327: break;
328: }
1.130 schwarze 329:
1.138 schwarze 330: /*
331: * If the last word extends to the end of the field without any
332: * trailing whitespace, the loop could not check yet whether it
333: * can remain on this line. So do the check now.
334: */
1.130 schwarze 335:
1.138 schwarze 336: if (graph && (vis <= vtarget || *nbr == 0)) {
337: *nbr = ic;
338: *vbr = vis;
339: }
340: }
1.130 schwarze 341:
1.138 schwarze 342: /*
343: * Print the contents of one field
344: * with an indentation of vbl visual columns,
1.142 schwarze 345: * and an input string length of nbr characters.
1.138 schwarze 346: */
347: static void
1.142 schwarze 348: term_field(struct termp *p, size_t vbl, size_t nbr)
1.138 schwarze 349: {
350: size_t ic; /* Character position in the input buffer. */
351: size_t vis; /* Visual position of the current character. */
1.148 ! schwarze 352: size_t vt; /* Visual position including tab offset. */
1.138 schwarze 353: size_t dv; /* Visual width of the current character. */
1.130 schwarze 354:
1.138 schwarze 355: vis = 0;
356: for (ic = p->tcol->col; ic < nbr; ic++) {
1.130 schwarze 357:
1.138 schwarze 358: /*
359: * To avoid the printing of trailing whitespace,
360: * do not print whitespace right away, only count it.
361: */
1.130 schwarze 362:
1.138 schwarze 363: switch (p->tcol->buf[ic]) {
364: case '\n':
365: case ASCII_BREAK:
366: continue;
367: case '\t':
368: case ' ':
369: case ASCII_NBRSP:
1.148 ! schwarze 370: if (p->tcol->buf[ic] == '\t') {
! 371: vt = p->tcol->taboff + vis;
! 372: dv = term_tab_next(vt) - vt;
! 373: } else
! 374: dv = (*p->width)(p, ' ');
1.140 schwarze 375: vbl += dv;
376: vis += dv;
1.138 schwarze 377: continue;
378: default:
379: break;
380: }
1.48 schwarze 381:
1.138 schwarze 382: /*
383: * We found a non-blank character to print,
384: * so write preceding white space now.
385: */
1.127 schwarze 386:
1.138 schwarze 387: if (vbl > 0) {
388: (*p->advance)(p, vbl);
389: p->viscol += vbl;
390: vbl = 0;
391: }
1.18 schwarze 392:
1.138 schwarze 393: /* Print the character and adjust the visual position. */
1.1 kristaps 394:
1.138 schwarze 395: (*p->letter)(p, p->tcol->buf[ic]);
396: if (p->tcol->buf[ic] == '\b') {
397: dv = (*p->width)(p, p->tcol->buf[ic - 1]);
398: p->viscol -= dv;
399: vis -= dv;
400: } else {
401: dv = (*p->width)(p, p->tcol->buf[ic]);
402: p->viscol += dv;
403: vis += dv;
404: }
405: }
406: p->tcol->col = nbr;
1.124 schwarze 407: }
408:
409: static void
410: endline(struct termp *p)
411: {
412: if ((p->flags & (TERMP_NEWMC | TERMP_ENDMC)) == TERMP_ENDMC) {
413: p->mc = NULL;
414: p->flags &= ~TERMP_ENDMC;
415: }
416: if (p->mc != NULL) {
417: if (p->viscol && p->maxrmargin >= p->viscol)
418: (*p->advance)(p, p->maxrmargin - p->viscol + 1);
419: p->flags |= TERMP_NOBUF | TERMP_NOSPACE;
420: term_word(p, p->mc);
421: p->flags &= ~(TERMP_NOBUF | TERMP_NEWMC);
422: }
423: p->viscol = 0;
424: p->minbl = 0;
425: (*p->endline)(p);
1.1 kristaps 426: }
427:
1.83 schwarze 428: /*
1.1 kristaps 429: * A newline only breaks an existing line; it won't assert vertical
430: * space. All data in the output buffer is flushed prior to the newline
431: * assertion.
432: */
433: void
434: term_newln(struct termp *p)
435: {
1.148 ! schwarze 436: p->tcol->taboff = 0;
1.1 kristaps 437: p->flags |= TERMP_NOSPACE;
1.129 schwarze 438: if (p->tcol->lastcol || p->viscol)
1.61 schwarze 439: term_flushln(p);
1.1 kristaps 440: }
441:
442: /*
443: * Asserts a vertical space (a full, empty line-break between lines).
444: * Note that if used twice, this will cause two blank spaces and so on.
445: * All data in the output buffer is flushed prior to the newline
446: * assertion.
447: */
448: void
449: term_vspace(struct termp *p)
450: {
451:
452: term_newln(p);
1.29 schwarze 453: p->viscol = 0;
1.124 schwarze 454: p->minbl = 0;
1.63 schwarze 455: if (0 < p->skipvsp)
456: p->skipvsp--;
457: else
458: (*p->endline)(p);
1.1 kristaps 459: }
460:
1.98 schwarze 461: /* Swap current and previous font; for \fP and .ft P */
1.20 schwarze 462: void
463: term_fontlast(struct termp *p)
464: {
465: enum termfont f;
1.11 schwarze 466:
1.20 schwarze 467: f = p->fontl;
468: p->fontl = p->fontq[p->fonti];
469: p->fontq[p->fonti] = f;
470: }
471:
1.98 schwarze 472: /* Set font, save current, discard previous; for \f, .ft, .B etc. */
1.20 schwarze 473: void
474: term_fontrepl(struct termp *p, enum termfont f)
475: {
476:
477: p->fontl = p->fontq[p->fonti];
478: p->fontq[p->fonti] = f;
1.1 kristaps 479: }
480:
1.98 schwarze 481: /* Set font, save previous. */
1.20 schwarze 482: void
483: term_fontpush(struct termp *p, enum termfont f)
1.1 kristaps 484: {
1.7 schwarze 485:
1.20 schwarze 486: p->fontl = p->fontq[p->fonti];
1.98 schwarze 487: if (++p->fonti == p->fontsz) {
488: p->fontsz += 8;
489: p->fontq = mandoc_reallocarray(p->fontq,
1.116 schwarze 490: p->fontsz, sizeof(*p->fontq));
1.98 schwarze 491: }
492: p->fontq[p->fonti] = f;
1.20 schwarze 493: }
1.1 kristaps 494:
1.98 schwarze 495: /* Flush to make the saved pointer current again. */
1.20 schwarze 496: void
1.104 schwarze 497: term_fontpopq(struct termp *p, int i)
1.20 schwarze 498: {
1.1 kristaps 499:
1.104 schwarze 500: assert(i >= 0);
501: if (p->fonti > i)
502: p->fonti = i;
1.20 schwarze 503: }
1.1 kristaps 504:
1.98 schwarze 505: /* Pop one font off the stack. */
1.20 schwarze 506: void
507: term_fontpop(struct termp *p)
508: {
1.1 kristaps 509:
1.20 schwarze 510: assert(p->fonti);
511: p->fonti--;
1.1 kristaps 512: }
513:
514: /*
515: * Handle pwords, partial words, which may be either a single word or a
516: * phrase that cannot be broken down (such as a literal string). This
517: * handles word styling.
518: */
1.7 schwarze 519: void
520: term_word(struct termp *p, const char *word)
1.1 kristaps 521: {
1.121 schwarze 522: struct roffsu su;
1.75 schwarze 523: const char nbrsp[2] = { ASCII_NBRSP, 0 };
1.59 schwarze 524: const char *seq, *cp;
525: int sz, uc;
1.122 schwarze 526: size_t csz, lsz, ssz;
1.59 schwarze 527: enum mandoc_esc esc;
1.1 kristaps 528:
1.124 schwarze 529: if ((p->flags & TERMP_NOBUF) == 0) {
530: if ((p->flags & TERMP_NOSPACE) == 0) {
531: if ((p->flags & TERMP_KEEP) == 0) {
1.40 schwarze 532: bufferc(p, ' ');
1.124 schwarze 533: if (p->flags & TERMP_SENTENCE)
534: bufferc(p, ' ');
535: } else
536: bufferc(p, ASCII_NBRSP);
537: }
538: if (p->flags & TERMP_PREKEEP)
539: p->flags |= TERMP_KEEP;
540: if (p->flags & TERMP_NONOSPACE)
541: p->flags |= TERMP_NOSPACE;
542: else
543: p->flags &= ~TERMP_NOSPACE;
544: p->flags &= ~(TERMP_SENTENCE | TERMP_NONEWLINE);
545: p->skipvsp = 0;
1.31 schwarze 546: }
547:
1.59 schwarze 548: while ('\0' != *word) {
1.64 schwarze 549: if ('\\' != *word) {
1.75 schwarze 550: if (TERMP_NBRWORD & p->flags) {
551: if (' ' == *word) {
552: encode(p, nbrsp, 1);
553: word++;
554: continue;
555: }
556: ssz = strcspn(word, "\\ ");
557: } else
558: ssz = strcspn(word, "\\");
1.45 schwarze 559: encode(p, word, ssz);
1.64 schwarze 560: word += (int)ssz;
1.20 schwarze 561: continue;
1.64 schwarze 562: }
1.20 schwarze 563:
1.59 schwarze 564: word++;
565: esc = mandoc_escape(&word, &seq, &sz);
566: switch (esc) {
1.83 schwarze 567: case ESCAPE_UNICODE:
1.89 schwarze 568: uc = mchars_num2uc(seq + 1, sz - 1);
1.56 schwarze 569: break;
1.83 schwarze 570: case ESCAPE_NUMBERED:
1.93 schwarze 571: uc = mchars_num2char(seq, sz);
572: if (uc < 0)
573: continue;
1.20 schwarze 574: break;
1.83 schwarze 575: case ESCAPE_SPECIAL:
1.89 schwarze 576: if (p->enc == TERMENC_ASCII) {
1.114 schwarze 577: cp = mchars_spec2str(seq, sz, &ssz);
1.92 schwarze 578: if (cp != NULL)
1.89 schwarze 579: encode(p, cp, ssz);
580: } else {
1.114 schwarze 581: uc = mchars_spec2cp(seq, sz);
1.90 schwarze 582: if (uc > 0)
583: encode1(p, uc);
1.89 schwarze 584: }
1.93 schwarze 585: continue;
1.137 schwarze 586: case ESCAPE_UNDEF:
587: uc = *seq;
588: break;
1.83 schwarze 589: case ESCAPE_FONTBOLD:
1.143 schwarze 590: case ESCAPE_FONTCB:
1.20 schwarze 591: term_fontrepl(p, TERMFONT_BOLD);
1.93 schwarze 592: continue;
1.83 schwarze 593: case ESCAPE_FONTITALIC:
1.143 schwarze 594: case ESCAPE_FONTCI:
1.20 schwarze 595: term_fontrepl(p, TERMFONT_UNDER);
1.93 schwarze 596: continue;
1.83 schwarze 597: case ESCAPE_FONTBI:
1.70 schwarze 598: term_fontrepl(p, TERMFONT_BI);
1.93 schwarze 599: continue;
1.83 schwarze 600: case ESCAPE_FONT:
1.143 schwarze 601: case ESCAPE_FONTCR:
1.83 schwarze 602: case ESCAPE_FONTROMAN:
1.20 schwarze 603: term_fontrepl(p, TERMFONT_NONE);
1.93 schwarze 604: continue;
1.83 schwarze 605: case ESCAPE_FONTPREV:
1.20 schwarze 606: term_fontlast(p);
1.130 schwarze 607: continue;
608: case ESCAPE_BREAK:
609: bufferc(p, '\n');
1.93 schwarze 610: continue;
1.83 schwarze 611: case ESCAPE_NOSPACE:
1.108 schwarze 612: if (p->flags & TERMP_BACKAFTER)
613: p->flags &= ~TERMP_BACKAFTER;
614: else if (*word == '\0')
1.97 schwarze 615: p->flags |= (TERMP_NOSPACE | TERMP_NONEWLINE);
1.121 schwarze 616: continue;
1.135 schwarze 617: case ESCAPE_DEVICE:
618: if (p->type == TERMTYPE_PDF)
619: encode(p, "pdf", 3);
620: else if (p->type == TERMTYPE_PS)
621: encode(p, "ps", 2);
622: else if (p->enc == TERMENC_ASCII)
623: encode(p, "ascii", 5);
624: else
625: encode(p, "utf8", 4);
626: continue;
1.121 schwarze 627: case ESCAPE_HORIZ:
1.146 schwarze 628: if (p->flags & TERMP_BACKAFTER) {
629: p->flags &= ~TERMP_BACKAFTER;
630: continue;
631: }
1.133 schwarze 632: if (*seq == '|') {
633: seq++;
634: uc = -p->col;
635: } else
636: uc = 0;
1.128 schwarze 637: if (a2roffsu(seq, &su, SCALE_EM) == NULL)
1.121 schwarze 638: continue;
1.133 schwarze 639: uc += term_hen(p, &su);
1.146 schwarze 640: if (uc >= 0) {
1.145 schwarze 641: while (uc > 0) {
642: uc -= term_len(p, 1);
1.146 schwarze 643: if (p->flags & TERMP_BACKBEFORE)
644: p->flags &= ~TERMP_BACKBEFORE;
645: else
646: bufferc(p, ASCII_NBRSP);
1.145 schwarze 647: }
1.146 schwarze 648: continue;
649: }
650: if (p->flags & TERMP_BACKBEFORE) {
651: p->flags &= ~TERMP_BACKBEFORE;
652: assert(p->col > 0);
653: p->col--;
654: }
655: if (p->col >= (size_t)(-uc)) {
1.121 schwarze 656: p->col += uc;
1.145 schwarze 657: } else {
1.121 schwarze 658: uc += p->col;
659: p->col = 0;
1.126 schwarze 660: if (p->tcol->offset > (size_t)(-uc)) {
1.121 schwarze 661: p->ti += uc;
1.126 schwarze 662: p->tcol->offset += uc;
1.121 schwarze 663: } else {
1.126 schwarze 664: p->ti -= p->tcol->offset;
665: p->tcol->offset = 0;
1.121 schwarze 666: }
1.122 schwarze 667: }
668: continue;
669: case ESCAPE_HLINE:
1.132 schwarze 670: if ((cp = a2roffsu(seq, &su, SCALE_EM)) == NULL)
1.122 schwarze 671: continue;
1.131 schwarze 672: uc = term_hen(p, &su);
1.122 schwarze 673: if (uc <= 0) {
1.126 schwarze 674: if (p->tcol->rmargin <= p->tcol->offset)
1.122 schwarze 675: continue;
1.126 schwarze 676: lsz = p->tcol->rmargin - p->tcol->offset;
1.122 schwarze 677: } else
678: lsz = uc;
1.132 schwarze 679: if (*cp == seq[-1])
1.122 schwarze 680: uc = -1;
1.132 schwarze 681: else if (*cp == '\\') {
682: seq = cp + 1;
1.122 schwarze 683: esc = mandoc_escape(&seq, &cp, &sz);
684: switch (esc) {
685: case ESCAPE_UNICODE:
686: uc = mchars_num2uc(cp + 1, sz - 1);
687: break;
688: case ESCAPE_NUMBERED:
689: uc = mchars_num2char(cp, sz);
690: break;
691: case ESCAPE_SPECIAL:
692: uc = mchars_spec2cp(cp, sz);
693: break;
1.137 schwarze 694: case ESCAPE_UNDEF:
695: uc = *seq;
696: break;
1.122 schwarze 697: default:
698: uc = -1;
699: break;
700: }
701: } else
1.132 schwarze 702: uc = *cp;
1.122 schwarze 703: if (uc < 0x20 || (uc > 0x7E && uc < 0xA0))
704: uc = '_';
705: if (p->enc == TERMENC_ASCII) {
706: cp = ascii_uc2str(uc);
707: csz = term_strlen(p, cp);
708: ssz = strlen(cp);
709: } else
710: csz = (*p->width)(p, uc);
711: while (lsz >= csz) {
712: if (p->enc == TERMENC_ASCII)
713: encode(p, cp, ssz);
714: else
715: encode1(p, uc);
716: lsz -= csz;
1.121 schwarze 717: }
1.93 schwarze 718: continue;
1.83 schwarze 719: case ESCAPE_SKIPCHAR:
1.108 schwarze 720: p->flags |= TERMP_BACKAFTER;
1.93 schwarze 721: continue;
1.103 schwarze 722: case ESCAPE_OVERSTRIKE:
723: cp = seq + sz;
724: while (seq < cp) {
725: if (*seq == '\\') {
726: mandoc_escape(&seq, NULL, NULL);
727: continue;
728: }
729: encode1(p, *seq++);
1.108 schwarze 730: if (seq < cp) {
731: if (p->flags & TERMP_BACKBEFORE)
732: p->flags |= TERMP_BACKAFTER;
733: else
734: p->flags |= TERMP_BACKBEFORE;
735: }
1.103 schwarze 736: }
1.109 schwarze 737: /* Trim trailing backspace/blank pair. */
1.129 schwarze 738: if (p->tcol->lastcol > 2 &&
739: (p->tcol->buf[p->tcol->lastcol - 1] == ' ' ||
740: p->tcol->buf[p->tcol->lastcol - 1] == '\t'))
741: p->tcol->lastcol -= 2;
742: if (p->col > p->tcol->lastcol)
743: p->col = p->tcol->lastcol;
1.108 schwarze 744: continue;
1.20 schwarze 745: default:
1.93 schwarze 746: continue;
747: }
748:
749: /*
750: * Common handling for Unicode and numbered
751: * character escape sequences.
752: */
753:
754: if (p->enc == TERMENC_ASCII) {
755: cp = ascii_uc2str(uc);
756: encode(p, cp, strlen(cp));
757: } else {
758: if ((uc < 0x20 && uc != 0x09) ||
759: (uc > 0x7E && uc < 0xA0))
760: uc = 0xFFFD;
761: encode1(p, uc);
1.20 schwarze 762: }
763: }
1.75 schwarze 764: p->flags &= ~TERMP_NBRWORD;
1.1 kristaps 765: }
766:
767: static void
1.126 schwarze 768: adjbuf(struct termp_col *c, size_t sz)
1.1 kristaps 769: {
1.126 schwarze 770: if (c->maxcols == 0)
771: c->maxcols = 1024;
772: while (c->maxcols <= sz)
773: c->maxcols <<= 2;
774: c->buf = mandoc_reallocarray(c->buf, c->maxcols, sizeof(*c->buf));
1.1 kristaps 775: }
776:
1.4 schwarze 777: static void
1.20 schwarze 778: bufferc(struct termp *p, char c)
779: {
1.124 schwarze 780: if (p->flags & TERMP_NOBUF) {
781: (*p->letter)(p, c);
782: return;
783: }
1.126 schwarze 784: if (p->col + 1 >= p->tcol->maxcols)
785: adjbuf(p->tcol, p->col + 1);
1.129 schwarze 786: if (p->tcol->lastcol <= p->col || (c != ' ' && c != ASCII_NBRSP))
1.126 schwarze 787: p->tcol->buf[p->col] = c;
1.129 schwarze 788: if (p->tcol->lastcol < ++p->col)
789: p->tcol->lastcol = p->col;
1.20 schwarze 790: }
791:
1.59 schwarze 792: /*
793: * See encode().
794: * Do this for a single (probably unicode) value.
795: * Does not check for non-decorated glyphs.
796: */
797: static void
798: encode1(struct termp *p, int c)
799: {
800: enum termfont f;
801:
1.124 schwarze 802: if (p->flags & TERMP_NOBUF) {
803: (*p->letter)(p, c);
804: return;
805: }
806:
1.126 schwarze 807: if (p->col + 7 >= p->tcol->maxcols)
808: adjbuf(p->tcol, p->col + 7);
1.59 schwarze 809:
1.115 schwarze 810: f = (c == ASCII_HYPH || c > 127 || isgraph(c)) ?
1.108 schwarze 811: p->fontq[p->fonti] : TERMFONT_NONE;
1.59 schwarze 812:
1.108 schwarze 813: if (p->flags & TERMP_BACKBEFORE) {
1.126 schwarze 814: if (p->tcol->buf[p->col - 1] == ' ' ||
815: p->tcol->buf[p->col - 1] == '\t')
1.109 schwarze 816: p->col--;
817: else
1.126 schwarze 818: p->tcol->buf[p->col++] = '\b';
1.108 schwarze 819: p->flags &= ~TERMP_BACKBEFORE;
820: }
1.126 schwarze 821: if (f == TERMFONT_UNDER || f == TERMFONT_BI) {
822: p->tcol->buf[p->col++] = '_';
823: p->tcol->buf[p->col++] = '\b';
824: }
825: if (f == TERMFONT_BOLD || f == TERMFONT_BI) {
826: if (c == ASCII_HYPH)
827: p->tcol->buf[p->col++] = '-';
1.70 schwarze 828: else
1.126 schwarze 829: p->tcol->buf[p->col++] = c;
830: p->tcol->buf[p->col++] = '\b';
1.70 schwarze 831: }
1.129 schwarze 832: if (p->tcol->lastcol <= p->col || (c != ' ' && c != ASCII_NBRSP))
1.126 schwarze 833: p->tcol->buf[p->col] = c;
1.129 schwarze 834: if (p->tcol->lastcol < ++p->col)
835: p->tcol->lastcol = p->col;
1.108 schwarze 836: if (p->flags & TERMP_BACKAFTER) {
837: p->flags |= TERMP_BACKBEFORE;
838: p->flags &= ~TERMP_BACKAFTER;
839: }
1.59 schwarze 840: }
1.20 schwarze 841:
842: static void
843: encode(struct termp *p, const char *word, size_t sz)
1.4 schwarze 844: {
1.71 schwarze 845: size_t i;
1.124 schwarze 846:
847: if (p->flags & TERMP_NOBUF) {
848: for (i = 0; i < sz; i++)
849: (*p->letter)(p, word[i]);
850: return;
851: }
1.59 schwarze 852:
1.126 schwarze 853: if (p->col + 2 + (sz * 5) >= p->tcol->maxcols)
854: adjbuf(p->tcol, p->col + 2 + (sz * 5));
1.46 schwarze 855:
1.71 schwarze 856: for (i = 0; i < sz; i++) {
1.70 schwarze 857: if (ASCII_HYPH == word[i] ||
858: isgraph((unsigned char)word[i]))
859: encode1(p, word[i]);
1.119 schwarze 860: else {
1.129 schwarze 861: if (p->tcol->lastcol <= p->col ||
1.125 schwarze 862: (word[i] != ' ' && word[i] != ASCII_NBRSP))
1.126 schwarze 863: p->tcol->buf[p->col] = word[i];
1.125 schwarze 864: p->col++;
1.119 schwarze 865:
866: /*
867: * Postpone the effect of \z while handling
868: * an overstrike sequence from ascii_uc2str().
869: */
870:
871: if (word[i] == '\b' &&
872: (p->flags & TERMP_BACKBEFORE)) {
873: p->flags &= ~TERMP_BACKBEFORE;
874: p->flags |= TERMP_BACKAFTER;
875: }
876: }
1.4 schwarze 877: }
1.129 schwarze 878: if (p->tcol->lastcol < p->col)
879: p->tcol->lastcol = p->col;
1.80 schwarze 880: }
881:
882: void
883: term_setwidth(struct termp *p, const char *wstr)
884: {
885: struct roffsu su;
1.107 schwarze 886: int iop, width;
1.80 schwarze 887:
1.81 schwarze 888: iop = 0;
889: width = 0;
1.80 schwarze 890: if (NULL != wstr) {
891: switch (*wstr) {
1.83 schwarze 892: case '+':
1.80 schwarze 893: iop = 1;
894: wstr++;
895: break;
1.83 schwarze 896: case '-':
1.80 schwarze 897: iop = -1;
898: wstr++;
899: break;
900: default:
901: break;
902: }
1.128 schwarze 903: if (a2roffsu(wstr, &su, SCALE_MAX) != NULL)
1.81 schwarze 904: width = term_hspan(p, &su);
905: else
1.80 schwarze 906: iop = 0;
907: }
908: (*p->setwidth)(p, iop, width);
1.4 schwarze 909: }
1.16 schwarze 910:
911: size_t
1.39 schwarze 912: term_len(const struct termp *p, size_t sz)
913: {
914:
1.112 schwarze 915: return (*p->width)(p, ' ') * sz;
1.39 schwarze 916: }
917:
1.64 schwarze 918: static size_t
919: cond_width(const struct termp *p, int c, int *skip)
920: {
921:
922: if (*skip) {
923: (*skip) = 0;
1.112 schwarze 924: return 0;
1.64 schwarze 925: } else
1.112 schwarze 926: return (*p->width)(p, c);
1.64 schwarze 927: }
1.39 schwarze 928:
929: size_t
930: term_strlen(const struct termp *p, const char *cp)
931: {
1.59 schwarze 932: size_t sz, rsz, i;
1.93 schwarze 933: int ssz, skip, uc;
1.50 schwarze 934: const char *seq, *rhs;
1.59 schwarze 935: enum mandoc_esc esc;
1.77 schwarze 936: static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
937: ASCII_BREAK, '\0' };
1.59 schwarze 938:
939: /*
940: * Account for escaped sequences within string length
941: * calculations. This follows the logic in term_word() as we
942: * must calculate the width of produced strings.
943: */
944:
945: sz = 0;
1.64 schwarze 946: skip = 0;
1.59 schwarze 947: while ('\0' != *cp) {
948: rsz = strcspn(cp, rej);
949: for (i = 0; i < rsz; i++)
1.64 schwarze 950: sz += cond_width(p, *cp++, &skip);
1.59 schwarze 951:
952: switch (*cp) {
1.83 schwarze 953: case '\\':
1.59 schwarze 954: cp++;
1.137 schwarze 955: rhs = NULL;
1.59 schwarze 956: esc = mandoc_escape(&cp, &seq, &ssz);
957: switch (esc) {
1.83 schwarze 958: case ESCAPE_UNICODE:
1.94 schwarze 959: uc = mchars_num2uc(seq + 1, ssz - 1);
1.59 schwarze 960: break;
1.83 schwarze 961: case ESCAPE_NUMBERED:
1.93 schwarze 962: uc = mchars_num2char(seq, ssz);
963: if (uc < 0)
964: continue;
1.50 schwarze 965: break;
1.83 schwarze 966: case ESCAPE_SPECIAL:
1.93 schwarze 967: if (p->enc == TERMENC_ASCII) {
1.114 schwarze 968: rhs = mchars_spec2str(seq, ssz, &rsz);
1.93 schwarze 969: if (rhs != NULL)
970: break;
971: } else {
1.114 schwarze 972: uc = mchars_spec2cp(seq, ssz);
1.93 schwarze 973: if (uc > 0)
974: sz += cond_width(p, uc, &skip);
1.89 schwarze 975: }
1.93 schwarze 976: continue;
1.137 schwarze 977: case ESCAPE_UNDEF:
978: uc = *seq;
979: break;
1.135 schwarze 980: case ESCAPE_DEVICE:
981: if (p->type == TERMTYPE_PDF) {
982: rhs = "pdf";
983: rsz = 3;
984: } else if (p->type == TERMTYPE_PS) {
985: rhs = "ps";
986: rsz = 2;
987: } else if (p->enc == TERMENC_ASCII) {
988: rhs = "ascii";
989: rsz = 5;
990: } else {
991: rhs = "utf8";
992: rsz = 4;
993: }
994: break;
1.83 schwarze 995: case ESCAPE_SKIPCHAR:
1.64 schwarze 996: skip = 1;
1.103 schwarze 997: continue;
998: case ESCAPE_OVERSTRIKE:
999: rsz = 0;
1000: rhs = seq + ssz;
1001: while (seq < rhs) {
1002: if (*seq == '\\') {
1003: mandoc_escape(&seq, NULL, NULL);
1004: continue;
1005: }
1006: i = (*p->width)(p, *seq++);
1007: if (rsz < i)
1008: rsz = i;
1009: }
1010: sz += rsz;
1.93 schwarze 1011: continue;
1.50 schwarze 1012: default:
1.93 schwarze 1013: continue;
1.50 schwarze 1014: }
1.39 schwarze 1015:
1.93 schwarze 1016: /*
1017: * Common handling for Unicode and numbered
1018: * character escape sequences.
1019: */
1020:
1021: if (rhs == NULL) {
1022: if (p->enc == TERMENC_ASCII) {
1023: rhs = ascii_uc2str(uc);
1024: rsz = strlen(rhs);
1025: } else {
1026: if ((uc < 0x20 && uc != 0x09) ||
1027: (uc > 0x7E && uc < 0xA0))
1028: uc = 0xFFFD;
1029: sz += cond_width(p, uc, &skip);
1030: continue;
1031: }
1032: }
1.59 schwarze 1033:
1.64 schwarze 1034: if (skip) {
1035: skip = 0;
1036: break;
1037: }
1.93 schwarze 1038:
1039: /*
1040: * Common handling for all escape sequences
1041: * printing more than one character.
1042: */
1.64 schwarze 1043:
1.59 schwarze 1044: for (i = 0; i < rsz; i++)
1045: sz += (*p->width)(p, *rhs++);
1046: break;
1.83 schwarze 1047: case ASCII_NBRSP:
1.64 schwarze 1048: sz += cond_width(p, ' ', &skip);
1.55 schwarze 1049: cp++;
1.59 schwarze 1050: break;
1.83 schwarze 1051: case ASCII_HYPH:
1.64 schwarze 1052: sz += cond_width(p, '-', &skip);
1.55 schwarze 1053: cp++;
1.59 schwarze 1054: break;
1055: default:
1056: break;
1057: }
1058: }
1.39 schwarze 1059:
1.112 schwarze 1060: return sz;
1.39 schwarze 1061: }
1062:
1.100 schwarze 1063: int
1.39 schwarze 1064: term_vspan(const struct termp *p, const struct roffsu *su)
1.16 schwarze 1065: {
1066: double r;
1.101 schwarze 1067: int ri;
1.16 schwarze 1068:
1069: switch (su->unit) {
1.99 schwarze 1070: case SCALE_BU:
1071: r = su->scale / 40.0;
1072: break;
1.83 schwarze 1073: case SCALE_CM:
1.99 schwarze 1074: r = su->scale * 6.0 / 2.54;
1075: break;
1076: case SCALE_FS:
1077: r = su->scale * 65536.0 / 40.0;
1.16 schwarze 1078: break;
1.83 schwarze 1079: case SCALE_IN:
1.86 schwarze 1080: r = su->scale * 6.0;
1.16 schwarze 1081: break;
1.99 schwarze 1082: case SCALE_MM:
1083: r = su->scale * 0.006;
1084: break;
1.83 schwarze 1085: case SCALE_PC:
1.16 schwarze 1086: r = su->scale;
1087: break;
1.83 schwarze 1088: case SCALE_PT:
1.99 schwarze 1089: r = su->scale / 12.0;
1.16 schwarze 1090: break;
1.99 schwarze 1091: case SCALE_EN:
1092: case SCALE_EM:
1093: r = su->scale * 0.6;
1.16 schwarze 1094: break;
1.83 schwarze 1095: case SCALE_VS:
1.16 schwarze 1096: r = su->scale;
1097: break;
1098: default:
1.99 schwarze 1099: abort();
1.16 schwarze 1100: }
1.101 schwarze 1101: ri = r > 0.0 ? r + 0.4995 : r - 0.4995;
1.112 schwarze 1102: return ri < 66 ? ri : 1;
1.16 schwarze 1103: }
1104:
1.107 schwarze 1105: /*
1.131 schwarze 1106: * Convert a scaling width to basic units, rounding towards 0.
1.107 schwarze 1107: */
1.100 schwarze 1108: int
1.39 schwarze 1109: term_hspan(const struct termp *p, const struct roffsu *su)
1.16 schwarze 1110: {
1111:
1.112 schwarze 1112: return (*p->hspan)(p, su);
1.131 schwarze 1113: }
1114:
1115: /*
1116: * Convert a scaling width to basic units, rounding to closest.
1117: */
1118: int
1119: term_hen(const struct termp *p, const struct roffsu *su)
1120: {
1121: int bu;
1122:
1123: if ((bu = (*p->hspan)(p, su)) >= 0)
1124: return (bu + 11) / 24;
1125: else
1126: return -((-bu + 11) / 24);
1.16 schwarze 1127: }