]>
git.cameronkatri.com Git - mandoc.git/blob - out.c
6ae41fd12a7c56684328f616a9276a34a899fa16
1 /* $Id: out.c,v 1.42 2011/07/24 18:15:14 kristaps Exp $ */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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.
22 #include <sys/types.h>
34 static void tblcalc_data(struct rofftbl
*, struct roffcol
*,
35 const struct tbl
*, const struct tbl_dat
*);
36 static void tblcalc_literal(struct rofftbl
*, struct roffcol
*,
37 const struct tbl_dat
*);
38 static void tblcalc_number(struct rofftbl
*, struct roffcol
*,
39 const struct tbl
*, const struct tbl_dat
*);
42 * Convert a `scaling unit' to a consistent form, or fail. Scaling
43 * units are documented in groff.7, mdoc.7, man.7.
46 a2roffsu(const char *src
, struct roffsu
*dst
, enum roffscale def
)
48 char buf
[BUFSIZ
], hasd
;
72 if ( ! isdigit((unsigned char)*src
)) {
83 if (BUFSIZ
== i
|| (*src
&& *(src
+ 1)))
111 if (SCALE_MAX
== def
)
128 /* FIXME: do this in the caller. */
129 if ((dst
->scale
= atof(buf
)) < 0)
136 * Calculate the abstract widths and decimal positions of columns in a
137 * table. This routine allocates the columns structures then runs over
138 * all rows and cells in the table. The function pointers in "tbl" are
139 * used for the actual width calculations.
142 tblcalc(struct rofftbl
*tbl
, const struct tbl_span
*sp
)
144 const struct tbl_dat
*dp
;
145 const struct tbl_head
*hp
;
149 * Allocate the master column specifiers. These will hold the
150 * widths and decimal positions for all cells in the column. It
151 * must be freed and nullified by the caller.
154 assert(NULL
== tbl
->cols
);
155 tbl
->cols
= mandoc_calloc
156 ((size_t)sp
->tbl
->cols
, sizeof(struct roffcol
));
160 for ( ; sp
; sp
= sp
->next
) {
161 if (TBL_SPAN_DATA
!= sp
->pos
)
164 * Account for the data cells in the layout, matching it
165 * to data cells in the data section.
167 for (dp
= sp
->first
; dp
; dp
= dp
->next
) {
169 col
= &tbl
->cols
[dp
->layout
->head
->ident
];
170 tblcalc_data(tbl
, col
, sp
->tbl
, dp
);
175 * Calculate width of the spanners. These get one space for a
176 * vertical line, two for a double-vertical line.
179 for ( ; hp
; hp
= hp
->next
) {
180 col
= &tbl
->cols
[hp
->ident
];
182 case (TBL_HEAD_VERT
):
183 col
->width
= (*tbl
->len
)(1, tbl
->arg
);
185 case (TBL_HEAD_DVERT
):
186 col
->width
= (*tbl
->len
)(2, tbl
->arg
);
195 tblcalc_data(struct rofftbl
*tbl
, struct roffcol
*col
,
196 const struct tbl
*tp
, const struct tbl_dat
*dp
)
200 /* Branch down into data sub-types. */
202 switch (dp
->layout
->pos
) {
203 case (TBL_CELL_HORIZ
):
205 case (TBL_CELL_DHORIZ
):
206 sz
= (*tbl
->len
)(1, tbl
->arg
);
210 case (TBL_CELL_LONG
):
212 case (TBL_CELL_CENTRE
):
214 case (TBL_CELL_LEFT
):
216 case (TBL_CELL_RIGHT
):
217 tblcalc_literal(tbl
, col
, dp
);
219 case (TBL_CELL_NUMBER
):
220 tblcalc_number(tbl
, col
, tp
, dp
);
222 case (TBL_CELL_DOWN
):
231 tblcalc_literal(struct rofftbl
*tbl
, struct roffcol
*col
,
232 const struct tbl_dat
*dp
)
234 size_t sz
, bufsz
, spsz
;
238 * Calculate our width and use the spacing, with a minimum
239 * spacing dictated by position (centre, e.g,. gets a space on
240 * either side, while right/left get a single adjacent space).
244 str
= dp
->string
? dp
->string
: "";
245 sz
= (*tbl
->slen
)(str
, tbl
->arg
);
247 /* FIXME: TBL_DATA_HORIZ et al.? */
250 switch (dp
->layout
->pos
) {
251 case (TBL_CELL_LONG
):
253 case (TBL_CELL_CENTRE
):
254 bufsz
= (*tbl
->len
)(1, tbl
->arg
);
257 bufsz
= (*tbl
->len
)(1, tbl
->arg
);
261 if (dp
->layout
->spacing
) {
262 spsz
= (*tbl
->len
)(dp
->layout
->spacing
, tbl
->arg
);
263 bufsz
= bufsz
> spsz
? bufsz
: spsz
;
272 tblcalc_number(struct rofftbl
*tbl
, struct roffcol
*col
,
273 const struct tbl
*tp
, const struct tbl_dat
*dp
)
276 size_t sz
, psz
, ssz
, d
;
282 * First calculate number width and decimal place (last + 1 for
283 * no-decimal numbers). If the stored decimal is subsequent
284 * ours, make our size longer by that difference
285 * (right-"shifting"); similarly, if ours is subsequent the
286 * stored, then extend the stored size by the difference.
287 * Finally, re-assign the stored values.
290 str
= dp
->string
? dp
->string
: "";
291 sz
= (*tbl
->slen
)(str
, tbl
->arg
);
293 /* FIXME: TBL_DATA_HORIZ et al.? */
295 buf
[0] = tp
->decimal
;
298 psz
= (*tbl
->slen
)(buf
, tbl
->arg
);
300 if (NULL
!= (cp
= strrchr(str
, tp
->decimal
))) {
302 for (ssz
= 0, i
= 0; cp
!= &str
[i
]; i
++) {
304 ssz
+= (*tbl
->slen
)(buf
, tbl
->arg
);
312 sz
+= (*tbl
->len
)(2, tbl
->arg
);
313 d
+= (*tbl
->len
)(1, tbl
->arg
);
315 /* Adjust the settings for this column. */
317 if (col
->decimal
> d
) {
318 sz
+= col
->decimal
- d
;
321 col
->width
+= d
- col
->decimal
;
325 if (d
> col
->decimal
)
328 /* Adjust for stipulated width. */
330 if (col
->width
< dp
->layout
->spacing
)
331 col
->width
= dp
->layout
->spacing
;