[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.11

1.11    ! schwarze    1: /*     $OpenBSD: eqn_term.c,v 1.10 2017/08/23 20:02:48 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:
                     26: #include "mandoc.h"
                     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.8       schwarze   53:        int delim;
                     54:
                     55:        /* Delimiters around this box? */
1.1       schwarze   56:
1.6       schwarze   57:        if ((bp->type == EQN_LIST && bp->expectargs > 1) ||
1.3       schwarze   58:            (bp->type == EQN_PILE && (bp->prev || bp->next)) ||
1.8       schwarze   59:            (bp->parent != NULL && (bp->parent->pos == EQNPOS_SQRT ||
                     60:            /* Diacritic followed by ^ or _. */
                     61:            ((bp->top != NULL || bp->bottom != NULL) &&
                     62:             bp->parent->type == EQN_SUBEXPR &&
                     63:             bp->parent->pos != EQNPOS_OVER && bp->next != NULL) ||
                     64:            /* Nested over, sub, sup, from, to. */
                     65:            (bp->type == EQN_SUBEXPR && bp->pos != EQNPOS_SQRT &&
                     66:             ((bp->parent->type == EQN_LIST && bp->expectargs == 1) ||
                     67:              (bp->parent->type == EQN_SUBEXPR &&
                     68:               bp->pos != EQNPOS_SQRT)))))) {
1.10      schwarze   69:                if ((bp->parent->type == EQN_SUBEXPR && bp->prev != NULL) ||
                     70:                    (bp->type == EQN_LIST &&
                     71:                     bp->first != NULL &&
                     72:                     bp->first->type != EQN_PILE &&
                     73:                     bp->first->type != EQN_MATRIX &&
                     74:                     bp->prev != NULL &&
                     75:                     (bp->prev->type == EQN_LIST ||
                     76:                      (bp->prev->type == EQN_TEXT &&
                     77:                       (*bp->prev->text == '\\' ||
                     78:                        isalpha((unsigned char)*bp->prev->text))))))
1.3       schwarze   79:                        p->flags |= TERMP_NOSPACE;
                     80:                term_word(p, bp->left != NULL ? bp->left : "(");
                     81:                p->flags |= TERMP_NOSPACE;
1.8       schwarze   82:                delim = 1;
                     83:        } else
                     84:                delim = 0;
                     85:
                     86:        /* Handle Fonts and text. */
                     87:
1.3       schwarze   88:        if (bp->font != EQNFONT_NONE)
1.1       schwarze   89:                term_fontpush(p, fontmap[(int)bp->font]);
                     90:
1.11    ! schwarze   91:        if (bp->text != NULL) {
        !            92:                if (strchr("!\"'),.:;?]}", *bp->text) != NULL)
        !            93:                        p->flags |= TERMP_NOSPACE;
1.1       schwarze   94:                term_word(p, bp->text);
1.11    ! schwarze   95:                if (*bp->text != '\0' && strchr("\"'([{",
        !            96:                    bp->text[strlen(bp->text) - 1]) != NULL)
        !            97:                        p->flags |= TERMP_NOSPACE;
        !            98:        }
1.1       schwarze   99:
1.8       schwarze  100:        /* Special box types. */
                    101:
1.3       schwarze  102:        if (bp->pos == EQNPOS_SQRT) {
                    103:                term_word(p, "sqrt");
1.5       schwarze  104:                if (bp->first != NULL) {
                    105:                        p->flags |= TERMP_NOSPACE;
                    106:                        eqn_box(p, bp->first);
                    107:                }
1.3       schwarze  108:        } else if (bp->type == EQN_SUBEXPR) {
                    109:                child = bp->first;
                    110:                eqn_box(p, child);
                    111:                p->flags |= TERMP_NOSPACE;
                    112:                term_word(p, bp->pos == EQNPOS_OVER ? "/" :
                    113:                    (bp->pos == EQNPOS_SUP ||
                    114:                     bp->pos == EQNPOS_TO) ? "^" : "_");
                    115:                child = child->next;
1.4       schwarze  116:                if (child != NULL) {
1.10      schwarze  117:                        p->flags |= TERMP_NOSPACE;
1.3       schwarze  118:                        eqn_box(p, child);
1.4       schwarze  119:                        if (bp->pos == EQNPOS_FROMTO ||
                    120:                            bp->pos == EQNPOS_SUBSUP) {
                    121:                                p->flags |= TERMP_NOSPACE;
                    122:                                term_word(p, "^");
                    123:                                p->flags |= TERMP_NOSPACE;
                    124:                                child = child->next;
                    125:                                if (child != NULL)
                    126:                                        eqn_box(p, child);
                    127:                        }
1.3       schwarze  128:                }
                    129:        } else {
                    130:                child = bp->first;
1.5       schwarze  131:                if (bp->type == EQN_MATRIX &&
1.6       schwarze  132:                    child != NULL &&
                    133:                    child->type == EQN_LIST &&
                    134:                    child->expectargs > 1)
1.3       schwarze  135:                        child = child->first;
                    136:                while (child != NULL) {
                    137:                        eqn_box(p,
                    138:                            bp->type == EQN_PILE &&
                    139:                            child->type == EQN_LIST &&
1.6       schwarze  140:                            child->expectargs > 1 &&
1.3       schwarze  141:                            child->args == 1 ?
                    142:                            child->first : child);
                    143:                        child = child->next;
                    144:                }
                    145:        }
1.1       schwarze  146:
1.8       schwarze  147:        /* Handle Fonts and diacritics. */
                    148:
1.3       schwarze  149:        if (bp->font != EQNFONT_NONE)
1.1       schwarze  150:                term_fontpop(p);
1.7       schwarze  151:        if (bp->top != NULL) {
                    152:                p->flags |= TERMP_NOSPACE;
                    153:                term_word(p, bp->top);
                    154:        }
                    155:        if (bp->bottom != NULL) {
                    156:                p->flags |= TERMP_NOSPACE;
                    157:                term_word(p, "_");
                    158:        }
1.8       schwarze  159:
                    160:        /* Right delimiter after this box? */
                    161:
                    162:        if (delim) {
1.3       schwarze  163:                p->flags |= TERMP_NOSPACE;
                    164:                term_word(p, bp->right != NULL ? bp->right : ")");
                    165:                if (bp->parent->type == EQN_SUBEXPR && bp->next != NULL)
                    166:                        p->flags |= TERMP_NOSPACE;
                    167:        }
1.1       schwarze  168: }