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

Annotation of mandoc/term.c, Revision 1.290

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

CVSweb