Merge "Send the empty string to IME when PlaceholderText shows" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / multi-language-support-impl.cpp
index 8b491b0..990d0ce 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -63,23 +63,38 @@ bool ValidateFontsPerScript::IsValidFont( FontId fontId ) const
   return false;
 }
 
-FontId DefaultFonts::FindFont( TextAbstraction::FontClient& fontClient, PointSize26Dot6 size ) const
+FontId DefaultFonts::FindFont( TextAbstraction::FontClient& fontClient,
+                               const TextAbstraction::FontDescription& description,
+                               PointSize26Dot6 size ) const
 {
-  for( Vector<FontId>::ConstIterator it = mFonts.Begin(),
-         endIt = mFonts.End();
+  for( std::vector<CacheItem>::const_iterator it = mFonts.begin(),
+         endIt = mFonts.end();
        it != endIt;
        ++it )
   {
-    const FontId fontId = *it;
-    if( size == fontClient.GetPointSize( fontId ) )
+    const CacheItem& item = *it;
+
+    if( ( ( TextAbstraction::FontWeight::NONE == description.weight ) || ( description.weight == item.description.weight ) ) &&
+        ( ( TextAbstraction::FontWidth::NONE == description.width )   || ( description.width == item.description.width ) ) &&
+        ( ( TextAbstraction::FontSlant::NONE == description.slant )   || ( description.slant == item.description.slant ) ) &&
+        ( size == fontClient.GetPointSize( item.fontId ) ) &&
+        ( description.family.empty() || ( description.family == item.description.family ) ) )
     {
-      return fontId;
+      return item.fontId;
     }
   }
 
   return 0u;
 }
 
+void DefaultFonts::Cache( const TextAbstraction::FontDescription& description, FontId fontId )
+{
+  CacheItem item;
+  item.description = description;
+  item.fontId = fontId;
+  mFonts.push_back( item );
+}
+
 MultilanguageSupport::MultilanguageSupport()
 : mDefaultFontPerScriptCache(),
   mValidFontsPerScriptCache()
@@ -374,7 +389,8 @@ void MultilanguageSupport::SetScripts( const Vector<Character>& text,
 void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
                                           const Vector<ScriptRun>& scripts,
                                           const Vector<FontDescriptionRun>& fontDescriptions,
-                                          FontId defaultFontId,
+                                          const TextAbstraction::FontDescription& defaultFontDescription,
+                                          TextAbstraction::PointSize26Dot6 defaultFontPointSize,
                                           CharacterIndex startIndex,
                                           Length numberOfCharacters,
                                           Vector<FontRun>& fonts )
@@ -424,67 +440,42 @@ void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
   // Get the font client.
   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
 
-  // Get the default font description and default size.
-  TextAbstraction::FontDescription defaultFontDescription;
-  TextAbstraction::PointSize26Dot6 defaultPointSize = TextAbstraction::FontClient::DEFAULT_POINT_SIZE;
-  if( defaultFontId > 0u )
-  {
-    fontClient.GetDescription( defaultFontId, defaultFontDescription );
-    defaultPointSize = fontClient.GetPointSize( defaultFontId );
-  }
-
-  // Merge font descriptions
-  Vector<FontId> fontIds;
-  fontIds.Resize( numberOfCharacters, defaultFontId );
-  Vector<bool> isDefaultFont;
-  isDefaultFont.Resize( numberOfCharacters, true );
-  MergeFontDescriptions( fontDescriptions,
-                         fontIds,
-                         isDefaultFont,
-                         defaultFontDescription,
-                         defaultPointSize,
-                         startIndex,
-                         numberOfCharacters );
-
   const Character* const textBuffer = text.Begin();
-  const FontId* const fontIdsBuffer = fontIds.Begin();
-  const bool* const isDefaultFontBuffer = isDefaultFont.Begin();
+
+  // Iterators of the script runs.
   Vector<ScriptRun>::ConstIterator scriptRunIt = scripts.Begin();
   Vector<ScriptRun>::ConstIterator scriptRunEndIt = scripts.End();
   bool isNewParagraphCharacter = false;
 
-  PointSize26Dot6 currentPointSize = defaultPointSize;
   FontId currentFontId = 0u;
   FontId previousFontId = 0u;
   bool isPreviousEmojiScript = false;
 
-  // Whether it's the first set of characters to be validated.
-  // Used in case the paragraph starts with characters common to all scripts.
-  bool isFirstSetToBeValidated = true;
-
   CharacterIndex lastCharacter = startIndex + numberOfCharacters;
   for( Length index = startIndex; index < lastCharacter; ++index )
   {
     // Get the current character.
     const Character character = *( textBuffer + index );
 
-    // Get the font for the current character.
-    FontId fontId = *( fontIdsBuffer + index - startIndex );
+    TextAbstraction::FontDescription currentFontDescription;
+    TextAbstraction::PointSize26Dot6 currentFontPointSize = defaultFontPointSize;
+    bool isDefaultFont = true;
+    MergeFontDescriptions( fontDescriptions,
+                           defaultFontDescription,
+                           defaultFontPointSize,
+                           index,
+                           currentFontDescription,
+                           currentFontPointSize,
+                           isDefaultFont );
 
-    // Whether the font being validated for the current character is a default one not set by the user.
-    const bool isDefault = *( isDefaultFontBuffer + index - startIndex );
+    // Get the font for the current character.
+    FontId fontId = fontClient.GetFontId( currentFontDescription, currentFontPointSize );
+    currentFontId = fontId;
 
     // Get the script for the current character.
-    const Script script = GetScript( index,
-                                     scriptRunIt,
-                                     scriptRunEndIt );
-
-    // Get the current point size.
-    if( currentFontId != fontId )
-    {
-      currentPointSize = fontClient.GetPointSize( fontId );
-      currentFontId = fontId;
-    }
+    Script script = GetScript( index,
+                               scriptRunIt,
+                               scriptRunEndIt );
 
 #ifdef DEBUG_ENABLED
     {
@@ -499,17 +490,24 @@ void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
                      description.path.c_str() );
     }
 #endif
+    if( script == TextAbstraction::UNKNOWN )
+    {
+      script = TextAbstraction::LATIN;
+    }
 
     // Validate whether the current character is supported by the given font.
     bool isValidFont = false;
 
     // Check first in the cache of default fonts per script and size.
 
-    DefaultFonts* defaultFonts = *( defaultFontPerScriptCacheBuffer + script );
     FontId cachedDefaultFontId = 0u;
+    DefaultFonts* defaultFonts = *( defaultFontPerScriptCacheBuffer + script );
     if( NULL != defaultFonts )
     {
-      cachedDefaultFontId = defaultFonts->FindFont( fontClient, currentPointSize );
+      // This cache stores fall-back fonts.
+      cachedDefaultFontId = defaultFonts->FindFont( fontClient,
+                                                    currentFontDescription,
+                                                    currentFontPointSize );
     }
 
     // Whether the cached default font is valid.
@@ -518,6 +516,12 @@ void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
     // The font is valid if it matches with the default one for the current script and size and it's different than zero.
     isValidFont = isValidCachedDefaultFont && ( fontId == cachedDefaultFontId );
 
+    if( isValidFont )
+    {
+      // Check if the font supports the character.
+      isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
+    }
+
     bool isCommonScript = false;
     bool isEmojiScript = TextAbstraction::EMOJI == script;
 
@@ -536,7 +540,6 @@ void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
       currentFontRun.fontId = fontId;
     }
 
