[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.144

1.144   ! schwarze    1: /* $OpenBSD: term.c,v 1.143 2021/08/10 12:36:42 schwarze Exp $ */
1.1       kristaps    2: /*
1.144   ! schwarze    3:  * Copyright (c) 2010-2021 Ingo Schwarze <schwarze@openbsd.org>
1.59      schwarze    4:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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.142     schwarze   39: static void             term_field(struct termp *, size_t, size_t);
1.138     schwarze   40: static void             term_fill(struct termp *, size_t *, size_t *,
                     41:                                size_t);
1.1       kristaps   42:
1.83      schwarze   43:
1.37      schwarze   44: void
1.129     schwarze   45: term_setcol(struct termp *p, size_t maxtcol)
                     46: {
                     47:        if (maxtcol > p->maxtcol) {
                     48:                p->tcols = mandoc_recallocarray(p->tcols,
                     49:                    p->maxtcol, maxtcol, sizeof(*p->tcols));
                     50:                p->maxtcol = maxtcol;
                     51:        }
                     52:        p->lasttcol = maxtcol - 1;
                     53:        p->tcol = p->tcols;
                     54: }
                     55:
                     56: void
1.37      schwarze   57: term_free(struct termp *p)
1.1       kristaps   58: {
1.144   ! schwarze   59:        term_tab_free();
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.142     schwarze  128:                vtarget = (p->flags & TERMP_NOBREAK) == 0 ? vfield :
1.138     schwarze  129:                    p->maxrmargin > p->viscol + vbl ?
                    130:                    p->maxrmargin - p->viscol - vbl : 0;
                    131:
                    132:                /*
                    133:                 * Figure out how much text will fit in the field.
                    134:                 * If there is whitespace only, print nothing.
                    135:                 */
                    136:
1.142     schwarze  137:                term_fill(p, &nbr, &vbr,
                    138:                    p->flags & TERMP_BRNEVER ? SIZE_MAX : vtarget);
1.138     schwarze  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:
1.142     schwarze  157:                term_field(p, vbl, nbr);
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;
1.141     schwarze  282:                        default:
                    283:                                abort();
1.22      schwarze  284:                        }
1.138     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.33      schwarze  292:                        }
1.138     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.33      schwarze  299:
1.138     schwarze  300:                case ASCII_HYPH:  /* Breakable hyphen. */
                    301:                        graph = 1;
1.33      schwarze  302:                        /*
1.138     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.33      schwarze  307:                         */
1.138     schwarze  308:                        p->tcol->buf[ic] = '-';
                    309:                        vis += (*p->width)(p, '-');
                    310:                        if (vis > vtarget) {
                    311:                                ic++;
                    312:                                break;
1.61      schwarze  313:                        }
1.138     schwarze  314:                        *nbr = ic + 1;
                    315:                        *vbr = vis;
                    316:                        continue;
1.61      schwarze  317:
1.138     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.1       kristaps  327:                }
1.138     schwarze  328:                break;
                    329:        }
1.130     schwarze  330:
1.138     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.130     schwarze  336:
1.138     schwarze  337:        if (graph && (vis <= vtarget || *nbr == 0)) {
                    338:                *nbr = ic;
                    339:                *vbr = vis;
                    340:        }
                    341: }
1.130     schwarze  342:
1.138     schwarze  343: /*
                    344:  * Print the contents of one field
                    345:  * with an indentation of       vbl      visual columns,
1.142     schwarze  346:  * and an input string length of nbr     characters.
1.138     schwarze  347:  */
                    348: static void
1.142     schwarze  349: term_field(struct termp *p, size_t vbl, size_t nbr)
1.138     schwarze  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:
1.140     schwarze  375:                        dv = (*p->width)(p, ' ');
                    376:                        vbl += dv;
                    377:                        vis += dv;
1.138     schwarze  378:                        continue;
                    379:                default:
                    380:                        break;
                    381:                }
1.48      schwarze  382:
1.138     schwarze  383:                /*
                    384:                 * We found a non-blank character to print,
                    385:                 * so write preceding white space now.
                    386:                 */
1.127     schwarze  387:
1.138     schwarze  388:                if (vbl > 0) {
                    389:                        (*p->advance)(p, vbl);
                    390:                        p->viscol += vbl;
                    391:                        vbl = 0;
                    392:                }
1.18      schwarze  393:
1.138     schwarze  394:                /* Print the character and adjust the visual position. */
1.1       kristaps  395:
1.138     schwarze  396:                (*p->letter)(p, p->tcol->buf[ic]);
                    397:                if (p->tcol->buf[ic] == '\b') {
                    398:                        dv = (*p->width)(p, p->tcol->buf[ic - 1]);
                    399:                        p->viscol -= dv;
                    400:                        vis -= dv;
                    401:                } else {
                    402:                        dv = (*p->width)(p, p->tcol->buf[ic]);
                    403:                        p->viscol += dv;
                    404:                        vis += dv;
                    405:                }
                    406:        }
                    407:        p->tcol->col = nbr;
