aboutsummaryrefslogtreecommitdiff
path: root/src/Text.cpp
blob: 3cafc69f1626e6e202c95c5b7a54c7d1273fc666 (plain)
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
#include "../include/Text.hpp"
#include "../include/Cache.hpp"
#include "../include/Gif.hpp"
#include <SFML/Graphics/RectangleShape.hpp>
#include <cmath>

namespace dchat
{
    const float TAB_WIDTH = 4.0f;
    const float EMOJI_PADDING = 5.0f;
    const float EMOJI_SCALE_WITH_TEXT = 1.0f;
    const float EMOJI_SCALE_STANDALONE = 5.0f;
    
    Text::Text(const sf::Font *_font) : 
        font(_font),
        characterSize(0),
        maxWidth(0.0f),
        color(sf::Color::White),
        dirty(false),
        plainText(false),
        totalHeight(0.0f)
    {
        
    }
    
    Text::Text(const sf::String &_str, const sf::Font *_font, unsigned int _characterSize, float _maxWidth, bool _plainText) : 
        font(_font),
        characterSize(_characterSize),
        vertices(sf::PrimitiveType::Quads),
        maxWidth(_maxWidth),
        color(sf::Color::White),
        dirty(true),
        plainText(_plainText),
        totalHeight(0.0f)
    {
        setString(_str);
    }
    
    void Text::setString(const sf::String &str)
    {
        if(str != this->str)
        {
            this->str = str;
            dirty = true;
            textElements.clear();
            stringSplitElements(this->str, 0);
        }
    }
    
    void Text::appendStringNewLine(const sf::String &str)
    {
        usize prevSize = this->str.getSize();
        this->str += '\n';
        this->str += str;
        dirty = true;
        stringSplitElements(this->str, prevSize);
    }
    
    void Text::setPosition(float x, float y)
    {
        position.x = x;
        position.y = y;
    }
    
    void Text::setPosition(const sf::Vector2f &position)
    {
        this->position = position;
    }
    
    void Text::setMaxWidth(float maxWidth)
    {
        if(maxWidth != this->maxWidth)
        {
            this->maxWidth = maxWidth;
            dirty = true;
        }
    }
    
    void Text::setCharacterSize(unsigned int characterSize)
    {
        if(characterSize != this->characterSize)
        {
            this->characterSize = characterSize;
            dirty = true;
        }
    }
    
    void Text::setFillColor(sf::Color color)
    {
        if(color != this->color)
        {
            this->color = color;
            dirty = true;
        }
    }
    
    float Text::getHeight() const
    {
        return totalHeight;
    }
    
    void Text::stringSplitElements(sf::String &stringToSplit, usize startIndex)
    {
        if(plainText)
        {
            StringViewUtf32 wholeStr(&stringToSplit[startIndex], stringToSplit.getSize() - startIndex);
            textElements.push_back({ wholeStr, TextElement::Type::TEXT });
            return;
        }
        
        size_t offset = startIndex;
        while(offset < stringToSplit.getSize())
        {
            size_t stringStart = offset;
            size_t foundStartIndex = stringToSplit.find("[emoji](", offset);
            size_t foundEndIndex = -1;
            if(foundStartIndex != -1)
            {
                offset += (foundStartIndex + 8);
                foundEndIndex = stringToSplit.find(")", offset);
            }
            
            if(foundEndIndex != -1)
            {
                StringViewUtf32 beforeEmojiStr(&stringToSplit[stringStart], foundStartIndex - stringStart);
                textElements.push_back({ beforeEmojiStr, TextElement::Type::TEXT });
                
                StringViewUtf32 url(&stringToSplit[offset], foundEndIndex - offset);
                textElements.push_back({ url, TextElement::Type::EMOJI });
                offset = foundEndIndex + 1;
            }
            else
            {
                StringViewUtf32 strToEnd(&stringToSplit[stringStart], stringToSplit.getSize() - stringStart);
                textElements.push_back({ strToEnd, TextElement::Type::TEXT });
                offset = stringToSplit.getSize();
            }
        }
        
        for(std::vector<TextElement>::iterator it = textElements.begin(); it != textElements.end();)
        {
            if(it->text.size == 0)
                it = textElements.erase(it);
            else
                ++it;
        }
    }
    
