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

Annotation of mandoc/term.c, Revision 1.248

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

CVSweb