[aseprite] 52/196: Add a simple cache mechanism to ft::Face (at the moment without cache)

Tobias Hansen thansen at moszumanska.debian.org
Wed Apr 20 18:50:00 UTC 2016


This is an automated email from the git hooks/post-receive script.

thansen pushed a commit to branch master
in repository aseprite.

commit d020a19bd21bcc4274ffa0b1f543603e9b06c3e5
Author: David Capello <davidcapello at gmail.com>
Date:   Thu Mar 10 17:10:17 2016 -0300

    Add a simple cache mechanism to ft::Face (at the moment without cache)
---
 src/ft/face.h | 55 ++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 40 insertions(+), 15 deletions(-)

diff --git a/src/ft/face.h b/src/ft/face.h
index d42398c..19657bb 100644
--- a/src/ft/face.h
+++ b/src/ft/face.h
@@ -14,12 +14,13 @@
 
 namespace ft {
 
-  class Face {
+  template<typename Cache>
+  class FaceT {
   public:
-    Face(FT_Face face = nullptr) : m_face(face) {
+    FaceT(FT_Face face = nullptr) : m_face(face) {
     }
 
-    ~Face() {
+    ~FaceT() {
       if (m_face)
         FT_Done_Face(m_face);
     }
@@ -37,10 +38,12 @@ namespace ft {
 
     void setAntialias(bool antialias) {
       m_antialias = antialias;
+      m_cache.invalidate();
     }
 
     void setSize(int size) {
       FT_Set_Pixel_Sizes(m_face, size, size);
+      m_cache.invalidate();
     }
 
     template<typename Iterator,
@@ -51,7 +54,7 @@ namespace ft {
       FT_UInt prev_glyph = 0;
       int x = 0;
       for (; first != end; ++first) {
-        FT_UInt glyph_index = FT_Get_Char_Index(m_face, *first);
+        FT_UInt glyph_index = m_cache.getGlyphIndex(m_face, *first);
 
         if (use_kerning && prev_glyph && glyph_index) {
           FT_Vector kerning;
@@ -60,16 +63,10 @@ namespace ft {
           x += kerning.x >> 6;
         }
 
-        FT_Error err = FT_Load_Glyph(
-          m_face, glyph_index,
-          FT_LOAD_RENDER |
-          FT_LOAD_NO_BITMAP |
-          (m_antialias ? FT_LOAD_TARGET_NORMAL:
-                         FT_LOAD_TARGET_MONO));
-
-        if (!err) {
-          callback(m_face->glyph, x);
-          x += m_face->glyph->advance.x >> 6;
+        FT_GlyphSlot glyph = m_cache.loadGlyph(m_face, glyph_index, m_antialias);
+        if (glyph) {
+          callback(glyph, x);
+          x += glyph->advance.x >> 6;
         }
 
         prev_glyph = glyph_index;
@@ -95,10 +92,38 @@ namespace ft {
   private:
     FT_Face m_face;
     bool m_antialias;
+    Cache m_cache;
 
-    DISABLE_COPYING(Face);
+    DISABLE_COPYING(FaceT);
   };
 
+  class NoCache {
+  public:
+    void invalidate() {
+      // Do nothing
+    }
+
+    FT_UInt getGlyphIndex(FT_Face face, int charCode) {
+      return FT_Get_Char_Index(face, charCode);
+    }
+
+    FT_GlyphSlot loadGlyph(FT_Face face, FT_UInt glyphIndex, bool antialias) {
+      FT_Error err = FT_Load_Glyph(
+        face, glyphIndex,
+        FT_LOAD_RENDER |
+        FT_LOAD_NO_BITMAP |
+        (antialias ? FT_LOAD_TARGET_NORMAL:
+                     FT_LOAD_TARGET_MONO));
+      if (!err)
+        return face->glyph;
+      else
+        return nullptr;
+    }
+
+  };
+
+  typedef FaceT<NoCache> Face;
+
 } // namespace ft
 
 #endif

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/aseprite.git



More information about the Pkg-games-commits mailing list