Annotation of mandoc/eqn_term.c, Revision 1.14
1.14 ! schwarze 1: /* $Id: eqn_term.c,v 1.13 2017/07/08 14:51:04 schwarze Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.9 schwarze 4: * Copyright (c) 2014, 2015, 2017 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>
1.14 ! schwarze 23: #include <ctype.h>
1.1 kristaps 24: #include <stdio.h>
25: #include <stdlib.h>
26: #include <string.h>
27:
28: #include "mandoc.h"
29: #include "out.h"
30: #include "term.h"
31:
1.4 kristaps 32: static const enum termfont fontmap[EQNFONT__MAX] = {
33: TERMFONT_NONE, /* EQNFONT_NONE */
34: TERMFONT_NONE, /* EQNFONT_ROMAN */
35: TERMFONT_BOLD, /* EQNFONT_BOLD */
36: TERMFONT_BOLD, /* EQNFONT_FAT */
37: TERMFONT_UNDER /* EQNFONT_ITALIC */
38: };
39:
1.3 kristaps 40: static void eqn_box(struct termp *, const struct eqn_box *);
1.1 kristaps 41:
1.5 schwarze 42:
1.1 kristaps 43: void
1.13 schwarze 44: term_eqn(struct termp *p, const struct eqn_box *bp)
1.1 kristaps 45: {
46:
1.13 schwarze 47: eqn_box(p, bp);
1.7 schwarze 48: p->flags &= ~TERMP_NOSPACE;
1.1 kristaps 49: }
50:
51: static void
52: eqn_box(struct termp *p, const struct eqn_box *bp)
53: {
1.7 schwarze 54: const struct eqn_box *child;
1.12 schwarze 55: int delim;
56:
57: /* Delimiters around this box? */
1.1 kristaps 58:
1.10 schwarze 59: if ((bp->type == EQN_LIST && bp->expectargs > 1) ||
1.7 schwarze 60: (bp->type == EQN_PILE && (bp->prev || bp->next)) ||
1.12 schwarze 61: (bp->parent != NULL && (bp->parent->pos == EQNPOS_SQRT ||
62: /* Diacritic followed by ^ or _. */
63: ((bp->top != NULL || bp->bottom != NULL) &&
64: bp->parent->type == EQN_SUBEXPR &&
65: bp->parent->pos != EQNPOS_OVER && bp->next != NULL) ||
66: /* Nested over, sub, sup, from, to. */
67: (bp->type == EQN_SUBEXPR && bp->pos != EQNPOS_SQRT &&
68: ((bp->parent->type == EQN_LIST && bp->expectargs == 1) ||
69: (bp->parent->type == EQN_SUBEXPR &&
70: bp->pos != EQNPOS_SQRT)))))) {
1.14 ! schwarze 71: if ((bp->parent->type == EQN_SUBEXPR && bp->prev != NULL) ||
! 72: (bp->type == EQN_LIST &&
! 73: bp->first != NULL &&
! 74: bp->first->type != EQN_PILE &&
! 75: bp->first->type != EQN_MATRIX &&
! 76: bp->prev != NULL &&
! 77: (bp->prev->type == EQN_LIST ||
! 78: (bp->prev->type == EQN_TEXT &&
! 79: (*bp->prev->text == '\\' ||
! 80: isalpha((unsigned char)*bp->prev->text))))))
1.7 schwarze 81: p->flags |= TERMP_NOSPACE;
82: term_word(p, bp->left != NULL ? bp->left : "(");
83: p->flags |= TERMP_NOSPACE;
1.12 schwarze 84: delim = 1;
85: } else
86: delim = 0;
87:
88: /* Handle Fonts and text. */
89:
1.7 schwarze 90: if (bp->font != EQNFONT_NONE)
1.4 kristaps 91: term_fontpush(p, fontmap[(int)bp->font]);
92:
1.7 schwarze 93: if (bp->text != NULL)
1.4 kristaps 94: term_word(p, bp->text);
1.1 kristaps 95:
1.12 schwarze 96: /* Special box types. */
97:
1.7 schwarze 98: if (bp->pos == EQNPOS_SQRT) {
99: term_word(p, "sqrt");
1.9 schwarze 100: if (bp->first != NULL) {
101: p->flags |= TERMP_NOSPACE;
102: eqn_box(p, bp->first);
103: }
1.7 schwarze 104: } else if (bp->type == EQN_SUBEXPR) {
105: child = bp->first;
106: eqn_box(p, child);
107: p->flags |= TERMP_NOSPACE;
108: term_word(p, bp->pos == EQNPOS_OVER ? "/" :
109: (bp->pos == EQNPOS_SUP ||
110: bp->pos == EQNPOS_TO) ? "^" : "_");
111: child = child->next;
1.8 schwarze 112: if (child != NULL) {
1.14 ! schwarze 113: p->flags |= TERMP_NOSPACE;
1.7 schwarze 114: eqn_box(p, child);
1.8 schwarze 115: if (bp->pos == EQNPOS_FROMTO ||
116: bp->pos == EQNPOS_SUBSUP) {
117: p->flags |= TERMP_NOSPACE;
118: term_word(p, "^");
119: p->flags |= TERMP_NOSPACE;
120: child = child->next;
121: if (child != NULL)
122: eqn_box(p, child);
123: }
1.7 schwarze 124: }
125: } else {
126: child = bp->first;
1.9 schwarze 127: if (bp->type == EQN_MATRIX &&
1.10 schwarze 128: child != NULL &&
129: child->type == EQN_LIST &&
130: child->expectargs > 1)
1.7 schwarze 131: child = child->first;
132: while (child != NULL) {
133: eqn_box(p,
134: bp->type == EQN_PILE &&
135: child->type == EQN_LIST &&
1.10 schwarze 136: child->expectargs > 1 &&
1.7 schwarze 137: child->args == 1 ?
138: child->first : child);
139: child = child->next;
140: }
141: }
1.1 kristaps 142:
1.12 schwarze 143: /* Handle Fonts and diacritics. */
144:
1.7 schwarze 145: if (bp->font != EQNFONT_NONE)
1.4 kristaps 146: term_fontpop(p);
1.11 schwarze 147: if (bp->top != NULL) {
148: p->flags |= TERMP_NOSPACE;
149: term_word(p, bp->top);
150: }
151: if (bp->bottom != NULL) {
152: p->flags |= TERMP_NOSPACE;
153: term_word(p, "_");
154: }
1.12 schwarze 155:
156: /* Right delimiter after this box? */
157:
158: if (delim) {
1.7 schwarze 159: p->flags |= TERMP_NOSPACE;
160: term_word(p, bp->right != NULL ? bp->right : ")");
161: if (bp->parent->type == EQN_SUBEXPR && bp->next != NULL)
162: p->flags |= TERMP_NOSPACE;
163: }
1.1 kristaps 164: }
CVSweb