d32809c16f8711d2c1c8528ffd70dc53fee7865c
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / multi-language-support-impl.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/multi-language-support-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/devel-api/adaptor-framework/singleton-service.h>
24 #include <dali/devel-api/text-abstraction/font-client.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/internal/text/multi-language-helper-functions.h>
28 #include <dali-toolkit/devel-api/styling/style-manager-devel.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace
37 {
38 #if defined(DEBUG_ENABLED)
39 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_MULTI_LANGUAGE_SUPPORT");
40 #endif
41
42 const Dali::Toolkit::Text::Character UTF32_A = 0x0041;
43 }
44
45 namespace Text
46 {
47
48 namespace Internal
49 {
50
51 bool ValidateFontsPerScript::IsValidFont( FontId fontId ) const
52 {
53   for( Vector<FontId>::ConstIterator it = mValidFonts.Begin(),
54          endIt = mValidFonts.End();
55        it != endIt;
56        ++it )
57   {
58     if( fontId == *it )
59     {
60       return true;
61     }
62   }
63
64   return false;
65 }
66
67 FontId DefaultFonts::FindFont( TextAbstraction::FontClient& fontClient,
68                                const TextAbstraction::FontDescription& description,
69                                PointSize26Dot6 size ) const
70 {
71   for( std::vector<CacheItem>::const_iterator it = mFonts.begin(),
72          endIt = mFonts.end();
73        it != endIt;
74        ++it )
75   {
76     const CacheItem& item = *it;
77
78     if( ( ( TextAbstraction::FontWeight::NONE == description.weight ) || ( description.weight == item.description.weight ) ) &&
79         ( ( TextAbstraction::FontWidth::NONE == description.width )   || ( description.width == item.description.width ) ) &&
80         ( ( TextAbstraction::FontSlant::NONE == description.slant )   || ( description.slant == item.description.slant ) ) &&
81         ( size == fontClient.GetPointSize( item.fontId ) ) &&
82         ( description.family.empty() || ( description.family == item.description.family ) ) )
83     {
84       return item.fontId;
85     }
86   }
87
88   return 0u;
89 }
90
91 void DefaultFonts::Cache( const TextAbstraction::FontDescription& description, FontId fontId )
92 {
93   CacheItem item;
94   item.description = description;
95   item.fontId = fontId;
96   mFonts.push_back( item );
97 }
98
99 MultilanguageSupport::MultilanguageSupport()
100 : mDefaultFontPerScriptCache(),
101   mValidFontsPerScriptCache()
102 {
103   // Initializes the default font cache to zero (invalid font).
104   // Reserves space to cache the default fonts and access them with the script as an index.
105   mDefaultFontPerScriptCache.Resize( TextAbstraction::UNKNOWN, NULL );
106
107   // Initializes the valid fonts cache to NULL (no valid fonts).
108   // Reserves space to cache the valid fonts and access them with the script as an index.
109   mValidFontsPerScriptCache.Resize( TextAbstraction::UNKNOWN, NULL );
110
111   // Add custom font directory
112   Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
113   if( styleManager )
114   {
115     std::string path;
116     Property::Map config = Toolkit::DevelStyleManager::GetConfigurations( styleManager );
117     if( config["customFontDirectory"].Get( path ) )
118     {
119       TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
120       fontClient.AddCustomFontDirectory( path.c_str() );
121     }
122   }
123
124 }
125
126 MultilanguageSupport::~MultilanguageSupport()
127 {
128   // Destroy the default font per script cache.
129   for( Vector<DefaultFonts*>::Iterator it = mDefaultFontPerScriptCache.Begin(),
130          endIt = mDefaultFontPerScriptCache.End();
131        it != endIt;
132        ++it )
133   {
134     delete *it;
135   }
136
137   // Destroy the valid fonts per script cache.
138   for( Vector<ValidateFontsPerScript*>::Iterator it = mValidFontsPerScriptCache.Begin(),
139          endIt = mValidFontsPerScriptCache.End();
140        it != endIt;
141        ++it )
142   {
143     delete *it;
144   }
145 }
146
147 Text::MultilanguageSupport MultilanguageSupport::Get()
148 {
149   Text::MultilanguageSupport multilanguageSupportHandle;
150
151   SingletonService service( SingletonService::Get() );
152   if( service )
153   {
154     // Check whether the singleton is already created
155     Dali::BaseHandle handle = service.GetSingleton( typeid( Text::MultilanguageSupport ) );
156     if( handle )
157     {
158       // If so, downcast the handle
159       MultilanguageSupport* impl = dynamic_cast< Internal::MultilanguageSupport* >( handle.GetObjectPtr() );
160       multilanguageSupportHandle = Text::MultilanguageSupport( impl );
161     }
162     else // create and register the object
163     {
164       multilanguageSupportHandle = Text::MultilanguageSupport( new MultilanguageSupport );
165       service.Register( typeid( multilanguageSupportHandle ), multilanguageSupportHandle );
166     }
167   }
168
169   return multilanguageSupportHandle;
170 }
171
172 void MultilanguageSupport::SetScripts( const Vector<Character>& text,
173                                        CharacterIndex startIndex,
174                                        Length numberOfCharacters,
175                                        Vector<ScriptRun>& scripts )
176 {
177   if( 0u == numberOfCharacters )
178   {
179     // Nothing to do if there are no characters.
180     return;
181   }
182
183   // Find the first index where to insert the script.
184   ScriptRunIndex scriptIndex = 0u;
185   if( 0u != startIndex )
186   {
187     for( Vector<ScriptRun>::ConstIterator it = scripts.Begin(),
188            endIt = scripts.End();
189          it != endIt;
190          ++it, ++scriptIndex )
191     {
192       const ScriptRun& run = *it;
193       if( startIndex < run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
194       {
195         // Run found.
196         break;
197       }
198     }
199   }
200
201   // Stores the current script run.
202   ScriptRun currentScriptRun;
203   currentScriptRun.characterRun.characterIndex = startIndex;
204   currentScriptRun.characterRun.numberOfCharacters = 0u;
205   currentScriptRun.script = TextAbstraction::UNKNOWN;
206
207   // Reserve some space to reduce the number of reallocations.
208   scripts.Reserve( text.Count() << 2u );
209
210   // Whether the first valid script needs to be set.
211   bool isFirstScriptToBeSet = true;
212
213   // Whether the first valid script is a right to left script.
214   bool isParagraphRTL = false;
215
216   // Count the number of characters which are valid for all scripts. i.e. white spaces or '\n'.
217   Length numberOfAllScriptCharacters = 0u;
218
219   // Pointers to the text buffer.
220   const Character* const textBuffer = text.Begin();
221
222   // Traverse all characters and set the scripts.
223   const Length lastCharacter = startIndex + numberOfCharacters;
224   for( Length index = startIndex; index < lastCharacter; ++index )
225   {
226     Character character = *( textBuffer + index );
227
228     // Get the script of the character.
229     Script script = TextAbstraction::GetCharacterScript( character );
230
231     // Some characters (like white spaces) are valid for many scripts. The rules to set a script
232     // for them are:
233     // - If they are at the begining of a paragraph they get the script of the first character with
234     //   a defined script. If they are at the end, they get the script of the last one.
235     // - If they are between two scripts with the same direction, they get the script of the previous
236     //   character with a defined script. If the two scripts have different directions, they get the
237     //   script of the first character of the paragraph with a defined script.
238
239     // Skip those characters valid for many scripts like white spaces or '\n'.
240     bool endOfText = index == lastCharacter;
241     while( !endOfText &&
242            ( TextAbstraction::COMMON == script ) )
243     {
244       if( TextAbstraction::EMOJI == currentScriptRun.script )
245       {
246         // Emojis doesn't mix well with characters common to all scripts. Insert the emoji run.
247         scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
248         ++scriptIndex;
249
250         // Initialize the new one.
251         currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
252         currentScriptRun.characterRun.numberOfCharacters = 0u;
253         currentScriptRun.script = TextAbstraction::UNKNOWN;
254         numberOfAllScriptCharacters = 0u;
255       }
256
257       // Count all these characters to be added into a script.
258       ++numberOfAllScriptCharacters;
259
260       if( TextAbstraction::IsNewParagraph( character ) )
261       {
262         // The character is a new paragraph.
263         // To know when there is a new paragraph is needed because if there is a white space
264         // between two scripts with different directions, it is added to the script with
265         // the same direction than the first script of the paragraph.
266         isFirstScriptToBeSet = true;
267
268         // Characters common to all scripts at the end of the paragraph are added to the last script.
269         currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
270
271         // Store the script run.
272         if( TextAbstraction::UNKNOWN == currentScriptRun.script )
273         {
274           currentScriptRun.script = TextAbstraction::LATIN;
275         }
276         scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
277         ++scriptIndex;
278
279         // Initialize the new one.
280         currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
281         currentScriptRun.characterRun.numberOfCharacters = 0u;
282         currentScriptRun.script = TextAbstraction::UNKNOWN;
283         numberOfAllScriptCharacters = 0u;
284       }
285
286       // Get the next character.
287       ++index;
288       endOfText = index == lastCharacter;
289       if( !endOfText )
290       {
291         character = *( textBuffer + index );
292         script = TextAbstraction::GetCharacterScript( character );
293       }
294     } // end while( !endOfText && ( TextAbstraction::COMMON == script ) )
295
296     if( endOfText )
297     {
298       // Last characters of the text are 'white spaces'.
299       // There is nothing else to do. Just add the remaining characters to the last script after this bucle.
300       break;
301     }
302
303     // Check if it is the first character of a paragraph.
304     if( isFirstScriptToBeSet &&
305         ( TextAbstraction::UNKNOWN != script ) &&
306         ( TextAbstraction::COMMON != script ) &&
307         ( TextAbstraction::EMOJI != script ) )
308     {
309       // Sets the direction of the first valid script.
310       isParagraphRTL = TextAbstraction::IsRightToLeftScript( script );
311       isFirstScriptToBeSet = false;
312     }
313
314     if( ( script != currentScriptRun.script ) &&
315         ( TextAbstraction::COMMON != script ) )
316     {
317       // Current run needs to be stored and a new one initialized.
318
319       if( ( isParagraphRTL == TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) ) &&
320           ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
321       {
322         // Previous script has the same direction than the first script of the paragraph.
323         // All the previously skipped characters need to be added to the previous script before it's stored.
324         currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
325         numberOfAllScriptCharacters = 0u;
326       }
327       else if( ( TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) == TextAbstraction::IsRightToLeftScript( script ) ) &&
328                ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
329       {
330         // Current script and previous one have the same direction.
331         // All the previously skipped characters need to be added to the previous script before it's stored.
332         currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
333         numberOfAllScriptCharacters = 0u;
334       }
335       else if( ( TextAbstraction::UNKNOWN == currentScriptRun.script ) &&
336                ( TextAbstraction::EMOJI == script ) )
337       {
338         currentScriptRun.script = TextAbstraction::LATIN;
339         currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
340         numberOfAllScriptCharacters = 0u;
341       }
342
343       if( 0u != currentScriptRun.characterRun.numberOfCharacters )
344       {
345         // Store the script run.
346         scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
347         ++scriptIndex;
348       }
349
350       // Initialize the new one.
351       currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
352       currentScriptRun.characterRun.numberOfCharacters = numberOfAllScriptCharacters + 1u; // Adds the white spaces which are at the begining of the script.
353       currentScriptRun.script = script;
354       numberOfAllScriptCharacters = 0u;
355     }
356     else
357     {
358       if( TextAbstraction::UNKNOWN != currentScriptRun.script )
359       {
360         // Adds white spaces between characters.
361         currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
362         numberOfAllScriptCharacters = 0u;
363       }
364
365       // Add one more character to the run.
366       ++currentScriptRun.characterRun.numberOfCharacters;
367     }
368   }
369
370   // Add remaining characters into the last script.
371   currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
372
373   if( 0u != currentScriptRun.characterRun.numberOfCharacters )
374   {
375     if( TextAbstraction::UNKNOWN == currentScriptRun.script )
376     {
377       // There are only white spaces in the last script. Set the latin script.
378       currentScriptRun.script = TextAbstraction::LATIN;
379     }
380
381     // Store the last run.
382     scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
383     ++scriptIndex;
384   }
385
386   if( scriptIndex < scripts.Count() )
387   {
388     // Update the indices of the next script runs.
389     const ScriptRun& run = *( scripts.Begin() + scriptIndex - 1u );
390     CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
391
392     for( Vector<ScriptRun>::Iterator it = scripts.Begin() + scriptIndex,
393            endIt = scripts.End();
394          it != endIt;
395          ++it )
396     {
397       ScriptRun& run = *it;
398       run.characterRun.characterIndex = nextCharacterIndex;
399       nextCharacterIndex += run.characterRun.numberOfCharacters;
400     }
401   }
402 }
403
404 void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
405                                           const Vector<ScriptRun>& scripts,
406                                           const Vector<FontDescriptionRun>& fontDescriptions,
407                                           const TextAbstraction::FontDescription& defaultFontDescription,
408                                           TextAbstraction::PointSize26Dot6 defaultFontPointSize,
409                                           CharacterIndex startIndex,
410                                           Length numberOfCharacters,
411                                           Vector<FontRun>& fonts )
412 {
413   DALI_LOG_INFO( gLogFilter, Debug::General, "-->MultilanguageSupport::ValidateFonts\n" );
414
415   if( 0u == numberOfCharacters )
416   {
417     DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
418     // Nothing to do if there are no characters.
419     return;
420   }
421
422   // Find the first index where to insert the font run.
423   FontRunIndex fontIndex = 0u;
424   if( 0u != startIndex )
425   {
426     for( Vector<FontRun>::ConstIterator it = fonts.Begin(),
427            endIt = fonts.End();
428          it != endIt;
429          ++it, ++fontIndex )
430     {
431       const FontRun& run = *it;
432       if( startIndex < run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
433       {
434         // Run found.
435         break;
436       }
437     }
438   }
439
440   // Traverse the characters and validate/set the fonts.
441
442   // Get the caches.
443   DefaultFonts** defaultFontPerScriptCacheBuffer = mDefaultFontPerScriptCache.Begin();
444   ValidateFontsPerScript** validFontsPerScriptCacheBuffer = mValidFontsPerScriptCache.Begin();
445
446   // Stores the validated font runs.
447   fonts.Reserve( fontDescriptions.Count() );
448
449   // Initializes a validated font run.
450   FontRun currentFontRun;
451   currentFontRun.characterRun.characterIndex = startIndex;
452   currentFontRun.characterRun.numberOfCharacters = 0u;
453   currentFontRun.fontId = 0u;
454
455   // Get the font client.
456   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
457
458   const Character* const textBuffer = text.Begin();
459
460   // Iterators of the script runs.
461   Vector<ScriptRun>::ConstIterator scriptRunIt = scripts.Begin();
462   Vector<ScriptRun>::ConstIterator scriptRunEndIt = scripts.End();
463   bool isNewParagraphCharacter = false;
464
465   FontId currentFontId = 0u;
466   FontId previousFontId = 0u;
467   bool isPreviousEmojiScript = false;
468
469   // Whether it's the first set of characters to be validated.
470   // Used in case the paragraph starts with characters common to all scripts.
471   bool isFirstSetToBeValidated = true;
472
473   CharacterIndex lastCharacter = startIndex + numberOfCharacters;
474   for( Length index = startIndex; index < lastCharacter; ++index )
475   {
476     // Get the current character.
477     const Character character = *( textBuffer + index );
478
479     TextAbstraction::FontDescription currentFontDescription;
480     TextAbstraction::PointSize26Dot6 currentFontPointSize = defaultFontPointSize;
481     bool isDefaultFont = true;
482     MergeFontDescriptions( fontDescriptions,
483                            defaultFontDescription,
484                            defaultFontPointSize,
485                            index,
486                            currentFontDescription,
487                            currentFontPointSize,
488                            isDefaultFont );
489
490     // Get the font for the current character.
491     FontId fontId = fontClient.GetFontId( currentFontDescription, currentFontPointSize );
492     currentFontId = fontId;
493
494     // Get the script for the current character.
495     Script script = GetScript( index,
496                                scriptRunIt,
497                                scriptRunEndIt );
498
499 #ifdef DEBUG_ENABLED
500     {
501       Dali::TextAbstraction::FontDescription description;
502       fontClient.GetDescription( fontId, description );
503
504       DALI_LOG_INFO( gLogFilter,
505                      Debug::Verbose,
506                      "  Initial font set\n  Character : %x, Script : %s, Font : %s \n",
507                      character,
508                      Dali::TextAbstraction::ScriptName[script],
509                      description.path.c_str() );
510     }
511 #endif
512     if( script == TextAbstraction::UNKNOWN )
513     {
514       script = TextAbstraction::LATIN;
515     }
516
517     // Validate whether the current character is supported by the given font.
518     bool isValidFont = false;
519
520     // Check first in the cache of default fonts per script and size.
521
522     FontId cachedDefaultFontId = 0u;
523     DefaultFonts* defaultFonts = *( defaultFontPerScriptCacheBuffer + script );
524     if( NULL != defaultFonts )
525     {
526       // This cache stores fall-back fonts.
527       cachedDefaultFontId = defaultFonts->FindFont( fontClient,
528                                                     currentFontDescription,
529                                                     currentFontPointSize );
530     }
531
532     // Whether the cached default font is valid.
533     const bool isValidCachedDefaultFont = 0u != cachedDefaultFontId;
534
535     // The font is valid if it matches with the default one for the current script and size and it's different than zero.
536     isValidFont = isValidCachedDefaultFont && ( fontId == cachedDefaultFontId );
537
538     if( isValidFont )
539     {
540       // Check if the font supports the character.
541       isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
542     }
543
544     bool isCommonScript = false;
545     bool isEmojiScript = TextAbstraction::EMOJI == script;
546
547     if( isEmojiScript && !isPreviousEmojiScript )
548     {
549       if( 0u != currentFontRun.characterRun.numberOfCharacters )
550       {
551         // Store the font run.
552         fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
553         ++fontIndex;
554       }
555
556       // Initialize the new one.
557       currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
558       currentFontRun.characterRun.numberOfCharacters = 0u;
559       currentFontRun.fontId = fontId;
560     }
561
562     // If the given font is not valid, it means either:
563     // - there is no cached font for the current script yet or,
564     // - the user has set a different font than the default one for the current script or,
565     // - the platform default font is different than the default font for the current script.
566
567     // Need to check if the given font supports the current character.
568     if( !isValidFont ) // (1)
569     {
570       // Whether the current character is common for all scripts (i.e. white spaces, ...)
571
572       // Is not desirable to cache fonts for the common script.
573       //
574       // i.e. Consider the text " à¤¹à¤¿à¤‚दी", the 'white space' has assigned the DEVANAGARI script.
575       //      The user may have set a font or the platform's default is used.
576       //
577       //      As the 'white space' is the first character, no font is cached so the font validation
578       //      retrieves a glyph from the given font.
579       //
580       //      Many fonts support 'white spaces' so probably the font set by the user or the platform's default
581       //      supports the 'white space'. However, that font may not support the DEVANAGARI script.
582       isCommonScript = TextAbstraction::IsCommonScript( character );
583
584       if( isCommonScript )
585       {
586         if( isValidCachedDefaultFont &&
587             ( isDefaultFont || ( currentFontId == previousFontId ) ) &&
588             !isEmojiScript )
589         {
590           // At this point the character common for all scripts has no font assigned.
591           // If there is a valid previously cached default font for it, use that one.
592           fontId = cachedDefaultFontId;
593         }
594       }
595       else
596       {
597         // Check in the valid fonts cache.
598         ValidateFontsPerScript* validateFontsPerScript = *( validFontsPerScriptCacheBuffer + script );
599
600         if( NULL != validateFontsPerScript )
601         {
602           // This cache stores valid fonts set by the user.
603           isValidFont = validateFontsPerScript->IsValidFont( fontId );
604
605           // It may happen that a validated font for a script doesn't have all the glyphs for that script.
606           // i.e a font validated for the CJK script may contain glyphs for the chinese language but not for the Japanese.
607           if( isValidFont )
608           {
609             // Checks if the current character is supported by the font is needed.
610             isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
611           }
612         }
613
614         if( !isValidFont ) // (2)
615         {
616           // The selected font is not stored in any cache.
617
618           // Checks if the current character is supported by the selected font.
619           isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
620
621           // Emojis are present in many monochrome fonts; prefer color by default.
622           if( isValidFont &&
623               isEmojiScript )
624           {
625             const GlyphIndex glyphIndex = fontClient.GetGlyphIndex( fontId, character );
626
627             // For color emojis, the font is valid if the glyph is a color glyph (the bitmap is RGBA).
628             isValidFont = fontClient.IsColorGlyph( fontId, glyphIndex );
629           }
630
631           // If there is a valid font, cache it.
632           if( isValidFont )
633           {
634             if( NULL == validateFontsPerScript )
635             {
636               validateFontsPerScript = new ValidateFontsPerScript();
637
638               *( validFontsPerScriptCacheBuffer + script ) = validateFontsPerScript;
639             }
640
641             validateFontsPerScript->mValidFonts.PushBack( fontId );
642           }
643
644           if( !isValidFont && ( fontId != cachedDefaultFontId ) ) // (3)
645           {
646             // The selected font by the user or the platform's default font has failed to validate the character.
647
648             // Checks if the previously discarted cached default font supports the character.
649             bool isValidCachedFont = false;
650             if( isValidCachedDefaultFont )
651             {
652               isValidCachedFont = fontClient.IsCharacterSupportedByFont( cachedDefaultFontId, character );
653             }
654
655             if( isValidCachedFont )
656             {
657               // Use the cached default font for the script if there is one.
658               fontId = cachedDefaultFontId;
659             }
660             else
661             {
662               // There is no valid cached default font for the script.
663
664               DefaultFonts* defaultFontsPerScript = NULL;
665
666               // Emojis are present in many monochrome fonts; prefer color by default.
667               const bool preferColor = ( TextAbstraction::EMOJI == script );
668
669               // Find a fallback-font.
670               fontId = fontClient.FindFallbackFont( character,
671                                                     currentFontDescription,
672                                                     currentFontPointSize,
673                                                     preferColor );
674
675               if( 0u == fontId )
676               {
677                 // If the system does not support a suitable font, fallback to Latin
678                 defaultFontsPerScript = *( defaultFontPerScriptCacheBuffer + TextAbstraction::LATIN );
679                 if( NULL != defaultFontsPerScript )
680                 {
681                   fontId = defaultFontsPerScript->FindFont( fontClient,
682                                                             currentFontDescription,
683                                                             currentFontPointSize );
684                 }
685               }
686
687               if( 0u == fontId )
688               {
689                 fontId = fontClient.FindDefaultFont( UTF32_A, currentFontPointSize );
690               }
691
692               // Cache the font.
693               if( NULL == defaultFontsPerScript )
694               {
695                 defaultFontsPerScript = *( defaultFontPerScriptCacheBuffer + script );
696
697                 if( NULL == defaultFontsPerScript )
698                 {
699                   defaultFontsPerScript = new DefaultFonts();
700                   *( defaultFontPerScriptCacheBuffer + script ) = defaultFontsPerScript;
701                 }
702               }
703               defaultFontsPerScript->Cache( currentFontDescription, fontId );
704             }
705           } // !isValidFont (3)
706         } // !isValidFont (2)
707       } // !isCommonScript
708     } // !isValidFont (1)
709
710 #ifdef DEBUG_ENABLED
711     {
712       Dali::TextAbstraction::FontDescription description;
713       fontClient.GetDescription( fontId, description );
714       DALI_LOG_INFO( gLogFilter,
715                      Debug::Verbose,
716                      "  Validated font set\n  Character : %x, Script : %s, Font : %s \n",
717                      character,
718                      Dali::TextAbstraction::ScriptName[script],
719                      description.path.c_str() );
720     }
721 #endif
722
723     if( isFirstSetToBeValidated && !isCommonScript )
724     {
725       currentFontRun.fontId = fontId;
726       isFirstSetToBeValidated = false;
727     }
728
729     // The font is now validated.
730     if( ( fontId != currentFontRun.fontId ) ||
731         isNewParagraphCharacter )
732     {
733       // Current run needs to be stored and a new one initialized.
734
735       if( 0u != currentFontRun.characterRun.numberOfCharacters )
736       {
737         // Store the font run.
738         fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
739         ++fontIndex;
740       }
741
742       // Initialize the new one.
743       currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
744       currentFontRun.characterRun.numberOfCharacters = 0u;
745       currentFontRun.fontId = fontId;
746
747       if( isNewParagraphCharacter )
748       {
749         isFirstSetToBeValidated = true;
750       }
751     }
752
753     // Add one more character to the run.
754     ++currentFontRun.characterRun.numberOfCharacters;
755
756     // Whether the current character is a new paragraph character.
757     isNewParagraphCharacter = TextAbstraction::IsNewParagraph( character );
758     previousFontId = currentFontId;
759     isPreviousEmojiScript = isEmojiScript;
760   } // end traverse characters.
761
762   if( 0u != currentFontRun.characterRun.numberOfCharacters )
763   {
764     // Store the last run.
765     fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
766     ++fontIndex;
767   }
768
769   if( fontIndex < fonts.Count() )
770   {
771     // Update the indices of the next font runs.
772     const FontRun& run = *( fonts.Begin() + fontIndex - 1u );
773     CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
774
775     for( Vector<FontRun>::Iterator it = fonts.Begin() + fontIndex,
776            endIt = fonts.End();
777          it != endIt;
778          ++it )
779     {
780       FontRun& run = *it;
781
782       run.characterRun.characterIndex = nextCharacterIndex;
783       nextCharacterIndex += run.characterRun.numberOfCharacters;
784     }
785   }
786
787   DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
788 }
789
790 } // namespace Internal
791
792 } // namespace Text
793
794 } // namespace Toolkit
795
796 } // namespace Dali