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

1.75    ! schwarze    1: /*     $Id: term.c,v 1.74 2013/12/24 23:04:29 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.75    ! schwarze  406:        const char       nbrsp[2] = { ASCII_NBRSP, 0 };
1.59      schwarze  407:        const char      *seq, *cp;
                    408:        char             c;
                    409:        int              sz, uc;
1.20      schwarze  410:        size_t           ssz;
1.59      schwarze  411:        enum mandoc_esc  esc;
1.1       kristaps  412:
1.31      schwarze  413:        if ( ! (TERMP_NOSPACE & p->flags)) {
1.40      schwarze  414:                if ( ! (TERMP_KEEP & p->flags)) {
1.31      schwarze  415:                        bufferc(p, ' ');
1.40      schwarze  416:                        if (TERMP_SENTENCE & p->flags)
                    417:                                bufferc(p, ' ');
                    418:                } else
                    419:                        bufferc(p, ASCII_NBRSP);
1.31      schwarze  420:        }
1.68      schwarze  421:        if (TERMP_PREKEEP & p->flags)
                    422:                p->flags |= TERMP_KEEP;
1.1       kristaps  423:
                    424:        if ( ! (p->flags & TERMP_NONOSPACE))
                    425:                p->flags &= ~TERMP_NOSPACE;
1.46      schwarze  426:        else
                    427:                p->flags |= TERMP_NOSPACE;
1.1       kristaps  428:
1.74      schwarze  429:        p->flags &= ~TERMP_SENTENCE;
1.31      schwarze  430:
1.59      schwarze  431:        while ('\0' != *word) {
1.64      schwarze  432:                if ('\\' != *word) {
                    433:                        if (TERMP_SKIPCHAR & p->flags) {
                    434:                                p->flags &= ~TERMP_SKIPCHAR;
                    435:                                word++;
                    436:                                continue;
                    437:                        }
1.75    ! schwarze  438:                        if (TERMP_NBRWORD & p->flags) {
        !           439:                                if (' ' == *word) {
        !           440:                                        encode(p, nbrsp, 1);
        !           441:                                        word++;
        !           442:                                        continue;
        !           443:                                }
        !           444:                                ssz = strcspn(word, "\\ ");
        !           445:                        } else
        !           446:                                ssz = strcspn(word, "\\");
1.45      schwarze  447:                        encode(p, word, ssz);
1.64      schwarze  448:                        word += (int)ssz;
1.20      schwarze  449:                        continue;
1.64      schwarze  450:                }
1.20      schwarze  451:
1.59      schwarze  452:                word++;
                    453:                esc = mandoc_escape(&word, &seq, &sz);
                    454:                if (ESCAPE_ERROR == esc)
                    455:                        break;
                    456:
                    457:                if (TERMENC_ASCII != p->enc)
                    458:                        switch (esc) {
                    459:                        case (ESCAPE_UNICODE):
                    460:                                uc = mchars_num2uc(seq + 1, sz - 1);
                    461:                                if ('\0' == uc)
                    462:                                        break;
                    463:                                encode1(p, uc);
                    464:                                continue;
                    465:                        case (ESCAPE_SPECIAL):
                    466:                                uc = mchars_spec2cp(p->symtab, seq, sz);
                    467:                                if (uc <= 0)
                    468:                                        break;
                    469:                                encode1(p, uc);
                    470:                                continue;
                    471:                        default:
                    472:                                break;
                    473:                        }
1.20      schwarze  474:
1.59      schwarze  475:                switch (esc) {
                    476:                case (ESCAPE_UNICODE):
                    477:                        encode1(p, '?');
1.56      schwarze  478:                        break;
1.59      schwarze  479:                case (ESCAPE_NUMBERED):
                    480:                        c = mchars_num2char(seq, sz);
                    481:                        if ('\0' != c)
                    482:                                encode(p, &c, 1);
1.20      schwarze  483:                        break;
1.59      schwarze  484:                case (ESCAPE_SPECIAL):
                    485:                        cp = mchars_spec2str(p->symtab, seq, sz, &ssz);
                    486:                        if (NULL != cp)
                    487:                                encode(p, cp, ssz);
                    488:                        else if (1 == ssz)
                    489:                                encode(p, seq, sz);
1.20      schwarze  490:                        break;
1.59      schwarze  491:                case (ESCAPE_FONTBOLD):
1.20      schwarze  492:                        term_fontrepl(p, TERMFONT_BOLD);
                    493:                        break;
1.59      schwarze  494:                case (ESCAPE_FONTITALIC):
1.20      schwarze  495:                        term_fontrepl(p, TERMFONT_UNDER);
                    496:                        break;
1.70      schwarze  497:                case (ESCAPE_FONTBI):
                    498:                        term_fontrepl(p, TERMFONT_BI);
                    499:                        break;
1.59      schwarze  500:                case (ESCAPE_FONT):
                    501:                        /* FALLTHROUGH */
                    502:                case (ESCAPE_FONTROMAN):
