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