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

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