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