forked from floft/sprouts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtext.cpp
457 lines (385 loc) · 7.37 KB
/
text.cpp
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include "text.h"
#include "texture_atlas.h"
#include <unordered_map>
#include "util.h"
#include <iostream>
/**************
*Description:
*Input:
*Output:
**************/
const Text::TextProperties Text::defaultTextProperties = Text::TextProperties();
namespace
{
const float charWidth = 1, charHeight = 1;
const TextureAtlas & Font = TextureAtlas::Font8x8;
const int fontWidth = 8, fontHeight = 8;
const int textureXRes = 128, textureYRes = 128;
const float pixelOffset = TextureAtlas::pixelOffset;
const wchar_t topPageTranslations[128] =
{
0x00C7,
0x00FC,
0x00E9,
0x00E2,
0x00E4,
0x00E0,
0x00E5,
0x00E7,
0x00EA,
0x00EB,
0x00E8,
0x00EF,
0x00EE,
0x00EC,
0x00C4,
0x00C5,
0x00C9,
0x00E6,
0x00C6,
0x00F4,
0x00F6,
0x00F2,
0x00FB,
0x00F9,
0x00FF,
0x00D6,
0x00DC,
0x00A2,
0x00A3,
0x00A5,
0x20A7,
0x0192,
0x00E1,
0x00ED,
0x00F3,
0x00FA,
0x00F1,
0x00D1,
0x00AA,
0x00BA,
0x00BF,
0x2310,
0x00AC,
0x00BD,
0x00BC,
0x00A1,
0x00AB,
0x00BB,
0x2591,
0x2592,
0x2593,
0x2502,
0x2524,
0x2561,
0x2562,
0x2556,
0x2555,
0x2563,
0x2551,
0x2557,
0x255D,
0x255C,
0x255B,
0x2510,
0x2514,
0x2534,
0x252C,
0x251C,
0x2500,
0x253C,
0x255E,
0x255F,
0x255A,
0x2554,
0x2569,
0x2566,
0x2560,
0x2550,
0x256C,
0x2567,
0x2568,
0x2564,
0x2565,
0x2559,
0x2558,
0x2552,
0x2553,
0x256B,
0x256A,
0x2518,
0x250C,
0x2588,
0x2584,
0x258C,
0x2590,
0x2580,
0x03B1,
0x00DF,
0x0393,
0x03C0,
0x03A3,
0x03C3,
0x00B5,
0x03C4,
0x03A6,
0x0398,
0x03A9,
0x03B4,
0x221E,
0x03C6,
0x03B5,
0x2229,
0x2261,
0x00B1,
0x2265,
0x2264,
0x2320,
0x2321,
0x00F7,
0x2248,
0x00B0,
0x2219,
0x00B7,
0x221A,
0x207F,
0x00B2,
0x25A0,
0x00A0
};
unordered_map<wchar_t, int> topPageTranslationMap;
initializer init2([]()
{
for(int i = 0x80; i < 0x100; i++)
{
topPageTranslationMap[topPageTranslations[i - 0x80]] = i;
}
});
int translateToCodePage437(wchar_t ch)
{
int character = (int)ch;
if(character == 0)
{
return character;
}
if(character >= 0x20 && character <= 0x7E)
{
return character;
}
switch(character)
{
case 0x263A:
return 0x01;
case 0x263B:
return 0x02;
case 0x2665:
return 0x03;
case 0x2666:
return 0x04;
case 0x2663:
return 0x05;
case 0x2660:
return 0x06;
case 0x2022:
return 0x07;
case 0x25D8:
return 0x08;
case 0x25CB:
return 0x09;
case 0x25D9:
return 0x0A;
case 0x2642:
return 0x0B;
case 0x2640:
return 0x0C;
case 0x266A:
return 0x0D;
case 0x266B:
return 0x0E;
case 0x263C:
return 0x0F;
case 0x25BA:
return 0x10;
case 0x25C4:
return 0x11;
case 0x2195:
return 0x12;
case 0x203C:
return 0x13;
case 0x00B6:
return 0x14;
case 0x00A7:
return 0x15;
case 0x25AC:
return 0x16;
case 0x21A8:
return 0x17;
case 0x2191:
return 0x18;
case 0x2193:
return 0x19;
case 0x2192:
return 0x1A;
case 0x2190:
return 0x1B;
case 0x221F:
return 0x1C;
case 0x2194:
return 0x1D;
case 0x25B2:
return 0x1E;
case 0x25BC:
return 0x1F;
case 0x2302:
return 0x7F;
case 0x03B2:
return 0xE1;
case 0x03A0:
case 0x220F:
return 0xE3;
case 0x2211:
return 0xE4;
case 0x03BC:
return 0xE6;
case 0x2126:
return 0xEA;
case 0x00F0:
case 0x2202:
return 0xEB;
case 0x2205:
case 0x03D5:
case 0x2300:
case 0x00F8:
return 0xED;
case 0x2208:
case 0x20AC:
return 0xEE;
default:
{
try
{
return topPageTranslationMap.at(character);
}
catch(out_of_range &e)
{
return (int)'?';
}
}
}
}
Mesh charMesh[256];
bool didInit = false;
void init()
{
if(didInit)
{
return;
}
didInit = true;
for(size_t i = 0; i < sizeof(charMesh) / sizeof(charMesh[0]); i++)
{
int left = (i % (textureXRes / fontWidth)) * fontWidth;
int top = (i / (textureXRes / fontWidth)) * fontHeight;
const int width = fontWidth;
const int height = fontHeight;
float minU = (left + pixelOffset) / textureXRes;
float maxU = (left + width - pixelOffset) / textureXRes;
float minV = 1 - (top + height - pixelOffset) / textureYRes;
float maxV = 1 - (top + pixelOffset) / textureYRes;
TextureDescriptor texture = Font.tdNoOffset();
texture = texture.subTexture(minU, maxU, minV, maxV);
charMesh[i] = Generate::quadrilateral(texture, VectorF(0, 0, 0), Color(1), VectorF(1, 0, 0), Color(1), VectorF(1, 1, 0), Color(1), VectorF(0, 1, 0), Color(1));
}
}
void renderChar(Mesh dest, Matrix m, Color color, wchar_t ch)
{
init();
//cout << "char:" << (char)ch << " pt:" << transform(m, VectorF(0, 0, 0)) << endl;
dest->add(scaleColors(color, transform(m, charMesh[translateToCodePage437(ch)])));
}
bool updateFromChar(float &x, float &y, float &w, float &h, wchar_t ch, const Text::TextProperties &properties)
{
if(ch == L'\n')
{
x = 0;
y += charHeight;
}
else if(ch == L'\r')
{
x = 0;
}
else if(ch == L'\t')
{
x = floor(x / properties.tabWidth + 1) * properties.tabWidth;
}
else if(ch == L' ')
{
x += charWidth;
}
else
{
x += charWidth;
if(x > w)
{
w = x;
}
if(y + charHeight > h)
{
h = y + charHeight;
}
return true;
}
return false;
}
}
float Text::width(wstring str, const TextProperties &properties)
{
float x = 0, y = 0, w = 0, h = 0;
for(wchar_t ch : str)
{
updateFromChar(x, y, w, h, ch, properties);
}
return w;
}
float Text::height(wstring str, const TextProperties &properties)
{
float x = 0, y = 0, w = 0, h = 0;
for(wchar_t ch : str)
{
updateFromChar(x, y, w, h, ch, properties);
}
return h;
}
float Text::xPos(wstring str, const TextProperties &properties)
{
float x = 0, y = 0, w = 0, h = 0;
for(wchar_t ch : str)
{
updateFromChar(x, y, w, h, ch, properties);
}
return x;
}
float Text::yPos(wstring str, const TextProperties &properties)
{
float x = 0, y = 0, w = 0, h = 0;
for(wchar_t ch : str)
{
updateFromChar(x, y, w, h, ch, properties);
}
return h - y - 1;
}
Mesh Text::mesh(wstring str, Color color, const TextProperties &properties)
{
float x = 0, y = 0, w = 0, h = 0;
float totalHeight = height(str, properties);
Mesh retval(new Mesh_t());
for(wchar_t ch : str)
{
Matrix mat = Matrix::translate(x, totalHeight - y - 1, 0);
if(updateFromChar(x, y, w, h, ch, properties))
{
renderChar(retval, mat, color, ch);
}
}
return retval;
}