[BACK]Return to term.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/term.c, Revision 1.159

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

CVSweb