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

Annotation of mandoc/tbl_term.c, Revision 1.59

1.59    ! schwarze    1: /*     $Id: tbl_term.c,v 1.58 2018/08/18 16:44:55 schwarze Exp $ */
1.1       kristaps    2: /*
1.20      kristaps    3:  * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.58      schwarze    4:  * Copyright (c) 2011-2018 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.53      schwarze   31: #define        IS_HORIZ(cp)    ((cp)->pos == TBL_CELL_HORIZ || \
                     32:                         (cp)->pos == TBL_CELL_DHORIZ)
                     33:
1.11      kristaps   34: static size_t  term_tbl_len(size_t, void *);
                     35: static size_t  term_tbl_strlen(const char *, void *);
1.46      schwarze   36: static size_t  term_tbl_sulen(const struct roffsu *, void *);
1.12      kristaps   37: static void    tbl_char(struct termp *, char, size_t);
1.25      schwarze   38: static void    tbl_data(struct termp *, const struct tbl_opts *,
1.53      schwarze   39:                        const struct tbl_cell *,
1.27      schwarze   40:                        const struct tbl_dat *,
1.11      kristaps   41:                        const struct roffcol *);
1.27      schwarze   42: static void    tbl_literal(struct termp *, const struct tbl_dat *,
1.11      kristaps   43:                        const struct roffcol *);
1.27      schwarze   44: static void    tbl_number(struct termp *, const struct tbl_opts *,
                     45:                        const struct tbl_dat *,
1.11      kristaps   46:                        const struct roffcol *);
1.33      schwarze   47: static void    tbl_hrule(struct termp *, const struct tbl_span *, int);
1.29      schwarze   48: static void    tbl_word(struct termp *, const struct tbl_dat *);
1.11      kristaps   49:
                     50:
                     51: static size_t
1.46      schwarze   52: term_tbl_sulen(const struct roffsu *su, void *arg)
                     53: {
1.57      schwarze   54:        int      i;
                     55:
                     56:        i = term_hen((const struct termp *)arg, su);
                     57:        return i > 0 ? i : 0;
1.46      schwarze   58: }
                     59:
                     60: static size_t
1.11      kristaps   61: term_tbl_strlen(const char *p, void *arg)
                     62: {
1.42      schwarze   63:        return term_strlen((const struct termp *)arg, p);
1.11      kristaps   64: }
                     65:
                     66: static size_t
                     67: term_tbl_len(size_t sz, void *arg)
                     68: {
1.42      schwarze   69:        return term_len((const struct termp *)arg, sz);
1.11      kristaps   70: }
1.1       kristaps   71:
                     72: void
                     73: term_tbl(struct termp *tp, const struct tbl_span *sp)
                     74: {
1.53      schwarze   75:        const struct tbl_cell   *cp, *cpn, *cpp;
1.11      kristaps   76:        const struct tbl_dat    *dp;
1.34      schwarze   77:        static size_t            offset;
1.47      schwarze   78:        size_t                   coloff, tsz;
                     79:        int                      ic, horiz, spans, vert, more;
                     80:        char                     fc;
1.39      schwarze   81:
1.4       kristaps   82:        /* Inhibit printing of spaces: we do padding ourselves. */
                     83:
1.47      schwarze   84:        tp->flags |= TERMP_NOSPACE | TERMP_NONOSPACE;
1.4       kristaps   85:
                     86:        /*
1.11      kristaps   87:         * The first time we're invoked for a given table block,
                     88:         * calculate the table widths and decimal positions.
1.4       kristaps   89:         */
                     90:
