1 /* $Id: out.c,v 1.54 2014/12/04 02:05:42 schwarze Exp $ */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2014 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.
20 #include <sys/types.h>
29 #include "mandoc_aux.h"
33 static void tblcalc_data(struct rofftbl *, struct roffcol *,
34 const struct tbl_opts *, const struct tbl_dat *);
35 static void tblcalc_literal(struct rofftbl *, struct roffcol *,
36 const struct tbl_dat *);
37 static void tblcalc_number(struct rofftbl *, struct roffcol *,
38 const struct tbl_opts *, 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((unsigned 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.0)
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.
142 tblcalc(struct rofftbl *tbl, const struct tbl_span *sp,
145 const struct tbl_dat *dp;
147 size_t ewidth, xwidth;
149 int icol, maxcol, necol, nxcol;
152 * Allocate the master column specifiers. These will hold the
153 * widths and decimal positions for all cells in the column. It
154 * must be freed and nullified by the caller.
157 assert(NULL == tbl->cols);
158 tbl->cols = mandoc_calloc((size_t)sp->opts->cols,
159 sizeof(struct roffcol));
161 for (maxcol = -1; sp; sp = sp->next) {
162 if (TBL_SPAN_DATA != sp->pos)
166 * Account for the data cells in the layout, matching it
167 * to data cells in the data section.
169 for (dp = sp->first; dp; dp = dp->next) {
170 /* Do not used spanned cells in the calculation. */
176 icol = dp->layout->head->ident;
179 col = tbl->cols + icol;
180 col->flags |= dp->layout->flags;
181 if (dp->layout->flags & TBL_CELL_WIGN)
183 tblcalc_data(tbl, col, sp->opts, dp);
188 * Count columns to equalize and columns to maximize.
189 * Find maximum width of the columns to equalize.
190 * Find total width of the columns *not* to maximize.
195 for (icol = 0; icol <= maxcol; icol++) {
196 col = tbl->cols + icol;
197 if (col->flags & TBL_CELL_EQUAL) {
199 if (ewidth < col->width)
202 if (col->flags & TBL_CELL_WMAX)
205 xwidth += col->width;
209 * Equalize columns, if requested for any of them.
210 * Update total width of the columns not to maximize.
214 for (icol = 0; icol <= maxcol; icol++) {
215 col = tbl->cols + icol;
216 if ( ! (col->flags & TBL_CELL_EQUAL))
218 if (col->width == ewidth)
220 if (nxcol && totalwidth)
221 xwidth += ewidth - col->width;
227 * If there are any columns to maximize, find the total
228 * available width, deducting 3n margins between columns.
229 * Distribute the available width evenly.
232 if (nxcol && totalwidth) {
233 xwidth = totalwidth - 3*maxcol - xwidth;
234 for (icol = 0; icol <= maxcol; icol++) {
235 col = tbl->cols + icol;
236 if ( ! (col->flags & TBL_CELL_WMAX))
238 col->width = xwidth / nxcol--;
239 xwidth -= col->width;
245 tblcalc_data(struct rofftbl *tbl, struct roffcol *col,
246 const struct tbl_opts *opts, const struct tbl_dat *dp)
250 /* Branch down into data sub-types. */
252 switch (dp->layout->pos) {
255 case TBL_CELL_DHORIZ:
256 sz = (*tbl->len)(1, tbl->arg);
262 case TBL_CELL_CENTRE:
267 tblcalc_literal(tbl, col, dp);
269 case TBL_CELL_NUMBER:
270 tblcalc_number(tbl, col, opts, dp);
281 tblcalc_literal(struct rofftbl *tbl, struct roffcol *col,
282 const struct tbl_dat *dp)
287 str = dp->string ? dp->string : "";
288 sz = (*tbl->slen)(str, tbl->arg);
295 tblcalc_number(struct rofftbl *tbl, struct roffcol *col,
296 const struct tbl_opts *opts, const struct tbl_dat *dp)
299 size_t sz, psz, ssz, d;
305 * First calculate number width and decimal place (last + 1 for
306 * non-decimal numbers). If the stored decimal is subsequent to
307 * ours, make our size longer by that difference
308 * (right-"shifting"); similarly, if ours is subsequent the
309 * stored, then extend the stored size by the difference.
310 * Finally, re-assign the stored values.
313 str = dp->string ? dp->string : "";
314 sz = (*tbl->slen)(str, tbl->arg);
316 /* FIXME: TBL_DATA_HORIZ et al.? */
318 buf[0] = opts->decimal;
321 psz = (*tbl->slen)(buf, tbl->arg);
323 if (NULL != (cp = strrchr(str, opts->decimal))) {
325 for (ssz = 0, i = 0; cp != &str[i]; i++) {
327 ssz += (*tbl->slen)(buf, tbl->arg);
333 /* Adjust the settings for this column. */
335 if (col->decimal > d) {
336 sz += col->decimal - d;
339 col->width += d - col->decimal;
343 if (d > col->decimal)