]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_term.c
1 /* $Id: tbl_term.c,v 1.54 2017/06/17 14:55:30 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
);
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
< sp
->opts
->cols
; ic
++)
114 tsz
+= tp
->tbl
.cols
[ic
].width
+ 3;
116 if (offset
+ tsz
> tp
->tcol
->rmargin
)
118 tp
->tcol
->offset
= offset
+ tp
->tcol
->rmargin
> tsz
?
119 (offset
+ tp
->tcol
->rmargin
- tsz
) / 2 : 0;
122 /* Horizontal frame at the start of boxed tables. */
124 if (sp
->opts
->opts
& TBL_OPT_DBOX
)
125 tbl_hrule(tp
, sp
, 3);
126 if (sp
->opts
->opts
& (TBL_OPT_DBOX
| TBL_OPT_BOX
))
127 tbl_hrule(tp
, sp
, 2);
130 /* Set up the columns. */
132 tp
->flags
|= TERMP_MULTICOL
;
136 case TBL_SPAN_DHORIZ
:
141 term_setcol(tp
, sp
->opts
->cols
+ 2);
142 coloff
= tp
->tcol
->offset
;
144 /* Set up a column for a left vertical frame. */
146 if (sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
) ||
149 tp
->tcol
->rmargin
= coloff
;
151 /* Set up the data columns. */
155 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
158 tp
->tcol
->offset
= coloff
;
160 coloff
+= tp
->tbl
.cols
[ic
].width
;
161 tp
->tcol
->rmargin
= coloff
;
163 if (ic
+ 1 < sp
->opts
->cols
)
175 /* Set up a column for a right vertical frame. */
178 tp
->tcol
->offset
= coloff
;
179 tp
->tcol
->rmargin
= tp
->maxrmargin
;
181 /* Spans may have reduced the number of columns. */
183 tp
->lasttcol
= tp
->tcol
- tp
->tcols
;
185 /* Fill the buffers for all data columns. */
187 tp
->tcol
= tp
->tcols
;
188 cp
= cpn
= sp
->layout
->first
;
191 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
202 tbl_data(tp
, sp
->opts
, cp
, dp
, tp
->tbl
.cols
+ ic
);
212 /* Print the vertical frame at the start of each row. */
214 tp
->tcol
= tp
->tcols
;
216 if (sp
->layout
->vert
||
217 (sp
->next
!= NULL
&& sp
->next
->layout
->vert
&&
218 sp
->next
->pos
== TBL_SPAN_DATA
) ||
219 (sp
->prev
!= NULL
&& sp
->prev
->layout
->vert
&&
220 (horiz
|| (IS_HORIZ(sp
->layout
->first
) &&
221 !IS_HORIZ(sp
->prev
->layout
->first
)))) ||
222 sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
))
223 fc
= horiz
|| IS_HORIZ(sp
->layout
->first
) ? '+' : '|';
224 else if (horiz
&& sp
->opts
->lvert
)
227 (*tp
->advance
)(tp
, tp
->tcols
->offset
);
228 (*tp
->letter
)(tp
, fc
);
229 tp
->viscol
= tp
->tcol
->offset
+ 1;
232 /* Print the data cells. */
236 tbl_hrule(tp
, sp
, 0);
239 cp
= sp
->layout
->first
;
240 cpn
= sp
->next
== NULL
? NULL
:
241 sp
->next
->layout
->first
;
242 cpp
= sp
->prev
== NULL
? NULL
:
243 sp
->prev
->layout
->first
;
246 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
249 * Figure out whether to print a
250 * vertical line after this cell
251 * and advance to next layout cell.
260 case TBL_CELL_DHORIZ
:
278 IS_HORIZ(cp
->next
) &&
279 !IS_HORIZ(cpp
->next
))))
284 sp
->opts
->opts
& TBL_OPT_ALLBOX
)
295 * Skip later cells in a span,
296 * figure out whether to start a span,
297 * and advance to next data cell.
310 * Print one line of text in the cell
311 * and remember whether there is more.
315 if (tp
->tcol
->col
< tp
->tcol
->lastcol
)
317 if (tp
->tcol
->col
< tp
->tcol
->lastcol
)
321 * Vertical frames between data cells,
322 * but not after the last column.
325 if (fc
== ' ' && ((vert
== 0 &&
326 (cp
== NULL
|| !IS_HORIZ(cp
))) ||
327 tp
->tcol
+ 1 == tp
->tcols
+ tp
->lasttcol
))
330 if (tp
->tcol
->rmargin
> tp
->viscol
) {
331 (*tp
->advance
)(tp
, tp
->tcol
->rmargin
333 tp
->viscol
= tp
->tcol
->rmargin
;
336 if (tp
->tcol
->rmargin
+ 1 > tp
->viscol
) {
337 (*tp
->letter
)(tp
, fc
);
341 if (tp
->tcol
+ 1 == tp
->tcols
+ tp
->lasttcol
)
344 if (fc
== ' ' && cp
!= NULL
) {
349 case TBL_CELL_DHORIZ
:
358 fc
== ' ' ? '|' : vert
? '+' : fc
);
363 cp
->pos
== TBL_CELL_HORIZ
)
365 else if (cp
!= NULL
&&
366 cp
->pos
== TBL_CELL_DHORIZ
)
371 if (vert
> 1 || fc
!= ' ') {
372 (*tp
->letter
)(tp
, fc
== ' ' ? '|' :
373 vert
> 1 ? '+' : fc
);
379 /* Print the vertical frame at the end of each row. */
382 if ((sp
->layout
->last
->vert
&&
383 sp
->layout
->last
->col
+ 1 == sp
->opts
->cols
) ||
385 sp
->next
->layout
->last
->vert
&&
386 sp
->next
->layout
->last
->col
+ 1 == sp
->opts
->cols
) ||
388 sp
->prev
->layout
->last
->vert
&&
389 sp
->prev
->layout
->last
->col
+ 1 == sp
->opts
->cols
&&
390 (horiz
|| (IS_HORIZ(sp
->layout
->last
) &&
391 !IS_HORIZ(sp
->prev
->layout
->last
)))) ||
392 (sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
)))
393 fc
= horiz
|| IS_HORIZ(sp
->layout
->last
) ? '+' : '|';
394 else if (horiz
&& sp
->opts
->rvert
)
397 if (horiz
== 0 && (IS_HORIZ(sp
->layout
->last
) == 0 ||
398 sp
->layout
->last
->col
+ 1 < sp
->opts
->cols
)) {
401 tp
->tcol
->offset
> tp
->viscol
?
402 tp
->tcol
->offset
- tp
->viscol
: 1);
404 (*tp
->letter
)(tp
, fc
);
411 * Clean up after this row. If it is the last line
412 * of the table, print the box line and clean up
413 * column data; otherwise, print the allbox line.
417 tp
->flags
&= ~TERMP_MULTICOL
;
418 tp
->tcol
->rmargin
= tp
->maxrmargin
;
419 if (sp
->next
== NULL
) {
420 if (sp
->opts
->opts
& (TBL_OPT_DBOX
| TBL_OPT_BOX
)) {
421 tbl_hrule(tp
, sp
, 2);
424 if (sp
->opts
->opts
& TBL_OPT_DBOX
) {
425 tbl_hrule(tp
, sp
, 3);
428 assert(tp
->tbl
.cols
);
431 tp
->tcol
->offset
= offset
;
432 } else if (horiz
== 0 && sp
->opts
->opts
& TBL_OPT_ALLBOX
&&
433 (sp
->next
== NULL
|| sp
->next
->pos
== TBL_SPAN_DATA
||
434 sp
->next
->next
!= NULL
))
435 tbl_hrule(tp
, sp
, 1);
437 tp
->flags
&= ~TERMP_NONOSPACE
;
441 * Kinds of horizontal rulers:
442 * 0: inside the table (single or double line with crossings)
443 * 1: inside the table (single or double line with crossings and ends)
444 * 2: inner frame (single line with crossings and ends)
445 * 3: outer frame (single line without crossings with ends)
448 tbl_hrule(struct termp
*tp
, const struct tbl_span
*sp
, int kind
)
450 const struct tbl_cell
*cp
, *cpn
, *cpp
;
454 line
= (kind
< 2 && TBL_SPAN_DHORIZ
== sp
->pos
) ? '=' : '-';
455 cross
= (kind
< 3) ? '+' : '-';
459 cp
= sp
->layout
->first
;
460 cpp
= kind
|| sp
->prev
== NULL
? NULL
: sp
->prev
->layout
->first
;
463 cpn
= kind
> 1 || sp
->next
== NULL
? NULL
: sp
->next
->layout
->first
;
467 tbl_char(tp
, line
, tp
->tbl
.cols
[cp
->col
].width
+ 1);
469 if ((cp
= cp
->next
) == NULL
)
472 if (vert
< cpp
->vert
)
477 if (vert
< cpn
->vert
)
481 if (sp
->opts
->opts
& TBL_OPT_ALLBOX
&& !vert
)
484 tbl_char(tp
, cross
, vert
);
486 tbl_char(tp
, line
, 2 - vert
);
495 tbl_data(struct termp
*tp
, const struct tbl_opts
*opts
,
496 const struct tbl_cell
*cp
, const struct tbl_dat
*dp
,
497 const struct roffcol
*col
)
501 tbl_char(tp
, '-', col
->width
);
503 case TBL_CELL_DHORIZ
:
504 tbl_char(tp
, '=', col
->width
);
511 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
517 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
520 case TBL_DATA_NHORIZ
:
521 tbl_char(tp
, '-', col
->width
);
523 case TBL_DATA_NDHORIZ
:
524 case TBL_DATA_DHORIZ
:
525 tbl_char(tp
, '=', col
->width
);
533 case TBL_CELL_CENTRE
:
536 tbl_literal(tp
, dp
, col
);
538 case TBL_CELL_NUMBER
:
539 tbl_number(tp
, opts
, dp
, col
);
542 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
550 tbl_char(struct termp
*tp
, char c
, size_t len
)
558 sz
= term_strlen(tp
, cp
);
560 for (i
= 0; i
< len
; i
+= sz
)
565 tbl_literal(struct termp
*tp
, const struct tbl_dat
*dp
,
566 const struct roffcol
*col
)
568 size_t len
, padl
, padr
, width
;
572 len
= term_strlen(tp
, dp
->string
);
574 ic
= dp
->layout
->col
;
577 width
+= tp
->tbl
.cols
[++ic
].width
+ 3;
579 padr
= width
> len
? width
- len
: 0;
582 switch (dp
->layout
->pos
) {
584 padl
= term_len(tp
, 1);
585 padr
= padr
> padl
? padr
- padl
: 0;
587 case TBL_CELL_CENTRE
:
601 tbl_char(tp
, ASCII_NBRSP
, padl
);
603 tbl_char(tp
, ASCII_NBRSP
, padr
);
607 tbl_number(struct termp
*tp
, const struct tbl_opts
*opts
,
608 const struct tbl_dat
*dp
,
609 const struct roffcol
*col
)
613 size_t sz
, psz
, ssz
, d
, padl
;
617 * See calc_data_number(). Left-pad by taking the offset of our
618 * and the maximum decimal; right-pad by the remaining amount.
623 sz
= term_strlen(tp
, dp
->string
);
625 buf
[0] = opts
->decimal
;
628 psz
= term_strlen(tp
, buf
);
630 if ((cp
= strrchr(dp
->string
, opts
->decimal
)) != NULL
) {
631 for (ssz
= 0, i
= 0; cp
!= &dp
->string
[i
]; i
++) {
632 buf
[0] = dp
->string
[i
];
633 ssz
+= term_strlen(tp
, buf
);
639 if (col
->decimal
> d
&& col
->width
> sz
) {
640 padl
= col
->decimal
- d
;
641 if (padl
+ sz
> col
->width
)
642 padl
= col
->width
- sz
;
643 tbl_char(tp
, ASCII_NBRSP
, padl
);
647 if (col
->width
> sz
+ padl
)
648 tbl_char(tp
, ASCII_NBRSP
, col
->width
- sz
- padl
);
652 tbl_word(struct termp
*tp
, const struct tbl_dat
*dp
)
656 prev_font
= tp
->fonti
;
657 if (dp
->layout
->flags
& TBL_CELL_BOLD
)
658 term_fontpush(tp
, TERMFONT_BOLD
);
659 else if (dp
->layout
->flags
& TBL_CELL_ITALIC
)
660 term_fontpush(tp
, TERMFONT_UNDER
);
662 term_word(tp
, dp
->string
);
664 term_fontpopq(tp
, prev_font
);