1.37      schwarze   91:        if (tp->tbl.cols == NULL) {
1.11      kristaps   92:                tp->tbl.len = term_tbl_len;
                     93:                tp->tbl.slen = term_tbl_strlen;
1.46      schwarze   94:                tp->tbl.sulen = term_tbl_sulen;
1.11      kristaps   95:                tp->tbl.arg = tp;
1.4       kristaps   96:
1.48      schwarze   97:                tblcalc(&tp->tbl, sp, tp->tcol->offset, tp->tcol->rmargin);
1.54      schwarze   98:
                     99:                /* Tables leak .ta settings to subsequent text. */
                    100:
                    101:                term_tab_set(tp, NULL);
                    102:                coloff = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
                    103:                    sp->opts->lvert;
                    104:                for (ic = 0; ic < sp->opts->cols; ic++) {
                    105:                        coloff += tp->tbl.cols[ic].width;
                    106:                        term_tab_iset(coloff);
1.55      schwarze  107:                        coloff += tp->tbl.cols[ic].spacing;
1.54      schwarze  108:                }
1.1       kristaps  109:
1.34      schwarze  110:                /* Center the table as a whole. */
                    111:
1.45      schwarze  112:                offset = tp->tcol->offset;
1.34      schwarze  113:                if (sp->opts->opts & TBL_OPT_CENTRE) {
                    114:                        tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)
                    115:                            ? 2 : !!sp->opts->lvert + !!sp->opts->rvert;
1.55      schwarze  116:                        for (ic = 0; ic + 1 < sp->opts->cols; ic++)
                    117:                                tsz += tp->tbl.cols[ic].width +
                    118:                                    tp->tbl.cols[ic].spacing;
                    119:                        if (sp->opts->cols)
                    120:                                tsz += tp->tbl.cols[sp->opts->cols - 1].width;
1.45      schwarze  121:                        if (offset + tsz > tp->tcol->rmargin)
1.34      schwarze  122:                                tsz -= 1;
1.45      schwarze  123:                        tp->tcol->offset = offset + tp->tcol->rmargin > tsz ?
                    124:                            (offset + tp->tcol->rmargin - tsz) / 2 : 0;
1.34      schwarze  125:                }
                    126:
1.33      schwarze  127:                /* Horizontal frame at the start of boxed tables. */
1.4       kristaps  128:
1.33      schwarze  129:                if (sp->opts->opts & TBL_OPT_DBOX)
1.53      schwarze  130:                        tbl_hrule(tp, sp, 3);
                    131:                if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX))
