]>
git.cameronkatri.com Git - mandoc.git/blob - out.c
1 /* $Id: out.c,v 1.79 2019/12/31 22:58:41 schwarze Exp $ */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011,2014,2015,2017,2018 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>
29 #include "mandoc_aux.h"
34 struct tbl_colgroup
*next
;
40 static size_t tblcalc_data(struct rofftbl
*, struct roffcol
*,
41 const struct tbl_opts
*, const struct tbl_dat
*,
43 static size_t tblcalc_literal(struct rofftbl
*, struct roffcol
*,
44 const struct tbl_dat
*, size_t);
45 static size_t tblcalc_number(struct rofftbl
*, struct roffcol
*,
46 const struct tbl_opts
*, const struct tbl_dat
*);
50 * Parse the *src string and store a scaling unit into *dst.
51 * If the string doesn't specify the unit, use the default.
52 * If no default is specified, fail.
53 * Return a pointer to the byte after the last byte used,
54 * or NULL on total failure.
57 a2roffsu(const char *src
, struct roffsu
*dst
, enum roffscale def
)
61 dst
->unit
= def
== SCALE_MAX
? SCALE_BU
: def
;
62 dst
->scale
= strtod(src
, &endptr
);
108 * Calculate the abstract widths and decimal positions of columns in a
109 * table. This routine allocates the columns structures then runs over
110 * all rows and cells in the table. The function pointers in "tbl" are
111 * used for the actual width calculations.
114 tblcalc(struct rofftbl
*tbl
, const struct tbl_span
*sp_first
,
115 size_t offset
, size_t rmargin
)
118 const struct tbl_opts
*opts
;
119 const struct tbl_span
*sp
;
120 const struct tbl_dat
*dp
;
122 struct tbl_colgroup
*first_group
, **gp
, *g
;
124 size_t ewidth
, min1
, min2
, wanted
, width
, xwidth
;
125 int done
, icol
, maxcol
, necol
, nxcol
, quirkcol
;
128 * Allocate the master column specifiers. These will hold the
129 * widths and decimal positions for all cells in the column. It
130 * must be freed and nullified by the caller.
133 assert(tbl
->cols
== NULL
);
134 tbl
->cols
= mandoc_calloc((size_t)sp_first
->opts
->cols
,
135 sizeof(struct roffcol
));
136 opts
= sp_first
->opts
;
140 for (sp
= sp_first
; sp
!= NULL
; sp
= sp
->next
) {
141 if (sp
->pos
!= TBL_SPAN_DATA
)
145 * Account for the data cells in the layout, matching it
146 * to data cells in the data section.
150 for (dp
= sp
->first
; dp
!= NULL
; dp
= dp
->next
) {
151 icol
= dp
->layout
->col
;
152 while (maxcol
< icol
+ dp
->hspans
)
153 tbl
->cols
[++maxcol
].spacing
= SIZE_MAX
;
154 col
= tbl
->cols
+ icol
;
155 col
->flags
|= dp
->layout
->flags
;
156 if (dp
->layout
->flags
& TBL_CELL_WIGN
)
159 /* Handle explicit width specifications. */
161 if (dp
->layout
->wstr
!= NULL
&&
162 dp
->layout
->width
== 0 &&
163 a2roffsu(dp
->layout
->wstr
, &su
, SCALE_EN
)
166 (*tbl
->sulen
)(&su
, tbl
->arg
);
167 if (col
->width
< dp
->layout
->width
)
168 col
->width
= dp
->layout
->width
;
169 if (dp
->layout
->spacing
!= SIZE_MAX
&&
170 (col
->spacing
== SIZE_MAX
||
171 col
->spacing
< dp
->layout
->spacing
))
172 col
->spacing
= dp
->layout
->spacing
;
175 * Calculate an automatic width.
176 * Except for spanning cells, apply it.
179 width
= tblcalc_data(tbl
,
180 dp
->hspans
== 0 ? col
: NULL
,
183 dp
->layout
->width
? dp
->layout
->width
:
184 rmargin
? (rmargin
+ sp
->opts
->cols
/ 2)
185 / (sp
->opts
->cols
+ 1) : 0);
190 * Build an ordered, singly linked list
191 * of all groups of columns joined by spans,
192 * recording the minimum width for each group.
195 while (*gp
!= NULL
&& ((*gp
)->startcol
< icol
||
196 (*gp
)->endcol
< icol
+ dp
->hspans
))
198 if (*gp
== NULL
|| (*gp
)->startcol
> icol
||
199 (*gp
)->endcol
> icol
+ dp
->hspans
) {
200 g
= mandoc_malloc(sizeof(*g
));
204 g
->endcol
= icol
+ dp
->hspans
;
206 } else if ((*gp
)->wanted
< width
)
207 (*gp
)->wanted
= width
;
212 * The minimum width of columns explicitly specified
213 * in the layout is 1n.
216 if (maxcol
< sp_first
->opts
->cols
- 1)
217 maxcol
= sp_first
->opts
->cols
- 1;
218 for (icol
= 0; icol
<= maxcol
; icol
++) {
219 col
= tbl
->cols
+ icol
;
224 * Column spacings are needed for span width
225 * calculations, so set the default values now.
228 if (col
->spacing
== SIZE_MAX
|| icol
== maxcol
)
233 * Replace the minimum widths with the missing widths,
234 * and dismiss groups that are already wide enough.
238 while ((g
= *gp
) != NULL
) {
240 for (icol
= g
->startcol
; icol
<= g
->endcol
; icol
++) {
241 width
= tbl
->cols
[icol
].width
;
242 if (icol
< g
->endcol
)
243 width
+= tbl
->cols
[icol
].spacing
;
244 if (g
->wanted
<= width
) {
248 (*gp
)->wanted
-= width
;
257 colwidth
= mandoc_reallocarray(NULL
, maxcol
+ 1, sizeof(*colwidth
));
258 while (first_group
!= NULL
) {
261 * Rebuild the array of the widths of all columns
262 * participating in spans that require expansion.
265 for (icol
= 0; icol
<= maxcol
; icol
++)
266 colwidth
[icol
] = SIZE_MAX
;
267 for (g
= first_group
; g
!= NULL
; g
= g
->next
)
268 for (icol
= g
->startcol
; icol
<= g
->endcol
; icol
++)
269 colwidth
[icol
] = tbl
->cols
[icol
].width
;
272 * Find the smallest and second smallest column width
273 * among the columns which may need expamsion.
276 min1
= min2
= SIZE_MAX
;
277 for (icol
= 0; icol
<= maxcol
; icol
++) {
278 if (min1
> colwidth
[icol
]) {
280 min1
= colwidth
[icol
];
281 } else if (min1
< colwidth
[icol
] &&
282 min2
> colwidth
[icol
])
283 min2
= colwidth
[icol
];
287 * Find the minimum wanted width
288 * for any one of the narrowest columns,
289 * and mark the columns wanting that width.
293 for (g
= first_group
; g
!= NULL
; g
= g
->next
) {
295 for (icol
= g
->startcol
; icol
<= g
->endcol
; icol
++)
296 if (tbl
->cols
[icol
].width
== min1
)
300 width
= min1
+ (g
->wanted
- 1) / necol
+ 1;
305 for (icol
= g
->startcol
; icol
<= g
->endcol
; icol
++)
306 if (colwidth
[icol
] == min1
||
307 (colwidth
[icol
] < min2
&&
308 colwidth
[icol
] > width
))
309 colwidth
[icol
] = width
;
312 /* Record the effect of the widening on the group list. */
315 while ((g
= *gp
) != NULL
) {
317 for (icol
= g
->startcol
; icol
<= g
->endcol
; icol
++) {
318 if (colwidth
[icol
] != wanted
||
319 tbl
->cols
[icol
].width
== wanted
)
321 if (g
->wanted
<= wanted
- min1
) {
325 g
->wanted
-= wanted
- min1
;
334 /* Record the effect of the widening on the columns. */
336 for (icol
= 0; icol
<= maxcol
; icol
++)
337 if (colwidth
[icol
] == wanted
)
338 tbl
->cols
[icol
].width
= wanted
;
343 * Align numbers with text.
344 * Count columns to equalize and columns to maximize.
345 * Find maximum width of the columns to equalize.
346 * Find total width of the columns *not* to maximize.
351 for (icol
= 0; icol
<= maxcol
; icol
++) {
352 col
= tbl
->cols
+ icol
;
353 if (col
->width
> col
->nwidth
)
354 col
->decimal
+= (col
->width
- col
->nwidth
) / 2;
356 col
->width
= col
->nwidth
;
357 if (col
->flags
& TBL_CELL_EQUAL
) {
359 if (ewidth
< col
->width
)
362 if (col
->flags
& TBL_CELL_WMAX
)
365 xwidth
+= col
->width
;
369 * Equalize columns, if requested for any of them.
370 * Update total width of the columns not to maximize.
374 for (icol
= 0; icol
<= maxcol
; icol
++) {
375 col
= tbl
->cols
+ icol
;
376 if ( ! (col
->flags
& TBL_CELL_EQUAL
))
378 if (col
->width
== ewidth
)
380 if (nxcol
&& rmargin
)
381 xwidth
+= ewidth
- col
->width
;
387 * If there are any columns to maximize, find the total
388 * available width, deducting 3n margins between columns.
389 * Distribute the available width evenly.
392 if (nxcol
&& rmargin
) {
394 (opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
) ?
395 2 : !!opts
->lvert
+ !!opts
->rvert
);
396 if (rmargin
<= offset
+ xwidth
)
398 xwidth
= rmargin
- offset
- xwidth
;
401 * Emulate a bug in GNU tbl width calculation that
402 * manifests itself for large numbers of x-columns.
403 * Emulating it for 5 x-columns gives identical
404 * behaviour for up to 6 x-columns.
408 quirkcol
= xwidth
% nxcol
+ 2;
409 if (quirkcol
!= 3 && quirkcol
!= 4)
416 for (icol
= 0; icol
<= maxcol
; icol
++) {
417 col
= tbl
->cols
+ icol
;
418 if ( ! (col
->flags
& TBL_CELL_WMAX
))
420 col
->width
= (double)xwidth
* ++necol
/ nxcol
422 if (necol
== quirkcol
)
424 ewidth
+= col
->width
;
430 tblcalc_data(struct rofftbl
*tbl
, struct roffcol
*col
,
431 const struct tbl_opts
*opts
, const struct tbl_dat
*dp
, size_t mw
)
435 /* Branch down into data sub-types. */
437 switch (dp
->layout
->pos
) {
439 case TBL_CELL_DHORIZ
:
440 sz
= (*tbl
->len
)(1, tbl
->arg
);
441 if (col
!= NULL
&& col
->width
< sz
)
445 case TBL_CELL_CENTRE
:
448 return tblcalc_literal(tbl
, col
, dp
, mw
);
449 case TBL_CELL_NUMBER
:
450 return tblcalc_number(tbl
, col
, opts
, dp
);
459 tblcalc_literal(struct rofftbl
*tbl
, struct roffcol
*col
,
460 const struct tbl_dat
*dp
, size_t mw
)
462 const char *str
; /* Beginning of the first line. */
463 const char *beg
; /* Beginning of the current line. */
464 char *end
; /* End of the current line. */
465 size_t lsz
; /* Length of the current line. */
466 size_t wsz
; /* Length of the current word. */
467 size_t msz
; /* Length of the longest line. */
469 if (dp
->string
== NULL
|| *dp
->string
== '\0')
471 str
= mw
? mandoc_strdup(dp
->string
) : dp
->string
;
473 for (beg
= str
; beg
!= NULL
&& *beg
!= '\0'; beg
= end
) {
474 end
= mw
? strchr(beg
, ' ') : NULL
;
480 wsz
= (*tbl
->slen
)(beg
, tbl
->arg
);
481 if (mw
&& lsz
&& lsz
+ 1 + wsz
<= mw
)
490 if (col
!= NULL
&& col
->width
< msz
)
496 tblcalc_number(struct rofftbl
*tbl
, struct roffcol
*col
,
497 const struct tbl_opts
*opts
, const struct tbl_dat
*dp
)
499 const char *cp
, *lastdigit
, *lastpoint
;
503 if (dp
->string
== NULL
|| *dp
->string
== '\0')
506 totsz
= (*tbl
->slen
)(dp
->string
, tbl
->arg
);
511 * Find the last digit and
512 * the last decimal point that is adjacent to a digit.
513 * The alignment indicator "\&" overrides everything.
516 lastdigit
= lastpoint
= NULL
;
517 for (cp
= dp
->string
; cp
[0] != '\0'; cp
++) {
518 if (cp
[0] == '\\' && cp
[1] == '&') {
519 lastdigit
= lastpoint
= cp
;
521 } else if (cp
[0] == opts
->decimal
&&
522 (isdigit((unsigned char)cp
[1]) ||
523 (cp
> dp
->string
&& isdigit((unsigned char)cp
[-1]))))
525 else if (isdigit((unsigned char)cp
[0]))
529 /* Not a number, treat as a literal string. */
531 if (lastdigit
== NULL
) {
532 if (col
!= NULL
&& col
->width
< totsz
)
537 /* Measure the width of the integer part. */
539 if (lastpoint
== NULL
)
540 lastpoint
= lastdigit
+ 1;
543 for (cp
= dp
->string
; cp
< lastpoint
; cp
++) {
545 intsz
+= (*tbl
->slen
)(buf
, tbl
->arg
);
549 * If this number has more integer digits than all numbers
550 * seen on earlier lines, shift them all to the right.
551 * If it has fewer, shift this number to the right.
554 if (intsz
> col
->decimal
) {
555 col
->nwidth
+= intsz
- col
->decimal
;
556 col
->decimal
= intsz
;
558 totsz
+= col
->decimal
- intsz
;
560 /* Update the maximum total width seen so far. */
562 if (totsz
> col
->nwidth
)