xref: /openbsd/usr.bin/mandoc/tbl_term.c (revision 4cfece93)
1 /*	$OpenBSD: tbl_term.c,v 1.61 2020/01/11 16:24:33 schwarze Exp $ */
2 /*
3  * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2011-2020 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 <sys/types.h>
19 
20 #include <assert.h>
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "mandoc.h"
27 #include "tbl.h"
28 #include "out.h"
29 #include "term.h"
30 
31 #define	IS_HORIZ(cp)	((cp)->pos == TBL_CELL_HORIZ || \
32 			 (cp)->pos == TBL_CELL_DHORIZ)
33 
34 
35 static	size_t	term_tbl_len(size_t, void *);
36 static	size_t	term_tbl_strlen(const char *, void *);
37 static	size_t	term_tbl_sulen(const struct roffsu *, void *);
38 static	void	tbl_data(struct termp *, const struct tbl_opts *,
39 			const struct tbl_cell *,
40 			const struct tbl_dat *,
41 			const struct roffcol *);
42 static	void	tbl_direct_border(struct termp *, int, size_t);
43 static	void	tbl_fill_border(struct termp *, int, size_t);
44 static	void	tbl_fill_char(struct termp *, char, size_t);
45 static	void	tbl_fill_string(struct termp *, const char *, size_t);
46 static	void	tbl_hrule(struct termp *, const struct tbl_span *,
47 			const struct tbl_span *, const struct tbl_span *,
48 			int);
49 static	void	tbl_literal(struct termp *, const struct tbl_dat *,
50 			const struct roffcol *);
51 static	void	tbl_number(struct termp *, const struct tbl_opts *,
52 			const struct tbl_dat *,
53 			const struct roffcol *);
54 static	void	tbl_word(struct termp *, const struct tbl_dat *);
55 
56 
57 /*
58  * The following border-character tables are indexed
59  * by ternary (3-based) numbers, as opposed to binary or decimal.
60  * Each ternary digit describes the line width in one direction:
61  * 0 means no line, 1 single or light line, 2 double or heavy line.
62  */
63 
64 /* Positional values of the four directions. */
65 #define	BRIGHT	1
66 #define	BDOWN	3
67 #define	BLEFT	(3 * 3)
68 #define	BUP	(3 * 3 * 3)
69 #define	BHORIZ	(BLEFT + BRIGHT)
70 
71 /* Code points to use for each combination of widths. */
72 static  const int borders_utf8[81] = {
73 	0x0020, 0x2576, 0x257a,  /* 000 right */
74 	0x2577, 0x250c, 0x250d,  /* 001 down */
75 	0x257b, 0x250e, 0x250f,  /* 002 */
76 	0x2574, 0x2500, 0x257c,  /* 010 left */
77 	0x2510, 0x252c, 0x252e,  /* 011 left down */
78 	0x2512, 0x2530, 0x2532,  /* 012 */
79 	0x2578, 0x257e, 0x2501,  /* 020 left */
80 	0x2511, 0x252d, 0x252f,  /* 021 left down */
81 	0x2513, 0x2531, 0x2533,  /* 022 */
82 	0x2575, 0x2514, 0x2515,  /* 100 up */
83 	0x2502, 0x251c, 0x251d,  /* 101 up down */
84 	0x257d, 0x251f, 0x2522,  /* 102 */
85 	0x2518, 0x2534, 0x2536,  /* 110 up left */
86 	0x2524, 0x253c, 0x253e,  /* 111 all */
87 	0x2527, 0x2541, 0x2546,  /* 112 */
88 	0x2519, 0x2535, 0x2537,  /* 120 up left */
89 	0x2525, 0x253d, 0x253f,  /* 121 all */
90 	0x252a, 0x2545, 0x2548,  /* 122 */
91 	0x2579, 0x2516, 0x2517,  /* 200 up */
92 	0x257f, 0x251e, 0x2521,  /* 201 up down */
93 	0x2503, 0x2520, 0x2523,  /* 202 */
94 	0x251a, 0x2538, 0x253a,  /* 210 up left */
95 	0x2526, 0x2540, 0x2544,  /* 211 all */
96 	0x2528, 0x2542, 0x254a,  /* 212 */
97 	0x251b, 0x2539, 0x253b,  /* 220 up left */
98 	0x2529, 0x2543, 0x2547,  /* 221 all */
99 	0x252b, 0x2549, 0x254b,  /* 222 */
100 };
101 
102 /* ASCII approximations for these code points, compatible with groff. */
103 static  const int borders_ascii[81] = {
104 	' ', '-', '=',  /* 000 right */
105 	'|', '+', '+',  /* 001 down */
106 	'|', '+', '+',  /* 002 */
107 	'-', '-', '=',  /* 010 left */
108 	'+', '+', '+',  /* 011 left down */
109 	'+', '+', '+',  /* 012 */
110 	'=', '=', '=',  /* 020 left */
111 	'+', '+', '+',  /* 021 left down */
112 	'+', '+', '+',  /* 022 */
113 	'|', '+', '+',  /* 100 up */
114 	'|', '+', '+',  /* 101 up down */
115 	'|', '+', '+',  /* 102 */
116 	'+', '+', '+',  /* 110 up left */
117 	'+', '+', '+',  /* 111 all */
118 	'+', '+', '+',  /* 112 */
119 	'+', '+', '+',  /* 120 up left */
120 	'+', '+', '+',  /* 121 all */
121 	'+', '+', '+',  /* 122 */
122 	'|', '+', '+',  /* 200 up */
123 	'|', '+', '+',  /* 201 up down */
124 	'|', '+', '+',  /* 202 */
125 	'+', '+', '+',  /* 210 up left */
126 	'+', '+', '+',  /* 211 all */
127 	'+', '+', '+',  /* 212 */
128 	'+', '+', '+',  /* 220 up left */
129 	'+', '+', '+',  /* 221 all */
130 	'+', '+', '+',  /* 222 */
131 };
132 
133 /* Either of the above according to the selected output encoding. */
134 static	const int *borders_locale;
135 
136 
137 static size_t
138 term_tbl_sulen(const struct roffsu *su, void *arg)
139 {
140 	int	 i;
141 
142 	i = term_hen((const struct termp *)arg, su);
143 	return i > 0 ? i : 0;
144 }
145 
146 static size_t
147 term_tbl_strlen(const char *p, void *arg)
148 {
149 	return term_strlen((const struct termp *)arg, p);
150 }
151 
152 static size_t
153 term_tbl_len(size_t sz, void *arg)
154 {
155 	return term_len((const struct termp *)arg, sz);
156 }
157 
158 
159 void
160 term_tbl(struct termp *tp, const struct tbl_span *sp)
161 {
162 	const struct tbl_cell	*cp, *cpn, *cpp, *cps;
163 	const struct tbl_dat	*dp;
164 	static size_t		 offset;
165 	size_t			 save_offset;
166 	size_t			 coloff, tsz;
167 	int			 hspans, ic, more;
168 	int			 dvert, fc, horiz, lhori, rhori, uvert;
169 
170 	/* Inhibit printing of spaces: we do padding ourselves. */
171 
172 	tp->flags |= TERMP_NOSPACE | TERMP_NONOSPACE;
173 	save_offset = tp->tcol->offset;
174 
175 	/*
176 	 * The first time we're invoked for a given table block,
177 	 * calculate the table widths and decimal positions.
178 	 */
179 
180 	if (tp->tbl.cols == NULL) {
181 		borders_locale = tp->enc == TERMENC_UTF8 ?
182 		    borders_utf8 : borders_ascii;
183 
184 		tp->tbl.len = term_tbl_len;
185 		tp->tbl.slen = term_tbl_strlen;
186 		tp->tbl.sulen = term_tbl_sulen;
187 		tp->tbl.arg = tp;
188 
189 		tblcalc(&tp->tbl, sp, tp->tcol->offset, tp->tcol->rmargin);
190 
191 		/* Tables leak .ta settings to subsequent text. */
192 
193 		term_tab_set(tp, NULL);
194 		coloff = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
195 		    sp->opts->lvert;
196 		for (ic = 0; ic < sp->opts->cols; ic++) {
197 			coloff += tp->tbl.cols[ic].width;
198 			term_tab_iset(coloff);
199 			coloff += tp->tbl.cols[ic].spacing;
200 		}
201 
202 		/* Center the table as a whole. */
203 
204 		offset = tp->tcol->offset;
205 		if (sp->opts->opts & TBL_OPT_CENTRE) {
206 			tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)
207 			    ? 2 : !!sp->opts->lvert + !!sp->opts->rvert;
208 			for (ic = 0; ic + 1 < sp->opts->cols; ic++)
209 				tsz += tp->tbl.cols[ic].width +
210 				    tp->tbl.cols[ic].spacing;
211 			if (sp->opts->cols)
212 				tsz += tp->tbl.cols[sp->opts->cols - 1].width;
213 			if (offset + tsz > tp->tcol->rmargin)
214 				tsz -= 1;
215 			offset = offset + tp->tcol->rmargin > tsz ?
216 			    (offset + tp->tcol->rmargin - tsz) / 2 : 0;
217 			tp->tcol->offset = offset;
218 		}
219 
220 		/* Horizontal frame at the start of boxed tables. */
221 
222 		if (tp->enc == TERMENC_ASCII &&
223 		    sp->opts->opts & TBL_OPT_DBOX)
224 			tbl_hrule(tp, NULL, sp, sp, TBL_OPT_DBOX);
225 		if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX))
226 			tbl_hrule(tp, NULL, sp, sp, TBL_OPT_BOX);
227 	}
228 
229 	/* Set up the columns. */
230 
231 	tp->flags |= TERMP_MULTICOL;
232 	tp->tcol->offset = offset;
233 	horiz = 0;
234 	switch (sp->pos) {
235 	case TBL_SPAN_HORIZ:
236 	case TBL_SPAN_DHORIZ:
237 		horiz = 1;
238 		term_setcol(tp, 1);
239 		break;
240 	case TBL_SPAN_DATA:
241 		term_setcol(tp, sp->opts->cols + 2);
242 		coloff = tp->tcol->offset;
243 
244 		/* Set up a column for a left vertical frame. */
245 
246 		if (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
247 		    sp->opts->lvert)
248 			coloff++;
249 		tp->tcol->rmargin = coloff;
250 
251 		/* Set up the data columns. */
252 
253 		dp = sp->first;
254 		hspans = 0;
255 		for (ic = 0; ic < sp->opts->cols; ic++) {
256 			if (hspans == 0) {
257 				tp->tcol++;
258 				tp->tcol->offset = coloff;
259 			}
260 			coloff += tp->tbl.cols[ic].width;
261 			tp->tcol->rmargin = coloff;
262 			if (ic + 1 < sp->opts->cols)
263 				coloff += tp->tbl.cols[ic].spacing;
264 			if (hspans) {
265 				hspans--;
266 				continue;
267 			}
268 			if (dp != NULL &&
269 			    (ic || sp->layout->first->pos != TBL_CELL_SPAN)) {
270 				hspans = dp->hspans;
271 				dp = dp->next;
272 			}
273 		}
274 
275 		/* Set up a column for a right vertical frame. */
276 
277 		tp->tcol++;
278 		tp->tcol->offset = coloff + 1;
279 		tp->tcol->rmargin = tp->maxrmargin;
280 
281 		/* Spans may have reduced the number of columns. */
282 
283 		tp->lasttcol = tp->tcol - tp->tcols;
284 
285 		/* Fill the buffers for all data columns. */
286 
287 		tp->tcol = tp->tcols;
288 		cp = cpn = sp->layout->first;
289 		dp = sp->first;
290 		hspans = 0;
291 		for (ic = 0; ic < sp->opts->cols; ic++) {
292 			if (cpn != NULL) {
293 				cp = cpn;
294 				cpn = cpn->next;
295 			}
296 			if (hspans) {
297 				hspans--;
298 				continue;
299 			}
300 			tp->tcol++;
301 			tp->col = 0;
302 			tbl_data(tp, sp->opts, cp, dp, tp->tbl.cols + ic);
303 			if (dp != NULL &&
304 			    (ic || sp->layout->first->pos != TBL_CELL_SPAN)) {
305 				hspans = dp->hspans;
306 				dp = dp->next;
307 			}
308 		}
309 		break;
310 	}
311 
312 	do {
313 		/* Print the vertical frame at the start of each row. */
314 
315 		tp->tcol = tp->tcols;
316 		uvert = dvert = sp->opts->opts & TBL_OPT_DBOX ? 2 :
317 		    sp->opts->opts & TBL_OPT_BOX ? 1 : 0;
318 		if (sp->pos == TBL_SPAN_DATA && uvert < sp->layout->vert)
319 			uvert = dvert = sp->layout->vert;
320 		if (sp->next != NULL && sp->next->pos == TBL_SPAN_DATA &&
321 		    dvert < sp->next->layout->vert)
322 			dvert = sp->next->layout->vert;
323 		if (sp->prev != NULL && uvert < sp->prev->layout->vert &&
324 		    (horiz || (IS_HORIZ(sp->layout->first) &&
325 		      !IS_HORIZ(sp->prev->layout->first))))
326 			uvert = sp->prev->layout->vert;
327 		rhori = sp->pos == TBL_SPAN_DHORIZ ||
328 		    (sp->first != NULL && sp->first->pos == TBL_DATA_DHORIZ) ||
329 		    sp->layout->first->pos == TBL_CELL_DHORIZ ? 2 :
330 		    sp->pos == TBL_SPAN_HORIZ ||
331 		    (sp->first != NULL && sp->first->pos == TBL_DATA_HORIZ) ||
332 		    sp->layout->first->pos == TBL_CELL_HORIZ ? 1 : 0;
333 		fc = BUP * uvert + BDOWN * dvert + BRIGHT * rhori;
334 		if (uvert > 0 || dvert > 0 || (horiz && sp->opts->lvert)) {
335 			(*tp->advance)(tp, tp->tcols->offset);
336 			tp->viscol = tp->tcol->offset;
337 			tbl_direct_border(tp, fc, 1);
338 		}
339 
340 		/* Print the data cells. */
341 
342 		more = 0;
343 		if (horiz)
344 			tbl_hrule(tp, sp->prev, sp, sp->next, 0);
345 		else {
346 			cp = sp->layout->first;
347 			cpn = sp->next == NULL ? NULL :
348 			    sp->next->layout->first;
349 			cpp = sp->prev == NULL ? NULL :
350 			    sp->prev->layout->first;
351 			dp = sp->first;
352 			hspans = 0;
353 			for (ic = 0; ic < sp->opts->cols; ic++) {
354 
355 				/*
356 				 * Figure out whether to print a
357 				 * vertical line after this cell
358 				 * and advance to next layout cell.
359 				 */
360 
361 				uvert = dvert = fc = 0;
362 				if (cp != NULL) {
363 					cps = cp;
364 					while (cps->next != NULL &&
365 					    cps->next->pos == TBL_CELL_SPAN)
366 						cps = cps->next;
367 					if (sp->pos == TBL_SPAN_DATA)
368 						uvert = dvert = cps->vert;
369 					switch (cp->pos) {
370 					case TBL_CELL_HORIZ:
371 						fc = BHORIZ;
372 						break;
373 					case TBL_CELL_DHORIZ:
374 						fc = BHORIZ * 2;
375 						break;
376 					default:
377 						break;
378 					}
379 				}
380 				if (cpp != NULL) {
381 					if (uvert < cpp->vert &&
382 					    cp != NULL &&
383 					    ((IS_HORIZ(cp) &&
384 					      !IS_HORIZ(cpp)) ||
385 					     (cp->next != NULL &&
386 					      cpp->next != NULL &&
387 					      IS_HORIZ(cp->next) &&
388 					      !IS_HORIZ(cpp->next))))
389 						uvert = cpp->vert;
390 					cpp = cpp->next;
391 				}
392 				if (sp->opts->opts & TBL_OPT_ALLBOX) {
393 					if (uvert == 0)
394 						uvert = 1;
395 					if (dvert == 0)
396 						dvert = 1;
397 				}
398 				if (cpn != NULL) {
399 					if (dvert == 0 ||
400 					    (dvert < cpn->vert &&
401 					     tp->enc == TERMENC_UTF8))
402 						dvert = cpn->vert;
403 					cpn = cpn->next;
404 				}
405 
406 				lhori = (cp != NULL &&
407 				     cp->pos == TBL_CELL_DHORIZ) ||
408 				    (dp != NULL &&
409 				     dp->pos == TBL_DATA_DHORIZ) ? 2 :
410 				    (cp != NULL &&
411 				     cp->pos == TBL_CELL_HORIZ) ||
412 				    (dp != NULL &&
413 				     dp->pos == TBL_DATA_HORIZ) ? 1 : 0;
414 
415 				/*
416 				 * Skip later cells in a span,
417 				 * figure out whether to start a span,
418 				 * and advance to next data cell.
419 				 */
420 
421 				if (hspans) {
422 					hspans--;
423 					cp = cp->next;
424 					continue;
425 				}
426 				if (dp != NULL && (ic ||
427 				    sp->layout->first->pos != TBL_CELL_SPAN)) {
428 					hspans = dp->hspans;
429 					dp = dp->next;
430 				}
431 
432 				/*
433 				 * Print one line of text in the cell
434 				 * and remember whether there is more.
435 				 */
436 
437 				tp->tcol++;
438 				if (tp->tcol->col < tp->tcol->lastcol)
439 					term_flushln(tp);
440 				if (tp->tcol->col < tp->tcol->lastcol)
441 					more = 1;
442 
443 				/*
444 				 * Vertical frames between data cells,
445 				 * but not after the last column.
446 				 */
447 
448 				if (fc == 0 &&
449 				    ((uvert == 0 && dvert == 0 &&
450 				      cp != NULL && (cp->next == NULL ||
451 				      !IS_HORIZ(cp->next))) ||
452 				     tp->tcol + 1 ==
453 				      tp->tcols + tp->lasttcol)) {
454 					if (cp != NULL)
455 						cp = cp->next;
456 					continue;
457 				}
458 
459 				if (tp->viscol < tp->tcol->rmargin) {
460 					(*tp->advance)(tp, tp->tcol->rmargin
461 					   - tp->viscol);
462 					tp->viscol = tp->tcol->rmargin;
463 				}
464 				while (tp->viscol < tp->tcol->rmargin +
465 				    tp->tbl.cols[ic].spacing / 2)
466 					tbl_direct_border(tp,
467 					    BHORIZ * lhori, 1);
468 
469 				if (tp->tcol + 1 == tp->tcols + tp->lasttcol)
470 					continue;
471 
472 				if (cp != NULL)
473 					cp = cp->next;
474 
475 				rhori = (cp != NULL &&
476 				     cp->pos == TBL_CELL_DHORIZ) ||
477 				    (dp != NULL &&
478 				     dp->pos == TBL_DATA_DHORIZ) ? 2 :
479 				    (cp != NULL &&
480 				     cp->pos == TBL_CELL_HORIZ) ||
481 				    (dp != NULL &&
482 				     dp->pos == TBL_DATA_HORIZ) ? 1 : 0;
483 
484 				if (tp->tbl.cols[ic].spacing)
485 					tbl_direct_border(tp,
486 					    BLEFT * lhori + BRIGHT * rhori +
487 					    BUP * uvert + BDOWN * dvert, 1);
488 
489 				if (tp->enc == TERMENC_UTF8)
490 					uvert = dvert = 0;
491 
492 				if (tp->tbl.cols[ic].spacing > 2 &&
493 				    (uvert > 1 || dvert > 1 || rhori))
494 					tbl_direct_border(tp,
495 					    BHORIZ * rhori +
496 					    BUP * (uvert > 1) +
497 					    BDOWN * (dvert > 1), 1);
498 			}
499 		}
500 
501 		/* Print the vertical frame at the end of each row. */
502 
503 		uvert = dvert = sp->opts->opts & TBL_OPT_DBOX ? 2 :
504 		    sp->opts->opts & TBL_OPT_BOX ? 1 : 0;
505 		if (sp->pos == TBL_SPAN_DATA &&
506 		    uvert < sp->layout->last->vert &&
507 		    sp->layout->last->col + 1 == sp->opts->cols)
508 			uvert = dvert = sp->layout->last->vert;
509 		if (sp->next != NULL &&
510 		    dvert < sp->next->layout->last->vert &&
511 		    sp->next->layout->last->col + 1 == sp->opts->cols)
512 			dvert = sp->next->layout->last->vert;
513 		if (sp->prev != NULL &&
514 		    uvert < sp->prev->layout->last->vert &&
515 		    sp->prev->layout->last->col + 1 == sp->opts->cols &&
516 		    (horiz || (IS_HORIZ(sp->layout->last) &&
517 		     !IS_HORIZ(sp->prev->layout->last))))
518 			uvert = sp->prev->layout->last->vert;
519 		lhori = sp->pos == TBL_SPAN_DHORIZ ||
520 		    (sp->last != NULL &&
521 		     sp->last->pos == TBL_DATA_DHORIZ &&
522 		     sp->last->layout->col + 1 == sp->opts->cols) ||
523 		    (sp->layout->last->pos == TBL_CELL_DHORIZ &&
524 		     sp->layout->last->col + 1 == sp->opts->cols) ? 2 :
525 		    sp->pos == TBL_SPAN_HORIZ ||
526 		    (sp->last != NULL &&
527 		     sp->last->pos == TBL_DATA_HORIZ &&
528 		     sp->last->layout->col + 1 == sp->opts->cols) ||
529 		    (sp->layout->last->pos == TBL_CELL_HORIZ &&
530 		     sp->layout->last->col + 1 == sp->opts->cols) ? 1 : 0;
531 		fc = BUP * uvert + BDOWN * dvert + BLEFT * lhori;
532 		if (uvert > 0 || dvert > 0 || (horiz && sp->opts->rvert)) {
533 			if (horiz == 0 && (IS_HORIZ(sp->layout->last) == 0 ||
534 			    sp->layout->last->col + 1 < sp->opts->cols)) {
535 				tp->tcol++;
536 				do {
537 					tbl_direct_border(tp,
538 					    BHORIZ * lhori, 1);
539 				} while (tp->viscol < tp->tcol->offset);
540 			}
541 			tbl_direct_border(tp, fc, 1);
542 		}
543 		(*tp->endline)(tp);
544 		tp->viscol = 0;
545 	} while (more);
546 
547 	/*
548 	 * Clean up after this row.  If it is the last line
549 	 * of the table, print the box line and clean up
550 	 * column data; otherwise, print the allbox line.
551 	 */
552 
553 	term_setcol(tp, 1);
554 	tp->flags &= ~TERMP_MULTICOL;
555 	tp->tcol->rmargin = tp->maxrmargin;
556 	if (sp->next == NULL) {
557 		if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
558 			tbl_hrule(tp, sp, sp, NULL, TBL_OPT_BOX);
559 			tp->skipvsp = 1;
560 		}
561 		if (tp->enc == TERMENC_ASCII &&
562 		    sp->opts->opts & TBL_OPT_DBOX) {
563 			tbl_hrule(tp, sp, sp, NULL, TBL_OPT_DBOX);
564 			tp->skipvsp = 2;
565 		}
566 		assert(tp->tbl.cols);
567 		free(tp->tbl.cols);
568 		tp->tbl.cols = NULL;
569 	} else if (horiz == 0 && sp->opts->opts & TBL_OPT_ALLBOX &&
570 	    (sp->next == NULL || sp->next->pos == TBL_SPAN_DATA ||
571 	     sp->next->next != NULL))
572 		tbl_hrule(tp, sp, sp, sp->next, TBL_OPT_ALLBOX);
573 
574 	tp->tcol->offset = save_offset;
575 	tp->flags &= ~TERMP_NONOSPACE;
576 }
577 
578 static void
579 tbl_hrule(struct termp *tp, const struct tbl_span *spp,
580     const struct tbl_span *sp, const struct tbl_span *spn, int flags)
581 {
582 	const struct tbl_cell	*cpp;    /* Layout cell above this line. */
583 	const struct tbl_cell	*cp;     /* Layout cell in this line. */
584 	const struct tbl_cell	*cpn;    /* Layout cell below this line. */
585 	const struct tbl_dat	*dpn;	 /* Data cell below this line. */
586 	const struct roffcol	*col;    /* Contains width and spacing. */
587 	int			 opts;   /* For the table as a whole. */
588 	int			 bw;	 /* Box line width. */
589 	int			 hw;     /* Horizontal line width. */
590 	int			 lw, rw; /* Left and right line widths. */
591 	int			 uw, dw; /* Vertical line widths. */
592 
593 	cpp = spp == NULL ? NULL : spp->layout->first;
594 	cp  = sp  == NULL ? NULL : sp->layout->first;
595 	cpn = spn == NULL ? NULL : spn->layout->first;
596 	dpn = NULL;
597 	if (spn != NULL) {
598 		if (spn->pos == TBL_SPAN_DATA)
599 			dpn = spn->first;
600 		else if (spn->next != NULL)
601 			dpn = spn->next->first;
602 	}
603 	opts = sp->opts->opts;
604 	bw = opts & TBL_OPT_DBOX ? (tp->enc == TERMENC_UTF8 ? 2 : 1) :
605 	    opts & (TBL_OPT_BOX | TBL_OPT_ALLBOX) ? 1 : 0;
606 	hw = flags == TBL_OPT_DBOX || flags == TBL_OPT_BOX ? bw :
607 	    sp->pos == TBL_SPAN_DHORIZ ? 2 : 1;
608 
609 	/* Print the left end of the line. */
610 
611 	if (tp->viscol == 0) {
612 		(*tp->advance)(tp, tp->tcols->offset);
613 		tp->viscol = tp->tcols->offset;
614 	}
615 	if (flags != 0)
616 		tbl_direct_border(tp,
617 		    (spp == NULL ? 0 : BUP * bw) +
618 		    (spn == NULL ? 0 : BDOWN * bw) +
619 		    (spp == NULL || cpn == NULL ||
620 		     cpn->pos != TBL_CELL_DOWN ? BRIGHT * hw : 0), 1);
621 
622 	col = tp->tbl.cols;
623 	for (;;) {
624 		if (cp == NULL)
625 			col++;
626 		else
627 			col = tp->tbl.cols + cp->col;
628 
629 		/* Print the horizontal line inside this column. */
630 
631 		lw = cpp == NULL || cpn == NULL ||
632 		    (cpn->pos != TBL_CELL_DOWN &&
633 		     (dpn == NULL || dpn->string == NULL ||
634 		      strcmp(dpn->string, "\\^") != 0))
635 		    ? hw : 0;
636 		tbl_direct_border(tp, BHORIZ * lw,
637 		    col->width + col->spacing / 2);
638 
639 		/*
640 		 * Figure out whether a vertical line is crossing
641 		 * at the end of this column,
642 		 * and advance to the next column.
643 		 */
644 
645 		uw = dw = 0;
646 		if (cpp != NULL) {
647 			if (flags != TBL_OPT_DBOX) {
648 				uw = cpp->vert;
649 				if (uw == 0 && opts & TBL_OPT_ALLBOX)
650 					uw = 1;
651 			}
652 			cpp = cpp->next;
653 		} else if (spp != NULL && opts & TBL_OPT_ALLBOX)
654 			uw = 1;
655 		if (cp != NULL)
656 			cp = cp->next;
657 		if (cpn != NULL) {
658 			if (flags != TBL_OPT_DBOX) {
659 				dw = cpn->vert;
660 				if (dw == 0 && opts & TBL_OPT_ALLBOX)
661 					dw = 1;
662 			}
663 			cpn = cpn->next;
664 			while (dpn != NULL && dpn->layout != cpn)
665 				dpn = dpn->next;
666 		} else if (spn != NULL && opts & TBL_OPT_ALLBOX)
667 			dw = 1;
668 		if (col + 1 == tp->tbl.cols + sp->opts->cols)
669 			break;
670 
671 		/* Vertical lines do not cross spanned cells. */
672 
673 		if (cpp != NULL && cpp->pos == TBL_CELL_SPAN)
674 			uw = 0;
675 		if (cpn != NULL && cpn->pos == TBL_CELL_SPAN)
676 			dw = 0;
677 
678 		/* The horizontal line inside the next column. */
679 
680 		rw = cpp == NULL || cpn == NULL ||
681 		    (cpn->pos != TBL_CELL_DOWN &&
682 		     (dpn == NULL || dpn->string == NULL ||
683 		      strcmp(dpn->string, "\\^") != 0))
684 		    ? hw : 0;
685 
686 		/* The line crossing at the end of this column. */
687 
688 		if (col->spacing)
689 			tbl_direct_border(tp, BLEFT * lw +
690 			    BRIGHT * rw + BUP * uw + BDOWN * dw, 1);
691 
692 		/*
693 		 * In ASCII output, a crossing may print two characters.
694 		 */
695 
696 		if (tp->enc != TERMENC_ASCII || (uw < 2 && dw < 2))
697 			uw = dw = 0;
698 		if (col->spacing > 2)
699 			tbl_direct_border(tp,
700                             BHORIZ * rw + BUP * uw + BDOWN * dw, 1);
701 
702 		/* Padding before the start of the next column. */
703 
704 		if (col->spacing > 4)
705 			tbl_direct_border(tp,
706 			    BHORIZ * rw, (col->spacing - 3) / 2);
707 	}
708 
709 	/* Print the right end of the line. */
710 
711 	if (flags != 0) {
712 		tbl_direct_border(tp,
713 		    (spp == NULL ? 0 : BUP * bw) +
714 		    (spn == NULL ? 0 : BDOWN * bw) +
715 		    (spp == NULL || spn == NULL ||
716 		     spn->layout->last->pos != TBL_CELL_DOWN ?
717 		     BLEFT * hw : 0), 1);
718 		(*tp->endline)(tp);
719 		tp->viscol = 0;
720 	}
721 }
722 
723 static void
724 tbl_data(struct termp *tp, const struct tbl_opts *opts,
725     const struct tbl_cell *cp, const struct tbl_dat *dp,
726     const struct roffcol *col)
727 {
728 	switch (cp->pos) {
729 	case TBL_CELL_HORIZ:
730 		tbl_fill_border(tp, BHORIZ, col->width);
731 		return;
732 	case TBL_CELL_DHORIZ:
733 		tbl_fill_border(tp, BHORIZ * 2, col->width);
734 		return;
735 	default:
736 		break;
737 	}
738 
739 	if (dp == NULL)
740 		return;
741 
742 	switch (dp->pos) {
743 	case TBL_DATA_NONE:
744 		return;
745 	case TBL_DATA_HORIZ:
746 	case TBL_DATA_NHORIZ:
747 		tbl_fill_border(tp, BHORIZ, col->width);
748 		return;
749 	case TBL_DATA_NDHORIZ:
750 	case TBL_DATA_DHORIZ:
751 		tbl_fill_border(tp, BHORIZ * 2, col->width);
752 		return;
753 	default:
754 		break;
755 	}
756 
757 	switch (cp->pos) {
758 	case TBL_CELL_LONG:
759 	case TBL_CELL_CENTRE:
760 	case TBL_CELL_LEFT:
761 	case TBL_CELL_RIGHT:
762 		tbl_literal(tp, dp, col);
763 		break;
764 	case TBL_CELL_NUMBER:
765 		tbl_number(tp, opts, dp, col);
766 		break;
767 	case TBL_CELL_DOWN:
768 	case TBL_CELL_SPAN:
769 		break;
770 	default:
771 		abort();
772 	}
773 }
774 
775 static void
776 tbl_fill_string(struct termp *tp, const char *cp, size_t len)
777 {
778 	size_t	 i, sz;
779 
780 	sz = term_strlen(tp, cp);
781 	for (i = 0; i < len; i += sz)
782 		term_word(tp, cp);
783 }
784 
785 static void
786 tbl_fill_char(struct termp *tp, char c, size_t len)
787 {
788 	char	 cp[2];
789 
790 	cp[0] = c;
791 	cp[1] = '\0';
792 	tbl_fill_string(tp, cp, len);
793 }
794 
795 static void
796 tbl_fill_border(struct termp *tp, int c, size_t len)
797 {
798 	char	 buf[12];
799 
800 	if ((c = borders_locale[c]) > 127) {
801 		(void)snprintf(buf, sizeof(buf), "\\[u%04x]", c);
802 		tbl_fill_string(tp, buf, len);
803 	} else
804 		tbl_fill_char(tp, c, len);
805 }
806 
807 static void
808 tbl_direct_border(struct termp *tp, int c, size_t len)
809 {
810 	size_t	 i, sz;
811 
812 	c = borders_locale[c];
813 	sz = (*tp->width)(tp, c);
814 	for (i = 0; i < len; i += sz) {
815 		(*tp->letter)(tp, c);
816 		tp->viscol += sz;
817 	}
818 }
819 
820 static void
821 tbl_literal(struct termp *tp, const struct tbl_dat *dp,
822 		const struct roffcol *col)
823 {
824 	size_t		 len, padl, padr, width;
825 	int		 ic, hspans;
826 
827 	assert(dp->string);
828 	len = term_strlen(tp, dp->string);
829 	width = col->width;
830 	ic = dp->layout->col;
831 	hspans = dp->hspans;
832 	while (hspans--)
833 		width += tp->tbl.cols[++ic].width + 3;
834 
835 	padr = width > len ? width - len : 0;
836 	padl = 0;
837 
838 	switch (dp->layout->pos) {
839 	case TBL_CELL_LONG:
840 		padl = term_len(tp, 1);
841 		padr = padr > padl ? padr - padl : 0;
842 		break;
843 	case TBL_CELL_CENTRE:
844 		if (2 > padr)
845 			break;
846 		padl = padr / 2;
847 		padr -= padl;
848 		break;
849 	case TBL_CELL_RIGHT:
850 		padl = padr;
851 		padr = 0;
852 		break;
853 	default:
854 		break;
855 	}
856 
857 	tbl_fill_char(tp, ASCII_NBRSP, padl);
858 	tbl_word(tp, dp);
859 	tbl_fill_char(tp, ASCII_NBRSP, padr);
860 }
861 
862 static void
863 tbl_number(struct termp *tp, const struct tbl_opts *opts,
864 		const struct tbl_dat *dp,
865 		const struct roffcol *col)
866 {
867 	const char	*cp, *lastdigit, *lastpoint;
868 	size_t		 intsz, padl, totsz;
869 	char		 buf[2];
870 
871 	/*
872 	 * Almost the same code as in tblcalc_number():
873 	 * First find the position of the decimal point.
874 	 */
875 
876 	assert(dp->string);
877 	lastdigit = lastpoint = NULL;
878 	for (cp = dp->string; cp[0] != '\0'; cp++) {
879 		if (cp[0] == '\\' && cp[1] == '&') {
880 			lastdigit = lastpoint = cp;
881 			break;
882 		} else if (cp[0] == opts->decimal &&
883 		    (isdigit((unsigned char)cp[1]) ||
884 		     (cp > dp->string && isdigit((unsigned char)cp[-1]))))
885 			lastpoint = cp;
886 		else if (isdigit((unsigned char)cp[0]))
887 			lastdigit = cp;
888 	}
889 
890 	/* Then measure both widths. */
891 
892 	padl = 0;
893 	totsz = term_strlen(tp, dp->string);
894 	if (lastdigit != NULL) {
895 		if (lastpoint == NULL)
896 			lastpoint = lastdigit + 1;
897 		intsz = 0;
898 		buf[1] = '\0';
899 		for (cp = dp->string; cp < lastpoint; cp++) {
900 			buf[0] = cp[0];
901 			intsz += term_strlen(tp, buf);
902 		}
903 
904 		/*
905 		 * Pad left to match the decimal position,
906 		 * but avoid exceeding the total column width.
907 		 */
908 
909 		if (col->decimal > intsz && col->width > totsz) {
910 			padl = col->decimal - intsz;
911 			if (padl + totsz > col->width)
912 				padl = col->width - totsz;
913 		}
914 
915 	/* If it is not a number, simply center the string. */
916 
917 	} else if (col->width > totsz)
918 		padl = (col->width - totsz) / 2;
919 
920 	tbl_fill_char(tp, ASCII_NBRSP, padl);
921 	tbl_word(tp, dp);
922 
923 	/* Pad right to fill the column.  */
924 
925 	if (col->width > padl + totsz)
926 		tbl_fill_char(tp, ASCII_NBRSP, col->width - padl - totsz);
927 }
928 
929 static void
930 tbl_word(struct termp *tp, const struct tbl_dat *dp)
931 {
932 	int		 prev_font;
933 
934 	prev_font = tp->fonti;
935 	if (dp->layout->flags & TBL_CELL_BOLD)
936 		term_fontpush(tp, TERMFONT_BOLD);
937 	else if (dp->layout->flags & TBL_CELL_ITALIC)
938 		term_fontpush(tp, TERMFONT_UNDER);
939 
940 	term_word(tp, dp->string);
941 
942 	term_fontpopq(tp, prev_font);
943 }
944