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

Annotation of mandoc/tbl_term.c, Revision 1.33

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

CVSweb