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

Annotation of mandoc/tbl_term.c, Revision 1.66

1.66    ! schwarze    1: /*     $Id: tbl_term.c,v 1.65 2018/11/29 23:08:13 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>
1.60      schwarze   23: #include <ctype.h>
1.1       kristaps   24: #include <stdio.h>
                     25: #include <stdlib.h>
                     26: #include <string.h>
                     27:
                     28: #include "mandoc.h"
1.66    ! schwarze   29: #include "tbl.h"
1.1       kristaps   30: #include "out.h"
                     31: #include "term.h"
                     32:
1.53      schwarze   33: #define        IS_HORIZ(cp)    ((cp)->pos == TBL_CELL_HORIZ || \
                     34:                         (cp)->pos == TBL_CELL_DHORIZ)
                     35:
1.62      schwarze   36:
1.11      kristaps   37: static size_t  term_tbl_len(size_t, void *);
                     38: static size_t  term_tbl_strlen(const char *, void *);
1.46      schwarze   39: static size_t  term_tbl_sulen(const struct roffsu *, void *);
1.25      schwarze   40: static void    tbl_data(struct termp *, const struct tbl_opts *,
1.53      schwarze   41:                        const struct tbl_cell *,
1.27      schwarze   42:                        const struct tbl_dat *,
1.11      kristaps   43:                        const struct roffcol *);
1.62      schwarze   44: static void    tbl_direct_border(struct termp *, int, size_t);
                     45: static void    tbl_fill_border(struct termp *, int, size_t);
                     46: static void    tbl_fill_char(struct termp *, char, size_t);
                     47: static void    tbl_fill_string(struct termp *, const char *, size_t);
1.64      schwarze   48: static void    tbl_hrule(struct termp *, const struct tbl_span *,
                     49:                        const struct tbl_span *, int);
1.27      schwarze   50: static void    tbl_literal(struct termp *, const struct tbl_dat *,
1.11      kristaps   51:                        const struct roffcol *);
1.27      schwarze   52: static void    tbl_number(struct termp *, const struct tbl_opts *,
                     53:                        const struct tbl_dat *,
1.11      kristaps   54:                        const struct roffcol *);
1.29      schwarze   55: static void    tbl_word(struct termp *, const struct tbl_dat *);
1.11      kristaps   56:
                     57:
1.62      schwarze   58: /*
                     59:  * The following border-character tables are indexed
                     60:  * by ternary (3-based) numbers, as opposed to binary or decimal.
                     61:  * Each ternary digit describes the line width in one direction:
                     62:  * 0 means no line, 1 single or light line, 2 double or heavy line.
                     63:  */
                     64:
                     65: /* Positional values of the four directions. */
                     66: #define        BRIGHT  1
                     67: #define        BDOWN   3
                     68: #define        BLEFT   (3 * 3)
                     69: #define        BUP     (3 * 3 * 3)
                     70: #define        BHORIZ  (BLEFT + BRIGHT)
                     71:
                     72: /* Code points to use for each combination of widths. */
                     73: static  const int borders_utf8[81] = {
                     74:        0x0020, 0x2576, 0x257a,  /* 000 right */
                     75:        0x2577, 0x250c, 0x250d,  /* 001 down */
                     76:        0x257b, 0x250e, 0x250f,  /* 002 */
                     77:        0x2574, 0x2500, 0x257c,  /* 010 left */
                     78:        0x2510, 0x252c, 0x252e,  /* 011 left down */
                     79:        0x2512, 0x2530, 0x2532,  /* 012 */
                     80:        0x2578, 0x257e, 0x2501,  /* 020 left */
                     81:        0x2511, 0x252d, 0x252f,  /* 021 left down */
                     82:        0x2513, 0x2531, 0x2533,  /* 022 */
                     83:        0x2575, 0x2514, 0x2515,  /* 100 up */
                     84:        0x2502, 0x251c, 0x251d,  /* 101 up down */
                     85:        0x257d, 0x251f, 0x2522,  /* 102 */
                     86:        0x2518, 0x2534, 0x2536,  /* 110 up left */
                     87:        0x2524, 0x253c, 0x253e,  /* 111 all */
                     88:        0x2527, 0x2541, 0x2546,  /* 112 */
                     89:        0x2519, 0x2535, 0x2537,  /* 120 up left */
                     90:        0x2525, 0x253d, 0x253f,  /* 121 all */
                     91:        0x252a, 0x2545, 0x2548,  /* 122 */
                     92:        0x2579, 0x2516, 0x2517,  /* 200 up */
                     93:        0x257f, 0x251e, 0x2521,  /* 201 up down */
                     94:        0x2503, 0x2520, 0x2523,  /* 202 */
                     95:        0x251a, 0x2538, 0x253a,  /* 210 up left */
                     96:        0x2526, 0x2540, 0x2544,  /* 211 all */
                     97:        0x2528, 0x2542, 0x254a,  /* 212 */
                     98:        0x251b, 0x2539, 0x253b,  /* 220 up left */
                     99:        0x2529, 0x2543, 0x2547,  /* 221 all */
                    100:        0x252b, 0x2549, 0x254b,  /* 222 */
                    101: };
                    102:
                    103: /* ASCII approximations for these code points, compatible with groff. */
                    104: static  const int borders_ascii[81] = {
                    105:        ' ', '-', '=',  /* 000 right */
                    106:        '|', '+', '+',  /* 001 down */
                    107:        '|', '+', '+',  /* 002 */
                    108:        '-', '-', '=',  /* 010 left */
                    109:        '+', '+', '+',  /* 011 left down */
                    110:        '+', '+', '+',  /* 012 */
                    111:        '=', '=', '=',  /* 020 left */
                    112:        '+', '+', '+',  /* 021 left down */
                    113:        '+', '+', '+',  /* 022 */
                    114:        '|', '+', '+',  /* 100 up */
                    115:        '|', '+', '+',  /* 101 up down */
                    116:        '|', '+', '+',  /* 102 */
                    117:        '+', '+', '+',  /* 110 up left */
                    118:        '+', '+', '+',  /* 111 all */
                    119:        '+', '+', '+',  /* 112 */
                    120:        '+', '+', '+',  /* 120 up left */
                    121:        '+', '+', '+',  /* 121 all */
                    122:        '+', '+', '+',  /* 122 */
                    123:        '|', '+', '+',  /* 200 up */
                    124:        '|', '+', '+',  /* 201 up down */
                    125:        '|', '+', '+',  /* 202 */
                    126:        '+', '+', '+',  /* 210 up left */
                    127:        '+', '+', '+',  /* 211 all */
                    128:        '+', '+', '+',  /* 212 */
                    129:        '+', '+', '+',  /* 220 up left */
                    130:        '+', '+', '+',  /* 221 all */
                    131:        '+', '+', '+',  /* 222 */
                    132: };
                    133:
                    134: /* Either of the above according to the selected output encoding. */
                    135: static const int *borders_locale;
                    136:
                    137:
1.11      kristaps  138: static size_t
1.46      schwarze  139: term_tbl_sulen(const struct roffsu *su, void *arg)
                    140: {
1.57      schwarze  141:        int      i;
                    142:
                    143:        i = term_hen((const struct termp *)arg, su);
                    144:        return i > 0 ? i : 0;
1.46      schwarze  145: }
                    146:
                    147: static size_t
1.11      kristaps  148: term_tbl_strlen(const char *p, void *arg)
                    149: {
1.42      schwarze  150:        return term_strlen((const struct termp *)arg, p);
1.11      kristaps  151: }
                    152:
                    153: static size_t
                    154: term_tbl_len(size_t sz, void *arg)
                    155: {
1.42      schwarze  156:        return term_len((const struct termp *)arg, sz);
1.11      kristaps  157: }
1.1       kristaps  158:
1.62      schwarze  159:
1.1       kristaps  160: void
                    161: term_tbl(struct termp *tp, const struct tbl_span *sp)
                    162: {
1.62      schwarze  163:        const struct tbl_cell   *cp, *cpn, *cpp, *cps;
1.11      kristaps  164:        const struct tbl_dat    *dp;
1.34      schwarze  165:        static size_t            offset;
1.47      schwarze  166:        size_t                   coloff, tsz;
1.62      schwarze  167:        int                      hspans, ic, more;
                    168:        int                      dvert, fc, horiz, line, uvert;
1.39      schwarze  169:
1.4       kristaps  170:        /* Inhibit printing of spaces: we do padding ourselves. */
                    171:
1.47      schwarze  172:        tp->flags |= TERMP_NOSPACE | TERMP_NONOSPACE;
1.4       kristaps  173:
                    174:        /*
1.11      kristaps  175:         * The first time we're invoked for a given table block,
                    176:         * calculate the table widths and decimal positions.
1.4       kristaps  177:         */
                    178:
1.37      schwarze  179:        if (tp->tbl.cols == NULL) {
1.62      schwarze  180:                borders_locale = tp->enc == TERMENC_UTF8 ?
                    181:                    borders_utf8 : borders_ascii;
                    182:
1.11      kristaps  183:                tp->tbl.len = term_tbl_len;
                    184:                tp->tbl.slen = term_tbl_strlen;
1.46      schwarze  185:                tp->tbl.sulen = term_tbl_sulen;
1.11      kristaps  186:                tp->tbl.arg = tp;
1.4       kristaps  187:
1.48      schwarze  188:                tblcalc(&tp->tbl, sp, tp->tcol->offset, tp->tcol->rmargin);
1.54      schwarze  189:
                    190:                /* Tables leak .ta settings to subsequent text. */
                    191:
                    192:                term_tab_set(tp, NULL);
                    193:                coloff = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
                    194:                    sp->opts->lvert;
                    195:                for (ic = 0; ic < sp->opts->cols; ic++) {
                    196:                        coloff += tp->tbl.cols[ic].width;
                    197:                        term_tab_iset(coloff);
1.55      schwarze  198:                        coloff += tp->tbl.cols[ic].spacing;
1.54      schwarze  199:                }
1.1       kristaps  200:
1.34      schwarze  201:                /* Center the table as a whole. */
                    202:
1.45      schwarze  203:                offset = tp->tcol->offset;
1.34      schwarze  204:                if (sp->opts->opts & TBL_OPT_CENTRE) {
                    205:                        tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)
                    206:                            ? 2 : !!sp->opts->lvert + !!sp->opts->rvert;
1.55      schwarze  207:                        for (ic = 0; ic + 1 < sp->opts->cols; ic++)
                    208:                                tsz += tp->tbl.cols[ic].width +
                    209:                                    tp->tbl.cols[ic].spacing;
                    210:                        if (sp->opts->cols)
                    211:                                tsz += tp->tbl.cols[sp->opts->cols - 1].width;
1.45      schwarze  212:                        if (offset + tsz > tp->tcol->rmargin)
1.34      schwarze  213:                                tsz -= 1;
1.45      schwarze  214:                        tp->tcol->offset = offset + tp->tcol->rmargin > tsz ?
                    215:                            (offset + tp->tcol->rmargin - tsz) / 2 : 0;
1.34      schwarze  216:                }
                    217:
1.33      schwarze  218:                /* Horizontal frame at the start of boxed tables. */
1.4       kristaps  219:
1.62      schwarze  220:                if (tp->enc == TERMENC_ASCII &&
                    221:                    sp->opts->opts & TBL_OPT_DBOX)
1.64      schwarze  222:                        tbl_hrule(tp, NULL, sp, TBL_OPT_DBOX);
1.53      schwarze  223:                if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX))
1.64      schwarze  224:                        tbl_hrule(tp, NULL, sp, TBL_OPT_BOX);
1.21      schwarze  225:        }
1.1       kristaps  226:
1.47      schwarze  227:        /* Set up the columns. */
1.1       kristaps  228:
1.47      schwarze  229:        tp->flags |= TERMP_MULTICOL;
                    230:        horiz = 0;
                    231:        switch (sp->pos) {
                    232:        case TBL_SPAN_HORIZ:
                    233:        case TBL_SPAN_DHORIZ:
                    234:                horiz = 1;
                    235:                term_setcol(tp, 1);
                    236:                break;
                    237:        case TBL_SPAN_DATA:
                    238:                term_setcol(tp, sp->opts->cols + 2);
                    239:                coloff = tp->tcol->offset;
                    240:
                    241:                /* Set up a column for a left vertical frame. */
                    242:
                    243:                if (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
                    244:                    sp->opts->lvert)
                    245:                        coloff++;
                    246:                tp->tcol->rmargin = coloff;
1.33      schwarze  247:
1.47      schwarze  248:                /* Set up the data columns. */
1.1       kristaps  249:
1.47      schwarze  250:                dp = sp->first;
1.61      schwarze  251:                hspans = 0;
1.47      schwarze  252:                for (ic = 0; ic < sp->opts->cols; ic++) {
1.61      schwarze  253:                        if (hspans == 0) {
1.47      schwarze  254:                                tp->tcol++;
                    255:                                tp->tcol->offset = coloff;
                    256:                        }
                    257:                        coloff += tp->tbl.cols[ic].width;
                    258:                        tp->tcol->rmargin = coloff;
                    259:                        if (ic + 1 < sp->opts->cols)
1.55      schwarze  260:                                coloff += tp->tbl.cols[ic].spacing;
1.61      schwarze  261:                        if (hspans) {
                    262:                                hspans--;
1.47      schwarze  263:                                continue;
                    264:                        }
                    265:                        if (dp == NULL)
                    266:                                continue;
1.61      schwarze  267:                        hspans = dp->hspans;
1.56      schwarze  268:                        if (ic || sp->layout->first->pos != TBL_CELL_SPAN)
                    269:                                dp = dp->next;
1.47      schwarze  270:                }
                    271:
                    272:                /* Set up a column for a right vertical frame. */
                    273:
                    274:                tp->tcol++;
1.55      schwarze  275:                tp->tcol->offset = coloff + 1;
1.53      schwarze  276:                tp->tcol->rmargin = tp->maxrmargin;
1.47      schwarze  277:
                    278:                /* Spans may have reduced the number of columns. */
                    279:
                    280:                tp->lasttcol = tp->tcol - tp->tcols;
                    281:
                    282:                /* Fill the buffers for all data columns. */
1.4       kristaps  283:
1.47      schwarze  284:                tp->tcol = tp->tcols;
1.53      schwarze  285:                cp = cpn = sp->layout->first;
1.4       kristaps  286:                dp = sp->first;
1.61      schwarze  287:                hspans = 0;
1.36      schwarze  288:                for (ic = 0; ic < sp->opts->cols; ic++) {
1.53      schwarze  289:                        if (cpn != NULL) {
                    290:                                cp = cpn;
                    291:                                cpn = cpn->next;
                    292:                        }
1.61      schwarze  293:                        if (hspans) {
                    294:                                hspans--;
1.47      schwarze  295:                                continue;
                    296:                        }
                    297:                        tp->tcol++;
                    298:                        tp->col = 0;
1.53      schwarze  299:                        tbl_data(tp, sp->opts, cp, dp, tp->tbl.cols + ic);
1.47      schwarze  300:                        if (dp == NULL)
                    301:                                continue;
1.61      schwarze  302:                        hspans = dp->hspans;
1.56      schwarze  303:                        if (cp->pos != TBL_CELL_SPAN)
                    304:                                dp = dp->next;
1.47      schwarze  305:                }
                    306:                break;
                    307:        }
                    308:
                    309:        do {
                    310:                /* Print the vertical frame at the start of each row. */
                    311:
                    312:                tp->tcol = tp->tcols;
1.62      schwarze  313:                uvert = dvert = sp->opts->opts & TBL_OPT_DBOX ? 2 :
                    314:                    sp->opts->opts & TBL_OPT_BOX ? 1 : 0;
                    315:                if (sp->pos == TBL_SPAN_DATA && uvert < sp->layout->vert)
                    316:                        uvert = dvert = sp->layout->vert;
                    317:                if (sp->next != NULL && sp->next->pos == TBL_SPAN_DATA &&
                    318:                    dvert < sp->next->layout->vert)
                    319:                        dvert = sp->next->layout->vert;
                    320:                if (sp->prev != NULL && uvert < sp->prev->layout->vert &&
                    321:                    (horiz || (IS_HORIZ(sp->layout->first) &&
                    322:                      !IS_HORIZ(sp->prev->layout->first))))
                    323:                        uvert = sp->prev->layout->vert;
                    324:                line = sp->pos == TBL_SPAN_DHORIZ ||
                    325:                    sp->layout->first->pos == TBL_CELL_DHORIZ ? 2 :
                    326:                    sp->pos == TBL_SPAN_HORIZ ||
                    327:                    sp->layout->first->pos == TBL_CELL_HORIZ ? 1 : 0;
                    328:                fc = BUP * uvert + BDOWN * dvert + BRIGHT * line;
                    329:                if (uvert > 0 || dvert > 0 || (horiz && sp->opts->lvert)) {
1.47      schwarze  330:                        (*tp->advance)(tp, tp->tcols->offset);
1.62      schwarze  331:                        tp->viscol = tp->tcol->offset;
                    332:                        tbl_direct_border(tp, fc, 1);
1.47      schwarze  333:                }
1.22      schwarze  334:
1.47      schwarze  335:                /* Print the data cells. */
1.21      schwarze  336:
1.47      schwarze  337:                more = 0;
1.62      schwarze  338:                if (horiz)
1.64      schwarze  339:                        tbl_hrule(tp, sp->prev, sp, 0);
1.62      schwarze  340:                else {
1.47      schwarze  341:                        cp = sp->layout->first;
1.53      schwarze  342:                        cpn = sp->next == NULL ? NULL :
                    343:                            sp->next->layout->first;
                    344:                        cpp = sp->prev == NULL ? NULL :
                    345:                            sp->prev->layout->first;
1.47      schwarze  346:                        dp = sp->first;
1.61      schwarze  347:                        hspans = 0;
1.47      schwarze  348:                        for (ic = 0; ic < sp->opts->cols; ic++) {
                    349:
1.53      schwarze  350:                                /*
                    351:                                 * Figure out whether to print a
                    352:                                 * vertical line after this cell
                    353:                                 * and advance to next layout cell.
                    354:                                 */
1.47      schwarze  355:
1.62      schwarze  356:                                uvert = dvert = fc = 0;
1.47      schwarze  357:                                if (cp != NULL) {
1.62      schwarze  358:                                        cps = cp;
                    359:                                        while (cps->next != NULL &&
                    360:                                            cps->next->pos == TBL_CELL_SPAN)
                    361:                                                cps = cps->next;
                    362:                                        if (sp->pos == TBL_SPAN_DATA)
                    363:                                                uvert = dvert = cps->vert;
1.53      schwarze  364:                                        switch (cp->pos) {
                    365:                                        case TBL_CELL_HORIZ:
1.62      schwarze  366:                                                fc = BHORIZ;
1.53      schwarze  367:                                                break;
                    368:                                        case TBL_CELL_DHORIZ:
1.62      schwarze  369:                                                fc = BHORIZ * 2;
1.53      schwarze  370:                                                break;
                    371:                                        default:
                    372:                                                break;
                    373:                                        }
                    374:                                }
                    375:                                if (cpp != NULL) {
1.62      schwarze  376:                                        if (uvert < cpp->vert &&
1.53      schwarze  377:                                            cp != NULL &&
                    378:                                            ((IS_HORIZ(cp) &&
                    379:                                              !IS_HORIZ(cpp)) ||
                    380:                                             (cp->next != NULL &&
                    381:                                              cpp->next != NULL &&
                    382:                                              IS_HORIZ(cp->next) &&
                    383:                                              !IS_HORIZ(cpp->next))))
1.62      schwarze  384:                                                uvert = cpp->vert;
1.53      schwarze  385:                                        cpp = cpp->next;
                    386:                                }
1.62      schwarze  387:                                if (sp->opts->opts & TBL_OPT_ALLBOX) {
                    388:                                        if (uvert == 0)
                    389:                                                uvert = 1;
                    390:                                        if (dvert == 0)
                    391:                                                dvert = 1;
                    392:                                }
1.53      schwarze  393:                                if (cpn != NULL) {
1.62      schwarze  394:                                        if (dvert == 0 ||
                    395:                                            (dvert < cpn->vert &&
                    396:                                             tp->enc == TERMENC_UTF8))
                    397:                                                dvert = cpn->vert;
1.53      schwarze  398:                                        cpn = cpn->next;
                    399:                                }
1.51      schwarze  400:
1.53      schwarze  401:                                /*
                    402:                                 * Skip later cells in a span,
                    403:                                 * figure out whether to start a span,
                    404:                                 * and advance to next data cell.
                    405:                                 */
1.51      schwarze  406:
1.61      schwarze  407:                                if (hspans) {
                    408:                                        hspans--;
1.62      schwarze  409:                                        cp = cp->next;
1.51      schwarze  410:                                        continue;
                    411:                                }
                    412:                                if (dp != NULL) {
1.61      schwarze  413:                                        hspans = dp->hspans;
1.56      schwarze  414:                                        if (ic || sp->layout->first->pos
                    415:                                            != TBL_CELL_SPAN)
                    416:                                                dp = dp->next;
1.51      schwarze  417:                                }
                    418:
1.53      schwarze  419:                                /*
                    420:                                 * Print one line of text in the cell
                    421:                                 * and remember whether there is more.
                    422:                                 */
1.51      schwarze  423:
                    424:                                tp->tcol++;
                    425:                                if (tp->tcol->col < tp->tcol->lastcol)
                    426:                                        term_flushln(tp);
                    427:                                if (tp->tcol->col < tp->tcol->lastcol)
                    428:                                        more = 1;
                    429:
                    430:                                /*
                    431:                                 * Vertical frames between data cells,
                    432:                                 * but not after the last column.
                    433:                                 */
                    434:
1.63      schwarze  435:                                if (fc == 0 &&
                    436:                                    ((uvert == 0 && dvert == 0 &&
                    437:                                      cp != NULL && (cp->next == NULL ||
1.62      schwarze  438:                                      !IS_HORIZ(cp->next))) ||
1.63      schwarze  439:                                     tp->tcol + 1 ==
                    440:                                      tp->tcols + tp->lasttcol)) {
                    441:                                        if (cp != NULL)
                    442:                                                cp = cp->next;
1.53      schwarze  443:                                        continue;
1.62      schwarze  444:                                }
1.53      schwarze  445:
1.55      schwarze  446:                                if (tp->viscol < tp->tcol->rmargin) {
1.53      schwarze  447:                                        (*tp->advance)(tp, tp->tcol->rmargin
                    448:                                           - tp->viscol);
                    449:                                        tp->viscol = tp->tcol->rmargin;
                    450:                                }
1.55      schwarze  451:                                while (tp->viscol < tp->tcol->rmargin +
1.62      schwarze  452:                                    tp->tbl.cols[ic].spacing / 2)
                    453:                                        tbl_direct_border(tp, fc, 1);
1.53      schwarze  454:
1.51      schwarze  455:                                if (tp->tcol + 1 == tp->tcols + tp->lasttcol)
                    456:                                        continue;
1.47      schwarze  457:
1.62      schwarze  458:                                if (cp != NULL) {
1.53      schwarze  459:                                        switch (cp->pos) {
                    460:                                        case TBL_CELL_HORIZ:
1.62      schwarze  461:                                                fc = BLEFT;
1.53      schwarze  462:                                                break;
                    463:                                        case TBL_CELL_DHORIZ:
1.62      schwarze  464:                                                fc = BLEFT * 2;
1.53      schwarze  465:                                                break;
                    466:                                        default:
1.62      schwarze  467:                                                fc = 0;
1.53      schwarze  468:                                                break;
                    469:                                        }
1.62      schwarze  470:                                        cp = cp->next;
1.53      schwarze  471:                                }
1.62      schwarze  472:                                if (cp != NULL) {
                    473:                                        switch (cp->pos) {
                    474:                                        case TBL_CELL_HORIZ:
                    475:                                                fc += BRIGHT;
                    476:                                                break;
                    477:                                        case TBL_CELL_DHORIZ:
                    478:                                                fc += BRIGHT * 2;
                    479:                                                break;
                    480:                                        default:
                    481:                                                break;
                    482:                                        }
1.55      schwarze  483:                                }
1.62      schwarze  484:                                if (tp->tbl.cols[ic].spacing)
                    485:                                        tbl_direct_border(tp, fc +
                    486:                                            BUP * uvert + BDOWN * dvert, 1);
                    487:
                    488:                                if (tp->enc == TERMENC_UTF8)
                    489:                                        uvert = dvert = 0;
1.53      schwarze  490:
1.62      schwarze  491:                                if (fc != 0) {
1.53      schwarze  492:                                        if (cp != NULL &&
                    493:                                            cp->pos == TBL_CELL_HORIZ)
1.62      schwarze  494:                                                fc = BHORIZ;
1.53      schwarze  495:                                        else if (cp != NULL &&
                    496:                                            cp->pos == TBL_CELL_DHORIZ)
1.62      schwarze  497:                                                fc = BHORIZ * 2;
1.53      schwarze  498:                                        else
1.62      schwarze  499:                                                fc = 0;
1.47      schwarze  500:                                }
1.55      schwarze  501:                                if (tp->tbl.cols[ic].spacing > 2 &&
1.62      schwarze  502:                                    (uvert > 1 || dvert > 1 || fc != 0))
                    503:                                        tbl_direct_border(tp, fc +
                    504:                                            BUP * (uvert > 1) +
                    505:                                            BDOWN * (dvert > 1), 1);
1.47      schwarze  506:                        }
                    507:                }
1.1       kristaps  508:
1.47      schwarze  509:                /* Print the vertical frame at the end of each row. */
1.16      kristaps  510:
1.62      schwarze  511:                uvert = dvert = sp->opts->opts & TBL_OPT_DBOX ? 2 :
                    512:                    sp->opts->opts & TBL_OPT_BOX ? 1 : 0;
                    513:                if (sp->pos == TBL_SPAN_DATA &&
                    514:                    uvert < sp->layout->last->vert &&
                    515:                    sp->layout->last->col + 1 == sp->opts->cols)
                    516:                        uvert = dvert = sp->layout->last->vert;
                    517:                if (sp->next != NULL &&
                    518:                    dvert < sp->next->layout->last->vert &&
                    519:                    sp->next->layout->last->col + 1 == sp->opts->cols)
                    520:                        dvert = sp->next->layout->last->vert;
                    521:                if (sp->prev != NULL &&
                    522:                    uvert < sp->prev->layout->last->vert &&
                    523:                    sp->prev->layout->last->col + 1 == sp->opts->cols &&
                    524:                    (horiz || (IS_HORIZ(sp->layout->last) &&
                    525:                     !IS_HORIZ(sp->prev->layout->last))))
                    526:                        uvert = sp->prev->layout->last->vert;
                    527:                line = sp->pos == TBL_SPAN_DHORIZ ||
                    528:                    (sp->layout->last->pos == TBL_CELL_DHORIZ &&
                    529:                     sp->layout->last->col + 1 == sp->opts->cols) ? 2 :
                    530:                    sp->pos == TBL_SPAN_HORIZ ||
                    531:                    (sp->layout->last->pos == TBL_CELL_HORIZ &&
                    532:                     sp->layout->last->col + 1 == sp->opts->cols) ? 1 : 0;
                    533:                fc = BUP * uvert + BDOWN * dvert + BLEFT * line;
                    534:                if (uvert > 0 || dvert > 0 || (horiz && sp->opts->rvert)) {
1.53      schwarze  535:                        if (horiz == 0 && (IS_HORIZ(sp->layout->last) == 0 ||
                    536:                            sp->layout->last->col + 1 < sp->opts->cols)) {
1.47      schwarze  537:                                tp->tcol++;
                    538:                                (*tp->advance)(tp,
                    539:                                    tp->tcol->offset > tp->viscol ?
                    540:                                    tp->tcol->offset - tp->viscol : 1);
                    541:                        }
1.62      schwarze  542:                        tbl_direct_border(tp, fc, 1);
1.47      schwarze  543:                }
                    544:                (*tp->endline)(tp);
                    545:                tp->viscol = 0;
                    546:        } while (more);