1.20      schwarze  503:                        term_fontrepl(p, TERMFONT_NONE);
                    504:                        break;
1.59      schwarze  505:                case (ESCAPE_FONTPREV):
1.20      schwarze  506:                        term_fontlast(p);
                    507:                        break;
1.59      schwarze  508:                case (ESCAPE_NOSPACE):
1.64      schwarze  509:                        if (TERMP_SKIPCHAR & p->flags)
                    510:                                p->flags &= ~TERMP_SKIPCHAR;
                    511:                        else if ('\0' == *word)
1.59      schwarze  512:                                p->flags |= TERMP_NOSPACE;
                    513:                        break;
1.64      schwarze  514:                case (ESCAPE_SKIPCHAR):
                    515:                        p->flags |= TERMP_SKIPCHAR;
                    516:                        break;
1.20      schwarze  517:                default:
                    518:                        break;
                    519:                }
                    520:        }
1.75    ! schwarze  521:        p->flags &= ~TERMP_NBRWORD;
1.1       kristaps  522: }
                    523:
                    524: static void
1.71      schwarze  525: adjbuf(struct termp *p, size_t sz)
1.1       kristaps  526: {
                    527:
1.20      schwarze  528:        if (0 == p->maxcols)
                    529:                p->maxcols = 1024;
                    530:        while (sz >= p->maxcols)
                    531:                p->maxcols <<= 2;
                    532:
1.71      schwarze  533:        p->buf = mandoc_realloc(p->buf, sizeof(int) * p->maxcols);
1.1       kristaps  534: }
                    535:
1.4       schwarze  536: static void
1.20      schwarze  537: bufferc(struct termp *p, char c)
                    538: {
                    539:
                    540:        if (p->col + 1 >= p->maxcols)
                    541:                adjbuf(p, p->col + 1);
                    542:
1.59      schwarze  543:        p->buf[p->col++] = c;
1.20      schwarze  544: }
                    545:
1.59      schwarze  546: /*
                    547:  * See encode().
                    548:  * Do this for a single (probably unicode) value.
                    549:  * Does not check for non-decorated glyphs.
                    550:  */
                    551: static void
                    552: encode1(struct termp *p, int c)
                    553: {
                    554:        enum termfont     f;
                    555:
1.64      schwarze  556:        if (TERMP_SKIPCHAR & p->flags) {
                    557:                p->flags &= ~TERMP_SKIPCHAR;
                    558:                return;
                    559:        }
                    560:
1.70      schwarze  561:        if (p->col + 6 >= p->maxcols)
                    562:                adjbuf(p, p->col + 6);
1.59      schwarze  563:
                    564:        f = term_fonttop(p);
                    565:
1.70      schwarze  566:        if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
1.59      schwarze  567:                p->buf[p->col++] = '_';
1.70      schwarze  568:                p->buf[p->col++] = 8;
                    569:        }
                    570:        if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
                    571:                if (ASCII_HYPH == c)
                    572:                        p->buf[p->col++] = '-';
                    573:                else
                    574:                        p->buf[p->col++] = c;
                    575:                p->buf[p->col++] = 8;
                    576:        }
1.59      schwarze  577:        p->buf[p->col++] = c;
                    578: }
1.20      schwarze  579:
                    580: static void
                    581: encode(struct termp *p, const char *word, size_t sz)
