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

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

1.60    ! schwarze    1: /*     $Id: term.c,v 1.59 2011/05/29 21:22:18 schwarze Exp $ */
1.1       kristaps    2: /*
1.59      schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.56      schwarze    4:  * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.2       schwarze    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.2       schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   17:  */
1.20      schwarze   18: #include <sys/types.h>
                     19:
1.1       kristaps   20: #include <assert.h>
1.20      schwarze   21: #include <ctype.h>
1.36      schwarze   22: #include <stdint.h>
1.1       kristaps   23: #include <stdio.h>
                     24: #include <stdlib.h>
                     25: #include <string.h>
                     26:
1.34      schwarze   27: #include "mandoc.h"
1.16      schwarze   28: #include "out.h"
1.1       kristaps   29: #include "term.h"
1.16      schwarze   30: #include "main.h"
1.1       kristaps   31:
1.59      schwarze   32: static void             adjbuf(struct termp *p, int);
                     33: static void             bufferc(struct termp *, char);
                     34: static void             encode(struct termp *, const char *, size_t);
                     35: static void             encode1(struct termp *, int);
1.1       kristaps   36:
1.37      schwarze   37: void
                     38: term_free(struct termp *p)
1.1       kristaps   39: {
                     40:
1.37      schwarze   41:        if (p->buf)
                     42:                free(p->buf);
                     43:        if (p->symtab)
1.59      schwarze   44:                mchars_free(p->symtab);
1.37      schwarze   45:
                     46:        free(p);
1.1       kristaps   47: }
                     48:
                     49:
1.13      schwarze   50: void
1.37      schwarze   51: term_begin(struct termp *p, term_margin head,
                     52:                term_margin foot, const void *arg)
1.1       kristaps   53: {
                     54:
1.37      schwarze   55:        p->headf = head;
                     56:        p->footf = foot;
                     57:        p->argf = arg;
                     58:        (*p->begin)(p);
1.1       kristaps   59: }
                     60:
                     61:
1.37      schwarze   62: void
                     63: term_end(struct termp *p)
