Annotation of mandoc/eqn_term.c, Revision 1.8
1.8 ! schwarze 1: /* $Id: eqn_term.c,v 1.7 2014/10/12 14:49:39 schwarze Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.8 ! schwarze 4: * Copyright (c) 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 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"
1.6 schwarze 19:
20: #include <sys/types.h>
1.1 kristaps 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:
1.4 kristaps 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 */
37: };
38:
1.3 kristaps 39: static void eqn_box(struct termp *, const struct eqn_box *);
1.1 kristaps 40:
1.5 schwarze 41:
1.1 kristaps 42: void
43: term_eqn(struct termp *p, const struct eqn *ep)
44: {
45:
46: eqn_box(p, ep->root);
1.7 schwarze 47: p->flags &= ~TERMP_NOSPACE;
1.1 kristaps 48: }
49:
50: static void
51: eqn_box(struct termp *p, const struct eqn_box *bp)
52: {
1.7 schwarze 53: const struct eqn_box *child;
1.1 kristaps 54:
1.7 schwarze 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)
1.4 kristaps 64: term_fontpush(p, fontmap[(int)bp->font]);
65:
1.7 schwarze 66: if (bp->text != NULL)
1.4 kristaps 67: term_word(p, bp->text);
1.1 kristaps 68:
1.7 schwarze 69: if (bp->pos == EQNPOS_SQRT) {
70: term_word(p, "sqrt");
71: p->flags |= TERMP_NOSPACE;
1.1 kristaps 72: eqn_box(p, bp->first);
1.7 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.8 ! schwarze 82: if (child != NULL) {
1.7 schwarze 83: eqn_box(p, child);
1.8 ! 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.7 schwarze 93: }
94: } else {
95: child = bp->first;
96: if (bp->type == EQN_MATRIX && child->type == EQN_LIST)
97: child = child->first;
98: while (child != NULL) {
99: eqn_box(p,
100: bp->type == EQN_PILE &&
101: child->type == EQN_LIST &&
102: child->args == 1 ?
103: child->first : child);
104: child = child->next;
105: }
106: }
1.1 kristaps 107:
1.7 schwarze 108: if (bp->font != EQNFONT_NONE)
1.4 kristaps 109: term_fontpop(p);
1.7 schwarze 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;
117: }
118:
119: if (bp->top != NULL) {
120: p->flags |= TERMP_NOSPACE;
121: term_word(p, bp->top);
122: }
123: if (bp->bottom != NULL) {
124: p->flags |= TERMP_NOSPACE;
125: term_word(p, "_");
126: }
1.1 kristaps 127: }
CVSweb