1.124     schwarze  408: }
                    409:
                    410: static void
                    411: endline(struct termp *p)
                    412: {
                    413:        if ((p->flags & (TERMP_NEWMC | TERMP_ENDMC)) == TERMP_ENDMC) {
                    414:                p->mc = NULL;
                    415:                p->flags &= ~TERMP_ENDMC;
                    416:        }
                    417:        if (p->mc != NULL) {
                    418:                if (p->viscol && p->maxrmargin >= p->viscol)
                    419:                        (*p->advance)(p, p->maxrmargin - p->viscol + 1);
                    420:                p->flags |= TERMP_NOBUF | TERMP_NOSPACE;
                    421:                term_word(p, p->mc);
                    422:                p->flags &= ~(TERMP_NOBUF | TERMP_NEWMC);
                    423:        }
                    424:        p->viscol = 0;
                    425:        p->minbl = 0;
                    426:        (*p->endline)(p);
1.1       kristaps  427: }
                    428:
1.83      schwarze  429: /*
1.1       kristaps  430:  * A newline only breaks an existing line; it won't assert vertical
                    431:  * space.  All data in the output buffer is flushed prior to the newline
                    432:  * assertion.
                    433:  */
                    434: void
                    435: term_newln(struct termp *p)
                    436: {
                    437:
                    438:        p->flags |= TERMP_NOSPACE;
1.129     schwarze  439:        if (p->tcol->lastcol || p->viscol)
1.61      schwarze  440:                term_flushln(p);
1.1       kristaps  441: }
                    442:
                    443: /*
                    444:  * Asserts a vertical space (a full, empty line-break between lines).
                    445:  * Note that if used twice, this will cause two blank spaces and so on.
                    446:  * All data in the output buffer is flushed prior to the newline
                    447:  * assertion.
                    448:  */
                    449: void
                    450: term_vspace(struct termp *p)
                    451: {
                    452:
                    453:        term_newln(p);
1.29      schwarze  454:        p->viscol = 0;
1.124     schwarze  455:        p->minbl = 0;
1.63      schwarze  456:        if (0 < p->skipvsp)
                    457:                p->skipvsp--;
                    458:        else
                    459:                (*p->endline)(p);
1.1       kristaps  460: }
                    461:
1.98      schwarze  462: /* Swap current and previous font; for \fP and .ft P */
1.20      schwarze  463: void
                    464: term_fontlast(struct termp *p)
                    465: {
                    466:        enum termfont    f;
1.11      schwarze  467:
1.20      schwarze  468:        f = p->fontl;
                    469:        p->fontl = p->fontq[p->fonti];
                    470:        p->fontq[p->fonti] = f;
                    471: }
                    472:
1.98      schwarze  473: /* Set font, save current, discard previous; for \f, .ft, .B etc. */
1.20      schwarze  474: void
                    475: term_fontrepl(struct termp *p, enum termfont f)
                    476: {
                    477:
                    478:        p->fontl = p->fontq[p->fonti];
                    479:        p->fontq[p->fonti] = f;
1.1       kristaps  480: }
                    481:
1.98      schwarze  482: /* Set font, save previous. */
1.20      schwarze  483: void
                    484: term_fontpush(struct termp *p, enum termfont f)
1.1       kristaps  485: {
1.7       schwarze  486:
1.20      schwarze  487:        p->fontl = p->fontq[p->fonti];
1.98      schwarze  488:        if (++p->fonti == p->fontsz) {
                    489:                p->fontsz += 8;
                    490:                p->fontq = mandoc_reallocarray(p->fontq,
1.116     schwarze  491:                    p->fontsz, sizeof(*p->fontq));
1.98      schwarze  492:        }
                    493:        p->fontq[p->fonti] = f;
1.20      schwarze  494: }
1.1       kristaps  495:
1.98      schwarze  496: /* Flush to make the saved pointer current again. */
1.20      schwarze  497: void
1.104     schwarze  498: term_fontpopq(struct termp *p, int i)
1.20      schwarze  499: {
1.1       kristaps  500:
1.104     schwarze  501:        assert(i >= 0);
                    502:        if (p->fonti > i)
                    503:                p->fonti = i;
1.20      schwarze  504: }
1.1       kristaps  505:
1.98      schwarze  506: /* Pop one font off the stack. */
1.20      schwarze  507: void
                    508: term_fontpop(struct termp *p)
                    509: {
1.1       kristaps  510:
1.20      schwarze  511:        assert(p->fonti);
                    512:        p->fonti--;
1.1       kristaps  513: }
                    514:
                    515: /*
                    516:  * Handle pwords, partial words, which may be either a single word or a
                    517:  * phrase that cannot be broken down (such as a literal string).  This
                    518:  * handles word styling.
                    519:  */
