2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/text/multi-language-support-impl.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/devel-api/common/singleton-service.h>
24 #include <dali/devel-api/text-abstraction/font-client.h>
27 #include <dali-toolkit/internal/text/multi-language-helper-functions.h>
37 #if defined(DEBUG_ENABLED)
38 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_MULTI_LANGUAGE_SUPPORT");
41 const Dali::Toolkit::Text::Character UTF32_A = 0x0041;
50 bool ValidateFontsPerScript::IsValidFont( FontId fontId ) const
52 for( Vector<FontId>::ConstIterator it = mValidFonts.Begin(),
53 endIt = mValidFonts.End();
66 FontId DefaultFonts::FindFont( TextAbstraction::FontClient& fontClient,
67 const TextAbstraction::FontDescription& description,
68 PointSize26Dot6 size ) const
70 for( std::vector<CacheItem>::const_iterator it = mFonts.begin(),
75 const CacheItem& item = *it;
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 ) ) )
90 void DefaultFonts::Cache( const TextAbstraction::FontDescription& description, FontId fontId )
93 item.description = description;
95 mFonts.push_back( item );
98 MultilanguageSupport::MultilanguageSupport()
99 : mDefaultFontPerScriptCache(),
100 mValidFontsPerScriptCache()
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 );
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 );
111 MultilanguageSupport::~MultilanguageSupport()
113 // Destroy the default font per script cache.
114 for( Vector<DefaultFonts*>::Iterator it = mDefaultFontPerScriptCache.Begin(),
115 endIt = mDefaultFontPerScriptCache.End();
122 // Destroy the valid fonts per script cache.
123 for( Vector<ValidateFontsPerScript*>::Iterator it = mValidFontsPerScriptCache.Begin(),
124 endIt = mValidFontsPerScriptCache.End();
132 Text::MultilanguageSupport MultilanguageSupport::Get()
134 Text::MultilanguageSupport multilanguageSupportHandle;
136 SingletonService service( SingletonService::Get() );
139 // Check whether the singleton is already created
140 Dali::BaseHandle handle = service.GetSingleton( typeid( Text::MultilanguageSupport ) );
143 // If so, downcast the handle
144 MultilanguageSupport* impl = dynamic_cast< Internal::MultilanguageSupport* >( handle.GetObjectPtr() );
145 multilanguageSupportHandle = Text::MultilanguageSupport( impl );
147 else // create and register the object
149 multilanguageSupportHandle = Text::MultilanguageSupport( new MultilanguageSupport );
150 service.Register( typeid( multilanguageSupportHandle ), multilanguageSupportHandle );
154 return multilanguageSupportHandle;
157 void MultilanguageSupport::SetScripts( const Vector<Character>& text,
158 CharacterIndex startIndex,
159 Length numberOfCharacters,
160 Vector<ScriptRun>& scripts )
162 if( 0u == numberOfCharacters )
164 // Nothing to do if there are no characters.
168 // Find the first index where to insert the script.
169 ScriptRunIndex scriptIndex = 0u;
170 if( 0u != startIndex )
172 for( Vector<ScriptRun>::ConstIterator it = scripts.Begin(),
173 endIt = scripts.End();
175 ++it, ++scriptIndex )
177 const ScriptRun& run = *it;
178 if( startIndex < run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
186 // Stores the current script run.
187 ScriptRun currentScriptRun;
188 currentScriptRun.characterRun.characterIndex = startIndex;
189 currentScriptRun.characterRun.numberOfCharacters = 0u;
190 currentScriptRun.script = TextAbstraction::UNKNOWN;
192 // Reserve some space to reduce the number of reallocations.
193 scripts.Reserve( text.Count() << 2u );
195 // Whether the first valid script needs to be set.
196 bool isFirstScriptToBeSet = true;
198 // Whether the first valid script is a right to left script.
199 bool isParagraphRTL = false;
201 // Count the number of characters which are valid for all scripts. i.e. white spaces or '\n'.
202 Length numberOfAllScriptCharacters = 0u;
204 // Pointers to the text buffer.
205 const Character* const textBuffer = text.Begin();
207 // Initialize whether is right to left direction
208 currentScriptRun.isRightToLeft = false;
210 // Traverse all characters and set the scripts.
211 const Length lastCharacter = startIndex + numberOfCharacters;
212 for( Length index = startIndex; index < lastCharacter; ++index )
214 Character character = *( textBuffer + index );
216 // Get the script of the character.
217 Script script = TextAbstraction::GetCharacterScript( character );
219 // Some characters (like white spaces) are valid for many scripts. The rules to set a script
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.
227 // Skip those characters valid for many scripts like white spaces or '\n'.
228 bool endOfText = index == lastCharacter;
230 ( TextAbstraction::COMMON == script ) )
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 );
235 if( TextAbstraction::EMOJI == currentScriptRun.script )
237 // Emojis doesn't mix well with characters common to all scripts. Insert the emoji run.
238 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
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;
248 // Count all these characters to be added into a script.
249 ++numberOfAllScriptCharacters;
251 if( TextAbstraction::IsNewParagraph( character ) )
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;
259 // Characters common to all scripts at the end of the paragraph are added to the last script.
260 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
262 // Store the script run.
263 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
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;
275 // Get the next character.
277 endOfText = index == lastCharacter;
280 character = *( textBuffer + index );
281 script = TextAbstraction::GetCharacterScript( character );
283 } // end while( !endOfText && ( TextAbstraction::COMMON == script ) )
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.
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 ) )
298 // Sets the direction of the first valid script.
299 isParagraphRTL = currentScriptRun.isRightToLeft || TextAbstraction::IsRightToLeftScript( script );
300 isFirstScriptToBeSet = false;
303 if( ( script != currentScriptRun.script ) &&
304 ( TextAbstraction::COMMON != script ) )
306 // Current run needs to be stored and a new one initialized.
308 if( ( isParagraphRTL == TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) ) &&
309 ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
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;
316 else if( ( TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) == TextAbstraction::IsRightToLeftScript( script ) ) &&
317 ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
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;
324 else if( ( TextAbstraction::UNKNOWN == currentScriptRun.script ) &&
325 ( TextAbstraction::EMOJI == script ) )
327 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
328 numberOfAllScriptCharacters = 0u;
331 if( 0u != currentScriptRun.characterRun.numberOfCharacters )
333 // Store the script run.
334 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
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 );
348 if( TextAbstraction::UNKNOWN != currentScriptRun.script )
350 // Adds white spaces between characters.
351 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
352 numberOfAllScriptCharacters = 0u;
355 // Add one more character to the run.
356 ++currentScriptRun.characterRun.numberOfCharacters;
360 // Add remaining characters into the last script.
361 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
363 if( 0u != currentScriptRun.characterRun.numberOfCharacters )
365 // Store the last run.
366 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
370 if( scriptIndex < scripts.Count() )
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;
376 for( Vector<ScriptRun>::Iterator it = scripts.Begin() + scriptIndex,
377 endIt = scripts.End();
381 ScriptRun& run = *it;
382 run.characterRun.characterIndex = nextCharacterIndex;
383 nextCharacterIndex += run.characterRun.numberOfCharacters;
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 )
397 DALI_LOG_INFO( gLogFilter, Debug::General, "-->MultilanguageSupport::ValidateFonts\n" );
399 if( 0u == numberOfCharacters )
401 DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
402 // Nothing to do if there are no characters.
406 // Find the first index where to insert the font run.
407 FontRunIndex fontIndex = 0u;
408 if( 0u != startIndex )
410 for( Vector<FontRun>::ConstIterator it = fonts.Begin(),
415 const FontRun& run = *it;
416 if( startIndex < run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
424 // Traverse the characters and validate/set the fonts.
427 DefaultFonts** defaultFontPerScriptCacheBuffer = mDefaultFontPerScriptCache.Begin();
428 ValidateFontsPerScript** validFontsPerScriptCacheBuffer = mValidFontsPerScriptCache.Begin();
430 // Stores the validated font runs.
431 fonts.Reserve( fontDescriptions.Count() );
433 // Initializes a validated font run.
434 FontRun currentFontRun;
435 currentFontRun.characterRun.characterIndex = startIndex;
436 currentFontRun.characterRun.numberOfCharacters = 0u;
437 currentFontRun.fontId = 0u;
438 currentFontRun.isBoldRequired = false;
439 currentFontRun.isItalicRequired = false;
441 // Get the font client.
442 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
444 const Character* const textBuffer = text.Begin();
446 // Iterators of the script runs.
447 Vector<ScriptRun>::ConstIterator scriptRunIt = scripts.Begin();
448 Vector<ScriptRun>::ConstIterator scriptRunEndIt = scripts.End();
449 bool isNewParagraphCharacter = false;
451 bool isPreviousEmojiScript = false;
453 CharacterIndex lastCharacter = startIndex + numberOfCharacters;
454 for( Length index = startIndex; index < lastCharacter; ++index )
456 // Get the current character.
457 const Character character = *( textBuffer + index );
458 bool isItalicRequired = false;
459 bool isBoldRequired = false;
461 // new description for current character
462 TextAbstraction::FontDescription currentFontDescription;
463 TextAbstraction::PointSize26Dot6 currentFontPointSize = defaultFontPointSize;
464 bool isDefaultFont = true;
465 MergeFontDescriptions( fontDescriptions,
466 defaultFontDescription,
467 defaultFontPointSize,
469 currentFontDescription,
470 currentFontPointSize,
473 // Get the font for the current character.
474 FontId fontId = fontClient.GetFontId( currentFontDescription, currentFontPointSize );
476 // Get the script for the current character.
477 Script script = GetScript( index,
483 Dali::TextAbstraction::FontDescription description;
484 fontClient.GetDescription( fontId, description );
486 DALI_LOG_INFO( gLogFilter,
488 " Initial font set\n Character : %x, Script : %s, Font : %s \n",
490 Dali::TextAbstraction::ScriptName[script],
491 description.path.c_str() );
495 // Validate whether the current character is supported by the given font.
496 bool isValidFont = false;
498 // Check first in the cache of default fonts per script and size.
500 FontId cachedDefaultFontId = 0u;
501 DefaultFonts* defaultFonts = *( defaultFontPerScriptCacheBuffer + script );
502 if( NULL != defaultFonts )
504 // This cache stores fall-back fonts.
505 cachedDefaultFontId = defaultFonts->FindFont( fontClient,
506 currentFontDescription,
507 currentFontPointSize );
510 // Whether the cached default font is valid.
511 const bool isValidCachedDefaultFont = 0u != cachedDefaultFontId;
513 // The font is valid if it matches with the default one for the current script and size and it's different than zero.
514 isValidFont = isValidCachedDefaultFont && ( fontId == cachedDefaultFontId );
518 // Check if the font supports the character.
519 isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
522 bool isCommonScript = false;
523 bool isEmojiScript = TextAbstraction::EMOJI == script;
525 if( isEmojiScript && !isPreviousEmojiScript )
527 if( 0u != currentFontRun.characterRun.numberOfCharacters )
529 // Store the font run.
530 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
534 // Initialize the new one.
535 currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
536 currentFontRun.characterRun.numberOfCharacters = 0u;
537 currentFontRun.fontId = fontId;
538 currentFontRun.isItalicRequired = false;
539 currentFontRun.isBoldRequired = false;
542 // If the given font is not valid, it means either:
543 // - there is no cached font for the current script yet or,
544 // - the user has set a different font than the default one for the current script or,
545 // - the platform default font is different than the default font for the current script.
547 // Need to check if the given font supports the current character.
548 if( !isValidFont ) // (1)
550 // Whether the current character is common for all scripts (i.e. white spaces, ...)
552 // Is not desirable to cache fonts for the common script.
554 // i.e. Consider the text " हिंदी", the 'white space' has assigned the DEVANAGARI script.
555 // The user may have set a font or the platform's default is used.
557 // As the 'white space' is the first character, no font is cached so the font validation
558 // retrieves a glyph from the given font.
560 // Many fonts support 'white spaces' so probably the font set by the user or the platform's default
561 // supports the 'white space'. However, that font may not support the DEVANAGARI script.
562 isCommonScript = TextAbstraction::IsCommonScript( character );
564 // Check in the valid fonts cache.
565 ValidateFontsPerScript* validateFontsPerScript = *( validFontsPerScriptCacheBuffer + script );
567 if( NULL != validateFontsPerScript )
569 // This cache stores valid fonts set by the user.
570 isValidFont = validateFontsPerScript->IsValidFont( fontId );
572 // It may happen that a validated font for a script doesn't have all the glyphs for that script.
573 // i.e a font validated for the CJK script may contain glyphs for the chinese language but not for the Japanese.
576 // Checks if the current character is supported by the font is needed.
577 isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
581 if( !isValidFont ) // (2)
583 // The selected font is not stored in any cache.
585 // Checks if the current character is supported by the selected font.
586 isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
588 // If there is a valid font, cache it.
589 if( isValidFont && !isCommonScript )
591 if( NULL == validateFontsPerScript )
593 validateFontsPerScript = new ValidateFontsPerScript();
595 *( validFontsPerScriptCacheBuffer + script ) = validateFontsPerScript;
598 validateFontsPerScript->mValidFonts.PushBack( fontId );
601 if( !isValidFont && ( fontId != cachedDefaultFontId ) && ( !TextAbstraction::IsNewParagraph( character ) )) // (3)
603 // The selected font by the user or the platform's default font has failed to validate the character.
605 // Checks if the previously discarted cached default font supports the character.
606 bool isValidCachedFont = false;
607 if( isValidCachedDefaultFont )
609 isValidCachedFont = fontClient.IsCharacterSupportedByFont( cachedDefaultFontId, character );
612 if( isValidCachedFont )
614 // Use the cached default font for the script if there is one.
615 fontId = cachedDefaultFontId;
619 // There is no valid cached default font for the script.
621 DefaultFonts* defaultFontsPerScript = NULL;
623 // Find a fallback-font.
624 fontId = fontClient.FindFallbackFont( character,
625 currentFontDescription,
626 currentFontPointSize,
631 fontId = fontClient.FindDefaultFont( UTF32_A, currentFontPointSize );
634 if ( !isCommonScript && (script != TextAbstraction::UNKNOWN) )
636 // Cache the font if it is not an unknown script
637 if( NULL == defaultFontsPerScript )
639 defaultFontsPerScript = *( defaultFontPerScriptCacheBuffer + script );
641 if( NULL == defaultFontsPerScript )
643 defaultFontsPerScript = new DefaultFonts();
644 *( defaultFontPerScriptCacheBuffer + script ) = defaultFontsPerScript;
647 defaultFontsPerScript->Cache( currentFontDescription, fontId );
650 } // !isValidFont (3)
651 } // !isValidFont (2)
652 } // !isValidFont (1)
656 Dali::TextAbstraction::FontDescription description;
657 fontClient.GetDescription( fontId, description );
658 DALI_LOG_INFO( gLogFilter,
660 " Validated font set\n Character : %x, Script : %s, Font : %s \n",
662 Dali::TextAbstraction::ScriptName[script],
663 description.path.c_str() );
667 // Whether bols style is required.
668 isBoldRequired = ( currentFontDescription.weight >= TextAbstraction::FontWeight::BOLD );
670 // Whether italic style is required.
671 isItalicRequired = ( currentFontDescription.slant >= TextAbstraction::FontSlant::ITALIC );
673 // The font is now validated.
674 if( ( fontId != currentFontRun.fontId ) ||
675 isNewParagraphCharacter ||
676 // If font id is same as previous but style is diffrent, initialize new one
677 ( ( fontId == currentFontRun.fontId ) && ( ( isBoldRequired != currentFontRun.isBoldRequired ) || ( isItalicRequired != currentFontRun.isItalicRequired ) ) ) )
679 // Current run needs to be stored and a new one initialized.
681 if( 0u != currentFontRun.characterRun.numberOfCharacters )
683 // Store the font run.
684 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
688 // Initialize the new one.
689 currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
690 currentFontRun.characterRun.numberOfCharacters = 0u;
691 currentFontRun.fontId = fontId;
692 currentFontRun.isBoldRequired = isBoldRequired;
693 currentFontRun.isItalicRequired = isItalicRequired;
696 // Add one more character to the run.
697 ++currentFontRun.characterRun.numberOfCharacters;
699 // Whether the current character is a new paragraph character.
700 isNewParagraphCharacter = TextAbstraction::IsNewParagraph( character );
701 isPreviousEmojiScript = isEmojiScript;
702 } // end traverse characters.
704 if( 0u != currentFontRun.characterRun.numberOfCharacters )
706 // Store the last run.
707 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
711 if( fontIndex < fonts.Count() )
713 // Update the indices of the next font runs.
714 const FontRun& run = *( fonts.Begin() + fontIndex - 1u );
715 CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
717 for( Vector<FontRun>::Iterator it = fonts.Begin() + fontIndex,
724 run.characterRun.characterIndex = nextCharacterIndex;
725 nextCharacterIndex += run.characterRun.numberOfCharacters;
729 DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
732 } // namespace Internal
736 } // namespace Toolkit