1.33      schwarze  132:                        tbl_hrule(tp, sp, 2);
1.21      schwarze  133:        }
1.1       kristaps  134:
1.47      schwarze  135:        /* Set up the columns. */
1.1       kristaps  136:
1.47      schwarze  137:        tp->flags |= TERMP_MULTICOL;
                    138:        horiz = 0;
                    139:        switch (sp->pos) {
                    140:        case TBL_SPAN_HORIZ:
                    141:        case TBL_SPAN_DHORIZ:
                    142:                horiz = 1;
                    143:                term_setcol(tp, 1);
                    144:                break;
                    145:        case TBL_SPAN_DATA:
                    146:                term_setcol(tp, sp->opts->cols + 2);
                    147:                coloff = tp->tcol->offset;
                    148:
                    149:                /* Set up a column for a left vertical frame. */
                    150:
                    151:                if (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
                    152:                    sp->opts->lvert)
                    153:                        coloff++;
                    154:                tp->tcol->rmargin = coloff;
1.33      schwarze  155:
1.47      schwarze  156:                /* Set up the data columns. */
1.1       kristaps  157:
1.47      schwarze  158:                dp = sp->first;
                    159:                spans = 0;
                    160:                for (ic = 0; ic < sp->opts->cols; ic++) {
                    161:                        if (spans == 0) {
                    162:                                tp->tcol++;
                    163:                                tp->tcol->offset = coloff;
                    164:                        }
                    165:                        coloff += tp->tbl.cols[ic].width;
                    166:                        tp->tcol->rmargin = coloff;
                    167:                        if (ic + 1 < sp->opts->cols)
1.55      schwarze  168:                                coloff += tp->tbl.cols[ic].spacing;
1.47      schwarze  169:                        if (spans) {
                    170:                                spans--;
                    171:                                continue;
                    172:                        }
                    173:                        if (dp == NULL)
                    174:                                continue;
                    175:                        spans = dp->spans;
1.56      schwarze  176:                        if (ic || sp->layout->first->pos != TBL_CELL_SPAN)
                    177:                                dp = dp->next;
1.47      schwarze  178:                }
                    179:
                    180:                /* Set up a column for a right vertical frame. */
                    181:
                    182:                tp->tcol++;
1.55      schwarze  183:                tp->tcol->offset = coloff + 1;
1.53      schwarze  184:                tp->tcol->rmargin = tp->maxrmargin;
1.47      schwarze  185:
                    186:                /* Spans may have reduced the number of columns. */
                    187:
                    188:                tp->lasttcol = tp->tcol - tp->tcols;
                    189:
                    190:                /* Fill the buffers for all data columns. */
1.4       kristaps  191:
1.47      schwarze  192:                tp->tcol = tp->tcols;
1.53      schwarze  193:                cp = cpn = sp->layout->first;
1.4       kristaps  194:                dp = sp->first;
1.16      kristaps  195:                spans = 0;
1.36      schwarze  196:                for (ic = 0; ic < sp->opts->cols; ic++) {
1.53      schwarze  197:                        if (cpn != NULL) {
                    198:                                cp = cpn;
                    199:                                cpn = cpn->next;
                    200:                        }
1.47      schwarze  201:                        if (spans) {
                    202:                                spans--;
                    203:                                continue;
                    204:                        }
                    205:                        tp->tcol++;
                    206:                        tp->col = 0;
1.53      schwarze  207:                        tbl_data(tp, sp->opts, cp, dp, tp->tbl.cols + ic);
1.47      schwarze  208:                        if (dp == NULL)
                    209:                                continue;
                    210:                        spans = dp->spans;
1.56      schwarze  211:                        if (cp->pos != TBL_CELL_SPAN)
                    212:                                dp = dp->next;
1.47      schwarze  213:                }
                    214:                break;
                    215:        }
                    216:
                    217:        do {
                    218:                /* Print the vertical frame at the start of each row. */
                    219:
                    220:                tp->tcol = tp->tcols;
                    221:                fc = '\0';
                    222:                if (sp->layout->vert ||
1.53      schwarze  223:                    (sp->next != NULL && sp->next->layout->vert &&
                    224:                     sp->next->pos == TBL_SPAN_DATA) ||
                    225:                    (sp->prev != NULL && sp->prev->layout->vert &&
                    226:                     (horiz || (IS_HORIZ(sp->layout->first) &&
                    227:                       !IS_HORIZ(sp->prev->layout->first)))) ||
1.47      schwarze  228:                    sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX))
1.53      schwarze  229:                        fc = horiz || IS_HORIZ(sp->layout->first) ? '+' : '|';
1.47      schwarze  230:                else if (horiz && sp->opts->lvert)
                    231:                        fc = '-';
                    232:                if (fc != '\0') {
                    233:                        (*tp->advance)(tp, tp->tcols->offset);
                    234:                        (*tp->letter)(tp, fc);
                    235:                        tp->viscol = tp->tcol->offset + 1;
                    236:                }
