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

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