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