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

Annotation of mandoc/out.c, Revision 1.71

1.71    ! schwarze    1: /*     $Id: out.c,v 1.70 2017/06/27 18:25:02 schwarze Exp $ */
1.1       kristaps    2: /*
1.36      schwarze    3:  * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.71    ! schwarze    4:  * Copyright (c) 2011,2014,2015,2017,2018 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.70      schwarze   23: #include <stdint.h>
1.1       kristaps   24: #include <stdlib.h>
1.6       kristaps   25: #include <string.h>
1.7       kristaps   26: #include <time.h>
1.1       kristaps   27:
1.47      schwarze   28: #include "mandoc_aux.h"
1.30      kristaps   29: #include "mandoc.h"
1.1       kristaps   30: #include "out.h"
                     31:
1.30      kristaps   32: static void    tblcalc_data(struct rofftbl *, struct roffcol *,
1.65      schwarze   33:                        const struct tbl_opts *, const struct tbl_dat *,
                     34:                        size_t);
1.30      kristaps   35: static void    tblcalc_literal(struct rofftbl *, struct roffcol *,
1.65      schwarze   36:                        const struct tbl_dat *, size_t);
1.30      kristaps   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.55      schwarze   42:  * Parse the *src string and store a scaling unit into *dst.
                     43:  * If the string doesn't specify the unit, use the default.
                     44:  * If no default is specified, fail.
1.64      schwarze   45:  * Return a pointer to the byte after the last byte used,
                     46:  * or NULL on total failure.
1.3       kristaps   47:  */
1.64      schwarze   48: const char *
1.5       kristaps   49: a2roffsu(const char *src, struct roffsu *dst, enum roffscale def)
1.1       kristaps   50: {
1.55      schwarze   51:        char            *endptr;
1.1       kristaps   52:
1.56      schwarze   53:        dst->unit = def == SCALE_MAX ? SCALE_BU : def;
                     54:        dst->scale = strtod(src, &endptr);
                     55:        if (endptr == src)
1.64      schwarze   56:                return NULL;
1.5       kristaps   57:
1.56      schwarze   58:        switch (*endptr++) {
1.48      schwarze   59:        case 'c':
1.56      schwarze   60:                dst->unit = SCALE_CM;
1.3       kristaps   61:                break;
1.48      schwarze   62:        case 'i':
1.56      schwarze   63:                dst->unit = SCALE_IN;
                     64:                break;
                     65:        case 'f':
                     66:                dst->unit = SCALE_FS;
                     67:                break;
                     68:        case 'M':
                     69:                dst->unit = SCALE_MM;
                     70:                break;
                     71:        case 'm':
                     72:                dst->unit = SCALE_EM;
                     73:                break;
                     74:        case 'n':
                     75:                dst->unit = SCALE_EN;
1.3       kristaps   76:                break;
1.48      schwarze   77:        case 'P':
1.56      schwarze   78:                dst->unit = SCALE_PC;
1.3       kristaps   79:                break;
1.48      schwarze   80:        case 'p':
1.56      schwarze   81:                dst->unit = SCALE_PT;
1.3       kristaps   82:                break;
1.56      schwarze   83:        case 'u':
                     84:                dst->unit = SCALE_BU;
1.3       kristaps   85:                break;
1.48      schwarze   86:        case 'v':
1.56      schwarze   87:                dst->unit = SCALE_VS;
1.3       kristaps   88:                break;
1.68      schwarze   89:        default:
1.56      schwarze   90:                endptr--;
1.5       kristaps   91:                if (SCALE_MAX == def)
1.64      schwarze   92:                        return NULL;
1.56      schwarze   93:                dst->unit = def;
1.3       kristaps   94:                break;
                     95:        }
1.64      schwarze   96:        return endptr;
1.8       kristaps   97: }
1.30      kristaps   98:
                     99: /*
                    100:  * Calculate the abstract widths and decimal positions of columns in a
                    101:  * table.  This routine allocates the columns structures then runs over
                    102:  * all rows and cells in the table.  The function pointers in "tbl" are
                    103:  * used for the actual width calculations.
                    104:  */
                    105: void
