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