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

1.41    ! schwarze    1: /*     $Id: term.c,v 1.40 2010/06/27 01:24:02 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 */
                    132:        int              j;     /* temporary loop index */
1.22      schwarze  133:        int              jhy;   /* last hyphen before line overflow */
1.19      schwarze  134:        size_t           maxvis, mmax;
1.1       kristaps  135:
                    136:        /*
                    137:         * First, establish the maximum columns of "visible" content.
                    138:         * This is usually the difference between the right-margin and
                    139:         * an indentation, but can be, for tagged lists or columns, a
1.19      schwarze  140:         * small set of values.
1.1       kristaps  141:         */
                    142:
                    143:        assert(p->offset < p->rmargin);
1.9       schwarze  144:
1.26      schwarze  145:        maxvis = (int)(p->rmargin - p->offset) - p->overstep < 0 ?
1.19      schwarze  146:                /* LINTED */
1.26      schwarze  147:                0 : p->rmargin - p->offset - p->overstep;
                    148:        mmax = (int)(p->maxrmargin - p->offset) - p->overstep < 0 ?
1.19      schwarze  149:                /* LINTED */
1.26      schwarze  150:                0 : p->maxrmargin - p->offset - p->overstep;
1.9       schwarze  151:
1.1       kristaps  152:        bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
1.19      schwarze  153:
1.33      schwarze  154:        /*
                    155:         * Indent the first line of a paragraph.
                    156:         */
                    157:        vbl = p->flags & TERMP_NOLPAD ? 0 : p->offset;
                    158:
1.19      schwarze  159:        /*
                    160:         * FIXME: if bp is zero, we still output the first word before
                    161:         * breaking the line.
                    162:         */
                    163:
1.33      schwarze  164:        vis = vend = i = 0;
1.22      schwarze  165:        while (i < (int)p->col) {
                    166:
                    167:                /*
1.30      schwarze  168:                 * Handle literal tab characters.
                    169:                 */
                    170:                for (j = i; j < (int)p->col; j++) {
                    171:                        if ('\t' != p->buf[j])
                    172:                                break;
1.33      schwarze  173:                        vend = (vis/p->tabwidth+1)*p->tabwidth;
1.30      schwarze  174:                        vbl += vend - vis;
                    175:                        vis = vend;
                    176:                }
1.22      schwarze  177:
1.1       kristaps  178:                /*
                    179:                 * Count up visible word characters.  Control sequences
                    180:                 * (starting with the CSI) aren't counted.  A space
                    181:                 * generates a non-printing word, which is valid (the
                    182:                 * space is printed according to regular spacing rules).
                    183:                 */
                    184:
                    185:                /* LINTED */
1.30      schwarze  186:                for (jhy = 0; j < (int)p->col; j++) {
                    187:                        if ((j && ' ' == p->buf[j]) || '\t' == p->buf[j])
1.1       kristaps  188:                                break;
1.35      schwarze  189:                        if (8 != p->buf[j]) {
1.22      schwarze  190:                                if (vend > vis && vend < bp &&
1.35      schwarze  191:                                    ASCII_HYPH == p->buf[j])
1.22      schwarze  192:                                        jhy = j;
                    193:                                vend++;
1.35      schwarze  194:                        } else
                    195:                                vend--;
1.1       kristaps  196:                }
                    197:
                    198:                /*
1.5       schwarze  199:                 * Find out whether we would exceed the right margin.
1.33      schwarze  200:                 * If so, break to the next line.
1.5       schwarze  201:                 */
1.33      schwarze  202:                if (vend > bp && 0 == jhy && vis > 0) {
1.22      schwarze  203:                        vend -= vis;
1.37      schwarze  204:                        (*p->endline)(p);
1.5       schwarze  205:                        if (TERMP_NOBREAK & p->flags) {
1.29      schwarze  206:                                p->viscol = p->rmargin;
1.37      schwarze  207:                                (*p->advance)(p, p->rmargin);
1.22      schwarze  208:                                vend += p->rmargin - p->offset;
1.5       schwarze  209:                        } else {
1.33      schwarze  210:                                p->viscol = 0;
                    211:                                vbl = p->offset;
1.5       schwarze  212:                        }
1.33      schwarze  213:
1.26      schwarze  214:                        /* Remove the p->overstep width. */
1.33      schwarze  215:
1.18      schwarze  216:                        bp += (int)/* LINTED */
1.26      schwarze  217:                                p->overstep;
                    218:                        p->overstep = 0;
1.1       kristaps  219:                }
                    220:
1.3       schwarze  221:                /*
1.30      schwarze  222:                 * Skip leading tabs, they were handled above.
                    223:                 */
                    224:                while (i < (int)p->col && '\t' == p->buf[i])
                    225:                        i++;
                    226:
1.33      schwarze  227:                /* Write out the [remaining] word. */
1.1       kristaps  228:                for ( ; i < (int)p->col; i++) {
1.25      schwarze  229:                        if (vend > bp && jhy > 0 && i > jhy)
1.30      schwarze  230:                                break;
                    231:                        if ('\t' == p->buf[i])
1.1       kristaps  232:                                break;
1.22      schwarze  233:                        if (' ' == p->buf[i]) {
1.33      schwarze  234:                                while (' ' == p->buf[i]) {
                    235:                                        vbl++;
                    236:                                        i++;
                    237:                                }
1.22      schwarze  238:                                break;
                    239:                        }
1.33      schwarze  240:                        if (ASCII_NBRSP == p->buf[i]) {
                    241:                                vbl++;
                    242:                                continue;
                    243:                        }
                    244:
                    245:                        /*
                    246:                         * Now we definitely know there will be
                    247:                         * printable characters to output,
                    248:                         * so write preceding white space now.
                    249:                         */
                    250:                        if (vbl) {
1.37      schwarze  251:                                (*p->advance)(p, vbl);
1.33      schwarze  252:                                p->viscol += vbl;
                    253:                                vbl = 0;
                    254:                        }
1.35      schwarze  255:
                    256:                        if (ASCII_HYPH == p->buf[i])
1.37      schwarze  257:                                (*p->letter)(p, '-');
1.35      schwarze  258:                        else
1.37      schwarze  259:                                (*p->letter)(p, p->buf[i]);
1.35      schwarze  260:
1.33      schwarze  261:                        p->viscol += 1;
1.1       kristaps  262:                }
1.33      schwarze  263:                vend += vbl;
1.22      schwarze  264:                vis = vend;
1.1       kristaps  265:        }
1.18      schwarze  266:
1.9       schwarze  267:        p->col = 0;
1.26      schwarze  268:        p->overstep = 0;
1.1       kristaps  269:
1.9       schwarze  270:        if ( ! (TERMP_NOBREAK & p->flags)) {
1.29      schwarze  271:                p->viscol = 0;
1.37      schwarze  272:                (*p->endline)(p);
1.1       kristaps  273:                return;
                    274:        }
                    275:
1.9       schwarze  276:        if (TERMP_HANG & p->flags) {
                    277:                /* We need one blank after the tag. */
1.26      schwarze  278:                p->overstep = /* LINTED */
1.9       schwarze  279:                        vis - maxvis + 1;
                    280:
                    281:                /*
                    282:                 * Behave exactly the same way as groff:
                    283:                 * If we have overstepped the margin, temporarily move
                    284:                 * it to the right and flag the rest of the line to be
                    285:                 * shorter.
                    286:                 * If we landed right at the margin, be happy.
                    287:                 * If we are one step before the margin, temporarily
                    288:                 * move it one step LEFT and flag the rest of the line
                    289:                 * to be longer.
                    290:                 */
1.26      schwarze  291:                if (p->overstep >= -1) {
                    292:                        assert((int)maxvis + p->overstep >= 0);
1.9       schwarze  293:                        /* LINTED */
1.26      schwarze  294:                        maxvis += p->overstep;
1.9       schwarze  295:                } else
1.26      schwarze  296:                        p->overstep = 0;
1.9       schwarze  297:
                    298:        } else if (TERMP_DANGLE & p->flags)
                    299:                return;
1.1       kristaps  300:
1.9       schwarze  301:        /* Right-pad. */
                    302:        if (maxvis > vis + /* LINTED */
1.29      schwarze  303:                        ((TERMP_TWOSPACE & p->flags) ? 1 : 0)) {
                    304:                p->viscol += maxvis - vis;
1.37      schwarze  305:                (*p->advance)(p, maxvis - vis);
                    306:                vis += (maxvis - vis);
1.29      schwarze  307:        } else {        /* ...or newline break. */
1.37      schwarze  308:                (*p->endline)(p);
1.29      schwarze  309:                p->viscol = p->rmargin;
1.37      schwarze  310:                (*p->advance)(p, p->rmargin);
1.9       schwarze  311:        }
1.1       kristaps  312: }
                    313:
                    314:
                    315: /*
                    316:  * A newline only breaks an existing line; it won't assert vertical
                    317:  * space.  All data in the output buffer is flushed prior to the newline
                    318:  * assertion.
                    319:  */
                    320: void
                    321: term_newln(struct termp *p)
                    322: {
                    323:
                    324:        p->flags |= TERMP_NOSPACE;
1.29      schwarze  325:        if (0 == p->col && 0 == p->viscol) {
1.1       kristaps  326:                p->flags &= ~TERMP_NOLPAD;
                    327:                return;
                    328:        }
                    329:        term_flushln(p);
                    330:        p->flags &= ~TERMP_NOLPAD;
                    331: }
                    332:
                    333:
                    334: /*
                    335:  * Asserts a vertical space (a full, empty line-break between lines).
                    336:  * Note that if used twice, this will cause two blank spaces and so on.
                    337:  * All data in the output buffer is flushed prior to the newline
                    338:  * assertion.
                    339:  */
                    340: void
                    341: term_vspace(struct termp *p)
                    342: {
                    343:
                    344:        term_newln(p);
1.29      schwarze  345:        p->viscol = 0;
1.37      schwarze  346:        (*p->endline)(p);
1.1       kristaps  347: }
                    348:
                    349:
                    350: static void
