[BACK]Return to out.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/out.c, Revision 1.52

1.52    ! schwarze    1: /*     $Id: out.c,v 1.51 2014/08/12 19:28:16 schwarze Exp $ */
1.1       kristaps    2: /*
1.36      schwarze    3:  * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.52    ! schwarze    4:  * Copyright (c) 2011, 2014 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    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:  */
1.12      kristaps   18: #include "config.h"
                     19:
1.1       kristaps   20: #include <sys/types.h>
                     21:
1.6       kristaps   22: #include <assert.h>
1.1       kristaps   23: #include <ctype.h>
1.3       kristaps   24: #include <stdio.h>
1.1       kristaps   25: #include <stdlib.h>
1.6       kristaps   26: #include <string.h>
1.7       kristaps   27: #include <time.h>
1.1       kristaps   28:
1.47      schwarze   29: #include "mandoc_aux.h"
1.30      kristaps   30: #include "mandoc.h"
1.1       kristaps   31: #include "out.h"
                     32:
1.30      kristaps   33: static void    tblcalc_data(struct rofftbl *, struct roffcol *,
1.45      schwarze   34:                        const struct tbl_opts *, const struct tbl_dat *);
1.30      kristaps   35: static void    tblcalc_literal(struct rofftbl *, struct roffcol *,
                     36:                        const struct tbl_dat *);
                     37: static void    tblcalc_number(struct rofftbl *, struct roffcol *,
1.45      schwarze   38:                        const struct tbl_opts *, const struct tbl_dat *);
1.30      kristaps   39:
1.48      schwarze   40:
                     41: /*
1.3       kristaps   42:  * Convert a `scaling unit' to a consistent form, or fail.  Scaling
1.5       kristaps   43:  * units are documented in groff.7, mdoc.7, man.7.
1.3       kristaps   44:  */
1.1       kristaps   45: int
1.5       kristaps   46: a2roffsu(const char *src, struct roffsu *dst, enum roffscale def)
1.1       kristaps   47: {
1.4       kristaps   48:        char             buf[BUFSIZ], hasd;
1.1       kristaps   49:        int              i;
1.3       kristaps   50:        enum roffscale   unit;
1.1       kristaps   51:
1.5       kristaps   52:        if ('\0' == *src)
                     53:                return(0);
                     54:
1.4       kristaps   55:        i = hasd = 0;
                     56:
                     57:        switch (*src) {
1.48      schwarze   58:        case '+':
1.4       kristaps   59:                src++;
                     60:                break;
1.48      schwarze   61:        case '-':
1.4       kristaps   62:                buf[i++] = *src++;
                     63:                break;
                     64:        default:
                     65:                break;
                     66:        }
                     67:
1.5       kristaps   68:        if ('\0' == *src)
                     69:                return(0);
                     70:
1.4       kristaps   71:        while (i < BUFSIZ) {
1.42      kristaps   72:                if ( ! isdigit((unsigned char)*src)) {
1.4       kristaps   73:                        if ('.' != *src)
                     74:                                break;
                     75:                        else if (hasd)
                     76:                                break;
                     77:                        else
                     78:                                hasd = 1;
                     79:                }
                     80:                buf[i++] = *src++;
                     81:        }
1.1       kristaps   82:
1.3       kristaps   83:        if (BUFSIZ == i || (*src && *(src + 1)))
1.1       kristaps   84:                return(0);
                     85:
1.4       kristaps   86:        buf[i] = '\0';
1.1       kristaps   87:
1.3       kristaps   88:        switch (*src) {
1.48      schwarze   89:        case 'c':
1.3       kristaps   90:                unit = SCALE_CM;
                     91:                break;
1.48      schwarze   92:        case 'i':
1.3       kristaps   93:                unit = SCALE_IN;
                     94:                break;
1.48      schwarze   95:        case 'P':
1.3       kristaps   96:                unit = SCALE_PC;
                     97:                break;
1.48      schwarze   98:        case 'p':
1.3       kristaps   99:                unit = SCALE_PT;
                    100:                break;
1.48      schwarze  101:        case 'f':
1.3       kristaps  102:                unit = SCALE_FS;
                    103:                break;
1.48      schwarze  104:        case 'v':
1.3       kristaps  105:                unit = SCALE_VS;
                    106:                break;
1.48      schwarze  107:        case 'm':
1.3       kristaps  108:                unit = SCALE_EM;
                    109:                break;
1.48      schwarze  110:        case '\0':
1.5       kristaps  111:                if (SCALE_MAX == def)
                    112:                        return(0);
1.51      schwarze  113:                unit = SCALE_EN;
1.5       kristaps  114:                break;
1.48      schwarze  115:        case 'u':
1.3       kristaps  116:                unit = SCALE_BU;
                    117:                break;
1.48      schwarze  118:        case 'M':
1.3       kristaps  119:                unit = SCALE_MM;
                    120:                break;
1.48      schwarze  121:        case 'n':
1.3       kristaps  122:                unit = SCALE_EN;
                    123:                break;
                    124:        default:
1.1       kristaps  125:                return(0);
1.3       kristaps  126:        }
1.1       kristaps  127:
1.23      kristaps  128:        /* FIXME: do this in the caller. */
1.49      schwarze  129:        if ((dst->scale = atof(buf)) < 0.0)
                    130:                dst->scale = 0.0;
1.3       kristaps  131:        dst->unit = unit;
                    132:        return(1);
1.8       kristaps  133: }
1.30      kristaps  134:
                    135: /*
                    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.
                    140:  */
                    141: void
