]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_term.c
1 /* $Id: tbl_term.c,v 1.38 2015/01/31 00:12:41 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 rmargin
= tp
->rmargin
;
70 maxrmargin
= tp
->maxrmargin
;
72 tp
->rmargin
= tp
->maxrmargin
= TERM_MAXMARGIN
;
74 /* Inhibit printing of spaces: we do padding ourselves. */
76 tp
->flags
|= TERMP_NONOSPACE
;
77 tp
->flags
|= TERMP_NOSPACE
;
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
) {
87 tp
->tbl
.len
= term_tbl_len
;
88 tp
->tbl
.slen
= term_tbl_strlen
;
91 tblcalc(&tp
->tbl
, sp
, rmargin
- tp
->offset
);
93 /* Center the table as a whole. */
96 if (sp
->opts
->opts
& TBL_OPT_CENTRE
) {
97 tsz
= sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
)
98 ? 2 : !!sp
->opts
->lvert
+ !!sp
->opts
->rvert
;
99 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++)
100 tsz
+= tp
->tbl
.cols
[ic
].width
+ 3;
102 if (offset
+ tsz
> rmargin
)
104 tp
->offset
= (offset
+ rmargin
> tsz
) ?
105 (offset
+ rmargin
- tsz
) / 2 : 0;
108 /* Horizontal frame at the start of boxed tables. */
110 if (sp
->opts
->opts
& TBL_OPT_DBOX
)
111 tbl_hrule(tp
, sp
, 2);
112 if (sp
->opts
->opts
& (TBL_OPT_DBOX
| TBL_OPT_BOX
))
113 tbl_hrule(tp
, sp
, 1);
116 /* Vertical frame at the start of each row. */
118 horiz
= sp
->pos
== TBL_SPAN_HORIZ
|| sp
->pos
== TBL_SPAN_DHORIZ
;
120 if (sp
->layout
->vert
||
121 (sp
->prev
!= NULL
&& sp
->prev
->layout
->vert
) ||
122 sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
))
123 term_word(tp
, horiz
? "+" : "|");
124 else if (sp
->opts
->lvert
)
125 tbl_char(tp
, horiz
? '-' : ASCII_NBRSP
, 1);
128 * Now print the actual data itself depending on the span type.
129 * Match data cells to column numbers.
132 if (sp
->pos
== TBL_SPAN_DATA
) {
133 cp
= sp
->layout
->first
;
136 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
139 * Remeber whether we need a vertical bar
143 vert
= cp
== NULL
? 0 : cp
->vert
;
146 * Print the data and advance to the next cell.
150 tbl_data(tp
, sp
->opts
, dp
, tp
->tbl
.cols
+ ic
);
161 * Separate columns, except in the middle
162 * of spans and after the last cell.
165 if (ic
+ 1 == sp
->opts
->cols
|| spans
)
168 tbl_char(tp
, ASCII_NBRSP
, 1);
170 tbl_char(tp
, '|', vert
);
172 tbl_char(tp
, ASCII_NBRSP
, 2 - vert
);
175 tbl_hrule(tp
, sp
, 0);
177 /* Vertical frame at the end of each row. */
179 if (sp
->layout
->last
->vert
||
180 (sp
->prev
!= NULL
&& sp
->prev
->layout
->last
->vert
) ||
181 (sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
)))
182 term_word(tp
, horiz
? "+" : " |");
183 else if (sp
->opts
->rvert
)
184 tbl_char(tp
, horiz
? '-' : ASCII_NBRSP
, 1);
188 * If we're the last row, clean up after ourselves: clear the
189 * existing table configuration and set it to NULL.
192 if (sp
->next
== NULL
) {
193 if (sp
->opts
->opts
& (TBL_OPT_DBOX
| TBL_OPT_BOX
)) {
194 tbl_hrule(tp
, sp
, 1);
197 if (sp
->opts
->opts
& TBL_OPT_DBOX
) {
198 tbl_hrule(tp
, sp
, 2);
201 assert(tp
->tbl
.cols
);
207 tp
->flags
&= ~TERMP_NONOSPACE
;
208 tp
->rmargin
= rmargin
;
209 tp
->maxrmargin
= maxrmargin
;
213 * Kinds of horizontal rulers:
214 * 0: inside the table (single or double line with crossings)
215 * 1: inner frame (single line with crossings and ends)
216 * 2: outer frame (single line without crossings with ends)
219 tbl_hrule(struct termp
*tp
, const struct tbl_span
*sp
, int kind
)
221 const struct tbl_cell
*c1
, *c2
;
225 line
= (kind
== 0 && TBL_SPAN_DHORIZ
== sp
->pos
) ? '=' : '-';
226 cross
= (kind
< 2) ? '+' : '-';
230 c1
= sp
->layout
->first
;
231 c2
= sp
->prev
== NULL
? NULL
: sp
->prev
->layout
->first
;
235 tbl_char(tp
, line
, tp
->tbl
.cols
[c1
->col
].width
+ 1);
237 if ((c1
= c1
->next
) == NULL
)
245 tbl_char(tp
, cross
, vert
);
247 tbl_char(tp
, line
, 2 - vert
);
256 tbl_data(struct termp
*tp
, const struct tbl_opts
*opts
,
257 const struct tbl_dat
*dp
,
258 const struct roffcol
*col
)
262 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
268 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
272 case TBL_DATA_NHORIZ
:
273 tbl_char(tp
, '-', col
->width
);
275 case TBL_DATA_NDHORIZ
:
277 case TBL_DATA_DHORIZ
:
278 tbl_char(tp
, '=', col
->width
);
284 switch (dp
->layout
->pos
) {
286 tbl_char(tp
, '-', col
->width
);
288 case TBL_CELL_DHORIZ
:
289 tbl_char(tp
, '=', col
->width
);
293 case TBL_CELL_CENTRE
:
298 tbl_literal(tp
, dp
, col
);
300 case TBL_CELL_NUMBER
:
301 tbl_number(tp
, opts
, dp
, col
);
304 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
313 tbl_char(struct termp
*tp
, char c
, size_t len
)
321 sz
= term_strlen(tp
, cp
);
323 for (i
= 0; i
< len
; i
+= sz
)
328 tbl_literal(struct termp
*tp
, const struct tbl_dat
*dp
,
329 const struct roffcol
*col
)
331 size_t len
, padl
, padr
, width
;
335 len
= term_strlen(tp
, dp
->string
);
337 ic
= dp
->layout
->col
;
340 width
+= tp
->tbl
.cols
[++ic
].width
+ 3;
342 padr
= width
> len
? width
- len
: 0;
345 switch (dp
->layout
->pos
) {
347 padl
= term_len(tp
, 1);
348 padr
= padr
> padl
? padr
- padl
: 0;
350 case TBL_CELL_CENTRE
:
364 tbl_char(tp
, ASCII_NBRSP
, padl
);
366 tbl_char(tp
, ASCII_NBRSP
, padr
);
370 tbl_number(struct termp
*tp
, const struct tbl_opts
*opts
,
371 const struct tbl_dat
*dp
,
372 const struct roffcol
*col
)
376 size_t sz
, psz
, ssz
, d
, padl
;
380 * See calc_data_number(). Left-pad by taking the offset of our
381 * and the maximum decimal; right-pad by the remaining amount.
386 sz
= term_strlen(tp
, dp
->string
);
388 buf
[0] = opts
->decimal
;
391 psz
= term_strlen(tp
, buf
);
393 if ((cp
= strrchr(dp
->string
, opts
->decimal
)) != NULL
) {
394 for (ssz
= 0, i
= 0; cp
!= &dp
->string
[i
]; i
++) {
395 buf
[0] = dp
->string
[i
];
396 ssz
+= term_strlen(tp
, buf
);
402 if (col
->decimal
> d
&& col
->width
> sz
) {
403 padl
= col
->decimal
- d
;
404 if (padl
+ sz
> col
->width
)
405 padl
= col
->width
- sz
;
406 tbl_char(tp
, ASCII_NBRSP
, padl
);
410 if (col
->width
> sz
+ padl
)
411 tbl_char(tp
, ASCII_NBRSP
, col
->width
- sz
- padl
);
415 tbl_word(struct termp
*tp
, const struct tbl_dat
*dp
)
419 prev_font
= tp
->fonti
;
420 if (dp
->layout
->flags
& TBL_CELL_BOLD
)
421 term_fontpush(tp
, TERMFONT_BOLD
);
422 else if (dp
->layout
->flags
& TBL_CELL_ITALIC
)
423 term_fontpush(tp
, TERMFONT_UNDER
);
425 term_word(tp
, dp
->string
);
427 term_fontpopq(tp
, prev_font
);