]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_term.c
1 /* $Id: tbl_term.c,v 1.50 2017/06/12 22:49:16 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
++) {
228 if (tp
->tcol
->col
< tp
->tcol
->lastcol
)
230 if (tp
->tcol
->col
< tp
->tcol
->lastcol
)
233 tp
->tcols
+ tp
->lasttcol
)
238 /* Vertical frames between data cells. */
246 sp
->opts
->opts
& TBL_OPT_ALLBOX
)
251 if (tp
->tcol
->rmargin
+ 1 > tp
->viscol
) {
252 (*tp
->advance
)(tp
, tp
->tcol
->rmargin
254 tp
->viscol
= tp
->tcol
->rmargin
+ 1;
257 (*tp
->letter
)(tp
, '|');
263 /* Print the vertical frame at the end of each row. */
266 if (sp
->layout
->last
->vert
||
267 (sp
->prev
!= NULL
&& sp
->prev
->layout
->last
->vert
) ||
268 (sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
)))
269 fc
= horiz
? '+' : '|';
270 else if (horiz
&& sp
->opts
->rvert
)
276 tp
->tcol
->offset
> tp
->viscol
?
277 tp
->tcol
->offset
- tp
->viscol
: 1);
279 (*tp
->letter
)(tp
, fc
);
286 * If we're the last row, clean up after ourselves: clear the
287 * existing table configuration and set it to NULL.
291 tp
->flags
&= ~TERMP_MULTICOL
;
292 tp
->tcol
->rmargin
= tp
->maxrmargin
;
293 if (sp
->next
== NULL
) {
294 if (sp
->opts
->opts
& (TBL_OPT_DBOX
| TBL_OPT_BOX
)) {
295 tbl_hrule(tp
, sp
, 1);
298 if (sp
->opts
->opts
& TBL_OPT_DBOX
) {
299 tbl_hrule(tp
, sp
, 2);
302 assert(tp
->tbl
.cols
);
305 tp
->tcol
->offset
= offset
;
306 } else if (horiz
== 0 && sp
->opts
->opts
& TBL_OPT_ALLBOX
&&
307 (sp
->next
== NULL
|| sp
->next
->pos
== TBL_SPAN_DATA
||
308 sp
->next
->next
!= NULL
))
309 tbl_hrule(tp
, sp
, 1);
311 tp
->flags
&= ~TERMP_NONOSPACE
;
315 * Kinds of horizontal rulers:
316 * 0: inside the table (single or double line with crossings)
317 * 1: inner frame (single line with crossings and ends)
318 * 2: outer frame (single line without crossings with ends)
321 tbl_hrule(struct termp
*tp
, const struct tbl_span
*sp
, int kind
)
323 const struct tbl_cell
*c1
, *c2
;
327 line
= (kind
== 0 && TBL_SPAN_DHORIZ
== sp
->pos
) ? '=' : '-';
328 cross
= (kind
< 2) ? '+' : '-';
332 c1
= sp
->layout
->first
;
333 c2
= sp
->prev
== NULL
? NULL
: sp
->prev
->layout
->first
;
337 tbl_char(tp
, line
, tp
->tbl
.cols
[c1
->col
].width
+ 1);
339 if ((c1
= c1
->next
) == NULL
)
346 if (sp
->opts
->opts
& TBL_OPT_ALLBOX
&& !vert
)
349 tbl_char(tp
, cross
, vert
);
351 tbl_char(tp
, line
, 2 - vert
);
360 tbl_data(struct termp
*tp
, const struct tbl_opts
*opts
,
361 const struct tbl_dat
*dp
,
362 const struct roffcol
*col
)
366 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
372 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
375 case TBL_DATA_NHORIZ
:
376 tbl_char(tp
, '-', col
->width
);
378 case TBL_DATA_NDHORIZ
:
379 case TBL_DATA_DHORIZ
:
380 tbl_char(tp
, '=', col
->width
);
386 switch (dp
->layout
->pos
) {
388 tbl_char(tp
, '-', col
->width
);
390 case TBL_CELL_DHORIZ
:
391 tbl_char(tp
, '=', col
->width
);
394 case TBL_CELL_CENTRE
:
397 tbl_literal(tp
, dp
, col
);
399 case TBL_CELL_NUMBER
:
400 tbl_number(tp
, opts
, dp
, col
);
403 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
411 tbl_char(struct termp
*tp
, char c
, size_t len
)
419 sz
= term_strlen(tp
, cp
);
421 for (i
= 0; i
< len
; i
+= sz
)
426 tbl_literal(struct termp
*tp
, const struct tbl_dat
*dp
,
427 const struct roffcol
*col
)
429 size_t len
, padl
, padr
, width
;
433 len
= term_strlen(tp
, dp
->string
);
435 ic
= dp
->layout
->col
;
438 width
+= tp
->tbl
.cols
[++ic
].width
+ 3;
440 padr
= width
> len
? width
- len
: 0;
443 switch (dp
->layout
->pos
) {
445 padl
= term_len(tp
, 1);
446 padr
= padr
> padl
? padr
- padl
: 0;
448 case TBL_CELL_CENTRE
:
462 tbl_char(tp
, ASCII_NBRSP
, padl
);
464 tbl_char(tp
, ASCII_NBRSP
, padr
);
468 tbl_number(struct termp
*tp
, const struct tbl_opts
*opts
,
469 const struct tbl_dat
*dp
,
470 const struct roffcol
*col
)
474 size_t sz
, psz
, ssz
, d
, padl
;
478 * See calc_data_number(). Left-pad by taking the offset of our
479 * and the maximum decimal; right-pad by the remaining amount.
484 sz
= term_strlen(tp
, dp
->string
);
486 buf
[0] = opts
->decimal
;
489 psz
= term_strlen(tp
, buf
);
491 if ((cp
= strrchr(dp
->string
, opts
->decimal
)) != NULL
) {
492 for (ssz
= 0, i
= 0; cp
!= &dp
->string
[i
]; i
++) {
493 buf
[0] = dp
->string
[i
];
494 ssz
+= term_strlen(tp
, buf
);
500 if (col
->decimal
> d
&& col
->width
> sz
) {
501 padl
= col
->decimal
- d
;
502 if (padl
+ sz
> col
->width
)
503 padl
= col
->width
- sz
;
504 tbl_char(tp
, ASCII_NBRSP
, padl
);
508 if (col
->width
> sz
+ padl
)
509 tbl_char(tp
, ASCII_NBRSP
, col
->width
- sz
- padl
);
513 tbl_word(struct termp
*tp
, const struct tbl_dat
*dp
)
517 prev_font
= tp
->fonti
;
518 if (dp
->layout
->flags
& TBL_CELL_BOLD
)
519 term_fontpush(tp
, TERMFONT_BOLD
);
520 else if (dp
->layout
->flags
& TBL_CELL_ITALIC
)
521 term_fontpush(tp
, TERMFONT_UNDER
);
523 term_word(tp
, dp
->string
);
525 term_fontpopq(tp
, prev_font
);