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

Annotation of mandoc/term.c, Revision 1.170

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

CVSweb