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