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

Annotation of mandoc/term.c, Revision 1.122

1.122   ! kristaps    1: /*     $Id: term.c,v 1.121 2009/11/05 07:21:02 kristaps 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:  */
                     17: #include <assert.h>
1.122   ! kristaps   18: #include <ctype.h>
1.22      kristaps   19: #include <stdio.h>
1.1       kristaps   20: #include <stdlib.h>
                     21: #include <string.h>
1.113     kristaps   22: #include <time.h>
1.1       kristaps   23:
1.101     kristaps   24: #include "chars.h"
1.107     kristaps   25: #include "out.h"
1.71      kristaps   26: #include "term.h"
                     27: #include "man.h"
                     28: #include "mdoc.h"
1.105     kristaps   29: #include "main.h"
1.1       kristaps   30:
1.103     kristaps   31: /* FIXME: accomodate non-breaking, non-collapsing white-space. */
                     32: /* FIXME: accomodate non-breaking, collapsing white-space. */
                     33:
1.71      kristaps   34: static struct termp     *term_alloc(enum termenc);
                     35: static void              term_free(struct termp *);
1.95      kristaps   36:
                     37: static void              do_escaped(struct termp *, const char **);
                     38: static void              do_special(struct termp *,
1.71      kristaps   39:                                const char *, size_t);
1.95      kristaps   40: static void              do_reserved(struct termp *,
1.94      kristaps   41:                                const char *, size_t);
1.95      kristaps   42: static void              buffer(struct termp *, char);
                     43: static void              encode(struct termp *, char);
1.1       kristaps   44:
                     45:
1.71      kristaps   46: void *
                     47: ascii_alloc(void)
1.10      kristaps   48: {
1.1       kristaps   49:
1.71      kristaps   50:        return(term_alloc(TERMENC_ASCII));
1.1       kristaps   51: }
                     52:
                     53:
1.99      kristaps   54: void
1.71      kristaps   55: terminal_free(void *arg)
1.11      kristaps   56: {
                     57:
1.71      kristaps   58:        term_free((struct termp *)arg);
1.11      kristaps   59: }
                     60:
                     61:
1.71      kristaps   62: static void
                     63: term_free(struct termp *p)
1.14      kristaps   64: {
                     65:
1.71      kristaps   66:        if (p->buf)
                     67:                free(p->buf);
1.102     kristaps   68:        if (p->symtab)
1.101     kristaps   69:                chars_free(p->symtab);
1.14      kristaps   70:
1.71      kristaps   71:        free(p);
1.14      kristaps   72: }
                     73:
                     74:
1.71      kristaps   75: static struct termp *
                     76: term_alloc(enum termenc enc)
1.14      kristaps   77: {
1.71      kristaps   78:        struct termp *p;
1.14      kristaps   79:
1.117     kristaps   80:        p = calloc(1, sizeof(struct termp));
                     81:        if (NULL == p) {
1.120     kristaps   82:                perror(NULL);
1.117     kristaps   83:                exit(EXIT_FAILURE);
                     84:        }
1.80      kristaps   85:        p->maxrmargin = 78;
1.71      kristaps   86:        p->enc = enc;
                     87:        return(p);
1.14      kristaps   88: }
                     89:
                     90:
