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

Annotation of mandoc/tbl_term.c, Revision 1.28

1.28    ! schwarze    1: /*     $Id: tbl_term.c,v 1.27 2014/04/20 16:46:05 schwarze Exp $ */
1.1       kristaps    2: /*
1.20      kristaps    3:  * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.26      schwarze    4:  * Copyright (c) 2011, 2012, 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:  */
                     18: #include "config.h"
1.28    ! schwarze   19:
        !            20: #include <sys/types.h>
1.1       kristaps   21:
                     22: #include <assert.h>
                     23: #include <stdio.h>
                     24: #include <stdlib.h>
                     25: #include <string.h>
                     26:
                     27: #include "mandoc.h"
                     28: #include "out.h"
                     29: #include "term.h"
                     30:
1.11      kristaps   31: static size_t  term_tbl_len(size_t, void *);
                     32: static size_t  term_tbl_strlen(const char *, void *);
1.12      kristaps   33: static void    tbl_char(struct termp *, char, size_t);
1.25      schwarze   34: static void    tbl_data(struct termp *, const struct tbl_opts *,
1.27      schwarze   35:                        const struct tbl_dat *,
1.11      kristaps   36:                        const struct roffcol *);
1.21      schwarze   37: static size_t  tbl_rulewidth(struct termp *, const struct tbl_head *);
                     38: static void    tbl_hframe(struct termp *, const struct tbl_span *, int);
1.27      schwarze   39: static void    tbl_literal(struct termp *, const struct tbl_dat *,
1.11      kristaps   40:                        const struct roffcol *);
1.27      schwarze   41: static void    tbl_number(struct termp *, const struct tbl_opts *,
                     42:                        const struct tbl_dat *,
1.11      kristaps   43:                        const struct roffcol *);
                     44: static void    tbl_hrule(struct termp *, const struct tbl_span *);
                     45: static void    tbl_vrule(struct termp *, const struct tbl_head *);
                     46:
                     47:
                     48: static size_t
                     49: term_tbl_strlen(const char *p, void *arg)
                     50: {
                     51:
                     52:        return(term_strlen((const struct termp *)arg, p));
                     53: }
                     54:
                     55: static size_t
                     56: term_tbl_len(size_t sz, void *arg)
                     57: {
                     58:
                     59:        return(term_len((const struct termp *)arg, sz));
                     60: }
