1 /* $Id: tbl_term.c,v 1.57 2017/07/31 16:14:10 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 #define IS_HORIZ(cp) ((cp)->pos == TBL_CELL_HORIZ || \
32 (cp)->pos == TBL_CELL_DHORIZ)
34 static size_t term_tbl_len(size_t, void *);
35 static size_t term_tbl_strlen(const char *, void *);
36 static size_t term_tbl_sulen(const struct roffsu
*, void *);
37 static void tbl_char(struct termp
*, char, size_t);
38 static void tbl_data(struct termp
*, const struct tbl_opts
*,
39 const struct tbl_cell
*,
40 const struct tbl_dat
*,
41 const struct roffcol
*);
42 static void tbl_literal(struct termp
*, const struct tbl_dat
*,
43 const struct roffcol
*);
44 static void tbl_number(struct termp
*, const struct tbl_opts
*,
45 const struct tbl_dat
*,
46 const struct roffcol
*);
47 static void tbl_hrule(struct termp
*, const struct tbl_span
*, int);
48 static void tbl_word(struct termp
*, const struct tbl_dat
*);
52 term_tbl_sulen(const struct roffsu
*su
, void *arg
)
56 i
= term_hen((const struct termp
*)arg
, su
);
61 term_tbl_strlen(const char *p
, void *arg
)
63 return term_strlen((const struct termp
*)arg
, p
);
67 term_tbl_len(size_t sz
, void *arg
)
69 return term_len((const struct termp
*)arg
, sz
);
73 term_tbl(struct termp
*tp
, const struct tbl_span
*sp
)
75 const struct tbl_cell
*cp
, *cpn
, *cpp
;
76 const struct tbl_dat
*dp
;
79 int ic
, horiz
, spans
, vert
, more
;
82 /* Inhibit printing of spaces: we do padding ourselves. */
84 tp
->flags
|= TERMP_NOSPACE
| TERMP_NONOSPACE
;
87 * The first time we're invoked for a given table block,
88 * calculate the table widths and decimal positions.
91 if (tp
->tbl
.cols
== NULL
) {
92 tp
->tbl
.len
= term_tbl_len
;
93 tp
->tbl
.slen
= term_tbl_strlen
;
94 tp
->tbl
.sulen
= term_tbl_sulen
;
97 tblcalc(&tp
->tbl
, sp
, tp
->tcol
->offset
, tp
->tcol
->rmargin
);
99 /* Tables leak .ta settings to subsequent text. */
101 term_tab_set(tp
, NULL
);
102 coloff
= sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
) ||
104 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
105 coloff
+= tp
->tbl
.cols
[ic
].width
;
106 term_tab_iset(coloff
);
107 coloff
+= tp
->tbl
.cols
[ic
].spacing
;
110 /* Center the table as a whole. */
112 offset
= tp
->tcol
->offset
;
113 if (sp
->opts
->opts
& TBL_OPT_CENTRE
) {
114 tsz
= sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
)
115 ? 2 : !!sp
->opts
->lvert
+ !!sp
->opts
->rvert
;
116 for (ic
= 0; ic
+ 1 < sp
->opts
->cols
; ic
++)
117 tsz
+= tp
->tbl
.cols
[ic
].width
+
118 tp
->tbl
.cols
[ic
].spacing
;
120 tsz
+= tp
->tbl
.cols
[sp
->opts
->cols
- 1].width
;
121 if (offset
+ tsz
> tp
->tcol
->rmargin
)
123 tp
->tcol
->offset
= offset
+ tp
->tcol
->rmargin
> tsz
?
124 (offset
+ tp
->tcol
->rmargin
- tsz
) / 2 : 0;
127 /* Horizontal frame at the start of boxed tables. */
129 if (sp
->opts
->opts
& TBL_OPT_DBOX
)
130 tbl_hrule(tp
, sp
, 3);
131 if (sp
->opts
->opts
& (TBL_OPT_DBOX
| TBL_OPT_BOX
))
132 tbl_hrule(tp
, sp
, 2);
135 /* Set up the columns. */
137 tp
->flags
|= TERMP_MULTICOL
;
141 case TBL_SPAN_DHORIZ
:
146 term_setcol(tp
, sp
->opts
->cols
+ 2);
147 coloff
= tp
->tcol
->offset
;
149 /* Set up a column for a left vertical frame. */
151 if (sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
) ||
154 tp
->tcol
->rmargin
= coloff
;
156 /* Set up the data columns. */
160 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
163 tp
->tcol
->offset
= coloff
;
165 coloff
+= tp
->tbl
.cols
[ic
].width
;
166 tp
->tcol
->rmargin
= coloff
;
167 if (ic
+ 1 < sp
->opts
->cols
)
168 coloff
+= tp
->tbl
.cols
[ic
].spacing
;
176 if (ic
|| sp
->layout
->first
->pos
!= TBL_CELL_SPAN
)
180 /* Set up a column for a right vertical frame. */
183 tp
->tcol
->offset
= coloff
+ 1;
184 tp
->tcol
->rmargin
= tp
->maxrmargin
;
186 /* Spans may have reduced the number of columns. */
188 tp
->lasttcol
= tp
->tcol
- tp
->tcols
;
190 /* Fill the buffers for all data columns. */
192 tp
->tcol
= tp
->tcols
;
193 cp
= cpn
= sp
->layout
->first
;
196 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
207 tbl_data(tp
, sp
->opts
, cp
, dp
, tp
->tbl
.cols
+ ic
);
211 if (cp
->pos
!= TBL_CELL_SPAN
)
218 /* Print the vertical frame at the start of each row. */
220 tp
->tcol
= tp
->tcols
;
222 if (sp
->layout
->vert
||
223 (sp
->next
!= NULL
&& sp
->next
->layout
->vert
&&
224 sp
->next
->pos
== TBL_SPAN_DATA
) ||
225 (sp
->prev
!= NULL
&& sp
->prev
->layout
->vert
&&
226 (horiz
|| (IS_HORIZ(sp
->layout
->first
) &&
227 !IS_HORIZ(sp
->prev
->layout
->first
)))) ||
228 sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
))
229 fc
= horiz
|| IS_HORIZ(sp
->layout
->first
) ? '+' : '|';
230 else if (horiz
&& sp
->opts
->lvert
)
233 (*tp
->advance
)(tp
, tp
->tcols
->offset
);
234 (*tp
->letter
)(tp
, fc
);
235 tp
->viscol
= tp
->tcol
->offset
+ 1;
238 /* Print the data cells. */
242 tbl_hrule(tp
, sp
, 0);
245 cp
= sp
->layout
->first
;
246 cpn
= sp
->next
== NULL
? NULL
:
247 sp
->next
->layout
->first
;
248 cpp
= sp
->prev
== NULL
? NULL
:
249 sp
->prev
->layout
->first
;
252 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
255 * Figure out whether to print a
256 * vertical line after this cell
257 * and advance to next layout cell.
266 case TBL_CELL_DHORIZ
:
284 IS_HORIZ(cp
->next
) &&
285 !IS_HORIZ(cpp
->next
))))
290 sp
->opts
->opts
& TBL_OPT_ALLBOX
)
301 * Skip later cells in a span,
302 * figure out whether to start a span,
303 * and advance to next data cell.
312 if (ic
|| sp
->layout
->first
->pos
318 * Print one line of text in the cell
319 * and remember whether there is more.
323 if (tp
->tcol
->col
< tp
->tcol
->lastcol
)
325 if (tp
->tcol
->col
< tp
->tcol
->lastcol
)
329 * Vertical frames between data cells,
330 * but not after the last column.
333 if (fc
== ' ' && ((vert
== 0 &&
334 (cp
== NULL
|| !IS_HORIZ(cp
))) ||
335 tp
->tcol
+ 1 == tp
->tcols
+ tp
->lasttcol
))
338 if (tp
->viscol
< tp
->tcol
->rmargin
) {
339 (*tp
->advance
)(tp
, tp
->tcol
->rmargin
341 tp
->viscol
= tp
->tcol
->rmargin
;
343 while (tp
->viscol
< tp
->tcol
->rmargin
+
344 tp
->tbl
.cols
[ic
].spacing
/ 2) {
345 (*tp
->letter
)(tp
, fc
);
349 if (tp
->tcol
+ 1 == tp
->tcols
+ tp
->lasttcol
)
352 if (fc
== ' ' && cp
!= NULL
) {
357 case TBL_CELL_DHORIZ
:
364 if (tp
->tbl
.cols
[ic
].spacing
) {
365 (*tp
->letter
)(tp
, fc
== ' ' ? '|' :
372 cp
->pos
== TBL_CELL_HORIZ
)
374 else if (cp
!= NULL
&&
375 cp
->pos
== TBL_CELL_DHORIZ
)
380 if (tp
->tbl
.cols
[ic
].spacing
> 2 &&
381 (vert
> 1 || fc
!= ' ')) {
382 (*tp
->letter
)(tp
, fc
== ' ' ? '|' :
383 vert
> 1 ? '+' : fc
);
389 /* Print the vertical frame at the end of each row. */
392 if ((sp
->layout
->last
->vert
&&
393 sp
->layout
->last
->col
+ 1 == sp
->opts
->cols
) ||
395 sp
->next
->layout
->last
->vert
&&
396 sp
->next
->layout
->last
->col
+ 1 == sp
->opts
->cols
) ||
398 sp
->prev
->layout
->last
->vert
&&
399 sp
->prev
->layout
->last
->col
+ 1 == sp
->opts
->cols
&&
400 (horiz
|| (IS_HORIZ(sp
->layout
->last
) &&
401 !IS_HORIZ(sp
->prev
->layout
->last
)))) ||
402 (sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
)))
403 fc
= horiz
|| IS_HORIZ(sp
->layout
->last
) ? '+' : '|';
404 else if (horiz
&& sp
->opts
->rvert
)
407 if (horiz
== 0 && (IS_HORIZ(sp
->layout
->last
) == 0 ||
408 sp
->layout
->last
->col
+ 1 < sp
->opts
->cols
)) {
411 tp
->tcol
->offset
> tp
->viscol
?
412 tp
->tcol
->offset
- tp
->viscol
: 1);
414 (*tp
->letter
)(tp
, fc
);
421 * Clean up after this row. If it is the last line
422 * of the table, print the box line and clean up
423 * column data; otherwise, print the allbox line.
427 tp
->flags
&= ~TERMP_MULTICOL
;
428 tp
->tcol
->rmargin
= tp
->maxrmargin
;
429 if (sp
->next
== NULL
) {
430 if (sp
->opts
->opts
& (TBL_OPT_DBOX
| TBL_OPT_BOX
)) {
431 tbl_hrule(tp
, sp
, 2);
434 if (sp
->opts
->opts
& TBL_OPT_DBOX
) {
435 tbl_hrule(tp
, sp
, 3);
438 assert(tp
->tbl
.cols
);
441 tp
->tcol
->offset
= offset
;
442 } else if (horiz
== 0 && sp
->opts
->opts
& TBL_OPT_ALLBOX
&&
443 (sp
->next
== NULL
|| sp
->next
->pos
== TBL_SPAN_DATA
||
444 sp
->next
->next
!= NULL
))
445 tbl_hrule(tp
, sp
, 1);
447 tp
->flags
&= ~TERMP_NONOSPACE
;
451 * Kinds of horizontal rulers:
452 * 0: inside the table (single or double line with crossings)
453 * 1: inside the table (single or double line with crossings and ends)
454 * 2: inner frame (single line with crossings and ends)
455 * 3: outer frame (single line without crossings with ends)
458 tbl_hrule(struct termp
*tp
, const struct tbl_span
*sp
, int kind
)
460 const struct tbl_cell
*cp
, *cpn
, *cpp
;
461 const struct roffcol
*col
;
465 line
= (kind
< 2 && TBL_SPAN_DHORIZ
== sp
->pos
) ? '=' : '-';
466 cross
= (kind
< 3) ? '+' : '-';
470 cp
= sp
->layout
->first
;
471 cpp
= kind
|| sp
->prev
== NULL
? NULL
: sp
->prev
->layout
->first
;
474 cpn
= kind
> 1 || sp
->next
== NULL
? NULL
: sp
->next
->layout
->first
;
478 col
= tp
->tbl
.cols
+ cp
->col
;
479 tbl_char(tp
, line
, col
->width
+ col
->spacing
/ 2);
481 if ((cp
= cp
->next
) == NULL
)
484 if (vert
< cpp
->vert
)
489 if (vert
< cpn
->vert
)
493 if (sp
->opts
->opts
& TBL_OPT_ALLBOX
&& !vert
)
496 tbl_char(tp
, vert
? cross
: line
, 1);
497 if (col
->spacing
> 2)
498 tbl_char(tp
, vert
> 1 ? cross
: line
, 1);
499 if (col
->spacing
> 4)
500 tbl_char(tp
, line
, (col
->spacing
- 3) / 2);
509 tbl_data(struct termp
*tp
, const struct tbl_opts
*opts
,
510 const struct tbl_cell
*cp
, const struct tbl_dat
*dp
,
511 const struct roffcol
*col
)
515 tbl_char(tp
, '-', col
->width
);
517 case TBL_CELL_DHORIZ
:
518 tbl_char(tp
, '=', col
->width
);
531 case TBL_DATA_NHORIZ
:
532 tbl_char(tp
, '-', col
->width
);
534 case TBL_DATA_NDHORIZ
:
535 case TBL_DATA_DHORIZ
:
536 tbl_char(tp
, '=', col
->width
);
544 case TBL_CELL_CENTRE
:
547 tbl_literal(tp
, dp
, col
);
549 case TBL_CELL_NUMBER
:
550 tbl_number(tp
, opts
, dp
, col
);
561 tbl_char(struct termp
*tp
, char c
, size_t len
)
569 sz
= term_strlen(tp
, cp
);
571 for (i
= 0; i
< len
; i
+= sz
)
576 tbl_literal(struct termp
*tp
, const struct tbl_dat
*dp
,
577 const struct roffcol
*col
)
579 size_t len
, padl
, padr
, width
;
583 len
= term_strlen(tp
, dp
->string
);
585 ic
= dp
->layout
->col
;
588 width
+= tp
->tbl
.cols
[++ic
].width
+ 3;
590 padr
= width
> len
? width
- len
: 0;
593 switch (dp
->layout
->pos
) {
595 padl
= term_len(tp
, 1);
596 padr
= padr
> padl
? padr
- padl
: 0;
598 case TBL_CELL_CENTRE
:
612 tbl_char(tp
, ASCII_NBRSP
, padl
);
614 tbl_char(tp
, ASCII_NBRSP
, padr
);
618 tbl_number(struct termp
*tp
, const struct tbl_opts
*opts
,
619 const struct tbl_dat
*dp
,
620 const struct roffcol
*col
)
624 size_t sz
, psz
, ssz
, d
, padl
;
628 * See calc_data_number(). Left-pad by taking the offset of our
629 * and the maximum decimal; right-pad by the remaining amount.
634 sz
= term_strlen(tp
, dp
->string
);
636 buf
[0] = opts
->decimal
;
639 psz
= term_strlen(tp
, buf
);
641 if ((cp
= strrchr(dp
->string
, opts
->decimal
)) != NULL
) {
642 for (ssz
= 0, i
= 0; cp
!= &dp
->string
[i
]; i
++) {
643 buf
[0] = dp
->string
[i
];
644 ssz
+= term_strlen(tp
, buf
);
650 if (col
->decimal
> d
&& col
->width
> sz
) {
651 padl
= col
->decimal
- d
;
652 if (padl
+ sz
> col
->width
)
653 padl
= col
->width
- sz
;
654 tbl_char(tp
, ASCII_NBRSP
, padl
);
658 if (col
->width
> sz
+ padl
)
659 tbl_char(tp
, ASCII_NBRSP
, col
->width
- sz
- padl
);
663 tbl_word(struct termp
*tp
, const struct tbl_dat
*dp
)
667 prev_font
= tp
->fonti
;
668 if (dp
->layout
->flags
& TBL_CELL_BOLD
)
669 term_fontpush(tp
, TERMFONT_BOLD
);
670 else if (dp
->layout
->flags
& TBL_CELL_ITALIC
)
671 term_fontpush(tp
, TERMFONT_UNDER
);
673 term_word(tp
, dp
->string
);
675 term_fontpopq(tp
, prev_font
);