1.22      schwarze  237:
1.47      schwarze  238:                /* Print the data cells. */
1.21      schwarze  239:
1.47      schwarze  240:                more = 0;
                    241:                if (horiz) {
                    242:                        tbl_hrule(tp, sp, 0);
                    243:                        term_flushln(tp);
                    244:                } else {
                    245:                        cp = sp->layout->first;
1.53      schwarze  246:                        cpn = sp->next == NULL ? NULL :
                    247:                            sp->next->layout->first;
                    248:                        cpp = sp->prev == NULL ? NULL :
                    249:                            sp->prev->layout->first;
1.47      schwarze  250:                        dp = sp->first;
                    251:                        spans = 0;
                    252:                        for (ic = 0; ic < sp->opts->cols; ic++) {
                    253:
1.53      schwarze  254:                                /*
                    255:                                 * Figure out whether to print a
                    256:                                 * vertical line after this cell
                    257:                                 * and advance to next layout cell.
                    258:                                 */
1.47      schwarze  259:
                    260:                                if (cp != NULL) {
                    261:                                        vert = cp->vert;
1.53      schwarze  262:                                        switch (cp->pos) {
                    263:                                        case TBL_CELL_HORIZ:
                    264:                                                fc = '-';
                    265:                                                break;
                    266:                                        case TBL_CELL_DHORIZ:
                    267:                                                fc = '=';
                    268:                                                break;
                    269:                                        default:
                    270:                                                fc = ' ';
                    271:                                                break;
                    272:                                        }
                    273:                                } else {
                    274:                                        vert = 0;
                    275:                                        fc = ' ';
                    276:                                }
                    277:                                if (cpp != NULL) {
                    278:                                        if (vert == 0 &&
                    279:                                            cp != NULL &&
                    280:                                            ((IS_HORIZ(cp) &&
                    281:                                              !IS_HORIZ(cpp)) ||
                    282:                                             (cp->next != NULL &&
                    283:                                              cpp->next != NULL &&
                    284:                                              IS_HORIZ(cp->next) &&
                    285:                                              !IS_HORIZ(cpp->next))))
                    286:                                                vert = cpp->vert;
                    287:                                        cpp = cpp->next;
                    288:                                }
                    289:                                if (vert == 0 &&
                    290:                                    sp->opts->opts & TBL_OPT_ALLBOX)
                    291:                                        vert = 1;
                    292:                                if (cpn != NULL) {
                    293:                                        if (vert == 0)
                    294:                                                vert = cpn->vert;
                    295:                                        cpn = cpn->next;
                    296:                                }
                    297:                                if (cp != NULL)
1.47      schwarze  298:                                        cp = cp->next;
1.51      schwarze  299:
1.53      schwarze  300:                                /*
                    301:                                 * Skip later cells in a span,
                    302:                                 * figure out whether to start a span,
                    303:                                 * and advance to next data cell.
                    304:                                 */
1.51      schwarze  305:
                    306:                                if (spans) {
                    307:                                        spans--;
                    308:                                        continue;
                    309:                                }
                    310:                                if (dp != NULL) {
                    311:                                        spans = dp->spans;
1.56      schwarze  312:                                        if (ic || sp->layout->first->pos
                    313:                                            != TBL_CELL_SPAN)
                    314:                                                dp = dp->next;
1.51      schwarze  315:                                }
                    316:
1.53      schwarze  317:                                /*
                    318:                                 * Print one line of text in the cell
                    319:                                 * and remember whether there is more.
                    320:                                 */
1.51      schwarze  321:
                    322:                                tp->tcol++;
                    323:                                if (tp->tcol->col < tp->tcol->lastcol)
                    324:                                        term_flushln(tp);
                    325:                                if (tp->tcol->col < tp->tcol->lastcol)
                    326:                                        more = 1;
                    327:
                    328:                                /*
                    329:                                 * Vertical frames between data cells,
                    330:                                 * but not after the last column.
                    331:                                 */
                    332:
1.53      schwarze  333:                                if (fc == ' ' && ((vert == 0 &&
                    334:                                     (cp == NULL || !IS_HORIZ(cp))) ||
                    335:                                    tp->tcol + 1 == tp->tcols + tp->lasttcol))
                    336:                                        continue;
                    337:
1.55      schwarze  338:                                if (tp->viscol < tp->tcol->rmargin) {
1.53      schwarze  339:                                        (*tp->advance)(tp, tp->tcol->rmargin
                    340:                                           - tp->viscol);
                    341:                                        tp->viscol = tp->tcol->rmargin;
                    342:                                }
1.55      schwarze  343:                                while (tp->viscol < tp->tcol->rmargin +
                    344:                                    tp->tbl.cols[ic].spacing / 2) {
1.53      schwarze  345:                                        (*tp->letter)(tp, fc);
                    346:                                        tp->viscol++;
                    347:                                }
                    348:
1.51      schwarze  349:                                if (tp->tcol + 1 == tp->tcols + tp->lasttcol)
                    350:                                        continue;
1.47      schwarze  351:
1.53      schwarze  352:                                if (fc == ' ' && cp != NULL) {
                    353:                                        switch (cp->pos) {
                    354:                                        case TBL_CELL_HORIZ:
                    355:                                                fc = '-';
                    356:                                                break;
                    357:                                        case TBL_CELL_DHORIZ:
                    358:                                                fc = '=';
                    359:                                                break;
                    360:                                        default:
                    361:                                                break;
                    362:                                        }
                    363:                                }
1.55      schwarze  364:                                if (tp->tbl.cols[ic].spacing) {
                    365:                                        (*tp->letter)(tp, fc == ' ' ? '|' :
                    366:                                            vert ? '+' : fc);
                    367:                                        tp->viscol++;
                    368:                                }
1.53      schwarze  369:
                    370:                                if (fc != ' ') {
                    371:                                        if (cp != NULL &&
                    372:                                            cp->pos == TBL_CELL_HORIZ)
                    373:                                                fc = '-';
                    374:                                        else if (cp != NULL &&
                    375:                                            cp->pos == TBL_CELL_DHORIZ)
                    376:                                                fc = '=';
                    377:                                        else
                    378:                                                fc = ' ';
1.47      schwarze  379:                                }
1.55      schwarze  380:                                if (tp->tbl.cols[ic].spacing > 2 &&
                    381:                                    (vert > 1 || fc != ' ')) {
1.53      schwarze  382:                                        (*tp->letter)(tp, fc == ' ' ? '|' :
                    383:                                            vert > 1 ? '+' : fc);
1.47      schwarze  384:                                        tp->viscol++;
1.33      schwarze  385:                                }
1.47      schwarze  386:                        }
                    387:                }