1.1       kristaps  547:
1.4       kristaps  548:        /*
1.53      schwarze  549:         * Clean up after this row.  If it is the last line
                    550:         * of the table, print the box line and clean up
                    551:         * column data; otherwise, print the allbox line.
1.4       kristaps  552:         */
                    553:
1.47      schwarze  554:        term_setcol(tp, 1);
                    555:        tp->flags &= ~TERMP_MULTICOL;
                    556:        tp->tcol->rmargin = tp->maxrmargin;
1.37      schwarze  557:        if (sp->next == NULL) {
1.33      schwarze  558:                if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
1.64      schwarze  559:                        tbl_hrule(tp, sp, NULL, TBL_OPT_BOX);
1.24      schwarze  560:                        tp->skipvsp = 1;
                    561:                }
1.62      schwarze  562:                if (tp->enc == TERMENC_ASCII &&
                    563:                    sp->opts->opts & TBL_OPT_DBOX) {
1.64      schwarze  564:                        tbl_hrule(tp, sp, NULL, TBL_OPT_DBOX);
1.24      schwarze  565:                        tp->skipvsp = 2;
                    566:                }
1.11      kristaps  567:                assert(tp->tbl.cols);
                    568:                free(tp->tbl.cols);
                    569:                tp->tbl.cols = NULL;
