xref: /minix/external/bsd/mdocml/dist/term.c (revision 0a6a1f1d)
1*0a6a1f1dSLionel Sambuc /*	Id: term.c,v 1.215 2013/12/31 18:07:42 schwarze Exp  */
2d65f6f70SBen Gras /*
392395e9cSLionel Sambuc  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2010, 2011, 2012, 2013 Ingo Schwarze <schwarze@openbsd.org>
5d65f6f70SBen Gras  *
6d65f6f70SBen Gras  * Permission to use, copy, modify, and distribute this software for any
7d65f6f70SBen Gras  * purpose with or without fee is hereby granted, provided that the above
8d65f6f70SBen Gras  * copyright notice and this permission notice appear in all copies.
9d65f6f70SBen Gras  *
10d65f6f70SBen Gras  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11d65f6f70SBen Gras  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12d65f6f70SBen Gras  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13d65f6f70SBen Gras  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14d65f6f70SBen Gras  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15d65f6f70SBen Gras  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16d65f6f70SBen Gras  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17d65f6f70SBen Gras  */
18d65f6f70SBen Gras #ifdef HAVE_CONFIG_H
19d65f6f70SBen Gras #include "config.h"
20d65f6f70SBen Gras #endif
21d65f6f70SBen Gras 
22d65f6f70SBen Gras #include <sys/types.h>
23d65f6f70SBen Gras 
24d65f6f70SBen Gras #include <assert.h>
25d65f6f70SBen Gras #include <ctype.h>
26d65f6f70SBen Gras #include <stdint.h>
27d65f6f70SBen Gras #include <stdio.h>
28d65f6f70SBen Gras #include <stdlib.h>
29d65f6f70SBen Gras #include <string.h>
30d65f6f70SBen Gras 
31d65f6f70SBen Gras #include "mandoc.h"
32d65f6f70SBen Gras #include "out.h"
33d65f6f70SBen Gras #include "term.h"
34d65f6f70SBen Gras #include "main.h"
35d65f6f70SBen Gras 
36*0a6a1f1dSLionel Sambuc static	size_t		 cond_width(const struct termp *, int, int *);
37*0a6a1f1dSLionel Sambuc static	void		 adjbuf(struct termp *p, size_t);
38d65f6f70SBen Gras static	void		 bufferc(struct termp *, char);
39d65f6f70SBen Gras static	void		 encode(struct termp *, const char *, size_t);
4092395e9cSLionel Sambuc static	void		 encode1(struct termp *, int);
41d65f6f70SBen Gras 
42d65f6f70SBen Gras void
term_free(struct termp * p)43d65f6f70SBen Gras term_free(struct termp *p)
44d65f6f70SBen Gras {
45d65f6f70SBen Gras 
46d65f6f70SBen Gras 	if (p->buf)
47d65f6f70SBen Gras 		free(p->buf);
48d65f6f70SBen Gras 	if (p->symtab)
4992395e9cSLionel Sambuc 		mchars_free(p->symtab);
50d65f6f70SBen Gras 
51d65f6f70SBen Gras 	free(p);
52d65f6f70SBen Gras }
53d65f6f70SBen Gras 
54d65f6f70SBen Gras 
55d65f6f70SBen Gras void
term_begin(struct termp * p,term_margin head,term_margin foot,const void * arg)56d65f6f70SBen Gras term_begin(struct termp *p, term_margin head,
57d65f6f70SBen Gras 		term_margin foot, const void *arg)
58d65f6f70SBen Gras {
59d65f6f70SBen Gras 
60d65f6f70SBen Gras 	p->headf = head;
61d65f6f70SBen Gras 	p->footf = foot;
62d65f6f70SBen Gras 	p->argf = arg;
63d65f6f70SBen Gras 	(*p->begin)(p);
64d65f6f70SBen Gras }
65d65f6f70SBen Gras 
66d65f6f70SBen Gras 
67d65f6f70SBen Gras void
term_end(struct termp * p)68d65f6f70SBen Gras term_end(struct termp *p)
69d65f6f70SBen Gras {
70d65f6f70SBen Gras 
71d65f6f70SBen Gras 	(*p->end)(p);
72d65f6f70SBen Gras }
73d65f6f70SBen Gras 
74d65f6f70SBen Gras /*
75d65f6f70SBen Gras  * Flush a line of text.  A "line" is loosely defined as being something
76d65f6f70SBen Gras  * that should be followed by a newline, regardless of whether it's
77d65f6f70SBen Gras  * broken apart by newlines getting there.  A line can also be a
78d65f6f70SBen Gras  * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
79d65f6f70SBen Gras  * not have a trailing newline.
80d65f6f70SBen Gras  *
81d65f6f70SBen Gras  * The following flags may be specified:
82d65f6f70SBen Gras  *
83d65f6f70SBen Gras  *  - TERMP_NOBREAK: this is the most important and is used when making
8492395e9cSLionel Sambuc  *    columns.  In short: don't print a newline and instead expect the
8592395e9cSLionel Sambuc  *    next call to do the padding up to the start of the next column.
86*0a6a1f1dSLionel Sambuc  *    p->trailspace may be set to 0, 1, or 2, depending on how many
87*0a6a1f1dSLionel Sambuc  *    space characters are required at the end of the column.
88d65f6f70SBen Gras  *
89d65f6f70SBen Gras  *  - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
90d65f6f70SBen Gras  *    the line is overrun, and don't pad-right if it's underrun.
91d65f6f70SBen Gras  *
92d65f6f70SBen Gras  *  - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
9392395e9cSLionel Sambuc  *    overrunning, instead save the position and continue at that point
94d65f6f70SBen Gras  *    when the next invocation.
95d65f6f70SBen Gras  *
96d65f6f70SBen Gras  *  In-line line breaking:
97d65f6f70SBen Gras  *
98d65f6f70SBen Gras  *  If TERMP_NOBREAK is specified and the line overruns the right
99d65f6f70SBen Gras  *  margin, it will break and pad-right to the right margin after
100d65f6f70SBen Gras  *  writing.  If maxrmargin is violated, it will break and continue
101d65f6f70SBen Gras  *  writing from the right-margin, which will lead to the above scenario
102d65f6f70SBen Gras  *  upon exit.  Otherwise, the line will break at the right margin.
103d65f6f70SBen Gras  */
104d65f6f70SBen Gras void
term_flushln(struct termp * p)105d65f6f70SBen Gras term_flushln(struct termp *p)
106d65f6f70SBen Gras {
107*0a6a1f1dSLionel Sambuc 	size_t		 i;     /* current input position in p->buf */
108*0a6a1f1dSLionel Sambuc 	int		 ntab;	/* number of tabs to prepend */
109d65f6f70SBen Gras 	size_t		 vis;   /* current visual position on output */
110d65f6f70SBen Gras 	size_t		 vbl;   /* number of blanks to prepend to output */
111d65f6f70SBen Gras 	size_t		 vend;	/* end of word visual position on output */
112d65f6f70SBen Gras 	size_t		 bp;    /* visual right border position */
113d65f6f70SBen Gras 	size_t		 dv;    /* temporary for visual pos calculations */
114*0a6a1f1dSLionel Sambuc 	size_t		 j;     /* temporary loop index for p->buf */
115*0a6a1f1dSLionel Sambuc 	size_t		 jhy;	/* last hyph before overflow w/r/t j */
116d65f6f70SBen Gras 	size_t		 maxvis; /* output position of visible boundary */
117d65f6f70SBen Gras 	size_t		 mmax; /* used in calculating bp */
118d65f6f70SBen Gras 
119d65f6f70SBen Gras 	/*
120d65f6f70SBen Gras 	 * First, establish the maximum columns of "visible" content.
121d65f6f70SBen Gras 	 * This is usually the difference between the right-margin and
122d65f6f70SBen Gras 	 * an indentation, but can be, for tagged lists or columns, a
123d65f6f70SBen Gras 	 * small set of values.
124*0a6a1f1dSLionel Sambuc 	 *
125*0a6a1f1dSLionel Sambuc 	 * The following unsigned-signed subtractions look strange,
126*0a6a1f1dSLionel Sambuc 	 * but they are actually correct.  If the int p->overstep
127*0a6a1f1dSLionel Sambuc 	 * is negative, it gets sign extended.  Subtracting that
128*0a6a1f1dSLionel Sambuc 	 * very large size_t effectively adds a small number to dv.
129d65f6f70SBen Gras 	 */
130d65f6f70SBen Gras 	assert  (p->rmargin >= p->offset);
131d65f6f70SBen Gras 	dv     = p->rmargin - p->offset;
132d65f6f70SBen Gras 	maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
133d65f6f70SBen Gras 	dv     = p->maxrmargin - p->offset;
134d65f6f70SBen Gras 	mmax   = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
135d65f6f70SBen Gras 
136d65f6f70SBen Gras 	bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
137d65f6f70SBen Gras 
138d65f6f70SBen Gras 	/*
13992395e9cSLionel Sambuc 	 * Calculate the required amount of padding.
140d65f6f70SBen Gras 	 */
14192395e9cSLionel Sambuc 	vbl = p->offset + p->overstep > p->viscol ?
14292395e9cSLionel Sambuc 	      p->offset + p->overstep - p->viscol : 0;
143d65f6f70SBen Gras 
144d65f6f70SBen Gras 	vis = vend = 0;
145d65f6f70SBen Gras 	i = 0;
146d65f6f70SBen Gras 
14792395e9cSLionel Sambuc 	while (i < p->col) {
148d65f6f70SBen Gras 		/*
149d65f6f70SBen Gras 		 * Handle literal tab characters: collapse all
150d65f6f70SBen Gras 		 * subsequent tabs into a single huge set of spaces.
151d65f6f70SBen Gras 		 */
152*0a6a1f1dSLionel Sambuc 		ntab = 0;
15392395e9cSLionel Sambuc 		while (i < p->col && '\t' == p->buf[i]) {
154d65f6f70SBen Gras 			vend = (vis / p->tabwidth + 1) * p->tabwidth;
155d65f6f70SBen Gras 			vbl += vend - vis;
156d65f6f70SBen Gras 			vis = vend;
157*0a6a1f1dSLionel Sambuc 			ntab++;
158d65f6f70SBen Gras 			i++;
159d65f6f70SBen Gras 		}
160d65f6f70SBen Gras 
161d65f6f70SBen Gras 		/*
162d65f6f70SBen Gras 		 * Count up visible word characters.  Control sequences
163d65f6f70SBen Gras 		 * (starting with the CSI) aren't counted.  A space
164d65f6f70SBen Gras 		 * generates a non-printing word, which is valid (the
165d65f6f70SBen Gras 		 * space is printed according to regular spacing rules).
166d65f6f70SBen Gras 		 */
167d65f6f70SBen Gras 
16892395e9cSLionel Sambuc 		for (j = i, jhy = 0; j < p->col; j++) {
169*0a6a1f1dSLionel Sambuc 			if (' ' == p->buf[j] || '\t' == p->buf[j])
170d65f6f70SBen Gras 				break;
171d65f6f70SBen Gras 
172d65f6f70SBen Gras 			/* Back over the the last printed character. */
173d65f6f70SBen Gras 			if (8 == p->buf[j]) {
174d65f6f70SBen Gras 				assert(j);
175d65f6f70SBen Gras 				vend -= (*p->width)(p, p->buf[j - 1]);
176d65f6f70SBen Gras 				continue;
177d65f6f70SBen Gras 			}
178d65f6f70SBen Gras 
179d65f6f70SBen Gras 			/* Regular word. */
180d65f6f70SBen Gras 			/* Break at the hyphen point if we overrun. */
181d65f6f70SBen Gras 			if (vend > vis && vend < bp &&
182d65f6f70SBen Gras 					ASCII_HYPH == p->buf[j])
183d65f6f70SBen Gras 				jhy = j;
184d65f6f70SBen Gras 
185d65f6f70SBen Gras 			vend += (*p->width)(p, p->buf[j]);
186d65f6f70SBen Gras 		}
187d65f6f70SBen Gras 
188d65f6f70SBen Gras 		/*
189d65f6f70SBen Gras 		 * Find out whether we would exceed the right margin.
190d65f6f70SBen Gras 		 * If so, break to the next line.
191d65f6f70SBen Gras 		 */
192d65f6f70SBen Gras 		if (vend > bp && 0 == jhy && vis > 0) {
193d65f6f70SBen Gras 			vend -= vis;
194d65f6f70SBen Gras 			(*p->endline)(p);
195d65f6f70SBen Gras 			p->viscol = 0;
19692395e9cSLionel Sambuc 			if (TERMP_NOBREAK & p->flags) {
19792395e9cSLionel Sambuc 				vbl = p->rmargin;
19892395e9cSLionel Sambuc 				vend += p->rmargin - p->offset;
19992395e9cSLionel Sambuc 			} else
200d65f6f70SBen Gras 				vbl = p->offset;
201d65f6f70SBen Gras 
202*0a6a1f1dSLionel Sambuc 			/* use pending tabs on the new line */
203*0a6a1f1dSLionel Sambuc 
204*0a6a1f1dSLionel Sambuc 			if (0 < ntab)
205*0a6a1f1dSLionel Sambuc 				vbl += ntab * p->tabwidth;
206*0a6a1f1dSLionel Sambuc 
207*0a6a1f1dSLionel Sambuc 			/*
208*0a6a1f1dSLionel Sambuc 			 * Remove the p->overstep width.
209*0a6a1f1dSLionel Sambuc 			 * Again, if p->overstep is negative,
210*0a6a1f1dSLionel Sambuc 			 * sign extension does the right thing.
211*0a6a1f1dSLionel Sambuc 			 */
212d65f6f70SBen Gras 
213d65f6f70SBen Gras 			bp += (size_t)p->overstep;
214d65f6f70SBen Gras 			p->overstep = 0;
215d65f6f70SBen Gras 		}
216d65f6f70SBen Gras 
217d65f6f70SBen Gras 		/* Write out the [remaining] word. */
21892395e9cSLionel Sambuc 		for ( ; i < p->col; i++) {
219d65f6f70SBen Gras 			if (vend > bp && jhy > 0 && i > jhy)
220d65f6f70SBen Gras 				break;
221d65f6f70SBen Gras 			if ('\t' == p->buf[i])
222d65f6f70SBen Gras 				break;
223d65f6f70SBen Gras 			if (' ' == p->buf[i]) {
224d65f6f70SBen Gras 				j = i;
225d65f6f70SBen Gras 				while (' ' == p->buf[i])
226d65f6f70SBen Gras 					i++;
227*0a6a1f1dSLionel Sambuc 				dv = (i - j) * (*p->width)(p, ' ');
228d65f6f70SBen Gras 				vbl += dv;
229d65f6f70SBen Gras 				vend += dv;
230d65f6f70SBen Gras 				break;
231d65f6f70SBen Gras 			}
232d65f6f70SBen Gras 			if (ASCII_NBRSP == p->buf[i]) {
233d65f6f70SBen Gras 				vbl += (*p->width)(p, ' ');
234d65f6f70SBen Gras 				continue;
235d65f6f70SBen Gras 			}
236d65f6f70SBen Gras 
237d65f6f70SBen Gras 			/*
238d65f6f70SBen Gras 			 * Now we definitely know there will be
239d65f6f70SBen Gras 			 * printable characters to output,
240d65f6f70SBen Gras 			 * so write preceding white space now.
241d65f6f70SBen Gras 			 */
242d65f6f70SBen Gras 			if (vbl) {
243d65f6f70SBen Gras 				(*p->advance)(p, vbl);
244d65f6f70SBen Gras 				p->viscol += vbl;
245d65f6f70SBen Gras 				vbl = 0;
246d65f6f70SBen Gras 			}
247d65f6f70SBen Gras 
248d65f6f70SBen Gras 			if (ASCII_HYPH == p->buf[i]) {
249d65f6f70SBen Gras 				(*p->letter)(p, '-');
250d65f6f70SBen Gras 				p->viscol += (*p->width)(p, '-');
25192395e9cSLionel Sambuc 				continue;
252d65f6f70SBen Gras 			}
25392395e9cSLionel Sambuc 
25492395e9cSLionel Sambuc 			(*p->letter)(p, p->buf[i]);
25592395e9cSLionel Sambuc 			if (8 == p->buf[i])
25692395e9cSLionel Sambuc 				p->viscol -= (*p->width)(p, p->buf[i-1]);
25792395e9cSLionel Sambuc 			else
25892395e9cSLionel Sambuc 				p->viscol += (*p->width)(p, p->buf[i]);
259d65f6f70SBen Gras 		}
260d65f6f70SBen Gras 		vis = vend;
261d65f6f70SBen Gras 	}
262d65f6f70SBen Gras 
263d65f6f70SBen Gras 	/*
264d65f6f70SBen Gras 	 * If there was trailing white space, it was not printed;
265d65f6f70SBen Gras 	 * so reset the cursor position accordingly.
266d65f6f70SBen Gras 	 */
26792395e9cSLionel Sambuc 	if (vis)
268d65f6f70SBen Gras 		vis -= vbl;
269d65f6f70SBen Gras 
270d65f6f70SBen Gras 	p->col = 0;
271d65f6f70SBen Gras 	p->overstep = 0;
272d65f6f70SBen Gras 
273d65f6f70SBen Gras 	if ( ! (TERMP_NOBREAK & p->flags)) {
274d65f6f70SBen Gras 		p->viscol = 0;
275d65f6f70SBen Gras 		(*p->endline)(p);
276d65f6f70SBen Gras 		return;
277d65f6f70SBen Gras 	}
278d65f6f70SBen Gras 
279d65f6f70SBen Gras 	if (TERMP_HANG & p->flags) {
280*0a6a1f1dSLionel Sambuc 		p->overstep = (int)(vis - maxvis +
281*0a6a1f1dSLionel Sambuc 				p->trailspace * (*p->width)(p, ' '));
282d65f6f70SBen Gras 
283d65f6f70SBen Gras 		/*
284d65f6f70SBen Gras 		 * If we have overstepped the margin, temporarily move
285d65f6f70SBen Gras 		 * it to the right and flag the rest of the line to be
286d65f6f70SBen Gras 		 * shorter.
287*0a6a1f1dSLionel Sambuc 		 * If there is a request to keep the columns together,
288*0a6a1f1dSLionel Sambuc 		 * allow negative overstep when the column is not full.
289d65f6f70SBen Gras 		 */
290*0a6a1f1dSLionel Sambuc 		if (p->trailspace && p->overstep < 0)
291d65f6f70SBen Gras 			p->overstep = 0;
29292395e9cSLionel Sambuc 		return;
293d65f6f70SBen Gras 
294d65f6f70SBen Gras 	} else if (TERMP_DANGLE & p->flags)
295d65f6f70SBen Gras 		return;
296d65f6f70SBen Gras 
29792395e9cSLionel Sambuc 	/* If the column was overrun, break the line. */
298*0a6a1f1dSLionel Sambuc 	if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
299d65f6f70SBen Gras 		(*p->endline)(p);
30092395e9cSLionel Sambuc 		p->viscol = 0;
301d65f6f70SBen Gras 	}
302d65f6f70SBen Gras }
303d65f6f70SBen Gras 
304d65f6f70SBen Gras 
305d65f6f70SBen Gras /*
306d65f6f70SBen Gras  * A newline only breaks an existing line; it won't assert vertical
307d65f6f70SBen Gras  * space.  All data in the output buffer is flushed prior to the newline
308d65f6f70SBen Gras  * assertion.
309d65f6f70SBen Gras  */
310d65f6f70SBen Gras void
term_newln(struct termp * p)311d65f6f70SBen Gras term_newln(struct termp *p)
312d65f6f70SBen Gras {
313d65f6f70SBen Gras 
314d65f6f70SBen Gras 	p->flags |= TERMP_NOSPACE;
31592395e9cSLionel Sambuc 	if (p->col || p->viscol)
316d65f6f70SBen Gras 		term_flushln(p);
317d65f6f70SBen Gras }
318d65f6f70SBen Gras 
319d65f6f70SBen Gras 
320d65f6f70SBen Gras /*
321d65f6f70SBen Gras  * Asserts a vertical space (a full, empty line-break between lines).
322d65f6f70SBen Gras  * Note that if used twice, this will cause two blank spaces and so on.
323d65f6f70SBen Gras  * All data in the output buffer is flushed prior to the newline
324d65f6f70SBen Gras  * assertion.
325d65f6f70SBen Gras  */
326d65f6f70SBen Gras void
term_vspace(struct termp * p)327d65f6f70SBen Gras term_vspace(struct termp *p)
328d65f6f70SBen Gras {
329d65f6f70SBen Gras 
330d65f6f70SBen Gras 	term_newln(p);
331d65f6f70SBen Gras 	p->viscol = 0;
332*0a6a1f1dSLionel Sambuc 	if (0 < p->skipvsp)
333*0a6a1f1dSLionel Sambuc 		p->skipvsp--;
334*0a6a1f1dSLionel Sambuc 	else
335d65f6f70SBen Gras 		(*p->endline)(p);
336d65f6f70SBen Gras }
337d65f6f70SBen Gras 
338d65f6f70SBen Gras void
term_fontlast(struct termp * p)339d65f6f70SBen Gras term_fontlast(struct termp *p)
340d65f6f70SBen Gras {
341d65f6f70SBen Gras 	enum termfont	 f;
342d65f6f70SBen Gras 
343d65f6f70SBen Gras 	f = p->fontl;
344d65f6f70SBen Gras 	p->fontl = p->fontq[p->fonti];
345d65f6f70SBen Gras 	p->fontq[p->fonti] = f;
346d65f6f70SBen Gras }
347d65f6f70SBen Gras 
348d65f6f70SBen Gras 
349d65f6f70SBen Gras void
term_fontrepl(struct termp * p,enum termfont f)350d65f6f70SBen Gras term_fontrepl(struct termp *p, enum termfont f)
351d65f6f70SBen Gras {
352d65f6f70SBen Gras 
353d65f6f70SBen Gras 	p->fontl = p->fontq[p->fonti];
354d65f6f70SBen Gras 	p->fontq[p->fonti] = f;
355d65f6f70SBen Gras }
356d65f6f70SBen Gras 
357d65f6f70SBen Gras 
358d65f6f70SBen Gras void
term_fontpush(struct termp * p,enum termfont f)359d65f6f70SBen Gras term_fontpush(struct termp *p, enum termfont f)
360d65f6f70SBen Gras {
361d65f6f70SBen Gras 
362d65f6f70SBen Gras 	assert(p->fonti + 1 < 10);
363d65f6f70SBen Gras 	p->fontl = p->fontq[p->fonti];
364d65f6f70SBen Gras 	p->fontq[++p->fonti] = f;
365d65f6f70SBen Gras }
366d65f6f70SBen Gras 
367d65f6f70SBen Gras 
368d65f6f70SBen Gras const void *
term_fontq(struct termp * p)369d65f6f70SBen Gras term_fontq(struct termp *p)
370d65f6f70SBen Gras {
371d65f6f70SBen Gras 
372d65f6f70SBen Gras 	return(&p->fontq[p->fonti]);
373d65f6f70SBen Gras }
374d65f6f70SBen Gras 
375d65f6f70SBen Gras 
376d65f6f70SBen Gras enum termfont
term_fonttop(struct termp * p)377d65f6f70SBen Gras term_fonttop(struct termp *p)
378d65f6f70SBen Gras {
379d65f6f70SBen Gras 
380d65f6f70SBen Gras 	return(p->fontq[p->fonti]);
381d65f6f70SBen Gras }
382d65f6f70SBen Gras 
383d65f6f70SBen Gras 
384d65f6f70SBen Gras void
term_fontpopq(struct termp * p,const void * key)385d65f6f70SBen Gras term_fontpopq(struct termp *p, const void *key)
386d65f6f70SBen Gras {
387d65f6f70SBen Gras 
388*0a6a1f1dSLionel Sambuc 	while (p->fonti >= 0 && key < (void *)(p->fontq + p->fonti))
389d65f6f70SBen Gras 		p->fonti--;
390d65f6f70SBen Gras 	assert(p->fonti >= 0);
391d65f6f70SBen Gras }
392d65f6f70SBen Gras 
393d65f6f70SBen Gras 
394d65f6f70SBen Gras void
term_fontpop(struct termp * p)395d65f6f70SBen Gras term_fontpop(struct termp *p)
396d65f6f70SBen Gras {
397d65f6f70SBen Gras 
398d65f6f70SBen Gras 	assert(p->fonti);
399d65f6f70SBen Gras 	p->fonti--;
400d65f6f70SBen Gras }
401d65f6f70SBen Gras 
402d65f6f70SBen Gras /*
403d65f6f70SBen Gras  * Handle pwords, partial words, which may be either a single word or a
404d65f6f70SBen Gras  * phrase that cannot be broken down (such as a literal string).  This
405d65f6f70SBen Gras  * handles word styling.
406d65f6f70SBen Gras  */
407d65f6f70SBen Gras void
term_word(struct termp * p,const char * word)408d65f6f70SBen Gras term_word(struct termp *p, const char *word)
409d65f6f70SBen Gras {
410*0a6a1f1dSLionel Sambuc 	const char	 nbrsp[2] = { ASCII_NBRSP, 0 };
41192395e9cSLionel Sambuc 	const char	*seq, *cp;
41292395e9cSLionel Sambuc 	char		 c;
41392395e9cSLionel Sambuc 	int		 sz, uc;
414d65f6f70SBen Gras 	size_t		 ssz;
41592395e9cSLionel Sambuc 	enum mandoc_esc	 esc;
416d65f6f70SBen Gras 
417d65f6f70SBen Gras 	if ( ! (TERMP_NOSPACE & p->flags)) {
418d65f6f70SBen Gras 		if ( ! (TERMP_KEEP & p->flags)) {
419d65f6f70SBen Gras 			bufferc(p, ' ');
420d65f6f70SBen Gras 			if (TERMP_SENTENCE & p->flags)
421d65f6f70SBen Gras 				bufferc(p, ' ');
422d65f6f70SBen Gras 		} else
423d65f6f70SBen Gras 			bufferc(p, ASCII_NBRSP);
424d65f6f70SBen Gras 	}
425*0a6a1f1dSLionel Sambuc 	if (TERMP_PREKEEP & p->flags)
426*0a6a1f1dSLionel Sambuc 		p->flags |= TERMP_KEEP;
427d65f6f70SBen Gras 
428d65f6f70SBen Gras 	if ( ! (p->flags & TERMP_NONOSPACE))
429d65f6f70SBen Gras 		p->flags &= ~TERMP_NOSPACE;
430d65f6f70SBen Gras 	else
431d65f6f70SBen Gras 		p->flags |= TERMP_NOSPACE;
432d65f6f70SBen Gras 
433*0a6a1f1dSLionel Sambuc 	p->flags &= ~TERMP_SENTENCE;
434d65f6f70SBen Gras 
43592395e9cSLionel Sambuc 	while ('\0' != *word) {
436*0a6a1f1dSLionel Sambuc 		if ('\\' != *word) {
437*0a6a1f1dSLionel Sambuc 			if (TERMP_SKIPCHAR & p->flags) {
438*0a6a1f1dSLionel Sambuc 				p->flags &= ~TERMP_SKIPCHAR;
439*0a6a1f1dSLionel Sambuc 				word++;
440d65f6f70SBen Gras 				continue;
441*0a6a1f1dSLionel Sambuc 			}
442*0a6a1f1dSLionel Sambuc 			if (TERMP_NBRWORD & p->flags) {
443*0a6a1f1dSLionel Sambuc 				if (' ' == *word) {
444*0a6a1f1dSLionel Sambuc 					encode(p, nbrsp, 1);
445*0a6a1f1dSLionel Sambuc 					word++;
446*0a6a1f1dSLionel Sambuc 					continue;
447*0a6a1f1dSLionel Sambuc 				}
448*0a6a1f1dSLionel Sambuc 				ssz = strcspn(word, "\\ ");
449*0a6a1f1dSLionel Sambuc 			} else
450*0a6a1f1dSLionel Sambuc 				ssz = strcspn(word, "\\");
451*0a6a1f1dSLionel Sambuc 			encode(p, word, ssz);
452*0a6a1f1dSLionel Sambuc 			word += (int)ssz;
453*0a6a1f1dSLionel Sambuc 			continue;
454*0a6a1f1dSLionel Sambuc 		}
455d65f6f70SBen Gras 
45692395e9cSLionel Sambuc 		word++;
45792395e9cSLionel Sambuc 		esc = mandoc_escape(&word, &seq, &sz);
45892395e9cSLionel Sambuc 		if (ESCAPE_ERROR == esc)
45992395e9cSLionel Sambuc 			break;
460d65f6f70SBen Gras 
46192395e9cSLionel Sambuc 		if (TERMENC_ASCII != p->enc)
46292395e9cSLionel Sambuc 			switch (esc) {
46392395e9cSLionel Sambuc 			case (ESCAPE_UNICODE):
46492395e9cSLionel Sambuc 				uc = mchars_num2uc(seq + 1, sz - 1);
46592395e9cSLionel Sambuc 				if ('\0' == uc)
466d65f6f70SBen Gras 					break;
46792395e9cSLionel Sambuc 				encode1(p, uc);
46892395e9cSLionel Sambuc 				continue;
46992395e9cSLionel Sambuc 			case (ESCAPE_SPECIAL):
47092395e9cSLionel Sambuc 				uc = mchars_spec2cp(p->symtab, seq, sz);
47192395e9cSLionel Sambuc 				if (uc <= 0)
472d65f6f70SBen Gras 					break;
47392395e9cSLionel Sambuc 				encode1(p, uc);
47492395e9cSLionel Sambuc 				continue;
47592395e9cSLionel Sambuc 			default:
47692395e9cSLionel Sambuc 				break;
47792395e9cSLionel Sambuc 			}
47892395e9cSLionel Sambuc 
47992395e9cSLionel Sambuc 		switch (esc) {
48092395e9cSLionel Sambuc 		case (ESCAPE_UNICODE):
48192395e9cSLionel Sambuc 			encode1(p, '?');
48292395e9cSLionel Sambuc 			break;
48392395e9cSLionel Sambuc 		case (ESCAPE_NUMBERED):
48492395e9cSLionel Sambuc 			c = mchars_num2char(seq, sz);
48592395e9cSLionel Sambuc 			if ('\0' != c)
48692395e9cSLionel Sambuc 				encode(p, &c, 1);
48792395e9cSLionel Sambuc 			break;
48892395e9cSLionel Sambuc 		case (ESCAPE_SPECIAL):
48992395e9cSLionel Sambuc 			cp = mchars_spec2str(p->symtab, seq, sz, &ssz);
49092395e9cSLionel Sambuc 			if (NULL != cp)
49192395e9cSLionel Sambuc 				encode(p, cp, ssz);
49292395e9cSLionel Sambuc 			else if (1 == ssz)
49392395e9cSLionel Sambuc 				encode(p, seq, sz);
49492395e9cSLionel Sambuc 			break;
49592395e9cSLionel Sambuc 		case (ESCAPE_FONTBOLD):
496d65f6f70SBen Gras 			term_fontrepl(p, TERMFONT_BOLD);
497d65f6f70SBen Gras 			break;
49892395e9cSLionel Sambuc 		case (ESCAPE_FONTITALIC):
499d65f6f70SBen Gras 			term_fontrepl(p, TERMFONT_UNDER);
500d65f6f70SBen Gras 			break;
501*0a6a1f1dSLionel Sambuc 		case (ESCAPE_FONTBI):
502*0a6a1f1dSLionel Sambuc 			term_fontrepl(p, TERMFONT_BI);
503*0a6a1f1dSLionel Sambuc 			break;
50492395e9cSLionel Sambuc 		case (ESCAPE_FONT):
50592395e9cSLionel Sambuc 			/* FALLTHROUGH */
50692395e9cSLionel Sambuc 		case (ESCAPE_FONTROMAN):
507d65f6f70SBen Gras 			term_fontrepl(p, TERMFONT_NONE);
508d65f6f70SBen Gras 			break;
50992395e9cSLionel Sambuc 		case (ESCAPE_FONTPREV):
510d65f6f70SBen Gras 			term_fontlast(p);
511d65f6f70SBen Gras 			break;
51292395e9cSLionel Sambuc 		case (ESCAPE_NOSPACE):
513*0a6a1f1dSLionel Sambuc 			if (TERMP_SKIPCHAR & p->flags)
514*0a6a1f1dSLionel Sambuc 				p->flags &= ~TERMP_SKIPCHAR;
515*0a6a1f1dSLionel Sambuc 			else if ('\0' == *word)
516d65f6f70SBen Gras 				p->flags |= TERMP_NOSPACE;
517d65f6f70SBen Gras 			break;
518*0a6a1f1dSLionel Sambuc 		case (ESCAPE_SKIPCHAR):
519*0a6a1f1dSLionel Sambuc 			p->flags |= TERMP_SKIPCHAR;
520*0a6a1f1dSLionel Sambuc 			break;
521d65f6f70SBen Gras 		default:
522d65f6f70SBen Gras 			break;
523d65f6f70SBen Gras 		}
524d65f6f70SBen Gras 	}
525*0a6a1f1dSLionel Sambuc 	p->flags &= ~TERMP_NBRWORD;
52692395e9cSLionel Sambuc }
527d65f6f70SBen Gras 
528d65f6f70SBen Gras static void
adjbuf(struct termp * p,size_t sz)529*0a6a1f1dSLionel Sambuc adjbuf(struct termp *p, size_t sz)
530d65f6f70SBen Gras {
531d65f6f70SBen Gras 
532d65f6f70SBen Gras 	if (0 == p->maxcols)
533d65f6f70SBen Gras 		p->maxcols = 1024;
534d65f6f70SBen Gras 	while (sz >= p->maxcols)
535d65f6f70SBen Gras 		p->maxcols <<= 2;
536d65f6f70SBen Gras 
537*0a6a1f1dSLionel Sambuc 	p->buf = mandoc_realloc(p->buf, sizeof(int) * p->maxcols);
538d65f6f70SBen Gras }
539d65f6f70SBen Gras 
540d65f6f70SBen Gras static void
bufferc(struct termp * p,char c)541d65f6f70SBen Gras bufferc(struct termp *p, char c)
542d65f6f70SBen Gras {
543d65f6f70SBen Gras 
544d65f6f70SBen Gras 	if (p->col + 1 >= p->maxcols)
545d65f6f70SBen Gras 		adjbuf(p, p->col + 1);
546d65f6f70SBen Gras 
54792395e9cSLionel Sambuc 	p->buf[p->col++] = c;
548d65f6f70SBen Gras }
549d65f6f70SBen Gras 
55092395e9cSLionel Sambuc /*
55192395e9cSLionel Sambuc  * See encode().
55292395e9cSLionel Sambuc  * Do this for a single (probably unicode) value.
55392395e9cSLionel Sambuc  * Does not check for non-decorated glyphs.
55492395e9cSLionel Sambuc  */
55592395e9cSLionel Sambuc static void
encode1(struct termp * p,int c)55692395e9cSLionel Sambuc encode1(struct termp *p, int c)
55792395e9cSLionel Sambuc {
55892395e9cSLionel Sambuc 	enum termfont	  f;
55992395e9cSLionel Sambuc 
560*0a6a1f1dSLionel Sambuc 	if (TERMP_SKIPCHAR & p->flags) {
561*0a6a1f1dSLionel Sambuc 		p->flags &= ~TERMP_SKIPCHAR;
562*0a6a1f1dSLionel Sambuc 		return;
563*0a6a1f1dSLionel Sambuc 	}
564*0a6a1f1dSLionel Sambuc 
565*0a6a1f1dSLionel Sambuc 	if (p->col + 6 >= p->maxcols)
566*0a6a1f1dSLionel Sambuc 		adjbuf(p, p->col + 6);
56792395e9cSLionel Sambuc 
56892395e9cSLionel Sambuc 	f = term_fonttop(p);
56992395e9cSLionel Sambuc 
570*0a6a1f1dSLionel Sambuc 	if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
57192395e9cSLionel Sambuc 		p->buf[p->col++] = '_';
57292395e9cSLionel Sambuc 		p->buf[p->col++] = 8;
573*0a6a1f1dSLionel Sambuc 	}
574*0a6a1f1dSLionel Sambuc 	if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
575*0a6a1f1dSLionel Sambuc 		if (ASCII_HYPH == c)
576*0a6a1f1dSLionel Sambuc 			p->buf[p->col++] = '-';
577*0a6a1f1dSLionel Sambuc 		else
578*0a6a1f1dSLionel Sambuc 			p->buf[p->col++] = c;
579*0a6a1f1dSLionel Sambuc 		p->buf[p->col++] = 8;
580*0a6a1f1dSLionel Sambuc 	}
58192395e9cSLionel Sambuc 	p->buf[p->col++] = c;
58292395e9cSLionel Sambuc }
583d65f6f70SBen Gras 
584d65f6f70SBen Gras static void
encode(struct termp * p,const char * word,size_t sz)585d65f6f70SBen Gras encode(struct termp *p, const char *word, size_t sz)
586d65f6f70SBen Gras {
587*0a6a1f1dSLionel Sambuc 	size_t		  i;
58892395e9cSLionel Sambuc 
589*0a6a1f1dSLionel Sambuc 	if (TERMP_SKIPCHAR & p->flags) {
590*0a6a1f1dSLionel Sambuc 		p->flags &= ~TERMP_SKIPCHAR;
591*0a6a1f1dSLionel Sambuc 		return;
592*0a6a1f1dSLionel Sambuc 	}
593d65f6f70SBen Gras 
594d65f6f70SBen Gras 	/*
595d65f6f70SBen Gras 	 * Encode and buffer a string of characters.  If the current
596d65f6f70SBen Gras 	 * font mode is unset, buffer directly, else encode then buffer
597d65f6f70SBen Gras 	 * character by character.
598d65f6f70SBen Gras 	 */
599d65f6f70SBen Gras 
600*0a6a1f1dSLionel Sambuc 	if (TERMFONT_NONE == term_fonttop(p)) {
601*0a6a1f1dSLionel Sambuc 		if (p->col + sz >= p->maxcols)
602*0a6a1f1dSLionel Sambuc 			adjbuf(p, p->col + sz);
603*0a6a1f1dSLionel Sambuc 		for (i = 0; i < sz; i++)
60492395e9cSLionel Sambuc 			p->buf[p->col++] = word[i];
605d65f6f70SBen Gras 		return;
606d65f6f70SBen Gras 	}
607d65f6f70SBen Gras 
608d65f6f70SBen Gras 	/* Pre-buffer, assuming worst-case. */
609d65f6f70SBen Gras 
610*0a6a1f1dSLionel Sambuc 	if (p->col + 1 + (sz * 5) >= p->maxcols)
611*0a6a1f1dSLionel Sambuc 		adjbuf(p, p->col + 1 + (sz * 5));
612d65f6f70SBen Gras 
613*0a6a1f1dSLionel Sambuc 	for (i = 0; i < sz; i++) {
614*0a6a1f1dSLionel Sambuc 		if (ASCII_HYPH == word[i] ||
615*0a6a1f1dSLionel Sambuc 		    isgraph((unsigned char)word[i]))
616*0a6a1f1dSLionel Sambuc 			encode1(p, word[i]);
617d65f6f70SBen Gras 		else
61892395e9cSLionel Sambuc 			p->buf[p->col++] = word[i];
619d65f6f70SBen Gras 	}
620d65f6f70SBen Gras }
621d65f6f70SBen Gras 
622d65f6f70SBen Gras size_t
term_len(const struct termp * p,size_t sz)623d65f6f70SBen Gras term_len(const struct termp *p, size_t sz)
624d65f6f70SBen Gras {
625d65f6f70SBen Gras 
626d65f6f70SBen Gras 	return((*p->width)(p, ' ') * sz);
627d65f6f70SBen Gras }
628d65f6f70SBen Gras 
629*0a6a1f1dSLionel Sambuc static size_t
cond_width(const struct termp * p,int c,int * skip)630*0a6a1f1dSLionel Sambuc cond_width(const struct termp *p, int c, int *skip)
631*0a6a1f1dSLionel Sambuc {
632*0a6a1f1dSLionel Sambuc 
633*0a6a1f1dSLionel Sambuc 	if (*skip) {
634*0a6a1f1dSLionel Sambuc 		(*skip) = 0;
635*0a6a1f1dSLionel Sambuc 		return(0);
636*0a6a1f1dSLionel Sambuc 	} else
637*0a6a1f1dSLionel Sambuc 		return((*p->width)(p, c));
638*0a6a1f1dSLionel Sambuc }
639d65f6f70SBen Gras 
640d65f6f70SBen Gras size_t
term_strlen(const struct termp * p,const char * cp)641d65f6f70SBen Gras term_strlen(const struct termp *p, const char *cp)
642d65f6f70SBen Gras {
64392395e9cSLionel Sambuc 	size_t		 sz, rsz, i;
644*0a6a1f1dSLionel Sambuc 	int		 ssz, skip, c;
645d65f6f70SBen Gras 	const char	*seq, *rhs;
64692395e9cSLionel Sambuc 	enum mandoc_esc	 esc;
64792395e9cSLionel Sambuc 	static const char rej[] = { '\\', ASCII_HYPH, ASCII_NBRSP, '\0' };
648d65f6f70SBen Gras 
649d65f6f70SBen Gras 	/*
650d65f6f70SBen Gras 	 * Account for escaped sequences within string length
65192395e9cSLionel Sambuc 	 * calculations.  This follows the logic in term_word() as we
65292395e9cSLionel Sambuc 	 * must calculate the width of produced strings.
653d65f6f70SBen Gras 	 */
654d65f6f70SBen Gras 
65592395e9cSLionel Sambuc 	sz = 0;
656*0a6a1f1dSLionel Sambuc 	skip = 0;
65792395e9cSLionel Sambuc 	while ('\0' != *cp) {
65892395e9cSLionel Sambuc 		rsz = strcspn(cp, rej);
65992395e9cSLionel Sambuc 		for (i = 0; i < rsz; i++)
660*0a6a1f1dSLionel Sambuc 			sz += cond_width(p, *cp++, &skip);
66192395e9cSLionel Sambuc 
66292395e9cSLionel Sambuc 		switch (*cp) {
66392395e9cSLionel Sambuc 		case ('\\'):
66492395e9cSLionel Sambuc 			cp++;
66592395e9cSLionel Sambuc 			esc = mandoc_escape(&cp, &seq, &ssz);
66692395e9cSLionel Sambuc 			if (ESCAPE_ERROR == esc)
66792395e9cSLionel Sambuc 				return(sz);
66892395e9cSLionel Sambuc 
66992395e9cSLionel Sambuc 			if (TERMENC_ASCII != p->enc)
67092395e9cSLionel Sambuc 				switch (esc) {
67192395e9cSLionel Sambuc 				case (ESCAPE_UNICODE):
67292395e9cSLionel Sambuc 					c = mchars_num2uc
67392395e9cSLionel Sambuc 						(seq + 1, ssz - 1);
67492395e9cSLionel Sambuc 					if ('\0' == c)
675d65f6f70SBen Gras 						break;
676*0a6a1f1dSLionel Sambuc 					sz += cond_width(p, c, &skip);
67792395e9cSLionel Sambuc 					continue;
67892395e9cSLionel Sambuc 				case (ESCAPE_SPECIAL):
67992395e9cSLionel Sambuc 					c = mchars_spec2cp
68092395e9cSLionel Sambuc 						(p->symtab, seq, ssz);
68192395e9cSLionel Sambuc 					if (c <= 0)
68292395e9cSLionel Sambuc 						break;
683*0a6a1f1dSLionel Sambuc 					sz += cond_width(p, c, &skip);
68492395e9cSLionel Sambuc 					continue;
68592395e9cSLionel Sambuc 				default:
68692395e9cSLionel Sambuc 					break;
68792395e9cSLionel Sambuc 				}
68892395e9cSLionel Sambuc 
68992395e9cSLionel Sambuc 			rhs = NULL;
69092395e9cSLionel Sambuc 
69192395e9cSLionel Sambuc 			switch (esc) {
69292395e9cSLionel Sambuc 			case (ESCAPE_UNICODE):
693*0a6a1f1dSLionel Sambuc 				sz += cond_width(p, '?', &skip);
69492395e9cSLionel Sambuc 				break;
69592395e9cSLionel Sambuc 			case (ESCAPE_NUMBERED):
69692395e9cSLionel Sambuc 				c = mchars_num2char(seq, ssz);
69792395e9cSLionel Sambuc 				if ('\0' != c)
698*0a6a1f1dSLionel Sambuc 					sz += cond_width(p, c, &skip);
69992395e9cSLionel Sambuc 				break;
70092395e9cSLionel Sambuc 			case (ESCAPE_SPECIAL):
70192395e9cSLionel Sambuc 				rhs = mchars_spec2str
702d65f6f70SBen Gras 					(p->symtab, seq, ssz, &rsz);
703d65f6f70SBen Gras 
70492395e9cSLionel Sambuc 				if (ssz != 1 || rhs)
705d65f6f70SBen Gras 					break;
706d65f6f70SBen Gras 
707d65f6f70SBen Gras 				rhs = seq;
708d65f6f70SBen Gras 				rsz = ssz;
709d65f6f70SBen Gras 				break;
710*0a6a1f1dSLionel Sambuc 			case (ESCAPE_SKIPCHAR):
711*0a6a1f1dSLionel Sambuc 				skip = 1;
712*0a6a1f1dSLionel Sambuc 				break;
713d65f6f70SBen Gras 			default:
714d65f6f70SBen Gras 				break;
715d65f6f70SBen Gras 			}
716d65f6f70SBen Gras 
71792395e9cSLionel Sambuc 			if (NULL == rhs)
71892395e9cSLionel Sambuc 				break;
71992395e9cSLionel Sambuc 
720*0a6a1f1dSLionel Sambuc 			if (skip) {
721*0a6a1f1dSLionel Sambuc 				skip = 0;
722*0a6a1f1dSLionel Sambuc 				break;
723*0a6a1f1dSLionel Sambuc 			}
724*0a6a1f1dSLionel Sambuc 
725d65f6f70SBen Gras 			for (i = 0; i < rsz; i++)
726d65f6f70SBen Gras 				sz += (*p->width)(p, *rhs++);
72792395e9cSLionel Sambuc 			break;
72892395e9cSLionel Sambuc 		case (ASCII_NBRSP):
729*0a6a1f1dSLionel Sambuc 			sz += cond_width(p, ' ', &skip);
730d65f6f70SBen Gras 			cp++;
73192395e9cSLionel Sambuc 			break;
73292395e9cSLionel Sambuc 		case (ASCII_HYPH):
733*0a6a1f1dSLionel Sambuc 			sz += cond_width(p, '-', &skip);
734d65f6f70SBen Gras 			cp++;
73592395e9cSLionel Sambuc 			break;
73692395e9cSLionel Sambuc 		default:
73792395e9cSLionel Sambuc 			break;
73892395e9cSLionel Sambuc 		}
73992395e9cSLionel Sambuc 	}
740d65f6f70SBen Gras 
741d65f6f70SBen Gras 	return(sz);
742d65f6f70SBen Gras }
743d65f6f70SBen Gras 
744d65f6f70SBen Gras /* ARGSUSED */
745d65f6f70SBen Gras size_t
term_vspan(const struct termp * p,const struct roffsu * su)746d65f6f70SBen Gras term_vspan(const struct termp *p, const struct roffsu *su)
747d65f6f70SBen Gras {
748d65f6f70SBen Gras 	double		 r;
749d65f6f70SBen Gras 
750d65f6f70SBen Gras 	switch (su->unit) {
751d65f6f70SBen Gras 	case (SCALE_CM):
752d65f6f70SBen Gras 		r = su->scale * 2;
753d65f6f70SBen Gras 		break;
754d65f6f70SBen Gras 	case (SCALE_IN):
755d65f6f70SBen Gras 		r = su->scale * 6;
756d65f6f70SBen Gras 		break;
757d65f6f70SBen Gras 	case (SCALE_PC):
758d65f6f70SBen Gras 		r = su->scale;
759d65f6f70SBen Gras 		break;
760d65f6f70SBen Gras 	case (SCALE_PT):
761d65f6f70SBen Gras 		r = su->scale / 8;
762d65f6f70SBen Gras 		break;
763d65f6f70SBen Gras 	case (SCALE_MM):
764d65f6f70SBen Gras 		r = su->scale / 1000;
765d65f6f70SBen Gras 		break;
766d65f6f70SBen Gras 	case (SCALE_VS):
767d65f6f70SBen Gras 		r = su->scale;
768d65f6f70SBen Gras 		break;
769d65f6f70SBen Gras 	default:
770d65f6f70SBen Gras 		r = su->scale - 1;
771d65f6f70SBen Gras 		break;
772d65f6f70SBen Gras 	}
773d65f6f70SBen Gras 
774d65f6f70SBen Gras 	if (r < 0.0)
775d65f6f70SBen Gras 		r = 0.0;
776d65f6f70SBen Gras 	return(/* LINTED */(size_t)
777d65f6f70SBen Gras 			r);
778d65f6f70SBen Gras }
779d65f6f70SBen Gras 
780d65f6f70SBen Gras size_t
term_hspan(const struct termp * p,const struct roffsu * su)781d65f6f70SBen Gras term_hspan(const struct termp *p, const struct roffsu *su)
782d65f6f70SBen Gras {
783d65f6f70SBen Gras 	double		 v;
784d65f6f70SBen Gras 
785d65f6f70SBen Gras 	v = ((*p->hspan)(p, su));
786d65f6f70SBen Gras 	if (v < 0.0)
787d65f6f70SBen Gras 		v = 0.0;
788d65f6f70SBen Gras 	return((size_t) /* LINTED */
789d65f6f70SBen Gras 			v);
790d65f6f70SBen Gras }
791