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

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