1.20      schwarze  351: spec(struct termp *p, const char *word, size_t len)
1.1       kristaps  352: {
                    353:        const char      *rhs;
                    354:        size_t           sz;
                    355:
1.15      schwarze  356:        rhs = chars_a2ascii(p->symtab, word, len, &sz);
1.20      schwarze  357:        if (rhs)
                    358:                encode(p, rhs, sz);
1.11      schwarze  359: }
                    360:
                    361:
                    362: static void
1.20      schwarze  363: res(struct termp *p, const char *word, size_t len)
1.11      schwarze  364: {
                    365:        const char      *rhs;
                    366:        size_t           sz;
                    367:
1.15      schwarze  368:        rhs = chars_a2res(p->symtab, word, len, &sz);
1.20      schwarze  369:        if (rhs)
                    370:                encode(p, rhs, sz);
                    371: }
                    372:
                    373:
                    374: void
                    375: term_fontlast(struct termp *p)
                    376: {
                    377:        enum termfont    f;
1.11      schwarze  378:
1.20      schwarze  379:        f = p->fontl;
                    380:        p->fontl = p->fontq[p->fonti];
                    381:        p->fontq[p->fonti] = f;
                    382: }
                    383:
                    384:
                    385: void
                    386: term_fontrepl(struct termp *p, enum termfont f)
                    387: {
                    388:
                    389:        p->fontl = p->fontq[p->fonti];
                    390:        p->fontq[p->fonti] = f;
1.1       kristaps  391: }
                    392:
                    393:
1.20      schwarze  394: void
                    395: term_fontpush(struct termp *p, enum termfont f)
1.1       kristaps  396: {
1.7       schwarze  397:
1.20      schwarze  398:        assert(p->fonti + 1 < 10);
                    399:        p->fontl = p->fontq[p->fonti];
                    400:        p->fontq[++p->fonti] = f;
                    401: }
1.1       kristaps  402:
                    403:
1.20      schwarze  404: const void *
                    405: term_fontq(struct termp *p)
                    406: {
1.1       kristaps  407:
1.20      schwarze  408:        return(&p->fontq[p->fonti]);
                    409: }
1.1       kristaps  410:
                    411:
1.20      schwarze  412: enum termfont
                    413: term_fonttop(struct termp *p)
                    414: {
1.1       kristaps  415:
1.20      schwarze  416:        return(p->fontq[p->fonti]);
                    417: }
1.7       schwarze  418:
                    419:
1.20      schwarze  420: void
                    421: term_fontpopq(struct termp *p, const void *key)
                    422: {
1.1       kristaps  423:
1.20      schwarze  424:        while (p->fonti >= 0 && key != &p->fontq[p->fonti])
                    425:                p->fonti--;
                    426:        assert(p->fonti >= 0);
                    427: }
1.1       kristaps  428:
                    429:
1.20      schwarze  430: void
                    431: term_fontpop(struct termp *p)
                    432: {
1.1       kristaps  433:
1.20      schwarze  434:        assert(p->fonti);
                    435:        p->fonti--;
1.1       kristaps  436: }
                    437:
                    438:
                    439: /*
                    440:  * Handle pwords, partial words, which may be either a single word or a
                    441:  * phrase that cannot be broken down (such as a literal string).  This
                    442:  * handles word styling.
                    443:  */
