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

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