1 /* $Id: tbl_term.c,v 1.27 2014/04/20 16:46:05 schwarze Exp $ */
3 * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2012, 2014 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.
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 size_t tbl_rulewidth(struct termp
*, const struct tbl_head
*);
38 static void tbl_hframe(struct termp
*, const struct tbl_span
*, int);
39 static void tbl_literal(struct termp
*, const struct tbl_dat
*,
40 const struct roffcol
*);
41 static void tbl_number(struct termp
*, const struct tbl_opts
*,
42 const struct tbl_dat
*,
43 const struct roffcol
*);
44 static void tbl_hrule(struct termp
*, const struct tbl_span
*);
45 static void tbl_vrule(struct termp
*, const struct tbl_head
*);
49 term_tbl_strlen(const char *p
, void *arg
)
52 return(term_strlen((const struct termp
*)arg
, p
));
56 term_tbl_len(size_t sz
, void *arg
)
59 return(term_len((const struct termp
*)arg
, sz
));
63 term_tbl(struct termp
*tp
, const struct tbl_span
*sp
)
65 const struct tbl_head
*hp
;
66 const struct tbl_dat
*dp
;
69 size_t rmargin
, maxrmargin
;
71 rmargin
= tp
->rmargin
;
72 maxrmargin
= tp
->maxrmargin
;
74 tp
->rmargin
= tp
->maxrmargin
= TERM_MAXMARGIN
;
76 /* Inhibit printing of spaces: we do padding ourselves. */
78 tp
->flags
|= TERMP_NONOSPACE
;
79 tp
->flags
|= TERMP_NOSPACE
;
82 * The first time we're invoked for a given table block,
83 * calculate the table widths and decimal positions.
86 if (TBL_SPAN_FIRST
& sp
->flags
) {
89 tp
->tbl
.len
= term_tbl_len
;
90 tp
->tbl
.slen
= term_tbl_strlen
;
93 tblcalc(&tp
->tbl
, sp
);
96 /* Horizontal frame at the start of boxed tables. */
98 if (TBL_SPAN_FIRST
& sp
->flags
) {
99 if (TBL_OPT_DBOX
& sp
->opts
->opts
)
100 tbl_hframe(tp
, sp
, 1);
101 if (TBL_OPT_DBOX
& sp
->opts
->opts
||
102 TBL_OPT_BOX
& sp
->opts
->opts
)
103 tbl_hframe(tp
, sp
, 0);
106 /* Vertical frame at the start of each row. */
108 if ((TBL_OPT_BOX
| TBL_OPT_DBOX
) & sp
->opts
->opts
||
110 term_word(tp
, TBL_SPAN_HORIZ
== sp
->pos
||
111 TBL_SPAN_DHORIZ
== sp
->pos
? "+" : "|");
114 * Now print the actual data itself depending on the span type.
115 * Spanner spans get a horizontal rule; data spanners have their
116 * data printed by matching data to header.
122 case TBL_SPAN_DHORIZ
:
126 /* Iterate over template headers. */
129 for (hp
= sp
->head
; hp
; hp
= hp
->next
) {
132 * If the current data header is invoked during
133 * a spanner ("spans" > 0), don't emit anything
140 /* Separate columns. */
142 if (NULL
!= hp
->prev
)
145 col
= &tp
->tbl
.cols
[hp
->ident
];
146 tbl_data(tp
, sp
->opts
, dp
, col
);
149 * Go to the next data cell and assign the
150 * number of subsequent spans, if applicable.
161 /* Vertical frame at the end of each row. */
163 if ((TBL_OPT_BOX
| TBL_OPT_DBOX
) & sp
->opts
->opts
||
165 term_word(tp
, TBL_SPAN_HORIZ
== sp
->pos
||
166 TBL_SPAN_DHORIZ
== sp
->pos
? "+" : " |");
170 * If we're the last row, clean up after ourselves: clear the
171 * existing table configuration and set it to NULL.
174 if (TBL_SPAN_LAST
& sp
->flags
) {
175 if (TBL_OPT_DBOX
& sp
->opts
->opts
||
176 TBL_OPT_BOX
& sp
->opts
->opts
) {
177 tbl_hframe(tp
, sp
, 0);
180 if (TBL_OPT_DBOX
& sp
->opts
->opts
) {
181 tbl_hframe(tp
, sp
, 1);
184 assert(tp
->tbl
.cols
);
189 tp
->flags
&= ~TERMP_NONOSPACE
;
190 tp
->rmargin
= rmargin
;
191 tp
->maxrmargin
= maxrmargin
;
196 * Horizontal rules extend across the entire table.
197 * Calculate the width by iterating over columns.
200 tbl_rulewidth(struct termp
*tp
, const struct tbl_head
*hp
)
204 width
= tp
->tbl
.cols
[hp
->ident
].width
;
206 /* Account for leading blanks. */
208 width
+= 2 - hp
->vert
;
210 /* Account for trailing blank. */
217 * Rules inside the table can be single or double
218 * and have crossings with vertical rules marked with pluses.
221 tbl_hrule(struct termp
*tp
, const struct tbl_span
*sp
)
223 const struct tbl_head
*hp
;
227 if (TBL_SPAN_DHORIZ
== sp
->pos
)
230 for (hp
= sp
->head
; hp
; hp
= hp
->next
) {
231 if (hp
->prev
&& hp
->vert
)
232 tbl_char(tp
, '+', hp
->vert
);
233 tbl_char(tp
, c
, tbl_rulewidth(tp
, hp
));
238 * Rules above and below the table are always single
239 * and have an additional plus at the beginning and end.
240 * For double frames, this function is called twice,
241 * and the outer one does not have crossings.
244 tbl_hframe(struct termp
*tp
, const struct tbl_span
*sp
, int outer
)
246 const struct tbl_head
*hp
;
249 for (hp
= sp
->head
; hp
; hp
= hp
->next
) {
250 if (hp
->prev
&& hp
->vert
)
251 tbl_char(tp
, (outer
? '-' : '+'), hp
->vert
);
252 tbl_char(tp
, '-', tbl_rulewidth(tp
, hp
));
259 tbl_data(struct termp
*tp
, const struct tbl_opts
*opts
,
260 const struct tbl_dat
*dp
,
261 const struct roffcol
*col
)
265 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
272 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
276 case TBL_DATA_NHORIZ
:
277 tbl_char(tp
, '-', col
->width
);
279 case TBL_DATA_NDHORIZ
:
281 case TBL_DATA_DHORIZ
:
282 tbl_char(tp
, '=', col
->width
);
288 switch (dp
->layout
->pos
) {
290 tbl_char(tp
, '-', col
->width
);
292 case TBL_CELL_DHORIZ
:
293 tbl_char(tp
, '=', col
->width
);
297 case TBL_CELL_CENTRE
:
302 tbl_literal(tp
, dp
, col
);
304 case TBL_CELL_NUMBER
:
305 tbl_number(tp
, opts
, dp
, col
);
308 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
317 tbl_vrule(struct termp
*tp
, const struct tbl_head
*hp
)
320 tbl_char(tp
, ASCII_NBRSP
, 1);
322 tbl_char(tp
, '|', hp
->vert
);
324 tbl_char(tp
, ASCII_NBRSP
, 2 - hp
->vert
);
328 tbl_char(struct termp
*tp
, char c
, size_t len
)
336 sz
= term_strlen(tp
, cp
);
338 for (i
= 0; i
< len
; i
+= sz
)
343 tbl_literal(struct termp
*tp
, const struct tbl_dat
*dp
,
344 const struct roffcol
*col
)
347 size_t width
, len
, padl
, padr
;
351 len
= term_strlen(tp
, dp
->string
);
353 hp
= dp
->layout
->head
->next
;
355 for (spans
= dp
->spans
; spans
--; hp
= hp
->next
)
356 width
+= tp
->tbl
.cols
[hp
->ident
].width
+ 3;
358 padr
= width
> len
? width
- len
: 0;
361 switch (dp
->layout
->pos
) {
363 padl
= term_len(tp
, 1);
364 padr
= padr
> padl
? padr
- padl
: 0;
366 case TBL_CELL_CENTRE
:
380 tbl_char(tp
, ASCII_NBRSP
, padl
);
381 term_word(tp
, dp
->string
);
382 tbl_char(tp
, ASCII_NBRSP
, padr
);
386 tbl_number(struct termp
*tp
, const struct tbl_opts
*opts
,
387 const struct tbl_dat
*dp
,
388 const struct roffcol
*col
)
392 size_t sz
, psz
, ssz
, d
, padl
;
396 * See calc_data_number(). Left-pad by taking the offset of our
397 * and the maximum decimal; right-pad by the remaining amount.
402 sz
= term_strlen(tp
, dp
->string
);
404 buf
[0] = opts
->decimal
;
407 psz
= term_strlen(tp
, buf
);
409 if (NULL
!= (cp
= strrchr(dp
->string
, opts
->decimal
))) {
411 for (ssz
= 0, i
= 0; cp
!= &dp
->string
[i
]; i
++) {
412 buf
[0] = dp
->string
[i
];
413 ssz
+= term_strlen(tp
, buf
);
419 padl
= col
->decimal
- d
;
421 tbl_char(tp
, ASCII_NBRSP
, padl
);
422 term_word(tp
, dp
->string
);
423 if (col
->width
> sz
+ padl
)
424 tbl_char(tp
, ASCII_NBRSP
, col
->width
- sz
- padl
);