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

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