1.71      kristaps   91: /*
                     92:  * Flush a line of text.  A "line" is loosely defined as being something
                     93:  * that should be followed by a newline, regardless of whether it's
                     94:  * broken apart by newlines getting there.  A line can also be a
                     95:  * fragment of a columnar list.
                     96:  *
                     97:  * Specifically, a line is whatever's in p->buf of length p->col, which
                     98:  * is zeroed after this function returns.
                     99:  *
1.84      kristaps  100:  * The usage of termp:flags is as follows:
1.71      kristaps  101:  *
                    102:  *  - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
                    103:  *    offset value.  This is useful when doing columnar lists where the
                    104:  *    prior column has right-padded.
                    105:  *
                    106:  *  - TERMP_NOBREAK: this is the most important and is used when making
                    107:  *    columns.  In short: don't print a newline and instead pad to the
                    108:  *    right margin.  Used in conjunction with TERMP_NOLPAD.
                    109:  *
1.91      kristaps  110:  *  - TERMP_TWOSPACE: when padding, make sure there are at least two
                    111:  *    space characters of padding.  Otherwise, rather break the line.
                    112:  *
1.84      kristaps  113:  *  - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
                    114:  *    the line is overrun, and don't pad-right if it's underrun.
                    115:  *
                    116:  *  - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
                    117:  *    overruning, instead save the position and continue at that point
                    118:  *    when the next invocation.
1.71      kristaps  119:  *
                    120:  *  In-line line breaking:
                    121:  *
                    122:  *  If TERMP_NOBREAK is specified and the line overruns the right
                    123:  *  margin, it will break and pad-right to the right margin after
                    124:  *  writing.  If maxrmargin is violated, it will break and continue
1.114     kristaps  125:  *  writing from the right-margin, which will lead to the above scenario
                    126:  *  upon exit.  Otherwise, the line will break at the right margin.
1.71      kristaps  127:  */
                    128: void
                    129: term_flushln(struct termp *p)