-
     // If the given font is not valid, it means either:
     // - there is no cached font for the current script yet or,
     // - the user has set a different font than the default one for the current script or,
@@ -562,13 +565,12 @@ void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
       if( isCommonScript )
       {
         if( isValidCachedDefaultFont &&
-            ( isDefault || ( currentFontId == previousFontId ) ) &&
+            ( isDefaultFont || ( currentFontId == previousFontId ) ) &&
             !isEmojiScript )
         {
           // At this point the character common for all scripts has no font assigned.
           // If there is a valid previously cached default font for it, use that one.
           fontId = cachedDefaultFontId;
-          isValidFont = true;
         }
       }
       else
@@ -578,25 +580,33 @@ void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
 
         if( NULL != validateFontsPerScript )
         {
+          // This cache stores valid fonts set by the user.
           isValidFont = validateFontsPerScript->IsValidFont( fontId );
+
+          // It may happen that a validated font for a script doesn't have all the glyphs for that script.
+          // i.e a font validated for the CJK script may contain glyphs for the chinese language but not for the Japanese.
+          if( isValidFont )
+          {
+            // Checks if the current character is supported by the font is needed.
+            isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
+          }
         }
 
         if( !isValidFont ) // (2)
         {
-          // Use the font client to validate the font.
-          const GlyphIndex glyphIndex = fontClient.GetGlyphIndex( fontId, character );
+          // The selected font is not stored in any cache.
 
-          // The font is valid if there is a glyph for that character.
-          isValidFont = 0u != glyphIndex;
+          // Checks if the current character is supported by the selected font.
+          isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
 
           // Emojis are present in many monochrome fonts; prefer color by default.
           if( isValidFont &&
               isEmojiScript )
           {
-            const PixelData bitmap = fontClient.CreateBitmap( fontId, glyphIndex );
+            const GlyphIndex glyphIndex = fontClient.GetGlyphIndex( fontId, character );
 
-            // For color emojis, the font is valid if the bitmap is RGBA.
-            isValidFont = bitmap && ( Pixel::BGRA8888 == bitmap.GetPixelFormat() );
+            // For color emojis, the font is valid if the glyph is a color glyph (the bitmap is RGBA).
+            isValidFont = fontClient.IsColorGlyph( fontId, glyphIndex );
           }
 
           // If there is a valid font, cache it.
@@ -612,12 +622,19 @@ void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
             validateFontsPerScript->mValidFonts.PushBack( fontId );
           }
 
