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

Annotation of mandoc/tbl_term.c, Revision 1.68

1.68    ! schwarze    1: /*     $Id: tbl_term.c,v 1.67 2019/01/31 16:06:22 schwarze Exp $ */
1.1       kristaps    2: /*
1.20      kristaps    3:  * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.67      schwarze    4:  * Copyright (c) 2011-2019 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.67      schwarze  166:        size_t                   save_offset;
1.47      schwarze  167:        size_t                   coloff, tsz;
1.62      schwarze  168:        int                      hspans, ic, more;
1.68    ! schwarze  169:        int                      dvert, fc, horiz, lhori, rhori, uvert;
1.39      schwarze  170:
1.4       kristaps  171:        /* Inhibit printing of spaces: we do padding ourselves. */
                    172:
1.47      schwarze  173:        tp->flags |= TERMP_NOSPACE | TERMP_NONOSPACE;
1.67      schwarze  174:        save_offset = tp->tcol->offset;
1.4       kristaps  175:
                    176:        /*
1.11      kristaps  177:         * The first time we're invoked for a given table block,
                    178:         * calculate the table widths and decimal positions.
1.4       kristaps  179:         */
                    180:
1.37      schwarze  181:        if (tp->tbl.cols == NULL) {
1.62      schwarze  182:                borders_locale = tp->enc == TERMENC_UTF8 ?
                    183:                    borders_utf8 : borders_ascii;
                    184:
1.11      kristaps  185:                tp->tbl.len = term_tbl_len;
                    186:                tp->tbl.slen = term_tbl_strlen;
1.46      schwarze  187:                tp->tbl.sulen = term_tbl_sulen;
1.11      kristaps  188:                tp->tbl.arg = tp;
1.4       kristaps  189:
1.48      schwarze  190:                tblcalc(&tp->tbl, sp, tp->tcol->offset, tp->tcol->rmargin);
1.54      schwarze  191:
                    192:                /* Tables leak .ta settings to subsequent text. */
                    193:
                    194:                term_tab_set(tp, NULL);
                    195:                coloff = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
                    196:                    sp->opts->lvert;
                    197:                for (ic = 0; ic < sp->opts->cols; ic++) {
                    198:                        coloff += tp->tbl.cols[ic].width;
                    199:                        term_tab_iset(coloff);
1.55      schwarze  200:                        coloff += tp->tbl.cols[ic].spacing;
1.54      schwarze  201:                }
1.1       kristaps  202:
1.34      schwarze  203:                /* Center the table as a whole. */
                    204:
1.45      schwarze  205:                offset = tp->tcol->offset;
1.34      schwarze  206:                if (sp->opts->opts & TBL_OPT_CENTRE) {
                    207:                        tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)
                    208:                            ? 2 : !!sp->opts->lvert + !!sp->opts->rvert;
1.55      schwarze  209:                        for (ic = 0; ic + 1 < sp->opts->cols; ic++)
                    210:                                tsz += tp->tbl.cols[ic].width +
                    211:                                    tp->tbl.cols[ic].spacing;
                    212:                        if (sp->opts->cols)
                    213:                                tsz += tp->tbl.cols[sp->opts->cols - 1].width;
1.45      schwarze  214:                        if (offset + tsz > tp->tcol->rmargin)
1.34      schwarze  215:                                tsz -= 1;
1.67      schwarze  216:                        offset = offset + tp->tcol->rmargin > tsz ?
1.45      schwarze  217:                            (offset + tp->tcol->rmargin - tsz) / 2 : 0;
1.67      schwarze  218:                        tp->tcol->offset = offset;
1.34      schwarze  219:                }
                    220:
1.33      schwarze  221:                /* Horizontal frame at the start of boxed tables. */
1.4       kristaps  222:
1.62      schwarze  223:                if (tp->enc == TERMENC_ASCII &&
                    224:                    sp->opts->opts & TBL_OPT_DBOX)
