[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.8

1.8     ! schwarze    1: /*     $Id: tbl_term.c,v 1.7 2011/01/16 01:11:50 schwarze Exp $ */
1.1       schwarze    2: /*
1.5       schwarze    3:  * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@kth.se>
1.8     ! schwarze    4:  * Copyright (c) 2011 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:  */
                     18: #include <assert.h>
                     19: #include <stdio.h>
                     20: #include <stdlib.h>
                     21: #include <string.h>
                     22:
1.5       schwarze   23: #include "mandoc.h"
1.2       schwarze   24: #include "out.h"
                     25: #include "term.h"
1.1       schwarze   26:
1.6       schwarze   27: static size_t  term_tbl_len(size_t, void *);
                     28: static size_t  term_tbl_strlen(const char *, void *);
                     29: static void    tbl_char(struct termp *, char, size_t);
                     30: static void    tbl_data(struct termp *, const struct tbl *,
                     31:                        const struct tbl_dat *,
                     32:                        const struct roffcol *);
                     33: static void    tbl_hframe(struct termp *, const struct tbl_span *);
                     34: static void    tbl_literal(struct termp *, const struct tbl_dat *,
                     35:                        const struct roffcol *);
                     36: static void    tbl_number(struct termp *, const struct tbl *,
                     37:                        const struct tbl_dat *,
                     38:                        const struct roffcol *);
                     39: static void    tbl_hrule(struct termp *, const struct tbl_span *);
                     40: static void    tbl_vframe(struct termp *, const struct tbl *);
                     41: static void    tbl_vrule(struct termp *, const struct tbl_head *);
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.6       schwarze   61:        const struct tbl_head   *hp;
                     62:        const struct tbl_dat    *dp;
                     63:        struct roffcol          *col;
1.7       schwarze   64:        int                      spans;
1.6       schwarze   65:        size_t                   rmargin, maxrmargin;
                     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.5       schwarze   82:        if (TBL_SPAN_FIRST & sp->flags) {
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.6       schwarze   89:                tblcalc(&tp->tbl, sp);
1.5       schwarze   90:        }
1.2       schwarze   91:
1.5       schwarze   92:        /* Horizontal frame at the start of boxed tables. */
1.1       schwarze   93:
1.5       schwarze   94:        if (TBL_SPAN_FIRST & sp->flags)
                     95:                tbl_hframe(tp, sp);
                     96:
                     97:        /* Vertical frame at the start of each row. */
                     98:
                     99:        tbl_vframe(tp, sp->tbl);
1.1       schwarze  100:
                    101:        /*
1.5       schwarze  102:         * Now print the actual data itself depending on the span type.
                    103:         * Spanner spans get a horizontal rule; data spanners have their
                    104:         * data printed by matching data to header.
1.1       schwarze  105:         */
                    106:
1.5       schwarze  107:        switch (sp->pos) {
                    108:        case (TBL_SPAN_HORIZ):
                    109:                /* FALLTHROUGH */
                    110:        case (TBL_SPAN_DHORIZ):
                    111:                tbl_hrule(tp, sp);
                    112:                break;
                    113:        case (TBL_SPAN_DATA):
                    114:                /* Iterate over template headers. */
                    115:                dp = sp->first;
1.7       schwarze  116:                spans = 0;
1.5       schwarze  117:                for (hp = sp->head; hp; hp = hp->next) {
1.7       schwarze  118:                        /*
                    119:                         * If the current data header is invoked during
                    120:                         * a spanner ("spans" > 0), don't emit anything
                    121:                         * at all.
                    122:                         */
1.5       schwarze  123:                        switch (hp->pos) {
1.1       schwarze  124:                        case (TBL_HEAD_VERT):
                    125:                                /* FALLTHROUGH */
                    126:                        case (TBL_HEAD_DVERT):
1.7       schwarze  127:                                if (spans <= 0)
                    128:                                        tbl_vrule(tp, hp);
1.5       schwarze  129:                                continue;
1.1       schwarze  130:                        case (TBL_HEAD_DATA):
                    131:                                break;
                    132:                        }
1.6       schwarze  133:
1.7       schwarze  134:                        if (--spans >= 0)
                    135:                                continue;
                    136:
1.6       schwarze  137:                        col = &tp->tbl.cols[hp->ident];
                    138:                        tbl_data(tp, sp->tbl, dp, col);
1.5       schwarze  139:
1.7       schwarze  140:                        /*
                    141:                         * Go to the next data cell and assign the
                    142:                         * number of subsequent spans, if applicable.
                    143:                         */
                    144:
                    145:                        if (dp) {
                    146:                                spans = dp->spans;
1.5       schwarze  147:                                dp = dp->next;
1.7       schwarze  148:                        }
1.1       schwarze  149:                }
1.5       schwarze  150:                break;
1.1       schwarze  151:        }
                    152:
1.5       schwarze  153:        tbl_vframe(tp, sp->tbl);
                    154:        term_flushln(tp);
1.1       schwarze  155:
1.5       schwarze  156:        /*
                    157:         * If we're the last row, clean up after ourselves: clear the
                    158:         * existing table configuration and set it to NULL.
                    159:         */
1.1       schwarze  160:
1.5       schwarze  161:        if (TBL_SPAN_LAST & sp->flags) {
                    162:                tbl_hframe(tp, sp);
1.6       schwarze  163:                assert(tp->tbl.cols);
                    164:                free(tp->tbl.cols);
                    165:                tp->tbl.cols = NULL;
1.1       schwarze  166:        }
                    167:
1.5       schwarze  168:        tp->flags &= ~TERMP_NONOSPACE;
1.6       schwarze  169:        tp->rmargin = rmargin;
                    170:        tp->maxrmargin = maxrmargin;
1.1       schwarze  171:
                    172: }
                    173:
                    174: static void
