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