723a3439cca94221f993b759862e982d22ffebad
[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 + 1, 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 + 1, 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         scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
273         ++scriptIndex;
274
275         // Initialize the new one.
276         currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
277         currentScriptRun.characterRun.numberOfCharacters = 0u;
278         currentScriptRun.script = TextAbstraction::UNKNOWN;
279         numberOfAllScriptCharacters = 0u;
280       }
281
282       // Get the next character.
283       ++index;
284       endOfText = index == lastCharacter;
285       if( !endOfText )
286       {
287         character = *( textBuffer + index );
288         script = TextAbstraction::GetCharacterScript( character );
289       }
290     } // end while( !endOfText && ( TextAbstraction::COMMON == script ) )
291
292     if( endOfText )
293     {
294       // Last characters of the text are 'white spaces'.
295       // There is nothing else to do. Just add the remaining characters to the last script after this bucle.
296       break;
297     }
298
299     // Check if it is the first character of a paragraph.
300     if( isFirstScriptToBeSet &&
301         ( TextAbstraction::UNKNOWN != script ) &&
302         ( TextAbstraction::COMMON != script ) &&
303         ( TextAbstraction::EMOJI != script ) )
304     {
305       // Sets the direction of the first valid script.
306       isParagraphRTL = TextAbstraction::IsRightToLeftScript( script );
307       isFirstScriptToBeSet = false;
308     }
309
310     if( ( script != currentScriptRun.script ) &&
311         ( TextAbstraction::COMMON != script ) )
312     {
313       // Current run needs to be stored and a new one initialized.
314
315       if( ( isParagraphRTL == TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) ) &&
316           ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
317       {
318         // Previous script has the same direction than the first script of the paragraph.
319         // All the previously skipped characters need to be added to the previous script before it's stored.
320         currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
321         numberOfAllScriptCharacters = 0u;
322       }
323       else if( ( TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) == TextAbstraction::IsRightToLeftScript( script ) ) &&
324                ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
325       {
326         // Current script and previous one have the same direction.
327         // All the previously skipped characters need to be added to the previous script before it's stored.
328         currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
329         numberOfAllScriptCharacters = 0u;
330       }
331       else if( ( TextAbstraction::UNKNOWN == currentScriptRun.script ) &&
332                ( TextAbstraction::EMOJI == script ) )
333       {
334         currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
335         numberOfAllScriptCharacters = 0u;
336       }
337
338       if( 0u != currentScriptRun.characterRun.numberOfCharacters )
339       {
340         // Store the script run.
341         scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
342         ++scriptIndex;
343       }
344
345       // Initialize the new one.
346       currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
347       currentScriptRun.characterRun.numberOfCharacters = numberOfAllScriptCharacters + 1u; // Adds the white spaces which are at the begining of the script.
348       currentScriptRun.script = script;
349       numberOfAllScriptCharacters = 0u;
350     }
351     else
352     {
353       if( TextAbstraction::UNKNOWN != currentScriptRun.script )
354       {
355         // Adds white spaces between characters.
356         currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
357         numberOfAllScriptCharacters = 0u;
358       }
359
360       // Add one more character to the run.
361       ++currentScriptRun.characterRun.numberOfCharacters;
362     }
363   }
364
365   // Add remaining characters into the last script.
366   currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
367
368   if( 0u != currentScriptRun.characterRun.numberOfCharacters )
369   {
370     // Store the last run.
371     scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
372     ++scriptIndex;
373   }
374
375   if( scriptIndex < scripts.Count() )
376   {
377     // Update the indices of the next script runs.
378     const ScriptRun& run = *( scripts.Begin() + scriptIndex - 1u );
379     CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
380
381     for( Vector<ScriptRun>::Iterator it = scripts.Begin() + scriptIndex,
382            endIt = scripts.End();
383          it != endIt;
384          ++it )
385     {
386       ScriptRun& run = *it;
387       run.characterRun.characterIndex = nextCharacterIndex;
388       nextCharacterIndex += run.characterRun.numberOfCharacters;
389     }
390   }
391 }
392
393 void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
394                                           const Vector<ScriptRun>& scripts,
395                                           const Vector<FontDescriptionRun>& fontDescriptions,
396                                           const TextAbstraction::FontDescription& defaultFontDescription,
397                                           TextAbstraction::PointSize26Dot6 defaultFontPointSize,
398                                           CharacterIndex startIndex,
399                                           Length numberOfCharacters,
400                                           Vector<FontRun>& fonts )
401 {
402   DALI_LOG_INFO( gLogFilter, Debug::General, "-->MultilanguageSupport::ValidateFonts\n" );
403
404   if( 0u == numberOfCharacters )
405   {
406     DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
407     // Nothing to do if there are no characters.
408     return;
409   }
410
411   // Find the first index where to insert the font run.
412   FontRunIndex fontIndex = 0u;
413   if( 0u != startIndex )
414   {
415     for( Vector<FontRun>::ConstIterator it = fonts.Begin(),
416            endIt = fonts.End();
417          it != endIt;
418          ++it, ++fontIndex )
419     {
420       const FontRun& run = *it;
421       if( startIndex < run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
422       {
423         // Run found.
424         break;
425       }
426     }
427   }
428
429   // Traverse the characters and validate/set the fonts.
430
431   // Get the caches.
432   DefaultFonts** defaultFontPerScriptCacheBuffer = mDefaultFontPerScriptCache.Begin();
433   ValidateFontsPerScript** validFontsPerScriptCacheBuffer = mValidFontsPerScriptCache.Begin();
434
435   // Stores the validated font runs.
436   fonts.Reserve( fontDescriptions.Count() );
437
438   // Initializes a validated font run.
439   FontRun currentFontRun;
440   currentFontRun.characterRun.characterIndex = startIndex;
441   currentFontRun.characterRun.numberOfCharacters = 0u;
442   currentFontRun.fontId = 0u;
443
444   // Get the font client.
445   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
446
447   const Character* const textBuffer = text.Begin();
448
449   // Iterators of the script runs.
450   Vector<ScriptRun>::ConstIterator scriptRunIt = scripts.Begin();
451   Vector<ScriptRun>::ConstIterator scriptRunEndIt = scripts.End();
452   bool isNewParagraphCharacter = false;
453
454   FontId currentFontId = 0u;
455   FontId previousFontId = 0u;
456   bool isPreviousEmojiScript = false;
457
458   CharacterIndex lastCharacter = startIndex + numberOfCharacters;
459   for( Length index = startIndex; index < lastCharacter; ++index )
460   {
461     // Get the current character.
462     const Character character = *( textBuffer + index );
463
464     TextAbstraction::FontDescription currentFontDescription;
465     TextAbstraction::PointSize26Dot6 currentFontPointSize = defaultFontPointSize;
466     bool isDefaultFont = true;
467     MergeFontDescriptions( fontDescriptions,
468                            defaultFontDescription,
469                            defaultFontPointSize,
470                            index,
471                            currentFontDescription,
472                            currentFontPointSize,
473                            isDefaultFont );
474
475     // Get the font for the current character.
476     FontId fontId = fontClient.GetFontId( currentFontDescription, currentFontPointSize );
477     currentFontId = fontId;
478
479     // Get the script for the current character.
480     Script script = GetScript( index,
481                                scriptRunIt,
482                                scriptRunEndIt );
483
484 #ifdef DEBUG_ENABLED
485     {
486       Dali::TextAbstraction::FontDescription description;
487       fontClient.GetDescription( fontId, description );
488
489       DALI_LOG_INFO( gLogFilter,
490                      Debug::Verbose,
491                      "  Initial font set\n  Character : %x, Script : %s, Font : %s \n",
492                      character,
493                      Dali::TextAbstraction::ScriptName[script],
494                      description.path.c_str() );
495     }
496 #endif
497
498     // Validate whether the current character is supported by the given font.
499     bool isValidFont = false;
500
501     // Check first in the cache of default fonts per script and size.
502
503     FontId cachedDefaultFontId = 0u;
504     DefaultFonts* defaultFonts = *( defaultFontPerScriptCacheBuffer + script );
505     if( NULL != defaultFonts )
506     {
507       // This cache stores fall-back fonts.
508       cachedDefaultFontId = defaultFonts->FindFont( fontClient,
509                                                     currentFontDescription,
510                                                     currentFontPointSize );
511     }
512
513     // Whether the cached default font is valid.
514     const bool isValidCachedDefaultFont = 0u != cachedDefaultFontId;
515
516     // The font is valid if it matches with the default one for the current script and size and it's different than zero.
517     isValidFont = isValidCachedDefaultFont && ( fontId == cachedDefaultFontId );
518
519     if( isValidFont )
520     {
521       // Check if the font supports the character.
522       isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
523     }
524
525     bool isCommonScript = false;
526     bool isEmojiScript = TextAbstraction::EMOJI == script;
527
528     if( isEmojiScript && !isPreviousEmojiScript )
529     {
530       if( 0u != currentFontRun.characterRun.numberOfCharacters )
531       {
532         // Store the font run.
533         fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
534         ++fontIndex;
535       }
536
537       // Initialize the new one.
538       currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
539       currentFontRun.characterRun.numberOfCharacters = 0u;
540       currentFontRun.fontId = fontId;
541     }
542
543     // If the given font is not valid, it means either:
544     // - there is no cached font for the current script yet or,
545     // - the user has set a different font than the default one for the current script or,
546     // - the platform default font is different than the default font for the current script.
547
548     // Need to check if the given font supports the current character.
549     if( !isValidFont ) // (1)
550     {
551       // Whether the current character is common for all scripts (i.e. white spaces, ...)
552
553       // Is not desirable to cache fonts for the common script.
554       //
555       // i.e. Consider the text " à¤¹à¤¿à¤‚दी", the 'white space' has assigned the DEVANAGARI script.
556       //      The user may have set a font or the platform's default is used.
557       //
558       //      As the 'white space' is the first character, no font is cached so the font validation
559       //      retrieves a glyph from the given font.
560       //
561       //      Many fonts support 'white spaces' so probably the font set by the user or the platform's default
562       //      supports the 'white space'. However, that font may not support the DEVANAGARI script.
563       isCommonScript = TextAbstraction::IsCommonScript( character );
564
565       if( isCommonScript )
566       {
567         if( isValidCachedDefaultFont &&
568             ( isDefaultFont || ( currentFontId == previousFontId ) ) &&
569             !isEmojiScript )
570         {
571           // At this point the character common for all scripts has no font assigned.
572           // If there is a valid previously cached default font for it, use that one.
573           fontId = cachedDefaultFontId;
574         }
575       }
576       else
577       {
578         // Check in the valid fonts cache.
579         ValidateFontsPerScript* validateFontsPerScript = *( validFontsPerScriptCacheBuffer + script );
580
581         if( NULL != validateFontsPerScript )
582         {
583           // This cache stores valid fonts set by the user.
584           isValidFont = validateFontsPerScript->IsValidFont( fontId );
585
586           // It may happen that a validated font for a script doesn't have all the glyphs for that script.
587           // i.e a font validated for the CJK script may contain glyphs for the chinese language but not for the Japanese.
588           if( isValidFont )
589           {
590             // Checks if the current character is supported by the font is needed.
591             isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
592           }
593         }
594
595         if( !isValidFont ) // (2)
596         {
597           // The selected font is not stored in any cache.
598
599           // Checks if the current character is supported by the selected font.
600           isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
601
602           // Emojis are present in many monochrome fonts; prefer color by default.
603           if( isValidFont &&
604               isEmojiScript )
605           {
606             const GlyphIndex glyphIndex = fontClient.GetGlyphIndex( fontId, character );
607
608             // For color emojis, the font is valid if the glyph is a color glyph (the bitmap is RGBA).
609             isValidFont = fontClient.IsColorGlyph( fontId, glyphIndex );
610           }
611
612           // If there is a valid font, cache it.
613           if( isValidFont )
614           {
615             if( NULL == validateFontsPerScript )
616             {
617               validateFontsPerScript = new ValidateFontsPerScript();
618
619               *( validFontsPerScriptCacheBuffer + script ) = validateFontsPerScript;
620             }
621
622             validateFontsPerScript->mValidFonts.PushBack( fontId );
623           }
624
625           if( !isValidFont && ( fontId != cachedDefaultFontId ) ) // (3)
626           {
627             // The selected font by the user or the platform's default font has failed to validate the character.
628
629             // Checks if the previously discarted cached default font supports the character.
630             bool isValidCachedFont = false;
631             if( isValidCachedDefaultFont )
632             {
633               isValidCachedFont = fontClient.IsCharacterSupportedByFont( cachedDefaultFontId, character );
634             }
635
636             if( isValidCachedFont )
637             {
638               // Use the cached default font for the script if there is one.
639               fontId = cachedDefaultFontId;
640             }
641             else
642             {
643               // There is no valid cached default font for the script.
644
645               DefaultFonts* defaultFontsPerScript = NULL;
646
647               // Emojis are present in many monochrome fonts; prefer color by default.
648               const bool preferColor = ( TextAbstraction::EMOJI == script );
649
650               // Find a fallback-font.
651               fontId = fontClient.FindFallbackFont( character,
652                                                     currentFontDescription,
653                                                     currentFontPointSize,
654                                                     preferColor );
655
656               if( 0u == fontId )
657               {
658                 // If the system does not support a suitable font, fallback to Latin
659                 defaultFontsPerScript = *( defaultFontPerScriptCacheBuffer + TextAbstraction::LATIN );
660                 if( NULL != defaultFontsPerScript )
661                 {
662                   fontId = defaultFontsPerScript->FindFont( fontClient,
663                                                             currentFontDescription,
664                                                             currentFontPointSize );
665                 }
666               }
667
668               if( 0u == fontId )
669               {
670                 fontId = fontClient.FindDefaultFont( UTF32_A, currentFontPointSize );
671               }
672
673               if ( script != TextAbstraction::UNKNOWN )
674               {
675                 // Cache the font if it is not an unknown script
676                 if( NULL == defaultFontsPerScript )
677                 {
678                   defaultFontsPerScript = *( defaultFontPerScriptCacheBuffer + script );
679
680                   if( NULL == defaultFontsPerScript )
681                   {
682                     defaultFontsPerScript = new DefaultFonts();
683                     *( defaultFontPerScriptCacheBuffer + script ) = defaultFontsPerScript;
684                   }
685                 }
686                 defaultFontsPerScript->Cache( currentFontDescription, fontId );
687               }
688             }
689           } // !isValidFont (3)
690         } // !isValidFont (2)
691       } // !isCommonScript
692     } // !isValidFont (1)
693
694 #ifdef DEBUG_ENABLED
695     {
696       Dali::TextAbstraction::FontDescription description;
697       fontClient.GetDescription( fontId, description );
698       DALI_LOG_INFO( gLogFilter,
699                      Debug::Verbose,
700                      "  Validated font set\n  Character : %x, Script : %s, Font : %s \n",
701                      character,
702                      Dali::TextAbstraction::ScriptName[script],
703                      description.path.c_str() );
704     }
705 #endif
706
707     // The font is now validated.
708     if( ( fontId != currentFontRun.fontId ) ||
709         isNewParagraphCharacter )
710     {
711       // Current run needs to be stored and a new one initialized.
712
713       if( 0u != currentFontRun.characterRun.numberOfCharacters )
714       {
715         // Store the font run.
716         fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
717         ++fontIndex;
718       }
719
720       // Initialize the new one.
721       currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
722       currentFontRun.characterRun.numberOfCharacters = 0u;
723       currentFontRun.fontId = fontId;
724     }
725
726     // Add one more character to the run.
727     ++currentFontRun.characterRun.numberOfCharacters;
728
729     // Whether the current character is a new paragraph character.
730     isNewParagraphCharacter = TextAbstraction::IsNewParagraph( character );
731     previousFontId = currentFontId;
732     isPreviousEmojiScript = isEmojiScript;
733   } // end traverse characters.
734
735   if( 0u != currentFontRun.characterRun.numberOfCharacters )
736   {
737     // Store the last run.
738     fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
739     ++fontIndex;
740   }
741
742   if( fontIndex < fonts.Count() )
743   {
744     // Update the indices of the next font runs.
745     const FontRun& run = *( fonts.Begin() + fontIndex - 1u );
746     CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
747
748     for( Vector<FontRun>::Iterator it = fonts.Begin() + fontIndex,
749            endIt = fonts.End();
750          it != endIt;
751          ++it )
752     {
753       FontRun& run = *it;
754
755       run.characterRun.characterIndex = nextCharacterIndex;
756       nextCharacterIndex += run.characterRun.numberOfCharacters;
757     }
758   }
759
760   DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
761 }
762
763 } // namespace Internal
764
765 } // namespace Text
766
767 } // namespace Toolkit
768
769 } // namespace Dali