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

Annotation of mandoc/term.c, Revision 1.260

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

CVSweb