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

Annotation of mandoc/out.c, Revision 1.41

1.41    ! kristaps    1: /*     $Id: out.c,v 1.40 2011/04/09 15:29:40 kristaps Exp $ */
1.1       kristaps    2: /*
1.36      schwarze    3:  * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.37      schwarze    4:  * Copyright (c) 2011 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: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
                     21:
1.1       kristaps   22: #include <sys/types.h>
                     23:
1.6       kristaps   24: #include <assert.h>
1.1       kristaps   25: #include <ctype.h>
1.3       kristaps   26: #include <stdio.h>
1.1       kristaps   27: #include <stdlib.h>
1.6       kristaps   28: #include <string.h>
1.7       kristaps   29: #include <time.h>
1.1       kristaps   30:
1.30      kristaps   31: #include "mandoc.h"
1.1       kristaps   32: #include "out.h"
                     33:
1.30      kristaps   34: static void    tblcalc_data(struct rofftbl *, struct roffcol *,
                     35:                        const struct tbl *, const struct tbl_dat *);
                     36: static void    tblcalc_literal(struct rofftbl *, struct roffcol *,
                     37:                        const struct tbl_dat *);
                     38: static void    tblcalc_number(struct rofftbl *, struct roffcol *,
                     39:                        const struct tbl *, const struct tbl_dat *);
                     40:
1.3       kristaps   41: /*
                     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) {
                     58:        case ('+'):
                     59:                src++;
                     60:                break;
                     61:        case ('-'):
                     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) {
                     72:                if ( ! isdigit((u_char)*src)) {
                     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) {
                     89:        case ('c'):
                     90:                unit = SCALE_CM;
                     91:                break;
                     92:        case ('i'):
                     93:                unit = SCALE_IN;
                     94:                break;
                     95:        case ('P'):
                     96:                unit = SCALE_PC;
                     97:                break;
                     98:        case ('p'):
                     99:                unit = SCALE_PT;
                    100:                break;
                    101:        case ('f'):
                    102:                unit = SCALE_FS;
                    103:                break;
                    104:        case ('v'):
                    105:                unit = SCALE_VS;
                    106:                break;
                    107:        case ('m'):
                    108:                unit = SCALE_EM;
                    109:                break;
                    110:        case ('\0'):
1.5       kristaps  111:                if (SCALE_MAX == def)
                    112:                        return(0);
                    113:                unit = SCALE_BU;
                    114:                break;
1.3       kristaps  115:        case ('u'):
                    116:                unit = SCALE_BU;
                    117:                break;
                    118:        case ('M'):
                    119:                unit = SCALE_MM;
                    120:                break;
                    121:        case ('n'):
                    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.4       kristaps  129:        if ((dst->scale = atof(buf)) < 0)
1.3       kristaps  130:                dst->scale = 0;
                    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
                    142: tblcalc(struct rofftbl *tbl, const struct tbl_span *sp)
                    143: {
                    144:        const struct tbl_dat    *dp;
                    145:        const struct tbl_head   *hp;
                    146:        struct roffcol          *col;
                    147:
                    148:        /*
                    149:         * Allocate the master column specifiers.  These will hold the
                    150:         * widths and decimal positions for all cells in the column.  It
                    151:         * must be freed and nullified by the caller.
                    152:         */
                    153:
                    154:        assert(NULL == tbl->cols);