1.5       schwarze  175: tbl_hrule(struct termp *tp, const struct tbl_span *sp)
1.1       schwarze  176: {
1.5       schwarze  177:        const struct tbl_head *hp;
                    178:        char             c;
1.6       schwarze  179:        size_t           width;
1.1       schwarze  180:
                    181:        /*
                    182:         * An hrule extends across the entire table and is demarked by a
                    183:         * standalone `_' or whatnot in lieu of a table row.  Spanning
                    184:         * headers are marked by a `+', as are table boundaries.
                    185:         */
                    186:
                    187:        c = '-';
1.5       schwarze  188:        if (TBL_SPAN_DHORIZ == sp->pos)
1.1       schwarze  189:                c = '=';
                    190:
                    191:        /* FIXME: don't use `+' between data and a spanner! */
                    192:
1.5       schwarze  193:        for (hp = sp->head; hp; hp = hp->next) {
1.6       schwarze  194:                width = tp->tbl.cols[hp->ident].width;
1.5       schwarze  195:                switch (hp->pos) {
1.1       schwarze  196:                case (TBL_HEAD_DATA):
1.8     ! schwarze  197:                        if (hp->next)
        !           198:                                width += 2;
1.5       schwarze  199:                        tbl_char(tp, c, width);
1.1       schwarze  200:                        break;
                    201:                case (TBL_HEAD_DVERT):
1.5       schwarze  202:                        tbl_char(tp, '+', width);
1.1       schwarze  203:                        /* FALLTHROUGH */
                    204:                case (TBL_HEAD_VERT):
1.5       schwarze  205:                        tbl_char(tp, '+', width);
1.1       schwarze  206:                        break;
                    207:                default:
                    208:                        abort();
                    209:                        /* NOTREACHED */
                    210:                }
                    211:        }
                    212: }
                    213:
                    214: static void
1.5       schwarze  215: tbl_hframe(struct termp *tp, const struct tbl_span *sp)
1.1       schwarze  216: {
1.5       schwarze  217:        const struct tbl_head *hp;
1.6       schwarze  218:        size_t           width;
1.1       schwarze  219:
1.5       schwarze  220:        if ( ! (TBL_OPT_BOX & sp->tbl->opts ||
                    221:                        TBL_OPT_DBOX & sp->tbl->opts))
1.1       schwarze  222:                return;
                    223:
                    224:        /*
                    225:         * Print out the horizontal part of a frame or double frame.  A
                    226:         * double frame has an unbroken `-' outer line the width of the
                    227:         * table, bordered by `+'.  The frame (or inner frame, in the
                    228:         * case of the double frame) is a `-' bordered by `+' and broken
                    229:         * by `+' whenever a span is encountered.
                    230:         */
                    231:
1.5       schwarze  232:        if (TBL_OPT_DBOX & sp->tbl->opts) {
                    233:                term_word(tp, "+");
                    234:                for (hp = sp->head; hp; hp = hp->next) {
1.6       schwarze  235:                        width = tp->tbl.cols[hp->ident].width;
1.5       schwarze  236:                        tbl_char(tp, '-', width);
                    237:                }
                    238:                term_word(tp, "+");
                    239:                term_flushln(tp);
1.1       schwarze  240:        }
                    241:
1.5       schwarze  242:        term_word(tp, "+");
                    243:        for (hp = sp->head; hp; hp = hp->next) {
1.6       schwarze  244:                width = tp->tbl.cols[hp->ident].width;
1.5       schwarze  245:                switch (hp->pos) {
1.1       schwarze  246:                case (TBL_HEAD_DATA):
1.5       schwarze  247:                        tbl_char(tp, '-', width);
1.1       schwarze  248:                        break;
                    249:                default:
1.5       schwarze  250:                        tbl_char(tp, '+', width);
1.1       schwarze  251:                        break;
                    252:                }
                    253:        }
1.5       schwarze  254:        term_word(tp, "+");
                    255:        term_flushln(tp);
1.1       schwarze  256: }
                    257:
                    258: static void
