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