Showing the changes to interface/src/ui/TextRenderer.cpp (1 of 7 files). Show all
Unified · Split
| @@ -24,22 +24,23 @@ · 23 unmodified lines | |||
| 24 | // the width/height of the cached glyph textures | 24 | // the width/height of the cached glyph textures |
| 25 | const int IMAGE_SIZE = 256; | 25 | const int IMAGE_SIZE = 256; |
| 26 | 26 | ||
| 27 | Glyph::Glyph(int textureID, const QPoint& location, const QRect& bounds, int width) : | 27 | static uint qHash(const TextRenderer::Properties& key, uint seed = 0) { |
| 28 | _textureID(textureID), _location(location), _bounds(bounds), _width(width) { | 28 | // can be switched to qHash(key.font, seed) when we require Qt 5.3+ |
| 29 | return qHash(key.font.family(), qHash(key.font.pointSize(), seed)); | ||
| 29 | } | 30 | } |
| 30 | 31 | ||
| 31 | TextRenderer::TextRenderer(const char* family, int pointSize, int weight, bool italic, | 32 | static bool operator==(const TextRenderer::Properties& p1, const TextRenderer::Properties& p2) { |
| 32 | EffectType effectType, int effectThickness, QColor color) : | 33 | return p1.font == p2.font && p1.effect == p2.effect && p1.effectThickness == p2.effectThickness && p1.color == p2.color; |
| 33 | _font(family, pointSize, weight, italic), | 34 | } |
| 34 | _metrics(_font), | 35 | |
| 35 | _effectType(effectType), | 36 | TextRenderer* TextRenderer::getInstance(const char* family, int pointSize, int weight, bool italic, |
| 36 | _effectThickness(effectThickness), | 37 | EffectType effect, int effectThickness, const QColor& color) { |
| 37 | _x(IMAGE_SIZE), | 38 | Properties properties = { QFont(family, pointSize, weight, italic), effect, effectThickness, color }; |
| 38 | _y(IMAGE_SIZE), | 39 | TextRenderer*& instance = _instances[properties]; |
| 39 | _rowHeight(0), | 40 | if (!instance) { |
| 40 | _color(color) { | 41 | instance = new TextRenderer(properties); |
| 41 | 42 | } | |
| 42 | _font.setKerning(false); | 43 | return instance; |
| 43 | } | 44 | } |
| 44 | 45 | ||
| 45 | TextRenderer::~TextRenderer() { | 46 | TextRenderer::~TextRenderer() { |
| @@ -122,6 +123,19 @@ int TextRenderer::computeWidth(const char* str) · 76 unmodified lines | |||
| 122 | return width; | 123 | return width; |
| 123 | } | 124 | } |
| 124 | 125 | ||
| 126 | TextRenderer::TextRenderer(const Properties& properties) : | ||
| 127 | _font(properties.font), | ||
| 128 | _metrics(_font), | ||
| 129 | _effectType(properties.effect), | ||
| 130 | _effectThickness(properties.effectThickness), | ||
| 131 | _x(IMAGE_SIZE), | ||
| 132 | _y(IMAGE_SIZE), | ||
| 133 | _rowHeight(0), | ||
| 134 | _color(properties.color) { | ||
| 135 | |||
| 136 | _font.setKerning(false); | ||
| 137 | } | ||
| 138 | |||
| 125 | const Glyph& TextRenderer::getGlyph(char c) { | 139 | const Glyph& TextRenderer::getGlyph(char c) { |
| 126 | Glyph& glyph = _glyphs[c]; | 140 | Glyph& glyph = _glyphs[c]; |
| 127 | if (glyph.isValid()) { | 141 | if (glyph.isValid()) { |
| @@ -213,3 +227,10 @@ const Glyph& TextRenderer::getGlyph(char c) { · 85 unmodified lines | |||
| 213 | glBindTexture(GL_TEXTURE_2D, 0); | 227 | glBindTexture(GL_TEXTURE_2D, 0); |
| 214 | return glyph; | 228 | return glyph; |
| 215 | } | 229 | } |
| 230 | |||
| 231 | QHash<TextRenderer::Properties, TextRenderer*> TextRenderer::_instances; | ||
| 232 | |||
| 233 | Glyph::Glyph(int textureID, const QPoint& location, const QRect& bounds, int width) : | ||
| 234 | _textureID(textureID), _location(location), _bounds(bounds), _width(width) { | ||
| 235 | } | ||
| 236 | |||