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

Annotation of mandoc/tbl_term.c, Revision 1.41

1.41    ! schwarze    1: /*     $Id: tbl_term.c,v 1.40 2015/03/06 15:48:53 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.39      schwarze   68:
1.11      kristaps   69:        rmargin = tp->rmargin;
                     70:        maxrmargin = tp->maxrmargin;
                     71:
                     72:        tp->rmargin = tp->maxrmargin = TERM_MAXMARGIN;
1.1       kristaps   73:
1.4       kristaps   74:        /* Inhibit printing of spaces: we do padding ourselves. */
                     75:
                     76:        tp->flags |= TERMP_NONOSPACE;
                     77:        tp->flags |= TERMP_NOSPACE;
                     78:
                     79:        /*
1.11      kristaps   80:         * The first time we're invoked for a given table block,
                     81:         * calculate the table widths and decimal positions.
1.4       kristaps   82:         */
                     83:
1.37      schwarze   84:        if (tp->tbl.cols == NULL) {
1.11      kristaps   85:                tp->tbl.len = term_tbl_len;
                     86:                tp->tbl.slen = term_tbl_strlen;
                     87:                tp->tbl.arg = tp;
1.4       kristaps   88:
1.30      schwarze   89:                tblcalc(&tp->tbl, sp, rmargin - tp->offset);
1.1       kristaps   90:
1.34      schwarze   91:                /* Center the table as a whole. */
                     92:
                     93:                offset = tp->offset;
                     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;
                    100:                        if (offset + tsz > rmargin)
                    101:                                tsz -= 1;
                    102:                        tp->offset = (offset + rmargin > tsz) ?
                    103:                            (offset + rmargin - tsz) / 2 : 0;
                    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.34      schwarze  202:                tp->offset = offset;
1.2       kristaps  203:        }
1.1       kristaps  204:
                    205:        tp->flags &= ~TERMP_NONOSPACE;
1.11      kristaps  206:        tp->rmargin = rmargin;
                    207:        tp->maxrmargin = maxrmargin;
1.1       kristaps  208: }
                    209:
1.21      schwarze  210: /*
1.33      schwarze  211:  * Kinds of horizontal rulers:
                    212:  * 0: inside the table (single or double line with crossings)
                    213:  * 1: inner frame (single line with crossings and ends)
                    214:  * 2: outer frame (single line without crossings with ends)
1.21      schwarze  215:  */
1.1       kristaps  216: static void
1.33      schwarze  217: tbl_hrule(struct termp *tp, const struct tbl_span *sp, int kind)
1.1       kristaps  218: {
1.33      schwarze  219:        const struct tbl_cell *c1, *c2;
                    220:        int      vert;
                    221:        char     line, cross;
                    222:
                    223:        line = (kind == 0 && TBL_SPAN_DHORIZ == sp->pos) ? '=' : '-';
                    224:        cross = (kind < 2) ? '+' : '-';
                    225:
                    226:        if (kind)
                    227:                term_word(tp, "+");
                    228:        c1 = sp->layout->first;
                    229:        c2 = sp->prev == NULL ? NULL : sp->prev->layout->first;
                    230:        if (c2 == c1)
                    231:                c2 = NULL;
                    232:        for (;;) {
1.36      schwarze  233:                tbl_char(tp, line, tp->tbl.cols[c1->col].width + 1);
1.33      schwarze  234:                vert = c1->vert;
                    235:                if ((c1 = c1->next) == NULL)
                    236:                         break;
                    237:                if (c2 != NULL) {
                    238:                        if (vert < c2->vert)
                    239:                                vert = c2->vert;
                    240:                        c2 = c2->next;
                    241:                }
                    242:                if (vert)
                    243:                        tbl_char(tp, cross, vert);
                    244:                if (vert < 2)
                    245:                        tbl_char(tp, line, 2 - vert);
1.22      schwarze  246:        }
1.33      schwarze  247:        if (kind) {
                    248:                term_word(tp, "+");
                    249:                term_flushln(tp);
1.22      schwarze  250:        }
1.1       kristaps  251: }
                    252:
                    253: static void
