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

Annotation of mandoc/term.c, Revision 1.292

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

CVSweb