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

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