1.45      schwarze  570:                tp->tcol->offset = offset;
1.50      schwarze  571:        } else if (horiz == 0 && sp->opts->opts & TBL_OPT_ALLBOX &&
                    572:            (sp->next == NULL || sp->next->pos == TBL_SPAN_DATA ||
                    573:             sp->next->next != NULL))
1.64      schwarze  574:                tbl_hrule(tp, sp, sp->next, TBL_OPT_ALLBOX);
1.49      schwarze  575:
1.47      schwarze  576:        tp->flags &= ~TERMP_NONOSPACE;
1.1       kristaps  577: }
                    578:
                    579: static void
1.64      schwarze  580: tbl_hrule(struct termp *tp, const struct tbl_span *spp,
                    581:     const struct tbl_span *spn, int flags)
1.1       kristaps  582: {
1.65      schwarze  583:        const struct tbl_cell   *cpp;    /* Layout cell above this line. */
                    584:        const struct tbl_cell   *cpn;    /* Layout cell below this line. */
                    585:        const struct tbl_dat    *dpn;    /* Data cell below this line. */
1.64      schwarze  586:        const struct roffcol    *col;    /* Contains width and spacing. */
                    587:        int                      opts;   /* For the table as a whole. */
                    588:        int                      bw;     /* Box line width. */
                    589:        int                      hw;     /* Horizontal line width. */
                    590:        int                      lw, rw; /* Left and right line widths. */
                    591:        int                      uw, dw; /* Vertical line widths. */
                    592:
                    593:        cpp = spp == NULL ? NULL : spp->layout->first;
                    594:        cpn = spn == NULL ? NULL : spn->layout->first;
1.65      schwarze  595:        dpn = NULL;
                    596:        if (spn != NULL) {
                    597:                if (spn->pos == TBL_SPAN_DATA)
                    598:                        dpn = spn->first;
                    599:                else if (spn->next != NULL)
                    600:                        dpn = spn->next->first;
                    601:        }
1.64      schwarze  602:        opts = spn == NULL ? spp->opts->opts : spn->opts->opts;
                    603:        bw = opts & TBL_OPT_DBOX ? (tp->enc == TERMENC_UTF8 ? 2 : 1) :
                    604:            opts & (TBL_OPT_BOX | TBL_OPT_ALLBOX) ? 1 : 0;
                    605:        hw = flags == TBL_OPT_DBOX || flags == TBL_OPT_BOX ? bw :
                    606:            spn->pos == TBL_SPAN_DHORIZ ? 2 : 1;
                    607:
                    608:        /* Print the left end of the line. */
                    609:
1.62      schwarze  610:        if (tp->viscol == 0) {
                    611:                (*tp->advance)(tp, tp->tcols->offset);
                    612:                tp->viscol = tp->tcols->offset;
                    613:        }
1.64      schwarze  614:        if (flags != 0)
                    615:                tbl_direct_border(tp,
                    616:                    (spp == NULL ? 0 : BUP * bw) +
                    617:                    (spn == NULL ? 0 : BDOWN * bw) +
                    618:                    (spp == NULL || cpn == NULL ||
                    619:                     cpn->pos != TBL_CELL_DOWN ? BRIGHT * hw : 0), 1);
                    620:
1.33      schwarze  621:        for (;;) {
1.64      schwarze  622:                col = tp->tbl.cols + (cpn == NULL ? cpp->col : cpn->col);
                    623:
                    624:                /* Print the horizontal line inside this column. */
                    625:
                    626:                lw = cpp == NULL || cpn == NULL ||
1.65      schwarze  627:                    (cpn->pos != TBL_CELL_DOWN &&
                    628:                     (dpn == NULL || strcmp(dpn->string, "\\^") != 0))
                    629:                    ? hw : 0;
1.64      schwarze  630:                tbl_direct_border(tp, BHORIZ * lw,
                    631:                    col->width + col->spacing / 2);
                    632:
                    633:                /*
                    634:                 * Figure out whether a vertical line is crossing
                    635:                 * at the end of this column,
                    636:                 * and advance to the next column.
                    637:                 */
                    638:
                    639:                uw = dw = 0;
1.53      schwarze  640:                if (cpp != NULL) {
1.64      schwarze  641:                        if (flags != TBL_OPT_DBOX) {
                    642:                                uw = cpp->vert;
                    643:                                if (uw == 0 && opts & TBL_OPT_ALLBOX)
                    644:                                        uw = 1;
                    645:                        }
1.53      schwarze  646:                        cpp = cpp->next;
                    647:                }
                    648:                if (cpn != NULL) {
1.64      schwarze  649:                        if (flags != TBL_OPT_DBOX) {
                    650:                                dw = cpn->vert;
                    651:                                if (dw == 0 && opts & TBL_OPT_ALLBOX)
                    652:                                        dw = 1;
                    653:                        }
1.53      schwarze  654:                        cpn = cpn->next;
1.65      schwarze  655:                        while (dpn != NULL && dpn->layout != cpn)
                    656:                                dpn = dpn->next;
1.33      schwarze  657:                }
1.64      schwarze  658:                if (cpp == NULL && cpn == NULL)
                    659:                        break;
                    660:
                    661:                /* Vertical lines do not cross spanned cells. */
                    662:
                    663:                if (cpp != NULL && cpp->pos == TBL_CELL_SPAN)
                    664:                        uw = 0;
                    665:                if (cpn != NULL && cpn->pos == TBL_CELL_SPAN)
                    666:                        dw = 0;
                    667:
                    668:                /* The horizontal line inside the next column. */
                    669:
                    670:                rw = cpp == NULL || cpn == NULL ||
1.65      schwarze  671:                    (cpn->pos != TBL_CELL_DOWN &&
                    672:                     (dpn == NULL || strcmp(dpn->string, "\\^") != 0))
                    673:                    ? hw : 0;
1.64      schwarze  674:
                    675:                /* The line crossing at the end of this column. */
                    676:
1.55      schwarze  677:                if (col->spacing)
1.64      schwarze  678:                        tbl_direct_border(tp, BLEFT * lw +
                    679:                            BRIGHT * rw + BUP * uw + BDOWN * dw, 1);
                    680:
                    681:                /*
                    682:                 * In ASCII output, a crossing may print two characters.
                    683:                 */
                    684:
                    685:                if (tp->enc != TERMENC_ASCII || (uw < 2 && dw < 2))
                    686:                        uw = dw = 0;
1.55      schwarze  687:                if (col->spacing > 2)
1.64      schwarze  688:                        tbl_direct_border(tp,
                    689:                             BHORIZ * rw + BUP * uw + BDOWN * dw, 1);
                    690:
                    691:                /* Padding before the start of the next column. */
                    692:
1.55      schwarze  693:                if (col->spacing > 4)
1.64      schwarze  694:                        tbl_direct_border(tp,
                    695:                            BHORIZ * rw, (col->spacing - 3) / 2);
1.22      schwarze  696:        }
1.64      schwarze  697:
                    698:        /* Print the right end of the line. */
                    699:
                    700:        if (flags != 0) {
                    701:                tbl_direct_border(tp,
                    702:                    (spp == NULL ? 0 : BUP * bw) +
                    703:                    (spn == NULL ? 0 : BDOWN * bw) +
                    704:                    (spp == NULL || spn == NULL ||
                    705:                     spn->layout->last->pos != TBL_CELL_DOWN ?
                    706:                     BLEFT * hw : 0), 1);
1.62      schwarze  707:                (*tp->endline)(tp);
                    708:                tp->viscol = 0;
1.22      schwarze  709:        }
1.1       kristaps  710: }
                    711:
                    712: static void
