xref: /illumos-gate/usr/src/cmd/mandoc/out.c (revision 4d131170)
1*4d131170SRobert Mustacchi /*	$Id: out.c,v 1.82 2021/09/07 17:07:58 schwarze Exp $ */
295c635efSGarrett D'Amore /*
395c635efSGarrett D'Amore  * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4*4d131170SRobert Mustacchi  * Copyright (c) 2011, 2014, 2015, 2017, 2018, 2019, 2021
5*4d131170SRobert Mustacchi  *               Ingo Schwarze <schwarze@openbsd.org>
695c635efSGarrett D'Amore  *
795c635efSGarrett D'Amore  * Permission to use, copy, modify, and distribute this software for any
895c635efSGarrett D'Amore  * purpose with or without fee is hereby granted, provided that the above
995c635efSGarrett D'Amore  * copyright notice and this permission notice appear in all copies.
1095c635efSGarrett D'Amore  *
1195c635efSGarrett D'Amore  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1295c635efSGarrett D'Amore  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1395c635efSGarrett D'Amore  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1495c635efSGarrett D'Amore  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1595c635efSGarrett D'Amore  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1695c635efSGarrett D'Amore  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1795c635efSGarrett D'Amore  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1895c635efSGarrett D'Amore  */
1995c635efSGarrett D'Amore #include "config.h"
2095c635efSGarrett D'Amore 
2195c635efSGarrett D'Amore #include <sys/types.h>
2295c635efSGarrett D'Amore 
2395c635efSGarrett D'Amore #include <assert.h>
24cec8643bSMichal Nowak #include <ctype.h>
25c66b8046SYuri Pankov #include <stdint.h>
26*4d131170SRobert Mustacchi #include <stdio.h>
2795c635efSGarrett D'Amore #include <stdlib.h>
2895c635efSGarrett D'Amore #include <string.h>
2995c635efSGarrett D'Amore #include <time.h>
3095c635efSGarrett D'Amore 
31260e9a87SYuri Pankov #include "mandoc_aux.h"
32*4d131170SRobert Mustacchi #include "mandoc.h"
33cec8643bSMichal Nowak #include "tbl.h"
3495c635efSGarrett D'Amore #include "out.h"
3595c635efSGarrett D'Amore 
36cec8643bSMichal Nowak struct	tbl_colgroup {
37cec8643bSMichal Nowak 	struct tbl_colgroup	*next;
38cec8643bSMichal Nowak 	size_t			 wanted;
39cec8643bSMichal Nowak 	int			 startcol;
40cec8643bSMichal Nowak 	int			 endcol;
41cec8643bSMichal Nowak };
42cec8643bSMichal Nowak 
43cec8643bSMichal Nowak static	size_t	tblcalc_data(struct rofftbl *, struct roffcol *,
44c66b8046SYuri Pankov 			const struct tbl_opts *, const struct tbl_dat *,
45c66b8046SYuri Pankov 			size_t);
46cec8643bSMichal Nowak static	size_t	tblcalc_literal(struct rofftbl *, struct roffcol *,
47c66b8046SYuri Pankov 			const struct tbl_dat *, size_t);
48cec8643bSMichal Nowak static	size_t	tblcalc_number(struct rofftbl *, struct roffcol *,
49698f87a4SGarrett D'Amore 			const struct tbl_opts *, const struct tbl_dat *);
5095c635efSGarrett D'Amore 
51260e9a87SYuri Pankov 
52260e9a87SYuri Pankov /*
53260e9a87SYuri Pankov  * Parse the *src string and store a scaling unit into *dst.
54260e9a87SYuri Pankov  * If the string doesn't specify the unit, use the default.
55260e9a87SYuri Pankov  * If no default is specified, fail.
56c66b8046SYuri Pankov  * Return a pointer to the byte after the last byte used,
57c66b8046SYuri Pankov  * or NULL on total failure.
5895c635efSGarrett D'Amore  */
59c66b8046SYuri Pankov const char *
a2roffsu(const char * src,struct roffsu * dst,enum roffscale def)6095c635efSGarrett D'Amore a2roffsu(const char *src, struct roffsu *dst, enum roffscale def)
6195c635efSGarrett D'Amore {
62260e9a87SYuri Pankov 	char		*endptr;
6395c635efSGarrett D'Amore 
64260e9a87SYuri Pankov 	dst->unit = def == SCALE_MAX ? SCALE_BU : def;
65260e9a87SYuri Pankov 	dst->scale = strtod(src, &endptr);
66260e9a87SYuri Pankov 	if (endptr == src)
67c66b8046SYuri Pankov 		return NULL;
6895c635efSGarrett D'Amore 
69260e9a87SYuri Pankov 	switch (*endptr++) {
70260e9a87SYuri Pankov 	case 'c':
71260e9a87SYuri Pankov 		dst->unit = SCALE_CM;
7295c635efSGarrett D'Amore 		break;
73260e9a87SYuri Pankov 	case 'i':
74260e9a87SYuri Pankov 		dst->unit = SCALE_IN;
7595c635efSGarrett D'Amore 		break;
76260e9a87SYuri Pankov 	case 'f':
77260e9a87SYuri Pankov 		dst->unit = SCALE_FS;
7895c635efSGarrett D'Amore 		break;
79260e9a87SYuri Pankov 	case 'M':
80260e9a87SYuri Pankov 		dst->unit = SCALE_MM;
8195c635efSGarrett D'Amore 		break;
82260e9a87SYuri Pankov 	case 'm':
83260e9a87SYuri Pankov 		dst->unit = SCALE_EM;
8495c635efSGarrett D'Amore 		break;
85260e9a87SYuri Pankov 	case 'n':
86260e9a87SYuri Pankov 		dst->unit = SCALE_EN;
8795c635efSGarrett D'Amore 		break;
88260e9a87SYuri Pankov 	case 'P':
89260e9a87SYuri Pankov 		dst->unit = SCALE_PC;
9095c635efSGarrett D'Amore 		break;
91260e9a87SYuri Pankov 	case 'p':
92260e9a87SYuri Pankov 		dst->unit = SCALE_PT;
9395c635efSGarrett D'Amore 		break;
94260e9a87SYuri Pankov 	case 'u':
95260e9a87SYuri Pankov 		dst->unit = SCALE_BU;
9695c635efSGarrett D'Amore 		break;
97260e9a87SYuri Pankov 	case 'v':
98260e9a87SYuri Pankov 		dst->unit = SCALE_VS;
9995c635efSGarrett D'Amore 		break;
100260e9a87SYuri Pankov 	default:
101c66b8046SYuri Pankov 		endptr--;
10295c635efSGarrett D'Amore 		if (SCALE_MAX == def)
103c66b8046SYuri Pankov 			return NULL;
104260e9a87SYuri Pankov 		dst->unit = def;
10595c635efSGarrett D'Amore 		break;
10695c635efSGarrett D'Amore 	}
107c66b8046SYuri Pankov 	return endptr;
10895c635efSGarrett D'Amore }
10995c635efSGarrett D'Amore 
11095c635efSGarrett D'Amore /*
11195c635efSGarrett D'Amore  * Calculate the abstract widths and decimal positions of columns in a
11295c635efSGarrett D'Amore  * table.  This routine allocates the columns structures then runs over
11395c635efSGarrett D'Amore  * all rows and cells in the table.  The function pointers in "tbl" are
11495c635efSGarrett D'Amore  * used for the actual width calculations.
11595c635efSGarrett D'Amore  */
11695c635efSGarrett D'Amore void
tblcalc(struct rofftbl * tbl,const struct tbl_span * sp_first,size_t offset,size_t rmargin)117cec8643bSMichal Nowak tblcalc(struct rofftbl *tbl, const struct tbl_span *sp_first,
118c66b8046SYuri Pankov     size_t offset, size_t rmargin)
11995c635efSGarrett D'Amore {
120c66b8046SYuri Pankov 	struct roffsu		 su;
121260e9a87SYuri Pankov 	const struct tbl_opts	*opts;
122cec8643bSMichal Nowak 	const struct tbl_span	*sp;
12395c635efSGarrett D'Amore 	const struct tbl_dat	*dp;
12495c635efSGarrett D'Amore 	struct roffcol		*col;
125cec8643bSMichal Nowak 	struct tbl_colgroup	*first_group, **gp, *g;
126cec8643bSMichal Nowak 	size_t			 ewidth, min1, min2, wanted, width, xwidth;
127cec8643bSMichal Nowak 	int			 done, icol, maxcol, necol, nxcol, quirkcol;
12895c635efSGarrett D'Amore 
12995c635efSGarrett D'Amore 	/*
13095c635efSGarrett D'Amore 	 * Allocate the master column specifiers.  These will hold the
13195c635efSGarrett D'Amore 	 * widths and decimal positions for all cells in the column.  It
13295c635efSGarrett D'Amore 	 * must be freed and nullified by the caller.
13395c635efSGarrett D'Amore 	 */
13495c635efSGarrett D'Amore 
135cec8643bSMichal Nowak 	assert(tbl->cols == NULL);
136cec8643bSMichal Nowak 	tbl->cols = mandoc_calloc((size_t)sp_first->opts->cols,
137260e9a87SYuri Pankov 	    sizeof(struct roffcol));
138cec8643bSMichal Nowak 	opts = sp_first->opts;
13995c635efSGarrett D'Amore 
140cec8643bSMichal Nowak 	maxcol = -1;
141cec8643bSMichal Nowak 	first_group = NULL;
142cec8643bSMichal Nowak 	for (sp = sp_first; sp != NULL; sp = sp->next) {
143cec8643bSMichal Nowak 		if (sp->pos != TBL_SPAN_DATA)
14495c635efSGarrett D'Amore 			continue;
145cec8643bSMichal Nowak 
14695c635efSGarrett D'Amore 		/*
14795c635efSGarrett D'Amore 		 * Account for the data cells in the layout, matching it
14895c635efSGarrett D'Amore 		 * to data cells in the data section.
14995c635efSGarrett D'Amore 		 */
150cec8643bSMichal Nowak 
151cec8643bSMichal Nowak 		gp = &first_group;
152cec8643bSMichal Nowak 		for (dp = sp->first; dp != NULL; dp = dp->next) {
153260e9a87SYuri Pankov 			icol = dp->layout->col;
154cec8643bSMichal Nowak 			while (maxcol < icol + dp->hspans)
155c66b8046SYuri Pankov 				tbl->cols[++maxcol].spacing = SIZE_MAX;
156260e9a87SYuri Pankov 			col = tbl->cols + icol;
157260e9a87SYuri Pankov 			col->flags |= dp->layout->flags;
158260e9a87SYuri Pankov 			if (dp->layout->flags & TBL_CELL_WIGN)
159260e9a87SYuri Pankov 				continue;
160cec8643bSMichal Nowak 
161cec8643bSMichal Nowak 			/* Handle explicit width specifications. */
162cec8643bSMichal Nowak 
163c66b8046SYuri Pankov 			if (dp->layout->wstr != NULL &&
164c66b8046SYuri Pankov 			    dp->layout->width == 0 &&
165c66b8046SYuri Pankov 			    a2roffsu(dp->layout->wstr, &su, SCALE_EN)
166c66b8046SYuri Pankov 			    != NULL)
167c66b8046SYuri Pankov 				dp->layout->width =
168c66b8046SYuri Pankov 				    (*tbl->sulen)(&su, tbl->arg);
169c66b8046SYuri Pankov 			if (col->width < dp->layout->width)
170c66b8046SYuri Pankov 				col->width = dp->layout->width;
171c66b8046SYuri Pankov 			if (dp->layout->spacing != SIZE_MAX &&
172c66b8046SYuri Pankov 			    (col->spacing == SIZE_MAX ||
173c66b8046SYuri Pankov 			     col->spacing < dp->layout->spacing))
174c66b8046SYuri Pankov 				col->spacing = dp->layout->spacing;
175cec8643bSMichal Nowak 
176cec8643bSMichal Nowak 			/*
177cec8643bSMichal Nowak 			 * Calculate an automatic width.
178cec8643bSMichal Nowak 			 * Except for spanning cells, apply it.
179cec8643bSMichal Nowak 			 */
180cec8643bSMichal Nowak 
181cec8643bSMichal Nowak 			width = tblcalc_data(tbl,
182cec8643bSMichal Nowak 			    dp->hspans == 0 ? col : NULL,
183cec8643bSMichal Nowak 			    opts, dp,
184c66b8046SYuri Pankov 			    dp->block == 0 ? 0 :
185c66b8046SYuri Pankov 			    dp->layout->width ? dp->layout->width :
186c66b8046SYuri Pankov 			    rmargin ? (rmargin + sp->opts->cols / 2)
187c66b8046SYuri Pankov 			    / (sp->opts->cols + 1) : 0);
188cec8643bSMichal Nowak 			if (dp->hspans == 0)
189cec8643bSMichal Nowak 				continue;
190cec8643bSMichal Nowak 
191cec8643bSMichal Nowak 			/*
192cec8643bSMichal Nowak 			 * Build an ordered, singly linked list
193cec8643bSMichal Nowak 			 * of all groups of columns joined by spans,
194cec8643bSMichal Nowak 			 * recording the minimum width for each group.
195cec8643bSMichal Nowak 			 */
196cec8643bSMichal Nowak 
197cec8643bSMichal Nowak 			while (*gp != NULL && ((*gp)->startcol < icol ||
198cec8643bSMichal Nowak 			    (*gp)->endcol < icol + dp->hspans))
199cec8643bSMichal Nowak 				gp = &(*gp)->next;
200cec8643bSMichal Nowak 			if (*gp == NULL || (*gp)->startcol > icol ||
201cec8643bSMichal Nowak                             (*gp)->endcol > icol + dp->hspans) {
202cec8643bSMichal Nowak 				g = mandoc_malloc(sizeof(*g));
203cec8643bSMichal Nowak 				g->next = *gp;
204cec8643bSMichal Nowak 				g->wanted = width;
205cec8643bSMichal Nowak 				g->startcol = icol;
206cec8643bSMichal Nowak 				g->endcol = icol + dp->hspans;
207cec8643bSMichal Nowak 				*gp = g;
208cec8643bSMichal Nowak 			} else if ((*gp)->wanted < width)
209cec8643bSMichal Nowak 				(*gp)->wanted = width;
210cec8643bSMichal Nowak 		}
211cec8643bSMichal Nowak 	}
212cec8643bSMichal Nowak 
213cec8643bSMichal Nowak 	/*
214*4d131170SRobert Mustacchi 	 * The minimum width of columns explicitly specified
215*4d131170SRobert Mustacchi 	 * in the layout is 1n.
216cec8643bSMichal Nowak 	 */
217cec8643bSMichal Nowak 
218*4d131170SRobert Mustacchi 	if (maxcol < sp_first->opts->cols - 1)
219*4d131170SRobert Mustacchi 		maxcol = sp_first->opts->cols - 1;
220*4d131170SRobert Mustacchi 	for (icol = 0; icol <= maxcol; icol++) {
221*4d131170SRobert Mustacchi 		col = tbl->cols + icol;
222*4d131170SRobert Mustacchi 		if (col->width < 1)
223*4d131170SRobert Mustacchi 			col->width = 1;
224*4d131170SRobert Mustacchi 
225*4d131170SRobert Mustacchi 		/*
226*4d131170SRobert Mustacchi 		 * Column spacings are needed for span width
227*4d131170SRobert Mustacchi 		 * calculations, so set the default values now.
228*4d131170SRobert Mustacchi 		 */
229*4d131170SRobert Mustacchi 
230*4d131170SRobert Mustacchi 		if (col->spacing == SIZE_MAX || icol == maxcol)
231*4d131170SRobert Mustacchi 			col->spacing = 3;
232*4d131170SRobert Mustacchi 	}
233cec8643bSMichal Nowak 
234cec8643bSMichal Nowak 	/*
235cec8643bSMichal Nowak 	 * Replace the minimum widths with the missing widths,
236cec8643bSMichal Nowak 	 * and dismiss groups that are already wide enough.
237cec8643bSMichal Nowak 	 */
238cec8643bSMichal Nowak 
239cec8643bSMichal Nowak 	gp = &first_group;
240cec8643bSMichal Nowak 	while ((g = *gp) != NULL) {
241cec8643bSMichal Nowak 		done = 0;
242cec8643bSMichal Nowak 		for (icol = g->startcol; icol <= g->endcol; icol++) {
243cec8643bSMichal Nowak 			width = tbl->cols[icol].width;
244cec8643bSMichal Nowak 			if (icol < g->endcol)
245cec8643bSMichal Nowak 				width += tbl->cols[icol].spacing;
246cec8643bSMichal Nowak 			if (g->wanted <= width) {
247cec8643bSMichal Nowak 				done = 1;
248cec8643bSMichal Nowak 				break;
249cec8643bSMichal Nowak 			} else
250cec8643bSMichal Nowak 				(*gp)->wanted -= width;
251cec8643bSMichal Nowak 		}
252cec8643bSMichal Nowak 		if (done) {
253cec8643bSMichal Nowak 			*gp = g->next;
254cec8643bSMichal Nowak 			free(g);
255cec8643bSMichal Nowak 		} else
256cec8643bSMichal Nowak 			gp = &(*gp)->next;
257cec8643bSMichal Nowak 	}
258cec8643bSMichal Nowak 
259cec8643bSMichal Nowak 	while (first_group != NULL) {
260cec8643bSMichal Nowak 
261cec8643bSMichal Nowak 		/*
262cec8643bSMichal Nowak 		 * Find the smallest and second smallest column width
263cec8643bSMichal Nowak 		 * among the columns which may need expamsion.
264cec8643bSMichal Nowak 		 */
265cec8643bSMichal Nowak 
266cec8643bSMichal Nowak 		min1 = min2 = SIZE_MAX;
267cec8643bSMichal Nowak 		for (icol = 0; icol <= maxcol; icol++) {
268*4d131170SRobert Mustacchi 			width = tbl->cols[icol].width;
269*4d131170SRobert Mustacchi 			if (min1 > width) {
270cec8643bSMichal Nowak 				min2 = min1;
271*4d131170SRobert Mustacchi 				min1 = width;
272*4d131170SRobert Mustacchi 			} else if (min1 < width && min2 > width)
273*4d131170SRobert Mustacchi 				min2 = width;
274cec8643bSMichal Nowak 		}
275cec8643bSMichal Nowak 
276cec8643bSMichal Nowak 		/*
277cec8643bSMichal Nowak 		 * Find the minimum wanted width
278cec8643bSMichal Nowak 		 * for any one of the narrowest columns,
279cec8643bSMichal Nowak 		 * and mark the columns wanting that width.
280cec8643bSMichal Nowak 		 */
281cec8643bSMichal Nowak 
282cec8643bSMichal Nowak 		wanted = min2;
283cec8643bSMichal Nowak 		for (g = first_group; g != NULL; g = g->next) {
284cec8643bSMichal Nowak 			necol = 0;
285cec8643bSMichal Nowak 			for (icol = g->startcol; icol <= g->endcol; icol++)
286cec8643bSMichal Nowak 				if (tbl->cols[icol].width == min1)
287cec8643bSMichal Nowak 					necol++;
288cec8643bSMichal Nowak 			if (necol == 0)
289cec8643bSMichal Nowak 				continue;
290cec8643bSMichal Nowak 			width = min1 + (g->wanted - 1) / necol + 1;
291cec8643bSMichal Nowak 			if (width > min2)
292cec8643bSMichal Nowak 				width = min2;
293cec8643bSMichal Nowak 			if (wanted > width)
294cec8643bSMichal Nowak 				wanted = width;
295cec8643bSMichal Nowak 		}
296cec8643bSMichal Nowak 
297*4d131170SRobert Mustacchi 		/* Record the effect of the widening. */
298cec8643bSMichal Nowak 
299cec8643bSMichal Nowak 		gp = &first_group;
300cec8643bSMichal Nowak 		while ((g = *gp) != NULL) {
301cec8643bSMichal Nowak 			done = 0;
302cec8643bSMichal Nowak 			for (icol = g->startcol; icol <= g->endcol; icol++) {
303*4d131170SRobert Mustacchi 				if (tbl->cols[icol].width != min1)
304cec8643bSMichal Nowak 					continue;
305cec8643bSMichal Nowak 				if (g->wanted <= wanted - min1) {
306*4d131170SRobert Mustacchi 					tbl->cols[icol].width += g->wanted;
307cec8643bSMichal Nowak 					done = 1;
308cec8643bSMichal Nowak 					break;
309cec8643bSMichal Nowak 				}
310*4d131170SRobert Mustacchi 				tbl->cols[icol].width = wanted;
311cec8643bSMichal Nowak 				g->wanted -= wanted - min1;
312cec8643bSMichal Nowak 			}
313cec8643bSMichal Nowak 			if (done) {
314cec8643bSMichal Nowak 				*gp = g->next;
315cec8643bSMichal Nowak 				free(g);
316cec8643bSMichal Nowak 			} else
317cec8643bSMichal Nowak 				gp = &(*gp)->next;
318260e9a87SYuri Pankov 		}
319260e9a87SYuri Pankov 	}
320260e9a87SYuri Pankov 
321260e9a87SYuri Pankov 	/*
322cec8643bSMichal Nowak 	 * Align numbers with text.
323260e9a87SYuri Pankov 	 * Count columns to equalize and columns to maximize.
324260e9a87SYuri Pankov 	 * Find maximum width of the columns to equalize.
325260e9a87SYuri Pankov 	 * Find total width of the columns *not* to maximize.
326260e9a87SYuri Pankov 	 */
327260e9a87SYuri Pankov 
328260e9a87SYuri Pankov 	necol = nxcol = 0;
329260e9a87SYuri Pankov 	ewidth = xwidth = 0;
330260e9a87SYuri Pankov 	for (icol = 0; icol <= maxcol; icol++) {
331260e9a87SYuri Pankov 		col = tbl->cols + icol;
332cec8643bSMichal Nowak 		if (col->width > col->nwidth)
333cec8643bSMichal Nowak 			col->decimal += (col->width - col->nwidth) / 2;
334260e9a87SYuri Pankov 		if (col->flags & TBL_CELL_EQUAL) {
335260e9a87SYuri Pankov 			necol++;
336260e9a87SYuri Pankov 			if (ewidth < col->width)
337260e9a87SYuri Pankov 				ewidth = col->width;
338260e9a87SYuri Pankov 		}
339260e9a87SYuri Pankov 		if (col->flags & TBL_CELL_WMAX)
340260e9a87SYuri Pankov 			nxcol++;
341260e9a87SYuri Pankov 		else
342260e9a87SYuri Pankov 			xwidth += col->width;
343260e9a87SYuri Pankov 	}
344260e9a87SYuri Pankov 
345260e9a87SYuri Pankov 	/*
346260e9a87SYuri Pankov 	 * Equalize columns, if requested for any of them.
347260e9a87SYuri Pankov 	 * Update total width of the columns not to maximize.
348260e9a87SYuri Pankov 	 */
349260e9a87SYuri Pankov 
350260e9a87SYuri Pankov 	if (necol) {
351260e9a87SYuri Pankov 		for (icol = 0; icol <= maxcol; icol++) {
352260e9a87SYuri Pankov 			col = tbl->cols + icol;
353260e9a87SYuri Pankov 			if ( ! (col->flags & TBL_CELL_EQUAL))
354260e9a87SYuri Pankov 				continue;
355260e9a87SYuri Pankov 			if (col->width == ewidth)
356260e9a87SYuri Pankov 				continue;
357c66b8046SYuri Pankov 			if (nxcol && rmargin)
358260e9a87SYuri Pankov 				xwidth += ewidth - col->width;
359260e9a87SYuri Pankov 			col->width = ewidth;
360260e9a87SYuri Pankov 		}
361260e9a87SYuri Pankov 	}
362260e9a87SYuri Pankov 
363260e9a87SYuri Pankov 	/*
364260e9a87SYuri Pankov 	 * If there are any columns to maximize, find the total
365260e9a87SYuri Pankov 	 * available width, deducting 3n margins between columns.
366260e9a87SYuri Pankov 	 * Distribute the available width evenly.
367260e9a87SYuri Pankov 	 */
368260e9a87SYuri Pankov 
369c66b8046SYuri Pankov 	if (nxcol && rmargin) {
370c66b8046SYuri Pankov 		xwidth += 3*maxcol +
371260e9a87SYuri Pankov 		    (opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ?
372260e9a87SYuri Pankov 		     2 : !!opts->lvert + !!opts->rvert);
373c66b8046SYuri Pankov 		if (rmargin <= offset + xwidth)
374c66b8046SYuri Pankov 			return;
375c66b8046SYuri Pankov 		xwidth = rmargin - offset - xwidth;
376260e9a87SYuri Pankov 
377260e9a87SYuri Pankov 		/*
378260e9a87SYuri Pankov 		 * Emulate a bug in GNU tbl width calculation that
379260e9a87SYuri Pankov 		 * manifests itself for large numbers of x-columns.
380260e9a87SYuri Pankov 		 * Emulating it for 5 x-columns gives identical
381260e9a87SYuri Pankov 		 * behaviour for up to 6 x-columns.
382260e9a87SYuri Pankov 		 */
383260e9a87SYuri Pankov 
384260e9a87SYuri Pankov 		if (nxcol == 5) {
385260e9a87SYuri Pankov 			quirkcol = xwidth % nxcol + 2;
386260e9a87SYuri Pankov 			if (quirkcol != 3 && quirkcol != 4)
387260e9a87SYuri Pankov 				quirkcol = -1;
388260e9a87SYuri Pankov 		} else
389260e9a87SYuri Pankov 			quirkcol = -1;
390260e9a87SYuri Pankov 
391260e9a87SYuri Pankov 		necol = 0;
392260e9a87SYuri Pankov 		ewidth = 0;
393260e9a87SYuri Pankov 		for (icol = 0; icol <= maxcol; icol++) {
394260e9a87SYuri Pankov 			col = tbl->cols + icol;
395260e9a87SYuri Pankov 			if ( ! (col->flags & TBL_CELL_WMAX))
396260e9a87SYuri Pankov 				continue;
397260e9a87SYuri Pankov 			col->width = (double)xwidth * ++necol / nxcol
398260e9a87SYuri Pankov 			    - ewidth + 0.4995;
399260e9a87SYuri Pankov 			if (necol == quirkcol)
400260e9a87SYuri Pankov 				col->width--;
401260e9a87SYuri Pankov 			ewidth += col->width;
40295c635efSGarrett D'Amore 		}
40395c635efSGarrett D'Amore 	}
40495c635efSGarrett D'Amore }
40595c635efSGarrett D'Amore 
406cec8643bSMichal Nowak static size_t
tblcalc_data(struct rofftbl * tbl,struct roffcol * col,const struct tbl_opts * opts,const struct tbl_dat * dp,size_t mw)40795c635efSGarrett D'Amore tblcalc_data(struct rofftbl *tbl, struct roffcol *col,
408c66b8046SYuri Pankov     const struct tbl_opts *opts, const struct tbl_dat *dp, size_t mw)
40995c635efSGarrett D'Amore {
41095c635efSGarrett D'Amore 	size_t		 sz;
41195c635efSGarrett D'Amore 
41295c635efSGarrett D'Amore 	/* Branch down into data sub-types. */
41395c635efSGarrett D'Amore 
41495c635efSGarrett D'Amore 	switch (dp->layout->pos) {
415260e9a87SYuri Pankov 	case TBL_CELL_HORIZ:
416260e9a87SYuri Pankov 	case TBL_CELL_DHORIZ:
41795c635efSGarrett D'Amore 		sz = (*tbl->len)(1, tbl->arg);
418cec8643bSMichal Nowak 		if (col != NULL && col->width < sz)
41995c635efSGarrett D'Amore 			col->width = sz;
420cec8643bSMichal Nowak 		return sz;
421260e9a87SYuri Pankov 	case TBL_CELL_LONG:
422260e9a87SYuri Pankov 	case TBL_CELL_CENTRE:
423260e9a87SYuri Pankov 	case TBL_CELL_LEFT:
424260e9a87SYuri Pankov 	case TBL_CELL_RIGHT:
425cec8643bSMichal Nowak 		return tblcalc_literal(tbl, col, dp, mw);
426260e9a87SYuri Pankov 	case TBL_CELL_NUMBER:
427cec8643bSMichal Nowak 		return tblcalc_number(tbl, col, opts, dp);
428260e9a87SYuri Pankov 	case TBL_CELL_DOWN:
429cec8643bSMichal Nowak 		return 0;
43095c635efSGarrett D'Amore 	default:
43195c635efSGarrett D'Amore 		abort();
43295c635efSGarrett D'Amore 	}
43395c635efSGarrett D'Amore }
43495c635efSGarrett D'Amore 
435cec8643bSMichal Nowak static size_t
tblcalc_literal(struct rofftbl * tbl,struct roffcol * col,const struct tbl_dat * dp,size_t mw)43695c635efSGarrett D'Amore tblcalc_literal(struct rofftbl *tbl, struct roffcol *col,
437c66b8046SYuri Pankov     const struct tbl_dat *dp, size_t mw)
43895c635efSGarrett D'Amore {
439c66b8046SYuri Pankov 	const char	*str;	/* Beginning of the first line. */
440c66b8046SYuri Pankov 	const char	*beg;	/* Beginning of the current line. */
441c66b8046SYuri Pankov 	char		*end;	/* End of the current line. */
442c66b8046SYuri Pankov 	size_t		 lsz;	/* Length of the current line. */
443c66b8046SYuri Pankov 	size_t		 wsz;	/* Length of the current word. */
444cec8643bSMichal Nowak 	size_t		 msz;   /* Length of the longest line. */
44595c635efSGarrett D'Amore 
446c66b8046SYuri Pankov 	if (dp->string == NULL || *dp->string == '\0')
447cec8643bSMichal Nowak 		return 0;
448c66b8046SYuri Pankov 	str = mw ? mandoc_strdup(dp->string) : dp->string;
449cec8643bSMichal Nowak 	msz = lsz = 0;
450c66b8046SYuri Pankov 	for (beg = str; beg != NULL && *beg != '\0'; beg = end) {
451c66b8046SYuri Pankov 		end = mw ? strchr(beg, ' ') : NULL;
452c66b8046SYuri Pankov 		if (end != NULL) {
453c66b8046SYuri Pankov 			*end++ = '\0';
454c66b8046SYuri Pankov 			while (*end == ' ')
455c66b8046SYuri Pankov 				end++;
456c66b8046SYuri Pankov 		}
457c66b8046SYuri Pankov 		wsz = (*tbl->slen)(beg, tbl->arg);
458c66b8046SYuri Pankov 		if (mw && lsz && lsz + 1 + wsz <= mw)
459c66b8046SYuri Pankov 			lsz += 1 + wsz;
460c66b8046SYuri Pankov 		else
461c66b8046SYuri Pankov 			lsz = wsz;
462cec8643bSMichal Nowak 		if (msz < lsz)
463cec8643bSMichal Nowak 			msz = lsz;
464c66b8046SYuri Pankov 	}
465c66b8046SYuri Pankov 	if (mw)
466c66b8046SYuri Pankov 		free((void *)str);
467cec8643bSMichal Nowak 	if (col != NULL && col->width < msz)
468cec8643bSMichal Nowak 		col->width = msz;
469cec8643bSMichal Nowak 	return msz;
47095c635efSGarrett D'Amore }
47195c635efSGarrett D'Amore 
472cec8643bSMichal Nowak static size_t
tblcalc_number(struct rofftbl * tbl,struct roffcol * col,const struct tbl_opts * opts,const struct tbl_dat * dp)47395c635efSGarrett D'Amore tblcalc_number(struct rofftbl *tbl, struct roffcol *col,
474698f87a4SGarrett D'Amore 		const struct tbl_opts *opts, const struct tbl_dat *dp)
47595c635efSGarrett D'Amore {
476cec8643bSMichal Nowak 	const char	*cp, *lastdigit, *lastpoint;
477cec8643bSMichal Nowak 	size_t		 intsz, totsz;
47895c635efSGarrett D'Amore 	char		 buf[2];
47995c635efSGarrett D'Amore 
480cec8643bSMichal Nowak 	if (dp->string == NULL || *dp->string == '\0')
481cec8643bSMichal Nowak 		return 0;
482cec8643bSMichal Nowak 
483cec8643bSMichal Nowak 	totsz = (*tbl->slen)(dp->string, tbl->arg);
484cec8643bSMichal Nowak 	if (col == NULL)
485cec8643bSMichal Nowak 		return totsz;
486cec8643bSMichal Nowak 
48795c635efSGarrett D'Amore 	/*
488cec8643bSMichal Nowak 	 * Find the last digit and
489cec8643bSMichal Nowak 	 * the last decimal point that is adjacent to a digit.
490cec8643bSMichal Nowak 	 * The alignment indicator "\&" overrides everything.
49195c635efSGarrett D'Amore 	 */
49295c635efSGarrett D'Amore 
493cec8643bSMichal Nowak 	lastdigit = lastpoint = NULL;
494cec8643bSMichal Nowak 	for (cp = dp->string; cp[0] != '\0'; cp++) {
495cec8643bSMichal Nowak 		if (cp[0] == '\\' && cp[1] == '&') {
496cec8643bSMichal Nowak 			lastdigit = lastpoint = cp;
497cec8643bSMichal Nowak 			break;
498cec8643bSMichal Nowak 		} else if (cp[0] == opts->decimal &&
499cec8643bSMichal Nowak 		    (isdigit((unsigned char)cp[1]) ||
500cec8643bSMichal Nowak 		     (cp > dp->string && isdigit((unsigned char)cp[-1]))))
501cec8643bSMichal Nowak 			lastpoint = cp;
502cec8643bSMichal Nowak 		else if (isdigit((unsigned char)cp[0]))
503cec8643bSMichal Nowak 			lastdigit = cp;
504cec8643bSMichal Nowak 	}
50595c635efSGarrett D'Amore 
506cec8643bSMichal Nowak 	/* Not a number, treat as a literal string. */
50795c635efSGarrett D'Amore 
508cec8643bSMichal Nowak 	if (lastdigit == NULL) {
509cec8643bSMichal Nowak 		if (col != NULL && col->width < totsz)
510cec8643bSMichal Nowak 			col->width = totsz;
511cec8643bSMichal Nowak 		return totsz;
512cec8643bSMichal Nowak 	}
51395c635efSGarrett D'Amore 
514cec8643bSMichal Nowak 	/* Measure the width of the integer part. */
51595c635efSGarrett D'Amore 
516cec8643bSMichal Nowak 	if (lastpoint == NULL)
517cec8643bSMichal Nowak 		lastpoint = lastdigit + 1;
518cec8643bSMichal Nowak 	intsz = 0;
519cec8643bSMichal Nowak 	buf[1] = '\0';
520cec8643bSMichal Nowak 	for (cp = dp->string; cp < lastpoint; cp++) {
521cec8643bSMichal Nowak 		buf[0] = cp[0];
522cec8643bSMichal Nowak 		intsz += (*tbl->slen)(buf, tbl->arg);
523cec8643bSMichal Nowak 	}
52495c635efSGarrett D'Amore 
525cec8643bSMichal Nowak 	/*
526cec8643bSMichal Nowak          * If this number has more integer digits than all numbers
527cec8643bSMichal Nowak          * seen on earlier lines, shift them all to the right.
528cec8643bSMichal Nowak 	 * If it has fewer, shift this number to the right.
529cec8643bSMichal Nowak 	 */
53095c635efSGarrett D'Amore 
531cec8643bSMichal Nowak 	if (intsz > col->decimal) {
532cec8643bSMichal Nowak 		col->nwidth += intsz - col->decimal;
533cec8643bSMichal Nowak 		col->decimal = intsz;
53495c635efSGarrett D'Amore 	} else
535cec8643bSMichal Nowak 		totsz += col->decimal - intsz;
536cec8643bSMichal Nowak 
537cec8643bSMichal Nowak 	/* Update the maximum total width seen so far. */
53895c635efSGarrett D'Amore 
539cec8643bSMichal Nowak 	if (totsz > col->nwidth)
540cec8643bSMichal Nowak 		col->nwidth = totsz;
541*4d131170SRobert Mustacchi 	if (col->nwidth > col->width)
542*4d131170SRobert Mustacchi 		col->width = col->nwidth;
543cec8643bSMichal Nowak 	return totsz;
54495c635efSGarrett D'Amore }
545