]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_term.c
1 /* $Id: tbl_term.c,v 1.56 2017/07/08 13:43:15 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
)
54 return term_hen((const struct termp
*)arg
, su
);
58 term_tbl_strlen(const char *p
, void *arg
)
60 return term_strlen((const struct termp
*)arg
, p
);
64 term_tbl_len(size_t sz
, void *arg
)
66 return term_len((const struct termp
*)arg
, sz
);
70 term_tbl(struct termp
*tp
, const struct tbl_span
*sp
)
72 const struct tbl_cell
*cp
, *cpn
, *cpp
;
73 const struct tbl_dat
*dp
;
76 int ic
, horiz
, spans
, vert
, more
;
79 /* Inhibit printing of spaces: we do padding ourselves. */
81 tp
->flags
|= TERMP_NOSPACE
| TERMP_NONOSPACE
;
84 * The first time we're invoked for a given table block,
85 * calculate the table widths and decimal positions.
88 if (tp
->tbl
.cols
== NULL
) {
89 tp
->tbl
.len
= term_tbl_len
;
90 tp
->tbl
.slen
= term_tbl_strlen
;
91 tp
->tbl
.sulen
= term_tbl_sulen
;
94 tblcalc(&tp
->tbl
, sp
, tp
->tcol
->offset
, tp
->tcol
->rmargin
);
96 /* Tables leak .ta settings to subsequent text. */
98 term_tab_set(tp
, NULL
);
99 coloff
= sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
) ||
101 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
102 coloff
+= tp
->tbl
.cols
[ic
].width
;
103 term_tab_iset(coloff
);
104 coloff
+= tp
->tbl
.cols
[ic
].spacing
;
107 /* Center the table as a whole. */
109 offset
= tp
->tcol
->offset
;
110 if (sp
->opts
->opts
& TBL_OPT_CENTRE
) {
111 tsz
= sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
)
112 ? 2 : !!sp
->opts
->lvert
+ !!sp
->opts
->rvert
;
113 for (ic
= 0; ic
+ 1 < sp
->opts
->cols
; ic
++)
114 tsz
+= tp
->tbl
.cols
[ic
].width
+
115 tp
->tbl
.cols
[ic
].spacing
;
117 tsz
+= tp
->tbl
.cols
[sp
->opts
->cols
- 1].width
;
118 if (offset
+ tsz
> tp
->tcol
->rmargin
)
120 tp
->tcol
->offset
= offset
+ tp
->tcol
->rmargin
> tsz
?
121 (offset
+ tp
->tcol
->rmargin
- tsz
) / 2 : 0;
124 /* Horizontal frame at the start of boxed tables. */
126 if (sp
->opts
->opts
& TBL_OPT_DBOX
)
127 tbl_hrule(tp
, sp
, 3);
128 if (sp
->opts
->opts
& (TBL_OPT_DBOX
| TBL_OPT_BOX
))
129 tbl_hrule(tp
, sp
, 2);
132 /* Set up the columns. */
134 tp
->flags
|= TERMP_MULTICOL
;
138 case TBL_SPAN_DHORIZ
:
143 term_setcol(tp
, sp
->opts
->cols
+ 2);
144 coloff
= tp
->tcol
->offset
;
146 /* Set up a column for a left vertical frame. */
148 if (sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
) ||
151 tp
->tcol
->rmargin
= coloff
;
153 /* Set up the data columns. */
157 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
160 tp
->tcol
->offset
= coloff
;
162 coloff
+= tp
->tbl
.cols
[ic
].width
;
163 tp
->tcol
->rmargin
= coloff
;
164 if (ic
+ 1 < sp
->opts
->cols
)
165 coloff
+= tp
->tbl
.cols
[ic
].spacing
;
173 if (ic
|| sp
->layout
->first
->pos
!= TBL_CELL_SPAN
)
177 /* Set up a column for a right vertical frame. */
180 tp
->tcol
->offset
= coloff
+ 1;
181 tp
->tcol
->rmargin
= tp
->maxrmargin
;
183 /* Spans may have reduced the number of columns. */
185 tp
->lasttcol
= tp
->tcol
- tp
->tcols
;
187 /* Fill the buffers for all data columns. */
189 tp
->tcol
= tp
->tcols
;
190 cp
= cpn
= sp
->layout
->first
;
193 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
204 tbl_data(tp
, sp
->opts
, cp
, dp
, tp
->tbl
.cols
+ ic
);
208 if (cp
->pos
!= TBL_CELL_SPAN
)
215 /* Print the vertical frame at the start of each row. */
217 tp
->tcol
= tp
->tcols
;
219 if (sp
->layout
->vert
||
220 (sp
->next
!= NULL
&& sp
->next
->layout
->vert
&&
221 sp
->next
->pos
== TBL_SPAN_DATA
) ||
222 (sp
->prev
!= NULL
&& sp
->prev
->layout
->vert
&&
223 (horiz
|| (IS_HORIZ(sp
->layout
->first
) &&
224 !IS_HORIZ(sp
->prev
->layout
->first
)))) ||
225 sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
))
226 fc
= horiz
|| IS_HORIZ(sp
->layout
->first
) ? '+' : '|';
227 else if (horiz
&& sp
->opts
->lvert
)
230 (*tp
->advance
)(tp
, tp
->tcols
->offset
);
231 (*tp
->letter
)(tp
, fc
);
232 tp
->viscol
= tp
->tcol
->offset
+ 1;
235 /* Print the data cells. */
239 tbl_hrule(tp
, sp
, 0);
242 cp
= sp
->layout
->first
;
243 cpn
= sp
->next
== NULL
? NULL
:
244 sp
->next
->layout
->first
;
245 cpp
= sp
->prev
== NULL
? NULL
:
246 sp
->prev
->layout
->first
;
249 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
252 * Figure out whether to print a
253 * vertical line after this cell
254 * and advance to next layout cell.
263 case TBL_CELL_DHORIZ
:
281 IS_HORIZ(cp
->next
) &&
282 !IS_HORIZ(cpp
->next
))))
287 sp
->opts
->opts
& TBL_OPT_ALLBOX
)
298 * Skip later cells in a span,
299 * figure out whether to start a span,
300 * and advance to next data cell.
309 if (ic
|| sp
->layout
->first
->pos
315 * Print one line of text in the cell
316 * and remember whether there is more.
320 if (tp
->tcol
->col
< tp
->tcol
->lastcol
)
322 if (tp
->tcol
->col
< tp
->tcol
->lastcol
)
326 * Vertical frames between data cells,
327 * but not after the last column.
330 if (fc
== ' ' && ((vert
== 0 &&
331 (cp
== NULL
|| !IS_HORIZ(cp
))) ||
332 tp
->tcol
+ 1 == tp
->tcols
+ tp
->lasttcol
))
335 if (tp
->viscol
< tp
->tcol
->rmargin
) {
336 (*tp
->advance
)(tp
, tp
->tcol
->rmargin
338 tp
->viscol
= tp
->tcol
->rmargin
;
340 while (tp
->viscol
< tp
->tcol
->rmargin
+
341 tp
->tbl
.cols
[ic
].spacing
/ 2) {
342 (*tp
->letter
)(tp
, fc
);
346 if (tp
->tcol
+ 1 == tp
->tcols
+ tp
->lasttcol
)
349 if (fc
== ' ' && cp
!= NULL
) {
354 case TBL_CELL_DHORIZ
:
361 if (tp
->tbl
.cols
[ic
].spacing
) {
362 (*tp
->letter
)(tp
, fc
== ' ' ? '|' :
369 cp
->pos
== TBL_CELL_HORIZ
)
371 else if (cp
!= NULL
&&
372 cp
->pos
== TBL_CELL_DHORIZ
)
377 if (tp
->tbl
.cols
[ic
].spacing
> 2 &&
378 (vert
> 1 || fc
!= ' ')) {
379 (*tp
->letter
)(tp
, fc
== ' ' ? '|' :
380 vert
> 1 ? '+' : fc
);
386 /* Print the vertical frame at the end of each row. */
389 if ((sp
->layout
->last
->vert
&&
390 sp
->layout
->last
->col
+ 1 == sp
->opts
->cols
) ||
392 sp
->next
->layout
->last
->vert
&&
393 sp
->next
->layout
->last
->col
+ 1 == sp
->opts
->cols
) ||
395 sp
->prev
->layout
->last
->vert
&&
396 sp
->prev
->layout
->last
->col
+ 1 == sp
->opts
->cols
&&
397 (horiz
|| (IS_HORIZ(sp
->layout
->last
) &&
398 !IS_HORIZ(sp
->prev
->layout
->last
)))) ||
399 (sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
)))
400 fc
= horiz
|| IS_HORIZ(sp
->layout
->last
) ? '+' : '|';
401 else if (horiz
&& sp
->opts
->rvert
)
404 if (horiz
== 0 && (IS_HORIZ(sp
->layout
->last
) == 0 ||
405 sp
->layout
->last
->col
+ 1 < sp
->opts
->cols
)) {
408 tp
->tcol
->offset
> tp
->viscol
?
409 tp
->tcol
->offset
- tp
->viscol
: 1);
411 (*tp
->letter
)(tp
, fc
);
418 * Clean up after this row. If it is the last line
419 * of the table, print the box line and clean up
420 * column data; otherwise, print the allbox line.
424 tp
->flags
&= ~TERMP_MULTICOL
;
425 tp
->tcol
->rmargin
= tp
->maxrmargin
;
426 if (sp
->next
== NULL
) {
427 if (sp
->opts
->opts
& (TBL_OPT_DBOX
| TBL_OPT_BOX
)) {
428 tbl_hrule(tp
, sp
, 2);
431 if (sp
->opts
->opts
& TBL_OPT_DBOX
) {
432 tbl_hrule(tp
, sp
, 3);
435 assert(tp
->tbl
.cols
);
438 tp
->tcol
->offset
= offset
;
439 } else if (horiz
== 0 && sp
->opts
->opts
& TBL_OPT_ALLBOX
&&
440 (sp
->next
== NULL
|| sp
->next
->pos
== TBL_SPAN_DATA
||
441 sp
->next
->next
!= NULL
))
442 tbl_hrule(tp
, sp
, 1);
444 tp
->flags
&= ~TERMP_NONOSPACE
;
448 * Kinds of horizontal rulers:
449 * 0: inside the table (single or double line with crossings)
450 * 1: inside the table (single or double line with crossings and ends)
451 * 2: inner frame (single line with crossings and ends)
452 * 3: outer frame (single line without crossings with ends)
455 tbl_hrule(struct termp
*tp
, const struct tbl_span
*sp
, int kind
)
457 const struct tbl_cell
*cp
, *cpn
, *cpp
;
458 const struct roffcol
*col
;
462 line
= (kind
< 2 && TBL_SPAN_DHORIZ
== sp
->pos
) ? '=' : '-';
463 cross
= (kind
< 3) ? '+' : '-';
467 cp
= sp
->layout
->first
;
468 cpp
= kind
|| sp
->prev
== NULL
? NULL
: sp
->prev
->layout
->first
;
471 cpn
= kind
> 1 || sp
->next
== NULL
? NULL
: sp
->next
->layout
->first
;
475 col
= tp
->tbl
.cols
+ cp
->col
;
476 tbl_char(tp
, line
, col
->width
+ col
->spacing
/ 2);
478 if ((cp
= cp
->next
) == NULL
)
481 if (vert
< cpp
->vert
)
486 if (vert
< cpn
->vert
)
490 if (sp
->opts
->opts
& TBL_OPT_ALLBOX
&& !vert
)
493 tbl_char(tp
, vert
? cross
: line
, 1);
494 if (col
->spacing
> 2)
495 tbl_char(tp
, vert
> 1 ? cross
: line
, 1);
496 if (col
->spacing
> 4)
497 tbl_char(tp
, line
, (col
->spacing
- 3) / 2);
506 tbl_data(struct termp
*tp
, const struct tbl_opts
*opts
,
507 const struct tbl_cell
*cp
, const struct tbl_dat
*dp
,
508 const struct roffcol
*col
)
512 tbl_char(tp
, '-', col
->width
);
514 case TBL_CELL_DHORIZ
:
515 tbl_char(tp
, '=', col
->width
);
528 case TBL_DATA_NHORIZ
:
529 tbl_char(tp
, '-', col
->width
);
531 case TBL_DATA_NDHORIZ
:
532 case TBL_DATA_DHORIZ
:
533 tbl_char(tp
, '=', col
->width
);
541 case TBL_CELL_CENTRE
:
544 tbl_literal(tp
, dp
, col
);
546 case TBL_CELL_NUMBER
:
547 tbl_number(tp
, opts
, dp
, col
);
558 tbl_char(struct termp
*tp
, char c
, size_t len
)
566 sz
= term_strlen(tp
, cp
);
568 for (i
= 0; i
< len
; i
+= sz
)
573 tbl_literal(struct termp
*tp
, const struct tbl_dat
*dp
,
574 const struct roffcol
*col
)
576 size_t len
, padl
, padr
, width
;
580 len
= term_strlen(tp
, dp
->string
);
582 ic
= dp
->layout
->col
;
585 width
+= tp
->tbl
.cols
[++ic
].width
+ 3;
587 padr
= width
> len
? width
- len
: 0;
590 switch (dp
->layout
->pos
) {
592 padl
= term_len(tp
, 1);
593 padr
= padr
> padl
? padr
- padl
: 0;
595 case TBL_CELL_CENTRE
:
609 tbl_char(tp
, ASCII_NBRSP
, padl
);
611 tbl_char(tp
, ASCII_NBRSP
, padr
);
615 tbl_number(struct termp
*tp
, const struct tbl_opts
*opts
,
616 const struct tbl_dat
*dp
,
617 const struct roffcol
*col
)
621 size_t sz
, psz
, ssz
, d
, padl
;
625 * See calc_data_number(). Left-pad by taking the offset of our
626 * and the maximum decimal; right-pad by the remaining amount.
631 sz
= term_strlen(tp
, dp
->string
);
633 buf
[0] = opts
->decimal
;
636 psz
= term_strlen(tp
, buf
);
638 if ((cp
= strrchr(dp
->string
, opts
->decimal
)) != NULL
) {
639 for (ssz
= 0, i
= 0; cp
!= &dp
->string
[i
]; i
++) {
640 buf
[0] = dp
->string
[i
];
641 ssz
+= term_strlen(tp
, buf
);
647 if (col
->decimal
> d
&& col
->width
> sz
) {
648 padl
= col
->decimal
- d
;
649 if (padl
+ sz
> col
->width
)
650 padl
= col
->width
- sz
;
651 tbl_char(tp
, ASCII_NBRSP
, padl
);
655 if (col
->width
> sz
+ padl
)
656 tbl_char(tp
, ASCII_NBRSP
, col
->width
- sz
- padl
);
660 tbl_word(struct termp
*tp
, const struct tbl_dat
*dp
)
664 prev_font
= tp
->fonti
;
665 if (dp
->layout
->flags
& TBL_CELL_BOLD
)
666 term_fontpush(tp
, TERMFONT_BOLD
);
667 else if (dp
->layout
->flags
& TBL_CELL_ITALIC
)
668 term_fontpush(tp
, TERMFONT_UNDER
);
670 term_word(tp
, dp
->string
);
672 term_fontpopq(tp
, prev_font
);