]>
git.cameronkatri.com Git - mandoc.git/blob - out.c
1 /* $Id: out.c,v 1.55 2014/12/23 03:28:01 schwarze Exp $ */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 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.
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 1 on success and 0 on failure.
46 a2roffsu(const char *src
, struct roffsu
*dst
, enum roffscale def
)
52 scale
= strtod(src
, &endptr
);
53 if (endptr
== src
|| (endptr
[0] != '\0' && endptr
[1] != '\0'))
96 /* FIXME: do this in the caller. */
105 * Calculate the abstract widths and decimal positions of columns in a
106 * table. This routine allocates the columns structures then runs over
107 * all rows and cells in the table. The function pointers in "tbl" are
108 * used for the actual width calculations.
111 tblcalc(struct rofftbl
*tbl
, const struct tbl_span
*sp
,
114 const struct tbl_dat
*dp
;
116 size_t ewidth
, xwidth
;
118 int icol
, maxcol
, necol
, nxcol
;
121 * Allocate the master column specifiers. These will hold the
122 * widths and decimal positions for all cells in the column. It
123 * must be freed and nullified by the caller.
126 assert(NULL
== tbl
->cols
);
127 tbl
->cols
= mandoc_calloc((size_t)sp
->opts
->cols
,
128 sizeof(struct roffcol
));
130 for (maxcol
= -1; sp
; sp
= sp
->next
) {
131 if (TBL_SPAN_DATA
!= sp
->pos
)
135 * Account for the data cells in the layout, matching it
136 * to data cells in the data section.
138 for (dp
= sp
->first
; dp
; dp
= dp
->next
) {
139 /* Do not used spanned cells in the calculation. */
145 icol
= dp
->layout
->head
->ident
;
148 col
= tbl
->cols
+ icol
;
149 col
->flags
|= dp
->layout
->flags
;
150 if (dp
->layout
->flags
& TBL_CELL_WIGN
)
152 tblcalc_data(tbl
, col
, sp
->opts
, dp
);
157 * Count columns to equalize and columns to maximize.
158 * Find maximum width of the columns to equalize.
159 * Find total width of the columns *not* to maximize.
164 for (icol
= 0; icol
<= maxcol
; icol
++) {
165 col
= tbl
->cols
+ icol
;
166 if (col
->flags
& TBL_CELL_EQUAL
) {
168 if (ewidth
< col
->width
)
171 if (col
->flags
& TBL_CELL_WMAX
)
174 xwidth
+= col
->width
;
178 * Equalize columns, if requested for any of them.
179 * Update total width of the columns not to maximize.
183 for (icol
= 0; icol
<= maxcol
; icol
++) {
184 col
= tbl
->cols
+ icol
;
185 if ( ! (col
->flags
& TBL_CELL_EQUAL
))
187 if (col
->width
== ewidth
)
189 if (nxcol
&& totalwidth
)
190 xwidth
+= ewidth
- col
->width
;
196 * If there are any columns to maximize, find the total
197 * available width, deducting 3n margins between columns.
198 * Distribute the available width evenly.
201 if (nxcol
&& totalwidth
) {
202 xwidth
= totalwidth
- 3*maxcol
- xwidth
;
203 for (icol
= 0; icol
<= maxcol
; icol
++) {
204 col
= tbl
->cols
+ icol
;
205 if ( ! (col
->flags
& TBL_CELL_WMAX
))
207 col
->width
= xwidth
/ nxcol
--;
208 xwidth
-= col
->width
;
214 tblcalc_data(struct rofftbl
*tbl
, struct roffcol
*col
,
215 const struct tbl_opts
*opts
, const struct tbl_dat
*dp
)
219 /* Branch down into data sub-types. */
221 switch (dp
->layout
->pos
) {
224 case TBL_CELL_DHORIZ
:
225 sz
= (*tbl
->len
)(1, tbl
->arg
);
231 case TBL_CELL_CENTRE
:
236 tblcalc_literal(tbl
, col
, dp
);
238 case TBL_CELL_NUMBER
:
239 tblcalc_number(tbl
, col
, opts
, dp
);
250 tblcalc_literal(struct rofftbl
*tbl
, struct roffcol
*col
,
251 const struct tbl_dat
*dp
)
256 str
= dp
->string
? dp
->string
: "";
257 sz
= (*tbl
->slen
)(str
, tbl
->arg
);
264 tblcalc_number(struct rofftbl
*tbl
, struct roffcol
*col
,
265 const struct tbl_opts
*opts
, const struct tbl_dat
*dp
)
268 size_t sz
, psz
, ssz
, d
;
274 * First calculate number width and decimal place (last + 1 for
275 * non-decimal numbers). If the stored decimal is subsequent to
276 * ours, make our size longer by that difference
277 * (right-"shifting"); similarly, if ours is subsequent the
278 * stored, then extend the stored size by the difference.
279 * Finally, re-assign the stored values.
282 str
= dp
->string
? dp
->string
: "";
283 sz
= (*tbl
->slen
)(str
, tbl
->arg
);
285 /* FIXME: TBL_DATA_HORIZ et al.? */
287 buf
[0] = opts
->decimal
;
290 psz
= (*tbl
->slen
)(buf
, tbl
->arg
);
292 if (NULL
!= (cp
= strrchr(str
, opts
->decimal
))) {
294 for (ssz
= 0, i
= 0; cp
!= &str
[i
]; i
++) {
296 ssz
+= (*tbl
->slen
)(buf
, tbl
->arg
);
302 /* Adjust the settings for this column. */
304 if (col
->decimal
> d
) {
305 sz
+= col
->decimal
- d
;
308 col
->width
+= d
- col
->decimal
;
312 if (d
> col
->decimal
)