1.7       schwarze  520: void
                    521: term_word(struct termp *p, const char *word)
1.1       kristaps  522: {
1.121     schwarze  523:        struct roffsu    su;
1.75      schwarze  524:        const char       nbrsp[2] = { ASCII_NBRSP, 0 };
1.59      schwarze  525:        const char      *seq, *cp;
                    526:        int              sz, uc;
1.122     schwarze  527:        size_t           csz, lsz, ssz;
1.59      schwarze  528:        enum mandoc_esc  esc;
1.1       kristaps  529:
1.124     schwarze  530:        if ((p->flags & TERMP_NOBUF) == 0) {
                    531:                if ((p->flags & TERMP_NOSPACE) == 0) {
                    532:                        if ((p->flags & TERMP_KEEP) == 0) {
1.40      schwarze  533:                                bufferc(p, ' ');
1.124     schwarze  534:                                if (p->flags & TERMP_SENTENCE)
                    535:                                        bufferc(p, ' ');
                    536:                        } else
                    537:                                bufferc(p, ASCII_NBRSP);
                    538:                }
                    539:                if (p->flags & TERMP_PREKEEP)
                    540:                        p->flags |= TERMP_KEEP;
                    541:                if (p->flags & TERMP_NONOSPACE)
                    542:                        p->flags |= TERMP_NOSPACE;
                    543:                else
                    544:                        p->flags &= ~TERMP_NOSPACE;
                    545:                p->flags &= ~(TERMP_SENTENCE | TERMP_NONEWLINE);
                    546:                p->skipvsp = 0;
1.31      schwarze  547:        }
                    548:
1.59      schwarze  549:        while ('\0' != *word) {
1.64      schwarze  550:                if ('\\' != *word) {
1.75      schwarze  551:                        if (TERMP_NBRWORD & p->flags) {
                    552:                                if (' ' == *word) {
                    553:                                        encode(p, nbrsp, 1);
                    554:                                        word++;
                    555:                                        continue;
                    556:                                }
                    557:                                ssz = strcspn(word, "\\ ");
                    558:                        } else
                    559:                                ssz = strcspn(word, "\\");
1.45      schwarze  560:                        encode(p, word, ssz);
1.64      schwarze  561:                        word += (int)ssz;
1.20      schwarze  562:                        continue;
1.64      schwarze  563:                }
1.20      schwarze  564:
1.59      schwarze  565:                word++;
                    566:                esc = mandoc_escape(&word, &seq, &sz);
                    567:                switch (esc) {
1.83      schwarze  568:                case ESCAPE_UNICODE:
1.89      schwarze  569:                        uc = mchars_num2uc(seq + 1, sz - 1);
1.56      schwarze  570:                        break;
1.83      schwarze  571:                case ESCAPE_NUMBERED:
1.93      schwarze  572:                        uc = mchars_num2char(seq, sz);
                    573:                        if (uc < 0)
                    574:                                continue;
1.20      schwarze  575:                        break;
1.83      schwarze  576:                case ESCAPE_SPECIAL:
1.89      schwarze  577:                        if (p->enc == TERMENC_ASCII) {
1.114     schwarze  578:                                cp = mchars_spec2str(seq, sz, &ssz);
1.92      schwarze  579:                                if (cp != NULL)
1.89      schwarze  580:                                        encode(p, cp, ssz);
                    581:                        } else {
1.114     schwarze  582:                                uc = mchars_spec2cp(seq, sz);
1.90      schwarze  583:                                if (uc > 0)
                    584:                                        encode1(p, uc);
1.89      schwarze  585:                        }
1.93      schwarze  586:                        continue;
1.137     schwarze  587:                case ESCAPE_UNDEF:
                    588:                        uc = *seq;
                    589:                        break;
1.83      schwarze  590:                case ESCAPE_FONTBOLD:
1.143     schwarze  591:                case ESCAPE_FONTCB:
1.20      schwarze  592:                        term_fontrepl(p, TERMFONT_BOLD);
1.93      schwarze  593:                        continue;
1.83      schwarze  594:                case ESCAPE_FONTITALIC:
1.143     schwarze  595:                case ESCAPE_FONTCI:
1.20      schwarze  596:                        term_fontrepl(p, TERMFONT_UNDER);
1.93      schwarze  597:                        continue;
1.83      schwarze  598:                case ESCAPE_FONTBI:
1.70      schwarze  599:                        term_fontrepl(p, TERMFONT_BI);
1.93      schwarze  600:                        continue;
1.83      schwarze  601:                case ESCAPE_FONT:
1.143     schwarze  602:                case ESCAPE_FONTCR:
1.83      schwarze  603:                case ESCAPE_FONTROMAN:
1.20      schwarze  604:                        term_fontrepl(p, TERMFONT_NONE);
1.93      schwarze  605:                        continue;
1.83      schwarze  606:                case ESCAPE_FONTPREV:
1.20      schwarze  607:                        term_fontlast(p);
1.130     schwarze  608:                        continue;
                    609:                case ESCAPE_BREAK:
                    610:                        bufferc(p, '\n');
1.93      schwarze  611:                        continue;
1.83      schwarze  612:                case ESCAPE_NOSPACE:
1.108     schwarze  613:                        if (p->flags & TERMP_BACKAFTER)
                    614:                                p->flags &= ~TERMP_BACKAFTER;
                    615:                        else if (*word == '\0')
1.97      schwarze  616:                                p->flags |= (TERMP_NOSPACE | TERMP_NONEWLINE);
1.121     schwarze  617:                        continue;
1.135     schwarze  618:                case ESCAPE_DEVICE:
                    619:                        if (p->type == TERMTYPE_PDF)
                    620:                                encode(p, "pdf", 3);
                    621:                        else if (p->type == TERMTYPE_PS)
                    622:                                encode(p, "ps", 2);
                    623:                        else if (p->enc == TERMENC_ASCII)
                    624:                                encode(p, "ascii", 5);
                    625:                        else
                    626:                                encode(p, "utf8", 4);
                    627:                        continue;
1.121     schwarze  628:                case ESCAPE_HORIZ:
1.133     schwarze  629:                        if (*seq == '|') {
                    630:                                seq++;
                    631:                                uc = -p->col;
                    632:                        } else
                    633:                                uc = 0;
1.128     schwarze  634:                        if (a2roffsu(seq, &su, SCALE_EM) == NULL)
1.121     schwarze  635:                                continue;
1.133     schwarze  636:                        uc += term_hen(p, &su);
1.121     schwarze  637:                        if (uc > 0)
                    638:                                while (uc-- > 0)
                    639:                                        bufferc(p, ASCII_NBRSP);
                    640:                        else if (p->col > (size_t)(-uc))
                    641:                                p->col += uc;
                    642:                        else {
                    643:                                uc += p->col;
                    644:                                p->col = 0;
1.126     schwarze  645:                                if (p->tcol->offset > (size_t)(-uc)) {
1.121     schwarze  646:                                        p->ti += uc;
1.126     schwarze  647:                                        p->tcol->offset += uc;
1.121     schwarze  648:                                } else {
1.126     schwarze  649:                                        p->ti -= p->tcol->offset;
                    650:                                        p->tcol->offset = 0;
1.121     schwarze  651:                                }
1.122     schwarze  652:                        }
                    653:                        continue;
                    654:                case ESCAPE_HLINE:
1.132     schwarze  655:                        if ((cp = a2roffsu(seq, &su, SCALE_EM)) == NULL)
1.122     schwarze  656:                                continue;
1.131     schwarze  657:                        uc = term_hen(p, &su);
1.122     schwarze  658:                        if (uc <= 0) {
1.126     schwarze  659:                                if (p->tcol->rmargin <= p->tcol->offset)
1.122     schwarze  660:                                        continue;
1.126     schwarze  661:                                lsz = p->tcol->rmargin - p->tcol->offset;
1.122     schwarze  662:                        } else
                    663:                                lsz = uc;
1.132     schwarze  664:                        if (*cp == seq[-1])
1.122     schwarze  665:                                uc = -1;
1.132     schwarze  666:                        else if (*cp == '\\') {
                    667:                                seq = cp + 1;
1.122     schwarze  668:                                esc = mandoc_escape(&seq, &cp, &sz);
                    669:                                switch (esc) {
                    670:                                case ESCAPE_UNICODE:
                    671:                                        uc = mchars_num2uc(cp + 1, sz - 1);
                    672:                                        break;
                    673:                                case ESCAPE_NUMBERED:
                    674:                                        uc = mchars_num2char(cp, sz);
                    675:                                        break;
                    676:                                case ESCAPE_SPECIAL:
                    677:                                        uc = mchars_spec2cp(cp, sz);
                    678:                                        break;
1.137     schwarze  679:                                case ESCAPE_UNDEF:
                    680:                                        uc = *seq;
                    681:                                        break;
1.122     schwarze  682:                                default:
                    683:                                        uc = -1;
                    684:                                        break;
                    685:                                }
                    686:                        } else
1.132     schwarze  687:                                uc = *cp;
1.122     schwarze  688:                        if (uc < 0x20 || (uc > 0x7E && uc < 0xA0))
                    689:                                uc = '_';
                    690:                        if (p->enc == TERMENC_ASCII) {
                    691:                                cp = ascii_uc2str(uc);
                    692:                                csz = term_strlen(p, cp);
                    693:                                ssz = strlen(cp);
                    694:                        } else
                    695:                                csz = (*p->width)(p, uc);
                    696:                        while (lsz >= csz) {
                    697:                                if (p->enc == TERMENC_ASCII)
                    698:                                        encode(p, cp, ssz);
                    699:                                else
                    700:                                        encode1(p, uc);
                    701:                                lsz -= csz;
1.121     schwarze  702:                        }
1.93      schwarze  703:                        continue;
1.83      schwarze  704:                case ESCAPE_SKIPCHAR:
1.108     schwarze  705:                        p->flags |= TERMP_BACKAFTER;
1.93      schwarze  706:                        continue;
1.103     schwarze  707:                case ESCAPE_OVERSTRIKE:
                    708:                        cp = seq + sz;
                    709:                        while (seq < cp) {
                    710:                                if (*seq == '\\') {
                    711:                                        mandoc_escape(&seq, NULL, NULL);
                    712:                                        continue;
                    713:                                }
                    714:                                encode1(p, *seq++);
1.108     schwarze  715:                                if (seq < cp) {
                    716:                                        if (p->flags & TERMP_BACKBEFORE)
                    717:                                                p->flags |= TERMP_BACKAFTER;
                    718:                                        else
                    719:                                                p->flags |= TERMP_BACKBEFORE;
                    720:                                }
1.103     schwarze  721:                        }
1.109     schwarze  722:                        /* Trim trailing backspace/blank pair. */
1.129     schwarze  723:                        if (p->tcol->lastcol > 2 &&
                    724:                            (p->tcol->buf[p->tcol->lastcol - 1] == ' ' ||
                    725:                             p->tcol->buf[p->tcol->lastcol - 1] == '\t'))
                    726:                                p->tcol->lastcol -= 2;
                    727:                        if (p->col > p->tcol->lastcol)
                    728:                                p->col = p->tcol->lastcol;
1.108     schwarze  729:                        continue;
1.20      schwarze  730:                default:
1.93      schwarze  731:                        continue;
                    732:                }
                    733:
                    734:                /*
                    735:                 * Common handling for Unicode and numbered
                    736:                 * character escape sequences.
                    737:                 */
                    738:
                    739:                if (p->enc == TERMENC_ASCII) {
                    740:                        cp = ascii_uc2str(uc);
                    741:                        encode(p, cp, strlen(cp));
                    742:                } else {
                    743:                        if ((uc < 0x20 && uc != 0x09) ||
                    744:                            (uc > 0x7E && uc < 0xA0))
                    745:                                uc = 0xFFFD;
                    746:                        encode1(p, uc);
1.20      schwarze  747:                }
                    748:        }
1.75      schwarze  749:        p->flags &= ~TERMP_NBRWORD;
1.1       kristaps  750: }
                    751:
                    752: static void
