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