]> git.cameronkatri.com Git - mandoc.git/blob - out.c
some scaling unit fixes:
[mandoc.git] / out.c
1 /* $Id: out.c,v 1.56 2014/12/23 09:31:46 schwarze Exp $ */
2 /*
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2014 Ingo Schwarze <schwarze@openbsd.org>
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 "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26
27 #include "mandoc_aux.h"
28 #include "mandoc.h"
29 #include "out.h"
30
31 static void tblcalc_data(struct rofftbl *, struct roffcol *,
32 const struct tbl_opts *, const struct tbl_dat *);
33 static void tblcalc_literal(struct rofftbl *, struct roffcol *,
34 const struct tbl_dat *);
35 static void tblcalc_number(struct rofftbl *, struct roffcol *,
36 const struct tbl_opts *, const struct tbl_dat *);
37
38
39 /*
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.
43 * Return 2 on complete success, 1 when a conversion was done,
44 * but there was trailing garbage, and 0 on total failure.
45 */
46 int
47 a2roffsu(const char *src, struct roffsu *dst, enum roffscale def)
48 {
49 char *endptr;
50
51 dst->unit = def == SCALE_MAX ? SCALE_BU : def;
52 dst->scale = strtod(src, &endptr);
53 if (endptr == src)
54 return(0);
55
56 switch (*endptr++) {
57 case 'c':
58 dst->unit = SCALE_CM;
59 break;
60 case 'i':
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;
74 break;
75 case 'P':
76 dst->unit = SCALE_PC;
77 break;
78 case 'p':
79 dst->unit = SCALE_PT;
80 break;
81 case 'u':
82 dst->unit = SCALE_BU;
83 break;
84 case 'v':
85 dst->unit = SCALE_VS;
86 break;
87 case '\0':
88 endptr--;
89 /* FALLTHROUGH */
90 default:
91 if (SCALE_MAX == def)
92 return(0);
93 dst->unit = def;
94 break;
95 }
96
97 /* FIXME: do this in the caller. */
98 if (dst->scale < 0.0)
99 dst->scale = 0.0;
100 return(*endptr == '\0' ? 2 : 1);
101 }
102
103 /*
104 * Calculate the abstract widths and decimal positions of columns in a
105 * table. This routine allocates the columns structures then runs over
106 * all rows and cells in the table. The function pointers in "tbl" are
107 * used for the actual width calculations.
108 */
109 void
110 tblcalc(struct rofftbl *tbl, const struct tbl_span *sp,
111 size_t totalwidth)
112 {
113 const struct tbl_dat *dp;
114 struct roffcol *col;
115 size_t ewidth, xwidth;
116 int spans;
117 int icol, maxcol, necol, nxcol;
118
119 /*
120 * Allocate the master column specifiers. These will hold the
121 * widths and decimal positions for all cells in the column. It
122 * must be freed and nullified by the caller.
123 */
124
125 assert(NULL == tbl->cols);
126 tbl->cols = mandoc_calloc((size_t)sp->opts->cols,
127 sizeof(struct roffcol));
128
129 for (maxcol = -1; sp; sp = sp->next) {
130 if (TBL_SPAN_DATA != sp->pos)
131 continue;
132 spans = 1;
133 /*
134 * Account for the data cells in the layout, matching it
135 * to data cells in the data section.
136 */
137 for (dp = sp->first; dp; dp = dp->next) {
138 /* Do not used spanned cells in the calculation. */
139 if (0 < --spans)
140 continue;
141 spans = dp->spans;
142 if (1 < spans)
143 continue;
144 icol = dp->layout->head->ident;
145 if (maxcol < icol)
146 maxcol = icol;
147 col = tbl->cols + icol;
148 col->flags |= dp->layout->flags;
149 if (dp->layout->flags & TBL_CELL_WIGN)
150 continue;
151 tblcalc_data(tbl, col, sp->opts, dp);
152 }
153 }
154
155 /*
156 * Count columns to equalize and columns to maximize.
157 * Find maximum width of the columns to equalize.
158 * Find total width of the columns *not* to maximize.
159 */
160
161 necol = nxcol = 0;
162 ewidth = xwidth = 0;
163 for (icol = 0; icol <= maxcol; icol++) {
164 col = tbl->cols + icol;
165 if (col->flags & TBL_CELL_EQUAL) {
166 necol++;
167 if (ewidth < col->width)
168 ewidth = col->width;
169 }
170 if (col->flags & TBL_CELL_WMAX)
171 nxcol++;
172 else
173 xwidth += col->width;
174 }
175
176 /*
177 * Equalize columns, if requested for any of them.
178 * Update total width of the columns not to maximize.
179 */
180
181 if (necol) {
182 for (icol = 0; icol <= maxcol; icol++) {
183 col = tbl->cols + icol;
184 if ( ! (col->flags & TBL_CELL_EQUAL))
185 continue;
186 if (col->width == ewidth)
187 continue;
188 if (nxcol && totalwidth)
189 xwidth += ewidth - col->width;
190 col->width = ewidth;
191 }
192 }
193
194 /*
195 * If there are any columns to maximize, find the total
196 * available width, deducting 3n margins between columns.
197 * Distribute the available width evenly.
198 */
199
200 if (nxcol && totalwidth) {
201 xwidth = totalwidth - 3*maxcol - xwidth;
202 for (icol = 0; icol <= maxcol; icol++) {
203 col = tbl->cols + icol;
204 if ( ! (col->flags & TBL_CELL_WMAX))
205 continue;
206 col->width = xwidth / nxcol--;
207 xwidth -= col->width;
208 }
209 }
210 }
211
212 static void
213 tblcalc_data(struct rofftbl *tbl, struct roffcol *col,
214 const struct tbl_opts *opts, const struct tbl_dat *dp)
215 {
216 size_t sz;
217
218 /* Branch down into data sub-types. */
219
220 switch (dp->layout->pos) {
221 case TBL_CELL_HORIZ:
222 /* FALLTHROUGH */
223 case TBL_CELL_DHORIZ:
224 sz = (*tbl->len)(1, tbl->arg);
225 if (col->width < sz)
226 col->width = sz;
227 break;
228 case TBL_CELL_LONG:
229 /* FALLTHROUGH */
230 case TBL_CELL_CENTRE:
231 /* FALLTHROUGH */
232 case TBL_CELL_LEFT:
233 /* FALLTHROUGH */
234 case TBL_CELL_RIGHT:
235 tblcalc_literal(tbl, col, dp);
236 break;
237 case TBL_CELL_NUMBER:
238 tblcalc_number(tbl, col, opts, dp);
239 break;
240 case TBL_CELL_DOWN:
241 break;
242 default:
243 abort();
244 /* NOTREACHED */
245 }
246 }
247
248 static void
249 tblcalc_literal(struct rofftbl *tbl, struct roffcol *col,
250 const struct tbl_dat *dp)
251 {
252 size_t sz;
253 const char *str;
254
255 str = dp->string ? dp->string : "";
256 sz = (*tbl->slen)(str, tbl->arg);
257
258 if (col->width < sz)
259 col->width = sz;
260 }
261
262 static void
263 tblcalc_number(struct rofftbl *tbl, struct roffcol *col,
264 const struct tbl_opts *opts, const struct tbl_dat *dp)
265 {
266 int i;
267 size_t sz, psz, ssz, d;
268 const char *str;
269 char *cp;
270 char buf[2];
271
272 /*
273 * First calculate number width and decimal place (last + 1 for
274 * non-decimal numbers). If the stored decimal is subsequent to
275 * ours, make our size longer by that difference
276 * (right-"shifting"); similarly, if ours is subsequent the
277 * stored, then extend the stored size by the difference.
278 * Finally, re-assign the stored values.
279 */
280
281 str = dp->string ? dp->string : "";
282 sz = (*tbl->slen)(str, tbl->arg);
283
284 /* FIXME: TBL_DATA_HORIZ et al.? */
285
286 buf[0] = opts->decimal;
287 buf[1] = '\0';
288
289 psz = (*tbl->slen)(buf, tbl->arg);
290
291 if (NULL != (cp = strrchr(str, opts->decimal))) {
292 buf[1] = '\0';
293 for (ssz = 0, i = 0; cp != &str[i]; i++) {
294 buf[0] = str[i];
295 ssz += (*tbl->slen)(buf, tbl->arg);
296 }
297 d = ssz + psz;
298 } else
299 d = sz + psz;
300
301 /* Adjust the settings for this column. */
302
303 if (col->decimal > d) {
304 sz += col->decimal - d;
305 d = col->decimal;
306 } else
307 col->width += d - col->decimal;
308
309 if (sz > col->width)
310 col->width = sz;
311 if (d > col->decimal)
312 col->decimal = d;
313 }