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

Annotation of mandoc/term.c, Revision 1.258

1.258   ! schwarze    1: /*     $Id: term.c,v 1.257 2016/04/12 15:30:00 schwarze Exp $ */
1.1       kristaps    2: /*
1.198     schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.258   ! schwarze    4:  * Copyright (c) 2010-2016 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.188     kristaps  144:                while (i < p->col && '\t' == p->buf[i]) {
1.154     kristaps  145:                        vend = (vis / p->tabwidth + 1) * p->tabwidth;
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.221     schwarze  195:                        if (TERMP_BRIND & p->flags) {
1.240     schwarze  196:                                vbl = p->rmargin;
                    197:                                vend += p->rmargin;
                    198:                                vend -= p->offset;
1.201     schwarze  199:                        } else
1.136     schwarze  200:                                vbl = p->offset;
1.205     schwarze  201:
                    202:                        /* use pending tabs on the new line */
                    203:
                    204:                        if (0 < ntab)
                    205:                                vbl += ntab * p->tabwidth;
1.130     kristaps  206:
1.212     schwarze  207:                        /*
                    208:                         * Remove the p->overstep width.
                    209:                         * Again, if p->overstep is negative,
                    210:                         * sign extension does the right thing.
                    211:                         */
1.130     kristaps  212:
1.174     schwarze  213:                        bp += (size_t)p->overstep;
1.129     kristaps  214:                        p->overstep = 0;
1.71      kristaps  215:                }
1.138     schwarze  216:
1.130     kristaps  217:                /* Write out the [remaining] word. */
1.188     kristaps  218:                for ( ; i < p->col; i++) {
1.140     kristaps  219:                        if (vend > bp && jhy > 0 && i > jhy)
                    220:                                break;
1.138     schwarze  221:                        if ('\t' == p->buf[i])
                    222:                                break;
1.136     schwarze  223:                        if (' ' == p->buf[i]) {
1.164     kristaps  224:                                j = i;
1.228     kristaps  225:                                while (i < p->col && ' ' == p->buf[i])
1.136     schwarze  226:                                        i++;
1.210     schwarze  227:                                dv = (i - j) * (*p->width)(p, ' ');
1.172     schwarze  228:                                vbl += dv;
                    229:                                vend += dv;
1.71      kristaps  230:                                break;
1.136     schwarze  231:                        }
                    232:                        if (ASCII_NBRSP == p->buf[i]) {
1.153     kristaps  233:                                vbl += (*p->width)(p, ' ');
1.136     schwarze  234:                                continue;
                    235:                        }
1.216     schwarze  236:                        if (ASCII_BREAK == p->buf[i])
                    237:                                continue;
1.130     kristaps  238:
1.136     schwarze  239:                        /*
                    240:                         * Now we definitely know there will be
                    241:                         * printable characters to output,
                    242:                         * so write preceding white space now.
                    243:                         */
                    244:                        if (vbl) {
1.146     kristaps  245:                                (*p->advance)(p, vbl);
1.139     schwarze  246:                                p->viscol += vbl;
1.136     schwarze  247:                                vbl = 0;
1.200     schwarze  248:                        }
                    249:
                    250:                        (*p->letter)(p, p->buf[i]);
                    251:                        if (8 == p->buf[i])
                    252:                                p->viscol -= (*p->width)(p, p->buf[i-1]);
1.222     schwarze  253:                        else
1.153     kristaps  254:                                p->viscol += (*p->width)(p, p->buf[i]);
1.136     schwarze  255:                }
                    256:                vis = vend;
1.71      kristaps  257:        }
1.168     schwarze  258:
                    259:        /*
                    260:         * If there was trailing white space, it was not printed;
                    261:         * so reset the cursor position accordingly.
                    262:         */
1.235     schwarze  263:        if (vis > vbl)
1.200     schwarze  264:                vis -= vbl;
1.235     schwarze  265:        else
                    266:                vis = 0;
