[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.36

1.36    ! schwarze    1: /*     $OpenBSD: out.c,v 1.35 2017/05/01 20:53:58 schwarze Exp $ */
1.1       schwarze    2: /*
1.9       schwarze    3:  * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.35      schwarze    4:  * Copyright (c) 2011, 2014, 2015, 2017 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.36    ! schwarze   41:  * Return a pointer to the byte after the last byte used,
        !            42:  * or NULL on total failure.
1.1       schwarze   43:  */
1.36    ! schwarze   44: const char *
1.1       schwarze   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.36    ! schwarze   52:                return NULL;
1.1       schwarze   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)
1.36    ! schwarze   90:                        return NULL;
1.28      schwarze   91:                dst->unit = def;
1.1       schwarze   92:                break;
                     93:        }
1.36    ! schwarze   94:        return endptr;
1.3       schwarze   95: }
1.9       schwarze   96:
                     97: /*
                     98:  * Calculate the abstract widths and decimal positions of columns in a
                     99:  * table.  This routine allocates the columns structures then runs over
                    100:  * all rows and cells in the table.  The function pointers in "tbl" are
                    101:  * used for the actual width calculations.
                    102:  */
                    103: void
1.24      schwarze  104: tblcalc(struct rofftbl *tbl, const struct tbl_span *sp,
                    105:        size_t totalwidth)