1.5       schwarze  259: tbl_data(struct termp *tp, const struct tbl *tbl,
                    260:                const struct tbl_dat *dp,
1.6       schwarze  261:                const struct roffcol *col)
1.1       schwarze  262: {
                    263:
1.5       schwarze  264:        if (NULL == dp) {
1.6       schwarze  265:                tbl_char(tp, ASCII_NBRSP, col->width);
1.1       schwarze  266:                return;
1.5       schwarze  267:        }
1.7       schwarze  268:        assert(dp->layout);
1.1       schwarze  269:
1.5       schwarze  270:        switch (dp->pos) {
                    271:        case (TBL_DATA_NONE):
1.6       schwarze  272:                tbl_char(tp, ASCII_NBRSP, col->width);
1.5       schwarze  273:                return;
                    274:        case (TBL_DATA_HORIZ):
                    275:                /* FALLTHROUGH */
                    276:        case (TBL_DATA_NHORIZ):
1.6       schwarze  277:                tbl_char(tp, '-', col->width);
1.5       schwarze  278:                return;
                    279:        case (TBL_DATA_NDHORIZ):
1.1       schwarze  280:                /* FALLTHROUGH */
1.5       schwarze  281:        case (TBL_DATA_DHORIZ):
1.6       schwarze  282:                tbl_char(tp, '=', col->width);
1.5       schwarze  283:                return;
1.1       schwarze  284:        default:
                    285:                break;
                    286:        }
1.5       schwarze  287:
1.7       schwarze  288:        switch (dp->layout->pos) {
1.1       schwarze  289:        case (TBL_CELL_HORIZ):
1.6       schwarze  290:                tbl_char(tp, '-', col->width);
1.5       schwarze  291:                break;
1.1       schwarze  292:        case (TBL_CELL_DHORIZ):
1.6       schwarze  293:                tbl_char(tp, '=', col->width);
1.1       schwarze  294:                break;
                    295:        case (TBL_CELL_LONG):
                    296:                /* FALLTHROUGH */
                    297:        case (TBL_CELL_CENTRE):
                    298:                /* FALLTHROUGH */
                    299:        case (TBL_CELL_LEFT):
                    300:                /* FALLTHROUGH */
                    301:        case (TBL_CELL_RIGHT):
1.6       schwarze  302:                tbl_literal(tp, dp, col);
1.1       schwarze  303:                break;
                    304:        case (TBL_CELL_NUMBER):
1.6       schwarze  305:                tbl_number(tp, tbl, dp, col);
1.4       schwarze  306:                break;
1.7       schwarze  307:        case (TBL_CELL_DOWN):
                    308:                tbl_char(tp, ASCII_NBRSP, col->width);
                    309:                break;
1.1       schwarze  310:        default:
                    311:                abort();
                    312:                /* NOTREACHED */
                    313:        }
                    314: }
1.6       schwarze  315:
1.1       schwarze  316: static void
1.6       schwarze  317: tbl_vrule(struct termp *tp, const struct tbl_head *hp)
1.1       schwarze  318: {
                    319:
1.5       schwarze  320:        switch (hp->pos) {
                    321:        case (TBL_HEAD_VERT):
                    322:                term_word(tp, "|");
                    323:                break;
                    324:        case (TBL_HEAD_DVERT):
                    325:                term_word(tp, "||");
                    326:                break;
                    327:        default:
                    328:                break;
                    329:        }
1.1       schwarze  330: }
                    331:
                    332: static void
1.5       schwarze  333: tbl_vframe(struct termp *tp, const struct tbl *tbl)
1.1       schwarze  334: {
                    335:
1.5       schwarze  336:        if (TBL_OPT_BOX & tbl->opts || TBL_OPT_DBOX & tbl->opts)
                    337:                term_word(tp, "|");
                    338: }
