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