1.9       schwarze  106: {
1.30      schwarze  107:        const struct tbl_opts   *opts;
1.9       schwarze  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.30      schwarze  112:        int                      icol, maxcol, necol, nxcol, quirkcol;
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.30      schwarze  123:        opts = sp->opts;
1.9       schwarze  124:
1.25      schwarze  125:        for (maxcol = -1; sp; sp = sp->next) {
1.9       schwarze  126:                if (TBL_SPAN_DATA != sp->pos)
                    127:                        continue;
1.16      schwarze  128:                spans = 1;
1.9       schwarze  129:                /*
                    130:                 * Account for the data cells in the layout, matching it
                    131:                 * to data cells in the data section.
                    132:                 */
                    133:                for (dp = sp->first; dp; dp = dp->next) {
1.16      schwarze  134:                        /* Do not used spanned cells in the calculation. */
                    135:                        if (0 < --spans)
                    136:                                continue;
                    137:                        spans = dp->spans;
                    138:                        if (1 < spans)
                    139:                                continue;
1.31      schwarze  140:                        icol = dp->layout->col;
1.24      schwarze  141:                        if (maxcol < icol)
                    142:                                maxcol = icol;
                    143:                        col = tbl->cols + icol;
                    144:                        col->flags |= dp->layout->flags;
                    145:                        if (dp->layout->flags & TBL_CELL_WIGN)
                    146:                                continue;
1.30      schwarze  147:                        tblcalc_data(tbl, col, opts, dp);
1.24      schwarze  148:                }
                    149:        }
                    150:
                    151:        /*
                    152:         * Count columns to equalize and columns to maximize.
                    153:         * Find maximum width of the columns to equalize.
                    154:         * Find total width of the columns *not* to maximize.
                    155:         */
                    156:
                    157:        necol = nxcol = 0;
                    158:        ewidth = xwidth = 0;
                    159:        for (icol = 0; icol <= maxcol; icol++) {
                    160:                col = tbl->cols + icol;
                    161:                if (col->flags & TBL_CELL_EQUAL) {
                    162:                        necol++;
                    163:                        if (ewidth < col->width)
                    164:                                ewidth = col->width;
                    165:                }
                    166:                if (col->flags & TBL_CELL_WMAX)
                    167:                        nxcol++;
                    168:                else
                    169:                        xwidth += col->width;
                    170:        }
                    171:
                    172:        /*
                    173:         * Equalize columns, if requested for any of them.
                    174:         * Update total width of the columns not to maximize.
                    175:         */
                    176:
                    177:        if (necol) {
                    178:                for (icol = 0; icol <= maxcol; icol++) {
                    179:                        col = tbl->cols + icol;
                    180:                        if ( ! (col->flags & TBL_CELL_EQUAL))
                    181:                                continue;
                    182:                        if (col->width == ewidth)
                    183:                                continue;
                    184:                        if (nxcol && totalwidth)
                    185:                                xwidth += ewidth - col->width;
                    186:                        col->width = ewidth;
                    187:                }
                    188:        }
                    189:
                    190:        /*
                    191:         * If there are any columns to maximize, find the total
                    192:         * available width, deducting 3n margins between columns.
                    193:         * Distribute the available width evenly.
                    194:         */
                    195:
                    196:        if (nxcol && totalwidth) {
1.35      schwarze  197:                xwidth += 3*maxcol +
1.30      schwarze  198:                    (opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ?
                    199:                     2 : !!opts->lvert + !!opts->rvert);
1.35      schwarze  200:                if (xwidth >= totalwidth)
                    201:                        return;
                    202:                xwidth = totalwidth - xwidth;
1.30      schwarze  203:
                    204:                /*
                    205:                 * Emulate a bug in GNU tbl width calculation that
                    206:                 * manifests itself for large numbers of x-columns.
                    207:                 * Emulating it for 5 x-columns gives identical
                    208:                 * behaviour for up to 6 x-columns.
                    209:                 */
                    210:
                    211:                if (nxcol == 5) {
                    212:                        quirkcol = xwidth % nxcol + 2;
                    213:                        if (quirkcol != 3 && quirkcol != 4)
                    214:                                quirkcol = -1;
                    215:                } else
                    216:                        quirkcol = -1;
                    217:
                    218:                necol = 0;
                    219:                ewidth = 0;
1.24      schwarze  220:                for (icol = 0; icol <= maxcol; icol++) {
                    221:                        col = tbl->cols + icol;
                    222:                        if ( ! (col->flags & TBL_CELL_WMAX))
                    223:                                continue;
1.30      schwarze  224:                        col->width = (double)xwidth * ++necol / nxcol
                    225:                            - ewidth + 0.4995;
                    226:                        if (necol == quirkcol)
                    227:                                col->width--;
                    228:                        ewidth += col->width;
1.9       schwarze  229:                }
                    230:        }
                    231: }
                    232:
                    233: static void
                    234: tblcalc_data(struct rofftbl *tbl, struct roffcol *col,
1.18      schwarze  235:                const struct tbl_opts *opts, const struct tbl_dat *dp)
1.9       schwarze  236: {
                    237:        size_t           sz;
                    238:
                    239:        /* Branch down into data sub-types. */
                    240:
                    241:        switch (dp->layout->pos) {
1.21      schwarze  242:        case TBL_CELL_HORIZ:
                    243:        case TBL_CELL_DHORIZ:
1.9       schwarze  244:                sz = (*tbl->len)(1, tbl->arg);
                    245:                if (col->width < sz)
                    246:                        col->width = sz;
                    247:                break;
1.21      schwarze  248:        case TBL_CELL_LONG:
                    249:        case TBL_CELL_CENTRE:
                    250:        case TBL_CELL_LEFT:
                    251:        case TBL_CELL_RIGHT:
1.9       schwarze  252:                tblcalc_literal(tbl, col, dp);
                    253:                break;
1.21      schwarze  254:        case TBL_CELL_NUMBER:
1.18      schwarze  255:                tblcalc_number(tbl, col, opts, dp);
1.9       schwarze  256:                break;
1.21      schwarze  257:        case TBL_CELL_DOWN:
1.10      schwarze  258:                break;
1.9       schwarze  259:        default:
                    260:                abort();
                    261:        }
                    262: }
                    263:
                    264: static void
                    265: tblcalc_literal(struct rofftbl *tbl, struct roffcol *col,
                    266:                const struct tbl_dat *dp)
                    267: {
1.16      schwarze  268:        size_t           sz;
1.10      schwarze  269:        const char      *str;
1.9       schwarze  270:
1.10      schwarze  271:        str = dp->string ? dp->string : "";
                    272:        sz = (*tbl->slen)(str, tbl->arg);
                    273:
1.9       schwarze  274:        if (col->width < sz)
                    275:                col->width = sz;
                    276: }
                    277:
                    278: static void
                    279: tblcalc_number(struct rofftbl *tbl, struct roffcol *col,
1.18      schwarze  280:                const struct tbl_opts *opts, const struct tbl_dat *dp)
1.9       schwarze  281: {
1.21      schwarze  282:        int              i;
1.10      schwarze  283:        size_t           sz, psz, ssz, d;
                    284:        const char      *str;
1.9       schwarze  285:        char            *cp;
                    286:        char             buf[2];
                    287:
                    288:        /*
                    289:         * First calculate number width and decimal place (last + 1 for
1.16      schwarze  290:         * non-decimal numbers).  If the stored decimal is subsequent to
1.9       schwarze  291:         * ours, make our size longer by that difference
                    292:         * (right-"shifting"); similarly, if ours is subsequent the
                    293:         * stored, then extend the stored size by the difference.
                    294:         * Finally, re-assign the stored values.
                    295:         */
                    296:
1.10      schwarze  297:        str = dp->string ? dp->string : "";
                    298:        sz = (*tbl->slen)(str, tbl->arg);
1.9       schwarze  299:
1.10      schwarze  300:        /* FIXME: TBL_DATA_HORIZ et al.? */
1.9       schwarze  301:
1.18      schwarze  302:        buf[0] = opts->decimal;
1.9       schwarze  303:        buf[1] = '\0';
                    304:
                    305:        psz = (*tbl->slen)(buf, tbl->arg);
                    306:
1.18      schwarze  307:        if (NULL != (cp = strrchr(str, opts->decimal))) {
1.9       schwarze  308:                buf[1] = '\0';
                    309:                for (ssz = 0, i = 0; cp != &str[i]; i++) {
                    310:                        buf[0] = str[i];
                    311:                        ssz += (*tbl->slen)(buf, tbl->arg);
                    312:                }
                    313:                d = ssz + psz;
                    314:        } else
                    315:                d = sz + psz;
                    316:
                    317:        /* Adjust the settings for this column. */
                    318:
                    319:        if (col->decimal > d) {
                    320:                sz += col->decimal - d;
                    321:                d = col->decimal;
                    322:        } else
                    323:                col->width += d - col->decimal;
                    324:
                    325:        if (sz > col->width)
                    326:                col->width = sz;
                    327:        if (d > col->decimal)
                    328:                col->decimal = d;
                    329: }