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