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