Annotation of src/usr.bin/mandoc/term.c, Revision 1.67
1.67 ! schwarze 1: /* $Id: term.c,v 1.66 2012/07/16 21:28:12 schwarze Exp $ */
1.1 kristaps 2: /*
1.59 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.63 schwarze 4: * Copyright (c) 2010, 2011, 2012 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
1.2 schwarze 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.2 schwarze 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.20 schwarze 18: #include <sys/types.h>
19:
1.1 kristaps 20: #include <assert.h>
1.20 schwarze 21: #include <ctype.h>
1.36 schwarze 22: #include <stdint.h>
1.1 kristaps 23: #include <stdio.h>
24: #include <stdlib.h>
25: #include <string.h>
26:
1.34 schwarze 27: #include "mandoc.h"
1.16 schwarze 28: #include "out.h"
1.1 kristaps 29: #include "term.h"
1.16 schwarze 30: #include "main.h"
1.1 kristaps 31:
1.64 schwarze 32: static size_t cond_width(const struct termp *, int, int *);
1.59 schwarze 33: static void adjbuf(struct termp *p, int);
34: static void bufferc(struct termp *, char);
35: static void encode(struct termp *, const char *, size_t);
36: static void encode1(struct termp *, int);
1.1 kristaps 37:
1.37 schwarze 38: void
39: term_free(struct termp *p)
1.1 kristaps 40: {
41:
1.37 schwarze 42: if (p->buf)
43: free(p->buf);
44: if (p->symtab)
1.59 schwarze 45: mchars_free(p->symtab);
1.37 schwarze 46:
47: free(p);
1.1 kristaps 48: }
49:
50:
1.13 schwarze 51: void
1.37 schwarze 52: term_begin(struct termp *p, term_margin head,
53: term_margin foot, const void *arg)
1.1 kristaps 54: {
55:
1.37 schwarze 56: p->headf = head;
57: p->footf = foot;
58: p->argf = arg;
59: (*p->begin)(p);
1.1 kristaps 60: }
61:
62:
1.37 schwarze 63: void
64: term_end(struct termp *p)
1.1 kristaps 65: {
66:
1.37 schwarze 67: (*p->end)(p);
1.1 kristaps 68: }
69:
70: /*
71: * Flush a line of text. A "line" is loosely defined as being something
72: * that should be followed by a newline, regardless of whether it's
73: * broken apart by newlines getting there. A line can also be a
1.27 schwarze 74: * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
75: * not have a trailing newline.
1.1 kristaps 76: *
1.27 schwarze 77: * The following flags may be specified:
1.1 kristaps 78: *
79: * - TERMP_NOBREAK: this is the most important and is used when making
1.61 schwarze 80: * columns. In short: don't print a newline and instead expect the
81: * next call to do the padding up to the start of the next column.
1.1 kristaps 82: *
1.61 schwarze 83: * - TERMP_TWOSPACE: make sure there is room for at least two space
84: * characters of padding. Otherwise, rather break the line.
1.9 schwarze 85: *
1.6 schwarze 86: * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
87: * the line is overrun, and don't pad-right if it's underrun.
88: *
89: * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
1.61 schwarze 90: * overrunning, instead save the position and continue at that point
1.6 schwarze 91: * when the next invocation.
1.1 kristaps 92: *
93: * In-line line breaking:
94: *
95: * If TERMP_NOBREAK is specified and the line overruns the right
96: * margin, it will break and pad-right to the right margin after
97: * writing. If maxrmargin is violated, it will break and continue
1.19 schwarze 98: * writing from the right-margin, which will lead to the above scenario
99: * upon exit. Otherwise, the line will break at the right margin.
1.1 kristaps 100: */
101: void
102: term_flushln(struct termp *p)
103: {
1.19 schwarze 104: int i; /* current input position in p->buf */
1.66 schwarze 105: int ntab; /* number of tabs to prepend */
1.19 schwarze 106: size_t vis; /* current visual position on output */
107: size_t vbl; /* number of blanks to prepend to output */
1.33 schwarze 108: size_t vend; /* end of word visual position on output */
1.19 schwarze 109: size_t bp; /* visual right border position */
1.51 schwarze 110: size_t dv; /* temporary for visual pos calculations */
1.42 schwarze 111: int j; /* temporary loop index for p->buf */
112: int jhy; /* last hyph before overflow w/r/t j */
113: size_t maxvis; /* output position of visible boundary */
114: size_t mmax; /* used in calculating bp */
1.1 kristaps 115:
116: /*
117: * First, establish the maximum columns of "visible" content.
118: * This is usually the difference between the right-margin and
119: * an indentation, but can be, for tagged lists or columns, a
1.19 schwarze 120: * small set of values.
1.1 kristaps 121: */
1.54 schwarze 122: assert (p->rmargin >= p->offset);
1.53 schwarze 123: dv = p->rmargin - p->offset;
124: maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
125: dv = p->maxrmargin - p->offset;
126: mmax = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
1.9 schwarze 127:
1.1 kristaps 128: bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
1.19 schwarze 129:
1.33 schwarze 130: /*
1.61 schwarze 131: * Calculate the required amount of padding.
1.33 schwarze 132: */
1.61 schwarze 133: vbl = p->offset + p->overstep > p->viscol ?
134: p->offset + p->overstep - p->viscol : 0;
1.33 schwarze 135:
1.53 schwarze 136: vis = vend = 0;
137: i = 0;
1.19 schwarze 138:
1.59 schwarze 139: while (i < p->col) {
1.22 schwarze 140: /*
1.42 schwarze 141: * Handle literal tab characters: collapse all
142: * subsequent tabs into a single huge set of spaces.
1.30 schwarze 143: */
1.66 schwarze 144: ntab = 0;
1.59 schwarze 145: while (i < p->col && '\t' == p->buf[i]) {
1.42 schwarze 146: vend = (vis / p->tabwidth + 1) * p->tabwidth;
1.30 schwarze 147: vbl += vend - vis;
148: vis = vend;
1.66 schwarze 149: ntab++;
1.49 schwarze 150: i++;
1.30 schwarze 151: }
1.22 schwarze 152:
1.1 kristaps 153: /*
154: * Count up visible word characters. Control sequences
155: * (starting with the CSI) aren't counted. A space
156: * generates a non-printing word, which is valid (the
157: * space is printed according to regular spacing rules).
158: */
159:
1.59 schwarze 160: for (j = i, jhy = 0; j < p->col; j++) {
1.30 schwarze 161: if ((j && ' ' == p->buf[j]) || '\t' == p->buf[j])
1.1 kristaps 162: break;
1.42 schwarze 163:
164: /* Back over the the last printed character. */
165: if (8 == p->buf[j]) {
166: assert(j);
167: vend -= (*p->width)(p, p->buf[j - 1]);
168: continue;
169: }
170:
171: /* Regular word. */
172: /* Break at the hyphen point if we overrun. */
173: if (vend > vis && vend < bp &&
174: ASCII_HYPH == p->buf[j])
175: jhy = j;
176:
177: vend += (*p->width)(p, p->buf[j]);
1.1 kristaps 178: }
179:
180: /*
1.5 schwarze 181: * Find out whether we would exceed the right margin.
1.33 schwarze 182: * If so, break to the next line.
1.5 schwarze 183: */
1.33 schwarze 184: if (vend > bp && 0 == jhy && vis > 0) {
1.22 schwarze 185: vend -= vis;
1.37 schwarze 186: (*p->endline)(p);
1.62 schwarze 187: p->viscol = 0;
1.5 schwarze 188: if (TERMP_NOBREAK & p->flags) {
1.62 schwarze 189: vbl = p->rmargin;
1.22 schwarze 190: vend += p->rmargin - p->offset;
1.62 schwarze 191: } else
1.33 schwarze 192: vbl = p->offset;
1.66 schwarze 193:
194: /* use pending tabs on the new line */
195:
196: if (0 < ntab)
197: vbl += ntab * p->tabwidth;
1.33 schwarze 198:
1.26 schwarze 199: /* Remove the p->overstep width. */
1.33 schwarze 200:
1.53 schwarze 201: bp += (size_t)p->overstep;
1.26 schwarze 202: p->overstep = 0;
1.1 kristaps 203: }
1.30 schwarze 204:
1.33 schwarze 205: /* Write out the [remaining] word. */
1.59 schwarze 206: for ( ; i < p->col; i++) {
1.25 schwarze 207: if (vend > bp && jhy > 0 && i > jhy)
1.30 schwarze 208: break;
209: if ('\t' == p->buf[i])
1.1 kristaps 210: break;
1.22 schwarze 211: if (' ' == p->buf[i]) {
1.46 schwarze 212: j = i;
213: while (' ' == p->buf[i])
1.33 schwarze 214: i++;
1.53 schwarze 215: dv = (size_t)(i - j) * (*p->width)(p, ' ');
1.51 schwarze 216: vbl += dv;
217: vend += dv;
1.22 schwarze 218: break;
219: }
1.33 schwarze 220: if (ASCII_NBRSP == p->buf[i]) {
1.42 schwarze 221: vbl += (*p->width)(p, ' ');
1.33 schwarze 222: continue;
223: }
224:
225: /*
226: * Now we definitely know there will be
227: * printable characters to output,
228: * so write preceding white space now.
229: */
230: if (vbl) {
1.37 schwarze 231: (*p->advance)(p, vbl);
1.33 schwarze 232: p->viscol += vbl;
233: vbl = 0;
234: }
1.35 schwarze 235:
1.42 schwarze 236: if (ASCII_HYPH == p->buf[i]) {
1.37 schwarze 237: (*p->letter)(p, '-');
1.42 schwarze 238: p->viscol += (*p->width)(p, '-');
1.61 schwarze 239: continue;
240: }
241:
242: (*p->letter)(p, p->buf[i]);
243: if (8 == p->buf[i])
244: p->viscol -= (*p->width)(p, p->buf[i-1]);
245: else
1.42 schwarze 246: p->viscol += (*p->width)(p, p->buf[i]);
1.1 kristaps 247: }
1.22 schwarze 248: vis = vend;
1.1 kristaps 249: }
1.48 schwarze 250:
251: /*
252: * If there was trailing white space, it was not printed;
253: * so reset the cursor position accordingly.
254: */
1.61 schwarze 255: if (vis)
256: vis -= vbl;
1.18 schwarze 257:
1.9 schwarze 258: p->col = 0;
1.26 schwarze 259: p->overstep = 0;
1.1 kristaps 260:
1.9 schwarze 261: if ( ! (TERMP_NOBREAK & p->flags)) {
1.29 schwarze 262: p->viscol = 0;
1.37 schwarze 263: (*p->endline)(p);
1.1 kristaps 264: return;
265: }
266:
1.9 schwarze 267: if (TERMP_HANG & p->flags) {
268: /* We need one blank after the tag. */
1.53 schwarze 269: p->overstep = (int)(vis - maxvis + (*p->width)(p, ' '));
1.9 schwarze 270:
271: /*
272: * If we have overstepped the margin, temporarily move
273: * it to the right and flag the rest of the line to be
274: * shorter.
275: */
1.65 schwarze 276: if (p->overstep < 0)
1.26 schwarze 277: p->overstep = 0;
1.61 schwarze 278: return;
1.9 schwarze 279:
280: } else if (TERMP_DANGLE & p->flags)
281: return;
1.1 kristaps 282:
1.61 schwarze 283: /* If the column was overrun, break the line. */
284: if (maxvis <= vis +
1.53 schwarze 285: ((TERMP_TWOSPACE & p->flags) ? (*p->width)(p, ' ') : 0)) {
1.37 schwarze 286: (*p->endline)(p);
1.61 schwarze 287: p->viscol = 0;
1.9 schwarze 288: }
1.1 kristaps 289: }
290:
291:
292: /*
293: * A newline only breaks an existing line; it won't assert vertical
294: * space. All data in the output buffer is flushed prior to the newline
295: * assertion.
296: */
297: void
298: term_newln(struct termp *p)
299: {
300:
301: p->flags |= TERMP_NOSPACE;
1.61 schwarze 302: if (p->col || p->viscol)
303: term_flushln(p);
1.1 kristaps 304: }
305:
306:
307: /*
308: * Asserts a vertical space (a full, empty line-break between lines).
309: * Note that if used twice, this will cause two blank spaces and so on.
310: * All data in the output buffer is flushed prior to the newline
311: * assertion.
312: */
313: void
314: term_vspace(struct termp *p)
315: {
316:
317: term_newln(p);
1.29 schwarze 318: p->viscol = 0;
1.63 schwarze 319: if (0 < p->skipvsp)
320: p->skipvsp--;
321: else
322: (*p->endline)(p);
1.1 kristaps 323: }
324:
1.20 schwarze 325: void
326: term_fontlast(struct termp *p)
327: {
328: enum termfont f;
1.11 schwarze 329:
1.20 schwarze 330: f = p->fontl;
331: p->fontl = p->fontq[p->fonti];
332: p->fontq[p->fonti] = f;
333: }
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;
1.1 kristaps 342: }
343:
344:
1.20 schwarze 345: void
346: term_fontpush(struct termp *p, enum termfont f)
1.1 kristaps 347: {
1.7 schwarze 348:
1.20 schwarze 349: assert(p->fonti + 1 < 10);
350: p->fontl = p->fontq[p->fonti];
351: p->fontq[++p->fonti] = f;
352: }
1.1 kristaps 353:
354:
1.20 schwarze 355: const void *
356: term_fontq(struct termp *p)
357: {
1.1 kristaps 358:
1.20 schwarze 359: return(&p->fontq[p->fonti]);
360: }
1.1 kristaps 361:
362:
1.20 schwarze 363: enum termfont
364: term_fonttop(struct termp *p)
365: {
1.1 kristaps 366:
1.20 schwarze 367: return(p->fontq[p->fonti]);
368: }
1.7 schwarze 369:
370:
1.20 schwarze 371: void
372: term_fontpopq(struct termp *p, const void *key)
373: {
1.1 kristaps 374:
1.67 ! schwarze 375: while (p->fonti >= 0 && key < (void *)(p->fontq + p->fonti))
1.20 schwarze 376: p->fonti--;
377: assert(p->fonti >= 0);
378: }
1.1 kristaps 379:
380:
1.20 schwarze 381: void
382: term_fontpop(struct termp *p)
383: {
1.1 kristaps 384:
1.20 schwarze 385: assert(p->fonti);
386: p->fonti--;
1.1 kristaps 387: }
388:
389: /*
390: * Handle pwords, partial words, which may be either a single word or a
391: * phrase that cannot be broken down (such as a literal string). This
392: * handles word styling.
393: */
1.7 schwarze 394: void
395: term_word(struct termp *p, const char *word)
1.1 kristaps 396: {
1.59 schwarze 397: const char *seq, *cp;
398: char c;
399: int sz, uc;
1.20 schwarze 400: size_t ssz;
1.59 schwarze 401: enum mandoc_esc esc;
1.1 kristaps 402:
1.31 schwarze 403: if ( ! (TERMP_NOSPACE & p->flags)) {
1.40 schwarze 404: if ( ! (TERMP_KEEP & p->flags)) {
405: if (TERMP_PREKEEP & p->flags)
406: p->flags |= TERMP_KEEP;
1.31 schwarze 407: bufferc(p, ' ');
1.40 schwarze 408: if (TERMP_SENTENCE & p->flags)
409: bufferc(p, ' ');
410: } else
411: bufferc(p, ASCII_NBRSP);
1.31 schwarze 412: }
1.1 kristaps 413:
414: if ( ! (p->flags & TERMP_NONOSPACE))
415: p->flags &= ~TERMP_NOSPACE;
1.46 schwarze 416: else
417: p->flags |= TERMP_NOSPACE;
1.1 kristaps 418:
1.52 schwarze 419: p->flags &= ~(TERMP_SENTENCE | TERMP_IGNDELIM);
1.31 schwarze 420:
1.59 schwarze 421: while ('\0' != *word) {
1.64 schwarze 422: if ('\\' != *word) {
423: if (TERMP_SKIPCHAR & p->flags) {
424: p->flags &= ~TERMP_SKIPCHAR;
425: word++;
426: continue;
427: }
428: ssz = strcspn(word, "\\");
1.45 schwarze 429: encode(p, word, ssz);
1.64 schwarze 430: word += (int)ssz;
1.20 schwarze 431: continue;
1.64 schwarze 432: }
1.20 schwarze 433:
1.59 schwarze 434: word++;
435: esc = mandoc_escape(&word, &seq, &sz);
436: if (ESCAPE_ERROR == esc)
437: break;
438:
439: if (TERMENC_ASCII != p->enc)
440: switch (esc) {
441: case (ESCAPE_UNICODE):
442: uc = mchars_num2uc(seq + 1, sz - 1);
443: if ('\0' == uc)
444: break;
445: encode1(p, uc);
446: continue;
447: case (ESCAPE_SPECIAL):
448: uc = mchars_spec2cp(p->symtab, seq, sz);
449: if (uc <= 0)
450: break;
451: encode1(p, uc);
452: continue;
453: default:
454: break;
455: }
1.20 schwarze 456:
1.59 schwarze 457: switch (esc) {
458: case (ESCAPE_UNICODE):
459: encode1(p, '?');
1.56 schwarze 460: break;
1.59 schwarze 461: case (ESCAPE_NUMBERED):
462: c = mchars_num2char(seq, sz);
463: if ('\0' != c)
464: encode(p, &c, 1);
1.20 schwarze 465: break;
1.59 schwarze 466: case (ESCAPE_SPECIAL):
467: cp = mchars_spec2str(p->symtab, seq, sz, &ssz);
468: if (NULL != cp)
469: encode(p, cp, ssz);
470: else if (1 == ssz)
471: encode(p, seq, sz);
1.20 schwarze 472: break;
1.59 schwarze 473: case (ESCAPE_FONTBOLD):
1.20 schwarze 474: term_fontrepl(p, TERMFONT_BOLD);
475: break;
1.59 schwarze 476: case (ESCAPE_FONTITALIC):
1.20 schwarze 477: term_fontrepl(p, TERMFONT_UNDER);
478: break;
1.59 schwarze 479: case (ESCAPE_FONT):
480: /* FALLTHROUGH */
481: case (ESCAPE_FONTROMAN):
1.20 schwarze 482: term_fontrepl(p, TERMFONT_NONE);
483: break;
1.59 schwarze 484: case (ESCAPE_FONTPREV):
1.20 schwarze 485: term_fontlast(p);
486: break;
1.59 schwarze 487: case (ESCAPE_NOSPACE):
1.64 schwarze 488: if (TERMP_SKIPCHAR & p->flags)
489: p->flags &= ~TERMP_SKIPCHAR;
490: else if ('\0' == *word)
1.59 schwarze 491: p->flags |= TERMP_NOSPACE;
492: break;
1.64 schwarze 493: case (ESCAPE_SKIPCHAR):
494: p->flags |= TERMP_SKIPCHAR;
495: break;
1.20 schwarze 496: default:
497: break;
498: }
499: }
1.1 kristaps 500: }
501:
502: static void
1.59 schwarze 503: adjbuf(struct termp *p, int sz)
1.1 kristaps 504: {
505:
1.20 schwarze 506: if (0 == p->maxcols)
507: p->maxcols = 1024;
508: while (sz >= p->maxcols)
509: p->maxcols <<= 2;
510:
1.59 schwarze 511: p->buf = mandoc_realloc
512: (p->buf, sizeof(int) * (size_t)p->maxcols);
1.1 kristaps 513: }
514:
1.4 schwarze 515: static void
1.20 schwarze 516: bufferc(struct termp *p, char c)
517: {
518:
519: if (p->col + 1 >= p->maxcols)
520: adjbuf(p, p->col + 1);
521:
1.59 schwarze 522: p->buf[p->col++] = c;
1.20 schwarze 523: }
524:
1.59 schwarze 525: /*
526: * See encode().
527: * Do this for a single (probably unicode) value.
528: * Does not check for non-decorated glyphs.
529: */
530: static void
531: encode1(struct termp *p, int c)
532: {
533: enum termfont f;
534:
1.64 schwarze 535: if (TERMP_SKIPCHAR & p->flags) {
536: p->flags &= ~TERMP_SKIPCHAR;
537: return;
538: }
539:
1.59 schwarze 540: if (p->col + 4 >= p->maxcols)
541: adjbuf(p, p->col + 4);
542:
543: f = term_fonttop(p);
544:
545: if (TERMFONT_NONE == f) {
546: p->buf[p->col++] = c;
547: return;
548: } else if (TERMFONT_UNDER == f) {
549: p->buf[p->col++] = '_';
550: } else
551: p->buf[p->col++] = c;
552:
553: p->buf[p->col++] = 8;
554: p->buf[p->col++] = c;
555: }
1.20 schwarze 556:
557: static void
558: encode(struct termp *p, const char *word, size_t sz)
1.4 schwarze 559: {
1.20 schwarze 560: enum termfont f;
1.59 schwarze 561: int i, len;
562:
1.64 schwarze 563: if (TERMP_SKIPCHAR & p->flags) {
564: p->flags &= ~TERMP_SKIPCHAR;
565: return;
566: }
567:
1.59 schwarze 568: /* LINTED */
569: len = sz;
1.20 schwarze 570:
571: /*
572: * Encode and buffer a string of characters. If the current
573: * font mode is unset, buffer directly, else encode then buffer
574: * character by character.
575: */
576:
1.38 schwarze 577: if (TERMFONT_NONE == (f = term_fonttop(p))) {
1.59 schwarze 578: if (p->col + len >= p->maxcols)
579: adjbuf(p, p->col + len);
580: for (i = 0; i < len; i++)
581: p->buf[p->col++] = word[i];
1.20 schwarze 582: return;
583: }
584:
1.46 schwarze 585: /* Pre-buffer, assuming worst-case. */
586:
1.59 schwarze 587: if (p->col + 1 + (len * 3) >= p->maxcols)
588: adjbuf(p, p->col + 1 + (len * 3));
1.46 schwarze 589:
1.59 schwarze 590: for (i = 0; i < len; i++) {
1.60 schwarze 591: if (ASCII_HYPH != word[i] &&
592: ! isgraph((unsigned char)word[i])) {
1.59 schwarze 593: p->buf[p->col++] = word[i];
1.20 schwarze 594: continue;
1.4 schwarze 595: }
1.20 schwarze 596:
597: if (TERMFONT_UNDER == f)
1.59 schwarze 598: p->buf[p->col++] = '_';
1.60 schwarze 599: else if (ASCII_HYPH == word[i])
600: p->buf[p->col++] = '-';
1.20 schwarze 601: else
1.59 schwarze 602: p->buf[p->col++] = word[i];
1.20 schwarze 603:
1.59 schwarze 604: p->buf[p->col++] = 8;
605: p->buf[p->col++] = word[i];
1.4 schwarze 606: }
607: }
1.16 schwarze 608:
609: size_t
1.39 schwarze 610: term_len(const struct termp *p, size_t sz)
611: {
612:
613: return((*p->width)(p, ' ') * sz);
614: }
615:
1.64 schwarze 616: static size_t
617: cond_width(const struct termp *p, int c, int *skip)
618: {
619:
620: if (*skip) {
621: (*skip) = 0;
622: return(0);
623: } else
624: return((*p->width)(p, c));
625: }
1.39 schwarze 626:
627: size_t
628: term_strlen(const struct termp *p, const char *cp)
629: {
1.59 schwarze 630: size_t sz, rsz, i;
1.64 schwarze 631: int ssz, skip, c;
1.50 schwarze 632: const char *seq, *rhs;
1.59 schwarze 633: enum mandoc_esc esc;
634: static const char rej[] = { '\\', ASCII_HYPH, ASCII_NBRSP, '\0' };
635:
636: /*
637: * Account for escaped sequences within string length
638: * calculations. This follows the logic in term_word() as we
639: * must calculate the width of produced strings.
640: */
641:
642: sz = 0;
1.64 schwarze 643: skip = 0;
1.59 schwarze 644: while ('\0' != *cp) {
645: rsz = strcspn(cp, rej);
646: for (i = 0; i < rsz; i++)
1.64 schwarze 647: sz += cond_width(p, *cp++, &skip);
1.59 schwarze 648:
649: c = 0;
650: switch (*cp) {
651: case ('\\'):
652: cp++;
653: esc = mandoc_escape(&cp, &seq, &ssz);
654: if (ESCAPE_ERROR == esc)
655: return(sz);
656:
657: if (TERMENC_ASCII != p->enc)
658: switch (esc) {
659: case (ESCAPE_UNICODE):
660: c = mchars_num2uc
661: (seq + 1, ssz - 1);
662: if ('\0' == c)
663: break;
1.64 schwarze 664: sz += cond_width(p, c, &skip);
1.59 schwarze 665: continue;
666: case (ESCAPE_SPECIAL):
667: c = mchars_spec2cp
668: (p->symtab, seq, ssz);
669: if (c <= 0)
670: break;
1.64 schwarze 671: sz += cond_width(p, c, &skip);
1.59 schwarze 672: continue;
673: default:
674: break;
675: }
676:
677: rhs = NULL;
1.50 schwarze 678:
1.59 schwarze 679: switch (esc) {
680: case (ESCAPE_UNICODE):
1.64 schwarze 681: sz += cond_width(p, '?', &skip);
1.59 schwarze 682: break;
683: case (ESCAPE_NUMBERED):
684: c = mchars_num2char(seq, ssz);
685: if ('\0' != c)
1.64 schwarze 686: sz += cond_width(p, c, &skip);
1.50 schwarze 687: break;
1.59 schwarze 688: case (ESCAPE_SPECIAL):
689: rhs = mchars_spec2str
1.50 schwarze 690: (p->symtab, seq, ssz, &rsz);
691:
1.59 schwarze 692: if (ssz != 1 || rhs)
1.50 schwarze 693: break;
694:
695: rhs = seq;
696: rsz = ssz;
697: break;
1.64 schwarze 698: case (ESCAPE_SKIPCHAR):
699: skip = 1;
700: break;
1.50 schwarze 701: default:
702: break;
703: }
1.39 schwarze 704:
1.59 schwarze 705: if (NULL == rhs)
706: break;
707:
1.64 schwarze 708: if (skip) {
709: skip = 0;
710: break;
711: }
712:
1.59 schwarze 713: for (i = 0; i < rsz; i++)
714: sz += (*p->width)(p, *rhs++);
715: break;
716: case (ASCII_NBRSP):
1.64 schwarze 717: sz += cond_width(p, ' ', &skip);
1.55 schwarze 718: cp++;
1.59 schwarze 719: break;
720: case (ASCII_HYPH):
1.64 schwarze 721: sz += cond_width(p, '-', &skip);
1.55 schwarze 722: cp++;
1.59 schwarze 723: break;
724: default:
725: break;
726: }
727: }
1.39 schwarze 728:
729: return(sz);
730: }
731:
1.44 schwarze 732: /* ARGSUSED */
1.39 schwarze 733: size_t
734: term_vspan(const struct termp *p, const struct roffsu *su)
1.16 schwarze 735: {
736: double r;
737:
738: switch (su->unit) {
739: case (SCALE_CM):
740: r = su->scale * 2;
741: break;
742: case (SCALE_IN):
743: r = su->scale * 6;
744: break;
745: case (SCALE_PC):
746: r = su->scale;
747: break;
748: case (SCALE_PT):
749: r = su->scale / 8;
750: break;
751: case (SCALE_MM):
752: r = su->scale / 1000;
753: break;
754: case (SCALE_VS):
755: r = su->scale;
756: break;
757: default:
758: r = su->scale - 1;
759: break;
760: }
761:
762: if (r < 0.0)
763: r = 0.0;
764: return(/* LINTED */(size_t)
765: r);
766: }
767:
768: size_t
1.39 schwarze 769: term_hspan(const struct termp *p, const struct roffsu *su)
1.16 schwarze 770: {
1.44 schwarze 771: double v;
1.16 schwarze 772:
1.44 schwarze 773: v = ((*p->hspan)(p, su));
774: if (v < 0.0)
775: v = 0.0;
776: return((size_t) /* LINTED */
777: v);
1.16 schwarze 778: }