splash/SplashFTFont.cc | 2 splash/SplashFTFont.h | 2 splash/SplashFTFontFile.cc | 2 splash/SplashFTFontFile.h | 2 splash/SplashFont.cc | 2 splash/SplashFont.h | 4 - splash/SplashFontEngine.cc | 100 ++++++++++++++++++--------------------------- splash/SplashFontEngine.h | 10 +--- splash/SplashFontFile.h | 2 9 files changed, 52 insertions(+), 74 deletions(-)
New commits: commit 3f08b610e4dbbbcb449e686d46360255efd78cfc Author: Oliver Sander <[email protected]> Date: Thu Sep 13 20:55:28 2018 +0200 Fold the constant splashFontCacheSize As the cache is a std::array now, the constant is only used in a single location. Hence there is no need to keep it as a separate name. diff --git a/splash/SplashFontEngine.h b/splash/SplashFontEngine.h index 193a1979..3e8a7545 100644 --- a/splash/SplashFontEngine.h +++ b/splash/SplashFontEngine.h @@ -45,10 +45,6 @@ class SplashFont; class SplashFontSrc; //------------------------------------------------------------------------ - -#define splashFontCacheSize 16 - -//------------------------------------------------------------------------ // SplashFontEngine //------------------------------------------------------------------------ @@ -96,7 +92,7 @@ public: private: - std::array<SplashFont*,splashFontCacheSize> fontCache; + std::array<SplashFont*,16> fontCache; SplashFTFontEngine *ftEngine; }; commit a7ffcfe9612970bf37edb1a78480d357d7ecd62b Author: Oliver Sander <[email protected]> Date: Thu Sep 13 20:53:32 2018 +0200 Reimplement the Splash font cache as a std::array std::find_if and std::rotate nicely replace the hand-written loops. diff --git a/splash/SplashFontEngine.cc b/splash/SplashFontEngine.cc index df47176f..d9f5118c 100644 --- a/splash/SplashFontEngine.cc +++ b/splash/SplashFontEngine.cc @@ -36,6 +36,8 @@ #ifdef HAVE_UNISTD_H # include <unistd.h> #endif +#include <algorithm> + #include "goo/gmem.h" #include "goo/GooString.h" #include "SplashMath.h" @@ -66,11 +68,7 @@ SplashFontEngine::SplashFontEngine( GBool enableFreeTypeHinting, GBool enableSlightHinting, GBool aa) { - int i; - - for (i = 0; i < splashFontCacheSize; ++i) { - fontCache[i] = nullptr; - } + std::fill(fontCache.begin(), fontCache.end(), nullptr); if (enableFreeType) { ftEngine = SplashFTFontEngine::init(aa, enableFreeTypeHinting, enableSlightHinting); @@ -80,12 +78,8 @@ SplashFontEngine::SplashFontEngine( } SplashFontEngine::~SplashFontEngine() { - int i; - - for (i = 0; i < splashFontCacheSize; ++i) { - if (fontCache[i]) { - delete fontCache[i]; - } + for (auto font : fontCache) { + delete font; } if (ftEngine) { @@ -94,12 +88,9 @@ SplashFontEngine::~SplashFontEngine() { } SplashFontFile *SplashFontEngine::getFontFile(SplashFontFileID *id) { - SplashFontFile *fontFile; - int i; - - for (i = 0; i < splashFontCacheSize; ++i) { - if (fontCache[i]) { - fontFile = fontCache[i]->getFontFile(); + for (auto font : fontCache) { + if (font) { + SplashFontFile *fontFile = font->getFontFile(); if (fontFile && fontFile->getID()->matches(id)) { return fontFile; } @@ -243,8 +234,6 @@ SplashFont *SplashFontEngine::getFont(SplashFontFile *fontFile, const SplashCoord *textMat, const SplashCoord *ctm) { SplashCoord mat[4]; - SplashFont *font; - int i, j; mat[0] = textMat[0] * ctm[0] + textMat[1] * ctm[2]; mat[1] = -(textMat[0] * ctm[1] + textMat[1] * ctm[3]); @@ -256,27 +245,24 @@ SplashFont *SplashFontEngine::getFont(SplashFontFile *fontFile, mat[2] = 0; mat[3] = 0.01; } - font = fontCache[0]; - if (font && font->matches(fontFile, mat, textMat)) { - return font; - } - for (i = 1; i < splashFontCacheSize; ++i) { - font = fontCache[i]; - if (font && font->matches(fontFile, mat, textMat)) { - for (j = i; j > 0; --j) { - fontCache[j] = fontCache[j-1]; - } - fontCache[0] = font; - return font; - } - } - font = fontFile->makeFont(mat, textMat); - if (fontCache[splashFontCacheSize - 1]) { - delete fontCache[splashFontCacheSize - 1]; + // Try to find the font in the cache + auto fontIt = std::find_if(fontCache.begin(), fontCache.end(), + [&](const SplashFont* font){return font && font->matches(fontFile, mat, textMat);} + ); + + // The requested font has been found in the cache + if (fontIt != fontCache.end()) { + std::rotate(fontCache.begin(), fontIt, fontIt+1); + return fontCache[0]; } - for (j = splashFontCacheSize - 1; j > 0; --j) { - fontCache[j] = fontCache[j-1]; + + // The requested font has not been found in the cache + auto newFont = fontFile->makeFont(mat, textMat); + if (fontCache.back()) { + delete fontCache.back(); } - fontCache[0] = font; - return font; + std::rotate(fontCache.begin(), fontCache.end()-1, fontCache.end()); + + fontCache[0] = newFont; + return fontCache[0]; } diff --git a/splash/SplashFontEngine.h b/splash/SplashFontEngine.h index ae96a9b1..193a1979 100644 --- a/splash/SplashFontEngine.h +++ b/splash/SplashFontEngine.h @@ -30,6 +30,8 @@ #pragma interface #endif +#include <array> + #include "goo/gtypes.h" #include "SplashTypes.h" @@ -94,7 +96,7 @@ public: private: - SplashFont *fontCache[splashFontCacheSize]; + std::array<SplashFont*,splashFontCacheSize> fontCache; SplashFTFontEngine *ftEngine; }; commit 588d96b572a28e5b0bef55e152d134b62818f85a Author: Oliver Sander <[email protected]> Date: Wed Sep 12 21:34:12 2018 +0200 Make SplashFont::matches a const method diff --git a/splash/SplashFont.h b/splash/SplashFont.h index 2bc5a8a1..ab3de8f1 100644 --- a/splash/SplashFont.h +++ b/splash/SplashFont.h @@ -66,7 +66,7 @@ public: // Return true if <this> matches the specified font file and matrix. GBool matches(SplashFontFile *fontFileA, SplashCoord *matA, - const SplashCoord *textMatA) { + const SplashCoord *textMatA) const { return fontFileA == fontFile && matA[0] == mat[0] && matA[1] == mat[1] && matA[2] == mat[2] && matA[3] == mat[3] && commit d248d3e59165824ca5279db4c451d63f762f1ac3 Author: Oliver Sander <[email protected]> Date: Wed Sep 12 16:17:41 2018 +0200 Make a few pointer arguments const This increases code legibility, if nothing else. diff --git a/splash/SplashFTFont.cc b/splash/SplashFTFont.cc index 2432811d..505b66a1 100644 --- a/splash/SplashFTFont.cc +++ b/splash/SplashFTFont.cc @@ -58,7 +58,7 @@ static int glyphPathCubicTo(const FT_Vector *ctrl1, const FT_Vector *ctrl2, //------------------------------------------------------------------------ SplashFTFont::SplashFTFont(SplashFTFontFile *fontFileA, SplashCoord *matA, - SplashCoord *textMatA): + const SplashCoord *textMatA): SplashFont(fontFileA, matA, textMatA, fontFileA->engine->aa), textScale(0), enableFreeTypeHinting(fontFileA->engine->enableFreeTypeHinting), diff --git a/splash/SplashFTFont.h b/splash/SplashFTFont.h index 44e1fc6b..5a6ae90e 100644 --- a/splash/SplashFTFont.h +++ b/splash/SplashFTFont.h @@ -44,7 +44,7 @@ class SplashFTFont: public SplashFont { public: SplashFTFont(SplashFTFontFile *fontFileA, SplashCoord *matA, - SplashCoord *textMatA); + const SplashCoord *textMatA); virtual ~SplashFTFont(); diff --git a/splash/SplashFTFontFile.cc b/splash/SplashFTFontFile.cc index 7fb22be9..91b627c2 100644 --- a/splash/SplashFTFontFile.cc +++ b/splash/SplashFTFontFile.cc @@ -137,7 +137,7 @@ SplashFTFontFile::~SplashFTFontFile() { } SplashFont *SplashFTFontFile::makeFont(SplashCoord *mat, - SplashCoord *textMat) { + const SplashCoord *textMat) { SplashFont *font; font = new SplashFTFont(this, mat, textMat); diff --git a/splash/SplashFTFontFile.h b/splash/SplashFTFontFile.h index 5931be3f..3ff8ff71 100644 --- a/splash/SplashFTFontFile.h +++ b/splash/SplashFTFontFile.h @@ -60,7 +60,7 @@ public: // Create a new SplashFTFont, i.e., a scaled instance of this font // file. SplashFont *makeFont(SplashCoord *mat, - SplashCoord *textMat) override; + const SplashCoord *textMat) override; // Provide access to the code-to-GID map int* getCodeToGID(); diff --git a/splash/SplashFont.cc b/splash/SplashFont.cc index 261aa419..4f3aedf5 100644 --- a/splash/SplashFont.cc +++ b/splash/SplashFont.cc @@ -46,7 +46,7 @@ struct SplashFontCacheTag { //------------------------------------------------------------------------ SplashFont::SplashFont(SplashFontFile *fontFileA, SplashCoord *matA, - SplashCoord *textMatA, GBool aaA) { + const SplashCoord *textMatA, GBool aaA) { fontFile = fontFileA; fontFile->incRefCnt(); mat[0] = matA[0]; diff --git a/splash/SplashFont.h b/splash/SplashFont.h index f8e72626..2bc5a8a1 100644 --- a/splash/SplashFont.h +++ b/splash/SplashFont.h @@ -51,7 +51,7 @@ class SplashFont { public: SplashFont(SplashFontFile *fontFileA, SplashCoord *matA, - SplashCoord *textMatA, GBool aaA); + const SplashCoord *textMatA, GBool aaA); // This must be called after the constructor, so that the subclass // constructor has a chance to compute the bbox. @@ -66,7 +66,7 @@ public: // Return true if <this> matches the specified font file and matrix. GBool matches(SplashFontFile *fontFileA, SplashCoord *matA, - SplashCoord *textMatA) { + const SplashCoord *textMatA) { return fontFileA == fontFile && matA[0] == mat[0] && matA[1] == mat[1] && matA[2] == mat[2] && matA[3] == mat[3] && diff --git a/splash/SplashFontEngine.cc b/splash/SplashFontEngine.cc index 17549c85..df47176f 100644 --- a/splash/SplashFontEngine.cc +++ b/splash/SplashFontEngine.cc @@ -240,8 +240,8 @@ void SplashFontEngine::setAA(GBool aa) { } SplashFont *SplashFontEngine::getFont(SplashFontFile *fontFile, - SplashCoord *textMat, - SplashCoord *ctm) { + const SplashCoord *textMat, + const SplashCoord *ctm) { SplashCoord mat[4]; SplashFont *font; int i, j; diff --git a/splash/SplashFontEngine.h b/splash/SplashFontEngine.h index 346d3697..ae96a9b1 100644 --- a/splash/SplashFontEngine.h +++ b/splash/SplashFontEngine.h @@ -88,7 +88,7 @@ public: // [x' y'] = [x y] * mat // Note that the Splash y axis points downward. SplashFont *getFont(SplashFontFile *fontFile, - SplashCoord *textMat, SplashCoord *ctm); + const SplashCoord *textMat, const SplashCoord *ctm); GBool getAA(); void setAA(GBool aa); diff --git a/splash/SplashFontFile.h b/splash/SplashFontFile.h index d5291519..f76538f0 100644 --- a/splash/SplashFontFile.h +++ b/splash/SplashFontFile.h @@ -73,7 +73,7 @@ public: // Create a new SplashFont, i.e., a scaled instance of this font // file. - virtual SplashFont *makeFont(SplashCoord *mat, SplashCoord *textMat) = 0; + virtual SplashFont *makeFont(SplashCoord *mat, const SplashCoord *textMat) = 0; // Get the font file ID. SplashFontFileID *getID() { return id; } commit 1243d28214471444333ea1972616ffb8c8926952 Author: Oliver Sander <[email protected]> Date: Wed Sep 12 16:02:43 2018 +0200 Minor simplification of font-loading logic If a pointer is explicitly set to nullptr, there is no need to test whether it really is nullptr in the very next line. diff --git a/splash/SplashFontEngine.cc b/splash/SplashFontEngine.cc index bebe36c2..17549c85 100644 --- a/splash/SplashFontEngine.cc +++ b/splash/SplashFontEngine.cc @@ -111,10 +111,9 @@ SplashFontFile *SplashFontEngine::getFontFile(SplashFontFileID *id) { SplashFontFile *SplashFontEngine::loadType1Font(SplashFontFileID *idA, SplashFontSrc *src, const char **enc) { - SplashFontFile *fontFile; + SplashFontFile *fontFile = nullptr; - fontFile = nullptr; - if (!fontFile && ftEngine) { + if (ftEngine) { fontFile = ftEngine->loadType1Font(idA, src, enc); } @@ -131,10 +130,9 @@ SplashFontFile *SplashFontEngine::loadType1Font(SplashFontFileID *idA, SplashFontFile *SplashFontEngine::loadType1CFont(SplashFontFileID *idA, SplashFontSrc *src, const char **enc) { - SplashFontFile *fontFile; + SplashFontFile *fontFile = nullptr; - fontFile = nullptr; - if (!fontFile && ftEngine) { + if (ftEngine) { fontFile = ftEngine->loadType1CFont(idA, src, enc); } @@ -151,10 +149,9 @@ SplashFontFile *SplashFontEngine::loadType1CFont(SplashFontFileID *idA, SplashFontFile *SplashFontEngine::loadOpenTypeT1CFont(SplashFontFileID *idA, SplashFontSrc *src, const char **enc) { - SplashFontFile *fontFile; + SplashFontFile *fontFile = nullptr; - fontFile = nullptr; - if (!fontFile && ftEngine) { + if (ftEngine) { fontFile = ftEngine->loadOpenTypeT1CFont(idA, src, enc); } @@ -170,10 +167,9 @@ SplashFontFile *SplashFontEngine::loadOpenTypeT1CFont(SplashFontFileID *idA, SplashFontFile *SplashFontEngine::loadCIDFont(SplashFontFileID *idA, SplashFontSrc *src) { - SplashFontFile *fontFile; + SplashFontFile *fontFile = nullptr; - fontFile = nullptr; - if (!fontFile && ftEngine) { + if (ftEngine) { fontFile = ftEngine->loadCIDFont(idA, src); } @@ -191,10 +187,9 @@ SplashFontFile *SplashFontEngine::loadOpenTypeCFFFont(SplashFontFileID *idA, SplashFontSrc *src, int *codeToGID, int codeToGIDLen) { - SplashFontFile *fontFile; + SplashFontFile *fontFile = nullptr; - fontFile = nullptr; - if (!fontFile && ftEngine) { + if (ftEngine) { fontFile = ftEngine->loadOpenTypeCFFFont(idA, src, codeToGID, codeToGIDLen); } @@ -213,10 +208,9 @@ SplashFontFile *SplashFontEngine::loadTrueTypeFont(SplashFontFileID *idA, int *codeToGID, int codeToGIDLen, int faceIndex) { - SplashFontFile *fontFile; + SplashFontFile *fontFile = nullptr; - fontFile = nullptr; - if (!fontFile && ftEngine) { + if (ftEngine) { fontFile = ftEngine->loadTrueTypeFont(idA, src, codeToGID, codeToGIDLen, faceIndex); } _______________________________________________ poppler mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/poppler
