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

1.3     ! schwarze    1: /*     $OpenBSD$ */
1.1       schwarze    2: /*
                      3:  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.3     ! schwarze    4:  * Copyright (c) 2014 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>
                     21: #include <stdio.h>
                     22: #include <stdlib.h>
                     23: #include <string.h>
                     24:
                     25: #include "mandoc.h"
                     26: #include "out.h"
                     27: #include "term.h"
                     28:
                     29: static const enum termfont fontmap[EQNFONT__MAX] = {
                     30:        TERMFONT_NONE, /* EQNFONT_NONE */
                     31:        TERMFONT_NONE, /* EQNFONT_ROMAN */
                     32:        TERMFONT_BOLD, /* EQNFONT_BOLD */
                     33:        TERMFONT_BOLD, /* EQNFONT_FAT */
                     34:        TERMFONT_UNDER /* EQNFONT_ITALIC */
                     35: };
                     36:
                     37: static void    eqn_box(struct termp *, const struct eqn_box *);
                     38:
1.2       schwarze   39:
1.1       schwarze   40: void
                     41: term_eqn(struct termp *p, const struct eqn *ep)
                     42: {
                     43:
                     44:        eqn_box(p, ep->root);
1.3     ! schwarze   45:        p->flags &= ~TERMP_NOSPACE;
1.1       schwarze   46: }
                     47:
                     48: static void
                     49: eqn_box(struct termp *p, const struct eqn_box *bp)
                     50: {
1.3     ! schwarze   51:        const struct eqn_box *child;
1.1       schwarze   52:
1.3     ! schwarze   53:        if (bp->type == EQN_LIST ||
        !            54:            (bp->type == EQN_PILE && (bp->prev || bp->next)) ||
        !            55:            (bp->parent != NULL && bp->parent->pos == EQNPOS_SQRT)) {
        !            56:                if (bp->parent->type == EQN_SUBEXPR && bp->prev != NULL)
        !            57:                        p->flags |= TERMP_NOSPACE;
        !            58:                term_word(p, bp->left != NULL ? bp->left : "(");
        !            59:                p->flags |= TERMP_NOSPACE;
        !            60:        }
        !            61:        if (bp->font != EQNFONT_NONE)
1.1       schwarze   62:                term_fontpush(p, fontmap[(int)bp->font]);
                     63:
1.3     ! schwarze   64:        if (bp->text != NULL)
1.1       schwarze   65:                term_word(p, bp->text);
                     66:
1.3     ! schwarze   67:        if (bp->pos == EQNPOS_SQRT) {
        !            68:                term_word(p, "sqrt");
        !            69:                p->flags |= TERMP_NOSPACE;
1.1       schwarze   70:                eqn_box(p, bp->first);
1.3     ! schwarze   71:        } else if (bp->type == EQN_SUBEXPR) {
        !            72:                child = bp->first;
        !            73:                eqn_box(p, child);
        !            74:                p->flags |= TERMP_NOSPACE;
        !            75:                term_word(p, bp->pos == EQNPOS_OVER ? "/" :
        !            76:                    (bp->pos == EQNPOS_SUP ||
        !            77:                     bp->pos == EQNPOS_TO) ? "^" : "_");
        !            78:                p->flags |= TERMP_NOSPACE;
        !            79:                child = child->next;
        !            80:                eqn_box(p, child);
        !            81:                if (bp->pos == EQNPOS_FROMTO ||
        !            82:                    bp->pos == EQNPOS_SUBSUP) {
        !            83:                        p->flags |= TERMP_NOSPACE;
        !            84:                        term_word(p, "^");
        !            85:                        p->flags |= TERMP_NOSPACE;
        !            86:                        child = child->next;
        !            87:                        eqn_box(p, child);
        !            88:                }
        !            89:        } else {
        !            90:                child = bp->first;
        !            91:                if (bp->type == EQN_MATRIX && child->type == EQN_LIST)
        !            92:                        child = child->first;
        !            93:                while (child != NULL) {
        !            94:                        eqn_box(p,
        !            95:                            bp->type == EQN_PILE &&
        !            96:                            child->type == EQN_LIST &&
        !            97:                            child->args == 1 ?
        !            98:                            child->first : child);
        !            99:                        child = child->next;
        !           100:                }
        !           101:        }
1.1       schwarze  102:
1.3     ! schwarze  103:        if (bp->font != EQNFONT_NONE)
1.1       schwarze  104:                term_fontpop(p);
1.3     ! schwarze  105:        if (bp->type == EQN_LIST ||
        !           106:            (bp->type == EQN_PILE && (bp->prev || bp->next)) ||
        !           107:            (bp->parent != NULL && bp->parent->pos == EQNPOS_SQRT)) {
        !           108:                p->flags |= TERMP_NOSPACE;
        !           109:                term_word(p, bp->right != NULL ? bp->right : ")");
        !           110:                if (bp->parent->type == EQN_SUBEXPR && bp->next != NULL)
        !           111:                        p->flags |= TERMP_NOSPACE;
        !           112:        }
        !           113:
        !           114:        if (bp->top != NULL) {
        !           115:                p->flags |= TERMP_NOSPACE;
        !           116:                term_word(p, bp->top);
        !           117:        }
        !           118:        if (bp->bottom != NULL) {
        !           119:                p->flags |= TERMP_NOSPACE;
        !           120:                term_word(p, "_");
        !           121:        }
1.1       schwarze  122: }