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

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