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

Annotation of mandoc/term.c, Revision 1.243

1.243   ! schwarze    1: /*     $Id: term.c,v 1.242 2014/12/24 23:32:42 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.74      kristaps   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   17:  */
1.128     kristaps   18: #include "config.h"
                     19:
1.126     kristaps   20: #include <sys/types.h>
                     21:
1.1       kristaps   22: #include <assert.h>
1.122     kristaps   23: #include <ctype.h>
1.22      kristaps   24: #include <stdio.h>
1.1       kristaps   25: #include <stdlib.h>
                     26: #include <string.h>
                     27:
1.137     kristaps   28: #include "mandoc.h"
1.218     schwarze   29: #include "mandoc_aux.h"
1.107     kristaps   30: #include "out.h"
1.71      kristaps   31: #include "term.h"
1.105     kristaps   32: #include "main.h"
1.1       kristaps   33:
1.203     schwarze   34: static size_t           cond_width(const struct termp *, int, int *);
1.210     schwarze   35: static void             adjbuf(struct termp *p, size_t);
1.191     kristaps   36: static void             bufferc(struct termp *, char);
                     37: static void             encode(struct termp *, const char *, size_t);
1.194     kristaps   38: static void             encode1(struct termp *, int);
1.11      kristaps   39:
1.222     schwarze   40:
1.145     kristaps   41: void
1.71      kristaps   42: term_free(struct termp *p)
1.14      kristaps   43: {
                     44:
1.231     schwarze   45:        free(p->buf);
1.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.142     kristaps   52:                term_margin foot, const void *arg)
                     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.15      kristaps  268:
1.91      kristaps  269:        if ( ! (TERMP_NOBREAK & p->flags)) {
1.139     schwarze  270:                p->viscol = 0;
1.146     kristaps  271:                (*p->endline)(p);
1.15      kristaps  272:                return;
1.71      kristaps  273:        }
1.15      kristaps  274:
1.91      kristaps  275:        if (TERMP_HANG & p->flags) {
1.242     schwarze  276:                p->overstep += (int)(p->offset + vis - p->rmargin +
1.222     schwarze  277:                    p->trailspace * (*p->width)(p, ' '));
1.91      kristaps  278:
                    279:                /*
1.92      kristaps  280:                 * If we have overstepped the margin, temporarily move
                    281:                 * it to the right and flag the rest of the line to be
                    282:                 * shorter.
1.212     schwarze  283:                 * If there is a request to keep the columns together,
                    284:                 * allow negative overstep when the column is not full.
1.91      kristaps  285:                 */
1.212     schwarze  286:                if (p->trailspace && p->overstep < 0)
1.129     kristaps  287:                        p->overstep = 0;
1.200     schwarze  288:                return;
1.91      kristaps  289:
                    290:        } else if (TERMP_DANGLE & p->flags)
                    291:                return;
1.15      kristaps  292:
1.200     schwarze  293:        /* If the column was overrun, break the line. */
1.211     schwarze  294:        if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
1.146     kristaps  295:                (*p->endline)(p);
1.200     schwarze  296:                p->viscol = 0;
1.91      kristaps  297:        }
1.15      kristaps  298: }
                    299:
1.222     schwarze  300: /*
1.71      kristaps  301:  * A newline only breaks an existing line; it won't assert vertical
                    302:  * space.  All data in the output buffer is flushed prior to the newline
                    303:  * assertion.
                    304:  */
                    305: void
                    306: term_newln(struct termp *p)
1.15      kristaps  307: {
                    308:
1.71      kristaps  309:        p->flags |= TERMP_NOSPACE;
1.200     schwarze  310:        if (p->col || p->viscol)
                    311:                term_flushln(p);
1.16      kristaps  312: }
                    313:
1.71      kristaps  314: /*
                    315:  * Asserts a vertical space (a full, empty line-break between lines).
                    316:  * Note that if used twice, this will cause two blank spaces and so on.
                    317:  * All data in the output buffer is flushed prior to the newline
                    318:  * assertion.
                    319:  */
                    320: void
                    321: term_vspace(struct termp *p)
