1*4d131170SRobert Mustacchi /* $Id: tbl_term.c,v 1.75 2021/08/10 12:55:04 schwarze Exp $ */
295c635efSGarrett D'Amore /*
395c635efSGarrett D'Amore * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4*4d131170SRobert Mustacchi * Copyright (c) 2011-2021 Ingo Schwarze <schwarze@openbsd.org>
595c635efSGarrett D'Amore *
695c635efSGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any
795c635efSGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above
895c635efSGarrett D'Amore * copyright notice and this permission notice appear in all copies.
995c635efSGarrett D'Amore *
1095c635efSGarrett D'Amore * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1195c635efSGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1295c635efSGarrett D'Amore * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1395c635efSGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1495c635efSGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1595c635efSGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1695c635efSGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1795c635efSGarrett D'Amore */
1895c635efSGarrett D'Amore #include "config.h"
19260e9a87SYuri Pankov
20260e9a87SYuri Pankov #include <sys/types.h>
2195c635efSGarrett D'Amore
2295c635efSGarrett D'Amore #include <assert.h>
23cec8643bSMichal Nowak #include <ctype.h>
2495c635efSGarrett D'Amore #include <stdio.h>
2595c635efSGarrett D'Amore #include <stdlib.h>
2695c635efSGarrett D'Amore #include <string.h>
2795c635efSGarrett D'Amore
2895c635efSGarrett D'Amore #include "mandoc.h"
29cec8643bSMichal Nowak #include "tbl.h"
3095c635efSGarrett D'Amore #include "out.h"
3195c635efSGarrett D'Amore #include "term.h"
3295c635efSGarrett D'Amore
33c66b8046SYuri Pankov #define IS_HORIZ(cp) ((cp)->pos == TBL_CELL_HORIZ || \
34c66b8046SYuri Pankov (cp)->pos == TBL_CELL_DHORIZ)
35c66b8046SYuri Pankov
36cec8643bSMichal Nowak
3795c635efSGarrett D'Amore static size_t term_tbl_len(size_t, void *);
3895c635efSGarrett D'Amore static size_t term_tbl_strlen(const char *, void *);
39c66b8046SYuri Pankov static size_t term_tbl_sulen(const struct roffsu *, void *);
40698f87a4SGarrett D'Amore static void tbl_data(struct termp *, const struct tbl_opts *,
41c66b8046SYuri Pankov const struct tbl_cell *,
4295c635efSGarrett D'Amore const struct tbl_dat *,
4395c635efSGarrett D'Amore const struct roffcol *);
44cec8643bSMichal Nowak static void tbl_direct_border(struct termp *, int, size_t);
45cec8643bSMichal Nowak static void tbl_fill_border(struct termp *, int, size_t);
46cec8643bSMichal Nowak static void tbl_fill_char(struct termp *, char, size_t);
47cec8643bSMichal Nowak static void tbl_fill_string(struct termp *, const char *, size_t);
48cec8643bSMichal Nowak static void tbl_hrule(struct termp *, const struct tbl_span *,
49*4d131170SRobert Mustacchi const struct tbl_span *, const struct tbl_span *,
50*4d131170SRobert Mustacchi int);
5195c635efSGarrett D'Amore static void tbl_literal(struct termp *, const struct tbl_dat *,
5295c635efSGarrett D'Amore const struct roffcol *);
53698f87a4SGarrett D'Amore static void tbl_number(struct termp *, const struct tbl_opts *,
5495c635efSGarrett D'Amore const struct tbl_dat *,
5595c635efSGarrett D'Amore const struct roffcol *);
56260e9a87SYuri Pankov static void tbl_word(struct termp *, const struct tbl_dat *);
5795c635efSGarrett D'Amore
5895c635efSGarrett D'Amore
59cec8643bSMichal Nowak /*
60cec8643bSMichal Nowak * The following border-character tables are indexed
61cec8643bSMichal Nowak * by ternary (3-based) numbers, as opposed to binary or decimal.
62cec8643bSMichal Nowak * Each ternary digit describes the line width in one direction:
63cec8643bSMichal Nowak * 0 means no line, 1 single or light line, 2 double or heavy line.
64cec8643bSMichal Nowak */
65cec8643bSMichal Nowak
66cec8643bSMichal Nowak /* Positional values of the four directions. */
67cec8643bSMichal Nowak #define BRIGHT 1
68cec8643bSMichal Nowak #define BDOWN 3
69cec8643bSMichal Nowak #define BLEFT (3 * 3)
70cec8643bSMichal Nowak #define BUP (3 * 3 * 3)
71cec8643bSMichal Nowak #define BHORIZ (BLEFT + BRIGHT)
72cec8643bSMichal Nowak
73cec8643bSMichal Nowak /* Code points to use for each combination of widths. */
74cec8643bSMichal Nowak static const int borders_utf8[81] = {
75cec8643bSMichal Nowak 0x0020, 0x2576, 0x257a, /* 000 right */
76cec8643bSMichal Nowak 0x2577, 0x250c, 0x250d, /* 001 down */
77cec8643bSMichal Nowak 0x257b, 0x250e, 0x250f, /* 002 */
78cec8643bSMichal Nowak 0x2574, 0x2500, 0x257c, /* 010 left */
79cec8643bSMichal Nowak 0x2510, 0x252c, 0x252e, /* 011 left down */
80cec8643bSMichal Nowak 0x2512, 0x2530, 0x2532, /* 012 */
81cec8643bSMichal Nowak 0x2578, 0x257e, 0x2501, /* 020 left */
82cec8643bSMichal Nowak 0x2511, 0x252d, 0x252f, /* 021 left down */
83cec8643bSMichal Nowak 0x2513, 0x2531, 0x2533, /* 022 */
84cec8643bSMichal Nowak 0x2575, 0x2514, 0x2515, /* 100 up */
85cec8643bSMichal Nowak 0x2502, 0x251c, 0x251d, /* 101 up down */
86cec8643bSMichal Nowak 0x257d, 0x251f, 0x2522, /* 102 */
87cec8643bSMichal Nowak 0x2518, 0x2534, 0x2536, /* 110 up left */
88cec8643bSMichal Nowak 0x2524, 0x253c, 0x253e, /* 111 all */
89cec8643bSMichal Nowak 0x2527, 0x2541, 0x2546, /* 112 */
90cec8643bSMichal Nowak 0x2519, 0x2535, 0x2537, /* 120 up left */
91cec8643bSMichal Nowak 0x2525, 0x253d, 0x253f, /* 121 all */
92cec8643bSMichal Nowak 0x252a, 0x2545, 0x2548, /* 122 */
93cec8643bSMichal Nowak 0x2579, 0x2516, 0x2517, /* 200 up */
94cec8643bSMichal Nowak 0x257f, 0x251e, 0x2521, /* 201 up down */
95cec8643bSMichal Nowak 0x2503, 0x2520, 0x2523, /* 202 */
96cec8643bSMichal Nowak 0x251a, 0x2538, 0x253a, /* 210 up left */
97cec8643bSMichal Nowak 0x2526, 0x2540, 0x2544, /* 211 all */
98cec8643bSMichal Nowak 0x2528, 0x2542, 0x254a, /* 212 */
99cec8643bSMichal Nowak 0x251b, 0x2539, 0x253b, /* 220 up left */
100cec8643bSMichal Nowak 0x2529, 0x2543, 0x2547, /* 221 all */
101cec8643bSMichal Nowak 0x252b, 0x2549, 0x254b, /* 222 */
102cec8643bSMichal Nowak };
103cec8643bSMichal Nowak
104cec8643bSMichal Nowak /* ASCII approximations for these code points, compatible with groff. */
105cec8643bSMichal Nowak static const int borders_ascii[81] = {
106cec8643bSMichal Nowak ' ', '-', '=', /* 000 right */
107cec8643bSMichal Nowak '|', '+', '+', /* 001 down */
108cec8643bSMichal Nowak '|', '+', '+', /* 002 */
109cec8643bSMichal Nowak '-', '-', '=', /* 010 left */
110cec8643bSMichal Nowak '+', '+', '+', /* 011 left down */
111cec8643bSMichal Nowak '+', '+', '+', /* 012 */
112cec8643bSMichal Nowak '=', '=', '=', /* 020 left */
113cec8643bSMichal Nowak '+', '+', '+', /* 021 left down */
114cec8643bSMichal Nowak '+', '+', '+', /* 022 */
115cec8643bSMichal Nowak '|', '+', '+', /* 100 up */
116cec8643bSMichal Nowak '|', '+', '+', /* 101 up down */
117cec8643bSMichal Nowak '|', '+', '+', /* 102 */
118cec8643bSMichal Nowak '+', '+', '+', /* 110 up left */
119cec8643bSMichal Nowak '+', '+', '+', /* 111 all */
120cec8643bSMichal Nowak '+', '+', '+', /* 112 */
121cec8643bSMichal Nowak '+', '+', '+', /* 120 up left */
122cec8643bSMichal Nowak '+', '+', '+', /* 121 all */
123cec8643bSMichal Nowak '+', '+', '+', /* 122 */
124cec8643bSMichal Nowak '|', '+', '+', /* 200 up */
125cec8643bSMichal Nowak '|', '+', '+', /* 201 up down */
126cec8643bSMichal Nowak '|', '+', '+', /* 202 */
127cec8643bSMichal Nowak '+', '+', '+', /* 210 up left */
128cec8643bSMichal Nowak '+', '+', '+', /* 211 all */
129cec8643bSMichal Nowak '+', '+', '+', /* 212 */
130cec8643bSMichal Nowak '+', '+', '+', /* 220 up left */
131cec8643bSMichal Nowak '+', '+', '+', /* 221 all */
132cec8643bSMichal Nowak '+', '+', '+', /* 222 */
133cec8643bSMichal Nowak };
134cec8643bSMichal Nowak
135cec8643bSMichal Nowak /* Either of the above according to the selected output encoding. */
136cec8643bSMichal Nowak static const int *borders_locale;
137cec8643bSMichal Nowak
138cec8643bSMichal Nowak
13995c635efSGarrett D'Amore static size_t
term_tbl_sulen(const struct roffsu * su,void * arg)140c66b8046SYuri Pankov term_tbl_sulen(const struct roffsu *su, void *arg)
141c66b8046SYuri Pankov {
142c66b8046SYuri Pankov int i;
143c66b8046SYuri Pankov
144c66b8046SYuri Pankov i = term_hen((const struct termp *)arg, su);
145c66b8046SYuri Pankov return i > 0 ? i : 0;
146c66b8046SYuri Pankov }
147c66b8046SYuri Pankov
148c66b8046SYuri Pankov static size_t
term_tbl_strlen(const char * p,void * arg)14995c635efSGarrett D'Amore term_tbl_strlen(const char *p, void *arg)
15095c635efSGarrett D'Amore {
151371584c2SYuri Pankov return term_strlen((const struct termp *)arg, p);
15295c635efSGarrett D'Amore }
15395c635efSGarrett D'Amore
15495c635efSGarrett D'Amore static size_t
term_tbl_len(size_t sz,void * arg)15595c635efSGarrett D'Amore term_tbl_len(size_t sz, void *arg)
15695c635efSGarrett D'Amore {
157371584c2SYuri Pankov return term_len((const struct termp *)arg, sz);
15895c635efSGarrett D'Amore }
15995c635efSGarrett D'Amore
160cec8643bSMichal Nowak
16195c635efSGarrett D'Amore void
term_tbl(struct termp * tp,const struct tbl_span * sp)16295c635efSGarrett D'Amore term_tbl(struct termp *tp, const struct tbl_span *sp)
16395c635efSGarrett D'Amore {
164cec8643bSMichal Nowak const struct tbl_cell *cp, *cpn, *cpp, *cps;
16595c635efSGarrett D'Amore const struct tbl_dat *dp;
166260e9a87SYuri Pankov static size_t offset;
167cec8643bSMichal Nowak size_t save_offset;
168c66b8046SYuri Pankov size_t coloff, tsz;
169cec8643bSMichal Nowak int hspans, ic, more;
170cec8643bSMichal Nowak int dvert, fc, horiz, lhori, rhori, uvert;
17195c635efSGarrett D'Amore
17295c635efSGarrett D'Amore /* Inhibit printing of spaces: we do padding ourselves. */
17395c635efSGarrett D'Amore
174c66b8046SYuri Pankov tp->flags |= TERMP_NOSPACE | TERMP_NONOSPACE;
175cec8643bSMichal Nowak save_offset = tp->tcol->offset;
17695c635efSGarrett D'Amore
17795c635efSGarrett D'Amore /*
17895c635efSGarrett D'Amore * The first time we're invoked for a given table block,
17995c635efSGarrett D'Amore * calculate the table widths and decimal positions.
18095c635efSGarrett D'Amore */
18195c635efSGarrett D'Amore
182260e9a87SYuri Pankov if (tp->tbl.cols == NULL) {
183cec8643bSMichal Nowak borders_locale = tp->enc == TERMENC_UTF8 ?
184cec8643bSMichal Nowak borders_utf8 : borders_ascii;
185cec8643bSMichal Nowak
18695c635efSGarrett D'Amore tp->tbl.len = term_tbl_len;
18795c635efSGarrett D'Amore tp->tbl.slen = term_tbl_strlen;
188c66b8046SYuri Pankov tp->tbl.sulen = term_tbl_sulen;
18995c635efSGarrett D'Amore tp->tbl.arg = tp;
19095c635efSGarrett D'Amore
191c66b8046SYuri Pankov tblcalc(&tp->tbl, sp, tp->tcol->offset, tp->tcol->rmargin);
192c66b8046SYuri Pankov
193260e9a87SYuri Pankov /* Center the table as a whole. */
194260e9a87SYuri Pankov
195c66b8046SYuri Pankov offset = tp->tcol->offset;
196260e9a87SYuri Pankov if (sp->opts->opts & TBL_OPT_CENTRE) {
197260e9a87SYuri Pankov tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)
198260e9a87SYuri Pankov ? 2 : !!sp->opts->lvert + !!sp->opts->rvert;
199c66b8046SYuri Pankov for (ic = 0; ic + 1 < sp->opts->cols; ic++)
200c66b8046SYuri Pankov tsz += tp->tbl.cols[ic].width +
201c66b8046SYuri Pankov tp->tbl.cols[ic].spacing;
202c66b8046SYuri Pankov if (sp->opts->cols)
203c66b8046SYuri Pankov tsz += tp->tbl.cols[sp->opts->cols - 1].width;
204c66b8046SYuri Pankov if (offset + tsz > tp->tcol->rmargin)
205260e9a87SYuri Pankov tsz -= 1;
206cec8643bSMichal Nowak offset = offset + tp->tcol->rmargin > tsz ?
207c66b8046SYuri Pankov (offset + tp->tcol->rmargin - tsz) / 2 : 0;
208cec8643bSMichal Nowak tp->tcol->offset = offset;
20995c635efSGarrett D'Amore }
21095c635efSGarrett D'Amore
21195c635efSGarrett D'Amore /* Horizontal frame at the start of boxed tables. */
21295c635efSGarrett D'Amore
213cec8643bSMichal Nowak if (tp->enc == TERMENC_ASCII &&
214cec8643bSMichal Nowak sp->opts->opts & TBL_OPT_DBOX)
215*4d131170SRobert Mustacchi tbl_hrule(tp, NULL, sp, sp, TBL_OPT_DBOX);
216260e9a87SYuri Pankov if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX))
217*4d131170SRobert Mustacchi tbl_hrule(tp, NULL, sp, sp, TBL_OPT_BOX);
21895c635efSGarrett D'Amore }
21995c635efSGarrett D'Amore
220c66b8046SYuri Pankov /* Set up the columns. */
22195c635efSGarrett D'Amore
222c66b8046SYuri Pankov tp->flags |= TERMP_MULTICOL;
223cec8643bSMichal Nowak tp->tcol->offset = offset;
224c66b8046SYuri Pankov horiz = 0;
225c66b8046SYuri Pankov switch (sp->pos) {
226c66b8046SYuri Pankov case TBL_SPAN_HORIZ:
227c66b8046SYuri Pankov case TBL_SPAN_DHORIZ:
228c66b8046SYuri Pankov horiz = 1;
229c66b8046SYuri Pankov term_setcol(tp, 1);
230c66b8046SYuri Pankov break;
231c66b8046SYuri Pankov case TBL_SPAN_DATA:
232c66b8046SYuri Pankov term_setcol(tp, sp->opts->cols + 2);
233c66b8046SYuri Pankov coloff = tp->tcol->offset;
234260e9a87SYuri Pankov
235c66b8046SYuri Pankov /* Set up a column for a left vertical frame. */
236c66b8046SYuri Pankov
237c66b8046SYuri Pankov if (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
238c66b8046SYuri Pankov sp->opts->lvert)
239c66b8046SYuri Pankov coloff++;
240c66b8046SYuri Pankov tp->tcol->rmargin = coloff;
241c66b8046SYuri Pankov
242c66b8046SYuri Pankov /* Set up the data columns. */
243c66b8046SYuri Pankov
244c66b8046SYuri Pankov dp = sp->first;
245cec8643bSMichal Nowak hspans = 0;
246c66b8046SYuri Pankov for (ic = 0; ic < sp->opts->cols; ic++) {
247cec8643bSMichal Nowak if (hspans == 0) {
248c66b8046SYuri Pankov tp->tcol++;
249c66b8046SYuri Pankov tp->tcol->offset = coloff;
250c66b8046SYuri Pankov }
251c66b8046SYuri Pankov coloff += tp->tbl.cols[ic].width;
252c66b8046SYuri Pankov tp->tcol->rmargin = coloff;
253c66b8046SYuri Pankov if (ic + 1 < sp->opts->cols)
254c66b8046SYuri Pankov coloff += tp->tbl.cols[ic].spacing;
255cec8643bSMichal Nowak if (hspans) {
256cec8643bSMichal Nowak hspans--;
257c66b8046SYuri Pankov continue;
258c66b8046SYuri Pankov }
259*4d131170SRobert Mustacchi if (dp != NULL &&
260*4d131170SRobert Mustacchi (ic || sp->layout->first->pos != TBL_CELL_SPAN)) {
261cec8643bSMichal Nowak hspans = dp->hspans;
262c66b8046SYuri Pankov dp = dp->next;
263c66b8046SYuri Pankov }
264*4d131170SRobert Mustacchi }
265c66b8046SYuri Pankov
266c66b8046SYuri Pankov /* Set up a column for a right vertical frame. */
267c66b8046SYuri Pankov
268c66b8046SYuri Pankov tp->tcol++;
269c66b8046SYuri Pankov tp->tcol->offset = coloff + 1;
270c66b8046SYuri Pankov tp->tcol->rmargin = tp->maxrmargin;
271c66b8046SYuri Pankov
272c66b8046SYuri Pankov /* Spans may have reduced the number of columns. */
273c66b8046SYuri Pankov
274c66b8046SYuri Pankov tp->lasttcol = tp->tcol - tp->tcols;
275c66b8046SYuri Pankov
276c66b8046SYuri Pankov /* Fill the buffers for all data columns. */
277c66b8046SYuri Pankov
278c66b8046SYuri Pankov tp->tcol = tp->tcols;
279c66b8046SYuri Pankov cp = cpn = sp->layout->first;
280c66b8046SYuri Pankov dp = sp->first;
281cec8643bSMichal Nowak hspans = 0;
282c66b8046SYuri Pankov for (ic = 0; ic < sp->opts->cols; ic++) {
283c66b8046SYuri Pankov if (cpn != NULL) {
284c66b8046SYuri Pankov cp = cpn;
285c66b8046SYuri Pankov cpn = cpn->next;
286c66b8046SYuri Pankov }
287cec8643bSMichal Nowak if (hspans) {
288cec8643bSMichal Nowak hspans--;
289c66b8046SYuri Pankov continue;
290c66b8046SYuri Pankov }
291c66b8046SYuri Pankov tp->tcol++;
292c66b8046SYuri Pankov tp->col = 0;
293c66b8046SYuri Pankov tbl_data(tp, sp->opts, cp, dp, tp->tbl.cols + ic);
294*4d131170SRobert Mustacchi if (dp != NULL &&
295*4d131170SRobert Mustacchi (ic || sp->layout->first->pos != TBL_CELL_SPAN)) {
296cec8643bSMichal Nowak hspans = dp->hspans;
297c66b8046SYuri Pankov dp = dp->next;
298c66b8046SYuri Pankov }
299*4d131170SRobert Mustacchi }
300c66b8046SYuri Pankov break;
301c66b8046SYuri Pankov }
302c66b8046SYuri Pankov
303c66b8046SYuri Pankov do {
304c66b8046SYuri Pankov /* Print the vertical frame at the start of each row. */
305c66b8046SYuri Pankov
306c66b8046SYuri Pankov tp->tcol = tp->tcols;
307cec8643bSMichal Nowak uvert = dvert = sp->opts->opts & TBL_OPT_DBOX ? 2 :
308cec8643bSMichal Nowak sp->opts->opts & TBL_OPT_BOX ? 1 : 0;
309cec8643bSMichal Nowak if (sp->pos == TBL_SPAN_DATA && uvert < sp->layout->vert)
310cec8643bSMichal Nowak uvert = dvert = sp->layout->vert;
311cec8643bSMichal Nowak if (sp->next != NULL && sp->next->pos == TBL_SPAN_DATA &&
312cec8643bSMichal Nowak dvert < sp->next->layout->vert)
313cec8643bSMichal Nowak dvert = sp->next->layout->vert;
314cec8643bSMichal Nowak if (sp->prev != NULL && uvert < sp->prev->layout->vert &&
315c66b8046SYuri Pankov (horiz || (IS_HORIZ(sp->layout->first) &&
316cec8643bSMichal Nowak !IS_HORIZ(sp->prev->layout->first))))
317cec8643bSMichal Nowak uvert = sp->prev->layout->vert;
318cec8643bSMichal Nowak rhori = sp->pos == TBL_SPAN_DHORIZ ||
319cec8643bSMichal Nowak (sp->first != NULL && sp->first->pos == TBL_DATA_DHORIZ) ||
320cec8643bSMichal Nowak sp->layout->first->pos == TBL_CELL_DHORIZ ? 2 :
321cec8643bSMichal Nowak sp->pos == TBL_SPAN_HORIZ ||
322cec8643bSMichal Nowak (sp->first != NULL && sp->first->pos == TBL_DATA_HORIZ) ||
323cec8643bSMichal Nowak sp->layout->first->pos == TBL_CELL_HORIZ ? 1 : 0;
324cec8643bSMichal Nowak fc = BUP * uvert + BDOWN * dvert + BRIGHT * rhori;
325cec8643bSMichal Nowak if (uvert > 0 || dvert > 0 || (horiz && sp->opts->lvert)) {
326c66b8046SYuri Pankov (*tp->advance)(tp, tp->tcols->offset);
327cec8643bSMichal Nowak tp->viscol = tp->tcol->offset;
328cec8643bSMichal Nowak tbl_direct_border(tp, fc, 1);
329c66b8046SYuri Pankov }
33095c635efSGarrett D'Amore
331c66b8046SYuri Pankov /* Print the data cells. */
33295c635efSGarrett D'Amore
333c66b8046SYuri Pankov more = 0;
334cec8643bSMichal Nowak if (horiz)
335*4d131170SRobert Mustacchi tbl_hrule(tp, sp->prev, sp, sp->next, 0);
336cec8643bSMichal Nowak else {
337260e9a87SYuri Pankov cp = sp->layout->first;
338c66b8046SYuri Pankov cpn = sp->next == NULL ? NULL :
339c66b8046SYuri Pankov sp->next->layout->first;
340c66b8046SYuri Pankov cpp = sp->prev == NULL ? NULL :
341c66b8046SYuri Pankov sp->prev->layout->first;
34295c635efSGarrett D'Amore dp = sp->first;
343cec8643bSMichal Nowak hspans = 0;
344260e9a87SYuri Pankov for (ic = 0; ic < sp->opts->cols; ic++) {
345698f87a4SGarrett D'Amore
34695c635efSGarrett D'Amore /*
347c66b8046SYuri Pankov * Figure out whether to print a
348c66b8046SYuri Pankov * vertical line after this cell
349c66b8046SYuri Pankov * and advance to next layout cell.
35095c635efSGarrett D'Amore */
35195c635efSGarrett D'Amore
352cec8643bSMichal Nowak uvert = dvert = fc = 0;
353c66b8046SYuri Pankov if (cp != NULL) {
354cec8643bSMichal Nowak cps = cp;
355cec8643bSMichal Nowak while (cps->next != NULL &&
356cec8643bSMichal Nowak cps->next->pos == TBL_CELL_SPAN)
357cec8643bSMichal Nowak cps = cps->next;
358cec8643bSMichal Nowak if (sp->pos == TBL_SPAN_DATA)
359cec8643bSMichal Nowak uvert = dvert = cps->vert;
360c66b8046SYuri Pankov switch (cp->pos) {
361c66b8046SYuri Pankov case TBL_CELL_HORIZ:
362cec8643bSMichal Nowak fc = BHORIZ;
363c66b8046SYuri Pankov break;
364c66b8046SYuri Pankov case TBL_CELL_DHORIZ:
365cec8643bSMichal Nowak fc = BHORIZ * 2;
366c66b8046SYuri Pankov break;
367c66b8046SYuri Pankov default:
368c66b8046SYuri Pankov break;
36995c635efSGarrett D'Amore }
370c66b8046SYuri Pankov }
371c66b8046SYuri Pankov if (cpp != NULL) {
372cec8643bSMichal Nowak if (uvert < cpp->vert &&
373c66b8046SYuri Pankov cp != NULL &&
374c66b8046SYuri Pankov ((IS_HORIZ(cp) &&
375c66b8046SYuri Pankov !IS_HORIZ(cpp)) ||
376c66b8046SYuri Pankov (cp->next != NULL &&
377c66b8046SYuri Pankov cpp->next != NULL &&
378c66b8046SYuri Pankov IS_HORIZ(cp->next) &&
379c66b8046SYuri Pankov !IS_HORIZ(cpp->next))))
380cec8643bSMichal Nowak uvert = cpp->vert;
381c66b8046SYuri Pankov cpp = cpp->next;
382c66b8046SYuri Pankov }
383cec8643bSMichal Nowak if (sp->opts->opts & TBL_OPT_ALLBOX) {
384cec8643bSMichal Nowak if (uvert == 0)
385cec8643bSMichal Nowak uvert = 1;
386cec8643bSMichal Nowak if (dvert == 0)
387cec8643bSMichal Nowak dvert = 1;
388cec8643bSMichal Nowak }
389c66b8046SYuri Pankov if (cpn != NULL) {
390cec8643bSMichal Nowak if (dvert == 0 ||
391cec8643bSMichal Nowak (dvert < cpn->vert &&
392cec8643bSMichal Nowak tp->enc == TERMENC_UTF8))
393cec8643bSMichal Nowak dvert = cpn->vert;
394c66b8046SYuri Pankov cpn = cpn->next;
395c66b8046SYuri Pankov }
396cec8643bSMichal Nowak
397cec8643bSMichal Nowak lhori = (cp != NULL &&
398cec8643bSMichal Nowak cp->pos == TBL_CELL_DHORIZ) ||
399cec8643bSMichal Nowak (dp != NULL &&
400cec8643bSMichal Nowak dp->pos == TBL_DATA_DHORIZ) ? 2 :
401cec8643bSMichal Nowak (cp != NULL &&
402cec8643bSMichal Nowak cp->pos == TBL_CELL_HORIZ) ||
403cec8643bSMichal Nowak (dp != NULL &&
404cec8643bSMichal Nowak dp->pos == TBL_DATA_HORIZ) ? 1 : 0;
405260e9a87SYuri Pankov
406260e9a87SYuri Pankov /*
407c66b8046SYuri Pankov * Skip later cells in a span,
408c66b8046SYuri Pankov * figure out whether to start a span,
409c66b8046SYuri Pankov * and advance to next data cell.
410260e9a87SYuri Pankov */
411260e9a87SYuri Pankov
412cec8643bSMichal Nowak if (hspans) {
413cec8643bSMichal Nowak hspans--;
414cec8643bSMichal Nowak cp = cp->next;
415260e9a87SYuri Pankov continue;
41695c635efSGarrett D'Amore }
417*4d131170SRobert Mustacchi if (dp != NULL && (ic ||
418*4d131170SRobert Mustacchi sp->layout->first->pos != TBL_CELL_SPAN)) {
419cec8643bSMichal Nowak hspans = dp->hspans;
420c66b8046SYuri Pankov dp = dp->next;
421c66b8046SYuri Pankov }
42295c635efSGarrett D'Amore
42395c635efSGarrett D'Amore /*
424c66b8046SYuri Pankov * Print one line of text in the cell
425c66b8046SYuri Pankov * and remember whether there is more.
42695c635efSGarrett D'Amore */
42795c635efSGarrett D'Amore
428c66b8046SYuri Pankov tp->tcol++;
429c66b8046SYuri Pankov if (tp->tcol->col < tp->tcol->lastcol)
430c66b8046SYuri Pankov term_flushln(tp);
431c66b8046SYuri Pankov if (tp->tcol->col < tp->tcol->lastcol)
432c66b8046SYuri Pankov more = 1;
433c66b8046SYuri Pankov
434c66b8046SYuri Pankov /*
435c66b8046SYuri Pankov * Vertical frames between data cells,
436c66b8046SYuri Pankov * but not after the last column.
437c66b8046SYuri Pankov */
438c66b8046SYuri Pankov
439cec8643bSMichal Nowak if (fc == 0 &&
440cec8643bSMichal Nowak ((uvert == 0 && dvert == 0 &&
441cec8643bSMichal Nowak cp != NULL && (cp->next == NULL ||
442cec8643bSMichal Nowak !IS_HORIZ(cp->next))) ||
443cec8643bSMichal Nowak tp->tcol + 1 ==
444cec8643bSMichal Nowak tp->tcols + tp->lasttcol)) {
445cec8643bSMichal Nowak if (cp != NULL)
446cec8643bSMichal Nowak cp = cp->next;
447c66b8046SYuri Pankov continue;
448cec8643bSMichal Nowak }
449c66b8046SYuri Pankov
450c66b8046SYuri Pankov if (tp->viscol < tp->tcol->rmargin) {
451c66b8046SYuri Pankov (*tp->advance)(tp, tp->tcol->rmargin
452c66b8046SYuri Pankov - tp->viscol);
453c66b8046SYuri Pankov tp->viscol = tp->tcol->rmargin;
454c66b8046SYuri Pankov }
455c66b8046SYuri Pankov while (tp->viscol < tp->tcol->rmargin +
456cec8643bSMichal Nowak tp->tbl.cols[ic].spacing / 2)
457cec8643bSMichal Nowak tbl_direct_border(tp,
458cec8643bSMichal Nowak BHORIZ * lhori, 1);
459c66b8046SYuri Pankov
460c66b8046SYuri Pankov if (tp->tcol + 1 == tp->tcols + tp->lasttcol)
461c66b8046SYuri Pankov continue;
462c66b8046SYuri Pankov
463cec8643bSMichal Nowak if (cp != NULL)
464cec8643bSMichal Nowak cp = cp->next;
465c66b8046SYuri Pankov
466cec8643bSMichal Nowak rhori = (cp != NULL &&
467cec8643bSMichal Nowak cp->pos == TBL_CELL_DHORIZ) ||
468cec8643bSMichal Nowak (dp != NULL &&
469cec8643bSMichal Nowak dp->pos == TBL_DATA_DHORIZ) ? 2 :
470cec8643bSMichal Nowak (cp != NULL &&
471cec8643bSMichal Nowak cp->pos == TBL_CELL_HORIZ) ||
472cec8643bSMichal Nowak (dp != NULL &&
473cec8643bSMichal Nowak dp->pos == TBL_DATA_HORIZ) ? 1 : 0;
474cec8643bSMichal Nowak
475cec8643bSMichal Nowak if (tp->tbl.cols[ic].spacing)
476cec8643bSMichal Nowak tbl_direct_border(tp,
477cec8643bSMichal Nowak BLEFT * lhori + BRIGHT * rhori +
478cec8643bSMichal Nowak BUP * uvert + BDOWN * dvert, 1);
479cec8643bSMichal Nowak
480cec8643bSMichal Nowak if (tp->enc == TERMENC_UTF8)
481cec8643bSMichal Nowak uvert = dvert = 0;
482cec8643bSMichal Nowak
483c66b8046SYuri Pankov if (tp->tbl.cols[ic].spacing > 2 &&
484cec8643bSMichal Nowak (uvert > 1 || dvert > 1 || rhori))
485cec8643bSMichal Nowak tbl_direct_border(tp,
486cec8643bSMichal Nowak BHORIZ * rhori +
487cec8643bSMichal Nowak BUP * (uvert > 1) +
488cec8643bSMichal Nowak BDOWN * (dvert > 1), 1);
489c66b8046SYuri Pankov }
490c66b8046SYuri Pankov }
491c66b8046SYuri Pankov
492c66b8046SYuri Pankov /* Print the vertical frame at the end of each row. */
493c66b8046SYuri Pankov
494cec8643bSMichal Nowak uvert = dvert = sp->opts->opts & TBL_OPT_DBOX ? 2 :
495cec8643bSMichal Nowak sp->opts->opts & TBL_OPT_BOX ? 1 : 0;
496cec8643bSMichal Nowak if (sp->pos == TBL_SPAN_DATA &&
497cec8643bSMichal Nowak uvert < sp->layout->last->vert &&
498cec8643bSMichal Nowak sp->layout->last->col + 1 == sp->opts->cols)
499cec8643bSMichal Nowak uvert = dvert = sp->layout->last->vert;
500cec8643bSMichal Nowak if (sp->next != NULL &&
501cec8643bSMichal Nowak dvert < sp->next->layout->last->vert &&
502cec8643bSMichal Nowak sp->next->layout->last->col + 1 == sp->opts->cols)
503cec8643bSMichal Nowak dvert = sp->next->layout->last->vert;
504cec8643bSMichal Nowak if (sp->prev != NULL &&
505cec8643bSMichal Nowak uvert < sp->prev->layout->last->vert &&
506c66b8046SYuri Pankov sp->prev->layout->last->col + 1 == sp->opts->cols &&
507c66b8046SYuri Pankov (horiz || (IS_HORIZ(sp->layout->last) &&
508cec8643bSMichal Nowak !IS_HORIZ(sp->prev->layout->last))))
509cec8643bSMichal Nowak uvert = sp->prev->layout->last->vert;
510cec8643bSMichal Nowak lhori = sp->pos == TBL_SPAN_DHORIZ ||
511cec8643bSMichal Nowak (sp->last != NULL &&
512cec8643bSMichal Nowak sp->last->pos == TBL_DATA_DHORIZ &&
513cec8643bSMichal Nowak sp->last->layout->col + 1 == sp->opts->cols) ||
514cec8643bSMichal Nowak (sp->layout->last->pos == TBL_CELL_DHORIZ &&
515cec8643bSMichal Nowak sp->layout->last->col + 1 == sp->opts->cols) ? 2 :
516cec8643bSMichal Nowak sp->pos == TBL_SPAN_HORIZ ||
517cec8643bSMichal Nowak (sp->last != NULL &&
518cec8643bSMichal Nowak sp->last->pos == TBL_DATA_HORIZ &&
519cec8643bSMichal Nowak sp->last->layout->col + 1 == sp->opts->cols) ||
520cec8643bSMichal Nowak (sp->layout->last->pos == TBL_CELL_HORIZ &&
521cec8643bSMichal Nowak sp->layout->last->col + 1 == sp->opts->cols) ? 1 : 0;
522cec8643bSMichal Nowak fc = BUP * uvert + BDOWN * dvert + BLEFT * lhori;
523cec8643bSMichal Nowak if (uvert > 0 || dvert > 0 || (horiz && sp->opts->rvert)) {
524c66b8046SYuri Pankov if (horiz == 0 && (IS_HORIZ(sp->layout->last) == 0 ||
525c66b8046SYuri Pankov sp->layout->last->col + 1 < sp->opts->cols)) {
526c66b8046SYuri Pankov tp->tcol++;
527cec8643bSMichal Nowak do {
528cec8643bSMichal Nowak tbl_direct_border(tp,
529cec8643bSMichal Nowak BHORIZ * lhori, 1);
530cec8643bSMichal Nowak } while (tp->viscol < tp->tcol->offset);
531c66b8046SYuri Pankov }
532cec8643bSMichal Nowak tbl_direct_border(tp, fc, 1);
533c66b8046SYuri Pankov }
534c66b8046SYuri Pankov (*tp->endline)(tp);
535c66b8046SYuri Pankov tp->viscol = 0;
536c66b8046SYuri Pankov } while (more);
537c66b8046SYuri Pankov
538c66b8046SYuri Pankov /*
539c66b8046SYuri Pankov * Clean up after this row. If it is the last line
540c66b8046SYuri Pankov * of the table, print the box line and clean up
541c66b8046SYuri Pankov * column data; otherwise, print the allbox line.
542c66b8046SYuri Pankov */
543c66b8046SYuri Pankov
544c66b8046SYuri Pankov term_setcol(tp, 1);
545c66b8046SYuri Pankov tp->flags &= ~TERMP_MULTICOL;
546c66b8046SYuri Pankov tp->tcol->rmargin = tp->maxrmargin;
547260e9a87SYuri Pankov if (sp->next == NULL) {
548260e9a87SYuri Pankov if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
549*4d131170SRobert Mustacchi tbl_hrule(tp, sp, sp, NULL, TBL_OPT_BOX);
550698f87a4SGarrett D'Amore tp->skipvsp = 1;
551698f87a4SGarrett D'Amore }
552cec8643bSMichal Nowak if (tp->enc == TERMENC_ASCII &&
553cec8643bSMichal Nowak sp->opts->opts & TBL_OPT_DBOX) {
554*4d131170SRobert Mustacchi tbl_hrule(tp, sp, sp, NULL, TBL_OPT_DBOX);
555698f87a4SGarrett D'Amore tp->skipvsp = 2;
556698f87a4SGarrett D'Amore }
55795c635efSGarrett D'Amore assert(tp->tbl.cols);
55895c635efSGarrett D'Amore free(tp->tbl.cols);
55995c635efSGarrett D'Amore tp->tbl.cols = NULL;
560c66b8046SYuri Pankov } else if (horiz == 0 && sp->opts->opts & TBL_OPT_ALLBOX &&
561c66b8046SYuri Pankov (sp->next == NULL || sp->next->pos == TBL_SPAN_DATA ||
562c66b8046SYuri Pankov sp->next->next != NULL))
563*4d131170SRobert Mustacchi tbl_hrule(tp, sp, sp, sp->next, TBL_OPT_ALLBOX);
56495c635efSGarrett D'Amore
565cec8643bSMichal Nowak tp->tcol->offset = save_offset;
56695c635efSGarrett D'Amore tp->flags &= ~TERMP_NONOSPACE;
56795c635efSGarrett D'Amore }
56895c635efSGarrett D'Amore
56995c635efSGarrett D'Amore static void
tbl_hrule(struct termp * tp,const struct tbl_span * spp,const struct tbl_span * sp,const struct tbl_span * spn,int flags)570cec8643bSMichal Nowak tbl_hrule(struct termp *tp, const struct tbl_span *spp,
571*4d131170SRobert Mustacchi const struct tbl_span *sp, const struct tbl_span *spn, int flags)
57295c635efSGarrett D'Amore {
573cec8643bSMichal Nowak const struct tbl_cell *cpp; /* Layout cell above this line. */
574*4d131170SRobert Mustacchi const struct tbl_cell *cp; /* Layout cell in this line. */
575cec8643bSMichal Nowak const struct tbl_cell *cpn; /* Layout cell below this line. */
576cec8643bSMichal Nowak const struct tbl_dat *dpn; /* Data cell below this line. */
577cec8643bSMichal Nowak const struct roffcol *col; /* Contains width and spacing. */
578cec8643bSMichal Nowak int opts; /* For the table as a whole. */
579cec8643bSMichal Nowak int bw; /* Box line width. */
580cec8643bSMichal Nowak int hw; /* Horizontal line width. */
581cec8643bSMichal Nowak int lw, rw; /* Left and right line widths. */
582cec8643bSMichal Nowak int uw, dw; /* Vertical line widths. */
58395c635efSGarrett D'Amore
584cec8643bSMichal Nowak cpp = spp == NULL ? NULL : spp->layout->first;
585*4d131170SRobert Mustacchi cp = sp == NULL ? NULL : sp->layout->first;
586cec8643bSMichal Nowak cpn = spn == NULL ? NULL : spn->layout->first;
587cec8643bSMichal Nowak dpn = NULL;
588cec8643bSMichal Nowak if (spn != NULL) {
589cec8643bSMichal Nowak if (spn->pos == TBL_SPAN_DATA)
590cec8643bSMichal Nowak dpn = spn->first;
591cec8643bSMichal Nowak else if (spn->next != NULL)
592cec8643bSMichal Nowak dpn = spn->next->first;
593cec8643bSMichal Nowak }
594*4d131170SRobert Mustacchi opts = sp->opts->opts;
595cec8643bSMichal Nowak bw = opts & TBL_OPT_DBOX ? (tp->enc == TERMENC_UTF8 ? 2 : 1) :
596cec8643bSMichal Nowak opts & (TBL_OPT_BOX | TBL_OPT_ALLBOX) ? 1 : 0;
597cec8643bSMichal Nowak hw = flags == TBL_OPT_DBOX || flags == TBL_OPT_BOX ? bw :
598*4d131170SRobert Mustacchi sp->pos == TBL_SPAN_DHORIZ ? 2 : 1;
59995c635efSGarrett D'Amore
600cec8643bSMichal Nowak /* Print the left end of the line. */
601cec8643bSMichal Nowak
602cec8643bSMichal Nowak if (tp->viscol == 0) {
603cec8643bSMichal Nowak (*tp->advance)(tp, tp->tcols->offset);
604cec8643bSMichal Nowak tp->viscol = tp->tcols->offset;
605cec8643bSMichal Nowak }
606cec8643bSMichal Nowak if (flags != 0)
607cec8643bSMichal Nowak tbl_direct_border(tp,
608cec8643bSMichal Nowak (spp == NULL ? 0 : BUP * bw) +
609cec8643bSMichal Nowak (spn == NULL ? 0 : BDOWN * bw) +
610cec8643bSMichal Nowak (spp == NULL || cpn == NULL ||
611cec8643bSMichal Nowak cpn->pos != TBL_CELL_DOWN ? BRIGHT * hw : 0), 1);
612cec8643bSMichal Nowak
613*4d131170SRobert Mustacchi col = tp->tbl.cols;
614260e9a87SYuri Pankov for (;;) {
615*4d131170SRobert Mustacchi if (cp == NULL)
616*4d131170SRobert Mustacchi col++;
617*4d131170SRobert Mustacchi else
618*4d131170SRobert Mustacchi col = tp->tbl.cols + cp->col;
619cec8643bSMichal Nowak
620cec8643bSMichal Nowak /* Print the horizontal line inside this column. */
621cec8643bSMichal Nowak
622cec8643bSMichal Nowak lw = cpp == NULL || cpn == NULL ||
623cec8643bSMichal Nowak (cpn->pos != TBL_CELL_DOWN &&
624*4d131170SRobert Mustacchi (dpn == NULL || dpn->string == NULL ||
625*4d131170SRobert Mustacchi strcmp(dpn->string, "\\^") != 0))
626cec8643bSMichal Nowak ? hw : 0;
627cec8643bSMichal Nowak tbl_direct_border(tp, BHORIZ * lw,
628cec8643bSMichal Nowak col->width + col->spacing / 2);
629cec8643bSMichal Nowak
630cec8643bSMichal Nowak /*
631cec8643bSMichal Nowak * Figure out whether a vertical line is crossing
632cec8643bSMichal Nowak * at the end of this column,
633cec8643bSMichal Nowak * and advance to the next column.
634cec8643bSMichal Nowak */
635cec8643bSMichal Nowak
636cec8643bSMichal Nowak uw = dw = 0;
637c66b8046SYuri Pankov if (cpp != NULL) {
638cec8643bSMichal Nowak if (flags != TBL_OPT_DBOX) {
639cec8643bSMichal Nowak uw = cpp->vert;
640cec8643bSMichal Nowak if (uw == 0 && opts & TBL_OPT_ALLBOX)
641cec8643bSMichal Nowak uw = 1;
642cec8643bSMichal Nowak }
643c66b8046SYuri Pankov cpp = cpp->next;
644*4d131170SRobert Mustacchi } else if (spp != NULL && opts & TBL_OPT_ALLBOX)
645*4d131170SRobert Mustacchi uw = 1;
646*4d131170SRobert Mustacchi if (cp != NULL)
647*4d131170SRobert Mustacchi cp = cp->next;
648c66b8046SYuri Pankov if (cpn != NULL) {
649cec8643bSMichal Nowak if (flags != TBL_OPT_DBOX) {
650cec8643bSMichal Nowak dw = cpn->vert;
651cec8643bSMichal Nowak if (dw == 0 && opts & TBL_OPT_ALLBOX)
652cec8643bSMichal Nowak dw = 1;
653cec8643bSMichal Nowak }
654c66b8046SYuri Pankov cpn = cpn->next;
655cec8643bSMichal Nowak while (dpn != NULL && dpn->layout != cpn)
656cec8643bSMichal Nowak dpn = dpn->next;
657*4d131170SRobert Mustacchi } else if (spn != NULL && opts & TBL_OPT_ALLBOX)
658*4d131170SRobert Mustacchi dw = 1;
659*4d131170SRobert Mustacchi if (col + 1 == tp->tbl.cols + sp->opts->cols)
660cec8643bSMichal Nowak break;
661cec8643bSMichal Nowak
662cec8643bSMichal Nowak /* Vertical lines do not cross spanned cells. */
663cec8643bSMichal Nowak
664cec8643bSMichal Nowak if (cpp != NULL && cpp->pos == TBL_CELL_SPAN)
665cec8643bSMichal Nowak uw = 0;
666cec8643bSMichal Nowak if (cpn != NULL && cpn->pos == TBL_CELL_SPAN)
667cec8643bSMichal Nowak dw = 0;
668cec8643bSMichal Nowak
669cec8643bSMichal Nowak /* The horizontal line inside the next column. */
670cec8643bSMichal Nowak
671cec8643bSMichal Nowak rw = cpp == NULL || cpn == NULL ||
672cec8643bSMichal Nowak (cpn->pos != TBL_CELL_DOWN &&
673*4d131170SRobert Mustacchi (dpn == NULL || dpn->string == NULL ||
674*4d131170SRobert Mustacchi strcmp(dpn->string, "\\^") != 0))
675cec8643bSMichal Nowak ? hw : 0;
676cec8643bSMichal Nowak
677cec8643bSMichal Nowak /* The line crossing at the end of this column. */
678cec8643bSMichal Nowak
679c66b8046SYuri Pankov if (col->spacing)
680cec8643bSMichal Nowak tbl_direct_border(tp, BLEFT * lw +
681cec8643bSMichal Nowak BRIGHT * rw + BUP * uw + BDOWN * dw, 1);
682cec8643bSMichal Nowak
683cec8643bSMichal Nowak /*
684cec8643bSMichal Nowak * In ASCII output, a crossing may print two characters.
685cec8643bSMichal Nowak */
686cec8643bSMichal Nowak
687cec8643bSMichal Nowak if (tp->enc != TERMENC_ASCII || (uw < 2 && dw < 2))
688cec8643bSMichal Nowak uw = dw = 0;
689c66b8046SYuri Pankov if (col->spacing > 2)
690cec8643bSMichal Nowak tbl_direct_border(tp,
691cec8643bSMichal Nowak BHORIZ * rw + BUP * uw + BDOWN * dw, 1);
692cec8643bSMichal Nowak
693cec8643bSMichal Nowak /* Padding before the start of the next column. */
694cec8643bSMichal Nowak
695c66b8046SYuri Pankov if (col->spacing > 4)
696cec8643bSMichal Nowak tbl_direct_border(tp,
697cec8643bSMichal Nowak BHORIZ * rw, (col->spacing - 3) / 2);
698260e9a87SYuri Pankov }
699cec8643bSMichal Nowak
700cec8643bSMichal Nowak /* Print the right end of the line. */
701cec8643bSMichal Nowak
702cec8643bSMichal Nowak if (flags != 0) {
703cec8643bSMichal Nowak tbl_direct_border(tp,
704cec8643bSMichal Nowak (spp == NULL ? 0 : BUP * bw) +
705cec8643bSMichal Nowak (spn == NULL ? 0 : BDOWN * bw) +
706cec8643bSMichal Nowak (spp == NULL || spn == NULL ||
707cec8643bSMichal Nowak spn->layout->last->pos != TBL_CELL_DOWN ?
708cec8643bSMichal Nowak BLEFT * hw : 0), 1);
709cec8643bSMichal Nowak (*tp->endline)(tp);
710cec8643bSMichal Nowak tp->viscol = 0;
71195c635efSGarrett D'Amore }
712260e9a87SYuri Pankov }
71395c635efSGarrett D'Amore
71495c635efSGarrett D'Amore static void
tbl_data(struct termp * tp,const struct tbl_opts * opts,const struct tbl_cell * cp,const struct tbl_dat * dp,const struct roffcol * col)715698f87a4SGarrett D'Amore tbl_data(struct termp *tp, const struct tbl_opts *opts,
716c66b8046SYuri Pankov const struct tbl_cell *cp, const struct tbl_dat *dp,
71795c635efSGarrett D'Amore const struct roffcol *col)
71895c635efSGarrett D'Amore {
719c66b8046SYuri Pankov switch (cp->pos) {
720c66b8046SYuri Pankov case TBL_CELL_HORIZ:
721cec8643bSMichal Nowak tbl_fill_border(tp, BHORIZ, col->width);
72295c635efSGarrett D'Amore return;
723c66b8046SYuri Pankov case TBL_CELL_DHORIZ:
724cec8643bSMichal Nowak tbl_fill_border(tp, BHORIZ * 2, col->width);
725c66b8046SYuri Pankov return;
726c66b8046SYuri Pankov default:
727c66b8046SYuri Pankov break;
72895c635efSGarrett D'Amore }
72995c635efSGarrett D'Amore
730c66b8046SYuri Pankov if (dp == NULL)
731c66b8046SYuri Pankov return;
732c66b8046SYuri Pankov
73395c635efSGarrett D'Amore switch (dp->pos) {
734260e9a87SYuri Pankov case TBL_DATA_NONE:
73595c635efSGarrett D'Amore return;
736260e9a87SYuri Pankov case TBL_DATA_HORIZ:
737260e9a87SYuri Pankov case TBL_DATA_NHORIZ:
738cec8643bSMichal Nowak tbl_fill_border(tp, BHORIZ, col->width);
73995c635efSGarrett D'Amore return;
740260e9a87SYuri Pankov case TBL_DATA_NDHORIZ:
741260e9a87SYuri Pankov case TBL_DATA_DHORIZ:
742cec8643bSMichal Nowak tbl_fill_border(tp, BHORIZ * 2, col->width);
74395c635efSGarrett D'Amore return;
74495c635efSGarrett D'Amore default:
74595c635efSGarrett D'Amore break;
74695c635efSGarrett D'Amore }
74795c635efSGarrett D'Amore
748c66b8046SYuri Pankov switch (cp->pos) {
749260e9a87SYuri Pankov case TBL_CELL_LONG:
750260e9a87SYuri Pankov case TBL_CELL_CENTRE:
751260e9a87SYuri Pankov case TBL_CELL_LEFT:
752260e9a87SYuri Pankov case TBL_CELL_RIGHT:
75395c635efSGarrett D'Amore tbl_literal(tp, dp, col);
75495c635efSGarrett D'Amore break;
755260e9a87SYuri Pankov case TBL_CELL_NUMBER:
756698f87a4SGarrett D'Amore tbl_number(tp, opts, dp, col);
75795c635efSGarrett D'Amore break;
758260e9a87SYuri Pankov case TBL_CELL_DOWN:
759c66b8046SYuri Pankov case TBL_CELL_SPAN:
76095c635efSGarrett D'Amore break;
76195c635efSGarrett D'Amore default:
76295c635efSGarrett D'Amore abort();
76395c635efSGarrett D'Amore }
76495c635efSGarrett D'Amore }
76595c635efSGarrett D'Amore
76695c635efSGarrett D'Amore static void
tbl_fill_string(struct termp * tp,const char * cp,size_t len)767cec8643bSMichal Nowak tbl_fill_string(struct termp *tp, const char *cp, size_t len)
76895c635efSGarrett D'Amore {
76995c635efSGarrett D'Amore size_t i, sz;
770cec8643bSMichal Nowak
771cec8643bSMichal Nowak sz = term_strlen(tp, cp);
772cec8643bSMichal Nowak for (i = 0; i < len; i += sz)
773cec8643bSMichal Nowak term_word(tp, cp);
774cec8643bSMichal Nowak }
775cec8643bSMichal Nowak
776cec8643bSMichal Nowak static void
tbl_fill_char(struct termp * tp,char c,size_t len)777cec8643bSMichal Nowak tbl_fill_char(struct termp *tp, char c, size_t len)
778cec8643bSMichal Nowak {
77995c635efSGarrett D'Amore char cp[2];
78095c635efSGarrett D'Amore
78195c635efSGarrett D'Amore cp[0] = c;
78295c635efSGarrett D'Amore cp[1] = '\0';
783cec8643bSMichal Nowak tbl_fill_string(tp, cp, len);
784cec8643bSMichal Nowak }
78595c635efSGarrett D'Amore
786cec8643bSMichal Nowak static void
tbl_fill_border(struct termp * tp,int c,size_t len)787cec8643bSMichal Nowak tbl_fill_border(struct termp *tp, int c, size_t len)
788cec8643bSMichal Nowak {
789cec8643bSMichal Nowak char buf[12];
79095c635efSGarrett D'Amore
791cec8643bSMichal Nowak if ((c = borders_locale[c]) > 127) {
792cec8643bSMichal Nowak (void)snprintf(buf, sizeof(buf), "\\[u%04x]", c);
793cec8643bSMichal Nowak tbl_fill_string(tp, buf, len);
794cec8643bSMichal Nowak } else
795cec8643bSMichal Nowak tbl_fill_char(tp, c, len);
796cec8643bSMichal Nowak }
797cec8643bSMichal Nowak
798cec8643bSMichal Nowak static void
tbl_direct_border(struct termp * tp,int c,size_t len)799cec8643bSMichal Nowak tbl_direct_border(struct termp *tp, int c, size_t len)
800cec8643bSMichal Nowak {
801cec8643bSMichal Nowak size_t i, sz;
802cec8643bSMichal Nowak
803cec8643bSMichal Nowak c = borders_locale[c];
804cec8643bSMichal Nowak sz = (*tp->width)(tp, c);
805cec8643bSMichal Nowak for (i = 0; i < len; i += sz) {
806cec8643bSMichal Nowak (*tp->letter)(tp, c);
807cec8643bSMichal Nowak tp->viscol += sz;
808cec8643bSMichal Nowak }
80995c635efSGarrett D'Amore }
81095c635efSGarrett D'Amore
81195c635efSGarrett D'Amore static void
tbl_literal(struct termp * tp,const struct tbl_dat * dp,const struct roffcol * col)81295c635efSGarrett D'Amore tbl_literal(struct termp *tp, const struct tbl_dat *dp,
81395c635efSGarrett D'Amore const struct roffcol *col)
81495c635efSGarrett D'Amore {
815260e9a87SYuri Pankov size_t len, padl, padr, width;
816cec8643bSMichal Nowak int ic, hspans;
81795c635efSGarrett D'Amore
81895c635efSGarrett D'Amore assert(dp->string);
81995c635efSGarrett D'Amore len = term_strlen(tp, dp->string);
820698f87a4SGarrett D'Amore width = col->width;
821260e9a87SYuri Pankov ic = dp->layout->col;
822cec8643bSMichal Nowak hspans = dp->hspans;
823cec8643bSMichal Nowak while (hspans--)
824260e9a87SYuri Pankov width += tp->tbl.cols[++ic].width + 3;
825698f87a4SGarrett D'Amore
826698f87a4SGarrett D'Amore padr = width > len ? width - len : 0;
82795c635efSGarrett D'Amore padl = 0;
82895c635efSGarrett D'Amore
82995c635efSGarrett D'Amore switch (dp->layout->pos) {
830260e9a87SYuri Pankov case TBL_CELL_LONG:
83195c635efSGarrett D'Amore padl = term_len(tp, 1);
83295c635efSGarrett D'Amore padr = padr > padl ? padr - padl : 0;
83395c635efSGarrett D'Amore break;
834260e9a87SYuri Pankov case TBL_CELL_CENTRE:
83595c635efSGarrett D'Amore if (2 > padr)
83695c635efSGarrett D'Amore break;
83795c635efSGarrett D'Amore padl = padr / 2;
83895c635efSGarrett D'Amore padr -= padl;
83995c635efSGarrett D'Amore break;
840260e9a87SYuri Pankov case TBL_CELL_RIGHT:
84195c635efSGarrett D'Amore padl = padr;
84295c635efSGarrett D'Amore padr = 0;
84395c635efSGarrett D'Amore break;
84495c635efSGarrett D'Amore default:
84595c635efSGarrett D'Amore break;
84695c635efSGarrett D'Amore }
84795c635efSGarrett D'Amore
848cec8643bSMichal Nowak tbl_fill_char(tp, ASCII_NBRSP, padl);
849260e9a87SYuri Pankov tbl_word(tp, dp);
850cec8643bSMichal Nowak tbl_fill_char(tp, ASCII_NBRSP, padr);
85195c635efSGarrett D'Amore }
85295c635efSGarrett D'Amore
85395c635efSGarrett D'Amore static void
tbl_number(struct termp * tp,const struct tbl_opts * opts,const struct tbl_dat * dp,const struct roffcol * col)854698f87a4SGarrett D'Amore tbl_number(struct termp *tp, const struct tbl_opts *opts,
85595c635efSGarrett D'Amore const struct tbl_dat *dp,
85695c635efSGarrett D'Amore const struct roffcol *col)
85795c635efSGarrett D'Amore {
858cec8643bSMichal Nowak const char *cp, *lastdigit, *lastpoint;
859cec8643bSMichal Nowak size_t intsz, padl, totsz;
86095c635efSGarrett D'Amore char buf[2];
86195c635efSGarrett D'Amore
86295c635efSGarrett D'Amore /*
863cec8643bSMichal Nowak * Almost the same code as in tblcalc_number():
864cec8643bSMichal Nowak * First find the position of the decimal point.
86595c635efSGarrett D'Amore */
86695c635efSGarrett D'Amore
86795c635efSGarrett D'Amore assert(dp->string);
868cec8643bSMichal Nowak lastdigit = lastpoint = NULL;
869cec8643bSMichal Nowak for (cp = dp->string; cp[0] != '\0'; cp++) {
870cec8643bSMichal Nowak if (cp[0] == '\\' && cp[1] == '&') {
871cec8643bSMichal Nowak lastdigit = lastpoint = cp;
872cec8643bSMichal Nowak break;
873cec8643bSMichal Nowak } else if (cp[0] == opts->decimal &&
874cec8643bSMichal Nowak (isdigit((unsigned char)cp[1]) ||
875cec8643bSMichal Nowak (cp > dp->string && isdigit((unsigned char)cp[-1]))))
876cec8643bSMichal Nowak lastpoint = cp;
877cec8643bSMichal Nowak else if (isdigit((unsigned char)cp[0]))
878cec8643bSMichal Nowak lastdigit = cp;
87995c635efSGarrett D'Amore }
88095c635efSGarrett D'Amore
881cec8643bSMichal Nowak /* Then measure both widths. */
882cec8643bSMichal Nowak
883260e9a87SYuri Pankov padl = 0;
884cec8643bSMichal Nowak totsz = term_strlen(tp, dp->string);
885cec8643bSMichal Nowak if (lastdigit != NULL) {
886cec8643bSMichal Nowak if (lastpoint == NULL)
887cec8643bSMichal Nowak lastpoint = lastdigit + 1;
888cec8643bSMichal Nowak intsz = 0;
889cec8643bSMichal Nowak buf[1] = '\0';
890cec8643bSMichal Nowak for (cp = dp->string; cp < lastpoint; cp++) {
891cec8643bSMichal Nowak buf[0] = cp[0];
892cec8643bSMichal Nowak intsz += term_strlen(tp, buf);
893cec8643bSMichal Nowak }
894cec8643bSMichal Nowak
895cec8643bSMichal Nowak /*
896cec8643bSMichal Nowak * Pad left to match the decimal position,
897cec8643bSMichal Nowak * but avoid exceeding the total column width.
898cec8643bSMichal Nowak */
899cec8643bSMichal Nowak
900cec8643bSMichal Nowak if (col->decimal > intsz && col->width > totsz) {
901cec8643bSMichal Nowak padl = col->decimal - intsz;
902cec8643bSMichal Nowak if (padl + totsz > col->width)
903cec8643bSMichal Nowak padl = col->width - totsz;
904cec8643bSMichal Nowak }
905cec8643bSMichal Nowak
906cec8643bSMichal Nowak /* If it is not a number, simply center the string. */
907cec8643bSMichal Nowak
908cec8643bSMichal Nowak } else if (col->width > totsz)
909cec8643bSMichal Nowak padl = (col->width - totsz) / 2;
910cec8643bSMichal Nowak
911cec8643bSMichal Nowak tbl_fill_char(tp, ASCII_NBRSP, padl);
912260e9a87SYuri Pankov tbl_word(tp, dp);
913cec8643bSMichal Nowak
914cec8643bSMichal Nowak /* Pad right to fill the column. */
915cec8643bSMichal Nowak
916cec8643bSMichal Nowak if (col->width > padl + totsz)
917cec8643bSMichal Nowak tbl_fill_char(tp, ASCII_NBRSP, col->width - padl - totsz);
91895c635efSGarrett D'Amore }
91995c635efSGarrett D'Amore
920260e9a87SYuri Pankov static void
tbl_word(struct termp * tp,const struct tbl_dat * dp)921260e9a87SYuri Pankov tbl_word(struct termp *tp, const struct tbl_dat *dp)
922260e9a87SYuri Pankov {
923260e9a87SYuri Pankov int prev_font;
924260e9a87SYuri Pankov
925260e9a87SYuri Pankov prev_font = tp->fonti;
926*4d131170SRobert Mustacchi switch (dp->layout->font) {
927*4d131170SRobert Mustacchi case ESCAPE_FONTBI:
928*4d131170SRobert Mustacchi term_fontpush(tp, TERMFONT_BI);
929*4d131170SRobert Mustacchi break;
930*4d131170SRobert Mustacchi case ESCAPE_FONTBOLD:
931*4d131170SRobert Mustacchi case ESCAPE_FONTCB:
932260e9a87SYuri Pankov term_fontpush(tp, TERMFONT_BOLD);
933*4d131170SRobert Mustacchi break;
934*4d131170SRobert Mustacchi case ESCAPE_FONTITALIC:
935*4d131170SRobert Mustacchi case ESCAPE_FONTCI:
936260e9a87SYuri Pankov term_fontpush(tp, TERMFONT_UNDER);
937*4d131170SRobert Mustacchi break;
938*4d131170SRobert Mustacchi case ESCAPE_FONTROMAN:
939*4d131170SRobert Mustacchi case ESCAPE_FONTCR:
940*4d131170SRobert Mustacchi break;
941*4d131170SRobert Mustacchi default:
942*4d131170SRobert Mustacchi abort();
943*4d131170SRobert Mustacchi }
944260e9a87SYuri Pankov
945260e9a87SYuri Pankov term_word(tp, dp->string);
946260e9a87SYuri Pankov
947260e9a87SYuri Pankov term_fontpopq(tp, prev_font);
948260e9a87SYuri Pankov }
949