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

Annotation of mandoc/term.c, Revision 1.140

1.140   ! kristaps    1: /*     $Id: term.c,v 1.139 2010/05/24 21:51:20 schwarze Exp $ */
1.1       kristaps    2: /*
1.75      kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.74      kristaps    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.74      kristaps    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   16:  */
1.128     kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.126     kristaps   21: #include <sys/types.h>
                     22:
1.1       kristaps   23: #include <assert.h>
1.122     kristaps   24: #include <ctype.h>
1.22      kristaps   25: #include <stdio.h>
1.1       kristaps   26: #include <stdlib.h>
                     27: #include <string.h>
1.113     kristaps   28: #include <time.h>
1.1       kristaps   29:
1.137     kristaps   30: #include "mandoc.h"
1.101     kristaps   31: #include "chars.h"
1.107     kristaps   32: #include "out.h"
1.71      kristaps   33: #include "term.h"
                     34: #include "man.h"
                     35: #include "mdoc.h"
1.105     kristaps   36: #include "main.h"
1.1       kristaps   37:
1.134     joerg      38: static struct termp     *term_alloc(enum termenc, size_t);
1.71      kristaps   39: static void              term_free(struct termp *);
1.125     kristaps   40: static void              spec(struct termp *, const char *, size_t);
                     41: static void              res(struct termp *, const char *, size_t);
                     42: static void              buffera(struct termp *, const char *, size_t);
                     43: static void              bufferc(struct termp *, char);
                     44: static void              adjbuf(struct termp *p, size_t);
                     45: static void              encode(struct termp *, const char *, size_t);
1.1       kristaps   46:
                     47:
1.71      kristaps   48: void *
1.134     joerg      49: ascii_alloc(size_t width)
1.10      kristaps   50: {
1.1       kristaps   51:
1.134     joerg      52:        return(term_alloc(TERMENC_ASCII, width));
1.1       kristaps   53: }
                     54:
                     55:
1.99      kristaps   56: void
1.71      kristaps   57: terminal_free(void *arg)
1.11      kristaps   58: {
                     59:
1.71      kristaps   60:        term_free((struct termp *)arg);
1.11      kristaps   61: }
                     62:
                     63:
1.71      kristaps   64: static void
                     65: term_free(struct termp *p)
1.14      kristaps   66: {
                     67:
1.71      kristaps   68:        if (p->buf)
                     69:                free(p->buf);
1.102     kristaps   70:        if (p->symtab)
1.101     kristaps   71:                chars_free(p->symtab);
1.14      kristaps   72:
1.71      kristaps   73:        free(p);
1.14      kristaps   74: }
                     75:
                     76:
1.71      kristaps   77: static struct termp *
1.134     joerg      78: term_alloc(enum termenc enc, size_t width)
1.14      kristaps   79: {
1.71      kristaps   80:        struct termp *p;
1.14      kristaps   81:
1.117     kristaps   82:        p = calloc(1, sizeof(struct termp));
                     83:        if (NULL == p) {
1.120     kristaps   84:                perror(NULL);
1.117     kristaps   85:                exit(EXIT_FAILURE);
                     86:        }
1.138     schwarze   87:        p->tabwidth = 5;
1.71      kristaps   88:        p->enc = enc;
1.134     joerg      89:        /* Enforce some lower boundary. */
                     90:        if (width < 60)
                     91:                width = 60;
                     92:        p->defrmargin = width - 2;
1.71      kristaps   93:        return(p);
1.14      kristaps   94: }
                     95:
                     96:
1.71      kristaps   97: /*
                     98:  * Flush a line of text.  A "line" is loosely defined as being something
                     99:  * that should be followed by a newline, regardless of whether it's
                    100:  * broken apart by newlines getting there.  A line can also be a
1.130     kristaps  101:  * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
                    102:  * not have a trailing newline.
1.71      kristaps  103:  *
1.130     kristaps  104:  * The following flags may be specified:
1.71      kristaps  105:  *
                    106:  *  - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
                    107:  *    offset value.  This is useful when doing columnar lists where the
                    108:  *    prior column has right-padded.
                    109:  *
                    110:  *  - TERMP_NOBREAK: this is the most important and is used when making
                    111:  *    columns.  In short: don't print a newline and instead pad to the
                    112:  *    right margin.  Used in conjunction with TERMP_NOLPAD.
                    113:  *
1.91      kristaps  114:  *  - TERMP_TWOSPACE: when padding, make sure there are at least two
                    115:  *    space characters of padding.  Otherwise, rather break the line.
                    116:  *
1.84      kristaps  117:  *  - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
                    118:  *    the line is overrun, and don't pad-right if it's underrun.
                    119:  *
                    120:  *  - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
                    121:  *    overruning, instead save the position and continue at that point
                    122:  *    when the next invocation.
1.71      kristaps  123:  *
                    124:  *  In-line line breaking:
                    125:  *
                    126:  *  If TERMP_NOBREAK is specified and the line overruns the right
                    127:  *  margin, it will break and pad-right to the right margin after
                    128:  *  writing.  If maxrmargin is violated, it will break and continue
1.114     kristaps  129:  *  writing from the right-margin, which will lead to the above scenario
                    130:  *  upon exit.  Otherwise, the line will break at the right margin.
1.71      kristaps  131:  */
                    132: void
                    133: term_flushln(struct termp *p)
