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