[4.0] FreeType fonts/library - Stop creating new instances. 81/185781/4
authorVictor Cebollada <v.cebollada@samsung.com>
Wed, 1 Aug 2018 16:00:24 +0000 (17:00 +0100)
committerVíctor Cebollada <v.cebollada@samsung.com>
Thu, 2 Aug 2018 13:48:02 +0000 (13:48 +0000)
* The shaping tool creates a new instance
of the FreeType library and the font faces instead of using the
ones cached in the font client.
  It causes a bug in the WatchFace emulator when a new model is
loaded (The text show the wrong glyphs shaped with the wrong font).

Change-Id: I1c9ff437b9eed42ece2290642c279bc228ffa442
Signed-off-by: Victor Cebollada <v.cebollada@samsung.com>
text/dali/internal/text-abstraction/font-client-impl.cpp
text/dali/internal/text-abstraction/font-client-impl.h
text/dali/internal/text-abstraction/font-client-plugin-impl.cpp
text/dali/internal/text-abstraction/font-client-plugin-impl.h
text/dali/internal/text-abstraction/shaping-impl.cpp

index ee026cf..25cb710 100644 (file)
@@ -297,6 +297,13 @@ bool FontClient::AddCustomFontDirectory( const FontPath& path )
   return mPlugin->AddCustomFontDirectory( path );
 }
 
+FT_FaceRec_* FontClient::GetFreetypeFace( FontId fontId )
+{
+  CreatePlugin();
+
+  return mPlugin->GetFreetypeFace( fontId );
+}
+
 void FontClient::CreatePlugin()
 {
   if( !mPlugin )
index 725f0e0..7612d51 100644 (file)
@@ -24,6 +24,9 @@
 // INTERNAL INCLUDES
 #include <dali/devel-api/text-abstraction/font-client.h>
 
+
+struct FT_FaceRec_;
+
 namespace Dali
 {
 
@@ -203,6 +206,15 @@ public:
    */
   bool AddCustomFontDirectory( const FontPath& path );
 
+  /**
+   * @brief Retrieves the pointer to the FreeType Font Face for the given @p fontId.
+   *
+   * @param[in] fontId The font id.
+   *
+   * @return The pointer to the FreeType Font Face.
+   */
+  FT_FaceRec_* GetFreetypeFace( FontId fontId );
+
 private:
 
   /**
index 5f5a304..64c00c0 100644 (file)
@@ -1288,6 +1288,19 @@ bool FontClient::Plugin::AddCustomFontDirectory( const FontPath& path )
   return FcConfigAppFontAddDir( nullptr, reinterpret_cast<const FcChar8 *>( path.c_str() ) );
 }
 
+FT_FaceRec_* FontClient::Plugin::GetFreetypeFace( FontId fontId )
+{
+  FT_Face fontFace = nullptr;
+
+  if( ( fontId > 0u ) &&
+      ( fontId - 1u < mFontFaceCache.size() ) )
+  {
+    fontFace = mFontFaceCache[fontId - 1u].mFreeTypeFace;
+  }
+
+  return fontFace;
+}
+
 void FontClient::Plugin::InitSystemFonts()
 {
   DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::InitSystemFonts\n" );
index 2e6e35a..31a1364 100644 (file)
@@ -333,6 +333,11 @@ struct FontClient::Plugin
    */
   bool AddCustomFontDirectory( const FontPath& path );
 
+  /**
+   * @copydoc Dali::TextAbstraction::Internal::FontClient::GetFreetypeFace()
+   */
+  FT_FaceRec_* GetFreetypeFace( FontId fontId );
+
 private:
 
   /**
index 4d389c5..228b3de 100644 (file)
 #include <dali/devel-api/text-abstraction/font-client.h>
 #include <dali/devel-api/text-abstraction/glyph-info.h>
 #include <dali/integration-api/debug.h>
+#include "font-client-impl.h"
 
 // EXTERNAL INCLUDES
 #include <harfbuzz/hb.h>
 #include <harfbuzz/hb-ft.h>
 
-#include <ft2build.h>
-#include <iostream>
-
 namespace Dali
 {
 
@@ -40,7 +38,7 @@ namespace TextAbstraction
 namespace Internal
 {
 
-const char*        DEFAULT_LANGUAGE = "en";
+const char* const  DEFAULT_LANGUAGE = "en";
 const unsigned int DEFAULT_LANGUAGE_LENGTH = 2u;
 const float        FROM_266 = 1.0f / 64.0f;
 
@@ -115,8 +113,7 @@ const hb_script_t SCRIPT_TO_HARFBUZZ[] =
 struct Shaping::Plugin
 {
   Plugin()
-  : mFreeTypeLibrary( NULL ),
-    mIndices(),
+  : mIndices(),
     mAdvance(),
     mCharacterMap(),
     mFontId( 0u )
@@ -125,16 +122,6 @@ struct Shaping::Plugin
 
   ~Plugin()
   {
-    FT_Done_FreeType( mFreeTypeLibrary );
-  }
-
-  void Initialize()
-  {
-    int error = FT_Init_FreeType( &mFreeTypeLibrary );
-    if( FT_Err_Ok != error )
-    {
-      DALI_LOG_ERROR( "FreeType Init error: %d\n", error );
-    }
   }
 
   Length Shape( const Character* const text,
@@ -157,30 +144,18 @@ struct Shaping::Plugin
     mOffset.Reserve( 2u * numberOfGlyphs );
 
     TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
-
-    // Get the font's path file name from the font Id.
-    FontDescription fontDescription;
-    fontClient.GetDescription( fontId, fontDescription );
+    TextAbstraction::Internal::FontClient& fontClientImpl = TextAbstraction::GetImplementation( fontClient );
 
     // Create a FreeType font's face.
     FT_Face face;
-    FT_Error retVal = FT_New_Face( mFreeTypeLibrary, fontDescription.path.c_str(), 0u, &face );
-    if( FT_Err_Ok != retVal )
+
+    face = fontClientImpl.GetFreetypeFace( fontId );
+    if( nullptr == face )
     {
-      DALI_LOG_ERROR( "Failed to open face: %s\n", fontDescription.path.c_str() );
+      // Nothing to do if the face is null.
       return 0u;
     }
 
-    unsigned int horizontalDpi = 0u;
-    unsigned int verticalDpi = 0u;
-    fontClient.GetDpi( horizontalDpi, verticalDpi );
-
-    FT_Set_Char_Size( face,
-                      0u,
-                      fontClient.GetPointSize( fontId ),
-                      horizontalDpi,
-                      verticalDpi );
-
     /* Get our harfbuzz font struct */
     hb_font_t* harfBuzzFont;
     harfBuzzFont = hb_ft_font_create( face, NULL );
@@ -274,7 +249,6 @@ struct Shaping::Plugin
     /* Cleanup */
     hb_buffer_destroy( harfBuzzBuffer );
     hb_font_destroy( harfBuzzFont );
-    FT_Done_Face( face );
 
     return mIndices.Count();
   }
@@ -304,8 +278,6 @@ struct Shaping::Plugin
     }
   }
 
-  FT_Library             mFreeTypeLibrary;
-
   Vector<CharacterIndex> mIndices;
   Vector<float>          mAdvance;
   Vector<float>          mOffset;
@@ -375,7 +347,6 @@ void Shaping::CreatePlugin()
   if( !mPlugin )
   {
     mPlugin = new Plugin();
-    mPlugin->Initialize();
   }
 }