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
24// the width/height of the cached glyph textures24// the width/height of the cached glyph textures
25const int IMAGE_SIZE = 256;25const int IMAGE_SIZE = 256;
26 26
27Glyph::Glyph(int textureID, const QPoint& location, const QRect& bounds, int width) :27static 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
31TextRenderer::TextRenderer(const char* family, int pointSize, int weight, bool italic,32static 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),36TextRenderer* 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
45TextRenderer::~TextRenderer() {46TextRenderer::~TextRenderer() {
@@ -122,6 +123,19 @@ int TextRenderer::computeWidth(const char* str) · 76 unmodified lines
122 return width;123 return width;
123}124}
124 125
126TextRenderer::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
125const Glyph& TextRenderer::getGlyph(char c) {139const 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
231QHash<TextRenderer::Properties, TextRenderer*> TextRenderer::_instances;
232
233Glyph::Glyph(int textureID, const QPoint& location, const QRect& bounds, int width) :
234 _textureID(textureID), _location(location), _bounds(bounds), _width(width) {
235}
236