]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_term.c
1 /* $Id: tbl_term.c,v 1.51 2017/06/13 14:39:13 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
, more
;
75 /* Inhibit printing of spaces: we do padding ourselves. */
77 tp
->flags
|= TERMP_NOSPACE
| TERMP_NONOSPACE
;
80 * The first time we're invoked for a given table block,
81 * calculate the table widths and decimal positions.
84 if (tp
->tbl
.cols
== NULL
) {
85 tp
->tbl
.len
= term_tbl_len
;
86 tp
->tbl
.slen
= term_tbl_strlen
;
87 tp
->tbl
.sulen
= term_tbl_sulen
;
90 tblcalc(&tp
->tbl
, sp
, tp
->tcol
->offset
, tp
->tcol
->rmargin
);
92 /* Center the table as a whole. */
94 offset
= tp
->tcol
->offset
;
95 if (sp
->opts
->opts
& TBL_OPT_CENTRE
) {
96 tsz
= sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
)
97 ? 2 : !!sp
->opts
->lvert
+ !!sp
->opts
->rvert
;
98 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++)
99 tsz
+= tp
->tbl
.cols
[ic
].width
+ 3;
101 if (offset
+ tsz
> tp
->tcol
->rmargin
)
103 tp
->tcol
->offset
= offset
+ tp
->tcol
->rmargin
> tsz
?
104 (offset
+ tp
->tcol
->rmargin
- tsz
) / 2 : 0;
107 /* Horizontal frame at the start of boxed tables. */
109 if (sp
->opts
->opts
& TBL_OPT_DBOX
)
110 tbl_hrule(tp
, sp
, 2);
111 if (sp
->opts
->opts
& (TBL_OPT_DBOX
| TBL_OPT_BOX
))
112 tbl_hrule(tp
, sp
, 1);
115 /* Set up the columns. */
117 tp
->flags
|= TERMP_MULTICOL
;
121 case TBL_SPAN_DHORIZ
:
126 term_setcol(tp
, sp
->opts
->cols
+ 2);
127 coloff
= tp
->tcol
->offset
;
129 /* Set up a column for a left vertical frame. */
131 if (sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
) ||
134 tp
->tcol
->rmargin
= coloff
;
136 /* Set up the data columns. */
140 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
143 tp
->tcol
->offset
= coloff
;
145 coloff
+= tp
->tbl
.cols
[ic
].width
;
146 tp
->tcol
->rmargin
= coloff
;
148 if (ic
+ 1 < sp
->opts
->cols
)
160 /* Set up a column for a right vertical frame. */
163 tp
->tcol
->offset
= coloff
;
164 if (sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
) ||
167 tp
->tcol
->rmargin
= coloff
;
169 /* Spans may have reduced the number of columns. */
171 tp
->lasttcol
= tp
->tcol
- tp
->tcols
;
173 /* Fill the buffers for all data columns. */
175 tp
->tcol
= tp
->tcols
;
178 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
185 tbl_data(tp
, sp
->opts
, dp
, tp
->tbl
.cols
+ ic
);
195 /* Print the vertical frame at the start of each row. */
197 tp
->tcol
= tp
->tcols
;
199 if (sp
->layout
->vert
||
200 (sp
->prev
!= NULL
&& sp
->prev
->layout
->vert
) ||
201 sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
))
202 fc
= horiz
? '+' : '|';
203 else if (horiz
&& sp
->opts
->lvert
)
206 (*tp
->advance
)(tp
, tp
->tcols
->offset
);
207 (*tp
->letter
)(tp
, fc
);
208 tp
->viscol
= tp
->tcol
->offset
+ 1;
211 /* Print the data cells. */
215 tbl_hrule(tp
, sp
, 0);
218 cp
= sp
->layout
->first
;
221 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
223 /* Advance to next layout cell. */
231 /* Skip later cells in a span. */
238 /* Advance to next data cell. */
245 /* Print one line of text in the cell. */
248 if (tp
->tcol
->col
< tp
->tcol
->lastcol
)
250 if (tp
->tcol
->col
< tp
->tcol
->lastcol
)
254 * Vertical frames between data cells,
255 * but not after the last column.
258 if (tp
->tcol
+ 1 == tp
->tcols
+ tp
->lasttcol
)
261 sp
->opts
->opts
& TBL_OPT_ALLBOX
)
266 if (tp
->tcol
->rmargin
+ 1 > tp
->viscol
) {
267 (*tp
->advance
)(tp
, tp
->tcol
->rmargin
269 tp
->viscol
= tp
->tcol
->rmargin
+ 1;
272 (*tp
->letter
)(tp
, '|');
278 /* Print the vertical frame at the end of each row. */
281 if (sp
->layout
->last
->vert
||
282 (sp
->prev
!= NULL
&& sp
->prev
->layout
->last
->vert
) ||
283 (sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
)))
284 fc
= horiz
? '+' : '|';
285 else if (horiz
&& sp
->opts
->rvert
)
291 tp
->tcol
->offset
> tp
->viscol
?
292 tp
->tcol
->offset
- tp
->viscol
: 1);
294 (*tp
->letter
)(tp
, fc
);
301 * If we're the last row, clean up after ourselves: clear the
302 * existing table configuration and set it to NULL.
306 tp
->flags
&= ~TERMP_MULTICOL
;
307 tp
->tcol
->rmargin
= tp
->maxrmargin
;
308 if (sp
->next
== NULL
) {
309 if (sp
->opts
->opts
& (TBL_OPT_DBOX
| TBL_OPT_BOX
)) {
310 tbl_hrule(tp
, sp
, 1);
313 if (sp
->opts
->opts
& TBL_OPT_DBOX
) {
314 tbl_hrule(tp
, sp
, 2);
317 assert(tp
->tbl
.cols
);
320 tp
->tcol
->offset
= offset
;
321 } else if (horiz
== 0 && sp
->opts
->opts
& TBL_OPT_ALLBOX
&&
322 (sp
->next
== NULL
|| sp
->next
->pos
== TBL_SPAN_DATA
||
323 sp
->next
->next
!= NULL
))
324 tbl_hrule(tp
, sp
, 1);
326 tp
->flags
&= ~TERMP_NONOSPACE
;
330 * Kinds of horizontal rulers:
331 * 0: inside the table (single or double line with crossings)
332 * 1: inner frame (single line with crossings and ends)
333 * 2: outer frame (single line without crossings with ends)
336 tbl_hrule(struct termp
*tp
, const struct tbl_span
*sp
, int kind
)
338 const struct tbl_cell
*c1
, *c2
;
342 line
= (kind
== 0 && TBL_SPAN_DHORIZ
== sp
->pos
) ? '=' : '-';
343 cross
= (kind
< 2) ? '+' : '-';
347 c1
= sp
->layout
->first
;
348 c2
= sp
->prev
== NULL
? NULL
: sp
->prev
->layout
->first
;
352 tbl_char(tp
, line
, tp
->tbl
.cols
[c1
->col
].width
+ 1);
354 if ((c1
= c1
->next
) == NULL
)
361 if (sp
->opts
->opts
& TBL_OPT_ALLBOX
&& !vert
)
364 tbl_char(tp
, cross
, vert
);
366 tbl_char(tp
, line
, 2 - vert
);
375 tbl_data(struct termp
*tp
, const struct tbl_opts
*opts
,
376 const struct tbl_dat
*dp
,
377 const struct roffcol
*col
)
381 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
387 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
390 case TBL_DATA_NHORIZ
:
391 tbl_char(tp
, '-', col
->width
);
393 case TBL_DATA_NDHORIZ
:
394 case TBL_DATA_DHORIZ
:
395 tbl_char(tp
, '=', col
->width
);
401 switch (dp
->layout
->pos
) {
403 tbl_char(tp
, '-', col
->width
);
405 case TBL_CELL_DHORIZ
:
406 tbl_char(tp
, '=', col
->width
);
409 case TBL_CELL_CENTRE
:
412 tbl_literal(tp
, dp
, col
);
414 case TBL_CELL_NUMBER
:
415 tbl_number(tp
, opts
, dp
, col
);
418 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
426 tbl_char(struct termp
*tp
, char c
, size_t len
)
434 sz
= term_strlen(tp
, cp
);
436 for (i
= 0; i
< len
; i
+= sz
)
441 tbl_literal(struct termp
*tp
, const struct tbl_dat
*dp
,
442 const struct roffcol
*col
)
444 size_t len
, padl
, padr
, width
;
448 len
= term_strlen(tp
, dp
->string
);
450 ic
= dp
->layout
->col
;
453 width
+= tp
->tbl
.cols
[++ic
].width
+ 3;
455 padr
= width
> len
? width
- len
: 0;
458 switch (dp
->layout
->pos
) {
460 padl
= term_len(tp
, 1);
461 padr
= padr
> padl
? padr
- padl
: 0;
463 case TBL_CELL_CENTRE
:
477 tbl_char(tp
, ASCII_NBRSP
, padl
);
479 tbl_char(tp
, ASCII_NBRSP
, padr
);
483 tbl_number(struct termp
*tp
, const struct tbl_opts
*opts
,
484 const struct tbl_dat
*dp
,
485 const struct roffcol
*col
)
489 size_t sz
, psz
, ssz
, d
, padl
;
493 * See calc_data_number(). Left-pad by taking the offset of our
494 * and the maximum decimal; right-pad by the remaining amount.
499 sz
= term_strlen(tp
, dp
->string
);
501 buf
[0] = opts
->decimal
;
504 psz
= term_strlen(tp
, buf
);
506 if ((cp
= strrchr(dp
->string
, opts
->decimal
)) != NULL
) {
507 for (ssz
= 0, i
= 0; cp
!= &dp
->string
[i
]; i
++) {
508 buf
[0] = dp
->string
[i
];
509 ssz
+= term_strlen(tp
, buf
);
515 if (col
->decimal
> d
&& col
->width
> sz
) {
516 padl
= col
->decimal
- d
;
517 if (padl
+ sz
> col
->width
)
518 padl
= col
->width
- sz
;
519 tbl_char(tp
, ASCII_NBRSP
, padl
);
523 if (col
->width
> sz
+ padl
)
524 tbl_char(tp
, ASCII_NBRSP
, col
->width
- sz
- padl
);
528 tbl_word(struct termp
*tp
, const struct tbl_dat
*dp
)
532 prev_font
= tp
->fonti
;
533 if (dp
->layout
->flags
& TBL_CELL_BOLD
)
534 term_fontpush(tp
, TERMFONT_BOLD
);
535 else if (dp
->layout
->flags
& TBL_CELL_ITALIC
)
536 term_fontpush(tp
, TERMFONT_UNDER
);
538 term_word(tp
, dp
->string
);
540 term_fontpopq(tp
, prev_font
);