1.1       kristaps   61:
                     62: void
                     63: term_tbl(struct termp *tp, const struct tbl_span *sp)
                     64: {
1.11      kristaps   65:        const struct tbl_head   *hp;
                     66:        const struct tbl_dat    *dp;
                     67:        struct roffcol          *col;
1.16      kristaps   68:        int                      spans;
1.27      schwarze   69:        size_t                   rmargin, maxrmargin;
1.11      kristaps   70:
                     71:        rmargin = tp->rmargin;
                     72:        maxrmargin = tp->maxrmargin;
                     73:
                     74:        tp->rmargin = tp->maxrmargin = TERM_MAXMARGIN;
1.1       kristaps   75:
1.4       kristaps   76:        /* Inhibit printing of spaces: we do padding ourselves. */
                     77:
                     78:        tp->flags |= TERMP_NONOSPACE;
                     79:        tp->flags |= TERMP_NOSPACE;
                     80:
                     81:        /*
1.11      kristaps   82:         * The first time we're invoked for a given table block,
                     83:         * calculate the table widths and decimal positions.
1.4       kristaps   84:         */
                     85:
1.2       kristaps   86:        if (TBL_SPAN_FIRST & sp->flags) {
1.11      kristaps   87:                term_flushln(tp);
                     88:
                     89:                tp->tbl.len = term_tbl_len;
                     90:                tp->tbl.slen = term_tbl_strlen;
                     91:                tp->tbl.arg = tp;
1.4       kristaps   92:
1.11      kristaps   93:                tblcalc(&tp->tbl, sp);
1.2       kristaps   94:        }
1.1       kristaps   95:
1.4       kristaps   96:        /* Horizontal frame at the start of boxed tables. */
                     97:
1.21      schwarze   98:        if (TBL_SPAN_FIRST & sp->flags) {
1.25      schwarze   99:                if (TBL_OPT_DBOX & sp->opts->opts)
1.21      schwarze  100:                        tbl_hframe(tp, sp, 1);
1.25      schwarze  101:                if (TBL_OPT_DBOX & sp->opts->opts ||
                    102:                    TBL_OPT_BOX  & sp->opts->opts)
1.21      schwarze  103:                        tbl_hframe(tp, sp, 0);
                    104:        }
1.1       kristaps  105:
1.4       kristaps  106:        /* Vertical frame at the start of each row. */
1.1       kristaps  107:
1.26      schwarze  108:        if ((TBL_OPT_BOX | TBL_OPT_DBOX) & sp->opts->opts ||
                    109:            sp->head->vert)
1.21      schwarze  110:                term_word(tp, TBL_SPAN_HORIZ == sp->pos ||
1.27      schwarze  111:                    TBL_SPAN_DHORIZ == sp->pos ? "+" : "|");
1.1       kristaps  112:
1.4       kristaps  113:        /*
                    114:         * Now print the actual data itself depending on the span type.
                    115:         * Spanner spans get a horizontal rule; data spanners have their
                    116:         * data printed by matching data to header.
                    117:         */
                    118:
1.1       kristaps  119:        switch (sp->pos) {
1.27      schwarze  120:        case TBL_SPAN_HORIZ:
1.1       kristaps  121:                /* FALLTHROUGH */
1.27      schwarze  122:        case TBL_SPAN_DHORIZ:
1.1       kristaps  123:                tbl_hrule(tp, sp);
                    124:                break;
1.27      schwarze  125:        case TBL_SPAN_DATA:
1.4       kristaps  126:                /* Iterate over template headers. */
                    127:                dp = sp->first;
1.16      kristaps  128:                spans = 0;
1.4       kristaps  129:                for (hp = sp->head; hp; hp = hp->next) {
1.22      schwarze  130:
1.27      schwarze  131:                        /*
1.16      kristaps  132:                         * If the current data header is invoked during
                    133:                         * a spanner ("spans" > 0), don't emit anything
                    134:                         * at all.
                    135:                         */
1.11      kristaps  136:
1.16      kristaps  137:                        if (--spans >= 0)
                    138:                                continue;
                    139:
1.22      schwarze  140:                        /* Separate columns. */
1.21      schwarze  141:
1.22      schwarze  142:                        if (NULL != hp->prev)
                    143:                                tbl_vrule(tp, hp);
1.21      schwarze  144:
1.11      kristaps  145:                        col = &tp->tbl.cols[hp->ident];
1.25      schwarze  146:                        tbl_data(tp, sp->opts, dp, col);
1.1       kristaps  147:
1.27      schwarze  148:                        /*
1.16      kristaps  149:                         * Go to the next data cell and assign the
                    150:                         * number of subsequent spans, if applicable.
                    151:                         */
                    152:
                    153:                        if (dp) {
                    154:                                spans = dp->spans;
1.1       kristaps  155:                                dp = dp->next;
1.16      kristaps  156:                        }
1.1       kristaps  157:                }
1.4       kristaps  158:                break;
1.1       kristaps  159:        }
                    160:
1.21      schwarze  161:        /* Vertical frame at the end of each row. */
                    162:
1.26      schwarze  163:        if ((TBL_OPT_BOX | TBL_OPT_DBOX) & sp->opts->opts ||
                    164:            sp->layout->vert)
1.21      schwarze  165:                term_word(tp, TBL_SPAN_HORIZ == sp->pos ||
1.27      schwarze  166:                    TBL_SPAN_DHORIZ == sp->pos ? "+" : " |");
1.1       kristaps  167:        term_flushln(tp);
                    168:
1.4       kristaps  169:        /*
                    170:         * If we're the last row, clean up after ourselves: clear the
                    171:         * existing table configuration and set it to NULL.
                    172:         */
                    173:
1.2       kristaps  174:        if (TBL_SPAN_LAST & sp->flags) {
1.25      schwarze  175:                if (TBL_OPT_DBOX & sp->opts->opts ||
                    176:                    TBL_OPT_BOX  & sp->opts->opts) {
1.21      schwarze  177:                        tbl_hframe(tp, sp, 0);
1.24      schwarze  178:                        tp->skipvsp = 1;
                    179:                }
1.25      schwarze  180:                if (TBL_OPT_DBOX & sp->opts->opts) {
1.21      schwarze  181:                        tbl_hframe(tp, sp, 1);
1.24      schwarze  182:                        tp->skipvsp = 2;
                    183:                }
1.11      kristaps  184:                assert(tp->tbl.cols);
                    185:                free(tp->tbl.cols);
                    186:                tp->tbl.cols = NULL;
1.2       kristaps  187:        }
1.1       kristaps  188:
                    189:        tp->flags &= ~TERMP_NONOSPACE;
1.11      kristaps  190:        tp->rmargin = rmargin;
                    191:        tp->maxrmargin = maxrmargin;
1.1       kristaps  192:
                    193: }
                    194:
1.21      schwarze  195: /*
                    196:  * Horizontal rules extend across the entire table.
                    197:  * Calculate the width by iterating over columns.
                    198:  */
                    199: static size_t
                    200: tbl_rulewidth(struct termp *tp, const struct tbl_head *hp)
                    201: {
                    202:        size_t           width;
                    203:
                    204:        width = tp->tbl.cols[hp->ident].width;
1.22      schwarze  205:
                    206:        /* Account for leading blanks. */
                    207:        if (hp->prev)
                    208:                width += 2 - hp->vert;
                    209:
                    210:        /* Account for trailing blank. */
                    211:        width++;
                    212:
1.21      schwarze  213:        return(width);
                    214: }
                    215:
                    216: /*
                    217:  * Rules inside the table can be single or double
                    218:  * and have crossings with vertical rules marked with pluses.
                    219:  */
1.1       kristaps  220: static void
                    221: tbl_hrule(struct termp *tp, const struct tbl_span *sp)
                    222: {
                    223:        const struct tbl_head *hp;
                    224:        char             c;
                    225:
                    226:        c = '-';
                    227:        if (TBL_SPAN_DHORIZ == sp->pos)
                    228:                c = '=';
                    229:
1.22      schwarze  230:        for (hp = sp->head; hp; hp = hp->next) {
                    231:                if (hp->prev && hp->vert)
                    232:                        tbl_char(tp, '+', hp->vert);
                    233:                tbl_char(tp, c, tbl_rulewidth(tp, hp));
                    234:        }
1.1       kristaps  235: }
                    236:
1.21      schwarze  237: /*
                    238:  * Rules above and below the table are always single
                    239:  * and have an additional plus at the beginning and end.
                    240:  * For double frames, this function is called twice,
                    241:  * and the outer one does not have crossings.
                    242:  */
1.1       kristaps  243: static void
1.21      schwarze  244: tbl_hframe(struct termp *tp, const struct tbl_span *sp, int outer)
1.1       kristaps  245: {
                    246:        const struct tbl_head *hp;
                    247:
                    248:        term_word(tp, "+");
1.22      schwarze  249:        for (hp = sp->head; hp; hp = hp->next) {
                    250:                if (hp->prev && hp->vert)
                    251:                        tbl_char(tp, (outer ? '-' : '+'), hp->vert);
                    252:                tbl_char(tp, '-', tbl_rulewidth(tp, hp));
                    253:        }
1.1       kristaps  254:        term_word(tp, "+");
                    255:        term_flushln(tp);
                    256: }
                    257:
                    258: static void
1.25      schwarze  259: tbl_data(struct termp *tp, const struct tbl_opts *opts,
1.27      schwarze  260:        const struct tbl_dat *dp,
                    261:        const struct roffcol *col)
