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

Annotation of mandoc/term.c, Revision 1.198

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

CVSweb