Annotation of src/usr.bin/mandoc/eqn_term.c, Revision 1.5
1.5 ! schwarze 1: /* $OpenBSD: eqn_term.c,v 1.4 2015/01/01 15:34:43 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>
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");
1.5 ! schwarze 69: if (bp->first != NULL) {
! 70: p->flags |= TERMP_NOSPACE;
! 71: eqn_box(p, bp->first);
! 72: }
1.3 schwarze 73: } else if (bp->type == EQN_SUBEXPR) {
74: child = bp->first;
75: eqn_box(p, child);
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;
81: child = child->next;
1.4 schwarze 82: if (child != NULL) {
1.3 schwarze 83: eqn_box(p, child);
1.4 schwarze 84: if (bp->pos == EQNPOS_FROMTO ||
85: bp->pos == EQNPOS_SUBSUP) {
86: p->flags |= TERMP_NOSPACE;
87: term_word(p, "^");
88: p->flags |= TERMP_NOSPACE;
89: child = child->next;
90: if (child != NULL)
91: eqn_box(p, child);
92: }
1.3 schwarze 93: }
94: } else {
95: child = bp->first;
1.5 ! schwarze 96: if (bp->type == EQN_MATRIX &&
! 97: child != NULL && child->type == EQN_LIST)
1.3 schwarze 98: child = child->first;
99: while (child != NULL) {
100: eqn_box(p,
101: bp->type == EQN_PILE &&
102: child->type == EQN_LIST &&
103: child->args == 1 ?
104: child->first : child);
105: child = child->next;
106: }
107: }
1.1 schwarze 108:
1.3 schwarze 109: if (bp->font != EQNFONT_NONE)
1.1 schwarze 110: term_fontpop(p);
1.3 schwarze 111: if (bp->type == EQN_LIST ||
112: (bp->type == EQN_PILE && (bp->prev || bp->next)) ||
113: (bp->parent != NULL && bp->parent->pos == EQNPOS_SQRT)) {
114: p->flags |= TERMP_NOSPACE;
115: term_word(p, bp->right != NULL ? bp->right : ")");
116: if (bp->parent->type == EQN_SUBEXPR && bp->next != NULL)
117: p->flags |= TERMP_NOSPACE;
118: }
119:
120: if (bp->top != NULL) {
121: p->flags |= TERMP_NOSPACE;
122: term_word(p, bp->top);
123: }
124: if (bp->bottom != NULL) {
125: p->flags |= TERMP_NOSPACE;
126: term_word(p, "_");
127: }
1.1 schwarze 128: }