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

Annotation of mandoc/tbl_term.c, Revision 1.35

1.35    ! schwarze    1: /*     $Id: tbl_term.c,v 1.34 2015/01/28 04:19:35 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.11      kristaps   63:        const struct tbl_head   *hp;
1.33      schwarze   64:        const struct tbl_cell   *cp;
1.11      kristaps   65:        const struct tbl_dat    *dp;
1.34      schwarze   66:        static size_t            offset;
                     67:        size_t                   rmargin, maxrmargin, tsz;
1.33      schwarze   68:        int                      horiz, spans, vert;
1.11      kristaps   69:
                     70:        rmargin = tp->rmargin;
                     71:        maxrmargin = tp->maxrmargin;
                     72:
                     73:        tp->rmargin = tp->maxrmargin = TERM_MAXMARGIN;
1.1       kristaps   74:
1.4       kristaps   75:        /* Inhibit printing of spaces: we do padding ourselves. */
                     76:
                     77:        tp->flags |= TERMP_NONOSPACE;
                     78:        tp->flags |= TERMP_NOSPACE;
                     79:
                     80:        /*
1.11      kristaps   81:         * The first time we're invoked for a given table block,
                     82:         * calculate the table widths and decimal positions.
1.4       kristaps   83:         */
                     84:
1.33      schwarze   85:        if (sp->flags & TBL_SPAN_FIRST) {
1.11      kristaps   86:                term_flushln(tp);
                     87:
                     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;
                    100:                        for (hp = sp->head; hp != NULL; hp = hp->next)
                    101:                                tsz += tp->tbl.cols[hp->ident].width + 3;
                    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.
                    130:         * Spanner spans get a horizontal rule; data spanners have their
                    131:         * data printed by matching data to header.
                    132:         */
                    133:
1.33      schwarze  134:        if (sp->pos == TBL_SPAN_DATA) {
1.4       kristaps  135:                /* Iterate over template headers. */
1.33      schwarze  136:                cp = sp->layout->first;
1.4       kristaps  137:                dp = sp->first;
1.16      kristaps  138:                spans = 0;
1.33      schwarze  139:                for (hp = sp->head; hp != NULL; hp = hp->next) {
1.22      schwarze  140:
1.27      schwarze  141:                        /*
1.33      schwarze  142:                         * Remeber whether we need a vertical bar
                    143:                         * after this cell.
1.16      kristaps  144:                         */
1.11      kristaps  145:
1.33      schwarze  146:                        vert = cp == NULL ? 0 : cp->vert;
1.21      schwarze  147:
1.33      schwarze  148:                        /*
                    149:                         * Print the data and advance to the next cell.
                    150:                         */
1.21      schwarze  151:
1.33      schwarze  152:                        if (spans == 0) {
                    153:                                tbl_data(tp, sp->opts, dp,
                    154:                                    tp->tbl.cols + hp->ident);
                    155:                                if (dp != NULL) {
                    156:                                        spans = dp->spans;
                    157:                                        dp = dp->next;
                    158:                                }
                    159:                        } else
                    160:                                spans--;
                    161:                        if (cp != NULL)
                    162:                                cp = cp->next;
1.1       kristaps  163:
1.27      schwarze  164:                        /*
1.33      schwarze  165:                         * Separate columns, except in the middle
                    166:                         * of spans and after the last cell.
1.16      kristaps  167:                         */
                    168:
1.33      schwarze  169:                        if (hp->next == NULL || spans)
                    170:                                continue;
                    171:
                    172:                        tbl_char(tp, ASCII_NBRSP, 1);
                    173:                        if (vert > 0)
                    174:                                tbl_char(tp, '|', vert);
                    175:                        if (vert < 2 && hp->next != NULL)
                    176:                                tbl_char(tp, ASCII_NBRSP, 2 - vert);
1.1       kristaps  177:                }
1.33      schwarze  178:        } else if (horiz)
                    179:                tbl_hrule(tp, sp, 0);
1.1       kristaps  180:
1.21      schwarze  181:        /* Vertical frame at the end of each row. */
                    182:
1.33      schwarze  183:        if (sp->layout->last->vert ||
                    184:            (sp->prev != NULL && sp->prev->layout->last->vert) ||
                    185:            (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)))
                    186:                term_word(tp, horiz ? "+" : " |");
                    187:        else if (sp->opts->rvert)
                    188:                tbl_char(tp, horiz ? '-' : ASCII_NBRSP, 1);