1.16      kristaps  322: {
                    323:
1.62      kristaps  324:        term_newln(p);
1.139     schwarze  325:        p->viscol = 0;
1.202     schwarze  326:        if (0 < p->skipvsp)
                    327:                p->skipvsp--;
                    328:        else
                    329:                (*p->endline)(p);
1.16      kristaps  330: }
                    331:
1.238     schwarze  332: /* Swap current and previous font; for \fP and .ft P */
1.125     kristaps  333: void
                    334: term_fontlast(struct termp *p)
                    335: {
                    336:        enum termfont    f;
                    337:
                    338:        f = p->fontl;
                    339:        p->fontl = p->fontq[p->fonti];
                    340:        p->fontq[p->fonti] = f;
                    341: }
                    342:
1.238     schwarze  343: /* Set font, save current, discard previous; for \f, .ft, .B etc. */
1.125     kristaps  344: void
                    345: term_fontrepl(struct termp *p, enum termfont f)
                    346: {
                    347:
                    348:        p->fontl = p->fontq[p->fonti];
                    349:        p->fontq[p->fonti] = f;
                    350: }
                    351:
1.238     schwarze  352: /* Set font, save previous. */
1.125     kristaps  353: void
                    354: term_fontpush(struct termp *p, enum termfont f)
                    355: {
                    356:
                    357:        p->fontl = p->fontq[p->fonti];
1.238     schwarze  358:        if (++p->fonti == p->fontsz) {
                    359:                p->fontsz += 8;
                    360:                p->fontq = mandoc_reallocarray(p->fontq,
                    361:                    p->fontsz, sizeof(enum termfont *));
                    362:        }
                    363:        p->fontq[p->fonti] = f;
1.125     kristaps  364: }
                    365:
1.238     schwarze  366: /* Retrieve pointer to current font. */
                    367: const enum termfont *
1.125     kristaps  368: term_fontq(struct termp *p)
                    369: {
                    370:
                    371:        return(&p->fontq[p->fonti]);
                    372: }
                    373:
1.238     schwarze  374: /* Flush to make the saved pointer current again. */
1.125     kristaps  375: void
1.238     schwarze  376: term_fontpopq(struct termp *p, const enum termfont *key)
1.125     kristaps  377: {
                    378:
1.238     schwarze  379:        while (p->fonti >= 0 && key < p->fontq + p->fonti)
1.125     kristaps  380:                p->fonti--;
                    381:        assert(p->fonti >= 0);
                    382: }
1.94      kristaps  383:
1.238     schwarze  384: /* Pop one font off the stack. */
1.125     kristaps  385: void
                    386: term_fontpop(struct termp *p)
                    387: {
                    388:
                    389:        assert(p->fonti);
                    390:        p->fonti--;
1.17      kristaps  391: }
                    392:
1.71      kristaps  393: /*
                    394:  * Handle pwords, partial words, which may be either a single word or a
                    395:  * phrase that cannot be broken down (such as a literal string).  This
                    396:  * handles word styling.
                    397:  */
1.86      kristaps  398: void
                    399: term_word(struct termp *p, const char *word)
