From 2c138d138ed4c51b2078f0e745e515d8b12872f7 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Sun, 22 Nov 2015 12:30:42 -1000 Subject: [PATCH] Fixed GZip memory font access --- specter/freetype2 | 2 +- specter/include/Specter/FontCache.hpp | 15 ++++++- specter/lib/FontCache.cpp | 60 ++++++++++++++++++++++----- 3 files changed, 63 insertions(+), 14 deletions(-) diff --git a/specter/freetype2 b/specter/freetype2 index 76e74ad73..bddbb5dd8 160000 --- a/specter/freetype2 +++ b/specter/freetype2 @@ -1 +1 @@ -Subproject commit 76e74ad732af7278fbed227fdf57f1f38f44ac76 +Subproject commit bddbb5dd8a91591218ba2f50f4ef1c90ed8a4ca7 diff --git a/specter/include/Specter/FontCache.hpp b/specter/include/Specter/FontCache.hpp index 1f3aca696..cafd6f933 100644 --- a/specter/include/Specter/FontCache.hpp +++ b/specter/include/Specter/FontCache.hpp @@ -10,6 +10,17 @@ namespace Specter { +class FreeTypeGZipMemFace +{ + FT_StreamRec m_comp = {}; + FT_StreamRec m_decomp = {}; + FT_Face m_face; +public: + FreeTypeGZipMemFace(FT_Library lib, const uint8_t* data, size_t sz); + ~FreeTypeGZipMemFace(); + operator FT_Face() {return m_face;} +}; + class FontHandle { }; @@ -24,8 +35,8 @@ class FontCache { const HECL::Runtime::FileStoreManager& m_fileMgr; FT_Library m_fontLib; - FT_Face m_regFace; - FT_Face m_monoFace; + FreeTypeGZipMemFace m_regFace; + FreeTypeGZipMemFace m_monoFace; public: FontCache(const HECL::Runtime::FileStoreManager& fileMgr); ~FontCache(); diff --git a/specter/lib/FontCache.cpp b/specter/lib/FontCache.cpp index e39b93a9d..d12df66a0 100644 --- a/specter/lib/FontCache.cpp +++ b/specter/lib/FontCache.cpp @@ -2,30 +2,68 @@ #include #include -extern "C" uint8_t* DROIDSANS_PERMISSIVE; +#include FT_GZIP_H +#include FT_SYSTEM_H +#include +#include + +extern "C" const uint8_t DROIDSANS_PERMISSIVE[]; extern "C" size_t DROIDSANS_PERMISSIVE_SZ; -extern "C" uint8_t* BMONOFONT; +extern "C" const uint8_t BMONOFONT[]; extern "C" size_t BMONOFONT_SZ; namespace Specter { static LogVisor::LogModule Log("Specter::FontCache"); -FontCache::FontCache(const HECL::Runtime::FileStoreManager& fileMgr) -: m_fileMgr(fileMgr) +FreeTypeGZipMemFace::FreeTypeGZipMemFace(FT_Library lib, const uint8_t* data, size_t sz) { - FT_Error err = FT_Init_FreeType(&m_fontLib); + m_comp.base = (unsigned char*)data; + m_comp.size = sz; + m_comp.memory = lib->memory; + if (FT_Stream_OpenGzip(&m_decomp, &m_comp)) + Log.report(LogVisor::FatalError, "unable to open FreeType gzip stream"); + + FT_Open_Args args = + { + FT_OPEN_STREAM, + nullptr, + 0, + nullptr, + &m_decomp + }; + + if (FT_Open_Face(lib, &args, 0, &m_face)) + Log.report(LogVisor::FatalError, "unable to open FreeType gzip face"); + + FT_Done_Face(m_face); + if (m_decomp.close) + m_decomp.close(&m_decomp); +} + +FreeTypeGZipMemFace::~FreeTypeGZipMemFace() +{ + FT_Done_Face(m_face); + if (m_decomp.close) + m_decomp.close(&m_decomp); +} + +static FT_Library InitLib() +{ + FT_Library ret; + FT_Error err = FT_Init_FreeType(&ret); if (err) Log.report(LogVisor::FatalError, "unable to FT_Init_FreeType"); - err = FT_New_Memory_Face(m_fontLib, DROIDSANS_PERMISSIVE, DROIDSANS_PERMISSIVE_SZ, 0, &m_regFace); - if (err) - Log.report(LogVisor::FatalError, "unable to FT_New_Memory_Face for main UI font"); - err = FT_New_Memory_Face(m_fontLib, BMONOFONT, BMONOFONT_SZ, 0, &m_monoFace); - if (err) - Log.report(LogVisor::FatalError, "unable to FT_New_Memory_Face for mono UI font"); + return ret; } +FontCache::FontCache(const HECL::Runtime::FileStoreManager& fileMgr) +: m_fileMgr(fileMgr), + m_fontLib(InitLib()), + m_regFace(m_fontLib, DROIDSANS_PERMISSIVE, DROIDSANS_PERMISSIVE_SZ), + m_monoFace(m_fontLib, BMONOFONT, BMONOFONT_SZ) {} + FontCache::~FontCache() { FT_Done_FreeType(m_fontLib);