1.53      kristaps  130: {
1.114     kristaps  131:        int              i;     /* current input position in p->buf */
                    132:        size_t           vis;   /* current visual position on output */
                    133:        size_t           vbl;   /* number of blanks to prepend to output */
                    134:        size_t           vsz;   /* visual characters to write to output */
                    135:        size_t           bp;    /* visual right border position */
                    136:        int              j;     /* temporary loop index */
                    137:        size_t           maxvis, mmax;
1.91      kristaps  138:        static int       overstep = 0;
1.53      kristaps  139:
1.71      kristaps  140:        /*
                    141:         * First, establish the maximum columns of "visible" content.
                    142:         * This is usually the difference between the right-margin and
                    143:         * an indentation, but can be, for tagged lists or columns, a
1.115     kristaps  144:         * small set of values.
1.71      kristaps  145:         */
1.53      kristaps  146:
1.71      kristaps  147:        assert(p->offset < p->rmargin);
1.92      kristaps  148:
1.114     kristaps  149:        maxvis = (int)(p->rmargin - p->offset) - overstep < 0 ?
1.119     kristaps  150:                /* LINTED */
                    151:                0 : p->rmargin - p->offset - overstep;
1.114     kristaps  152:        mmax = (int)(p->maxrmargin - p->offset) - overstep < 0 ?
1.119     kristaps  153:                /* LINTED */
                    154:                0 : p->maxrmargin - p->offset - overstep;
1.92      kristaps  155:
1.71      kristaps  156:        bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
1.115     kristaps  157:
                    158:        /*
                    159:         * FIXME: if bp is zero, we still output the first word before
                    160:         * breaking the line.
                    161:         */
                    162:
1.71      kristaps  163:        vis = 0;
1.84      kristaps  164:
1.71      kristaps  165:        /*
                    166:         * If in the standard case (left-justified), then begin with our
                    167:         * indentation, otherwise (columns, etc.) just start spitting
                    168:         * out text.
                    169:         */
1.53      kristaps  170:
1.71      kristaps  171:        if ( ! (p->flags & TERMP_NOLPAD))
                    172:                /* LINTED */
                    173:                for (j = 0; j < (int)p->offset; j++)
                    174:                        putchar(' ');
                    175:
                    176:        for (i = 0; i < (int)p->col; i++) {
                    177:                /*
                    178:                 * Count up visible word characters.  Control sequences
                    179:                 * (starting with the CSI) aren't counted.  A space
                    180:                 * generates a non-printing word, which is valid (the
                    181:                 * space is printed according to regular spacing rules).
                    182:                 */
                    183:
                    184:                /* LINTED */
                    185:                for (j = i, vsz = 0; j < (int)p->col; j++) {
1.93      kristaps  186:                        if (j && ' ' == p->buf[j])
1.71      kristaps  187:                                break;
                    188:                        else if (8 == p->buf[j])
1.89      kristaps  189:                                vsz--;
1.71      kristaps  190:                        else
                    191:                                vsz++;
                    192:                }
1.53      kristaps  193:
1.71      kristaps  194:                /*
1.81      kristaps  195:                 * Choose the number of blanks to prepend: no blank at the
                    196:                 * beginning of a line, one between words -- but do not
                    197:                 * actually write them yet.
1.71      kristaps  198:                 */
1.81      kristaps  199:                vbl = (size_t)(0 == vis ? 0 : 1);
1.71      kristaps  200:
1.81      kristaps  201:                /*
                    202:                 * Find out whether we would exceed the right margin.
                    203:                 * If so, break to the next line.  (TODO: hyphenate)
                    204:                 * Otherwise, write the chosen number of blanks now.
                    205:                 */
                    206:                if (vis && vis + vbl + vsz > bp) {
                    207:                        putchar('\n');
                    208:                        if (TERMP_NOBREAK & p->flags) {
                    209:                                for (j = 0; j < (int)p->rmargin; j++)
                    210:                                        putchar(' ');
                    211:                                vis = p->rmargin - p->offset;
                    212:                        } else {
1.71      kristaps  213:                                for (j = 0; j < (int)p->offset; j++)
                    214:                                        putchar(' ');
                    215:                                vis = 0;
1.81      kristaps  216:                        }
1.104     kristaps  217:                        /* Remove the overstep width. */
1.112     kristaps  218:                        bp += (int)/* LINTED */
                    219:                                overstep;
1.110     kristaps  220:                        overstep = 0;
1.81      kristaps  221:                } else {
                    222:                        for (j = 0; j < (int)vbl; j++)
1.71      kristaps  223:                                putchar(' ');
1.81      kristaps  224:                        vis += vbl;
1.71      kristaps  225:                }
1.53      kristaps  226:
1.78      kristaps  227:                /*
1.81      kristaps  228:                 * Finally, write out the word.
1.71      kristaps  229:                 */
                    230:                for ( ; i < (int)p->col; i++) {
                    231:                        if (' ' == p->buf[i])
                    232:                                break;
1.121     kristaps  233:
                    234:                        /* The unit sep. is a non-breaking space. */
                    235:                        if (31 == p->buf[i])
                    236:                                putchar(' ');
                    237:                        else
                    238:                                putchar(p->buf[i]);
1.71      kristaps  239:                }
                    240:                vis += vsz;
                    241:        }
1.111     kristaps  242:
1.91      kristaps  243:        p->col = 0;
1.111     kristaps  244:        overstep = 0;
1.15      kristaps  245:
1.91      kristaps  246:        if ( ! (TERMP_NOBREAK & p->flags)) {
                    247:                putchar('\n');
1.15      kristaps  248:                return;
1.71      kristaps  249:        }
1.15      kristaps  250:
1.91      kristaps  251:        if (TERMP_HANG & p->flags) {
                    252:                /* We need one blank after the tag. */
1.92      kristaps  253:                overstep = /* LINTED */
                    254:                        vis - maxvis + 1;
1.91      kristaps  255:
                    256:                /*
                    257:                 * Behave exactly the same way as groff:
1.92      kristaps  258:                 * If we have overstepped the margin, temporarily move
                    259:                 * it to the right and flag the rest of the line to be
                    260:                 * shorter.
1.91      kristaps  261:                 * If we landed right at the margin, be happy.
1.92      kristaps  262:                 * If we are one step before the margin, temporarily
                    263:                 * move it one step LEFT and flag the rest of the line
                    264:                 * to be longer.
1.91      kristaps  265:                 */
1.92      kristaps  266:                if (overstep >= -1) {
                    267:                        assert((int)maxvis + overstep >= 0);
                    268:                        /* LINTED */
1.91      kristaps  269:                        maxvis += overstep;
1.92      kristaps  270:                } else
1.91      kristaps  271:                        overstep = 0;
                    272:
                    273:        } else if (TERMP_DANGLE & p->flags)
                    274:                return;
1.15      kristaps  275:
1.92      kristaps  276:        /* Right-pad. */
                    277:        if (maxvis > vis + /* LINTED */
                    278:                        ((TERMP_TWOSPACE & p->flags) ? 1 : 0))
1.91      kristaps  279:                for ( ; vis < maxvis; vis++)
                    280:                        putchar(' ');
1.92      kristaps  281:        else {  /* ...or newline break. */
1.71      kristaps  282:                putchar('\n');
1.91      kristaps  283:                for (i = 0; i < (int)p->rmargin; i++)
                    284:                        putchar(' ');
                    285:        }
1.15      kristaps  286: }
                    287:
                    288:
1.71      kristaps  289: /*
                    290:  * A newline only breaks an existing line; it won't assert vertical
                    291:  * space.  All data in the output buffer is flushed prior to the newline
                    292:  * assertion.
                    293:  */
                    294: void
                    295: term_newln(struct termp *p)
1.15      kristaps  296: {
                    297:
1.71      kristaps  298:        p->flags |= TERMP_NOSPACE;
                    299:        if (0 == p->col) {
                    300:                p->flags &= ~TERMP_NOLPAD;
1.15      kristaps  301:                return;
1.16      kristaps  302:        }
1.71      kristaps  303:        term_flushln(p);
                    304:        p->flags &= ~TERMP_NOLPAD;
1.16      kristaps  305: }
                    306:
                    307:
1.71      kristaps  308: /*
                    309:  * Asserts a vertical space (a full, empty line-break between lines).
                    310:  * Note that if used twice, this will cause two blank spaces and so on.
                    311:  * All data in the output buffer is flushed prior to the newline
                    312:  * assertion.
                    313:  */
                    314: void
                    315: term_vspace(struct termp *p)
1.16      kristaps  316: {
                    317:
1.62      kristaps  318:        term_newln(p);
1.71      kristaps  319:        putchar('\n');
1.16      kristaps  320: }
                    321:
                    322:
1.71      kristaps  323: static void
1.95      kristaps  324: do_special(struct termp *p, const char *word, size_t len)
1.17      kristaps  325: {
1.71      kristaps  326:        const char      *rhs;
                    327:        size_t           sz;
1.79      kristaps  328:        int              i;
1.17      kristaps  329:
1.101     kristaps  330:        rhs = chars_a2ascii(p->symtab, word, len, &sz);
1.86      kristaps  331:
1.96      kristaps  332:        if (NULL == rhs) {
1.97      kristaps  333: #if 0
1.96      kristaps  334:                fputs("Unknown special character: ", stderr);
                    335:                for (i = 0; i < (int)len; i++)
                    336:                        fputc(word[i], stderr);
                    337:                fputc('\n', stderr);
                    338: #endif
1.94      kristaps  339:                return;
1.96      kristaps  340:        }
1.94      kristaps  341:        for (i = 0; i < (int)sz; i++)
1.95      kristaps  342:                encode(p, rhs[i]);
1.94      kristaps  343: }
                    344:
                    345:
                    346: static void
1.95      kristaps  347: do_reserved(struct termp *p, const char *word, size_t len)
1.94      kristaps  348: {
                    349:        const char      *rhs;
                    350:        size_t           sz;
                    351:        int              i;
                    352:
1.101     kristaps  353:        rhs = chars_a2res(p->symtab, word, len, &sz);
1.94      kristaps  354:
1.96      kristaps  355:        if (NULL == rhs) {
                    356: #if 0
                    357:                fputs("Unknown reserved word: ", stderr);
                    358:                for (i = 0; i < (int)len; i++)
                    359:                        fputc(word[i], stderr);
                    360:                fputc('\n', stderr);
                    361: #endif
1.94      kristaps  362:                return;
1.96      kristaps  363:        }
1.94      kristaps  364:        for (i = 0; i < (int)sz; i++)
1.95      kristaps  365:                encode(p, rhs[i]);
1.17      kristaps  366: }
                    367:
                    368:
1.71      kristaps  369: /*
                    370:  * Handle an escape sequence: determine its length and pass it to the
                    371:  * escape-symbol look table.  Note that we assume mdoc(3) has validated
                    372:  * the escape sequence (we assert upon badly-formed escape sequences).
                    373:  */
                    374: static void
