[BACK]Return to tbl_term.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/tbl_term.c, Revision 1.25

1.25    ! schwarze    1: /*     $OpenBSD: tbl_term.c,v 1.24 2015/01/30 04:08:37 schwarze Exp $ */
1.1       schwarze    2: /*
1.9       schwarze    3:  * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.21      schwarze    4:  * Copyright (c) 2011, 2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
1.1       schwarze    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:  */
1.17      schwarze   18: #include <sys/types.h>
                     19:
1.1       schwarze   20: #include <assert.h>
                     21: #include <stdio.h>
                     22: #include <stdlib.h>
                     23: #include <string.h>
                     24:
1.5       schwarze   25: #include "mandoc.h"
1.2       schwarze   26: #include "out.h"
                     27: #include "term.h"
1.1       schwarze   28:
1.6       schwarze   29: static size_t  term_tbl_len(size_t, void *);
                     30: static size_t  term_tbl_strlen(const char *, void *);
                     31: static void    tbl_char(struct termp *, char, size_t);
1.14      schwarze   32: static void    tbl_data(struct termp *, const struct tbl_opts *,
1.16      schwarze   33:                        const struct tbl_dat *,
1.6       schwarze   34:                        const struct roffcol *);
1.16      schwarze   35: static void    tbl_literal(struct termp *, const struct tbl_dat *,
1.6       schwarze   36:                        const struct roffcol *);
1.16      schwarze   37: static void    tbl_number(struct termp *, const struct tbl_opts *,
                     38:                        const struct tbl_dat *,
1.6       schwarze   39:                        const struct roffcol *);
1.21      schwarze   40: static void    tbl_hrule(struct termp *, const struct tbl_span *, int);
1.17      schwarze   41: static void    tbl_word(struct termp *, const struct tbl_dat *);
1.1       schwarze   42:
1.6       schwarze   43:
                     44: static size_t
                     45: term_tbl_strlen(const char *p, void *arg)
                     46: {
                     47:
                     48:        return(term_strlen((const struct termp *)arg, p));
                     49: }
                     50:
                     51: static size_t
                     52: term_tbl_len(size_t sz, void *arg)
                     53: {
                     54:
                     55:        return(term_len((const struct termp *)arg, sz));
                     56: }
