]>
git.cameronkatri.com Git - mandoc.git/blob - out.c
1 /* $Id: out.c,v 1.83 2021/09/28 17:06:59 schwarze Exp $ */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2014, 2015, 2017, 2018, 2019, 2021
5 * Ingo Schwarze <schwarze@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
31 #include "mandoc_aux.h"
37 struct tbl_colgroup
*next
;
43 static size_t tblcalc_data(struct rofftbl
*, struct roffcol
*,
44 const struct tbl_opts
*, const struct tbl_dat
*,
46 static size_t tblcalc_literal(struct rofftbl
*, struct roffcol
*,
47 const struct tbl_dat
*, size_t);
48 static size_t tblcalc_number(struct rofftbl
*, struct roffcol
*,
49 const struct tbl_opts
*, const struct tbl_dat
*);
53 * Parse the *src string and store a scaling unit into *dst.
54 * If the string doesn't specify the unit, use the default.
55 * If no default is specified, fail.
56 * Return a pointer to the byte after the last byte used,
57 * or NULL on total failure.
60 a2roffsu(const char *src
, struct roffsu
*dst
, enum roffscale def
)
64 dst
->unit
= def
== SCALE_MAX
? SCALE_BU
: def
;
65 dst
->scale
= strtod(src
, &endptr
);
102 if (SCALE_MAX
== def
)
111 * Calculate the abstract widths and decimal positions of columns in a
112 * table. This routine allocates the columns structures then runs over
113 * all rows and cells in the table. The function pointers in "tbl" are
114 * used for the actual width calculations.
117 tblcalc(struct rofftbl
*tbl
, const struct tbl_span
*sp_first
,
118 size_t offset
, size_t rmargin
)
121 const struct tbl_opts
*opts
;
122 const struct tbl_span
*sp
;
123 const struct tbl_dat
*dp
;
125 struct tbl_colgroup
*first_group
, **gp
, *g
;
127 size_t ewidth
, min1
, min2
, wanted
, width
, xwidth
;
128 int done
, icol
, maxcol
, necol
, nxcol
, quirkcol
;
131 * Allocate the master column specifiers. These will hold the
132 * widths and decimal positions for all cells in the column. It
133 * must be freed and nullified by the caller.
136 assert(tbl
->cols
== NULL
);
137 tbl
->cols
= mandoc_calloc((size_t)sp_first
->opts
->cols
,
138 sizeof(struct roffcol
));
139 opts
= sp_first
->opts
;
143 for (sp
= sp_first
; sp
!= NULL
; sp
= sp
->next
) {
144 if (sp
->pos
!= TBL_SPAN_DATA
)
148 * Account for the data cells in the layout, matching it
149 * to data cells in the data section.
153 for (dp
= sp
->first
; dp
!= NULL
; dp
= dp
->next
) {
154 icol
= dp
->layout
->col
;
155 while (maxcol
< icol
+ dp
->hspans
)
156 tbl
->cols
[++maxcol
].spacing
= SIZE_MAX
;
157 col
= tbl
->cols
+ icol
;
158 col
->flags
|= dp
->layout
->flags
;
159 if (dp
->layout
->flags
& TBL_CELL_WIGN
)
162 /* Handle explicit width specifications. */
164 if (dp
->layout
->wstr
!= NULL
&&
165 dp
->layout
->width
== 0 &&
166 a2roffsu(dp
->layout
->wstr
, &su
, SCALE_EN
)
169 (*tbl
->sulen
)(&su
, tbl
->arg
);
170 if (col
->width
< dp
->layout
->width
)
171 col
->width
= dp
->layout
->width
;
172 if (dp
->layout
->spacing
!= SIZE_MAX
&&
173 (col
->spacing
== SIZE_MAX
||
174 col
->spacing
< dp
->layout
->spacing
))
175 col
->spacing
= dp
->layout
->spacing
;
178 * Calculate an automatic width.
179 * Except for spanning cells, apply it.
182 width
= tblcalc_data(tbl
,
183 dp
->hspans
== 0 ? col
: NULL
,
186 dp
->layout
->width
? dp
->layout
->width
:
187 rmargin
? (rmargin
+ sp
->opts
->cols
/ 2)
188 / (sp
->opts
->cols
+ 1) : 0);
193 * Build an ordered, singly linked list
194 * of all groups of columns joined by spans,
195 * recording the minimum width for each group.
198 while (*gp
!= NULL
&& ((*gp
)->startcol
< icol
||
199 (*gp
)->endcol
< icol
+ dp
->hspans
))
201 if (*gp
== NULL
|| (*gp
)->startcol
> icol
||
202 (*gp
)->endcol
> icol
+ dp
->hspans
) {
203 g
= mandoc_malloc(sizeof(*g
));
207 g
->endcol
= icol
+ dp
->hspans
;
209 } else if ((*gp
)->wanted
< width
)
210 (*gp
)->wanted
= width
;
215 * The minimum width of columns explicitly specified
216 * in the layout is 1n.
219 if (maxcol
< sp_first
->opts
->cols
- 1)
220 maxcol
= sp_first
->opts
->cols
- 1;
221 for (icol
= 0; icol
<= maxcol
; icol
++) {
222 col
= tbl
->cols
+ icol
;
227 * Column spacings are needed for span width
228 * calculations, so set the default values now.
231 if (col
->spacing
== SIZE_MAX
|| icol
== maxcol
)
236 * Replace the minimum widths with the missing widths,
237 * and dismiss groups that are already wide enough.
241 while ((g
= *gp
) != NULL
) {
243 for (icol
= g
->startcol
; icol
<= g
->endcol
; icol
++) {
244 width
= tbl
->cols
[icol
].width
;
245 if (icol
< g
->endcol
)
246 width
+= tbl
->cols
[icol
].spacing
;
247 if (g
->wanted
<= width
) {
251 (*gp
)->wanted
-= width
;
260 colwidth
= mandoc_reallocarray(NULL
, maxcol
+ 1, sizeof(*colwidth
));
261 while (first_group
!= NULL
) {
264 * Rebuild the array of the widths of all columns
265 * participating in spans that require expansion.
268 for (icol
= 0; icol
<= maxcol
; icol
++)
269 colwidth
[icol
] = SIZE_MAX
;
270 for (g
= first_group
; g
!= NULL
; g
= g
->next
)
271 for (icol
= g
->startcol
; icol
<= g
->endcol
; icol
++)
272 colwidth
[icol
] = tbl
->cols
[icol
].width
;
275 * Find the smallest and second smallest column width
276 * among the columns which may need expamsion.
279 min1
= min2
= SIZE_MAX
;
280 for (icol
= 0; icol
<= maxcol
; icol
++) {
281 width
= colwidth
[icol
];
285 } else if (min1
< width
&& min2
> width
)
290 * Find the minimum wanted width
291 * for any one of the narrowest columns,
292 * and mark the columns wanting that width.
296 for (g
= first_group
; g
!= NULL
; g
= g
->next
) {
298 for (icol
= g
->startcol
; icol
<= g
->endcol
; icol
++)
299 if (colwidth
[icol
] == min1
)
303 width
= min1
+ (g
->wanted
- 1) / necol
+ 1;
310 /* Record the effect of the widening. */
313 while ((g
= *gp
) != NULL
) {
315 for (icol
= g
->startcol
; icol
<= g
->endcol
; icol
++) {
316 if (colwidth
[icol
] != min1
)
318 if (g
->wanted
<= wanted
- min1
) {
319 tbl
->cols
[icol
].width
+= g
->wanted
;
323 tbl
->cols
[icol
].width
= wanted
;
324 g
->wanted
-= wanted
- min1
;
336 * Align numbers with text.
337 * Count columns to equalize and columns to maximize.
338 * Find maximum width of the columns to equalize.
339 * Find total width of the columns *not* to maximize.
344 for (icol
= 0; icol
<= maxcol
; icol
++) {
345 col
= tbl
->cols
+ icol
;
346 if (col
->width
> col
->nwidth
)
347 col
->decimal
+= (col
->width
- col
->nwidth
) / 2;
348 if (col
->flags
& TBL_CELL_EQUAL
) {
350 if (ewidth
< col
->width
)
353 if (col
->flags
& TBL_CELL_WMAX
)
356 xwidth
+= col
->width
;
360 * Equalize columns, if requested for any of them.
361 * Update total width of the columns not to maximize.
365 for (icol
= 0; icol
<= maxcol
; icol
++) {
366 col
= tbl
->cols
+ icol
;
367 if ( ! (col
->flags
& TBL_CELL_EQUAL
))
369 if (col
->width
== ewidth
)
371 if (nxcol
&& rmargin
)
372 xwidth
+= ewidth
- col
->width
;
378 * If there are any columns to maximize, find the total
379 * available width, deducting 3n margins between columns.
380 * Distribute the available width evenly.
383 if (nxcol
&& rmargin
) {
385 (opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
) ?
386 2 : !!opts
->lvert
+ !!opts
->rvert
);
387 if (rmargin
<= offset
+ xwidth
)
389 xwidth
= rmargin
- offset
- xwidth
;
392 * Emulate a bug in GNU tbl width calculation that
393 * manifests itself for large numbers of x-columns.
394 * Emulating it for 5 x-columns gives identical
395 * behaviour for up to 6 x-columns.
399 quirkcol
= xwidth
% nxcol
+ 2;
400 if (quirkcol
!= 3 && quirkcol
!= 4)
407 for (icol
= 0; icol
<= maxcol
; icol
++) {
408 col
= tbl
->cols
+ icol
;
409 if ( ! (col
->flags
& TBL_CELL_WMAX
))
411 col
->width
= (double)xwidth
* ++necol
/ nxcol
413 if (necol
== quirkcol
)
415 ewidth
+= col
->width
;
421 tblcalc_data(struct rofftbl
*tbl
, struct roffcol
*col
,
422 const struct tbl_opts
*opts
, const struct tbl_dat
*dp
, size_t mw
)
426 /* Branch down into data sub-types. */
428 switch (dp
->layout
->pos
) {
430 case TBL_CELL_DHORIZ
:
431 sz
= (*tbl
->len
)(1, tbl
->arg
);
432 if (col
!= NULL
&& col
->width
< sz
)
436 case TBL_CELL_CENTRE
:
439 return tblcalc_literal(tbl
, col
, dp
, mw
);
440 case TBL_CELL_NUMBER
:
441 return tblcalc_number(tbl
, col
, opts
, dp
);
450 tblcalc_literal(struct rofftbl
*tbl
, struct roffcol
*col
,
451 const struct tbl_dat
*dp
, size_t mw
)
453 const char *str
; /* Beginning of the first line. */
454 const char *beg
; /* Beginning of the current line. */
455 char *end
; /* End of the current line. */
456 size_t lsz
; /* Length of the current line. */
457 size_t wsz
; /* Length of the current word. */
458 size_t msz
; /* Length of the longest line. */
460 if (dp
->string
== NULL
|| *dp
->string
== '\0')
462 str
= mw
? mandoc_strdup(dp
->string
) : dp
->string
;
464 for (beg
= str
; beg
!= NULL
&& *beg
!= '\0'; beg
= end
) {
465 end
= mw
? strchr(beg
, ' ') : NULL
;
471 wsz
= (*tbl
->slen
)(beg
, tbl
->arg
);
472 if (mw
&& lsz
&& lsz
+ 1 + wsz
<= mw
)
481 if (col
!= NULL
&& col
->width
< msz
)
487 tblcalc_number(struct rofftbl
*tbl
, struct roffcol
*col
,
488 const struct tbl_opts
*opts
, const struct tbl_dat
*dp
)
490 const char *cp
, *lastdigit
, *lastpoint
;
494 if (dp
->string
== NULL
|| *dp
->string
== '\0')
497 totsz
= (*tbl
->slen
)(dp
->string
, tbl
->arg
);
502 * Find the last digit and
503 * the last decimal point that is adjacent to a digit.
504 * The alignment indicator "\&" overrides everything.
507 lastdigit
= lastpoint
= NULL
;
508 for (cp
= dp
->string
; cp
[0] != '\0'; cp
++) {
509 if (cp
[0] == '\\' && cp
[1] == '&') {
510 lastdigit
= lastpoint
= cp
;
512 } else if (cp
[0] == opts
->decimal
&&
513 (isdigit((unsigned char)cp
[1]) ||
514 (cp
> dp
->string
&& isdigit((unsigned char)cp
[-1]))))
516 else if (isdigit((unsigned char)cp
[0]))
520 /* Not a number, treat as a literal string. */
522 if (lastdigit
== NULL
) {
523 if (col
!= NULL
&& col
->width
< totsz
)
528 /* Measure the width of the integer part. */
530 if (lastpoint
== NULL
)
531 lastpoint
= lastdigit
+ 1;
534 for (cp
= dp
->string
; cp
< lastpoint
; cp
++) {
536 intsz
+= (*tbl
->slen
)(buf
, tbl
->arg
);
540 * If this number has more integer digits than all numbers
541 * seen on earlier lines, shift them all to the right.
542 * If it has fewer, shift this number to the right.
545 if (intsz
> col
->decimal
) {
546 col
->nwidth
+= intsz
- col
->decimal
;
547 col
->decimal
= intsz
;
549 totsz
+= col
->decimal
- intsz
;
551 /* Update the maximum total width seen so far. */
553 if (totsz
> col
->nwidth
)
555 if (col
->nwidth
> col
->width
)
556 col
->width
= col
->nwidth
;