[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.33

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