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