2 * Copyright (c) 2017 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/adaptor-framework/singleton-service.h>
24 #include <dali/devel-api/text-abstraction/font-client.h>
27 #include <dali-toolkit/internal/text/multi-language-helper-functions.h>
28 #include <dali-toolkit/devel-api/styling/style-manager-devel.h>
38 #if defined(DEBUG_ENABLED)
39 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_MULTI_LANGUAGE_SUPPORT");
42 const Dali::Toolkit::Text::Character UTF32_A = 0x0041;
51 bool ValidateFontsPerScript::IsValidFont( FontId fontId ) const
53 for( Vector<FontId>::ConstIterator it = mValidFonts.Begin(),
54 endIt = mValidFonts.End();
67 FontId DefaultFonts::FindFont( TextAbstraction::FontClient& fontClient,
68 const TextAbstraction::FontDescription& description,
69 PointSize26Dot6 size ) const
71 for( std::vector<CacheItem>::const_iterator it = mFonts.begin(),
76 const CacheItem& item = *it;
78 if( ( ( TextAbstraction::FontWeight::NONE == description.weight ) || ( description.weight == item.description.weight ) ) &&
79 ( ( TextAbstraction::FontWidth::NONE == description.width ) || ( description.width == item.description.width ) ) &&
80 ( ( TextAbstraction::FontSlant::NONE == description.slant ) || ( description.slant == item.description.slant ) ) &&
81 ( size == fontClient.GetPointSize( item.fontId ) ) &&
82 ( description.family.empty() || ( description.family == item.description.family ) ) )
91 void DefaultFonts::Cache( const TextAbstraction::FontDescription& description, FontId fontId )
94 item.description = description;
96 mFonts.push_back( item );
99 MultilanguageSupport::MultilanguageSupport()
100 : mDefaultFontPerScriptCache(),
101 mValidFontsPerScriptCache()
103 // Initializes the default font cache to zero (invalid font).
104 // Reserves space to cache the default fonts and access them with the script as an index.
105 mDefaultFontPerScriptCache.Resize( TextAbstraction::UNKNOWN + 1, NULL );
107 // Initializes the valid fonts cache to NULL (no valid fonts).
108 // Reserves space to cache the valid fonts and access them with the script as an index.
109 mValidFontsPerScriptCache.Resize( TextAbstraction::UNKNOWN + 1, NULL );
111 // Add custom font directory
112 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
116 Property::Map config = Toolkit::DevelStyleManager::GetConfigurations( styleManager );
117 if( config["customFontDirectory"].Get( path ) )
119 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
120 fontClient.AddCustomFontDirectory( path.c_str() );
126 MultilanguageSupport::~MultilanguageSupport()
128 // Destroy the default font per script cache.
129 for( Vector<DefaultFonts*>::Iterator it = mDefaultFontPerScriptCache.Begin(),
130 endIt = mDefaultFontPerScriptCache.End();
137 // Destroy the valid fonts per script cache.
138 for( Vector<ValidateFontsPerScript*>::Iterator it = mValidFontsPerScriptCache.Begin(),
139 endIt = mValidFontsPerScriptCache.End();
147 Text::MultilanguageSupport MultilanguageSupport::Get()
149 Text::MultilanguageSupport multilanguageSupportHandle;
151 SingletonService service( SingletonService::Get() );
154 // Check whether the singleton is already created
155 Dali::BaseHandle handle = service.GetSingleton( typeid( Text::MultilanguageSupport ) );
158 // If so, downcast the handle
159 MultilanguageSupport* impl = dynamic_cast< Internal::MultilanguageSupport* >( handle.GetObjectPtr() );
160 multilanguageSupportHandle = Text::MultilanguageSupport( impl );
162 else // create and register the object
164 multilanguageSupportHandle = Text::MultilanguageSupport( new MultilanguageSupport );
165 service.Register( typeid( multilanguageSupportHandle ), multilanguageSupportHandle );
169 return multilanguageSupportHandle;
172 void MultilanguageSupport::SetScripts( const Vector<Character>& text,
173 CharacterIndex startIndex,
174 Length numberOfCharacters,
175 Vector<ScriptRun>& scripts )
177 if( 0u == numberOfCharacters )
179 // Nothing to do if there are no characters.
183 // Find the first index where to insert the script.
184 ScriptRunIndex scriptIndex = 0u;
185 if( 0u != startIndex )
187 for( Vector<ScriptRun>::ConstIterator it = scripts.Begin(),
188 endIt = scripts.End();
190 ++it, ++scriptIndex )
192 const ScriptRun& run = *it;
193 if( startIndex < run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
201 // Stores the current script run.
202 ScriptRun currentScriptRun;
203 currentScriptRun.characterRun.characterIndex = startIndex;
204 currentScriptRun.characterRun.numberOfCharacters = 0u;
205 currentScriptRun.script = TextAbstraction::UNKNOWN;
207 // Reserve some space to reduce the number of reallocations.
208 scripts.Reserve( text.Count() << 2u );
210 // Whether the first valid script needs to be set.
211 bool isFirstScriptToBeSet = true;
213 // Whether the first valid script is a right to left script.
214 bool isParagraphRTL = false;
216 // Count the number of characters which are valid for all scripts. i.e. white spaces or '\n'.
217 Length numberOfAllScriptCharacters = 0u;
219 // Pointers to the text buffer.
220 const Character* const textBuffer = text.Begin();
222 // Traverse all characters and set the scripts.
223 const Length lastCharacter = startIndex + numberOfCharacters;
224 for( Length index = startIndex; index < lastCharacter; ++index )
226 Character character = *( textBuffer + index );
228 // Get the script of the character.
229 Script script = TextAbstraction::GetCharacterScript( character );
231 // Some characters (like white spaces) are valid for many scripts. The rules to set a script
233 // - If they are at the begining of a paragraph they get the script of the first character with
234 // a defined script. If they are at the end, they get the script of the last one.
235 // - If they are between two scripts with the same direction, they get the script of the previous
236 // character with a defined script. If the two scripts have different directions, they get the
237 // script of the first character of the paragraph with a defined script.
239 // Skip those characters valid for many scripts like white spaces or '\n'.
240 bool endOfText = index == lastCharacter;
242 ( TextAbstraction::COMMON == script ) )
244 if( TextAbstraction::EMOJI == currentScriptRun.script )
246 // Emojis doesn't mix well with characters common to all scripts. Insert the emoji run.
247 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
250 // Initialize the new one.
251 currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
252 currentScriptRun.characterRun.numberOfCharacters = 0u;
253 currentScriptRun.script = TextAbstraction::UNKNOWN;
254 numberOfAllScriptCharacters = 0u;
257 // Count all these characters to be added into a script.
258 ++numberOfAllScriptCharacters;
260 if( TextAbstraction::IsNewParagraph( character ) )
262 // The character is a new paragraph.
263 // To know when there is a new paragraph is needed because if there is a white space
264 // between two scripts with different directions, it is added to the script with
265 // the same direction than the first script of the paragraph.
266 isFirstScriptToBeSet = true;
268 // Characters common to all scripts at the end of the paragraph are added to the last script.
269 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
271 // Store the script run.
272 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
275 // Initialize the new one.
276 currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
277 currentScriptRun.characterRun.numberOfCharacters = 0u;
278 currentScriptRun.script = TextAbstraction::UNKNOWN;
279 numberOfAllScriptCharacters = 0u;
282 // Get the next character.
284 endOfText = index == lastCharacter;
287 character = *( textBuffer + index );
288 script = TextAbstraction::GetCharacterScript( character );
290 } // end while( !endOfText && ( TextAbstraction::COMMON == script ) )
294 // Last characters of the text are 'white spaces'.
295 // There is nothing else to do. Just add the remaining characters to the last script after this bucle.
299 // Check if it is the first character of a paragraph.
300 if( isFirstScriptToBeSet &&
301 ( TextAbstraction::UNKNOWN != script ) &&
302 ( TextAbstraction::COMMON != script ) &&
303 ( TextAbstraction::EMOJI != script ) )
305 // Sets the direction of the first valid script.
306 isParagraphRTL = TextAbstraction::IsRightToLeftScript( script );
307 isFirstScriptToBeSet = false;
310 if( ( script != currentScriptRun.script ) &&
311 ( TextAbstraction::COMMON != script ) )
313 // Current run needs to be stored and a new one initialized.
315 if( ( isParagraphRTL == TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) ) &&
316 ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
318 // Previous script has the same direction than the first script of the paragraph.
319 // All the previously skipped characters need to be added to the previous script before it's stored.
320 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
321 numberOfAllScriptCharacters = 0u;
323 else if( ( TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) == TextAbstraction::IsRightToLeftScript( script ) ) &&
324 ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
326 // Current script and previous one have the same direction.
327 // All the previously skipped characters need to be added to the previous script before it's stored.
328 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
329 numberOfAllScriptCharacters = 0u;
331 else if( ( TextAbstraction::UNKNOWN == currentScriptRun.script ) &&
332 ( TextAbstraction::EMOJI == script ) )
334 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
335 numberOfAllScriptCharacters = 0u;
338 if( 0u != currentScriptRun.characterRun.numberOfCharacters )
340 // Store the script run.
341 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
345 // Initialize the new one.
346 currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
347 currentScriptRun.characterRun.numberOfCharacters = numberOfAllScriptCharacters + 1u; // Adds the white spaces which are at the begining of the script.
348 currentScriptRun.script = script;
349 numberOfAllScriptCharacters = 0u;
353 if( TextAbstraction::UNKNOWN != currentScriptRun.script )
355 // Adds white spaces between characters.
356 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
357 numberOfAllScriptCharacters = 0u;
360 // Add one more character to the run.
361 ++currentScriptRun.characterRun.numberOfCharacters;
365 // Add remaining characters into the last script.
366 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
368 if( 0u != currentScriptRun.characterRun.numberOfCharacters )
370 // Store the last run.
371 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
375 if( scriptIndex < scripts.Count() )
377 // Update the indices of the next script runs.
378 const ScriptRun& run = *( scripts.Begin() + scriptIndex - 1u );
379 CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
381 for( Vector<ScriptRun>::Iterator it = scripts.Begin() + scriptIndex,
382 endIt = scripts.End();
386 ScriptRun& run = *it;
387 run.characterRun.characterIndex = nextCharacterIndex;
388 nextCharacterIndex += run.characterRun.numberOfCharacters;
393 void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
394 const Vector<ScriptRun>& scripts,
395 const Vector<FontDescriptionRun>& fontDescriptions,
396 const TextAbstraction::FontDescription& defaultFontDescription,
397 TextAbstraction::PointSize26Dot6 defaultFontPointSize,
398 CharacterIndex startIndex,
399 Length numberOfCharacters,
400 Vector<FontRun>& fonts )
402 DALI_LOG_INFO( gLogFilter, Debug::General, "-->MultilanguageSupport::ValidateFonts\n" );
404 if( 0u == numberOfCharacters )
406 DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
407 // Nothing to do if there are no characters.
411 // Find the first index where to insert the font run.
412 FontRunIndex fontIndex = 0u;
413 if( 0u != startIndex )
415 for( Vector<FontRun>::ConstIterator it = fonts.Begin(),
420 const FontRun& run = *it;
421 if( startIndex < run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
429 // Traverse the characters and validate/set the fonts.
432 DefaultFonts** defaultFontPerScriptCacheBuffer = mDefaultFontPerScriptCache.Begin();
433 ValidateFontsPerScript** validFontsPerScriptCacheBuffer = mValidFontsPerScriptCache.Begin();
435 // Stores the validated font runs.
436 fonts.Reserve( fontDescriptions.Count() );
438 // Initializes a validated font run.
439 FontRun currentFontRun;
440 currentFontRun.characterRun.characterIndex = startIndex;
441 currentFontRun.characterRun.numberOfCharacters = 0u;
442 currentFontRun.fontId = 0u;
444 // Get the font client.
445 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
447 const Character* const textBuffer = text.Begin();
449 // Iterators of the script runs.
450 Vector<ScriptRun>::ConstIterator scriptRunIt = scripts.Begin();
451 Vector<ScriptRun>::ConstIterator scriptRunEndIt = scripts.End();
452 bool isNewParagraphCharacter = false;
454 FontId currentFontId = 0u;
455 FontId previousFontId = 0u;
456 bool isPreviousEmojiScript = false;
458 CharacterIndex lastCharacter = startIndex + numberOfCharacters;
459 for( Length index = startIndex; index < lastCharacter; ++index )
461 // Get the current character.
462 const Character character = *( textBuffer + index );
464 TextAbstraction::FontDescription currentFontDescription;
465 TextAbstraction::PointSize26Dot6 currentFontPointSize = defaultFontPointSize;
466 bool isDefaultFont = true;
467 MergeFontDescriptions( fontDescriptions,
468 defaultFontDescription,
469 defaultFontPointSize,
471 currentFontDescription,
472 currentFontPointSize,
475 // Get the font for the current character.
476 FontId fontId = fontClient.GetFontId( currentFontDescription, currentFontPointSize );
477 currentFontId = fontId;
479 // Get the script for the current character.
480 Script script = GetScript( index,
486 Dali::TextAbstraction::FontDescription description;
487 fontClient.GetDescription( fontId, description );
489 DALI_LOG_INFO( gLogFilter,
491 " Initial font set\n Character : %x, Script : %s, Font : %s \n",
493 Dali::TextAbstraction::ScriptName[script],
494 description.path.c_str() );
498 // Validate whether the current character is supported by the given font.
499 bool isValidFont = false;
501 // Check first in the cache of default fonts per script and size.
503 FontId cachedDefaultFontId = 0u;
504 DefaultFonts* defaultFonts = *( defaultFontPerScriptCacheBuffer + script );
505 if( NULL != defaultFonts )
507 // This cache stores fall-back fonts.
508 cachedDefaultFontId = defaultFonts->FindFont( fontClient,
509 currentFontDescription,
510 currentFontPointSize );
513 // Whether the cached default font is valid.
514 const bool isValidCachedDefaultFont = 0u != cachedDefaultFontId;
516 // The font is valid if it matches with the default one for the current script and size and it's different than zero.
517 isValidFont = isValidCachedDefaultFont && ( fontId == cachedDefaultFontId );
521 // Check if the font supports the character.
522 isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
525 bool isCommonScript = false;
526 bool isEmojiScript = TextAbstraction::EMOJI == script;
528 if( isEmojiScript && !isPreviousEmojiScript )
530 if( 0u != currentFontRun.characterRun.numberOfCharacters )
532 // Store the font run.
533 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
537 // Initialize the new one.
538 currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
539 currentFontRun.characterRun.numberOfCharacters = 0u;
540 currentFontRun.fontId = fontId;
543 // If the given font is not valid, it means either:
544 // - there is no cached font for the current script yet or,
545 // - the user has set a different font than the default one for the current script or,
546 // - the platform default font is different than the default font for the current script.
548 // Need to check if the given font supports the current character.
549 if( !isValidFont ) // (1)
551 // Whether the current character is common for all scripts (i.e. white spaces, ...)
553 // Is not desirable to cache fonts for the common script.
555 // i.e. Consider the text " हिंदी", the 'white space' has assigned the DEVANAGARI script.
556 // The user may have set a font or the platform's default is used.
558 // As the 'white space' is the first character, no font is cached so the font validation
559 // retrieves a glyph from the given font.
561 // Many fonts support 'white spaces' so probably the font set by the user or the platform's default
562 // supports the 'white space'. However, that font may not support the DEVANAGARI script.
563 isCommonScript = TextAbstraction::IsCommonScript( character );
567 if( isValidCachedDefaultFont &&
568 ( isDefaultFont || ( currentFontId == previousFontId ) ) &&
571 // At this point the character common for all scripts has no font assigned.
572 // If there is a valid previously cached default font for it, use that one.
573 fontId = cachedDefaultFontId;
578 // Check in the valid fonts cache.
579 ValidateFontsPerScript* validateFontsPerScript = *( validFontsPerScriptCacheBuffer + script );
581 if( NULL != validateFontsPerScript )
583 // This cache stores valid fonts set by the user.
584 isValidFont = validateFontsPerScript->IsValidFont( fontId );
586 // It may happen that a validated font for a script doesn't have all the glyphs for that script.
587 // i.e a font validated for the CJK script may contain glyphs for the chinese language but not for the Japanese.
590 // Checks if the current character is supported by the font is needed.
591 isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
595 if( !isValidFont ) // (2)
597 // The selected font is not stored in any cache.
599 // Checks if the current character is supported by the selected font.
600 isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
602 // Emojis are present in many monochrome fonts; prefer color by default.
606 const GlyphIndex glyphIndex = fontClient.GetGlyphIndex( fontId, character );
608 // For color emojis, the font is valid if the glyph is a color glyph (the bitmap is RGBA).
609 isValidFont = fontClient.IsColorGlyph( fontId, glyphIndex );
612 // If there is a valid font, cache it.
615 if( NULL == validateFontsPerScript )
617 validateFontsPerScript = new ValidateFontsPerScript();
619 *( validFontsPerScriptCacheBuffer + script ) = validateFontsPerScript;
622 validateFontsPerScript->mValidFonts.PushBack( fontId );
625 if( !isValidFont && ( fontId != cachedDefaultFontId ) ) // (3)
627 // The selected font by the user or the platform's default font has failed to validate the character.
629 // Checks if the previously discarted cached default font supports the character.
630 bool isValidCachedFont = false;
631 if( isValidCachedDefaultFont )
633 isValidCachedFont = fontClient.IsCharacterSupportedByFont( cachedDefaultFontId, character );
636 if( isValidCachedFont )
638 // Use the cached default font for the script if there is one.
639 fontId = cachedDefaultFontId;
643 // There is no valid cached default font for the script.
645 DefaultFonts* defaultFontsPerScript = NULL;
647 // Emojis are present in many monochrome fonts; prefer color by default.
648 const bool preferColor = ( TextAbstraction::EMOJI == script );
650 // Find a fallback-font.
651 fontId = fontClient.FindFallbackFont( character,
652 currentFontDescription,
653 currentFontPointSize,
658 // If the system does not support a suitable font, fallback to Latin
659 defaultFontsPerScript = *( defaultFontPerScriptCacheBuffer + TextAbstraction::LATIN );
660 if( NULL != defaultFontsPerScript )
662 fontId = defaultFontsPerScript->FindFont( fontClient,
663 currentFontDescription,
664 currentFontPointSize );
670 fontId = fontClient.FindDefaultFont( UTF32_A, currentFontPointSize );
673 if ( script != TextAbstraction::UNKNOWN )
675 // Cache the font if it is not an unknown script
676 if( NULL == defaultFontsPerScript )
678 defaultFontsPerScript = *( defaultFontPerScriptCacheBuffer + script );
680 if( NULL == defaultFontsPerScript )
682 defaultFontsPerScript = new DefaultFonts();
683 *( defaultFontPerScriptCacheBuffer + script ) = defaultFontsPerScript;
686 defaultFontsPerScript->Cache( currentFontDescription, fontId );
689 } // !isValidFont (3)
690 } // !isValidFont (2)
692 } // !isValidFont (1)
696 Dali::TextAbstraction::FontDescription description;
697 fontClient.GetDescription( fontId, description );
698 DALI_LOG_INFO( gLogFilter,
700 " Validated font set\n Character : %x, Script : %s, Font : %s \n",
702 Dali::TextAbstraction::ScriptName[script],
703 description.path.c_str() );
707 // The font is now validated.
708 if( ( fontId != currentFontRun.fontId ) ||
709 isNewParagraphCharacter )
711 // Current run needs to be stored and a new one initialized.
713 if( 0u != currentFontRun.characterRun.numberOfCharacters )
715 // Store the font run.
716 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
720 // Initialize the new one.
721 currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
722 currentFontRun.characterRun.numberOfCharacters = 0u;
723 currentFontRun.fontId = fontId;
726 // Add one more character to the run.
727 ++currentFontRun.characterRun.numberOfCharacters;
729 // Whether the current character is a new paragraph character.
730 isNewParagraphCharacter = TextAbstraction::IsNewParagraph( character );
731 previousFontId = currentFontId;
732 isPreviousEmojiScript = isEmojiScript;
733 } // end traverse characters.
735 if( 0u != currentFontRun.characterRun.numberOfCharacters )
737 // Store the last run.
738 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
742 if( fontIndex < fonts.Count() )
744 // Update the indices of the next font runs.
745 const FontRun& run = *( fonts.Begin() + fontIndex - 1u );
746 CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
748 for( Vector<FontRun>::Iterator it = fonts.Begin() + fontIndex,
755 run.characterRun.characterIndex = nextCharacterIndex;
756 nextCharacterIndex += run.characterRun.numberOfCharacters;
760 DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
763 } // namespace Internal
767 } // namespace Toolkit