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