Import mdocml-1.13.1
[dragonfly.git] / contrib / mdocml / term.c
CommitLineData
070c62a6 1/* $Id: term.c,v 1.226 2014/08/01 19:38:29 schwarze Exp $ */
80387638 2/*
36342e81 3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
070c62a6 4 * Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org>
80387638
SW
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
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.
17 */
18#ifdef HAVE_CONFIG_H
19#include "config.h"
20#endif
21
22#include <sys/types.h>
23
24#include <assert.h>
25#include <ctype.h>
80387638
SW
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30#include "mandoc.h"
070c62a6 31#include "mandoc_aux.h"
80387638
SW
32#include "out.h"
33#include "term.h"
34#include "main.h"
35
f88b6c16
FF
36static size_t cond_width(const struct termp *, int, int *);
37static void adjbuf(struct termp *p, size_t);
a4c7eb57
SW
38static void bufferc(struct termp *, char);
39static void encode(struct termp *, const char *, size_t);
40static void encode1(struct termp *, int);
80387638 41
070c62a6 42
80387638
SW
43void
44term_free(struct termp *p)
45{
46
47 if (p->buf)
48 free(p->buf);
49 if (p->symtab)
a4c7eb57 50 mchars_free(p->symtab);
80387638
SW
51
52 free(p);
53}
54
80387638 55void
070c62a6 56term_begin(struct termp *p, term_margin head,
80387638
SW
57 term_margin foot, const void *arg)
58{
59
60 p->headf = head;
61 p->footf = foot;
62 p->argf = arg;
63 (*p->begin)(p);
64}
65
80387638
SW
66void
67term_end(struct termp *p)
68{
69
70 (*p->end)(p);
71}
72
80387638 73/*
070c62a6
FF
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.
80387638
SW
78 * The following flags may be specified:
79 *
070c62a6
FF
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.
80387638
SW
95 */
96void
97term_flushln(struct termp *p)
98{
f88b6c16
FF
99 size_t i; /* current input position in p->buf */
100 int ntab; /* number of tabs to prepend */
80387638
SW
101 size_t vis; /* current visual position on output */
102 size_t vbl; /* number of blanks to prepend to output */
103 size_t vend; /* end of word visual position on output */
104 size_t bp; /* visual right border position */
105 size_t dv; /* temporary for visual pos calculations */
f88b6c16
FF
106 size_t j; /* temporary loop index for p->buf */
107 size_t jhy; /* last hyph before overflow w/r/t j */
80387638
SW
108 size_t maxvis; /* output position of visible boundary */
109 size_t mmax; /* used in calculating bp */
110
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
7888c61d
FF
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.
80387638
SW
121 */
122 assert (p->rmargin >= p->offset);
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;
127
128 bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
129
130 /*
36342e81 131 * Calculate the required amount of padding.
80387638 132 */
36342e81
SW
133 vbl = p->offset + p->overstep > p->viscol ?
134 p->offset + p->overstep - p->viscol : 0;
80387638
SW
135
136 vis = vend = 0;
137 i = 0;
138
a4c7eb57 139 while (i < p->col) {
80387638
SW
140 /*
141 * Handle literal tab characters: collapse all
142 * subsequent tabs into a single huge set of spaces.
143 */
f88b6c16 144 ntab = 0;
a4c7eb57 145 while (i < p->col && '\t' == p->buf[i]) {
80387638
SW
146 vend = (vis / p->tabwidth + 1) * p->tabwidth;
147 vbl += vend - vis;
148 vis = vend;
f88b6c16 149 ntab++;
80387638
SW
150 i++;
151 }
152
153 /*
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
a4c7eb57 160 for (j = i, jhy = 0; j < p->col; j++) {
f88b6c16 161 if (' ' == p->buf[j] || '\t' == p->buf[j])
80387638
SW
162 break;
163
164 /* Back over the the last printed character. */
165 if (8 == p->buf[j]) {
166 assert(j);
167 vend -= (*p->width)(p, p->buf[j - 1]);
168 continue;
169 }
170
171 /* Regular word. */
172 /* Break at the hyphen point if we overrun. */
070c62a6
FF
173 if (vend > vis && vend < bp &&
174 (ASCII_HYPH == p->buf[j] ||
175 ASCII_BREAK == p->buf[j]))
80387638
SW
176 jhy = j;
177
070c62a6
FF
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
80387638
SW
185 vend += (*p->width)(p, p->buf[j]);
186 }
187
188 /*
189 * Find out whether we would exceed the right margin.
190 * If so, break to the next line.
191 */
192 if (vend > bp && 0 == jhy && vis > 0) {
193 vend -= vis;
194 (*p->endline)(p);
36342e81 195 p->viscol = 0;
070c62a6 196 if (TERMP_BRIND & p->flags) {
36342e81 197 vbl = p->rmargin;
80387638 198 vend += p->rmargin - p->offset;
36342e81 199 } else
80387638 200 vbl = p->offset;
80387638 201
f88b6c16
FF
202 /* use pending tabs on the new line */
203
204 if (0 < ntab)
205 vbl += ntab * p->tabwidth;
206
7888c61d
FF
207 /*
208 * Remove the p->overstep width.
209 * Again, if p->overstep is negative,
210 * sign extension does the right thing.
211 */
80387638
SW
212
213 bp += (size_t)p->overstep;
214 p->overstep = 0;
215 }
216
217 /* Write out the [remaining] word. */
a4c7eb57 218 for ( ; i < p->col; i++) {
80387638
SW
219 if (vend > bp && jhy > 0 && i > jhy)
220 break;
221 if ('\t' == p->buf[i])
222 break;
223 if (' ' == p->buf[i]) {
224 j = i;
225 while (' ' == p->buf[i])
226 i++;
f88b6c16 227 dv = (i - j) * (*p->width)(p, ' ');
80387638
SW
228 vbl += dv;
229 vend += dv;
230 break;
231 }
232 if (ASCII_NBRSP == p->buf[i]) {
233 vbl += (*p->width)(p, ' ');
234 continue;
235 }
070c62a6
FF
236 if (ASCII_BREAK == p->buf[i])
237 continue;
80387638
SW
238
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) {
245 (*p->advance)(p, vbl);
246 p->viscol += vbl;
247 vbl = 0;
248 }
249
36342e81
SW
250 (*p->letter)(p, p->buf[i]);
251 if (8 == p->buf[i])
252 p->viscol -= (*p->width)(p, p->buf[i-1]);
070c62a6 253 else
36342e81 254 p->viscol += (*p->width)(p, p->buf[i]);
80387638
SW
255 }
256 vis = vend;
257 }
258
259 /*
260 * If there was trailing white space, it was not printed;
261 * so reset the cursor position accordingly.
262 */
36342e81
SW
263 if (vis)
264 vis -= vbl;
80387638
SW
265
266 p->col = 0;
267 p->overstep = 0;
268
269 if ( ! (TERMP_NOBREAK & p->flags)) {
270 p->viscol = 0;
271 (*p->endline)(p);
272 return;
273 }
274
275 if (TERMP_HANG & p->flags) {
7888c61d 276 p->overstep = (int)(vis - maxvis +
070c62a6 277 p->trailspace * (*p->width)(p, ' '));
80387638
SW
278
279 /*
80387638
SW
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.
7888c61d
FF
283 * If there is a request to keep the columns together,
284 * allow negative overstep when the column is not full.
80387638 285 */
7888c61d 286 if (p->trailspace && p->overstep < 0)
80387638 287 p->overstep = 0;
36342e81 288 return;
80387638
SW
289
290 } else if (TERMP_DANGLE & p->flags)
291 return;
292
36342e81 293 /* If the column was overrun, break the line. */
7888c61d 294 if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
80387638 295 (*p->endline)(p);
36342e81 296 p->viscol = 0;
80387638
SW
297 }
298}
299
070c62a6 300/*
80387638
SW
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 */
305void
306term_newln(struct termp *p)
307{
308
309 p->flags |= TERMP_NOSPACE;
36342e81
SW
310 if (p->col || p->viscol)
311 term_flushln(p);
80387638
SW
312}
313
80387638
SW
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 */
320void
321term_vspace(struct termp *p)
322{
323
324 term_newln(p);
325 p->viscol = 0;
f88b6c16
FF
326 if (0 < p->skipvsp)
327 p->skipvsp--;
328 else
329 (*p->endline)(p);
80387638
SW
330}
331
80387638
SW
332void
333term_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
80387638
SW
342void
343term_fontrepl(struct termp *p, enum termfont f)
344{
345
346 p->fontl = p->fontq[p->fonti];
347 p->fontq[p->fonti] = f;
348}
349
80387638
SW
350void
351term_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
80387638
SW
359const void *
360term_fontq(struct termp *p)
361{
362
363 return(&p->fontq[p->fonti]);
364}
365
80387638
SW
366enum termfont
367term_fonttop(struct termp *p)
368{
369
370 return(p->fontq[p->fonti]);
371}
372
80387638
SW
373void
374term_fontpopq(struct termp *p, const void *key)
375{
376
f88b6c16 377 while (p->fonti >= 0 && key < (void *)(p->fontq + p->fonti))
80387638
SW
378 p->fonti--;
379 assert(p->fonti >= 0);
380}
381
80387638
SW
382void
383term_fontpop(struct termp *p)
384{
385
386 assert(p->fonti);
387 p->fonti--;
388}
389
80387638
SW
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 */
395void
396term_word(struct termp *p, const char *word)
397{
7888c61d 398 const char nbrsp[2] = { ASCII_NBRSP, 0 };
a4c7eb57
SW
399 const char *seq, *cp;
400 char c;
401 int sz, uc;
80387638 402 size_t ssz;
a4c7eb57 403 enum mandoc_esc esc;
80387638 404
80387638
SW
405 if ( ! (TERMP_NOSPACE & p->flags)) {
406 if ( ! (TERMP_KEEP & p->flags)) {
80387638
SW
407 bufferc(p, ' ');
408 if (TERMP_SENTENCE & p->flags)
409 bufferc(p, ' ');
410 } else
411 bufferc(p, ASCII_NBRSP);
412 }
f88b6c16
FF
413 if (TERMP_PREKEEP & p->flags)
414 p->flags |= TERMP_KEEP;
80387638
SW
415
416 if ( ! (p->flags & TERMP_NONOSPACE))
417 p->flags &= ~TERMP_NOSPACE;
418 else
419 p->flags |= TERMP_NOSPACE;
420
7888c61d 421 p->flags &= ~TERMP_SENTENCE;
80387638 422
a4c7eb57 423 while ('\0' != *word) {
f88b6c16
FF
424 if ('\\' != *word) {
425 if (TERMP_SKIPCHAR & p->flags) {
426 p->flags &= ~TERMP_SKIPCHAR;
427 word++;
428 continue;
429 }
7888c61d
FF
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, "\\");
80387638 439 encode(p, word, ssz);
f88b6c16 440 word += (int)ssz;
80387638 441 continue;
f88b6c16 442 }
80387638 443
a4c7eb57
SW
444 word++;
445 esc = mandoc_escape(&word, &seq, &sz);
446 if (ESCAPE_ERROR == esc)
070c62a6 447 continue;
a4c7eb57
SW
448
449 if (TERMENC_ASCII != p->enc)
450 switch (esc) {
070c62a6 451 case ESCAPE_UNICODE:
a4c7eb57
SW
452 uc = mchars_num2uc(seq + 1, sz - 1);
453 if ('\0' == uc)
454 break;
455 encode1(p, uc);
456 continue;
070c62a6 457 case ESCAPE_SPECIAL:
a4c7eb57
SW
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 }
80387638 466
a4c7eb57 467 switch (esc) {
070c62a6 468 case ESCAPE_UNICODE:
a4c7eb57 469 encode1(p, '?');
60e1e752 470 break;
070c62a6 471 case ESCAPE_NUMBERED:
a4c7eb57
SW
472 c = mchars_num2char(seq, sz);
473 if ('\0' != c)
474 encode(p, &c, 1);
80387638 475 break;
070c62a6 476 case ESCAPE_SPECIAL:
a4c7eb57 477 cp = mchars_spec2str(p->symtab, seq, sz, &ssz);
070c62a6 478 if (NULL != cp)
a4c7eb57
SW
479 encode(p, cp, ssz);
480 else if (1 == ssz)
481 encode(p, seq, sz);
80387638 482 break;
070c62a6 483 case ESCAPE_FONTBOLD:
80387638
SW
484 term_fontrepl(p, TERMFONT_BOLD);
485 break;
070c62a6 486 case ESCAPE_FONTITALIC:
80387638
SW
487 term_fontrepl(p, TERMFONT_UNDER);
488 break;
070c62a6 489 case ESCAPE_FONTBI:
f88b6c16
FF
490 term_fontrepl(p, TERMFONT_BI);
491 break;
070c62a6 492 case ESCAPE_FONT:
a4c7eb57 493 /* FALLTHROUGH */
070c62a6 494 case ESCAPE_FONTROMAN:
80387638
SW
495 term_fontrepl(p, TERMFONT_NONE);
496 break;
070c62a6 497 case ESCAPE_FONTPREV:
80387638
SW
498 term_fontlast(p);
499 break;
070c62a6 500 case ESCAPE_NOSPACE:
f88b6c16
FF
501 if (TERMP_SKIPCHAR & p->flags)
502 p->flags &= ~TERMP_SKIPCHAR;
503 else if ('\0' == *word)
a4c7eb57
SW
504 p->flags |= TERMP_NOSPACE;
505 break;
070c62a6 506 case ESCAPE_SKIPCHAR:
f88b6c16
FF
507 p->flags |= TERMP_SKIPCHAR;
508 break;
80387638
SW
509 default:
510 break;
511 }
80387638 512 }
7888c61d 513 p->flags &= ~TERMP_NBRWORD;
80387638
SW
514}
515
80387638 516static void
f88b6c16 517adjbuf(struct termp *p, size_t sz)
80387638
SW
518{
519
520 if (0 == p->maxcols)
521 p->maxcols = 1024;
522 while (sz >= p->maxcols)
523 p->maxcols <<= 2;
524
070c62a6 525 p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int));
80387638
SW
526}
527
80387638
SW
528static void
529bufferc(struct termp *p, char c)
530{
531
532 if (p->col + 1 >= p->maxcols)
533 adjbuf(p, p->col + 1);
534
a4c7eb57 535 p->buf[p->col++] = c;
80387638
SW
536}
537
a4c7eb57
SW
538/*
539 * See encode().
540 * Do this for a single (probably unicode) value.
541 * Does not check for non-decorated glyphs.
542 */
543static void
544encode1(struct termp *p, int c)
545{
546 enum termfont f;
547
f88b6c16
FF
548 if (TERMP_SKIPCHAR & p->flags) {
549 p->flags &= ~TERMP_SKIPCHAR;
550 return;
551 }
552
553 if (p->col + 6 >= p->maxcols)
554 adjbuf(p, p->col + 6);
a4c7eb57
SW
555
556 f = term_fonttop(p);
557
f88b6c16 558 if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
a4c7eb57 559 p->buf[p->col++] = '_';
f88b6c16
FF
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 }
a4c7eb57
SW
569 p->buf[p->col++] = c;
570}
80387638
SW
571
572static void
573encode(struct termp *p, const char *word, size_t sz)
574{
f88b6c16 575 size_t i;
a4c7eb57 576
f88b6c16
FF
577 if (TERMP_SKIPCHAR & p->flags) {
578 p->flags &= ~TERMP_SKIPCHAR;
579 return;
580 }
80387638
SW
581
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
f88b6c16 588 if (TERMFONT_NONE == term_fonttop(p)) {
070c62a6 589 if (p->col + sz >= p->maxcols)
f88b6c16
FF
590 adjbuf(p, p->col + sz);
591 for (i = 0; i < sz; i++)
a4c7eb57 592 p->buf[p->col++] = word[i];
80387638
SW
593 return;
594 }
595
596 /* Pre-buffer, assuming worst-case. */
597
f88b6c16
FF
598 if (p->col + 1 + (sz * 5) >= p->maxcols)
599 adjbuf(p, p->col + 1 + (sz * 5));
80387638 600
f88b6c16
FF
601 for (i = 0; i < sz; i++) {
602 if (ASCII_HYPH == word[i] ||
603 isgraph((unsigned char)word[i]))
604 encode1(p, word[i]);
80387638 605 else
a4c7eb57 606 p->buf[p->col++] = word[i];
80387638
SW
607 }
608}
609
070c62a6
FF
610void
611term_setwidth(struct termp *p, const char *wstr)
612{
613 struct roffsu su;
614 size_t width;
615 int iop;
616
617 iop = 0;
618 width = 0;
619 if (NULL != wstr) {
620 switch (*wstr) {
621 case '+':
622 iop = 1;
623 wstr++;
624 break;
625 case '-':
626 iop = -1;
627 wstr++;
628 break;
629 default:
630 break;
631 }
632 if (a2roffsu(wstr, &su, SCALE_MAX))
633 width = term_hspan(p, &su);
634 else
635 iop = 0;
636 }
637 (*p->setwidth)(p, iop, width);
638}
639
80387638
SW
640size_t
641term_len(const struct termp *p, size_t sz)
642{
643
644 return((*p->width)(p, ' ') * sz);
645}
646
f88b6c16
FF
647static size_t
648cond_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}
80387638
SW
657
658size_t
659term_strlen(const struct termp *p, const char *cp)
660{
a4c7eb57 661 size_t sz, rsz, i;
f88b6c16 662 int ssz, skip, c;
80387638 663 const char *seq, *rhs;
a4c7eb57 664 enum mandoc_esc esc;
070c62a6
FF
665 static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
666 ASCII_BREAK, '\0' };
80387638 667
a4c7eb57
SW
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 */
80387638 673
a4c7eb57 674 sz = 0;
f88b6c16 675 skip = 0;
a4c7eb57
SW
676 while ('\0' != *cp) {
677 rsz = strcspn(cp, rej);
678 for (i = 0; i < rsz; i++)
f88b6c16 679 sz += cond_width(p, *cp++, &skip);
a4c7eb57 680
a4c7eb57 681 switch (*cp) {
070c62a6 682 case '\\':
a4c7eb57
SW
683 cp++;
684 esc = mandoc_escape(&cp, &seq, &ssz);
685 if (ESCAPE_ERROR == esc)
070c62a6 686 continue;
a4c7eb57
SW
687
688 if (TERMENC_ASCII != p->enc)
689 switch (esc) {
070c62a6
FF
690 case ESCAPE_UNICODE:
691 c = mchars_num2uc(seq + 1,
692 ssz - 1);
a4c7eb57
SW
693 if ('\0' == c)
694 break;
f88b6c16 695 sz += cond_width(p, c, &skip);
a4c7eb57 696 continue;
070c62a6
FF
697 case ESCAPE_SPECIAL:
698 c = mchars_spec2cp(p->symtab,
699 seq, ssz);
a4c7eb57
SW
700 if (c <= 0)
701 break;
f88b6c16 702 sz += cond_width(p, c, &skip);
a4c7eb57
SW
703 continue;
704 default:
705 break;
706 }
707
708 rhs = NULL;
709
710 switch (esc) {
070c62a6 711 case ESCAPE_UNICODE:
f88b6c16 712 sz += cond_width(p, '?', &skip);
80387638 713 break;
070c62a6 714 case ESCAPE_NUMBERED:
a4c7eb57
SW
715 c = mchars_num2char(seq, ssz);
716 if ('\0' != c)
f88b6c16 717 sz += cond_width(p, c, &skip);
a4c7eb57 718 break;
070c62a6
FF
719 case ESCAPE_SPECIAL:
720 rhs = mchars_spec2str(p->symtab,
721 seq, ssz, &rsz);
80387638 722
a4c7eb57 723 if (ssz != 1 || rhs)
80387638
SW
724 break;
725
726 rhs = seq;
727 rsz = ssz;
728 break;
070c62a6 729 case ESCAPE_SKIPCHAR:
f88b6c16
FF
730 skip = 1;
731 break;
80387638 732 default:
80387638
SW
733 break;
734 }
735
a4c7eb57
SW
736 if (NULL == rhs)
737 break;
738
f88b6c16
FF
739 if (skip) {
740 skip = 0;
741 break;
742 }
743
a4c7eb57
SW
744 for (i = 0; i < rsz; i++)
745 sz += (*p->width)(p, *rhs++);
746 break;
070c62a6 747 case ASCII_NBRSP:
f88b6c16 748 sz += cond_width(p, ' ', &skip);
80387638 749 cp++;
a4c7eb57 750 break;
070c62a6 751 case ASCII_HYPH:
f88b6c16 752 sz += cond_width(p, '-', &skip);
80387638 753 cp++;
070c62a6
FF
754 /* FALLTHROUGH */
755 case ASCII_BREAK:
a4c7eb57
SW
756 break;
757 default:
758 break;
759 }
760 }
80387638
SW
761
762 return(sz);
763}
764
80387638
SW
765size_t
766term_vspan(const struct termp *p, const struct roffsu *su)
767{
768 double r;
769
770 switch (su->unit) {
070c62a6
FF
771 case SCALE_CM:
772 r = su->scale * 2.0;
80387638 773 break;
070c62a6
FF
774 case SCALE_IN:
775 r = su->scale * 6.0;
80387638 776 break;
070c62a6 777 case SCALE_PC:
80387638
SW
778 r = su->scale;
779 break;
070c62a6
FF
780 case SCALE_PT:
781 r = su->scale / 8.0;
80387638 782 break;
070c62a6
FF
783 case SCALE_MM:
784 r = su->scale / 1000.0;
80387638 785 break;
070c62a6 786 case SCALE_VS:
80387638
SW
787 r = su->scale;
788 break;
789 default:
070c62a6 790 r = su->scale - 1.0;
80387638
SW
791 break;
792 }
793
794 if (r < 0.0)
795 r = 0.0;
070c62a6 796 return((size_t)(r + 0.0005));
80387638
SW
797}
798
80387638
SW
799size_t
800term_hspan(const struct termp *p, const struct roffsu *su)
801{
802 double v;
803
070c62a6 804 v = (*p->hspan)(p, su);
80387638
SW
805 if (v < 0.0)
806 v = 0.0;
070c62a6 807 return((size_t)(v + 0.0005));
80387638 808}