1 /* $Id: eqn_term.c,v 1.8 2015/01/01 15:36:08 schwarze Exp $ */
3 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
20 #include <sys/types.h>
31 static const enum termfont fontmap[EQNFONT__MAX] = {
32 TERMFONT_NONE, /* EQNFONT_NONE */
33 TERMFONT_NONE, /* EQNFONT_ROMAN */
34 TERMFONT_BOLD, /* EQNFONT_BOLD */
35 TERMFONT_BOLD, /* EQNFONT_FAT */
36 TERMFONT_UNDER /* EQNFONT_ITALIC */
39 static void eqn_box(struct termp *, const struct eqn_box *);
43 term_eqn(struct termp *p, const struct eqn *ep)
47 p->flags &= ~TERMP_NOSPACE;
51 eqn_box(struct termp *p, const struct eqn_box *bp)
53 const struct eqn_box *child;
55 if (bp->type == EQN_LIST ||
56 (bp->type == EQN_PILE && (bp->prev || bp->next)) ||
57 (bp->parent != NULL && bp->parent->pos == EQNPOS_SQRT)) {
58 if (bp->parent->type == EQN_SUBEXPR && bp->prev != NULL)
59 p->flags |= TERMP_NOSPACE;
60 term_word(p, bp->left != NULL ? bp->left : "(");
61 p->flags |= TERMP_NOSPACE;
63 if (bp->font != EQNFONT_NONE)
64 term_fontpush(p, fontmap[(int)bp->font]);
67 term_word(p, bp->text);
69 if (bp->pos == EQNPOS_SQRT) {
71 p->flags |= TERMP_NOSPACE;
72 eqn_box(p, bp->first);
73 } else if (bp->type == EQN_SUBEXPR) {
76 p->flags |= TERMP_NOSPACE;
77 term_word(p, bp->pos == EQNPOS_OVER ? "/" :
78 (bp->pos == EQNPOS_SUP ||
79 bp->pos == EQNPOS_TO) ? "^" : "_");
80 p->flags |= TERMP_NOSPACE;
84 if (bp->pos == EQNPOS_FROMTO ||
85 bp->pos == EQNPOS_SUBSUP) {
86 p->flags |= TERMP_NOSPACE;
88 p->flags |= TERMP_NOSPACE;
96 if (bp->type == EQN_MATRIX && child->type == EQN_LIST)
98 while (child != NULL) {
100 bp->type == EQN_PILE &&
101 child->type == EQN_LIST &&
103 child->first : child);
108 if (bp->font != EQNFONT_NONE)
110 if (bp->type == EQN_LIST ||
111 (bp->type == EQN_PILE && (bp->prev || bp->next)) ||
112 (bp->parent != NULL && bp->parent->pos == EQNPOS_SQRT)) {
113 p->flags |= TERMP_NOSPACE;
114 term_word(p, bp->right != NULL ? bp->right : ")");
115 if (bp->parent->type == EQN_SUBEXPR && bp->next != NULL)
116 p->flags |= TERMP_NOSPACE;
119 if (bp->top != NULL) {
120 p->flags |= TERMP_NOSPACE;
121 term_word(p, bp->top);
123 if (bp->bottom != NULL) {
124 p->flags |= TERMP_NOSPACE;