xref: /freebsd/contrib/mandoc/out.c (revision 06410c1b51637e5e1f392d553b5008948af58014)
1 /* $Id: out.c,v 1.87 2025/07/16 14:33:08 schwarze Exp $ */
2 /*
3  * Copyright (c) 2011, 2014, 2015, 2017, 2018, 2019, 2021, 2025
4  *               Ingo Schwarze <schwarze@openbsd.org>
5  * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
6  *
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.
10  *
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.
18  */
19 #include "config.h"
20 
21 #include <sys/types.h>
22 
23 #include <assert.h>
24 #include <ctype.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30 
31 #include "mandoc_aux.h"
32 #include "mandoc.h"
33 #include "tbl.h"
34 #include "out.h"
35 
36 struct	tbl_colgroup {
37 	struct tbl_colgroup	*next;
38 	size_t			 wanted;
39 	int			 startcol;
40 	int			 endcol;
41 };
42 
43 static	size_t	tblcalc_data(struct rofftbl *, struct roffcol *,
44 			const struct tbl_opts *, const struct tbl_dat *,
45 			size_t);
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 *);
50 
51 
52 /*
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.
58  */
59 const char *
60 a2roffsu(const char *src, struct roffsu *dst, enum roffscale def)
61 {
62 	char		*endptr;
63 
64 	dst->unit = def == SCALE_MAX ? SCALE_BU : def;
65 	dst->scale = strtod(src, &endptr);
66 	if (endptr == src)
67 		return NULL;
68 
69 	switch (*endptr++) {
70 	case 'c':
71 		dst->unit = SCALE_CM;
72 		break;
73 	case 'i':
74 		dst->unit = SCALE_IN;
75 		break;
76 	case 'f':
77 		dst->unit = SCALE_FS;
78 		break;
79 	case 'M':
80 		dst->unit = SCALE_MM;
81 		break;
82 	case 'm':
83 		dst->unit = SCALE_EM;
84 		break;
85 	case 'n':
86 		dst->unit = SCALE_EN;
87 		break;
88 	case 'P':
89 		dst->unit = SCALE_PC;
90 		break;
91 	case 'p':
92 		dst->unit = SCALE_PT;
93 		break;
94 	case 'u':
95 		dst->unit = SCALE_BU;
96 		break;
97 	case 'v':
98 		dst->unit = SCALE_VS;
99 		break;
100 	default:
101 		endptr--;
102 		if (SCALE_MAX == def)
103 			return NULL;
104 		dst->unit = def;
105 		break;
106 	}
107 	return endptr;
108 }
109 
110 /*
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.
115  */
116 void
117 tblcalc(struct rofftbl *tbl, const struct tbl_span *sp_first,
118     size_t offset, size_t rmargin)
119 {
120 	const struct tbl_opts	*opts;
121 	const struct tbl_span	*sp;
122 	const struct tbl_dat	*dp;
123 	struct roffcol		*col;
124 	struct tbl_colgroup	*first_group, **gp, *g;
125 
126 	/* Widths in basic units. */
127 	size_t	*colwidth; /* Widths of all columns. */
128 	size_t	 min1;     /* Width of the narrowest column. */
129 	size_t	 min2;     /* Width of the second narrowest column. */
130 	size_t	 wanted;   /* For any of the narrowest columns. */
131 	size_t	 xwidth;   /* Total width of columns not to expand. */
132 	size_t	 ewidth;   /* Width of widest column to equalize. */
133 	size_t	 width;    /* Width of the data in basic units. */
134 	size_t	 enw;      /* Width of one EN unit. */
135 
136 	int	 icol;     /* Column number, starting at zero. */
137 	int	 maxcol;   /* Number of last column. */
138 	int	 necol;    /* Number of columns to equalize. */
139 	int	 nxcol;    /* Number of columns to expand. */
140 	int	 done;	   /* Boolean: this group is wide enough. */
141 	int	 quirkcol;
142 
143 	/*
144 	 * Allocate the master column specifiers.  These will hold the
145 	 * widths and decimal positions for all cells in the column.  It
146 	 * must be freed and nullified by the caller.
147 	 */
148 
149 	assert(tbl->cols == NULL);
150 	tbl->cols = mandoc_calloc((size_t)sp_first->opts->cols,
151 	    sizeof(struct roffcol));
152 	opts = sp_first->opts;
153 
154 	maxcol = -1;
155 	first_group = NULL;
156 	enw = (*tbl->len)(1, tbl->arg);
157 	for (sp = sp_first; sp != NULL; sp = sp->next) {
158 		if (sp->pos != TBL_SPAN_DATA)
159 			continue;
160 
161 		/*
162 		 * Account for the data cells in the layout, matching it
163 		 * to data cells in the data section.
164 		 */
165 
166 		for (dp = sp->first; dp != NULL; dp = dp->next) {
167 			icol = dp->layout->col;
168 			while (maxcol < icol + dp->hspans)
169 				tbl->cols[++maxcol].spacing = SIZE_MAX;
170 			col = tbl->cols + icol;
171 			col->flags |= dp->layout->flags;
172 			if (dp->layout->flags & TBL_CELL_WIGN)
173 				continue;
174 
175 			/* Handle explicit width specifications. */
176 			if (col->width < dp->layout->width)
177 				col->width = dp->layout->width;
178 			if (dp->layout->spacing != SIZE_MAX &&
179 			    (col->spacing == SIZE_MAX ||
180 			     col->spacing < dp->layout->spacing))
181 				col->spacing = dp->layout->spacing;
182 
183 			/*
184 			 * Calculate an automatic width.
185 			 * Except for spanning cells, apply it.
186 			 */
187 
188 			width = tblcalc_data(tbl,
189 			    dp->hspans == 0 ? col : NULL,
190 			    opts, dp,
191 			    dp->block == 0 ? 0 :
192 			    dp->layout->width ? dp->layout->width :
193 			    rmargin ? (rmargin / enw + sp->opts->cols / 2) /
194 			    (sp->opts->cols + 1) * enw : 0);
195 			if (dp->hspans == 0)
196 				continue;
197 
198 			/*
199 			 * Build a singly linked list
200 			 * of all groups of columns joined by spans,
201 			 * recording the minimum width for each group.
202 			 */
203 
204 			gp = &first_group;
205 			while (*gp != NULL && ((*gp)->startcol != icol ||
206 			    (*gp)->endcol != icol + dp->hspans))
207 				gp = &(*gp)->next;
208 			if (*gp == NULL) {
209 				g = mandoc_malloc(sizeof(*g));
210 				g->next = *gp;
211 				g->wanted = width;
212 				g->startcol = icol;
213 				g->endcol = icol + dp->hspans;
214 				*gp = g;
215 			} else if ((*gp)->wanted < width)
216 				(*gp)->wanted = width;
217 		}
218 	}
219 
220 	/*
221 	 * The minimum width of columns explicitly specified
222 	 * in the layout is 1n.
223 	 */
224 
225 	if (maxcol < sp_first->opts->cols - 1)
226 		maxcol = sp_first->opts->cols - 1;
227 	for (icol = 0; icol <= maxcol; icol++) {
228 		col = tbl->cols + icol;
229 		if (col->width < enw)
230 			col->width = enw;
231 
232 		/*
233 		 * Column spacings are needed for span width
234 		 * calculations, so set the default values now.
235 		 */
236 
237 		if (col->spacing == SIZE_MAX || icol == maxcol)
238 			col->spacing = 3;
239 	}
240 
241 	/*
242 	 * Replace the minimum widths with the missing widths,
243 	 * and dismiss groups that are already wide enough.
244 	 */
245 
246 	gp = &first_group;
247 	while ((g = *gp) != NULL) {
248 		done = 0;
249 		for (icol = g->startcol; icol <= g->endcol; icol++) {
250 			width = tbl->cols[icol].width;
251 			if (icol < g->endcol)
252 				width += (*tbl->len)(tbl->cols[icol].spacing,
253 				    tbl->arg);
254 			if (g->wanted <= width) {
255 				done = 1;
256 				break;
257 			} else
258 				g->wanted -= width;
259 		}
260 		if (done) {
261 			*gp = g->next;
262 			free(g);
263 		} else
264 			gp = &g->next;
265 	}
266 
267 	colwidth = mandoc_reallocarray(NULL, maxcol + 1, sizeof(*colwidth));
268 	while (first_group != NULL) {
269 
270 		/*
271 		 * Rebuild the array of the widths of all columns
272 		 * participating in spans that require expansion.
273 		 */
274 
275 		for (icol = 0; icol <= maxcol; icol++)
276 			colwidth[icol] = SIZE_MAX;
277 		for (g = first_group; g != NULL; g = g->next)
278 			for (icol = g->startcol; icol <= g->endcol; icol++)
279 				colwidth[icol] = tbl->cols[icol].width;
280 
281 		/*
282 		 * Find the smallest and second smallest column width
283 		 * among the columns which may need expamsion.
284 		 */
285 
286 		min1 = min2 = SIZE_MAX;
287 		for (icol = 0; icol <= maxcol; icol++) {
288 			width = colwidth[icol];
289 			if (min1 > width) {
290 				min2 = min1;
291 				min1 = width;
292 			} else if (min1 < width && min2 > width)
293 				min2 = width;
294 		}
295 
296 		/*
297 		 * Find the minimum wanted width
298 		 * for any one of the narrowest columns,
299 		 * and mark the columns wanting that width.
300 		 */
301 
302 		wanted = min2;
303 		for (g = first_group; g != NULL; g = g->next) {
304 			necol = 0;
305 			for (icol = g->startcol; icol <= g->endcol; icol++)
306 				if (colwidth[icol] == min1)
307 					necol++;
308 			if (necol == 0)
309 				continue;
310 			width = min1 + (g->wanted - 1) / necol + 1;
311 			if (width > min2)
312 				width = min2;
313 			if (wanted > width)
314 				wanted = width;
315 		}
316 
317 		/* Record the effect of the widening. */
318 
319 		gp = &first_group;
320 		while ((g = *gp) != NULL) {
321 			done = 0;
322 			for (icol = g->startcol; icol <= g->endcol; icol++) {
323 				if (colwidth[icol] != min1)
324 					continue;
325 				if (g->wanted <= wanted - min1) {
326 					tbl->cols[icol].width += g->wanted;
327 					done = 1;
328 					break;
329 				}
330 				tbl->cols[icol].width = wanted;
331 				g->wanted -= wanted - min1;
332 			}
333 			if (done) {
334 				*gp = g->next;
335 				free(g);
336 			} else
337 				gp = &g->next;
338 		}
339 	}
340 	free(colwidth);
341 
342 	/*
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.
347 	 */
348 
349 	necol = nxcol = 0;
350 	ewidth = xwidth = 0;
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;
355 		if (col->flags & TBL_CELL_EQUAL) {
356 			necol++;
357 			if (ewidth < col->width)
358 				ewidth = col->width;
359 		}
360 		if (col->flags & TBL_CELL_WMAX)
361 			nxcol++;
362 		else
363 			xwidth += col->width;
364 	}
365 
366 	/*
367 	 * Equalize columns, if requested for any of them.
368 	 * Update total width of the columns not to maximize.
369 	 */
370 
371 	if (necol) {
372 		for (icol = 0; icol <= maxcol; icol++) {
373 			col = tbl->cols + icol;
374 			if ( ! (col->flags & TBL_CELL_EQUAL))
375 				continue;
376 			if (col->width == ewidth)
377 				continue;
378 			if (nxcol && rmargin)
379 				xwidth += ewidth - col->width;
380 			col->width = ewidth;
381 		}
382 	}
383 
384 	/*
385 	 * If there are any columns to maximize, find the total
386 	 * available width, deducting 3n margins between columns.
387 	 * Distribute the available width evenly.
388 	 */
389 
390 	if (nxcol && rmargin) {
391 		xwidth += (*tbl->len)(3 * maxcol +
392 		    (opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ?
393 		     2 : !!opts->lvert + !!opts->rvert), tbl->arg);
394 		if (rmargin <= offset + xwidth)
395 			return;
396 		xwidth = rmargin - offset - xwidth;
397 
398 		/*
399 		 * Emulate a bug in GNU tbl width calculation that
400 		 * manifests itself for large numbers of x-columns.
401 		 * Emulating it for 5 x-columns gives identical
402 		 * behaviour for up to 6 x-columns.
403 		 */
404 
405 		if (nxcol == 5) {
406 			quirkcol = xwidth / enw % nxcol + 2;
407 			if (quirkcol != 3 && quirkcol != 4)
408 				quirkcol = -1;
409 		} else
410 			quirkcol = -1;
411 
412 		necol = 0;
413 		ewidth = 0;
414 		for (icol = 0; icol <= maxcol; icol++) {
415 			col = tbl->cols + icol;
416 			if ( ! (col->flags & TBL_CELL_WMAX))
417 				continue;
418 			col->width = (double)xwidth * ++necol / nxcol
419 			    - ewidth + 0.4995;
420 			if (necol == quirkcol)
421 				col->width -= enw;
422 			ewidth += col->width;
423 		}
424 	}
425 }
426 
427 static size_t
428 tblcalc_data(struct rofftbl *tbl, struct roffcol *col,
429     const struct tbl_opts *opts, const struct tbl_dat *dp, size_t mw)
430 {
431 	size_t		 sz;
432 
433 	/* Branch down into data sub-types. */
434 
435 	switch (dp->layout->pos) {
436 	case TBL_CELL_HORIZ:
437 	case TBL_CELL_DHORIZ:
438 		sz = (*tbl->len)(1, tbl->arg);
439 		if (col != NULL && col->width < sz)
440 			col->width = sz;
441 		return sz;
442 	case TBL_CELL_LONG:
443 	case TBL_CELL_CENTRE:
444 	case TBL_CELL_LEFT:
445 	case TBL_CELL_RIGHT:
446 		return tblcalc_literal(tbl, col, dp, mw);
447 	case TBL_CELL_NUMBER:
448 		return tblcalc_number(tbl, col, opts, dp);
449 	case TBL_CELL_DOWN:
450 		return 0;
451 	default:
452 		abort();
453 	}
454 }
455 
456 static size_t
457 tblcalc_literal(struct rofftbl *tbl, struct roffcol *col,
458     const struct tbl_dat *dp, size_t mw)
459 {
460 	const char	*str;	/* Beginning of the first line. */
461 	const char	*beg;	/* Beginning of the current line. */
462 	char		*end;	/* End of the current line. */
463 
464 	/* Widths in basic units. */
465 	size_t		 lsz;	/* Of the current line. */
466 	size_t		 wsz;	/* Of the current word. */
467 	size_t		 msz;   /* Of the longest line. */
468 	size_t		 enw;	/* Of one EN unit. */
469 
470 	if (dp->string == NULL || *dp->string == '\0')
471 		return 0;
472 	str = mw ? mandoc_strdup(dp->string) : dp->string;
473 	msz = lsz = 0;
474 	for (beg = str; beg != NULL && *beg != '\0'; beg = end) {
475 		end = mw ? strchr(beg, ' ') : NULL;
476 		if (end != NULL) {
477 			*end++ = '\0';
478 			while (*end == ' ')
479 				end++;
480 		}
481 		wsz = (*tbl->slen)(beg, tbl->arg);
482 		enw = (*tbl->len)(1, tbl->arg);
483 		if (mw && lsz && lsz + enw + wsz <= mw)
484 			lsz += enw + wsz;
485 		else
486 			lsz = wsz;
487 		if (msz < lsz)
488 			msz = lsz;
489 	}
490 	if (mw)
491 		free((void *)str);
492 	if (col != NULL && col->width < msz)
493 		col->width = msz;
494 	return msz;
495 }
496 
497 static size_t
498 tblcalc_number(struct rofftbl *tbl, struct roffcol *col,
499 		const struct tbl_opts *opts, const struct tbl_dat *dp)
500 {
501 	const char	*cp, *lastdigit, *lastpoint;
502 	size_t		 totsz;	/* Total width of the number in basic units. */
503 	size_t		 intsz; /* Width of the integer part in basic units. */
504 	char		 buf[2];
505 
506 	if (dp->string == NULL || *dp->string == '\0')
507 		return 0;
508 
509 	totsz = (*tbl->slen)(dp->string, tbl->arg);
510 	if (col == NULL)
511 		return totsz;
512 
513 	/*
514 	 * Find the last digit and
515 	 * the last decimal point that is adjacent to a digit.
516 	 * The alignment indicator "\&" overrides everything.
517 	 */
518 
519 	lastdigit = lastpoint = NULL;
520 	for (cp = dp->string; cp[0] != '\0'; cp++) {
521 		if (cp[0] == '\\' && cp[1] == '&') {
522 			lastdigit = lastpoint = cp;
523 			break;
524 		} else if (cp[0] == opts->decimal &&
525 		    (isdigit((unsigned char)cp[1]) ||
526 		     (cp > dp->string && isdigit((unsigned char)cp[-1]))))
527 			lastpoint = cp;
528 		else if (isdigit((unsigned char)cp[0]))
529 			lastdigit = cp;
530 	}
531 
532 	/* Not a number, treat as a literal string. */
533 
534 	if (lastdigit == NULL) {
535 		if (col != NULL && col->width < totsz)
536 			col->width = totsz;
537 		return totsz;
538 	}
539 
540 	/* Measure the width of the integer part. */
541 
542 	if (lastpoint == NULL)
543 		lastpoint = lastdigit + 1;
544 	intsz = 0;
545 	buf[1] = '\0';
546 	for (cp = dp->string; cp < lastpoint; cp++) {
547 		buf[0] = cp[0];
548 		intsz += (*tbl->slen)(buf, tbl->arg);
549 	}
550 
551 	/*
552          * If this number has more integer digits than all numbers
553          * seen on earlier lines, shift them all to the right.
554 	 * If it has fewer, shift this number to the right.
555 	 */
556 
557 	if (intsz > col->decimal) {
558 		col->nwidth += intsz - col->decimal;
559 		col->decimal = intsz;
560 	} else
561 		totsz += col->decimal - intsz;
562 
563 	/* Update the maximum total width seen so far. */
564 
565 	if (totsz > col->nwidth)
566 		col->nwidth = totsz;
567 	if (col->nwidth > col->width)
568 		col->width = col->nwidth;
569 	return totsz;
570 }
571