1.7       schwarze  444: void
                    445: term_word(struct termp *p, const char *word)
1.1       kristaps  446: {
1.20      schwarze  447:        const char      *sv, *seq;
                    448:        int              sz;
                    449:        size_t           ssz;
                    450:        enum roffdeco    deco;
1.1       kristaps  451:
1.14      schwarze  452:        sv = word;
                    453:
1.20      schwarze  454:        if (word[0] && '\0' == word[1])
1.14      schwarze  455:                switch (word[0]) {
                    456:                case('.'):
                    457:                        /* FALLTHROUGH */
                    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:                        if ( ! (TERMP_IGNDELIM & p->flags))
                    472:                                p->flags |= TERMP_NOSPACE;
                    473:                        break;
                    474:                default:
                    475:                        break;
                    476:                }
1.1       kristaps  477:
1.31      schwarze  478:        if ( ! (TERMP_NOSPACE & p->flags)) {
1.40      schwarze  479:                if ( ! (TERMP_KEEP & p->flags)) {
                    480:                        if (TERMP_PREKEEP & p->flags)
                    481:                                p->flags |= TERMP_KEEP;
1.31      schwarze  482:                        bufferc(p, ' ');
1.40      schwarze  483:                        if (TERMP_SENTENCE & p->flags)
                    484:                                bufferc(p, ' ');
                    485:                } else
                    486:                        bufferc(p, ASCII_NBRSP);
1.31      schwarze  487:        }
1.1       kristaps  488:
                    489:        if ( ! (p->flags & TERMP_NONOSPACE))
                    490:                p->flags &= ~TERMP_NOSPACE;
                    491:
1.31      schwarze  492:        p->flags &= ~TERMP_SENTENCE;
                    493:
1.20      schwarze  494:        /* FIXME: use strcspn. */
                    495:
                    496:        while (*word) {
                    497:                if ('\\' != *word) {
                    498:                        encode(p, word, 1);
                    499:                        word++;
                    500:                        continue;
                    501:                }
                    502:
                    503:                seq = ++word;
                    504:                sz = a2roffdeco(&deco, &seq, &ssz);
                    505:
                    506:                switch (deco) {
                    507:                case (DECO_RESERVED):
                    508:                        res(p, seq, ssz);
                    509:                        break;
                    510:                case (DECO_SPECIAL):
                    511:                        spec(p, seq, ssz);
                    512:                        break;
                    513:                case (DECO_BOLD):
                    514:                        term_fontrepl(p, TERMFONT_BOLD);
                    515:                        break;
                    516:                case (DECO_ITALIC):
                    517:                        term_fontrepl(p, TERMFONT_UNDER);
                    518:                        break;
                    519:                case (DECO_ROMAN):
                    520:                        term_fontrepl(p, TERMFONT_NONE);
                    521:                        break;
                    522:                case (DECO_PREVIOUS):
                    523:                        term_fontlast(p);
                    524:                        break;
                    525:                default:
                    526:                        break;
                    527:                }
                    528:
                    529:                word += sz;
                    530:                if (DECO_NOSPACE == deco && '\0' == *word)
                    531:                        p->flags |= TERMP_NOSPACE;
                    532:        }
1.1       kristaps  533:
1.31      schwarze  534:        /*
                    535:         * Note that we don't process the pipe: the parser sees it as
                    536:         * punctuation, but we don't in terms of typography.
                    537:         */
1.14      schwarze  538:        if (sv[0] && 0 == sv[1])
                    539:                switch (sv[0]) {
                    540:                case('('):
                    541:                        /* FALLTHROUGH */
                    542:                case('['):
                    543:                        p->flags |= TERMP_NOSPACE;
                    544:                        break;
                    545:                default:
                    546:                        break;
                    547:                }
1.1       kristaps  548: }
                    549:
                    550:
                    551: static void
1.20      schwarze  552: adjbuf(struct termp *p, size_t sz)
1.1       kristaps  553: {
                    554:
1.20      schwarze  555:        if (0 == p->maxcols)
                    556:                p->maxcols = 1024;
                    557:        while (sz >= p->maxcols)
                    558:                p->maxcols <<= 2;
                    559:
                    560:        p->buf = realloc(p->buf, p->maxcols);
                    561:        if (NULL == p->buf) {
                    562:                perror(NULL);
                    563:                exit(EXIT_FAILURE);
1.1       kristaps  564:        }
                    565: }
                    566:
1.4       schwarze  567:
                    568: static void
1.20      schwarze  569: buffera(struct termp *p, const char *word, size_t sz)
                    570: {
                    571:
                    572:        if (p->col + sz >= p->maxcols)
                    573:                adjbuf(p, p->col + sz);
                    574:
                    575:        memcpy(&p->buf[(int)p->col], word, sz);
                    576:        p->col += sz;
                    577: }
                    578:
                    579:
                    580: static void
                    581: bufferc(struct termp *p, char c)
                    582: {
                    583:
                    584:        if (p->col + 1 >= p->maxcols)
                    585:                adjbuf(p, p->col + 1);
                    586:
                    587:        p->buf[(int)p->col++] = c;
                    588: }
                    589:
                    590:
                    591: static void
                    592: encode(struct termp *p, const char *word, size_t sz)
1.4       schwarze  593: {
1.20      schwarze  594:        enum termfont     f;
                    595:        int               i;
                    596:
                    597:        /*
                    598:         * Encode and buffer a string of characters.  If the current
                    599:         * font mode is unset, buffer directly, else encode then buffer
                    600:         * character by character.
                    601:         */
                    602:
1.38      schwarze  603:        if (TERMFONT_NONE == (f = term_fonttop(p))) {
1.20      schwarze  604:                buffera(p, word, sz);
                    605:                return;
                    606:        }
                    607:
                    608:        for (i = 0; i < (int)sz; i++) {
                    609:                if ( ! isgraph((u_char)word[i])) {
                    610:                        bufferc(p, word[i]);
                    611:                        continue;
1.4       schwarze  612:                }
1.20      schwarze  613:
                    614:                if (TERMFONT_UNDER == f)
                    615:                        bufferc(p, '_');
                    616:                else
                    617:                        bufferc(p, word[i]);
                    618:
                    619:                bufferc(p, 8);
                    620:                bufferc(p, 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: {
                    636:        size_t           sz;
                    637:
                    638:        for (sz = 0; *cp; cp++)
                    639:                sz += (*p->width)(p, *cp);
                    640:
                    641:        return(sz);
                    642: }
                    643:
                    644:
                    645: size_t
                    646: term_vspan(const struct termp *p, const struct roffsu *su)
1.16      schwarze  647: {
                    648:        double           r;
                    649:
                    650:        switch (su->unit) {
                    651:        case (SCALE_CM):
                    652:                r = su->scale * 2;
                    653:                break;
                    654:        case (SCALE_IN):
                    655:                r = su->scale * 6;
                    656:                break;
                    657:        case (SCALE_PC):
                    658:                r = su->scale;
                    659:                break;
                    660:        case (SCALE_PT):
                    661:                r = su->scale / 8;
                    662:                break;
                    663:        case (SCALE_MM):
                    664:                r = su->scale / 1000;
                    665:                break;
                    666:        case (SCALE_VS):
                    667:                r = su->scale;
                    668:                break;
                    669:        default:
                    670:                r = su->scale - 1;
                    671:                break;
                    672:        }
                    673:
                    674:        if (r < 0.0)
                    675:                r = 0.0;
                    676:        return(/* LINTED */(size_t)
                    677:                        r);
                    678: }
                    679:
                    680:
                    681: size_t
1.39      schwarze  682: term_hspan(const struct termp *p, const struct roffsu *su)
1.16      schwarze  683: {
                    684:        double           r;
                    685:
                    686:        /* XXX: CM, IN, and PT are approximations. */
                    687:
                    688:        switch (su->unit) {
                    689:        case (SCALE_CM):
                    690:                r = 4 * su->scale;
                    691:                break;
                    692:        case (SCALE_IN):
                    693:                /* XXX: this is an approximation. */
                    694:                r = 10 * su->scale;
                    695:                break;
                    696:        case (SCALE_PC):
                    697:                r = (10 * su->scale) / 6;
                    698:                break;
                    699:        case (SCALE_PT):
                    700:                r = (10 * su->scale) / 72;
                    701:                break;
                    702:        case (SCALE_MM):
                    703:                r = su->scale / 1000; /* FIXME: double-check. */
                    704:                break;
                    705:        case (SCALE_VS):
                    706:                r = su->scale * 2 - 1; /* FIXME: double-check. */
                    707:                break;
                    708:        default:
                    709:                r = su->scale;
                    710:                break;
                    711:        }
                    712:
                    713:        if (r < 0.0)
                    714:                r = 0.0;
                    715:        return((size_t)/* LINTED */
                    716:                        r);
                    717: }
                    718:
                    719: