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

1.10    ! schwarze    1: /*     $OpenBSD: eqn_term.c,v 1.9 2017/07/08 14:51:01 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.3       schwarze   91:        if (bp->text != NULL)
1.1       schwarze   92:                term_word(p, bp->text);
                     93:
1.8       schwarze   94:        /* Special box types. */
                     95:
1.3       schwarze   96:        if (bp->pos == EQNPOS_SQRT) {
                     97:                term_word(p, "sqrt");
1.5       schwarze   98:                if (bp->first != NULL) {
                     99:                        p->flags |= TERMP_NOSPACE;
                    100:                        eqn_box(p, bp->first);
                    101:                }
1.3       schwarze  102:        } else if (bp->type == EQN_SUBEXPR) {
                    103:                child = bp->first;
                    104:                eqn_box(p, child);
                    105:                p->flags |= TERMP_NOSPACE;
                    106:                term_word(p, bp->pos == EQNPOS_OVER ? "/" :
                    107:                    (bp->pos == EQNPOS_SUP ||
                    108:                     bp->pos == EQNPOS_TO) ? "^" : "_");
                    109:                child = child->next;
1.4       schwarze  110:                if (child != NULL) {
1.10    ! schwarze  111:                        p->flags |= TERMP_NOSPACE;
1.3       schwarze  112:                        eqn_box(p, child);
1.4       schwarze  113:                        if (bp->pos == EQNPOS_FROMTO ||
                    114:                            bp->pos == EQNPOS_SUBSUP) {
                    115:                                p->flags |= TERMP_NOSPACE;
                    116:                                term_word(p, "^");
                    117:                                p->flags |= TERMP_NOSPACE;
                    118:                                child = child->next;
                    119:                                if (child != NULL)
                    120:                                        eqn_box(p, child);
                    121:                        }
1.3       schwarze  122:                }
                    123:        } else {
                    124:                child = bp->first;
1.5       schwarze  125:                if (bp->type == EQN_MATRIX &&
1.6       schwarze  126:                    child != NULL &&
                    127:                    child->type == EQN_LIST &&
                    128:                    child->expectargs > 1)
1.3       schwarze  129:                        child = child->first;
                    130:                while (child != NULL) {
                    131:                        eqn_box(p,
                    132:                            bp->type == EQN_PILE &&
                    133:                            child->type == EQN_LIST &&
1.6       schwarze  134:                            child->expectargs > 1 &&
1.3       schwarze  135:                            child->args == 1 ?
                    136:                            child->first : child);
                    137:                        child = child->next;
                    138:                }
                    139:        }
1.1       schwarze  140:
1.8       schwarze  141:        /* Handle Fonts and diacritics. */
                    142:
1.3       schwarze  143:        if (bp->font != EQNFONT_NONE)
1.1       schwarze  144:                term_fontpop(p);
1.7       schwarze  145:        if (bp->top != NULL) {
                    146:                p->flags |= TERMP_NOSPACE;
                    147:                term_word(p, bp->top);
                    148:        }
                    149:        if (bp->bottom != NULL) {
                    150:                p->flags |= TERMP_NOSPACE;
                    151:                term_word(p, "_");
                    152:        }
1.8       schwarze  153:
                    154:        /* Right delimiter after this box? */
                    155:
                    156:        if (delim) {
1.3       schwarze  157:                p->flags |= TERMP_NOSPACE;
                    158:                term_word(p, bp->right != NULL ? bp->right : ")");
                    159:                if (bp->parent->type == EQN_SUBEXPR && bp->next != NULL)
                    160:                        p->flags |= TERMP_NOSPACE;
                    161:        }
1.1       schwarze  162: }