]>
git.cameronkatri.com Git - mandoc.git/blob - out.c
1 /* $Id: out.c,v 1.69 2017/06/15 00:27:52 schwarze Exp $ */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2014, 2015, 2017 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
*,
34 static void tblcalc_literal(struct rofftbl
*, struct roffcol
*,
35 const struct tbl_dat
*, size_t);
36 static void tblcalc_number(struct rofftbl
*, struct roffcol
*,
37 const struct tbl_opts
*, const struct tbl_dat
*);
41 * Parse the *src string and store a scaling unit into *dst.
42 * If the string doesn't specify the unit, use the default.
43 * If no default is specified, fail.
44 * Return a pointer to the byte after the last byte used,
45 * or NULL on total failure.
48 a2roffsu(const char *src
, struct roffsu
*dst
, enum roffscale def
)
52 dst
->unit
= def
== SCALE_MAX
? SCALE_BU
: def
;
53 dst
->scale
= strtod(src
, &endptr
);
99 * Calculate the abstract widths and decimal positions of columns in a
100 * table. This routine allocates the columns structures then runs over
101 * all rows and cells in the table. The function pointers in "tbl" are
102 * used for the actual width calculations.
105 tblcalc(struct rofftbl
*tbl
, const struct tbl_span
*sp
,
106 size_t offset
, size_t rmargin
)
109 const struct tbl_opts
*opts
;
110 const struct tbl_dat
*dp
;
112 size_t ewidth
, xwidth
;
114 int icol
, maxcol
, necol
, nxcol
, quirkcol
;
117 * Allocate the master column specifiers. These will hold the
118 * widths and decimal positions for all cells in the column. It
119 * must be freed and nullified by the caller.
122 assert(NULL
== tbl
->cols
);
123 tbl
->cols
= mandoc_calloc((size_t)sp
->opts
->cols
,
124 sizeof(struct roffcol
));
127 for (maxcol
= -1; sp
; sp
= sp
->next
) {
128 if (TBL_SPAN_DATA
!= sp
->pos
)
132 * Account for the data cells in the layout, matching it
133 * to data cells in the data section.
135 for (dp
= sp
->first
; dp
; dp
= dp
->next
) {
136 /* Do not used spanned cells in the calculation. */
142 icol
= dp
->layout
->col
;
145 col
= tbl
->cols
+ icol
;
146 col
->flags
|= dp
->layout
->flags
;
147 if (dp
->layout
->flags
& TBL_CELL_WIGN
)
149 if (dp
->layout
->wstr
!= NULL
&&
150 dp
->layout
->width
== 0 &&
151 a2roffsu(dp
->layout
->wstr
, &su
, SCALE_EN
)
154 (*tbl
->sulen
)(&su
, tbl
->arg
);
155 if (col
->width
< dp
->layout
->width
)
156 col
->width
= dp
->layout
->width
;
157 tblcalc_data(tbl
, col
, opts
, dp
,
159 dp
->layout
->width
? dp
->layout
->width
:
160 rmargin
? (rmargin
+ sp
->opts
->cols
/ 2)
161 / (sp
->opts
->cols
+ 1) : 0);
166 * Count columns to equalize and columns to maximize.
167 * Find maximum width of the columns to equalize.
168 * Find total width of the columns *not* to maximize.
173 for (icol
= 0; icol
<= maxcol
; icol
++) {
174 col
= tbl
->cols
+ icol
;
175 if (col
->flags
& TBL_CELL_EQUAL
) {
177 if (ewidth
< col
->width
)
180 if (col
->flags
& TBL_CELL_WMAX
)
183 xwidth
+= col
->width
;
187 * Equalize columns, if requested for any of them.
188 * Update total width of the columns not to maximize.
192 for (icol
= 0; icol
<= maxcol
; icol
++) {
193 col
= tbl
->cols
+ icol
;
194 if ( ! (col
->flags
& TBL_CELL_EQUAL
))
196 if (col
->width
== ewidth
)
198 if (nxcol
&& rmargin
)
199 xwidth
+= ewidth
- col
->width
;
205 * If there are any columns to maximize, find the total
206 * available width, deducting 3n margins between columns.
207 * Distribute the available width evenly.
210 if (nxcol
&& rmargin
) {
212 (opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
) ?
213 2 : !!opts
->lvert
+ !!opts
->rvert
);
214 if (rmargin
<= offset
+ xwidth
)
216 xwidth
= rmargin
- offset
- xwidth
;
219 * Emulate a bug in GNU tbl width calculation that
220 * manifests itself for large numbers of x-columns.
221 * Emulating it for 5 x-columns gives identical
222 * behaviour for up to 6 x-columns.
226 quirkcol
= xwidth
% nxcol
+ 2;
227 if (quirkcol
!= 3 && quirkcol
!= 4)
234 for (icol
= 0; icol
<= maxcol
; icol
++) {
235 col
= tbl
->cols
+ icol
;
236 if ( ! (col
->flags
& TBL_CELL_WMAX
))
238 col
->width
= (double)xwidth
* ++necol
/ nxcol
240 if (necol
== quirkcol
)
242 ewidth
+= col
->width
;
248 tblcalc_data(struct rofftbl
*tbl
, struct roffcol
*col
,
249 const struct tbl_opts
*opts
, const struct tbl_dat
*dp
, size_t mw
)
253 /* Branch down into data sub-types. */
255 switch (dp
->layout
->pos
) {
257 case TBL_CELL_DHORIZ
:
258 sz
= (*tbl
->len
)(1, tbl
->arg
);
263 case TBL_CELL_CENTRE
:
266 tblcalc_literal(tbl
, col
, dp
, mw
);
268 case TBL_CELL_NUMBER
:
269 tblcalc_number(tbl
, col
, opts
, dp
);
279 tblcalc_literal(struct rofftbl
*tbl
, struct roffcol
*col
,
280 const struct tbl_dat
*dp
, size_t mw
)
282 const char *str
; /* Beginning of the first line. */
283 const char *beg
; /* Beginning of the current line. */
284 char *end
; /* End of the current line. */
285 size_t lsz
; /* Length of the current line. */
286 size_t wsz
; /* Length of the current word. */
288 if (dp
->string
== NULL
|| *dp
->string
== '\0')
290 str
= mw
? mandoc_strdup(dp
->string
) : dp
->string
;
292 for (beg
= str
; beg
!= NULL
&& *beg
!= '\0'; beg
= end
) {
293 end
= mw
? strchr(beg
, ' ') : NULL
;
299 wsz
= (*tbl
->slen
)(beg
, tbl
->arg
);
300 if (mw
&& lsz
&& lsz
+ 1 + wsz
<= mw
)
304 if (col
->width
< lsz
)
312 tblcalc_number(struct rofftbl
*tbl
, struct roffcol
*col
,
313 const struct tbl_opts
*opts
, const struct tbl_dat
*dp
)
316 size_t sz
, psz
, ssz
, d
;
322 * First calculate number width and decimal place (last + 1 for
323 * non-decimal numbers). If the stored decimal is subsequent to
324 * ours, make our size longer by that difference
325 * (right-"shifting"); similarly, if ours is subsequent the
326 * stored, then extend the stored size by the difference.
327 * Finally, re-assign the stored values.
330 str
= dp
->string
? dp
->string
: "";
331 sz
= (*tbl
->slen
)(str
, tbl
->arg
);
333 /* FIXME: TBL_DATA_HORIZ et al.? */
335 buf
[0] = opts
->decimal
;
338 psz
= (*tbl
->slen
)(buf
, tbl
->arg
);
340 if (NULL
!= (cp
= strrchr(str
, opts
->decimal
))) {
342 for (ssz
= 0, i
= 0; cp
!= &str
[i
]; i
++) {
344 ssz
+= (*tbl
->slen
)(buf
, tbl
->arg
);
350 /* Adjust the settings for this column. */
352 if (col
->decimal
> d
) {
353 sz
+= col
->decimal
- d
;
356 col
->width
+= d
- col
->decimal
;
360 if (d
> col
->decimal
)