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