1.52    ! schwarze  142: tblcalc(struct rofftbl *tbl, const struct tbl_span *sp,
        !           143:        size_t totalwidth)
1.30      kristaps  144: {
                    145:        const struct tbl_dat    *dp;
                    146:        struct roffcol          *col;
1.52    ! schwarze  147:        size_t                   ewidth, xwidth;
1.43      schwarze  148:        int                      spans;
1.52    ! schwarze  149:        int                      icol, maxcol, necol, nxcol;
1.30      kristaps  150:
                    151:        /*
                    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.
                    155:         */
                    156:
                    157:        assert(NULL == tbl->cols);
1.48      schwarze  158:        tbl->cols = mandoc_calloc((size_t)sp->opts->cols,
                    159:            sizeof(struct roffcol));
1.30      kristaps  160:
1.52    ! schwarze  161:        for (maxcol = 0; sp; sp = sp->next) {
1.30      kristaps  162:                if (TBL_SPAN_DATA != sp->pos)
                    163:                        continue;
1.43      schwarze  164:                spans = 1;
1.30      kristaps  165:                /*
                    166:                 * Account for the data cells in the layout, matching it
                    167:                 * to data cells in the data section.
                    168:                 */
                    169:                for (dp = sp->first; dp; dp = dp->next) {
1.43      schwarze  170:                        /* Do not used spanned cells in the calculation. */
                    171:                        if (0 < --spans)
                    172:                                continue;
                    173:                        spans = dp->spans;
                    174:                        if (1 < spans)
                    175:                                continue;
1.52    ! schwarze  176:                        icol = dp->layout->head->ident;
        !           177:                        if (maxcol < icol)
        !           178:                                maxcol = icol;
        !           179:                        col = tbl->cols + icol;
        !           180:                        col->flags |= dp->layout->flags;
        !           181:                        if (dp->layout->flags & TBL_CELL_WIGN)
        !           182:                                continue;
1.45      schwarze  183:                        tblcalc_data(tbl, col, sp->opts, dp);
1.52    ! schwarze  184:                }
        !           185:        }
        !           186:
        !           187:        /*
        !           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.
        !           191:         */
        !           192:
        !           193:        necol = nxcol = 0;
        !           194:        ewidth = xwidth = 0;
        !           195:        for (icol = 0; icol <= maxcol; icol++) {
        !           196:                col = tbl->cols + icol;
        !           197:                if (col->flags & TBL_CELL_EQUAL) {
        !           198:                        necol++;
        !           199:                        if (ewidth < col->width)
        !           200:                                ewidth = col->width;
        !           201:                }
        !           202:                if (col->flags & TBL_CELL_WMAX)
        !           203:                        nxcol++;
        !           204:                else
        !           205:                        xwidth += col->width;
        !           206:        }
        !           207:
        !           208:        /*
        !           209:         * Equalize columns, if requested for any of them.
        !           210:         * Update total width of the columns not to maximize.
        !           211:         */
        !           212:
        !           213:        if (necol) {
        !           214:                for (icol = 0; icol <= maxcol; icol++) {
        !           215:                        col = tbl->cols + icol;
        !           216:                        if ( ! (col->flags & TBL_CELL_EQUAL))
        !           217:                                continue;
        !           218:                        if (col->width == ewidth)
        !           219:                                continue;
        !           220:                        if (nxcol && totalwidth)
        !           221:                                xwidth += ewidth - col->width;
        !           222:                        col->width = ewidth;
        !           223:                }
        !           224:        }
        !           225:
        !           226:        /*
        !           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.
        !           230:         */
        !           231:
        !           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))
        !           237:                                continue;
        !           238:                        col->width = xwidth / nxcol--;
        !           239:                        xwidth -= col->width;