1.64      schwarze  225:                        tbl_hrule(tp, NULL, sp, TBL_OPT_DBOX);
1.53      schwarze  226:                if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX))
1.64      schwarze  227:                        tbl_hrule(tp, NULL, sp, TBL_OPT_BOX);
1.21      schwarze  228:        }
1.1       kristaps  229:
1.47      schwarze  230:        /* Set up the columns. */
1.1       kristaps  231:
1.47      schwarze  232:        tp->flags |= TERMP_MULTICOL;
1.67      schwarze  233:        tp->tcol->offset = offset;
1.47      schwarze  234:        horiz = 0;
                    235:        switch (sp->pos) {
                    236:        case TBL_SPAN_HORIZ:
                    237:        case TBL_SPAN_DHORIZ:
                    238:                horiz = 1;
                    239:                term_setcol(tp, 1);
                    240:                break;
                    241:        case TBL_SPAN_DATA:
                    242:                term_setcol(tp, sp->opts->cols + 2);
                    243:                coloff = tp->tcol->offset;
                    244:
                    245:                /* Set up a column for a left vertical frame. */
                    246:
                    247:                if (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
                    248:                    sp->opts->lvert)
                    249:                        coloff++;
                    250:                tp->tcol->rmargin = coloff;
1.33      schwarze  251:
1.47      schwarze  252:                /* Set up the data columns. */
1.1       kristaps  253:
1.47      schwarze  254:                dp = sp->first;
1.61      schwarze  255:                hspans = 0;
1.47      schwarze  256:                for (ic = 0; ic < sp->opts->cols; ic++) {
1.61      schwarze  257:                        if (hspans == 0) {
1.47      schwarze  258:                                tp->tcol++;
                    259:                                tp->tcol->offset = coloff;
                    260:                        }
                    261:                        coloff += tp->tbl.cols[ic].width;
                    262:                        tp->tcol->rmargin = coloff;
                    263:                        if (ic + 1 < sp->opts->cols)
1.55      schwarze  264:                                coloff += tp->tbl.cols[ic].spacing;
1.61      schwarze  265:                        if (hspans) {
                    266:                                hspans--;
1.47      schwarze  267:                                continue;
                    268:                        }
                    269:                        if (dp == NULL)
                    270:                                continue;
1.61      schwarze  271:                        hspans = dp->hspans;
1.56      schwarze  272:                        if (ic || sp->layout->first->pos != TBL_CELL_SPAN)
                    273:                                dp = dp->next;
1.47      schwarze  274:                }
                    275:
                    276:                /* Set up a column for a right vertical frame. */
                    277:
                    278:                tp->tcol++;
1.55      schwarze  279:                tp->tcol->offset = coloff + 1;
1.53      schwarze  280:                tp->tcol->rmargin = tp->maxrmargin;
1.47      schwarze  281:
                    282:                /* Spans may have reduced the number of columns. */
                    283:
                    284:                tp->lasttcol = tp->tcol - tp->tcols;
                    285:
                    286:                /* Fill the buffers for all data columns. */
1.4       kristaps  287:
1.47      schwarze  288:                tp->tcol = tp->tcols;
1.53      schwarze  289:                cp = cpn = sp->layout->first;
1.4       kristaps  290:                dp = sp->first;
1.61      schwarze  291:                hspans = 0;
1.36      schwarze  292:                for (ic = 0; ic < sp->opts->cols; ic++) {
1.53      schwarze  293:                        if (cpn != NULL) {
                    294:                                cp = cpn;
                    295:                                cpn = cpn->next;
                    296:                        }
1.61      schwarze  297:                        if (hspans) {
                    298:                                hspans--;
1.47      schwarze  299:                                continue;
                    300:                        }
                    301:                        tp->tcol++;
                    302:                        tp->col = 0;
1.53      schwarze  303:                        tbl_data(tp, sp->opts, cp, dp, tp->tbl.cols + ic);
1.47      schwarze  304:                        if (dp == NULL)
                    305:                                continue;
1.61      schwarze  306:                        hspans = dp->hspans;
1.56      schwarze  307:                        if (cp->pos != TBL_CELL_SPAN)
                    308:                                dp = dp->next;
1.47      schwarze  309:                }
                    310:                break;
                    311:        }
                    312:
                    313:        do {
                    314:                /* Print the vertical frame at the start of each row. */
                    315:
                    316:                tp->tcol = tp->tcols;
1.62      schwarze  317:                uvert = dvert = sp->opts->opts & TBL_OPT_DBOX ? 2 :
                    318:                    sp->opts->opts & TBL_OPT_BOX ? 1 : 0;
                    319:                if (sp->pos == TBL_SPAN_DATA && uvert < sp->layout->vert)
                    320:                        uvert = dvert = sp->layout->vert;
                    321:                if (sp->next != NULL && sp->next->pos == TBL_SPAN_DATA &&
                    322:                    dvert < sp->next->layout->vert)
                    323:                        dvert = sp->next->layout->vert;
                    324:                if (sp->prev != NULL && uvert < sp->prev->layout->vert &&
                    325:                    (horiz || (IS_HORIZ(sp->layout->first) &&
                    326:                      !IS_HORIZ(sp->prev->layout->first))))
                    327:                        uvert = sp->prev->layout->vert;
1.68    ! schwarze  328:                rhori = sp->pos == TBL_SPAN_DHORIZ ||
        !           329:                    (sp->first != NULL && sp->first->pos == TBL_DATA_DHORIZ) ||
1.62      schwarze  330:                    sp->layout->first->pos == TBL_CELL_DHORIZ ? 2 :
                    331:                    sp->pos == TBL_SPAN_HORIZ ||
1.68    ! schwarze  332:                    (sp->first != NULL && sp->first->pos == TBL_DATA_HORIZ) ||
1.62      schwarze  333:                    sp->layout->first->pos == TBL_CELL_HORIZ ? 1 : 0;
1.68    ! schwarze  334:                fc = BUP * uvert + BDOWN * dvert + BRIGHT * rhori;
1.62      schwarze  335:                if (uvert > 0 || dvert > 0 || (horiz && sp->opts->lvert)) {
1.47      schwarze  336:                        (*tp->advance)(tp, tp->tcols->offset);
1.62      schwarze  337:                        tp->viscol = tp->tcol->offset;
                    338:                        tbl_direct_border(tp, fc, 1);
1.47      schwarze  339:                }
1.22      schwarze  340:
1.47      schwarze  341:                /* Print the data cells. */
1.21      schwarze  342:
1.47      schwarze  343:                more = 0;
1.62      schwarze  344:                if (horiz)
1.64      schwarze  345:                        tbl_hrule(tp, sp->prev, sp, 0);
1.62      schwarze  346:                else {
1.47      schwarze  347:                        cp = sp->layout->first;
1.53      schwarze  348:                        cpn = sp->next == NULL ? NULL :
                    349:                            sp->next->layout->first;
                    350:                        cpp = sp->prev == NULL ? NULL :
                    351:                            sp->prev->layout->first;
1.47      schwarze  352:                        dp = sp->first;
1.61      schwarze  353:                        hspans = 0;
1.47      schwarze  354:                        for (ic = 0; ic < sp->opts->cols; ic++) {
                    355:
1.53      schwarze  356:                                /*
                    357:                                 * Figure out whether to print a
                    358:                                 * vertical line after this cell
                    359:                                 * and advance to next layout cell.
                    360:                                 */
1.47      schwarze  361:
1.62      schwarze  362:                                uvert = dvert = fc = 0;
1.47      schwarze  363:                                if (cp != NULL) {
1.62      schwarze  364:                                        cps = cp;
                    365:                                        while (cps->next != NULL &&
                    366:                                            cps->next->pos == TBL_CELL_SPAN)
                    367:                                                cps = cps->next;
                    368:                                        if (sp->pos == TBL_SPAN_DATA)
                    369:                                                uvert = dvert = cps->vert;
1.53      schwarze  370:                                        switch (cp->pos) {
                    371:                                        case TBL_CELL_HORIZ:
1.62      schwarze  372:                                                fc = BHORIZ;
1.53      schwarze  373:                                                break;
                    374:                                        case TBL_CELL_DHORIZ:
1.62      schwarze  375:                                                fc = BHORIZ * 2;
1.53      schwarze  376:                                                break;
                    377:                                        default:
                    378:                                                break;
                    379:                                        }
                    380:                                }
                    381:                                if (cpp != NULL) {
1.62      schwarze  382:                                        if (uvert < cpp->vert &&
1.53      schwarze  383:                                            cp != NULL &&
                    384:                                            ((IS_HORIZ(cp) &&
                    385:                                              !IS_HORIZ(cpp)) ||
                    386:                                             (cp->next != NULL &&
                    387:                                              cpp->next != NULL &&
                    388:                                              IS_HORIZ(cp->next) &&
                    389:                                              !IS_HORIZ(cpp->next))))
1.62      schwarze  390:                                                uvert = cpp->vert;
1.53      schwarze  391:                                        cpp = cpp->next;
                    392:                                }
1.62      schwarze  393:                                if (sp->opts->opts & TBL_OPT_ALLBOX) {
                    394:                                        if (uvert == 0)
                    395:                                                uvert = 1;
                    396:                                        if (dvert == 0)
                    397:                                                dvert = 1;
                    398:                                }
1.53      schwarze  399:                                if (cpn != NULL) {
1.62      schwarze  400:                                        if (dvert == 0 ||
                    401:                                            (dvert < cpn->vert &&
                    402:                                             tp->enc == TERMENC_UTF8))
                    403:                                                dvert = cpn->vert;
1.53      schwarze  404:                                        cpn = cpn->next;
                    405:                                }
1.51      schwarze  406:
1.68    ! schwarze  407:                                lhori = (cp != NULL &&
        !           408:                                     cp->pos == TBL_CELL_DHORIZ) ||
        !           409:                                    (dp != NULL &&
        !           410:                                     dp->pos == TBL_DATA_DHORIZ) ? 2 :
        !           411:                                    (cp != NULL &&
        !           412:                                     cp->pos == TBL_CELL_HORIZ) ||
        !           413:                                    (dp != NULL &&
        !           414:                                     dp->pos == TBL_DATA_HORIZ) ? 1 : 0;
        !           415:
1.53      schwarze  416:                                /*
                    417:                                 * Skip later cells in a span,
                    418:                                 * figure out whether to start a span,
                    419:                                 * and advance to next data cell.
                    420:                                 */
1.51      schwarze  421:
1.61      schwarze  422:                                if (hspans) {
                    423:                                        hspans--;
1.62      schwarze  424:                                        cp = cp->next;
1.51      schwarze  425:                                        continue;
                    426:                                }
                    427:                                if (dp != NULL) {
1.61      schwarze  428:                                        hspans = dp->hspans;
1.56      schwarze  429:                                        if (ic || sp->layout->first->pos
                    430:                                            != TBL_CELL_SPAN)
                    431:                                                dp = dp->next;
1.51      schwarze  432:                                }
                    433:
1.53      schwarze  434:                                /*
                    435:                                 * Print one line of text in the cell
                    436:                                 * and remember whether there is more.
                    437:                                 */
1.51      schwarze  438:
                    439:                                tp->tcol++;
                    440:                                if (tp->tcol->col < tp->tcol->lastcol)
                    441:                                        term_flushln(tp);
                    442:                                if (tp->tcol->col < tp->tcol->lastcol)
                    443:                                        more = 1;
                    444:
                    445:                                /*
                    446:                                 * Vertical frames between data cells,
                    447:                                 * but not after the last column.
                    448:                                 */
                    449:
1.63      schwarze  450:                                if (fc == 0 &&
                    451:                                    ((uvert == 0 && dvert == 0 &&
                    452:                                      cp != NULL && (cp->next == NULL ||
1.62      schwarze  453:                                      !IS_HORIZ(cp->next))) ||
1.63      schwarze  454:                                     tp->tcol + 1 ==
                    455:                                      tp->tcols + tp->lasttcol)) {
                    456:                                        if (cp != NULL)
                    457:                                                cp = cp->next;
1.53      schwarze  458:                                        continue;
1.62      schwarze  459:                                }
1.53      schwarze  460:
1.55      schwarze  461:                                if (tp->viscol < tp->tcol->rmargin) {
1.53      schwarze  462:                                        (*tp->advance)(tp, tp->tcol->rmargin
                    463:                                           - tp->viscol);
                    464:                                        tp->viscol = tp->tcol->rmargin;
                    465:                                }
1.55      schwarze  466:                                while (tp->viscol < tp->tcol->rmargin +
1.62      schwarze  467:                                    tp->tbl.cols[ic].spacing / 2)
1.68    ! schwarze  468:                                        tbl_direct_border(tp,
        !           469:                                            BHORIZ * lhori, 1);
1.53      schwarze  470:
1.51      schwarze  471:                                if (tp->tcol + 1 == tp->tcols + tp->lasttcol)
                    472:                                        continue;
1.47      schwarze  473:
1.68    ! schwarze  474:                                if (cp != NULL)
1.62      schwarze  475:                                        cp = cp->next;
1.68    ! schwarze  476:
        !           477:                                rhori = (cp != NULL &&
        !           478:                                     cp->pos == TBL_CELL_DHORIZ) ||
        !           479:                                    (dp != NULL &&
        !           480:                                     dp->pos == TBL_DATA_DHORIZ) ? 2 :
        !           481:                                    (cp != NULL &&
        !           482:                                     cp->pos == TBL_CELL_HORIZ) ||
        !           483:                                    (dp != NULL &&
        !           484:                                     dp->pos == TBL_DATA_HORIZ) ? 1 : 0;
        !           485:
1.62      schwarze  486:                                if (tp->tbl.cols[ic].spacing)
1.68    ! schwarze  487:                                        tbl_direct_border(tp,
        !           488:                                            BLEFT * lhori + BRIGHT * rhori +
1.62      schwarze  489:                                            BUP * uvert + BDOWN * dvert, 1);
                    490:
                    491:                                if (tp->enc == TERMENC_UTF8)
                    492:                                        uvert = dvert = 0;
1.53      schwarze  493:
1.55      schwarze  494:                                if (tp->tbl.cols[ic].spacing > 2 &&
1.68    ! schwarze  495:                                    (uvert > 1 || dvert > 1 || rhori))
        !           496:                                        tbl_direct_border(tp,
        !           497:                                            BHORIZ * rhori +
1.62      schwarze  498:                                            BUP * (uvert > 1) +
                    499:                                            BDOWN * (dvert > 1), 1);
1.47      schwarze  500:                        }
                    501:                }
1.1       kristaps  502:
1.47      schwarze  503:                /* Print the vertical frame at the end of each row. */
1.16      kristaps  504:
1.62      schwarze  505:                uvert = dvert = sp->opts->opts & TBL_OPT_DBOX ? 2 :
                    506:                    sp->opts->opts & TBL_OPT_BOX ? 1 : 0;
                    507:                if (sp->pos == TBL_SPAN_DATA &&
                    508:                    uvert < sp->layout->last->vert &&
                    509:                    sp->layout->last->col + 1 == sp->opts->cols)
                    510:                        uvert = dvert = sp->layout->last->vert;
                    511:                if (sp->next != NULL &&
                    512:                    dvert < sp->next->layout->last->vert &&
                    513:                    sp->next->layout->last->col + 1 == sp->opts->cols)
                    514:                        dvert = sp->next->layout->last->vert;
                    515:                if (sp->prev != NULL &&
                    516:                    uvert < sp->prev->layout->last->vert &&
                    517:                    sp->prev->layout->last->col + 1 == sp->opts->cols &&
                    518:                    (horiz || (IS_HORIZ(sp->layout->last) &&
                    519:                     !IS_HORIZ(sp->prev->layout->last))))
                    520:                        uvert = sp->prev->layout->last->vert;
1.68    ! schwarze  521:                lhori = sp->pos == TBL_SPAN_DHORIZ ||
        !           522:                    (sp->last != NULL &&
        !           523:                     sp->last->pos == TBL_DATA_DHORIZ &&
        !           524:                     sp->last->layout->col + 1 == sp->opts->cols) ||
1.62      schwarze  525:                    (sp->layout->last->pos == TBL_CELL_DHORIZ &&
                    526:                     sp->layout->last->col + 1 == sp->opts->cols) ? 2 :
                    527:                    sp->pos == TBL_SPAN_HORIZ ||
1.68    ! schwarze  528:                    (sp->last != NULL &&
        !           529:                     sp->last->pos == TBL_DATA_HORIZ &&
        !           530:                     sp->last->layout->col + 1 == sp->opts->cols) ||
1.62      schwarze  531:                    (sp->layout->last->pos == TBL_CELL_HORIZ &&
                    532:                     sp->layout->last->col + 1 == sp->opts->cols) ? 1 : 0;
1.68    ! schwarze  533:                fc = BUP * uvert + BDOWN * dvert + BLEFT * lhori;
1.62      schwarze  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++;
1.68    ! schwarze  538:                                do {
        !           539:                                        tbl_direct_border(tp,
        !           540:                                            BHORIZ * lhori, 1);
        !           541:                                } while (tp->viscol < tp->tcol->offset);
1.47      schwarze  542:                        }
1.62      schwarze  543:                        tbl_direct_border(tp, fc, 1);
1.47      schwarze  544:                }
                    545:                (*tp->endline)(tp);
                    546:                tp->viscol = 0;
                    547:        } while (more);
