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