Annotation of src/usr.bin/mandoc/eqn_term.c, Revision 1.8
1.8 ! schwarze 1: /* $OpenBSD: eqn_term.c,v 1.7 2017/07/06 00:08:52 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.8 ! schwarze 52: int delim;
! 53:
! 54: /* Delimiters around this box? */
1.1 schwarze 55:
1.6 schwarze 56: if ((bp->type == EQN_LIST && bp->expectargs > 1) ||
1.3 schwarze 57: (bp->type == EQN_PILE && (bp->prev || bp->next)) ||
1.8 ! schwarze 58: (bp->parent != NULL && (bp->parent->pos == EQNPOS_SQRT ||
! 59: /* Diacritic followed by ^ or _. */
! 60: ((bp->top != NULL || bp->bottom != NULL) &&
! 61: bp->parent->type == EQN_SUBEXPR &&
! 62: bp->parent->pos != EQNPOS_OVER && bp->next != NULL) ||
! 63: /* Nested over, sub, sup, from, to. */
! 64: (bp->type == EQN_SUBEXPR && bp->pos != EQNPOS_SQRT &&
! 65: ((bp->parent->type == EQN_LIST && bp->expectargs == 1) ||
! 66: (bp->parent->type == EQN_SUBEXPR &&
! 67: bp->pos != EQNPOS_SQRT)))))) {
1.3 schwarze 68: if (bp->parent->type == EQN_SUBEXPR && bp->prev != NULL)
69: p->flags |= TERMP_NOSPACE;
70: term_word(p, bp->left != NULL ? bp->left : "(");
71: p->flags |= TERMP_NOSPACE;
1.8 ! schwarze 72: delim = 1;
! 73: } else
! 74: delim = 0;
! 75:
! 76: /* Handle Fonts and text. */
! 77:
1.3 schwarze 78: if (bp->font != EQNFONT_NONE)
1.1 schwarze 79: term_fontpush(p, fontmap[(int)bp->font]);
80:
1.3 schwarze 81: if (bp->text != NULL)
1.1 schwarze 82: term_word(p, bp->text);
83:
1.8 ! schwarze 84: /* Special box types. */
! 85:
1.3 schwarze 86: if (bp->pos == EQNPOS_SQRT) {
87: term_word(p, "sqrt");
1.5 schwarze 88: if (bp->first != NULL) {
89: p->flags |= TERMP_NOSPACE;
90: eqn_box(p, bp->first);
91: }
1.3 schwarze 92: } else if (bp->type == EQN_SUBEXPR) {
93: child = bp->first;
94: eqn_box(p, child);
95: p->flags |= TERMP_NOSPACE;
96: term_word(p, bp->pos == EQNPOS_OVER ? "/" :
97: (bp->pos == EQNPOS_SUP ||
98: bp->pos == EQNPOS_TO) ? "^" : "_");
99: p->flags |= TERMP_NOSPACE;
100: child = child->next;
1.4 schwarze 101: if (child != NULL) {
1.3 schwarze 102: eqn_box(p, child);
1.4 schwarze 103: if (bp->pos == EQNPOS_FROMTO ||
104: bp->pos == EQNPOS_SUBSUP) {
105: p->flags |= TERMP_NOSPACE;
106: term_word(p, "^");
107: p->flags |= TERMP_NOSPACE;
108: child = child->next;
109: if (child != NULL)
110: eqn_box(p, child);
111: }
1.3 schwarze 112: }
113: } else {
114: child = bp->first;
1.5 schwarze 115: if (bp->type == EQN_MATRIX &&
1.6 schwarze 116: child != NULL &&
117: child->type == EQN_LIST &&
118: child->expectargs > 1)
1.3 schwarze 119: child = child->first;
120: while (child != NULL) {
121: eqn_box(p,
122: bp->type == EQN_PILE &&
123: child->type == EQN_LIST &&
1.6 schwarze 124: child->expectargs > 1 &&
1.3 schwarze 125: child->args == 1 ?
126: child->first : child);
127: child = child->next;
128: }
129: }
1.1 schwarze 130:
1.8 ! schwarze 131: /* Handle Fonts and diacritics. */
! 132:
1.3 schwarze 133: if (bp->font != EQNFONT_NONE)
1.1 schwarze 134: term_fontpop(p);
1.7 schwarze 135: if (bp->top != NULL) {
136: p->flags |= TERMP_NOSPACE;
137: term_word(p, bp->top);
138: }
139: if (bp->bottom != NULL) {
140: p->flags |= TERMP_NOSPACE;
141: term_word(p, "_");
142: }
1.8 ! schwarze 143:
! 144: /* Right delimiter after this box? */
! 145:
! 146: if (delim) {
1.3 schwarze 147: p->flags |= TERMP_NOSPACE;
148: term_word(p, bp->right != NULL ? bp->right : ")");
149: if (bp->parent->type == EQN_SUBEXPR && bp->next != NULL)
150: p->flags |= TERMP_NOSPACE;
151: }
1.1 schwarze 152: }