1.111     kristaps  267:
1.91      kristaps  268:        p->col = 0;
1.129     kristaps  269:        p->overstep = 0;
1.248     schwarze  270:        p->flags &= ~(TERMP_BACKAFTER | TERMP_BACKBEFORE);
1.15      kristaps  271:
1.91      kristaps  272:        if ( ! (TERMP_NOBREAK & p->flags)) {
1.139     schwarze  273:                p->viscol = 0;
1.146     kristaps  274:                (*p->endline)(p);
1.15      kristaps  275:                return;
1.71      kristaps  276:        }
1.15      kristaps  277:
1.91      kristaps  278:        if (TERMP_HANG & p->flags) {
1.242     schwarze  279:                p->overstep += (int)(p->offset + vis - p->rmargin +
1.222     schwarze  280:                    p->trailspace * (*p->width)(p, ' '));
1.91      kristaps  281:
                    282:                /*
1.92      kristaps  283:                 * If we have overstepped the margin, temporarily move
                    284:                 * it to the right and flag the rest of the line to be
                    285:                 * shorter.
1.212     schwarze  286:                 * If there is a request to keep the columns together,
                    287:                 * allow negative overstep when the column is not full.
1.91      kristaps  288:                 */
1.212     schwarze  289:                if (p->trailspace && p->overstep < 0)
1.129     kristaps  290:                        p->overstep = 0;
1.200     schwarze  291:                return;
1.91      kristaps  292:
                    293:        } else if (TERMP_DANGLE & p->flags)
                    294:                return;
1.250     schwarze  295:
                    296:        /* Trailing whitespace is significant in some columns. */
                    297:        if (vis && vbl && (TERMP_BRTRSP & p->flags))
                    298:                vis += vbl;
1.15      kristaps  299:
1.200     schwarze  300:        /* If the column was overrun, break the line. */
1.211     schwarze  301:        if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
1.146     kristaps  302:                (*p->endline)(p);
1.200     schwarze  303:                p->viscol = 0;
1.91      kristaps  304:        }
1.15      kristaps  305: }
                    306:
1.222     schwarze  307: /*
1.71      kristaps  308:  * A newline only breaks an existing line; it won't assert vertical
                    309:  * space.  All data in the output buffer is flushed prior to the newline
                    310:  * assertion.
                    311:  */
                    312: void
                    313: term_newln(struct termp *p)
1.15      kristaps  314: {
                    315:
1.71      kristaps  316:        p->flags |= TERMP_NOSPACE;
1.200     schwarze  317:        if (p->col || p->viscol)
                    318:                term_flushln(p);
1.16      kristaps  319: }
                    320:
1.71      kristaps  321: /*
                    322:  * Asserts a vertical space (a full, empty line-break between lines).
                    323:  * Note that if used twice, this will cause two blank spaces and so on.
                    324:  * All data in the output buffer is flushed prior to the newline
                    325:  * assertion.
                    326:  */
                    327: void
                    328: term_vspace(struct termp *p)
1.16      kristaps  329: {
                    330:
1.62      kristaps  331:        term_newln(p);
1.139     schwarze  332:        p->viscol = 0;
1.202     schwarze  333:        if (0 < p->skipvsp)
                    334:                p->skipvsp--;
                    335:        else
                    336:                (*p->endline)(p);
1.16      kristaps  337: }
                    338:
1.238     schwarze  339: /* Swap current and previous font; for \fP and .ft P */
1.125     kristaps  340: void
                    341: term_fontlast(struct termp *p)
                    342: {
                    343:        enum termfont    f;
                    344:
                    345:        f = p->fontl;
                    346:        p->fontl = p->fontq[p->fonti];
                    347:        p->fontq[p->fonti] = f;
                    348: }
                    349:
1.238     schwarze  350: /* Set font, save current, discard previous; for \f, .ft, .B etc. */
1.125     kristaps  351: void
                    352: term_fontrepl(struct termp *p, enum termfont f)
                    353: {
                    354:
                    355:        p->fontl = p->fontq[p->fonti];
                    356:        p->fontq[p->fonti] = f;
                    357: }
                    358:
1.238     schwarze  359: /* Set font, save previous. */
1.125     kristaps  360: void
                    361: term_fontpush(struct termp *p, enum termfont f)
                    362: {
                    363:
                    364:        p->fontl = p->fontq[p->fonti];
1.238     schwarze  365:        if (++p->fonti == p->fontsz) {
                    366:                p->fontsz += 8;
                    367:                p->fontq = mandoc_reallocarray(p->fontq,
1.256     schwarze  368:                    p->fontsz, sizeof(*p->fontq));
1.238     schwarze  369:        }
                    370:        p->fontq[p->fonti] = f;
1.125     kristaps  371: }
                    372:
1.238     schwarze  373: /* Flush to make the saved pointer current again. */
1.125     kristaps  374: void
1.244     schwarze  375: term_fontpopq(struct termp *p, int i)
1.125     kristaps  376: {
                    377:
1.244     schwarze  378:        assert(i >= 0);
                    379:        if (p->fonti > i)
                    380:                p->fonti = i;
1.125     kristaps  381: }
1.94      kristaps  382:
1.238     schwarze  383: /* Pop one font off the stack. */
1.125     kristaps  384: void
                    385: term_fontpop(struct termp *p)
                    386: {
                    387:
                    388:        assert(p->fonti);
                    389:        p->fonti--;
1.17      kristaps  390: }
                    391:
1.71      kristaps  392: /*
                    393:  * Handle pwords, partial words, which may be either a single word or a
                    394:  * phrase that cannot be broken down (such as a literal string).  This
                    395:  * handles word styling.
                    396:  */
1.86      kristaps  397: void
                    398: term_word(struct termp *p, const char *word)
1.65      kristaps  399: {
1.214     schwarze  400:        const char       nbrsp[2] = { ASCII_NBRSP, 0 };
1.191     kristaps  401:        const char      *seq, *cp;
1.194     kristaps  402:        int              sz, uc;
1.124     kristaps  403:        size_t           ssz;
1.184     kristaps  404:        enum mandoc_esc  esc;
1.100     kristaps  405:
1.133     kristaps  406:        if ( ! (TERMP_NOSPACE & p->flags)) {
1.151     schwarze  407:                if ( ! (TERMP_KEEP & p->flags)) {
1.133     kristaps  408:                        bufferc(p, ' ');
1.151     schwarze  409:                        if (TERMP_SENTENCE & p->flags)
                    410:                                bufferc(p, ' ');
                    411:                } else
                    412:                        bufferc(p, ASCII_NBRSP);
1.133     kristaps  413:        }
1.207     schwarze  414:        if (TERMP_PREKEEP & p->flags)
                    415:                p->flags |= TERMP_KEEP;
1.65      kristaps  416:
1.71      kristaps  417:        if ( ! (p->flags & TERMP_NONOSPACE))
                    418:                p->flags &= ~TERMP_NOSPACE;
1.166     kristaps  419:        else
                    420:                p->flags |= TERMP_NOSPACE;
1.133     kristaps  421:
1.237     schwarze  422:        p->flags &= ~(TERMP_SENTENCE | TERMP_NONEWLINE);
1.245     schwarze  423:        p->skipvsp = 0;
1.65      kristaps  424:
1.184     kristaps  425:        while ('\0' != *word) {
1.203     schwarze  426:                if ('\\' != *word) {
1.214     schwarze  427:                        if (TERMP_NBRWORD & p->flags) {
                    428:                                if (' ' == *word) {
                    429:                                        encode(p, nbrsp, 1);
                    430:                                        word++;
                    431:                                        continue;
                    432:                                }
                    433:                                ssz = strcspn(word, "\\ ");
                    434:                        } else
                    435:                                ssz = strcspn(word, "\\");
1.162     kristaps  436:                        encode(p, word, ssz);
1.203     schwarze  437:                        word += (int)ssz;
1.124     kristaps  438:                        continue;
1.203     schwarze  439:                }
1.124     kristaps  440:
1.184     kristaps  441:                word++;
                    442:                esc = mandoc_escape(&word, &seq, &sz);
                    443:                if (ESCAPE_ERROR == esc)
1.224     schwarze  444:                        continue;
1.124     kristaps  445:
1.184     kristaps  446:                switch (esc) {
1.222     schwarze  447:                case ESCAPE_UNICODE:
1.229     schwarze  448:                        uc = mchars_num2uc(seq + 1, sz - 1);
1.192     kristaps  449:                        break;
1.222     schwarze  450:                case ESCAPE_NUMBERED:
1.233     schwarze  451:                        uc = mchars_num2char(seq, sz);
                    452:                        if (uc < 0)
                    453:                                continue;
1.184     kristaps  454:                        break;
1.222     schwarze  455:                case ESCAPE_SPECIAL:
1.229     schwarze  456:                        if (p->enc == TERMENC_ASCII) {
1.254     schwarze  457:                                cp = mchars_spec2str(seq, sz, &ssz);
1.232     schwarze  458:                                if (cp != NULL)
1.229     schwarze  459:                                        encode(p, cp, ssz);
                    460:                        } else {
1.254     schwarze  461:                                uc = mchars_spec2cp(seq, sz);
1.230     schwarze  462:                                if (uc > 0)
                    463:                                        encode1(p, uc);
1.229     schwarze  464:                        }
1.233     schwarze  465:                        continue;
1.222     schwarze  466:                case ESCAPE_FONTBOLD:
1.125     kristaps  467:                        term_fontrepl(p, TERMFONT_BOLD);
1.233     schwarze  468:                        continue;
1.222     schwarze  469:                case ESCAPE_FONTITALIC:
1.125     kristaps  470:                        term_fontrepl(p, TERMFONT_UNDER);
1.233     schwarze  471:                        continue;
1.222     schwarze  472:                case ESCAPE_FONTBI:
1.209     schwarze  473:                        term_fontrepl(p, TERMFONT_BI);
1.233     schwarze  474:                        continue;
1.222     schwarze  475:                case ESCAPE_FONT:
                    476:                case ESCAPE_FONTROMAN:
1.125     kristaps  477:                        term_fontrepl(p, TERMFONT_NONE);
1.233     schwarze  478:                        continue;
1.222     schwarze  479:                case ESCAPE_FONTPREV:
1.125     kristaps  480:                        term_fontlast(p);
1.233     schwarze  481:                        continue;
1.222     schwarze  482:                case ESCAPE_NOSPACE:
1.248     schwarze  483:                        if (p->flags & TERMP_BACKAFTER)
                    484:                                p->flags &= ~TERMP_BACKAFTER;
                    485:                        else if (*word == '\0')
1.237     schwarze  486:                                p->flags |= (TERMP_NOSPACE | TERMP_NONEWLINE);
1.233     schwarze  487:                        continue;
1.222     schwarze  488:                case ESCAPE_SKIPCHAR:
1.248     schwarze  489:                        p->flags |= TERMP_BACKAFTER;
1.233     schwarze  490:                        continue;
1.243     schwarze  491:                case ESCAPE_OVERSTRIKE:
                    492:                        cp = seq + sz;
                    493:                        while (seq < cp) {
                    494:                                if (*seq == '\\') {
                    495:                                        mandoc_escape(&seq, NULL, NULL);
                    496:                                        continue;
                    497:                                }
                    498:                                encode1(p, *seq++);
1.248     schwarze  499:                                if (seq < cp) {
                    500:                                        if (p->flags & TERMP_BACKBEFORE)
                    501:                                                p->flags |= TERMP_BACKAFTER;
                    502:                                        else
                    503:                                                p->flags |= TERMP_BACKBEFORE;
                    504:                                }
1.243     schwarze  505:                        }
1.249     schwarze  506:                        /* Trim trailing backspace/blank pair. */
1.258   ! schwarze  507:                        if (p->col > 2 &&
        !           508:                            (p->buf[p->col - 1] == ' ' ||
        !           509:                             p->buf[p->col - 1] == '\t'))
1.249     schwarze  510:                                p->col -= 2;
1.248     schwarze  511:                        continue;
1.124     kristaps  512:                default:
1.233     schwarze  513:                        continue;
                    514:                }
                    515:
                    516:                /*
                    517:                 * Common handling for Unicode and numbered
                    518:                 * character escape sequences.
                    519:                 */
                    520:
                    521:                if (p->enc == TERMENC_ASCII) {
                    522:                        cp = ascii_uc2str(uc);
                    523:                        encode(p, cp, strlen(cp));
                    524:                } else {
                    525:                        if ((uc < 0x20 && uc != 0x09) ||
                    526:                            (uc > 0x7E && uc < 0xA0))
                    527:                                uc = 0xFFFD;
                    528:                        encode1(p, uc);
1.124     kristaps  529:                }
                    530:        }
1.214     schwarze  531:        p->flags &= ~TERMP_NBRWORD;
1.65      kristaps  532: }
                    533:
1.71      kristaps  534: static void
1.210     schwarze  535: adjbuf(struct termp *p, size_t sz)
1.51      kristaps  536: {
                    537:
1.125     kristaps  538:        if (0 == p->maxcols)
                    539:                p->maxcols = 1024;
                    540:        while (sz >= p->maxcols)
                    541:                p->maxcols <<= 2;
                    542:
1.223     schwarze  543:        p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int));
1.51      kristaps  544: }
                    545:
1.79      kristaps  546: static void
1.125     kristaps  547: bufferc(struct termp *p, char c)
                    548: {
                    549:
                    550:        if (p->col + 1 >= p->maxcols)
                    551:                adjbuf(p, p->col + 1);
                    552:
1.188     kristaps  553:        p->buf[p->col++] = c;
1.125     kristaps  554: }
                    555:
1.194     kristaps  556: /*
                    557:  * See encode().
                    558:  * Do this for a single (probably unicode) value.
                    559:  * Does not check for non-decorated glyphs.
                    560:  */
                    561: static void
                    562: encode1(struct termp *p, int c)
                    563: {
                    564:        enum termfont     f;
                    565:
1.248     schwarze  566:        if (p->col + 7 >= p->maxcols)
                    567:                adjbuf(p, p->col + 7);
1.194     kristaps  568:
1.255     schwarze  569:        f = (c == ASCII_HYPH || c > 127 || isgraph(c)) ?
1.248     schwarze  570:            p->fontq[p->fonti] : TERMFONT_NONE;
1.194     kristaps  571:
1.248     schwarze  572:        if (p->flags & TERMP_BACKBEFORE) {
1.258   ! schwarze  573:                if (p->buf[p->col - 1] == ' ' || p->buf[p->col - 1] == '\t')
1.249     schwarze  574:                        p->col--;
                    575:                else
                    576:                        p->buf[p->col++] = 8;
1.248     schwarze  577:                p->flags &= ~TERMP_BACKBEFORE;
                    578:        }
1.209     schwarze  579:        if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
1.194     kristaps  580:                p->buf[p->col++] = '_';
1.209     schwarze  581:                p->buf[p->col++] = 8;
                    582:        }
                    583:        if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
                    584:                if (ASCII_HYPH == c)
                    585:                        p->buf[p->col++] = '-';
                    586:                else
                    587:                        p->buf[p->col++] = c;
                    588:                p->buf[p->col++] = 8;
                    589:        }
