1 /* $Id: tbl_term.c,v 1.46 2017/06/08 18:11:22 schwarze Exp $ */
3 * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011,2012,2014,2015,2017 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
31 static size_t term_tbl_len(size_t, void *);
32 static size_t term_tbl_strlen(const char *, void *);
33 static size_t term_tbl_sulen(const struct roffsu *, void *);
34 static void tbl_char(struct termp *, char, size_t);
35 static void tbl_data(struct termp *, const struct tbl_opts *,
36 const struct tbl_dat *,
37 const struct roffcol *);
38 static void tbl_literal(struct termp *, const struct tbl_dat *,
39 const struct roffcol *);
40 static void tbl_number(struct termp *, const struct tbl_opts *,
41 const struct tbl_dat *,
42 const struct roffcol *);
43 static void tbl_hrule(struct termp *, const struct tbl_span *, int);
44 static void tbl_word(struct termp *, const struct tbl_dat *);
48 term_tbl_sulen(const struct roffsu *su, void *arg)
50 return term_hspan((const struct termp *)arg, su) / 24;
54 term_tbl_strlen(const char *p, void *arg)
56 return term_strlen((const struct termp *)arg, p);
60 term_tbl_len(size_t sz, void *arg)
62 return term_len((const struct termp *)arg, sz);
66 term_tbl(struct termp *tp, const struct tbl_span *sp)
68 const struct tbl_cell *cp;
69 const struct tbl_dat *dp;
72 int ic, horiz, spans, vert;
74 /* Inhibit printing of spaces: we do padding ourselves. */
76 tp->flags |= TERMP_NOSPACE | TERMP_NONOSPACE | TERMP_BRNEVER;
79 * The first time we're invoked for a given table block,
80 * calculate the table widths and decimal positions.
83 if (tp->tbl.cols == NULL) {
84 tp->tbl.len = term_tbl_len;
85 tp->tbl.slen = term_tbl_strlen;
86 tp->tbl.sulen = term_tbl_sulen;
89 tblcalc(&tp->tbl, sp, tp->tcol->rmargin - tp->tcol->offset);
91 /* Center the table as a whole. */
93 offset = tp->tcol->offset;
94 if (sp->opts->opts & TBL_OPT_CENTRE) {
95 tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)
96 ? 2 : !!sp->opts->lvert + !!sp->opts->rvert;
97 for (ic = 0; ic < sp->opts->cols; ic++)
98 tsz += tp->tbl.cols[ic].width + 3;
100 if (offset + tsz > tp->tcol->rmargin)
102 tp->tcol->offset = offset + tp->tcol->rmargin > tsz ?
103 (offset + tp->tcol->rmargin - tsz) / 2 : 0;
106 /* Horizontal frame at the start of boxed tables. */
108 if (sp->opts->opts & TBL_OPT_DBOX)
109 tbl_hrule(tp, sp, 2);
110 if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX))
111 tbl_hrule(tp, sp, 1);
114 /* Vertical frame at the start of each row. */
116 horiz = sp->pos == TBL_SPAN_HORIZ || sp->pos == TBL_SPAN_DHORIZ;
118 if (sp->layout->vert ||
119 (sp->prev != NULL && sp->prev->layout->vert) ||
120 sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX))
121 term_word(tp, horiz ? "+" : "|");
122 else if (sp->opts->lvert)
123 tbl_char(tp, horiz ? '-' : ASCII_NBRSP, 1);
126 * Now print the actual data itself depending on the span type.
127 * Match data cells to column numbers.
130 if (sp->pos == TBL_SPAN_DATA) {
131 cp = sp->layout->first;
134 for (ic = 0; ic < sp->opts->cols; ic++) {
137 * Remeber whether we need a vertical bar
141 vert = cp == NULL ? 0 : cp->vert;
144 * Print the data and advance to the next cell.
148 tbl_data(tp, sp->opts, dp, tp->tbl.cols + ic);
159 * Separate columns, except in the middle
160 * of spans and after the last cell.
163 if (ic + 1 == sp->opts->cols || spans)
166 tbl_char(tp, ASCII_NBRSP, 1);
168 tbl_char(tp, '|', vert);
170 tbl_char(tp, ASCII_NBRSP, 2 - vert);
173 tbl_hrule(tp, sp, 0);
175 /* Vertical frame at the end of each row. */
177 if (sp->layout->last->vert ||
178 (sp->prev != NULL && sp->prev->layout->last->vert) ||
179 (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)))
180 term_word(tp, horiz ? "+" : " |");
181 else if (sp->opts->rvert)
182 tbl_char(tp, horiz ? '-' : ASCII_NBRSP, 1);
186 * If we're the last row, clean up after ourselves: clear the
187 * existing table configuration and set it to NULL.
190 if (sp->next == NULL) {
191 if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
192 tbl_hrule(tp, sp, 1);
195 if (sp->opts->opts & TBL_OPT_DBOX) {
196 tbl_hrule(tp, sp, 2);
199 assert(tp->tbl.cols);
202 tp->tcol->offset = offset;
204 tp->flags &= ~(TERMP_NONOSPACE | TERMP_BRNEVER);
208 * Kinds of horizontal rulers:
209 * 0: inside the table (single or double line with crossings)
210 * 1: inner frame (single line with crossings and ends)
211 * 2: outer frame (single line without crossings with ends)
214 tbl_hrule(struct termp *tp, const struct tbl_span *sp, int kind)
216 const struct tbl_cell *c1, *c2;
220 line = (kind == 0 && TBL_SPAN_DHORIZ == sp->pos) ? '=' : '-';
221 cross = (kind < 2) ? '+' : '-';
225 c1 = sp->layout->first;
226 c2 = sp->prev == NULL ? NULL : sp->prev->layout->first;
230 tbl_char(tp, line, tp->tbl.cols[c1->col].width + 1);
232 if ((c1 = c1->next) == NULL)
240 tbl_char(tp, cross, vert);
242 tbl_char(tp, line, 2 - vert);
251 tbl_data(struct termp *tp, const struct tbl_opts *opts,
252 const struct tbl_dat *dp,
253 const struct roffcol *col)
257 tbl_char(tp, ASCII_NBRSP, col->width);
263 tbl_char(tp, ASCII_NBRSP, col->width);
266 case TBL_DATA_NHORIZ:
267 tbl_char(tp, '-', col->width);
269 case TBL_DATA_NDHORIZ:
270 case TBL_DATA_DHORIZ:
271 tbl_char(tp, '=', col->width);
277 switch (dp->layout->pos) {
279 tbl_char(tp, '-', col->width);
281 case TBL_CELL_DHORIZ:
282 tbl_char(tp, '=', col->width);
285 case TBL_CELL_CENTRE:
288 tbl_literal(tp, dp, col);
290 case TBL_CELL_NUMBER:
291 tbl_number(tp, opts, dp, col);
294 tbl_char(tp, ASCII_NBRSP, col->width);
302 tbl_char(struct termp *tp, char c, size_t len)
310 sz = term_strlen(tp, cp);
312 for (i = 0; i < len; i += sz)
317 tbl_literal(struct termp *tp, const struct tbl_dat *dp,
318 const struct roffcol *col)
320 size_t len, padl, padr, width;
324 len = term_strlen(tp, dp->string);
326 ic = dp->layout->col;
329 width += tp->tbl.cols[++ic].width + 3;
331 padr = width > len ? width - len : 0;
334 switch (dp->layout->pos) {
336 padl = term_len(tp, 1);
337 padr = padr > padl ? padr - padl : 0;
339 case TBL_CELL_CENTRE:
353 tbl_char(tp, ASCII_NBRSP, padl);
355 tbl_char(tp, ASCII_NBRSP, padr);
359 tbl_number(struct termp *tp, const struct tbl_opts *opts,
360 const struct tbl_dat *dp,
361 const struct roffcol *col)
365 size_t sz, psz, ssz, d, padl;
369 * See calc_data_number(). Left-pad by taking the offset of our
370 * and the maximum decimal; right-pad by the remaining amount.
375 sz = term_strlen(tp, dp->string);
377 buf[0] = opts->decimal;
380 psz = term_strlen(tp, buf);
382 if ((cp = strrchr(dp->string, opts->decimal)) != NULL) {
383 for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
384 buf[0] = dp->string[i];
385 ssz += term_strlen(tp, buf);
391 if (col->decimal > d && col->width > sz) {
392 padl = col->decimal - d;
393 if (padl + sz > col->width)
394 padl = col->width - sz;
395 tbl_char(tp, ASCII_NBRSP, padl);
399 if (col->width > sz + padl)
400 tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);
404 tbl_word(struct termp *tp, const struct tbl_dat *dp)
408 prev_font = tp->fonti;
409 if (dp->layout->flags & TBL_CELL_BOLD)
410 term_fontpush(tp, TERMFONT_BOLD);
411 else if (dp->layout->flags & TBL_CELL_ITALIC)
412 term_fontpush(tp, TERMFONT_UNDER);
414 term_word(tp, dp->string);
416 term_fontpopq(tp, prev_font);