Annotation of mandoc/out.c, Revision 1.64
1.64 ! schwarze 1: /* $Id: out.c,v 1.63 2017/05/01 20:54:59 schwarze Exp $ */
1.1 kristaps 2: /*
1.36 schwarze 3: * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.63 schwarze 4: * Copyright (c) 2011, 2014, 2015, 2017 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.1 kristaps 23: #include <stdlib.h>
1.6 kristaps 24: #include <string.h>
1.7 kristaps 25: #include <time.h>
1.1 kristaps 26:
1.47 schwarze 27: #include "mandoc_aux.h"
1.30 kristaps 28: #include "mandoc.h"
1.1 kristaps 29: #include "out.h"
30:
1.30 kristaps 31: static void tblcalc_data(struct rofftbl *, struct roffcol *,
1.45 schwarze 32: const struct tbl_opts *, const struct tbl_dat *);
1.30 kristaps 33: static void tblcalc_literal(struct rofftbl *, struct roffcol *,
34: const struct tbl_dat *);
35: static void tblcalc_number(struct rofftbl *, struct roffcol *,
1.45 schwarze 36: const struct tbl_opts *, const struct tbl_dat *);
1.30 kristaps 37:
1.48 schwarze 38:
39: /*
1.55 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.64 ! schwarze 43: * Return a pointer to the byte after the last byte used,
! 44: * or NULL on total failure.
1.3 kristaps 45: */
1.64 ! schwarze 46: const char *
1.5 kristaps 47: a2roffsu(const char *src, struct roffsu *dst, enum roffscale def)
1.1 kristaps 48: {
1.55 schwarze 49: char *endptr;
1.1 kristaps 50:
1.56 schwarze 51: dst->unit = def == SCALE_MAX ? SCALE_BU : def;
52: dst->scale = strtod(src, &endptr);
53: if (endptr == src)
1.64 ! schwarze 54: return NULL;
1.5 kristaps 55:
1.56 schwarze 56: switch (*endptr++) {
1.48 schwarze 57: case 'c':
1.56 schwarze 58: dst->unit = SCALE_CM;
1.3 kristaps 59: break;
1.48 schwarze 60: case 'i':
1.56 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.3 kristaps 74: break;
1.48 schwarze 75: case 'P':
1.56 schwarze 76: dst->unit = SCALE_PC;
1.3 kristaps 77: break;
1.48 schwarze 78: case 'p':
1.56 schwarze 79: dst->unit = SCALE_PT;
1.3 kristaps 80: break;
1.56 schwarze 81: case 'u':
82: dst->unit = SCALE_BU;
1.3 kristaps 83: break;
1.48 schwarze 84: case 'v':
1.56 schwarze 85: dst->unit = SCALE_VS;
1.3 kristaps 86: break;
1.48 schwarze 87: case '\0':
1.56 schwarze 88: endptr--;
89: /* FALLTHROUGH */
90: default:
1.5 kristaps 91: if (SCALE_MAX == def)
1.64 ! schwarze 92: return NULL;
1.56 schwarze 93: dst->unit = def;
1.3 kristaps 94: break;
95: }
1.64 ! schwarze 96: return endptr;
1.8 kristaps 97: }
1.30 kristaps 98:
99: /*
100: * Calculate the abstract widths and decimal positions of columns in a
101: * table. This routine allocates the columns structures then runs over
102: * all rows and cells in the table. The function pointers in "tbl" are
103: * used for the actual width calculations.
104: */
105: void
1.52 schwarze 106: tblcalc(struct rofftbl *tbl, const struct tbl_span *sp,
107: size_t totalwidth)
1.30 kristaps 108: {
1.58 schwarze 109: const struct tbl_opts *opts;
1.30 kristaps 110: const struct tbl_dat *dp;
111: struct roffcol *col;
1.52 schwarze 112: size_t ewidth, xwidth;
1.43 schwarze 113: int spans;
1.58 schwarze 114: int icol, maxcol, necol, nxcol, quirkcol;
1.30 kristaps 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.48 schwarze 123: tbl->cols = mandoc_calloc((size_t)sp->opts->cols,
124: sizeof(struct roffcol));
1.58 schwarze 125: opts = sp->opts;
1.30 kristaps 126:
1.53 schwarze 127: for (maxcol = -1; sp; sp = sp->next) {
1.30 kristaps 128: if (TBL_SPAN_DATA != sp->pos)
129: continue;
1.43 schwarze 130: spans = 1;
1.30 kristaps 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.43 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.59 schwarze 142: icol = dp->layout->col;
1.52 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.58 schwarze 149: tblcalc_data(tbl, col, opts, dp);
1.52 schwarze 150: }
151: }
152:
153: /*
154: * Count columns to equalize and columns to maximize.
155: * Find maximum width of the columns to equalize.
156: * Find total width of the columns *not* to maximize.
157: */
158:
159: necol = nxcol = 0;
160: ewidth = xwidth = 0;
161: for (icol = 0; icol <= maxcol; icol++) {
162: col = tbl->cols + icol;
163: if (col->flags & TBL_CELL_EQUAL) {
164: necol++;
165: if (ewidth < col->width)
166: ewidth = col->width;
167: }
168: if (col->flags & TBL_CELL_WMAX)
169: nxcol++;
170: else
171: xwidth += col->width;
172: }
173:
174: /*
175: * Equalize columns, if requested for any of them.
176: * Update total width of the columns not to maximize.
177: */
178:
179: if (necol) {
180: for (icol = 0; icol <= maxcol; icol++) {
181: col = tbl->cols + icol;
182: if ( ! (col->flags & TBL_CELL_EQUAL))
183: continue;
184: if (col->width == ewidth)
185: continue;
186: if (nxcol && totalwidth)
187: xwidth += ewidth - col->width;
188: col->width = ewidth;
189: }
190: }
191:
192: /*
193: * If there are any columns to maximize, find the total
194: * available width, deducting 3n margins between columns.
195: * Distribute the available width evenly.
196: */
197:
198: if (nxcol && totalwidth) {
1.63 schwarze 199: xwidth += 3*maxcol +
1.58 schwarze 200: (opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ?
201: 2 : !!opts->lvert + !!opts->rvert);
1.63 schwarze 202: if (xwidth >= totalwidth)
203: return;
204: xwidth = totalwidth - xwidth;
1.58 schwarze 205:
206: /*
207: * Emulate a bug in GNU tbl width calculation that
208: * manifests itself for large numbers of x-columns.
209: * Emulating it for 5 x-columns gives identical
210: * behaviour for up to 6 x-columns.
211: */
212:
213: if (nxcol == 5) {
214: quirkcol = xwidth % nxcol + 2;
215: if (quirkcol != 3 && quirkcol != 4)
216: quirkcol = -1;
217: } else
218: quirkcol = -1;
219:
220: necol = 0;
221: ewidth = 0;
1.52 schwarze 222: for (icol = 0; icol <= maxcol; icol++) {
223: col = tbl->cols + icol;
224: if ( ! (col->flags & TBL_CELL_WMAX))
225: continue;
1.58 schwarze 226: col->width = (double)xwidth * ++necol / nxcol
227: - ewidth + 0.4995;
228: if (necol == quirkcol)
229: col->width--;
230: ewidth += col->width;
1.30 kristaps 231: }
232: }
233: }
234:
235: static void
236: tblcalc_data(struct rofftbl *tbl, struct roffcol *col,
1.45 schwarze 237: const struct tbl_opts *opts, const struct tbl_dat *dp)
1.30 kristaps 238: {
239: size_t sz;
240:
241: /* Branch down into data sub-types. */
242:
243: switch (dp->layout->pos) {
1.48 schwarze 244: case TBL_CELL_HORIZ:
245: case TBL_CELL_DHORIZ:
1.30 kristaps 246: sz = (*tbl->len)(1, tbl->arg);
247: if (col->width < sz)
248: col->width = sz;
249: break;
1.48 schwarze 250: case TBL_CELL_LONG:
251: case TBL_CELL_CENTRE:
252: case TBL_CELL_LEFT:
253: case TBL_CELL_RIGHT:
1.30 kristaps 254: tblcalc_literal(tbl, col, dp);
255: break;
1.48 schwarze 256: case TBL_CELL_NUMBER:
1.45 schwarze 257: tblcalc_number(tbl, col, opts, dp);
1.35 kristaps 258: break;
1.48 schwarze 259: case TBL_CELL_DOWN:
1.30 kristaps 260: break;
261: default:
262: abort();
263: }
264: }
265:
266: static void
267: tblcalc_literal(struct rofftbl *tbl, struct roffcol *col,
268: const struct tbl_dat *dp)
269: {
1.43 schwarze 270: size_t sz;
1.34 kristaps 271: const char *str;
1.30 kristaps 272:
1.34 kristaps 273: str = dp->string ? dp->string : "";
274: sz = (*tbl->slen)(str, tbl->arg);
275:
1.30 kristaps 276: if (col->width < sz)
277: col->width = sz;
278: }
279:
280: static void
281: tblcalc_number(struct rofftbl *tbl, struct roffcol *col,
1.45 schwarze 282: const struct tbl_opts *opts, const struct tbl_dat *dp)
1.30 kristaps 283: {
1.48 schwarze 284: int i;
1.34 kristaps 285: size_t sz, psz, ssz, d;
286: const char *str;
1.30 kristaps 287: char *cp;
288: char buf[2];
289:
290: /*
291: * First calculate number width and decimal place (last + 1 for
1.43 schwarze 292: * non-decimal numbers). If the stored decimal is subsequent to
1.30 kristaps 293: * ours, make our size longer by that difference
294: * (right-"shifting"); similarly, if ours is subsequent the
295: * stored, then extend the stored size by the difference.
296: * Finally, re-assign the stored values.
297: */
298:
1.34 kristaps 299: str = dp->string ? dp->string : "";
300: sz = (*tbl->slen)(str, tbl->arg);
1.30 kristaps 301:
1.34 kristaps 302: /* FIXME: TBL_DATA_HORIZ et al.? */
1.30 kristaps 303:
1.45 schwarze 304: buf[0] = opts->decimal;
1.30 kristaps 305: buf[1] = '\0';
306:
307: psz = (*tbl->slen)(buf, tbl->arg);
308:
1.45 schwarze 309: if (NULL != (cp = strrchr(str, opts->decimal))) {
1.30 kristaps 310: buf[1] = '\0';
311: for (ssz = 0, i = 0; cp != &str[i]; i++) {
312: buf[0] = str[i];
313: ssz += (*tbl->slen)(buf, tbl->arg);
314: }
315: d = ssz + psz;
316: } else
317: d = sz + psz;
318:
319: /* Adjust the settings for this column. */
320:
321: if (col->decimal > d) {
322: sz += col->decimal - d;
323: d = col->decimal;
324: } else
325: col->width += d - col->decimal;
326:
327: if (sz > col->width)
328: col->width = sz;
329: if (d > col->decimal)
330: col->decimal = d;
331: }
CVSweb