1.1       kristaps  388:
1.47      schwarze  389:                /* Print the vertical frame at the end of each row. */
1.16      kristaps  390:
1.47      schwarze  391:                fc = '\0';
1.53      schwarze  392:                if ((sp->layout->last->vert &&
                    393:                     sp->layout->last->col + 1 == sp->opts->cols) ||
                    394:                    (sp->next != NULL &&
                    395:                     sp->next->layout->last->vert &&
                    396:                     sp->next->layout->last->col + 1 == sp->opts->cols) ||
                    397:                    (sp->prev != NULL &&
                    398:                     sp->prev->layout->last->vert &&
                    399:                     sp->prev->layout->last->col + 1 == sp->opts->cols &&
                    400:                     (horiz || (IS_HORIZ(sp->layout->last) &&
                    401:                      !IS_HORIZ(sp->prev->layout->last)))) ||
1.47      schwarze  402:                    (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)))
1.53      schwarze  403:                        fc = horiz || IS_HORIZ(sp->layout->last) ? '+' : '|';
1.47      schwarze  404:                else if (horiz && sp->opts->rvert)
                    405:                        fc = '-';
                    406:                if (fc != '\0') {
1.53      schwarze  407:                        if (horiz == 0 && (IS_HORIZ(sp->layout->last) == 0 ||
                    408:                            sp->layout->last->col + 1 < sp->opts->cols)) {
1.47      schwarze  409:                                tp->tcol++;
                    410:                                (*tp->advance)(tp,
                    411:                                    tp->tcol->offset > tp->viscol ?
                    412:                                    tp->tcol->offset - tp->viscol : 1);
                    413:                        }
                    414:                        (*tp->letter)(tp, fc);
                    415:                }
                    416:                (*tp->endline)(tp);
                    417:                tp->viscol = 0;
                    418:        } while (more);
1.1       kristaps  419:
1.4       kristaps  420:        /*
1.53      schwarze  421:         * Clean up after this row.  If it is the last line
                    422:         * of the table, print the box line and clean up
                    423:         * column data; otherwise, print the allbox line.
1.4       kristaps  424:         */
                    425:
1.47      schwarze  426:        term_setcol(tp, 1);
                    427:        tp->flags &= ~TERMP_MULTICOL;
                    428:        tp->tcol->rmargin = tp->maxrmargin;
1.37      schwarze  429:        if (sp->next == NULL) {
1.33      schwarze  430:                if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
1.53      schwarze  431:                        tbl_hrule(tp, sp, 2);
1.24      schwarze  432:                        tp->skipvsp = 1;
                    433:                }
1.33      schwarze  434:                if (sp->opts->opts & TBL_OPT_DBOX) {
1.53      schwarze  435:                        tbl_hrule(tp, sp, 3);
1.24      schwarze  436:                        tp->skipvsp = 2;
                    437:                }
