[dali_1.3.47] Merge branch '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   bool isPreviousEmojiScript = false;
440
441   // Description of fallback font which is selected at current iteration.
442   TextAbstraction::FontDescription selectedFontDescription;
443
444   CharacterIndex lastCharacter = startIndex + numberOfCharacters;
445   for( Length index = startIndex; index < lastCharacter; ++index )
446   {
447     // Get the current character.
448     const Character character = *( textBuffer + index );
449     bool needSoftwareBoldening = false;
450     bool needSoftwareItalic = false;
451
452     // new description for current character
453     TextAbstraction::FontDescription currentFontDescription;
454     TextAbstraction::PointSize26Dot6 currentFontPointSize = defaultFontPointSize;
455     bool isDefaultFont = true;
456     MergeFontDescriptions( fontDescriptions,
457                            defaultFontDescription,
458                            defaultFontPointSize,
459                            index,
460                            currentFontDescription,
461                            currentFontPointSize,
462                            isDefaultFont );
463
464     // Get the font for the current character.
465     FontId fontId = fontClient.GetFontId( currentFontDescription, currentFontPointSize );
466
467     // Get the script for the current character.
468     Script script = GetScript( index,
469                                scriptRunIt,
470                                scriptRunEndIt );
471
472 #ifdef DEBUG_ENABLED
473     {
474       Dali::TextAbstraction::FontDescription description;
475       fontClient.GetDescription( fontId, description );
476
477       DALI_LOG_INFO( gLogFilter,
478                      Debug::Verbose,
479                      "  Initial font set\n  Character : %x, Script : %s, Font : %s \n",
480                      character,
481                      Dali::TextAbstraction::ScriptName[script],
482                      description.path.c_str() );
483     }
484 #endif
485
486     // Validate whether the current character is supported by the given font.
487     bool isValidFont = false;
488
489     // Check first in the cache of default fonts per script and size.
490
491     FontId cachedDefaultFontId = 0u;
492     DefaultFonts* defaultFonts = *( defaultFontPerScriptCacheBuffer + script );
493     if( NULL != defaultFonts )
494     {
495       // This cache stores fall-back fonts.
496       cachedDefaultFontId = defaultFonts->FindFont( fontClient,
497                                                     currentFontDescription,
498                                                     currentFontPointSize );
499     }
500
501     // Whether the cached default font is valid.
502     const bool isValidCachedDefaultFont = 0u != cachedDefaultFontId;
503
504     // The font is valid if it matches with the default one for the current script and size and it's different than zero.
505     isValidFont = isValidCachedDefaultFont && ( fontId == cachedDefaultFontId );
506
507     if( isValidFont )
508     {
509       // Check if the font supports the character.
510       isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
511     }
512
513     bool isCommonScript = false;
514     bool isEmojiScript = TextAbstraction::EMOJI == script;
515
516     if( isEmojiScript && !isPreviousEmojiScript )
517     {
518       if( 0u != currentFontRun.characterRun.numberOfCharacters )
519       {
520         // Store the font run.
521         fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
522         ++fontIndex;
523       }
524
525       // Initialize the new one.
526       currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
527       currentFontRun.characterRun.numberOfCharacters = 0u;
528       currentFontRun.fontId = fontId;
529     }
530
531     // If the given font is not valid, it means either:
532     // - there is no cached font for the current script yet or,
533     // - the user has set a different font than the default one for the current script or,
534     // - the platform default font is different than the default font for the current script.
535
536     // Need to check if the given font supports the current character.
537     if( !isValidFont ) // (1)
538     {
539       // Whether the current character is common for all scripts (i.e. white spaces, ...)
540
541       // Is not desirable to cache fonts for the common script.
542       //
543       // i.e. Consider the text " à¤¹à¤¿à¤‚दी", the 'white space' has assigned the DEVANAGARI script.
544       //      The user may have set a font or the platform's default is used.
545       //
546       //      As the 'white space' is the first character, no font is cached so the font validation
547       //      retrieves a glyph from the given font.
548       //
549       //      Many fonts support 'white spaces' so probably the font set by the user or the platform's default
550       //      supports the 'white space'. However, that font may not support the DEVANAGARI script.
551       isCommonScript = TextAbstraction::IsCommonScript( character );
552
553       // Check in the valid fonts cache.
554       ValidateFontsPerScript* validateFontsPerScript = *( validFontsPerScriptCacheBuffer + script );
555
556       if( NULL != validateFontsPerScript )
557       {
558         // This cache stores valid fonts set by the user.
559         isValidFont = validateFontsPerScript->IsValidFont( fontId );
560
561         // It may happen that a validated font for a script doesn't have all the glyphs for that script.
562         // i.e a font validated for the CJK script may contain glyphs for the chinese language but not for the Japanese.
563         if( isValidFont )
564         {
565           // Checks if the current character is supported by the font is needed.
566           isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
567         }
568       }
569
570       if( !isValidFont ) // (2)
571       {
572         // The selected font is not stored in any cache.
573
574         // Checks if the current character is supported by the selected font.
575         isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
576
577         // Emojis are present in many monochrome fonts; prefer color by default.
578         if( isValidFont &&
579             isEmojiScript )
580         {
581           const GlyphIndex glyphIndex = fontClient.GetGlyphIndex( fontId, character );
582
583           // For color emojis, the font is valid if the glyph is a color glyph (the bitmap is RGBA).
584           isValidFont = fontClient.IsColorGlyph( fontId, glyphIndex );
585         }
586
587         // If there is a valid font, cache it.
588         if( isValidFont && !isCommonScript )
589         {
590           if( NULL == validateFontsPerScript )
591           {
592             validateFontsPerScript = new ValidateFontsPerScript();
593
594             *( validFontsPerScriptCacheBuffer + script ) = validateFontsPerScript;
595           }
596
597           validateFontsPerScript->mValidFonts.PushBack( fontId );
598         }
599
600         if( !isValidFont && ( fontId != cachedDefaultFontId ) && ( !TextAbstraction::IsNewParagraph( character ) )) // (3)
601         {
602           // The selected font by the user or the platform's default font has failed to validate the character.
603
604           // Checks if the previously discarted cached default font supports the character.
605           bool isValidCachedFont = false;
606           if( isValidCachedDefaultFont )
607           {
608             isValidCachedFont = fontClient.IsCharacterSupportedByFont( cachedDefaultFontId, character );
609           }
610
611           if( isValidCachedFont )
612           {
613             // Use the cached default font for the script if there is one.
614             fontId = cachedDefaultFontId;
615           }
616           else
617           {
618             // There is no valid cached default font for the script.
619
620             DefaultFonts* defaultFontsPerScript = NULL;
621
622             // Emojis are present in many monochrome fonts; prefer color by default.
623             const bool preferColor = ( TextAbstraction::EMOJI == script );
624
625             // Find a fallback-font.
626             fontId = fontClient.FindFallbackFont( character,
627                                                   currentFontDescription,
628                                                   currentFontPointSize,
629                                                   preferColor );
630
631             if( 0u == fontId )
632             {
633               fontId = fontClient.FindDefaultFont( UTF32_A, currentFontPointSize );
634             }
635
636             if ( !isCommonScript && (script != TextAbstraction::UNKNOWN) )
637             {
638               // Cache the font if it is not an unknown script
639               if( NULL == defaultFontsPerScript )
640               {
641                 defaultFontsPerScript = *( defaultFontPerScriptCacheBuffer + script );
642
643                 if( NULL == defaultFontsPerScript )
644                 {
645                   defaultFontsPerScript = new DefaultFonts();
646                   *( defaultFontPerScriptCacheBuffer + script ) = defaultFontsPerScript;
647                 }
648               }
649               defaultFontsPerScript->Cache( currentFontDescription, fontId );
650             }
651           }
652         } // !isValidFont (3)
653       } // !isValidFont (2)
654     } // !isValidFont (1)
655
656 #ifdef DEBUG_ENABLED
657     {
658       Dali::TextAbstraction::FontDescription description;
659       fontClient.GetDescription( fontId, description );
660       DALI_LOG_INFO( gLogFilter,
661                      Debug::Verbose,
662                      "  Validated font set\n  Character : %x, Script : %s, Font : %s \n",
663                      character,
664                      Dali::TextAbstraction::ScriptName[script],
665                      description.path.c_str() );
666     }
667 #endif
668
669     if( fontId != currentFontRun.fontId )
670     {
671       fontClient.GetDescription(fontId,selectedFontDescription);
672     }
673
674     // Developer sets bold to character but selected font cannot support it
675     needSoftwareBoldening = ( currentFontDescription.weight >= TextAbstraction::FontWeight::BOLD ) && ( selectedFontDescription.weight < TextAbstraction::FontWeight::BOLD );
676
677     // Developer sets italic to character but selected font cannot support it
678     needSoftwareItalic = ( currentFontDescription.slant == TextAbstraction::FontSlant::ITALIC ) && ( selectedFontDescription.slant < TextAbstraction::FontSlant::ITALIC );
679
680     // The font is now validated.
681     if( ( fontId != currentFontRun.fontId ) ||
682         isNewParagraphCharacter ||
683         // If font id is same as previous but style is diffrent, initialize new one
684         ( ( fontId == currentFontRun.fontId ) && ( ( needSoftwareBoldening != currentFontRun.softwareBold ) || ( needSoftwareItalic != currentFontRun.softwareItalic ) ) ) )
685     {
686       // Current run needs to be stored and a new one initialized.
687
688       if( 0u != currentFontRun.characterRun.numberOfCharacters )
689       {
690         // Store the font run.
691         fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
692         ++fontIndex;
693       }
694
695       // Initialize the new one.
696       currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
697       currentFontRun.characterRun.numberOfCharacters = 0u;
698       currentFontRun.fontId = fontId;
699       currentFontRun.softwareItalic = needSoftwareItalic;
700       currentFontRun.softwareBold = needSoftwareBoldening;
701     }
702
703     // Add one more character to the run.
704     ++currentFontRun.characterRun.numberOfCharacters;
705
706     // Whether the current character is a new paragraph character.
707     isNewParagraphCharacter = TextAbstraction::IsNewParagraph( character );
708     isPreviousEmojiScript = isEmojiScript;
709   } // end traverse characters.
710
711   if( 0u != currentFontRun.characterRun.numberOfCharacters )
712   {
713     // Store the last run.
714     fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
715     ++fontIndex;
716   }
717
718   if( fontIndex < fonts.Count() )
719   {
720     // Update the indices of the next font runs.
721     const FontRun& run = *( fonts.Begin() + fontIndex - 1u );
722     CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
723
724     for( Vector<FontRun>::Iterator it = fonts.Begin() + fontIndex,
725            endIt = fonts.End();
726          it != endIt;
727          ++it )
728     {
729       FontRun& run = *it;
730
731       run.characterRun.characterIndex = nextCharacterIndex;
732       nextCharacterIndex += run.characterRun.numberOfCharacters;
733     }
734   }
735
736   DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
737 }
738
739 } // namespace Internal
740
741 } // namespace Text
742
743 } // namespace Toolkit
744
745 } // namespace Dali