1.1       kristaps  262: {
                    263:
                    264:        if (NULL == dp) {
1.11      kristaps  265:                tbl_char(tp, ASCII_NBRSP, col->width);
1.1       kristaps  266:                return;
                    267:        }
1.16      kristaps  268:        assert(dp->layout);
1.1       kristaps  269:
                    270:        switch (dp->pos) {
1.27      schwarze  271:        case TBL_DATA_NONE:
1.11      kristaps  272:                tbl_char(tp, ASCII_NBRSP, col->width);
1.10      kristaps  273:                return;
1.27      schwarze  274:        case TBL_DATA_HORIZ:
1.1       kristaps  275:                /* FALLTHROUGH */
1.27      schwarze  276:        case TBL_DATA_NHORIZ:
1.11      kristaps  277:                tbl_char(tp, '-', col->width);
1.7       kristaps  278:                return;
1.27      schwarze  279:        case TBL_DATA_NDHORIZ:
1.7       kristaps  280:                /* FALLTHROUGH */
1.27      schwarze  281:        case TBL_DATA_DHORIZ:
1.11      kristaps  282:                tbl_char(tp, '=', col->width);
1.1       kristaps  283:                return;
                    284:        default:
                    285:                break;
                    286:        }
1.27      schwarze  287:
1.16      kristaps  288:        switch (dp->layout->pos) {
1.27      schwarze  289:        case TBL_CELL_HORIZ:
1.11      kristaps  290:                tbl_char(tp, '-', col->width);
1.7       kristaps  291:                break;
1.27      schwarze  292:        case TBL_CELL_DHORIZ:
1.11      kristaps  293:                tbl_char(tp, '=', col->width);
1.1       kristaps  294:                break;
1.27      schwarze  295:        case TBL_CELL_LONG:
1.1       kristaps  296:                /* FALLTHROUGH */
1.27      schwarze  297:        case TBL_CELL_CENTRE:
1.1       kristaps  298:                /* FALLTHROUGH */
1.27      schwarze  299:        case TBL_CELL_LEFT:
1.1       kristaps  300:                /* FALLTHROUGH */
1.27      schwarze  301:        case TBL_CELL_RIGHT:
1.11      kristaps  302:                tbl_literal(tp, dp, col);
1.1       kristaps  303:                break;
1.27      schwarze  304:        case TBL_CELL_NUMBER:
1.25      schwarze  305:                tbl_number(tp, opts, dp, col);
1.18      kristaps  306:                break;
1.27      schwarze  307:        case TBL_CELL_DOWN:
1.18      kristaps  308:                tbl_char(tp, ASCII_NBRSP, col->width);
1.1       kristaps  309:                break;
                    310:        default:
                    311:                abort();
                    312:                /* NOTREACHED */
                    313:        }
                    314: }
1.11      kristaps  315:
1.1       kristaps  316: static void
1.11      kristaps  317: tbl_vrule(struct termp *tp, const struct tbl_head *hp)
1.1       kristaps  318: {
                    319:
1.22      schwarze  320:        tbl_char(tp, ASCII_NBRSP, 1);
                    321:        if (0 < hp->vert)
                    322:                tbl_char(tp, '|', hp->vert);
                    323:        if (2 > hp->vert)
                    324:                tbl_char(tp, ASCII_NBRSP, 2 - hp->vert);
1.1       kristaps  325: }
                    326:
                    327: static void
1.12      kristaps  328: tbl_char(struct termp *tp, char c, size_t len)
1.1       kristaps  329: {
1.12      kristaps  330:        size_t          i, sz;
                    331:        char            cp[2];
                    332:
                    333:        cp[0] = c;
                    334:        cp[1] = '\0';
1.1       kristaps  335:
1.3       kristaps  336:        sz = term_strlen(tp, cp);
                    337:
                    338:        for (i = 0; i < len; i += sz)
1.1       kristaps  339:                term_word(tp, cp);
                    340: }
                    341:
                    342: static void