1.52      schwarze  106: tblcalc(struct rofftbl *tbl, const struct tbl_span *sp,
1.66      schwarze  107:     size_t offset, size_t rmargin)
1.30      kristaps  108: {
1.65      schwarze  109:        struct roffsu            su;
1.58      schwarze  110:        const struct tbl_opts   *opts;
1.30      kristaps  111:        const struct tbl_dat    *dp;
                    112:        struct roffcol          *col;
1.52      schwarze  113:        size_t                   ewidth, xwidth;
1.43      schwarze  114:        int                      spans;
1.58      schwarze  115:        int                      icol, maxcol, necol, nxcol, quirkcol;
1.30      kristaps  116:
                    117:        /*
                    118:         * Allocate the master column specifiers.  These will hold the
                    119:         * widths and decimal positions for all cells in the column.  It
                    120:         * must be freed and nullified by the caller.
                    121:         */
                    122:
                    123:        assert(NULL == tbl->cols);
1.48      schwarze  124:        tbl->cols = mandoc_calloc((size_t)sp->opts->cols,
                    125:            sizeof(struct roffcol));
1.58      schwarze  126:        opts = sp->opts;
1.30      kristaps  127:
1.53      schwarze  128:        for (maxcol = -1; sp; sp = sp->next) {
1.30      kristaps  129:                if (TBL_SPAN_DATA != sp->pos)
                    130:                        continue;
1.43      schwarze  131:                spans = 1;
1.30      kristaps  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.43      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.59      schwarze  143:                        icol = dp->layout->col;
1.70      schwarze  144:                        while (maxcol < icol)
                    145:                                tbl->cols[++maxcol].spacing = SIZE_MAX;
1.52      schwarze  146:                        col = tbl->cols + icol;
                    147:                        col->flags |= dp->layout->flags;
                    148:                        if (dp->layout->flags & TBL_CELL_WIGN)
                    149:                                continue;
1.65      schwarze  150:                        if (dp->layout->wstr != NULL &&
                    151:                            dp->layout->width == 0 &&
                    152:                            a2roffsu(dp->layout->wstr, &su, SCALE_EN)
                    153:                            != NULL)
                    154:                                dp->layout->width =
                    155:                                    (*tbl->sulen)(&su, tbl->arg);
                    156:                        if (col->width < dp->layout->width)
                    157:                                col->width = dp->layout->width;
1.70      schwarze  158:                        if (dp->layout->spacing != SIZE_MAX &&
                    159:                            (col->spacing == SIZE_MAX ||
                    160:                             col->spacing < dp->layout->spacing))
                    161:                                col->spacing = dp->layout->spacing;
1.66      schwarze  162:                        tblcalc_data(tbl, col, opts, dp,
1.67      schwarze  163:                            dp->block == 0 ? 0 :
                    164:                            dp->layout->width ? dp->layout->width :
1.69      schwarze  165:                            rmargin ? (rmargin + sp->opts->cols / 2)
                    166:                            / (sp->opts->cols + 1) : 0);
1.52      schwarze  167:                }
                    168:        }
                    169:
                    170:        /*
                    171:         * Count columns to equalize and columns to maximize.
                    172:         * Find maximum width of the columns to equalize.
                    173:         * Find total width of the columns *not* to maximize.
                    174:         */
                    175:
                    176:        necol = nxcol = 0;
                    177:        ewidth = xwidth = 0;
                    178:        for (icol = 0; icol <= maxcol; icol++) {
                    179:                col = tbl->cols + icol;
1.70      schwarze  180:                if (col->spacing == SIZE_MAX || icol == maxcol)
                    181:                        col->spacing = 3;
1.52      schwarze  182:                if (col->flags & TBL_CELL_EQUAL) {
                    183:                        necol++;
                    184:                        if (ewidth < col->width)
                    185:                                ewidth = col->width;
                    186:                }
                    187:                if (col->flags & TBL_CELL_WMAX)
                    188:                        nxcol++;
                    189:                else
                    190:                        xwidth += col->width;
                    191:        }
                    192:
                    193:        /*
                    194:         * Equalize columns, if requested for any of them.
                    195:         * Update total width of the columns not to maximize.
                    196:         */
                    197:
                    198:        if (necol) {
                    199:                for (icol = 0; icol <= maxcol; icol++) {
                    200:                        col = tbl->cols + icol;
                    201:                        if ( ! (col->flags & TBL_CELL_EQUAL))
                    202:                                continue;
                    203:                        if (col->width == ewidth)
                    204:                                continue;
1.66      schwarze  205:                        if (nxcol && rmargin)
1.52      schwarze  206:                                xwidth += ewidth - col->width;
                    207:                        col->width = ewidth;
                    208:                }
                    209:        }
                    210:
                    211:        /*
                    212:         * If there are any columns to maximize, find the total
                    213:         * available width, deducting 3n margins between columns.
                    214:         * Distribute the available width evenly.
                    215:         */
                    216:
