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