1.1       kristaps  548:
1.4       kristaps  549:        /*
1.53      schwarze  550:         * Clean up after this row.  If it is the last line
                    551:         * of the table, print the box line and clean up
                    552:         * column data; otherwise, print the allbox line.
1.4       kristaps  553:         */
                    554:
1.47      schwarze  555:        term_setcol(tp, 1);
                    556:        tp->flags &= ~TERMP_MULTICOL;
                    557:        tp->tcol->rmargin = tp->maxrmargin;
1.37      schwarze  558:        if (sp->next == NULL) {
1.33      schwarze  559:                if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
1.64      schwarze  560:                        tbl_hrule(tp, sp, NULL, TBL_OPT_BOX);
1.24      schwarze  561:                        tp->skipvsp = 1;
                    562:                }
1.62      schwarze  563:                if (tp->enc == TERMENC_ASCII &&
                    564:                    sp->opts->opts & TBL_OPT_DBOX) {
1.64      schwarze  565:                        tbl_hrule(tp, sp, NULL, TBL_OPT_DBOX);
1.24      schwarze  566:                        tp->skipvsp = 2;
                    567:                }
1.11      kristaps  568:                assert(tp->tbl.cols);
                    569:                free(tp->tbl.cols);
                    570:                tp->tbl.cols = NULL;
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.67      schwarze  576:        tp->tcol->offset = save_offset;
1.47      schwarze  577:        tp->flags &= ~TERMP_NONOSPACE;
1.1       kristaps  578: }
                    579:
                    580: static void
1.64      schwarze  581: tbl_hrule(struct termp *tp, const struct tbl_span *spp,
                    582:     const struct tbl_span *spn, int flags)
1.1       kristaps  583: {
1.65      schwarze  584:        const struct tbl_cell   *cpp;    /* Layout cell above this line. */
                    585:        const struct tbl_cell   *cpn;    /* Layout cell below this line. */
                    586:        const struct tbl_dat    *dpn;    /* Data cell below this line. */
1.64      schwarze  587:        const struct roffcol    *col;    /* Contains width and spacing. */
                    588:        int                      opts;   /* For the table as a whole. */
                    589:        int                      bw;     /* Box line width. */
                    590:        int                      hw;     /* Horizontal line width. */
                    591:        int                      lw, rw; /* Left and right line widths. */
                    592:        int                      uw, dw; /* Vertical line widths. */
                    593:
                    594:        cpp = spp == NULL ? NULL : spp->layout->first;
                    595:        cpn = spn == NULL ? NULL : spn->layout->first;
1.65      schwarze  596:        dpn = NULL;
                    597:        if (spn != NULL) {
                    598:                if (spn->pos == TBL_SPAN_DATA)
                    599:                        dpn = spn->first;
                    600:                else if (spn->next != NULL)
                    601:                        dpn = spn->next->first;
                    602:        }
1.64      schwarze  603:        opts = spn == NULL ? spp->opts->opts : spn->opts->opts;
                    604:        bw = opts & TBL_OPT_DBOX ? (tp->enc == TERMENC_UTF8 ? 2 : 1) :
                    605:            opts & (TBL_OPT_BOX | TBL_OPT_ALLBOX) ? 1 : 0;
                    606:        hw = flags == TBL_OPT_DBOX || flags == TBL_OPT_BOX ? bw :
                    607:            spn->pos == TBL_SPAN_DHORIZ ? 2 : 1;
                    608:
                    609:        /* Print the left end of the line. */
                    610:
1.62      schwarze  611:        if (tp->viscol == 0) {
                    612:                (*tp->advance)(tp, tp->tcols->offset);
                    613:                tp->viscol = tp->tcols->offset;
                    614:        }
1.64      schwarze  615:        if (flags != 0)
                    616:                tbl_direct_border(tp,
                    617:                    (spp == NULL ? 0 : BUP * bw) +
                    618:                    (spn == NULL ? 0 : BDOWN * bw) +
                    619:                    (spp == NULL || cpn == NULL ||
                    620:                     cpn->pos != TBL_CELL_DOWN ? BRIGHT * hw : 0), 1);
                    621:
1.33      schwarze  622:        for (;;) {
1.64      schwarze  623:                col = tp->tbl.cols + (cpn == NULL ? cpp->col : cpn->col);
                    624:
                    625:                /* Print the horizontal line inside this column. */
                    626:
                    627:                lw = cpp == NULL || cpn == NULL ||
1.65      schwarze  628:                    (cpn->pos != TBL_CELL_DOWN &&
                    629:                     (dpn == NULL || strcmp(dpn->string, "\\^") != 0))
                    630:                    ? hw : 0;
1.64      schwarze  631:                tbl_direct_border(tp, BHORIZ * lw,
                    632:                    col->width + col->spacing / 2);
                    633:
                    634:                /*
                    635:                 * Figure out whether a vertical line is crossing
                    636:                 * at the end of this column,
                    637:                 * and advance to the next column.
                    638:                 */
                    639:
                    640:                uw = dw = 0;
1.53      schwarze  641:                if (cpp != NULL) {
1.64      schwarze  642:                        if (flags != TBL_OPT_DBOX) {
                    643:                                uw = cpp->vert;
                    644:                                if (uw == 0 && opts & TBL_OPT_ALLBOX)
                    645:                                        uw = 1;
                    646:                        }
1.53      schwarze  647:                        cpp = cpp->next;
                    648:                }
                    649:                if (cpn != NULL) {
1.64      schwarze  650:                        if (flags != TBL_OPT_DBOX) {
                    651:                                dw = cpn->vert;
                    652:                                if (dw == 0 && opts & TBL_OPT_ALLBOX)
                    653:                                        dw = 1;
                    654:                        }
1.53      schwarze  655:                        cpn = cpn->next;
1.65      schwarze  656:                        while (dpn != NULL && dpn->layout != cpn)
                    657:                                dpn = dpn->next;
1.33      schwarze  658:                }
1.64      schwarze  659:                if (cpp == NULL && cpn == NULL)
                    660:                        break;
                    661:
                    662:                /* Vertical lines do not cross spanned cells. */
                    663:
                    664:                if (cpp != NULL && cpp->pos == TBL_CELL_SPAN)
                    665:                        uw = 0;
                    666:                if (cpn != NULL && cpn->pos == TBL_CELL_SPAN)
                    667:                        dw = 0;
                    668:
                    669:                /* The horizontal line inside the next column. */
                    670:
                    671:                rw = cpp == NULL || cpn == NULL ||
1.65      schwarze  672:                    (cpn->pos != TBL_CELL_DOWN &&
                    673:                     (dpn == NULL || strcmp(dpn->string, "\\^") != 0))
                    674:                    ? hw : 0;
1.64      schwarze  675:
                    676:                /* The line crossing at the end of this column. */
                    677:
1.55      schwarze  678:                if (col->spacing)
1.64      schwarze  679:                        tbl_direct_border(tp, BLEFT * lw +
                    680:                            BRIGHT * rw + BUP * uw + BDOWN * dw, 1);
                    681:
                    682:                /*
                    683:                 * In ASCII output, a crossing may print two characters.
                    684:                 */
                    685:
                    686:                if (tp->enc != TERMENC_ASCII || (uw < 2 && dw < 2))
                    687:                        uw = dw = 0;
1.55      schwarze  688:                if (col->spacing > 2)
1.64      schwarze  689:                        tbl_direct_border(tp,
                    690:                             BHORIZ * rw + BUP * uw + BDOWN * dw, 1);
                    691:
                    692:                /* Padding before the start of the next column. */
                    693:
1.55      schwarze  694:                if (col->spacing > 4)
1.64      schwarze  695:                        tbl_direct_border(tp,
                    696:                            BHORIZ * rw, (col->spacing - 3) / 2);
1.22      schwarze  697:        }
1.64      schwarze  698:
                    699:        /* Print the right end of the line. */
                    700:
                    701:        if (flags != 0) {
                    702:                tbl_direct_border(tp,
                    703:                    (spp == NULL ? 0 : BUP * bw) +
                    704:                    (spn == NULL ? 0 : BDOWN * bw) +
                    705:                    (spp == NULL || spn == NULL ||
                    706:                     spn->layout->last->pos != TBL_CELL_DOWN ?
                    707:                     BLEFT * hw : 0), 1);
1.62      schwarze  708:                (*tp->endline)(tp);
                    709:                tp->viscol = 0;
1.22      schwarze  710:        }
1.1       kristaps  711: }
                    712:
                    713: static void
1.25      schwarze  714: tbl_data(struct termp *tp, const struct tbl_opts *opts,
1.53      schwarze  715:     const struct tbl_cell *cp, const struct tbl_dat *dp,
                    716:     const struct roffcol *col)
1.1       kristaps  717: {
1.53      schwarze  718:        switch (cp->pos) {
                    719:        case TBL_CELL_HORIZ:
1.62      schwarze  720:                tbl_fill_border(tp, BHORIZ, col->width);
1.53      schwarze  721:                return;
                    722:        case TBL_CELL_DHORIZ:
1.62      schwarze  723:                tbl_fill_border(tp, BHORIZ * 2, col->width);
1.53      schwarze  724:                return;
                    725:        default:
                    726:                break;
                    727:        }
1.1       kristaps  728:
1.56      schwarze  729:        if (dp == NULL)
1.1       kristaps  730:                return;
                    731:
                    732:        switch (dp->pos) {
1.27      schwarze  733:        case TBL_DATA_NONE:
1.10      kristaps  734:                return;
1.27      schwarze  735:        case TBL_DATA_HORIZ:
                    736:        case TBL_DATA_NHORIZ:
1.62      schwarze  737:                tbl_fill_border(tp, BHORIZ, col->width);
1.7       kristaps  738:                return;
1.27      schwarze  739:        case TBL_DATA_NDHORIZ:
                    740:        case TBL_DATA_DHORIZ:
1.62      schwarze  741:                tbl_fill_border(tp, BHORIZ * 2, col->width);
1.1       kristaps  742:                return;
                    743:        default:
                    744:                break;
                    745:        }
1.27      schwarze  746:
1.53      schwarze  747:        switch (cp->pos) {
1.27      schwarze  748:        case TBL_CELL_LONG:
                    749:        case TBL_CELL_CENTRE:
                    750:        case TBL_CELL_LEFT:
                    751:        case TBL_CELL_RIGHT:
1.11      kristaps  752:                tbl_literal(tp, dp, col);
1.1       kristaps  753:                break;
1.27      schwarze  754:        case TBL_CELL_NUMBER:
1.25      schwarze  755:                tbl_number(tp, opts, dp, col);
1.18      kristaps  756:                break;
1.27      schwarze  757:        case TBL_CELL_DOWN:
1.56      schwarze  758:        case TBL_CELL_SPAN:
1.1       kristaps  759:                break;
                    760:        default:
                    761:                abort();
                    762:        }
                    763: }
                    764:
                    765: static void
1.62      schwarze  766: tbl_fill_string(struct termp *tp, const char *cp, size_t len)
                    767: {
                    768:        size_t   i, sz;
                    769:
                    770:        sz = term_strlen(tp, cp);
                    771:        for (i = 0; i < len; i += sz)
                    772:                term_word(tp, cp);
                    773: }
                    774:
                    775: static void
                    776: tbl_fill_char(struct termp *tp, char c, size_t len)
1.1       kristaps  777: {
1.62      schwarze  778:        char     cp[2];
1.12      kristaps  779:
                    780:        cp[0] = c;
                    781:        cp[1] = '\0';
1.62      schwarze  782:        tbl_fill_string(tp, cp, len);
                    783: }
1.1       kristaps  784:
1.62      schwarze  785: static void
                    786: tbl_fill_border(struct termp *tp, int c, size_t len)
                    787: {
                    788:        char     buf[12];
                    789:
                    790:        if ((c = borders_locale[c]) > 127) {
                    791:                (void)snprintf(buf, sizeof(buf), "\\[u%04x]", c);
                    792:                tbl_fill_string(tp, buf, len);
                    793:        } else
                    794:                tbl_fill_char(tp, c, len);
                    795: }
                    796:
                    797: static void
                    798: tbl_direct_border(struct termp *tp, int c, size_t len)
                    799: {
                    800:        size_t   i, sz;
1.3       kristaps  801:
1.62      schwarze  802:        c = borders_locale[c];
                    803:        sz = (*tp->width)(tp, c);
                    804:        for (i = 0; i < len; i += sz) {
                    805:                (*tp->letter)(tp, c);
                    806:                tp->viscol += sz;
                    807:        }
1.1       kristaps  808: }
                    809:
                    810: static void
1.27      schwarze  811: tbl_literal(struct termp *tp, const struct tbl_dat *dp,
1.11      kristaps  812:                const struct roffcol *col)
1.1       kristaps  813: {
1.36      schwarze  814:        size_t           len, padl, padr, width;
1.61      schwarze  815:        int              ic, hspans;
1.1       kristaps  816:
1.17      kristaps  817:        assert(dp->string);
1.21      schwarze  818:        len = term_strlen(tp, dp->string);
1.23      schwarze  819:        width = col->width;
1.36      schwarze  820:        ic = dp->layout->col;
1.61      schwarze  821:        hspans = dp->hspans;
                    822:        while (hspans--)
1.36      schwarze  823:                width += tp->tbl.cols[++ic].width + 3;
1.23      schwarze  824:
                    825:        padr = width > len ? width - len : 0;
1.21      schwarze  826:        padl = 0;
1.1       kristaps  827:
1.16      kristaps  828:        switch (dp->layout->pos) {
1.27      schwarze  829:        case TBL_CELL_LONG:
1.21      schwarze  830:                padl = term_len(tp, 1);
                    831:                padr = padr > padl ? padr - padl : 0;
1.1       kristaps  832:                break;
1.27      schwarze  833:        case TBL_CELL_CENTRE:
1.21      schwarze  834:                if (2 > padr)
1.19      schwarze  835:                        break;
1.21      schwarze  836:                padl = padr / 2;
1.19      schwarze  837:                padr -= padl;
1.1       kristaps  838:                break;
1.27      schwarze  839:        case TBL_CELL_RIGHT:
1.21      schwarze  840:                padl = padr;
                    841:                padr = 0;
1.1       kristaps  842:                break;
                    843:        default:
                    844:                break;
                    845:        }
                    846:
1.62      schwarze  847:        tbl_fill_char(tp, ASCII_NBRSP, padl);
1.29      schwarze  848:        tbl_word(tp, dp);
1.62      schwarze  849:        tbl_fill_char(tp, ASCII_NBRSP, padr);
1.1       kristaps  850: }
                    851:
                    852: static void