1.27      schwarze  343: tbl_literal(struct termp *tp, const struct tbl_dat *dp,
1.11      kristaps  344:                const struct roffcol *col)
1.1       kristaps  345: {
1.23      schwarze  346:        struct tbl_head         *hp;
                    347:        size_t                   width, len, padl, padr;
                    348:        int                      spans;
1.1       kristaps  349:
1.17      kristaps  350:        assert(dp->string);
1.21      schwarze  351:        len = term_strlen(tp, dp->string);
1.23      schwarze  352:
                    353:        hp = dp->layout->head->next;
                    354:        width = col->width;
                    355:        for (spans = dp->spans; spans--; hp = hp->next)
                    356:                width += tp->tbl.cols[hp->ident].width + 3;
                    357:
                    358:        padr = width > len ? width - len : 0;
1.21      schwarze  359:        padl = 0;
1.1       kristaps  360:
1.16      kristaps  361:        switch (dp->layout->pos) {
1.27      schwarze  362:        case TBL_CELL_LONG:
1.21      schwarze  363:                padl = term_len(tp, 1);
                    364:                padr = padr > padl ? padr - padl : 0;
1.1       kristaps  365:                break;
1.27      schwarze  366:        case TBL_CELL_CENTRE:
1.21      schwarze  367:                if (2 > padr)
1.19      schwarze  368:                        break;
1.21      schwarze  369:                padl = padr / 2;
1.19      schwarze  370:                padr -= padl;
1.1       kristaps  371:                break;
1.27      schwarze  372:        case TBL_CELL_RIGHT:
1.21      schwarze  373:                padl = padr;
                    374:                padr = 0;
1.1       kristaps  375:                break;
                    376:        default:
                    377:                break;
                    378:        }
                    379:
                    380:        tbl_char(tp, ASCII_NBRSP, padl);
1.17      kristaps  381:        term_word(tp, dp->string);
1.21      schwarze  382:        tbl_char(tp, ASCII_NBRSP, padr);
1.1       kristaps  383: }
                    384:
                    385: static void
1.25      schwarze  386: tbl_number(struct termp *tp, const struct tbl_opts *opts,
1.2       kristaps  387:                const struct tbl_dat *dp,
1.11      kristaps  388:                const struct roffcol *col)
1.1       kristaps  389: {
1.11      kristaps  390:        char            *cp;
                    391:        char             buf[2];
                    392:        size_t           sz, psz, ssz, d, padl;
                    393:        int              i;
1.1       kristaps  394:
                    395:        /*
                    396:         * See calc_data_number().  Left-pad by taking the offset of our
                    397:         * and the maximum decimal; right-pad by the remaining amount.
                    398:         */
                    399:
1.17      kristaps  400:        assert(dp->string);
1.2       kristaps  401:
1.17      kristaps  402:        sz = term_strlen(tp, dp->string);
1.2       kristaps  403:
1.25      schwarze  404:        buf[0] = opts->decimal;
1.11      kristaps  405:        buf[1] = '\0';
1.2       kristaps  406:
1.11      kristaps  407:        psz = term_strlen(tp, buf);
1.10      kristaps  408:
1.25      schwarze  409:        if (NULL != (cp = strrchr(dp->string, opts->decimal))) {
1.5       kristaps  410:                buf[1] = '\0';
1.17      kristaps  411:                for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
                    412:                        buf[0] = dp->string[i];
1.5       kristaps  413:                        ssz += term_strlen(tp, buf);
                    414:                }
                    415:                d = ssz + psz;
                    416:        } else
                    417:                d = sz + psz;
1.2       kristaps  418:
1.11      kristaps  419:        padl = col->decimal - d;
1.2       kristaps  420:
1.11      kristaps  421:        tbl_char(tp, ASCII_NBRSP, padl);
1.17      kristaps  422:        term_word(tp, dp->string);
1.21      schwarze  423:        if (col->width > sz + padl)
                    424:                tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);
1.2       kristaps  425: }
                    426:

CVSweb