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

Annotation of mandoc/term.c, Revision 1.181

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

CVSweb