    // Logic loosely based on https://github.com/SFML/SFML/wiki/Source:-CurvedText
    void Text::updateGeometry()
    {
        vertices.clear();
        float hspace = font->getGlyph(' ', characterSize, false).advance;
        float vspace = font->getLineSpacing(characterSize);
        
        sf::Vector2f glyphPos;
        sf::Uint32 prevCodePoint = 0;
        size_t lastSpacingWordWrapIndex = -1;
        float lastSpacingAccumulatedOffset = 0.0f;
        for(usize textElementIndex = 0; textElementIndex < textElements.size(); ++textElementIndex)
        {
            TextElement &textElement = textElements[textElementIndex];
            if(textElement.type == TextElement::Type::EMOJI)
            {
                bool ownLineLeft = false;
                if(textElementIndex == 0)
                    ownLineLeft = true;
                else
                {
                    TextElement &prevElement = textElements[textElementIndex - 1];
                    if(prevElement.text[prevElement.text.size - 1] == '\n')
                        ownLineLeft = true;
                }
                
                bool ownLineRight = false;
                if(textElementIndex == textElements.size() - 1)
                    ownLineRight = true;
                else
                {
                    TextElement &nextElement = textElements[textElementIndex + 1];
                    if(nextElement.text[0] == '\n')
                        ownLineRight = true;
                }
                
                if(ownLineLeft && ownLineRight)
                    textElement.ownLine = true;
                
                float emojiSize = vspace * (textElement.ownLine ? EMOJI_SCALE_STANDALONE : EMOJI_SCALE_WITH_TEXT);
                
                glyphPos.x += EMOJI_PADDING;
                textElement.position.x = glyphPos.x;
                if(textElement.ownLine)
                {
                    textElement.position.y = glyphPos.y;
                    // TODO: Find a better way to do this, @totalHeight is wrong because we add emojiSize and then vspace
                    glyphPos.y += emojiSize - vspace;
                }
                else
                {
                    textElement.position.y = glyphPos.y + vspace * 0.5f - emojiSize * 0.5f;
                }
                glyphPos.x += emojiSize + EMOJI_PADDING;
                if(glyphPos.x > maxWidth)
                {
                    glyphPos.x = 0.0f;
                    glyphPos.y += vspace;
                }
                
                continue;
            }
            
            usize vertexOffset = vertices.getVertexCount();
            vertices.resize(vertices.getVertexCount() + (4 * textElement.text.size));
            textElement.position = glyphPos;
            for(size_t i = 0; i < textElement.text.size; ++i)
            {
                sf::Uint32 codePoint = textElement.text[i];
                float kerning = font->getKerning(prevCodePoint, codePoint, characterSize);
                prevCodePoint = codePoint;
                glyphPos.x += kerning;
                
                switch(codePoint)
                {
                    case ' ':
                    {
                        glyphPos.x += hspace;
                        if(glyphPos.x > maxWidth * 0.5f)
                        {
                            lastSpacingWordWrapIndex = i;
                            lastSpacingAccumulatedOffset = glyphPos.x;
                        }
                        continue;
                    }
                    case '\t':
                    {
                        glyphPos.x += (hspace * TAB_WIDTH);
                        if(glyphPos.x > maxWidth * 0.5f)
                        {
                            lastSpacingWordWrapIndex = i;
                            lastSpacingAccumulatedOffset = glyphPos.x;
                        }
                        continue;
                    }
                    case '\n':
                    {
                        glyphPos.x = 0.0f;
                        glyphPos.y += vspace;
                        continue;
                    }
                    case '\v':
                    {
                        glyphPos.y += (vspace * TAB_WIDTH);
                        continue;
                    }
                }
                
                const sf::Glyph &glyph = font->getGlyph(codePoint, characterSize, false);
                if(glyphPos.x + glyph.advance > maxWidth)
                {
                    // If there was a space in the text and text width is too long, then we need to word wrap at space index instead,
                    // which means we need to change the position of all vertices after the space to the current vertex
                    //printf("last spacing word wrap index: %zu\n", lastSpacingWordWrapIndex);
                    if(lastSpacingWordWrapIndex != -1)
                    {
                        for(size_t j = lastSpacingWordWrapIndex; j < i; ++j)
                        {
                            for(size_t k = 0; k < 4; ++k)
                            {
                                sf::Vector2f &vertexPos = vertices[vertexOffset + j * 4 + k].position;
                                vertexPos.x -= lastSpacingAccumulatedOffset;
                                vertexPos.y += vspace;
                            }
                        }
                        
                        glyphPos.x -= lastSpacingAccumulatedOffset;
                        lastSpacingWordWrapIndex = -1;
                        lastSpacingAccumulatedOffset = 0.0f;
                    }
                    else
                        glyphPos.x = 0.0f;
                    
                    glyphPos.y += vspace;
                }
                
                sf::Vector2f vertexTopLeft(glyphPos.x + glyph.bounds.left, glyphPos.y + glyph.bounds.top);
                sf::Vector2f vertexTopRight(glyphPos.x + glyph.bounds.left + glyph.bounds.width, glyphPos.y + glyph.bounds.top);
                sf::Vector2f vertexBottomLeft(glyphPos.x + glyph.bounds.left, glyphPos.y + glyph.bounds.top + glyph.bounds.height);
                sf::Vector2f vertexBottomRight(glyphPos.x + glyph.bounds.left + glyph.bounds.width, glyphPos.y + glyph.bounds.top + glyph.bounds.height);
                
                sf::Vector2f textureTopLeft(glyph.textureRect.left, glyph.textureRect.top);
                sf::Vector2f textureTopRight(glyph.textureRect.left + glyph.textureRect.width, glyph.textureRect.top);
                sf::Vector2f textureBottomLeft(glyph.textureRect.left, glyph.textureRect.top + glyph.textureRect.height);
                sf::Vector2f textureBottomRight(glyph.textureRect.left + glyph.textureRect.width, glyph.textureRect.top + glyph.textureRect.height);
                
                vertices[vertexOffset + i * 4 + 0] = { vertexTopLeft, color, textureTopLeft };
                vertices[vertexOffset + i * 4 + 1] = { vertexTopRight, color, textureTopRight };
                vertices[vertexOffset + i * 4 + 2] = { vertexBottomRight, color, textureBottomRight };
                vertices[vertexOffset + i * 4 + 3] = { vertexBottomLeft, color, textureBottomLeft };
                
                glyphPos.x += glyph.advance;
            }
        }
        totalHeight = glyphPos.y + vspace;
    }
    
    void Text::draw(sf::RenderTarget &target, Cache &cache)
    {
        if(dirty)
        {
            updateGeometry();
            dirty = false;
        }
        
        float vspace = font->getLineSpacing(characterSize);
        
        sf::RenderStates states;
        sf::Vector2f pos = position;
        pos.y += floor(vspace); // Origin is at bottom left, we want it to be at top left
        
        // TODO: Do not use maxWidth here. Max width might be set to 99999 and actual text width might be 200. Text width should be calculated instead
        //sf::FloatRect targetRect(0.0f, 0.0f, maxWidth, target.getSize().y);
        //sf::FloatRect textRect(pos.x, pos.y, maxWidth, )
        //colRect.contains()
        //if(pos.x + maxWidth <= 0.0f || pos.x >= maxWidth || pos.y + totalHeight <= 0.0f || pos.y >= target.getSize().y) return;
        if(pos.y + totalHeight <= 0.0f || pos.y >= target.getSize().y) return;
        
        states.transform.translate(pos);
        states.texture = &font->getTexture(characterSize);
        target.draw(vertices, states);
        
        for(TextElement &textElement : textElements)
        {
            if(textElement.type == TextElement::Type::EMOJI)
            {
                sf::Vector2f pos = position;
                pos += textElement.position;
                pos.x = floor(pos.x);
                pos.y = floor(pos.y);
                float emojiSize = vspace * (textElement.ownLine ? EMOJI_SCALE_STANDALONE : EMOJI_SCALE_WITH_TEXT);
                sf::Vector2f size(emojiSize, emojiSize);
                
                // TODO: Optimize this (add unordered_map that takes StringViewUtf32 as key)
                auto u8Str = sf::String::fromUtf32(textElement.text.data, textElement.text.data + textElement.text.size).toUtf8();
                const std::string &utf8Str = *(std::basic_string<char>*)&u8Str;
                const ImageByUrlResult imageByUrlResult = cache.getImageByUrl(utf8Str);
                if(imageByUrlResult.type == ImageByUrlResult::Type::CACHED)
                {
                    if(imageByUrlResult.isGif)
                    {
                        auto gifSize = imageByUrlResult.gif->getSize();
                        float widthToHeightRatio = (float)gifSize.x / (float)gifSize.y;
                        
                        imageByUrlResult.gif->setPosition(pos);
                        imageByUrlResult.gif->setScale(sf::Vector2f(size.x / (float)gifSize.x * widthToHeightRatio, size.y / (float)gifSize.y));
                        imageByUrlResult.gif->draw(target);
                    }
                    else
                    {
                        auto textureSize = imageByUrlResult.texture->getSize();
                        float widthToHeightRatio = (float)textureSize.x / (float)textureSize.y;
                        
                        // TODO: Store this sprite somewhere, might not be efficient to create a new sprite object every frame
                        sf::Sprite sprite(*imageByUrlResult.texture);
                        sprite.setPosition(pos);
                        sprite.setScale(size.x / (float)textureSize.x * widthToHeightRatio, size.y / (float)textureSize.y);
                        target.draw(sprite);
                    }
                }
                else
                {
                    sf::RectangleShape rect(size);
                    rect.setFillColor(sf::Color::White);
                    rect.setPosition(pos);
                    target.draw(rect);
                }
            }
        }
    }
}