Annotation of mandoc/out.c, Revision 1.57
1.57 ! schwarze 1: /* $Id: out.c,v 1.56 2014/12/23 09:31:46 schwarze Exp $ */
1.1 kristaps 2: /*
1.36 schwarze 3: * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.52 schwarze 4: * Copyright (c) 2011, 2014 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.56 schwarze 43: * Return 2 on complete success, 1 when a conversion was done,
44: * but there was trailing garbage, and 0 on total failure.
1.3 kristaps 45: */
1.1 kristaps 46: int
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.5 kristaps 54: return(0);
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)
92: return(0);
1.56 schwarze 93: dst->unit = def;
1.3 kristaps 94: break;
95: }
1.1 kristaps 96:
1.56 schwarze 97: return(*endptr == '\0' ? 2 : 1);
1.8 kristaps 98: }
1.30 kristaps 99:
100: /*
101: * Calculate the abstract widths and decimal positions of columns in a
102: * table. This routine allocates the columns structures then runs over
103: * all rows and cells in the table. The function pointers in "tbl" are
104: * used for the actual width calculations.
105: */
106: void
1.52 schwarze 107: tblcalc(struct rofftbl *tbl, const struct tbl_span *sp,
108: size_t totalwidth)
1.30 kristaps 109: {
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.52 schwarze 114: int icol, maxcol, necol, nxcol;
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.30 kristaps 125:
1.53 schwarze 126: for (maxcol = -1; sp; sp = sp->next) {
1.30 kristaps 127: if (TBL_SPAN_DATA != sp->pos)
128: continue;
1.43 schwarze 129: spans = 1;
1.30 kristaps 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.43 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.52 schwarze 141: icol = dp->layout->head->ident;
142: if (maxcol < icol)
143: maxcol = icol;
144: col = tbl->cols + icol;
145: col->flags |= dp->layout->flags;
146: if (dp->layout->flags & TBL_CELL_WIGN)
147: continue;
1.45 schwarze 148: tblcalc_data(tbl, col, sp->opts, dp);
1.52 schwarze 149: }
150: }
151:
152: /*
153: * Count columns to equalize and columns to maximize.
154: * Find maximum width of the columns to equalize.
155: * Find total width of the columns *not* to maximize.
156: */
157:
158: necol = nxcol = 0;
159: ewidth = xwidth = 0;
160: for (icol = 0; icol <= maxcol; icol++) {
161: col = tbl->cols + icol;
162: if (col->flags & TBL_CELL_EQUAL) {
163: necol++;
164: if (ewidth < col->width)
165: ewidth = col->width;
166: }
167: if (col->flags & TBL_CELL_WMAX)
168: nxcol++;
169: else
170: xwidth += col->width;
171: }
172:
173: /*
174: * Equalize columns, if requested for any of them.
175: * Update total width of the columns not to maximize.
176: */
177:
178: if (necol) {
179: for (icol = 0; icol <= maxcol; icol++) {
180: col = tbl->cols + icol;
181: if ( ! (col->flags & TBL_CELL_EQUAL))
182: continue;
183: if (col->width == ewidth)
184: continue;
185: if (nxcol && totalwidth)
186: xwidth += ewidth - col->width;
187: col->width = ewidth;
188: }
189: }
190:
191: /*
192: * If there are any columns to maximize, find the total
193: * available width, deducting 3n margins between columns.
194: * Distribute the available width evenly.
195: */
196:
197: if (nxcol && totalwidth) {
198: xwidth = totalwidth - 3*maxcol - xwidth;
199: for (icol = 0; icol <= maxcol; icol++) {
200: col = tbl->cols + icol;
201: if ( ! (col->flags & TBL_CELL_WMAX))
202: continue;
203: col->width = xwidth / nxcol--;
204: xwidth -= col->width;
1.30 kristaps 205: }
206: }
207: }
208:
209: static void
210: tblcalc_data(struct rofftbl *tbl, struct roffcol *col,
1.45 schwarze 211: const struct tbl_opts *opts, const struct tbl_dat *dp)
1.30 kristaps 212: {
213: size_t sz;
214:
215: /* Branch down into data sub-types. */
216:
217: switch (dp->layout->pos) {
1.48 schwarze 218: case TBL_CELL_HORIZ:
1.30 kristaps 219: /* FALLTHROUGH */
1.48 schwarze 220: case TBL_CELL_DHORIZ:
1.30 kristaps 221: sz = (*tbl->len)(1, tbl->arg);
222: if (col->width < sz)
223: col->width = sz;
224: break;
1.48 schwarze 225: case TBL_CELL_LONG:
1.30 kristaps 226: /* FALLTHROUGH */
1.48 schwarze 227: case TBL_CELL_CENTRE:
1.30 kristaps 228: /* FALLTHROUGH */
1.48 schwarze 229: case TBL_CELL_LEFT:
1.30 kristaps 230: /* FALLTHROUGH */
1.48 schwarze 231: case TBL_CELL_RIGHT:
1.30 kristaps 232: tblcalc_literal(tbl, col, dp);
233: break;
1.48 schwarze 234: case TBL_CELL_NUMBER:
1.45 schwarze 235: tblcalc_number(tbl, col, opts, dp);
1.35 kristaps 236: break;
1.48 schwarze 237: case TBL_CELL_DOWN:
1.30 kristaps 238: break;
239: default:
240: abort();
241: /* NOTREACHED */
242: }
243: }
244:
245: static void
246: tblcalc_literal(struct rofftbl *tbl, struct roffcol *col,
247: const struct tbl_dat *dp)
248: {
1.43 schwarze 249: size_t sz;
1.34 kristaps 250: const char *str;
1.30 kristaps 251:
1.34 kristaps 252: str = dp->string ? dp->string : "";
253: sz = (*tbl->slen)(str, tbl->arg);
254:
1.30 kristaps 255: if (col->width < sz)
256: col->width = sz;
257: }
258:
259: static void
260: tblcalc_number(struct rofftbl *tbl, struct roffcol *col,
1.45 schwarze 261: const struct tbl_opts *opts, const struct tbl_dat *dp)
1.30 kristaps 262: {
1.48 schwarze 263: int i;
1.34 kristaps 264: size_t sz, psz, ssz, d;
265: const char *str;
1.30 kristaps 266: char *cp;
267: char buf[2];
268:
269: /*
270: * First calculate number width and decimal place (last + 1 for
1.43 schwarze 271: * non-decimal numbers). If the stored decimal is subsequent to
1.30 kristaps 272: * ours, make our size longer by that difference
273: * (right-"shifting"); similarly, if ours is subsequent the
274: * stored, then extend the stored size by the difference.
275: * Finally, re-assign the stored values.
276: */
277:
1.34 kristaps 278: str = dp->string ? dp->string : "";
279: sz = (*tbl->slen)(str, tbl->arg);
1.30 kristaps 280:
1.34 kristaps 281: /* FIXME: TBL_DATA_HORIZ et al.? */
1.30 kristaps 282:
1.45 schwarze 283: buf[0] = opts->decimal;
1.30 kristaps 284: buf[1] = '\0';
285:
286: psz = (*tbl->slen)(buf, tbl->arg);
287:
1.45 schwarze 288: if (NULL != (cp = strrchr(str, opts->decimal))) {
1.30 kristaps 289: buf[1] = '\0';
290: for (ssz = 0, i = 0; cp != &str[i]; i++) {
291: buf[0] = str[i];
292: ssz += (*tbl->slen)(buf, tbl->arg);
293: }
294: d = ssz + psz;
295: } else
296: d = sz + psz;
297:
298: /* Adjust the settings for this column. */
299:
300: if (col->decimal > d) {
301: sz += col->decimal - d;
302: d = col->decimal;
303: } else
304: col->width += d - col->decimal;
305:
306: if (sz > col->width)
307: col->width = sz;
308: if (d > col->decimal)
309: col->decimal = d;
310: }
CVSweb