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

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