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

Annotation of mandoc/tbl_term.c, Revision 1.54

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

CVSweb