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

Annotation of mandoc/term.c, Revision 1.279

1.279   ! schwarze    1: /*     $Id: term.c,v 1.278 2019/01/03 19:59:55 schwarze Exp $ */
1.1       kristaps    2: /*
1.198     schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.278     schwarze    4:  * Copyright (c) 2010-2019 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.74      kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.246     schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.74      kristaps   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.246     schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.74      kristaps   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.
1.1       kristaps   17:  */
1.128     kristaps   18: #include "config.h"
                     19:
1.126     kristaps   20: #include <sys/types.h>
                     21:
1.1       kristaps   22: #include <assert.h>
1.122     kristaps   23: #include <ctype.h>
1.278     schwarze   24: #include <stdint.h>
1.22      kristaps   25: #include <stdio.h>
1.1       kristaps   26: #include <stdlib.h>
                     27: #include <string.h>
                     28:
1.137     kristaps   29: #include "mandoc.h"
1.218     schwarze   30: #include "mandoc_aux.h"
1.107     kristaps   31: #include "out.h"
1.71      kristaps   32: #include "term.h"
1.105     kristaps   33: #include "main.h"
1.1       kristaps   34:
1.203     schwarze   35: static size_t           cond_width(const struct termp *, int, int *);
1.266     schwarze   36: static void             adjbuf(struct termp_col *, size_t);
1.191     kristaps   37: static void             bufferc(struct termp *, char);
                     38: static void             encode(struct termp *, const char *, size_t);
1.194     kristaps   39: static void             encode1(struct termp *, int);
1.264     schwarze   40: static void             endline(struct termp *);
1.278     schwarze   41: static void             term_field(struct termp *, size_t, size_t,
                     42:                                size_t, size_t);
                     43: static void             term_fill(struct termp *, size_t *, size_t *,
                     44:                                size_t);
1.11      kristaps   45:
1.222     schwarze   46:
1.145     kristaps   47: void
1.269     schwarze   48: term_setcol(struct termp *p, size_t maxtcol)
                     49: {
                     50:        if (maxtcol > p->maxtcol) {
                     51:                p->tcols = mandoc_recallocarray(p->tcols,
                     52:                    p->maxtcol, maxtcol, sizeof(*p->tcols));
                     53:                p->maxtcol = maxtcol;
                     54:        }
                     55:        p->lasttcol = maxtcol - 1;
                     56:        p->tcol = p->tcols;
                     57: }
                     58:
                     59: void
1.71      kristaps   60: term_free(struct termp *p)
1.14      kristaps   61: {
1.266     schwarze   62:        for (p->tcol = p->tcols; p->tcol < p->tcols + p->maxtcol; p->tcol++)
                     63:                free(p->tcol->buf);
                     64:        free(p->tcols);
1.238     schwarze   65:        free(p->fontq);
1.142     kristaps   66:        free(p);
                     67: }
                     68:
                     69: void
1.222     schwarze   70: term_begin(struct termp *p, term_margin head,
1.246     schwarze   71:                term_margin foot, const struct roff_meta *arg)
1.142     kristaps   72: {
                     73:
                     74:        p->headf = head;
                     75:        p->footf = foot;
                     76:        p->argf = arg;
1.146     kristaps   77:        (*p->begin)(p);
1.142     kristaps   78: }
                     79:
                     80: void
                     81: term_end(struct termp *p)
                     82: {
                     83:
1.146     kristaps   84:        (*p->end)(p);
1.14      kristaps   85: }
                     86:
1.71      kristaps   87: /*
1.221     schwarze   88:  * Flush a chunk of text.  By default, break the output line each time
                     89:  * the right margin is reached, and continue output on the next line
                     90:  * at the same offset as the chunk itself.  By default, also break the
1.278     schwarze   91:  * output line at the end of the chunk.  There are many flags modifying
                     92:  * this behaviour, see the comments in the body of the function.
1.71      kristaps   93:  */
                     94: void
                     95: term_flushln(struct termp *p)