1.126     schwarze  753: adjbuf(struct termp_col *c, size_t sz)
1.1       kristaps  754: {
1.126     schwarze  755:        if (c->maxcols == 0)
                    756:                c->maxcols = 1024;
                    757:        while (c->maxcols <= sz)
                    758:                c->maxcols <<= 2;
                    759:        c->buf = mandoc_reallocarray(c->buf, c->maxcols, sizeof(*c->buf));
1.1       kristaps  760: }
                    761:
1.4       schwarze  762: static void
1.20      schwarze  763: bufferc(struct termp *p, char c)
                    764: {
1.124     schwarze  765:        if (p->flags & TERMP_NOBUF) {
                    766:                (*p->letter)(p, c);
                    767:                return;
                    768:        }
1.126     schwarze  769:        if (p->col + 1 >= p->tcol->maxcols)
                    770:                adjbuf(p->tcol, p->col + 1);
1.129     schwarze  771:        if (p->tcol->lastcol <= p->col || (c != ' ' && c != ASCII_NBRSP))
1.126     schwarze  772:                p->tcol->buf[p->col] = c;
1.129     schwarze  773:        if (p->tcol->lastcol < ++p->col)
                    774:                p->tcol->lastcol = p->col;
1.20      schwarze  775: }
                    776:
1.59      schwarze  777: /*
                    778:  * See encode().
                    779:  * Do this for a single (probably unicode) value.
                    780:  * Does not check for non-decorated glyphs.
                    781:  */
                    782: static void
                    783: encode1(struct termp *p, int c)
                    784: {
                    785:        enum termfont     f;
                    786:
1.124     schwarze  787:        if (p->flags & TERMP_NOBUF) {
                    788:                (*p->letter)(p, c);
                    789:                return;
                    790:        }
                    791:
1.126     schwarze  792:        if (p->col + 7 >= p->tcol->maxcols)
                    793:                adjbuf(p->tcol, p->col + 7);
1.59      schwarze  794:
1.115     schwarze  795:        f = (c == ASCII_HYPH || c > 127 || isgraph(c)) ?
1.108     schwarze  796:            p->fontq[p->fonti] : TERMFONT_NONE;
1.59      schwarze  797:
1.108     schwarze  798:        if (p->flags & TERMP_BACKBEFORE) {
1.126     schwarze  799:                if (p->tcol->buf[p->col - 1] == ' ' ||
                    800:                    p->tcol->buf[p->col - 1] == '\t')
1.109     schwarze  801:                        p->col--;
                    802:                else
1.126     schwarze  803:                        p->tcol->buf[p->col++] = '\b';
1.108     schwarze  804:                p->flags &= ~TERMP_BACKBEFORE;
                    805:        }
1.126     schwarze  806:        if (f == TERMFONT_UNDER || f == TERMFONT_BI) {
                    807:                p->tcol->buf[p->col++] = '_';
                    808:                p->tcol->buf[p->col++] = '\b';
                    809:        }
                    810:        if (f == TERMFONT_BOLD || f == TERMFONT_BI) {
                    811:                if (c == ASCII_HYPH)
                    812:                        p->tcol->buf[p->col++] = '-';
1.70      schwarze  813:                else
1.126     schwarze  814:                        p->tcol->buf[p->col++] = c;
                    815:                p->tcol->buf[p->col++] = '\b';
1.70      schwarze  816:        }
1.129     schwarze  817:        if (p->tcol->lastcol <= p->col || (c != ' ' && c != ASCII_NBRSP))
1.126     schwarze  818:                p->tcol->buf[p->col] = c;
1.129     schwarze  819:        if (p->tcol->lastcol < ++p->col)
                    820:                p->tcol->lastcol = p->col;
1.108     schwarze  821:        if (p->flags & TERMP_BACKAFTER) {
                    822:                p->flags |= TERMP_BACKBEFORE;
                    823:                p->flags &= ~TERMP_BACKAFTER;
                    824:        }
1.59      schwarze  825: }
1.20      schwarze  826:
                    827: static void
                    828: encode(struct termp *p, const char *word, size_t sz)
