Annotation of src/usr.bin/mandoc/out.c, Revision 1.22
1.22 ! schwarze 1: /* $Id: out.c,v 1.21 2014/04/20 16:44:44 schwarze Exp $ */
1.1 schwarze 2: /*
1.9 schwarze 3: * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.12 schwarze 4: * Copyright (c) 2011 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 <ctype.h>
22: #include <stdio.h>
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.1 schwarze 29: #include "out.h"
30:
1.9 schwarze 31: static void tblcalc_data(struct rofftbl *, struct roffcol *,
1.18 schwarze 32: const struct tbl_opts *, const struct tbl_dat *);
1.9 schwarze 33: static void tblcalc_literal(struct rofftbl *, struct roffcol *,
34: const struct tbl_dat *);
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.1 schwarze 40: * Convert a `scaling unit' to a consistent form, or fail. Scaling
41: * units are documented in groff.7, mdoc.7, man.7.
42: */
43: int
44: a2roffsu(const char *src, struct roffsu *dst, enum roffscale def)
45: {
46: char buf[BUFSIZ], hasd;
47: int i;
48: enum roffscale unit;
49:
50: if ('\0' == *src)
51: return(0);
52:
53: i = hasd = 0;
54:
55: switch (*src) {
1.21 schwarze 56: case '+':
1.1 schwarze 57: src++;
58: break;
1.21 schwarze 59: case '-':
1.1 schwarze 60: buf[i++] = *src++;
61: break;
62: default:
63: break;
64: }
65:
66: if ('\0' == *src)
67: return(0);
68:
69: while (i < BUFSIZ) {
1.15 schwarze 70: if ( ! isdigit((unsigned char)*src)) {
1.1 schwarze 71: if ('.' != *src)
72: break;
73: else if (hasd)
74: break;
75: else
76: hasd = 1;
77: }
78: buf[i++] = *src++;
79: }
80:
81: if (BUFSIZ == i || (*src && *(src + 1)))
82: return(0);
83:
84: buf[i] = '\0';
85:
86: switch (*src) {
1.21 schwarze 87: case 'c':
1.1 schwarze 88: unit = SCALE_CM;
89: break;
1.21 schwarze 90: case 'i':
1.1 schwarze 91: unit = SCALE_IN;
92: break;
1.21 schwarze 93: case 'P':
1.1 schwarze 94: unit = SCALE_PC;
95: break;
1.21 schwarze 96: case 'p':
1.1 schwarze 97: unit = SCALE_PT;
98: break;
1.21 schwarze 99: case 'f':
1.1 schwarze 100: unit = SCALE_FS;
101: break;
1.21 schwarze 102: case 'v':
1.1 schwarze 103: unit = SCALE_VS;
104: break;
1.21 schwarze 105: case 'm':
1.1 schwarze 106: unit = SCALE_EM;
107: break;
1.21 schwarze 108: case '\0':
1.1 schwarze 109: if (SCALE_MAX == def)
110: return(0);
111: unit = SCALE_BU;
112: break;
1.21 schwarze 113: case 'u':
1.1 schwarze 114: unit = SCALE_BU;
115: break;
1.21 schwarze 116: case 'M':
1.1 schwarze 117: unit = SCALE_MM;
118: break;
1.21 schwarze 119: case 'n':
1.1 schwarze 120: unit = SCALE_EN;
121: break;
122: default:
123: return(0);
124: }
125:
1.6 schwarze 126: /* FIXME: do this in the caller. */
1.22 ! schwarze 127: if ((dst->scale = atof(buf)) < 0.0)
! 128: dst->scale = 0.0;
1.1 schwarze 129: dst->unit = unit;
130: return(1);
1.3 schwarze 131: }
1.9 schwarze 132:
133: /*
134: * Calculate the abstract widths and decimal positions of columns in a
135: * table. This routine allocates the columns structures then runs over
136: * all rows and cells in the table. The function pointers in "tbl" are
137: * used for the actual width calculations.
138: */
139: void
140: tblcalc(struct rofftbl *tbl, const struct tbl_span *sp)
141: {
142: const struct tbl_dat *dp;
143: struct roffcol *col;
1.16 schwarze 144: int spans;
1.9 schwarze 145:
146: /*
147: * Allocate the master column specifiers. These will hold the
148: * widths and decimal positions for all cells in the column. It
149: * must be freed and nullified by the caller.
150: */
151:
152: assert(NULL == tbl->cols);
1.21 schwarze 153: tbl->cols = mandoc_calloc((size_t)sp->opts->cols,
154: sizeof(struct roffcol));
1.9 schwarze 155:
156: for ( ; sp; sp = sp->next) {
157: if (TBL_SPAN_DATA != sp->pos)
158: continue;
1.16 schwarze 159: spans = 1;
1.9 schwarze 160: /*
161: * Account for the data cells in the layout, matching it
162: * to data cells in the data section.
163: */
164: for (dp = sp->first; dp; dp = dp->next) {
1.16 schwarze 165: /* Do not used spanned cells in the calculation. */
166: if (0 < --spans)
167: continue;
168: spans = dp->spans;
169: if (1 < spans)
170: continue;
1.10 schwarze 171: assert(dp->layout);
1.9 schwarze 172: col = &tbl->cols[dp->layout->head->ident];
1.18 schwarze 173: tblcalc_data(tbl, col, sp->opts, dp);
1.9 schwarze 174: }
175: }
176: }
177:
178: static void
179: tblcalc_data(struct rofftbl *tbl, struct roffcol *col,
1.18 schwarze 180: const struct tbl_opts *opts, const struct tbl_dat *dp)
1.9 schwarze 181: {
182: size_t sz;
183:
184: /* Branch down into data sub-types. */
185:
186: switch (dp->layout->pos) {
1.21 schwarze 187: case TBL_CELL_HORIZ:
1.9 schwarze 188: /* FALLTHROUGH */
1.21 schwarze 189: case TBL_CELL_DHORIZ:
1.9 schwarze 190: sz = (*tbl->len)(1, tbl->arg);
191: if (col->width < sz)
192: col->width = sz;
193: break;
1.21 schwarze 194: case TBL_CELL_LONG:
1.9 schwarze 195: /* FALLTHROUGH */
1.21 schwarze 196: case TBL_CELL_CENTRE:
1.9 schwarze 197: /* FALLTHROUGH */
1.21 schwarze 198: case TBL_CELL_LEFT:
1.9 schwarze 199: /* FALLTHROUGH */
1.21 schwarze 200: case TBL_CELL_RIGHT:
1.9 schwarze 201: tblcalc_literal(tbl, col, dp);
202: break;
1.21 schwarze 203: case TBL_CELL_NUMBER:
1.18 schwarze 204: tblcalc_number(tbl, col, opts, dp);
1.9 schwarze 205: break;
1.21 schwarze 206: case TBL_CELL_DOWN:
1.10 schwarze 207: break;
1.9 schwarze 208: default:
209: abort();
210: /* NOTREACHED */
211: }
212: }
213:
214: static void
215: tblcalc_literal(struct rofftbl *tbl, struct roffcol *col,
216: const struct tbl_dat *dp)
217: {
1.16 schwarze 218: size_t sz;
1.10 schwarze 219: const char *str;
1.9 schwarze 220:
1.10 schwarze 221: str = dp->string ? dp->string : "";
222: sz = (*tbl->slen)(str, tbl->arg);
223:
1.9 schwarze 224: if (col->width < sz)
225: col->width = sz;
226: }
227:
228: static void
229: tblcalc_number(struct rofftbl *tbl, struct roffcol *col,
1.18 schwarze 230: const struct tbl_opts *opts, const struct tbl_dat *dp)
1.9 schwarze 231: {
1.21 schwarze 232: int i;
1.10 schwarze 233: size_t sz, psz, ssz, d;
234: const char *str;
1.9 schwarze 235: char *cp;
236: char buf[2];
237:
238: /*
239: * First calculate number width and decimal place (last + 1 for
1.16 schwarze 240: * non-decimal numbers). If the stored decimal is subsequent to
1.9 schwarze 241: * ours, make our size longer by that difference
242: * (right-"shifting"); similarly, if ours is subsequent the
243: * stored, then extend the stored size by the difference.
244: * Finally, re-assign the stored values.
245: */
246:
1.10 schwarze 247: str = dp->string ? dp->string : "";
248: sz = (*tbl->slen)(str, tbl->arg);
1.9 schwarze 249:
1.10 schwarze 250: /* FIXME: TBL_DATA_HORIZ et al.? */
1.9 schwarze 251:
1.18 schwarze 252: buf[0] = opts->decimal;
1.9 schwarze 253: buf[1] = '\0';
254:
255: psz = (*tbl->slen)(buf, tbl->arg);
256:
1.18 schwarze 257: if (NULL != (cp = strrchr(str, opts->decimal))) {
1.9 schwarze 258: buf[1] = '\0';
259: for (ssz = 0, i = 0; cp != &str[i]; i++) {
260: buf[0] = str[i];
261: ssz += (*tbl->slen)(buf, tbl->arg);
262: }
263: d = ssz + psz;
264: } else
265: d = sz + psz;
266:
267: /* Adjust the settings for this column. */
268:
269: if (col->decimal > d) {
270: sz += col->decimal - d;
271: d = col->decimal;
272: } else
273: col->width += d - col->decimal;
274:
275: if (sz > col->width)
276: col->width = sz;
277: if (d > col->decimal)
278: col->decimal = d;
279: }