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