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

Annotation of mandoc/term.c, Revision 1.237

1.237   ! schwarze    1: /*     $Id: term.c,v 1.236 2014/11/21 01:52:53 schwarze Exp $ */
1.1       kristaps    2: /*
1.198     schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.216     schwarze    4:  * Copyright (c) 2010-2014 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: #include "config.h"
                     19:
1.126     kristaps   20: #include <sys/types.h>
                     21:
1.1       kristaps   22: #include <assert.h>
1.122     kristaps   23: #include <ctype.h>
1.22      kristaps   24: #include <stdio.h>
1.1       kristaps   25: #include <stdlib.h>
                     26: #include <string.h>
                     27:
1.137     kristaps   28: #include "mandoc.h"
1.218     schwarze   29: #include "mandoc_aux.h"
1.107     kristaps   30: #include "out.h"
1.71      kristaps   31: #include "term.h"
1.105     kristaps   32: #include "main.h"
1.1       kristaps   33:
1.203     schwarze   34: static size_t           cond_width(const struct termp *, int, int *);
1.210     schwarze   35: static void             adjbuf(struct termp *p, size_t);
1.191     kristaps   36: static void             bufferc(struct termp *, char);
                     37: static void             encode(struct termp *, const char *, size_t);
1.194     kristaps   38: static void             encode1(struct termp *, int);
1.11      kristaps   39:
1.222     schwarze   40:
1.145     kristaps   41: void
1.71      kristaps   42: term_free(struct termp *p)
1.14      kristaps   43: {
                     44:
1.231     schwarze   45:        free(p->buf);
1.142     kristaps   46:        free(p);
                     47: }
                     48:
                     49: void
1.222     schwarze   50: term_begin(struct termp *p, term_margin head,
1.142     kristaps   51:                term_margin foot, const void *arg)
                     52: {
                     53:
                     54:        p->headf = head;
                     55:        p->footf = foot;
                     56:        p->argf = arg;
1.146     kristaps   57:        (*p->begin)(p);
1.142     kristaps   58: }
                     59:
                     60: void
                     61: term_end(struct termp *p)
                     62: {
                     63:
1.146     kristaps   64:        (*p->end)(p);
1.14      kristaps   65: }
                     66:
1.71      kristaps   67: /*
1.221     schwarze   68:  * Flush a chunk of text.  By default, break the output line each time
                     69:  * the right margin is reached, and continue output on the next line
                     70:  * at the same offset as the chunk itself.  By default, also break the
                     71:  * output line at the end of the chunk.
1.130     kristaps   72:  * The following flags may be specified:
1.71      kristaps   73:  *
1.221     schwarze   74:  *  - TERMP_NOBREAK: Do not break the output line at the right margin,
                     75:  *    but only at the max right margin.  Also, do not break the output
                     76:  *    line at the end of the chunk, such that the next call can pad to
                     77:  *    the next column.  However, if less than p->trailspace blanks,
                     78:  *    which can be 0, 1, or 2, remain to the right margin, the line
                     79:  *    will be broken.
                     80:  *  - TERMP_BRIND: If the chunk does not fit and the output line has
                     81:  *    to be broken, start the next line at the right margin instead
                     82:  *    of at the offset.  Used together with TERMP_NOBREAK for the tags
                     83:  *    in various kinds of tagged lists.
                     84:  *  - TERMP_DANGLE: Do not break the output line at the right margin,
                     85:  *    append the next chunk after it even if this one is too long.
                     86:  *    To be used together with TERMP_NOBREAK.
                     87:  *  - TERMP_HANG: Like TERMP_DANGLE, and also suppress padding before
                     88:  *    the next chunk if this column is not full.
1.71      kristaps   89:  */
                     90: void
                     91: term_flushln(struct termp *p)