1.1       schwarze  339:
1.6       schwarze  340: static void
                    341: tbl_char(struct termp *tp, char c, size_t len)
1.5       schwarze  342: {
1.6       schwarze  343:        size_t          i, sz;
1.5       schwarze  344:        char            cp[2];
1.1       schwarze  345:
1.5       schwarze  346:        cp[0] = c;
                    347:        cp[1] = '\0';
1.1       schwarze  348:
1.5       schwarze  349:        sz = term_strlen(tp, cp);
1.1       schwarze  350:
1.5       schwarze  351:        for (i = 0; i < len; i += sz)
                    352:                term_word(tp, cp);
1.1       schwarze  353: }
                    354:
                    355: static void
1.6       schwarze  356: tbl_literal(struct termp *tp, const struct tbl_dat *dp,
                    357:                const struct roffcol *col)
1.1       schwarze  358: {
1.6       schwarze  359:        size_t           padl, padr, ssz;
1.1       schwarze  360:
                    361:        padl = padr = 0;
                    362:
1.7       schwarze  363:        assert(dp->string);
1.6       schwarze  364:
1.5       schwarze  365:        ssz = term_len(tp, 1);
                    366:
1.7       schwarze  367:        switch (dp->layout->pos) {
1.1       schwarze  368:        case (TBL_CELL_LONG):
1.5       schwarze  369:                padl = ssz;
1.7       schwarze  370:                padr = col->width - term_strlen(tp, dp->string) - ssz;
1.1       schwarze  371:                break;
                    372:        case (TBL_CELL_CENTRE):
1.8     ! schwarze  373:                padr = col->width - term_strlen(tp, dp->string);
        !           374:                if (3 > padr)
        !           375:                        break;
        !           376:                padl = (padr - 1) / 2;
        !           377:                padr -= padl;
1.1       schwarze  378:                break;
                    379:        case (TBL_CELL_RIGHT):
1.7       schwarze  380:                padl = col->width - term_strlen(tp, dp->string);
1.1       schwarze  381:                break;
                    382:        default:
1.7       schwarze  383:                padr = col->width - term_strlen(tp, dp->string);
1.1       schwarze  384:                break;
                    385:        }
                    386:
1.5       schwarze  387:        tbl_char(tp, ASCII_NBRSP, padl);
1.7       schwarze  388:        term_word(tp, dp->string);
1.8     ! schwarze  389:        tbl_char(tp, ASCII_NBRSP, padr + 2);
1.1       schwarze  390: }
                    391:
1.5       schwarze  392: static void
1.6       schwarze  393: tbl_number(struct termp *tp, const struct tbl *tbl,
1.5       schwarze  394:                const struct tbl_dat *dp,
1.6       schwarze  395:                const struct roffcol *col)
1.5       schwarze  396: {
1.6       schwarze  397:        char            *cp;
                    398:        char             buf[2];
                    399:        size_t           sz, psz, ssz, d, padl;
                    400:        int              i;
1.5       schwarze  401:
                    402:        /*
                    403:         * See calc_data_number().  Left-pad by taking the offset of our
                    404:         * and the maximum decimal; right-pad by the remaining amount.
                    405:         */
                    406:
1.7       schwarze  407:        assert(dp->string);
1.5       schwarze  408:
1.7       schwarze  409:        sz = term_strlen(tp, dp->string);
1.5       schwarze  410:
1.6       schwarze  411:        buf[0] = tbl->decimal;
                    412:        buf[1] = '\0';
1.5       schwarze  413:
1.6       schwarze  414:        psz = term_strlen(tp, buf);
1.5       schwarze  415:
1.7       schwarze  416:        if (NULL != (cp = strrchr(dp->string, tbl->decimal))) {
1.5       schwarze  417:                buf[1] = '\0';
1.7       schwarze  418:                for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
                    419:                        buf[0] = dp->string[i];
1.5       schwarze  420:                        ssz += term_strlen(tp, buf);
                    421:                }
                    422:                d = ssz + psz;
                    423:        } else
                    424:                d = sz + psz;
                    425:
                    426:        sz += term_len(tp, 2);
1.6       schwarze  427:        d += term_len(tp, 1);
1.5       schwarze  428:
1.6       schwarze  429:        padl = col->decimal - d;
1.5       schwarze  430:
1.6       schwarze  431:        tbl_char(tp, ASCII_NBRSP, padl);
1.7       schwarze  432:        term_word(tp, dp->string);
1.6       schwarze  433:        tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);
1.5       schwarze  434: }
1.1       schwarze  435: