1 /* $Id: out.c,v 1.40 2011/04/09 15:29:40 kristaps Exp $ */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
22 #include <sys/types.h>
34 static void tblcalc_data(struct rofftbl *, struct roffcol *,
35 const struct tbl *, const struct tbl_dat *);
36 static void tblcalc_literal(struct rofftbl *, struct roffcol *,
37 const struct tbl_dat *);
38 static void tblcalc_number(struct rofftbl *, struct roffcol *,
39 const struct tbl *, const struct tbl_dat *);
42 * Convert a `scaling unit' to a consistent form, or fail. Scaling
43 * units are documented in groff.7, mdoc.7, man.7.
46 a2roffsu(const char *src, struct roffsu *dst, enum roffscale def)
48 char buf[BUFSIZ], hasd;
72 if ( ! isdigit((u_char)*src)) {
83 if (BUFSIZ == i || (*src && *(src + 1)))
111 if (SCALE_MAX == def)
128 /* FIXME: do this in the caller. */
129 if ((dst->scale = atof(buf)) < 0)
137 * Correctly writes the time in nroff form, which differs from standard
138 * form in that a space isn't printed in lieu of the extra %e field for
139 * single-digit dates.
142 time2a(time_t t, char *dst, size_t sz)
150 localtime_r(&t, &tm);
157 if (0 == (nsz = strftime(p, sz, "%B ", &tm)))
163 if (0 == strftime(buf, sizeof(buf), "%e, ", &tm))
166 nsz = strlcat(p, buf + (' ' == buf[0] ? 1 : 0), sz);
174 (void)strftime(p, sz, "%Y", &tm);
178 * Calculate the abstract widths and decimal positions of columns in a
179 * table. This routine allocates the columns structures then runs over
180 * all rows and cells in the table. The function pointers in "tbl" are
181 * used for the actual width calculations.
184 tblcalc(struct rofftbl *tbl, const struct tbl_span *sp)
186 const struct tbl_dat *dp;
187 const struct tbl_head *hp;
191 * Allocate the master column specifiers. These will hold the
192 * widths and decimal positions for all cells in the column. It
193 * must be freed and nullified by the caller.
196 assert(NULL == tbl->cols);
197 tbl->cols = mandoc_calloc
198 ((size_t)sp->tbl->cols, sizeof(struct roffcol));
202 for ( ; sp; sp = sp->next) {
203 if (TBL_SPAN_DATA != sp->pos)
206 * Account for the data cells in the layout, matching it
207 * to data cells in the data section.
209 for (dp = sp->first; dp; dp = dp->next) {
211 col = &tbl->cols[dp->layout->head->ident];
212 tblcalc_data(tbl, col, sp->tbl, dp);
217 * Calculate width of the spanners. These get one space for a
218 * vertical line, two for a double-vertical line.
221 for ( ; hp; hp = hp->next) {
222 col = &tbl->cols[hp->ident];
224 case (TBL_HEAD_VERT):
225 col->width = (*tbl->len)(1, tbl->arg);
227 case (TBL_HEAD_DVERT):
228 col->width = (*tbl->len)(2, tbl->arg);
237 tblcalc_data(struct rofftbl *tbl, struct roffcol *col,
238 const struct tbl *tp, const struct tbl_dat *dp)
242 /* Branch down into data sub-types. */
244 switch (dp->layout->pos) {
245 case (TBL_CELL_HORIZ):
247 case (TBL_CELL_DHORIZ):
248 sz = (*tbl->len)(1, tbl->arg);
252 case (TBL_CELL_LONG):
254 case (TBL_CELL_CENTRE):
256 case (TBL_CELL_LEFT):
258 case (TBL_CELL_RIGHT):
259 tblcalc_literal(tbl, col, dp);
261 case (TBL_CELL_NUMBER):
262 tblcalc_number(tbl, col, tp, dp);
264 case (TBL_CELL_DOWN):
273 tblcalc_literal(struct rofftbl *tbl, struct roffcol *col,
274 const struct tbl_dat *dp)
276 size_t sz, bufsz, spsz;
280 * Calculate our width and use the spacing, with a minimum
281 * spacing dictated by position (centre, e.g,. gets a space on
282 * either side, while right/left get a single adjacent space).
286 str = dp->string ? dp->string : "";
287 sz = (*tbl->slen)(str, tbl->arg);
289 /* FIXME: TBL_DATA_HORIZ et al.? */
292 switch (dp->layout->pos) {
293 case (TBL_CELL_LONG):
295 case (TBL_CELL_CENTRE):
296 bufsz = (*tbl->len)(1, tbl->arg);
299 bufsz = (*tbl->len)(1, tbl->arg);
303 if (dp->layout->spacing) {
304 spsz = (*tbl->len)(dp->layout->spacing, tbl->arg);
305 bufsz = bufsz > spsz ? bufsz : spsz;
314 tblcalc_number(struct rofftbl *tbl, struct roffcol *col,
315 const struct tbl *tp, const struct tbl_dat *dp)
318 size_t sz, psz, ssz, d;
324 * First calculate number width and decimal place (last + 1 for
325 * no-decimal numbers). If the stored decimal is subsequent
326 * ours, make our size longer by that difference
327 * (right-"shifting"); similarly, if ours is subsequent the
328 * stored, then extend the stored size by the difference.
329 * Finally, re-assign the stored values.
332 str = dp->string ? dp->string : "";
333 sz = (*tbl->slen)(str, tbl->arg);
335 /* FIXME: TBL_DATA_HORIZ et al.? */
337 buf[0] = tp->decimal;
340 psz = (*tbl->slen)(buf, tbl->arg);
342 if (NULL != (cp = strrchr(str, tp->decimal))) {
344 for (ssz = 0, i = 0; cp != &str[i]; i++) {
346 ssz += (*tbl->slen)(buf, tbl->arg);
354 sz += (*tbl->len)(2, tbl->arg);
355 d += (*tbl->len)(1, tbl->arg);
357 /* Adjust the settings for this column. */
359 if (col->decimal > d) {
360 sz += col->decimal - d;
363 col->width += d - col->decimal;
367 if (d > col->decimal)
370 /* Adjust for stipulated width. */
372 if (col->width < dp->layout->spacing)
373 col->width = dp->layout->spacing;