1.1       kristaps  189:        term_flushln(tp);
                    190:
1.4       kristaps  191:        /*
                    192:         * If we're the last row, clean up after ourselves: clear the
                    193:         * existing table configuration and set it to NULL.
                    194:         */
                    195:
1.33      schwarze  196:        if (sp->flags & TBL_SPAN_LAST) {
                    197:                if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
                    198:                        tbl_hrule(tp, sp, 1);
1.24      schwarze  199:                        tp->skipvsp = 1;
                    200:                }
1.33      schwarze  201:                if (sp->opts->opts & TBL_OPT_DBOX) {
                    202:                        tbl_hrule(tp, sp, 2);
1.24      schwarze  203:                        tp->skipvsp = 2;
                    204:                }
1.11      kristaps  205:                assert(tp->tbl.cols);
                    206:                free(tp->tbl.cols);
                    207:                tp->tbl.cols = NULL;
1.34      schwarze  208:                tp->offset = offset;
1.2       kristaps  209:        }
1.1       kristaps  210:
                    211:        tp->flags &= ~TERMP_NONOSPACE;
1.11      kristaps  212:        tp->rmargin = rmargin;
                    213:        tp->maxrmargin = maxrmargin;
1.1       kristaps  214: }
                    215:
1.21      schwarze  216: /*
1.33      schwarze  217:  * Kinds of horizontal rulers:
                    218:  * 0: inside the table (single or double line with crossings)
                    219:  * 1: inner frame (single line with crossings and ends)
                    220:  * 2: outer frame (single line without crossings with ends)
1.21      schwarze  221:  */
1.1       kristaps  222: static void
1.33      schwarze  223: tbl_hrule(struct termp *tp, const struct tbl_span *sp, int kind)
1.1       kristaps  224: {
1.33      schwarze  225:        const struct tbl_cell *c1, *c2;
                    226:        int      vert;
                    227:        char     line, cross;
                    228:
                    229:        line = (kind == 0 && TBL_SPAN_DHORIZ == sp->pos) ? '=' : '-';
                    230:        cross = (kind < 2) ? '+' : '-';
                    231:
                    232:        if (kind)
                    233:                term_word(tp, "+");
                    234:        c1 = sp->layout->first;
                    235:        c2 = sp->prev == NULL ? NULL : sp->prev->layout->first;
                    236:        if (c2 == c1)
                    237:                c2 = NULL;
                    238:        for (;;) {
                    239:                tbl_char(tp, line, tp->tbl.cols[c1->head->ident].width + 1);
                    240:                vert = c1->vert;
                    241:                if ((c1 = c1->next) == NULL)
                    242:                         break;
                    243:                if (c2 != NULL) {
                    244:                        if (vert < c2->vert)
                    245:                                vert = c2->vert;
                    246:                        c2 = c2->next;
                    247:                }
                    248:                if (vert)
                    249:                        tbl_char(tp, cross, vert);
                    250:                if (vert < 2)
                    251:                        tbl_char(tp, line, 2 - vert);
1.22      schwarze  252:        }
1.33      schwarze  253:        if (kind) {
                    254:                term_word(tp, "+");
                    255:                term_flushln(tp);
1.22      schwarze  256:        }
1.1       kristaps  257: }
                    258:
                    259: static void
1.25      schwarze  260: tbl_data(struct termp *tp, const struct tbl_opts *opts,
1.27      schwarze  261:        const struct tbl_dat *dp,
                    262:        const struct roffcol *col)