1.11      kristaps  438:                assert(tp->tbl.cols);
                    439:                free(tp->tbl.cols);
                    440:                tp->tbl.cols = NULL;
1.45      schwarze  441:                tp->tcol->offset = offset;
1.50      schwarze  442:        } else if (horiz == 0 && sp->opts->opts & TBL_OPT_ALLBOX &&
                    443:            (sp->next == NULL || sp->next->pos == TBL_SPAN_DATA ||
                    444:             sp->next->next != NULL))
1.49      schwarze  445:                tbl_hrule(tp, sp, 1);
                    446:
1.47      schwarze  447:        tp->flags &= ~TERMP_NONOSPACE;
1.1       kristaps  448: }
                    449:
1.21      schwarze  450: /*
1.33      schwarze  451:  * Kinds of horizontal rulers:
                    452:  * 0: inside the table (single or double line with crossings)
1.53      schwarze  453:  * 1: inside the table (single or double line with crossings and ends)
                    454:  * 2: inner frame (single line with crossings and ends)
                    455:  * 3: outer frame (single line without crossings with ends)
1.21      schwarze  456:  */
1.1       kristaps  457: static void
1.33      schwarze  458: tbl_hrule(struct termp *tp, const struct tbl_span *sp, int kind)
1.1       kristaps  459: {
1.53      schwarze  460:        const struct tbl_cell *cp, *cpn, *cpp;
1.55      schwarze  461:        const struct roffcol *col;
1.33      schwarze  462:        int      vert;
1.59    ! schwarze  463:        char     cross, line, stdcross, stdline;
1.33      schwarze  464:
1.59    ! schwarze  465:        stdline = (kind < 2 && TBL_SPAN_DHORIZ == sp->pos) ? '=' : '-';
        !           466:        stdcross = (kind < 3) ? '+' : '-';
1.33      schwarze  467:
1.53      schwarze  468:        cp = sp->layout->first;
                    469:        cpp = kind || sp->prev == NULL ? NULL : sp->prev->layout->first;
                    470:        if (cpp == cp)
                    471:                cpp = NULL;
                    472:        cpn = kind > 1 || sp->next == NULL ? NULL : sp->next->layout->first;
                    473:        if (cpn == cp)
                    474:                cpn = NULL;
1.59    ! schwarze  475:        if (kind)
        !           476:                term_word(tp,
        !           477:                    cpn == NULL || cpn->pos != TBL_CELL_DOWN ? "+" : "|");
1.33      schwarze  478:        for (;;) {
1.55      schwarze  479:                col = tp->tbl.cols + cp->col;
1.59    ! schwarze  480:                if (cpn == NULL || cpn->pos != TBL_CELL_DOWN) {
        !           481:                        line = stdline;
        !           482:                        cross = stdcross;
        !           483:                } else {
        !           484:                        line = ' ';
        !           485:                        cross = (kind < 3) ? '|' : ' ';
        !           486:                }
1.55      schwarze  487:                tbl_char(tp, line, col->width + col->spacing / 2);
1.53      schwarze  488:                vert = cp->vert;
                    489:                if ((cp = cp->next) == NULL)
1.33      schwarze  490:                         break;
1.53      schwarze  491:                if (cpp != NULL) {
                    492:                        if (vert < cpp->vert)
                    493:                                vert = cpp->vert;
                    494:                        cpp = cpp->next;
                    495:                }
                    496:                if (cpn != NULL) {
                    497:                        if (vert < cpn->vert)
                    498:                                vert = cpn->vert;
                    499:                        cpn = cpn->next;
1.33      schwarze  500:                }
1.59    ! schwarze  501:                if (cpn == NULL || cpn->pos != TBL_CELL_DOWN) {
        !           502:                        line = stdline;
        !           503:                        cross = stdcross;
        !           504:                } else
        !           505:                        line = ' ';
1.49      schwarze  506:                if (sp->opts->opts & TBL_OPT_ALLBOX && !vert)
                    507:                        vert = 1;
1.55      schwarze  508:                if (col->spacing)
                    509:                        tbl_char(tp, vert ? cross : line, 1);
                    510:                if (col->spacing > 2)
                    511:                        tbl_char(tp, vert > 1 ? cross : line, 1);
                    512:                if (col->spacing > 4)
                    513:                        tbl_char(tp, line, (col->spacing - 3) / 2);
1.22      schwarze  514:        }
1.33      schwarze  515:        if (kind) {
1.59    ! schwarze  516:                term_word(tp,
        !           517:                    cpn == NULL || cpn->pos != TBL_CELL_DOWN ? "+" : "|");
1.33      schwarze  518:                term_flushln(tp);
1.22      schwarze  519:        }
1.1       kristaps  520: }
                    521:
                    522: static void
