Add trace log to check text performance
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / multi-language-support-impl.cpp
1 /*
2  * Copyright (c) 2022 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 #include <dali/integration-api/trace.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/internal/text/emoji-helper.h>
29 #include <dali-toolkit/internal/text/multi-language-helper-functions.h>
30
31 namespace Dali
32 {
33 namespace Toolkit
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 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_FONT_PERFORMANCE_MARKER, false);
42
43 const Dali::Toolkit::Text::Character UTF32_A = 0x0041;
44 } // namespace
45
46 namespace Text
47 {
48 namespace Internal
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::GetNumberOfScripts(), 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::GetNumberOfScripts(), 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 - 1u;
212
213   for(Length index = startIndex; index <= lastCharacter; ++index)
214   {
215     Character character = *(textBuffer + index);
216
217     // Get the script of the character.
218     Script script = TextAbstraction::GetCharacterScript(character);
219
220     // Some characters (like white spaces) are valid for many scripts. The rules to set a script
221     // for them are:
222     // - If they are at the begining of a paragraph they get the script of the first character with
223     //   a defined script. If they are at the end, they get the script of the last one.
224     // - If they are between two scripts with the same direction, they get the script of the previous
225     //   character with a defined script. If the two scripts have different directions, they get the
226     //   script of the first character of the paragraph with a defined script.
227
228     // Skip those characters valid for many scripts like white spaces or '\n'.
229     bool endOfText = index > lastCharacter;
230
231     //Handle all Emoji Sequence cases
232     if(IsNewSequence(textBuffer, currentScriptRun.script, index, lastCharacter, script))
233     {
234       AddCurrentScriptAndCreatNewScript(script,
235                                         false,
236                                         false,
237                                         currentScriptRun,
238                                         numberOfAllScriptCharacters,
239                                         scripts,
240                                         scriptIndex);
241     }
242     else if(IsScriptChangedToFollowSequence(currentScriptRun.script, character, script))
243     {
244       currentScriptRun.script = script;
245     }
246     else if(IsOneOfEmojiScripts(currentScriptRun.script) && (TextAbstraction::COMMON == script))
247     {
248       // Emojis doesn't mix well with characters common to all scripts. Insert the emoji run.
249       AddCurrentScriptAndCreatNewScript(TextAbstraction::UNKNOWN,
250                                         false,
251                                         false,
252                                         currentScriptRun,
253                                         numberOfAllScriptCharacters,
254                                         scripts,
255                                         scriptIndex);
256     }
257
258     while(!endOfText &&
259           (TextAbstraction::COMMON == script))
260     {
261       // Check if whether is right to left markup and Keeps true if the previous value was true.
262       currentScriptRun.isRightToLeft = currentScriptRun.isRightToLeft || TextAbstraction::IsRightToLeftMark(character);
263
264       // Count all these characters to be added into a script.
265       ++numberOfAllScriptCharacters;
266
267       if(TextAbstraction::IsNewParagraph(character))
268       {
269         // The character is a new paragraph.
270         // To know when there is a new paragraph is needed because if there is a white space
271         // between two scripts with different directions, it is added to the script with
272         // the same direction than the first script of the paragraph.
273         isFirstScriptToBeSet = true;
274
275         AddCurrentScriptAndCreatNewScript(TextAbstraction::UNKNOWN,
276                                           false,
277                                           false,
278                                           currentScriptRun,
279                                           numberOfAllScriptCharacters,
280                                           scripts,
281                                           scriptIndex);
282       }
283
284       // Get the next character.
285       ++index;
286       endOfText = index > lastCharacter;
287       if(!endOfText)
288       {
289         character = *(textBuffer + index);
290         script    = TextAbstraction::GetCharacterScript(character);
291
292         //Handle all Emoji Sequence cases
293         if(IsNewSequence(textBuffer, currentScriptRun.script, index, lastCharacter, script))
294         {
295           AddCurrentScriptAndCreatNewScript(script,
296                                             false,
297                                             false,
298                                             currentScriptRun,
299                                             numberOfAllScriptCharacters,
300                                             scripts,
301                                             scriptIndex);
302         }
303         else if(IsScriptChangedToFollowSequence(currentScriptRun.script, character, script))
304         {
305           currentScriptRun.script = script;
306         }
307       }
308     } // end while( !endOfText && ( TextAbstraction::COMMON == script ) )
309
310     if(endOfText)
311     {
312       // Last characters of the text are 'white spaces'.
313       // There is nothing else to do. Just add the remaining characters to the last script after this bucle.
314       break;
315     }
316
317     // Check if it is the first character of a paragraph.
318     if(isFirstScriptToBeSet &&
319        (TextAbstraction::UNKNOWN != script) &&
320        (TextAbstraction::COMMON != script) &&
321        (TextAbstraction::EMOJI != script) &&
322        (TextAbstraction::EMOJI_TEXT != script) &&
323        (TextAbstraction::EMOJI_COLOR != script) &&
324        (!TextAbstraction::IsSymbolScript(script)))
325     {
326       // Sets the direction of the first valid script.
327       isParagraphRTL       = currentScriptRun.isRightToLeft || TextAbstraction::IsRightToLeftScript(script);
328       isFirstScriptToBeSet = false;
329     }
330
331     if((script != currentScriptRun.script) &&
332        (TextAbstraction::COMMON != script))
333     {
334       // Current run needs to be stored and a new one initialized.
335
336       if((isParagraphRTL == TextAbstraction::IsRightToLeftScript(currentScriptRun.script)) &&
337          (TextAbstraction::UNKNOWN != currentScriptRun.script))
338       {
339         // Previous script has the same direction than the first script of the paragraph.
340         // All the previously skipped characters need to be added to the previous script before it's stored.
341         currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
342         numberOfAllScriptCharacters = 0u;
343       }
344       else if((TextAbstraction::IsRightToLeftScript(currentScriptRun.script) == TextAbstraction::IsRightToLeftScript(script)) &&
345               (TextAbstraction::UNKNOWN != currentScriptRun.script))
346       {
347         // Current script and previous one have the same direction.
348         // All the previously skipped characters need to be added to the previous script before it's stored.
349         currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
350         numberOfAllScriptCharacters = 0u;
351       }
352       else if((TextAbstraction::UNKNOWN == currentScriptRun.script) &&
353               (TextAbstraction::IsSymbolOrEmojiOrTextScript(script)))
354       {
355         currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
356         numberOfAllScriptCharacters = 0u;
357       }
358
359       // Adds the white spaces which are at the begining of the script.
360       numberOfAllScriptCharacters++;
361       AddCurrentScriptAndCreatNewScript(script,
362                                         TextAbstraction::IsRightToLeftScript(script),
363                                         true,
364                                         currentScriptRun,
365                                         numberOfAllScriptCharacters,
366                                         scripts,
367                                         scriptIndex);
368     }
369     else
370     {
371       if(TextAbstraction::UNKNOWN != currentScriptRun.script)
372       {
373         // Adds white spaces between characters.
374         currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
375         numberOfAllScriptCharacters = 0u;
376       }
377
378       // Add one more character to the run.
379       ++currentScriptRun.characterRun.numberOfCharacters;
380     }
381   }
382
383   // Add remaining characters into the last script.
384   currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
385
386   if(0u != currentScriptRun.characterRun.numberOfCharacters)
387   {
388     // Store the last run.
389     scripts.Insert(scripts.Begin() + scriptIndex, currentScriptRun);
390     ++scriptIndex;
391   }
392
393   if(scriptIndex < scripts.Count())
394   {
395     // Update the indices of the next script runs.
396     const ScriptRun& run                = *(scripts.Begin() + scriptIndex - 1u);
397     CharacterIndex   nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
398
399     for(Vector<ScriptRun>::Iterator it    = scripts.Begin() + scriptIndex,
400                                     endIt = scripts.End();
401         it != endIt;
402         ++it)
403     {
404       ScriptRun& run                  = *it;
405       run.characterRun.characterIndex = nextCharacterIndex;
406       nextCharacterIndex += run.characterRun.numberOfCharacters;
407     }
408   }
409 }
410
411 void MultilanguageSupport::ValidateFonts(const Vector<Character>&                text,
412                                          const Vector<ScriptRun>&                scripts,
413                                          const Vector<FontDescriptionRun>&       fontDescriptions,
414                                          const TextAbstraction::FontDescription& defaultFontDescription,
415                                          TextAbstraction::PointSize26Dot6        defaultFontPointSize,
416                                          CharacterIndex                          startIndex,
417                                          Length                                  numberOfCharacters,
418                                          Vector<FontRun>&                        fonts)
419 {
420   DALI_LOG_INFO(gLogFilter, Debug::General, "-->MultilanguageSupport::ValidateFonts\n");
421
422   if(0u == numberOfCharacters)
423   {
424     DALI_LOG_INFO(gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n");
425     // Nothing to do if there are no characters.
426     return;
427   }
428
429   DALI_TRACE_SCOPE(gTraceFilter, "DALI_TEXT_FONTS_VALIDATE");
430
431   // Find the first index where to insert the font run.
432   FontRunIndex fontIndex = 0u;
433   if(0u != startIndex)
434   {
435     for(Vector<FontRun>::ConstIterator it    = fonts.Begin(),
436                                        endIt = fonts.End();
437         it != endIt;
438         ++it, ++fontIndex)
439     {
440       const FontRun& run = *it;
441       if(startIndex < run.characterRun.characterIndex + run.characterRun.numberOfCharacters)
442       {
443         // Run found.
444         break;
445       }
446     }
447   }
448
449   // Traverse the characters and validate/set the fonts.
450
451   // Get the caches.
452   DefaultFonts**           defaultFontPerScriptCacheBuffer = mDefaultFontPerScriptCache.Begin();
453   ValidateFontsPerScript** validFontsPerScriptCacheBuffer  = mValidFontsPerScriptCache.Begin();
454
455   // Stores the validated font runs.
456   fonts.Reserve(fontDescriptions.Count());
457
458   // Initializes a validated font run.
459   FontRun currentFontRun;
460   currentFontRun.characterRun.characterIndex     = startIndex;
461   currentFontRun.characterRun.numberOfCharacters = 0u;
462   currentFontRun.fontId                          = 0u;
463   currentFontRun.isBoldRequired                  = false;
464   currentFontRun.isItalicRequired                = false;
465
466   // Get the font client.
467   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
468
469   const Character* const textBuffer = text.Begin();
470
471   // Iterators of the script runs.
472   Vector<ScriptRun>::ConstIterator scriptRunIt             = scripts.Begin();
473   Vector<ScriptRun>::ConstIterator scriptRunEndIt          = scripts.End();
474   bool                             isNewParagraphCharacter = false;
475
476   FontId previousEmojiFontId = 0u;
477   FontId currentFontId       = 0u;
478   FontId previousFontId      = 0u;
479   TextAbstraction::Script previousScript = TextAbstraction::UNKNOWN;
480
481   CharacterIndex lastCharacter = startIndex + numberOfCharacters - 1u;
482   for(Length index = startIndex; index <= lastCharacter; ++index)
483   {
484     // Get the current character.
485     const Character character        = *(textBuffer + index);
486     bool            isItalicRequired = false;
487     bool            isBoldRequired   = false;
488
489     // new description for current character
490     TextAbstraction::FontDescription currentFontDescription;
491     TextAbstraction::PointSize26Dot6 currentFontPointSize = defaultFontPointSize;
492     bool                             isDefaultFont        = true;
493     MergeFontDescriptions(fontDescriptions,
494                           defaultFontDescription,
495                           defaultFontPointSize,
496                           index,
497                           currentFontDescription,
498                           currentFontPointSize,
499                           isDefaultFont);
500
501     // Get the font for the current character.
502     FontId fontId = fontClient.GetFontId(currentFontDescription, currentFontPointSize);
503     currentFontId = fontId;
504
505     // Get the script for the current character.
506     Script script = GetScript(index,
507                               scriptRunIt,
508                               scriptRunEndIt);
509
510 #ifdef DEBUG_ENABLED
511     if(gLogFilter->IsEnabledFor(Debug::Verbose))
512     {
513       Dali::TextAbstraction::FontDescription description;
514       fontClient.GetDescription(fontId, description);
515
516       DALI_LOG_INFO(gLogFilter,
517                     Debug::Verbose,
518                     "  Initial font set\n  Character : %x, Script : %s, Font : %s \n",
519                     character,
520                     Dali::TextAbstraction::ScriptName[script],
521                     description.path.c_str());
522     }
523 #endif
524
525     // Validate whether the current character is supported by the given font.
526     bool isValidFont = false;
527
528     // Check first in the cache of default fonts per script and size.
529
530     FontId        cachedDefaultFontId = 0u;
531     DefaultFonts* defaultFonts        = *(defaultFontPerScriptCacheBuffer + script);
532     if(NULL != defaultFonts)
533     {
534       // This cache stores fall-back fonts.
535       cachedDefaultFontId = defaultFonts->FindFont(fontClient,
536                                                    currentFontDescription,
537                                                    currentFontPointSize);
538     }
539
540     // Whether the cached default font is valid.
541     const bool isValidCachedDefaultFont = 0u != cachedDefaultFontId;
542
543     // The font is valid if it matches with the default one for the current script and size and it's different than zero.
544     isValidFont = isValidCachedDefaultFont && (fontId == cachedDefaultFontId);
545
546     if(isValidFont)
547     {
548       // Check if the font supports the character.
549       isValidFont = fontClient.IsCharacterSupportedByFont(fontId, character);
550     }
551
552     bool isCommonScript = false;
553     bool isEmojiScript  = TextAbstraction::IsOneOfEmojiScripts(script);
554
555     if(isEmojiScript && (previousScript == script))
556     {
557       // Emoji sequence should use the previous emoji font.
558       if(0u != previousEmojiFontId)
559       {
560         fontId      = previousEmojiFontId;
561         isValidFont = true;
562       }
563     }
564
565     if(TextAbstraction::IsSpace(character) &&
566        TextAbstraction::HasLigatureMustBreak(script) &&
567        isValidCachedDefaultFont &&
568        (isDefaultFont || (currentFontId == previousFontId)))
569     {
570       fontId      = cachedDefaultFontId;
571       isValidFont = true;
572     }
573
574     // If the given font is not valid, it means either:
575     // - there is no cached font for the current script yet or,
576     // - the user has set a different font than the default one for the current script or,
577     // - the platform default font is different than the default font for the current script.
578
579     // Need to check if the given font supports the current character.
580     if(!isValidFont) // (1)
581     {
582       // Whether the current character is common for all scripts (i.e. white spaces, ...)
583
584       // Is not desirable to cache fonts for the common script.
585       //
586       // i.e. Consider the text " à¤¹à¤¿à¤‚दी", the 'white space' has assigned the DEVANAGARI script.
587       //      The user may have set a font or the platform's default is used.
588       //
589       //      As the 'white space' is the first character, no font is cached so the font validation
590       //      retrieves a glyph from the given font.
591       //
592       //      Many fonts support 'white spaces' so probably the font set by the user or the platform's default
593       //      supports the 'white space'. However, that font may not support the DEVANAGARI script.
594       isCommonScript = TextAbstraction::IsCommonScript(character) || TextAbstraction::IsEmojiPresentationSelector(character);
595
596       // Check in the valid fonts cache.
597       ValidateFontsPerScript* validateFontsPerScript = *(validFontsPerScriptCacheBuffer + script);
598
599       if(NULL != validateFontsPerScript)
600       {
601         // This cache stores valid fonts set by the user.
602         isValidFont = validateFontsPerScript->IsValidFont(fontId);
603
604         // It may happen that a validated font for a script doesn't have all the glyphs for that script.
605         // i.e a font validated for the CJK script may contain glyphs for the chinese language but not for the Japanese.
606         if(isValidFont)
607         {
608           // Checks if the current character is supported by the font is needed.
609           isValidFont = fontClient.IsCharacterSupportedByFont(fontId, character);
610         }
611       }
612
613       if(!isValidFont) // (2)
614       {
615         // The selected font is not stored in any cache.
616
617         // Checks if the current character is supported by the selected font.
618         isValidFont = fontClient.IsCharacterSupportedByFont(fontId, character);
619
620         // If there is a valid font, cache it.
621         if(isValidFont && !isCommonScript)
622         {
623           if(NULL == validateFontsPerScript)
624           {
625             validateFontsPerScript = new ValidateFontsPerScript();
626
627             *(validFontsPerScriptCacheBuffer + script) = validateFontsPerScript;
628           }
629
630           validateFontsPerScript->mValidFonts.PushBack(fontId);
631         }
632
633         if(!isValidFont && (fontId != cachedDefaultFontId) && (!TextAbstraction::IsNewParagraph(character))) // (3)
634         {
635           // The selected font by the user or the platform's default font has failed to validate the character.
636
637           // Checks if the previously discarted cached default font supports the character.
638           bool isValidCachedFont = false;
639           if(isValidCachedDefaultFont)
640           {
641             isValidCachedFont = fontClient.IsCharacterSupportedByFont(cachedDefaultFontId, character);
642           }
643
644           if(isValidCachedFont)
645           {
646             // Use the cached default font for the script if there is one.
647             fontId = cachedDefaultFontId;
648             isValidFont = true;
649           }
650           else
651           {
652             // There is no valid cached default font for the script.
653
654             DefaultFonts* defaultFontsPerScript = NULL;
655
656             // Find a fallback-font.
657             fontId = fontClient.FindFallbackFont(character,
658                                                  currentFontDescription,
659                                                  currentFontPointSize,
660                                                  false);
661
662             if(0u == fontId)
663             {
664               fontId = fontClient.FindDefaultFont(UTF32_A, currentFontPointSize);
665             }
666
667             if(!isCommonScript && (script != TextAbstraction::UNKNOWN))
668             {
669               // Cache the font if it is not an unknown script
670               if(NULL == defaultFontsPerScript)
671               {
672                 defaultFontsPerScript = *(defaultFontPerScriptCacheBuffer + script);
673
674                 if(NULL == defaultFontsPerScript)
675                 {
676                   defaultFontsPerScript                       = new DefaultFonts();
677                   *(defaultFontPerScriptCacheBuffer + script) = defaultFontsPerScript;
678                 }
679               }
680               defaultFontsPerScript->Cache(currentFontDescription, fontId);
681               isValidFont = true;
682             }
683           }
684         } // !isValidFont (3)
685       }   // !isValidFont (2)
686     }     // !isValidFont (1)
687
688     if(isEmojiScript && (previousScript != script))
689     {
690       //New Emoji sequence should select font according to the variation selector (VS15 or VS16).
691       if(0u != currentFontRun.characterRun.numberOfCharacters)
692       {
693         // Store the font run.
694         fonts.Insert(fonts.Begin() + fontIndex, currentFontRun);
695         ++fontIndex;
696       }
697
698       // Initialize the new one.
699       currentFontRun.characterRun.characterIndex     = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
700       currentFontRun.characterRun.numberOfCharacters = 0u;
701       currentFontRun.fontId                          = fontId;
702       currentFontRun.isItalicRequired                = false;
703       currentFontRun.isBoldRequired                  = false;
704
705       if(TextAbstraction::IsEmojiColorScript(script) || TextAbstraction::IsEmojiTextScript(script))
706       {
707         bool       isModifiedByVariationSelector = false;
708         GlyphIndex glyphIndexChar                = fontClient.GetGlyphIndex(fontId, character);
709         GlyphIndex glyphIndexCharByVS            = fontClient.GetGlyphIndex(fontId, character, Text::GetVariationSelectorByScript(script));
710
711         isModifiedByVariationSelector = glyphIndexChar != glyphIndexCharByVS;
712
713         if(isModifiedByVariationSelector)
714         {
715           FontId requestedFontId = fontClient.FindDefaultFont(character, currentFontPointSize, IsEmojiColorScript(script));
716           if(0u != requestedFontId)
717           {
718             currentFontRun.fontId = fontId = requestedFontId;
719             isValidFont                    = true;
720           }
721         }
722       }
723     }
724
725     // Store the font id when the first character is an emoji.
726     if(isEmojiScript)
727     {
728       if(0u != fontId && previousScript != script)
729       {
730         previousEmojiFontId = fontId;
731       }
732     }
733     else
734     {
735       previousEmojiFontId = 0u;
736     }
737
738 #ifdef DEBUG_ENABLED
739     if(gLogFilter->IsEnabledFor(Debug::Verbose))
740     {
741       Dali::TextAbstraction::FontDescription description;
742       fontClient.GetDescription(fontId, description);
743       DALI_LOG_INFO(gLogFilter,
744                     Debug::Verbose,
745                     "  Validated font set\n  Character : %x, Script : %s, Font : %s \n",
746                     character,
747                     Dali::TextAbstraction::ScriptName[script],
748                     description.path.c_str());
749     }
750 #endif
751     if(!isValidFont && !isCommonScript)
752     {
753       Dali::TextAbstraction::FontDescription descriptionForLog;
754       fontClient.GetDescription(fontId, descriptionForLog);
755       DALI_LOG_RELEASE_INFO("Validated font set fail : Character : %x, Script : %s, Font : %s \n",
756                             character,
757                             Dali::TextAbstraction::ScriptName[script],
758                             descriptionForLog.path.c_str());
759     }
760
761     // Whether bols style is required.
762     isBoldRequired = (currentFontDescription.weight >= TextAbstraction::FontWeight::BOLD);
763
764     // Whether italic style is required.
765     isItalicRequired = (currentFontDescription.slant >= TextAbstraction::FontSlant::ITALIC);
766
767     // The font is now validated.
768     if((fontId != currentFontRun.fontId) ||
769        isNewParagraphCharacter ||
770        // If font id is same as previous but style is diffrent, initialize new one
771        ((fontId == currentFontRun.fontId) && ((isBoldRequired != currentFontRun.isBoldRequired) || (isItalicRequired != currentFontRun.isItalicRequired))))
772     {
773       // Current run needs to be stored and a new one initialized.
774
775       if(0u != currentFontRun.characterRun.numberOfCharacters)
776       {
777         // Store the font run.
778         fonts.Insert(fonts.Begin() + fontIndex, currentFontRun);
779         ++fontIndex;
780       }
781
782       // Initialize the new one.
783       currentFontRun.characterRun.characterIndex     = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
784       currentFontRun.characterRun.numberOfCharacters = 0u;
785       currentFontRun.fontId                          = fontId;
786       currentFontRun.isBoldRequired                  = isBoldRequired;
787       currentFontRun.isItalicRequired                = isItalicRequired;
788     }
789
790     // Add one more character to the run.
791     ++currentFontRun.characterRun.numberOfCharacters;
792
793     // Whether the current character is a new paragraph character.
794     isNewParagraphCharacter = TextAbstraction::IsNewParagraph(character);
795     previousScript          = script;
796     previousFontId          = currentFontId;
797   } // end traverse characters.
798
799   if(0u != currentFontRun.characterRun.numberOfCharacters)
800   {
801     // Store the last run.
802     fonts.Insert(fonts.Begin() + fontIndex, currentFontRun);
803     ++fontIndex;
804   }
805
806   if(fontIndex < fonts.Count())
807   {
808     // Update the indices of the next font runs.
809     const FontRun& run                = *(fonts.Begin() + fontIndex - 1u);
810     CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
811
812     for(Vector<FontRun>::Iterator it    = fonts.Begin() + fontIndex,
813                                   endIt = fonts.End();
814         it != endIt;
815         ++it)
816     {
817       FontRun& run = *it;
818
819       run.characterRun.characterIndex = nextCharacterIndex;
820       nextCharacterIndex += run.characterRun.numberOfCharacters;
821     }
822   }
823
824   DALI_LOG_INFO(gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n");
825 }
826
827 void MultilanguageSupport::AddCurrentScriptAndCreatNewScript(const Script       requestedScript,
828                                                              const bool         isRightToLeft,
829                                                              const bool         addScriptCharactersToNewScript,
830                                                              ScriptRun&         currentScriptRun,
831                                                              Length&            numberOfAllScriptCharacters,
832                                                              Vector<ScriptRun>& scripts,
833                                                              ScriptRunIndex&    scriptIndex)
834 {
835   // Add the pending characters to the current script
836   currentScriptRun.characterRun.numberOfCharacters += (addScriptCharactersToNewScript ? 0u : numberOfAllScriptCharacters);
837
838   // In-case the current script is empty then no need to add it for scripts
839   if(0u != currentScriptRun.characterRun.numberOfCharacters)
840   {
841     // Store the script run.
842     scripts.Insert(scripts.Begin() + scriptIndex, currentScriptRun);
843     ++scriptIndex;
844   }
845
846   // Initialize the new one by the requested script
847   currentScriptRun.characterRun.characterIndex     = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
848   currentScriptRun.characterRun.numberOfCharacters = (addScriptCharactersToNewScript ? numberOfAllScriptCharacters : 0u);
849   currentScriptRun.script                          = requestedScript;
850   numberOfAllScriptCharacters                      = 0u;
851   // Initialize whether is right to left direction
852   currentScriptRun.isRightToLeft = isRightToLeft;
853 }
854
855 } // namespace Internal
856
857 } // namespace Text
858
859 } // namespace Toolkit
860
861 } // namespace Dali