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