1.25      schwarze  713: tbl_data(struct termp *tp, const struct tbl_opts *opts,
1.53      schwarze  714:     const struct tbl_cell *cp, const struct tbl_dat *dp,
                    715:     const struct roffcol *col)
1.1       kristaps  716: {
1.53      schwarze  717:        switch (cp->pos) {
                    718:        case TBL_CELL_HORIZ:
1.62      schwarze  719:                tbl_fill_border(tp, BHORIZ, col->width);
1.53      schwarze  720:                return;
                    721:        case TBL_CELL_DHORIZ:
1.62      schwarze  722:                tbl_fill_border(tp, BHORIZ * 2, col->width);
1.53      schwarze  723:                return;
                    724:        default:
                    725:                break;
                    726:        }
1.1       kristaps  727:
1.56      schwarze  728:        if (dp == NULL)
1.1       kristaps  729:                return;
                    730:
                    731:        switch (dp->pos) {
1.27      schwarze  732:        case TBL_DATA_NONE:
1.10      kristaps  733:                return;
1.27      schwarze  734:        case TBL_DATA_HORIZ:
                    735:        case TBL_DATA_NHORIZ:
1.62      schwarze  736:                tbl_fill_border(tp, BHORIZ, col->width);
1.7       kristaps  737:                return;
1.27      schwarze  738:        case TBL_DATA_NDHORIZ:
                    739:        case TBL_DATA_DHORIZ:
1.62      schwarze  740:                tbl_fill_border(tp, BHORIZ * 2, col->width);
1.1       kristaps  741:                return;
                    742:        default:
                    743:                break;
                    744:        }
1.27      schwarze  745:
1.53      schwarze  746:        switch (cp->pos) {
1.27      schwarze  747:        case TBL_CELL_LONG:
                    748:        case TBL_CELL_CENTRE:
                    749:        case TBL_CELL_LEFT:
                    750:        case TBL_CELL_RIGHT:
1.11      kristaps  751:                tbl_literal(tp, dp, col);
1.1       kristaps  752:                break;
1.27      schwarze  753:        case TBL_CELL_NUMBER:
1.25      schwarze  754:                tbl_number(tp, opts, dp, col);
1.18      kristaps  755:                break;
1.27      schwarze  756:        case TBL_CELL_DOWN:
1.56      schwarze  757:        case TBL_CELL_SPAN:
1.1       kristaps  758:                break;
                    759:        default:
                    760:                abort();
                    761:        }
                    762: }
                    763:
                    764: static void
