xref: /openbsd/usr.bin/mandoc/out.c (revision 1fddd665)
1*1fddd665Sschwarze /*	$OpenBSD: out.c,v 1.57 2021/10/17 21:03:05 schwarze Exp $ */
24175bdabSschwarze /*
3ec04407bSschwarze  * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4e968d739Sschwarze  * Copyright (c) 2011, 2014, 2015, 2017, 2018, 2019, 2021
5e968d739Sschwarze  *               Ingo Schwarze <schwarze@openbsd.org>
64175bdabSschwarze  *
74175bdabSschwarze  * Permission to use, copy, modify, and distribute this software for any
84175bdabSschwarze  * purpose with or without fee is hereby granted, provided that the above
94175bdabSschwarze  * copyright notice and this permission notice appear in all copies.
104175bdabSschwarze  *
114175bdabSschwarze  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
124175bdabSschwarze  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
134175bdabSschwarze  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
144175bdabSschwarze  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
154175bdabSschwarze  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
164175bdabSschwarze  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
174175bdabSschwarze  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
184175bdabSschwarze  */
194175bdabSschwarze #include <sys/types.h>
204175bdabSschwarze 
21b822ca0dSschwarze #include <assert.h>
22a7d5d23dSschwarze #include <ctype.h>
23e8a65004Sschwarze #include <stdint.h>
247d063611Sschwarze #include <stdio.h>
254175bdabSschwarze #include <stdlib.h>
26b822ca0dSschwarze #include <string.h>
27b822ca0dSschwarze #include <time.h>
284175bdabSschwarze 
294f4f7972Sschwarze #include "mandoc_aux.h"
307d063611Sschwarze #include "mandoc.h"
31fae2491eSschwarze #include "tbl.h"
324175bdabSschwarze #include "out.h"
334175bdabSschwarze 
349f0607b7Sschwarze struct	tbl_colgroup {
359f0607b7Sschwarze 	struct tbl_colgroup	*next;
369f0607b7Sschwarze 	size_t			 wanted;
379f0607b7Sschwarze 	int			 startcol;
389f0607b7Sschwarze 	int			 endcol;
399f0607b7Sschwarze };
409f0607b7Sschwarze 
419f0607b7Sschwarze static	size_t	tblcalc_data(struct rofftbl *, struct roffcol *,
422c3e66c4Sschwarze 			const struct tbl_opts *, const struct tbl_dat *,
432c3e66c4Sschwarze 			size_t);
449f0607b7Sschwarze static	size_t	tblcalc_literal(struct rofftbl *, struct roffcol *,
452c3e66c4Sschwarze 			const struct tbl_dat *, size_t);
469f0607b7Sschwarze static	size_t	tblcalc_number(struct rofftbl *, struct roffcol *,
47c7497e73Sschwarze 			const struct tbl_opts *, const struct tbl_dat *);
48ec04407bSschwarze 
4949aff9f8Sschwarze 
504175bdabSschwarze /*
51a09d548bSschwarze  * Parse the *src string and store a scaling unit into *dst.
52a09d548bSschwarze  * If the string doesn't specify the unit, use the default.
53a09d548bSschwarze  * If no default is specified, fail.
54ecd22486Sschwarze  * Return a pointer to the byte after the last byte used,
55ecd22486Sschwarze  * or NULL on total failure.
564175bdabSschwarze  */
57ecd22486Sschwarze const char *
a2roffsu(const char * src,struct roffsu * dst,enum roffscale def)584175bdabSschwarze a2roffsu(const char *src, struct roffsu *dst, enum roffscale def)
594175bdabSschwarze {
60a09d548bSschwarze 	char		*endptr;
614175bdabSschwarze 
6284f230baSschwarze 	dst->unit = def == SCALE_MAX ? SCALE_BU : def;
6384f230baSschwarze 	dst->scale = strtod(src, &endptr);
6484f230baSschwarze 	if (endptr == src)
65ecd22486Sschwarze 		return NULL;
664175bdabSschwarze 
6784f230baSschwarze 	switch (*endptr++) {
6849aff9f8Sschwarze 	case 'c':
6984f230baSschwarze 		dst->unit = SCALE_CM;
704175bdabSschwarze 		break;
7149aff9f8Sschwarze 	case 'i':
7284f230baSschwarze 		dst->unit = SCALE_IN;
734175bdabSschwarze 		break;
7449aff9f8Sschwarze 	case 'f':
7584f230baSschwarze 		dst->unit = SCALE_FS;
764175bdabSschwarze 		break;
7749aff9f8Sschwarze 	case 'M':
7884f230baSschwarze 		dst->unit = SCALE_MM;
7984f230baSschwarze 		break;
8084f230baSschwarze 	case 'm':
8184f230baSschwarze 		dst->unit = SCALE_EM;
824175bdabSschwarze 		break;
8349aff9f8Sschwarze 	case 'n':
8484f230baSschwarze 		dst->unit = SCALE_EN;
854175bdabSschwarze 		break;
8684f230baSschwarze 	case 'P':
8784f230baSschwarze 		dst->unit = SCALE_PC;
8884f230baSschwarze 		break;
8984f230baSschwarze 	case 'p':
9084f230baSschwarze 		dst->unit = SCALE_PT;
9184f230baSschwarze 		break;
9284f230baSschwarze 	case 'u':
9384f230baSschwarze 		dst->unit = SCALE_BU;
9484f230baSschwarze 		break;
9584f230baSschwarze 	case 'v':
9684f230baSschwarze 		dst->unit = SCALE_VS;
9784f230baSschwarze 		break;
984175bdabSschwarze 	default:
9923b9ae24Sschwarze 		endptr--;
10084f230baSschwarze 		if (SCALE_MAX == def)
101ecd22486Sschwarze 			return NULL;
10284f230baSschwarze 		dst->unit = def;
10384f230baSschwarze 		break;
1044175bdabSschwarze 	}
105ecd22486Sschwarze 	return endptr;
1064175bdabSschwarze }
107b822ca0dSschwarze 
108ec04407bSschwarze /*
109ec04407bSschwarze  * Calculate the abstract widths and decimal positions of columns in a
110ec04407bSschwarze  * table.  This routine allocates the columns structures then runs over
111ec04407bSschwarze  * all rows and cells in the table.  The function pointers in "tbl" are
112ec04407bSschwarze  * used for the actual width calculations.
113ec04407bSschwarze  */
114ec04407bSschwarze void
tblcalc(struct rofftbl * tbl,const struct tbl_span * sp_first,size_t offset,size_t rmargin)1159f0607b7Sschwarze tblcalc(struct rofftbl *tbl, const struct tbl_span *sp_first,
116d1c3ec49Sschwarze     size_t offset, size_t rmargin)
117ec04407bSschwarze {
1182c3e66c4Sschwarze 	struct roffsu		 su;
11999d91ba7Sschwarze 	const struct tbl_opts	*opts;
1209f0607b7Sschwarze 	const struct tbl_span	*sp;
121ec04407bSschwarze 	const struct tbl_dat	*dp;
122ec04407bSschwarze 	struct roffcol		*col;
1239f0607b7Sschwarze 	struct tbl_colgroup	*first_group, **gp, *g;
1247d11b6e3Sschwarze 	size_t			*colwidth;
1259f0607b7Sschwarze 	size_t			 ewidth, min1, min2, wanted, width, xwidth;
1269f0607b7Sschwarze 	int			 done, icol, maxcol, necol, nxcol, quirkcol;
127ec04407bSschwarze 
128ec04407bSschwarze 	/*
129ec04407bSschwarze 	 * Allocate the master column specifiers.  These will hold the
130ec04407bSschwarze 	 * widths and decimal positions for all cells in the column.  It
131ec04407bSschwarze 	 * must be freed and nullified by the caller.
132ec04407bSschwarze 	 */
133ec04407bSschwarze 
1349f0607b7Sschwarze 	assert(tbl->cols == NULL);
1359f0607b7Sschwarze 	tbl->cols = mandoc_calloc((size_t)sp_first->opts->cols,
13649aff9f8Sschwarze 	    sizeof(struct roffcol));
1379f0607b7Sschwarze 	opts = sp_first->opts;
138ec04407bSschwarze 
1399f0607b7Sschwarze 	maxcol = -1;
1409f0607b7Sschwarze 	first_group = NULL;
1419f0607b7Sschwarze 	for (sp = sp_first; sp != NULL; sp = sp->next) {
1429f0607b7Sschwarze 		if (sp->pos != TBL_SPAN_DATA)
143ec04407bSschwarze 			continue;
1449f0607b7Sschwarze 
145ec04407bSschwarze 		/*
146ec04407bSschwarze 		 * Account for the data cells in the layout, matching it
147ec04407bSschwarze 		 * to data cells in the data section.
148ec04407bSschwarze 		 */
1499f0607b7Sschwarze 
1509f0607b7Sschwarze 		for (dp = sp->first; dp != NULL; dp = dp->next) {
1515f6d1ba3Sschwarze 			icol = dp->layout->col;
152f6d3ad1cSschwarze 			while (maxcol < icol + dp->hspans)
153e8a65004Sschwarze 				tbl->cols[++maxcol].spacing = SIZE_MAX;
15446fa2066Sschwarze 			col = tbl->cols + icol;
15546fa2066Sschwarze 			col->flags |= dp->layout->flags;
15646fa2066Sschwarze 			if (dp->layout->flags & TBL_CELL_WIGN)
15746fa2066Sschwarze 				continue;
1589f0607b7Sschwarze 
1599f0607b7Sschwarze 			/* Handle explicit width specifications. */
1609f0607b7Sschwarze 
1612c3e66c4Sschwarze 			if (dp->layout->wstr != NULL &&
1622c3e66c4Sschwarze 			    dp->layout->width == 0 &&
1632c3e66c4Sschwarze 			    a2roffsu(dp->layout->wstr, &su, SCALE_EN)
1642c3e66c4Sschwarze 			    != NULL)
1652c3e66c4Sschwarze 				dp->layout->width =
1662c3e66c4Sschwarze 				    (*tbl->sulen)(&su, tbl->arg);
1672c3e66c4Sschwarze 			if (col->width < dp->layout->width)
1682c3e66c4Sschwarze 				col->width = dp->layout->width;
169e8a65004Sschwarze 			if (dp->layout->spacing != SIZE_MAX &&
170e8a65004Sschwarze 			    (col->spacing == SIZE_MAX ||
171e8a65004Sschwarze 			     col->spacing < dp->layout->spacing))
172e8a65004Sschwarze 				col->spacing = dp->layout->spacing;
1739f0607b7Sschwarze 
1749f0607b7Sschwarze 			/*
1759f0607b7Sschwarze 			 * Calculate an automatic width.
1769f0607b7Sschwarze 			 * Except for spanning cells, apply it.
1779f0607b7Sschwarze 			 */
1789f0607b7Sschwarze 
1799f0607b7Sschwarze 			width = tblcalc_data(tbl,
1809f0607b7Sschwarze 			    dp->hspans == 0 ? col : NULL,
1819f0607b7Sschwarze 			    opts, dp,
182238123d0Sschwarze 			    dp->block == 0 ? 0 :
183238123d0Sschwarze 			    dp->layout->width ? dp->layout->width :
184e6d4e954Sschwarze 			    rmargin ? (rmargin + sp->opts->cols / 2)
185e6d4e954Sschwarze 			    / (sp->opts->cols + 1) : 0);
1869f0607b7Sschwarze 			if (dp->hspans == 0)
1879f0607b7Sschwarze 				continue;
1889f0607b7Sschwarze 
1899f0607b7Sschwarze 			/*
1901d61b63dSschwarze 			 * Build a singly linked list
1919f0607b7Sschwarze 			 * of all groups of columns joined by spans,
1929f0607b7Sschwarze 			 * recording the minimum width for each group.
1939f0607b7Sschwarze 			 */
1949f0607b7Sschwarze 
1951d61b63dSschwarze 			gp = &first_group;
1961d61b63dSschwarze 			while (*gp != NULL && ((*gp)->startcol != icol ||
1971d61b63dSschwarze 			    (*gp)->endcol != icol + dp->hspans))
1989f0607b7Sschwarze 				gp = &(*gp)->next;
1991d61b63dSschwarze 			if (*gp == NULL) {
2009f0607b7Sschwarze 				g = mandoc_malloc(sizeof(*g));
2019f0607b7Sschwarze 				g->next = *gp;
2029f0607b7Sschwarze 				g->wanted = width;
2039f0607b7Sschwarze 				g->startcol = icol;
2049f0607b7Sschwarze 				g->endcol = icol + dp->hspans;
2059f0607b7Sschwarze 				*gp = g;
2069f0607b7Sschwarze 			} else if ((*gp)->wanted < width)
2079f0607b7Sschwarze 				(*gp)->wanted = width;
208ec04407bSschwarze 		}
209ec04407bSschwarze 	}
21046fa2066Sschwarze 
21146fa2066Sschwarze 	/*
212ea3fff40Sschwarze 	 * The minimum width of columns explicitly specified
213ea3fff40Sschwarze 	 * in the layout is 1n.
2149f0607b7Sschwarze 	 */
2159f0607b7Sschwarze 
216ea3fff40Sschwarze 	if (maxcol < sp_first->opts->cols - 1)
217ea3fff40Sschwarze 		maxcol = sp_first->opts->cols - 1;
218ea3fff40Sschwarze 	for (icol = 0; icol <= maxcol; icol++) {
219ea3fff40Sschwarze 		col = tbl->cols + icol;
220ea3fff40Sschwarze 		if (col->width < 1)
221ea3fff40Sschwarze 			col->width = 1;
222ea3fff40Sschwarze 
223ea3fff40Sschwarze 		/*
224ea3fff40Sschwarze 		 * Column spacings are needed for span width
225ea3fff40Sschwarze 		 * calculations, so set the default values now.
226ea3fff40Sschwarze 		 */
227ea3fff40Sschwarze 
228ea3fff40Sschwarze 		if (col->spacing == SIZE_MAX || icol == maxcol)
229ea3fff40Sschwarze 			col->spacing = 3;
230ea3fff40Sschwarze 	}
2319f0607b7Sschwarze 
2329f0607b7Sschwarze 	/*
2339f0607b7Sschwarze 	 * Replace the minimum widths with the missing widths,
2349f0607b7Sschwarze 	 * and dismiss groups that are already wide enough.
2359f0607b7Sschwarze 	 */
2369f0607b7Sschwarze 
2379f0607b7Sschwarze 	gp = &first_group;
2389f0607b7Sschwarze 	while ((g = *gp) != NULL) {
2399f0607b7Sschwarze 		done = 0;
2409f0607b7Sschwarze 		for (icol = g->startcol; icol <= g->endcol; icol++) {
2419f0607b7Sschwarze 			width = tbl->cols[icol].width;
2429f0607b7Sschwarze 			if (icol < g->endcol)
2439f0607b7Sschwarze 				width += tbl->cols[icol].spacing;
2449f0607b7Sschwarze 			if (g->wanted <= width) {
2459f0607b7Sschwarze 				done = 1;
2469f0607b7Sschwarze 				break;
2479f0607b7Sschwarze 			} else
248*1fddd665Sschwarze 				g->wanted -= width;
2499f0607b7Sschwarze 		}
2509f0607b7Sschwarze 		if (done) {
2519f0607b7Sschwarze 			*gp = g->next;
2529f0607b7Sschwarze 			free(g);
2539f0607b7Sschwarze 		} else
254*1fddd665Sschwarze 			gp = &g->next;
2559f0607b7Sschwarze 	}
2569f0607b7Sschwarze 
2577d11b6e3Sschwarze 	colwidth = mandoc_reallocarray(NULL, maxcol + 1, sizeof(*colwidth));
2589f0607b7Sschwarze 	while (first_group != NULL) {
2599f0607b7Sschwarze 
2609f0607b7Sschwarze 		/*
2617d11b6e3Sschwarze 		 * Rebuild the array of the widths of all columns
2627d11b6e3Sschwarze 		 * participating in spans that require expansion.
2637d11b6e3Sschwarze 		 */
2647d11b6e3Sschwarze 
2657d11b6e3Sschwarze 		for (icol = 0; icol <= maxcol; icol++)
2667d11b6e3Sschwarze 			colwidth[icol] = SIZE_MAX;
2677d11b6e3Sschwarze 		for (g = first_group; g != NULL; g = g->next)
2687d11b6e3Sschwarze 			for (icol = g->startcol; icol <= g->endcol; icol++)
2697d11b6e3Sschwarze 				colwidth[icol] = tbl->cols[icol].width;
2707d11b6e3Sschwarze 
2717d11b6e3Sschwarze 		/*
2729f0607b7Sschwarze 		 * Find the smallest and second smallest column width
2739f0607b7Sschwarze 		 * among the columns which may need expamsion.
2749f0607b7Sschwarze 		 */
2759f0607b7Sschwarze 
2769f0607b7Sschwarze 		min1 = min2 = SIZE_MAX;
2779f0607b7Sschwarze 		for (icol = 0; icol <= maxcol; icol++) {
2787d11b6e3Sschwarze 			width = colwidth[icol];
27956155c97Sschwarze 			if (min1 > width) {
2809f0607b7Sschwarze 				min2 = min1;
28156155c97Sschwarze 				min1 = width;
28256155c97Sschwarze 			} else if (min1 < width && min2 > width)
28356155c97Sschwarze 				min2 = width;
2849f0607b7Sschwarze 		}
2859f0607b7Sschwarze 
2869f0607b7Sschwarze 		/*
2879f0607b7Sschwarze 		 * Find the minimum wanted width
2889f0607b7Sschwarze 		 * for any one of the narrowest columns,
2899f0607b7Sschwarze 		 * and mark the columns wanting that width.
2909f0607b7Sschwarze 		 */
2919f0607b7Sschwarze 
2929f0607b7Sschwarze 		wanted = min2;
2939f0607b7Sschwarze 		for (g = first_group; g != NULL; g = g->next) {
2949f0607b7Sschwarze 			necol = 0;
2959f0607b7Sschwarze 			for (icol = g->startcol; icol <= g->endcol; icol++)
2967d11b6e3Sschwarze 				if (colwidth[icol] == min1)
2979f0607b7Sschwarze 					necol++;
2989f0607b7Sschwarze 			if (necol == 0)
2999f0607b7Sschwarze 				continue;
3009f0607b7Sschwarze 			width = min1 + (g->wanted - 1) / necol + 1;
3019f0607b7Sschwarze 			if (width > min2)
3029f0607b7Sschwarze 				width = min2;
3039f0607b7Sschwarze 			if (wanted > width)
3049f0607b7Sschwarze 				wanted = width;
3059f0607b7Sschwarze 		}
3069f0607b7Sschwarze 
30756155c97Sschwarze 		/* Record the effect of the widening. */
3089f0607b7Sschwarze 
3099f0607b7Sschwarze 		gp = &first_group;
3109f0607b7Sschwarze 		while ((g = *gp) != NULL) {
3119f0607b7Sschwarze 			done = 0;
3129f0607b7Sschwarze 			for (icol = g->startcol; icol <= g->endcol; icol++) {
3137d11b6e3Sschwarze 				if (colwidth[icol] != min1)
3149f0607b7Sschwarze 					continue;
3159f0607b7Sschwarze 				if (g->wanted <= wanted - min1) {
31656155c97Sschwarze 					tbl->cols[icol].width += g->wanted;
3179f0607b7Sschwarze 					done = 1;
3189f0607b7Sschwarze 					break;
3199f0607b7Sschwarze 				}
32056155c97Sschwarze 				tbl->cols[icol].width = wanted;
3219f0607b7Sschwarze 				g->wanted -= wanted - min1;
3229f0607b7Sschwarze 			}
3239f0607b7Sschwarze 			if (done) {
3249f0607b7Sschwarze 				*gp = g->next;
3259f0607b7Sschwarze 				free(g);
3269f0607b7Sschwarze 			} else
327*1fddd665Sschwarze 				gp = &g->next;
3289f0607b7Sschwarze 		}
3299f0607b7Sschwarze 	}
3307d11b6e3Sschwarze 	free(colwidth);
3319f0607b7Sschwarze 
3329f0607b7Sschwarze 	/*
3333a412390Sschwarze 	 * Align numbers with text.
33446fa2066Sschwarze 	 * Count columns to equalize and columns to maximize.
33546fa2066Sschwarze 	 * Find maximum width of the columns to equalize.
33646fa2066Sschwarze 	 * Find total width of the columns *not* to maximize.
33746fa2066Sschwarze 	 */
33846fa2066Sschwarze 
33946fa2066Sschwarze 	necol = nxcol = 0;
34046fa2066Sschwarze 	ewidth = xwidth = 0;
34146fa2066Sschwarze 	for (icol = 0; icol <= maxcol; icol++) {
34246fa2066Sschwarze 		col = tbl->cols + icol;
3433a412390Sschwarze 		if (col->width > col->nwidth)
3443a412390Sschwarze 			col->decimal += (col->width - col->nwidth) / 2;
34546fa2066Sschwarze 		if (col->flags & TBL_CELL_EQUAL) {
34646fa2066Sschwarze 			necol++;
34746fa2066Sschwarze 			if (ewidth < col->width)
34846fa2066Sschwarze 				ewidth = col->width;
34946fa2066Sschwarze 		}
35046fa2066Sschwarze 		if (col->flags & TBL_CELL_WMAX)
35146fa2066Sschwarze 			nxcol++;
35246fa2066Sschwarze 		else
35346fa2066Sschwarze 			xwidth += col->width;
35446fa2066Sschwarze 	}
35546fa2066Sschwarze 
35646fa2066Sschwarze 	/*
35746fa2066Sschwarze 	 * Equalize columns, if requested for any of them.
35846fa2066Sschwarze 	 * Update total width of the columns not to maximize.
35946fa2066Sschwarze 	 */
36046fa2066Sschwarze 
36146fa2066Sschwarze 	if (necol) {
36246fa2066Sschwarze 		for (icol = 0; icol <= maxcol; icol++) {
36346fa2066Sschwarze 			col = tbl->cols + icol;
36446fa2066Sschwarze 			if ( ! (col->flags & TBL_CELL_EQUAL))
36546fa2066Sschwarze 				continue;
36646fa2066Sschwarze 			if (col->width == ewidth)
36746fa2066Sschwarze 				continue;
368d1c3ec49Sschwarze 			if (nxcol && rmargin)
36946fa2066Sschwarze 				xwidth += ewidth - col->width;
37046fa2066Sschwarze 			col->width = ewidth;
37146fa2066Sschwarze 		}
37246fa2066Sschwarze 	}
37346fa2066Sschwarze 
37446fa2066Sschwarze 	/*
37546fa2066Sschwarze 	 * If there are any columns to maximize, find the total
37646fa2066Sschwarze 	 * available width, deducting 3n margins between columns.
37746fa2066Sschwarze 	 * Distribute the available width evenly.
37846fa2066Sschwarze 	 */
37946fa2066Sschwarze 
380d1c3ec49Sschwarze 	if (nxcol && rmargin) {
381eb274914Sschwarze 		xwidth += 3*maxcol +
38299d91ba7Sschwarze 		    (opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ?
38399d91ba7Sschwarze 		     2 : !!opts->lvert + !!opts->rvert);
384d1c3ec49Sschwarze 		if (rmargin <= offset + xwidth)
385eb274914Sschwarze 			return;
386d1c3ec49Sschwarze 		xwidth = rmargin - offset - xwidth;
38799d91ba7Sschwarze 
38899d91ba7Sschwarze 		/*
38999d91ba7Sschwarze 		 * Emulate a bug in GNU tbl width calculation that
39099d91ba7Sschwarze 		 * manifests itself for large numbers of x-columns.
39199d91ba7Sschwarze 		 * Emulating it for 5 x-columns gives identical
39299d91ba7Sschwarze 		 * behaviour for up to 6 x-columns.
39399d91ba7Sschwarze 		 */
39499d91ba7Sschwarze 
39599d91ba7Sschwarze 		if (nxcol == 5) {
39699d91ba7Sschwarze 			quirkcol = xwidth % nxcol + 2;
39799d91ba7Sschwarze 			if (quirkcol != 3 && quirkcol != 4)
39899d91ba7Sschwarze 				quirkcol = -1;
39999d91ba7Sschwarze 		} else
40099d91ba7Sschwarze 			quirkcol = -1;
40199d91ba7Sschwarze 
40299d91ba7Sschwarze 		necol = 0;
40399d91ba7Sschwarze 		ewidth = 0;
40446fa2066Sschwarze 		for (icol = 0; icol <= maxcol; icol++) {
40546fa2066Sschwarze 			col = tbl->cols + icol;
40646fa2066Sschwarze 			if ( ! (col->flags & TBL_CELL_WMAX))
40746fa2066Sschwarze 				continue;
40899d91ba7Sschwarze 			col->width = (double)xwidth * ++necol / nxcol
40999d91ba7Sschwarze 			    - ewidth + 0.4995;
41099d91ba7Sschwarze 			if (necol == quirkcol)
41199d91ba7Sschwarze 				col->width--;
41299d91ba7Sschwarze 			ewidth += col->width;
41346fa2066Sschwarze 		}
41446fa2066Sschwarze 	}
415ec04407bSschwarze }
416ec04407bSschwarze 
4179f0607b7Sschwarze static size_t
tblcalc_data(struct rofftbl * tbl,struct roffcol * col,const struct tbl_opts * opts,const struct tbl_dat * dp,size_t mw)418ec04407bSschwarze tblcalc_data(struct rofftbl *tbl, struct roffcol *col,
4192c3e66c4Sschwarze     const struct tbl_opts *opts, const struct tbl_dat *dp, size_t mw)
420ec04407bSschwarze {
421ec04407bSschwarze 	size_t		 sz;
422ec04407bSschwarze 
423ec04407bSschwarze 	/* Branch down into data sub-types. */
424ec04407bSschwarze 
425ec04407bSschwarze 	switch (dp->layout->pos) {
42649aff9f8Sschwarze 	case TBL_CELL_HORIZ:
42749aff9f8Sschwarze 	case TBL_CELL_DHORIZ:
428ec04407bSschwarze 		sz = (*tbl->len)(1, tbl->arg);
4299f0607b7Sschwarze 		if (col != NULL && col->width < sz)
430ec04407bSschwarze 			col->width = sz;
4319f0607b7Sschwarze 		return sz;
43249aff9f8Sschwarze 	case TBL_CELL_LONG:
43349aff9f8Sschwarze 	case TBL_CELL_CENTRE:
43449aff9f8Sschwarze 	case TBL_CELL_LEFT:
43549aff9f8Sschwarze 	case TBL_CELL_RIGHT:
4369f0607b7Sschwarze 		return tblcalc_literal(tbl, col, dp, mw);
43749aff9f8Sschwarze 	case TBL_CELL_NUMBER:
4389f0607b7Sschwarze 		return tblcalc_number(tbl, col, opts, dp);
43949aff9f8Sschwarze 	case TBL_CELL_DOWN:
4409f0607b7Sschwarze 		return 0;
441ec04407bSschwarze 	default:
442ec04407bSschwarze 		abort();
443ec04407bSschwarze 	}
444ec04407bSschwarze }
445ec04407bSschwarze 
4469f0607b7Sschwarze static size_t
tblcalc_literal(struct rofftbl * tbl,struct roffcol * col,const struct tbl_dat * dp,size_t mw)447ec04407bSschwarze tblcalc_literal(struct rofftbl *tbl, struct roffcol *col,
4482c3e66c4Sschwarze     const struct tbl_dat *dp, size_t mw)
449ec04407bSschwarze {
4502c3e66c4Sschwarze 	const char	*str;	/* Beginning of the first line. */
4512c3e66c4Sschwarze 	const char	*beg;	/* Beginning of the current line. */
4522c3e66c4Sschwarze 	char		*end;	/* End of the current line. */
453d1c3ec49Sschwarze 	size_t		 lsz;	/* Length of the current line. */
454d1c3ec49Sschwarze 	size_t		 wsz;	/* Length of the current word. */
4559f0607b7Sschwarze 	size_t		 msz;   /* Length of the longest line. */
456ec04407bSschwarze 
4572c3e66c4Sschwarze 	if (dp->string == NULL || *dp->string == '\0')
4589f0607b7Sschwarze 		return 0;
4592c3e66c4Sschwarze 	str = mw ? mandoc_strdup(dp->string) : dp->string;
4609f0607b7Sschwarze 	msz = lsz = 0;
4612c3e66c4Sschwarze 	for (beg = str; beg != NULL && *beg != '\0'; beg = end) {
4622c3e66c4Sschwarze 		end = mw ? strchr(beg, ' ') : NULL;
4632c3e66c4Sschwarze 		if (end != NULL) {
4642c3e66c4Sschwarze 			*end++ = '\0';
4652c3e66c4Sschwarze 			while (*end == ' ')
4662c3e66c4Sschwarze 				end++;
4672c3e66c4Sschwarze 		}
468d1c3ec49Sschwarze 		wsz = (*tbl->slen)(beg, tbl->arg);
469d1c3ec49Sschwarze 		if (mw && lsz && lsz + 1 + wsz <= mw)
470d1c3ec49Sschwarze 			lsz += 1 + wsz;
471d1c3ec49Sschwarze 		else
472d1c3ec49Sschwarze 			lsz = wsz;
4739f0607b7Sschwarze 		if (msz < lsz)
4749f0607b7Sschwarze 			msz = lsz;
475ec04407bSschwarze 	}
4762c3e66c4Sschwarze 	if (mw)
4772c3e66c4Sschwarze 		free((void *)str);
4789f0607b7Sschwarze 	if (col != NULL && col->width < msz)
4799f0607b7Sschwarze 		col->width = msz;
4809f0607b7Sschwarze 	return msz;
4812c3e66c4Sschwarze }
482ec04407bSschwarze 
4839f0607b7Sschwarze static size_t
tblcalc_number(struct rofftbl * tbl,struct roffcol * col,const struct tbl_opts * opts,const struct tbl_dat * dp)484ec04407bSschwarze tblcalc_number(struct rofftbl *tbl, struct roffcol *col,
485c7497e73Sschwarze 		const struct tbl_opts *opts, const struct tbl_dat *dp)
486ec04407bSschwarze {
487a7d5d23dSschwarze 	const char	*cp, *lastdigit, *lastpoint;
488a7d5d23dSschwarze 	size_t		 intsz, totsz;
489ec04407bSschwarze 	char		 buf[2];
490ec04407bSschwarze 
491a7d5d23dSschwarze 	if (dp->string == NULL || *dp->string == '\0')
4929f0607b7Sschwarze 		return 0;
4939f0607b7Sschwarze 
4949f0607b7Sschwarze 	totsz = (*tbl->slen)(dp->string, tbl->arg);
4959f0607b7Sschwarze 	if (col == NULL)
4969f0607b7Sschwarze 		return totsz;
497a7d5d23dSschwarze 
498ec04407bSschwarze 	/*
499a7d5d23dSschwarze 	 * Find the last digit and
500a7d5d23dSschwarze 	 * the last decimal point that is adjacent to a digit.
501a7d5d23dSschwarze 	 * The alignment indicator "\&" overrides everything.
502ec04407bSschwarze 	 */
503ec04407bSschwarze 
504a7d5d23dSschwarze 	lastdigit = lastpoint = NULL;
505a7d5d23dSschwarze 	for (cp = dp->string; cp[0] != '\0'; cp++) {
506a7d5d23dSschwarze 		if (cp[0] == '\\' && cp[1] == '&') {
507a7d5d23dSschwarze 			lastdigit = lastpoint = cp;
508a7d5d23dSschwarze 			break;
509a7d5d23dSschwarze 		} else if (cp[0] == opts->decimal &&
510a7d5d23dSschwarze 		    (isdigit((unsigned char)cp[1]) ||
511a7d5d23dSschwarze 		     (cp > dp->string && isdigit((unsigned char)cp[-1]))))
512a7d5d23dSschwarze 			lastpoint = cp;
513a7d5d23dSschwarze 		else if (isdigit((unsigned char)cp[0]))
514a7d5d23dSschwarze 			lastdigit = cp;
515ec04407bSschwarze 	}
516a7d5d23dSschwarze 
517a7d5d23dSschwarze 	/* Not a number, treat as a literal string. */
518a7d5d23dSschwarze 
519a7d5d23dSschwarze 	if (lastdigit == NULL) {
5209f0607b7Sschwarze 		if (col != NULL && col->width < totsz)
521a7d5d23dSschwarze 			col->width = totsz;
5229f0607b7Sschwarze 		return totsz;
523a7d5d23dSschwarze 	}
524a7d5d23dSschwarze 
525a7d5d23dSschwarze 	/* Measure the width of the integer part. */
526a7d5d23dSschwarze 
527a7d5d23dSschwarze 	if (lastpoint == NULL)
528a7d5d23dSschwarze 		lastpoint = lastdigit + 1;
529a7d5d23dSschwarze 	intsz = 0;
530a7d5d23dSschwarze 	buf[1] = '\0';
531a7d5d23dSschwarze 	for (cp = dp->string; cp < lastpoint; cp++) {
532a7d5d23dSschwarze 		buf[0] = cp[0];
533a7d5d23dSschwarze 		intsz += (*tbl->slen)(buf, tbl->arg);
534a7d5d23dSschwarze 	}
535a7d5d23dSschwarze 
536a7d5d23dSschwarze 	/*
537a7d5d23dSschwarze          * If this number has more integer digits than all numbers
538a7d5d23dSschwarze          * seen on earlier lines, shift them all to the right.
539a7d5d23dSschwarze 	 * If it has fewer, shift this number to the right.
540a7d5d23dSschwarze 	 */
541a7d5d23dSschwarze 
542a7d5d23dSschwarze 	if (intsz > col->decimal) {
543a7d5d23dSschwarze 		col->nwidth += intsz - col->decimal;
544a7d5d23dSschwarze 		col->decimal = intsz;
545ec04407bSschwarze 	} else
546a7d5d23dSschwarze 		totsz += col->decimal - intsz;
547ec04407bSschwarze 
548a7d5d23dSschwarze 	/* Update the maximum total width seen so far. */
549ec04407bSschwarze 
550a7d5d23dSschwarze 	if (totsz > col->nwidth)
551a7d5d23dSschwarze 		col->nwidth = totsz;
552e968d739Sschwarze 	if (col->nwidth > col->width)
553e968d739Sschwarze 		col->width = col->nwidth;
5549f0607b7Sschwarze 	return totsz;
555ec04407bSschwarze }
556