1.4       schwarze  582: {
1.71      schwarze  583:        size_t            i;
1.59      schwarze  584:
1.64      schwarze  585:        if (TERMP_SKIPCHAR & p->flags) {
                    586:                p->flags &= ~TERMP_SKIPCHAR;
                    587:                return;
                    588:        }
                    589:
1.20      schwarze  590:        /*
                    591:         * Encode and buffer a string of characters.  If the current
                    592:         * font mode is unset, buffer directly, else encode then buffer
                    593:         * character by character.
                    594:         */
                    595:
1.70      schwarze  596:        if (TERMFONT_NONE == term_fonttop(p)) {
1.71      schwarze  597:                if (p->col + sz >= p->maxcols)
                    598:                        adjbuf(p, p->col + sz);
                    599:                for (i = 0; i < sz; i++)
1.59      schwarze  600:                        p->buf[p->col++] = word[i];
1.20      schwarze  601:                return;
                    602:        }
                    603:
1.46      schwarze  604:        /* Pre-buffer, assuming worst-case. */
                    605:
1.71      schwarze  606:        if (p->col + 1 + (sz * 5) >= p->maxcols)
                    607:                adjbuf(p, p->col + 1 + (sz * 5));
1.46      schwarze  608:
1.71      schwarze  609:        for (i = 0; i < sz; i++) {
1.70      schwarze  610:                if (ASCII_HYPH == word[i] ||
                    611:                    isgraph((unsigned char)word[i]))
                    612:                        encode1(p, word[i]);
1.20      schwarze  613:                else
1.59      schwarze  614:                        p->buf[p->col++] = word[i];
1.4       schwarze  615:        }
                    616: }
1.16      schwarze  617:
                    618: size_t
1.39      schwarze  619: term_len(const struct termp *p, size_t sz)
                    620: {
                    621:
                    622:        return((*p->width)(p, ' ') * sz);
                    623: }
                    624:
1.64      schwarze  625: static size_t
                    626: cond_width(const struct termp *p, int c, int *skip)
                    627: {
                    628:
                    629:        if (*skip) {
                    630:                (*skip) = 0;
                    631:                return(0);
                    632:        } else
                    633:                return((*p->width)(p, c));
                    634: }