1.53      kristaps   92: {
1.210     schwarze   93:        size_t           i;     /* current input position in p->buf */
1.205     schwarze   94:        int              ntab;  /* number of tabs to prepend */
1.114     kristaps   95:        size_t           vis;   /* current visual position on output */
                     96:        size_t           vbl;   /* number of blanks to prepend to output */
1.136     schwarze   97:        size_t           vend;  /* end of word visual position on output */
1.114     kristaps   98:        size_t           bp;    /* visual right border position */
1.172     schwarze   99:        size_t           dv;    /* temporary for visual pos calculations */
1.210     schwarze  100:        size_t           j;     /* temporary loop index for p->buf */
                    101:        size_t           jhy;   /* last hyph before overflow w/r/t j */
1.152     kristaps  102:        size_t           maxvis; /* output position of visible boundary */
1.236     schwarze  103:        size_t           rmargin; /* the rightmost of the two margins */
1.53      kristaps  104:
1.71      kristaps  105:        /*
                    106:         * First, establish the maximum columns of "visible" content.
                    107:         * This is usually the difference between the right-margin and
                    108:         * an indentation, but can be, for tagged lists or columns, a
1.212     schwarze  109:         * small set of values.
                    110:         *
                    111:         * The following unsigned-signed subtractions look strange,
                    112:         * but they are actually correct.  If the int p->overstep
                    113:         * is negative, it gets sign extended.  Subtracting that
                    114:         * very large size_t effectively adds a small number to dv.
1.71      kristaps  115:         */
1.236     schwarze  116:        rmargin = p->rmargin > p->offset ? p->rmargin : p->offset;
                    117:        dv = p->rmargin - p->offset;
1.174     schwarze  118:        maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
1.92      kristaps  119:
1.236     schwarze  120:        if (p->flags & TERMP_NOBREAK) {
                    121:                dv = p->maxrmargin > p->offset ?
                    122:                     p->maxrmargin - p->offset : 0;
                    123:                bp = (int)dv > p->overstep ?
                    124:                     dv - (size_t)p->overstep : 0;
                    125:        } else
                    126:                bp = maxvis;
1.115     kristaps  127:
1.136     schwarze  128:        /*
1.200     schwarze  129:         * Calculate the required amount of padding.
1.136     schwarze  130:         */
1.200     schwarze  131:        vbl = p->offset + p->overstep > p->viscol ?
                    132:              p->offset + p->overstep - p->viscol : 0;
1.136     schwarze  133:
1.174     schwarze  134:        vis = vend = 0;
                    135:        i = 0;
1.115     kristaps  136:
1.188     kristaps  137:        while (i < p->col) {
1.71      kristaps  138:                /*
1.154     kristaps  139:                 * Handle literal tab characters: collapse all
                    140:                 * subsequent tabs into a single huge set of spaces.
1.138     schwarze  141:                 */
1.205     schwarze  142:                ntab = 0;
1.188     kristaps  143:                while (i < p->col && '\t' == p->buf[i]) {
1.154     kristaps  144:                        vend = (vis / p->tabwidth + 1) * p->tabwidth;
1.138     schwarze  145:                        vbl += vend - vis;
                    146:                        vis = vend;
1.205     schwarze  147:                        ntab++;
1.169     schwarze  148:                        i++;
1.138     schwarze  149:                }
                    150:
                    151:                /*
1.71      kristaps  152:                 * Count up visible word characters.  Control sequences
                    153:                 * (starting with the CSI) aren't counted.  A space
                    154:                 * generates a non-printing word, which is valid (the
                    155:                 * space is printed according to regular spacing rules).
                    156:                 */
                    157:
1.188     kristaps  158:                for (j = i, jhy = 0; j < p->col; j++) {
1.208     schwarze  159:                        if (' ' == p->buf[j] || '\t' == p->buf[j])
1.71      kristaps  160:                                break;
1.154     kristaps  161:
                    162:                        /* Back over the the last printed character. */
                    163:                        if (8 == p->buf[j]) {
1.153     kristaps  164:                                assert(j);
                    165:                                vend -= (*p->width)(p, p->buf[j - 1]);
1.154     kristaps  166:                                continue;
1.153     kristaps  167:                        }
1.154     kristaps  168:
                    169:                        /* Regular word. */
                    170:                        /* Break at the hyphen point if we overrun. */
1.222     schwarze  171:                        if (vend > vis && vend < bp &&
1.216     schwarze  172:                            (ASCII_HYPH == p->buf[j] ||
                    173:                             ASCII_BREAK == p->buf[j]))
1.154     kristaps  174:                                jhy = j;
                    175:
1.217     schwarze  176:                        /*
                    177:                         * Hyphenation now decided, put back a real
                    178:                         * hyphen such that we get the correct width.
                    179:                         */
                    180:                        if (ASCII_HYPH == p->buf[j])
                    181:                                p->buf[j] = '-';
                    182:
1.154     kristaps  183:                        vend += (*p->width)(p, p->buf[j]);
1.71      kristaps  184:                }
1.53      kristaps  185:
1.71      kristaps  186:                /*
1.81      kristaps  187:                 * Find out whether we would exceed the right margin.
1.136     schwarze  188:                 * If so, break to the next line.
1.81      kristaps  189:                 */
1.140     kristaps  190:                if (vend > bp && 0 == jhy && vis > 0) {
1.136     schwarze  191:                        vend -= vis;
1.146     kristaps  192:                        (*p->endline)(p);
1.201     schwarze  193:                        p->viscol = 0;
1.221     schwarze  194:                        if (TERMP_BRIND & p->flags) {
1.236     schwarze  195:                                vbl = rmargin;
                    196:                                vend += rmargin - p->offset;
1.201     schwarze  197:                        } else
1.136     schwarze  198:                                vbl = p->offset;
1.205     schwarze  199:
                    200:                        /* use pending tabs on the new line */
                    201:
                    202:                        if (0 < ntab)
                    203:                                vbl += ntab * p->tabwidth;
1.130     kristaps  204:
1.212     schwarze  205:                        /*
                    206:                         * Remove the p->overstep width.
                    207:                         * Again, if p->overstep is negative,
                    208:                         * sign extension does the right thing.
                    209:                         */
1.130     kristaps  210:
1.174     schwarze  211:                        bp += (size_t)p->overstep;
1.129     kristaps  212:                        p->overstep = 0;
1.71      kristaps  213:                }
1.138     schwarze  214:
1.130     kristaps  215:                /* Write out the [remaining] word. */
1.188     kristaps  216:                for ( ; i < p->col; i++) {
1.140     kristaps  217:                        if (vend > bp && jhy > 0 && i > jhy)
                    218:                                break;
1.138     schwarze  219:                        if ('\t' == p->buf[i])
                    220:                                break;
1.136     schwarze  221:                        if (' ' == p->buf[i]) {
1.164     kristaps  222:                                j = i;
1.228     kristaps  223:                                while (i < p->col && ' ' == p->buf[i])
1.136     schwarze  224:                                        i++;
1.210     schwarze  225:                                dv = (i - j) * (*p->width)(p, ' ');
1.172     schwarze  226:                                vbl += dv;
                    227:                                vend += dv;
1.71      kristaps  228:                                break;
1.136     schwarze  229:                        }
                    230:                        if (ASCII_NBRSP == p->buf[i]) {
1.153     kristaps  231:                                vbl += (*p->width)(p, ' ');
1.136     schwarze  232:                                continue;
                    233:                        }
1.216     schwarze  234:                        if (ASCII_BREAK == p->buf[i])
                    235:                                continue;
1.130     kristaps  236:
1.136     schwarze  237:                        /*
                    238:                         * Now we definitely know there will be
                    239:                         * printable characters to output,
                    240:                         * so write preceding white space now.
                    241:                         */
                    242:                        if (vbl) {
1.146     kristaps  243:                                (*p->advance)(p, vbl);
1.139     schwarze  244:                                p->viscol += vbl;
1.136     schwarze  245:                                vbl = 0;
1.200     schwarze  246:                        }
                    247:
                    248:                        (*p->letter)(p, p->buf[i]);
                    249:                        if (8 == p->buf[i])
                    250:                                p->viscol -= (*p->width)(p, p->buf[i-1]);
1.222     schwarze  251:                        else
1.153     kristaps  252:                                p->viscol += (*p->width)(p, p->buf[i]);
1.136     schwarze  253:                }
                    254:                vis = vend;
1.71      kristaps  255:        }
1.168     schwarze  256:
                    257:        /*
                    258:         * If there was trailing white space, it was not printed;
                    259:         * so reset the cursor position accordingly.
                    260:         */
1.235     schwarze  261:        if (vis > vbl)
1.200     schwarze  262:                vis -= vbl;
1.235     schwarze  263:        else
                    264:                vis = 0;
1.111     kristaps  265:
1.91      kristaps  266:        p->col = 0;
1.129     kristaps  267:        p->overstep = 0;
1.15      kristaps  268:
1.91      kristaps  269:        if ( ! (TERMP_NOBREAK & p->flags)) {
1.139     schwarze  270:                p->viscol = 0;
1.146     kristaps  271:                (*p->endline)(p);
1.15      kristaps  272:                return;
1.71      kristaps  273:        }
1.15      kristaps  274:
1.91      kristaps  275:        if (TERMP_HANG & p->flags) {
1.211     schwarze  276:                p->overstep = (int)(vis - maxvis +
1.222     schwarze  277:                    p->trailspace * (*p->width)(p, ' '));
1.91      kristaps  278:
                    279:                /*
1.92      kristaps  280:                 * If we have overstepped the margin, temporarily move
                    281:                 * it to the right and flag the rest of the line to be
                    282:                 * shorter.
1.212     schwarze  283:                 * If there is a request to keep the columns together,
                    284:                 * allow negative overstep when the column is not full.
1.91      kristaps  285:                 */
1.212     schwarze  286:                if (p->trailspace && p->overstep < 0)
1.129     kristaps  287:                        p->overstep = 0;
1.200     schwarze  288:                return;
1.91      kristaps  289:
                    290:        } else if (TERMP_DANGLE & p->flags)
                    291:                return;
1.15      kristaps  292:
1.200     schwarze  293:        /* If the column was overrun, break the line. */
1.211     schwarze  294:        if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
1.146     kristaps  295:                (*p->endline)(p);
1.200     schwarze  296:                p->viscol = 0;
1.91      kristaps  297:        }
1.15      kristaps  298: }
                    299:
1.222     schwarze  300: /*
1.71      kristaps  301:  * A newline only breaks an existing line; it won't assert vertical
                    302:  * space.  All data in the output buffer is flushed prior to the newline
                    303:  * assertion.
                    304:  */
                    305: void
                    306: term_newln(struct termp *p)
1.15      kristaps  307: {
                    308:
1.71      kristaps  309:        p->flags |= TERMP_NOSPACE;
1.200     schwarze  310:        if (p->col || p->viscol)
                    311:                term_flushln(p);
1.16      kristaps  312: }
                    313:
1.71      kristaps  314: /*
                    315:  * Asserts a vertical space (a full, empty line-break between lines).
                    316:  * Note that if used twice, this will cause two blank spaces and so on.
                    317:  * All data in the output buffer is flushed prior to the newline
                    318:  * assertion.
                    319:  */
                    320: void
                    321: term_vspace(struct termp *p)
1.16      kristaps  322: {
                    323:
1.62      kristaps  324:        term_newln(p);
1.139     schwarze  325:        p->viscol = 0;
1.202     schwarze  326:        if (0 < p->skipvsp)
                    327:                p->skipvsp--;
                    328:        else
                    329:                (*p->endline)(p);
1.16      kristaps  330: }
                    331:
1.125     kristaps  332: void
                    333: term_fontlast(struct termp *p)
                    334: {
                    335:        enum termfont    f;
                    336:
                    337:        f = p->fontl;
                    338:        p->fontl = p->fontq[p->fonti];
                    339:        p->fontq[p->fonti] = f;
                    340: }
                    341:
                    342: void
                    343: term_fontrepl(struct termp *p, enum termfont f)
                    344: {
                    345:
                    346:        p->fontl = p->fontq[p->fonti];
                    347:        p->fontq[p->fonti] = f;
                    348: }
                    349:
                    350: void
                    351: term_fontpush(struct termp *p, enum termfont f)
                    352: {
                    353:
                    354:        assert(p->fonti + 1 < 10);
                    355:        p->fontl = p->fontq[p->fonti];
                    356:        p->fontq[++p->fonti] = f;
                    357: }
                    358:
                    359: const void *
                    360: term_fontq(struct termp *p)
                    361: {
                    362:
                    363:        return(&p->fontq[p->fonti]);
                    364: }
                    365:
                    366: enum termfont
                    367: term_fonttop(struct termp *p)
                    368: {
                    369:
                    370:        return(p->fontq[p->fonti]);
                    371: }
                    372:
                    373: void
                    374: term_fontpopq(struct termp *p, const void *key)
                    375: {
                    376:
1.206     schwarze  377:        while (p->fonti >= 0 && key < (void *)(p->fontq + p->fonti))
1.125     kristaps  378:                p->fonti--;
                    379:        assert(p->fonti >= 0);
                    380: }
