xref: /titanic_50/usr/src/cmd/mandoc/term.c (revision 42cc51e07cdbcad3b9aca8d9d991fc09b251feb7)
1 /*	$Id: term.c,v 1.245 2015/03/06 13:02:43 schwarze Exp $ */
2 /*
3  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2010-2015 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 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 #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 void *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_BRIND: If the chunk does not fit and the output line has
82  *    to be broken, start the next line at the right margin instead
83  *    of at the offset.  Used together with TERMP_NOBREAK for the tags
84  *    in various kinds of tagged lists.
85  *  - TERMP_DANGLE: Do not break the output line at the right margin,
86  *    append the next chunk after it even if this one is too long.
87  *    To be used together with TERMP_NOBREAK.
88  *  - TERMP_HANG: Like TERMP_DANGLE, and also suppress padding before
89  *    the next chunk if this column is not full.
90  */
91 void
92 term_flushln(struct termp *p)
93 {
94 	size_t		 i;     /* current input position in p->buf */
95 	int		 ntab;	/* number of tabs to prepend */
96 	size_t		 vis;   /* current visual position on output */
97 	size_t		 vbl;   /* number of blanks to prepend to output */
98 	size_t		 vend;	/* end of word visual position on output */
99 	size_t		 bp;    /* visual right border position */
100 	size_t		 dv;    /* temporary for visual pos calculations */
101 	size_t		 j;     /* temporary loop index for p->buf */
102 	size_t		 jhy;	/* last hyph before overflow w/r/t j */
103 	size_t		 maxvis; /* output position of visible boundary */
104 
105 	/*
106 	 * First, establish the maximum columns of "visible" content.
107 	 * This is usually the difference between the right-margin and
108 	 * an indentation, but can be, for tagged lists or columns, a
109 	 * small set of values.
110 	 *
111 	 * The following unsigned-signed subtractions look strange,
112 	 * but they are actually correct.  If the int p->overstep
113 	 * is negative, it gets sign extended.  Subtracting that
114 	 * very large size_t effectively adds a small number to dv.
115 	 */
116 	dv = p->rmargin > p->offset ? p->rmargin - p->offset : 0;
117 	maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
118 
119 	if (p->flags & TERMP_NOBREAK) {
120 		dv = p->maxrmargin > p->offset ?
121 		     p->maxrmargin - p->offset : 0;
122 		bp = (int)dv > p->overstep ?
123 		     dv - (size_t)p->overstep : 0;
124 	} else
125 		bp = maxvis;
126 
127 	/*
128 	 * Calculate the required amount of padding.
129 	 */
130 	vbl = p->offset + p->overstep > p->viscol ?
131 	      p->offset + p->overstep - p->viscol : 0;
132 
133 	vis = vend = 0;
134 	i = 0;
135 
136 	while (i < p->col) {
137 		/*
138 		 * Handle literal tab characters: collapse all
139 		 * subsequent tabs into a single huge set of spaces.
140 		 */
141 		ntab = 0;
142 		while (i < p->col && '\t' == p->buf[i]) {
143 			vend = (vis / p->tabwidth + 1) * p->tabwidth;
144 			vbl += vend - vis;
145 			vis = vend;
146 			ntab++;
147 			i++;
148 		}
149 
150 		/*
151 		 * Count up visible word characters.  Control sequences
152 		 * (starting with the CSI) aren't counted.  A space
153 		 * generates a non-printing word, which is valid (the
154 		 * space is printed according to regular spacing rules).
155 		 */
156 
157 		for (j = i, jhy = 0; j < p->col; j++) {
158 			if (' ' == p->buf[j] || '\t' == p->buf[j])
159 				break;
160 
161 			/* Back over the the last printed character. */
162 			if (8 == p->buf[j]) {
163 				assert(j);
164 				vend -= (*p->width)(p, p->buf[j - 1]);
165 				continue;
166 			}
167 
168 			/* Regular word. */
169 			/* Break at the hyphen point if we overrun. */
170 			if (vend > vis && vend < bp &&
171 			    (ASCII_HYPH == p->buf[j] ||
172 			     ASCII_BREAK == p->buf[j]))
173 				jhy = j;
174 
175 			/*
176 			 * Hyphenation now decided, put back a real
177 			 * hyphen such that we get the correct width.
178 			 */
179 			if (ASCII_HYPH == p->buf[j])
180 				p->buf[j] = '-';
181 
182 			vend += (*p->width)(p, p->buf[j]);
183 		}
184 
185 		/*
186 		 * Find out whether we would exceed the right margin.
187 		 * If so, break to the next line.
188 		 */
189 		if (vend > bp && 0 == jhy && vis > 0) {
190 			vend -= vis;
191 			(*p->endline)(p);
192 			p->viscol = 0;
193 			if (TERMP_BRIND & p->flags) {
194 				vbl = p->rmargin;
195 				vend += p->rmargin;
196 				vend -= p->offset;
197 			} else
198 				vbl = p->offset;
199 
200 			/* use pending tabs on the new line */
201 
202 			if (0 < ntab)
203 				vbl += ntab * p->tabwidth;
204 
205 			/*
206 			 * Remove the p->overstep width.
207 			 * Again, if p->overstep is negative,
208 			 * sign extension does the right thing.
209 			 */
210 
211 			bp += (size_t)p->overstep;
212 			p->overstep = 0;
213 		}
214 
215 		/* Write out the [remaining] word. */
216 		for ( ; i < p->col; i++) {
217 			if (vend > bp && jhy > 0 && i > jhy)
218 				break;
219 			if ('\t' == p->buf[i])
220 				break;
221 			if (' ' == p->buf[i]) {
222 				j = i;
223 				while (i < p->col && ' ' == p->buf[i])
224 					i++;
225 				dv = (i - j) * (*p->width)(p, ' ');
226 				vbl += dv;
227 				vend += dv;
228 				break;
229 			}
230 			if (ASCII_NBRSP == p->buf[i]) {
231 				vbl += (*p->width)(p, ' ');
232 				continue;
233 			}
234 			if (ASCII_BREAK == p->buf[i])
235 				continue;
236 
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) {
243 				(*p->advance)(p, vbl);
244 				p->viscol += vbl;
245 				vbl = 0;
246 			}
247 
248 			(*p->letter)(p, p->buf[i]);
249 			if (8 == p->buf[i])
250 				p->viscol -= (*p->width)(p, p->buf[i-1]);
251 			else
252 				p->viscol += (*p->width)(p, p->buf[i]);
253 		}
254 		vis = vend;
255 	}
256 
257 	/*
258 	 * If there was trailing white space, it was not printed;
259 	 * so reset the cursor position accordingly.
260 	 */
261 	if (vis > vbl)
262 		vis -= vbl;
263 	else
264 		vis = 0;
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) {
276 		p->overstep += (int)(p->offset + vis - p->rmargin +
277 		    p->trailspace * (*p->width)(p, ' '));
278 
279 		/*
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.
283 		 * If there is a request to keep the columns together,
284 		 * allow negative overstep when the column is not full.
285 		 */
286 		if (p->trailspace && p->overstep < 0)
287 			p->overstep = 0;
288 		return;
289 
290 	} else if (TERMP_DANGLE & p->flags)
291 		return;
292 
293 	/* If the column was overrun, break the line. */
294 	if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
295 		(*p->endline)(p);
296 		p->viscol = 0;
297 	}
298 }
299 
300 /*
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)
307 {
308 
309 	p->flags |= TERMP_NOSPACE;
310 	if (p->col || p->viscol)
311 		term_flushln(p);
312 }
313 
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)
322 {
323 
324 	term_newln(p);
325 	p->viscol = 0;
326 	if (0 < p->skipvsp)
327 		p->skipvsp--;
328 	else
329 		(*p->endline)(p);
330 }
331 
332 /* Swap current and previous font; for \fP and .ft P */
333 void
334 term_fontlast(struct termp *p)
335 {
336 	enum termfont	 f;
337 
338 	f = p->fontl;
339 	p->fontl = p->fontq[p->fonti];
340 	p->fontq[p->fonti] = f;
341 }
342 
343 /* Set font, save current, discard previous; for \f, .ft, .B etc. */
344 void
345 term_fontrepl(struct termp *p, enum termfont f)
346 {
347 
348 	p->fontl = p->fontq[p->fonti];
349 	p->fontq[p->fonti] = f;
350 }
351 
352 /* Set font, save previous. */
353 void
354 term_fontpush(struct termp *p, enum termfont f)
355 {
356 
357 	p->fontl = p->fontq[p->fonti];
358 	if (++p->fonti == p->fontsz) {
359 		p->fontsz += 8;
360 		p->fontq = mandoc_reallocarray(p->fontq,
361 		    p->fontsz, sizeof(enum termfont *));
362 	}
363 	p->fontq[p->fonti] = f;
364 }
365 
366 /* Flush to make the saved pointer current again. */
367 void
368 term_fontpopq(struct termp *p, int i)
369 {
370 
371 	assert(i >= 0);
372 	if (p->fonti > i)
373 		p->fonti = i;
374 }
375 
376 /* Pop one font off the stack. */
377 void
378 term_fontpop(struct termp *p)
379 {
380 
381 	assert(p->fonti);
382 	p->fonti--;
383 }
384 
385 /*
386  * Handle pwords, partial words, which may be either a single word or a
387  * phrase that cannot be broken down (such as a literal string).  This
388  * handles word styling.
389  */
390 void
391 term_word(struct termp *p, const char *word)
392 {
393 	const char	 nbrsp[2] = { ASCII_NBRSP, 0 };
394 	const char	*seq, *cp;
395 	int		 sz, uc;
396 	size_t		 ssz;
397 	enum mandoc_esc	 esc;
398 
399 	if ( ! (TERMP_NOSPACE & p->flags)) {
400 		if ( ! (TERMP_KEEP & p->flags)) {
401 			bufferc(p, ' ');
402 			if (TERMP_SENTENCE & p->flags)
403 				bufferc(p, ' ');
404 		} else
405 			bufferc(p, ASCII_NBRSP);
406 	}
407 	if (TERMP_PREKEEP & p->flags)
408 		p->flags |= TERMP_KEEP;
409 
410 	if ( ! (p->flags & TERMP_NONOSPACE))
411 		p->flags &= ~TERMP_NOSPACE;
412 	else
413 		p->flags |= TERMP_NOSPACE;
414 
415 	p->flags &= ~(TERMP_SENTENCE | TERMP_NONEWLINE);
416 	p->skipvsp = 0;
417 
418 	while ('\0' != *word) {
419 		if ('\\' != *word) {
420 			if (TERMP_SKIPCHAR & p->flags) {
421 				p->flags &= ~TERMP_SKIPCHAR;
422 				word++;
423 				continue;
424 			}
425 			if (TERMP_NBRWORD & p->flags) {
426 				if (' ' == *word) {
427 					encode(p, nbrsp, 1);
428 					word++;
429 					continue;
430 				}
431 				ssz = strcspn(word, "\\ ");
432 			} else
433 				ssz = strcspn(word, "\\");
434 			encode(p, word, ssz);
435 			word += (int)ssz;
436 			continue;
437 		}
438 
439 		word++;
440 		esc = mandoc_escape(&word, &seq, &sz);
441 		if (ESCAPE_ERROR == esc)
442 			continue;
443 
444 		switch (esc) {
445 		case ESCAPE_UNICODE:
446 			uc = mchars_num2uc(seq + 1, sz - 1);
447 			break;
448 		case ESCAPE_NUMBERED:
449 			uc = mchars_num2char(seq, sz);
450 			if (uc < 0)
451 				continue;
452 			break;
453 		case ESCAPE_SPECIAL:
454 			if (p->enc == TERMENC_ASCII) {
455 				cp = mchars_spec2str(p->symtab,
456 				    seq, sz, &ssz);
457 				if (cp != NULL)
458 					encode(p, cp, ssz);
459 			} else {
460 				uc = mchars_spec2cp(p->symtab, seq, sz);
461 				if (uc > 0)
462 					encode1(p, uc);
463 			}
464 			continue;
465 		case ESCAPE_FONTBOLD:
466 			term_fontrepl(p, TERMFONT_BOLD);
467 			continue;
468 		case ESCAPE_FONTITALIC:
469 			term_fontrepl(p, TERMFONT_UNDER);
470 			continue;
471 		case ESCAPE_FONTBI:
472 			term_fontrepl(p, TERMFONT_BI);
473 			continue;
474 		case ESCAPE_FONT:
475 			/* FALLTHROUGH */
476 		case ESCAPE_FONTROMAN:
477 			term_fontrepl(p, TERMFONT_NONE);
478 			continue;
479 		case ESCAPE_FONTPREV:
480 			term_fontlast(p);
481 			continue;
482 		case ESCAPE_NOSPACE:
483 			if (TERMP_SKIPCHAR & p->flags)
484 				p->flags &= ~TERMP_SKIPCHAR;
485 			else if ('\0' == *word)
486 				p->flags |= (TERMP_NOSPACE | TERMP_NONEWLINE);
487 			continue;
488 		case ESCAPE_SKIPCHAR:
489 			p->flags |= TERMP_SKIPCHAR;
490 			continue;
491 		case ESCAPE_OVERSTRIKE:
492 			cp = seq + sz;
493 			while (seq < cp) {
494 				if (*seq == '\\') {
495 					mandoc_escape(&seq, NULL, NULL);
496 					continue;
497 				}
498 				encode1(p, *seq++);
499 				if (seq < cp)
500 					encode(p, "\b", 1);
501 			}
502 		default:
503 			continue;
504 		}
505 
506 		/*
507 		 * Common handling for Unicode and numbered
508 		 * character escape sequences.
509 		 */
510 
511 		if (p->enc == TERMENC_ASCII) {
512 			cp = ascii_uc2str(uc);
513 			encode(p, cp, strlen(cp));
514 		} else {
515 			if ((uc < 0x20 && uc != 0x09) ||
516 			    (uc > 0x7E && uc < 0xA0))
517 				uc = 0xFFFD;
518 			encode1(p, uc);
519 		}
520 	}
521 	p->flags &= ~TERMP_NBRWORD;
522 }
523 
524 static void
525 adjbuf(struct termp *p, size_t sz)
526 {
527 
528 	if (0 == p->maxcols)
529 		p->maxcols = 1024;
530 	while (sz >= p->maxcols)
531 		p->maxcols <<= 2;
532 
533 	p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int));
534 }
535 
536 static void
537 bufferc(struct termp *p, char c)
538 {
539 
540 	if (p->col + 1 >= p->maxcols)
541 		adjbuf(p, p->col + 1);
542 
543 	p->buf[p->col++] = c;
544 }
545 
546 /*
547  * See encode().
548  * Do this for a single (probably unicode) value.
549  * Does not check for non-decorated glyphs.
550  */
551 static void
552 encode1(struct termp *p, int c)
553 {
554 	enum termfont	  f;
555 
556 	if (TERMP_SKIPCHAR & p->flags) {
557 		p->flags &= ~TERMP_SKIPCHAR;
558 		return;
559 	}
560 
561 	if (p->col + 6 >= p->maxcols)
562 		adjbuf(p, p->col + 6);
563 
564 	f = p->fontq[p->fonti];
565 
566 	if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
567 		p->buf[p->col++] = '_';
568 		p->buf[p->col++] = 8;
569 	}
570 	if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
571 		if (ASCII_HYPH == c)
572 			p->buf[p->col++] = '-';
573 		else
574 			p->buf[p->col++] = c;
575 		p->buf[p->col++] = 8;
576 	}
577 	p->buf[p->col++] = c;
578 }
579 
580 static void
581 encode(struct termp *p, const char *word, size_t sz)
582 {
583 	size_t		  i;
584 
585 	if (TERMP_SKIPCHAR & p->flags) {
586 		p->flags &= ~TERMP_SKIPCHAR;
587 		return;
588 	}
589 
590 	/*
591 	 * Encode and buffer a string of characters.  If the current
592 	 * font mode is unset, buffer directly, else encode then buffer
593 	 * character by character.
594 	 */
595 
596 	if (p->fontq[p->fonti] == TERMFONT_NONE) {
597 		if (p->col + sz >= p->maxcols)
598 			adjbuf(p, p->col + sz);
599 		for (i = 0; i < sz; i++)
600 			p->buf[p->col++] = word[i];
601 		return;
602 	}
603 
604 	/* Pre-buffer, assuming worst-case. */
605 
606 	if (p->col + 1 + (sz * 5) >= p->maxcols)
607 		adjbuf(p, p->col + 1 + (sz * 5));
608 
609 	for (i = 0; i < sz; i++) {
610 		if (ASCII_HYPH == word[i] ||
611 		    isgraph((unsigned char)word[i]))
612 			encode1(p, word[i]);
613 		else
614 			p->buf[p->col++] = word[i];
615 	}
616 }
617 
618 void
619 term_setwidth(struct termp *p, const char *wstr)
620 {
621 	struct roffsu	 su;
622 	size_t		 width;
623 	int		 iop;
624 
625 	iop = 0;
626 	width = 0;
627 	if (NULL != wstr) {
628 		switch (*wstr) {
629 		case '+':
630 			iop = 1;
631 			wstr++;
632 			break;
633 		case '-':
634 			iop = -1;
635 			wstr++;
636 			break;
637 		default:
638 			break;
639 		}
640 		if (a2roffsu(wstr, &su, SCALE_MAX))
641 			width = term_hspan(p, &su);
642 		else
643 			iop = 0;
644 	}
645 	(*p->setwidth)(p, iop, width);
646 }
647 
648 size_t
649 term_len(const struct termp *p, size_t sz)
650 {
651 
652 	return((*p->width)(p, ' ') * sz);
653 }
654 
655 static size_t
656 cond_width(const struct termp *p, int c, int *skip)
657 {
658 
659 	if (*skip) {
660 		(*skip) = 0;
661 		return(0);
662 	} else
663 		return((*p->width)(p, c));
664 }
665 
666 size_t
667 term_strlen(const struct termp *p, const char *cp)
668 {
669 	size_t		 sz, rsz, i;
670 	int		 ssz, skip, uc;
671 	const char	*seq, *rhs;
672 	enum mandoc_esc	 esc;
673 	static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
674 			ASCII_BREAK, '\0' };
675 
676 	/*
677 	 * Account for escaped sequences within string length
678 	 * calculations.  This follows the logic in term_word() as we
679 	 * must calculate the width of produced strings.
680 	 */
681 
682 	sz = 0;
683 	skip = 0;
684 	while ('\0' != *cp) {
685 		rsz = strcspn(cp, rej);
686 		for (i = 0; i < rsz; i++)
687 			sz += cond_width(p, *cp++, &skip);
688 
689 		switch (*cp) {
690 		case '\\':
691 			cp++;
692 			esc = mandoc_escape(&cp, &seq, &ssz);
693 			if (ESCAPE_ERROR == esc)
694 				continue;
695 
696 			rhs = NULL;
697 
698 			switch (esc) {
699 			case ESCAPE_UNICODE:
700 				uc = mchars_num2uc(seq + 1, ssz - 1);
701 				break;
702 			case ESCAPE_NUMBERED:
703 				uc = mchars_num2char(seq, ssz);
704 				if (uc < 0)
705 					continue;
706 				break;
707 			case ESCAPE_SPECIAL:
708 				if (p->enc == TERMENC_ASCII) {
709 					rhs = mchars_spec2str(p->symtab,
710 					    seq, ssz, &rsz);
711 					if (rhs != NULL)
712 						break;
713 				} else {
714 					uc = mchars_spec2cp(p->symtab,
715 					    seq, ssz);
716 					if (uc > 0)
717 						sz += cond_width(p, uc, &skip);
718 				}
719 				continue;
720 			case ESCAPE_SKIPCHAR:
721 				skip = 1;
722 				continue;
723 			case ESCAPE_OVERSTRIKE:
724 				rsz = 0;
725 				rhs = seq + ssz;
726 				while (seq < rhs) {
727 					if (*seq == '\\') {
728 						mandoc_escape(&seq, NULL, NULL);
729 						continue;
730 					}
731 					i = (*p->width)(p, *seq++);
732 					if (rsz < i)
733 						rsz = i;
734 				}
735 				sz += rsz;
736 				continue;
737 			default:
738 				continue;
739 			}
740 
741 			/*
742 			 * Common handling for Unicode and numbered
743 			 * character escape sequences.
744 			 */
745 
746 			if (rhs == NULL) {
747 				if (p->enc == TERMENC_ASCII) {
748 					rhs = ascii_uc2str(uc);
749 					rsz = strlen(rhs);
750 				} else {
751 					if ((uc < 0x20 && uc != 0x09) ||
752 					    (uc > 0x7E && uc < 0xA0))
753 						uc = 0xFFFD;
754 					sz += cond_width(p, uc, &skip);
755 					continue;
756 				}
757 			}
758 
759 			if (skip) {
760 				skip = 0;
761 				break;
762 			}
763 
764 			/*
765 			 * Common handling for all escape sequences
766 			 * printing more than one character.
767 			 */
768 
769 			for (i = 0; i < rsz; i++)
770 				sz += (*p->width)(p, *rhs++);
771 			break;
772 		case ASCII_NBRSP:
773 			sz += cond_width(p, ' ', &skip);
774 			cp++;
775 			break;
776 		case ASCII_HYPH:
777 			sz += cond_width(p, '-', &skip);
778 			cp++;
779 			/* FALLTHROUGH */
780 		case ASCII_BREAK:
781 			break;
782 		default:
783 			break;
784 		}
785 	}
786 
787 	return(sz);
788 }
789 
790 int
791 term_vspan(const struct termp *p, const struct roffsu *su)
792 {
793 	double		 r;
794 	int		 ri;
795 
796 	switch (su->unit) {
797 	case SCALE_BU:
798 		r = su->scale / 40.0;
799 		break;
800 	case SCALE_CM:
801 		r = su->scale * 6.0 / 2.54;
802 		break;
803 	case SCALE_FS:
804 		r = su->scale * 65536.0 / 40.0;
805 		break;
806 	case SCALE_IN:
807 		r = su->scale * 6.0;
808 		break;
809 	case SCALE_MM:
810 		r = su->scale * 0.006;
811 		break;
812 	case SCALE_PC:
813 		r = su->scale;
814 		break;
815 	case SCALE_PT:
816 		r = su->scale / 12.0;
817 		break;
818 	case SCALE_EN:
819 		/* FALLTHROUGH */
820 	case SCALE_EM:
821 		r = su->scale * 0.6;
822 		break;
823 	case SCALE_VS:
824 		r = su->scale;
825 		break;
826 	default:
827 		abort();
828 		/* NOTREACHED */
829 	}
830 	ri = r > 0.0 ? r + 0.4995 : r - 0.4995;
831 	return(ri < 66 ? ri : 1);
832 }
833 
834 int
835 term_hspan(const struct termp *p, const struct roffsu *su)
836 {
837 	double		 v;
838 
839 	v = (*p->hspan)(p, su);
840 	return(v > 0.0 ? v + 0.0005 : v - 0.0005);
841 }
842