1.5       schwarze   57:
                     58: void
                     59: term_tbl(struct termp *tp, const struct tbl_span *sp)
                     60: {
1.21      schwarze   61:        const struct tbl_cell   *cp;
1.6       schwarze   62:        const struct tbl_dat    *dp;
1.22      schwarze   63:        static size_t            offset;
                     64:        size_t                   rmargin, maxrmargin, tsz;
1.24      schwarze   65:        int                      ic, horiz, spans, vert;
1.6       schwarze   66:
                     67:        rmargin = tp->rmargin;
                     68:        maxrmargin = tp->maxrmargin;
                     69:
                     70:        tp->rmargin = tp->maxrmargin = TERM_MAXMARGIN;
1.5       schwarze   71:
                     72:        /* Inhibit printing of spaces: we do padding ourselves. */
                     73:
                     74:        tp->flags |= TERMP_NONOSPACE;
                     75:        tp->flags |= TERMP_NOSPACE;
1.1       schwarze   76:
                     77:        /*
1.6       schwarze   78:         * The first time we're invoked for a given table block,
                     79:         * calculate the table widths and decimal positions.
1.1       schwarze   80:         */
                     81:
1.25    ! schwarze   82:        if (tp->tbl.cols == NULL) {
1.6       schwarze   83:                term_flushln(tp);
                     84:
                     85:                tp->tbl.len = term_tbl_len;
                     86:                tp->tbl.slen = term_tbl_strlen;
                     87:                tp->tbl.arg = tp;
1.5       schwarze   88:
1.18      schwarze   89:                tblcalc(&tp->tbl, sp, rmargin - tp->offset);
1.2       schwarze   90:
1.22      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.24      schwarze   97:                        for (ic = 0; ic < sp->opts->cols; ic++)
                     98:                                tsz += tp->tbl.cols[ic].width + 3;
1.22      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.21      schwarze  106:                /* Horizontal frame at the start of boxed tables. */
1.1       schwarze  107:
1.21      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.10      schwarze  112:        }
1.5       schwarze  113:
                    114:        /* Vertical frame at the start of each row. */
                    115:
1.21      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       schwarze  124:
                    125:        /*
1.5       schwarze  126:         * Now print the actual data itself depending on the span type.
1.24      schwarze  127:         * Match data cells to column numbers.
1.1       schwarze  128:         */
                    129:
1.21      schwarze  130:        if (sp->pos == TBL_SPAN_DATA) {
                    131:                cp = sp->layout->first;
1.5       schwarze  132:                dp = sp->first;
1.7       schwarze  133:                spans = 0;
1.24      schwarze  134:                for (ic = 0; ic < sp->opts->cols; ic++) {
1.11      schwarze  135:
1.16      schwarze  136:                        /*
1.21      schwarze  137:                         * Remeber whether we need a vertical bar
                    138:                         * after this cell.
1.7       schwarze  139:                         */
1.6       schwarze  140:
1.21      schwarze  141:                        vert = cp == NULL ? 0 : cp->vert;
1.10      schwarze  142:
1.21      schwarze  143:                        /*
                    144:                         * Print the data and advance to the next cell.
                    145:                         */
1.10      schwarze  146:
1.21      schwarze  147:                        if (spans == 0) {
1.24      schwarze  148:                                tbl_data(tp, sp->opts, dp, tp->tbl.cols + ic);
1.21      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.5       schwarze  157:
1.16      schwarze  158:                        /*
1.21      schwarze  159:                         * Separate columns, except in the middle
                    160:                         * of spans and after the last cell.
1.7       schwarze  161:                         */
                    162:
1.24      schwarze  163:                        if (ic + 1 == sp->opts->cols || spans)
1.21      schwarze  164:                                continue;
                    165:
                    166:                        tbl_char(tp, ASCII_NBRSP, 1);
                    167:                        if (vert > 0)
                    168:                                tbl_char(tp, '|', vert);
1.24      schwarze  169:                        if (vert < 2)
1.21      schwarze  170:                                tbl_char(tp, ASCII_NBRSP, 2 - vert);
1.1       schwarze  171:                }
1.21      schwarze  172:        } else if (horiz)
                    173:                tbl_hrule(tp, sp, 0);
1.1       schwarze  174:
1.10      schwarze  175:        /* Vertical frame at the end of each row. */
                    176:
1.21      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.5       schwarze  183:        term_flushln(tp);
1.1       schwarze  184:
1.5       schwarze  185:        /*
                    186:         * If we're the last row, clean up after ourselves: clear the
                    187:         * existing table configuration and set it to NULL.
                    188:         */
1.1       schwarze  189:
1.25    ! schwarze  190:        if (sp->next == NULL) {
1.21      schwarze  191:                if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
                    192:                        tbl_hrule(tp, sp, 1);
1.13      schwarze  193:                        tp->skipvsp = 1;
                    194:                }
1.21      schwarze  195:                if (sp->opts->opts & TBL_OPT_DBOX) {
                    196:                        tbl_hrule(tp, sp, 2);
1.13      schwarze  197:                        tp->skipvsp = 2;
                    198:                }
1.6       schwarze  199:                assert(tp->tbl.cols);
                    200:                free(tp->tbl.cols);
                    201:                tp->tbl.cols = NULL;
1.22      schwarze  202:                tp->offset = offset;
1.1       schwarze  203:        }
                    204:
1.5       schwarze  205:        tp->flags &= ~TERMP_NONOSPACE;
1.6       schwarze  206:        tp->rmargin = rmargin;
                    207:        tp->maxrmargin = maxrmargin;
1.1       schwarze  208: }
                    209:
1.10      schwarze  210: /*
1.21      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.10      schwarze  215:  */
1.1       schwarze  216: static void
1.21      schwarze  217: tbl_hrule(struct termp *tp, const struct tbl_span *sp, int kind)
1.1       schwarze  218: {
1.21      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.24      schwarze  233:                tbl_char(tp, line, tp->tbl.cols[c1->col].width + 1);
1.21      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.11      schwarze  246:        }
1.21      schwarze  247:        if (kind) {
                    248:                term_word(tp, "+");
                    249:                term_flushln(tp);
1.11      schwarze  250:        }
1.1       schwarze  251: }
                    252:
                    253: static void
