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>
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, 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, 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 // Traverse all characters and set the scripts.
208 const Length lastCharacter = startIndex + numberOfCharacters;
209 for( Length index = startIndex; index < lastCharacter; ++index )
211 Character character = *( textBuffer + index );
213 // Get the script of the character.
214 Script script = TextAbstraction::GetCharacterScript( character );
216 // Some characters (like white spaces) are valid for many scripts. The rules to set a script
218 // - If they are at the begining of a paragraph they get the script of the first character with
219 // a defined script. If they are at the end, they get the script of the last one.
220 // - If they are between two scripts with the same direction, they get the script of the previous
221 // character with a defined script. If the two scripts have different directions, they get the
222 // script of the first character of the paragraph with a defined script.
224 // Skip those characters valid for many scripts like white spaces or '\n'.
225 bool endOfText = index == lastCharacter;
227 ( TextAbstraction::COMMON == script ) )
229 if( TextAbstraction::EMOJI == currentScriptRun.script )
231 // Emojis doesn't mix well with characters common to all scripts. Insert the emoji run.
232 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
235 // Initialize the new one.
236 currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
237 currentScriptRun.characterRun.numberOfCharacters = 0u;
238 currentScriptRun.script = TextAbstraction::UNKNOWN;
239 numberOfAllScriptCharacters = 0u;
242 // Count all these characters to be added into a script.
243 ++numberOfAllScriptCharacters;
245 if( TextAbstraction::IsNewParagraph( character ) )
247 // The character is a new paragraph.
248 // To know when there is a new paragraph is needed because if there is a white space
249 // between two scripts with different directions, it is added to the script with
250 // the same direction than the first script of the paragraph.
251 isFirstScriptToBeSet = true;
253 // Characters common to all scripts at the end of the paragraph are added to the last script.
254 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
256 // Store the script run.
257 if( TextAbstraction::UNKNOWN == currentScriptRun.script )
259 currentScriptRun.script = TextAbstraction::LATIN;
261 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
264 // Initialize the new one.
265 currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
266 currentScriptRun.characterRun.numberOfCharacters = 0u;
267 currentScriptRun.script = TextAbstraction::UNKNOWN;
268 numberOfAllScriptCharacters = 0u;
271 // Get the next character.
273 endOfText = index == lastCharacter;
276 character = *( textBuffer + index );
277 script = TextAbstraction::GetCharacterScript( character );
279 } // end while( !endOfText && ( TextAbstraction::COMMON == script ) )
283 // Last characters of the text are 'white spaces'.
284 // There is nothing else to do. Just add the remaining characters to the last script after this bucle.
288 // Check if it is the first character of a paragraph.
289 if( isFirstScriptToBeSet &&
290 ( TextAbstraction::UNKNOWN != script ) &&
291 ( TextAbstraction::COMMON != script ) &&
292 ( TextAbstraction::EMOJI != script ) )
294 // Sets the direction of the first valid script.
295 isParagraphRTL = TextAbstraction::IsRightToLeftScript( script );
296 isFirstScriptToBeSet = false;
299 if( ( script != currentScriptRun.script ) &&
300 ( TextAbstraction::COMMON != script ) )
302 // Current run needs to be stored and a new one initialized.
304 if( ( isParagraphRTL == TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) ) &&
305 ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
307 // Previous script has the same direction than the first script of the paragraph.
308 // All the previously skipped characters need to be added to the previous script before it's stored.
309 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
310 numberOfAllScriptCharacters = 0u;
312 else if( ( TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) == TextAbstraction::IsRightToLeftScript( script ) ) &&
313 ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
315 // Current script and previous one have the same direction.
316 // All the previously skipped characters need to be added to the previous script before it's stored.
317 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
318 numberOfAllScriptCharacters = 0u;
320 else if( ( TextAbstraction::UNKNOWN == currentScriptRun.script ) &&
321 ( TextAbstraction::EMOJI == script ) )
323 currentScriptRun.script = TextAbstraction::LATIN;
324 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
325 numberOfAllScriptCharacters = 0u;
328 if( 0u != currentScriptRun.characterRun.numberOfCharacters )
330 // Store the script run.
331 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
335 // Initialize the new one.
336 currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
337 currentScriptRun.characterRun.numberOfCharacters = numberOfAllScriptCharacters + 1u; // Adds the white spaces which are at the begining of the script.
338 currentScriptRun.script = script;
339 numberOfAllScriptCharacters = 0u;
343 if( TextAbstraction::UNKNOWN != currentScriptRun.script )
345 // Adds white spaces between characters.
346 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
347 numberOfAllScriptCharacters = 0u;
350 // Add one more character to the run.
351 ++currentScriptRun.characterRun.numberOfCharacters;
355 // Add remaining characters into the last script.
356 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
358 if( 0u != currentScriptRun.characterRun.numberOfCharacters )
360 if( TextAbstraction::UNKNOWN == currentScriptRun.script )
362 // There are only white spaces in the last script. Set the latin script.
363 currentScriptRun.script = TextAbstraction::LATIN;
366 // Store the last run.
367 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
371 if( scriptIndex < scripts.Count() )
373 // Update the indices of the next script runs.
374 const ScriptRun& run = *( scripts.Begin() + scriptIndex - 1u );
375 CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
377 for( Vector<ScriptRun>::Iterator it = scripts.Begin() + scriptIndex,
378 endIt = scripts.End();
382 ScriptRun& run = *it;
383 run.characterRun.characterIndex = nextCharacterIndex;
384 nextCharacterIndex += run.characterRun.numberOfCharacters;
389 void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
390 const Vector<ScriptRun>& scripts,
391 const Vector<FontDescriptionRun>& fontDescriptions,
392 const TextAbstraction::FontDescription& defaultFontDescription,
393 TextAbstraction::PointSize26Dot6 defaultFontPointSize,
394 CharacterIndex startIndex,
395 Length numberOfCharacters,
396 Vector<FontRun>& fonts )
398 DALI_LOG_INFO( gLogFilter, Debug::General, "-->MultilanguageSupport::ValidateFonts\n" );
400 if( 0u == numberOfCharacters )
402 DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
403 // Nothing to do if there are no characters.
407 // Find the first index where to insert the font run.
408 FontRunIndex fontIndex = 0u;
409 if( 0u != startIndex )
411 for( Vector<FontRun>::ConstIterator it = fonts.Begin(),
416 const FontRun& run = *it;
417 if( startIndex < run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
425 // Traverse the characters and validate/set the fonts.
428 DefaultFonts** defaultFontPerScriptCacheBuffer = mDefaultFontPerScriptCache.Begin();
429 ValidateFontsPerScript** validFontsPerScriptCacheBuffer = mValidFontsPerScriptCache.Begin();
431 // Stores the validated font runs.
432 fonts.Reserve( fontDescriptions.Count() );
434 // Initializes a validated font run.
435 FontRun currentFontRun;
436 currentFontRun.characterRun.characterIndex = startIndex;
437 currentFontRun.characterRun.numberOfCharacters = 0u;
438 currentFontRun.fontId = 0u;
440 // Get the font client.
441 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
443 const Character* const textBuffer = text.Begin();
445 // Iterators of the script runs.
446 Vector<ScriptRun>::ConstIterator scriptRunIt = scripts.Begin();
447 Vector<ScriptRun>::ConstIterator scriptRunEndIt = scripts.End();
448 bool isNewParagraphCharacter = false;
450 FontId currentFontId = 0u;
451 FontId previousFontId = 0u;
452 bool isPreviousEmojiScript = false;
454 CharacterIndex lastCharacter = startIndex + numberOfCharacters;
455 for( Length index = startIndex; index < lastCharacter; ++index )
457 // Get the current character.
458 const Character character = *( textBuffer + index );
460 TextAbstraction::FontDescription currentFontDescription;
461 TextAbstraction::PointSize26Dot6 currentFontPointSize = defaultFontPointSize;
462 bool isDefaultFont = true;
463 MergeFontDescriptions( fontDescriptions,
464 defaultFontDescription,
465 defaultFontPointSize,
467 currentFontDescription,
468 currentFontPointSize,
471 // Get the font for the current character.
472 FontId fontId = fontClient.GetFontId( currentFontDescription, currentFontPointSize );
473 currentFontId = fontId;
475 // Get the script for the current character.
476 Script script = GetScript( index,
482 Dali::TextAbstraction::FontDescription description;
483 fontClient.GetDescription( fontId, description );
485 DALI_LOG_INFO( gLogFilter,
487 " Initial font set\n Character : %x, Script : %s, Font : %s \n",
489 Dali::TextAbstraction::ScriptName[script],
490 description.path.c_str() );
493 if( script == TextAbstraction::UNKNOWN )
495 script = TextAbstraction::LATIN;
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 );
674 if( NULL == defaultFontsPerScript )
676 defaultFontsPerScript = *( defaultFontPerScriptCacheBuffer + script );
678 if( NULL == defaultFontsPerScript )
680 defaultFontsPerScript = new DefaultFonts();
681 *( defaultFontPerScriptCacheBuffer + script ) = defaultFontsPerScript;
684 defaultFontsPerScript->Cache( currentFontDescription, fontId );
686 } // !isValidFont (3)
687 } // !isValidFont (2)
689 } // !isValidFont (1)
693 Dali::TextAbstraction::FontDescription description;
694 fontClient.GetDescription( fontId, description );
695 DALI_LOG_INFO( gLogFilter,
697 " Validated font set\n Character : %x, Script : %s, Font : %s \n",
699 Dali::TextAbstraction::ScriptName[script],
700 description.path.c_str() );
704 // The font is now validated.
705 if( ( fontId != currentFontRun.fontId ) ||
706 isNewParagraphCharacter )
708 // Current run needs to be stored and a new one initialized.
710 if( 0u != currentFontRun.characterRun.numberOfCharacters )
712 // Store the font run.
713 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
717 // Initialize the new one.
718 currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
719 currentFontRun.characterRun.numberOfCharacters = 0u;
720 currentFontRun.fontId = fontId;
723 // Add one more character to the run.
724 ++currentFontRun.characterRun.numberOfCharacters;
726 // Whether the current character is a new paragraph character.
727 isNewParagraphCharacter = TextAbstraction::IsNewParagraph( character );
728 previousFontId = currentFontId;
729 isPreviousEmojiScript = isEmojiScript;
730 } // end traverse characters.
732 if( 0u != currentFontRun.characterRun.numberOfCharacters )
734 // Store the last run.
735 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
739 if( fontIndex < fonts.Count() )
741 // Update the indices of the next font runs.
742 const FontRun& run = *( fonts.Begin() + fontIndex - 1u );
743 CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
745 for( Vector<FontRun>::Iterator it = fonts.Begin() + fontIndex,
752 run.characterRun.characterIndex = nextCharacterIndex;
753 nextCharacterIndex += run.characterRun.numberOfCharacters;
757 DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
760 } // namespace Internal
764 } // namespace Toolkit