1.1       kristaps  263: {
                    264:
1.35    ! schwarze  265:        if (dp == NULL) {
1.11      kristaps  266:                tbl_char(tp, ASCII_NBRSP, col->width);
1.1       kristaps  267:                return;
                    268:        }
                    269:
                    270:        switch (dp->pos) {
1.27      schwarze  271:        case TBL_DATA_NONE:
1.11      kristaps  272:                tbl_char(tp, ASCII_NBRSP, col->width);
1.10      kristaps  273:                return;
1.27      schwarze  274:        case TBL_DATA_HORIZ:
1.1       kristaps  275:                /* FALLTHROUGH */
1.27      schwarze  276:        case TBL_DATA_NHORIZ:
1.11      kristaps  277:                tbl_char(tp, '-', col->width);
1.7       kristaps  278:                return;
1.27      schwarze  279:        case TBL_DATA_NDHORIZ:
1.7       kristaps  280:                /* FALLTHROUGH */
1.27      schwarze  281:        case TBL_DATA_DHORIZ:
1.11      kristaps  282:                tbl_char(tp, '=', col->width);
1.1       kristaps  283:                return;
                    284:        default:
                    285:                break;
                    286:        }
1.27      schwarze  287:
1.16      kristaps  288:        switch (dp->layout->pos) {
1.27      schwarze  289:        case TBL_CELL_HORIZ:
1.11      kristaps  290:                tbl_char(tp, '-', col->width);
1.7       kristaps  291:                break;
1.27      schwarze  292:        case TBL_CELL_DHORIZ:
1.11      kristaps  293:                tbl_char(tp, '=', col->width);
1.1       kristaps  294:                break;
1.27      schwarze  295:        case TBL_CELL_LONG:
1.1       kristaps  296:                /* FALLTHROUGH */
1.27      schwarze  297:        case TBL_CELL_CENTRE:
1.1       kristaps  298:                /* FALLTHROUGH */
1.27      schwarze  299:        case TBL_CELL_LEFT:
1.1       kristaps  300:                /* FALLTHROUGH */
1.27      schwarze  301:        case TBL_CELL_RIGHT:
1.11      kristaps  302:                tbl_literal(tp, dp, col);
1.1       kristaps  303:                break;
1.27      schwarze  304:        case TBL_CELL_NUMBER:
1.25      schwarze  305:                tbl_number(tp, opts, dp, col);
1.18      kristaps  306:                break;
1.27      schwarze  307:        case TBL_CELL_DOWN:
1.18      kristaps  308:                tbl_char(tp, ASCII_NBRSP, col->width);
1.1       kristaps  309:                break;
                    310:        default:
                    311:                abort();
                    312:                /* NOTREACHED */
                    313:        }
                    314: }
                    315:
                    316: static void
1.12      kristaps  317: tbl_char(struct termp *tp, char c, size_t len)
1.1       kristaps  318: {
1.12      kristaps  319:        size_t          i, sz;
                    320:        char            cp[2];
                    321:
                    322:        cp[0] = c;
                    323:        cp[1] = '\0';
1.1       kristaps  324:
1.3       kristaps  325:        sz = term_strlen(tp, cp);
                    326:
                    327:        for (i = 0; i < len; i += sz)
1.1       kristaps  328:                term_word(tp, cp);
                    329: }
                    330:
                    331: static void