1.39      kristaps  155:        tbl->cols = mandoc_calloc
1.38      kristaps  156:                ((size_t)sp->tbl->cols, sizeof(struct roffcol));
1.30      kristaps  157:
                    158:        hp = sp->head;
                    159:
                    160:        for ( ; sp; sp = sp->next) {
                    161:                if (TBL_SPAN_DATA != sp->pos)
                    162:                        continue;
                    163:                /*
                    164:                 * Account for the data cells in the layout, matching it
                    165:                 * to data cells in the data section.
                    166:                 */
                    167:                for (dp = sp->first; dp; dp = dp->next) {
1.33      kristaps  168:                        assert(dp->layout);
1.30      kristaps  169:                        col = &tbl->cols[dp->layout->head->ident];
                    170:                        tblcalc_data(tbl, col, sp->tbl, dp);
                    171:                }
                    172:        }
                    173:
                    174:        /*
                    175:         * Calculate width of the spanners.  These get one space for a
                    176:         * vertical line, two for a double-vertical line.
                    177:         */
                    178:
                    179:        for ( ; hp; hp = hp->next) {
                    180:                col = &tbl->cols[hp->ident];
                    181:                switch (hp->pos) {
                    182:                case (TBL_HEAD_VERT):
                    183:                        col->width = (*tbl->len)(1, tbl->arg);
                    184:                        break;
                    185:                case (TBL_HEAD_DVERT):
                    186:                        col->width = (*tbl->len)(2, tbl->arg);
                    187:                        break;
                    188:                default:
                    189:                        break;
                    190:                }
                    191:        }
                    192: }
                    193:
                    194: static void
                    195: tblcalc_data(struct rofftbl *tbl, struct roffcol *col,
                    196:                const struct tbl *tp, const struct tbl_dat *dp)
                    197: {
                    198:        size_t           sz;
                    199:
                    200:        /* Branch down into data sub-types. */
                    201:
                    202:        switch (dp->layout->pos) {
                    203:        case (TBL_CELL_HORIZ):
                    204:                /* FALLTHROUGH */
                    205:        case (TBL_CELL_DHORIZ):
                    206:                sz = (*tbl->len)(1, tbl->arg);
                    207:                if (col->width < sz)
                    208:                        col->width = sz;
                    209:                break;
                    210:        case (TBL_CELL_LONG):
                    211:                /* FALLTHROUGH */
                    212:        case (TBL_CELL_CENTRE):
                    213:                /* FALLTHROUGH */
                    214:        case (TBL_CELL_LEFT):
                    215:                /* FALLTHROUGH */
                    216:        case (TBL_CELL_RIGHT):
                    217:                tblcalc_literal(tbl, col, dp);
                    218:                break;
                    219:        case (TBL_CELL_NUMBER):
                    220:                tblcalc_number(tbl, col, tp, dp);
1.35      kristaps  221:                break;
                    222:        case (TBL_CELL_DOWN):
1.30      kristaps  223:                break;
                    224:        default:
                    225:                abort();
                    226:                /* NOTREACHED */
                    227:        }
                    228: }
                    229:
                    230: static void
                    231: tblcalc_literal(struct rofftbl *tbl, struct roffcol *col,
                    232:                const struct tbl_dat *dp)
                    233: {
                    234:        size_t           sz, bufsz, spsz;
1.34      kristaps  235:        const char      *str;
1.30      kristaps  236:
                    237:        /*
                    238:         * Calculate our width and use the spacing, with a minimum
                    239:         * spacing dictated by position (centre, e.g,. gets a space on
                    240:         * either side, while right/left get a single adjacent space).
                    241:         */
                    242:
1.34      kristaps  243:        bufsz = spsz = 0;
                    244:        str = dp->string ? dp->string : "";
                    245:        sz = (*tbl->slen)(str, tbl->arg);
                    246:
                    247:        /* FIXME: TBL_DATA_HORIZ et al.? */
1.30      kristaps  248:
                    249:        assert(dp->layout);
                    250:        switch (dp->layout->pos) {
                    251:        case (TBL_CELL_LONG):
                    252:                /* FALLTHROUGH */
                    253:        case (TBL_CELL_CENTRE):
1.36      schwarze  254:                bufsz = (*tbl->len)(1, tbl->arg);
1.30      kristaps  255:                break;
                    256:        default:
                    257:                bufsz = (*tbl->len)(1, tbl->arg);
                    258:                break;
                    259:        }
                    260:
                    261:        if (dp->layout->spacing) {
                    262:                spsz = (*tbl->len)(dp->layout->spacing, tbl->arg);
                    263:                bufsz = bufsz > spsz ? bufsz : spsz;
                    264:        }
                    265:
                    266:        sz += bufsz;
                    267:        if (col->width < sz)
                    268:                col->width = sz;
                    269: }
                    270:
                    271: static void
                    272: tblcalc_number(struct rofftbl *tbl, struct roffcol *col,
                    273:                const struct tbl *tp, const struct tbl_dat *dp)
                    274: {
                    275:        int              i;
1.34      kristaps  276:        size_t           sz, psz, ssz, d;
                    277:        const char      *str;
1.30      kristaps  278:        char            *cp;
                    279:        char             buf[2];
                    280:
                    281:        /*
                    282:         * First calculate number width and decimal place (last + 1 for
                    283:         * no-decimal numbers).  If the stored decimal is subsequent
                    284:         * ours, make our size longer by that difference
                    285:         * (right-"shifting"); similarly, if ours is subsequent the
                    286:         * stored, then extend the stored size by the difference.
                    287:         * Finally, re-assign the stored values.
                    288:         */
                    289:
1.34      kristaps  290:        str = dp->string ? dp->string : "";
                    291:        sz = (*tbl->slen)(str, tbl->arg);
1.30      kristaps  292:
1.34      kristaps  293:        /* FIXME: TBL_DATA_HORIZ et al.? */
1.30      kristaps  294:
                    295:        buf[0] = tp->decimal;
                    296:        buf[1] = '\0';
                    297:
                    298:        psz = (*tbl->slen)(buf, tbl->arg);
                    299:
1.32      kristaps  300:        if (NULL != (cp = strrchr(str, tp->decimal))) {
1.30      kristaps  301:                buf[1] = '\0';
                    302:                for (ssz = 0, i = 0; cp != &str[i]; i++) {
                    303:                        buf[0] = str[i];
                    304:                        ssz += (*tbl->slen)(buf, tbl->arg);
                    305:                }
                    306:                d = ssz + psz;
                    307:        } else
                    308:                d = sz + psz;
                    309:
                    310:        /* Padding. */
                    311:
                    312:        sz += (*tbl->len)(2, tbl->arg);
                    313:        d += (*tbl->len)(1, tbl->arg);
                    314:
                    315:        /* Adjust the settings for this column. */
                    316:
                    317:        if (col->decimal > d) {
                    318:                sz += col->decimal - d;
                    319:                d = col->decimal;
                    320:        } else
                    321:                col->width += d - col->decimal;
                    322:
                    323:        if (sz > col->width)
                    324:                col->width = sz;
                    325:        if (d > col->decimal)
                    326:                col->decimal = d;
1.31      kristaps  327:
                    328:        /* Adjust for stipulated width. */
                    329:
1.34      kristaps  330:        if (col->width < dp->layout->spacing)
                    331:                col->width = dp->layout->spacing;
1.30      kristaps  332: }
                    333:
                    334:

CVSweb