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