1.94      kristaps  381:
1.125     kristaps  382: void
                    383: term_fontpop(struct termp *p)
                    384: {
                    385:
                    386:        assert(p->fonti);
                    387:        p->fonti--;
1.17      kristaps  388: }
                    389:
1.71      kristaps  390: /*
                    391:  * Handle pwords, partial words, which may be either a single word or a
                    392:  * phrase that cannot be broken down (such as a literal string).  This
                    393:  * handles word styling.
                    394:  */
1.86      kristaps  395: void
                    396: term_word(struct termp *p, const char *word)
1.65      kristaps  397: {
1.214     schwarze  398:        const char       nbrsp[2] = { ASCII_NBRSP, 0 };
1.191     kristaps  399:        const char      *seq, *cp;
1.194     kristaps  400:        int              sz, uc;
1.124     kristaps  401:        size_t           ssz;
1.184     kristaps  402:        enum mandoc_esc  esc;
1.100     kristaps  403:
1.133     kristaps  404:        if ( ! (TERMP_NOSPACE & p->flags)) {
1.151     schwarze  405:                if ( ! (TERMP_KEEP & p->flags)) {
1.133     kristaps  406:                        bufferc(p, ' ');
1.151     schwarze  407:                        if (TERMP_SENTENCE & p->flags)
                    408:                                bufferc(p, ' ');
                    409:                } else
                    410:                        bufferc(p, ASCII_NBRSP);
1.133     kristaps  411:        }
1.207     schwarze  412:        if (TERMP_PREKEEP & p->flags)
                    413:                p->flags |= TERMP_KEEP;
1.65      kristaps  414:
1.71      kristaps  415:        if ( ! (p->flags & TERMP_NONOSPACE))
                    416:                p->flags &= ~TERMP_NOSPACE;
1.166     kristaps  417:        else
                    418:                p->flags |= TERMP_NOSPACE;
1.133     kristaps  419:
1.237   ! schwarze  420:        p->flags &= ~(TERMP_SENTENCE | TERMP_NONEWLINE);
1.65      kristaps  421:
1.184     kristaps  422:        while ('\0' != *word) {
1.203     schwarze  423:                if ('\\' != *word) {
                    424:                        if (TERMP_SKIPCHAR & p->flags) {
                    425:                                p->flags &= ~TERMP_SKIPCHAR;
                    426:                                word++;
                    427:                                continue;
                    428:                        }
1.214     schwarze  429:                        if (TERMP_NBRWORD & p->flags) {
                    430:                                if (' ' == *word) {
                    431:                                        encode(p, nbrsp, 1);
                    432:                                        word++;
                    433:                                        continue;
                    434:                                }
                    435:                                ssz = strcspn(word, "\\ ");
                    436:                        } else
                    437:                                ssz = strcspn(word, "\\");
1.162     kristaps  438:                        encode(p, word, ssz);
1.203     schwarze  439:                        word += (int)ssz;
1.124     kristaps  440:                        continue;
1.203     schwarze  441:                }
1.124     kristaps  442:
1.184     kristaps  443:                word++;
                    444:                esc = mandoc_escape(&word, &seq, &sz);
                    445:                if (ESCAPE_ERROR == esc)
1.224     schwarze  446:                        continue;
1.124     kristaps  447:
1.184     kristaps  448:                switch (esc) {
1.222     schwarze  449:                case ESCAPE_UNICODE:
1.229     schwarze  450:                        uc = mchars_num2uc(seq + 1, sz - 1);
1.192     kristaps  451:                        break;
1.222     schwarze  452:                case ESCAPE_NUMBERED:
1.233     schwarze  453:                        uc = mchars_num2char(seq, sz);
                    454:                        if (uc < 0)
                    455:                                continue;
1.184     kristaps  456:                        break;
1.222     schwarze  457:                case ESCAPE_SPECIAL:
1.229     schwarze  458:                        if (p->enc == TERMENC_ASCII) {
                    459:                                cp = mchars_spec2str(p->symtab,
                    460:                                    seq, sz, &ssz);
1.232     schwarze  461:                                if (cp != NULL)
1.229     schwarze  462:                                        encode(p, cp, ssz);
                    463:                        } else {
                    464:                                uc = mchars_spec2cp(p->symtab, seq, sz);
1.230     schwarze  465:                                if (uc > 0)
                    466:                                        encode1(p, uc);
1.229     schwarze  467:                        }
1.233     schwarze  468:                        continue;
1.222     schwarze  469:                case ESCAPE_FONTBOLD:
1.125     kristaps  470:                        term_fontrepl(p, TERMFONT_BOLD);
1.233     schwarze  471:                        continue;
1.222     schwarze  472:                case ESCAPE_FONTITALIC:
1.125     kristaps  473:                        term_fontrepl(p, TERMFONT_UNDER);
1.233     schwarze  474:                        continue;
1.222     schwarze  475:                case ESCAPE_FONTBI:
1.209     schwarze  476:                        term_fontrepl(p, TERMFONT_BI);
1.233     schwarze  477:                        continue;
1.222     schwarze  478:                case ESCAPE_FONT:
1.195     kristaps  479:                        /* FALLTHROUGH */
1.222     schwarze  480:                case ESCAPE_FONTROMAN:
1.125     kristaps  481:                        term_fontrepl(p, TERMFONT_NONE);
1.233     schwarze  482:                        continue;
1.222     schwarze  483:                case ESCAPE_FONTPREV:
1.125     kristaps  484:                        term_fontlast(p);
1.233     schwarze  485:                        continue;
1.222     schwarze  486:                case ESCAPE_NOSPACE:
1.203     schwarze  487:                        if (TERMP_SKIPCHAR & p->flags)
                    488:                                p->flags &= ~TERMP_SKIPCHAR;
                    489:                        else if ('\0' == *word)
1.237   ! schwarze  490:                                p->flags |= (TERMP_NOSPACE | TERMP_NONEWLINE);
1.233     schwarze  491:                        continue;
1.222     schwarze  492:                case ESCAPE_SKIPCHAR:
1.203     schwarze  493:                        p->flags |= TERMP_SKIPCHAR;
1.233     schwarze  494:                        continue;
1.124     kristaps  495:                default:
1.233     schwarze  496:                        continue;
                    497:                }
                    498:
                    499:                /*
                    500:                 * Common handling for Unicode and numbered
                    501:                 * character escape sequences.
                    502:                 */
                    503:
                    504:                if (p->enc == TERMENC_ASCII) {
                    505:                        cp = ascii_uc2str(uc);
                    506:                        encode(p, cp, strlen(cp));
                    507:                } else {
                    508:                        if ((uc < 0x20 && uc != 0x09) ||
                    509:                            (uc > 0x7E && uc < 0xA0))
                    510:                                uc = 0xFFFD;
                    511:                        encode1(p, uc);
1.124     kristaps  512:                }
                    513:        }
1.214     schwarze  514:        p->flags &= ~TERMP_NBRWORD;
1.65      kristaps  515: }
                    516:
1.71      kristaps  517: static void
1.210     schwarze  518: adjbuf(struct termp *p, size_t sz)
1.51      kristaps  519: {
                    520:
1.125     kristaps  521:        if (0 == p->maxcols)
                    522:                p->maxcols = 1024;
                    523:        while (sz >= p->maxcols)
                    524:                p->maxcols <<= 2;
                    525:
1.223     schwarze  526:        p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int));
1.51      kristaps  527: }
                    528:
1.79      kristaps  529: static void
1.125     kristaps  530: bufferc(struct termp *p, char c)
                    531: {
                    532:
                    533:        if (p->col + 1 >= p->maxcols)
                    534:                adjbuf(p, p->col + 1);
                    535:
1.188     kristaps  536:        p->buf[p->col++] = c;
1.125     kristaps  537: }
                    538:
1.194     kristaps  539: /*
                    540:  * See encode().
                    541:  * Do this for a single (probably unicode) value.
                    542:  * Does not check for non-decorated glyphs.
                    543:  */
                    544: static void
                    545: encode1(struct termp *p, int c)
                    546: {
                    547:        enum termfont     f;
                    548:
1.203     schwarze  549:        if (TERMP_SKIPCHAR & p->flags) {
                    550:                p->flags &= ~TERMP_SKIPCHAR;
                    551:                return;
                    552:        }
                    553:
1.209     schwarze  554:        if (p->col + 6 >= p->maxcols)
                    555:                adjbuf(p, p->col + 6);
1.194     kristaps  556:
                    557:        f = term_fonttop(p);
                    558:
1.209     schwarze  559:        if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
1.194     kristaps  560:                p->buf[p->col++] = '_';
1.209     schwarze  561:                p->buf[p->col++] = 8;
                    562:        }
                    563:        if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
                    564:                if (ASCII_HYPH == c)
                    565:                        p->buf[p->col++] = '-';
                    566:                else
                    567:                        p->buf[p->col++] = c;
                    568:                p->buf[p->col++] = 8;
                    569:        }
