xref: /titanic_41/usr/src/cmd/mandoc/term.c (revision ffb8ebfab4941f959e7caea93ecfb348cfa3515e)
1*ffb8ebfaSGarrett D'Amore /*	$Id: term.c,v 1.214 2013/12/25 00:39:31 schwarze Exp $ */
232a712daSGarrett D'Amore /*
332a712daSGarrett D'Amore  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4*ffb8ebfaSGarrett D'Amore  * Copyright (c) 2010, 2011, 2012, 2013 Ingo Schwarze <schwarze@openbsd.org>
532a712daSGarrett D'Amore  *
632a712daSGarrett D'Amore  * Permission to use, copy, modify, and distribute this software for any
732a712daSGarrett D'Amore  * purpose with or without fee is hereby granted, provided that the above
832a712daSGarrett D'Amore  * copyright notice and this permission notice appear in all copies.
932a712daSGarrett D'Amore  *
1032a712daSGarrett D'Amore  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1132a712daSGarrett D'Amore  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1232a712daSGarrett D'Amore  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1332a712daSGarrett D'Amore  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1432a712daSGarrett D'Amore  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1532a712daSGarrett D'Amore  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1632a712daSGarrett D'Amore  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1732a712daSGarrett D'Amore  */
1832a712daSGarrett D'Amore #ifdef HAVE_CONFIG_H
1932a712daSGarrett D'Amore #include "config.h"
2032a712daSGarrett D'Amore #endif
2132a712daSGarrett D'Amore 
2232a712daSGarrett D'Amore #include <sys/types.h>
2332a712daSGarrett D'Amore 
2432a712daSGarrett D'Amore #include <assert.h>
2532a712daSGarrett D'Amore #include <ctype.h>
2632a712daSGarrett D'Amore #include <stdint.h>
2732a712daSGarrett D'Amore #include <stdio.h>
2832a712daSGarrett D'Amore #include <stdlib.h>
2932a712daSGarrett D'Amore #include <string.h>
3032a712daSGarrett D'Amore 
3132a712daSGarrett D'Amore #include "mandoc.h"
3232a712daSGarrett D'Amore #include "out.h"
3332a712daSGarrett D'Amore #include "term.h"
3432a712daSGarrett D'Amore #include "main.h"
3532a712daSGarrett D'Amore 
36*ffb8ebfaSGarrett D'Amore static	size_t		 cond_width(const struct termp *, int, int *);
37*ffb8ebfaSGarrett D'Amore static	void		 adjbuf(struct termp *p, size_t);
3832a712daSGarrett D'Amore static	void		 bufferc(struct termp *, char);
3932a712daSGarrett D'Amore static	void		 encode(struct termp *, const char *, size_t);
4032a712daSGarrett D'Amore static	void		 encode1(struct termp *, int);
4132a712daSGarrett D'Amore 
4232a712daSGarrett D'Amore void
term_free(struct termp * p)4332a712daSGarrett D'Amore term_free(struct termp *p)
4432a712daSGarrett D'Amore {
4532a712daSGarrett D'Amore 
4632a712daSGarrett D'Amore 	if (p->buf)
4732a712daSGarrett D'Amore 		free(p->buf);
4832a712daSGarrett D'Amore 	if (p->symtab)
4932a712daSGarrett D'Amore 		mchars_free(p->symtab);
5032a712daSGarrett D'Amore 
5132a712daSGarrett D'Amore 	free(p);
5232a712daSGarrett D'Amore }
5332a712daSGarrett D'Amore 
5432a712daSGarrett D'Amore 
5532a712daSGarrett D'Amore void
term_begin(struct termp * p,term_margin head,term_margin foot,const void * arg)5632a712daSGarrett D'Amore term_begin(struct termp *p, term_margin head,
5732a712daSGarrett D'Amore 		term_margin foot, const void *arg)
5832a712daSGarrett D'Amore {
5932a712daSGarrett D'Amore 
6032a712daSGarrett D'Amore 	p->headf = head;
6132a712daSGarrett D'Amore 	p->footf = foot;
6232a712daSGarrett D'Amore 	p->argf = arg;
6332a712daSGarrett D'Amore 	(*p->begin)(p);
6432a712daSGarrett D'Amore }
6532a712daSGarrett D'Amore 
6632a712daSGarrett D'Amore 
6732a712daSGarrett D'Amore void
term_end(struct termp * p)6832a712daSGarrett D'Amore term_end(struct termp *p)
6932a712daSGarrett D'Amore {
7032a712daSGarrett D'Amore 
7132a712daSGarrett D'Amore 	(*p->end)(p);
7232a712daSGarrett D'Amore }
7332a712daSGarrett D'Amore 
7432a712daSGarrett D'Amore /*
7532a712daSGarrett D'Amore  * Flush a line of text.  A "line" is loosely defined as being something
7632a712daSGarrett D'Amore  * that should be followed by a newline, regardless of whether it's
7732a712daSGarrett D'Amore  * broken apart by newlines getting there.  A line can also be a
7832a712daSGarrett D'Amore  * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
7932a712daSGarrett D'Amore  * not have a trailing newline.
8032a712daSGarrett D'Amore  *
8132a712daSGarrett D'Amore  * The following flags may be specified:
8232a712daSGarrett D'Amore  *
8332a712daSGarrett D'Amore  *  - TERMP_NOBREAK: this is the most important and is used when making
8432a712daSGarrett D'Amore  *    columns.  In short: don't print a newline and instead expect the
8532a712daSGarrett D'Amore  *    next call to do the padding up to the start of the next column.
86*ffb8ebfaSGarrett D'Amore  *    p->trailspace may be set to 0, 1, or 2, depending on how many
87*ffb8ebfaSGarrett D'Amore  *    space characters are required at the end of the column.
8832a712daSGarrett D'Amore  *
8932a712daSGarrett D'Amore  *  - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
9032a712daSGarrett D'Amore  *    the line is overrun, and don't pad-right if it's underrun.
9132a712daSGarrett D'Amore  *
9232a712daSGarrett D'Amore  *  - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
9332a712daSGarrett D'Amore  *    overrunning, instead save the position and continue at that point
9432a712daSGarrett D'Amore  *    when the next invocation.
9532a712daSGarrett D'Amore  *
9632a712daSGarrett D'Amore  *  In-line line breaking:
9732a712daSGarrett D'Amore  *
9832a712daSGarrett D'Amore  *  If TERMP_NOBREAK is specified and the line overruns the right
9932a712daSGarrett D'Amore  *  margin, it will break and pad-right to the right margin after
10032a712daSGarrett D'Amore  *  writing.  If maxrmargin is violated, it will break and continue
10132a712daSGarrett D'Amore  *  writing from the right-margin, which will lead to the above scenario
10232a712daSGarrett D'Amore  *  upon exit.  Otherwise, the line will break at the right margin.
10332a712daSGarrett D'Amore  */
10432a712daSGarrett D'Amore void
term_flushln(struct termp * p)10532a712daSGarrett D'Amore term_flushln(struct termp *p)
10632a712daSGarrett D'Amore {
107*ffb8ebfaSGarrett D'Amore 	size_t		 i;     /* current input position in p->buf */
108*ffb8ebfaSGarrett D'Amore 	int		 ntab;	/* number of tabs to prepend */
10932a712daSGarrett D'Amore 	size_t		 vis;   /* current visual position on output */
11032a712daSGarrett D'Amore 	size_t		 vbl;   /* number of blanks to prepend to output */
11132a712daSGarrett D'Amore 	size_t		 vend;	/* end of word visual position on output */
11232a712daSGarrett D'Amore 	size_t		 bp;    /* visual right border position */
11332a712daSGarrett D'Amore 	size_t		 dv;    /* temporary for visual pos calculations */
114*ffb8ebfaSGarrett D'Amore 	size_t		 j;     /* temporary loop index for p->buf */
115*ffb8ebfaSGarrett D'Amore 	size_t		 jhy;	/* last hyph before overflow w/r/t j */
11632a712daSGarrett D'Amore 	size_t		 maxvis; /* output position of visible boundary */
11732a712daSGarrett D'Amore 	size_t		 mmax; /* used in calculating bp */
11832a712daSGarrett D'Amore 
11932a712daSGarrett D'Amore 	/*
12032a712daSGarrett D'Amore 	 * First, establish the maximum columns of "visible" content.
12132a712daSGarrett D'Amore 	 * This is usually the difference between the right-margin and
12232a712daSGarrett D'Amore 	 * an indentation, but can be, for tagged lists or columns, a
12332a712daSGarrett D'Amore 	 * small set of values.
124*ffb8ebfaSGarrett D'Amore 	 *
125*ffb8ebfaSGarrett D'Amore 	 * The following unsigned-signed subtractions look strange,
126*ffb8ebfaSGarrett D'Amore 	 * but they are actually correct.  If the int p->overstep
127*ffb8ebfaSGarrett D'Amore 	 * is negative, it gets sign extended.  Subtracting that
128*ffb8ebfaSGarrett D'Amore 	 * very large size_t effectively adds a small number to dv.
12932a712daSGarrett D'Amore 	 */
13032a712daSGarrett D'Amore 	assert  (p->rmargin >= p->offset);
13132a712daSGarrett D'Amore 	dv     = p->rmargin - p->offset;
13232a712daSGarrett D'Amore 	maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
13332a712daSGarrett D'Amore 	dv     = p->maxrmargin - p->offset;
13432a712daSGarrett D'Amore 	mmax   = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
13532a712daSGarrett D'Amore 
13632a712daSGarrett D'Amore 	bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
13732a712daSGarrett D'Amore 
13832a712daSGarrett D'Amore 	/*
13932a712daSGarrett D'Amore 	 * Calculate the required amount of padding.
14032a712daSGarrett D'Amore 	 */
14132a712daSGarrett D'Amore 	vbl = p->offset + p->overstep > p->viscol ?
14232a712daSGarrett D'Amore 	      p->offset + p->overstep - p->viscol : 0;
14332a712daSGarrett D'Amore 
14432a712daSGarrett D'Amore 	vis = vend = 0;
14532a712daSGarrett D'Amore 	i = 0;
14632a712daSGarrett D'Amore 
14732a712daSGarrett D'Amore 	while (i < p->col) {
14832a712daSGarrett D'Amore 		/*
14932a712daSGarrett D'Amore 		 * Handle literal tab characters: collapse all
15032a712daSGarrett D'Amore 		 * subsequent tabs into a single huge set of spaces.
15132a712daSGarrett D'Amore 		 */
152*ffb8ebfaSGarrett D'Amore 		ntab = 0;
15332a712daSGarrett D'Amore 		while (i < p->col && '\t' == p->buf[i]) {
15432a712daSGarrett D'Amore 			vend = (vis / p->tabwidth + 1) * p->tabwidth;
15532a712daSGarrett D'Amore 			vbl += vend - vis;
15632a712daSGarrett D'Amore 			vis = vend;
157*ffb8ebfaSGarrett D'Amore 			ntab++;
15832a712daSGarrett D'Amore 			i++;
15932a712daSGarrett D'Amore 		}
16032a712daSGarrett D'Amore 
16132a712daSGarrett D'Amore 		/*
16232a712daSGarrett D'Amore 		 * Count up visible word characters.  Control sequences
16332a712daSGarrett D'Amore 		 * (starting with the CSI) aren't counted.  A space
16432a712daSGarrett D'Amore 		 * generates a non-printing word, which is valid (the
16532a712daSGarrett D'Amore 		 * space is printed according to regular spacing rules).
16632a712daSGarrett D'Amore 		 */
16732a712daSGarrett D'Amore 
16832a712daSGarrett D'Amore 		for (j = i, jhy = 0; j < p->col; j++) {
169*ffb8ebfaSGarrett D'Amore 			if (' ' == p->buf[j] || '\t' == p->buf[j])
17032a712daSGarrett D'Amore 				break;
17132a712daSGarrett D'Amore 
17232a712daSGarrett D'Amore 			/* Back over the the last printed character. */
17332a712daSGarrett D'Amore 			if (8 == p->buf[j]) {
17432a712daSGarrett D'Amore 				assert(j);
17532a712daSGarrett D'Amore 				vend -= (*p->width)(p, p->buf[j - 1]);
17632a712daSGarrett D'Amore 				continue;
17732a712daSGarrett D'Amore 			}
17832a712daSGarrett D'Amore 
17932a712daSGarrett D'Amore 			/* Regular word. */
18032a712daSGarrett D'Amore 			/* Break at the hyphen point if we overrun. */
18132a712daSGarrett D'Amore 			if (vend > vis && vend < bp &&
18232a712daSGarrett D'Amore 					ASCII_HYPH == p->buf[j])
18332a712daSGarrett D'Amore 				jhy = j;
18432a712daSGarrett D'Amore 
18532a712daSGarrett D'Amore 			vend += (*p->width)(p, p->buf[j]);
18632a712daSGarrett D'Amore 		}
18732a712daSGarrett D'Amore 
18832a712daSGarrett D'Amore 		/*
18932a712daSGarrett D'Amore 		 * Find out whether we would exceed the right margin.
19032a712daSGarrett D'Amore 		 * If so, break to the next line.
19132a712daSGarrett D'Amore 		 */
19232a712daSGarrett D'Amore 		if (vend > bp && 0 == jhy && vis > 0) {
19332a712daSGarrett D'Amore 			vend -= vis;
19432a712daSGarrett D'Amore 			(*p->endline)(p);
19532a712daSGarrett D'Amore 			p->viscol = 0;
19632a712daSGarrett D'Amore 			if (TERMP_NOBREAK & p->flags) {
19732a712daSGarrett D'Amore 				vbl = p->rmargin;
19832a712daSGarrett D'Amore 				vend += p->rmargin - p->offset;
19932a712daSGarrett D'Amore 			} else
20032a712daSGarrett D'Amore 				vbl = p->offset;
20132a712daSGarrett D'Amore 
202*ffb8ebfaSGarrett D'Amore 			/* use pending tabs on the new line */
203*ffb8ebfaSGarrett D'Amore 
204*ffb8ebfaSGarrett D'Amore 			if (0 < ntab)
205*ffb8ebfaSGarrett D'Amore 				vbl += ntab * p->tabwidth;
206*ffb8ebfaSGarrett D'Amore 
207*ffb8ebfaSGarrett D'Amore 			/*
208*ffb8ebfaSGarrett D'Amore 			 * Remove the p->overstep width.
209*ffb8ebfaSGarrett D'Amore 			 * Again, if p->overstep is negative,
210*ffb8ebfaSGarrett D'Amore 			 * sign extension does the right thing.
211*ffb8ebfaSGarrett D'Amore 			 */
21232a712daSGarrett D'Amore 
21332a712daSGarrett D'Amore 			bp += (size_t)p->overstep;
21432a712daSGarrett D'Amore 			p->overstep = 0;
21532a712daSGarrett D'Amore 		}
21632a712daSGarrett D'Amore 
21732a712daSGarrett D'Amore 		/* Write out the [remaining] word. */
21832a712daSGarrett D'Amore 		for ( ; i < p->col; i++) {
21932a712daSGarrett D'Amore 			if (vend > bp && jhy > 0 && i > jhy)
22032a712daSGarrett D'Amore 				break;
22132a712daSGarrett D'Amore 			if ('\t' == p->buf[i])
22232a712daSGarrett D'Amore 				break;
22332a712daSGarrett D'Amore 			if (' ' == p->buf[i]) {
22432a712daSGarrett D'Amore 				j = i;
22532a712daSGarrett D'Amore 				while (' ' == p->buf[i])
22632a712daSGarrett D'Amore 					i++;
227*ffb8ebfaSGarrett D'Amore 				dv = (i - j) * (*p->width)(p, ' ');
22832a712daSGarrett D'Amore 				vbl += dv;
22932a712daSGarrett D'Amore 				vend += dv;
23032a712daSGarrett D'Amore 				break;
23132a712daSGarrett D'Amore 			}
23232a712daSGarrett D'Amore 			if (ASCII_NBRSP == p->buf[i]) {
23332a712daSGarrett D'Amore 				vbl += (*p->width)(p, ' ');
23432a712daSGarrett D'Amore 				continue;
23532a712daSGarrett D'Amore 			}
23632a712daSGarrett D'Amore 
23732a712daSGarrett D'Amore 			/*
23832a712daSGarrett D'Amore 			 * Now we definitely know there will be
23932a712daSGarrett D'Amore 			 * printable characters to output,
24032a712daSGarrett D'Amore 			 * so write preceding white space now.
24132a712daSGarrett D'Amore 			 */
24232a712daSGarrett D'Amore 			if (vbl) {
24332a712daSGarrett D'Amore 				(*p->advance)(p, vbl);
24432a712daSGarrett D'Amore 				p->viscol += vbl;
24532a712daSGarrett D'Amore 				vbl = 0;
24632a712daSGarrett D'Amore 			}
24732a712daSGarrett D'Amore 
24832a712daSGarrett D'Amore 			if (ASCII_HYPH == p->buf[i]) {
24932a712daSGarrett D'Amore 				(*p->letter)(p, '-');
25032a712daSGarrett D'Amore 				p->viscol += (*p->width)(p, '-');
25132a712daSGarrett D'Amore 				continue;
25232a712daSGarrett D'Amore 			}
25332a712daSGarrett D'Amore 
25432a712daSGarrett D'Amore 			(*p->letter)(p, p->buf[i]);
25532a712daSGarrett D'Amore 			if (8 == p->buf[i])
25632a712daSGarrett D'Amore 				p->viscol -= (*p->width)(p, p->buf[i-1]);
25732a712daSGarrett D'Amore 			else
25832a712daSGarrett D'Amore 				p->viscol += (*p->width)(p, p->buf[i]);
25932a712daSGarrett D'Amore 		}
26032a712daSGarrett D'Amore 		vis = vend;
26132a712daSGarrett D'Amore 	}
26232a712daSGarrett D'Amore 
26332a712daSGarrett D'Amore 	/*
26432a712daSGarrett D'Amore 	 * If there was trailing white space, it was not printed;
26532a712daSGarrett D'Amore 	 * so reset the cursor position accordingly.
26632a712daSGarrett D'Amore 	 */
26732a712daSGarrett D'Amore 	if (vis)
26832a712daSGarrett D'Amore 		vis -= vbl;
26932a712daSGarrett D'Amore 
27032a712daSGarrett D'Amore 	p->col = 0;
27132a712daSGarrett D'Amore 	p->overstep = 0;
27232a712daSGarrett D'Amore 
27332a712daSGarrett D'Amore 	if ( ! (TERMP_NOBREAK & p->flags)) {
27432a712daSGarrett D'Amore 		p->viscol = 0;
27532a712daSGarrett D'Amore 		(*p->endline)(p);
27632a712daSGarrett D'Amore 		return;
27732a712daSGarrett D'Amore 	}
27832a712daSGarrett D'Amore 
27932a712daSGarrett D'Amore 	if (TERMP_HANG & p->flags) {
280*ffb8ebfaSGarrett D'Amore 		p->overstep = (int)(vis - maxvis +
281*ffb8ebfaSGarrett D'Amore 				p->trailspace * (*p->width)(p, ' '));
28232a712daSGarrett D'Amore 
28332a712daSGarrett D'Amore 		/*
28432a712daSGarrett D'Amore 		 * If we have overstepped the margin, temporarily move
28532a712daSGarrett D'Amore 		 * it to the right and flag the rest of the line to be
28632a712daSGarrett D'Amore 		 * shorter.
287*ffb8ebfaSGarrett D'Amore 		 * If there is a request to keep the columns together,
288*ffb8ebfaSGarrett D'Amore 		 * allow negative overstep when the column is not full.
28932a712daSGarrett D'Amore 		 */
290*ffb8ebfaSGarrett D'Amore 		if (p->trailspace && p->overstep < 0)
29132a712daSGarrett D'Amore 			p->overstep = 0;
29232a712daSGarrett D'Amore 		return;
29332a712daSGarrett D'Amore 
29432a712daSGarrett D'Amore 	} else if (TERMP_DANGLE & p->flags)
29532a712daSGarrett D'Amore 		return;
29632a712daSGarrett D'Amore 
29732a712daSGarrett D'Amore 	/* If the column was overrun, break the line. */
298*ffb8ebfaSGarrett D'Amore 	if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
29932a712daSGarrett D'Amore 		(*p->endline)(p);
30032a712daSGarrett D'Amore 		p->viscol = 0;
30132a712daSGarrett D'Amore 	}
30232a712daSGarrett D'Amore }
30332a712daSGarrett D'Amore 
30432a712daSGarrett D'Amore 
30532a712daSGarrett D'Amore /*
30632a712daSGarrett D'Amore  * A newline only breaks an existing line; it won't assert vertical
30732a712daSGarrett D'Amore  * space.  All data in the output buffer is flushed prior to the newline
30832a712daSGarrett D'Amore  * assertion.
30932a712daSGarrett D'Amore  */
31032a712daSGarrett D'Amore void
term_newln(struct termp * p)31132a712daSGarrett D'Amore term_newln(struct termp *p)
31232a712daSGarrett D'Amore {
31332a712daSGarrett D'Amore 
31432a712daSGarrett D'Amore 	p->flags |= TERMP_NOSPACE;
31532a712daSGarrett D'Amore 	if (p->col || p->viscol)
31632a712daSGarrett D'Amore 		term_flushln(p);
31732a712daSGarrett D'Amore }
31832a712daSGarrett D'Amore 
31932a712daSGarrett D'Amore 
32032a712daSGarrett D'Amore /*
32132a712daSGarrett D'Amore  * Asserts a vertical space (a full, empty line-break between lines).
32232a712daSGarrett D'Amore  * Note that if used twice, this will cause two blank spaces and so on.
32332a712daSGarrett D'Amore  * All data in the output buffer is flushed prior to the newline
32432a712daSGarrett D'Amore  * assertion.
32532a712daSGarrett D'Amore  */
32632a712daSGarrett D'Amore void
term_vspace(struct termp * p)32732a712daSGarrett D'Amore term_vspace(struct termp *p)
32832a712daSGarrett D'Amore {
32932a712daSGarrett D'Amore 
33032a712daSGarrett D'Amore 	term_newln(p);
33132a712daSGarrett D'Amore 	p->viscol = 0;
332*ffb8ebfaSGarrett D'Amore 	if (0 < p->skipvsp)
333*ffb8ebfaSGarrett D'Amore 		p->skipvsp--;
334*ffb8ebfaSGarrett D'Amore 	else
33532a712daSGarrett D'Amore 		(*p->endline)(p);
33632a712daSGarrett D'Amore }
33732a712daSGarrett D'Amore 
33832a712daSGarrett D'Amore void
term_fontlast(struct termp * p)33932a712daSGarrett D'Amore term_fontlast(struct termp *p)
34032a712daSGarrett D'Amore {
34132a712daSGarrett D'Amore 	enum termfont	 f;
34232a712daSGarrett D'Amore 
34332a712daSGarrett D'Amore 	f = p->fontl;
34432a712daSGarrett D'Amore 	p->fontl = p->fontq[p->fonti];
34532a712daSGarrett D'Amore 	p->fontq[p->fonti] = f;
34632a712daSGarrett D'Amore }
34732a712daSGarrett D'Amore 
34832a712daSGarrett D'Amore 
34932a712daSGarrett D'Amore void
term_fontrepl(struct termp * p,enum termfont f)35032a712daSGarrett D'Amore term_fontrepl(struct termp *p, enum termfont f)
35132a712daSGarrett D'Amore {
35232a712daSGarrett D'Amore 
35332a712daSGarrett D'Amore 	p->fontl = p->fontq[p->fonti];
35432a712daSGarrett D'Amore 	p->fontq[p->fonti] = f;
35532a712daSGarrett D'Amore }
35632a712daSGarrett D'Amore 
35732a712daSGarrett D'Amore 
35832a712daSGarrett D'Amore void
term_fontpush(struct termp * p,enum termfont f)35932a712daSGarrett D'Amore term_fontpush(struct termp *p, enum termfont f)
36032a712daSGarrett D'Amore {
36132a712daSGarrett D'Amore 
36232a712daSGarrett D'Amore 	assert(p->fonti + 1 < 10);
36332a712daSGarrett D'Amore 	p->fontl = p->fontq[p->fonti];
36432a712daSGarrett D'Amore 	p->fontq[++p->fonti] = f;
36532a712daSGarrett D'Amore }
36632a712daSGarrett D'Amore 
36732a712daSGarrett D'Amore 
36832a712daSGarrett D'Amore const void *
term_fontq(struct termp * p)36932a712daSGarrett D'Amore term_fontq(struct termp *p)
37032a712daSGarrett D'Amore {
37132a712daSGarrett D'Amore 
37232a712daSGarrett D'Amore 	return(&p->fontq[p->fonti]);
37332a712daSGarrett D'Amore }
37432a712daSGarrett D'Amore 
37532a712daSGarrett D'Amore 
37632a712daSGarrett D'Amore enum termfont
term_fonttop(struct termp * p)37732a712daSGarrett D'Amore term_fonttop(struct termp *p)
37832a712daSGarrett D'Amore {
37932a712daSGarrett D'Amore 
38032a712daSGarrett D'Amore 	return(p->fontq[p->fonti]);
38132a712daSGarrett D'Amore }
38232a712daSGarrett D'Amore 
38332a712daSGarrett D'Amore 
38432a712daSGarrett D'Amore void
term_fontpopq(struct termp * p,const void * key)38532a712daSGarrett D'Amore term_fontpopq(struct termp *p, const void *key)
38632a712daSGarrett D'Amore {
38732a712daSGarrett D'Amore 
388*ffb8ebfaSGarrett D'Amore 	while (p->fonti >= 0 && key < (void *)(p->fontq + p->fonti))
38932a712daSGarrett D'Amore 		p->fonti--;
39032a712daSGarrett D'Amore 	assert(p->fonti >= 0);
39132a712daSGarrett D'Amore }
39232a712daSGarrett D'Amore 
39332a712daSGarrett D'Amore 
39432a712daSGarrett D'Amore void
term_fontpop(struct termp * p)39532a712daSGarrett D'Amore term_fontpop(struct termp *p)
39632a712daSGarrett D'Amore {
39732a712daSGarrett D'Amore 
39832a712daSGarrett D'Amore 	assert(p->fonti);
39932a712daSGarrett D'Amore 	p->fonti--;
40032a712daSGarrett D'Amore }
40132a712daSGarrett D'Amore 
40232a712daSGarrett D'Amore /*
40332a712daSGarrett D'Amore  * Handle pwords, partial words, which may be either a single word or a
40432a712daSGarrett D'Amore  * phrase that cannot be broken down (such as a literal string).  This
40532a712daSGarrett D'Amore  * handles word styling.
40632a712daSGarrett D'Amore  */
40732a712daSGarrett D'Amore void
term_word(struct termp * p,const char * word)40832a712daSGarrett D'Amore term_word(struct termp *p, const char *word)
40932a712daSGarrett D'Amore {
410*ffb8ebfaSGarrett D'Amore 	const char	 nbrsp[2] = { ASCII_NBRSP, 0 };
41132a712daSGarrett D'Amore 	const char	*seq, *cp;
41232a712daSGarrett D'Amore 	char		 c;
41332a712daSGarrett D'Amore 	int		 sz, uc;
41432a712daSGarrett D'Amore 	size_t		 ssz;
41532a712daSGarrett D'Amore 	enum mandoc_esc	 esc;
41632a712daSGarrett D'Amore 
41732a712daSGarrett D'Amore 	if ( ! (TERMP_NOSPACE & p->flags)) {
41832a712daSGarrett D'Amore 		if ( ! (TERMP_KEEP & p->flags)) {
41932a712daSGarrett D'Amore 			bufferc(p, ' ');
42032a712daSGarrett D'Amore 			if (TERMP_SENTENCE & p->flags)
42132a712daSGarrett D'Amore 				bufferc(p, ' ');
42232a712daSGarrett D'Amore 		} else
42332a712daSGarrett D'Amore 			bufferc(p, ASCII_NBRSP);
42432a712daSGarrett D'Amore 	}
425*ffb8ebfaSGarrett D'Amore 	if (TERMP_PREKEEP & p->flags)
426*ffb8ebfaSGarrett D'Amore 		p->flags |= TERMP_KEEP;
42732a712daSGarrett D'Amore 
42832a712daSGarrett D'Amore 	if ( ! (p->flags & TERMP_NONOSPACE))
42932a712daSGarrett D'Amore 		p->flags &= ~TERMP_NOSPACE;
43032a712daSGarrett D'Amore 	else
43132a712daSGarrett D'Amore 		p->flags |= TERMP_NOSPACE;
43232a712daSGarrett D'Amore 
433*ffb8ebfaSGarrett D'Amore 	p->flags &= ~TERMP_SENTENCE;
43432a712daSGarrett D'Amore 
43532a712daSGarrett D'Amore 	while ('\0' != *word) {
436*ffb8ebfaSGarrett D'Amore 		if ('\\' != *word) {
437*ffb8ebfaSGarrett D'Amore 			if (TERMP_SKIPCHAR & p->flags) {
438*ffb8ebfaSGarrett D'Amore 				p->flags &= ~TERMP_SKIPCHAR;
439*ffb8ebfaSGarrett D'Amore 				word++;
44032a712daSGarrett D'Amore 				continue;
441*ffb8ebfaSGarrett D'Amore 			}
442*ffb8ebfaSGarrett D'Amore 			if (TERMP_NBRWORD & p->flags) {
443*ffb8ebfaSGarrett D'Amore 				if (' ' == *word) {
444*ffb8ebfaSGarrett D'Amore 					encode(p, nbrsp, 1);
445*ffb8ebfaSGarrett D'Amore 					word++;
446*ffb8ebfaSGarrett D'Amore 					continue;
447*ffb8ebfaSGarrett D'Amore 				}
448*ffb8ebfaSGarrett D'Amore 				ssz = strcspn(word, "\\ ");
449*ffb8ebfaSGarrett D'Amore 			} else
450*ffb8ebfaSGarrett D'Amore 				ssz = strcspn(word, "\\");
451*ffb8ebfaSGarrett D'Amore 			encode(p, word, ssz);
452*ffb8ebfaSGarrett D'Amore 			word += (int)ssz;
453*ffb8ebfaSGarrett D'Amore 			continue;
454*ffb8ebfaSGarrett D'Amore 		}
45532a712daSGarrett D'Amore 
45632a712daSGarrett D'Amore 		word++;
45732a712daSGarrett D'Amore 		esc = mandoc_escape(&word, &seq, &sz);
45832a712daSGarrett D'Amore 		if (ESCAPE_ERROR == esc)
45932a712daSGarrett D'Amore 			break;
46032a712daSGarrett D'Amore 
46132a712daSGarrett D'Amore 		if (TERMENC_ASCII != p->enc)
46232a712daSGarrett D'Amore 			switch (esc) {
46332a712daSGarrett D'Amore 			case (ESCAPE_UNICODE):
46432a712daSGarrett D'Amore 				uc = mchars_num2uc(seq + 1, sz - 1);
46532a712daSGarrett D'Amore 				if ('\0' == uc)
46632a712daSGarrett D'Amore 					break;
46732a712daSGarrett D'Amore 				encode1(p, uc);
46832a712daSGarrett D'Amore 				continue;
46932a712daSGarrett D'Amore 			case (ESCAPE_SPECIAL):
47032a712daSGarrett D'Amore 				uc = mchars_spec2cp(p->symtab, seq, sz);
47132a712daSGarrett D'Amore 				if (uc <= 0)
47232a712daSGarrett D'Amore 					break;
47332a712daSGarrett D'Amore 				encode1(p, uc);
47432a712daSGarrett D'Amore 				continue;
47532a712daSGarrett D'Amore 			default:
47632a712daSGarrett D'Amore 				break;
47732a712daSGarrett D'Amore 			}
47832a712daSGarrett D'Amore 
47932a712daSGarrett D'Amore 		switch (esc) {
48032a712daSGarrett D'Amore 		case (ESCAPE_UNICODE):
48132a712daSGarrett D'Amore 			encode1(p, '?');
48232a712daSGarrett D'Amore 			break;
48332a712daSGarrett D'Amore 		case (ESCAPE_NUMBERED):
48432a712daSGarrett D'Amore 			c = mchars_num2char(seq, sz);
48532a712daSGarrett D'Amore 			if ('\0' != c)
48632a712daSGarrett D'Amore 				encode(p, &c, 1);
48732a712daSGarrett D'Amore 			break;
48832a712daSGarrett D'Amore 		case (ESCAPE_SPECIAL):
48932a712daSGarrett D'Amore 			cp = mchars_spec2str(p->symtab, seq, sz, &ssz);
49032a712daSGarrett D'Amore 			if (NULL != cp)
49132a712daSGarrett D'Amore 				encode(p, cp, ssz);
49232a712daSGarrett D'Amore 			else if (1 == ssz)
49332a712daSGarrett D'Amore 				encode(p, seq, sz);
49432a712daSGarrett D'Amore 			break;
49532a712daSGarrett D'Amore 		case (ESCAPE_FONTBOLD):
49632a712daSGarrett D'Amore 			term_fontrepl(p, TERMFONT_BOLD);
49732a712daSGarrett D'Amore 			break;
49832a712daSGarrett D'Amore 		case (ESCAPE_FONTITALIC):
49932a712daSGarrett D'Amore 			term_fontrepl(p, TERMFONT_UNDER);
50032a712daSGarrett D'Amore 			break;
501*ffb8ebfaSGarrett D'Amore 		case (ESCAPE_FONTBI):
502*ffb8ebfaSGarrett D'Amore 			term_fontrepl(p, TERMFONT_BI);
503*ffb8ebfaSGarrett D'Amore 			break;
50432a712daSGarrett D'Amore 		case (ESCAPE_FONT):
50532a712daSGarrett D'Amore 			/* FALLTHROUGH */
50632a712daSGarrett D'Amore 		case (ESCAPE_FONTROMAN):
50732a712daSGarrett D'Amore 			term_fontrepl(p, TERMFONT_NONE);
50832a712daSGarrett D'Amore 			break;
50932a712daSGarrett D'Amore 		case (ESCAPE_FONTPREV):
51032a712daSGarrett D'Amore 			term_fontlast(p);
51132a712daSGarrett D'Amore 			break;
51232a712daSGarrett D'Amore 		case (ESCAPE_NOSPACE):
513*ffb8ebfaSGarrett D'Amore 			if (TERMP_SKIPCHAR & p->flags)
514*ffb8ebfaSGarrett D'Amore 				p->flags &= ~TERMP_SKIPCHAR;
515*ffb8ebfaSGarrett D'Amore 			else if ('\0' == *word)
51632a712daSGarrett D'Amore 				p->flags |= TERMP_NOSPACE;
51732a712daSGarrett D'Amore 			break;
518*ffb8ebfaSGarrett D'Amore 		case (ESCAPE_SKIPCHAR):
519*ffb8ebfaSGarrett D'Amore 			p->flags |= TERMP_SKIPCHAR;
520*ffb8ebfaSGarrett D'Amore 			break;
52132a712daSGarrett D'Amore 		default:
52232a712daSGarrett D'Amore 			break;
52332a712daSGarrett D'Amore 		}
52432a712daSGarrett D'Amore 	}
525*ffb8ebfaSGarrett D'Amore 	p->flags &= ~TERMP_NBRWORD;
52632a712daSGarrett D'Amore }
52732a712daSGarrett D'Amore 
52832a712daSGarrett D'Amore static void
adjbuf(struct termp * p,size_t sz)529*ffb8ebfaSGarrett D'Amore adjbuf(struct termp *p, size_t sz)
53032a712daSGarrett D'Amore {
53132a712daSGarrett D'Amore 
53232a712daSGarrett D'Amore 	if (0 == p->maxcols)
53332a712daSGarrett D'Amore 		p->maxcols = 1024;
53432a712daSGarrett D'Amore 	while (sz >= p->maxcols)
53532a712daSGarrett D'Amore 		p->maxcols <<= 2;
53632a712daSGarrett D'Amore 
537*ffb8ebfaSGarrett D'Amore 	p->buf = mandoc_realloc(p->buf, sizeof(int) * p->maxcols);
53832a712daSGarrett D'Amore }
53932a712daSGarrett D'Amore 
54032a712daSGarrett D'Amore static void
bufferc(struct termp * p,char c)54132a712daSGarrett D'Amore bufferc(struct termp *p, char c)
54232a712daSGarrett D'Amore {
54332a712daSGarrett D'Amore 
54432a712daSGarrett D'Amore 	if (p->col + 1 >= p->maxcols)
54532a712daSGarrett D'Amore 		adjbuf(p, p->col + 1);
54632a712daSGarrett D'Amore 
54732a712daSGarrett D'Amore 	p->buf[p->col++] = c;
54832a712daSGarrett D'Amore }
54932a712daSGarrett D'Amore 
55032a712daSGarrett D'Amore /*
55132a712daSGarrett D'Amore  * See encode().
55232a712daSGarrett D'Amore  * Do this for a single (probably unicode) value.
55332a712daSGarrett D'Amore  * Does not check for non-decorated glyphs.
55432a712daSGarrett D'Amore  */
55532a712daSGarrett D'Amore static void
encode1(struct termp * p,int c)55632a712daSGarrett D'Amore encode1(struct termp *p, int c)
55732a712daSGarrett D'Amore {
55832a712daSGarrett D'Amore 	enum termfont	  f;
55932a712daSGarrett D'Amore 
560*ffb8ebfaSGarrett D'Amore 	if (TERMP_SKIPCHAR & p->flags) {
561*ffb8ebfaSGarrett D'Amore 		p->flags &= ~TERMP_SKIPCHAR;
562*ffb8ebfaSGarrett D'Amore 		return;
563*ffb8ebfaSGarrett D'Amore 	}
564*ffb8ebfaSGarrett D'Amore 
565*ffb8ebfaSGarrett D'Amore 	if (p->col + 6 >= p->maxcols)
566*ffb8ebfaSGarrett D'Amore 		adjbuf(p, p->col + 6);
56732a712daSGarrett D'Amore 
56832a712daSGarrett D'Amore 	f = term_fonttop(p);
56932a712daSGarrett D'Amore 
570*ffb8ebfaSGarrett D'Amore 	if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
57132a712daSGarrett D'Amore 		p->buf[p->col++] = '_';
57232a712daSGarrett D'Amore 		p->buf[p->col++] = 8;
573*ffb8ebfaSGarrett D'Amore 	}
574*ffb8ebfaSGarrett D'Amore 	if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
575*ffb8ebfaSGarrett D'Amore 		if (ASCII_HYPH == c)
576*ffb8ebfaSGarrett D'Amore 			p->buf[p->col++] = '-';
577*ffb8ebfaSGarrett D'Amore 		else
578*ffb8ebfaSGarrett D'Amore 			p->buf[p->col++] = c;
579*ffb8ebfaSGarrett D'Amore 		p->buf[p->col++] = 8;
580*ffb8ebfaSGarrett D'Amore 	}
58132a712daSGarrett D'Amore 	p->buf[p->col++] = c;
58232a712daSGarrett D'Amore }
58332a712daSGarrett D'Amore 
58432a712daSGarrett D'Amore static void
encode(struct termp * p,const char * word,size_t sz)58532a712daSGarrett D'Amore encode(struct termp *p, const char *word, size_t sz)
58632a712daSGarrett D'Amore {
587*ffb8ebfaSGarrett D'Amore 	size_t		  i;
58832a712daSGarrett D'Amore 
589*ffb8ebfaSGarrett D'Amore 	if (TERMP_SKIPCHAR & p->flags) {
590*ffb8ebfaSGarrett D'Amore 		p->flags &= ~TERMP_SKIPCHAR;
591*ffb8ebfaSGarrett D'Amore 		return;
592*ffb8ebfaSGarrett D'Amore 	}
59332a712daSGarrett D'Amore 
59432a712daSGarrett D'Amore 	/*
59532a712daSGarrett D'Amore 	 * Encode and buffer a string of characters.  If the current
59632a712daSGarrett D'Amore 	 * font mode is unset, buffer directly, else encode then buffer
59732a712daSGarrett D'Amore 	 * character by character.
59832a712daSGarrett D'Amore 	 */
59932a712daSGarrett D'Amore 
600*ffb8ebfaSGarrett D'Amore 	if (TERMFONT_NONE == term_fonttop(p)) {
601*ffb8ebfaSGarrett D'Amore 		if (p->col + sz >= p->maxcols)
602*ffb8ebfaSGarrett D'Amore 			adjbuf(p, p->col + sz);
603*ffb8ebfaSGarrett D'Amore 		for (i = 0; i < sz; i++)
60432a712daSGarrett D'Amore 			p->buf[p->col++] = word[i];
60532a712daSGarrett D'Amore 		return;
60632a712daSGarrett D'Amore 	}
60732a712daSGarrett D'Amore 
60832a712daSGarrett D'Amore 	/* Pre-buffer, assuming worst-case. */
60932a712daSGarrett D'Amore 
610*ffb8ebfaSGarrett D'Amore 	if (p->col + 1 + (sz * 5) >= p->maxcols)
611*ffb8ebfaSGarrett D'Amore 		adjbuf(p, p->col + 1 + (sz * 5));
61232a712daSGarrett D'Amore 
613*ffb8ebfaSGarrett D'Amore 	for (i = 0; i < sz; i++) {
614*ffb8ebfaSGarrett D'Amore 		if (ASCII_HYPH == word[i] ||
615*ffb8ebfaSGarrett D'Amore 		    isgraph((unsigned char)word[i]))
616*ffb8ebfaSGarrett D'Amore 			encode1(p, word[i]);
61732a712daSGarrett D'Amore 		else
61832a712daSGarrett D'Amore 			p->buf[p->col++] = word[i];
61932a712daSGarrett D'Amore 	}
62032a712daSGarrett D'Amore }
62132a712daSGarrett D'Amore 
62232a712daSGarrett D'Amore size_t
term_len(const struct termp * p,size_t sz)62332a712daSGarrett D'Amore term_len(const struct termp *p, size_t sz)
62432a712daSGarrett D'Amore {
62532a712daSGarrett D'Amore 
62632a712daSGarrett D'Amore 	return((*p->width)(p, ' ') * sz);
62732a712daSGarrett D'Amore }
62832a712daSGarrett D'Amore 
629*ffb8ebfaSGarrett D'Amore static size_t
cond_width(const struct termp * p,int c,int * skip)630*ffb8ebfaSGarrett D'Amore cond_width(const struct termp *p, int c, int *skip)
631*ffb8ebfaSGarrett D'Amore {
632*ffb8ebfaSGarrett D'Amore 
633*ffb8ebfaSGarrett D'Amore 	if (*skip) {
634*ffb8ebfaSGarrett D'Amore 		(*skip) = 0;
635*ffb8ebfaSGarrett D'Amore 		return(0);
636*ffb8ebfaSGarrett D'Amore 	} else
637*ffb8ebfaSGarrett D'Amore 		return((*p->width)(p, c));
638*ffb8ebfaSGarrett D'Amore }
63932a712daSGarrett D'Amore 
64032a712daSGarrett D'Amore size_t
term_strlen(const struct termp * p,const char * cp)64132a712daSGarrett D'Amore term_strlen(const struct termp *p, const char *cp)
64232a712daSGarrett D'Amore {
64332a712daSGarrett D'Amore 	size_t		 sz, rsz, i;
644*ffb8ebfaSGarrett D'Amore 	int		 ssz, skip, c;
64532a712daSGarrett D'Amore 	const char	*seq, *rhs;
64632a712daSGarrett D'Amore 	enum mandoc_esc	 esc;
64732a712daSGarrett D'Amore 	static const char rej[] = { '\\', ASCII_HYPH, ASCII_NBRSP, '\0' };
64832a712daSGarrett D'Amore 
64932a712daSGarrett D'Amore 	/*
65032a712daSGarrett D'Amore 	 * Account for escaped sequences within string length
65132a712daSGarrett D'Amore 	 * calculations.  This follows the logic in term_word() as we
65232a712daSGarrett D'Amore 	 * must calculate the width of produced strings.
65332a712daSGarrett D'Amore 	 */
65432a712daSGarrett D'Amore 
65532a712daSGarrett D'Amore 	sz = 0;
656*ffb8ebfaSGarrett D'Amore 	skip = 0;
65732a712daSGarrett D'Amore 	while ('\0' != *cp) {
65832a712daSGarrett D'Amore 		rsz = strcspn(cp, rej);
65932a712daSGarrett D'Amore 		for (i = 0; i < rsz; i++)
660*ffb8ebfaSGarrett D'Amore 			sz += cond_width(p, *cp++, &skip);
66132a712daSGarrett D'Amore 
66232a712daSGarrett D'Amore 		c = 0;
66332a712daSGarrett D'Amore 		switch (*cp) {
66432a712daSGarrett D'Amore 		case ('\\'):
66532a712daSGarrett D'Amore 			cp++;
66632a712daSGarrett D'Amore 			esc = mandoc_escape(&cp, &seq, &ssz);
66732a712daSGarrett D'Amore 			if (ESCAPE_ERROR == esc)
66832a712daSGarrett D'Amore 				return(sz);
66932a712daSGarrett D'Amore 
67032a712daSGarrett D'Amore 			if (TERMENC_ASCII != p->enc)
67132a712daSGarrett D'Amore 				switch (esc) {
67232a712daSGarrett D'Amore 				case (ESCAPE_UNICODE):
67332a712daSGarrett D'Amore 					c = mchars_num2uc
67432a712daSGarrett D'Amore 						(seq + 1, ssz - 1);
67532a712daSGarrett D'Amore 					if ('\0' == c)
67632a712daSGarrett D'Amore 						break;
677*ffb8ebfaSGarrett D'Amore 					sz += cond_width(p, c, &skip);
67832a712daSGarrett D'Amore 					continue;
67932a712daSGarrett D'Amore 				case (ESCAPE_SPECIAL):
68032a712daSGarrett D'Amore 					c = mchars_spec2cp
68132a712daSGarrett D'Amore 						(p->symtab, seq, ssz);
68232a712daSGarrett D'Amore 					if (c <= 0)
68332a712daSGarrett D'Amore 						break;
684*ffb8ebfaSGarrett D'Amore 					sz += cond_width(p, c, &skip);
68532a712daSGarrett D'Amore 					continue;
68632a712daSGarrett D'Amore 				default:
68732a712daSGarrett D'Amore 					break;
68832a712daSGarrett D'Amore 				}
68932a712daSGarrett D'Amore 
69032a712daSGarrett D'Amore 			rhs = NULL;
69132a712daSGarrett D'Amore 
69232a712daSGarrett D'Amore 			switch (esc) {
69332a712daSGarrett D'Amore 			case (ESCAPE_UNICODE):
694*ffb8ebfaSGarrett D'Amore 				sz += cond_width(p, '?', &skip);
69532a712daSGarrett D'Amore 				break;
69632a712daSGarrett D'Amore 			case (ESCAPE_NUMBERED):
69732a712daSGarrett D'Amore 				c = mchars_num2char(seq, ssz);
69832a712daSGarrett D'Amore 				if ('\0' != c)
699*ffb8ebfaSGarrett D'Amore 					sz += cond_width(p, c, &skip);
70032a712daSGarrett D'Amore 				break;
70132a712daSGarrett D'Amore 			case (ESCAPE_SPECIAL):
70232a712daSGarrett D'Amore 				rhs = mchars_spec2str
70332a712daSGarrett D'Amore 					(p->symtab, seq, ssz, &rsz);
70432a712daSGarrett D'Amore 
70532a712daSGarrett D'Amore 				if (ssz != 1 || rhs)
70632a712daSGarrett D'Amore 					break;
70732a712daSGarrett D'Amore 
70832a712daSGarrett D'Amore 				rhs = seq;
70932a712daSGarrett D'Amore 				rsz = ssz;
71032a712daSGarrett D'Amore 				break;
711*ffb8ebfaSGarrett D'Amore 			case (ESCAPE_SKIPCHAR):
712*ffb8ebfaSGarrett D'Amore 				skip = 1;
713*ffb8ebfaSGarrett D'Amore 				break;
71432a712daSGarrett D'Amore 			default:
71532a712daSGarrett D'Amore 				break;
71632a712daSGarrett D'Amore 			}
71732a712daSGarrett D'Amore 
71832a712daSGarrett D'Amore 			if (NULL == rhs)
71932a712daSGarrett D'Amore 				break;
72032a712daSGarrett D'Amore 
721*ffb8ebfaSGarrett D'Amore 			if (skip) {
722*ffb8ebfaSGarrett D'Amore 				skip = 0;
723*ffb8ebfaSGarrett D'Amore 				break;
724*ffb8ebfaSGarrett D'Amore 			}
725*ffb8ebfaSGarrett D'Amore 
72632a712daSGarrett D'Amore 			for (i = 0; i < rsz; i++)
72732a712daSGarrett D'Amore 				sz += (*p->width)(p, *rhs++);
72832a712daSGarrett D'Amore 			break;
72932a712daSGarrett D'Amore 		case (ASCII_NBRSP):
730*ffb8ebfaSGarrett D'Amore 			sz += cond_width(p, ' ', &skip);
73132a712daSGarrett D'Amore 			cp++;
73232a712daSGarrett D'Amore 			break;
73332a712daSGarrett D'Amore 		case (ASCII_HYPH):
734*ffb8ebfaSGarrett D'Amore 			sz += cond_width(p, '-', &skip);
73532a712daSGarrett D'Amore 			cp++;
73632a712daSGarrett D'Amore 			break;
73732a712daSGarrett D'Amore 		default:
73832a712daSGarrett D'Amore 			break;
73932a712daSGarrett D'Amore 		}
74032a712daSGarrett D'Amore 	}
74132a712daSGarrett D'Amore 
74232a712daSGarrett D'Amore 	return(sz);
74332a712daSGarrett D'Amore }
74432a712daSGarrett D'Amore 
74532a712daSGarrett D'Amore /* ARGSUSED */
74632a712daSGarrett D'Amore size_t
term_vspan(const struct termp * p,const struct roffsu * su)74732a712daSGarrett D'Amore term_vspan(const struct termp *p, const struct roffsu *su)
74832a712daSGarrett D'Amore {
74932a712daSGarrett D'Amore 	double		 r;
75032a712daSGarrett D'Amore 
75132a712daSGarrett D'Amore 	switch (su->unit) {
75232a712daSGarrett D'Amore 	case (SCALE_CM):
75332a712daSGarrett D'Amore 		r = su->scale * 2;
75432a712daSGarrett D'Amore 		break;
75532a712daSGarrett D'Amore 	case (SCALE_IN):
75632a712daSGarrett D'Amore 		r = su->scale * 6;
75732a712daSGarrett D'Amore 		break;
75832a712daSGarrett D'Amore 	case (SCALE_PC):
75932a712daSGarrett D'Amore 		r = su->scale;
76032a712daSGarrett D'Amore 		break;
76132a712daSGarrett D'Amore 	case (SCALE_PT):
76232a712daSGarrett D'Amore 		r = su->scale / 8;
76332a712daSGarrett D'Amore 		break;
76432a712daSGarrett D'Amore 	case (SCALE_MM):
76532a712daSGarrett D'Amore 		r = su->scale / 1000;
76632a712daSGarrett D'Amore 		break;
76732a712daSGarrett D'Amore 	case (SCALE_VS):
76832a712daSGarrett D'Amore 		r = su->scale;
76932a712daSGarrett D'Amore 		break;
77032a712daSGarrett D'Amore 	default:
77132a712daSGarrett D'Amore 		r = su->scale - 1;
77232a712daSGarrett D'Amore 		break;
77332a712daSGarrett D'Amore 	}
77432a712daSGarrett D'Amore 
77532a712daSGarrett D'Amore 	if (r < 0.0)
77632a712daSGarrett D'Amore 		r = 0.0;
77732a712daSGarrett D'Amore 	return(/* LINTED */(size_t)
77832a712daSGarrett D'Amore 			r);
77932a712daSGarrett D'Amore }
78032a712daSGarrett D'Amore 
78132a712daSGarrett D'Amore size_t
term_hspan(const struct termp * p,const struct roffsu * su)78232a712daSGarrett D'Amore term_hspan(const struct termp *p, const struct roffsu *su)
78332a712daSGarrett D'Amore {
78432a712daSGarrett D'Amore 	double		 v;
78532a712daSGarrett D'Amore 
78632a712daSGarrett D'Amore 	v = ((*p->hspan)(p, su));
78732a712daSGarrett D'Amore 	if (v < 0.0)
78832a712daSGarrett D'Amore 		v = 0.0;
78932a712daSGarrett D'Amore 	return((size_t) /* LINTED */
79032a712daSGarrett D'Amore 			v);
79132a712daSGarrett D'Amore }
792