1.65      kristaps  400: {
1.214     schwarze  401:        const char       nbrsp[2] = { ASCII_NBRSP, 0 };
1.191     kristaps  402:        const char      *seq, *cp;
1.194     kristaps  403:        int              sz, uc;
1.124     kristaps  404:        size_t           ssz;
1.184     kristaps  405:        enum mandoc_esc  esc;
1.100     kristaps  406:
1.133     kristaps  407:        if ( ! (TERMP_NOSPACE & p->flags)) {
1.151     schwarze  408:                if ( ! (TERMP_KEEP & p->flags)) {
1.133     kristaps  409:                        bufferc(p, ' ');
1.151     schwarze  410:                        if (TERMP_SENTENCE & p->flags)
                    411:                                bufferc(p, ' ');
                    412:                } else
                    413:                        bufferc(p, ASCII_NBRSP);
1.133     kristaps  414:        }
1.207     schwarze  415:        if (TERMP_PREKEEP & p->flags)
                    416:                p->flags |= TERMP_KEEP;
1.65      kristaps  417:
1.71      kristaps  418:        if ( ! (p->flags & TERMP_NONOSPACE))
                    419:                p->flags &= ~TERMP_NOSPACE;
1.166     kristaps  420:        else
                    421:                p->flags |= TERMP_NOSPACE;
1.133     kristaps  422:
1.237     schwarze  423:        p->flags &= ~(TERMP_SENTENCE | TERMP_NONEWLINE);
1.65      kristaps  424:
1.184     kristaps  425:        while ('\0' != *word) {
1.203     schwarze  426:                if ('\\' != *word) {
                    427:                        if (TERMP_SKIPCHAR & p->flags) {
                    428:                                p->flags &= ~TERMP_SKIPCHAR;
                    429:                                word++;
                    430:                                continue;
                    431:                        }
1.214     schwarze  432:                        if (TERMP_NBRWORD & p->flags) {
                    433:                                if (' ' == *word) {
                    434:                                        encode(p, nbrsp, 1);
                    435:                                        word++;
                    436:                                        continue;
                    437:                                }
                    438:                                ssz = strcspn(word, "\\ ");
                    439:                        } else
                    440:                                ssz = strcspn(word, "\\");
1.162     kristaps  441:                        encode(p, word, ssz);
1.203     schwarze  442:                        word += (int)ssz;
1.124     kristaps  443:                        continue;
1.203     schwarze  444:                }
1.124     kristaps  445:
1.184     kristaps  446:                word++;
                    447:                esc = mandoc_escape(&word, &seq, &sz);
                    448:                if (ESCAPE_ERROR == esc)
1.224     schwarze  449:                        continue;
1.124     kristaps  450:
1.184     kristaps  451:                switch (esc) {
1.222     schwarze  452:                case ESCAPE_UNICODE:
1.229     schwarze  453:                        uc = mchars_num2uc(seq + 1, sz - 1);
1.192     kristaps  454:                        break;
1.222     schwarze  455:                case ESCAPE_NUMBERED:
1.233     schwarze  456:                        uc = mchars_num2char(seq, sz);
                    457:                        if (uc < 0)
                    458:                                continue;
1.184     kristaps  459:                        break;
1.222     schwarze  460:                case ESCAPE_SPECIAL:
1.229     schwarze  461:                        if (p->enc == TERMENC_ASCII) {
                    462:                                cp = mchars_spec2str(p->symtab,
                    463:                                    seq, sz, &ssz);
1.232     schwarze  464:                                if (cp != NULL)
1.229     schwarze  465:                                        encode(p, cp, ssz);
                    466:                        } else {
                    467:                                uc = mchars_spec2cp(p->symtab, seq, sz);
1.230     schwarze  468:                                if (uc > 0)
                    469:                                        encode1(p, uc);
1.229     schwarze  470:                        }
1.233     schwarze  471:                        continue;
1.222     schwarze  472:                case ESCAPE_FONTBOLD:
1.125     kristaps  473:                        term_fontrepl(p, TERMFONT_BOLD);
1.233     schwarze  474:                        continue;
1.222     schwarze  475:                case ESCAPE_FONTITALIC:
1.125     kristaps  476:                        term_fontrepl(p, TERMFONT_UNDER);
1.233     schwarze  477:                        continue;
1.222     schwarze  478:                case ESCAPE_FONTBI:
1.209     schwarze  479:                        term_fontrepl(p, TERMFONT_BI);
1.233     schwarze  480:                        continue;
1.222     schwarze  481:                case ESCAPE_FONT:
1.195     kristaps  482:                        /* FALLTHROUGH */
1.222     schwarze  483:                case ESCAPE_FONTROMAN:
1.125     kristaps  484:                        term_fontrepl(p, TERMFONT_NONE);
1.233     schwarze  485:                        continue;
1.222     schwarze  486:                case ESCAPE_FONTPREV:
1.125     kristaps  487:                        term_fontlast(p);
1.233     schwarze  488:                        continue;
1.222     schwarze  489:                case ESCAPE_NOSPACE:
1.203     schwarze  490:                        if (TERMP_SKIPCHAR & p->flags)
                    491:                                p->flags &= ~TERMP_SKIPCHAR;
                    492:                        else if ('\0' == *word)
1.237     schwarze  493:                                p->flags |= (TERMP_NOSPACE | TERMP_NONEWLINE);
1.233     schwarze  494:                        continue;
1.222     schwarze  495:                case ESCAPE_SKIPCHAR:
1.203     schwarze  496:                        p->flags |= TERMP_SKIPCHAR;
1.233     schwarze  497:                        continue;
1.243   ! schwarze  498:                case ESCAPE_OVERSTRIKE:
        !           499:                        cp = seq + sz;
        !           500:                        while (seq < cp) {
        !           501:                                if (*seq == '\\') {
        !           502:                                        mandoc_escape(&seq, NULL, NULL);
        !           503:                                        continue;
        !           504:                                }
        !           505:                                encode1(p, *seq++);
        !           506:                                if (seq < cp)
        !           507:                                        encode(p, "\b", 1);
        !           508:                        }
1.124     kristaps  509:                default:
1.233     schwarze  510:                        continue;
                    511:                }
                    512:
                    513:                /*
                    514:                 * Common handling for Unicode and numbered
                    515:                 * character escape sequences.
                    516:                 */
                    517:
                    518:                if (p->enc == TERMENC_ASCII) {
                    519:                        cp = ascii_uc2str(uc);
                    520:                        encode(p, cp, strlen(cp));
                    521:                } else {
                    522:                        if ((uc < 0x20 && uc != 0x09) ||
                    523:                            (uc > 0x7E && uc < 0xA0))
                    524:                                uc = 0xFFFD;
                    525:                        encode1(p, uc);
1.124     kristaps  526:                }
                    527:        }
1.214     schwarze  528:        p->flags &= ~TERMP_NBRWORD;
1.65      kristaps  529: }
                    530:
1.71      kristaps  531: static void
1.210     schwarze  532: adjbuf(struct termp *p, size_t sz)
1.51      kristaps  533: {
                    534:
1.125     kristaps  535:        if (0 == p->maxcols)
                    536:                p->maxcols = 1024;
                    537:        while (sz >= p->maxcols)
                    538:                p->maxcols <<= 2;
                    539:
1.223     schwarze  540:        p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int));
1.51      kristaps  541: }
                    542:
1.79      kristaps  543: static void
1.125     kristaps  544: bufferc(struct termp *p, char c)
                    545: {
                    546:
                    547:        if (p->col + 1 >= p->maxcols)
                    548:                adjbuf(p, p->col + 1);
                    549:
1.188     kristaps  550:        p->buf[p->col++] = c;
1.125     kristaps  551: }
                    552:
1.194     kristaps  553: /*
                    554:  * See encode().
                    555:  * Do this for a single (probably unicode) value.
                    556:  * Does not check for non-decorated glyphs.
                    557:  */
                    558: static void
                    559: encode1(struct termp *p, int c)
                    560: {
                    561:        enum termfont     f;
                    562:
1.203     schwarze  563:        if (TERMP_SKIPCHAR & p->flags) {
                    564:                p->flags &= ~TERMP_SKIPCHAR;
                    565:                return;
                    566:        }
                    567:
1.209     schwarze  568:        if (p->col + 6 >= p->maxcols)
                    569:                adjbuf(p, p->col + 6);
1.194     kristaps  570:
1.238     schwarze  571:        f = *term_fontq(p);
1.194     kristaps  572:
1.209     schwarze  573:        if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
1.194     kristaps  574:                p->buf[p->col++] = '_';
1.209     schwarze  575:                p->buf[p->col++] = 8;
                    576:        }
                    577:        if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
                    578:                if (ASCII_HYPH == c)
                    579:                        p->buf[p->col++] = '-';
                    580:                else
                    581:                        p->buf[p->col++] = c;
                    582:                p->buf[p->col++] = 8;
                    583:        }
