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

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