]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_term.c
1 /* $Id: tbl_term.c,v 1.39 2015/03/06 11:03:03 schwarze Exp $ */
3 * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2012, 2014, 2015 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 void tbl_char(struct termp
*, char, size_t);
34 static void tbl_data(struct termp
*, const struct tbl_opts
*,
35 const struct tbl_dat
*,
36 const struct roffcol
*);
37 static void tbl_literal(struct termp
*, const struct tbl_dat
*,
38 const struct roffcol
*);
39 static void tbl_number(struct termp
*, const struct tbl_opts
*,
40 const struct tbl_dat
*,
41 const struct roffcol
*);
42 static void tbl_hrule(struct termp
*, const struct tbl_span
*, int);
43 static void tbl_word(struct termp
*, const struct tbl_dat
*);
47 term_tbl_strlen(const char *p
, void *arg
)
50 return(term_strlen((const struct termp
*)arg
, p
));
54 term_tbl_len(size_t sz
, void *arg
)
57 return(term_len((const struct termp
*)arg
, sz
));
61 term_tbl(struct termp
*tp
, const struct tbl_span
*sp
)
63 const struct tbl_cell
*cp
;
64 const struct tbl_dat
*dp
;
66 size_t rmargin
, maxrmargin
, tsz
;
67 int ic
, horiz
, spans
, vert
;
69 if (tp
->tbl
.cols
== NULL
)
72 rmargin
= tp
->rmargin
;
73 maxrmargin
= tp
->maxrmargin
;
75 tp
->rmargin
= tp
->maxrmargin
= TERM_MAXMARGIN
;
77 /* Inhibit printing of spaces: we do padding ourselves. */
79 tp
->flags
|= TERMP_NONOSPACE
;
80 tp
->flags
|= TERMP_NOSPACE
;
83 * The first time we're invoked for a given table block,
84 * calculate the table widths and decimal positions.
87 if (tp
->tbl
.cols
== NULL
) {
88 tp
->tbl
.len
= term_tbl_len
;
89 tp
->tbl
.slen
= term_tbl_strlen
;
92 tblcalc(&tp
->tbl
, sp
, rmargin
- tp
->offset
);
94 /* Center the table as a whole. */
97 if (sp
->opts
->opts
& TBL_OPT_CENTRE
) {
98 tsz
= sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
)
99 ? 2 : !!sp
->opts
->lvert
+ !!sp
->opts
->rvert
;
100 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++)
101 tsz
+= tp
->tbl
.cols
[ic
].width
+ 3;
103 if (offset
+ tsz
> rmargin
)
105 tp
->offset
= (offset
+ rmargin
> tsz
) ?
106 (offset
+ rmargin
- tsz
) / 2 : 0;
109 /* Horizontal frame at the start of boxed tables. */
111 if (sp
->opts
->opts
& TBL_OPT_DBOX
)
112 tbl_hrule(tp
, sp
, 2);
113 if (sp
->opts
->opts
& (TBL_OPT_DBOX
| TBL_OPT_BOX
))
114 tbl_hrule(tp
, sp
, 1);
117 /* Vertical frame at the start of each row. */
119 horiz
= sp
->pos
== TBL_SPAN_HORIZ
|| sp
->pos
== TBL_SPAN_DHORIZ
;
121 if (sp
->layout
->vert
||
122 (sp
->prev
!= NULL
&& sp
->prev
->layout
->vert
) ||
123 sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
))
124 term_word(tp
, horiz
? "+" : "|");
125 else if (sp
->opts
->lvert
)
126 tbl_char(tp
, horiz
? '-' : ASCII_NBRSP
, 1);
129 * Now print the actual data itself depending on the span type.
130 * Match data cells to column numbers.
133 if (sp
->pos
== TBL_SPAN_DATA
) {
134 cp
= sp
->layout
->first
;
137 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
140 * Remeber whether we need a vertical bar
144 vert
= cp
== NULL
? 0 : cp
->vert
;
147 * Print the data and advance to the next cell.
151 tbl_data(tp
, sp
->opts
, dp
, tp
->tbl
.cols
+ ic
);
162 * Separate columns, except in the middle
163 * of spans and after the last cell.
166 if (ic
+ 1 == sp
->opts
->cols
|| spans
)
169 tbl_char(tp
, ASCII_NBRSP
, 1);
171 tbl_char(tp
, '|', vert
);
173 tbl_char(tp
, ASCII_NBRSP
, 2 - vert
);
176 tbl_hrule(tp
, sp
, 0);
178 /* Vertical frame at the end of each row. */
180 if (sp
->layout
->last
->vert
||
181 (sp
->prev
!= NULL
&& sp
->prev
->layout
->last
->vert
) ||
182 (sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
)))
183 term_word(tp
, horiz
? "+" : " |");
184 else if (sp
->opts
->rvert
)
185 tbl_char(tp
, horiz
? '-' : ASCII_NBRSP
, 1);
189 * If we're the last row, clean up after ourselves: clear the
190 * existing table configuration and set it to NULL.
193 if (sp
->next
== NULL
) {
194 if (sp
->opts
->opts
& (TBL_OPT_DBOX
| TBL_OPT_BOX
)) {
195 tbl_hrule(tp
, sp
, 1);
198 if (sp
->opts
->opts
& TBL_OPT_DBOX
) {
199 tbl_hrule(tp
, sp
, 2);
202 assert(tp
->tbl
.cols
);
208 tp
->flags
&= ~TERMP_NONOSPACE
;
209 tp
->rmargin
= rmargin
;
210 tp
->maxrmargin
= maxrmargin
;
214 * Kinds of horizontal rulers:
215 * 0: inside the table (single or double line with crossings)
216 * 1: inner frame (single line with crossings and ends)
217 * 2: outer frame (single line without crossings with ends)
220 tbl_hrule(struct termp
*tp
, const struct tbl_span
*sp
, int kind
)
222 const struct tbl_cell
*c1
, *c2
;
226 line
= (kind
== 0 && TBL_SPAN_DHORIZ
== sp
->pos
) ? '=' : '-';
227 cross
= (kind
< 2) ? '+' : '-';
231 c1
= sp
->layout
->first
;
232 c2
= sp
->prev
== NULL
? NULL
: sp
->prev
->layout
->first
;
236 tbl_char(tp
, line
, tp
->tbl
.cols
[c1
->col
].width
+ 1);
238 if ((c1
= c1
->next
) == NULL
)
246 tbl_char(tp
, cross
, vert
);
248 tbl_char(tp
, line
, 2 - vert
);
257 tbl_data(struct termp
*tp
, const struct tbl_opts
*opts
,
258 const struct tbl_dat
*dp
,
259 const struct roffcol
*col
)
263 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
269 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
273 case TBL_DATA_NHORIZ
:
274 tbl_char(tp
, '-', col
->width
);
276 case TBL_DATA_NDHORIZ
:
278 case TBL_DATA_DHORIZ
:
279 tbl_char(tp
, '=', col
->width
);
285 switch (dp
->layout
->pos
) {
287 tbl_char(tp
, '-', col
->width
);
289 case TBL_CELL_DHORIZ
:
290 tbl_char(tp
, '=', col
->width
);
294 case TBL_CELL_CENTRE
:
299 tbl_literal(tp
, dp
, col
);
301 case TBL_CELL_NUMBER
:
302 tbl_number(tp
, opts
, dp
, col
);
305 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
314 tbl_char(struct termp
*tp
, char c
, size_t len
)
322 sz
= term_strlen(tp
, cp
);
324 for (i
= 0; i
< len
; i
+= sz
)
329 tbl_literal(struct termp
*tp
, const struct tbl_dat
*dp
,
330 const struct roffcol
*col
)
332 size_t len
, padl
, padr
, width
;
336 len
= term_strlen(tp
, dp
->string
);
338 ic
= dp
->layout
->col
;
341 width
+= tp
->tbl
.cols
[++ic
].width
+ 3;
343 padr
= width
> len
? width
- len
: 0;
346 switch (dp
->layout
->pos
) {
348 padl
= term_len(tp
, 1);
349 padr
= padr
> padl
? padr
- padl
: 0;
351 case TBL_CELL_CENTRE
:
365 tbl_char(tp
, ASCII_NBRSP
, padl
);
367 tbl_char(tp
, ASCII_NBRSP
, padr
);
371 tbl_number(struct termp
*tp
, const struct tbl_opts
*opts
,
372 const struct tbl_dat
*dp
,
373 const struct roffcol
*col
)
377 size_t sz
, psz
, ssz
, d
, padl
;
381 * See calc_data_number(). Left-pad by taking the offset of our
382 * and the maximum decimal; right-pad by the remaining amount.
387 sz
= term_strlen(tp
, dp
->string
);
389 buf
[0] = opts
->decimal
;
392 psz
= term_strlen(tp
, buf
);
394 if ((cp
= strrchr(dp
->string
, opts
->decimal
)) != NULL
) {
395 for (ssz
= 0, i
= 0; cp
!= &dp
->string
[i
]; i
++) {
396 buf
[0] = dp
->string
[i
];
397 ssz
+= term_strlen(tp
, buf
);
403 if (col
->decimal
> d
&& col
->width
> sz
) {
404 padl
= col
->decimal
- d
;
405 if (padl
+ sz
> col
->width
)
406 padl
= col
->width
- sz
;
407 tbl_char(tp
, ASCII_NBRSP
, padl
);
411 if (col
->width
> sz
+ padl
)
412 tbl_char(tp
, ASCII_NBRSP
, col
->width
- sz
- padl
);
416 tbl_word(struct termp
*tp
, const struct tbl_dat
*dp
)
420 prev_font
= tp
->fonti
;
421 if (dp
->layout
->flags
& TBL_CELL_BOLD
)
422 term_fontpush(tp
, TERMFONT_BOLD
);
423 else if (dp
->layout
->flags
& TBL_CELL_ITALIC
)
424 term_fontpush(tp
, TERMFONT_UNDER
);
426 term_word(tp
, dp
->string
);
428 term_fontpopq(tp
, prev_font
);