1.66      schwarze  217:        if (nxcol && rmargin) {
1.63      schwarze  218:                xwidth += 3*maxcol +
1.58      schwarze  219:                    (opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ?
                    220:                     2 : !!opts->lvert + !!opts->rvert);
1.66      schwarze  221:                if (rmargin <= offset + xwidth)
1.63      schwarze  222:                        return;
1.66      schwarze  223:                xwidth = rmargin - offset - xwidth;
1.58      schwarze  224:
                    225:                /*
                    226:                 * Emulate a bug in GNU tbl width calculation that
                    227:                 * manifests itself for large numbers of x-columns.
                    228:                 * Emulating it for 5 x-columns gives identical
                    229:                 * behaviour for up to 6 x-columns.
                    230:                 */
                    231:
                    232:                if (nxcol == 5) {
                    233:                        quirkcol = xwidth % nxcol + 2;
                    234:                        if (quirkcol != 3 && quirkcol != 4)
                    235:                                quirkcol = -1;
                    236:                } else
                    237:                        quirkcol = -1;
                    238:
                    239:                necol = 0;
                    240:                ewidth = 0;
1.52      schwarze  241:                for (icol = 0; icol <= maxcol; icol++) {
                    242:                        col = tbl->cols + icol;
                    243:                        if ( ! (col->flags & TBL_CELL_WMAX))
                    244:                                continue;
1.58      schwarze  245:                        col->width = (double)xwidth * ++necol / nxcol
                    246:                            - ewidth + 0.4995;
                    247:                        if (necol == quirkcol)
                    248:                                col->width--;
                    249:                        ewidth += col->width;
1.30      kristaps  250:                }
                    251:        }
                    252: }
                    253:
                    254: static void
                    255: tblcalc_data(struct rofftbl *tbl, struct roffcol *col,
1.65      schwarze  256:     const struct tbl_opts *opts, const struct tbl_dat *dp, size_t mw)
1.30      kristaps  257: {
                    258:        size_t           sz;
                    259:
                    260:        /* Branch down into data sub-types. */
                    261:
                    262:        switch (dp->layout->pos) {
1.48      schwarze  263:        case TBL_CELL_HORIZ:
                    264:        case TBL_CELL_DHORIZ:
1.30      kristaps  265:                sz = (*tbl->len)(1, tbl->arg);
                    266:                if (col->width < sz)
                    267:                        col->width = sz;
                    268:                break;
1.48      schwarze  269:        case TBL_CELL_LONG:
                    270:        case TBL_CELL_CENTRE:
                    271:        case TBL_CELL_LEFT:
                    272:        case TBL_CELL_RIGHT:
1.65      schwarze  273:                tblcalc_literal(tbl, col, dp, mw);
1.30      kristaps  274:                break;
1.48      schwarze  275:        case TBL_CELL_NUMBER:
1.45      schwarze  276:                tblcalc_number(tbl, col, opts, dp);
1.35      kristaps  277:                break;
1.48      schwarze  278:        case TBL_CELL_DOWN:
1.30      kristaps  279:                break;
                    280:        default:
                    281:                abort();
                    282:        }
                    283: }
                    284:
                    285: static void
                    286: tblcalc_literal(struct rofftbl *tbl, struct roffcol *col,
1.65      schwarze  287:     const struct tbl_dat *dp, size_t mw)
1.30      kristaps  288: {
1.65      schwarze  289:        const char      *str;   /* Beginning of the first line. */
                    290:        const char      *beg;   /* Beginning of the current line. */
                    291:        char            *end;   /* End of the current line. */
1.66      schwarze  292:        size_t           lsz;   /* Length of the current line. */
                    293:        size_t           wsz;   /* Length of the current word. */
1.65      schwarze  294:
                    295:        if (dp->string == NULL || *dp->string == '\0')
                    296:                return;
                    297:        str = mw ? mandoc_strdup(dp->string) : dp->string;
1.66      schwarze  298:        lsz = 0;
1.65      schwarze  299:        for (beg = str; beg != NULL && *beg != '\0'; beg = end) {
                    300:                end = mw ? strchr(beg, ' ') : NULL;
                    301:                if (end != NULL) {
                    302:                        *end++ = '\0';
                    303:                        while (*end == ' ')
                    304:                                end++;
                    305:                }
1.66      schwarze  306:                wsz = (*tbl->slen)(beg, tbl->arg);
                    307:                if (mw && lsz && lsz + 1 + wsz <= mw)
                    308:                        lsz += 1 + wsz;
                    309:                else
                    310:                        lsz = wsz;
                    311:                if (col->width < lsz)
                    312:                        col->width = lsz;
1.65      schwarze  313:        }
                    314:        if (mw)
                    315:                free((void *)str);
1.30      kristaps  316: }
                    317:
                    318: static void
                    319: tblcalc_number(struct rofftbl *tbl, struct roffcol *col,
1.45      schwarze  320:                const struct tbl_opts *opts, const struct tbl_dat *dp)
1.30      kristaps  321: {
1.48      schwarze  322:        int              i;
1.71    ! schwarze  323:        size_t           sz, ssz, d;
1.34      kristaps  324:        const char      *str;
1.30      kristaps  325:        char            *cp;
                    326:        char             buf[2];
                    327:
                    328:        /*
                    329:         * First calculate number width and decimal place (last + 1 for
1.43      schwarze  330:         * non-decimal numbers).  If the stored decimal is subsequent to
1.30      kristaps  331:         * ours, make our size longer by that difference
                    332:         * (right-"shifting"); similarly, if ours is subsequent the
                    333:         * stored, then extend the stored size by the difference.
                    334:         * Finally, re-assign the stored values.
                    335:         */
                    336:
1.34      kristaps  337:        str = dp->string ? dp->string : "";
                    338:        sz = (*tbl->slen)(str, tbl->arg);
1.30      kristaps  339:
1.34      kristaps  340:        /* FIXME: TBL_DATA_HORIZ et al.? */
1.30      kristaps  341:
1.45      schwarze  342:        buf[0] = opts->decimal;
1.30      kristaps  343:        buf[1] = '\0';
                    344:
1.45      schwarze  345:        if (NULL != (cp = strrchr(str, opts->decimal))) {
1.30      kristaps  346:                buf[1] = '\0';
                    347:                for (ssz = 0, i = 0; cp != &str[i]; i++) {
                    348:                        buf[0] = str[i];
                    349:                        ssz += (*tbl->slen)(buf, tbl->arg);
                    350:                }
1.71    ! schwarze  351:                d = ssz;
1.30      kristaps  352:        } else
1.71    ! schwarze  353:                d = sz;
1.30      kristaps  354:
                    355:        /* Adjust the settings for this column. */
                    356:
                    357:        if (col->decimal > d) {
                    358:                sz += col->decimal - d;
                    359:                d = col->decimal;
                    360:        } else
                    361:                col->width += d - col->decimal;
                    362:
                    363:        if (sz > col->width)
                    364:                col->width = sz;
                    365:        if (d > col->decimal)
                    366:                col->decimal = d;
                    367: }

CVSweb