Annotation of mandoc/eqn_term.c, Revision 1.16
1.16 ! schwarze 1: /* $Id: eqn_term.c,v 1.15 2017/08/23 20:29:42 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 &&
! 99: ((cp[-1] == '-' && bp->prev == NULL) ||
! 100: strchr("\"'([{", cp[-1]) != NULL))
1.15 schwarze 101: p->flags |= TERMP_NOSPACE;
102: }
1.1 kristaps 103:
1.12 schwarze 104: /* Special box types. */
105:
1.7 schwarze 106: if (bp->pos == EQNPOS_SQRT) {
107: term_word(p, "sqrt");
1.9 schwarze 108: if (bp->first != NULL) {
109: p->flags |= TERMP_NOSPACE;
110: eqn_box(p, bp->first);
111: }
1.7 schwarze 112: } else if (bp->type == EQN_SUBEXPR) {
113: child = bp->first;
114: eqn_box(p, child);
115: p->flags |= TERMP_NOSPACE;
116: term_word(p, bp->pos == EQNPOS_OVER ? "/" :
117: (bp->pos == EQNPOS_SUP ||
118: bp->pos == EQNPOS_TO) ? "^" : "_");
119: child = child->next;
1.8 schwarze 120: if (child != NULL) {
1.14 schwarze 121: p->flags |= TERMP_NOSPACE;
1.7 schwarze 122: eqn_box(p, child);
1.8 schwarze 123: if (bp->pos == EQNPOS_FROMTO ||
124: bp->pos == EQNPOS_SUBSUP) {
125: p->flags |= TERMP_NOSPACE;
126: term_word(p, "^");
127: p->flags |= TERMP_NOSPACE;
128: child = child->next;
129: if (child != NULL)
130: eqn_box(p, child);
131: }
1.7 schwarze 132: }
133: } else {
134: child = bp->first;
1.9 schwarze 135: if (bp->type == EQN_MATRIX &&
1.10 schwarze 136: child != NULL &&
137: child->type == EQN_LIST &&
138: child->expectargs > 1)
1.7 schwarze 139: child = child->first;
140: while (child != NULL) {
141: eqn_box(p,
142: bp->type == EQN_PILE &&
143: child->type == EQN_LIST &&
1.10 schwarze 144: child->expectargs > 1 &&
1.7 schwarze 145: child->args == 1 ?
146: child->first : child);
147: child = child->next;
148: }
149: }
1.1 kristaps 150:
1.12 schwarze 151: /* Handle Fonts and diacritics. */
152:
1.7 schwarze 153: if (bp->font != EQNFONT_NONE)
1.4 kristaps 154: term_fontpop(p);
1.11 schwarze 155: if (bp->top != NULL) {
156: p->flags |= TERMP_NOSPACE;
157: term_word(p, bp->top);
158: }
159: if (bp->bottom != NULL) {
160: p->flags |= TERMP_NOSPACE;
161: term_word(p, "_");
162: }
1.12 schwarze 163:
164: /* Right delimiter after this box? */
165:
166: if (delim) {
1.7 schwarze 167: p->flags |= TERMP_NOSPACE;
168: term_word(p, bp->right != NULL ? bp->right : ")");
169: if (bp->parent->type == EQN_SUBEXPR && bp->next != NULL)
170: p->flags |= TERMP_NOSPACE;
171: }
1.1 kristaps 172: }
CVSweb