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

Annotation of mandoc/tbl_term.c, Revision 1.46

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

CVSweb