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