1.53      kristaps   96: {
1.278     schwarze   97:        size_t   vbl;      /* Number of blanks to prepend to the output. */
                     98:        size_t   vbr;      /* Actual visual position of the end of field. */
                     99:        size_t   vfield;   /* Desired visual field width. */
                    100:        size_t   vtarget;  /* Desired visual position of the right margin. */
                    101:        size_t   ic;       /* Character position in the input buffer. */
                    102:        size_t   nbr;      /* Number of characters to print in this field. */
                    103:
                    104:        /*
                    105:         * Normally, start writing at the left margin, but with the
                    106:         * NOPAD flag, start writing at the current position instead.
                    107:         */
1.53      kristaps  108:
1.266     schwarze  109:        vbl = (p->flags & TERMP_NOPAD) || p->tcol->offset < p->viscol ?
                    110:            0 : p->tcol->offset - p->viscol;
1.263     schwarze  111:        if (p->minbl && vbl < p->minbl)
                    112:                vbl = p->minbl;
1.115     kristaps  113:
1.274     schwarze  114:        if ((p->flags & TERMP_MULTICOL) == 0)
1.267     schwarze  115:                p->tcol->col = 0;
1.278     schwarze  116:
                    117:        /* Loop over output lines. */
                    118:
                    119:        for (;;) {
                    120:                vfield = p->tcol->rmargin > p->viscol + vbl ?
                    121:                    p->tcol->rmargin - p->viscol - vbl : 0;
1.267     schwarze  122:
1.71      kristaps  123:                /*
1.278     schwarze  124:                 * Normally, break the line at the the right margin
                    125:                 * of the field, but with the NOBREAK flag, only
                    126:                 * break it at the max right margin of the screen,
                    127:                 * and with the BRNEVER flag, never break it at all.
1.138     schwarze  128:                 */
1.267     schwarze  129:
1.278     schwarze  130:                vtarget = p->flags & TERMP_BRNEVER ? SIZE_MAX :
                    131:                    (p->flags & TERMP_NOBREAK) == 0 ? vfield :
                    132:                    p->maxrmargin > p->viscol + vbl ?
                    133:                    p->maxrmargin - p->viscol - vbl : 0;
                    134:
                    135:                /*
                    136:                 * Figure out how much text will fit in the field.
                    137:                 * If there is whitespace only, print nothing.
                    138:                 */
                    139:
                    140:                term_fill(p, &nbr, &vbr, vtarget);
                    141:                if (nbr == 0)
                    142:                        break;
1.279   ! schwarze  143:
        !           144:                /*
        !           145:                 * With the CENTER or RIGHT flag, increase the indentation
        !           146:                 * to center the text between the left and right margins
        !           147:                 * or to adjust it to the right margin, respectively.
        !           148:                 */
        !           149:
        !           150:                if (vbr < vtarget) {
        !           151:                        if (p->flags & TERMP_CENTER)
        !           152:                                vbl += (vtarget - vbr) / 2;
        !           153:                        else if (p->flags & TERMP_RIGHT)
        !           154:                                vbl += vtarget - vbr;
        !           155:                }
        !           156:
        !           157:                /* Finally, print the field content. */
1.278     schwarze  158:
                    159:                term_field(p, vbl, nbr, vbr, vtarget);
1.138     schwarze  160:
                    161:                /*
1.278     schwarze  162:                 * If there is no text left in the field, exit the loop.
                    163:                 * If the BRTRSP flag is set, consider trailing
                    164:                 * whitespace significant when deciding whether
                    165:                 * the field fits or not.
1.71      kristaps  166:                 */
                    167:
1.278     schwarze  168:                for (ic = p->tcol->col; ic < p->tcol->lastcol; ic++) {
                    169:                        switch (p->tcol->buf[ic]) {
                    170:                        case '\t':
                    171:                                if (p->flags & TERMP_BRTRSP)
                    172:                                        vbr = term_tab_next(vbr);
                    173:                                continue;
                    174:                        case ' ':
                    175:                                if (p->flags & TERMP_BRTRSP)
                    176:                                        vbr += (*p->width)(p, ' ');
1.270     schwarze  177:                                continue;
1.278     schwarze  178:                        case '\n':
                    179:                        case ASCII_BREAK:
                    180:                                continue;
                    181:                        default:
                    182:                                break;
1.270     schwarze  183:                        }
1.278     schwarze  184:                        break;
                    185:                }
                    186:                if (ic == p->tcol->lastcol)
                    187:                        break;
1.154     kristaps  188:
1.278     schwarze  189:                /*
                    190:                 * At the location of an automtic line break, input
                    191:                 * space characters are consumed by the line break.
                    192:                 */
1.154     kristaps  193:
1.278     schwarze  194:                while (p->tcol->col < p->tcol->lastcol &&
                    195:                    p->tcol->buf[p->tcol->col] == ' ')
                    196:                        p->tcol->col++;
1.154     kristaps  197:
1.278     schwarze  198:                /*
                    199:                 * In multi-column mode, leave the rest of the text
                    200:                 * in the buffer to be handled by a subsequent
                    201:                 * invocation, such that the other columns of the
                    202:                 * table can be handled first.
                    203:                 * In single-column mode, simply break the line.
                    204:                 */
                    205:
                    206:                if (p->flags & TERMP_MULTICOL)
                    207:                        return;
1.217     schwarze  208:
1.278     schwarze  209:                endline(p);
                    210:                p->viscol = 0;
1.53      kristaps  211:
1.71      kristaps  212:                /*
1.278     schwarze  213:                 * Normally, start the next line at the same indentation
                    214:                 * as this one, but with the BRIND flag, start it at the
                    215:                 * right margin instead.  This is used together with
                    216:                 * NOBREAK for the tags in various kinds of tagged lists.
1.81      kristaps  217:                 */
1.267     schwarze  218:
1.278     schwarze  219:                vbl = p->flags & TERMP_BRIND ?
                    220:                    p->tcol->rmargin : p->tcol->offset;
                    221:        }
1.267     schwarze  222:
1.278     schwarze  223:        /* Reset output state in preparation for the next field. */
1.205     schwarze  224:
1.278     schwarze  225:        p->col = p->tcol->col = p->tcol->lastcol = 0;
                    226:        p->minbl = p->trailspace;
                    227:        p->flags &= ~(TERMP_BACKAFTER | TERMP_BACKBEFORE | TERMP_NOPAD);
1.260     schwarze  228:
1.278     schwarze  229:        if (p->flags & TERMP_MULTICOL)
                    230:                return;
1.260     schwarze  231:
1.278     schwarze  232:        /*
                    233:         * The HANG flag means that the next field
                    234:         * always follows on the same line.
                    235:         * The NOBREAK flag means that the next field
                    236:         * follows on the same line unless the field was overrun.
                    237:         * Normally, break the line at the end of each field.
                    238:         */
1.205     schwarze  239:
1.278     schwarze  240:        if ((p->flags & TERMP_HANG) == 0 &&
                    241:            ((p->flags & TERMP_NOBREAK) == 0 ||
                    242:             vbr + term_len(p, p->trailspace) > vfield))
                    243:                endline(p);
                    244: }
