1 |
|
|
/* $OpenBSD: eqn_term.c,v 1.13 2017/08/23 21:56:04 schwarze Exp $ */ |
2 |
|
|
/* |
3 |
|
|
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv> |
4 |
|
|
* Copyright (c) 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org> |
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 <sys/types.h> |
19 |
|
|
|
20 |
|
|
#include <assert.h> |
21 |
|
|
#include <ctype.h> |
22 |
|
|
#include <stdio.h> |
23 |
|
|
#include <stdlib.h> |
24 |
|
|
#include <string.h> |
25 |
|
|
|
26 |
|
|
#include "mandoc.h" |
27 |
|
|
#include "out.h" |
28 |
|
|
#include "term.h" |
29 |
|
|
|
30 |
|
|
static const enum termfont fontmap[EQNFONT__MAX] = { |
31 |
|
|
TERMFONT_NONE, /* EQNFONT_NONE */ |
32 |
|
|
TERMFONT_NONE, /* EQNFONT_ROMAN */ |
33 |
|
|
TERMFONT_BOLD, /* EQNFONT_BOLD */ |
34 |
|
|
TERMFONT_BOLD, /* EQNFONT_FAT */ |
35 |
|
|
TERMFONT_UNDER /* EQNFONT_ITALIC */ |
36 |
|
|
}; |
37 |
|
|
|
38 |
|
|
static void eqn_box(struct termp *, const struct eqn_box *); |
39 |
|
|
|
40 |
|
|
|
41 |
|
|
void |
42 |
|
|
term_eqn(struct termp *p, const struct eqn_box *bp) |
43 |
|
|
{ |
44 |
|
|
|
45 |
|
612 |
eqn_box(p, bp); |
46 |
|
306 |
p->flags &= ~TERMP_NOSPACE; |
47 |
|
306 |
} |
48 |
|
|
|
49 |
|
|
static void |
50 |
|
|
eqn_box(struct termp *p, const struct eqn_box *bp) |
51 |
|
|
{ |
52 |
|
|
const struct eqn_box *child; |
53 |
|
|
const char *cp; |
54 |
|
|
int delim; |
55 |
|
|
|
56 |
|
|
/* Delimiters around this box? */ |
57 |
|
|
|
58 |
✓✓✓✓ ✓✗ |
8109 |
if ((bp->type == EQN_LIST && bp->expectargs > 1) || |
59 |
✓✓✓✓ ✓✓ |
3636 |
(bp->type == EQN_PILE && (bp->prev || bp->next)) || |
60 |
✓✓✓✓
|
6804 |
(bp->parent != NULL && (bp->parent->pos == EQNPOS_SQRT || |
61 |
|
|
/* Diacritic followed by ^ or _. */ |
62 |
✓✓✓✓
|
6282 |
((bp->top != NULL || bp->bottom != NULL) && |
63 |
✓✓ |
189 |
bp->parent->type == EQN_SUBEXPR && |
64 |
✓✓✓✓
|
162 |
bp->parent->pos != EQNPOS_OVER && bp->next != NULL) || |
65 |
|
|
/* Nested over, sub, sup, from, to. */ |
66 |
✓✓✓✓
|
3663 |
(bp->type == EQN_SUBEXPR && bp->pos != EQNPOS_SQRT && |
67 |
✓✓✓✗
|
504 |
((bp->parent->type == EQN_LIST && bp->expectargs == 1) || |
68 |
✓✓ |
423 |
(bp->parent->type == EQN_SUBEXPR && |
69 |
|
117 |
bp->pos != EQNPOS_SQRT)))))) { |
70 |
✓✓✓✓ ✗✗ |
576 |
if ((bp->parent->type == EQN_SUBEXPR && bp->prev != NULL) || |
71 |
✓✓ |
216 |
(bp->type == EQN_LIST && |
72 |
✓✓ |
144 |
bp->first != NULL && |
73 |
✓✓ |
135 |
bp->first->type != EQN_PILE && |
74 |
✓✓ |
126 |
bp->first->type != EQN_MATRIX && |
75 |
✓✓ |
99 |
bp->prev != NULL && |
76 |
✗✓ |
9 |
(bp->prev->type == EQN_LIST || |
77 |
|
|
(bp->prev->type == EQN_TEXT && |
78 |
|
|
(*bp->prev->text == '\\' || |
79 |
|
|
isalpha((unsigned char)*bp->prev->text)))))) |
80 |
|
126 |
p->flags |= TERMP_NOSPACE; |
81 |
✓✓ |
711 |
term_word(p, bp->left != NULL ? bp->left : "("); |
82 |
|
333 |
p->flags |= TERMP_NOSPACE; |
83 |
|
|
delim = 1; |
84 |
|
333 |
} else |
85 |
|
|
delim = 0; |
86 |
|
|
|
87 |
|
|
/* Handle Fonts and text. */ |
88 |
|
|
|
89 |
✓✓ |
3699 |
if (bp->font != EQNFONT_NONE) |
90 |
|
2601 |
term_fontpush(p, fontmap[(int)bp->font]); |
91 |
|
|
|
92 |
✓✓ |
3699 |
if (bp->text != NULL) { |
93 |
✓✓ |
2250 |
if (strchr("!\"'),.:;?]}", *bp->text) != NULL) |
94 |
|
63 |
p->flags |= TERMP_NOSPACE; |
95 |
|
2250 |
term_word(p, bp->text); |
96 |
✓✓✗✓
|
2412 |
if ((cp = strchr(bp->text, '\0')) > bp->text && |
97 |
✓✗ |
2241 |
(strchr("\"'([{", cp[-1]) != NULL || |
98 |
✓✓✓✗
|
3114 |
(bp->prev == NULL && (cp[-1] == '-' || |
99 |
✓✓ |
873 |
(cp >= bp->text + 5 && |
100 |
|
162 |
strcmp(cp - 5, "\\[mi]") == 0))))) |
101 |
|
|
p->flags |= TERMP_NOSPACE; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
/* Special box types. */ |
105 |
|
|
|
106 |
✓✓ |
3699 |
if (bp->pos == EQNPOS_SQRT) { |
107 |
|
63 |
term_word(p, "sqrt"); |
108 |
✓✓ |
63 |
if (bp->first != NULL) { |
109 |
|
54 |
p->flags |= TERMP_NOSPACE; |
110 |
|
54 |
eqn_box(p, bp->first); |
111 |
|
54 |
} |
112 |
✓✓ |
3636 |
} else if (bp->type == EQN_SUBEXPR) { |
113 |
|
|
child = bp->first; |
114 |
|
423 |
eqn_box(p, child); |
115 |
|
423 |
p->flags |= TERMP_NOSPACE; |
116 |
✓✓ |
1170 |
term_word(p, bp->pos == EQNPOS_OVER ? "/" : |
117 |
✓✓ |
891 |
(bp->pos == EQNPOS_SUP || |
118 |
|
243 |
bp->pos == EQNPOS_TO) ? "^" : "_"); |
119 |
|
423 |
child = child->next; |
120 |
✓✓ |
423 |
if (child != NULL) { |
121 |
|
387 |
p->flags |= TERMP_NOSPACE; |
122 |
|
387 |
eqn_box(p, child); |
123 |
✓✓✓✓
|
729 |
if (bp->pos == EQNPOS_FROMTO || |
124 |
|
342 |
bp->pos == EQNPOS_SUBSUP) { |
125 |
|
108 |
p->flags |= TERMP_NOSPACE; |
126 |
|
108 |
term_word(p, "^"); |
127 |
|
108 |
p->flags |= TERMP_NOSPACE; |
128 |
|
108 |
child = child->next; |
129 |
✓✓ |
108 |
if (child != NULL) |
130 |
|
90 |
eqn_box(p, child); |
131 |
|
|
} |
132 |
|
|
} |
133 |
|
|
} else { |
134 |
|
|
child = bp->first; |
135 |
✓✓✓✗
|
6444 |
if (bp->type == EQN_MATRIX && |
136 |
|
3213 |
child != NULL && |
137 |
✓✗ |
18 |
child->type == EQN_LIST && |
138 |
|
18 |
child->expectargs > 1) |
139 |
|
18 |
child = child->first; |
140 |
✓✓ |
8091 |
while (child != NULL) { |
141 |
|
2439 |
eqn_box(p, |
142 |
✓✓✓✗
|
7389 |
bp->type == EQN_PILE && |
143 |
✓✗ |
72 |
child->type == EQN_LIST && |
144 |
✓✗ |
72 |
child->expectargs > 1 && |
145 |
|
72 |
child->args == 1 ? |
146 |
|
72 |
child->first : child); |
147 |
|
2439 |
child = child->next; |
148 |
|
|
} |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
/* Handle Fonts and diacritics. */ |
152 |
|
|
|
153 |
✓✓ |
3699 |
if (bp->font != EQNFONT_NONE) |
154 |
|
2601 |
term_fontpop(p); |
155 |
✓✓ |
3699 |
if (bp->top != NULL) { |
156 |
|
171 |
p->flags |= TERMP_NOSPACE; |
157 |
|
171 |
term_word(p, bp->top); |
158 |
|
171 |
} |
159 |
✓✓ |
3699 |
if (bp->bottom != NULL) { |
160 |
|
27 |
p->flags |= TERMP_NOSPACE; |
161 |
|
27 |
term_word(p, "_"); |
162 |
|
27 |
} |
163 |
|
|
|
164 |
|
|
/* Right delimiter after this box? */ |
165 |
|
|
|
166 |
✓✓ |
3699 |
if (delim) { |
167 |
|
333 |
p->flags |= TERMP_NOSPACE; |
168 |
✓✓ |
711 |
term_word(p, bp->right != NULL ? bp->right : ")"); |
169 |
✓✓✓✓
|
576 |
if (bp->parent->type == EQN_SUBEXPR && bp->next != NULL) |
170 |
|
99 |
p->flags |= TERMP_NOSPACE; |
171 |
|
|
} |
172 |
|
3699 |
} |