1.62      schwarze  765: tbl_fill_string(struct termp *tp, const char *cp, size_t len)
                    766: {
                    767:        size_t   i, sz;
                    768:
                    769:        sz = term_strlen(tp, cp);
                    770:        for (i = 0; i < len; i += sz)
                    771:                term_word(tp, cp);
                    772: }
                    773:
                    774: static void
                    775: tbl_fill_char(struct termp *tp, char c, size_t len)
1.1       kristaps  776: {
1.62      schwarze  777:        char     cp[2];
1.12      kristaps  778:
                    779:        cp[0] = c;
                    780:        cp[1] = '\0';
1.62      schwarze  781:        tbl_fill_string(tp, cp, len);
                    782: }
1.1       kristaps  783:
1.62      schwarze  784: static void
                    785: tbl_fill_border(struct termp *tp, int c, size_t len)
                    786: {
                    787:        char     buf[12];
                    788:
                    789:        if ((c = borders_locale[c]) > 127) {
                    790:                (void)snprintf(buf, sizeof(buf), "\\[u%04x]", c);
                    791:                tbl_fill_string(tp, buf, len);
                    792:        } else
                    793:                tbl_fill_char(tp, c, len);
                    794: }
                    795:
                    796: static void
                    797: tbl_direct_border(struct termp *tp, int c, size_t len)
                    798: {
                    799:        size_t   i, sz;
1.3       kristaps  800:
1.62      schwarze  801:        c = borders_locale[c];
                    802:        sz = (*tp->width)(tp, c);
                    803:        for (i = 0; i < len; i += sz) {
                    804:                (*tp->letter)(tp, c);
                    805:                tp->viscol += sz;
                    806:        }
1.1       kristaps  807: }
                    808:
                    809: static void
