]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_term.c
1 /* $Id: tbl_term.c,v 1.1 2011/01/02 12:21:07 kristaps Exp $ */
3 * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 /* FIXME: `n' modifier doesn't always do the right thing. */
31 /* FIXME: `n' modifier doesn't use the cell-spacing buffer. */
33 static inline void tbl_char(struct termp
*, char, int);
34 static void tbl_hframe(struct termp
*,
35 const struct tbl_span
*);
36 static void tbl_data_number(struct termp
*,
38 const struct tbl_dat
*, int);
39 static void tbl_data_literal(struct termp
*,
40 const struct tbl_dat
*, int);
41 static void tbl_data_spanner(struct termp
*,
42 const struct tbl_dat
*, int);
43 static void tbl_data(struct termp
*, const struct tbl
*,
44 const struct tbl_dat
*, int);
45 static void tbl_spanner(struct termp
*,
46 const struct tbl_head
*);
47 static void tbl_hrule(struct termp
*,
48 const struct tbl_span
*);
49 static void tbl_vframe(struct termp
*, const struct tbl
*);
52 term_tbl(struct termp
*tp
, const struct tbl_span
*sp
)
54 const struct tbl_head
*hp
;
55 const struct tbl_dat
*dp
;
57 if (TBL_SPAN_FIRST
& sp
->flags
)
60 if (TBL_SPAN_FIRST
& sp
->flags
)
63 tp
->flags
|= TERMP_NONOSPACE
;
64 tp
->flags
|= TERMP_NOSPACE
;
66 tbl_vframe(tp
, sp
->tbl
);
69 case (TBL_SPAN_HORIZ
):
71 case (TBL_SPAN_DHORIZ
):
73 tbl_vframe(tp
, sp
->tbl
);
75 tp
->flags
&= ~TERMP_NONOSPACE
;
82 for (hp
= sp
->head
; hp
; hp
= hp
->next
) {
86 case (TBL_HEAD_DVERT
):
90 tbl_data(tp
, sp
->tbl
, dp
, hp
->width
);
100 tbl_vframe(tp
, sp
->tbl
);
103 if (TBL_SPAN_LAST
& sp
->flags
)
106 tp
->flags
&= ~TERMP_NONOSPACE
;
111 tbl_hrule(struct termp
*tp
, const struct tbl_span
*sp
)
113 const struct tbl_head
*hp
;
117 * An hrule extends across the entire table and is demarked by a
118 * standalone `_' or whatnot in lieu of a table row. Spanning
119 * headers are marked by a `+', as are table boundaries.
123 if (TBL_SPAN_DHORIZ
== sp
->pos
)
126 /* FIXME: don't use `+' between data and a spanner! */
128 for (hp
= sp
->head
; hp
; hp
= hp
->next
) {
130 case (TBL_HEAD_DATA
):
131 tbl_char(tp
, c
, hp
->width
);
133 case (TBL_HEAD_DVERT
):
134 tbl_char(tp
, '+', hp
->width
);
136 case (TBL_HEAD_VERT
):
137 tbl_char(tp
, '+', hp
->width
);
147 tbl_hframe(struct termp
*tp
, const struct tbl_span
*sp
)
149 const struct tbl_head
*hp
;
151 if ( ! (TBL_OPT_BOX
& sp
->tbl
->opts
||
152 TBL_OPT_DBOX
& sp
->tbl
->opts
))
155 tp
->flags
|= TERMP_NONOSPACE
;
156 tp
->flags
|= TERMP_NOSPACE
;
159 * Print out the horizontal part of a frame or double frame. A
160 * double frame has an unbroken `-' outer line the width of the
161 * table, bordered by `+'. The frame (or inner frame, in the
162 * case of the double frame) is a `-' bordered by `+' and broken
163 * by `+' whenever a span is encountered.
166 if (TBL_OPT_DBOX
& sp
->tbl
->opts
) {
168 for (hp
= sp
->head
; hp
; hp
= hp
->next
)
169 tbl_char(tp
, '-', hp
->width
);
175 for (hp
= sp
->head
; hp
; hp
= hp
->next
) {
177 case (TBL_HEAD_DATA
):
178 tbl_char(tp
, '-', hp
->width
);
181 tbl_char(tp
, '+', hp
->width
);
190 tbl_data(struct termp
*tp
, const struct tbl
*tbl
,
191 const struct tbl_dat
*dp
, int width
)
196 tbl_char(tp
, ASCII_NBRSP
, width
);
201 case (TBL_DATA_HORIZ
):
203 case (TBL_DATA_DHORIZ
):
204 tbl_data_spanner(tp
, dp
, width
);
210 pos
= dp
->layout
? dp
->layout
->pos
: TBL_CELL_LEFT
;
213 case (TBL_CELL_HORIZ
):
215 case (TBL_CELL_DHORIZ
):
216 tbl_data_spanner(tp
, dp
, width
);
218 case (TBL_CELL_LONG
):
220 case (TBL_CELL_CENTRE
):
222 case (TBL_CELL_LEFT
):
224 case (TBL_CELL_RIGHT
):
225 tbl_data_literal(tp
, dp
, width
);
227 case (TBL_CELL_NUMBER
):
228 tbl_data_number(tp
, tbl
, dp
, width
);
236 tbl_spanner(struct termp
*tp
, const struct tbl_head
*hp
)
240 case (TBL_HEAD_VERT
):
243 case (TBL_HEAD_DVERT
):
252 tbl_vframe(struct termp
*tp
, const struct tbl
*tbl
)
254 /* Always just a single vertical line. */
256 if (TBL_OPT_BOX
& tbl
->opts
|| TBL_OPT_DBOX
& tbl
->opts
)
262 tbl_char(struct termp
*tp
, char c
, int len
)
270 for (i
= 0; i
< len
; i
++)
275 tbl_data_spanner(struct termp
*tp
, const struct tbl_dat
*dp
, int width
)
279 case (TBL_DATA_HORIZ
):
280 case (TBL_DATA_NHORIZ
):
281 tbl_char(tp
, '-', width
);
283 case (TBL_DATA_DHORIZ
):
284 case (TBL_DATA_NDHORIZ
):
285 tbl_char(tp
, '=', width
);
293 tbl_data_literal(struct termp
*tp
, const struct tbl_dat
*dp
, int width
)
300 pos
= dp
->layout
? dp
->layout
->pos
: TBL_CELL_LEFT
;
303 case (TBL_CELL_LONG
):
305 padr
= width
- (int)strlen(dp
->string
) - 1;
307 case (TBL_CELL_CENTRE
):
308 padl
= width
- (int)strlen(dp
->string
);
314 case (TBL_CELL_RIGHT
):
315 padl
= width
- (int)strlen(dp
->string
);
318 padr
= width
- (int)strlen(dp
->string
);
322 tbl_char(tp
, ASCII_NBRSP
, padl
);
323 term_word(tp
, dp
->string
);
324 tbl_char(tp
, ASCII_NBRSP
, padr
);
328 tbl_data_number(struct termp
*tp
, const struct tbl
*tbl
,
329 const struct tbl_dat
*dp
, int width
)
335 * See calc_data_number(). Left-pad by taking the offset of our
336 * and the maximum decimal; right-pad by the remaining amount.
339 sz
= (int)strlen(dp
->string
);
342 if (NULL
== (decp
= strchr(dp
->string
, pnt
))) {
345 d
= (int)(decp
- dp
->string
) + 1;
348 assert(d
<= dp
->layout
->head
->decimal
);
349 assert(sz
- d
<= dp
->layout
->head
->width
-
350 dp
->layout
->head
->decimal
);
352 padl
= dp
->layout
->head
->decimal
- d
+ 1;
353 assert(width
- sz
- padl
);
355 tbl_char(tp
, ASCII_NBRSP
, padl
);
356 term_word(tp
, dp
->string
);
357 tbl_char(tp
, ASCII_NBRSP
, width
- sz
- padl
);