1.14      schwarze  254: tbl_data(struct termp *tp, const struct tbl_opts *opts,
1.16      schwarze  255:        const struct tbl_dat *dp,
                    256:        const struct roffcol *col)
1.1       schwarze  257: {
                    258:
1.23      schwarze  259:        if (dp == NULL) {
1.6       schwarze  260:                tbl_char(tp, ASCII_NBRSP, col->width);
1.1       schwarze  261:                return;
1.5       schwarze  262:        }
1.1       schwarze  263:
1.5       schwarze  264:        switch (dp->pos) {
1.16      schwarze  265:        case TBL_DATA_NONE:
1.6       schwarze  266:                tbl_char(tp, ASCII_NBRSP, col->width);
1.5       schwarze  267:                return;
1.16      schwarze  268:        case TBL_DATA_HORIZ:
1.5       schwarze  269:                /* FALLTHROUGH */
1.16      schwarze  270:        case TBL_DATA_NHORIZ:
1.6       schwarze  271:                tbl_char(tp, '-', col->width);
1.5       schwarze  272:                return;
1.16      schwarze  273:        case TBL_DATA_NDHORIZ:
1.1       schwarze  274:                /* FALLTHROUGH */
1.16      schwarze  275:        case TBL_DATA_DHORIZ:
1.6       schwarze  276:                tbl_char(tp, '=', col->width);
1.5       schwarze  277:                return;
1.1       schwarze  278:        default:
                    279:                break;
                    280:        }
1.16      schwarze  281:
1.7       schwarze  282:        switch (dp->layout->pos) {
1.16      schwarze  283:        case TBL_CELL_HORIZ:
1.6       schwarze  284:                tbl_char(tp, '-', col->width);
1.5       schwarze  285:                break;
1.16      schwarze  286:        case TBL_CELL_DHORIZ:
1.6       schwarze  287:                tbl_char(tp, '=', col->width);
1.1       schwarze  288:                break;
1.16      schwarze  289:        case TBL_CELL_LONG:
1.1       schwarze  290:                /* FALLTHROUGH */
1.16      schwarze  291:        case TBL_CELL_CENTRE:
1.1       schwarze  292:                /* FALLTHROUGH */
1.16      schwarze  293:        case TBL_CELL_LEFT:
1.1       schwarze  294:                /* FALLTHROUGH */
1.16      schwarze  295:        case TBL_CELL_RIGHT:
1.6       schwarze  296:                tbl_literal(tp, dp, col);
1.1       schwarze  297:                break;
1.16      schwarze  298:        case TBL_CELL_NUMBER:
1.14      schwarze  299:                tbl_number(tp, opts, dp, col);
1.4       schwarze  300:                break;
1.16      schwarze  301:        case TBL_CELL_DOWN:
1.7       schwarze  302:                tbl_char(tp, ASCII_NBRSP, col->width);
                    303:                break;
1.1       schwarze  304:        default:
                    305:                abort();
                    306:                /* NOTREACHED */
                    307:        }
                    308: }
                    309:
                    310: static void
1.6       schwarze  311: tbl_char(struct termp *tp, char c, size_t len)
1.5       schwarze  312: {
1.6       schwarze  313:        size_t          i, sz;
1.5       schwarze  314:        char            cp[2];
1.1       schwarze  315:
1.5       schwarze  316:        cp[0] = c;
                    317:        cp[1] = '\0';
1.1       schwarze  318:
1.5       schwarze  319:        sz = term_strlen(tp, cp);
1.1       schwarze  320:
1.5       schwarze  321:        for (i = 0; i < len; i += sz)
                    322:                term_word(tp, cp);
1.1       schwarze  323: }
                    324:
                    325: static void