1.27      schwarze  810: tbl_literal(struct termp *tp, const struct tbl_dat *dp,
1.11      kristaps  811:                const struct roffcol *col)
1.1       kristaps  812: {
1.36      schwarze  813:        size_t           len, padl, padr, width;
1.61      schwarze  814:        int              ic, hspans;
1.1       kristaps  815:
1.17      kristaps  816:        assert(dp->string);
1.21      schwarze  817:        len = term_strlen(tp, dp->string);
1.23      schwarze  818:        width = col->width;
1.36      schwarze  819:        ic = dp->layout->col;
1.61      schwarze  820:        hspans = dp->hspans;
                    821:        while (hspans--)
1.36      schwarze  822:                width += tp->tbl.cols[++ic].width + 3;
1.23      schwarze  823:
                    824:        padr = width > len ? width - len : 0;
1.21      schwarze  825:        padl = 0;
1.1       kristaps  826:
1.16      kristaps  827:        switch (dp->layout->pos) {
1.27      schwarze  828:        case TBL_CELL_LONG:
1.21      schwarze  829:                padl = term_len(tp, 1);
                    830:                padr = padr > padl ? padr - padl : 0;
1.1       kristaps  831:                break;
1.27      schwarze  832:        case TBL_CELL_CENTRE:
1.21      schwarze  833:                if (2 > padr)
1.19      schwarze  834:                        break;
1.21      schwarze  835:                padl = padr / 2;
1.19      schwarze  836:                padr -= padl;
1.1       kristaps  837:                break;
1.27      schwarze  838:        case TBL_CELL_RIGHT:
1.21      schwarze  839:                padl = padr;
                    840:                padr = 0;
1.1       kristaps  841:                break;
                    842:        default:
                    843:                break;
                    844:        }
                    845:
1.62      schwarze  846:        tbl_fill_char(tp, ASCII_NBRSP, padl);
1.29      schwarze  847:        tbl_word(tp, dp);
1.62      schwarze  848:        tbl_fill_char(tp, ASCII_NBRSP, padr);
1.1       kristaps  849: }
                    850:
                    851: static void
