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 |
#include "fsCNativeTextSprite.h"
#include <cstring>
#include <fsCore/fsStr.h>
#include <fsCore/fsSColour.h>
#include <fsCore/fsRect.h>
#include "../fsCFont.h"
#include "fsCBgfx2d.h"
#include "fsAppCore/fsIDisplay.h"
namespace
{
// bgfx backend has no rich-text rendering yet. fsStr::removeXmlTags() deliberately
// preserves <b>/<i>/<font ...> tags (axmol/cocos2d-x render them), so without this
// they'd show up as literal control codes here. Strip them, keeping the enclosed
// text, so this backend falls back to plain readable text instead.
fsStr richTextTagsStrip(const fsStr& pText)
{
fsStr result = pText;
fsStr::sizeT start = result.find('<');
while (start != fsStr::npos)
{
const fsStr::sizeT end = result.find('>', start);
if (end == fsStr::npos)
{
break;
}
result.erase(start, end - start + 1);
start = result.find('<');
}
return result;
}
// Minimal UTF-8 decoder, mirroring raylib's GetCodepoint() behaviour of
// returning '?' (0x3f) for malformed sequences
fsS32 codepointGet(const char* pText, fsS32& pByteCount)
{
const auto* const bytes = reinterpret_cast<const unsigned char*>(pText);
pByteCount = 1;
if (bytes[0] < 0x80)
{
return bytes[0];
}
if ((bytes[0] & 0xe0) == 0xc0 && (bytes[1] & 0xc0) == 0x80)
{
pByteCount = 2;
return ((bytes[0] & 0x1f) << 6) | (bytes[1] & 0x3f);
}
if ((bytes[0] & 0xf0) == 0xe0 && (bytes[1] & 0xc0) == 0x80 && (bytes[2] & 0xc0) == 0x80)
{
pByteCount = 3;
return ((bytes[0] & 0x0f) << 12) | ((bytes[1] & 0x3f) << 6) | (bytes[2] & 0x3f);
}
if ((bytes[0] & 0xf8) == 0xf0 && (bytes[1] & 0xc0) == 0x80 && (bytes[2] & 0xc0) == 0x80 && (bytes[3] & 0xc0) == 0x80)
{
pByteCount = 4;
return ((bytes[0] & 0x07) << 18) | ((bytes[1] & 0x3f) << 12) | ((bytes[2] & 0x3f) << 6) | (bytes[3] & 0x3f);
}
return 0x3f;
}
}
void fsCNativeTextSprite::outlineSet(const fsSColour4B& pOutline, fsF32 pSize) const<--- The member function 'fsCNativeTextSprite::outlineSet' can be static.
{
// no impl
}
void fsCNativeTextSprite::glowSet(const fsSColour4B& pGlow) const<--- The member function 'fsCNativeTextSprite::glowSet' can be static.
{
// no impl
}
void fsCNativeTextSprite::shadowSet(const fsSColour4B& pShadow, fsVec2d pOffset, fsF32 pBlurRadius) const<--- The member function 'fsCNativeTextSprite::shadowSet' can be static.
{
// no impl
}
fsF32 fsCNativeTextSprite::glyphWidthGet(fsS32 pCodepoint) const
{
if (const auto* const glyph = mFont->glyphGet(pCodepoint))
{
return glyph->mXAdvance;
}
return 0.0f;
}
void fsCNativeTextSprite::glyphAdd(fsS32 pCodepoint, fsF32 pPenX, fsF32 pBaselineY, fsU32 pColourAbgr)
{
const auto* const glyph = mFont->glyphGet(pCodepoint);
if (!glyph)
{
return;
}
const fsF32 width = glyph->mX1 - glyph->mX0;
const fsF32 height = glyph->mY1 - glyph->mY0;
if (width <= 0.0f || height <= 0.0f)
{
return;
}
const fsF32 atlasWidth = static_cast<fsF32>(mFont->mAtlasWidth);
const fsF32 atlasHeight = static_cast<fsF32>(mFont->mAtlasHeight);
fsNBgfx2d::batchQuad(mRenderMatrix,
mFont->mTexture, 0,
pPenX + glyph->mXOff, pBaselineY + glyph->mYOff,
width, height,
glyph->mX0 / atlasWidth, glyph->mY0 / atlasHeight,
glyph->mX1 / atlasWidth, glyph->mY1 / atlasHeight,
pColourAbgr);
}
fsF32 fsCNativeTextSprite::lineStart(fsF32 pLineWidth) const
{
if (mHorizontalAlignment == fsITextSpriteComponent::eHORIZONTAL_ALIGNMENT::eHALIGN_CENTRE)
return (mRect.mWidth - pLineWidth) / 2;
if (mHorizontalAlignment == fsITextSpriteComponent::eHORIZONTAL_ALIGNMENT::eHALIGN_RIGHT)
return mRect.mWidth - pLineWidth;
// if (mHorizontalAlignment == fsITextSpriteComponent::eHORIZONTAL_ALIGNMENT::eHALIGN_LEFT)
return 0;
}
fsS32 fsCNativeTextSprite::lineCountGet(const char* pText) const
{
const fsS32 length = static_cast<fsS32>(std::strlen(pText));
fsF32 textOffsetX = 0.0f;
fsS32 lineCount = 0;
for (fsS32 i = 0; i < length; i++)
{
fsS32 codepointByteCount = 0;
const fsS32 codepoint = codepointGet(&pText[i], codepointByteCount);
i += (codepointByteCount - 1);
fsF32 glyphWidth = 0;
if (codepoint != '\n')
{
glyphWidth = glyphWidthGet(codepoint);
}
if ((textOffsetX + glyphWidth) > mRect.mWidth)
{
textOffsetX = 0;
++lineCount;
}
else if (codepoint == '\n')
{
textOffsetX = 0;
++lineCount;
}
else
{
if ((textOffsetX != 0) || (codepoint != ' ')) textOffsetX += glyphWidth; // avoid leading spaces
}
}
return lineCount;
}
// Draw text inside rectangle limits with word wrapping - port of the raylib
// backend's DrawTextBoxed on top of the stb_truetype glyph atlas
void fsCNativeTextSprite::drawTextBoxed(const char* pText, fsBool pWordWrap, const fsSColour4B& pTint)
{
const fsS32 length = static_cast<fsS32>(std::strlen(pText));
const fsU32 colour = fsNBgfx2d::abgrGet(pTint.mR, pTint.mG, pTint.mB, pTint.mA);
enum { MEASURE_STATE = 0, DRAW_STATE = 1 };
int state = pWordWrap ? MEASURE_STATE : DRAW_STATE;
int startLine = -1; // Index where to begin drawing (where a line begins)
int endLine = -1; // Index where to stop drawing (where a line ends)
int lastk = -1; // Holds last value of the character position
const fsF32 glyphHeight = mFont->mLineHeight;
fsF32 textOffsetX = 0.0f;
fsF32 textOffsetY = -lineCountGet(pText) * glyphHeight / 2; // Offset between lines (on line break '\n')
for (int i = 0, k = 0; i < length; i++, k++)
{
fsS32 codepointByteCount = 0;
const fsS32 codepoint = codepointGet(&pText[i], codepointByteCount);
i += (codepointByteCount - 1);
fsF32 glyphWidth = 0;
if (codepoint != '\n')
{
glyphWidth = glyphWidthGet(codepoint);
}
// NOTE: When wordWrap is ON we first measure how much of the text we can draw before going outside of the rec container
// We store this info in startLine and endLine, then we change states, draw the text between those two variables
// and change states again and again recursively until the end of the text (or until we get outside of the container).
// When wordWrap is OFF we don't need the measure state so we go to the drawing state immediately
// and begin drawing on the next line before we can get outside the container.
if (state == MEASURE_STATE)
{
if ((codepoint == ' ') || (codepoint == '\t') || (codepoint == '\n')) endLine = i;
if ((textOffsetX + glyphWidth) > mRect.mWidth)
{
endLine = (endLine < 1)? i : endLine;
if (i == endLine) endLine -= codepointByteCount;
if ((startLine + codepointByteCount) == endLine) endLine = (i - codepointByteCount);
state = !state;
}
else if ((i + 1) == length)
{
textOffsetX += glyphWidth;
endLine = i;
state = !state;
}
else if (codepoint == '\n')
{
textOffsetX += glyphWidth;
state = !state;
}
if (state == DRAW_STATE)
{
textOffsetX = lineStart(textOffsetX);
i = startLine;
// Save character position when we switch states
int tmp = lastk;
lastk = k - 1;
k = tmp;
}
}
else
{
if (codepoint == '\n')
{
if (!pWordWrap)
{
textOffsetY += glyphHeight;
textOffsetX = 0;
}
}
else
{
if (!pWordWrap && ((textOffsetX + glyphWidth) > mRect.mWidth))
{
textOffsetY += glyphHeight;
textOffsetX = 0;
}
// When text overflows rectangle height limit, just stop drawing
if ((textOffsetY + mFont->mPointSize) > mRect.mHeight) break;
// Draw current character glyph
if ((codepoint != ' ') && (codepoint != '\t'))
{
glyphAdd(codepoint,
mRect.mX + textOffsetX,
mRect.mY + textOffsetY + mFont->mAscent,
colour);
}
}
if (pWordWrap && (i == endLine))
{
textOffsetY += glyphHeight;
textOffsetX = 0;
startLine = endLine;
endLine = -1;
k = lastk;
state = !state;
}
}
if ((textOffsetX != 0) || (codepoint != ' ')) textOffsetX += glyphWidth; // avoid leading spaces
}
}
void fsCNativeTextSprite::renderDo()
{
if (!mFont)
{
return;
}
drawTextBoxed(mText.utf8Get(), true, mTint);
}
void fsCNativeTextSprite::colourSetDo(const fsSColour4B& pColour)
{
mTint = pColour;
}
void fsCNativeTextSprite::alignmentSet(fsS32 pAlignH, fsS32 pAlignV)
{
mHorizontalAlignment = static_cast<fsTHorizontalAlign>(pAlignH);
mVerticalAlignment = static_cast<fsTVerticalAlign>(pAlignV);
}
void fsCNativeTextSprite::initialise(const fsStr& pText, fsIFont* pFont)
{
mFont = static_cast<fsCFont*>(pFont);
textSet(pText);
}
fsS32 fsCNativeTextSprite::stringWidthInPixelsGet() const
{
if (!mFont)
{
return 0;
}
const char* const text = mText.utf8Get();
const fsS32 length = static_cast<fsS32>(std::strlen(text));
fsF32 maxWidth = 0.0f;
fsF32 lineWidth = 0.0f;
for (fsS32 i = 0; i < length; ++i)
{
fsS32 codepointByteCount = 0;
const fsS32 codepoint = codepointGet(&text[i], codepointByteCount);
i += (codepointByteCount - 1);
if (codepoint == '\n')
{
maxWidth = lineWidth > maxWidth ? lineWidth : maxWidth;
lineWidth = 0.0f;
}
else
{
lineWidth += glyphWidthGet(codepoint);
}
}
maxWidth = lineWidth > maxWidth ? lineWidth : maxWidth;
return static_cast<fsS32>(maxWidth);
}
fsS32 fsCNativeTextSprite::stringHeightInPixelsGet() const
{
if (!mFont)
{
return 0;
}
const char* const text = mText.utf8Get();
fsS32 lineCount = 1;
for (const char* c = text; *c; ++c)
{
if (*c == '\n')
{
++lineCount;
}
}
return static_cast<fsS32>(lineCount * mFont->mLineHeight);
}
void fsCNativeTextSprite::formattingRectSizeSet(const fsRect& pFormattingRect)
{
mBoundingBox = pFormattingRect;
mRect.mWidth = mBoundingBox.widthGet();
mRect.mHeight = mBoundingBox.heightGet();
positionRecompute();
}
fsStr fsCNativeTextSprite::textGet() const
{
return mText;
}
void fsCNativeTextSprite::textSet(const fsStr& pText)
{
// Can be called every frame by callers reapplying unchanged text - skip
// the re-measure below when nothing actually changed.
const fsStr strippedText = richTextTagsStrip(pText);
if (strippedText == mText)
{
return;
}
mText = strippedText;
positionRecompute();
}
void fsCNativeTextSprite::positionRecompute()
{
// textSet() and formattingRectSizeSet() can each run before the other has
// ever been called, so this is the single place that (re)computes position
// from whichever values are current for both.
if (!mFont)
{
return;
}
const fsS32 currentHeight = stringHeightInPixelsGet();
mRect.mX = mBoundingBox.xCoordGet() - mRect.mWidth / 2 - (currentHeight / 2.0f);
mRect.mY = mBoundingBox.yCoordGet() - (currentHeight / 2.0f);
}
fsCNativeTextSprite::fsCNativeTextSprite():
fsINative(),
mFont(nullptr)
{
}
fsCNativeTextSprite::~fsCNativeTextSprite()
{
}
|