1.25      schwarze  254: tbl_data(struct termp *tp, const struct tbl_opts *opts,
1.27      schwarze  255:        const struct tbl_dat *dp,
                    256:        const struct roffcol *col)
1.1       kristaps  257: {
                    258:
1.35      schwarze  259:        if (dp == NULL) {
1.11      kristaps  260:                tbl_char(tp, ASCII_NBRSP, col->width);
1.1       kristaps  261:                return;
                    262:        }
                    263:
                    264:        switch (dp->pos) {
1.27      schwarze  265:        case TBL_DATA_NONE:
1.11      kristaps  266:                tbl_char(tp, ASCII_NBRSP, col->width);
1.10      kristaps  267:                return;
1.27      schwarze  268:        case TBL_DATA_HORIZ:
1.1       kristaps  269:                /* FALLTHROUGH */
1.27      schwarze  270:        case TBL_DATA_NHORIZ:
1.11      kristaps  271:                tbl_char(tp, '-', col->width);
1.7       kristaps  272:                return;
1.27      schwarze  273:        case TBL_DATA_NDHORIZ:
1.7       kristaps  274:                /* FALLTHROUGH */
1.27      schwarze  275:        case TBL_DATA_DHORIZ:
1.11      kristaps  276:                tbl_char(tp, '=', col->width);
1.1       kristaps  277:                return;
                    278:        default:
                    279:                break;
                    280:        }
1.27      schwarze  281:
1.16      kristaps  282:        switch (dp->layout->pos) {
1.27      schwarze  283:        case TBL_CELL_HORIZ:
1.11      kristaps  284:                tbl_char(tp, '-', col->width);
1.7       kristaps  285:                break;
1.27      schwarze  286:        case TBL_CELL_DHORIZ:
1.11      kristaps  287:                tbl_char(tp, '=', col->width);
1.1       kristaps  288:                break;
1.27      schwarze  289:        case TBL_CELL_LONG:
1.1       kristaps  290:                /* FALLTHROUGH */
1.27      schwarze  291:        case TBL_CELL_CENTRE:
1.1       kristaps  292:                /* FALLTHROUGH */
1.27      schwarze  293:        case TBL_CELL_LEFT:
1.1       kristaps  294:                /* FALLTHROUGH */
1.27      schwarze  295:        case TBL_CELL_RIGHT:
1.11      kristaps  296:                tbl_literal(tp, dp, col);
1.1       kristaps  297:                break;
1.27      schwarze  298:        case TBL_CELL_NUMBER:
1.25      schwarze  299:                tbl_number(tp, opts, dp, col);
1.18      kristaps  300:                break;
1.27      schwarze  301:        case TBL_CELL_DOWN:
1.18      kristaps  302:                tbl_char(tp, ASCII_NBRSP, col->width);
1.1       kristaps  303:                break;
                    304:        default:
                    305:                abort();
                    306:        }
                    307: }
                    308:
                    309: static void
1.12      kristaps  310: tbl_char(struct termp *tp, char c, size_t len)
1.1       kristaps  311: {
1.12      kristaps  312:        size_t          i, sz;
                    313:        char            cp[2];
                    314:
                    315:        cp[0] = c;
                    316:        cp[1] = '\0';
1.1       kristaps  317:
1.3       kristaps  318:        sz = term_strlen(tp, cp);
                    319:
                    320:        for (i = 0; i < len; i += sz)
1.1       kristaps  321:                term_word(tp, cp);
                    322: }
                    323:
                    324: static void
