Annotation of mandoc/term.c, Revision 1.265
1.265 ! schwarze 1: /* $Id: term.c,v 1.264 2017/06/04 22:44:15 schwarze Exp $ */
1.1 kristaps 2: /*
1.198 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.259 schwarze 4: * Copyright (c) 2010-2017 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.22 kristaps 24: #include <stdio.h>
1.1 kristaps 25: #include <stdlib.h>
26: #include <string.h>
27:
1.137 kristaps 28: #include "mandoc.h"
1.218 schwarze 29: #include "mandoc_aux.h"
1.107 kristaps 30: #include "out.h"
1.71 kristaps 31: #include "term.h"
1.105 kristaps 32: #include "main.h"
1.1 kristaps 33:
1.203 schwarze 34: static size_t cond_width(const struct termp *, int, int *);
1.210 schwarze 35: static void adjbuf(struct termp *p, size_t);
1.191 kristaps 36: static void bufferc(struct termp *, char);
37: static void encode(struct termp *, const char *, size_t);
1.194 kristaps 38: static void encode1(struct termp *, int);
1.264 schwarze 39: static void endline(struct termp *);
1.11 kristaps 40:
1.222 schwarze 41:
1.145 kristaps 42: void
1.71 kristaps 43: term_free(struct termp *p)
1.14 kristaps 44: {
45:
1.231 schwarze 46: free(p->buf);
1.238 schwarze 47: free(p->fontq);
1.142 kristaps 48: free(p);
49: }
50:
51: void
1.222 schwarze 52: term_begin(struct termp *p, term_margin head,
1.246 schwarze 53: term_margin foot, const struct roff_meta *arg)
1.142 kristaps 54: {
55:
56: p->headf = head;
57: p->footf = foot;
58: p->argf = arg;
1.146 kristaps 59: (*p->begin)(p);
1.142 kristaps 60: }
61:
62: void
63: term_end(struct termp *p)
64: {
65:
1.146 kristaps 66: (*p->end)(p);
1.14 kristaps 67: }
68:
1.71 kristaps 69: /*
1.221 schwarze 70: * Flush a chunk of text. By default, break the output line each time
71: * the right margin is reached, and continue output on the next line
72: * at the same offset as the chunk itself. By default, also break the
73: * output line at the end of the chunk.
1.130 kristaps 74: * The following flags may be specified:
1.71 kristaps 75: *
1.221 schwarze 76: * - TERMP_NOBREAK: Do not break the output line at the right margin,
77: * but only at the max right margin. Also, do not break the output
78: * line at the end of the chunk, such that the next call can pad to
79: * the next column. However, if less than p->trailspace blanks,
80: * which can be 0, 1, or 2, remain to the right margin, the line
81: * will be broken.
1.250 schwarze 82: * - TERMP_BRTRSP: Consider trailing whitespace significant
83: * when deciding whether the chunk fits or not.
1.221 schwarze 84: * - TERMP_BRIND: If the chunk does not fit and the output line has
85: * to be broken, start the next line at the right margin instead
86: * of at the offset. Used together with TERMP_NOBREAK for the tags
87: * in various kinds of tagged lists.
1.263 schwarze 88: * - TERMP_HANG: Do not break the output line at the right margin,
1.221 schwarze 89: * append the next chunk after it even if this one is too long.
90: * To be used together with TERMP_NOBREAK.
1.263 schwarze 91: * - TERMP_NOPAD: Start writing at the current position,
92: * do not pad with blank characters up to the offset.
1.71 kristaps 93: */
94: void
95: term_flushln(struct termp *p)
1.53 kristaps 96: {
1.210 schwarze 97: size_t i; /* current input position in p->buf */
1.205 schwarze 98: int ntab; /* number of tabs to prepend */
1.114 kristaps 99: size_t vis; /* current visual position on output */
100: size_t vbl; /* number of blanks to prepend to output */
1.136 schwarze 101: size_t vend; /* end of word visual position on output */
1.114 kristaps 102: size_t bp; /* visual right border position */
1.172 schwarze 103: size_t dv; /* temporary for visual pos calculations */
1.210 schwarze 104: size_t j; /* temporary loop index for p->buf */
105: size_t jhy; /* last hyph before overflow w/r/t j */
1.152 kristaps 106: size_t maxvis; /* output position of visible boundary */
1.53 kristaps 107:
1.263 schwarze 108: vbl = (p->flags & TERMP_NOPAD) || p->offset < p->viscol ? 0 :
109: p->offset - p->viscol;
110: if (p->minbl && vbl < p->minbl)
111: vbl = p->minbl;
112: maxvis = p->rmargin > p->viscol + vbl ?
113: p->rmargin - p->viscol - vbl : 0;
114: bp = !(p->flags & TERMP_NOBREAK) ? maxvis :
115: p->maxrmargin > p->viscol + vbl ?
116: p->maxrmargin - p->viscol - vbl : 0;
1.174 schwarze 117: vis = vend = 0;
118: i = 0;
1.115 kristaps 119:
1.265 ! schwarze 120: while (i < p->lastcol) {
1.71 kristaps 121: /*
1.154 kristaps 122: * Handle literal tab characters: collapse all
123: * subsequent tabs into a single huge set of spaces.
1.138 schwarze 124: */
1.205 schwarze 125: ntab = 0;
1.265 ! schwarze 126: while (i < p->lastcol && p->buf[i] == '\t') {
1.260 schwarze 127: vend = term_tab_next(vis);
1.138 schwarze 128: vbl += vend - vis;
129: vis = vend;
1.205 schwarze 130: ntab++;
1.169 schwarze 131: i++;
1.138 schwarze 132: }
133:
134: /*
1.71 kristaps 135: * Count up visible word characters. Control sequences
136: * (starting with the CSI) aren't counted. A space
137: * generates a non-printing word, which is valid (the
138: * space is printed according to regular spacing rules).
139: */
140:
1.265 ! schwarze 141: for (j = i, jhy = 0; j < p->lastcol; j++) {
1.208 schwarze 142: if (' ' == p->buf[j] || '\t' == p->buf[j])
1.71 kristaps 143: break;
1.154 kristaps 144:
1.257 schwarze 145: /* Back over the last printed character. */
1.154 kristaps 146: if (8 == p->buf[j]) {
1.153 kristaps 147: assert(j);
148: vend -= (*p->width)(p, p->buf[j - 1]);
1.154 kristaps 149: continue;
1.153 kristaps 150: }
1.154 kristaps 151:
152: /* Regular word. */
153: /* Break at the hyphen point if we overrun. */
1.222 schwarze 154: if (vend > vis && vend < bp &&
1.216 schwarze 155: (ASCII_HYPH == p->buf[j] ||
156: ASCII_BREAK == p->buf[j]))
1.154 kristaps 157: jhy = j;
158:
1.217 schwarze 159: /*
160: * Hyphenation now decided, put back a real
161: * hyphen such that we get the correct width.
162: */
163: if (ASCII_HYPH == p->buf[j])
164: p->buf[j] = '-';
165:
1.154 kristaps 166: vend += (*p->width)(p, p->buf[j]);
1.71 kristaps 167: }
1.53 kristaps 168:
1.71 kristaps 169: /*
1.81 kristaps 170: * Find out whether we would exceed the right margin.
1.136 schwarze 171: * If so, break to the next line.
1.81 kristaps 172: */
1.264 schwarze 173: if (vend > bp && 0 == jhy && vis > 0 &&
174: (p->flags & TERMP_BRNEVER) == 0) {
1.136 schwarze 175: vend -= vis;
1.264 schwarze 176: endline(p);
1.205 schwarze 177:
1.260 schwarze 178: /* Use pending tabs on the new line. */
179:
180: vbl = 0;
181: while (ntab--)
182: vbl = term_tab_next(vbl);
183:
184: /* Re-establish indentation. */
1.205 schwarze 185:
1.263 schwarze 186: if (p->flags & TERMP_BRIND)
1.260 schwarze 187: vbl += p->rmargin;
1.263 schwarze 188: else
1.260 schwarze 189: vbl += p->offset;
1.263 schwarze 190: maxvis = p->rmargin > vbl ? p->rmargin - vbl : 0;
191: bp = !(p->flags & TERMP_NOBREAK) ? maxvis :
192: p->maxrmargin > vbl ? p->maxrmargin - vbl : 0;
1.71 kristaps 193: }
1.138 schwarze 194:
1.130 kristaps 195: /* Write out the [remaining] word. */
1.265 ! schwarze 196: for ( ; i < p->lastcol; i++) {
1.140 kristaps 197: if (vend > bp && jhy > 0 && i > jhy)
198: break;
1.138 schwarze 199: if ('\t' == p->buf[i])
200: break;
1.136 schwarze 201: if (' ' == p->buf[i]) {
1.164 kristaps 202: j = i;
1.265 ! schwarze 203: while (i < p->lastcol && ' ' == p->buf[i])
1.136 schwarze 204: i++;
1.210 schwarze 205: dv = (i - j) * (*p->width)(p, ' ');
1.172 schwarze 206: vbl += dv;
207: vend += dv;
1.71 kristaps 208: break;
1.136 schwarze 209: }
210: if (ASCII_NBRSP == p->buf[i]) {
1.153 kristaps 211: vbl += (*p->width)(p, ' ');
1.136 schwarze 212: continue;
213: }
1.216 schwarze 214: if (ASCII_BREAK == p->buf[i])
215: continue;
1.130 kristaps 216:
1.136 schwarze 217: /*
218: * Now we definitely know there will be
219: * printable characters to output,
220: * so write preceding white space now.
221: */
222: if (vbl) {
1.146 kristaps 223: (*p->advance)(p, vbl);
1.139 schwarze 224: p->viscol += vbl;
1.136 schwarze 225: vbl = 0;
1.200 schwarze 226: }
227:
228: (*p->letter)(p, p->buf[i]);
229: if (8 == p->buf[i])
230: p->viscol -= (*p->width)(p, p->buf[i-1]);
1.222 schwarze 231: else
1.153 kristaps 232: p->viscol += (*p->width)(p, p->buf[i]);
1.136 schwarze 233: }
234: vis = vend;
1.71 kristaps 235: }
1.168 schwarze 236:
237: /*
238: * If there was trailing white space, it was not printed;
239: * so reset the cursor position accordingly.
240: */
1.235 schwarze 241: if (vis > vbl)
1.200 schwarze 242: vis -= vbl;
1.235 schwarze 243: else
244: vis = 0;
1.111 kristaps 245:
1.265 ! schwarze 246: p->col = p->lastcol = 0;
1.264 schwarze 247: p->minbl = p->trailspace;
1.263 schwarze 248: p->flags &= ~(TERMP_BACKAFTER | TERMP_BACKBEFORE | TERMP_NOPAD);
1.15 kristaps 249:
1.250 schwarze 250: /* Trailing whitespace is significant in some columns. */
251: if (vis && vbl && (TERMP_BRTRSP & p->flags))
252: vis += vbl;
1.15 kristaps 253:
1.200 schwarze 254: /* If the column was overrun, break the line. */
1.264 schwarze 255: if ((p->flags & TERMP_NOBREAK) == 0 ||
256: ((p->flags & TERMP_HANG) == 0 &&
257: vis + p->trailspace * (*p->width)(p, ' ') > maxvis))
258: endline(p);
259: }
260:
261: static void
262: endline(struct termp *p)
263: {
264: if ((p->flags & (TERMP_NEWMC | TERMP_ENDMC)) == TERMP_ENDMC) {
265: p->mc = NULL;
266: p->flags &= ~TERMP_ENDMC;
267: }
268: if (p->mc != NULL) {
269: if (p->viscol && p->maxrmargin >= p->viscol)
270: (*p->advance)(p, p->maxrmargin - p->viscol + 1);
271: p->flags |= TERMP_NOBUF | TERMP_NOSPACE;
272: term_word(p, p->mc);
273: p->flags &= ~(TERMP_NOBUF | TERMP_NEWMC);
274: }
275: p->viscol = 0;
276: p->minbl = 0;
277: (*p->endline)(p);
1.15 kristaps 278: }
279:
1.222 schwarze 280: /*
1.71 kristaps 281: * A newline only breaks an existing line; it won't assert vertical
282: * space. All data in the output buffer is flushed prior to the newline
283: * assertion.
284: */
285: void
286: term_newln(struct termp *p)
1.15 kristaps 287: {
288:
1.71 kristaps 289: p->flags |= TERMP_NOSPACE;
1.265 ! schwarze 290: if (p->lastcol || p->viscol)
1.200 schwarze 291: term_flushln(p);
1.16 kristaps 292: }
293:
1.71 kristaps 294: /*
295: * Asserts a vertical space (a full, empty line-break between lines).
296: * Note that if used twice, this will cause two blank spaces and so on.
297: * All data in the output buffer is flushed prior to the newline
298: * assertion.
299: */
300: void
301: term_vspace(struct termp *p)
1.16 kristaps 302: {
303:
1.62 kristaps 304: term_newln(p);
1.139 schwarze 305: p->viscol = 0;
1.264 schwarze 306: p->minbl = 0;
1.202 schwarze 307: if (0 < p->skipvsp)
308: p->skipvsp--;
309: else
310: (*p->endline)(p);
1.16 kristaps 311: }
312:
1.238 schwarze 313: /* Swap current and previous font; for \fP and .ft P */
1.125 kristaps 314: void
315: term_fontlast(struct termp *p)
316: {
317: enum termfont f;
318:
319: f = p->fontl;
320: p->fontl = p->fontq[p->fonti];
321: p->fontq[p->fonti] = f;
322: }
323:
1.238 schwarze 324: /* Set font, save current, discard previous; for \f, .ft, .B etc. */
1.125 kristaps 325: void
326: term_fontrepl(struct termp *p, enum termfont f)
327: {
328:
329: p->fontl = p->fontq[p->fonti];
330: p->fontq[p->fonti] = f;
331: }
332:
1.238 schwarze 333: /* Set font, save previous. */
1.125 kristaps 334: void
335: term_fontpush(struct termp *p, enum termfont f)
336: {
337:
338: p->fontl = p->fontq[p->fonti];
1.238 schwarze 339: if (++p->fonti == p->fontsz) {
340: p->fontsz += 8;
341: p->fontq = mandoc_reallocarray(p->fontq,
1.256 schwarze 342: p->fontsz, sizeof(*p->fontq));
1.238 schwarze 343: }
344: p->fontq[p->fonti] = f;
1.125 kristaps 345: }
346:
1.238 schwarze 347: /* Flush to make the saved pointer current again. */
1.125 kristaps 348: void
1.244 schwarze 349: term_fontpopq(struct termp *p, int i)
1.125 kristaps 350: {
351:
1.244 schwarze 352: assert(i >= 0);
353: if (p->fonti > i)
354: p->fonti = i;
1.125 kristaps 355: }
1.94 kristaps 356:
1.238 schwarze 357: /* Pop one font off the stack. */
1.125 kristaps 358: void
359: term_fontpop(struct termp *p)
360: {
361:
362: assert(p->fonti);
363: p->fonti--;
1.17 kristaps 364: }
365:
1.71 kristaps 366: /*
367: * Handle pwords, partial words, which may be either a single word or a
368: * phrase that cannot be broken down (such as a literal string). This
369: * handles word styling.
370: */
1.86 kristaps 371: void
372: term_word(struct termp *p, const char *word)
1.65 kristaps 373: {
1.261 schwarze 374: struct roffsu su;
1.214 schwarze 375: const char nbrsp[2] = { ASCII_NBRSP, 0 };
1.191 kristaps 376: const char *seq, *cp;
1.194 kristaps 377: int sz, uc;
1.262 schwarze 378: size_t csz, lsz, ssz;
1.184 kristaps 379: enum mandoc_esc esc;
1.100 kristaps 380:
1.264 schwarze 381: if ((p->flags & TERMP_NOBUF) == 0) {
382: if ((p->flags & TERMP_NOSPACE) == 0) {
383: if ((p->flags & TERMP_KEEP) == 0) {
1.151 schwarze 384: bufferc(p, ' ');
1.264 schwarze 385: if (p->flags & TERMP_SENTENCE)
386: bufferc(p, ' ');
387: } else
388: bufferc(p, ASCII_NBRSP);
389: }
390: if (p->flags & TERMP_PREKEEP)
391: p->flags |= TERMP_KEEP;
392: if (p->flags & TERMP_NONOSPACE)
393: p->flags |= TERMP_NOSPACE;
394: else
395: p->flags &= ~TERMP_NOSPACE;
396: p->flags &= ~(TERMP_SENTENCE | TERMP_NONEWLINE);
397: p->skipvsp = 0;
1.133 kristaps 398: }
1.65 kristaps 399:
1.184 kristaps 400: while ('\0' != *word) {
1.203 schwarze 401: if ('\\' != *word) {
1.214 schwarze 402: if (TERMP_NBRWORD & p->flags) {
403: if (' ' == *word) {
404: encode(p, nbrsp, 1);
405: word++;
406: continue;
407: }
408: ssz = strcspn(word, "\\ ");
409: } else
410: ssz = strcspn(word, "\\");
1.162 kristaps 411: encode(p, word, ssz);
1.203 schwarze 412: word += (int)ssz;
1.124 kristaps 413: continue;
1.203 schwarze 414: }
1.124 kristaps 415:
1.184 kristaps 416: word++;
417: esc = mandoc_escape(&word, &seq, &sz);
418: if (ESCAPE_ERROR == esc)
1.224 schwarze 419: continue;
1.124 kristaps 420:
1.184 kristaps 421: switch (esc) {
1.222 schwarze 422: case ESCAPE_UNICODE:
1.229 schwarze 423: uc = mchars_num2uc(seq + 1, sz - 1);
1.192 kristaps 424: break;
1.222 schwarze 425: case ESCAPE_NUMBERED:
1.233 schwarze 426: uc = mchars_num2char(seq, sz);
427: if (uc < 0)
428: continue;
1.184 kristaps 429: break;
1.222 schwarze 430: case ESCAPE_SPECIAL:
1.229 schwarze 431: if (p->enc == TERMENC_ASCII) {
1.254 schwarze 432: cp = mchars_spec2str(seq, sz, &ssz);
1.232 schwarze 433: if (cp != NULL)
1.229 schwarze 434: encode(p, cp, ssz);
435: } else {
1.254 schwarze 436: uc = mchars_spec2cp(seq, sz);
1.230 schwarze 437: if (uc > 0)
438: encode1(p, uc);
1.229 schwarze 439: }
1.233 schwarze 440: continue;
1.222 schwarze 441: case ESCAPE_FONTBOLD:
1.125 kristaps 442: term_fontrepl(p, TERMFONT_BOLD);
1.233 schwarze 443: continue;
1.222 schwarze 444: case ESCAPE_FONTITALIC:
1.125 kristaps 445: term_fontrepl(p, TERMFONT_UNDER);
1.233 schwarze 446: continue;
1.222 schwarze 447: case ESCAPE_FONTBI:
1.209 schwarze 448: term_fontrepl(p, TERMFONT_BI);
1.233 schwarze 449: continue;
1.222 schwarze 450: case ESCAPE_FONT:
451: case ESCAPE_FONTROMAN:
1.125 kristaps 452: term_fontrepl(p, TERMFONT_NONE);
1.233 schwarze 453: continue;
1.222 schwarze 454: case ESCAPE_FONTPREV:
1.125 kristaps 455: term_fontlast(p);
1.233 schwarze 456: continue;
1.222 schwarze 457: case ESCAPE_NOSPACE:
1.248 schwarze 458: if (p->flags & TERMP_BACKAFTER)
459: p->flags &= ~TERMP_BACKAFTER;
460: else if (*word == '\0')
1.237 schwarze 461: p->flags |= (TERMP_NOSPACE | TERMP_NONEWLINE);
1.261 schwarze 462: continue;
463: case ESCAPE_HORIZ:
464: if (a2roffsu(seq, &su, SCALE_EM) == 0)
465: continue;
466: uc = term_hspan(p, &su) / 24;
467: if (uc > 0)
468: while (uc-- > 0)
469: bufferc(p, ASCII_NBRSP);
470: else if (p->col > (size_t)(-uc))
471: p->col += uc;
472: else {
473: uc += p->col;
474: p->col = 0;
475: if (p->offset > (size_t)(-uc)) {
476: p->ti += uc;
477: p->offset += uc;
478: } else {
479: p->ti -= p->offset;
480: p->offset = 0;
481: }
1.262 schwarze 482: }
483: continue;
484: case ESCAPE_HLINE:
485: if (a2roffsu(seq, &su, SCALE_EM) == 0)
486: continue;
487: uc = term_hspan(p, &su) / 24;
488: if (uc <= 0) {
489: if (p->rmargin <= p->offset)
490: continue;
491: lsz = p->rmargin - p->offset;
492: } else
493: lsz = uc;
494: while (sz &&
495: strchr(" %&()*+-./0123456789:<=>", *seq)) {
496: seq++;
497: sz--;
498: }
499: if (sz && strchr("cifMmnPpuv", *seq)) {
500: seq++;
501: sz--;
502: }
503: if (sz == 0)
504: uc = -1;
505: else if (*seq == '\\') {
506: seq++;
507: esc = mandoc_escape(&seq, &cp, &sz);
508: switch (esc) {
509: case ESCAPE_UNICODE:
510: uc = mchars_num2uc(cp + 1, sz - 1);
511: break;
512: case ESCAPE_NUMBERED:
513: uc = mchars_num2char(cp, sz);
514: break;
515: case ESCAPE_SPECIAL:
516: uc = mchars_spec2cp(cp, sz);
517: break;
518: default:
519: uc = -1;
520: break;
521: }
522: } else
523: uc = *seq;
524: if (uc < 0x20 || (uc > 0x7E && uc < 0xA0))
525: uc = '_';
526: if (p->enc == TERMENC_ASCII) {
527: cp = ascii_uc2str(uc);
528: csz = term_strlen(p, cp);
529: ssz = strlen(cp);
530: } else
531: csz = (*p->width)(p, uc);
532: while (lsz >= csz) {
533: if (p->enc == TERMENC_ASCII)
534: encode(p, cp, ssz);
535: else
536: encode1(p, uc);
537: lsz -= csz;
1.261 schwarze 538: }
1.233 schwarze 539: continue;
1.222 schwarze 540: case ESCAPE_SKIPCHAR:
1.248 schwarze 541: p->flags |= TERMP_BACKAFTER;
1.233 schwarze 542: continue;
1.243 schwarze 543: case ESCAPE_OVERSTRIKE:
544: cp = seq + sz;
545: while (seq < cp) {
546: if (*seq == '\\') {
547: mandoc_escape(&seq, NULL, NULL);
548: continue;
549: }
550: encode1(p, *seq++);
1.248 schwarze 551: if (seq < cp) {
552: if (p->flags & TERMP_BACKBEFORE)
553: p->flags |= TERMP_BACKAFTER;
554: else
555: p->flags |= TERMP_BACKBEFORE;
556: }
1.243 schwarze 557: }
1.249 schwarze 558: /* Trim trailing backspace/blank pair. */
1.265 ! schwarze 559: if (p->lastcol > 2 &&
! 560: (p->buf[p->lastcol - 1] == ' ' ||
! 561: p->buf[p->lastcol - 1] == '\t'))
! 562: p->lastcol -= 2;
! 563: if (p->col > p->lastcol)
! 564: p->col = p->lastcol;
1.248 schwarze 565: continue;
1.124 kristaps 566: default:
1.233 schwarze 567: continue;
568: }
569:
570: /*
571: * Common handling for Unicode and numbered
572: * character escape sequences.
573: */
574:
575: if (p->enc == TERMENC_ASCII) {
576: cp = ascii_uc2str(uc);
577: encode(p, cp, strlen(cp));
578: } else {
579: if ((uc < 0x20 && uc != 0x09) ||
580: (uc > 0x7E && uc < 0xA0))
581: uc = 0xFFFD;
582: encode1(p, uc);
1.124 kristaps 583: }
584: }
1.214 schwarze 585: p->flags &= ~TERMP_NBRWORD;
1.65 kristaps 586: }
587:
1.71 kristaps 588: static void
1.210 schwarze 589: adjbuf(struct termp *p, size_t sz)
1.51 kristaps 590: {
591:
1.125 kristaps 592: if (0 == p->maxcols)
593: p->maxcols = 1024;
594: while (sz >= p->maxcols)
595: p->maxcols <<= 2;
596:
1.223 schwarze 597: p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int));
1.51 kristaps 598: }
599:
1.79 kristaps 600: static void
1.125 kristaps 601: bufferc(struct termp *p, char c)
602: {
1.264 schwarze 603: if (p->flags & TERMP_NOBUF) {
604: (*p->letter)(p, c);
605: return;
606: }
1.125 kristaps 607: if (p->col + 1 >= p->maxcols)
608: adjbuf(p, p->col + 1);
1.265 ! schwarze 609: if (p->lastcol <= p->col || (c != ' ' && c != ASCII_NBRSP))
! 610: p->buf[p->col] = c;
! 611: if (p->lastcol < ++p->col)
! 612: p->lastcol = p->col;
1.125 kristaps 613: }
614:
1.194 kristaps 615: /*
616: * See encode().
617: * Do this for a single (probably unicode) value.
618: * Does not check for non-decorated glyphs.
619: */
620: static void
621: encode1(struct termp *p, int c)
622: {
623: enum termfont f;
624:
1.264 schwarze 625: if (p->flags & TERMP_NOBUF) {
626: (*p->letter)(p, c);
627: return;
628: }
629:
1.248 schwarze 630: if (p->col + 7 >= p->maxcols)
631: adjbuf(p, p->col + 7);
1.194 kristaps 632:
1.255 schwarze 633: f = (c == ASCII_HYPH || c > 127 || isgraph(c)) ?
1.248 schwarze 634: p->fontq[p->fonti] : TERMFONT_NONE;
1.194 kristaps 635:
1.248 schwarze 636: if (p->flags & TERMP_BACKBEFORE) {
1.258 schwarze 637: if (p->buf[p->col - 1] == ' ' || p->buf[p->col - 1] == '\t')
1.249 schwarze 638: p->col--;
639: else
640: p->buf[p->col++] = 8;
1.248 schwarze 641: p->flags &= ~TERMP_BACKBEFORE;
642: }
1.209 schwarze 643: if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
1.194 kristaps 644: p->buf[p->col++] = '_';
1.209 schwarze 645: p->buf[p->col++] = 8;
646: }
647: if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
648: if (ASCII_HYPH == c)
649: p->buf[p->col++] = '-';
650: else
651: p->buf[p->col++] = c;
652: p->buf[p->col++] = 8;
653: }
1.265 ! schwarze 654: if (p->lastcol <= p->col || (c != ' ' && c != ASCII_NBRSP))
! 655: p->buf[p->col] = c;
! 656: if (p->lastcol < ++p->col)
! 657: p->lastcol = p->col;
1.248 schwarze 658: if (p->flags & TERMP_BACKAFTER) {
659: p->flags |= TERMP_BACKBEFORE;
660: p->flags &= ~TERMP_BACKAFTER;
661: }
1.194 kristaps 662: }
663:
1.125 kristaps 664: static void
665: encode(struct termp *p, const char *word, size_t sz)
666: {
1.210 schwarze 667: size_t i;
1.264 schwarze 668:
669: if (p->flags & TERMP_NOBUF) {
670: for (i = 0; i < sz; i++)
671: (*p->letter)(p, word[i]);
672: return;
673: }
1.188 kristaps 674:
1.248 schwarze 675: if (p->col + 2 + (sz * 5) >= p->maxcols)
676: adjbuf(p, p->col + 2 + (sz * 5));
1.165 kristaps 677:
1.210 schwarze 678: for (i = 0; i < sz; i++) {
1.209 schwarze 679: if (ASCII_HYPH == word[i] ||
680: isgraph((unsigned char)word[i]))
681: encode1(p, word[i]);
1.259 schwarze 682: else {
1.265 ! schwarze 683: if (p->lastcol <= p->col ||
! 684: (word[i] != ' ' && word[i] != ASCII_NBRSP))
! 685: p->buf[p->col] = word[i];
! 686: p->col++;
1.259 schwarze 687:
688: /*
689: * Postpone the effect of \z while handling
690: * an overstrike sequence from ascii_uc2str().
691: */
692:
693: if (word[i] == '\b' &&
694: (p->flags & TERMP_BACKBEFORE)) {
695: p->flags &= ~TERMP_BACKBEFORE;
696: p->flags |= TERMP_BACKAFTER;
697: }
698: }
1.79 kristaps 699: }
1.265 ! schwarze 700: if (p->lastcol < p->col)
! 701: p->lastcol = p->col;
1.219 schwarze 702: }
703:
704: void
705: term_setwidth(struct termp *p, const char *wstr)
706: {
707: struct roffsu su;
1.247 schwarze 708: int iop, width;
1.219 schwarze 709:
1.220 schwarze 710: iop = 0;
711: width = 0;
1.219 schwarze 712: if (NULL != wstr) {
713: switch (*wstr) {
1.222 schwarze 714: case '+':
1.219 schwarze 715: iop = 1;
716: wstr++;
717: break;
1.222 schwarze 718: case '-':
1.219 schwarze 719: iop = -1;
720: wstr++;
721: break;
722: default:
723: break;
724: }
1.220 schwarze 725: if (a2roffsu(wstr, &su, SCALE_MAX))
726: width = term_hspan(p, &su);
727: else
1.219 schwarze 728: iop = 0;
729: }
730: (*p->setwidth)(p, iop, width);
1.79 kristaps 731: }
1.106 kristaps 732:
1.107 kristaps 733: size_t
1.149 kristaps 734: term_len(const struct termp *p, size_t sz)
735: {
736:
1.252 schwarze 737: return (*p->width)(p, ' ') * sz;
1.149 kristaps 738: }
739:
1.203 schwarze 740: static size_t
741: cond_width(const struct termp *p, int c, int *skip)
742: {
743:
744: if (*skip) {
745: (*skip) = 0;
1.252 schwarze 746: return 0;
1.203 schwarze 747: } else
1.252 schwarze 748: return (*p->width)(p, c);
1.203 schwarze 749: }
1.149 kristaps 750:
751: size_t
752: term_strlen(const struct termp *p, const char *cp)
753: {
1.184 kristaps 754: size_t sz, rsz, i;
1.233 schwarze 755: int ssz, skip, uc;
1.171 kristaps 756: const char *seq, *rhs;
1.196 kristaps 757: enum mandoc_esc esc;
1.216 schwarze 758: static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
759: ASCII_BREAK, '\0' };
1.171 kristaps 760:
1.184 kristaps 761: /*
762: * Account for escaped sequences within string length
763: * calculations. This follows the logic in term_word() as we
764: * must calculate the width of produced strings.
765: */
766:
767: sz = 0;
1.203 schwarze 768: skip = 0;
1.189 kristaps 769: while ('\0' != *cp) {
770: rsz = strcspn(cp, rej);
771: for (i = 0; i < rsz; i++)
1.203 schwarze 772: sz += cond_width(p, *cp++, &skip);
1.189 kristaps 773:
1.184 kristaps 774: switch (*cp) {
1.222 schwarze 775: case '\\':
1.189 kristaps 776: cp++;
1.196 kristaps 777: esc = mandoc_escape(&cp, &seq, &ssz);
778: if (ESCAPE_ERROR == esc)
1.224 schwarze 779: continue;
1.196 kristaps 780:
781: rhs = NULL;
782:
783: switch (esc) {
1.222 schwarze 784: case ESCAPE_UNICODE:
1.234 schwarze 785: uc = mchars_num2uc(seq + 1, ssz - 1);
1.194 kristaps 786: break;
1.222 schwarze 787: case ESCAPE_NUMBERED:
1.233 schwarze 788: uc = mchars_num2char(seq, ssz);
789: if (uc < 0)
790: continue;
1.171 kristaps 791: break;
1.222 schwarze 792: case ESCAPE_SPECIAL:
1.233 schwarze 793: if (p->enc == TERMENC_ASCII) {
1.254 schwarze 794: rhs = mchars_spec2str(seq, ssz, &rsz);
1.233 schwarze 795: if (rhs != NULL)
796: break;
797: } else {
1.254 schwarze 798: uc = mchars_spec2cp(seq, ssz);
1.233 schwarze 799: if (uc > 0)
800: sz += cond_width(p, uc, &skip);
1.229 schwarze 801: }
1.233 schwarze 802: continue;
1.222 schwarze 803: case ESCAPE_SKIPCHAR:
1.203 schwarze 804: skip = 1;
1.243 schwarze 805: continue;
806: case ESCAPE_OVERSTRIKE:
807: rsz = 0;
808: rhs = seq + ssz;
809: while (seq < rhs) {
810: if (*seq == '\\') {
811: mandoc_escape(&seq, NULL, NULL);
812: continue;
813: }
814: i = (*p->width)(p, *seq++);
815: if (rsz < i)
816: rsz = i;
817: }
818: sz += rsz;
1.233 schwarze 819: continue;
1.171 kristaps 820: default:
1.233 schwarze 821: continue;
1.171 kristaps 822: }
1.149 kristaps 823:
1.233 schwarze 824: /*
825: * Common handling for Unicode and numbered
826: * character escape sequences.
827: */
828:
829: if (rhs == NULL) {
830: if (p->enc == TERMENC_ASCII) {
831: rhs = ascii_uc2str(uc);
832: rsz = strlen(rhs);
833: } else {
834: if ((uc < 0x20 && uc != 0x09) ||
835: (uc > 0x7E && uc < 0xA0))
836: uc = 0xFFFD;
837: sz += cond_width(p, uc, &skip);
838: continue;
839: }
840: }
1.184 kristaps 841:
1.203 schwarze 842: if (skip) {
843: skip = 0;
844: break;
845: }
1.233 schwarze 846:
847: /*
848: * Common handling for all escape sequences
849: * printing more than one character.
850: */
1.203 schwarze 851:
1.184 kristaps 852: for (i = 0; i < rsz; i++)
853: sz += (*p->width)(p, *rhs++);
854: break;
1.222 schwarze 855: case ASCII_NBRSP:
1.203 schwarze 856: sz += cond_width(p, ' ', &skip);
1.176 kristaps 857: cp++;
1.184 kristaps 858: break;
1.222 schwarze 859: case ASCII_HYPH:
1.203 schwarze 860: sz += cond_width(p, '-', &skip);
1.176 kristaps 861: cp++;
1.184 kristaps 862: break;
863: default:
864: break;
865: }
1.189 kristaps 866: }
1.149 kristaps 867:
1.252 schwarze 868: return sz;
1.149 kristaps 869: }
870:
1.240 schwarze 871: int
1.149 kristaps 872: term_vspan(const struct termp *p, const struct roffsu *su)
1.106 kristaps 873: {
874: double r;
1.241 schwarze 875: int ri;
1.106 kristaps 876:
1.107 kristaps 877: switch (su->unit) {
1.239 schwarze 878: case SCALE_BU:
879: r = su->scale / 40.0;
880: break;
1.222 schwarze 881: case SCALE_CM:
1.239 schwarze 882: r = su->scale * 6.0 / 2.54;
883: break;
884: case SCALE_FS:
885: r = su->scale * 65536.0 / 40.0;
1.106 kristaps 886: break;
1.222 schwarze 887: case SCALE_IN:
1.225 schwarze 888: r = su->scale * 6.0;
1.106 kristaps 889: break;
1.239 schwarze 890: case SCALE_MM:
891: r = su->scale * 0.006;
892: break;
1.222 schwarze 893: case SCALE_PC:
1.107 kristaps 894: r = su->scale;
1.106 kristaps 895: break;
1.222 schwarze 896: case SCALE_PT:
1.239 schwarze 897: r = su->scale / 12.0;
1.106 kristaps 898: break;
1.239 schwarze 899: case SCALE_EN:
900: case SCALE_EM:
901: r = su->scale * 0.6;
1.106 kristaps 902: break;
1.222 schwarze 903: case SCALE_VS:
1.107 kristaps 904: r = su->scale;
1.106 kristaps 905: break;
906: default:
1.239 schwarze 907: abort();
1.106 kristaps 908: }
1.241 schwarze 909: ri = r > 0.0 ? r + 0.4995 : r - 0.4995;
1.252 schwarze 910: return ri < 66 ? ri : 1;
1.106 kristaps 911: }
912:
1.247 schwarze 913: /*
914: * Convert a scaling width to basic units, rounding down.
915: */
1.240 schwarze 916: int
1.149 kristaps 917: term_hspan(const struct termp *p, const struct roffsu *su)
1.106 kristaps 918: {
1.108 kristaps 919:
1.252 schwarze 920: return (*p->hspan)(p, su);
1.106 kristaps 921: }
CVSweb