1.25      schwarze  853: tbl_number(struct termp *tp, const struct tbl_opts *opts,
1.2       kristaps  854:                const struct tbl_dat *dp,
1.11      kristaps  855:                const struct roffcol *col)
1.1       kristaps  856: {
1.60      schwarze  857:        const char      *cp, *lastdigit, *lastpoint;
                    858:        size_t           intsz, padl, totsz;
1.11      kristaps  859:        char             buf[2];
1.1       kristaps  860:
                    861:        /*
1.60      schwarze  862:         * Almost the same code as in tblcalc_number():
                    863:         * First find the position of the decimal point.
1.1       kristaps  864:         */
                    865:
1.17      kristaps  866:        assert(dp->string);
1.60      schwarze  867:        lastdigit = lastpoint = NULL;
                    868:        for (cp = dp->string; cp[0] != '\0'; cp++) {
                    869:                if (cp[0] == '\\' && cp[1] == '&') {
                    870:                        lastdigit = lastpoint = cp;
                    871:                        break;
                    872:                } else if (cp[0] == opts->decimal &&
                    873:                    (isdigit((unsigned char)cp[1]) ||
                    874:                     (cp > dp->string && isdigit((unsigned char)cp[-1]))))
                    875:                        lastpoint = cp;
                    876:                else if (isdigit((unsigned char)cp[0]))
                    877:                        lastdigit = cp;
                    878:        }
                    879:
                    880:        /* Then measure both widths. */
1.2       kristaps  881:
1.60      schwarze  882:        padl = 0;
                    883:        totsz = term_strlen(tp, dp->string);
                    884:        if (lastdigit != NULL) {
                    885:                if (lastpoint == NULL)
                    886:                        lastpoint = lastdigit + 1;
                    887:                intsz = 0;
                    888:                buf[1] = '\0';
                    889:                for (cp = dp->string; cp < lastpoint; cp++) {
                    890:                        buf[0] = cp[0];
                    891:                        intsz += term_strlen(tp, buf);
                    892:                }
                    893:
                    894:                /*
                    895:                 * Pad left to match the decimal position,
                    896:                 * but avoid exceeding the total column width.
                    897:                 */
                    898:
                    899:                if (col->decimal > intsz && col->width > totsz) {
                    900:                        padl = col->decimal - intsz;
                    901:                        if (padl + totsz > col->width)
                    902:                                padl = col->width - totsz;
                    903:                }
1.2       kristaps  904:
1.60      schwarze  905:        /* If it is not a number, simply center the string. */
1.2       kristaps  906:
1.60      schwarze  907:        } else if (col->width > totsz)
                    908:                padl = (col->width - totsz) / 2;
                    909:
1.62      schwarze  910:        tbl_fill_char(tp, ASCII_NBRSP, padl);
1.29      schwarze  911:        tbl_word(tp, dp);
1.60      schwarze  912:
                    913:        /* Pad right to fill the column.  */
                    914:
                    915:        if (col->width > padl + totsz)
1.62      schwarze  916:                tbl_fill_char(tp, ASCII_NBRSP, col->width - padl - totsz);
1.2       kristaps  917: }
                    918:
1.29      schwarze  919: static void
                    920: tbl_word(struct termp *tp, const struct tbl_dat *dp)
                    921: {
1.38      schwarze  922:        int              prev_font;
1.29      schwarze  923:
1.38      schwarze  924:        prev_font = tp->fonti;
1.29      schwarze  925:        if (dp->layout->flags & TBL_CELL_BOLD)
                    926:                term_fontpush(tp, TERMFONT_BOLD);
                    927:        else if (dp->layout->flags & TBL_CELL_ITALIC)
                    928:                term_fontpush(tp, TERMFONT_UNDER);
                    929:
                    930:        term_word(tp, dp->string);
                    931:
                    932:        term_fontpopq(tp, prev_font);
                    933: }

CVSweb