[BACK]Return to term.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/term.c, Revision 1.139

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