1.27      schwarze  332: tbl_literal(struct termp *tp, const struct tbl_dat *dp,
1.11      kristaps  333:                const struct roffcol *col)
1.1       kristaps  334: {
1.23      schwarze  335:        struct tbl_head         *hp;
                    336:        size_t                   width, len, padl, padr;
                    337:        int                      spans;
1.1       kristaps  338:
1.17      kristaps  339:        assert(dp->string);
1.21      schwarze  340:        len = term_strlen(tp, dp->string);
1.23      schwarze  341:
                    342:        hp = dp->layout->head->next;
                    343:        width = col->width;
                    344:        for (spans = dp->spans; spans--; hp = hp->next)
                    345:                width += tp->tbl.cols[hp->ident].width + 3;
                    346:
                    347:        padr = width > len ? width - len : 0;
1.21      schwarze  348:        padl = 0;
1.1       kristaps  349:
1.16      kristaps  350:        switch (dp->layout->pos) {
1.27      schwarze  351:        case TBL_CELL_LONG:
1.21      schwarze  352:                padl = term_len(tp, 1);
                    353:                padr = padr > padl ? padr - padl : 0;
1.1       kristaps  354:                break;
1.27      schwarze  355:        case TBL_CELL_CENTRE:
1.21      schwarze  356:                if (2 > padr)
1.19      schwarze  357:                        break;
1.21      schwarze  358:                padl = padr / 2;
1.19      schwarze  359:                padr -= padl;
1.1       kristaps  360:                break;
1.27      schwarze  361:        case TBL_CELL_RIGHT:
1.21      schwarze  362:                padl = padr;
                    363:                padr = 0;
1.1       kristaps  364:                break;
                    365:        default:
                    366:                break;
                    367:        }
                    368:
                    369:        tbl_char(tp, ASCII_NBRSP, padl);
1.29      schwarze  370:        tbl_word(tp, dp);
1.21      schwarze  371:        tbl_char(tp, ASCII_NBRSP, padr);
1.1       kristaps  372: }
                    373:
                    374: static void
1.25      schwarze  375: tbl_number(struct termp *tp, const struct tbl_opts *opts,
1.2       kristaps  376:                const struct tbl_dat *dp,
1.11      kristaps  377:                const struct roffcol *col)
1.1       kristaps  378: {
1.11      kristaps  379:        char            *cp;
                    380:        char             buf[2];
                    381:        size_t           sz, psz, ssz, d, padl;
                    382:        int              i;
1.1       kristaps  383:
                    384:        /*
                    385:         * See calc_data_number().  Left-pad by taking the offset of our
                    386:         * and the maximum decimal; right-pad by the remaining amount.
                    387:         */
                    388:
1.17      kristaps  389:        assert(dp->string);
1.2       kristaps  390:
1.17      kristaps  391:        sz = term_strlen(tp, dp->string);
1.2       kristaps  392:
1.25      schwarze  393:        buf[0] = opts->decimal;
1.11      kristaps  394:        buf[1] = '\0';
1.2       kristaps  395:
1.11      kristaps  396:        psz = term_strlen(tp, buf);
1.10      kristaps  397:
1.35    ! schwarze  398:        if ((cp = strrchr(dp->string, opts->decimal)) != NULL) {
1.17      kristaps  399:                for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
                    400:                        buf[0] = dp->string[i];
1.5       kristaps  401:                        ssz += term_strlen(tp, buf);
                    402:                }
                    403:                d = ssz + psz;
                    404:        } else
                    405:                d = sz + psz;
1.2       kristaps  406:
1.32      schwarze  407:        if (col->decimal > d && col->width > sz) {
                    408:                padl = col->decimal - d;
                    409:                if (padl + sz > col->width)
                    410:                        padl = col->width - sz;
                    411:                tbl_char(tp, ASCII_NBRSP, padl);
                    412:        } else
                    413:                padl = 0;
1.29      schwarze  414:        tbl_word(tp, dp);
1.21      schwarze  415:        if (col->width > sz + padl)
                    416:                tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);
1.2       kristaps  417: }
                    418:
1.29      schwarze  419: static void
                    420: tbl_word(struct termp *tp, const struct tbl_dat *dp)
                    421: {
                    422:        const void      *prev_font;
                    423:
                    424:        prev_font = term_fontq(tp);
                    425:        if (dp->layout->flags & TBL_CELL_BOLD)
                    426:                term_fontpush(tp, TERMFONT_BOLD);
                    427:        else if (dp->layout->flags & TBL_CELL_ITALIC)
                    428:                term_fontpush(tp, TERMFONT_UNDER);
                    429:
                    430:        term_word(tp, dp->string);
                    431:
                    432:        term_fontpopq(tp, prev_font);
                    433: }

CVSweb