1.16      schwarze  326: tbl_literal(struct termp *tp, const struct tbl_dat *dp,
1.6       schwarze  327:                const struct roffcol *col)
1.1       schwarze  328: {
1.24      schwarze  329:        size_t           len, padl, padr, width;
                    330:        int              ic, spans;
1.1       schwarze  331:
1.7       schwarze  332:        assert(dp->string);
1.10      schwarze  333:        len = term_strlen(tp, dp->string);
1.12      schwarze  334:        width = col->width;
1.24      schwarze  335:        ic = dp->layout->col;
                    336:        spans = dp->spans;
                    337:        while (spans--)
                    338:                width += tp->tbl.cols[++ic].width + 3;
1.12      schwarze  339:
                    340:        padr = width > len ? width - len : 0;
1.10      schwarze  341:        padl = 0;
1.5       schwarze  342:
1.7       schwarze  343:        switch (dp->layout->pos) {
1.16      schwarze  344:        case TBL_CELL_LONG:
1.10      schwarze  345:                padl = term_len(tp, 1);
                    346:                padr = padr > padl ? padr - padl : 0;
1.1       schwarze  347:                break;
1.16      schwarze  348:        case TBL_CELL_CENTRE:
1.10      schwarze  349:                if (2 > padr)
1.8       schwarze  350:                        break;
1.10      schwarze  351:                padl = padr / 2;
1.8       schwarze  352:                padr -= padl;
1.1       schwarze  353:                break;
1.16      schwarze  354:        case TBL_CELL_RIGHT:
1.10      schwarze  355:                padl = padr;
                    356:                padr = 0;
1.1       schwarze  357:                break;
                    358:        default:
                    359:                break;
                    360:        }
                    361:
1.5       schwarze  362:        tbl_char(tp, ASCII_NBRSP, padl);
1.17      schwarze  363:        tbl_word(tp, dp);
1.10      schwarze  364:        tbl_char(tp, ASCII_NBRSP, padr);
1.1       schwarze  365: }
                    366:
1.5       schwarze  367: static void
1.14      schwarze  368: tbl_number(struct termp *tp, const struct tbl_opts *opts,
1.5       schwarze  369:                const struct tbl_dat *dp,
1.6       schwarze  370:                const struct roffcol *col)
1.5       schwarze  371: {
1.6       schwarze  372:        char            *cp;
                    373:        char             buf[2];
                    374:        size_t           sz, psz, ssz, d, padl;
                    375:        int              i;
1.5       schwarze  376:
                    377:        /*
                    378:         * See calc_data_number().  Left-pad by taking the offset of our
                    379:         * and the maximum decimal; right-pad by the remaining amount.
                    380:         */
                    381:
1.7       schwarze  382:        assert(dp->string);
1.5       schwarze  383:
1.7       schwarze  384:        sz = term_strlen(tp, dp->string);
1.5       schwarze  385:
1.14      schwarze  386:        buf[0] = opts->decimal;
1.6       schwarze  387:        buf[1] = '\0';
1.5       schwarze  388:
1.6       schwarze  389:        psz = term_strlen(tp, buf);
1.5       schwarze  390:
1.23      schwarze  391:        if ((cp = strrchr(dp->string, opts->decimal)) != NULL) {
1.7       schwarze  392:                for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
                    393:                        buf[0] = dp->string[i];
1.5       schwarze  394:                        ssz += term_strlen(tp, buf);
                    395:                }
                    396:                d = ssz + psz;
                    397:        } else
                    398:                d = sz + psz;
                    399:
1.20      schwarze  400:        if (col->decimal > d && col->width > sz) {
                    401:                padl = col->decimal - d;
                    402:                if (padl + sz > col->width)
                    403:                        padl = col->width - sz;
                    404:                tbl_char(tp, ASCII_NBRSP, padl);
                    405:        } else
                    406:                padl = 0;
1.17      schwarze  407:        tbl_word(tp, dp);
1.10      schwarze  408:        if (col->width > sz + padl)
                    409:                tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);
1.5       schwarze  410: }
1.1       schwarze  411:
1.17      schwarze  412: static void
                    413: tbl_word(struct termp *tp, const struct tbl_dat *dp)
                    414: {
                    415:        const void      *prev_font;
                    416:
                    417:        prev_font = term_fontq(tp);
                    418:        if (dp->layout->flags & TBL_CELL_BOLD)
                    419:                term_fontpush(tp, TERMFONT_BOLD);
                    420:        else if (dp->layout->flags & TBL_CELL_ITALIC)
                    421:                term_fontpush(tp, TERMFONT_UNDER);
                    422:
                    423:        term_word(tp, dp->string);
                    424:
                    425:        term_fontpopq(tp, prev_font);
                    426: }