From 16571a884bebd0b6bd30d01a990fb342324024b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 27 Jan 2013 12:59:55 +0100 Subject: [PATCH] Text: prerender font using dedicated function, not in constructor. The number of possible combinations would explode for no reason when opening font from different locations will be implemented. --- src/Text/Font.cpp | 48 +++++++++++++++++++++-------------------------- src/Text/Font.h | 16 ++++++++-------- 2 files changed, 29 insertions(+), 35 deletions(-) diff --git a/src/Text/Font.cpp b/src/Text/Font.cpp index 8a6130fcd..de8627bee 100644 --- a/src/Text/Font.cpp +++ b/src/Text/Font.cpp @@ -26,13 +26,27 @@ namespace Magnum { namespace Text { Font::Font(FontRenderer& renderer, const std::string& fontFile, GLfloat size): _size(size) { - /** @todo Use delegating constructor when GCC 4.6 support is dropped */ - create(renderer, fontFile); + #ifndef MAGNUM_TARGET_GLES + MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::ARB::texture_rg); + #else + MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::EXT::texture_rg); + #endif + + /* Create FreeType font */ + CORRADE_INTERNAL_ASSERT(FT_New_Face(renderer.library(), fontFile.c_str(), 0, &_ftFont) == 0); + CORRADE_INTERNAL_ASSERT(FT_Set_Char_Size(_ftFont, 0, _size*64, 100, 100) == 0); + + /* Create Harfbuzz font */ + _hbFont = hb_ft_font_create(_ftFont, nullptr); + + /* Set up the texture */ + _texture.setWrapping(Texture2D::Wrapping::ClampToEdge) + ->setMinificationFilter(Texture2D::Filter::LinearInterpolation) + ->setMagnificationFilter(Texture2D::Filter::LinearInterpolation); } -Font::Font(FontRenderer& renderer, const std::string& fontFile, GLfloat size, const std::string& characters, const Vector2i& atlasSize): _size(size) { - /** @todo Use delegating constructor when GCC 4.6 support is dropped */ - create(renderer, fontFile); +void Font::prerender(const std::string& characters, const Vector2i& atlasSize) { + glyphs.clear(); /** @bug Crash when atlas is too small */ /** @todo Get rid of duplicate characters */ @@ -79,8 +93,8 @@ Font::Font(FontRenderer& renderer, const std::string& fontFile, GLfloat size, co /* Save character texture position and texture coordinates for given character index */ glyphs.insert({charIndices[i], std::make_tuple( - Rectangle::fromSize(Vector2(glyph->bitmap_left, glyph->bitmap_top-charPositions[i].height())/size, - Vector2(charPositions[i].size())/size), + Rectangle::fromSize(Vector2(glyph->bitmap_left, glyph->bitmap_top-charPositions[i].height())/_size, + Vector2(charPositions[i].size())/_size), Rectangle(Vector2(charPositions[i].bottomLeft())/atlasSize, Vector2(charPositions[i].topRight())/atlasSize) )}); @@ -94,26 +108,6 @@ Font::Font(FontRenderer& renderer, const std::string& fontFile, GLfloat size, co #endif } -void Font::create(FontRenderer& renderer, const std::string& fontFile) { - #ifndef MAGNUM_TARGET_GLES - MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::ARB::texture_rg); - #else - MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::EXT::texture_rg); - #endif - - /* Create FreeType font */ - CORRADE_INTERNAL_ASSERT(FT_New_Face(renderer.library(), fontFile.c_str(), 0, &_ftFont) == 0); - CORRADE_INTERNAL_ASSERT(FT_Set_Char_Size(_ftFont, 0, _size*64, 100, 100) == 0); - - /* Create Harfbuzz font */ - _hbFont = hb_ft_font_create(_ftFont, nullptr); - - /* Set up the texture */ - _texture.setWrapping(Texture2D::Wrapping::ClampToEdge) - ->setMinificationFilter(Texture2D::Filter::LinearInterpolation) - ->setMagnificationFilter(Texture2D::Filter::LinearInterpolation); -} - void Font::destroy() { if(!_ftFont) return; diff --git a/src/Text/Font.h b/src/Text/Font.h index 326a58ca6..30e697163 100644 --- a/src/Text/Font.h +++ b/src/Text/Font.h @@ -46,24 +46,25 @@ class MAGNUM_TEXT_EXPORT Font { public: /** - * @brief Empty font constructor + * @brief Create font from file * @param renderer %Font renderer * @param fontFile %Font file * @param size %Font size * - * Creates font with no prerendered characters. + * Creates font with no prerendered characters. See prerender() for + * more information. */ Font(FontRenderer& renderer, const std::string& fontFile, GLfloat size); /** - * @brief Create font for given character set - * @param renderer %Font renderer - * @param fontFile %Font file - * @param size %Font size + * @brief Prerender given character set * @param characters Characters to render * @param atlasSize Size of resulting atlas + * + * Creates new atlas with prerendered characters, replacing the + * previous one (if any). */ - Font(FontRenderer& renderer, const std::string& fontFile, GLfloat size, const std::string& characters, const Vector2i& atlasSize); + void prerender(const std::string& characters, const Vector2i& atlasSize); ~Font(); @@ -95,7 +96,6 @@ class MAGNUM_TEXT_EXPORT Font { inline hb_font_t* font() { return _hbFont; } private: - void MAGNUM_TEXT_LOCAL create(FontRenderer& renderer, const std::string& fontFile); void MAGNUM_TEXT_LOCAL destroy(); void MAGNUM_TEXT_LOCAL move();