1.30      kristaps  240:                }
                    241:        }
                    242: }
                    243:
                    244: static void
                    245: tblcalc_data(struct rofftbl *tbl, struct roffcol *col,
1.45      schwarze  246:                const struct tbl_opts *opts, const struct tbl_dat *dp)
1.30      kristaps  247: {
                    248:        size_t           sz;
                    249:
                    250:        /* Branch down into data sub-types. */
                    251:
                    252:        switch (dp->layout->pos) {
1.48      schwarze  253:        case TBL_CELL_HORIZ:
1.30      kristaps  254:                /* FALLTHROUGH */
1.48      schwarze  255:        case TBL_CELL_DHORIZ:
1.30      kristaps  256:                sz = (*tbl->len)(1, tbl->arg);
                    257:                if (col->width < sz)
                    258:                        col->width = sz;
                    259:                break;
1.48      schwarze  260:        case TBL_CELL_LONG:
1.30      kristaps  261:                /* FALLTHROUGH */
1.48      schwarze  262:        case TBL_CELL_CENTRE:
1.30      kristaps  263:                /* FALLTHROUGH */
1.48      schwarze  264:        case TBL_CELL_LEFT:
1.30      kristaps  265:                /* FALLTHROUGH */
1.48      schwarze  266:        case TBL_CELL_RIGHT:
1.30      kristaps  267:                tblcalc_literal(tbl, col, dp);
                    268:                break;
1.48      schwarze  269:        case TBL_CELL_NUMBER:
1.45      schwarze  270:                tblcalc_number(tbl, col, opts, dp);
1.35      kristaps  271:                break;
1.48      schwarze  272:        case TBL_CELL_DOWN:
1.30      kristaps  273:                break;
                    274:        default:
                    275:                abort();
                    276:                /* NOTREACHED */
                    277:        }
                    278: }
                    279:
                    280: static void
                    281: tblcalc_literal(struct rofftbl *tbl, struct roffcol *col,
                    282:                const struct tbl_dat *dp)
                    283: {
1.43      schwarze  284:        size_t           sz;
1.34      kristaps  285:        const char      *str;
1.30      kristaps  286:
1.34      kristaps  287:        str = dp->string ? dp->string : "";
                    288:        sz = (*tbl->slen)(str, tbl->arg);
                    289:
1.30      kristaps  290:        if (col->width < sz)
                    291:                col->width = sz;
                    292: }
                    293:
                    294: static void
                    295: tblcalc_number(struct rofftbl *tbl, struct roffcol *col,
1.45      schwarze  296:                const struct tbl_opts *opts, const struct tbl_dat *dp)
1.30      kristaps  297: {
1.48      schwarze  298:        int              i;
1.34      kristaps  299:        size_t           sz, psz, ssz, d;
                    300:        const char      *str;
1.30      kristaps  301:        char            *cp;
                    302:        char             buf[2];
                    303:
                    304:        /*
                    305:         * First calculate number width and decimal place (last + 1 for
1.43      schwarze  306:         * non-decimal numbers).  If the stored decimal is subsequent to
1.30      kristaps  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.
                    311:         */
                    312:
1.34      kristaps  313:        str = dp->string ? dp->string : "";
                    314:        sz = (*tbl->slen)(str, tbl->arg);
1.30      kristaps  315:
1.34      kristaps  316:        /* FIXME: TBL_DATA_HORIZ et al.? */
1.30      kristaps  317:
1.45      schwarze  318:        buf[0] = opts->decimal;
1.30      kristaps  319:        buf[1] = '\0';
                    320:
                    321:        psz = (*tbl->slen)(buf, tbl->arg);
                    322:
1.45      schwarze  323:        if (NULL != (cp = strrchr(str, opts->decimal))) {
1.30      kristaps  324:                buf[1] = '\0';
                    325:                for (ssz = 0, i = 0; cp != &str[i]; i++) {
                    326:                        buf[0] = str[i];
                    327:                        ssz += (*tbl->slen)(buf, tbl->arg);
                    328:                }
                    329:                d = ssz + psz;
                    330:        } else
                    331:                d = sz + psz;
                    332:
                    333:        /* Adjust the settings for this column. */
                    334:
                    335:        if (col->decimal > d) {
                    336:                sz += col->decimal - d;
                    337:                d = col->decimal;
                    338:        } else
                    339:                col->width += d - col->decimal;
                    340:
                    341:        if (sz > col->width)
                    342:                col->width = sz;
                    343:        if (d > col->decimal)
                    344:                col->decimal = d;
                    345: }

CVSweb