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