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

Annotation of mandoc/tbl_term.c, Revision 1.45

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

CVSweb