1.53      kristaps  134: {
1.114     kristaps  135:        int              i;     /* current input position in p->buf */
                    136:        size_t           vis;   /* current visual position on output */
                    137:        size_t           vbl;   /* number of blanks to prepend to output */
1.136     schwarze  138:        size_t           vend;  /* end of word visual position on output */
1.114     kristaps  139:        size_t           bp;    /* visual right border position */
                    140:        int              j;     /* temporary loop index */
1.140   ! kristaps  141:        int              jhy;   /* last hyphen before line overflow */
1.114     kristaps  142:        size_t           maxvis, mmax;
1.53      kristaps  143:
1.71      kristaps  144:        /*
                    145:         * First, establish the maximum columns of "visible" content.
                    146:         * This is usually the difference between the right-margin and
                    147:         * an indentation, but can be, for tagged lists or columns, a
1.115     kristaps  148:         * small set of values.
1.71      kristaps  149:         */
1.53      kristaps  150:
1.71      kristaps  151:        assert(p->offset < p->rmargin);
1.92      kristaps  152:
1.129     kristaps  153:        maxvis = (int)(p->rmargin - p->offset) - p->overstep < 0 ?
1.119     kristaps  154:                /* LINTED */
1.129     kristaps  155:                0 : p->rmargin - p->offset - p->overstep;
                    156:        mmax = (int)(p->maxrmargin - p->offset) - p->overstep < 0 ?
1.119     kristaps  157:                /* LINTED */
1.129     kristaps  158:                0 : p->maxrmargin - p->offset - p->overstep;
1.92      kristaps  159:
1.71      kristaps  160:        bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
1.115     kristaps  161:
1.136     schwarze  162:        /*
                    163:         * Indent the first line of a paragraph.
                    164:         */
                    165:        vbl = p->flags & TERMP_NOLPAD ? 0 : p->offset;
                    166:
1.115     kristaps  167:        /*
                    168:         * FIXME: if bp is zero, we still output the first word before
                    169:         * breaking the line.
                    170:         */
                    171:
1.136     schwarze  172:        vis = vend = i = 0;
                    173:        while (i < (int)p->col) {
1.71      kristaps  174:
                    175:                /*
1.138     schwarze  176:                 * Handle literal tab characters.
                    177:                 */
                    178:                for (j = i; j < (int)p->col; j++) {
                    179:                        if ('\t' != p->buf[j])
                    180:                                break;
                    181:                        vend = (vis/p->tabwidth+1)*p->tabwidth;
                    182:                        vbl += vend - vis;
                    183:                        vis = vend;
                    184:                }
                    185:
                    186:                /*
1.71      kristaps  187:                 * Count up visible word characters.  Control sequences
                    188:                 * (starting with the CSI) aren't counted.  A space
                    189:                 * generates a non-printing word, which is valid (the
                    190:                 * space is printed according to regular spacing rules).
                    191:                 */
                    192:
                    193:                /* LINTED */
1.140   ! kristaps  194:                for (jhy = 0; j < (int)p->col; j++) {
1.138     schwarze  195:                        if ((j && ' ' == p->buf[j]) || '\t' == p->buf[j])
1.71      kristaps  196:                                break;
1.140   ! kristaps  197:                        if (8 != p->buf[j]) {
        !           198:                                if (vend > vis && vend < bp &&
        !           199:                                    ASCII_HYPH == p->buf[j])
        !           200:                                        jhy = j;
        !           201:                                vend++;
        !           202:                        } else
1.136     schwarze  203:                                vend--;
1.71      kristaps  204:                }
1.53      kristaps  205:
1.71      kristaps  206:                /*
1.81      kristaps  207:                 * Find out whether we would exceed the right margin.
1.136     schwarze  208:                 * If so, break to the next line.
1.81      kristaps  209:                 */
1.140   ! kristaps  210:                if (vend > bp && 0 == jhy && vis > 0) {
1.136     schwarze  211:                        vend -= vis;
1.81      kristaps  212:                        putchar('\n');
                    213:                        if (TERMP_NOBREAK & p->flags) {
1.139     schwarze  214:                                p->viscol = p->rmargin;
1.81      kristaps  215:                                for (j = 0; j < (int)p->rmargin; j++)
                    216:                                        putchar(' ');
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.53      kristaps  229:
1.138     schwarze  230:                /*
                    231:                 * Skip leading tabs, they were handled above.
                    232:                 */
                    233:                while (i < (int)p->col && '\t' == p->buf[i])
                    234:                        i++;
                    235:
1.130     kristaps  236:                /* Write out the [remaining] word. */
1.136     schwarze  237:                for ( ; i < (int)p->col; i++) {
1.140   ! kristaps  238:                        if (vend > bp && jhy > 0 && i > jhy)
        !           239:                                break;
1.138     schwarze  240:                        if ('\t' == p->buf[i])
                    241:                                break;
1.136     schwarze  242:                        if (' ' == p->buf[i]) {
                    243:                                while (' ' == p->buf[i]) {
                    244:                                        vbl++;
                    245:                                        i++;
                    246:                                }
1.71      kristaps  247:                                break;
1.136     schwarze  248:                        }
                    249:                        if (ASCII_NBRSP == p->buf[i]) {
                    250:                                vbl++;
                    251:                                continue;
                    252:                        }
1.130     kristaps  253:
1.136     schwarze  254:                        /*
                    255:                         * Now we definitely know there will be
                    256:                         * printable characters to output,
                    257:                         * so write preceding white space now.
                    258:                         */
                    259:                        if (vbl) {
                    260:                                for (j = 0; j < (int)vbl; j++)
                    261:                                        putchar(' ');
1.139     schwarze  262:                                p->viscol += vbl;
1.136     schwarze  263:                                vbl = 0;
                    264:                        }
1.140   ! kristaps  265:
        !           266:                        if (ASCII_HYPH == p->buf[i])
        !           267:                                putchar('-');
        !           268:                        else
        !           269:                                putchar(p->buf[i]);
        !           270:
1.139     schwarze  271:                        p->viscol += 1;
1.136     schwarze  272:                }
                    273:                vend += vbl;
                    274:                vis = vend;
1.71      kristaps  275:        }
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.91      kristaps  282:                putchar('\n');
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.92      kristaps  289:                        vis - maxvis + 1;
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.139     schwarze  313:                        ((TERMP_TWOSPACE & p->flags) ? 1 : 0)) {
                    314:                p->viscol += maxvis - vis;
1.91      kristaps  315:                for ( ; vis < maxvis; vis++)
                    316:                        putchar(' ');
1.139     schwarze  317:        } else {        /* ...or newline break. */
1.71      kristaps  318:                putchar('\n');
1.139     schwarze  319:                p->viscol = p->rmargin;
1.91      kristaps  320:                for (i = 0; i < (int)p->rmargin; i++)
                    321:                        putchar(' ');
                    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.71      kristaps  357:        putchar('\n');
1.16      kristaps  358: }
                    359:
                    360:
1.71      kristaps  361: static void
1.125     kristaps  362: spec(struct termp *p, 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.101     kristaps  367:        rhs = chars_a2ascii(p->symtab, word, len, &sz);
1.125     kristaps  368:        if (rhs)
                    369:                encode(p, rhs, sz);
1.94      kristaps  370: }
                    371:
                    372:
                    373: static void
1.125     kristaps  374: res(struct termp *p, const char *word, size_t len)
1.94      kristaps  375: {
                    376:        const char      *rhs;
                    377:        size_t           sz;
                    378:
1.101     kristaps  379:        rhs = chars_a2res(p->symtab, word, len, &sz);
1.125     kristaps  380:        if (rhs)
                    381:                encode(p, rhs, sz);
                    382: }
                    383:
                    384:
                    385: void
                    386: term_fontlast(struct termp *p)
                    387: {
                    388:        enum termfont    f;
                    389:
                    390:        f = p->fontl;
                    391:        p->fontl = p->fontq[p->fonti];
                    392:        p->fontq[p->fonti] = f;
                    393: }
                    394:
                    395:
                    396: void
                    397: term_fontrepl(struct termp *p, enum termfont f)
                    398: {
                    399:
                    400:        p->fontl = p->fontq[p->fonti];
                    401:        p->fontq[p->fonti] = f;
                    402: }
                    403:
                    404:
                    405: void
                    406: term_fontpush(struct termp *p, enum termfont f)
                    407: {
                    408:
                    409:        assert(p->fonti + 1 < 10);
                    410:        p->fontl = p->fontq[p->fonti];
                    411:        p->fontq[++p->fonti] = f;
                    412: }
                    413:
                    414:
                    415: const void *
                    416: term_fontq(struct termp *p)
                    417: {
                    418:
                    419:        return(&p->fontq[p->fonti]);
                    420: }
                    421:
                    422:
                    423: enum termfont
                    424: term_fonttop(struct termp *p)
                    425: {
                    426:
                    427:        return(p->fontq[p->fonti]);
                    428: }
                    429:
                    430:
                    431: void
                    432: term_fontpopq(struct termp *p, const void *key)
                    433: {
                    434:
                    435:        while (p->fonti >= 0 && key != &p->fontq[p->fonti])
                    436:                p->fonti--;
                    437:        assert(p->fonti >= 0);
                    438: }
1.94      kristaps  439:
1.125     kristaps  440:
                    441: void
                    442: term_fontpop(struct termp *p)
                    443: {
                    444:
                    445:        assert(p->fonti);
                    446:        p->fonti--;
1.17      kristaps  447: }
                    448:
                    449:
1.71      kristaps  450: /*
                    451:  * Handle pwords, partial words, which may be either a single word or a
                    452:  * phrase that cannot be broken down (such as a literal string).  This
                    453:  * handles word styling.
                    454:  */
1.86      kristaps  455: void
                    456: term_word(struct termp *p, const char *word)
1.65      kristaps  457: {
1.124     kristaps  458:        const char      *sv, *seq;
1.125     kristaps  459:        int              sz;
1.124     kristaps  460:        size_t           ssz;
                    461:        enum roffdeco    deco;
1.71      kristaps  462:
1.100     kristaps  463:        sv = word;
                    464:
1.123     kristaps  465:        if (word[0] && '\0' == word[1])
1.100     kristaps  466:                switch (word[0]) {
                    467:                case('.'):
                    468:                        /* FALLTHROUGH */
                    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:                        if ( ! (TERMP_IGNDELIM & p->flags))
                    483:                                p->flags |= TERMP_NOSPACE;
                    484:                        break;
                    485:                default:
                    486:                        break;
                    487:                }
1.65      kristaps  488:
1.133     kristaps  489:        if ( ! (TERMP_NOSPACE & p->flags)) {
1.125     kristaps  490:                bufferc(p, ' ');
1.133     kristaps  491:                if (TERMP_SENTENCE & p->flags)
                    492:                        bufferc(p, ' ');
                    493:        }
1.65      kristaps  494:
1.71      kristaps  495:        if ( ! (p->flags & TERMP_NONOSPACE))
                    496:                p->flags &= ~TERMP_NOSPACE;
1.133     kristaps  497:
                    498:        p->flags &= ~TERMP_SENTENCE;
1.65      kristaps  499:
1.125     kristaps  500:        /* FIXME: use strcspn. */
1.124     kristaps  501:
                    502:        while (*word) {
                    503:                if ('\\' != *word) {
1.125     kristaps  504:                        encode(p, word, 1);
1.124     kristaps  505:                        word++;
                    506:                        continue;
                    507:                }
                    508:
                    509:                seq = ++word;
                    510:                sz = a2roffdeco(&deco, &seq, &ssz);
                    511:
                    512:                switch (deco) {
                    513:                case (DECO_RESERVED):
1.125     kristaps  514:                        res(p, seq, ssz);
1.124     kristaps  515:                        break;
                    516:                case (DECO_SPECIAL):
1.125     kristaps  517:                        spec(p, seq, ssz);
1.124     kristaps  518:                        break;
                    519:                case (DECO_BOLD):
1.125     kristaps  520:                        term_fontrepl(p, TERMFONT_BOLD);
1.124     kristaps  521:                        break;
                    522:                case (DECO_ITALIC):
1.125     kristaps  523:                        term_fontrepl(p, TERMFONT_UNDER);
1.124     kristaps  524:                        break;
                    525:                case (DECO_ROMAN):
1.125     kristaps  526:                        term_fontrepl(p, TERMFONT_NONE);
1.124     kristaps  527:                        break;
                    528:                case (DECO_PREVIOUS):
1.125     kristaps  529:                        term_fontlast(p);
1.124     kristaps  530:                        break;
                    531:                default:
                    532:                        break;
                    533:                }
1.127     kristaps  534:
1.124     kristaps  535:                word += sz;
1.127     kristaps  536:                if (DECO_NOSPACE == deco && '\0' == *word)
                    537:                        p->flags |= TERMP_NOSPACE;
1.124     kristaps  538:        }
1.65      kristaps  539:
1.131     kristaps  540:        /*
                    541:         * Note that we don't process the pipe: the parser sees it as
                    542:         * punctuation, but we don't in terms of typography.
                    543:         */
1.100     kristaps  544:        if (sv[0] && 0 == sv[1])
                    545:                switch (sv[0]) {
                    546:                case('('):
                    547:                        /* FALLTHROUGH */
                    548:                case('['):
                    549:                        p->flags |= TERMP_NOSPACE;
                    550:                        break;
                    551:                default:
                    552:                        break;
                    553:                }
1.65      kristaps  554: }
                    555:
                    556:
1.71      kristaps  557: static void
1.125     kristaps  558: adjbuf(struct termp *p, size_t sz)
1.51      kristaps  559: {
                    560:
1.125     kristaps  561:        if (0 == p->maxcols)
                    562:                p->maxcols = 1024;
                    563:        while (sz >= p->maxcols)
                    564:                p->maxcols <<= 2;
                    565:
                    566:        p->buf = realloc(p->buf, p->maxcols);
                    567:        if (NULL == p->buf) {
                    568:                perror(NULL);
                    569:                exit(EXIT_FAILURE);
1.71      kristaps  570:        }
1.51      kristaps  571: }
                    572:
1.79      kristaps  573:
                    574: static void
1.125     kristaps  575: buffera(struct termp *p, const char *word, size_t sz)
1.79      kristaps  576: {
1.125     kristaps  577:
                    578:        if (p->col + sz >= p->maxcols)
                    579:                adjbuf(p, p->col + sz);
                    580:
1.126     kristaps  581:        memcpy(&p->buf[(int)p->col], word, sz);
1.125     kristaps  582:        p->col += sz;
                    583: }
                    584:
                    585:
                    586: static void
                    587: bufferc(struct termp *p, char c)
                    588: {
                    589:
                    590:        if (p->col + 1 >= p->maxcols)
                    591:                adjbuf(p, p->col + 1);
                    592:
1.126     kristaps  593:        p->buf[(int)p->col++] = c;
1.125     kristaps  594: }
                    595:
                    596:
                    597: static void
                    598: encode(struct termp *p, const char *word, size_t sz)
                    599: {
                    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:
                    609:        if (TERMFONT_NONE == (f = term_fonttop(p))) {
                    610:                buffera(p, word, sz);
                    611:                return;
                    612:        }
                    613:
                    614:        for (i = 0; i < (int)sz; i++) {
                    615:                if ( ! isgraph((u_char)word[i])) {
                    616:                        bufferc(p, word[i]);
                    617:                        continue;
1.79      kristaps  618:                }
1.125     kristaps  619:
                    620:                if (TERMFONT_UNDER == f)
                    621:                        bufferc(p, '_');
                    622:                else
                    623:                        bufferc(p, word[i]);
                    624:
                    625:                bufferc(p, 8);
                    626:                bufferc(p, word[i]);
1.79      kristaps  627:        }
                    628: }
1.106     kristaps  629:
                    630:
1.107     kristaps  631: size_t
                    632: term_vspan(const struct roffsu *su)
1.106     kristaps  633: {
                    634:        double           r;
                    635:
1.107     kristaps  636:        switch (su->unit) {
1.106     kristaps  637:        case (SCALE_CM):
1.107     kristaps  638:                r = su->scale * 2;
1.106     kristaps  639:                break;
                    640:        case (SCALE_IN):
1.107     kristaps  641:                r = su->scale * 6;
1.106     kristaps  642:                break;
                    643:        case (SCALE_PC):
1.107     kristaps  644:                r = su->scale;
1.106     kristaps  645:                break;
                    646:        case (SCALE_PT):
1.107     kristaps  647:                r = su->scale / 8;
1.106     kristaps  648:                break;
                    649:        case (SCALE_MM):
1.107     kristaps  650:                r = su->scale / 1000;
1.106     kristaps  651:                break;
                    652:        case (SCALE_VS):
1.107     kristaps  653:                r = su->scale;
1.106     kristaps  654:                break;
                    655:        default:
1.107     kristaps  656:                r = su->scale - 1;
1.106     kristaps  657:                break;
                    658:        }
                    659:
                    660:        if (r < 0.0)
                    661:                r = 0.0;
1.107     kristaps  662:        return(/* LINTED */(size_t)
1.106     kristaps  663:                        r);
                    664: }
                    665:
                    666:
1.107     kristaps  667: size_t
                    668: term_hspan(const struct roffsu *su)
1.106     kristaps  669: {
                    670:        double           r;
                    671:
1.108     kristaps  672:        /* XXX: CM, IN, and PT are approximations. */
                    673:
1.107     kristaps  674:        switch (su->unit) {
1.106     kristaps  675:        case (SCALE_CM):
1.108     kristaps  676:                r = 4 * su->scale;
1.106     kristaps  677:                break;
                    678:        case (SCALE_IN):
1.108     kristaps  679:                /* XXX: this is an approximation. */
                    680:                r = 10 * su->scale;
1.106     kristaps  681:                break;
                    682:        case (SCALE_PC):
1.108     kristaps  683:                r = (10 * su->scale) / 6;
1.106     kristaps  684:                break;
                    685:        case (SCALE_PT):
1.108     kristaps  686:                r = (10 * su->scale) / 72;
1.106     kristaps  687:                break;
                    688:        case (SCALE_MM):
1.107     kristaps  689:                r = su->scale / 1000; /* FIXME: double-check. */
1.106     kristaps  690:                break;
                    691:        case (SCALE_VS):
1.107     kristaps  692:                r = su->scale * 2 - 1; /* FIXME: double-check. */
1.106     kristaps  693:                break;
                    694:        default:
1.107     kristaps  695:                r = su->scale;
1.106     kristaps  696:                break;
                    697:        }
                    698:
                    699:        if (r < 0.0)
                    700:                r = 0.0;
1.107     kristaps  701:        return((size_t)/* LINTED */
1.106     kristaps  702:                        r);
                    703: }
                    704:
                    705:

CVSweb