diff options
Diffstat (limited to 'src/Text.cpp')
-rw-r--r-- | src/Text.cpp | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/src/Text.cpp b/src/Text.cpp index 171bd70..a9f1147 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -14,6 +14,7 @@ namespace QuickMedia { static const float TAB_WIDTH = 4.0f; static const float WORD_WRAP_MIN_SIZE = 80.0f; + static const sf::Color URL_COLOR(35, 140, 245); size_t StringViewUtf32::find(const StringViewUtf32 &other, size_t offset) const { if(offset >= size) @@ -322,6 +323,12 @@ namespace QuickMedia assert(dirty); dirtyText = false; splitTextByFont(); + // TODO: Optimize + auto u8 = str.toUtf8(); + std::string *u8_str = (std::string*)&u8; + url_ranges = extract_urls(*u8_str); + convert_utf8_to_utf32_ranges(*u8_str, url_ranges); + dirty = true; } if(!update_even_if_not_dirty && !dirty) @@ -343,9 +350,12 @@ namespace QuickMedia float latin_font_height = latin_font->getGlyph(' ', characterSize, false).advance; float hspace = latin_font_height + characterSpacing; float vspace = latin_font->getLineSpacing(characterSize); // TODO: What about japanese font??? + + size_t url_range_index = 0; sf::Vector2f glyphPos; sf::Uint32 prevCodePoint = 0; + // TODO: Only do this if dirtyText for(usize textElementIndex = 0; textElementIndex < textElements.size(); ++textElementIndex) { TextElement &textElement = textElements[textElementIndex]; @@ -392,6 +402,17 @@ namespace QuickMedia textElement.position = glyphPos; for(size_t i = 0; i < textElement.text.size; ++i) { + sf::Color text_color = color; + if(url_range_index < url_ranges.size()) { + size_t string_offset = (textElement.text.data + i) - str.getData(); + if(string_offset >= url_ranges[url_range_index].start && string_offset < url_ranges[url_range_index].start + url_ranges[url_range_index].length) { + text_color = URL_COLOR; + text_color.a = color.a; + if(string_offset + 1 == url_ranges[url_range_index].start + url_ranges[url_range_index].length) + ++url_range_index; + } + } + sf::Uint32 codePoint = textElement.text[i]; // TODO: Make this work when combining multiple different fonts (for example latin and japanese). // For japanese we could use a hack, because all japanese characters are monospace (exception being half-width characters). @@ -473,12 +494,12 @@ namespace QuickMedia 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[vertices_index].append({ vertexTopRight, color, textureTopRight }); - vertices[vertices_index].append({ vertexTopLeft, color, textureTopLeft }); - vertices[vertices_index].append({ vertexBottomLeft, color, textureBottomLeft }); - vertices[vertices_index].append({ vertexBottomLeft, color, textureBottomLeft }); - vertices[vertices_index].append({ vertexBottomRight, color, textureBottomRight }); - vertices[vertices_index].append({ vertexTopRight, color, textureTopRight }); + vertices[vertices_index].append({ vertexTopRight, text_color, textureTopRight }); + vertices[vertices_index].append({ vertexTopLeft, text_color, textureTopLeft }); + vertices[vertices_index].append({ vertexBottomLeft, text_color, textureBottomLeft }); + vertices[vertices_index].append({ vertexBottomLeft, text_color, textureBottomLeft }); + vertices[vertices_index].append({ vertexBottomRight, text_color, textureBottomRight }); + vertices[vertices_index].append({ vertexTopRight, text_color, textureTopRight }); glyphPos.x += glyph.advance + characterSpacing; vertices_linear.push_back({vertices_index, vertexStart, 0, codePoint}); @@ -559,6 +580,7 @@ namespace QuickMedia } boundingBox.height = num_lines * line_height; + //url_ranges.clear(); if(!editable) vertices_linear.clear(); } |