1.1       kristaps   64: {
                     65:
1.37      schwarze   66:        (*p->end)(p);
1.1       kristaps   67: }
                     68:
                     69: /*
                     70:  * Flush a line of text.  A "line" is loosely defined as being something
                     71:  * that should be followed by a newline, regardless of whether it's
                     72:  * broken apart by newlines getting there.  A line can also be a
1.27      schwarze   73:  * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
                     74:  * not have a trailing newline.
1.1       kristaps   75:  *
1.27      schwarze   76:  * The following flags may be specified:
1.1       kristaps   77:  *
                     78:  *  - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
                     79:  *    offset value.  This is useful when doing columnar lists where the
                     80:  *    prior column has right-padded.
                     81:  *
                     82:  *  - TERMP_NOBREAK: this is the most important and is used when making
                     83:  *    columns.  In short: don't print a newline and instead pad to the
                     84:  *    right margin.  Used in conjunction with TERMP_NOLPAD.
                     85:  *
1.9       schwarze   86:  *  - TERMP_TWOSPACE: when padding, make sure there are at least two
                     87:  *    space characters of padding.  Otherwise, rather break the line.
                     88:  *
1.6       schwarze   89:  *  - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
                     90:  *    the line is overrun, and don't pad-right if it's underrun.
                     91:  *
                     92:  *  - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
                     93:  *    overruning, instead save the position and continue at that point
                     94:  *    when the next invocation.
1.1       kristaps   95:  *
                     96:  *  In-line line breaking:
                     97:  *
                     98:  *  If TERMP_NOBREAK is specified and the line overruns the right
                     99:  *  margin, it will break and pad-right to the right margin after
                    100:  *  writing.  If maxrmargin is violated, it will break and continue
1.19      schwarze  101:  *  writing from the right-margin, which will lead to the above scenario
                    102:  *  upon exit.  Otherwise, the line will break at the right margin.
1.1       kristaps  103:  */
                    104: void
                    105: term_flushln(struct termp *p)
                    106: {
1.19      schwarze  107:        int              i;     /* current input position in p->buf */
                    108:        size_t           vis;   /* current visual position on output */
                    109:        size_t           vbl;   /* number of blanks to prepend to output */
1.33      schwarze  110:        size_t           vend;  /* end of word visual position on output */
1.19      schwarze  111:        size_t           bp;    /* visual right border position */
1.51      schwarze  112:        size_t           dv;    /* temporary for visual pos calculations */
1.42      schwarze  113:        int              j;     /* temporary loop index for p->buf */
                    114:        int              jhy;   /* last hyph before overflow w/r/t j */
                    115:        size_t           maxvis; /* output position of visible boundary */
                    116:        size_t           mmax; /* used in calculating bp */
1.1       kristaps  117:
                    118:        /*
                    119:         * First, establish the maximum columns of "visible" content.
                    120:         * This is usually the difference between the right-margin and
                    121:         * an indentation, but can be, for tagged lists or columns, a
1.19      schwarze  122:         * small set of values.
1.1       kristaps  123:         */
1.54      schwarze  124:        assert  (p->rmargin >= p->offset);
1.53      schwarze  125:        dv     = p->rmargin - p->offset;
                    126:        maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
                    127:        dv     = p->maxrmargin - p->offset;
                    128:        mmax   = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
1.9       schwarze  129:
1.1       kristaps  130:        bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
1.19      schwarze  131:
1.33      schwarze  132:        /*
                    133:         * Indent the first line of a paragraph.
                    134:         */
1.53      schwarze  135:        vbl = p->flags & TERMP_NOLPAD ? (size_t)0 : p->offset;
1.33      schwarze  136:
1.53      schwarze  137:        vis = vend = 0;
                    138:        i = 0;
1.19      schwarze  139:
1.59      schwarze  140:        while (i < p->col) {
1.22      schwarze  141:                /*
1.42      schwarze  142:                 * Handle literal tab characters: collapse all
                    143:                 * subsequent tabs into a single huge set of spaces.
1.30      schwarze  144:                 */
1.59      schwarze  145:                while (i < p->col && '\t' == p->buf[i]) {
1.42      schwarze  146:                        vend = (vis / p->tabwidth + 1) * p->tabwidth;
1.30      schwarze  147:                        vbl += vend - vis;
                    148:                        vis = vend;
1.49      schwarze  149:                        i++;
1.30      schwarze  150:                }
1.22      schwarze  151:
1.1       kristaps  152:                /*
                    153:                 * Count up visible word characters.  Control sequences
                    154:                 * (starting with the CSI) aren't counted.  A space
                    155:                 * generates a non-printing word, which is valid (the
                    156:                 * space is printed according to regular spacing rules).
                    157:                 */
                    158:
1.59      schwarze  159:                for (j = i, jhy = 0; j < p->col; j++) {
1.30      schwarze  160:                        if ((j && ' ' == p->buf[j]) || '\t' == p->buf[j])
1.1       kristaps  161:                                break;
1.42      schwarze  162:
                    163:                        /* Back over the the last printed character. */
                    164:                        if (8 == p->buf[j]) {
                    165:                                assert(j);
                    166:                                vend -= (*p->width)(p, p->buf[j - 1]);
                    167:                                continue;
                    168:                        }
                    169:
                    170:                        /* Regular word. */
                    171:                        /* Break at the hyphen point if we overrun. */
                    172:                        if (vend > vis && vend < bp &&
                    173:                                        ASCII_HYPH == p->buf[j])
                    174:                                jhy = j;
                    175:
                    176:                        vend += (*p->width)(p, p->buf[j]);
1.1       kristaps  177:                }
                    178:
                    179:                /*
1.5       schwarze  180:                 * Find out whether we would exceed the right margin.
1.33      schwarze  181:                 * If so, break to the next line.
1.5       schwarze  182:                 */
1.33      schwarze  183:                if (vend > bp && 0 == jhy && vis > 0) {
1.22      schwarze  184:                        vend -= vis;
1.37      schwarze  185:                        (*p->endline)(p);
1.5       schwarze  186:                        if (TERMP_NOBREAK & p->flags) {
1.29      schwarze  187:                                p->viscol = p->rmargin;
1.37      schwarze  188:                                (*p->advance)(p, p->rmargin);
1.22      schwarze  189:                                vend += p->rmargin - p->offset;
1.5       schwarze  190:                        } else {
1.33      schwarze  191:                                p->viscol = 0;
                    192:                                vbl = p->offset;
1.5       schwarze  193:                        }
1.33      schwarze  194:
1.26      schwarze  195:                        /* Remove the p->overstep width. */
1.33      schwarze  196:
1.53      schwarze  197:                        bp += (size_t)p->overstep;
1.26      schwarze  198:                        p->overstep = 0;
1.1       kristaps  199:                }
1.30      schwarze  200:
1.33      schwarze  201:                /* Write out the [remaining] word. */
1.59      schwarze  202:                for ( ; i < p->col; i++) {
1.25      schwarze  203:                        if (vend > bp && jhy > 0 && i > jhy)
1.30      schwarze  204:                                break;
                    205:                        if ('\t' == p->buf[i])
1.1       kristaps  206:                                break;
1.22      schwarze  207:                        if (' ' == p->buf[i]) {
1.46      schwarze  208:                                j = i;
                    209:                                while (' ' == p->buf[i])
1.33      schwarze  210:                                        i++;
1.53      schwarze  211:                                dv = (size_t)(i - j) * (*p->width)(p, ' ');
1.51      schwarze  212:                                vbl += dv;
                    213:                                vend += dv;
1.22      schwarze  214:                                break;
                    215:                        }
1.33      schwarze  216:                        if (ASCII_NBRSP == p->buf[i]) {
1.42      schwarze  217:                                vbl += (*p->width)(p, ' ');
1.33      schwarze  218:                                continue;
                    219:                        }
                    220:
                    221:                        /*
                    222:                         * Now we definitely know there will be
                    223:                         * printable characters to output,
                    224:                         * so write preceding white space now.
                    225:                         */
                    226:                        if (vbl) {
1.37      schwarze  227:                                (*p->advance)(p, vbl);
1.33      schwarze  228:                                p->viscol += vbl;
                    229:                                vbl = 0;
                    230:                        }
1.35      schwarze  231:
1.42      schwarze  232:                        if (ASCII_HYPH == p->buf[i]) {
1.37      schwarze  233:                                (*p->letter)(p, '-');
1.42      schwarze  234:                                p->viscol += (*p->width)(p, '-');
                    235:                        } else {
1.37      schwarze  236:                                (*p->letter)(p, p->buf[i]);
1.42      schwarze  237:                                p->viscol += (*p->width)(p, p->buf[i]);
                    238:                        }
1.1       kristaps  239:                }
1.22      schwarze  240:                vis = vend;
1.1       kristaps  241:        }
1.48      schwarze  242:
                    243:        /*
                    244:         * If there was trailing white space, it was not printed;
                    245:         * so reset the cursor position accordingly.
                    246:         */
                    247:        vis -= vbl;
1.18      schwarze  248:
1.9       schwarze  249:        p->col = 0;
1.26      schwarze  250:        p->overstep = 0;
1.1       kristaps  251:
1.9       schwarze  252:        if ( ! (TERMP_NOBREAK & p->flags)) {
1.29      schwarze  253:                p->viscol = 0;
1.37      schwarze  254:                (*p->endline)(p);
1.1       kristaps  255:                return;
                    256:        }
                    257:
1.9       schwarze  258:        if (TERMP_HANG & p->flags) {
                    259:                /* We need one blank after the tag. */
1.53      schwarze  260:                p->overstep = (int)(vis - maxvis + (*p->width)(p, ' '));
1.9       schwarze  261:
                    262:                /*
                    263:                 * Behave exactly the same way as groff:
                    264:                 * If we have overstepped the margin, temporarily move
                    265:                 * it to the right and flag the rest of the line to be
                    266:                 * shorter.
                    267:                 * If we landed right at the margin, be happy.
                    268:                 * If we are one step before the margin, temporarily
                    269:                 * move it one step LEFT and flag the rest of the line
                    270:                 * to be longer.
                    271:                 */
1.26      schwarze  272:                if (p->overstep >= -1) {
                    273:                        assert((int)maxvis + p->overstep >= 0);
1.53      schwarze  274:                        maxvis += (size_t)p->overstep;
1.9       schwarze  275:                } else
1.26      schwarze  276:                        p->overstep = 0;
1.9       schwarze  277:
                    278:        } else if (TERMP_DANGLE & p->flags)
                    279:                return;
1.1       kristaps  280:
1.9       schwarze  281:        /* Right-pad. */
1.53      schwarze  282:        if (maxvis > vis +
                    283:            ((TERMP_TWOSPACE & p->flags) ? (*p->width)(p, ' ') : 0)) {
1.29      schwarze  284:                p->viscol += maxvis - vis;
1.37      schwarze  285:                (*p->advance)(p, maxvis - vis);
                    286:                vis += (maxvis - vis);
1.29      schwarze  287:        } else {        /* ...or newline break. */
1.37      schwarze  288:                (*p->endline)(p);
1.29      schwarze  289:                p->viscol = p->rmargin;
1.37      schwarze  290:                (*p->advance)(p, p->rmargin);
1.9       schwarze  291:        }
1.1       kristaps  292: }
                    293:
                    294:
                    295: /*
                    296:  * A newline only breaks an existing line; it won't assert vertical
                    297:  * space.  All data in the output buffer is flushed prior to the newline
                    298:  * assertion.
                    299:  */
                    300: void
                    301: term_newln(struct termp *p)
                    302: {
                    303:
                    304:        p->flags |= TERMP_NOSPACE;
1.29      schwarze  305:        if (0 == p->col && 0 == p->viscol) {
1.1       kristaps  306:                p->flags &= ~TERMP_NOLPAD;
                    307:                return;
                    308:        }
                    309:        term_flushln(p);
                    310:        p->flags &= ~TERMP_NOLPAD;
                    311: }
                    312:
                    313:
                    314: /*
                    315:  * Asserts a vertical space (a full, empty line-break between lines).
                    316:  * Note that if used twice, this will cause two blank spaces and so on.
                    317:  * All data in the output buffer is flushed prior to the newline
                    318:  * assertion.
                    319:  */
                    320: void
                    321: term_vspace(struct termp *p)
                    322: {
                    323:
                    324:        term_newln(p);
1.29      schwarze  325:        p->viscol = 0;
1.37      schwarze  326:        (*p->endline)(p);
1.1       kristaps  327: }
                    328:
1.20      schwarze  329: void
                    330: term_fontlast(struct termp *p)
                    331: {
                    332:        enum termfont    f;
1.11      schwarze  333:
1.20      schwarze  334:        f = p->fontl;
                    335:        p->fontl = p->fontq[p->fonti];
                    336:        p->fontq[p->fonti] = f;
                    337: }
                    338:
                    339:
                    340: void
                    341: term_fontrepl(struct termp *p, enum termfont f)
                    342: {
                    343:
                    344:        p->fontl = p->fontq[p->fonti];
                    345:        p->fontq[p->fonti] = f;
1.1       kristaps  346: }
                    347:
                    348:
1.20      schwarze  349: void
                    350: term_fontpush(struct termp *p, enum termfont f)
1.1       kristaps  351: {
1.7       schwarze  352:
1.20      schwarze  353:        assert(p->fonti + 1 < 10);
                    354:        p->fontl = p->fontq[p->fonti];
                    355:        p->fontq[++p->fonti] = f;
                    356: }
1.1       kristaps  357:
                    358:
1.20      schwarze  359: const void *
                    360: term_fontq(struct termp *p)
                    361: {
1.1       kristaps  362:
1.20      schwarze  363:        return(&p->fontq[p->fonti]);
                    364: }
1.1       kristaps  365:
                    366:
1.20      schwarze  367: enum termfont
                    368: term_fonttop(struct termp *p)
                    369: {
1.1       kristaps  370:
1.20      schwarze  371:        return(p->fontq[p->fonti]);
                    372: }
1.7       schwarze  373:
                    374:
1.20      schwarze  375: void
                    376: term_fontpopq(struct termp *p, const void *key)
                    377: {
1.1       kristaps  378:
1.20      schwarze  379:        while (p->fonti >= 0 && key != &p->fontq[p->fonti])
                    380:                p->fonti--;
                    381:        assert(p->fonti >= 0);
                    382: }
1.1       kristaps  383:
                    384:
1.20      schwarze  385: void
                    386: term_fontpop(struct termp *p)
                    387: {
1.1       kristaps  388:
1.20      schwarze  389:        assert(p->fonti);
                    390:        p->fonti--;
1.1       kristaps  391: }
                    392:
                    393: /*
                    394:  * Handle pwords, partial words, which may be either a single word or a
                    395:  * phrase that cannot be broken down (such as a literal string).  This
                    396:  * handles word styling.
                    397:  */
1.7       schwarze  398: void
                    399: term_word(struct termp *p, const char *word)
1.1       kristaps  400: {
1.59      schwarze  401:        const char      *seq, *cp;
                    402:        char             c;
                    403:        int              sz, uc;
1.20      schwarze  404:        size_t           ssz;
1.59      schwarze  405:        enum mandoc_esc  esc;
1.1       kristaps  406:
1.31      schwarze  407:        if ( ! (TERMP_NOSPACE & p->flags)) {
1.40      schwarze  408:                if ( ! (TERMP_KEEP & p->flags)) {
                    409:                        if (TERMP_PREKEEP & p->flags)
                    410:                                p->flags |= TERMP_KEEP;
1.31      schwarze  411:                        bufferc(p, ' ');
1.40      schwarze  412:                        if (TERMP_SENTENCE & p->flags)
                    413:                                bufferc(p, ' ');
                    414:                } else
                    415:                        bufferc(p, ASCII_NBRSP);
1.31      schwarze  416:        }
1.1       kristaps  417:
                    418:        if ( ! (p->flags & TERMP_NONOSPACE))
                    419:                p->flags &= ~TERMP_NOSPACE;
1.46      schwarze  420:        else
                    421:                p->flags |= TERMP_NOSPACE;
1.1       kristaps  422:
1.52      schwarze  423:        p->flags &= ~(TERMP_SENTENCE | TERMP_IGNDELIM);
1.31      schwarze  424:
1.59      schwarze  425:        while ('\0' != *word) {
1.45      schwarze  426:                if ((ssz = strcspn(word, "\\")) > 0)
                    427:                        encode(p, word, ssz);
1.20      schwarze  428:
1.57      schwarze  429:                word += (int)ssz;
1.45      schwarze  430:                if ('\\' != *word)
1.20      schwarze  431:                        continue;
                    432:
1.59      schwarze  433:                word++;
                    434:                esc = mandoc_escape(&word, &seq, &sz);
                    435:                if (ESCAPE_ERROR == esc)
                    436:                        break;
                    437:
                    438:                if (TERMENC_ASCII != p->enc)
                    439:                        switch (esc) {
                    440:                        case (ESCAPE_UNICODE):
                    441:                                uc = mchars_num2uc(seq + 1, sz - 1);
                    442:                                if ('\0' == uc)
                    443:                                        break;
                    444:                                encode1(p, uc);
                    445:                                continue;
                    446:                        case (ESCAPE_SPECIAL):
                    447:                                uc = mchars_spec2cp(p->symtab, seq, sz);
                    448:                                if (uc <= 0)
                    449:                                        break;
                    450:                                encode1(p, uc);
                    451:                                continue;
                    452:                        default:
                    453:                                break;
                    454:                        }
1.20      schwarze  455:
1.59      schwarze  456:                switch (esc) {
                    457:                case (ESCAPE_UNICODE):
                    458:                        encode1(p, '?');
1.56      schwarze  459:                        break;
1.59      schwarze  460:                case (ESCAPE_NUMBERED):
                    461:                        c = mchars_num2char(seq, sz);
                    462:                        if ('\0' != c)
                    463:                                encode(p, &c, 1);
1.20      schwarze  464:                        break;
1.59      schwarze  465:                case (ESCAPE_SPECIAL):
                    466:                        cp = mchars_spec2str(p->symtab, seq, sz, &ssz);
                    467:                        if (NULL != cp)
                    468:                                encode(p, cp, ssz);
                    469:                        else if (1 == ssz)
                    470:                                encode(p, seq, sz);
1.20      schwarze  471:                        break;
1.59      schwarze  472:                case (ESCAPE_FONTBOLD):
1.20      schwarze  473:                        term_fontrepl(p, TERMFONT_BOLD);
                    474:                        break;
1.59      schwarze  475:                case (ESCAPE_FONTITALIC):
1.20      schwarze  476:                        term_fontrepl(p, TERMFONT_UNDER);
                    477:                        break;
1.59      schwarze  478:                case (ESCAPE_FONT):
                    479:                        /* FALLTHROUGH */
                    480:                case (ESCAPE_FONTROMAN):
1.20      schwarze  481:                        term_fontrepl(p, TERMFONT_NONE);
                    482:                        break;
1.59      schwarze  483:                case (ESCAPE_FONTPREV):
1.20      schwarze  484:                        term_fontlast(p);
                    485:                        break;
1.59      schwarze  486:                case (ESCAPE_NOSPACE):
                    487:                        if ('\0' == *word)
                    488:                                p->flags |= TERMP_NOSPACE;
                    489:                        break;
1.20      schwarze  490:                default:
                    491:                        break;
                    492:                }
                    493:        }
1.1       kristaps  494: }
                    495:
                    496: static void
