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

Annotation of mandoc/tbl_term.c, Revision 1.39

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

CVSweb