1.27      schwarze  325: tbl_literal(struct termp *tp, const struct tbl_dat *dp,
1.11      kristaps  326:                const struct roffcol *col)
1.1       kristaps  327: {
1.36      schwarze  328:        size_t           len, padl, padr, width;
                    329:        int              ic, spans;
1.1       kristaps  330:
1.17      kristaps  331:        assert(dp->string);
1.21      schwarze  332:        len = term_strlen(tp, dp->string);
1.23      schwarze  333:        width = col->width;
1.36      schwarze  334:        ic = dp->layout->col;
                    335:        spans = dp->spans;
                    336:        while (spans--)
                    337:                width += tp->tbl.cols[++ic].width + 3;
1.23      schwarze  338:
                    339:        padr = width > len ? width - len : 0;
1.21      schwarze  340:        padl = 0;
1.1       kristaps  341:
1.16      kristaps  342:        switch (dp->layout->pos) {
1.27      schwarze  343:        case TBL_CELL_LONG:
1.21      schwarze  344:                padl = term_len(tp, 1);
                    345:                padr = padr > padl ? padr - padl : 0;
1.1       kristaps  346:                break;
1.27      schwarze  347:        case TBL_CELL_CENTRE:
1.21      schwarze  348:                if (2 > padr)
1.19      schwarze  349:                        break;
1.21      schwarze  350:                padl = padr / 2;
1.19      schwarze  351:                padr -= padl;
1.1       kristaps  352:                break;
1.27      schwarze  353:        case TBL_CELL_RIGHT:
1.21      schwarze  354:                padl = padr;
                    355:                padr = 0;
1.1       kristaps  356:                break;
                    357:        default:
                    358:                break;
                    359:        }
                    360:
                    361:        tbl_char(tp, ASCII_NBRSP, padl);
1.29      schwarze  362:        tbl_word(tp, dp);
1.21      schwarze  363:        tbl_char(tp, ASCII_NBRSP, padr);
1.1       kristaps  364: }
                    365:
                    366: static void
1.25      schwarze  367: tbl_number(struct termp *tp, const struct tbl_opts *opts,
1.2       kristaps  368:                const struct tbl_dat *dp,
1.11      kristaps  369:                const struct roffcol *col)
1.1       kristaps  370: {
1.11      kristaps  371:        char            *cp;
                    372:        char             buf[2];
                    373:        size_t           sz, psz, ssz, d, padl;
                    374:        int              i;
1.1       kristaps  375:
                    376:        /*
                    377:         * See calc_data_number().  Left-pad by taking the offset of our
                    378:         * and the maximum decimal; right-pad by the remaining amount.
                    379:         */
                    380:
1.17      kristaps  381:        assert(dp->string);
1.2       kristaps  382:
1.17      kristaps  383:        sz = term_strlen(tp, dp->string);
1.2       kristaps  384:
1.25      schwarze  385:        buf[0] = opts->decimal;
1.11      kristaps  386:        buf[1] = '\0';
1.2       kristaps  387:
1.11      kristaps  388:        psz = term_strlen(tp, buf);
1.10      kristaps  389:
1.35      schwarze  390:        if ((cp = strrchr(dp->string, opts->decimal)) != NULL) {
1.17      kristaps  391:                for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
                    392:                        buf[0] = dp->string[i];
1.5       kristaps  393:                        ssz += term_strlen(tp, buf);
                    394:                }
                    395:                d = ssz + psz;
                    396:        } else
                    397:                d = sz + psz;
1.2       kristaps  398:
1.32      schwarze  399:        if (col->decimal > d && col->width > sz) {
                    400:                padl = col->decimal - d;
                    401:                if (padl + sz > col->width)
                    402:                        padl = col->width - sz;
                    403:                tbl_char(tp, ASCII_NBRSP, padl);
                    404:        } else
                    405:                padl = 0;
1.29      schwarze  406:        tbl_word(tp, dp);
1.21      schwarze  407:        if (col->width > sz + padl)
                    408:                tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);
1.2       kristaps  409: }
                    410:
1.29      schwarze  411: static void
                    412: tbl_word(struct termp *tp, const struct tbl_dat *dp)
                    413: {
1.38      schwarze  414:        int              prev_font;
1.29      schwarze  415:
1.38      schwarze  416:        prev_font = tp->fonti;
1.29      schwarze  417:        if (dp->layout->flags & TBL_CELL_BOLD)
                    418:                term_fontpush(tp, TERMFONT_BOLD);
                    419:        else if (dp->layout->flags & TBL_CELL_ITALIC)
                    420:                term_fontpush(tp, TERMFONT_UNDER);
                    421:
                    422:        term_word(tp, dp->string);
                    423:
                    424:        term_fontpopq(tp, prev_font);
                    425: }

CVSweb