1.59      schwarze  497: adjbuf(struct termp *p, int sz)
1.1       kristaps  498: {
                    499:
1.20      schwarze  500:        if (0 == p->maxcols)
                    501:                p->maxcols = 1024;
                    502:        while (sz >= p->maxcols)
                    503:                p->maxcols <<= 2;
                    504:
1.59      schwarze  505:        p->buf = mandoc_realloc
                    506:                (p->buf, sizeof(int) * (size_t)p->maxcols);
1.1       kristaps  507: }
                    508:
1.4       schwarze  509: static void
1.20      schwarze  510: bufferc(struct termp *p, char c)
                    511: {
                    512:
                    513:        if (p->col + 1 >= p->maxcols)
                    514:                adjbuf(p, p->col + 1);
                    515:
1.59      schwarze  516:        p->buf[p->col++] = c;
1.20      schwarze  517: }
                    518:
1.59      schwarze  519: /*
                    520:  * See encode().
                    521:  * Do this for a single (probably unicode) value.
                    522:  * Does not check for non-decorated glyphs.
                    523:  */
                    524: static void
                    525: encode1(struct termp *p, int c)
                    526: {
                    527:        enum termfont     f;
                    528:
                    529:        if (p->col + 4 >= p->maxcols)
                    530:                adjbuf(p, p->col + 4);
                    531:
                    532:        f = term_fonttop(p);
                    533:
                    534:        if (TERMFONT_NONE == f) {
                    535:                p->buf[p->col++] = c;
                    536:                return;
                    537:        } else if (TERMFONT_UNDER == f) {
                    538:                p->buf[p->col++] = '_';
                    539:        } else
                    540:                p->buf[p->col++] = c;
                    541:
                    542:        p->buf[p->col++] = 8;
                    543:        p->buf[p->col++] = c;
                    544: }