1.194     kristaps  590:        p->buf[p->col++] = c;
1.248     schwarze  591:        if (p->flags & TERMP_BACKAFTER) {
                    592:                p->flags |= TERMP_BACKBEFORE;
                    593:                p->flags &= ~TERMP_BACKAFTER;
                    594:        }
1.194     kristaps  595: }
                    596:
1.125     kristaps  597: static void
                    598: encode(struct termp *p, const char *word, size_t sz)
                    599: {
1.210     schwarze  600:        size_t            i;
1.188     kristaps  601:
1.248     schwarze  602:        if (p->col + 2 + (sz * 5) >= p->maxcols)
                    603:                adjbuf(p, p->col + 2 + (sz * 5));
1.165     kristaps  604:
1.210     schwarze  605:        for (i = 0; i < sz; i++) {
1.209     schwarze  606:                if (ASCII_HYPH == word[i] ||
                    607:                    isgraph((unsigned char)word[i]))
                    608:                        encode1(p, word[i]);
1.125     kristaps  609:                else
1.188     kristaps  610:                        p->buf[p->col++] = word[i];
1.79      kristaps  611:        }
1.219     schwarze  612: }
                    613:
                    614: void
                    615: term_setwidth(struct termp *p, const char *wstr)
                    616: {
                    617:        struct roffsu    su;
1.247     schwarze  618:        int              iop, width;
1.219     schwarze  619:
1.220     schwarze  620:        iop = 0;
                    621:        width = 0;
1.219     schwarze  622:        if (NULL != wstr) {
                    623:                switch (*wstr) {
1.222     schwarze  624:                case '+':
1.219     schwarze  625:                        iop = 1;
                    626:                        wstr++;
                    627:                        break;
1.222     schwarze  628:                case '-':
1.219     schwarze  629:                        iop = -1;
                    630:                        wstr++;
                    631:                        break;
                    632:                default:
                    633:                        break;
                    634:                }
1.220     schwarze  635:                if (a2roffsu(wstr, &su, SCALE_MAX))
                    636:                        width = term_hspan(p, &su);
                    637:                else
1.219     schwarze  638:                        iop = 0;
                    639:        }
                    640:        (*p->setwidth)(p, iop, width);
1.79      kristaps  641: }
1.106     kristaps  642:
1.107     kristaps  643: size_t
1.149     kristaps  644: term_len(const struct termp *p, size_t sz)
                    645: {
                    646:
1.252     schwarze  647:        return (*p->width)(p, ' ') * sz;
1.149     kristaps  648: }
                    649:
1.203     schwarze  650: static size_t
                    651: cond_width(const struct termp *p, int c, int *skip)
                    652: {
                    653:
                    654:        if (*skip) {
                    655:                (*skip) = 0;
1.252     schwarze  656:                return 0;
1.203     schwarze  657:        } else
1.252     schwarze  658:                return (*p->width)(p, c);
1.203     schwarze  659: }
1.149     kristaps  660:
                    661: size_t
                    662: term_strlen(const struct termp *p, const char *cp)
                    663: {
1.184     kristaps  664:        size_t           sz, rsz, i;
1.233     schwarze  665:        int              ssz, skip, uc;
1.171     kristaps  666:        const char      *seq, *rhs;
1.196     kristaps  667:        enum mandoc_esc  esc;
1.216     schwarze  668:        static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
                    669:                        ASCII_BREAK, '\0' };
1.171     kristaps  670:
1.184     kristaps  671:        /*
                    672:         * Account for escaped sequences within string length
                    673:         * calculations.  This follows the logic in term_word() as we
                    674:         * must calculate the width of produced strings.
                    675:         */
                    676:
                    677:        sz = 0;
1.203     schwarze  678:        skip = 0;
1.189     kristaps  679:        while ('\0' != *cp) {
                    680:                rsz = strcspn(cp, rej);
                    681:                for (i = 0; i < rsz; i++)
1.203     schwarze  682:                        sz += cond_width(p, *cp++, &skip);
1.189     kristaps  683:
1.184     kristaps  684:                switch (*cp) {
1.222     schwarze  685:                case '\\':
1.189     kristaps  686:                        cp++;
1.196     kristaps  687:                        esc = mandoc_escape(&cp, &seq, &ssz);
                    688:                        if (ESCAPE_ERROR == esc)
1.224     schwarze  689:                                continue;
1.196     kristaps  690:
                    691:                        rhs = NULL;
                    692:
                    693:                        switch (esc) {
1.222     schwarze  694:                        case ESCAPE_UNICODE:
1.234     schwarze  695:                                uc = mchars_num2uc(seq + 1, ssz - 1);
1.194     kristaps  696:                                break;
1.222     schwarze  697:                        case ESCAPE_NUMBERED:
1.233     schwarze  698:                                uc = mchars_num2char(seq, ssz);
                    699:                                if (uc < 0)
                    700:                                        continue;
1.171     kristaps  701:                                break;
1.222     schwarze  702:                        case ESCAPE_SPECIAL:
1.233     schwarze  703:                                if (p->enc == TERMENC_ASCII) {
1.254     schwarze  704:                                        rhs = mchars_spec2str(seq, ssz, &rsz);
1.233     schwarze  705:                                        if (rhs != NULL)
                    706:                                                break;
                    707:                                } else {
1.254     schwarze  708:                                        uc = mchars_spec2cp(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.243     schwarze  715:                                continue;
                    716:                        case ESCAPE_OVERSTRIKE:
                    717:                                rsz = 0;
                    718:                                rhs = seq + ssz;
                    719:                                while (seq < rhs) {
                    720:                                        if (*seq == '\\') {
                    721:                                                mandoc_escape(&seq, NULL, NULL);
                    722:                                                continue;
                    723:                                        }
                    724:                                        i = (*p->width)(p, *seq++);
                    725:                                        if (rsz < i)
                    726:                                                rsz = i;
                    727:                                }
                    728:                                sz += rsz;
1.233     schwarze  729:                                continue;
1.171     kristaps  730:                        default:
1.233     schwarze  731:                                continue;
1.171     kristaps  732:                        }
1.149     kristaps  733:
1.233     schwarze  734:                        /*
                    735:                         * Common handling for Unicode and numbered
                    736:                         * character escape sequences.
                    737:                         */
                    738:
                    739:                        if (rhs == NULL) {
                    740:                                if (p->enc == TERMENC_ASCII) {
                    741:                                        rhs = ascii_uc2str(uc);
                    742:                                        rsz = strlen(rhs);
                    743:                                } else {
                    744:                                        if ((uc < 0x20 && uc != 0x09) ||
                    745:                                            (uc > 0x7E && uc < 0xA0))
                    746:                                                uc = 0xFFFD;
                    747:                                        sz += cond_width(p, uc, &skip);
                    748:                                        continue;
                    749:                                }
                    750:                        }
1.184     kristaps  751:
1.203     schwarze  752:                        if (skip) {
                    753:                                skip = 0;
                    754:                                break;
                    755:                        }
1.233     schwarze  756:
                    757:                        /*
                    758:                         * Common handling for all escape sequences
                    759:                         * printing more than one character.
                    760:                         */
1.203     schwarze  761:
1.184     kristaps  762:                        for (i = 0; i < rsz; i++)
                    763:                                sz += (*p->width)(p, *rhs++);
                    764:                        break;
1.222     schwarze  765:                case ASCII_NBRSP:
1.203     schwarze  766:                        sz += cond_width(p, ' ', &skip);
1.176     kristaps  767:                        cp++;
1.184     kristaps  768:                        break;
1.222     schwarze  769:                case ASCII_HYPH:
1.203     schwarze  770:                        sz += cond_width(p, '-', &skip);
1.176     kristaps  771:                        cp++;
1.184     kristaps  772:                        break;
                    773:                default:
                    774:                        break;
                    775:                }
1.189     kristaps  776:        }
1.149     kristaps  777:
1.252     schwarze  778:        return sz;
1.149     kristaps  779: }
                    780:
1.240     schwarze  781: int
1.149     kristaps  782: term_vspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps  783: {
                    784:        double           r;
1.241     schwarze  785:        int              ri;
1.106     kristaps  786:
1.107     kristaps  787:        switch (su->unit) {
1.239     schwarze  788:        case SCALE_BU:
                    789:                r = su->scale / 40.0;
                    790:                break;
1.222     schwarze  791:        case SCALE_CM:
1.239     schwarze  792:                r = su->scale * 6.0 / 2.54;
                    793:                break;
                    794:        case SCALE_FS:
                    795:                r = su->scale * 65536.0 / 40.0;
1.106     kristaps  796:                break;
1.222     schwarze  797:        case SCALE_IN:
1.225     schwarze  798:                r = su->scale * 6.0;
1.106     kristaps  799:                break;
1.239     schwarze  800:        case SCALE_MM:
                    801:                r = su->scale * 0.006;
                    802:                break;
1.222     schwarze  803:        case SCALE_PC:
1.107     kristaps  804:                r = su->scale;
1.106     kristaps  805:                break;
1.222     schwarze  806:        case SCALE_PT:
1.239     schwarze  807:                r = su->scale / 12.0;
1.106     kristaps  808:                break;
1.239     schwarze  809:        case SCALE_EN:
                    810:        case SCALE_EM:
                    811:                r = su->scale * 0.6;
1.106     kristaps  812:                break;
1.222     schwarze  813:        case SCALE_VS:
1.107     kristaps  814:                r = su->scale;
1.106     kristaps  815:                break;
                    816:        default:
1.239     schwarze  817:                abort();
1.106     kristaps  818:        }
1.241     schwarze  819:        ri = r > 0.0 ? r + 0.4995 : r - 0.4995;
1.252     schwarze  820:        return ri < 66 ? ri : 1;
1.106     kristaps  821: }
                    822:
1.247     schwarze  823: /*
                    824:  * Convert a scaling width to basic units, rounding down.
                    825:  */
1.240     schwarze  826: int
1.149     kristaps  827: term_hspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps  828: {
1.108     kristaps  829:
1.252     schwarze  830:        return (*p->hspan)(p, su);
1.106     kristaps  831: }

CVSweb