1.25      schwarze  523: tbl_data(struct termp *tp, const struct tbl_opts *opts,
1.53      schwarze  524:     const struct tbl_cell *cp, const struct tbl_dat *dp,
                    525:     const struct roffcol *col)
1.1       kristaps  526: {
1.53      schwarze  527:        switch (cp->pos) {
                    528:        case TBL_CELL_HORIZ:
                    529:                tbl_char(tp, '-', col->width);
                    530:                return;
                    531:        case TBL_CELL_DHORIZ:
                    532:                tbl_char(tp, '=', col->width);
                    533:                return;
                    534:        default:
                    535:                break;
                    536:        }
1.1       kristaps  537:
1.56      schwarze  538:        if (dp == NULL)
1.1       kristaps  539:                return;
                    540:
                    541:        switch (dp->pos) {
1.27      schwarze  542:        case TBL_DATA_NONE:
1.10      kristaps  543:                return;
1.27      schwarze  544:        case TBL_DATA_HORIZ:
                    545:        case TBL_DATA_NHORIZ:
1.11      kristaps  546:                tbl_char(tp, '-', col->width);
1.7       kristaps  547:                return;
1.27      schwarze  548:        case TBL_DATA_NDHORIZ:
                    549:        case TBL_DATA_DHORIZ:
1.11      kristaps  550:                tbl_char(tp, '=', col->width);
1.1       kristaps  551:                return;
                    552:        default:
                    553:                break;
                    554:        }
1.27      schwarze  555:
1.53      schwarze  556:        switch (cp->pos) {
1.27      schwarze  557:        case TBL_CELL_LONG:
                    558:        case TBL_CELL_CENTRE:
                    559:        case TBL_CELL_LEFT:
                    560:        case TBL_CELL_RIGHT:
1.11      kristaps  561:                tbl_literal(tp, dp, col);
1.1       kristaps  562:                break;
1.27      schwarze  563:        case TBL_CELL_NUMBER:
1.25      schwarze  564:                tbl_number(tp, opts, dp, col);
1.18      kristaps  565:                break;
1.27      schwarze  566:        case TBL_CELL_DOWN:
1.56      schwarze  567:        case TBL_CELL_SPAN:
1.1       kristaps  568:                break;
                    569:        default:
                    570:                abort();
                    571:        }
                    572: }
                    573:
                    574: static void
1.12      kristaps  575: tbl_char(struct termp *tp, char c, size_t len)
1.1       kristaps  576: {
1.12      kristaps  577:        size_t          i, sz;
                    578:        char            cp[2];
                    579:
                    580:        cp[0] = c;
                    581:        cp[1] = '\0';
1.1       kristaps  582:
1.3       kristaps  583:        sz = term_strlen(tp, cp);
                    584:
                    585:        for (i = 0; i < len; i += sz)
1.1       kristaps  586:                term_word(tp, cp);
                    587: }
                    588:
                    589: static void
