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

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