1.194     kristaps  584:        p->buf[p->col++] = c;
                    585: }
                    586:
1.125     kristaps  587: static void
                    588: encode(struct termp *p, const char *word, size_t sz)
                    589: {
1.210     schwarze  590:        size_t            i;
1.188     kristaps  591:
1.203     schwarze  592:        if (TERMP_SKIPCHAR & p->flags) {
                    593:                p->flags &= ~TERMP_SKIPCHAR;
                    594:                return;
                    595:        }
                    596:
1.125     kristaps  597:        /*
                    598:         * Encode and buffer a string of characters.  If the current
                    599:         * font mode is unset, buffer directly, else encode then buffer
                    600:         * character by character.
                    601:         */
                    602:
1.238     schwarze  603:        if (*term_fontq(p) == TERMFONT_NONE) {
1.222     schwarze  604:                if (p->col + sz >= p->maxcols)
1.210     schwarze  605:                        adjbuf(p, p->col + sz);
                    606:                for (i = 0; i < sz; i++)
1.188     kristaps  607:                        p->buf[p->col++] = word[i];
1.125     kristaps  608:                return;
                    609:        }
                    610:
1.165     kristaps  611:        /* Pre-buffer, assuming worst-case. */
                    612:
1.210     schwarze  613:        if (p->col + 1 + (sz * 5) >= p->maxcols)
                    614:                adjbuf(p, p->col + 1 + (sz * 5));
1.165     kristaps  615:
1.210     schwarze  616:        for (i = 0; i < sz; i++) {
1.209     schwarze  617:                if (ASCII_HYPH == word[i] ||
                    618:                    isgraph((unsigned char)word[i]))
                    619:                        encode1(p, word[i]);
1.125     kristaps  620:                else
1.188     kristaps  621:                        p->buf[p->col++] = word[i];
1.79      kristaps  622:        }
1.219     schwarze  623: }
                    624:
                    625: void
                    626: term_setwidth(struct termp *p, const char *wstr)
                    627: {
                    628:        struct roffsu    su;
                    629:        size_t           width;
                    630:        int              iop;
                    631:
1.220     schwarze  632:        iop = 0;
                    633:        width = 0;
1.219     schwarze  634:        if (NULL != wstr) {
                    635:                switch (*wstr) {
1.222     schwarze  636:                case '+':
1.219     schwarze  637:                        iop = 1;
                    638:                        wstr++;
                    639:                        break;
1.222     schwarze  640:                case '-':
1.219     schwarze  641:                        iop = -1;
                    642:                        wstr++;
                    643:                        break;
                    644:                default:
                    645:                        break;
                    646:                }
1.220     schwarze  647:                if (a2roffsu(wstr, &su, SCALE_MAX))
                    648:                        width = term_hspan(p, &su);
                    649:                else
1.219     schwarze  650:                        iop = 0;
                    651:        }
                    652:        (*p->setwidth)(p, iop, width);
1.79      kristaps  653: }
1.106     kristaps  654:
1.107     kristaps  655: size_t
1.149     kristaps  656: term_len(const struct termp *p, size_t sz)
                    657: {
                    658:
                    659:        return((*p->width)(p, ' ') * sz);
                    660: }
                    661:
1.203     schwarze  662: static size_t
                    663: cond_width(const struct termp *p, int c, int *skip)
                    664: {
                    665:
                    666:        if (*skip) {
                    667:                (*skip) = 0;
                    668:                return(0);
                    669:        } else
                    670:                return((*p->width)(p, c));
                    671: }