1.4       schwarze  829: {
1.71      schwarze  830:        size_t            i;
1.124     schwarze  831:
                    832:        if (p->flags & TERMP_NOBUF) {
                    833:                for (i = 0; i < sz; i++)
                    834:                        (*p->letter)(p, word[i]);
                    835:                return;
                    836:        }
1.59      schwarze  837:
1.126     schwarze  838:        if (p->col + 2 + (sz * 5) >= p->tcol->maxcols)
                    839:                adjbuf(p->tcol, p->col + 2 + (sz * 5));
1.46      schwarze  840:
1.71      schwarze  841:        for (i = 0; i < sz; i++) {
1.70      schwarze  842:                if (ASCII_HYPH == word[i] ||
                    843:                    isgraph((unsigned char)word[i]))
                    844:                        encode1(p, word[i]);
1.119     schwarze  845:                else {
1.129     schwarze  846:                        if (p->tcol->lastcol <= p->col ||
1.125     schwarze  847:                            (word[i] != ' ' && word[i] != ASCII_NBRSP))
1.126     schwarze  848:                                p->tcol->buf[p->col] = word[i];
1.125     schwarze  849:                        p->col++;
1.119     schwarze  850:
                    851:                        /*
                    852:                         * Postpone the effect of \z while handling
                    853:                         * an overstrike sequence from ascii_uc2str().
                    854:                         */
                    855:
                    856:                        if (word[i] == '\b' &&
                    857:                            (p->flags & TERMP_BACKBEFORE)) {
                    858:                                p->flags &= ~TERMP_BACKBEFORE;
                    859:                                p->flags |= TERMP_BACKAFTER;
                    860:                        }
                    861:                }
1.4       schwarze  862:        }
1.129     schwarze  863:        if (p->tcol->lastcol < p->col)
                    864:                p->tcol->lastcol = p->col;
1.80      schwarze  865: }
                    866:
                    867: void
                    868: term_setwidth(struct termp *p, const char *wstr)
                    869: {
                    870:        struct roffsu    su;
1.107     schwarze  871:        int              iop, width;
1.80      schwarze  872:
1.81      schwarze  873:        iop = 0;
                    874:        width = 0;
1.80      schwarze  875:        if (NULL != wstr) {
                    876:                switch (*wstr) {
1.83      schwarze  877:                case '+':
1.80      schwarze  878:                        iop = 1;
                    879:                        wstr++;
                    880:                        break;
1.83      schwarze  881:                case '-':
1.80      schwarze  882:                        iop = -1;
                    883:                        wstr++;
                    884:                        break;
                    885:                default:
                    886:                        break;
                    887:                }
1.128     schwarze  888:                if (a2roffsu(wstr, &su, SCALE_MAX) != NULL)
1.81      schwarze  889:                        width = term_hspan(p, &su);
                    890:                else
1.80      schwarze  891:                        iop = 0;
                    892:        }
                    893:        (*p->setwidth)(p, iop, width);
1.4       schwarze  894: }
1.16      schwarze  895:
                    896: size_t
