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