1.149     kristaps  672:
                    673: size_t
                    674: term_strlen(const struct termp *p, const char *cp)
                    675: {
1.184     kristaps  676:        size_t           sz, rsz, i;
1.233     schwarze  677:        int              ssz, skip, uc;
1.171     kristaps  678:        const char      *seq, *rhs;
1.196     kristaps  679:        enum mandoc_esc  esc;
1.216     schwarze  680:        static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
                    681:                        ASCII_BREAK, '\0' };
1.171     kristaps  682:
1.184     kristaps  683:        /*
                    684:         * Account for escaped sequences within string length
                    685:         * calculations.  This follows the logic in term_word() as we
                    686:         * must calculate the width of produced strings.
                    687:         */
                    688:
                    689:        sz = 0;
1.203     schwarze  690:        skip = 0;
1.189     kristaps  691:        while ('\0' != *cp) {
                    692:                rsz = strcspn(cp, rej);
                    693:                for (i = 0; i < rsz; i++)
1.203     schwarze  694:                        sz += cond_width(p, *cp++, &skip);
1.189     kristaps  695:
1.184     kristaps  696:                switch (*cp) {
1.222     schwarze  697:                case '\\':
1.189     kristaps  698:                        cp++;
1.196     kristaps  699:                        esc = mandoc_escape(&cp, &seq, &ssz);
                    700:                        if (ESCAPE_ERROR == esc)
1.224     schwarze  701:                                continue;
1.196     kristaps  702:
                    703:                        rhs = NULL;
                    704:
                    705:                        switch (esc) {
1.222     schwarze  706:                        case ESCAPE_UNICODE:
1.234     schwarze  707:                                uc = mchars_num2uc(seq + 1, ssz - 1);
1.194     kristaps  708:                                break;
1.222     schwarze  709:                        case ESCAPE_NUMBERED:
1.233     schwarze  710:                                uc = mchars_num2char(seq, ssz);
                    711:                                if (uc < 0)
                    712:                                        continue;
1.171     kristaps  713:                                break;
1.222     schwarze  714:                        case ESCAPE_SPECIAL:
1.233     schwarze  715:                                if (p->enc == TERMENC_ASCII) {
1.229     schwarze  716:                                        rhs = mchars_spec2str(p->symtab,
                    717:                                            seq, ssz, &rsz);
1.233     schwarze  718:                                        if (rhs != NULL)
                    719:                                                break;
                    720:                                } else {
                    721:                                        uc = mchars_spec2cp(p->symtab,
1.229     schwarze  722:                                            seq, ssz);
1.233     schwarze  723:                                        if (uc > 0)
                    724:                                                sz += cond_width(p, uc, &skip);
1.229     schwarze  725:                                }
1.233     schwarze  726:                                continue;
1.222     schwarze  727:                        case ESCAPE_SKIPCHAR:
1.203     schwarze  728:                                skip = 1;
1.243   ! schwarze  729:                                continue;
        !           730:                        case ESCAPE_OVERSTRIKE:
        !           731:                                rsz = 0;
        !           732:                                rhs = seq + ssz;
        !           733:                                while (seq < rhs) {
        !           734:                                        if (*seq == '\\') {
        !           735:                                                mandoc_escape(&seq, NULL, NULL);
        !           736:                                                continue;
        !           737:                                        }
        !           738:                                        i = (*p->width)(p, *seq++);
        !           739:                                        if (rsz < i)
        !           740:                                                rsz = i;
        !           741:                                }
        !           742:                                sz += rsz;
1.233     schwarze  743:                                continue;
1.171     kristaps  744:                        default:
1.233     schwarze  745:                                continue;
1.171     kristaps  746:                        }
1.149     kristaps  747:
1.233     schwarze  748:                        /*
                    749:                         * Common handling for Unicode and numbered
                    750:                         * character escape sequences.
                    751:                         */
                    752:
                    753:                        if (rhs == NULL) {
                    754:                                if (p->enc == TERMENC_ASCII) {
                    755:                                        rhs = ascii_uc2str(uc);
                    756:                                        rsz = strlen(rhs);
                    757:                                } else {
                    758:                                        if ((uc < 0x20 && uc != 0x09) ||
                    759:                                            (uc > 0x7E && uc < 0xA0))
                    760:                                                uc = 0xFFFD;
                    761:                                        sz += cond_width(p, uc, &skip);
                    762:                                        continue;
                    763:                                }
                    764:                        }
1.184     kristaps  765:
1.203     schwarze  766:                        if (skip) {
                    767:                                skip = 0;
                    768:                                break;
                    769:                        }
1.233     schwarze  770:
                    771:                        /*
                    772:                         * Common handling for all escape sequences
                    773:                         * printing more than one character.
                    774:                         */
1.203     schwarze  775:
1.184     kristaps  776:                        for (i = 0; i < rsz; i++)
                    777:                                sz += (*p->width)(p, *rhs++);
                    778:                        break;
1.222     schwarze  779:                case ASCII_NBRSP:
1.203     schwarze  780:                        sz += cond_width(p, ' ', &skip);
1.176     kristaps  781:                        cp++;
1.184     kristaps  782:                        break;
1.222     schwarze  783:                case ASCII_HYPH:
1.203     schwarze  784:                        sz += cond_width(p, '-', &skip);
1.176     kristaps  785:                        cp++;
1.216     schwarze  786:                        /* FALLTHROUGH */
1.222     schwarze  787:                case ASCII_BREAK:
1.184     kristaps  788:                        break;
                    789:                default:
                    790:                        break;
                    791:                }
1.189     kristaps  792:        }
1.149     kristaps  793:
                    794:        return(sz);
                    795: }
                    796:
1.240     schwarze  797: int
1.149     kristaps  798: term_vspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps  799: {
                    800:        double           r;
1.241     schwarze  801:        int              ri;
1.106     kristaps  802:
1.107     kristaps  803:        switch (su->unit) {
1.239     schwarze  804:        case SCALE_BU:
                    805:                r = su->scale / 40.0;
                    806:                break;
1.222     schwarze  807:        case SCALE_CM:
1.239     schwarze  808:                r = su->scale * 6.0 / 2.54;
                    809:                break;
                    810:        case SCALE_FS:
                    811:                r = su->scale * 65536.0 / 40.0;
1.106     kristaps  812:                break;
1.222     schwarze  813:        case SCALE_IN:
1.225     schwarze  814:                r = su->scale * 6.0;
1.106     kristaps  815:                break;
1.239     schwarze  816:        case SCALE_MM:
                    817:                r = su->scale * 0.006;
                    818:                break;
1.222     schwarze  819:        case SCALE_PC:
1.107     kristaps  820:                r = su->scale;
1.106     kristaps  821:                break;
1.222     schwarze  822:        case SCALE_PT:
1.239     schwarze  823:                r = su->scale / 12.0;
1.106     kristaps  824:                break;
1.239     schwarze  825:        case SCALE_EN:
                    826:                /* FALLTHROUGH */
                    827:        case SCALE_EM:
                    828:                r = su->scale * 0.6;
1.106     kristaps  829:                break;
1.222     schwarze  830:        case SCALE_VS:
1.107     kristaps  831:                r = su->scale;
1.106     kristaps  832:                break;
                    833:        default:
1.239     schwarze  834:                abort();
                    835:                /* NOTREACHED */
1.106     kristaps  836:        }
1.241     schwarze  837:        ri = r > 0.0 ? r + 0.4995 : r - 0.4995;
                    838:        return(ri < 66 ? ri : 1);
1.106     kristaps  839: }
                    840:
1.240     schwarze  841: int
1.149     kristaps  842: term_hspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps  843: {
1.156     kristaps  844:        double           v;
1.108     kristaps  845:
1.225     schwarze  846:        v = (*p->hspan)(p, su);
1.240     schwarze  847:        return(v > 0.0 ? v + 0.0005 : v - 0.0005);
1.106     kristaps  848: }

CVSweb