1 /* $Id: out.c,v 1.62 2015/10/12 00:08:16 schwarze Exp $ */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 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>
27 #include "mandoc_aux.h"
31 static void tblcalc_data(struct rofftbl
*, struct roffcol
*,
32 const struct tbl_opts
*, const struct tbl_dat
*);
33 static void tblcalc_literal(struct rofftbl
*, struct roffcol
*,
34 const struct tbl_dat
*);
35 static void tblcalc_number(struct rofftbl
*, struct roffcol
*,
36 const struct tbl_opts
*, const struct tbl_dat
*);
40 * Parse the *src string and store a scaling unit into *dst.
41 * If the string doesn't specify the unit, use the default.
42 * If no default is specified, fail.
43 * Return 2 on complete success, 1 when a conversion was done,
44 * but there was trailing garbage, and 0 on total failure.
47 a2roffsu(const char *src
, struct roffsu
*dst
, enum roffscale def
)
51 dst
->unit
= def
== SCALE_MAX
? SCALE_BU
: def
;
52 dst
->scale
= strtod(src
, &endptr
);
97 return *endptr
== '\0' ? 2 : 1;
101 * Calculate the abstract widths and decimal positions of columns in a
102 * table. This routine allocates the columns structures then runs over
103 * all rows and cells in the table. The function pointers in "tbl" are
104 * used for the actual width calculations.
107 tblcalc(struct rofftbl
*tbl
, const struct tbl_span
*sp
,
110 const struct tbl_opts
*opts
;
111 const struct tbl_dat
*dp
;
113 size_t ewidth
, xwidth
;
115 int icol
, maxcol
, necol
, nxcol
, quirkcol
;
118 * Allocate the master column specifiers. These will hold the
119 * widths and decimal positions for all cells in the column. It
120 * must be freed and nullified by the caller.
123 assert(NULL
== tbl
->cols
);
124 tbl
->cols
= mandoc_calloc((size_t)sp
->opts
->cols
,
125 sizeof(struct roffcol
));
128 for (maxcol
= -1; sp
; sp
= sp
->next
) {
129 if (TBL_SPAN_DATA
!= sp
->pos
)
133 * Account for the data cells in the layout, matching it
134 * to data cells in the data section.
136 for (dp
= sp
->first
; dp
; dp
= dp
->next
) {
137 /* Do not used spanned cells in the calculation. */
143 icol
= dp
->layout
->col
;
146 col
= tbl
->cols
+ icol
;
147 col
->flags
|= dp
->layout
->flags
;
148 if (dp
->layout
->flags
& TBL_CELL_WIGN
)
150 tblcalc_data(tbl
, col
, opts
, dp
);
155 * Count columns to equalize and columns to maximize.
156 * Find maximum width of the columns to equalize.
157 * Find total width of the columns *not* to maximize.
162 for (icol
= 0; icol
<= maxcol
; icol
++) {
163 col
= tbl
->cols
+ icol
;
164 if (col
->flags
& TBL_CELL_EQUAL
) {
166 if (ewidth
< col
->width
)
169 if (col
->flags
& TBL_CELL_WMAX
)
172 xwidth
+= col
->width
;
176 * Equalize columns, if requested for any of them.
177 * Update total width of the columns not to maximize.
181 for (icol
= 0; icol
<= maxcol
; icol
++) {
182 col
= tbl
->cols
+ icol
;
183 if ( ! (col
->flags
& TBL_CELL_EQUAL
))
185 if (col
->width
== ewidth
)
187 if (nxcol
&& totalwidth
)
188 xwidth
+= ewidth
- col
->width
;
194 * If there are any columns to maximize, find the total
195 * available width, deducting 3n margins between columns.
196 * Distribute the available width evenly.
199 if (nxcol
&& totalwidth
) {
200 xwidth
= totalwidth
- xwidth
- 3*maxcol
-
201 (opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
) ?
202 2 : !!opts
->lvert
+ !!opts
->rvert
);
205 * Emulate a bug in GNU tbl width calculation that
206 * manifests itself for large numbers of x-columns.
207 * Emulating it for 5 x-columns gives identical
208 * behaviour for up to 6 x-columns.
212 quirkcol
= xwidth
% nxcol
+ 2;
213 if (quirkcol
!= 3 && quirkcol
!= 4)
220 for (icol
= 0; icol
<= maxcol
; icol
++) {
221 col
= tbl
->cols
+ icol
;
222 if ( ! (col
->flags
& TBL_CELL_WMAX
))
224 col
->width
= (double)xwidth
* ++necol
/ nxcol
226 if (necol
== quirkcol
)
228 ewidth
+= col
->width
;
234 tblcalc_data(struct rofftbl
*tbl
, struct roffcol
*col
,
235 const struct tbl_opts
*opts
, const struct tbl_dat
*dp
)
239 /* Branch down into data sub-types. */
241 switch (dp
->layout
->pos
) {
243 case TBL_CELL_DHORIZ
:
244 sz
= (*tbl
->len
)(1, tbl
->arg
);
249 case TBL_CELL_CENTRE
:
252 tblcalc_literal(tbl
, col
, dp
);
254 case TBL_CELL_NUMBER
:
255 tblcalc_number(tbl
, col
, opts
, dp
);
265 tblcalc_literal(struct rofftbl
*tbl
, struct roffcol
*col
,
266 const struct tbl_dat
*dp
)
271 str
= dp
->string
? dp
->string
: "";
272 sz
= (*tbl
->slen
)(str
, tbl
->arg
);
279 tblcalc_number(struct rofftbl
*tbl
, struct roffcol
*col
,
280 const struct tbl_opts
*opts
, const struct tbl_dat
*dp
)
283 size_t sz
, psz
, ssz
, d
;
289 * First calculate number width and decimal place (last + 1 for
290 * non-decimal numbers). If the stored decimal is subsequent to
291 * ours, make our size longer by that difference
292 * (right-"shifting"); similarly, if ours is subsequent the
293 * stored, then extend the stored size by the difference.
294 * Finally, re-assign the stored values.
297 str
= dp
->string
? dp
->string
: "";
298 sz
= (*tbl
->slen
)(str
, tbl
->arg
);
300 /* FIXME: TBL_DATA_HORIZ et al.? */
302 buf
[0] = opts
->decimal
;
305 psz
= (*tbl
->slen
)(buf
, tbl
->arg
);
307 if (NULL
!= (cp
= strrchr(str
, opts
->decimal
))) {
309 for (ssz
= 0, i
= 0; cp
!= &str
[i
]; i
++) {
311 ssz
+= (*tbl
->slen
)(buf
, tbl
->arg
);
317 /* Adjust the settings for this column. */
319 if (col
->decimal
> d
) {
320 sz
+= col
->decimal
- d
;
323 col
->width
+= d
- col
->decimal
;
327 if (d
> col
->decimal
)