1.25      schwarze  852: tbl_number(struct termp *tp, const struct tbl_opts *opts,
1.2       kristaps  853:                const struct tbl_dat *dp,
1.11      kristaps  854:                const struct roffcol *col)
1.1       kristaps  855: {
1.60      schwarze  856:        const char      *cp, *lastdigit, *lastpoint;
                    857:        size_t           intsz, padl, totsz;
1.11      kristaps  858:        char             buf[2];
1.1       kristaps  859:
                    860:        /*
1.60      schwarze  861:         * Almost the same code as in tblcalc_number():
                    862:         * First find the position of the decimal point.
1.1       kristaps  863:         */
                    864:
1.17      kristaps  865:        assert(dp->string);
1.60      schwarze  866:        lastdigit = lastpoint = NULL;
                    867:        for (cp = dp->string; cp[0] != '\0'; cp++) {
                    868:                if (cp[0] == '\\' && cp[1] == '&') {
                    869:                        lastdigit = lastpoint = cp;
                    870:                        break;
                    871:                } else if (cp[0] == opts->decimal &&
                    872:                    (isdigit((unsigned char)cp[1]) ||
                    873:                     (cp > dp->string && isdigit((unsigned char)cp[-1]))))
                    874:                        lastpoint = cp;
                    875:                else if (isdigit((unsigned char)cp[0]))
                    876:                        lastdigit = cp;
                    877:        }
                    878:
                    879:        /* Then measure both widths. */
1.2       kristaps  880:
1.60      schwarze  881:        padl = 0;
                    882:        totsz = term_strlen(tp, dp->string);
                    883:        if (lastdigit != NULL) {
                    884:                if (lastpoint == NULL)
                    885:                        lastpoint = lastdigit + 1;
                    886:                intsz = 0;
                    887:                buf[1] = '\0';
                    888:                for (cp = dp->string; cp < lastpoint; cp++) {
                    889:                        buf[0] = cp[0];
                    890:                        intsz += term_strlen(tp, buf);
                    891:                }
                    892:
                    893:                /*
                    894:                 * Pad left to match the decimal position,
                    895:                 * but avoid exceeding the total column width.
                    896:                 */
                    897:
                    898:                if (col->decimal > intsz && col->width > totsz) {
                    899:                        padl = col->decimal - intsz;
                    900:                        if (padl + totsz > col->width)
                    901:                                padl = col->width - totsz;
                    902:                }
1.2       kristaps  903:
1.60      schwarze  904:        /* If it is not a number, simply center the string. */
1.2       kristaps  905:
1.60      schwarze  906:        } else if (col->width > totsz)
                    907:                padl = (col->width - totsz) / 2;
                    908:
1.62      schwarze  909:        tbl_fill_char(tp, ASCII_NBRSP, padl);
1.29      schwarze  910:        tbl_word(tp, dp);
1.60      schwarze  911:
                    912:        /* Pad right to fill the column.  */
                    913:
                    914:        if (col->width > padl + totsz)
1.62      schwarze  915:                tbl_fill_char(tp, ASCII_NBRSP, col->width - padl - totsz);
1.2       kristaps  916: }
                    917:
1.29      schwarze  918: static void
                    919: tbl_word(struct termp *tp, const struct tbl_dat *dp)
                    920: {
1.38      schwarze  921:        int              prev_font;
1.29      schwarze  922:
1.38      schwarze  923:        prev_font = tp->fonti;
1.29      schwarze  924:        if (dp->layout->flags & TBL_CELL_BOLD)
                    925:                term_fontpush(tp, TERMFONT_BOLD);
                    926:        else if (dp->layout->flags & TBL_CELL_ITALIC)
                    927:                term_fontpush(tp, TERMFONT_UNDER);
                    928:
                    929:        term_word(tp, dp->string);
                    930:
                    931:        term_fontpopq(tp, prev_font);
                    932: }

CVSweb