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