1.39      schwarze  897: term_len(const struct termp *p, size_t sz)
                    898: {
                    899:
1.112     schwarze  900:        return (*p->width)(p, ' ') * sz;
1.39      schwarze  901: }
                    902:
1.64      schwarze  903: static size_t
                    904: cond_width(const struct termp *p, int c, int *skip)
                    905: {
                    906:
                    907:        if (*skip) {
                    908:                (*skip) = 0;
1.112     schwarze  909:                return 0;
1.64      schwarze  910:        } else
1.112     schwarze  911:                return (*p->width)(p, c);
1.64      schwarze  912: }
1.39      schwarze  913:
                    914: size_t
                    915: term_strlen(const struct termp *p, const char *cp)
                    916: {
1.59      schwarze  917:        size_t           sz, rsz, i;
1.93      schwarze  918:        int              ssz, skip, uc;
1.50      schwarze  919:        const char      *seq, *rhs;
1.59      schwarze  920:        enum mandoc_esc  esc;
1.77      schwarze  921:        static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
                    922:                        ASCII_BREAK, '\0' };
1.59      schwarze  923:
                    924:        /*
                    925:         * Account for escaped sequences within string length
                    926:         * calculations.  This follows the logic in term_word() as we
                    927:         * must calculate the width of produced strings.
                    928:         */
                    929:
                    930:        sz = 0;
1.64      schwarze  931:        skip = 0;
1.59      schwarze  932:        while ('\0' != *cp) {
                    933:                rsz = strcspn(cp, rej);
                    934:                for (i = 0; i < rsz; i++)
1.64      schwarze  935:                        sz += cond_width(p, *cp++, &skip);
1.59      schwarze  936:
                    937:                switch (*cp) {
1.83      schwarze  938:                case '\\':
1.59      schwarze  939:                        cp++;
1.137     schwarze  940:                        rhs = NULL;
1.59      schwarze  941:                        esc = mandoc_escape(&cp, &seq, &ssz);
                    942:                        switch (esc) {
1.83      schwarze  943:                        case ESCAPE_UNICODE:
1.94      schwarze  944:                                uc = mchars_num2uc(seq + 1, ssz - 1);
1.59      schwarze  945:                                break;
1.83      schwarze  946:                        case ESCAPE_NUMBERED:
1.93      schwarze  947:                                uc = mchars_num2char(seq, ssz);
                    948:                                if (uc < 0)
                    949:                                        continue;
1.50      schwarze  950:                                break;
1.83      schwarze  951:                        case ESCAPE_SPECIAL:
1.93      schwarze  952:                                if (p->enc == TERMENC_ASCII) {
1.114     schwarze  953:                                        rhs = mchars_spec2str(seq, ssz, &rsz);
1.93      schwarze  954:                                        if (rhs != NULL)
                    955:                                                break;
                    956:                                } else {
1.114     schwarze  957:                                        uc = mchars_spec2cp(seq, ssz);
1.93      schwarze  958:                                        if (uc > 0)
                    959:                                                sz += cond_width(p, uc, &skip);
1.89      schwarze  960:                                }
1.93      schwarze  961:                                continue;
1.137     schwarze  962:                        case ESCAPE_UNDEF:
                    963:                                uc = *seq;
                    964:                                break;
1.135     schwarze  965:                        case ESCAPE_DEVICE:
                    966:                                if (p->type == TERMTYPE_PDF) {
                    967:                                        rhs = "pdf";
                    968:                                        rsz = 3;
                    969:                                } else if (p->type == TERMTYPE_PS) {
                    970:                                        rhs = "ps";
                    971:                                        rsz = 2;
                    972:                                } else if (p->enc == TERMENC_ASCII) {
                    973:                                        rhs = "ascii";
                    974:                                        rsz = 5;
                    975:                                } else {
                    976:                                        rhs = "utf8";
                    977:                                        rsz = 4;
                    978:                                }
                    979:                                break;
1.83      schwarze  980:                        case ESCAPE_SKIPCHAR:
1.64      schwarze  981:                                skip = 1;
1.103     schwarze  982:                                continue;
                    983:                        case ESCAPE_OVERSTRIKE:
                    984:                                rsz = 0;
                    985:                                rhs = seq + ssz;
                    986:                                while (seq < rhs) {
                    987:                                        if (*seq == '\\') {
                    988:                                                mandoc_escape(&seq, NULL, NULL);
                    989:                                                continue;
                    990:                                        }
                    991:                                        i = (*p->width)(p, *seq++);
                    992:                                        if (rsz < i)
                    993:                                                rsz = i;
                    994:                                }
                    995:                                sz += rsz;
1.93      schwarze  996:                                continue;
1.50      schwarze  997:                        default:
1.93      schwarze  998:                                continue;
1.50      schwarze  999:                        }
1.39      schwarze 1000:
1.93      schwarze 1001:                        /*
                   1002:                         * Common handling for Unicode and numbered
                   1003:                         * character escape sequences.
                   1004:                         */
                   1005:
                   1006:                        if (rhs == NULL) {
                   1007:                                if (p->enc == TERMENC_ASCII) {
                   1008:                                        rhs = ascii_uc2str(uc);
                   1009:                                        rsz = strlen(rhs);
                   1010:                                } else {
                   1011:                                        if ((uc < 0x20 && uc != 0x09) ||
                   1012:                                            (uc > 0x7E && uc < 0xA0))
                   1013:                                                uc = 0xFFFD;
                   1014:                                        sz += cond_width(p, uc, &skip);
                   1015:                                        continue;
                   1016:                                }
                   1017:                        }
1.59      schwarze 1018:
1.64      schwarze 1019:                        if (skip) {
                   1020:                                skip = 0;
                   1021:                                break;
                   1022:                        }
1.93      schwarze 1023:
                   1024:                        /*
                   1025:                         * Common handling for all escape sequences
                   1026:                         * printing more than one character.
                   1027:                         */
1.64      schwarze 1028:
1.59      schwarze 1029:                        for (i = 0; i < rsz; i++)
                   1030:                                sz += (*p->width)(p, *rhs++);
                   1031:                        break;
1.83      schwarze 1032:                case ASCII_NBRSP:
1.64      schwarze 1033:                        sz += cond_width(p, ' ', &skip);
1.55      schwarze 1034:                        cp++;
1.59      schwarze 1035:                        break;
1.83      schwarze 1036:                case ASCII_HYPH:
1.64      schwarze 1037:                        sz += cond_width(p, '-', &skip);
1.55      schwarze 1038:                        cp++;
1.59      schwarze 1039:                        break;
                   1040:                default:
                   1041:                        break;
                   1042:                }
                   1043:        }
