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