[BACK]Return to out.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/out.c, Revision 1.29

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