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

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