From 6458b03a7689d1886e1e1367c97874e1ee2ef84a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 27 Jan 2013 15:22:36 +0100 Subject: [PATCH] Text: ability to create Font from memory instead of file. --- src/Text/Font.cpp | 24 +++++++++++++++++------- src/Text/Font.h | 10 ++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/Text/Font.cpp b/src/Text/Font.cpp index 249ac0b22..a2d02e97a 100644 --- a/src/Text/Font.cpp +++ b/src/Text/Font.cpp @@ -27,19 +27,29 @@ namespace Magnum { namespace Text { Font::Font(FontRenderer& renderer, const std::string& fontFile, GLfloat size): _size(size) { - #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_OUTPUT(FT_New_Face(renderer.library(), fontFile.c_str(), 0, &_ftFont) == 0); + + finishConstruction(); +} + +Font::Font(FontRenderer& renderer, const unsigned char* data, std::size_t dataSize, GLfloat size): _size(size) { + CORRADE_INTERNAL_ASSERT_OUTPUT(FT_New_Memory_Face(renderer.library(), data, dataSize, 0, &_ftFont) == 0); + + finishConstruction(); +} + +void Font::finishConstruction() { CORRADE_INTERNAL_ASSERT_OUTPUT(FT_Set_Char_Size(_ftFont, 0, _size*64, 100, 100) == 0); /* Create Harfbuzz font */ _hbFont = hb_ft_font_create(_ftFont, nullptr); + #ifndef MAGNUM_TARGET_GLES + MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::ARB::texture_rg); + #else + MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::EXT::texture_rg); + #endif + /* Set up the texture */ _texture.setWrapping(Texture2D::Wrapping::ClampToEdge) ->setMinificationFilter(Texture2D::Filter::LinearInterpolation) diff --git a/src/Text/Font.h b/src/Text/Font.h index 1bc84f567..0a59f6975 100644 --- a/src/Text/Font.h +++ b/src/Text/Font.h @@ -53,6 +53,15 @@ class MAGNUM_TEXT_EXPORT Font { */ explicit Font(FontRenderer& renderer, const std::string& fontFile, GLfloat size); + /** + * @brief Create font from memory + * @param renderer %Font renderer + * @param data %Font data + * @param size %Font data size + * @param size %Font size + */ + explicit Font(FontRenderer& renderer, const unsigned char* data, std::size_t dataSize, GLfloat size); + /** * @brief Prerender given character set * @param characters Characters to render @@ -93,6 +102,7 @@ class MAGNUM_TEXT_EXPORT Font { inline hb_font_t* font() { return _hbFont; } private: + void MAGNUM_TEXT_LOCAL finishConstruction(); void MAGNUM_TEXT_LOCAL destroy(); void MAGNUM_TEXT_LOCAL move();