From cab31e719025069752b7a21900d14adc3533a584 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Tue, 3 Feb 2015 18:43:24 -0800 Subject: [PATCH] Working on text coordinates --- libraries/render-utils/src/TextRenderer.cpp | 54 +++++++++++---------- libraries/render-utils/src/TextRenderer.h | 12 +---- 2 files changed, 31 insertions(+), 35 deletions(-) diff --git a/libraries/render-utils/src/TextRenderer.cpp b/libraries/render-utils/src/TextRenderer.cpp index c84f51e187..24aebbe04e 100644 --- a/libraries/render-utils/src/TextRenderer.cpp +++ b/libraries/render-utils/src/TextRenderer.cpp @@ -36,8 +36,6 @@ #include "FontTimeless.h" #include "FontCourierPrime.h" - - namespace Shaders { // Normally we could use 'enum class' to avoid namespace pollution, // but we want easy conversion to GLuint @@ -120,7 +118,6 @@ void main() { FragColor = vec4(Color.rgb, a); })XXXX"; - // stores the font metrics for a single character struct Glyph { QChar c; @@ -131,16 +128,12 @@ struct Glyph { float d; // xadvance - adjusts character positioning size_t indexOffset; - int width() const { - return size.x; - } QRectF bounds() const; QRectF textureBounds(const glm::vec2 & textureSize) const; void read(QIODevice & in); }; - void Glyph::read(QIODevice & in) { uint16_t charcode; readStream(in, charcode); @@ -203,7 +196,6 @@ public: float maxWidth) const; }; - const Glyph & Font::getGlyph(const QChar & c) const { if (!_glyphs.contains(c)) { return _glyphs[QChar('?')]; @@ -463,24 +455,35 @@ TextRenderer* TextRenderer::getInstance(const char* family, float pointSize, int if (pointSize < 0) { pointSize = DEFAULT_POINT_SIZE; } - return new TextRenderer(Properties{ QString(family), pointSize, effect, effectThickness, color }); -} - -template -Font * loadFont(T (&t)[N]) { - Font * result = new Font(); - QBuffer buffer; - buffer.setData((const char*)t, N); - buffer.open(QBuffer::ReadOnly); - result->read(buffer); - return result; + return new TextRenderer(family, pointSize, weight, italic, effect, effectThickness, color); } static QHash LOADED_FONTS; +template +void fillBuffer(QBuffer & buffer, T(&t)[N]) { + buffer.setData((const char*)t, N); +} + +Font * loadFont(QIODevice & buffer) { + Font * result = new Font(); + result->read(buffer); + return result; +} + +template +Font * loadFont(T(&t)[N]) { + QBuffer buffer; + buffer.setData((const char*)t, N); + buffer.open(QBuffer::ReadOnly); + return loadFont(buffer); +} + Font * loadFont(const QString & family) { + if (!LOADED_FONTS.contains(family)) { if (family == MONO_FONT_FAMILY) { + LOADED_FONTS[family] = loadFont(SDFF_COURIER_PRIME); } else if (family == INCONSOLATA_FONT_FAMILY) { LOADED_FONTS[family] = loadFont(SDFF_INCONSOLATA_MEDIUM); @@ -496,12 +499,13 @@ Font * loadFont(const QString & family) { return LOADED_FONTS[family]; } -TextRenderer::TextRenderer(const Properties& properties) : - _effectType(properties.effect), - _pointSize(properties.pointSize), - _effectThickness(properties.effectThickness), - _color(properties.color), - _font(loadFont(properties.font)) +TextRenderer::TextRenderer(const char* family, float pointSize, int weight, bool italic, + EffectType effect, int effectThickness, const QColor& color) : + _effectType(effect), + _pointSize(pointSize), + _effectThickness(effectThickness), + _color(color), + _font(loadFont(family)) { } diff --git a/libraries/render-utils/src/TextRenderer.h b/libraries/render-utils/src/TextRenderer.h index e7a13f8486..0a9c069207 100644 --- a/libraries/render-utils/src/TextRenderer.h +++ b/libraries/render-utils/src/TextRenderer.h @@ -51,15 +51,6 @@ class TextRenderer { public: enum EffectType { NO_EFFECT, SHADOW_EFFECT, OUTLINE_EFFECT }; - class Properties { - public: - QString font; - float pointSize; - EffectType effect; - int effectThickness; - QColor color; - }; - static TextRenderer* getInstance(const char* family, float pointSize = -1, int weight = -1, bool italic = false, EffectType effect = NO_EFFECT, int effectThickness = 1, const QColor& color = QColor(255, 255, 255)); @@ -85,7 +76,8 @@ public: float maxWidth = -1); private: - TextRenderer(const Properties& properties); + TextRenderer(const char* family, float pointSize = -1, int weight = -1, bool italic = false, + EffectType effect = NO_EFFECT, int effectThickness = 1, const QColor& color = QColor(255, 255, 255)); // the type of effect to apply const EffectType _effectType;