1.27      schwarze  590: tbl_literal(struct termp *tp, const struct tbl_dat *dp,
1.11      kristaps  591:                const struct roffcol *col)
1.1       kristaps  592: {
1.36      schwarze  593:        size_t           len, padl, padr, width;
                    594:        int              ic, spans;
1.1       kristaps  595:
1.17      kristaps  596:        assert(dp->string);
1.21      schwarze  597:        len = term_strlen(tp, dp->string);
1.23      schwarze  598:        width = col->width;
1.36      schwarze  599:        ic = dp->layout->col;
                    600:        spans = dp->spans;
                    601:        while (spans--)
                    602:                width += tp->tbl.cols[++ic].width + 3;
1.23      schwarze  603:
                    604:        padr = width > len ? width - len : 0;
1.21      schwarze  605:        padl = 0;
1.1       kristaps  606:
1.16      kristaps  607:        switch (dp->layout->pos) {
1.27      schwarze  608:        case TBL_CELL_LONG:
1.21      schwarze  609:                padl = term_len(tp, 1);
                    610:                padr = padr > padl ? padr - padl : 0;
1.1       kristaps  611:                break;
1.27      schwarze  612:        case TBL_CELL_CENTRE:
1.21      schwarze  613:                if (2 > padr)
1.19      schwarze  614:                        break;
1.21      schwarze  615:                padl = padr / 2;
1.19      schwarze  616:                padr -= padl;
1.1       kristaps  617:                break;
1.27      schwarze  618:        case TBL_CELL_RIGHT:
1.21      schwarze  619:                padl = padr;
                    620:                padr = 0;
1.1       kristaps  621:                break;
                    622:        default:
                    623:                break;
                    624:        }
                    625:
                    626:        tbl_char(tp, ASCII_NBRSP, padl);
1.29      schwarze  627:        tbl_word(tp, dp);
1.21      schwarze  628:        tbl_char(tp, ASCII_NBRSP, padr);
1.1       kristaps  629: }
                    630:
                    631: static void
1.25      schwarze  632: tbl_number(struct termp *tp, const struct tbl_opts *opts,
1.2       kristaps  633:                const struct tbl_dat *dp,
1.11      kristaps  634:                const struct roffcol *col)
1.1       kristaps  635: {
1.11      kristaps  636:        char            *cp;
                    637:        char             buf[2];
1.58      schwarze  638:        size_t           sz, ssz, d, padl;
1.11      kristaps  639:        int              i;
1.1       kristaps  640:
                    641:        /*
                    642:         * See calc_data_number().  Left-pad by taking the offset of our
                    643:         * and the maximum decimal; right-pad by the remaining amount.
                    644:         */
                    645:
1.17      kristaps  646:        assert(dp->string);
1.2       kristaps  647:
1.17      kristaps  648:        sz = term_strlen(tp, dp->string);
1.2       kristaps  649:
1.25      schwarze  650:        buf[0] = opts->decimal;
1.11      kristaps  651:        buf[1] = '\0';
1.2       kristaps  652:
1.35      schwarze  653:        if ((cp = strrchr(dp->string, opts->decimal)) != NULL) {
1.17      kristaps  654:                for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
                    655:                        buf[0] = dp->string[i];
1.5       kristaps  656:                        ssz += term_strlen(tp, buf);
                    657:                }
1.58      schwarze  658:                d = ssz;
1.5       kristaps  659:        } else
1.58      schwarze  660:                d = sz;
1.2       kristaps  661:
1.32      schwarze  662:        if (col->decimal > d && col->width > sz) {
                    663:                padl = col->decimal - d;
                    664:                if (padl + sz > col->width)
                    665:                        padl = col->width - sz;
                    666:                tbl_char(tp, ASCII_NBRSP, padl);
                    667:        } else
                    668:                padl = 0;
1.29      schwarze  669:        tbl_word(tp, dp);
1.21      schwarze  670:        if (col->width > sz + padl)
                    671:                tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);
1.2       kristaps  672: }
                    673:
1.29      schwarze  674: static void
                    675: tbl_word(struct termp *tp, const struct tbl_dat *dp)
                    676: {
1.38      schwarze  677:        int              prev_font;
1.29      schwarze  678:
1.38      schwarze  679:        prev_font = tp->fonti;
1.29      schwarze  680:        if (dp->layout->flags & TBL_CELL_BOLD)
                    681:                term_fontpush(tp, TERMFONT_BOLD);
                    682:        else if (dp->layout->flags & TBL_CELL_ITALIC)
                    683:                term_fontpush(tp, TERMFONT_UNDER);
                    684:
                    685:        term_word(tp, dp->string);
                    686:
                    687:        term_fontpopq(tp, prev_font);
                    688: }

CVSweb