1.95      kristaps  375: do_escaped(struct termp *p, const char **word)
1.17      kristaps  376: {
1.122   ! kristaps  377:        int              j, type, sv;
1.86      kristaps  378:        const char      *wp;
                    379:
                    380:        wp = *word;
1.97      kristaps  381:        type = 1;
1.17      kristaps  382:
1.86      kristaps  383:        if (0 == *(++wp)) {
                    384:                *word = wp;
1.71      kristaps  385:                return;
1.86      kristaps  386:        }
1.17      kristaps  387:
1.86      kristaps  388:        if ('(' == *wp) {
                    389:                wp++;
                    390:                if (0 == *wp || 0 == *(wp + 1)) {
                    391:                        *word = 0 == *wp ? wp : wp + 1;
1.71      kristaps  392:                        return;
1.86      kristaps  393:                }
1.22      kristaps  394:
1.95      kristaps  395:                do_special(p, wp, 2);
1.86      kristaps  396:                *word = ++wp;
1.71      kristaps  397:                return;
1.22      kristaps  398:
1.86      kristaps  399:        } else if ('*' == *wp) {
                    400:                if (0 == *(++wp)) {
                    401:                        *word = wp;
1.71      kristaps  402:                        return;
1.86      kristaps  403:                }
1.22      kristaps  404:
1.86      kristaps  405:                switch (*wp) {
1.71      kristaps  406:                case ('('):
1.86      kristaps  407:                        wp++;
                    408:                        if (0 == *wp || 0 == *(wp + 1)) {
                    409:                                *word = 0 == *wp ? wp : wp + 1;
1.71      kristaps  410:                                return;
1.86      kristaps  411:                        }
1.65      kristaps  412:
1.95      kristaps  413:                        do_reserved(p, wp, 2);
1.86      kristaps  414:                        *word = ++wp;
1.71      kristaps  415:                        return;
                    416:                case ('['):
1.97      kristaps  417:                        type = 0;
1.71      kristaps  418:                        break;
                    419:                default:
1.95      kristaps  420:                        do_reserved(p, wp, 1);
1.86      kristaps  421:                        *word = wp;
1.71      kristaps  422:                        return;
                    423:                }
                    424:
1.86      kristaps  425:        } else if ('f' == *wp) {
                    426:                if (0 == *(++wp)) {
                    427:                        *word = wp;
1.71      kristaps  428:                        return;
1.86      kristaps  429:                }
                    430:
                    431:                switch (*wp) {
1.122   ! kristaps  432:                case ('3'):
        !           433:                        /* FALLTHROUGH */
1.71      kristaps  434:                case ('B'):
1.122   ! kristaps  435:                        p->metamask = p->metafont;
        !           436:                        p->metafont |= METAF_BOLD;
1.71      kristaps  437:                        break;
1.122   ! kristaps  438:                case ('2'):
        !           439:                        /* FALLTHROUGH */
1.71      kristaps  440:                case ('I'):
1.122   ! kristaps  441:                        p->metamask = p->metafont;
        !           442:                        p->metafont |= METAF_UNDER;
1.71      kristaps  443:                        break;
                    444:                case ('P'):
1.122   ! kristaps  445:                        sv = p->metamask;
        !           446:                        p->metamask = p->metafont;
        !           447:                        p->metafont = sv;
        !           448:                        break;
        !           449:                case ('1'):
1.71      kristaps  450:                        /* FALLTHROUGH */
                    451:                case ('R'):
1.122   ! kristaps  452:                        p->metamask = p->metafont;
        !           453:                        p->metafont &= ~METAF_UNDER;
        !           454:                        p->metafont &= ~METAF_BOLD;
1.71      kristaps  455:                        break;
                    456:                default:
                    457:                        break;
                    458:                }
1.86      kristaps  459:
                    460:                *word = wp;
1.71      kristaps  461:                return;
1.22      kristaps  462:
1.86      kristaps  463:        } else if ('[' != *wp) {
1.95      kristaps  464:                do_special(p, wp, 1);
1.86      kristaps  465:                *word = wp;
1.71      kristaps  466:                return;
                    467:        }
1.28      kristaps  468:
1.86      kristaps  469:        wp++;
                    470:        for (j = 0; *wp && ']' != *wp; wp++, j++)
1.71      kristaps  471:                /* Loop... */ ;
1.28      kristaps  472:
1.86      kristaps  473:        if (0 == *wp) {
                    474:                *word = wp;
1.71      kristaps  475:                return;
1.86      kristaps  476:        }
1.48      kristaps  477:
1.97      kristaps  478:        if (type)
                    479:                do_special(p, wp - j, (size_t)j);
                    480:        else
                    481:                do_reserved(p, wp - j, (size_t)j);
1.86      kristaps  482:        *word = wp;
1.48      kristaps  483: }
                    484:
                    485:
1.71      kristaps  486: /*
                    487:  * Handle pwords, partial words, which may be either a single word or a
                    488:  * phrase that cannot be broken down (such as a literal string).  This
                    489:  * handles word styling.
                    490:  */
1.86      kristaps  491: void
                    492: term_word(struct termp *p, const char *word)
1.65      kristaps  493: {
1.88      kristaps  494:        const char       *sv;
1.71      kristaps  495:
1.100     kristaps  496:        sv = word;
                    497:
                    498:        if (word[0] && 0 == word[1])
                    499:                switch (word[0]) {
                    500:                case('.'):
                    501:                        /* FALLTHROUGH */
                    502:                case(','):
                    503:                        /* FALLTHROUGH */
                    504:                case(';'):
                    505:                        /* FALLTHROUGH */
                    506:                case(':'):
                    507:                        /* FALLTHROUGH */
                    508:                case('?'):
                    509:                        /* FALLTHROUGH */
                    510:                case('!'):
                    511:                        /* FALLTHROUGH */
                    512:                case(')'):
                    513:                        /* FALLTHROUGH */
                    514:                case(']'):
                    515:                        /* FALLTHROUGH */
                    516:                case('}'):
                    517:                        if ( ! (TERMP_IGNDELIM & p->flags))
                    518:                                p->flags |= TERMP_NOSPACE;
                    519:                        break;
                    520:                default:
                    521:                        break;
                    522:                }
1.65      kristaps  523:
1.71      kristaps  524:        if ( ! (TERMP_NOSPACE & p->flags))
1.95      kristaps  525:                buffer(p, ' ');
1.65      kristaps  526:
1.71      kristaps  527:        if ( ! (p->flags & TERMP_NONOSPACE))
                    528:                p->flags &= ~TERMP_NOSPACE;
1.65      kristaps  529:
1.100     kristaps  530:        for ( ; *word; word++)
1.86      kristaps  531:                if ('\\' != *word)
1.95      kristaps  532:                        encode(p, *word);
1.79      kristaps  533:                else
1.95      kristaps  534:                        do_escaped(p, &word);
1.65      kristaps  535:
1.100     kristaps  536:        if (sv[0] && 0 == sv[1])
                    537:                switch (sv[0]) {
                    538:                case('('):
                    539:                        /* FALLTHROUGH */
                    540:                case('['):
                    541:                        /* FALLTHROUGH */
                    542:                case('{'):
                    543:                        p->flags |= TERMP_NOSPACE;
                    544:                        break;
                    545:                default:
                    546:                        break;
                    547:                }
1.65      kristaps  548: }
                    549:
                    550:
1.71      kristaps  551: /*
                    552:  * Insert a single character into the line-buffer.  If the buffer's
                    553:  * space is exceeded, then allocate more space by doubling the buffer
                    554:  * size.
                    555:  */
                    556: static void
1.95      kristaps  557: buffer(struct termp *p, char c)
1.51      kristaps  558: {
1.71      kristaps  559:        size_t           s;
1.51      kristaps  560:
1.71      kristaps  561:        if (p->col + 1 >= p->maxcols) {
                    562:                if (0 == p->maxcols)
                    563:                        p->maxcols = 256;
                    564:                s = p->maxcols * 2;
                    565:                p->buf = realloc(p->buf, s);
1.118     kristaps  566:                if (NULL == p->buf) {
1.120     kristaps  567:                        perror(NULL);
1.118     kristaps  568:                        exit(EXIT_FAILURE);
                    569:                }
1.71      kristaps  570:                p->maxcols = s;
                    571:        }
                    572:        p->buf[(int)(p->col)++] = c;
1.51      kristaps  573: }
                    574:
1.79      kristaps  575:
                    576: static void
1.95      kristaps  577: encode(struct termp *p, char c)
1.79      kristaps  578: {
1.89      kristaps  579:
1.122   ! kristaps  580:        if (isgraph((u_char)c)) {
        !           581:                if (p->under || METAF_UNDER & p->metafont) {
1.109     kristaps  582:                        buffer(p, '_');
                    583:                        buffer(p, 8);
                    584:                }
1.122   ! kristaps  585:                if (p->bold || METAF_BOLD & p->metafont) {
1.95      kristaps  586:                        buffer(p, c);
                    587:                        buffer(p, 8);
1.79      kristaps  588:                }
                    589:        }
1.95      kristaps  590:        buffer(p, c);
1.79      kristaps  591: }
1.106     kristaps  592:
                    593:
1.107     kristaps  594: size_t
                    595: term_vspan(const struct roffsu *su)
1.106     kristaps  596: {
                    597:        double           r;
                    598:
1.107     kristaps  599:        switch (su->unit) {
1.106     kristaps  600:        case (SCALE_CM):
1.107     kristaps  601:                r = su->scale * 2;
1.106     kristaps  602:                break;
                    603:        case (SCALE_IN):
1.107     kristaps  604:                r = su->scale * 6;
1.106     kristaps  605:                break;
                    606:        case (SCALE_PC):
1.107     kristaps  607:                r = su->scale;
1.106     kristaps  608:                break;
                    609:        case (SCALE_PT):
1.107     kristaps  610:                r = su->scale / 8;
1.106     kristaps  611:                break;
                    612:        case (SCALE_MM):
1.107     kristaps  613:                r = su->scale / 1000;
1.106     kristaps  614:                break;
                    615:        case (SCALE_VS):
1.107     kristaps  616:                r = su->scale;
1.106     kristaps  617:                break;
                    618:        default:
1.107     kristaps  619:                r = su->scale - 1;
1.106     kristaps  620:                break;
                    621:        }
                    622:
                    623:        if (r < 0.0)
                    624:                r = 0.0;
1.107     kristaps  625:        return(/* LINTED */(size_t)
1.106     kristaps  626:                        r);
                    627: }
                    628:
                    629:
1.107     kristaps  630: size_t
                    631: term_hspan(const struct roffsu *su)
1.106     kristaps  632: {
                    633:        double           r;
                    634:
1.108     kristaps  635:        /* XXX: CM, IN, and PT are approximations. */
                    636:
1.107     kristaps  637:        switch (su->unit) {
1.106     kristaps  638:        case (SCALE_CM):
1.108     kristaps  639:                r = 4 * su->scale;
1.106     kristaps  640:                break;
                    641:        case (SCALE_IN):
1.108     kristaps  642:                /* XXX: this is an approximation. */
                    643:                r = 10 * su->scale;
1.106     kristaps  644:                break;
                    645:        case (SCALE_PC):
1.108     kristaps  646:                r = (10 * su->scale) / 6;
1.106     kristaps  647:                break;
                    648:        case (SCALE_PT):
1.108     kristaps  649:                r = (10 * su->scale) / 72;
1.106     kristaps  650:                break;
                    651:        case (SCALE_MM):
1.107     kristaps  652:                r = su->scale / 1000; /* FIXME: double-check. */
1.106     kristaps  653:                break;
                    654:        case (SCALE_VS):
1.107     kristaps  655:                r = su->scale * 2 - 1; /* FIXME: double-check. */
1.106     kristaps  656:                break;
                    657:        default:
1.107     kristaps  658:                r = su->scale;
1.106     kristaps  659:                break;
                    660:        }
                    661:
                    662:        if (r < 0.0)
                    663:                r = 0.0;
1.107     kristaps  664:        return((size_t)/* LINTED */
1.106     kristaps  665:                        r);
                    666: }
                    667:
                    668:

CVSweb