Annotation of src/usr.bin/mandoc/out.c, Revision 1.44
1.44 ! schwarze 1: /* $OpenBSD: out.c,v 1.43 2018/08/18 16:44:52 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.42 schwarze 21: #include <stdint.h>
1.1 schwarze 22: #include <stdlib.h>
1.2 schwarze 23: #include <string.h>
24: #include <time.h>
1.1 schwarze 25:
1.20 schwarze 26: #include "mandoc_aux.h"
1.9 schwarze 27: #include "mandoc.h"
1.1 schwarze 28: #include "out.h"
29:
1.9 schwarze 30: static void tblcalc_data(struct rofftbl *, struct roffcol *,
1.37 schwarze 31: const struct tbl_opts *, const struct tbl_dat *,
32: size_t);
1.9 schwarze 33: static void tblcalc_literal(struct rofftbl *, struct roffcol *,
1.37 schwarze 34: const struct tbl_dat *, size_t);
1.9 schwarze 35: static void tblcalc_number(struct rofftbl *, struct roffcol *,
1.18 schwarze 36: const struct tbl_opts *, const struct tbl_dat *);
1.9 schwarze 37:
1.21 schwarze 38:
39: /*
1.27 schwarze 40: * Parse the *src string and store a scaling unit into *dst.
41: * If the string doesn't specify the unit, use the default.
42: * If no default is specified, fail.
1.36 schwarze 43: * Return a pointer to the byte after the last byte used,
44: * or NULL on total failure.
1.1 schwarze 45: */
1.36 schwarze 46: const char *
1.1 schwarze 47: a2roffsu(const char *src, struct roffsu *dst, enum roffscale def)
48: {
1.27 schwarze 49: char *endptr;
1.1 schwarze 50:
1.28 schwarze 51: dst->unit = def == SCALE_MAX ? SCALE_BU : def;
52: dst->scale = strtod(src, &endptr);
53: if (endptr == src)
1.36 schwarze 54: return NULL;
1.1 schwarze 55:
1.28 schwarze 56: switch (*endptr++) {
1.21 schwarze 57: case 'c':
1.28 schwarze 58: dst->unit = SCALE_CM;
1.1 schwarze 59: break;
1.21 schwarze 60: case 'i':
1.28 schwarze 61: dst->unit = SCALE_IN;
62: break;
63: case 'f':
64: dst->unit = SCALE_FS;
65: break;
66: case 'M':
67: dst->unit = SCALE_MM;
68: break;
69: case 'm':
70: dst->unit = SCALE_EM;
71: break;
72: case 'n':
73: dst->unit = SCALE_EN;
1.1 schwarze 74: break;
1.21 schwarze 75: case 'P':
1.28 schwarze 76: dst->unit = SCALE_PC;
1.1 schwarze 77: break;
1.21 schwarze 78: case 'p':
1.28 schwarze 79: dst->unit = SCALE_PT;
1.1 schwarze 80: break;
1.28 schwarze 81: case 'u':
82: dst->unit = SCALE_BU;
1.1 schwarze 83: break;
1.21 schwarze 84: case 'v':
1.28 schwarze 85: dst->unit = SCALE_VS;
1.1 schwarze 86: break;
1.40 schwarze 87: default:
1.28 schwarze 88: endptr--;
1.1 schwarze 89: if (SCALE_MAX == def)
1.36 schwarze 90: return NULL;
1.28 schwarze 91: dst->unit = def;
1.1 schwarze 92: break;
93: }
1.36 schwarze 94: return endptr;
1.3 schwarze 95: }
1.9 schwarze 96:
97: /*
98: * Calculate the abstract widths and decimal positions of columns in a
99: * table. This routine allocates the columns structures then runs over
100: * all rows and cells in the table. The function pointers in "tbl" are
101: * used for the actual width calculations.
102: */
103: void
1.24 schwarze 104: tblcalc(struct rofftbl *tbl, const struct tbl_span *sp,
1.38 schwarze 105: size_t offset, size_t rmargin)
1.9 schwarze 106: {
1.37 schwarze 107: struct roffsu su;
1.30 schwarze 108: const struct tbl_opts *opts;
1.9 schwarze 109: const struct tbl_dat *dp;
110: struct roffcol *col;
1.24 schwarze 111: size_t ewidth, xwidth;
1.16 schwarze 112: int spans;
1.30 schwarze 113: int icol, maxcol, necol, nxcol, quirkcol;
1.9 schwarze 114:
115: /*
116: * Allocate the master column specifiers. These will hold the
117: * widths and decimal positions for all cells in the column. It
118: * must be freed and nullified by the caller.
119: */
120:
121: assert(NULL == tbl->cols);
1.21 schwarze 122: tbl->cols = mandoc_calloc((size_t)sp->opts->cols,
123: sizeof(struct roffcol));
1.30 schwarze 124: opts = sp->opts;
1.9 schwarze 125:
1.25 schwarze 126: for (maxcol = -1; sp; sp = sp->next) {
1.9 schwarze 127: if (TBL_SPAN_DATA != sp->pos)
128: continue;
1.16 schwarze 129: spans = 1;
1.9 schwarze 130: /*
131: * Account for the data cells in the layout, matching it
132: * to data cells in the data section.
133: */
134: for (dp = sp->first; dp; dp = dp->next) {
1.16 schwarze 135: /* Do not used spanned cells in the calculation. */
136: if (0 < --spans)
137: continue;
138: spans = dp->spans;
139: if (1 < spans)
140: continue;
1.31 schwarze 141: icol = dp->layout->col;
1.42 schwarze 142: while (maxcol < icol)
143: tbl->cols[++maxcol].spacing = SIZE_MAX;
1.24 schwarze 144: col = tbl->cols + icol;
145: col->flags |= dp->layout->flags;
146: if (dp->layout->flags & TBL_CELL_WIGN)
147: continue;
1.37 schwarze 148: if (dp->layout->wstr != NULL &&
149: dp->layout->width == 0 &&
150: a2roffsu(dp->layout->wstr, &su, SCALE_EN)
151: != NULL)
152: dp->layout->width =
153: (*tbl->sulen)(&su, tbl->arg);
154: if (col->width < dp->layout->width)
155: col->width = dp->layout->width;
1.42 schwarze 156: if (dp->layout->spacing != SIZE_MAX &&
157: (col->spacing == SIZE_MAX ||
158: col->spacing < dp->layout->spacing))
159: col->spacing = dp->layout->spacing;
1.38 schwarze 160: tblcalc_data(tbl, col, opts, dp,
1.39 schwarze 161: dp->block == 0 ? 0 :
162: dp->layout->width ? dp->layout->width :
1.41 schwarze 163: rmargin ? (rmargin + sp->opts->cols / 2)
164: / (sp->opts->cols + 1) : 0);
1.24 schwarze 165: }
166: }
167:
168: /*
1.44 ! schwarze 169: * Align numbers with text.
1.24 schwarze 170: * Count columns to equalize and columns to maximize.
171: * Find maximum width of the columns to equalize.
172: * Find total width of the columns *not* to maximize.
173: */
174:
175: necol = nxcol = 0;
176: ewidth = xwidth = 0;
177: for (icol = 0; icol <= maxcol; icol++) {
178: col = tbl->cols + icol;
1.44 ! schwarze 179: if (col->width > col->nwidth)
! 180: col->decimal += (col->width - col->nwidth) / 2;
! 181: else
! 182: col->width = col->nwidth;
1.42 schwarze 183: if (col->spacing == SIZE_MAX || icol == maxcol)
184: col->spacing = 3;
1.24 schwarze 185: if (col->flags & TBL_CELL_EQUAL) {
186: necol++;
187: if (ewidth < col->width)
188: ewidth = col->width;
189: }
190: if (col->flags & TBL_CELL_WMAX)
191: nxcol++;
192: else
193: xwidth += col->width;
194: }
195:
196: /*
197: * Equalize columns, if requested for any of them.
198: * Update total width of the columns not to maximize.
199: */
200:
201: if (necol) {
202: for (icol = 0; icol <= maxcol; icol++) {
203: col = tbl->cols + icol;
204: if ( ! (col->flags & TBL_CELL_EQUAL))
205: continue;
206: if (col->width == ewidth)
207: continue;
1.38 schwarze 208: if (nxcol && rmargin)
1.24 schwarze 209: xwidth += ewidth - col->width;
210: col->width = ewidth;
211: }
212: }
213:
214: /*
215: * If there are any columns to maximize, find the total
216: * available width, deducting 3n margins between columns.
217: * Distribute the available width evenly.
218: */
219:
1.38 schwarze 220: if (nxcol && rmargin) {
1.35 schwarze 221: xwidth += 3*maxcol +
1.30 schwarze 222: (opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ?
223: 2 : !!opts->lvert + !!opts->rvert);
1.38 schwarze 224: if (rmargin <= offset + xwidth)
1.35 schwarze 225: return;
1.38 schwarze 226: xwidth = rmargin - offset - xwidth;
1.30 schwarze 227:
228: /*
229: * Emulate a bug in GNU tbl width calculation that
230: * manifests itself for large numbers of x-columns.
231: * Emulating it for 5 x-columns gives identical
232: * behaviour for up to 6 x-columns.
233: */
234:
235: if (nxcol == 5) {
236: quirkcol = xwidth % nxcol + 2;
237: if (quirkcol != 3 && quirkcol != 4)
238: quirkcol = -1;
239: } else
240: quirkcol = -1;
241:
242: necol = 0;
243: ewidth = 0;
1.24 schwarze 244: for (icol = 0; icol <= maxcol; icol++) {
245: col = tbl->cols + icol;
246: if ( ! (col->flags & TBL_CELL_WMAX))
247: continue;
1.30 schwarze 248: col->width = (double)xwidth * ++necol / nxcol
249: - ewidth + 0.4995;
250: if (necol == quirkcol)
251: col->width--;
252: ewidth += col->width;
1.9 schwarze 253: }
254: }
255: }
256:
257: static void
258: tblcalc_data(struct rofftbl *tbl, struct roffcol *col,
1.37 schwarze 259: const struct tbl_opts *opts, const struct tbl_dat *dp, size_t mw)
1.9 schwarze 260: {
261: size_t sz;
262:
263: /* Branch down into data sub-types. */
264:
265: switch (dp->layout->pos) {
1.21 schwarze 266: case TBL_CELL_HORIZ:
267: case TBL_CELL_DHORIZ:
1.9 schwarze 268: sz = (*tbl->len)(1, tbl->arg);
269: if (col->width < sz)
270: col->width = sz;
271: break;
1.21 schwarze 272: case TBL_CELL_LONG:
273: case TBL_CELL_CENTRE:
274: case TBL_CELL_LEFT:
275: case TBL_CELL_RIGHT:
1.37 schwarze 276: tblcalc_literal(tbl, col, dp, mw);
1.9 schwarze 277: break;
1.21 schwarze 278: case TBL_CELL_NUMBER:
1.18 schwarze 279: tblcalc_number(tbl, col, opts, dp);
1.9 schwarze 280: break;
1.21 schwarze 281: case TBL_CELL_DOWN:
1.10 schwarze 282: break;
1.9 schwarze 283: default:
284: abort();
285: }
286: }
287:
288: static void
289: tblcalc_literal(struct rofftbl *tbl, struct roffcol *col,
1.37 schwarze 290: const struct tbl_dat *dp, size_t mw)
1.9 schwarze 291: {
1.37 schwarze 292: const char *str; /* Beginning of the first line. */
293: const char *beg; /* Beginning of the current line. */
294: char *end; /* End of the current line. */
1.38 schwarze 295: size_t lsz; /* Length of the current line. */
296: size_t wsz; /* Length of the current word. */
1.37 schwarze 297:
298: if (dp->string == NULL || *dp->string == '\0')
299: return;
300: str = mw ? mandoc_strdup(dp->string) : dp->string;
1.38 schwarze 301: lsz = 0;
1.37 schwarze 302: for (beg = str; beg != NULL && *beg != '\0'; beg = end) {
303: end = mw ? strchr(beg, ' ') : NULL;
304: if (end != NULL) {
305: *end++ = '\0';
306: while (*end == ' ')
307: end++;
308: }
1.38 schwarze 309: wsz = (*tbl->slen)(beg, tbl->arg);
310: if (mw && lsz && lsz + 1 + wsz <= mw)
311: lsz += 1 + wsz;
312: else
313: lsz = wsz;
314: if (col->width < lsz)
315: col->width = lsz;
1.37 schwarze 316: }
317: if (mw)
318: free((void *)str);
1.9 schwarze 319: }
320:
321: static void
322: tblcalc_number(struct rofftbl *tbl, struct roffcol *col,
1.18 schwarze 323: const struct tbl_opts *opts, const struct tbl_dat *dp)
1.9 schwarze 324: {
1.21 schwarze 325: int i;
1.43 schwarze 326: size_t sz, ssz, d;
1.10 schwarze 327: const char *str;
1.9 schwarze 328: char *cp;
329: char buf[2];
330:
331: /*
332: * First calculate number width and decimal place (last + 1 for
1.16 schwarze 333: * non-decimal numbers). If the stored decimal is subsequent to
1.9 schwarze 334: * ours, make our size longer by that difference
335: * (right-"shifting"); similarly, if ours is subsequent the
336: * stored, then extend the stored size by the difference.
337: * Finally, re-assign the stored values.
338: */
339:
1.10 schwarze 340: str = dp->string ? dp->string : "";
341: sz = (*tbl->slen)(str, tbl->arg);
1.9 schwarze 342:
1.10 schwarze 343: /* FIXME: TBL_DATA_HORIZ et al.? */
1.9 schwarze 344:
1.18 schwarze 345: buf[0] = opts->decimal;
1.9 schwarze 346: buf[1] = '\0';
347:
1.18 schwarze 348: if (NULL != (cp = strrchr(str, opts->decimal))) {
1.9 schwarze 349: buf[1] = '\0';
350: for (ssz = 0, i = 0; cp != &str[i]; i++) {
351: buf[0] = str[i];
352: ssz += (*tbl->slen)(buf, tbl->arg);
353: }
1.43 schwarze 354: d = ssz;
1.9 schwarze 355: } else
1.43 schwarze 356: d = sz;
1.9 schwarze 357:
358: /* Adjust the settings for this column. */
359:
360: if (col->decimal > d) {
361: sz += col->decimal - d;
362: d = col->decimal;
363: } else
1.44 ! schwarze 364: col->nwidth += d - col->decimal;
1.9 schwarze 365:
1.44 ! schwarze 366: if (sz > col->nwidth)
! 367: col->nwidth = sz;
1.9 schwarze 368: if (d > col->decimal)
369: col->decimal = d;
370: }