-          if( !isValidFont ) // (3)
+          if( !isValidFont && ( fontId != cachedDefaultFontId ) ) // (3)
           {
-            // The given font has not been validated.
+            // The selected font by the user or the platform's default font has failed to validate the character.
 
+            // Checks if the previously discarted cached default font supports the character.
+            bool isValidCachedFont = false;
             if( isValidCachedDefaultFont )
             {
+              isValidCachedFont = fontClient.IsCharacterSupportedByFont( cachedDefaultFontId, character );
+            }
+
+            if( isValidCachedFont )
+            {
               // Use the cached default font for the script if there is one.
               fontId = cachedDefaultFontId;
             }
@@ -631,7 +648,10 @@ void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
               const bool preferColor = ( TextAbstraction::EMOJI == script );
 
               // Find a fallback-font.
-              fontId = fontClient.FindFallbackFont( currentFontId, character, currentPointSize, preferColor );
+              fontId = fontClient.FindFallbackFont( character,
+                                                    currentFontDescription,
+                                                    currentFontPointSize,
+                                                    preferColor );
 
               if( 0u == fontId )
               {
@@ -639,13 +659,15 @@ void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
                 defaultFontsPerScript = *( defaultFontPerScriptCacheBuffer + TextAbstraction::LATIN );
                 if( NULL != defaultFontsPerScript )
                 {
-                  fontId = defaultFontsPerScript->FindFont( fontClient, currentPointSize );
+                  fontId = defaultFontsPerScript->FindFont( fontClient,
+                                                            currentFontDescription,
+                                                            currentFontPointSize );
                 }
               }
 
               if( 0u == fontId )
               {
-                fontId = fontClient.FindDefaultFont( UTF32_A, currentPointSize );
+                fontId = fontClient.FindDefaultFont( UTF32_A, currentFontPointSize );
               }
 
               // Cache the font.
@@ -659,7 +681,7 @@ void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
                   *( defaultFontPerScriptCacheBuffer + script ) = defaultFontsPerScript;
                 }
               }
-              defaultFontsPerScript->mFonts.PushBack( fontId );
+              defaultFontsPerScript->Cache( currentFontDescription, fontId );
             }
           } // !isValidFont (3)
         } // !isValidFont (2)
@@ -679,12 +701,6 @@ void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
     }
 #endif
 
-    if( isFirstSetToBeValidated && !isCommonScript )
-    {
-      currentFontRun.fontId = fontId;
-      isFirstSetToBeValidated = false;
-    }
-
     // The font is now validated.
     if( ( fontId != currentFontRun.fontId ) ||
         isNewParagraphCharacter )
@@ -702,11 +718,6 @@ void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
       currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
       currentFontRun.characterRun.numberOfCharacters = 0u;
       currentFontRun.fontId = fontId;
-
-      if( isNewParagraphCharacter )
-      {
-        isFirstSetToBeValidated = true;
-      }
     }
 
     // Add one more character to the run.