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