1.138     schwarze  245:
1.278     schwarze  246: /*
                    247:  * Store the number of input characters to print in this field in *nbr
                    248:  * and their total visual width to print in *vbr.
                    249:  * If there is only whitespace in the field, both remain zero.
                    250:  * The desired visual width of the field is provided by vtarget.
                    251:  * If the first word is longer, the field will be overrun.
                    252:  */
                    253: static void
                    254: term_fill(struct termp *p, size_t *nbr, size_t *vbr, size_t vtarget)
                    255: {
                    256:        size_t   ic;        /* Character position in the input buffer. */
                    257:        size_t   vis;       /* Visual position of the current character. */
                    258:        size_t   vn;        /* Visual position of the next character. */
                    259:        int      breakline; /* Break at the end of this word. */
                    260:        int      graph;     /* Last character was non-blank. */
                    261:
                    262:        *nbr = *vbr = vis = 0;
                    263:        breakline = graph = 0;
                    264:        for (ic = p->tcol->col; ic < p->tcol->lastcol; ic++) {
                    265:                switch (p->tcol->buf[ic]) {
                    266:                case '\b':  /* Escape \o (overstrike) or backspace markup. */
                    267:                        assert(ic > 0);
                    268:                        vis -= (*p->width)(p, p->tcol->buf[ic - 1]);
                    269:                        continue;
1.267     schwarze  270:
1.278     schwarze  271:                case '\t':  /* Normal ASCII whitespace. */
                    272:                case ' ':
                    273:                case ASCII_BREAK:  /* Escape \: (breakpoint). */
                    274:                        switch (p->tcol->buf[ic]) {
                    275:                        case '\t':
                    276:                                vn = term_tab_next(vis);
1.140     kristaps  277:                                break;
1.278     schwarze  278:                        case ' ':
                    279:                                vn = vis + (*p->width)(p, ' ');
1.138     schwarze  280:                                break;
1.278     schwarze  281:                        case ASCII_BREAK:
                    282:                                vn = vis;
1.71      kristaps  283:                                break;
1.136     schwarze  284:                        }
1.278     schwarze  285:                        /* Can break at the end of a word. */
                    286:                        if (breakline || vn > vtarget)
                    287:                                break;
                    288:                        if (graph) {
                    289:                                *nbr = ic;
                    290:                                *vbr = vis;
                    291:                                graph = 0;
1.136     schwarze  292:                        }
1.278     schwarze  293:                        vis = vn;
                    294:                        continue;
                    295:
                    296:                case '\n':  /* Escape \p (break at the end of the word). */
                    297:                        breakline = 1;
                    298:                        continue;
1.130     kristaps  299:
1.278     schwarze  300:                case ASCII_HYPH:  /* Breakable hyphen. */
                    301:                        graph = 1;
1.136     schwarze  302:                        /*
1.278     schwarze  303:                         * We are about to decide whether to break the
                    304:                         * line or not, so we no longer need this hyphen
                    305:                         * to be marked as breakable.  Put back a real
                    306:                         * hyphen such that we get the correct width.
1.136     schwarze  307:                         */
1.278     schwarze  308:                        p->tcol->buf[ic] = '-';
                    309:                        vis += (*p->width)(p, '-');
                    310:                        if (vis > vtarget) {
                    311:                                ic++;
                    312:                                break;
1.200     schwarze  313:                        }
1.278     schwarze  314:                        *nbr = ic + 1;
                    315:                        *vbr = vis;
                    316:                        continue;
1.200     schwarze  317:
1.278     schwarze  318:                case ASCII_NBRSP:  /* Non-breakable space. */
                    319:                        p->tcol->buf[ic] = ' ';
                    320:                        /* FALLTHROUGH */
                    321:                default:  /* Printable character. */
                    322:                        graph = 1;
                    323:                        vis += (*p->width)(p, p->tcol->buf[ic]);
                    324:                        if (vis > vtarget && *nbr > 0)
                    325:                                return;
                    326:                        continue;
1.136     schwarze  327:                }
1.278     schwarze  328:                break;
                    329:        }
1.270     schwarze  330:
1.278     schwarze  331:        /*
                    332:         * If the last word extends to the end of the field without any
                    333:         * trailing whitespace, the loop could not check yet whether it
                    334:         * can remain on this line.  So do the check now.
                    335:         */
1.270     schwarze  336:
1.278     schwarze  337:        if (graph && (vis <= vtarget || *nbr == 0)) {
                    338:                *nbr = ic;
                    339:                *vbr = vis;
                    340:        }
                    341: }
1.270     schwarze  342:
1.278     schwarze  343: /*
                    344:  * Print the contents of one field
                    345:  * with an indentation of       vbl      visual columns,
                    346:  * an input string length of    nbr      characters,
                    347:  * an output width of           vbr      visual columns,
                    348:  * and a desired field width of         vtarget  visual columns.
                    349:  */
                    350: static void
                    351: term_field(struct termp *p, size_t vbl, size_t nbr, size_t vbr, size_t vtarget)
                    352: {
                    353:        size_t   ic;    /* Character position in the input buffer. */
                    354:        size_t   vis;   /* Visual position of the current character. */
                    355:        size_t   dv;    /* Visual width of the current character. */
                    356:        size_t   vn;    /* Visual position of the next character. */
1.270     schwarze  357:
1.278     schwarze  358:        vis = 0;
                    359:        for (ic = p->tcol->col; ic < nbr; ic++) {
1.270     schwarze  360:
1.278     schwarze  361:                /*
                    362:                 * To avoid the printing of trailing whitespace,
                    363:                 * do not print whitespace right away, only count it.
                    364:                 */
1.270     schwarze  365:
1.278     schwarze  366:                switch (p->tcol->buf[ic]) {
                    367:                case '\n':
                    368:                case ASCII_BREAK:
                    369:                        continue;
                    370:                case '\t':
                    371:                        vn = term_tab_next(vis);
                    372:                        vbl += vn - vis;
                    373:                        vis = vn;
                    374:                        continue;
                    375:                case ' ':
                    376:                case ASCII_NBRSP:
                    377:                        vbl++;
                    378:                        vis++;
                    379:                        continue;
                    380:                default:
                    381:                        break;
                    382:                }
1.168     schwarze  383:
1.278     schwarze  384:                /*
                    385:                 * We found a non-blank character to print,
                    386:                 * so write preceding white space now.
                    387:                 */
1.267     schwarze  388:
1.278     schwarze  389:                if (vbl > 0) {
                    390:                        (*p->advance)(p, vbl);
                    391:                        p->viscol += vbl;
                    392:                        vbl = 0;
                    393:                }
1.111     kristaps  394:
1.278     schwarze  395:                /* Print the character and adjust the visual position. */
1.15      kristaps  396:
1.278     schwarze  397:                (*p->letter)(p, p->tcol->buf[ic]);
                    398:                if (p->tcol->buf[ic] == '\b') {
                    399:                        dv = (*p->width)(p, p->tcol->buf[ic - 1]);
                    400:                        p->viscol -= dv;
                    401:                        vis -= dv;
                    402:                } else {
                    403:                        dv = (*p->width)(p, p->tcol->buf[ic]);
                    404:                        p->viscol += dv;
                    405:                        vis += dv;
                    406:                }
                    407:        }
                    408:        p->tcol->col = nbr;
1.264     schwarze  409: }
                    410:
                    411: static void
                    412: endline(struct termp *p)
                    413: {
                    414:        if ((p->flags & (TERMP_NEWMC | TERMP_ENDMC)) == TERMP_ENDMC) {
                    415:                p->mc = NULL;
                    416:                p->flags &= ~TERMP_ENDMC;
                    417:        }
                    418:        if (p->mc != NULL) {
                    419:                if (p->viscol && p->maxrmargin >= p->viscol)
                    420:                        (*p->advance)(p, p->maxrmargin - p->viscol + 1);
                    421:                p->flags |= TERMP_NOBUF | TERMP_NOSPACE;
                    422:                term_word(p, p->mc);
                    423:                p->flags &= ~(TERMP_NOBUF | TERMP_NEWMC);
                    424:        }
                    425:        p->viscol = 0;
                    426:        p->minbl = 0;
                    427:        (*p->endline)(p);
1.15      kristaps  428: }
                    429:
1.222     schwarze  430: /*
1.71      kristaps  431:  * A newline only breaks an existing line; it won't assert vertical
                    432:  * space.  All data in the output buffer is flushed prior to the newline
                    433:  * assertion.
                    434:  */
                    435: void
                    436: term_newln(struct termp *p)
1.15      kristaps  437: {
                    438:
1.71      kristaps  439:        p->flags |= TERMP_NOSPACE;
1.269     schwarze  440:        if (p->tcol->lastcol || p->viscol)
1.200     schwarze  441:                term_flushln(p);
1.16      kristaps  442: }
                    443:
1.71      kristaps  444: /*
                    445:  * Asserts a vertical space (a full, empty line-break between lines).
                    446:  * Note that if used twice, this will cause two blank spaces and so on.
                    447:  * All data in the output buffer is flushed prior to the newline
                    448:  * assertion.
                    449:  */
                    450: void
                    451: term_vspace(struct termp *p)
1.16      kristaps  452: {
                    453:
1.62      kristaps  454:        term_newln(p);
1.139     schwarze  455:        p->viscol = 0;
1.264     schwarze  456:        p->minbl = 0;
1.202     schwarze  457:        if (0 < p->skipvsp)
                    458:                p->skipvsp--;
                    459:        else
                    460:                (*p->endline)(p);
1.16      kristaps  461: }
                    462:
1.238     schwarze  463: /* Swap current and previous font; for \fP and .ft P */
1.125     kristaps  464: void
                    465: term_fontlast(struct termp *p)
                    466: {
                    467:        enum termfont    f;
                    468:
                    469:        f = p->fontl;
                    470:        p->fontl = p->fontq[p->fonti];
                    471:        p->fontq[p->fonti] = f;
                    472: }
                    473:
1.238     schwarze  474: /* Set font, save current, discard previous; for \f, .ft, .B etc. */
1.125     kristaps  475: void
                    476: term_fontrepl(struct termp *p, enum termfont f)
                    477: {
                    478:
                    479:        p->fontl = p->fontq[p->fonti];
                    480:        p->fontq[p->fonti] = f;
                    481: }
                    482:
1.238     schwarze  483: /* Set font, save previous. */
1.125     kristaps  484: void
                    485: term_fontpush(struct termp *p, enum termfont f)
                    486: {
                    487:
                    488:        p->fontl = p->fontq[p->fonti];
1.238     schwarze  489:        if (++p->fonti == p->fontsz) {
                    490:                p->fontsz += 8;
                    491:                p->fontq = mandoc_reallocarray(p->fontq,
1.256     schwarze  492:                    p->fontsz, sizeof(*p->fontq));
1.238     schwarze  493:        }
                    494:        p->fontq[p->fonti] = f;
1.125     kristaps  495: }
                    496:
1.238     schwarze  497: /* Flush to make the saved pointer current again. */
1.125     kristaps  498: void
1.244     schwarze  499: term_fontpopq(struct termp *p, int i)
1.125     kristaps  500: {
                    501:
1.244     schwarze  502:        assert(i >= 0);
                    503:        if (p->fonti > i)
                    504:                p->fonti = i;
1.125     kristaps  505: }
1.94      kristaps  506:
1.238     schwarze  507: /* Pop one font off the stack. */
1.125     kristaps  508: void
                    509: term_fontpop(struct termp *p)
                    510: {
                    511:
                    512:        assert(p->fonti);
                    513:        p->fonti--;
1.17      kristaps  514: }
                    515:
1.71      kristaps  516: /*
                    517:  * Handle pwords, partial words, which may be either a single word or a
                    518:  * phrase that cannot be broken down (such as a literal string).  This
                    519:  * handles word styling.
                    520:  */
1.86      kristaps  521: void
                    522: term_word(struct termp *p, const char *word)