1.194     kristaps  570:        p->buf[p->col++] = c;
                    571: }
                    572:
1.125     kristaps  573: static void
                    574: encode(struct termp *p, const char *word, size_t sz)
                    575: {
1.210     schwarze  576:        size_t            i;
1.188     kristaps  577:
1.203     schwarze  578:        if (TERMP_SKIPCHAR & p->flags) {
                    579:                p->flags &= ~TERMP_SKIPCHAR;
                    580:                return;
                    581:        }
                    582:
1.125     kristaps  583:        /*
                    584:         * Encode and buffer a string of characters.  If the current
                    585:         * font mode is unset, buffer directly, else encode then buffer
                    586:         * character by character.
                    587:         */
                    588:
1.209     schwarze  589:        if (TERMFONT_NONE == term_fonttop(p)) {
1.222     schwarze  590:                if (p->col + sz >= p->maxcols)
1.210     schwarze  591:                        adjbuf(p, p->col + sz);
                    592:                for (i = 0; i < sz; i++)
1.188     kristaps  593:                        p->buf[p->col++] = word[i];
1.125     kristaps  594:                return;
                    595:        }
                    596:
1.165     kristaps  597:        /* Pre-buffer, assuming worst-case. */
                    598:
1.210     schwarze  599:        if (p->col + 1 + (sz * 5) >= p->maxcols)
                    600:                adjbuf(p, p->col + 1 + (sz * 5));
1.165     kristaps  601:
1.210     schwarze  602:        for (i = 0; i < sz; i++) {
1.209     schwarze  603:                if (ASCII_HYPH == word[i] ||
                    604:                    isgraph((unsigned char)word[i]))
                    605:                        encode1(p, word[i]);
1.125     kristaps  606:                else
1.188     kristaps  607:                        p->buf[p->col++] = word[i];
1.79      kristaps  608:        }
1.219     schwarze  609: }
                    610:
                    611: void
                    612: term_setwidth(struct termp *p, const char *wstr)
                    613: {
                    614:        struct roffsu    su;
                    615:        size_t           width;
                    616:        int              iop;
                    617:
1.220     schwarze  618:        iop = 0;
                    619:        width = 0;
1.219     schwarze  620:        if (NULL != wstr) {
                    621:                switch (*wstr) {
1.222     schwarze  622:                case '+':
1.219     schwarze  623:                        iop = 1;
                    624:                        wstr++;
                    625:                        break;
1.222     schwarze  626:                case '-':
1.219     schwarze  627:                        iop = -1;
                    628:                        wstr++;
                    629:                        break;
                    630:                default:
                    631:                        break;
                    632:                }
1.220     schwarze  633:                if (a2roffsu(wstr, &su, SCALE_MAX))
                    634:                        width = term_hspan(p, &su);
                    635:                else
1.219     schwarze  636:                        iop = 0;
                    637:        }
                    638:        (*p->setwidth)(p, iop, width);
1.79      kristaps  639: }
1.106     kristaps  640:
1.107     kristaps  641: size_t
1.149     kristaps  642: term_len(const struct termp *p, size_t sz)
                    643: {
                    644:
                    645:        return((*p->width)(p, ' ') * sz);
                    646: }
                    647:
1.203     schwarze  648: static size_t
                    649: cond_width(const struct termp *p, int c, int *skip)
                    650: {
                    651:
                    652:        if (*skip) {
                    653:                (*skip) = 0;
                    654:                return(0);
                    655:        } else
                    656:                return((*p->width)(p, c));
                    657: }
1.149     kristaps  658:
                    659: size_t
                    660: term_strlen(const struct termp *p, const char *cp)
                    661: {
1.184     kristaps  662:        size_t           sz, rsz, i;
1.233     schwarze  663:        int              ssz, skip, uc;
1.171     kristaps  664:        const char      *seq, *rhs;
1.196     kristaps  665:        enum mandoc_esc  esc;
1.216     schwarze  666:        static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
                    667:                        ASCII_BREAK, '\0' };
1.171     kristaps  668:
1.184     kristaps  669:        /*
                    670:         * Account for escaped sequences within string length
                    671:         * calculations.  This follows the logic in term_word() as we
                    672:         * must calculate the width of produced strings.
                    673:         */
                    674:
                    675:        sz = 0;
1.203     schwarze  676:        skip = 0;
1.189     kristaps  677:        while ('\0' != *cp) {
                    678:                rsz = strcspn(cp, rej);
                    679:                for (i = 0; i < rsz; i++)
1.203     schwarze  680:                        sz += cond_width(p, *cp++, &skip);
1.189     kristaps  681:
1.184     kristaps  682:                switch (*cp) {
1.222     schwarze  683:                case '\\':
1.189     kristaps  684:                        cp++;
1.196     kristaps  685:                        esc = mandoc_escape(&cp, &seq, &ssz);
                    686:                        if (ESCAPE_ERROR == esc)
1.224     schwarze  687:                                continue;
1.196     kristaps  688:
                    689:                        rhs = NULL;
                    690:
                    691:                        switch (esc) {
1.222     schwarze  692:                        case ESCAPE_UNICODE:
1.234     schwarze  693:                                uc = mchars_num2uc(seq + 1, ssz - 1);
1.194     kristaps  694:                                break;
1.222     schwarze  695:                        case ESCAPE_NUMBERED:
1.233     schwarze  696:                                uc = mchars_num2char(seq, ssz);
                    697:                                if (uc < 0)
                    698:                                        continue;
1.171     kristaps  699:                                break;
1.222     schwarze  700:                        case ESCAPE_SPECIAL:
1.233     schwarze  701:                                if (p->enc == TERMENC_ASCII) {
1.229     schwarze  702:                                        rhs = mchars_spec2str(p->symtab,
                    703:                                            seq, ssz, &rsz);
1.233     schwarze  704:                                        if (rhs != NULL)
                    705:                                                break;
                    706:                                } else {
                    707:                                        uc = mchars_spec2cp(p->symtab,
1.229     schwarze  708:                                            seq, ssz);
1.233     schwarze  709:                                        if (uc > 0)
                    710:                                                sz += cond_width(p, uc, &skip);
1.229     schwarze  711:                                }
1.233     schwarze  712:                                continue;
1.222     schwarze  713:                        case ESCAPE_SKIPCHAR:
1.203     schwarze  714:                                skip = 1;
1.233     schwarze  715:                                continue;
1.171     kristaps  716:                        default:
1.233     schwarze  717:                                continue;
1.171     kristaps  718:                        }
1.149     kristaps  719:
1.233     schwarze  720:                        /*
                    721:                         * Common handling for Unicode and numbered
                    722:                         * character escape sequences.
                    723:                         */
                    724:
                    725:                        if (rhs == NULL) {
                    726:                                if (p->enc == TERMENC_ASCII) {
                    727:                                        rhs = ascii_uc2str(uc);
                    728:                                        rsz = strlen(rhs);
                    729:                                } else {
                    730:                                        if ((uc < 0x20 && uc != 0x09) ||
                    731:                                            (uc > 0x7E && uc < 0xA0))
                    732:                                                uc = 0xFFFD;
                    733:                                        sz += cond_width(p, uc, &skip);
                    734:                                        continue;
                    735:                                }
                    736:                        }
1.184     kristaps  737:
1.203     schwarze  738:                        if (skip) {
                    739:                                skip = 0;
                    740:                                break;
                    741:                        }
1.233     schwarze  742:
                    743:                        /*
                    744:                         * Common handling for all escape sequences
                    745:                         * printing more than one character.
                    746:                         */
1.203     schwarze  747:
1.184     kristaps  748:                        for (i = 0; i < rsz; i++)
                    749:                                sz += (*p->width)(p, *rhs++);
                    750:                        break;
1.222     schwarze  751:                case ASCII_NBRSP:
1.203     schwarze  752:                        sz += cond_width(p, ' ', &skip);
1.176     kristaps  753:                        cp++;
1.184     kristaps  754:                        break;
1.222     schwarze  755:                case ASCII_HYPH:
1.203     schwarze  756:                        sz += cond_width(p, '-', &skip);
1.176     kristaps  757:                        cp++;
1.216     schwarze  758:                        /* FALLTHROUGH */
1.222     schwarze  759:                case ASCII_BREAK:
1.184     kristaps  760:                        break;
                    761:                default:
                    762:                        break;
                    763:                }
1.189     kristaps  764:        }
1.149     kristaps  765:
                    766:        return(sz);
                    767: }
                    768:
                    769: size_t
                    770: term_vspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps  771: {
                    772:        double           r;
                    773:
1.107     kristaps  774:        switch (su->unit) {
1.222     schwarze  775:        case SCALE_CM:
1.225     schwarze  776:                r = su->scale * 2.0;
1.106     kristaps  777:                break;
1.222     schwarze  778:        case SCALE_IN:
1.225     schwarze  779:                r = su->scale * 6.0;
1.106     kristaps  780:                break;
1.222     schwarze  781:        case SCALE_PC:
1.107     kristaps  782:                r = su->scale;
1.106     kristaps  783:                break;
1.222     schwarze  784:        case SCALE_PT:
1.225     schwarze  785:                r = su->scale / 8.0;
1.106     kristaps  786:                break;
1.222     schwarze  787:        case SCALE_MM:
1.225     schwarze  788:                r = su->scale / 1000.0;
1.106     kristaps  789:                break;
1.222     schwarze  790:        case SCALE_VS:
1.107     kristaps  791:                r = su->scale;
1.106     kristaps  792:                break;
                    793:        default:
1.225     schwarze  794:                r = su->scale - 1.0;
1.106     kristaps  795:                break;
                    796:        }
                    797:
                    798:        if (r < 0.0)
                    799:                r = 0.0;
1.226     schwarze  800:        return((size_t)(r + 0.0005));
1.106     kristaps  801: }
                    802:
1.107     kristaps  803: size_t
1.149     kristaps  804: term_hspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps  805: {
1.156     kristaps  806:        double           v;
1.108     kristaps  807:
1.225     schwarze  808:        v = (*p->hspan)(p, su);
1.156     kristaps  809:        if (v < 0.0)
                    810:                v = 0.0;
1.226     schwarze  811:        return((size_t)(v + 0.0005));
1.106     kristaps  812: }

CVSweb