]> git.cameronkatri.com Git - mandoc.git/blob - term.c
Partial implementation of \h (horizontal line drawing function).
[mandoc.git] / term.c
1 /* $Id: term.c,v 1.262 2017/06/02 19:21:23 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2017 Ingo Schwarze <schwarze@openbsd.org>
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 AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "mandoc.h"
29 #include "mandoc_aux.h"
30 #include "out.h"
31 #include "term.h"
32 #include "main.h"
33
34 static size_t cond_width(const struct termp *, int, int *);
35 static void adjbuf(struct termp *p, size_t);
36 static void bufferc(struct termp *, char);
37 static void encode(struct termp *, const char *, size_t);
38 static void encode1(struct termp *, int);
39
40
41 void
42 term_free(struct termp *p)
43 {
44
45 free(p->buf);
46 free(p->fontq);
47 free(p);
48 }
49
50 void
51 term_begin(struct termp *p, term_margin head,
52 term_margin foot, const struct roff_meta *arg)
53 {
54
55 p->headf = head;
56 p->footf = foot;
57 p->argf = arg;
58 (*p->begin)(p);
59 }
60
61 void
62 term_end(struct termp *p)
63 {
64
65 (*p->end)(p);
66 }
67
68 /*
69 * Flush a chunk of text. By default, break the output line each time
70 * the right margin is reached, and continue output on the next line
71 * at the same offset as the chunk itself. By default, also break the
72 * output line at the end of the chunk.
73 * The following flags may be specified:
74 *
75 * - TERMP_NOBREAK: Do not break the output line at the right margin,
76 * but only at the max right margin. Also, do not break the output
77 * line at the end of the chunk, such that the next call can pad to
78 * the next column. However, if less than p->trailspace blanks,
79 * which can be 0, 1, or 2, remain to the right margin, the line
80 * will be broken.
81 * - TERMP_BRTRSP: Consider trailing whitespace significant
82 * when deciding whether the chunk fits or not.
83 * - TERMP_BRIND: If the chunk does not fit and the output line has
84 * to be broken, start the next line at the right margin instead
85 * of at the offset. Used together with TERMP_NOBREAK for the tags
86 * in various kinds of tagged lists.
87 * - TERMP_DANGLE: Do not break the output line at the right margin,
88 * append the next chunk after it even if this one is too long.
89 * To be used together with TERMP_NOBREAK.
90 * - TERMP_HANG: Like TERMP_DANGLE, and also suppress padding before
91 * the next chunk if this column is not full.
92 */
93 void
94 term_flushln(struct termp *p)
95 {
96 size_t i; /* current input position in p->buf */
97 int ntab; /* number of tabs to prepend */
98 size_t vis; /* current visual position on output */
99 size_t vbl; /* number of blanks to prepend to output */
100 size_t vend; /* end of word visual position on output */
101 size_t bp; /* visual right border position */
102 size_t dv; /* temporary for visual pos calculations */
103 size_t j; /* temporary loop index for p->buf */
104 size_t jhy; /* last hyph before overflow w/r/t j */
105 size_t maxvis; /* output position of visible boundary */
106
107 /*
108 * First, establish the maximum columns of "visible" content.
109 * This is usually the difference between the right-margin and
110 * an indentation, but can be, for tagged lists or columns, a
111 * small set of values.
112 *
113 * The following unsigned-signed subtractions look strange,
114 * but they are actually correct. If the int p->overstep
115 * is negative, it gets sign extended. Subtracting that
116 * very large size_t effectively adds a small number to dv.
117 */
118 dv = p->rmargin > p->offset ? p->rmargin - p->offset : 0;
119 maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
120
121 if (p->flags & TERMP_NOBREAK) {
122 dv = p->maxrmargin > p->offset ?
123 p->maxrmargin - p->offset : 0;
124 bp = (int)dv > p->overstep ?
125 dv - (size_t)p->overstep : 0;
126 } else
127 bp = maxvis;
128
129 /*
130 * Calculate the required amount of padding.
131 */
132 vbl = p->offset + p->overstep > p->viscol ?
133 p->offset + p->overstep - p->viscol : 0;
134
135 vis = vend = 0;
136 i = 0;
137
138 while (i < p->col) {
139 /*
140 * Handle literal tab characters: collapse all
141 * subsequent tabs into a single huge set of spaces.
142 */
143 ntab = 0;
144 while (i < p->col && p->buf[i] == '\t') {
145 vend = term_tab_next(vis);
146 vbl += vend - vis;
147 vis = vend;
148 ntab++;
149 i++;
150 }
151
152 /*
153 * Count up visible word characters. Control sequences
154 * (starting with the CSI) aren't counted. A space
155 * generates a non-printing word, which is valid (the
156 * space is printed according to regular spacing rules).
157 */
158
159 for (j = i, jhy = 0; j < p->col; j++) {
160 if (' ' == p->buf[j] || '\t' == p->buf[j])
161 break;
162
163 /* Back over the last printed character. */
164 if (8 == p->buf[j]) {
165 assert(j);
166 vend -= (*p->width)(p, p->buf[j - 1]);
167 continue;
168 }
169
170 /* Regular word. */
171 /* Break at the hyphen point if we overrun. */
172 if (vend > vis && vend < bp &&
173 (ASCII_HYPH == p->buf[j] ||
174 ASCII_BREAK == p->buf[j]))
175 jhy = j;
176
177 /*
178 * Hyphenation now decided, put back a real
179 * hyphen such that we get the correct width.
180 */
181 if (ASCII_HYPH == p->buf[j])
182 p->buf[j] = '-';
183
184 vend += (*p->width)(p, p->buf[j]);
185 }
186
187 /*
188 * Find out whether we would exceed the right margin.
189 * If so, break to the next line.
190 */
191 if (vend > bp && 0 == jhy && vis > 0) {
192 vend -= vis;
193 (*p->endline)(p);
194 p->viscol = 0;
195
196 /* Use pending tabs on the new line. */
197
198 vbl = 0;
199 while (ntab--)
200 vbl = term_tab_next(vbl);
201
202 /* Re-establish indentation. */
203
204 if (p->flags & TERMP_BRIND) {
205 vbl += p->rmargin;
206 vend += p->rmargin - p->offset;
207 } else
208 vbl += p->offset;
209
210 /*
211 * Remove the p->overstep width.
212 * Again, if p->overstep is negative,
213 * sign extension does the right thing.
214 */
215
216 bp += (size_t)p->overstep;
217 p->overstep = 0;
218 }
219
220 /* Write out the [remaining] word. */
221 for ( ; i < p->col; i++) {
222 if (vend > bp && jhy > 0 && i > jhy)
223 break;
224 if ('\t' == p->buf[i])
225 break;
226 if (' ' == p->buf[i]) {
227 j = i;
228 while (i < p->col && ' ' == p->buf[i])
229 i++;
230 dv = (i - j) * (*p->width)(p, ' ');
231 vbl += dv;
232 vend += dv;
233 break;
234 }
235 if (ASCII_NBRSP == p->buf[i]) {
236 vbl += (*p->width)(p, ' ');
237 continue;
238 }
239 if (ASCII_BREAK == p->buf[i])
240 continue;
241
242 /*
243 * Now we definitely know there will be
244 * printable characters to output,
245 * so write preceding white space now.
246 */
247 if (vbl) {
248 (*p->advance)(p, vbl);
249 p->viscol += vbl;
250 vbl = 0;
251 }
252
253 (*p->letter)(p, p->buf[i]);
254 if (8 == p->buf[i])
255 p->viscol -= (*p->width)(p, p->buf[i-1]);
256 else
257 p->viscol += (*p->width)(p, p->buf[i]);
258 }
259 vis = vend;
260 }
261
262 /*
263 * If there was trailing white space, it was not printed;
264 * so reset the cursor position accordingly.
265 */
266 if (vis > vbl)
267 vis -= vbl;
268 else
269 vis = 0;
270
271 p->col = 0;
272 p->overstep = 0;
273 p->flags &= ~(TERMP_BACKAFTER | TERMP_BACKBEFORE);
274
275 if ( ! (TERMP_NOBREAK & p->flags)) {
276 p->viscol = 0;
277 (*p->endline)(p);
278 return;
279 }
280
281 if (TERMP_HANG & p->flags) {
282 p->overstep += (int)(p->offset + vis - p->rmargin +
283 p->trailspace * (*p->width)(p, ' '));
284
285 /*
286 * If we have overstepped the margin, temporarily move
287 * it to the right and flag the rest of the line to be
288 * shorter.
289 * If there is a request to keep the columns together,
290 * allow negative overstep when the column is not full.
291 */
292 if (p->trailspace && p->overstep < 0)
293 p->overstep = 0;
294 return;
295
296 } else if (TERMP_DANGLE & p->flags)
297 return;
298
299 /* Trailing whitespace is significant in some columns. */
300 if (vis && vbl && (TERMP_BRTRSP & p->flags))
301 vis += vbl;
302
303 /* If the column was overrun, break the line. */
304 if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
305 (*p->endline)(p);
306 p->viscol = 0;
307 }
308 }
309
310 /*
311 * A newline only breaks an existing line; it won't assert vertical
312 * space. All data in the output buffer is flushed prior to the newline
313 * assertion.
314 */
315 void
316 term_newln(struct termp *p)
317 {
318
319 p->flags |= TERMP_NOSPACE;
320 if (p->col || p->viscol)
321 term_flushln(p);
322 }
323
324 /*
325 * Asserts a vertical space (a full, empty line-break between lines).
326 * Note that if used twice, this will cause two blank spaces and so on.
327 * All data in the output buffer is flushed prior to the newline
328 * assertion.
329 */
330 void
331 term_vspace(struct termp *p)
332 {
333
334 term_newln(p);
335 p->viscol = 0;
336 if (0 < p->skipvsp)
337 p->skipvsp--;
338 else
339 (*p->endline)(p);
340 }
341
342 /* Swap current and previous font; for \fP and .ft P */
343 void
344 term_fontlast(struct termp *p)
345 {
346 enum termfont f;
347
348 f = p->fontl;
349 p->fontl = p->fontq[p->fonti];
350 p->fontq[p->fonti] = f;
351 }
352
353 /* Set font, save current, discard previous; for \f, .ft, .B etc. */
354 void
355 term_fontrepl(struct termp *p, enum termfont f)
356 {
357
358 p->fontl = p->fontq[p->fonti];
359 p->fontq[p->fonti] = f;
360 }
361
362 /* Set font, save previous. */
363 void
364 term_fontpush(struct termp *p, enum termfont f)
365 {
366
367 p->fontl = p->fontq[p->fonti];
368 if (++p->fonti == p->fontsz) {
369 p->fontsz += 8;
370 p->fontq = mandoc_reallocarray(p->fontq,
371 p->fontsz, sizeof(*p->fontq));
372 }
373 p->fontq[p->fonti] = f;
374 }
375
376 /* Flush to make the saved pointer current again. */
377 void
378 term_fontpopq(struct termp *p, int i)
379 {
380
381 assert(i >= 0);
382 if (p->fonti > i)
383 p->fonti = i;
384 }
385
386 /* Pop one font off the stack. */
387 void
388 term_fontpop(struct termp *p)
389 {
390
391 assert(p->fonti);
392 p->fonti--;
393 }
394
395 /*
396 * Handle pwords, partial words, which may be either a single word or a
397 * phrase that cannot be broken down (such as a literal string). This
398 * handles word styling.
399 */
400 void
401 term_word(struct termp *p, const char *word)
402 {
403 struct roffsu su;
404 const char nbrsp[2] = { ASCII_NBRSP, 0 };
405 const char *seq, *cp;
406 int sz, uc;
407 size_t csz, lsz, ssz;
408 enum mandoc_esc esc;
409
410 if ( ! (TERMP_NOSPACE & p->flags)) {
411 if ( ! (TERMP_KEEP & p->flags)) {
412 bufferc(p, ' ');
413 if (TERMP_SENTENCE & p->flags)
414 bufferc(p, ' ');
415 } else
416 bufferc(p, ASCII_NBRSP);
417 }
418 if (TERMP_PREKEEP & p->flags)
419 p->flags |= TERMP_KEEP;
420
421 if ( ! (p->flags & TERMP_NONOSPACE))
422 p->flags &= ~TERMP_NOSPACE;
423 else
424 p->flags |= TERMP_NOSPACE;
425
426 p->flags &= ~(TERMP_SENTENCE | TERMP_NONEWLINE);
427 p->skipvsp = 0;
428
429 while ('\0' != *word) {
430 if ('\\' != *word) {
431 if (TERMP_NBRWORD & p->flags) {
432 if (' ' == *word) {
433 encode(p, nbrsp, 1);
434 word++;
435 continue;
436 }
437 ssz = strcspn(word, "\\ ");
438 } else
439 ssz = strcspn(word, "\\");
440 encode(p, word, ssz);
441 word += (int)ssz;
442 continue;
443 }
444
445 word++;
446 esc = mandoc_escape(&word, &seq, &sz);
447 if (ESCAPE_ERROR == esc)
448 continue;
449
450 switch (esc) {
451 case ESCAPE_UNICODE:
452 uc = mchars_num2uc(seq + 1, sz - 1);
453 break;
454 case ESCAPE_NUMBERED:
455 uc = mchars_num2char(seq, sz);
456 if (uc < 0)
457 continue;
458 break;
459 case ESCAPE_SPECIAL:
460 if (p->enc == TERMENC_ASCII) {
461 cp = mchars_spec2str(seq, sz, &ssz);
462 if (cp != NULL)
463 encode(p, cp, ssz);
464 } else {
465 uc = mchars_spec2cp(seq, sz);
466 if (uc > 0)
467 encode1(p, uc);
468 }
469 continue;
470 case ESCAPE_FONTBOLD:
471 term_fontrepl(p, TERMFONT_BOLD);
472 continue;
473 case ESCAPE_FONTITALIC:
474 term_fontrepl(p, TERMFONT_UNDER);
475 continue;
476 case ESCAPE_FONTBI:
477 term_fontrepl(p, TERMFONT_BI);
478 continue;
479 case ESCAPE_FONT:
480 case ESCAPE_FONTROMAN:
481 term_fontrepl(p, TERMFONT_NONE);
482 continue;
483 case ESCAPE_FONTPREV:
484 term_fontlast(p);
485 continue;
486 case ESCAPE_NOSPACE:
487 if (p->flags & TERMP_BACKAFTER)
488 p->flags &= ~TERMP_BACKAFTER;
489 else if (*word == '\0')
490 p->flags |= (TERMP_NOSPACE | TERMP_NONEWLINE);
491 continue;
492 case ESCAPE_HORIZ:
493 if (a2roffsu(seq, &su, SCALE_EM) == 0)
494 continue;
495 uc = term_hspan(p, &su) / 24;
496 if (uc > 0)
497 while (uc-- > 0)
498 bufferc(p, ASCII_NBRSP);
499 else if (p->col > (size_t)(-uc))
500 p->col += uc;
501 else {
502 uc += p->col;
503 p->col = 0;
504 if (p->offset > (size_t)(-uc)) {
505 p->ti += uc;
506 p->offset += uc;
507 } else {
508 p->ti -= p->offset;
509 p->offset = 0;
510 }
511 }
512 continue;
513 case ESCAPE_HLINE:
514 if (a2roffsu(seq, &su, SCALE_EM) == 0)
515 continue;
516 uc = term_hspan(p, &su) / 24;
517 if (uc <= 0) {
518 if (p->rmargin <= p->offset)
519 continue;
520 lsz = p->rmargin - p->offset;
521 } else
522 lsz = uc;
523 while (sz &&
524 strchr(" %&()*+-./0123456789:<=>", *seq)) {
525 seq++;
526 sz--;
527 }
528 if (sz && strchr("cifMmnPpuv", *seq)) {
529 seq++;
530 sz--;
531 }
532 if (sz == 0)
533 uc = -1;
534 else if (*seq == '\\') {
535 seq++;
536 esc = mandoc_escape(&seq, &cp, &sz);
537 switch (esc) {
538 case ESCAPE_UNICODE:
539 uc = mchars_num2uc(cp + 1, sz - 1);
540 break;
541 case ESCAPE_NUMBERED:
542 uc = mchars_num2char(cp, sz);
543 break;
544 case ESCAPE_SPECIAL:
545 uc = mchars_spec2cp(cp, sz);
546 break;
547 default:
548 uc = -1;
549 break;
550 }
551 } else
552 uc = *seq;
553 if (uc < 0x20 || (uc > 0x7E && uc < 0xA0))
554 uc = '_';
555 if (p->enc == TERMENC_ASCII) {
556 cp = ascii_uc2str(uc);
557 csz = term_strlen(p, cp);
558 ssz = strlen(cp);
559 } else
560 csz = (*p->width)(p, uc);
561 while (lsz >= csz) {
562 if (p->enc == TERMENC_ASCII)
563 encode(p, cp, ssz);
564 else
565 encode1(p, uc);
566 lsz -= csz;
567 }
568 continue;
569 case ESCAPE_SKIPCHAR:
570 p->flags |= TERMP_BACKAFTER;
571 continue;
572 case ESCAPE_OVERSTRIKE:
573 cp = seq + sz;
574 while (seq < cp) {
575 if (*seq == '\\') {
576 mandoc_escape(&seq, NULL, NULL);
577 continue;
578 }
579 encode1(p, *seq++);
580 if (seq < cp) {
581 if (p->flags & TERMP_BACKBEFORE)
582 p->flags |= TERMP_BACKAFTER;
583 else
584 p->flags |= TERMP_BACKBEFORE;
585 }
586 }
587 /* Trim trailing backspace/blank pair. */
588 if (p->col > 2 &&
589 (p->buf[p->col - 1] == ' ' ||
590 p->buf[p->col - 1] == '\t'))
591 p->col -= 2;
592 continue;
593 default:
594 continue;
595 }
596
597 /*
598 * Common handling for Unicode and numbered
599 * character escape sequences.
600 */
601
602 if (p->enc == TERMENC_ASCII) {
603 cp = ascii_uc2str(uc);
604 encode(p, cp, strlen(cp));
605 } else {
606 if ((uc < 0x20 && uc != 0x09) ||
607 (uc > 0x7E && uc < 0xA0))
608 uc = 0xFFFD;
609 encode1(p, uc);
610 }
611 }
612 p->flags &= ~TERMP_NBRWORD;
613 }
614
615 static void
616 adjbuf(struct termp *p, size_t sz)
617 {
618
619 if (0 == p->maxcols)
620 p->maxcols = 1024;
621 while (sz >= p->maxcols)
622 p->maxcols <<= 2;
623
624 p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int));
625 }
626
627 static void
628 bufferc(struct termp *p, char c)
629 {
630
631 if (p->col + 1 >= p->maxcols)
632 adjbuf(p, p->col + 1);
633
634 p->buf[p->col++] = c;
635 }
636
637 /*
638 * See encode().
639 * Do this for a single (probably unicode) value.
640 * Does not check for non-decorated glyphs.
641 */
642 static void
643 encode1(struct termp *p, int c)
644 {
645 enum termfont f;
646
647 if (p->col + 7 >= p->maxcols)
648 adjbuf(p, p->col + 7);
649
650 f = (c == ASCII_HYPH || c > 127 || isgraph(c)) ?
651 p->fontq[p->fonti] : TERMFONT_NONE;
652
653 if (p->flags & TERMP_BACKBEFORE) {
654 if (p->buf[p->col - 1] == ' ' || p->buf[p->col - 1] == '\t')
655 p->col--;
656 else
657 p->buf[p->col++] = 8;
658 p->flags &= ~TERMP_BACKBEFORE;
659 }
660 if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
661 p->buf[p->col++] = '_';
662 p->buf[p->col++] = 8;
663 }
664 if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
665 if (ASCII_HYPH == c)
666 p->buf[p->col++] = '-';
667 else
668 p->buf[p->col++] = c;
669 p->buf[p->col++] = 8;
670 }
671 p->buf[p->col++] = c;
672 if (p->flags & TERMP_BACKAFTER) {
673 p->flags |= TERMP_BACKBEFORE;
674 p->flags &= ~TERMP_BACKAFTER;
675 }
676 }
677
678 static void
679 encode(struct termp *p, const char *word, size_t sz)
680 {
681 size_t i;
682
683 if (p->col + 2 + (sz * 5) >= p->maxcols)
684 adjbuf(p, p->col + 2 + (sz * 5));
685
686 for (i = 0; i < sz; i++) {
687 if (ASCII_HYPH == word[i] ||
688 isgraph((unsigned char)word[i]))
689 encode1(p, word[i]);
690 else {
691 p->buf[p->col++] = word[i];
692
693 /*
694 * Postpone the effect of \z while handling
695 * an overstrike sequence from ascii_uc2str().
696 */
697
698 if (word[i] == '\b' &&
699 (p->flags & TERMP_BACKBEFORE)) {
700 p->flags &= ~TERMP_BACKBEFORE;
701 p->flags |= TERMP_BACKAFTER;
702 }
703 }
704 }
705 }
706
707 void
708 term_setwidth(struct termp *p, const char *wstr)
709 {
710 struct roffsu su;
711 int iop, width;
712
713 iop = 0;
714 width = 0;
715 if (NULL != wstr) {
716 switch (*wstr) {
717 case '+':
718 iop = 1;
719 wstr++;
720 break;
721 case '-':
722 iop = -1;
723 wstr++;
724 break;
725 default:
726 break;
727 }
728 if (a2roffsu(wstr, &su, SCALE_MAX))
729 width = term_hspan(p, &su);
730 else
731 iop = 0;
732 }
733 (*p->setwidth)(p, iop, width);
734 }
735
736 size_t
737 term_len(const struct termp *p, size_t sz)
738 {
739
740 return (*p->width)(p, ' ') * sz;
741 }
742
743 static size_t
744 cond_width(const struct termp *p, int c, int *skip)
745 {
746
747 if (*skip) {
748 (*skip) = 0;
749 return 0;
750 } else
751 return (*p->width)(p, c);
752 }
753
754 size_t
755 term_strlen(const struct termp *p, const char *cp)
756 {
757 size_t sz, rsz, i;
758 int ssz, skip, uc;
759 const char *seq, *rhs;
760 enum mandoc_esc esc;
761 static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
762 ASCII_BREAK, '\0' };
763
764 /*
765 * Account for escaped sequences within string length
766 * calculations. This follows the logic in term_word() as we
767 * must calculate the width of produced strings.
768 */
769
770 sz = 0;
771 skip = 0;
772 while ('\0' != *cp) {
773 rsz = strcspn(cp, rej);
774 for (i = 0; i < rsz; i++)
775 sz += cond_width(p, *cp++, &skip);
776
777 switch (*cp) {
778 case '\\':
779 cp++;
780 esc = mandoc_escape(&cp, &seq, &ssz);
781 if (ESCAPE_ERROR == esc)
782 continue;
783
784 rhs = NULL;
785
786 switch (esc) {
787 case ESCAPE_UNICODE:
788 uc = mchars_num2uc(seq + 1, ssz - 1);
789 break;
790 case ESCAPE_NUMBERED:
791 uc = mchars_num2char(seq, ssz);
792 if (uc < 0)
793 continue;
794 break;
795 case ESCAPE_SPECIAL:
796 if (p->enc == TERMENC_ASCII) {
797 rhs = mchars_spec2str(seq, ssz, &rsz);
798 if (rhs != NULL)
799 break;
800 } else {
801 uc = mchars_spec2cp(seq, ssz);
802 if (uc > 0)
803 sz += cond_width(p, uc, &skip);
804 }
805 continue;
806 case ESCAPE_SKIPCHAR:
807 skip = 1;
808 continue;
809 case ESCAPE_OVERSTRIKE:
810 rsz = 0;
811 rhs = seq + ssz;
812 while (seq < rhs) {
813 if (*seq == '\\') {
814 mandoc_escape(&seq, NULL, NULL);
815 continue;
816 }
817 i = (*p->width)(p, *seq++);
818 if (rsz < i)
819 rsz = i;
820 }
821 sz += rsz;
822 continue;
823 default:
824 continue;
825 }
826
827 /*
828 * Common handling for Unicode and numbered
829 * character escape sequences.
830 */
831
832 if (rhs == NULL) {
833 if (p->enc == TERMENC_ASCII) {
834 rhs = ascii_uc2str(uc);
835 rsz = strlen(rhs);
836 } else {
837 if ((uc < 0x20 && uc != 0x09) ||
838 (uc > 0x7E && uc < 0xA0))
839 uc = 0xFFFD;
840 sz += cond_width(p, uc, &skip);
841 continue;
842 }
843 }
844
845 if (skip) {
846 skip = 0;
847 break;
848 }
849
850 /*
851 * Common handling for all escape sequences
852 * printing more than one character.
853 */
854
855 for (i = 0; i < rsz; i++)
856 sz += (*p->width)(p, *rhs++);
857 break;
858 case ASCII_NBRSP:
859 sz += cond_width(p, ' ', &skip);
860 cp++;
861 break;
862 case ASCII_HYPH:
863 sz += cond_width(p, '-', &skip);
864 cp++;
865 break;
866 default:
867 break;
868 }
869 }
870
871 return sz;
872 }
873
874 int
875 term_vspan(const struct termp *p, const struct roffsu *su)
876 {
877 double r;
878 int ri;
879
880 switch (su->unit) {
881 case SCALE_BU:
882 r = su->scale / 40.0;
883 break;
884 case SCALE_CM:
885 r = su->scale * 6.0 / 2.54;
886 break;
887 case SCALE_FS:
888 r = su->scale * 65536.0 / 40.0;
889 break;
890 case SCALE_IN:
891 r = su->scale * 6.0;
892 break;
893 case SCALE_MM:
894 r = su->scale * 0.006;
895 break;
896 case SCALE_PC:
897 r = su->scale;
898 break;
899 case SCALE_PT:
900 r = su->scale / 12.0;
901 break;
902 case SCALE_EN:
903 case SCALE_EM:
904 r = su->scale * 0.6;
905 break;
906 case SCALE_VS:
907 r = su->scale;
908 break;
909 default:
910 abort();
911 }
912 ri = r > 0.0 ? r + 0.4995 : r - 0.4995;
913 return ri < 66 ? ri : 1;
914 }
915
916 /*
917 * Convert a scaling width to basic units, rounding down.
918 */
919 int
920 term_hspan(const struct termp *p, const struct roffsu *su)
921 {
922
923 return (*p->hspan)(p, su);
924 }