1.65      kristaps  523: {
1.261     schwarze  524:        struct roffsu    su;
1.214     schwarze  525:        const char       nbrsp[2] = { ASCII_NBRSP, 0 };
1.191     kristaps  526:        const char      *seq, *cp;
1.194     kristaps  527:        int              sz, uc;
1.262     schwarze  528:        size_t           csz, lsz, ssz;
1.184     kristaps  529:        enum mandoc_esc  esc;
1.100     kristaps  530:
1.264     schwarze  531:        if ((p->flags & TERMP_NOBUF) == 0) {
                    532:                if ((p->flags & TERMP_NOSPACE) == 0) {
                    533:                        if ((p->flags & TERMP_KEEP) == 0) {
1.151     schwarze  534:                                bufferc(p, ' ');
1.264     schwarze  535:                                if (p->flags & TERMP_SENTENCE)
                    536:                                        bufferc(p, ' ');
                    537:                        } else
                    538:                                bufferc(p, ASCII_NBRSP);
                    539:                }
                    540:                if (p->flags & TERMP_PREKEEP)
                    541:                        p->flags |= TERMP_KEEP;
                    542:                if (p->flags & TERMP_NONOSPACE)
                    543:                        p->flags |= TERMP_NOSPACE;
                    544:                else
                    545:                        p->flags &= ~TERMP_NOSPACE;
                    546:                p->flags &= ~(TERMP_SENTENCE | TERMP_NONEWLINE);
                    547:                p->skipvsp = 0;
1.133     kristaps  548:        }
1.65      kristaps  549:
1.184     kristaps  550:        while ('\0' != *word) {
1.203     schwarze  551:                if ('\\' != *word) {
1.214     schwarze  552:                        if (TERMP_NBRWORD & p->flags) {
                    553:                                if (' ' == *word) {
                    554:                                        encode(p, nbrsp, 1);
                    555:                                        word++;
                    556:                                        continue;
                    557:                                }
                    558:                                ssz = strcspn(word, "\\ ");
                    559:                        } else
                    560:                                ssz = strcspn(word, "\\");
1.162     kristaps  561:                        encode(p, word, ssz);
1.203     schwarze  562:                        word += (int)ssz;
1.124     kristaps  563:                        continue;
1.203     schwarze  564:                }
1.124     kristaps  565:
1.184     kristaps  566:                word++;
                    567:                esc = mandoc_escape(&word, &seq, &sz);
                    568:                switch (esc) {
1.222     schwarze  569:                case ESCAPE_UNICODE:
1.229     schwarze  570:                        uc = mchars_num2uc(seq + 1, sz - 1);
1.192     kristaps  571:                        break;
1.222     schwarze  572:                case ESCAPE_NUMBERED:
1.233     schwarze  573:                        uc = mchars_num2char(seq, sz);
                    574:                        if (uc < 0)
                    575:                                continue;
1.184     kristaps  576:                        break;
1.222     schwarze  577:                case ESCAPE_SPECIAL:
1.229     schwarze  578:                        if (p->enc == TERMENC_ASCII) {
1.254     schwarze  579:                                cp = mchars_spec2str(seq, sz, &ssz);
1.232     schwarze  580:                                if (cp != NULL)
1.229     schwarze  581:                                        encode(p, cp, ssz);
                    582:                        } else {
1.254     schwarze  583:                                uc = mchars_spec2cp(seq, sz);
1.230     schwarze  584:                                if (uc > 0)
                    585:                                        encode1(p, uc);
1.229     schwarze  586:                        }
1.233     schwarze  587:                        continue;
1.277     schwarze  588:                case ESCAPE_UNDEF:
                    589:                        uc = *seq;
                    590:                        break;
1.222     schwarze  591:                case ESCAPE_FONTBOLD:
1.125     kristaps  592:                        term_fontrepl(p, TERMFONT_BOLD);
1.233     schwarze  593:                        continue;
1.222     schwarze  594:                case ESCAPE_FONTITALIC:
1.125     kristaps  595:                        term_fontrepl(p, TERMFONT_UNDER);
1.233     schwarze  596:                        continue;
1.222     schwarze  597:                case ESCAPE_FONTBI:
1.209     schwarze  598:                        term_fontrepl(p, TERMFONT_BI);
1.233     schwarze  599:                        continue;
1.222     schwarze  600:                case ESCAPE_FONT:
1.276     schwarze  601:                case ESCAPE_FONTCW:
1.222     schwarze  602:                case ESCAPE_FONTROMAN:
1.125     kristaps  603:                        term_fontrepl(p, TERMFONT_NONE);
1.233     schwarze  604:                        continue;
1.222     schwarze  605:                case ESCAPE_FONTPREV:
1.125     kristaps  606:                        term_fontlast(p);
1.270     schwarze  607:                        continue;
                    608:                case ESCAPE_BREAK:
                    609:                        bufferc(p, '\n');
1.233     schwarze  610:                        continue;
1.222     schwarze  611:                case ESCAPE_NOSPACE:
1.248     schwarze  612:                        if (p->flags & TERMP_BACKAFTER)
                    613:                                p->flags &= ~TERMP_BACKAFTER;
                    614:                        else if (*word == '\0')
1.237     schwarze  615:                                p->flags |= (TERMP_NOSPACE | TERMP_NONEWLINE);
1.261     schwarze  616:                        continue;
1.275     schwarze  617:                case ESCAPE_DEVICE:
                    618:                        if (p->type == TERMTYPE_PDF)
                    619:                                encode(p, "pdf", 3);
                    620:                        else if (p->type == TERMTYPE_PS)
                    621:                                encode(p, "ps", 2);
                    622:                        else if (p->enc == TERMENC_ASCII)
                    623:                                encode(p, "ascii", 5);
                    624:                        else
                    625:                                encode(p, "utf8", 4);
                    626:                        continue;
1.261     schwarze  627:                case ESCAPE_HORIZ:
1.273     schwarze  628:                        if (*seq == '|') {
                    629:                                seq++;
                    630:                                uc = -p->col;
                    631:                        } else
                    632:                                uc = 0;
1.268     schwarze  633:                        if (a2roffsu(seq, &su, SCALE_EM) == NULL)
1.261     schwarze  634:                                continue;
1.273     schwarze  635:                        uc += term_hen(p, &su);
1.261     schwarze  636:                        if (uc > 0)
                    637:                                while (uc-- > 0)
                    638:                                        bufferc(p, ASCII_NBRSP);
                    639:                        else if (p->col > (size_t)(-uc))
                    640:                                p->col += uc;
                    641:                        else {
                    642:                                uc += p->col;
                    643:                                p->col = 0;
1.266     schwarze  644:                                if (p->tcol->offset > (size_t)(-uc)) {
1.261     schwarze  645:                                        p->ti += uc;
1.266     schwarze  646:                                        p->tcol->offset += uc;
1.261     schwarze  647:                                } else {
1.266     schwarze  648:                                        p->ti -= p->tcol->offset;
                    649:                                        p->tcol->offset = 0;
1.261     schwarze  650:                                }
1.262     schwarze  651:                        }
                    652:                        continue;
                    653:                case ESCAPE_HLINE:
1.272     schwarze  654:                        if ((cp = a2roffsu(seq, &su, SCALE_EM)) == NULL)
1.262     schwarze  655:                                continue;
1.271     schwarze  656:                        uc = term_hen(p, &su);
1.262     schwarze  657:                        if (uc <= 0) {
1.266     schwarze  658:                                if (p->tcol->rmargin <= p->tcol->offset)
1.262     schwarze  659:                                        continue;
1.266     schwarze  660:                                lsz = p->tcol->rmargin - p->tcol->offset;
1.262     schwarze  661:                        } else
                    662:                                lsz = uc;
1.272     schwarze  663:                        if (*cp == seq[-1])
1.262     schwarze  664:                                uc = -1;
1.272     schwarze  665:                        else if (*cp == '\\') {
                    666:                                seq = cp + 1;
1.262     schwarze  667:                                esc = mandoc_escape(&seq, &cp, &sz);
                    668:                                switch (esc) {
                    669:                                case ESCAPE_UNICODE:
                    670:                                        uc = mchars_num2uc(cp + 1, sz - 1);
                    671:                                        break;
                    672:                                case ESCAPE_NUMBERED:
                    673:                                        uc = mchars_num2char(cp, sz);
                    674:                                        break;
                    675:                                case ESCAPE_SPECIAL:
                    676:                                        uc = mchars_spec2cp(cp, sz);
                    677:                                        break;
1.277     schwarze  678:                                case ESCAPE_UNDEF:
                    679:                                        uc = *seq;
                    680:                                        break;
1.262     schwarze  681:                                default:
                    682:                                        uc = -1;
                    683:                                        break;
                    684:                                }
                    685:                        } else
1.272     schwarze  686:                                uc = *cp;
1.262     schwarze  687:                        if (uc < 0x20 || (uc > 0x7E && uc < 0xA0))
                    688:                                uc = '_';
                    689:                        if (p->enc == TERMENC_ASCII) {
                    690:                                cp = ascii_uc2str(uc);
                    691:                                csz = term_strlen(p, cp);
                    692:                                ssz = strlen(cp);
                    693:                        } else
                    694:                                csz = (*p->width)(p, uc);
                    695:                        while (lsz >= csz) {
                    696:                                if (p->enc == TERMENC_ASCII)
                    697:                                        encode(p, cp, ssz);
                    698:                                else
                    699:                                        encode1(p, uc);
                    700:                                lsz -= csz;
1.261     schwarze  701:                        }
1.233     schwarze  702:                        continue;
1.222     schwarze  703:                case ESCAPE_SKIPCHAR:
1.248     schwarze  704:                        p->flags |= TERMP_BACKAFTER;
1.233     schwarze  705:                        continue;
1.243     schwarze  706:                case ESCAPE_OVERSTRIKE:
                    707:                        cp = seq + sz;
                    708:                        while (seq < cp) {
                    709:                                if (*seq == '\\') {
                    710:                                        mandoc_escape(&seq, NULL, NULL);
                    711:                                        continue;
                    712:                                }
                    713:                                encode1(p, *seq++);
1.248     schwarze  714:                                if (seq < cp) {
                    715:                                        if (p->flags & TERMP_BACKBEFORE)
                    716:                                                p->flags |= TERMP_BACKAFTER;
                    717:                                        else
                    718:                                                p->flags |= TERMP_BACKBEFORE;
                    719:                                }
1.243     schwarze  720:                        }
1.249     schwarze  721:                        /* Trim trailing backspace/blank pair. */
1.269     schwarze  722:                        if (p->tcol->lastcol > 2 &&
                    723:                            (p->tcol->buf[p->tcol->lastcol - 1] == ' ' ||
                    724:                             p->tcol->buf[p->tcol->lastcol - 1] == '\t'))
                    725:                                p->tcol->lastcol -= 2;
                    726:                        if (p->col > p->tcol->lastcol)
                    727:                                p->col = p->tcol->lastcol;
1.248     schwarze  728:                        continue;
1.124     kristaps  729:                default:
1.233     schwarze  730:                        continue;
                    731:                }
                    732:
                    733:                /*
                    734:                 * Common handling for Unicode and numbered
                    735:                 * character escape sequences.
                    736:                 */
                    737:
                    738:                if (p->enc == TERMENC_ASCII) {
                    739:                        cp = ascii_uc2str(uc);
                    740:                        encode(p, cp, strlen(cp));
                    741:                } else {
                    742:                        if ((uc < 0x20 && uc != 0x09) ||
                    743:                            (uc > 0x7E && uc < 0xA0))
                    744:                                uc = 0xFFFD;
                    745:                        encode1(p, uc);
1.124     kristaps  746:                }
                    747:        }
1.214     schwarze  748:        p->flags &= ~TERMP_NBRWORD;
1.65      kristaps  749: }
                    750:
1.71      kristaps  751: static void
1.266     schwarze  752: adjbuf(struct termp_col *c, size_t sz)
1.51      kristaps  753: {
1.266     schwarze  754:        if (c->maxcols == 0)
                    755:                c->maxcols = 1024;
                    756:        while (c->maxcols <= sz)
                    757:                c->maxcols <<= 2;
                    758:        c->buf = mandoc_reallocarray(c->buf, c->maxcols, sizeof(*c->buf));
1.51      kristaps  759: }
                    760:
1.79      kristaps  761: static void
1.125     kristaps  762: bufferc(struct termp *p, char c)
                    763: {
1.264     schwarze  764:        if (p->flags & TERMP_NOBUF) {
                    765:                (*p->letter)(p, c);
                    766:                return;
                    767:        }
1.266     schwarze  768:        if (p->col + 1 >= p->tcol->maxcols)
                    769:                adjbuf(p->tcol, p->col + 1);
1.269     schwarze  770:        if (p->tcol->lastcol <= p->col || (c != ' ' && c != ASCII_NBRSP))
1.266     schwarze  771:                p->tcol->buf[p->col] = c;
1.269     schwarze  772:        if (p->tcol->lastcol < ++p->col)
                    773:                p->tcol->lastcol = p->col;
1.125     kristaps  774: }
                    775:
1.194     kristaps  776: /*
                    777:  * See encode().
                    778:  * Do this for a single (probably unicode) value.
                    779:  * Does not check for non-decorated glyphs.
                    780:  */
                    781: static void
                    782: encode1(struct termp *p, int c)
                    783: {
                    784:        enum termfont     f;
                    785:
1.264     schwarze  786:        if (p->flags & TERMP_NOBUF) {
                    787:                (*p->letter)(p, c);
                    788:                return;
                    789:        }
                    790:
1.266     schwarze  791:        if (p->col + 7 >= p->tcol->maxcols)
                    792:                adjbuf(p->tcol, p->col + 7);
1.194     kristaps  793:
1.255     schwarze  794:        f = (c == ASCII_HYPH || c > 127 || isgraph(c)) ?
1.248     schwarze  795:            p->fontq[p->fonti] : TERMFONT_NONE;
1.194     kristaps  796:
1.248     schwarze  797:        if (p->flags & TERMP_BACKBEFORE) {
1.266     schwarze  798:                if (p->tcol->buf[p->col - 1] == ' ' ||
                    799:                    p->tcol->buf[p->col - 1] == '\t')
1.249     schwarze  800:                        p->col--;
                    801:                else
1.266     schwarze  802:                        p->tcol->buf[p->col++] = '\b';
1.248     schwarze  803:                p->flags &= ~TERMP_BACKBEFORE;
                    804:        }
1.266     schwarze  805:        if (f == TERMFONT_UNDER || f == TERMFONT_BI) {
                    806:                p->tcol->buf[p->col++] = '_';
                    807:                p->tcol->buf[p->col++] = '\b';
                    808:        }
                    809:        if (f == TERMFONT_BOLD || f == TERMFONT_BI) {
                    810:                if (c == ASCII_HYPH)
                    811:                        p->tcol->buf[p->col++] = '-';
1.209     schwarze  812:                else
1.266     schwarze  813:                        p->tcol->buf[p->col++] = c;
                    814:                p->tcol->buf[p->col++] = '\b';
1.209     schwarze  815:        }
1.269     schwarze  816:        if (p->tcol->lastcol <= p->col || (c != ' ' && c != ASCII_NBRSP))
1.266     schwarze  817:                p->tcol->buf[p->col] = c;
1.269     schwarze  818:        if (p->tcol->lastcol < ++p->col)
                    819:                p->tcol->lastcol = p->col;
1.248     schwarze  820:        if (p->flags & TERMP_BACKAFTER) {
                    821:                p->flags |= TERMP_BACKBEFORE;
                    822:                p->flags &= ~TERMP_BACKAFTER;
                    823:        }
1.194     kristaps  824: }
                    825:
