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