]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_term.c
1 /* $Id: tbl_term.c,v 1.19 2011/01/25 12:07:30 schwarze Exp $ */
3 * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@kth.se>
4 * Copyright (c) 2011 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
*,
35 const struct tbl_dat
*,
36 const struct roffcol
*);
37 static void tbl_hframe(struct termp
*, const struct tbl_span
*);
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
*,
41 const struct tbl_dat
*,
42 const struct roffcol
*);
43 static void tbl_hrule(struct termp
*, const struct tbl_span
*);
44 static void tbl_vframe(struct termp
*, const struct tbl
*);
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
)
101 /* Vertical frame at the start of each row. */
103 tbl_vframe(tp
, sp
->tbl
);
106 * Now print the actual data itself depending on the span type.
107 * Spanner spans get a horizontal rule; data spanners have their
108 * data printed by matching data to header.
112 case (TBL_SPAN_HORIZ
):
114 case (TBL_SPAN_DHORIZ
):
117 case (TBL_SPAN_DATA
):
118 /* Iterate over template headers. */
121 for (hp
= sp
->head
; hp
; hp
= hp
->next
) {
123 * If the current data header is invoked during
124 * a spanner ("spans" > 0), don't emit anything
128 case (TBL_HEAD_VERT
):
130 case (TBL_HEAD_DVERT
):
134 case (TBL_HEAD_DATA
):
141 col
= &tp
->tbl
.cols
[hp
->ident
];
142 tbl_data(tp
, sp
->tbl
, dp
, col
);
145 * Go to the next data cell and assign the
146 * number of subsequent spans, if applicable.
157 tbl_vframe(tp
, sp
->tbl
);
161 * If we're the last row, clean up after ourselves: clear the
162 * existing table configuration and set it to NULL.
165 if (TBL_SPAN_LAST
& sp
->flags
) {
167 assert(tp
->tbl
.cols
);
172 tp
->flags
&= ~TERMP_NONOSPACE
;
173 tp
->rmargin
= rmargin
;
174 tp
->maxrmargin
= maxrmargin
;
179 tbl_hrule(struct termp
*tp
, const struct tbl_span
*sp
)
181 const struct tbl_head
*hp
;
186 * An hrule extends across the entire table and is demarked by a
187 * standalone `_' or whatnot in lieu of a table row. Spanning
188 * headers are marked by a `+', as are table boundaries.
192 if (TBL_SPAN_DHORIZ
== sp
->pos
)
195 /* FIXME: don't use `+' between data and a spanner! */
197 for (hp
= sp
->head
; hp
; hp
= hp
->next
) {
198 width
= tp
->tbl
.cols
[hp
->ident
].width
;
200 case (TBL_HEAD_DATA
):
203 tbl_char(tp
, c
, width
);
205 case (TBL_HEAD_DVERT
):
206 tbl_char(tp
, '+', width
);
208 case (TBL_HEAD_VERT
):
209 tbl_char(tp
, '+', width
);
219 tbl_hframe(struct termp
*tp
, const struct tbl_span
*sp
)
221 const struct tbl_head
*hp
;
224 if ( ! (TBL_OPT_BOX
& sp
->tbl
->opts
||
225 TBL_OPT_DBOX
& sp
->tbl
->opts
))
229 * Print out the horizontal part of a frame or double frame. A
230 * double frame has an unbroken `-' outer line the width of the
231 * table, bordered by `+'. The frame (or inner frame, in the
232 * case of the double frame) is a `-' bordered by `+' and broken
233 * by `+' whenever a span is encountered.
236 if (TBL_OPT_DBOX
& sp
->tbl
->opts
) {
238 for (hp
= sp
->head
; hp
; hp
= hp
->next
) {
239 width
= tp
->tbl
.cols
[hp
->ident
].width
;
240 tbl_char(tp
, '-', width
);
247 for (hp
= sp
->head
; hp
; hp
= hp
->next
) {
248 width
= tp
->tbl
.cols
[hp
->ident
].width
;
250 case (TBL_HEAD_DATA
):
251 tbl_char(tp
, '-', width
);
254 tbl_char(tp
, '+', width
);
263 tbl_data(struct termp
*tp
, const struct tbl
*tbl
,
264 const struct tbl_dat
*dp
,
265 const struct roffcol
*col
)
269 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
275 case (TBL_DATA_NONE
):
276 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
278 case (TBL_DATA_HORIZ
):
280 case (TBL_DATA_NHORIZ
):
281 tbl_char(tp
, '-', col
->width
);
283 case (TBL_DATA_NDHORIZ
):
285 case (TBL_DATA_DHORIZ
):
286 tbl_char(tp
, '=', col
->width
);
292 switch (dp
->layout
->pos
) {
293 case (TBL_CELL_HORIZ
):
294 tbl_char(tp
, '-', col
->width
);
296 case (TBL_CELL_DHORIZ
):
297 tbl_char(tp
, '=', col
->width
);
299 case (TBL_CELL_LONG
):
301 case (TBL_CELL_CENTRE
):
303 case (TBL_CELL_LEFT
):
305 case (TBL_CELL_RIGHT
):
306 tbl_literal(tp
, dp
, col
);
308 case (TBL_CELL_NUMBER
):
309 tbl_number(tp
, tbl
, dp
, col
);
311 case (TBL_CELL_DOWN
):
312 tbl_char(tp
, ASCII_NBRSP
, col
->width
);
321 tbl_vrule(struct termp
*tp
, const struct tbl_head
*hp
)
325 case (TBL_HEAD_VERT
):
328 case (TBL_HEAD_DVERT
):
337 tbl_vframe(struct termp
*tp
, const struct tbl
*tbl
)
340 if (TBL_OPT_BOX
& tbl
->opts
|| TBL_OPT_DBOX
& tbl
->opts
)
345 tbl_char(struct termp
*tp
, char c
, size_t len
)
353 sz
= term_strlen(tp
, cp
);
355 for (i
= 0; i
< len
; i
+= sz
)
360 tbl_literal(struct termp
*tp
, const struct tbl_dat
*dp
,
361 const struct roffcol
*col
)
363 size_t padl
, padr
, ssz
;
369 ssz
= term_len(tp
, 1);
371 switch (dp
->layout
->pos
) {
372 case (TBL_CELL_LONG
):
374 padr
= col
->width
- term_strlen(tp
, dp
->string
) - ssz
;
376 case (TBL_CELL_CENTRE
):
377 padr
= col
->width
- term_strlen(tp
, dp
->string
);
380 padl
= (padr
- 1) / 2;
383 case (TBL_CELL_RIGHT
):
384 padl
= col
->width
- term_strlen(tp
, dp
->string
);
387 padr
= col
->width
- term_strlen(tp
, dp
->string
);
391 tbl_char(tp
, ASCII_NBRSP
, padl
);
392 term_word(tp
, dp
->string
);
393 tbl_char(tp
, ASCII_NBRSP
, padr
+ 2);
397 tbl_number(struct termp
*tp
, const struct tbl
*tbl
,
398 const struct tbl_dat
*dp
,
399 const struct roffcol
*col
)
403 size_t sz
, psz
, ssz
, d
, padl
;
407 * See calc_data_number(). Left-pad by taking the offset of our
408 * and the maximum decimal; right-pad by the remaining amount.
413 sz
= term_strlen(tp
, dp
->string
);
415 buf
[0] = tbl
->decimal
;
418 psz
= term_strlen(tp
, buf
);
420 if (NULL
!= (cp
= strrchr(dp
->string
, tbl
->decimal
))) {
422 for (ssz
= 0, i
= 0; cp
!= &dp
->string
[i
]; i
++) {
423 buf
[0] = dp
->string
[i
];
424 ssz
+= term_strlen(tp
, buf
);
430 sz
+= term_len(tp
, 2);
431 d
+= term_len(tp
, 1);
433 padl
= col
->decimal
- d
;
435 tbl_char(tp
, ASCII_NBRSP
, padl
);
436 term_word(tp
, dp
->string
);
437 tbl_char(tp
, ASCII_NBRSP
, col
->width
- sz
- padl
);