1.20      schwarze  545:
                    546: static void
                    547: encode(struct termp *p, const char *word, size_t sz)
1.4       schwarze  548: {
1.20      schwarze  549:        enum termfont     f;
1.59      schwarze  550:        int               i, len;
                    551:
                    552:        /* LINTED */
                    553:        len = sz;
1.20      schwarze  554:
                    555:        /*
                    556:         * Encode and buffer a string of characters.  If the current
                    557:         * font mode is unset, buffer directly, else encode then buffer
                    558:         * character by character.
                    559:         */
                    560:
1.38      schwarze  561:        if (TERMFONT_NONE == (f = term_fonttop(p))) {
1.59      schwarze  562:                if (p->col + len >= p->maxcols)
                    563:                        adjbuf(p, p->col + len);
                    564:                for (i = 0; i < len; i++)
                    565:                        p->buf[p->col++] = word[i];
1.20      schwarze  566:                return;
                    567:        }
                    568:
1.46      schwarze  569:        /* Pre-buffer, assuming worst-case. */
                    570:
1.59      schwarze  571:        if (p->col + 1 + (len * 3) >= p->maxcols)
                    572:                adjbuf(p, p->col + 1 + (len * 3));
1.46      schwarze  573:
1.59      schwarze  574:        for (i = 0; i < len; i++) {
1.60    ! schwarze  575:                if (ASCII_HYPH != word[i] &&
        !           576:                    ! isgraph((unsigned char)word[i])) {
1.59      schwarze  577:                        p->buf[p->col++] = word[i];
1.20      schwarze  578:                        continue;
1.4       schwarze  579:                }
1.20      schwarze  580:
                    581:                if (TERMFONT_UNDER == f)
1.59      schwarze  582:                        p->buf[p->col++] = '_';
1.60    ! schwarze  583:                else if (ASCII_HYPH == word[i])
        !           584:                        p->buf[p->col++] = '-';
1.20      schwarze  585:                else
1.59      schwarze  586:                        p->buf[p->col++] = word[i];
1.20      schwarze  587:
1.59      schwarze  588:                p->buf[p->col++] = 8;
                    589:                p->buf[p->col++] = word[i];
1.4       schwarze  590:        }
                    591: }
