Annotation of mandoc/term.c, Revision 1.232
1.232 ! schwarze 1: /* $Id: term.c,v 1.231 2014/10/28 17:36:19 schwarze Exp $ */
1.1 kristaps 2: /*
1.198 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.216 schwarze 4: * Copyright (c) 2010-2014 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.74 kristaps 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
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.142 kristaps 46: free(p);
47: }
48:
49: void
1.222 schwarze 50: term_begin(struct termp *p, term_margin head,
1.142 kristaps 51: term_margin foot, const void *arg)
52: {
53:
54: p->headf = head;
55: p->footf = foot;
56: p->argf = arg;
1.146 kristaps 57: (*p->begin)(p);
1.142 kristaps 58: }
59:
60: void
61: term_end(struct termp *p)
62: {
63:
1.146 kristaps 64: (*p->end)(p);
1.14 kristaps 65: }
66:
1.71 kristaps 67: /*
1.221 schwarze 68: * Flush a chunk of text. By default, break the output line each time
69: * the right margin is reached, and continue output on the next line
70: * at the same offset as the chunk itself. By default, also break the
71: * output line at the end of the chunk.
1.130 kristaps 72: * The following flags may be specified:
1.71 kristaps 73: *
1.221 schwarze 74: * - TERMP_NOBREAK: Do not break the output line at the right margin,
75: * but only at the max right margin. Also, do not break the output
76: * line at the end of the chunk, such that the next call can pad to
77: * the next column. However, if less than p->trailspace blanks,
78: * which can be 0, 1, or 2, remain to the right margin, the line
79: * will be broken.
80: * - TERMP_BRIND: If the chunk does not fit and the output line has
81: * to be broken, start the next line at the right margin instead
82: * of at the offset. Used together with TERMP_NOBREAK for the tags
83: * in various kinds of tagged lists.
84: * - TERMP_DANGLE: Do not break the output line at the right margin,
85: * append the next chunk after it even if this one is too long.
86: * To be used together with TERMP_NOBREAK.
87: * - TERMP_HANG: Like TERMP_DANGLE, and also suppress padding before
88: * the next chunk if this column is not full.
1.71 kristaps 89: */
90: void
91: term_flushln(struct termp *p)
1.53 kristaps 92: {
1.210 schwarze 93: size_t i; /* current input position in p->buf */
1.205 schwarze 94: int ntab; /* number of tabs to prepend */
1.114 kristaps 95: size_t vis; /* current visual position on output */
96: size_t vbl; /* number of blanks to prepend to output */
1.136 schwarze 97: size_t vend; /* end of word visual position on output */
1.114 kristaps 98: size_t bp; /* visual right border position */
1.172 schwarze 99: size_t dv; /* temporary for visual pos calculations */
1.210 schwarze 100: size_t j; /* temporary loop index for p->buf */
101: size_t jhy; /* last hyph before overflow w/r/t j */
1.152 kristaps 102: size_t maxvis; /* output position of visible boundary */
103: size_t mmax; /* used in calculating bp */
1.53 kristaps 104:
1.71 kristaps 105: /*
106: * First, establish the maximum columns of "visible" content.
107: * This is usually the difference between the right-margin and
108: * an indentation, but can be, for tagged lists or columns, a
1.212 schwarze 109: * small set of values.
110: *
111: * The following unsigned-signed subtractions look strange,
112: * but they are actually correct. If the int p->overstep
113: * is negative, it gets sign extended. Subtracting that
114: * very large size_t effectively adds a small number to dv.
1.71 kristaps 115: */
1.175 kristaps 116: assert (p->rmargin >= p->offset);
1.174 schwarze 117: dv = p->rmargin - p->offset;
118: maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
119: dv = p->maxrmargin - p->offset;
120: mmax = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
1.92 kristaps 121:
1.71 kristaps 122: bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
1.115 kristaps 123:
1.136 schwarze 124: /*
1.200 schwarze 125: * Calculate the required amount of padding.
1.136 schwarze 126: */
1.200 schwarze 127: vbl = p->offset + p->overstep > p->viscol ?
128: p->offset + p->overstep - p->viscol : 0;
1.136 schwarze 129:
1.174 schwarze 130: vis = vend = 0;
131: i = 0;
1.115 kristaps 132:
1.188 kristaps 133: while (i < p->col) {
1.71 kristaps 134: /*
1.154 kristaps 135: * Handle literal tab characters: collapse all
136: * subsequent tabs into a single huge set of spaces.
1.138 schwarze 137: */
1.205 schwarze 138: ntab = 0;
1.188 kristaps 139: while (i < p->col && '\t' == p->buf[i]) {
1.154 kristaps 140: vend = (vis / p->tabwidth + 1) * p->tabwidth;
1.138 schwarze 141: vbl += vend - vis;
142: vis = vend;
1.205 schwarze 143: ntab++;
1.169 schwarze 144: i++;
1.138 schwarze 145: }
146:
147: /*
1.71 kristaps 148: * Count up visible word characters. Control sequences
149: * (starting with the CSI) aren't counted. A space
150: * generates a non-printing word, which is valid (the
151: * space is printed according to regular spacing rules).
152: */
153:
1.188 kristaps 154: for (j = i, jhy = 0; j < p->col; j++) {
1.208 schwarze 155: if (' ' == p->buf[j] || '\t' == p->buf[j])
1.71 kristaps 156: break;
1.154 kristaps 157:
158: /* Back over the the last printed character. */
159: if (8 == p->buf[j]) {
1.153 kristaps 160: assert(j);
161: vend -= (*p->width)(p, p->buf[j - 1]);
1.154 kristaps 162: continue;
1.153 kristaps 163: }
1.154 kristaps 164:
165: /* Regular word. */
166: /* Break at the hyphen point if we overrun. */
1.222 schwarze 167: if (vend > vis && vend < bp &&
1.216 schwarze 168: (ASCII_HYPH == p->buf[j] ||
169: ASCII_BREAK == p->buf[j]))
1.154 kristaps 170: jhy = j;
171:
1.217 schwarze 172: /*
173: * Hyphenation now decided, put back a real
174: * hyphen such that we get the correct width.
175: */
176: if (ASCII_HYPH == p->buf[j])
177: p->buf[j] = '-';
178:
1.154 kristaps 179: vend += (*p->width)(p, p->buf[j]);
1.71 kristaps 180: }
1.53 kristaps 181:
1.71 kristaps 182: /*
1.81 kristaps 183: * Find out whether we would exceed the right margin.
1.136 schwarze 184: * If so, break to the next line.
1.81 kristaps 185: */
1.140 kristaps 186: if (vend > bp && 0 == jhy && vis > 0) {
1.136 schwarze 187: vend -= vis;
1.146 kristaps 188: (*p->endline)(p);
1.201 schwarze 189: p->viscol = 0;
1.221 schwarze 190: if (TERMP_BRIND & p->flags) {
1.201 schwarze 191: vbl = p->rmargin;
1.136 schwarze 192: vend += p->rmargin - p->offset;
1.201 schwarze 193: } else
1.136 schwarze 194: vbl = p->offset;
1.205 schwarze 195:
196: /* use pending tabs on the new line */
197:
198: if (0 < ntab)
199: vbl += ntab * p->tabwidth;
1.130 kristaps 200:
1.212 schwarze 201: /*
202: * Remove the p->overstep width.
203: * Again, if p->overstep is negative,
204: * sign extension does the right thing.
205: */
1.130 kristaps 206:
1.174 schwarze 207: bp += (size_t)p->overstep;
1.129 kristaps 208: p->overstep = 0;
1.71 kristaps 209: }
1.138 schwarze 210:
1.130 kristaps 211: /* Write out the [remaining] word. */
1.188 kristaps 212: for ( ; i < p->col; i++) {
1.140 kristaps 213: if (vend > bp && jhy > 0 && i > jhy)
214: break;
1.138 schwarze 215: if ('\t' == p->buf[i])
216: break;
1.136 schwarze 217: if (' ' == p->buf[i]) {
1.164 kristaps 218: j = i;
1.228 kristaps 219: while (i < p->col && ' ' == p->buf[i])
1.136 schwarze 220: i++;
1.210 schwarze 221: dv = (i - j) * (*p->width)(p, ' ');
1.172 schwarze 222: vbl += dv;
223: vend += dv;
1.71 kristaps 224: break;
1.136 schwarze 225: }
226: if (ASCII_NBRSP == p->buf[i]) {
1.153 kristaps 227: vbl += (*p->width)(p, ' ');
1.136 schwarze 228: continue;
229: }
1.216 schwarze 230: if (ASCII_BREAK == p->buf[i])
231: continue;
1.130 kristaps 232:
1.136 schwarze 233: /*
234: * Now we definitely know there will be
235: * printable characters to output,
236: * so write preceding white space now.
237: */
238: if (vbl) {
1.146 kristaps 239: (*p->advance)(p, vbl);
1.139 schwarze 240: p->viscol += vbl;
1.136 schwarze 241: vbl = 0;
1.200 schwarze 242: }
243:
244: (*p->letter)(p, p->buf[i]);
245: if (8 == p->buf[i])
246: p->viscol -= (*p->width)(p, p->buf[i-1]);
1.222 schwarze 247: else
1.153 kristaps 248: p->viscol += (*p->width)(p, p->buf[i]);
1.136 schwarze 249: }
250: vis = vend;
1.71 kristaps 251: }
1.168 schwarze 252:
253: /*
254: * If there was trailing white space, it was not printed;
255: * so reset the cursor position accordingly.
256: */
1.200 schwarze 257: if (vis)
258: vis -= vbl;
1.111 kristaps 259:
1.91 kristaps 260: p->col = 0;
1.129 kristaps 261: p->overstep = 0;
1.15 kristaps 262:
1.91 kristaps 263: if ( ! (TERMP_NOBREAK & p->flags)) {
1.139 schwarze 264: p->viscol = 0;
1.146 kristaps 265: (*p->endline)(p);
1.15 kristaps 266: return;
1.71 kristaps 267: }
1.15 kristaps 268:
1.91 kristaps 269: if (TERMP_HANG & p->flags) {
1.211 schwarze 270: p->overstep = (int)(vis - maxvis +
1.222 schwarze 271: p->trailspace * (*p->width)(p, ' '));
1.91 kristaps 272:
273: /*
1.92 kristaps 274: * If we have overstepped the margin, temporarily move
275: * it to the right and flag the rest of the line to be
276: * shorter.
1.212 schwarze 277: * If there is a request to keep the columns together,
278: * allow negative overstep when the column is not full.
1.91 kristaps 279: */
1.212 schwarze 280: if (p->trailspace && p->overstep < 0)
1.129 kristaps 281: p->overstep = 0;
1.200 schwarze 282: return;
1.91 kristaps 283:
284: } else if (TERMP_DANGLE & p->flags)
285: return;
1.15 kristaps 286:
1.200 schwarze 287: /* If the column was overrun, break the line. */
1.211 schwarze 288: if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
1.146 kristaps 289: (*p->endline)(p);
1.200 schwarze 290: p->viscol = 0;
1.91 kristaps 291: }
1.15 kristaps 292: }
293:
1.222 schwarze 294: /*
1.71 kristaps 295: * A newline only breaks an existing line; it won't assert vertical
296: * space. All data in the output buffer is flushed prior to the newline
297: * assertion.
298: */
299: void
300: term_newln(struct termp *p)
1.15 kristaps 301: {
302:
1.71 kristaps 303: p->flags |= TERMP_NOSPACE;
1.200 schwarze 304: if (p->col || p->viscol)
305: term_flushln(p);
1.16 kristaps 306: }
307:
1.71 kristaps 308: /*
309: * Asserts a vertical space (a full, empty line-break between lines).
310: * Note that if used twice, this will cause two blank spaces and so on.
311: * All data in the output buffer is flushed prior to the newline
312: * assertion.
313: */
314: void
315: term_vspace(struct termp *p)
1.16 kristaps 316: {
317:
1.62 kristaps 318: term_newln(p);
1.139 schwarze 319: p->viscol = 0;
1.202 schwarze 320: if (0 < p->skipvsp)
321: p->skipvsp--;
322: else
323: (*p->endline)(p);
1.16 kristaps 324: }
325:
1.125 kristaps 326: void
327: term_fontlast(struct termp *p)
328: {
329: enum termfont f;
330:
331: f = p->fontl;
332: p->fontl = p->fontq[p->fonti];
333: p->fontq[p->fonti] = f;
334: }
335:
336: void
337: term_fontrepl(struct termp *p, enum termfont f)
338: {
339:
340: p->fontl = p->fontq[p->fonti];
341: p->fontq[p->fonti] = f;
342: }
343:
344: void
345: term_fontpush(struct termp *p, enum termfont f)
346: {
347:
348: assert(p->fonti + 1 < 10);
349: p->fontl = p->fontq[p->fonti];
350: p->fontq[++p->fonti] = f;
351: }
352:
353: const void *
354: term_fontq(struct termp *p)
355: {
356:
357: return(&p->fontq[p->fonti]);
358: }
359:
360: enum termfont
361: term_fonttop(struct termp *p)
362: {
363:
364: return(p->fontq[p->fonti]);
365: }
366:
367: void
368: term_fontpopq(struct termp *p, const void *key)
369: {
370:
1.206 schwarze 371: while (p->fonti >= 0 && key < (void *)(p->fontq + p->fonti))
1.125 kristaps 372: p->fonti--;
373: assert(p->fonti >= 0);
374: }
1.94 kristaps 375:
1.125 kristaps 376: void
377: term_fontpop(struct termp *p)
378: {
379:
380: assert(p->fonti);
381: p->fonti--;
1.17 kristaps 382: }
383:
1.71 kristaps 384: /*
385: * Handle pwords, partial words, which may be either a single word or a
386: * phrase that cannot be broken down (such as a literal string). This
387: * handles word styling.
388: */
1.86 kristaps 389: void
390: term_word(struct termp *p, const char *word)
1.65 kristaps 391: {
1.214 schwarze 392: const char nbrsp[2] = { ASCII_NBRSP, 0 };
1.191 kristaps 393: const char *seq, *cp;
394: char c;
1.194 kristaps 395: int sz, uc;
1.124 kristaps 396: size_t ssz;
1.184 kristaps 397: enum mandoc_esc esc;
1.100 kristaps 398:
1.133 kristaps 399: if ( ! (TERMP_NOSPACE & p->flags)) {
1.151 schwarze 400: if ( ! (TERMP_KEEP & p->flags)) {
1.133 kristaps 401: bufferc(p, ' ');
1.151 schwarze 402: if (TERMP_SENTENCE & p->flags)
403: bufferc(p, ' ');
404: } else
405: bufferc(p, ASCII_NBRSP);
1.133 kristaps 406: }
1.207 schwarze 407: if (TERMP_PREKEEP & p->flags)
408: p->flags |= TERMP_KEEP;
1.65 kristaps 409:
1.71 kristaps 410: if ( ! (p->flags & TERMP_NONOSPACE))
411: p->flags &= ~TERMP_NOSPACE;
1.166 kristaps 412: else
413: p->flags |= TERMP_NOSPACE;
1.133 kristaps 414:
1.213 schwarze 415: p->flags &= ~TERMP_SENTENCE;
1.65 kristaps 416:
1.184 kristaps 417: while ('\0' != *word) {
1.203 schwarze 418: if ('\\' != *word) {
419: if (TERMP_SKIPCHAR & p->flags) {
420: p->flags &= ~TERMP_SKIPCHAR;
421: word++;
422: continue;
423: }
1.214 schwarze 424: if (TERMP_NBRWORD & p->flags) {
425: if (' ' == *word) {
426: encode(p, nbrsp, 1);
427: word++;
428: continue;
429: }
430: ssz = strcspn(word, "\\ ");
431: } else
432: ssz = strcspn(word, "\\");
1.162 kristaps 433: encode(p, word, ssz);
1.203 schwarze 434: word += (int)ssz;
1.124 kristaps 435: continue;
1.203 schwarze 436: }
1.124 kristaps 437:
1.184 kristaps 438: word++;
439: esc = mandoc_escape(&word, &seq, &sz);
440: if (ESCAPE_ERROR == esc)
1.224 schwarze 441: continue;
1.124 kristaps 442:
1.184 kristaps 443: switch (esc) {
1.222 schwarze 444: case ESCAPE_UNICODE:
1.229 schwarze 445: uc = mchars_num2uc(seq + 1, sz - 1);
446: if (p->enc == TERMENC_ASCII) {
447: cp = ascii_uc2str(uc);
448: encode(p, cp, strlen(cp));
449: } else
450: encode1(p, uc);
1.192 kristaps 451: break;
1.222 schwarze 452: case ESCAPE_NUMBERED:
1.196 kristaps 453: c = mchars_num2char(seq, sz);
454: if ('\0' != c)
1.191 kristaps 455: encode(p, &c, 1);
1.184 kristaps 456: break;
1.222 schwarze 457: case ESCAPE_SPECIAL:
1.229 schwarze 458: if (p->enc == TERMENC_ASCII) {
459: cp = mchars_spec2str(p->symtab,
460: seq, sz, &ssz);
1.232 ! schwarze 461: if (cp != NULL)
1.229 schwarze 462: encode(p, cp, ssz);
463: } else {
464: uc = mchars_spec2cp(p->symtab, seq, sz);
1.230 schwarze 465: if (uc > 0)
466: encode1(p, uc);
1.229 schwarze 467: }
1.124 kristaps 468: break;
1.222 schwarze 469: case ESCAPE_FONTBOLD:
1.125 kristaps 470: term_fontrepl(p, TERMFONT_BOLD);
1.124 kristaps 471: break;
1.222 schwarze 472: case ESCAPE_FONTITALIC:
1.125 kristaps 473: term_fontrepl(p, TERMFONT_UNDER);
1.124 kristaps 474: break;
1.222 schwarze 475: case ESCAPE_FONTBI:
1.209 schwarze 476: term_fontrepl(p, TERMFONT_BI);
477: break;
1.222 schwarze 478: case ESCAPE_FONT:
1.195 kristaps 479: /* FALLTHROUGH */
1.222 schwarze 480: case ESCAPE_FONTROMAN:
1.125 kristaps 481: term_fontrepl(p, TERMFONT_NONE);
1.124 kristaps 482: break;
1.222 schwarze 483: case ESCAPE_FONTPREV:
1.125 kristaps 484: term_fontlast(p);
1.124 kristaps 485: break;
1.222 schwarze 486: case ESCAPE_NOSPACE:
1.203 schwarze 487: if (TERMP_SKIPCHAR & p->flags)
488: p->flags &= ~TERMP_SKIPCHAR;
489: else if ('\0' == *word)
1.184 kristaps 490: p->flags |= TERMP_NOSPACE;
491: break;
1.222 schwarze 492: case ESCAPE_SKIPCHAR:
1.203 schwarze 493: p->flags |= TERMP_SKIPCHAR;
494: break;
1.124 kristaps 495: default:
496: break;
497: }
498: }
1.214 schwarze 499: p->flags &= ~TERMP_NBRWORD;
1.65 kristaps 500: }
501:
1.71 kristaps 502: static void
1.210 schwarze 503: adjbuf(struct termp *p, size_t sz)
1.51 kristaps 504: {
505:
1.125 kristaps 506: if (0 == p->maxcols)
507: p->maxcols = 1024;
508: while (sz >= p->maxcols)
509: p->maxcols <<= 2;
510:
1.223 schwarze 511: p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int));
1.51 kristaps 512: }
513:
1.79 kristaps 514: static void
1.125 kristaps 515: bufferc(struct termp *p, char c)
516: {
517:
518: if (p->col + 1 >= p->maxcols)
519: adjbuf(p, p->col + 1);
520:
1.188 kristaps 521: p->buf[p->col++] = c;
1.125 kristaps 522: }
523:
1.194 kristaps 524: /*
525: * See encode().
526: * Do this for a single (probably unicode) value.
527: * Does not check for non-decorated glyphs.
528: */
529: static void
530: encode1(struct termp *p, int c)
531: {
532: enum termfont f;
533:
1.203 schwarze 534: if (TERMP_SKIPCHAR & p->flags) {
535: p->flags &= ~TERMP_SKIPCHAR;
536: return;
537: }
538:
1.209 schwarze 539: if (p->col + 6 >= p->maxcols)
540: adjbuf(p, p->col + 6);
1.194 kristaps 541:
542: f = term_fonttop(p);
543:
1.209 schwarze 544: if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
1.194 kristaps 545: p->buf[p->col++] = '_';
1.209 schwarze 546: p->buf[p->col++] = 8;
547: }
548: if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
549: if (ASCII_HYPH == c)
550: p->buf[p->col++] = '-';
551: else
552: p->buf[p->col++] = c;
553: p->buf[p->col++] = 8;
554: }
1.194 kristaps 555: p->buf[p->col++] = c;
556: }
557:
1.125 kristaps 558: static void
559: encode(struct termp *p, const char *word, size_t sz)
560: {
1.210 schwarze 561: size_t i;
1.188 kristaps 562:
1.203 schwarze 563: if (TERMP_SKIPCHAR & p->flags) {
564: p->flags &= ~TERMP_SKIPCHAR;
565: return;
566: }
567:
1.125 kristaps 568: /*
569: * Encode and buffer a string of characters. If the current
570: * font mode is unset, buffer directly, else encode then buffer
571: * character by character.
572: */
573:
1.209 schwarze 574: if (TERMFONT_NONE == term_fonttop(p)) {
1.222 schwarze 575: if (p->col + sz >= p->maxcols)
1.210 schwarze 576: adjbuf(p, p->col + sz);
577: for (i = 0; i < sz; i++)
1.188 kristaps 578: p->buf[p->col++] = word[i];
1.125 kristaps 579: return;
580: }
581:
1.165 kristaps 582: /* Pre-buffer, assuming worst-case. */
583:
1.210 schwarze 584: if (p->col + 1 + (sz * 5) >= p->maxcols)
585: adjbuf(p, p->col + 1 + (sz * 5));
1.165 kristaps 586:
1.210 schwarze 587: for (i = 0; i < sz; i++) {
1.209 schwarze 588: if (ASCII_HYPH == word[i] ||
589: isgraph((unsigned char)word[i]))
590: encode1(p, word[i]);
1.125 kristaps 591: else
1.188 kristaps 592: p->buf[p->col++] = word[i];
1.79 kristaps 593: }
1.219 schwarze 594: }
595:
596: void
597: term_setwidth(struct termp *p, const char *wstr)
598: {
599: struct roffsu su;
600: size_t width;
601: int iop;
602:
1.220 schwarze 603: iop = 0;
604: width = 0;
1.219 schwarze 605: if (NULL != wstr) {
606: switch (*wstr) {
1.222 schwarze 607: case '+':
1.219 schwarze 608: iop = 1;
609: wstr++;
610: break;
1.222 schwarze 611: case '-':
1.219 schwarze 612: iop = -1;
613: wstr++;
614: break;
615: default:
616: break;
617: }
1.220 schwarze 618: if (a2roffsu(wstr, &su, SCALE_MAX))
619: width = term_hspan(p, &su);
620: else
1.219 schwarze 621: iop = 0;
622: }
623: (*p->setwidth)(p, iop, width);
1.79 kristaps 624: }
1.106 kristaps 625:
1.107 kristaps 626: size_t
1.149 kristaps 627: term_len(const struct termp *p, size_t sz)
628: {
629:
630: return((*p->width)(p, ' ') * sz);
631: }
632:
1.203 schwarze 633: static size_t
634: cond_width(const struct termp *p, int c, int *skip)
635: {
636:
637: if (*skip) {
638: (*skip) = 0;
639: return(0);
640: } else
641: return((*p->width)(p, c));
642: }
1.149 kristaps 643:
644: size_t
645: term_strlen(const struct termp *p, const char *cp)
646: {
1.184 kristaps 647: size_t sz, rsz, i;
1.203 schwarze 648: int ssz, skip, c;
1.171 kristaps 649: const char *seq, *rhs;
1.196 kristaps 650: enum mandoc_esc esc;
1.216 schwarze 651: static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
652: ASCII_BREAK, '\0' };
1.171 kristaps 653:
1.184 kristaps 654: /*
655: * Account for escaped sequences within string length
656: * calculations. This follows the logic in term_word() as we
657: * must calculate the width of produced strings.
658: */
659:
660: sz = 0;
1.203 schwarze 661: skip = 0;
1.189 kristaps 662: while ('\0' != *cp) {
663: rsz = strcspn(cp, rej);
664: for (i = 0; i < rsz; i++)
1.203 schwarze 665: sz += cond_width(p, *cp++, &skip);
1.189 kristaps 666:
1.184 kristaps 667: switch (*cp) {
1.222 schwarze 668: case '\\':
1.189 kristaps 669: cp++;
1.196 kristaps 670: esc = mandoc_escape(&cp, &seq, &ssz);
671: if (ESCAPE_ERROR == esc)
1.224 schwarze 672: continue;
1.196 kristaps 673:
674: rhs = NULL;
675:
676: switch (esc) {
1.222 schwarze 677: case ESCAPE_UNICODE:
1.229 schwarze 678: c = mchars_num2uc(seq + 1, sz - 1);
679: if (p->enc == TERMENC_ASCII) {
680: rhs = ascii_uc2str(c);
681: rsz = strlen(rhs);
682: } else
683: sz += cond_width(p, c, &skip);
1.194 kristaps 684: break;
1.222 schwarze 685: case ESCAPE_NUMBERED:
1.194 kristaps 686: c = mchars_num2char(seq, ssz);
1.190 kristaps 687: if ('\0' != c)
1.203 schwarze 688: sz += cond_width(p, c, &skip);
1.171 kristaps 689: break;
1.222 schwarze 690: case ESCAPE_SPECIAL:
1.232 ! schwarze 691: if (p->enc == TERMENC_ASCII)
1.229 schwarze 692: rhs = mchars_spec2str(p->symtab,
693: seq, ssz, &rsz);
1.232 ! schwarze 694: else {
1.229 schwarze 695: c = mchars_spec2cp(p->symtab,
696: seq, ssz);
1.230 schwarze 697: if (c > 0)
698: sz += cond_width(p, c, &skip);
1.229 schwarze 699: }
1.171 kristaps 700: break;
1.222 schwarze 701: case ESCAPE_SKIPCHAR:
1.203 schwarze 702: skip = 1;
703: break;
1.171 kristaps 704: default:
705: break;
706: }
1.149 kristaps 707:
1.184 kristaps 708: if (NULL == rhs)
709: break;
710:
1.203 schwarze 711: if (skip) {
712: skip = 0;
713: break;
714: }
715:
1.184 kristaps 716: for (i = 0; i < rsz; i++)
717: sz += (*p->width)(p, *rhs++);
718: break;
1.222 schwarze 719: case ASCII_NBRSP:
1.203 schwarze 720: sz += cond_width(p, ' ', &skip);
1.176 kristaps 721: cp++;
1.184 kristaps 722: break;
1.222 schwarze 723: case ASCII_HYPH:
1.203 schwarze 724: sz += cond_width(p, '-', &skip);
1.176 kristaps 725: cp++;
1.216 schwarze 726: /* FALLTHROUGH */
1.222 schwarze 727: case ASCII_BREAK:
1.184 kristaps 728: break;
729: default:
730: break;
731: }
1.189 kristaps 732: }
1.149 kristaps 733:
734: return(sz);
735: }
736:
737: size_t
738: term_vspan(const struct termp *p, const struct roffsu *su)
1.106 kristaps 739: {
740: double r;
741:
1.107 kristaps 742: switch (su->unit) {
1.222 schwarze 743: case SCALE_CM:
1.225 schwarze 744: r = su->scale * 2.0;
1.106 kristaps 745: break;
1.222 schwarze 746: case SCALE_IN:
1.225 schwarze 747: r = su->scale * 6.0;
1.106 kristaps 748: break;
1.222 schwarze 749: case SCALE_PC:
1.107 kristaps 750: r = su->scale;
1.106 kristaps 751: break;
1.222 schwarze 752: case SCALE_PT:
1.225 schwarze 753: r = su->scale / 8.0;
1.106 kristaps 754: break;
1.222 schwarze 755: case SCALE_MM:
1.225 schwarze 756: r = su->scale / 1000.0;
1.106 kristaps 757: break;
1.222 schwarze 758: case SCALE_VS:
1.107 kristaps 759: r = su->scale;
1.106 kristaps 760: break;
761: default:
1.225 schwarze 762: r = su->scale - 1.0;
1.106 kristaps 763: break;
764: }
765:
766: if (r < 0.0)
767: r = 0.0;
1.226 schwarze 768: return((size_t)(r + 0.0005));
1.106 kristaps 769: }
770:
1.107 kristaps 771: size_t
1.149 kristaps 772: term_hspan(const struct termp *p, const struct roffsu *su)
1.106 kristaps 773: {
1.156 kristaps 774: double v;
1.108 kristaps 775:
1.225 schwarze 776: v = (*p->hspan)(p, su);
1.156 kristaps 777: if (v < 0.0)
778: v = 0.0;
1.226 schwarze 779: return((size_t)(v + 0.0005));
1.106 kristaps 780: }
CVSweb