Annotation of mandoc/eqn_term.c, Revision 1.17
1.17 ! schwarze 1: /* $Id: eqn_term.c,v 1.16 2017/08/23 20:49:15 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.16 schwarze 55: const char *cp;
1.12 schwarze 56: int delim;
57:
58: /* Delimiters around this box? */
1.1 kristaps 59:
1.10 schwarze 60: if ((bp->type == EQN_LIST && bp->expectargs > 1) ||
1.7 schwarze 61: (bp->type == EQN_PILE && (bp->prev || bp->next)) ||
1.12 schwarze 62: (bp->parent != NULL && (bp->parent->pos == EQNPOS_SQRT ||
63: /* Diacritic followed by ^ or _. */
64: ((bp->top != NULL || bp->bottom != NULL) &&
65: bp->parent->type == EQN_SUBEXPR &&
66: bp->parent->pos != EQNPOS_OVER && bp->next != NULL) ||
67: /* Nested over, sub, sup, from, to. */
68: (bp->type == EQN_SUBEXPR && bp->pos != EQNPOS_SQRT &&
69: ((bp->parent->type == EQN_LIST && bp->expectargs == 1) ||
70: (bp->parent->type == EQN_SUBEXPR &&
71: bp->pos != EQNPOS_SQRT)))))) {
1.14 schwarze 72: if ((bp->parent->type == EQN_SUBEXPR && bp->prev != NULL) ||
73: (bp->type == EQN_LIST &&
74: bp->first != NULL &&
75: bp->first->type != EQN_PILE &&
76: bp->first->type != EQN_MATRIX &&
77: bp->prev != NULL &&
78: (bp->prev->type == EQN_LIST ||
79: (bp->prev->type == EQN_TEXT &&
80: (*bp->prev->text == '\\' ||
81: isalpha((unsigned char)*bp->prev->text))))))
1.7 schwarze 82: p->flags |= TERMP_NOSPACE;
83: term_word(p, bp->left != NULL ? bp->left : "(");
84: p->flags |= TERMP_NOSPACE;
1.12 schwarze 85: delim = 1;
86: } else
87: delim = 0;
88:
89: /* Handle Fonts and text. */
90:
1.7 schwarze 91: if (bp->font != EQNFONT_NONE)
1.4 kristaps 92: term_fontpush(p, fontmap[(int)bp->font]);
93:
1.15 schwarze 94: if (bp->text != NULL) {
95: if (strchr("!\"'),.:;?]}", *bp->text) != NULL)
96: p->flags |= TERMP_NOSPACE;
1.4 kristaps 97: term_word(p, bp->text);
1.16 schwarze 98: if ((cp = strchr(bp->text, '\0')) > bp->text &&
1.17 ! schwarze 99: (strchr("\"'([{", cp[-1]) != NULL ||
! 100: (bp->prev == NULL && (cp[-1] == '-' ||
! 101: (cp >= bp->text + 5 &&
! 102: strcmp(cp - 5, "\\[mi]") == 0)))))
1.15 schwarze 103: p->flags |= TERMP_NOSPACE;
104: }
1.1 kristaps 105:
1.12 schwarze 106: /* Special box types. */
107:
1.7 schwarze 108: if (bp->pos == EQNPOS_SQRT) {
109: term_word(p, "sqrt");
1.9 schwarze 110: if (bp->first != NULL) {
111: p->flags |= TERMP_NOSPACE;
112: eqn_box(p, bp->first);
113: }
1.7 schwarze 114: } else if (bp->type == EQN_SUBEXPR) {
115: child = bp->first;
116: eqn_box(p, child);
117: p->flags |= TERMP_NOSPACE;
118: term_word(p, bp->pos == EQNPOS_OVER ? "/" :
119: (bp->pos == EQNPOS_SUP ||
120: bp->pos == EQNPOS_TO) ? "^" : "_");
121: child = child->next;
1.8 schwarze 122: if (child != NULL) {
1.14 schwarze 123: p->flags |= TERMP_NOSPACE;
1.7 schwarze 124: eqn_box(p, child);
1.8 schwarze 125: if (bp->pos == EQNPOS_FROMTO ||
126: bp->pos == EQNPOS_SUBSUP) {
127: p->flags |= TERMP_NOSPACE;
128: term_word(p, "^");
129: p->flags |= TERMP_NOSPACE;
130: child = child->next;
131: if (child != NULL)
132: eqn_box(p, child);
133: }
1.7 schwarze 134: }
135: } else {
136: child = bp->first;
1.9 schwarze 137: if (bp->type == EQN_MATRIX &&
1.10 schwarze 138: child != NULL &&
139: child->type == EQN_LIST &&
140: child->expectargs > 1)
1.7 schwarze 141: child = child->first;
142: while (child != NULL) {
143: eqn_box(p,
144: bp->type == EQN_PILE &&
145: child->type == EQN_LIST &&
1.10 schwarze 146: child->expectargs > 1 &&
1.7 schwarze 147: child->args == 1 ?
148: child->first : child);
149: child = child->next;
150: }
151: }
1.1 kristaps 152:
1.12 schwarze 153: /* Handle Fonts and diacritics. */
154:
1.7 schwarze 155: if (bp->font != EQNFONT_NONE)
1.4 kristaps 156: term_fontpop(p);
1.11 schwarze 157: if (bp->top != NULL) {
158: p->flags |= TERMP_NOSPACE;
159: term_word(p, bp->top);
160: }
161: if (bp->bottom != NULL) {
162: p->flags |= TERMP_NOSPACE;
163: term_word(p, "_");
164: }
1.12 schwarze 165:
166: /* Right delimiter after this box? */
167:
168: if (delim) {
1.7 schwarze 169: p->flags |= TERMP_NOSPACE;
170: term_word(p, bp->right != NULL ? bp->right : ")");
171: if (bp->parent->type == EQN_SUBEXPR && bp->next != NULL)
172: p->flags |= TERMP_NOSPACE;
173: }
1.1 kristaps 174: }
CVSweb