eqn_term.c revision 1.1.1.5
1/*	Id: eqn_term.c,v 1.9 2017/02/12 14:19:01 schwarze Exp  */
2/*
3 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
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 */
18#include "config.h"
19
20#include <sys/types.h>
21
22#include <assert.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include "mandoc.h"
28#include "out.h"
29#include "term.h"
30
31static	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 */
37};
38
39static void	eqn_box(struct termp *, const struct eqn_box *);
40
41
42void
43term_eqn(struct termp *p, const struct eqn *ep)
44{
45
46	eqn_box(p, ep->root);
47	p->flags &= ~TERMP_NOSPACE;
48}
49
50static void
51eqn_box(struct termp *p, const struct eqn_box *bp)
52{
53	const struct eqn_box *child;
54
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;
62	}
63	if (bp->font != EQNFONT_NONE)
64		term_fontpush(p, fontmap[(int)bp->font]);
65
66	if (bp->text != NULL)
67		term_word(p, bp->text);
68
69	if (bp->pos == EQNPOS_SQRT) {
70		term_word(p, "sqrt");
71		if (bp->first != NULL) {
72			p->flags |= TERMP_NOSPACE;
73			eqn_box(p, bp->first);
74		}
75	} else if (bp->type == EQN_SUBEXPR) {
76		child = bp->first;
77		eqn_box(p, child);
78		p->flags |= TERMP_NOSPACE;
79		term_word(p, bp->pos == EQNPOS_OVER ? "/" :
80		    (bp->pos == EQNPOS_SUP ||
81		     bp->pos == EQNPOS_TO) ? "^" : "_");
82		p->flags |= TERMP_NOSPACE;
83		child = child->next;
84		if (child != NULL) {
85			eqn_box(p, child);
86			if (bp->pos == EQNPOS_FROMTO ||
87			    bp->pos == EQNPOS_SUBSUP) {
88				p->flags |= TERMP_NOSPACE;
89				term_word(p, "^");
90				p->flags |= TERMP_NOSPACE;
91				child = child->next;
92				if (child != NULL)
93					eqn_box(p, child);
94			}
95		}
96	} else {
97		child = bp->first;
98		if (bp->type == EQN_MATRIX &&
99		    child != NULL && child->type == EQN_LIST)
100			child = child->first;
101		while (child != NULL) {
102			eqn_box(p,
103			    bp->type == EQN_PILE &&
104			    child->type == EQN_LIST &&
105			    child->args == 1 ?
106			    child->first : child);
107			child = child->next;
108		}
109	}
110
111	if (bp->font != EQNFONT_NONE)
112		term_fontpop(p);
113	if (bp->type == EQN_LIST ||
114	    (bp->type == EQN_PILE && (bp->prev || bp->next)) ||
115	    (bp->parent != NULL && bp->parent->pos == EQNPOS_SQRT)) {
116		p->flags |= TERMP_NOSPACE;
117		term_word(p, bp->right != NULL ? bp->right : ")");
118		if (bp->parent->type == EQN_SUBEXPR && bp->next != NULL)
119			p->flags |= TERMP_NOSPACE;
120	}
121
122	if (bp->top != NULL) {
123		p->flags |= TERMP_NOSPACE;
124		term_word(p, bp->top);
125	}
126	if (bp->bottom != NULL) {
127		p->flags |= TERMP_NOSPACE;
128		term_word(p, "_");
129	}
130}
131