1.16      schwarze  592:
                    593: size_t
1.39      schwarze  594: term_len(const struct termp *p, size_t sz)
                    595: {
                    596:
                    597:        return((*p->width)(p, ' ') * sz);
                    598: }
                    599:
                    600:
                    601: size_t
                    602: term_strlen(const struct termp *p, const char *cp)
                    603: {
1.59      schwarze  604:        size_t           sz, rsz, i;
                    605:        int              ssz, c;
1.50      schwarze  606:        const char      *seq, *rhs;
1.59      schwarze  607:        enum mandoc_esc  esc;
                    608:        static const char rej[] = { '\\', ASCII_HYPH, ASCII_NBRSP, '\0' };
                    609:
                    610:        /*
                    611:         * Account for escaped sequences within string length
                    612:         * calculations.  This follows the logic in term_word() as we
                    613:         * must calculate the width of produced strings.
                    614:         */
                    615:
                    616:        sz = 0;
                    617:        while ('\0' != *cp) {
                    618:                rsz = strcspn(cp, rej);
                    619:                for (i = 0; i < rsz; i++)
                    620:                        sz += (*p->width)(p, *cp++);
                    621:
                    622:                c = 0;
                    623:                switch (*cp) {
                    624:                case ('\\'):
                    625:                        cp++;
                    626:                        esc = mandoc_escape(&cp, &seq, &ssz);
                    627:                        if (ESCAPE_ERROR == esc)
                    628:                                return(sz);
                    629:
                    630:                        if (TERMENC_ASCII != p->enc)
                    631:                                switch (esc) {
                    632:                                case (ESCAPE_UNICODE):
                    633:                                        c = mchars_num2uc
                    634:                                                (seq + 1, ssz - 1);
                    635:                                        if ('\0' == c)
                    636:                                                break;
                    637:                                        sz += (*p->width)(p, c);
                    638:                                        continue;
                    639:                                case (ESCAPE_SPECIAL):
                    640:                                        c = mchars_spec2cp
                    641:                                                (p->symtab, seq, ssz);
                    642:                                        if (c <= 0)
                    643:                                                break;
                    644:                                        sz += (*p->width)(p, c);
                    645:                                        continue;
                    646:                                default:
                    647:                                        break;
                    648:                                }
                    649:
                    650:                        rhs = NULL;
1.50      schwarze  651:
1.59      schwarze  652:                        switch (esc) {
                    653:                        case (ESCAPE_UNICODE):
                    654:                                sz += (*p->width)(p, '?');
                    655:                                break;
                    656:                        case (ESCAPE_NUMBERED):
                    657:                                c = mchars_num2char(seq, ssz);
                    658:                                if ('\0' != c)
                    659:                                        sz += (*p->width)(p, c);
1.50      schwarze  660:                                break;
1.59      schwarze  661:                        case (ESCAPE_SPECIAL):
                    662:                                rhs = mchars_spec2str
1.50      schwarze  663:                                        (p->symtab, seq, ssz, &rsz);
                    664:
1.59      schwarze  665:                                if (ssz != 1 || rhs)
1.50      schwarze  666:                                        break;
                    667:
                    668:                                rhs = seq;
                    669:                                rsz = ssz;
                    670:                                break;
                    671:                        default:
                    672:                                break;
                    673:                        }
1.39      schwarze  674:
1.59      schwarze  675:                        if (NULL == rhs)
                    676:                                break;
                    677:
                    678:                        for (i = 0; i < rsz; i++)
                    679:                                sz += (*p->width)(p, *rhs++);
                    680:                        break;
                    681:                case (ASCII_NBRSP):
1.55      schwarze  682:                        sz += (*p->width)(p, ' ');
                    683:                        cp++;
1.59      schwarze  684:                        break;
                    685:                case (ASCII_HYPH):
1.55      schwarze  686:                        sz += (*p->width)(p, '-');
                    687:                        cp++;
1.59      schwarze  688:                        break;
                    689:                default:
                    690:                        break;
                    691:                }
                    692:        }
1.39      schwarze  693:
                    694:        return(sz);
                    695: }
                    696:
1.44      schwarze  697: /* ARGSUSED */
1.39      schwarze  698: size_t
                    699: term_vspan(const struct termp *p, const struct roffsu *su)
1.16      schwarze  700: {
                    701:        double           r;
                    702:
                    703:        switch (su->unit) {
                    704:        case (SCALE_CM):
                    705:                r = su->scale * 2;
                    706:                break;
                    707:        case (SCALE_IN):
                    708:                r = su->scale * 6;
                    709:                break;
                    710:        case (SCALE_PC):
                    711:                r = su->scale;
                    712:                break;
                    713:        case (SCALE_PT):
                    714:                r = su->scale / 8;
                    715:                break;
                    716:        case (SCALE_MM):
                    717:                r = su->scale / 1000;
                    718:                break;
                    719:        case (SCALE_VS):
                    720:                r = su->scale;
                    721:                break;
                    722:        default:
                    723:                r = su->scale - 1;
                    724:                break;
                    725:        }
                    726:
                    727:        if (r < 0.0)
                    728:                r = 0.0;
                    729:        return(/* LINTED */(size_t)
                    730:                        r);
                    731: }
                    732:
                    733: size_t
1.39      schwarze  734: term_hspan(const struct termp *p, const struct roffsu *su)
1.16      schwarze  735: {
1.44      schwarze  736:        double           v;
1.16      schwarze  737:
1.44      schwarze  738:        v = ((*p->hspan)(p, su));
                    739:        if (v < 0.0)
                    740:                v = 0.0;
                    741:        return((size_t) /* LINTED */
                    742:                        v);
1.16      schwarze  743: }