1.39      schwarze  635:
                    636: size_t
                    637: term_strlen(const struct termp *p, const char *cp)
                    638: {
1.59      schwarze  639:        size_t           sz, rsz, i;
1.64      schwarze  640:        int              ssz, skip, c;
1.50      schwarze  641:        const char      *seq, *rhs;
1.59      schwarze  642:        enum mandoc_esc  esc;
                    643:        static const char rej[] = { '\\', ASCII_HYPH, ASCII_NBRSP, '\0' };
                    644:
                    645:        /*
                    646:         * Account for escaped sequences within string length
                    647:         * calculations.  This follows the logic in term_word() as we
                    648:         * must calculate the width of produced strings.
                    649:         */
                    650:
                    651:        sz = 0;
1.64      schwarze  652:        skip = 0;
1.59      schwarze  653:        while ('\0' != *cp) {
                    654:                rsz = strcspn(cp, rej);
                    655:                for (i = 0; i < rsz; i++)
1.64      schwarze  656:                        sz += cond_width(p, *cp++, &skip);
1.59      schwarze  657:
                    658:                c = 0;
                    659:                switch (*cp) {
                    660:                case ('\\'):
                    661:                        cp++;
                    662:                        esc = mandoc_escape(&cp, &seq, &ssz);
                    663:                        if (ESCAPE_ERROR == esc)
                    664:                                return(sz);
                    665:
                    666:                        if (TERMENC_ASCII != p->enc)
                    667:                                switch (esc) {
                    668:                                case (ESCAPE_UNICODE):
                    669:                                        c = mchars_num2uc
                    670:                                                (seq + 1, ssz - 1);
                    671:                                        if ('\0' == c)
                    672:                                                break;
1.64      schwarze  673:                                        sz += cond_width(p, c, &skip);
1.59      schwarze  674:                                        continue;
                    675:                                case (ESCAPE_SPECIAL):
                    676:                                        c = mchars_spec2cp
                    677:                                                (p->symtab, seq, ssz);
                    678:                                        if (c <= 0)
                    679:                                                break;
1.64      schwarze  680:                                        sz += cond_width(p, c, &skip);
1.59      schwarze  681:                                        continue;
                    682:                                default:
                    683:                                        break;
                    684:                                }
                    685:
                    686:                        rhs = NULL;
1.50      schwarze  687:
1.59      schwarze  688:                        switch (esc) {
                    689:                        case (ESCAPE_UNICODE):
1.64      schwarze  690:                                sz += cond_width(p, '?', &skip);
1.59      schwarze  691:                                break;
                    692:                        case (ESCAPE_NUMBERED):
                    693:                                c = mchars_num2char(seq, ssz);
                    694:                                if ('\0' != c)
1.64      schwarze  695:                                        sz += cond_width(p, c, &skip);
1.50      schwarze  696:                                break;
1.59      schwarze  697:                        case (ESCAPE_SPECIAL):
                    698:                                rhs = mchars_spec2str
1.50      schwarze  699:                                        (p->symtab, seq, ssz, &rsz);
                    700:
1.59      schwarze  701:                                if (ssz != 1 || rhs)
1.50      schwarze  702:                                        break;
                    703:
                    704:                                rhs = seq;
                    705:                                rsz = ssz;
                    706:                                break;
1.64      schwarze  707:                        case (ESCAPE_SKIPCHAR):
                    708:                                skip = 1;
                    709:                                break;
1.50      schwarze  710:                        default:
                    711:                                break;
                    712:                        }
1.39      schwarze  713:
1.59      schwarze  714:                        if (NULL == rhs)
                    715:                                break;
                    716:
1.64      schwarze  717:                        if (skip) {
                    718:                                skip = 0;
                    719:                                break;
                    720:                        }
                    721:
1.59      schwarze  722:                        for (i = 0; i < rsz; i++)
                    723:                                sz += (*p->width)(p, *rhs++);
                    724:                        break;
                    725:                case (ASCII_NBRSP):
1.64      schwarze  726:                        sz += cond_width(p, ' ', &skip);
1.55      schwarze  727:                        cp++;
1.59      schwarze  728:                        break;
                    729:                case (ASCII_HYPH):
1.64      schwarze  730:                        sz += cond_width(p, '-', &skip);
1.55      schwarze  731:                        cp++;
1.59      schwarze  732:                        break;
                    733:                default:
                    734:                        break;
                    735:                }
                    736:        }
1.39      schwarze  737:
                    738:        return(sz);
                    739: }
                    740:
1.44      schwarze  741: /* ARGSUSED */
1.39      schwarze  742: size_t
                    743: term_vspan(const struct termp *p, const struct roffsu *su)
1.16      schwarze  744: {
                    745:        double           r;
                    746:
                    747:        switch (su->unit) {
                    748:        case (SCALE_CM):
                    749:                r = su->scale * 2;
                    750:                break;
                    751:        case (SCALE_IN):
                    752:                r = su->scale * 6;
                    753:                break;
                    754:        case (SCALE_PC):
                    755:                r = su->scale;
                    756:                break;
                    757:        case (SCALE_PT):
                    758:                r = su->scale / 8;
                    759:                break;
                    760:        case (SCALE_MM):
                    761:                r = su->scale / 1000;
                    762:                break;
                    763:        case (SCALE_VS):
                    764:                r = su->scale;
                    765:                break;
                    766:        default:
                    767:                r = su->scale - 1;
                    768:                break;
                    769:        }
                    770:
                    771:        if (r < 0.0)
                    772:                r = 0.0;
                    773:        return(/* LINTED */(size_t)
                    774:                        r);
                    775: }
                    776:
                    777: size_t
1.39      schwarze  778: term_hspan(const struct termp *p, const struct roffsu *su)
1.16      schwarze  779: {
1.44      schwarze  780:        double           v;
1.16      schwarze  781:
1.44      schwarze  782:        v = ((*p->hspan)(p, su));
                    783:        if (v < 0.0)
                    784:                v = 0.0;
                    785:        return((size_t) /* LINTED */
                    786:                        v);
1.16      schwarze  787: }