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