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