[BACK]Return to eqn_term.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/eqn_term.c, Revision 1.15

1.15    ! schwarze    1: /*     $OpenBSD: eqn_term.c,v 1.14 2018/10/02 12:14:44 schwarze Exp $ */
1.1       schwarze    2: /*
                      3:  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.5       schwarze    4:  * Copyright (c) 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
1.1       schwarze    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     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.
                     17:  */
1.3       schwarze   18: #include <sys/types.h>
                     19:
1.1       schwarze   20: #include <assert.h>
1.10      schwarze   21: #include <ctype.h>
1.1       schwarze   22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
                     25:
1.15    ! schwarze   26: #include "eqn.h"
1.1       schwarze   27: #include "out.h"
                     28: #include "term.h"
                     29:
                     30: static const enum termfont fontmap[EQNFONT__MAX] = {
                     31:        TERMFONT_NONE, /* EQNFONT_NONE */
                     32:        TERMFONT_NONE, /* EQNFONT_ROMAN */
                     33:        TERMFONT_BOLD, /* EQNFONT_BOLD */
                     34:        TERMFONT_BOLD, /* EQNFONT_FAT */
                     35:        TERMFONT_UNDER /* EQNFONT_ITALIC */
                     36: };
                     37:
                     38: static void    eqn_box(struct termp *, const struct eqn_box *);
                     39:
1.2       schwarze   40:
1.1       schwarze   41: void
1.9       schwarze   42: term_eqn(struct termp *p, const struct eqn_box *bp)
1.1       schwarze   43: {
                     44:
1.9       schwarze   45:        eqn_box(p, bp);
1.3       schwarze   46:        p->flags &= ~TERMP_NOSPACE;
1.1       schwarze   47: }
                     48:
                     49: static void
                     50: eqn_box(struct termp *p, const struct eqn_box *bp)
                     51: {
1.3       schwarze   52:        const struct eqn_box *child;
1.12      schwarze   53:        const char *cp;
1.8       schwarze   54:        int delim;
                     55:
                     56:        /* Delimiters around this box? */
1.1       schwarze   57:
1.6       schwarze   58:        if ((bp->type == EQN_LIST && bp->expectargs > 1) ||
1.3       schwarze   59:            (bp->type == EQN_PILE && (bp->prev || bp->next)) ||
1.8       schwarze   60:            (bp->parent != NULL && (bp->parent->pos == EQNPOS_SQRT ||
                     61:            /* Diacritic followed by ^ or _. */
                     62:            ((bp->top != NULL || bp->bottom != NULL) &&
                     63:             bp->parent->type == EQN_SUBEXPR &&
                     64:             bp->parent->pos != EQNPOS_OVER && bp->next != NULL) ||
                     65:            /* Nested over, sub, sup, from, to. */
                     66:            (bp->type == EQN_SUBEXPR && bp->pos != EQNPOS_SQRT &&
                     67:             ((bp->parent->type == EQN_LIST && bp->expectargs == 1) ||
                     68:              (bp->parent->type == EQN_SUBEXPR &&
                     69:               bp->pos != EQNPOS_SQRT)))))) {
1.10      schwarze   70:                if ((bp->parent->type == EQN_SUBEXPR && bp->prev != NULL) ||
                     71:                    (bp->type == EQN_LIST &&
                     72:                     bp->first != NULL &&
                     73:                     bp->first->type != EQN_PILE &&
                     74:                     bp->first->type != EQN_MATRIX &&
                     75:                     bp->prev != NULL &&
                     76:                     (bp->prev->type == EQN_LIST ||
                     77:                      (bp->prev->type == EQN_TEXT &&
                     78:                       (*bp->prev->text == '\\' ||
                     79:                        isalpha((unsigned char)*bp->prev->text))))))
1.3       schwarze   80:                        p->flags |= TERMP_NOSPACE;
                     81:                term_word(p, bp->left != NULL ? bp->left : "(");
                     82:                p->flags |= TERMP_NOSPACE;
1.8       schwarze   83:                delim = 1;
                     84:        } else
                     85:                delim = 0;
                     86:
                     87:        /* Handle Fonts and text. */
                     88:
1.3       schwarze   89:        if (bp->font != EQNFONT_NONE)
1.1       schwarze   90:                term_fontpush(p, fontmap[(int)bp->font]);
                     91:
1.11      schwarze   92:        if (bp->text != NULL) {
                     93:                if (strchr("!\"'),.:;?]}", *bp->text) != NULL)
                     94:                        p->flags |= TERMP_NOSPACE;
1.1       schwarze   95:                term_word(p, bp->text);
1.12      schwarze   96:                if ((cp = strchr(bp->text, '\0')) > bp->text &&
1.13      schwarze   97:                    (strchr("\"'([{", cp[-1]) != NULL ||
                     98:                     (bp->prev == NULL && (cp[-1] == '-' ||
                     99:                      (cp >= bp->text + 5 &&
                    100:                       strcmp(cp - 5, "\\[mi]") == 0)))))
1.11      schwarze  101:                        p->flags |= TERMP_NOSPACE;
                    102:        }
1.1       schwarze  103:
1.8       schwarze  104:        /* Special box types. */
                    105:
1.3       schwarze  106:        if (bp->pos == EQNPOS_SQRT) {
1.14      schwarze  107:                term_word(p, "\\(sr");
1.5       schwarze  108:                if (bp->first != NULL) {
                    109:                        p->flags |= TERMP_NOSPACE;
                    110:                        eqn_box(p, bp->first);
                    111:                }
1.3       schwarze  112:        } else if (bp->type == EQN_SUBEXPR) {
                    113:                child = bp->first;
                    114:                eqn_box(p, child);
                    115:                p->flags |= TERMP_NOSPACE;
                    116:                term_word(p, bp->pos == EQNPOS_OVER ? "/" :
                    117:                    (bp->pos == EQNPOS_SUP ||
                    118:                     bp->pos == EQNPOS_TO) ? "^" : "_");
                    119:                child = child->next;
1.4       schwarze  120:                if (child != NULL) {
1.10      schwarze  121:                        p->flags |= TERMP_NOSPACE;
1.3       schwarze  122:                        eqn_box(p, child);
1.4       schwarze  123:                        if (bp->pos == EQNPOS_FROMTO ||
                    124:                            bp->pos == EQNPOS_SUBSUP) {
                    125:                                p->flags |= TERMP_NOSPACE;
                    126:                                term_word(p, "^");
                    127:                                p->flags |= TERMP_NOSPACE;
                    128:                                child = child->next;
                    129:                                if (child != NULL)
                    130:                                        eqn_box(p, child);
                    131:                        }
1.3       schwarze  132:                }
                    133:        } else {
                    134:                child = bp->first;
1.5       schwarze  135:                if (bp->type == EQN_MATRIX &&
1.6       schwarze  136:                    child != NULL &&
                    137:                    child->type == EQN_LIST &&
                    138:                    child->expectargs > 1)
1.3       schwarze  139:                        child = child->first;
                    140:                while (child != NULL) {
                    141:                        eqn_box(p,
                    142:                            bp->type == EQN_PILE &&
                    143:                            child->type == EQN_LIST &&
1.6       schwarze  144:                            child->expectargs > 1 &&
1.3       schwarze  145:                            child->args == 1 ?
                    146:                            child->first : child);
                    147:                        child = child->next;
                    148:                }
                    149:        }
1.1       schwarze  150:
1.8       schwarze  151:        /* Handle Fonts and diacritics. */
                    152:
1.3       schwarze  153:        if (bp->font != EQNFONT_NONE)
1.1       schwarze  154:                term_fontpop(p);
1.7       schwarze  155:        if (bp->top != NULL) {
                    156:                p->flags |= TERMP_NOSPACE;
                    157:                term_word(p, bp->top);
                    158:        }
                    159:        if (bp->bottom != NULL) {
                    160:                p->flags |= TERMP_NOSPACE;
                    161:                term_word(p, "_");
                    162:        }
1.8       schwarze  163:
                    164:        /* Right delimiter after this box? */
                    165:
                    166:        if (delim) {
1.3       schwarze  167:                p->flags |= TERMP_NOSPACE;
                    168:                term_word(p, bp->right != NULL ? bp->right : ")");
                    169:                if (bp->parent->type == EQN_SUBEXPR && bp->next != NULL)
                    170:                        p->flags |= TERMP_NOSPACE;
                    171:        }
1.1       schwarze  172: }