History

Share instances of TextRenderer.

Andrzej Kapolka committed 12 years ago

Commit de8dd4c1 parent 2216f897 files · +70 −37Browse files

Showing the changes to interface/src/ui/TextRenderer.cpp (1 of 7 files). Show all

Unified · Split

interface/src/ui/TextRenderer.cpp+35−14
@@ -24,22 +24,23 @@ · 23 unmodified lines
2424 // the width/height of the cached glyph textures
2525 const int IMAGE_SIZE = 256;
2626
27−Glyph::Glyph(int textureID, const QPoint& location, const QRect& bounds, int width) :
28− _textureID(textureID), _location(location), _bounds(bounds), _width(width) {
27+static uint qHash(const TextRenderer::Properties& key, uint seed = 0) {
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));
2930 }
3031
31−TextRenderer::TextRenderer(const char* family, int pointSize, int weight, bool italic,
32− EffectType effectType, int effectThickness, QColor color) :
33− _font(family, pointSize, weight, italic),
34− _metrics(_font),
35− _effectType(effectType),
36− _effectThickness(effectThickness),
37− _x(IMAGE_SIZE),
38− _y(IMAGE_SIZE),
39− _rowHeight(0),
40− _color(color) {
41−
42− _font.setKerning(false);
32+static bool operator==(const TextRenderer::Properties& p1, const TextRenderer::Properties& p2) {
33+ return p1.font == p2.font && p1.effect == p2.effect && p1.effectThickness == p2.effectThickness && p1.color == p2.color;
34+}
35+
36+TextRenderer* TextRenderer::getInstance(const char* family, int pointSize, int weight, bool italic,
37+ EffectType effect, int effectThickness, const QColor& color) {
38+ Properties properties = { QFont(family, pointSize, weight, italic), effect, effectThickness, color };
39+ TextRenderer*& instance = _instances[properties];
40+ if (!instance) {
41+ instance = new TextRenderer(properties);
42+ }
43+ return instance;
4344 }
4445
4546 TextRenderer::~TextRenderer() {
@@ -122,6 +123,19 @@ int TextRenderer::computeWidth(const char* str) · 76 unmodified lines
122123 return width;
123124 }
124125
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+
125139 const Glyph& TextRenderer::getGlyph(char c) {
126140 Glyph& glyph = _glyphs[c];
127141 if (glyph.isValid()) {
@@ -213,3 +227,10 @@ const Glyph& TextRenderer::getGlyph(char c) { · 85 unmodified lines
213227 glBindTexture(GL_TEXTURE_2D, 0);
214228 return glyph;
215229 }
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+