Annotation of mandoc/term.c, Revision 1.214
1.214 ! schwarze 1: /* $Id: term.c,v 1.213 2013/12/24 23:04:36 schwarze Exp $ */
1.1 kristaps 2: /*
1.198 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.209 schwarze 4: * Copyright (c) 2010, 2011, 2012, 2013 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.141 kristaps 26: #include <stdint.h>
1.22 kristaps 27: #include <stdio.h>
1.1 kristaps 28: #include <stdlib.h>
29: #include <string.h>
30:
1.137 kristaps 31: #include "mandoc.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 &&
182: ASCII_HYPH == p->buf[j])
183: jhy = j;
184:
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.81 kristaps 196: if (TERMP_NOBREAK & 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.130 kristaps 236:
1.136 schwarze 237: /*
238: * Now we definitely know there will be
239: * printable characters to output,
240: * so write preceding white space now.
241: */
242: if (vbl) {
1.146 kristaps 243: (*p->advance)(p, vbl);
1.139 schwarze 244: p->viscol += vbl;
1.136 schwarze 245: vbl = 0;
246: }
1.140 kristaps 247:
1.153 kristaps 248: if (ASCII_HYPH == p->buf[i]) {
1.146 kristaps 249: (*p->letter)(p, '-');
1.153 kristaps 250: p->viscol += (*p->width)(p, '-');
1.200 schwarze 251: continue;
252: }
253:
254: (*p->letter)(p, p->buf[i]);
255: if (8 == p->buf[i])
256: p->viscol -= (*p->width)(p, p->buf[i-1]);
257: else
1.153 kristaps 258: p->viscol += (*p->width)(p, p->buf[i]);
1.136 schwarze 259: }
260: vis = vend;
1.71 kristaps 261: }
1.168 schwarze 262:
263: /*
264: * If there was trailing white space, it was not printed;
265: * so reset the cursor position accordingly.
266: */
1.200 schwarze 267: if (vis)
268: vis -= vbl;
1.111 kristaps 269:
1.91 kristaps 270: p->col = 0;
1.129 kristaps 271: p->overstep = 0;
1.15 kristaps 272:
1.91 kristaps 273: if ( ! (TERMP_NOBREAK & p->flags)) {
1.139 schwarze 274: p->viscol = 0;
1.146 kristaps 275: (*p->endline)(p);
1.15 kristaps 276: return;
1.71 kristaps 277: }
1.15 kristaps 278:
1.91 kristaps 279: if (TERMP_HANG & p->flags) {
1.211 schwarze 280: p->overstep = (int)(vis - maxvis +
281: p->trailspace * (*p->width)(p, ' '));
1.91 kristaps 282:
283: /*
1.92 kristaps 284: * If we have overstepped the margin, temporarily move
285: * it to the right and flag the rest of the line to be
286: * shorter.
1.212 schwarze 287: * If there is a request to keep the columns together,
288: * allow negative overstep when the column is not full.
1.91 kristaps 289: */
1.212 schwarze 290: if (p->trailspace && p->overstep < 0)
1.129 kristaps 291: p->overstep = 0;
1.200 schwarze 292: return;
1.91 kristaps 293:
294: } else if (TERMP_DANGLE & p->flags)
295: return;
1.15 kristaps 296:
1.200 schwarze 297: /* If the column was overrun, break the line. */
1.211 schwarze 298: if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
1.146 kristaps 299: (*p->endline)(p);
1.200 schwarze 300: p->viscol = 0;
1.91 kristaps 301: }
1.15 kristaps 302: }
303:
304:
1.71 kristaps 305: /*
306: * A newline only breaks an existing line; it won't assert vertical
307: * space. All data in the output buffer is flushed prior to the newline
308: * assertion.
309: */
310: void
311: term_newln(struct termp *p)
1.15 kristaps 312: {
313:
1.71 kristaps 314: p->flags |= TERMP_NOSPACE;
1.200 schwarze 315: if (p->col || p->viscol)
316: term_flushln(p);
1.16 kristaps 317: }
318:
319:
1.71 kristaps 320: /*
321: * Asserts a vertical space (a full, empty line-break between lines).
322: * Note that if used twice, this will cause two blank spaces and so on.
323: * All data in the output buffer is flushed prior to the newline
324: * assertion.
325: */
326: void
327: term_vspace(struct termp *p)
1.16 kristaps 328: {
329:
1.62 kristaps 330: term_newln(p);
1.139 schwarze 331: p->viscol = 0;
1.202 schwarze 332: if (0 < p->skipvsp)
333: p->skipvsp--;
334: else
335: (*p->endline)(p);
1.16 kristaps 336: }
337:
1.125 kristaps 338: void
339: term_fontlast(struct termp *p)
340: {
341: enum termfont f;
342:
343: f = p->fontl;
344: p->fontl = p->fontq[p->fonti];
345: p->fontq[p->fonti] = f;
346: }
347:
348:
349: void
350: term_fontrepl(struct termp *p, enum termfont f)
351: {
352:
353: p->fontl = p->fontq[p->fonti];
354: p->fontq[p->fonti] = f;
355: }
356:
357:
358: void
359: term_fontpush(struct termp *p, enum termfont f)
360: {
361:
362: assert(p->fonti + 1 < 10);
363: p->fontl = p->fontq[p->fonti];
364: p->fontq[++p->fonti] = f;
365: }
366:
367:
368: const void *
369: term_fontq(struct termp *p)
370: {
371:
372: return(&p->fontq[p->fonti]);
373: }
374:
375:
376: enum termfont
377: term_fonttop(struct termp *p)
378: {
379:
380: return(p->fontq[p->fonti]);
381: }
382:
383:
384: void
385: term_fontpopq(struct termp *p, const void *key)
386: {
387:
1.206 schwarze 388: while (p->fonti >= 0 && key < (void *)(p->fontq + p->fonti))
1.125 kristaps 389: p->fonti--;
390: assert(p->fonti >= 0);
391: }
1.94 kristaps 392:
1.125 kristaps 393:
394: void
395: term_fontpop(struct termp *p)
396: {
397:
398: assert(p->fonti);
399: p->fonti--;
1.17 kristaps 400: }
401:
1.71 kristaps 402: /*
403: * Handle pwords, partial words, which may be either a single word or a
404: * phrase that cannot be broken down (such as a literal string). This
405: * handles word styling.
406: */
1.86 kristaps 407: void
408: term_word(struct termp *p, const char *word)
1.65 kristaps 409: {
1.214 ! schwarze 410: const char nbrsp[2] = { ASCII_NBRSP, 0 };
1.191 kristaps 411: const char *seq, *cp;
412: char c;
1.194 kristaps 413: int sz, uc;
1.124 kristaps 414: size_t ssz;
1.184 kristaps 415: enum mandoc_esc esc;
1.100 kristaps 416:
1.133 kristaps 417: if ( ! (TERMP_NOSPACE & p->flags)) {
1.151 schwarze 418: if ( ! (TERMP_KEEP & p->flags)) {
1.133 kristaps 419: bufferc(p, ' ');
1.151 schwarze 420: if (TERMP_SENTENCE & p->flags)
421: bufferc(p, ' ');
422: } else
423: bufferc(p, ASCII_NBRSP);
1.133 kristaps 424: }
1.207 schwarze 425: if (TERMP_PREKEEP & p->flags)
426: p->flags |= TERMP_KEEP;
1.65 kristaps 427:
1.71 kristaps 428: if ( ! (p->flags & TERMP_NONOSPACE))
429: p->flags &= ~TERMP_NOSPACE;
1.166 kristaps 430: else
431: p->flags |= TERMP_NOSPACE;
1.133 kristaps 432:
1.213 schwarze 433: p->flags &= ~TERMP_SENTENCE;
1.65 kristaps 434:
1.184 kristaps 435: while ('\0' != *word) {
1.203 schwarze 436: if ('\\' != *word) {
437: if (TERMP_SKIPCHAR & p->flags) {
438: p->flags &= ~TERMP_SKIPCHAR;
439: word++;
440: continue;
441: }
1.214 ! schwarze 442: if (TERMP_NBRWORD & p->flags) {
! 443: if (' ' == *word) {
! 444: encode(p, nbrsp, 1);
! 445: word++;
! 446: continue;
! 447: }
! 448: ssz = strcspn(word, "\\ ");
! 449: } else
! 450: ssz = strcspn(word, "\\");
1.162 kristaps 451: encode(p, word, ssz);
1.203 schwarze 452: word += (int)ssz;
1.124 kristaps 453: continue;
1.203 schwarze 454: }
1.124 kristaps 455:
1.184 kristaps 456: word++;
457: esc = mandoc_escape(&word, &seq, &sz);
458: if (ESCAPE_ERROR == esc)
459: break;
1.124 kristaps 460:
1.196 kristaps 461: if (TERMENC_ASCII != p->enc)
462: switch (esc) {
463: case (ESCAPE_UNICODE):
464: uc = mchars_num2uc(seq + 1, sz - 1);
465: if ('\0' == uc)
466: break;
467: encode1(p, uc);
468: continue;
469: case (ESCAPE_SPECIAL):
470: uc = mchars_spec2cp(p->symtab, seq, sz);
471: if (uc <= 0)
472: break;
473: encode1(p, uc);
474: continue;
475: default:
476: break;
477: }
478:
1.184 kristaps 479: switch (esc) {
1.192 kristaps 480: case (ESCAPE_UNICODE):
1.196 kristaps 481: encode1(p, '?');
1.192 kristaps 482: break;
1.184 kristaps 483: case (ESCAPE_NUMBERED):
1.196 kristaps 484: c = mchars_num2char(seq, sz);
485: if ('\0' != c)
1.191 kristaps 486: encode(p, &c, 1);
1.184 kristaps 487: break;
488: case (ESCAPE_SPECIAL):
1.191 kristaps 489: cp = mchars_spec2str(p->symtab, seq, sz, &ssz);
490: if (NULL != cp)
491: encode(p, cp, ssz);
492: else if (1 == ssz)
493: encode(p, seq, sz);
1.124 kristaps 494: break;
1.184 kristaps 495: case (ESCAPE_FONTBOLD):
1.125 kristaps 496: term_fontrepl(p, TERMFONT_BOLD);
1.124 kristaps 497: break;
1.184 kristaps 498: case (ESCAPE_FONTITALIC):
1.125 kristaps 499: term_fontrepl(p, TERMFONT_UNDER);
1.124 kristaps 500: break;
1.209 schwarze 501: case (ESCAPE_FONTBI):
502: term_fontrepl(p, TERMFONT_BI);
503: break;
1.195 kristaps 504: case (ESCAPE_FONT):
505: /* FALLTHROUGH */
1.184 kristaps 506: case (ESCAPE_FONTROMAN):
1.125 kristaps 507: term_fontrepl(p, TERMFONT_NONE);
1.124 kristaps 508: break;
1.184 kristaps 509: case (ESCAPE_FONTPREV):
1.125 kristaps 510: term_fontlast(p);
1.124 kristaps 511: break;
1.184 kristaps 512: case (ESCAPE_NOSPACE):
1.203 schwarze 513: if (TERMP_SKIPCHAR & p->flags)
514: p->flags &= ~TERMP_SKIPCHAR;
515: else if ('\0' == *word)
1.184 kristaps 516: p->flags |= TERMP_NOSPACE;
517: break;
1.203 schwarze 518: case (ESCAPE_SKIPCHAR):
519: p->flags |= TERMP_SKIPCHAR;
520: break;
1.124 kristaps 521: default:
522: break;
523: }
524: }
1.214 ! schwarze 525: p->flags &= ~TERMP_NBRWORD;
1.65 kristaps 526: }
527:
1.71 kristaps 528: static void
1.210 schwarze 529: adjbuf(struct termp *p, size_t sz)
1.51 kristaps 530: {
531:
1.125 kristaps 532: if (0 == p->maxcols)
533: p->maxcols = 1024;
534: while (sz >= p->maxcols)
535: p->maxcols <<= 2;
536:
1.210 schwarze 537: p->buf = mandoc_realloc(p->buf, sizeof(int) * p->maxcols);
1.51 kristaps 538: }
539:
1.79 kristaps 540: static void
1.125 kristaps 541: bufferc(struct termp *p, char c)
542: {
543:
544: if (p->col + 1 >= p->maxcols)
545: adjbuf(p, p->col + 1);
546:
1.188 kristaps 547: p->buf[p->col++] = c;
1.125 kristaps 548: }
549:
1.194 kristaps 550: /*
551: * See encode().
552: * Do this for a single (probably unicode) value.
553: * Does not check for non-decorated glyphs.
554: */
555: static void
556: encode1(struct termp *p, int c)
557: {
558: enum termfont f;
559:
1.203 schwarze 560: if (TERMP_SKIPCHAR & p->flags) {
561: p->flags &= ~TERMP_SKIPCHAR;
562: return;
563: }
564:
1.209 schwarze 565: if (p->col + 6 >= p->maxcols)
566: adjbuf(p, p->col + 6);
1.194 kristaps 567:
568: f = term_fonttop(p);
569:
1.209 schwarze 570: if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
1.194 kristaps 571: p->buf[p->col++] = '_';
1.209 schwarze 572: p->buf[p->col++] = 8;
573: }
574: if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
575: if (ASCII_HYPH == c)
576: p->buf[p->col++] = '-';
577: else
578: p->buf[p->col++] = c;
579: p->buf[p->col++] = 8;
580: }
1.194 kristaps 581: p->buf[p->col++] = c;
582: }
583:
1.125 kristaps 584: static void
585: encode(struct termp *p, const char *word, size_t sz)
586: {
1.210 schwarze 587: size_t i;
1.188 kristaps 588:
1.203 schwarze 589: if (TERMP_SKIPCHAR & p->flags) {
590: p->flags &= ~TERMP_SKIPCHAR;
591: return;
592: }
593:
1.125 kristaps 594: /*
595: * Encode and buffer a string of characters. If the current
596: * font mode is unset, buffer directly, else encode then buffer
597: * character by character.
598: */
599:
1.209 schwarze 600: if (TERMFONT_NONE == term_fonttop(p)) {
1.210 schwarze 601: if (p->col + sz >= p->maxcols)
602: adjbuf(p, p->col + sz);
603: for (i = 0; i < sz; i++)
1.188 kristaps 604: p->buf[p->col++] = word[i];
1.125 kristaps 605: return;
606: }
607:
1.165 kristaps 608: /* Pre-buffer, assuming worst-case. */
609:
1.210 schwarze 610: if (p->col + 1 + (sz * 5) >= p->maxcols)
611: adjbuf(p, p->col + 1 + (sz * 5));
1.165 kristaps 612:
1.210 schwarze 613: for (i = 0; i < sz; i++) {
1.209 schwarze 614: if (ASCII_HYPH == word[i] ||
615: isgraph((unsigned char)word[i]))
616: encode1(p, word[i]);
1.125 kristaps 617: else
1.188 kristaps 618: p->buf[p->col++] = word[i];
1.79 kristaps 619: }
620: }
1.106 kristaps 621:
1.107 kristaps 622: size_t
1.149 kristaps 623: term_len(const struct termp *p, size_t sz)
624: {
625:
626: return((*p->width)(p, ' ') * sz);
627: }
628:
1.203 schwarze 629: static size_t
630: cond_width(const struct termp *p, int c, int *skip)
631: {
632:
633: if (*skip) {
634: (*skip) = 0;
635: return(0);
636: } else
637: return((*p->width)(p, c));
638: }
1.149 kristaps 639:
640: size_t
641: term_strlen(const struct termp *p, const char *cp)
642: {
1.184 kristaps 643: size_t sz, rsz, i;
1.203 schwarze 644: int ssz, skip, c;
1.171 kristaps 645: const char *seq, *rhs;
1.196 kristaps 646: enum mandoc_esc esc;
1.189 kristaps 647: static const char rej[] = { '\\', ASCII_HYPH, ASCII_NBRSP, '\0' };
1.171 kristaps 648:
1.184 kristaps 649: /*
650: * Account for escaped sequences within string length
651: * calculations. This follows the logic in term_word() as we
652: * must calculate the width of produced strings.
653: */
654:
655: sz = 0;
1.203 schwarze 656: skip = 0;
1.189 kristaps 657: while ('\0' != *cp) {
658: rsz = strcspn(cp, rej);
659: for (i = 0; i < rsz; i++)
1.203 schwarze 660: sz += cond_width(p, *cp++, &skip);
1.189 kristaps 661:
1.192 kristaps 662: c = 0;
1.184 kristaps 663: switch (*cp) {
664: case ('\\'):
1.189 kristaps 665: cp++;
1.196 kristaps 666: esc = mandoc_escape(&cp, &seq, &ssz);
667: if (ESCAPE_ERROR == esc)
1.184 kristaps 668: return(sz);
1.196 kristaps 669:
670: if (TERMENC_ASCII != p->enc)
671: switch (esc) {
672: case (ESCAPE_UNICODE):
673: c = mchars_num2uc
674: (seq + 1, ssz - 1);
675: if ('\0' == c)
676: break;
1.203 schwarze 677: sz += cond_width(p, c, &skip);
1.196 kristaps 678: continue;
679: case (ESCAPE_SPECIAL):
680: c = mchars_spec2cp
681: (p->symtab, seq, ssz);
682: if (c <= 0)
683: break;
1.203 schwarze 684: sz += cond_width(p, c, &skip);
1.196 kristaps 685: continue;
686: default:
1.194 kristaps 687: break;
688: }
1.196 kristaps 689:
690: rhs = NULL;
691:
692: switch (esc) {
693: case (ESCAPE_UNICODE):
1.203 schwarze 694: sz += cond_width(p, '?', &skip);
1.194 kristaps 695: break;
1.190 kristaps 696: case (ESCAPE_NUMBERED):
1.194 kristaps 697: c = mchars_num2char(seq, ssz);
1.190 kristaps 698: if ('\0' != c)
1.203 schwarze 699: sz += cond_width(p, c, &skip);
1.171 kristaps 700: break;
1.184 kristaps 701: case (ESCAPE_SPECIAL):
1.185 kristaps 702: rhs = mchars_spec2str
1.171 kristaps 703: (p->symtab, seq, ssz, &rsz);
704:
1.184 kristaps 705: if (ssz != 1 || rhs)
1.171 kristaps 706: break;
707:
708: rhs = seq;
709: rsz = ssz;
710: break;
1.203 schwarze 711: case (ESCAPE_SKIPCHAR):
712: skip = 1;
713: break;
1.171 kristaps 714: default:
715: break;
716: }
1.149 kristaps 717:
1.184 kristaps 718: if (NULL == rhs)
719: break;
720:
1.203 schwarze 721: if (skip) {
722: skip = 0;
723: break;
724: }
725:
1.184 kristaps 726: for (i = 0; i < rsz; i++)
727: sz += (*p->width)(p, *rhs++);
728: break;
729: case (ASCII_NBRSP):
1.203 schwarze 730: sz += cond_width(p, ' ', &skip);
1.176 kristaps 731: cp++;
1.184 kristaps 732: break;
733: case (ASCII_HYPH):
1.203 schwarze 734: sz += cond_width(p, '-', &skip);
1.176 kristaps 735: cp++;
1.184 kristaps 736: break;
737: default:
738: break;
739: }
1.189 kristaps 740: }
1.149 kristaps 741:
742: return(sz);
743: }
744:
1.157 kristaps 745: /* ARGSUSED */
1.149 kristaps 746: size_t
747: term_vspan(const struct termp *p, const struct roffsu *su)
1.106 kristaps 748: {
749: double r;
750:
1.107 kristaps 751: switch (su->unit) {
1.106 kristaps 752: case (SCALE_CM):
1.107 kristaps 753: r = su->scale * 2;
1.106 kristaps 754: break;
755: case (SCALE_IN):
1.107 kristaps 756: r = su->scale * 6;
1.106 kristaps 757: break;
758: case (SCALE_PC):
1.107 kristaps 759: r = su->scale;
1.106 kristaps 760: break;
761: case (SCALE_PT):
1.107 kristaps 762: r = su->scale / 8;
1.106 kristaps 763: break;
764: case (SCALE_MM):
1.107 kristaps 765: r = su->scale / 1000;
1.106 kristaps 766: break;
767: case (SCALE_VS):
1.107 kristaps 768: r = su->scale;
1.106 kristaps 769: break;
770: default:
1.107 kristaps 771: r = su->scale - 1;
1.106 kristaps 772: break;
773: }
774:
775: if (r < 0.0)
776: r = 0.0;
1.107 kristaps 777: return(/* LINTED */(size_t)
1.106 kristaps 778: r);
779: }
780:
1.107 kristaps 781: size_t
1.149 kristaps 782: term_hspan(const struct termp *p, const struct roffsu *su)
1.106 kristaps 783: {
1.156 kristaps 784: double v;
1.108 kristaps 785:
1.156 kristaps 786: v = ((*p->hspan)(p, su));
787: if (v < 0.0)
788: v = 0.0;
789: return((size_t) /* LINTED */
790: v);
1.106 kristaps 791: }
CVSweb