From d9922a840be6588927af13f3fb03553cd89844dd Mon Sep 17 00:00:00 2001 From: danij Date: Wed, 13 Nov 2013 23:53:19 +0000 Subject: [PATCH] Refactor|AbstractFont|Resources|Client: Relocated more functionality out of AbstractFont --- .../client/include/resource/abstractfont.h | 25 +++----- doomsday/client/include/resource/bitmapfont.h | 6 +- .../include/resource/compositebitmapfont.h | 4 ++ doomsday/client/src/render/rend_font.cpp | 23 ++++---- doomsday/client/src/resource/abstractfont.cpp | 13 +---- doomsday/client/src/resource/bitmapfont.cpp | 57 ++++++++++++++----- .../src/resource/compositebitmapfont.cpp | 29 +++++++++- doomsday/client/src/resource/fonts.cpp | 4 +- 8 files changed, 103 insertions(+), 58 deletions(-) diff --git a/doomsday/client/include/resource/abstractfont.h b/doomsday/client/include/resource/abstractfont.h index 1d570f5402..1c62778876 100644 --- a/doomsday/client/include/resource/abstractfont.h +++ b/doomsday/client/include/resource/abstractfont.h @@ -43,41 +43,32 @@ class AbstractFont { public: - /// @ref fontFlags. - int _flags; - /// Unique identifier of the primary binding in the owning collection. fontid_t _primaryBind; - /// Font metrics. - int _leading; - int _ascent; - int _descent; - - /// Do fonts have margins? Is this a pixel border in the composited character - /// map texture (perhaps per-glyph)? - de::Vector2ui _margin; + /// @ref fontFlags. + int _flags; AbstractFont(fontid_t bindId = 0); virtual ~AbstractFont() {} DENG2_AS_IS_METHODS() - fontid_t primaryBind() const; - void setPrimaryBind(fontid_t bindId); - /// @return @ref fontFlags int flags() const; - int ascent(); - int descent(); - int lineSpacing(); + virtual int ascent(); + virtual int descent(); + virtual int lineSpacing(); virtual void glInit(); virtual void glDeinit(); virtual de::Rectanglei const &glyphPosCoords(uchar ch) = 0; virtual de::Rectanglei const &glyphTexCoords(uchar ch) = 0; + + fontid_t primaryBind() const; + void setPrimaryBind(fontid_t bindId); }; #endif // CLIENT_RESOURCE_ABSTRACTFONT_H diff --git a/doomsday/client/include/resource/bitmapfont.h b/doomsday/client/include/resource/bitmapfont.h index 6d435762d9..d0981d3e73 100644 --- a/doomsday/client/include/resource/bitmapfont.h +++ b/doomsday/client/include/resource/bitmapfont.h @@ -38,12 +38,16 @@ class BitmapFont : public AbstractFont static BitmapFont *fromFile(fontid_t bindId, de::String resourcePath); - void rebuildFromFile(de::String resourcePath); void setFilePath(de::String resourcePath); /// @return GL-texture name. GLuint textureGLName() const; de::Vector2i const &textureDimensions() const; + de::Vector2ui const &textureMargin() const; + + int ascent(); + int descent(); + int lineSpacing(); void glInit(); void glDeinit(); diff --git a/doomsday/client/include/resource/compositebitmapfont.h b/doomsday/client/include/resource/compositebitmapfont.h index 6ccf851237..254a0e9b46 100644 --- a/doomsday/client/include/resource/compositebitmapfont.h +++ b/doomsday/client/include/resource/compositebitmapfont.h @@ -62,6 +62,10 @@ class CompositeBitmapFont : public AbstractFont */ void rebuildFromDef(ded_compositefont_t *def); + int ascent(); + int descent(); + int lineSpacing(); + void glInit(); void glDeinit(); diff --git a/doomsday/client/src/render/rend_font.cpp b/doomsday/client/src/render/rend_font.cpp index a731bc75fa..616a85d7e4 100644 --- a/doomsday/client/src/render/rend_font.cpp +++ b/doomsday/client/src/render/rend_font.cpp @@ -776,6 +776,18 @@ static void drawChar(uchar ch, float x, float y, AbstractFont *font, /// @todo We should not need to re-bind this texture here. GL_BindTextureUnmanaged(bmapFont->textureGLName(), gl::ClampToEdge, gl::ClampToEdge, filterUI? gl::Linear : gl::Nearest); + + Vector2ui const &texMargin = bmapFont->textureMargin(); + if(texMargin.x) + { + geometry.topLeft.x -= texMargin.x; + geometry.setWidth(geometry.width() + texMargin.x * 2); + } + if(texMargin.y) + { + geometry.topLeft.y -= texMargin.y; + geometry.setHeight(geometry.height() + texMargin.y * 2); + } } else if(CompositeBitmapFont *compFont = font->maybeAs()) { @@ -786,17 +798,6 @@ static void drawChar(uchar ch, float x, float y, AbstractFont *font, } } - if(font->_margin.x) - { - geometry.topLeft.x -= font->_margin.x; - geometry.setWidth(geometry.width() + font->_margin.x * 2); - } - if(font->_margin.y) - { - geometry.topLeft.y -= font->_margin.y; - geometry.setHeight(geometry.height() + font->_margin.y * 2); - } - Vector2i coords[4] = { font->glyphTexCoords(ch).topLeft, font->glyphTexCoords(ch).topRight(), font->glyphTexCoords(ch).bottomRight, diff --git a/doomsday/client/src/resource/abstractfont.cpp b/doomsday/client/src/resource/abstractfont.cpp index b3963ce6b9..13539ac846 100644 --- a/doomsday/client/src/resource/abstractfont.cpp +++ b/doomsday/client/src/resource/abstractfont.cpp @@ -25,9 +25,6 @@ using namespace de; AbstractFont::AbstractFont(fontid_t bindId) : _flags(0) , _primaryBind(bindId) - , _leading(0) - , _ascent(0) - , _descent(0) {} void AbstractFont::glInit() @@ -53,19 +50,15 @@ int AbstractFont::flags() const int AbstractFont::ascent() { - glInit(); - return _ascent; + return 0; } int AbstractFont::descent() { - glInit(); - return _descent; + return 0; } int AbstractFont::lineSpacing() { - glInit(); - return _leading; + return 0; } - diff --git a/doomsday/client/src/resource/bitmapfont.cpp b/doomsday/client/src/resource/bitmapfont.cpp index 4f98884920..947d87210b 100644 --- a/doomsday/client/src/resource/bitmapfont.cpp +++ b/doomsday/client/src/resource/bitmapfont.cpp @@ -52,11 +52,17 @@ struct Glyph DENG2_PIMPL(BitmapFont) { - String filePath; ///< The "archived" version of this font (if any). - GLuint texGLName; ///< GL-texture name. - Vector2i texDimensions; ///< Texture dimensions in pixels. + String filePath; ///< The "archived" version of this font (if any). + GLuint texGLName; ///< GL-texture name. + de::Vector2ui texMargin; ///< Margin in pixels. + Vector2i texDimensions; ///< Texture dimensions in pixels. bool needGLInit; + /// Font metrics. + int leading; + int ascent; + int descent; + Glyph glyphs[MAX_CHARS]; Glyph missingGlyph; @@ -64,6 +70,9 @@ DENG2_PIMPL(BitmapFont) : Base(i) , texGLName(0) , needGLInit(true) + , leading(0) + , ascent(0) + , descent(0) {} ~Instance() @@ -83,7 +92,7 @@ DENG2_PIMPL(BitmapFont) self._flags |= FF_COLORIZE; self._flags &= ~FF_SHADOWED; - self._margin = Vector2ui(0, 0); + texMargin = Vector2ui(0, 0); // Load in the data. texDimensions.x = inShort(file); @@ -99,7 +108,7 @@ DENG2_PIMPL(BitmapFont) ushort w = inByte(file); ushort h = inByte(file); - ch->posCoords = Rectanglei::fromSize(Vector2i(0, 0), Vector2ui(w, h) - self._margin * 2); + ch->posCoords = Rectanglei::fromSize(Vector2i(0, 0), Vector2ui(w, h) - texMargin * 2); ch->texCoords = Rectanglei::fromSize(Vector2i(x, y), Vector2ui(w, h)); avgSize += ch->posCoords.size(); @@ -152,12 +161,12 @@ DENG2_PIMPL(BitmapFont) texDimensions.x = inShort(file); texDimensions.y = M_CeilPow2(inShort(file)); int glyphCount = inShort(file); - self._margin.x = self._margin.y = inShort(file); + texMargin.x = texMargin.y = inShort(file); - self._leading = inShort(file); + leading = inShort(file); /*glyphHeight =*/ inShort(file); // Unused. - self._ascent = inShort(file); - self._descent = inShort(file); + ascent = inShort(file); + descent = inShort(file); Vector2ui avgSize; for(int i = 0; i < glyphCount; ++i) @@ -169,7 +178,7 @@ DENG2_PIMPL(BitmapFont) ushort h = inShort(file); Glyph *ch = &glyphs[code]; - ch->posCoords = Rectanglei::fromSize(Vector2i(0, 0), self._margin * 2 - Vector2ui(w, h)); + ch->posCoords = Rectanglei::fromSize(Vector2i(0, 0), texMargin * 2 - Vector2ui(w, h)); ch->texCoords = Rectanglei::fromSize(Vector2i(x, y), Vector2ui(w, h)); avgSize += ch->posCoords.size(); @@ -213,11 +222,6 @@ BitmapFont::BitmapFont(fontid_t bindId) : AbstractFont(), d(new Instance(this)) setPrimaryBind(bindId); } -void BitmapFont::rebuildFromFile(String resourcePath) -{ - setFilePath(resourcePath); -} - BitmapFont *BitmapFont::fromFile(fontid_t bindId, String resourcePath) // static { BitmapFont *font = new BitmapFont(bindId); @@ -230,6 +234,24 @@ BitmapFont *BitmapFont::fromFile(fontid_t bindId, String resourcePath) // static return font; } +int BitmapFont::ascent() +{ + glInit(); + return d->ascent; +} + +int BitmapFont::descent() +{ + glInit(); + return d->descent; +} + +int BitmapFont::lineSpacing() +{ + glInit(); + return d->leading; +} + Rectanglei const &BitmapFont::glyphPosCoords(uchar ch) { glInit(); @@ -320,3 +342,8 @@ Vector2i const &BitmapFont::textureDimensions() const { return d->texDimensions; } + +Vector2ui const &BitmapFont::textureMargin() const +{ + return d->texMargin; +} diff --git a/doomsday/client/src/resource/compositebitmapfont.cpp b/doomsday/client/src/resource/compositebitmapfont.cpp index eb3e2a49eb..a66fde0305 100644 --- a/doomsday/client/src/resource/compositebitmapfont.cpp +++ b/doomsday/client/src/resource/compositebitmapfont.cpp @@ -33,6 +33,11 @@ DENG2_PIMPL(CompositeBitmapFont) ded_compositefont_t *def; /// Definition on which "this" font is derived (if any). bool needGLInit; + /// Font metrics. + int leading; + int ascent; + int descent; + Glyph glyphs[MAX_CHARS]; Glyph missingGlyph; @@ -40,6 +45,9 @@ DENG2_PIMPL(CompositeBitmapFont) : Base(i) , def(0) , needGLInit(true) + , leading(0) + , ascent(0) + , descent(0) { zap(glyphs); zap(missingGlyph); @@ -65,6 +73,24 @@ CompositeBitmapFont::CompositeBitmapFont(fontid_t bindId) setPrimaryBind(bindId); } +int CompositeBitmapFont::ascent() +{ + glInit(); + return d->ascent; +} + +int CompositeBitmapFont::descent() +{ + glInit(); + return d->descent; +} + +int CompositeBitmapFont::lineSpacing() +{ + glInit(); + return d->leading; +} + Rectanglei const &CompositeBitmapFont::glyphPosCoords(uchar ch) { glInit(); @@ -142,8 +168,7 @@ void CompositeBitmapFont::glInit() } ch->geometry = Rectanglei::fromSize(Vector2i(info.geometry.origin.xy), - Vector2ui(info.geometry.size.width, info.geometry.size.height)) - .expanded(_margin.toVector2i()); + Vector2ui(info.geometry.size.width, info.geometry.size.height)); avgSize += ch->geometry.size(); ++foundGlyphs; diff --git a/doomsday/client/src/resource/fonts.cpp b/doomsday/client/src/resource/fonts.cpp index f699299586..5a626e0fdc 100644 --- a/doomsday/client/src/resource/fonts.cpp +++ b/doomsday/client/src/resource/fonts.cpp @@ -508,7 +508,7 @@ DENG2_PIMPL(Fonts) LOG_DEBUG("A Font with uri \"%s\" already exists, returning existing.") << self.composeUri(id); #endif - bmapFont->rebuildFromFile(resourcePath); + bmapFont->setFilePath(resourcePath); } return record->font; } @@ -1106,7 +1106,7 @@ AbstractFont *Fonts::createFontFromFile(Uri const &uri, char const *resourcePath { if(BitmapFont *bmapFont = font->maybeAs()) { - bmapFont->rebuildFromFile(resourcePath); + bmapFont->setFilePath(resourcePath); } } else