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