-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtextsurface.cpp
More file actions
452 lines (392 loc) · 13.2 KB
/
textsurface.cpp
File metadata and controls
452 lines (392 loc) · 13.2 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
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
#include <fea/rendering/textsurface.hpp>
#include <fea/rendering/font.hpp>
#include <fea/assert.hpp>
#include <iostream>
#include <cwctype>
std::wstring utf8_to_utf16(const std::string& utf8)
{
std::vector<unsigned long> unicode;
size_t i = 0;
while (i < utf8.size())
{
unsigned long uni;
size_t todo;
unsigned char ch = utf8[i++];
if (ch <= 0x7F)
{
uni = ch;
todo = 0;
}
else if (ch <= 0xBF)
{
throw std::logic_error("not a UTF-8 string");
}
else if (ch <= 0xDF)
{
uni = ch&0x1F;
todo = 1;
}
else if (ch <= 0xEF)
{
uni = ch&0x0F;
todo = 2;
}
else if (ch <= 0xF7)
{
uni = ch&0x07;
todo = 3;
}
else
{
throw std::logic_error("not a UTF-8 string");
}
for (size_t j = 0; j < todo; ++j)
{
if (i == utf8.size())
throw std::logic_error("not a UTF-8 string");
unsigned char ch = utf8[i++];
if (ch < 0x80 || ch > 0xBF)
throw std::logic_error("not a UTF-8 string");
uni <<= 6;
uni += ch & 0x3F;
}
if (uni >= 0xD800 && uni <= 0xDFFF)
throw std::logic_error("not a UTF-8 string");
if (uni > 0x10FFFF)
throw std::logic_error("not a UTF-8 string");
unicode.push_back(uni);
}
std::wstring utf16;
for (size_t i = 0; i < unicode.size(); ++i)
{
unsigned long uni = unicode[i];
if (uni <= 0xFFFF)
{
utf16 += (wchar_t)uni;
}
else
{
uni -= 0x10000;
utf16 += (wchar_t)((uni >> 10) + 0xD800);
utf16 += (wchar_t)((uni & 0x3FF) + 0xDC00);
}
}
return utf16;
}
namespace fea
{
TextSurface::Writing::Writing(const std::wstring& text, const Font* font, const glm::vec2& penPosition, const float scale, const Color& color, bool positionSet)
:
mText(text),
mFont(font),
mPenPosition(penPosition),
mScale(scale),
mColor(color),
mPositionSet(positionSet)
{
}
TextSurface::TextSurface() :
mScale(1.0f),
mPenSet(true),
mHorizontalAlign(0.0f),
mLineHeight(0.0f),
mLineWidth(0.0f),
mWordWrap(false)
{
mAtlasSize = 64;
mAtlas = texture_atlas_new(mAtlasSize, mAtlasSize, 1);
mDrawMode = GL_TRIANGLES;
mCurrentFont = nullptr;
}
TextSurface::~TextSurface()
{
if(mAtlas)
texture_atlas_delete(mAtlas);
for(auto& cached : mFontCache)
{
texture_font_delete(cached.second);
}
}
TextSurface::TextSurface(TextSurface&& other)
{
mAtlas = other.mAtlas;
mCurrentFont = other.mCurrentFont;
mPenPosition = other.mPenPosition;
mScale = other.mScale;
mColor = other.mColor;
mHorizontalAlign = other.mHorizontalAlign;
mLineHeight = other.mLineHeight;
mLineWidth = other.mLineWidth;
mWordWrap = other.mWordWrap;
mAtlasSize = other.mAtlasSize;
mFontCache = std::move(other.mFontCache);
mWritings = std::move(other.mWritings);
mLowBounds = other.mLowBounds;
mHighBounds = other.mHighBounds;
other.mAtlas = nullptr;
}
TextSurface& TextSurface::operator=(TextSurface&& other)
{
mAtlas = other.mAtlas;
mCurrentFont = other.mCurrentFont;
mPenPosition = other.mPenPosition;
mScale = other.mScale;
mColor = other.mColor;
mHorizontalAlign = other.mHorizontalAlign;
mLineHeight = other.mLineHeight;
mLineWidth = other.mLineWidth;
mWordWrap = other.mWordWrap;
mAtlasSize = other.mAtlasSize;
mFontCache = std::move(other.mFontCache);
mWritings = std::move(other.mWritings);
mLowBounds = other.mLowBounds;
mHighBounds = other.mHighBounds;
other.mAtlas = nullptr;
return *this;
}
void TextSurface::write(const std::string& text)
{
write(utf8_to_utf16(text));
}
void TextSurface::write(const std::wstring& text)
{
FEA_ASSERT(mCurrentFont != nullptr, "Cannot write text with no font set!\n");
mWritings.push_back(Writing(text, mCurrentFont, mPenSetPosition, mScale, mColor, mPenSet));
addText(text);
mPenSet = false;
}
void TextSurface::setPenFont(const Font& font)
{
cacheFont(font);
mCurrentFont = &font;
}
void TextSurface::setPenPosition(const glm::vec2 position)
{
mPenPosition = position;
mPenSetPosition = position;
mPenSet = true;
}
void TextSurface::setPenScale(const float scale)
{
mScale = scale;
}
void TextSurface::setPenColor(const Color& color)
{
mColor = color;
}
void TextSurface::setHorizontalAlign(const float coord)
{
mHorizontalAlign = coord;
}
void TextSurface::setLineHeight(float height)
{
mLineHeight = height;
}
void TextSurface::setLineWidth(float width)
{
mLineWidth = width;
}
void TextSurface::enableWordWrap(bool enabled)
{
mWordWrap = enabled;
}
void TextSurface::newLine(const float distance, const float indentation)
{
mPenPosition.x = mHorizontalAlign + indentation * mScale;
mPenPosition.y += distance * mScale;
mPenSetPosition = mPenPosition;
mPenSet = true;
}
void TextSurface::newLine()
{
mPenPosition.x = mHorizontalAlign;
mPenPosition.y += mLineHeight * mScale;
mPenSetPosition = mPenPosition;
mPenSet = true;
}
std::vector<RenderEntity> TextSurface::getRenderInfo() const
{
std::vector<RenderEntity> temp = Drawable2D::getRenderInfo();
temp[0].mUniforms.emplace_back("texture", TEXTURE, mAtlas->id);
return temp;
}
void TextSurface::rewrite()
{
mVertices.clear();
mTexCoords.clear();
mVertexColors.clear();
//const glm::vec2 originalPosition = mPenPosition;
//const Font* originalFont = mCurrentFont;
//const float originalScale = mScale;
//const Color originalColor = mColor;
for(auto& writing : mWritings)
{
if(writing.mPositionSet)
mPenPosition = writing.mPenPosition;
mCurrentFont = writing.mFont;
cacheFont(*mCurrentFont);
mScale = writing.mScale;
mColor = writing.mColor;
addText(writing.mText);
}
//mPenPosition = originalPosition;
//mCurrentFont = originalFont;
//mScale = originalScale;
//mColor = originalColor;
}
void TextSurface::clear()
{
mVertices.clear();
mTexCoords.clear();
mVertexColors.clear();
mWritings.clear();
mLowBounds = glm::vec2();
mHighBounds = glm::vec2();
}
glm::vec2 TextSurface::getSize()
{
return glm::vec2(mHighBounds.x - mLowBounds.x, mHighBounds.y - mLowBounds.y);
}
void TextSurface::addText(const std::wstring& text)
{
std::vector<float> verticesToAdd;
std::vector<float> texCoordsToAdd;
std::vector<float> colorsToAdd;
glm::vec2 penTempPosition = mPenPosition;
//make sure all glyphs are available
for(int32_t i = 0; i < text.size(); ++i )
{
texture_glyph_t* glyph = texture_font_get_glyph( mFontCache.at(*mCurrentFont), text[i] );
if(glyph == nullptr)
{
mAtlasSize *= 2;
texture_atlas_delete(mAtlas);
mAtlas = texture_atlas_new(mAtlasSize, mAtlasSize, 1);
for(auto font : mFontCache)
{
texture_font_delete(font.second);
}
mFontCache.clear();
rewrite();
return;
}
}
int32_t currentWordEnd = -1;
float wordWidth = 0.0f;
for(int32_t i = 0; i < text.size(); ++i)
{
if(mWordWrap)
{
if(std::iswspace(text[i]) == 0 && i > currentWordEnd)
{
int32_t currentWordStart = i;
int32_t currentWordEnd = i;
int32_t wordIterator = i;
while(wordIterator < text.size() && std::iswspace(text[wordIterator]) == 0)
{
currentWordEnd++;
wordIterator++;
}
wordWidth = 0.0f;
for(int32_t iter = currentWordStart; iter < currentWordEnd; iter++)
{
texture_glyph_t* glyph = texture_font_get_glyph( mFontCache.at(*mCurrentFont), text[iter]);
wordWidth += glyph->advance_x * mScale;
}
if((penTempPosition.x + wordWidth) - mHorizontalAlign > mLineWidth)
{
if(wordWidth < mLineWidth)
{
penTempPosition.x = mHorizontalAlign;
penTempPosition.y += mLineHeight * mScale;
}
}
}
if(penTempPosition.x - mHorizontalAlign > mLineWidth)
{
penTempPosition.x = mHorizontalAlign;
penTempPosition.y += mLineHeight * mScale;
}
}
if(text[i] == '\n')
{
penTempPosition.x = mHorizontalAlign;
penTempPosition.y += mLineHeight * mScale;
continue;
}
texture_glyph_t* glyph = texture_font_get_glyph(mFontCache.at(*mCurrentFont), text[i]);
float kerning = 0.0f;
if( i > 0)
{
kerning = texture_glyph_get_kerning( glyph, text[i-1] );
}
penTempPosition.x += kerning * mScale;
float x0 = ( penTempPosition.x + glyph->offset_x * mScale );
float y0 = ( penTempPosition.y - glyph->offset_y * mScale);
float x1 = ( x0 + glyph->width * mScale );
float y1 = ( y0 + glyph->height * mScale);
float s0 = glyph->s0;
float t0 = glyph->t0;
float s1 = glyph->s1;
float t1 = glyph->t1;
if(mLowBounds.x == mHighBounds.x)
{
mLowBounds.x = x0;
mHighBounds.x = x1;
mLowBounds.y = y0;
mHighBounds.y = y1;
}
if(x0 < mLowBounds.x)
mLowBounds.x = x0;
if(x1 > mHighBounds.x)
mHighBounds.x = x1;
if(y0 < mLowBounds.y)
mLowBounds.y = y0;
if(y1 > mHighBounds.y)
mHighBounds.y = y1;
verticesToAdd.insert(verticesToAdd.end(), {x0, y0,
x0, y1,
x1, y1,
x0, y0,
x1, y1,
x1, y0});
texCoordsToAdd.insert(texCoordsToAdd.end(), {s0, t0,
s0, t1,
s1, t1,
s0, t0,
s1, t1,
s1, t0});
colorsToAdd.insert(colorsToAdd.end(), {mColor.rAsFloat(), mColor.gAsFloat(), mColor.bAsFloat(), 1.0f,
mColor.rAsFloat(), mColor.gAsFloat(), mColor.bAsFloat(), 1.0f,
mColor.rAsFloat(), mColor.gAsFloat(), mColor.bAsFloat(), 1.0f,
mColor.rAsFloat(), mColor.gAsFloat(), mColor.bAsFloat(), 1.0f,
mColor.rAsFloat(), mColor.gAsFloat(), mColor.bAsFloat(), 1.0f,
mColor.rAsFloat(), mColor.gAsFloat(), mColor.bAsFloat(), 1.0f});
penTempPosition.x += glyph->advance_x * mScale;
}
mPenPosition = penTempPosition;
mVertices.insert(mVertices.end(), verticesToAdd.begin(), verticesToAdd.end());
mTexCoords.insert(mTexCoords.end(), texCoordsToAdd.begin(), texCoordsToAdd.end());
mVertexColors.insert(mVertexColors.end(), colorsToAdd.begin(), colorsToAdd.end());
}
void TextSurface::cacheFont(const Font& font)
{
if(mFontCache.find(font) == mFontCache.end())
{
texture_font_t* created = texture_font_new(mAtlas, font.getPath().c_str(), font.getSize());
if(created == nullptr)
{
throw std::logic_error("Error! Could not create font from file '" + font.getPath() + "' maybe the file does not exist?");
}
else
{
if(mFontCache.find(font) == mFontCache.end())
mFontCache.emplace(font, created);
else
{
texture_font_delete(mFontCache.at(font));
}
}
}
}
}