-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiTeX.cpp
More file actions
217 lines (187 loc) · 9.78 KB
/
Copy pathLiTeX.cpp
File metadata and controls
217 lines (187 loc) · 9.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <iostream>
#include <string>
#include <vector>
#include "RenderEngine.cpp"
typedef unsigned long long ull;
typedef unsigned char ubyte;
typedef char byte;
std::string Between(std::string str, ubyte start, char char1, char char2, bool reverse = false, bool quiet = false) {
ubyte c1Indx = 0;
ubyte c1Count = 0;
ubyte c2Count = 0;
for (ubyte i = start; reverse ? i < ubyte(str.size()) : i >= 0; reverse ? i-- : i++) {
if (str[i] == char1) {
if (c1Count == 0) c1Indx = i;
c1Count++;
}
else if (str[i] == char2) {
c2Count++;
if (c1Count == c2Count)
return reverse
? std::string(&str[i + 1], &str[c1Indx])
: std::string(&str[c1Indx + 1], &str[i]);
}
}
if (!quiet) {
std::cerr << "\033[1;31m";
std::cerr << "Could not find contents between '"
<< std::string(1, char1)
<< "' and '"
<< std::string(1, char2)
<< "' in \"" << str
<< "\" starting at "
<< (int)start << ". ["
<< str.substr(start)
<< "] Reverse: " << (reverse ? "True" : "False")
<< std::endl;
std::cerr << "\033[0m";
}
throw new std::invalid_argument("");
}
ubyte lastFinishedBarHt = 0;
const std::string specials[5] = { "pi", "e", "im", "perm", "comb" };
Render GenerateRender(std::string eq, bool exp = false) {
std::string lead = exp ? "^" : "";
ubyte barHeight = 4;
ubyte hang = 0;
ubyte lastHeight = 0;
Render render = Render(std::vector<ull>(), ubyte(10));
for (ubyte i = 0; i < ubyte(eq.length()); i++) {
// TRIVIAL CHARS ----------------------------------------------------------------------- TRIVIAL CHARS
if (isdigit(eq[i]) || isalpha(eq[i]) ||
eq[i] == '/' || eq[i] == '*' || eq[i] == '+' || eq[i] == '-' ||
eq[i] == '`' || eq[i] == '~' || eq[i] == '.' || eq[i] == '='
) {
Render gl = ReadGlyph(lead + eq[i]);
render = AppendRenders(render, gl, barHeight);
lastHeight = gl.height;
}
// PARENTHESIS AND ABS ----------------------------------------------------------- PARENTHESIS AND ABS
else if (eq[i] == '(' || eq[i] == '[') {
char pair[2] = { eq[i], eq[i] == '(' ? ')' : ']' };
std::string contents = Between(eq, i, pair[0], pair[1]);
i += ubyte(contents.size()) + 1;
Render renderedConts = GenerateRender(contents, exp);
byte resizeBy = max(0, renderedConts.height - (exp ? 7 : 10));
// Opening parenthesis
render = AppendRenders(render,
ReadGlyph(lead + std::string(1, pair[0]), -resizeBy, pair[0] == '[', exp),
barHeight, lastFinishedBarHt
);
barHeight = max(barHeight, lastFinishedBarHt);
// Contents
render = AppendRenders(render, renderedConts, barHeight, lastFinishedBarHt);
// Closing parenthesis
render = AppendRenders(render,
ReadGlyph(lead + std::string(1, pair[1]), resizeBy, pair[0] == '[', exp),
barHeight, lastFinishedBarHt
);
lastHeight = renderedConts.height;
}
// EXPONENTS AND SUBSCRIPT --------------------------------------------------- EXPONENTS AND SUBSCRIPT
else if (eq[i] == '^' or eq[i] == '_') {
bool isPower = eq[i] == '^';
std::string contents = Between(eq, i, '{', '}');
std::cout << "Found exponent with contents " << contents << std::endl;
i += ubyte(contents.size()) + 2;
Render renderedConts = GenerateRender(contents, true);
if (isPower) {
render = AppendRenders(render, renderedConts, 0, 0, exp ? 3 : 4, lastHeight + hang);
lastHeight += renderedConts.height - (exp ? 3 : 4);
}
else {
render = AppendRenders(render, renderedConts, barHeight, renderedConts.height - (exp ? 2 : 0));
barHeight += max(0, renderedConts.height) - barHeight - (exp ? 2 : 0);
hang += renderedConts.height - 4;
}
}
// ESCAPE SEQUENCES ----------------------------------------------------------------- ESCAPE SEQUENCES
else if (eq[i] == '\\') {
std::string escSeq = "";
try { escSeq = Between(eq, i, '\\', '{', true); } catch (...) { }
// SPECIAL CHARS ----------------------------------- SPECIAL CHARS
if (escSeq == "") {
// Check if escape sequence is special character
for (int spec = 0; spec < 5 /* Number of specials */; spec++) {
std::string attempt = "";
// If the special length exceedes the length of the string, substr will throw error
try { attempt = escSeq.substr(i + 1, specials[spec].size()); } catch (...) { continue; }
// Found escape sequence that is a special character, append it
if (attempt == specials[spec]) {
escSeq = attempt;
Render gl = ReadGlyph(lead + escSeq);
render = AppendRenders(render, gl, barHeight);
lastHeight = gl.height;
break;
}
}
// No valid special was found
if (escSeq == "") {
std::cerr << "\033[1;31m";
std::cerr << "Invalid escape sequence at '" << eq.substr(i) << "'" << std::endl;
std::cerr << "\033[0m";
throw new std::invalid_argument("");
}
}
// FRACTIONS ------------------------------------------- FRACTIONS
else if (escSeq == "frac") {
std::cout << "Found fraction!" << std::endl;
i += 5; // get index to first brace
std::string numer = Between(eq, i, '{', '}');
std::string denom = Between(eq, i + numer.size() + 2, '{', '}');
i += numer.size() + 2 + denom.size() + 1; // pass expression
Render rendNumer = GenerateRender(numer, true);
Render rendDenom = GenerateRender(denom, true);
// Positive shift means denom moves, negative shift means numer moves
short shift = (rendNumer.bitmap.size() - rendDenom.bitmap.size()) / 2;
Render fraction = Render(
std::vector<ull>(max(rendNumer.bitmap.size(), rendDenom.bitmap.size()) + 2),
2 + rendNumer.height + rendDenom.height
);
fraction = MergeRenders(fraction, rendNumer, 1 - (shift < 0 ? shift : 0), rendDenom.height + 2);
fraction = MergeRenders(fraction, rendDenom, 1 + (shift > 0 ? shift : 0), 0);
Render bar = Render(std::vector<ull>(max(rendNumer.bitmap.size(), rendDenom.bitmap.size()) + 2, 1), 1);
bar.bitmap[0] = 0;
fraction = MergeRenders(fraction, bar, 0, rendDenom.height);
render = AppendRenders(render, fraction, barHeight, rendDenom.height);
barHeight = max(barHeight, rendDenom.height);
lastHeight = rendNumer.height + rendDenom.height + 2;
}
// RADICALS -------------------------------------------- RADICALS
else if (escSeq == "sqrt") {
std::cout << "Found radical!" << std::endl;
i += 5; // get index to first brace
std::string index = Between(eq, i, '{', '}');
std::string radicand = Between(eq, i + index.size() + 2, '{', '}');
i += index.size() + 2 + radicand.size() + 1; // Pass expression
Render rendIndex = GenerateRender(index, true);
Render rendRadicand = GenerateRender(radicand);
Render radical = Render(
std::vector<ull>(5 + rendIndex.bitmap.size() + rendRadicand.bitmap.size(), 0),
2 + rendRadicand.height + max(0, rendIndex.height + 2 - rendRadicand.height)
);
Render stem = Render(std::vector<ull>({
(1ull << (rendRadicand.height/2 + (rendRadicand.height % 2 != 0))) - 1,
((1ull << (rendRadicand.height / 2)) - 1) << (rendRadicand.height / 2)
}), radical.height - 1);
// Place square root "hook"
radical = MergeRenders(radical, ReadGlyph("rad"), rendIndex.bitmap.size() - 2, 0);
// Place stem
radical = MergeRenders(radical, stem, rendIndex.bitmap.size() + 1, 0);
// Place bar
radical = MergeRenders(radical, Render(std::vector<ull>(rendRadicand.bitmap.size() + 3, 1ull), 1), rendIndex.bitmap.size() + 2, rendRadicand.height);
// Place end marker
radical = MergeRenders(radical, Render(std::vector<ull>(1, 0b11ull), 2), rendRadicand.bitmap.size() + rendIndex.bitmap.size() + 4, rendRadicand.height - 2);
// Place index
radical = MergeRenders(radical, rendIndex, 0, 4);
// Place contents
radical = MergeRenders(radical, rendRadicand, rendIndex.bitmap.size() + 3, 0);
render = AppendRenders(render, radical, barHeight, lastFinishedBarHt);
barHeight = max(barHeight, lastFinishedBarHt);
lastHeight = radical.height + hang;
}
}
}
lastFinishedBarHt = barHeight;
return render;
}