1.39      schwarze 1044:
1.112     schwarze 1045:        return sz;
1.39      schwarze 1046: }
                   1047:
1.100     schwarze 1048: int
1.39      schwarze 1049: term_vspan(const struct termp *p, const struct roffsu *su)
1.16      schwarze 1050: {
                   1051:        double           r;
1.101     schwarze 1052:        int              ri;
1.16      schwarze 1053:
                   1054:        switch (su->unit) {
1.99      schwarze 1055:        case SCALE_BU:
                   1056:                r = su->scale / 40.0;
                   1057:                break;
1.83      schwarze 1058:        case SCALE_CM:
1.99      schwarze 1059:                r = su->scale * 6.0 / 2.54;
                   1060:                break;
                   1061:        case SCALE_FS:
                   1062:                r = su->scale * 65536.0 / 40.0;
1.16      schwarze 1063:                break;
1.83      schwarze 1064:        case SCALE_IN:
1.86      schwarze 1065:                r = su->scale * 6.0;
1.16      schwarze 1066:                break;
1.99      schwarze 1067:        case SCALE_MM:
                   1068:                r = su->scale * 0.006;
                   1069:                break;
1.83      schwarze 1070:        case SCALE_PC:
1.16      schwarze 1071:                r = su->scale;
                   1072:                break;
1.83      schwarze 1073:        case SCALE_PT:
1.99      schwarze 1074:                r = su->scale / 12.0;
1.16      schwarze 1075:                break;
1.99      schwarze 1076:        case SCALE_EN:
                   1077:        case SCALE_EM:
                   1078:                r = su->scale * 0.6;
1.16      schwarze 1079:                break;
1.83      schwarze 1080:        case SCALE_VS:
1.16      schwarze 1081:                r = su->scale;
                   1082:                break;
                   1083:        default:
1.99      schwarze 1084:                abort();
1.16      schwarze 1085:        }
1.101     schwarze 1086:        ri = r > 0.0 ? r + 0.4995 : r - 0.4995;
1.112     schwarze 1087:        return ri < 66 ? ri : 1;
1.16      schwarze 1088: }
                   1089:
1.107     schwarze 1090: /*
1.131     schwarze 1091:  * Convert a scaling width to basic units, rounding towards 0.
1.107     schwarze 1092:  */
1.100     schwarze 1093: int
1.39      schwarze 1094: term_hspan(const struct termp *p, const struct roffsu *su)
1.16      schwarze 1095: {
                   1096:
1.112     schwarze 1097:        return (*p->hspan)(p, su);
1.131     schwarze 1098: }
                   1099:
                   1100: /*
                   1101:  * Convert a scaling width to basic units, rounding to closest.
                   1102:  */
                   1103: int
                   1104: term_hen(const struct termp *p, const struct roffsu *su)
                   1105: {
                   1106:        int bu;
                   1107:
                   1108:        if ((bu = (*p->hspan)(p, su)) >= 0)
                   1109:                return (bu + 11) / 24;
                   1110:        else
                   1111:                return -((-bu + 11) / 24);
1.16      schwarze 1112: }