1.125     kristaps  826: static void
                    827: encode(struct termp *p, const char *word, size_t sz)
                    828: {
1.210     schwarze  829:        size_t            i;
1.264     schwarze  830:
                    831:        if (p->flags & TERMP_NOBUF) {
                    832:                for (i = 0; i < sz; i++)
                    833:                        (*p->letter)(p, word[i]);
                    834:                return;
                    835:        }
1.188     kristaps  836:
1.266     schwarze  837:        if (p->col + 2 + (sz * 5) >= p->tcol->maxcols)
                    838:                adjbuf(p->tcol, p->col + 2 + (sz * 5));
1.165     kristaps  839:
1.210     schwarze  840:        for (i = 0; i < sz; i++) {
1.209     schwarze  841:                if (ASCII_HYPH == word[i] ||
                    842:                    isgraph((unsigned char)word[i]))
                    843:                        encode1(p, word[i]);
1.259     schwarze  844:                else {
1.269     schwarze  845:                        if (p->tcol->lastcol <= p->col ||
1.265     schwarze  846:                            (word[i] != ' ' && word[i] != ASCII_NBRSP))
1.266     schwarze  847:                                p->tcol->buf[p->col] = word[i];
1.265     schwarze  848:                        p->col++;
1.259     schwarze  849:
                    850:                        /*
                    851:                         * Postpone the effect of \z while handling
                    852:                         * an overstrike sequence from ascii_uc2str().
                    853:                         */
                    854:
                    855:                        if (word[i] == '\b' &&
                    856:                            (p->flags & TERMP_BACKBEFORE)) {
                    857:                                p->flags &= ~TERMP_BACKBEFORE;
                    858:                                p->flags |= TERMP_BACKAFTER;
                    859:                        }
                    860:                }
1.79      kristaps  861:        }
1.269     schwarze  862:        if (p->tcol->lastcol < p->col)
                    863:                p->tcol->lastcol = p->col;
1.219     schwarze  864: }
                    865:
                    866: void
                    867: term_setwidth(struct termp *p, const char *wstr)
                    868: {
                    869:        struct roffsu    su;
1.247     schwarze  870:        int              iop, width;
1.219     schwarze  871:
1.220     schwarze  872:        iop = 0;
                    873:        width = 0;
1.219     schwarze  874:        if (NULL != wstr) {
                    875:                switch (*wstr) {
1.222     schwarze  876:                case '+':
1.219     schwarze  877:                        iop = 1;
                    878:                        wstr++;
                    879:                        break;
1.222     schwarze  880:                case '-':
1.219     schwarze  881:                        iop = -1;
                    882:                        wstr++;
                    883:                        break;
                    884:                default:
                    885:                        break;
                    886:                }
1.268     schwarze  887:                if (a2roffsu(wstr, &su, SCALE_MAX) != NULL)
1.220     schwarze  888:                        width = term_hspan(p, &su);
                    889:                else
1.219     schwarze  890:                        iop = 0;
                    891:        }
                    892:        (*p->setwidth)(p, iop, width);
1.79      kristaps  893: }
1.106     kristaps  894:
1.107     kristaps  895: size_t
1.149     kristaps  896: term_len(const struct termp *p, size_t sz)
                    897: {
                    898:
1.252     schwarze  899:        return (*p->width)(p, ' ') * sz;
1.149     kristaps  900: }
                    901:
1.203     schwarze  902: static size_t
                    903: cond_width(const struct termp *p, int c, int *skip)
                    904: {
                    905:
                    906:        if (*skip) {
                    907:                (*skip) = 0;
1.252     schwarze  908:                return 0;
1.203     schwarze  909:        } else
1.252     schwarze  910:                return (*p->width)(p, c);
1.203     schwarze  911: }
1.149     kristaps  912:
                    913: size_t
                    914: term_strlen(const struct termp *p, const char *cp)
                    915: {
1.184     kristaps  916:        size_t           sz, rsz, i;
1.233     schwarze  917:        int              ssz, skip, uc;
1.171     kristaps  918:        const char      *seq, *rhs;
1.196     kristaps  919:        enum mandoc_esc  esc;
1.216     schwarze  920:        static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
                    921:                        ASCII_BREAK, '\0' };
1.171     kristaps  922:
1.184     kristaps  923:        /*
                    924:         * Account for escaped sequences within string length
                    925:         * calculations.  This follows the logic in term_word() as we
                    926:         * must calculate the width of produced strings.
                    927:         */
                    928:
                    929:        sz = 0;
