[BACK]Return to tbl_term.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/tbl_term.c, Revision 1.23

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