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

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