1.203     schwarze  930:        skip = 0;
1.189     kristaps  931:        while ('\0' != *cp) {
                    932:                rsz = strcspn(cp, rej);
                    933:                for (i = 0; i < rsz; i++)
1.203     schwarze  934:                        sz += cond_width(p, *cp++, &skip);
1.189     kristaps  935:
1.184     kristaps  936:                switch (*cp) {
1.222     schwarze  937:                case '\\':
1.189     kristaps  938:                        cp++;
1.277     schwarze  939:                        rhs = NULL;
1.196     kristaps  940:                        esc = mandoc_escape(&cp, &seq, &ssz);
                    941:                        switch (esc) {
1.222     schwarze  942:                        case ESCAPE_UNICODE:
1.234     schwarze  943:                                uc = mchars_num2uc(seq + 1, ssz - 1);
1.194     kristaps  944:                                break;
1.222     schwarze  945:                        case ESCAPE_NUMBERED:
1.233     schwarze  946:                                uc = mchars_num2char(seq, ssz);
                    947:                                if (uc < 0)
                    948:                                        continue;
1.171     kristaps  949:                                break;
1.222     schwarze  950:                        case ESCAPE_SPECIAL:
1.233     schwarze  951:                                if (p->enc == TERMENC_ASCII) {
1.254     schwarze  952:                                        rhs = mchars_spec2str(seq, ssz, &rsz);
1.233     schwarze  953:                                        if (rhs != NULL)
                    954:                                                break;
                    955:                                } else {
1.254     schwarze  956:                                        uc = mchars_spec2cp(seq, ssz);
1.233     schwarze  957:                                        if (uc > 0)
                    958:                                                sz += cond_width(p, uc, &skip);
1.229     schwarze  959:                                }
1.233     schwarze  960:                                continue;
1.277     schwarze  961:                        case ESCAPE_UNDEF:
                    962:                                uc = *seq;
                    963:                                break;
1.275     schwarze  964:                        case ESCAPE_DEVICE:
                    965:                                if (p->type == TERMTYPE_PDF) {
                    966:                                        rhs = "pdf";
                    967:                                        rsz = 3;
                    968:                                } else if (p->type == TERMTYPE_PS) {
                    969:                                        rhs = "ps";
                    970:                                        rsz = 2;
                    971:                                } else if (p->enc == TERMENC_ASCII) {
                    972:                                        rhs = "ascii";
                    973:                                        rsz = 5;
                    974:                                } else {
                    975:                                        rhs = "utf8";
                    976:                                        rsz = 4;
                    977:                                }
                    978:                                break;
1.222     schwarze  979:                        case ESCAPE_SKIPCHAR:
1.203     schwarze  980:                                skip = 1;
1.243     schwarze  981:                                continue;
                    982:                        case ESCAPE_OVERSTRIKE:
                    983:                                rsz = 0;
                    984:                                rhs = seq + ssz;
                    985:                                while (seq < rhs) {
                    986:                                        if (*seq == '\\') {
                    987:                                                mandoc_escape(&seq, NULL, NULL);
                    988:                                                continue;
                    989:                                        }
                    990:                                        i = (*p->width)(p, *seq++);
                    991:                                        if (rsz < i)
                    992:                                                rsz = i;
                    993:                                }
                    994:                                sz += rsz;
1.233     schwarze  995:                                continue;
1.171     kristaps  996:                        default:
1.233     schwarze  997:                                continue;
1.171     kristaps  998:                        }
1.149     kristaps  999:
1.233     schwarze 1000:                        /*
                   1001:                         * Common handling for Unicode and numbered
                   1002:                         * character escape sequences.
                   1003:                         */
                   1004:
                   1005:                        if (rhs == NULL) {
                   1006:                                if (p->enc == TERMENC_ASCII) {
                   1007:                                        rhs = ascii_uc2str(uc);
                   1008:                                        rsz = strlen(rhs);
                   1009:                                } else {
                   1010:                                        if ((uc < 0x20 && uc != 0x09) ||
                   1011:                                            (uc > 0x7E && uc < 0xA0))
                   1012:                                                uc = 0xFFFD;
                   1013:                                        sz += cond_width(p, uc, &skip);
                   1014:                                        continue;
                   1015:                                }
                   1016:                        }
1.184     kristaps 1017:
1.203     schwarze 1018:                        if (skip) {
                   1019:                                skip = 0;
                   1020:                                break;
                   1021:                        }
1.233     schwarze 1022:
                   1023:                        /*
                   1024:                         * Common handling for all escape sequences
                   1025:                         * printing more than one character.
                   1026:                         */
1.203     schwarze 1027:
1.184     kristaps 1028:                        for (i = 0; i < rsz; i++)
                   1029:                                sz += (*p->width)(p, *rhs++);
                   1030:                        break;
1.222     schwarze 1031:                case ASCII_NBRSP:
1.203     schwarze 1032:                        sz += cond_width(p, ' ', &skip);
1.176     kristaps 1033:                        cp++;
1.184     kristaps 1034:                        break;
1.222     schwarze 1035:                case ASCII_HYPH:
1.203     schwarze 1036:                        sz += cond_width(p, '-', &skip);
1.176     kristaps 1037:                        cp++;
1.184     kristaps 1038:                        break;
                   1039:                default:
                   1040:                        break;
                   1041:                }
1.189     kristaps 1042:        }
1.149     kristaps 1043:
1.252     schwarze 1044:        return sz;
1.149     kristaps 1045: }
                   1046:
1.240     schwarze 1047: int
1.149     kristaps 1048: term_vspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps 1049: {
                   1050:        double           r;
1.241     schwarze 1051:        int              ri;
1.106     kristaps 1052:
1.107     kristaps 1053:        switch (su->unit) {
1.239     schwarze 1054:        case SCALE_BU:
                   1055:                r = su->scale / 40.0;
                   1056:                break;
1.222     schwarze 1057:        case SCALE_CM:
1.239     schwarze 1058:                r = su->scale * 6.0 / 2.54;
                   1059:                break;
                   1060:        case SCALE_FS:
                   1061:                r = su->scale * 65536.0 / 40.0;
1.106     kristaps 1062:                break;
1.222     schwarze 1063:        case SCALE_IN:
1.225     schwarze 1064:                r = su->scale * 6.0;
1.106     kristaps 1065:                break;
1.239     schwarze 1066:        case SCALE_MM:
                   1067:                r = su->scale * 0.006;
                   1068:                break;
1.222     schwarze 1069:        case SCALE_PC:
1.107     kristaps 1070:                r = su->scale;
1.106     kristaps 1071:                break;
1.222     schwarze 1072:        case SCALE_PT:
1.239     schwarze 1073:                r = su->scale / 12.0;
1.106     kristaps 1074:                break;
1.239     schwarze 1075:        case SCALE_EN:
                   1076:        case SCALE_EM:
                   1077:                r = su->scale * 0.6;
1.106     kristaps 1078:                break;
1.222     schwarze 1079:        case SCALE_VS:
1.107     kristaps 1080:                r = su->scale;
1.106     kristaps 1081:                break;
                   1082:        default:
1.239     schwarze 1083:                abort();
1.106     kristaps 1084:        }
1.241     schwarze 1085:        ri = r > 0.0 ? r + 0.4995 : r - 0.4995;
1.252     schwarze 1086:        return ri < 66 ? ri : 1;
1.106     kristaps 1087: }
                   1088:
1.247     schwarze 1089: /*
1.271     schwarze 1090:  * Convert a scaling width to basic units, rounding towards 0.
1.247     schwarze 1091:  */
1.240     schwarze 1092: int
1.149     kristaps 1093: term_hspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps 1094: {
1.108     kristaps 1095:
1.252     schwarze 1096:        return (*p->hspan)(p, su);
1.271     schwarze 1097: }
                   1098:
                   1099: /*
                   1100:  * Convert a scaling width to basic units, rounding to closest.
                   1101:  */
                   1102: int
                   1103: term_hen(const struct termp *p, const struct roffsu *su)
                   1104: {
                   1105:        int bu;
                   1106:
                   1107:        if ((bu = (*p->hspan)(p, su)) >= 0)
                   1108:                return (bu + 11) / 24;
                   1109:        else
                   1110:                return -((-bu + 11) / 24);
1.106     kristaps 1111: }

CVSweb