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