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 + 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 // 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 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
260 // Initialize the new one.
261 currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
262 currentScriptRun.characterRun.numberOfCharacters = 0u;
263 currentScriptRun.script = TextAbstraction::UNKNOWN;
264 numberOfAllScriptCharacters = 0u;
267 // Get the next character.
269 endOfText = index == lastCharacter;
272 character = *( textBuffer + index );
273 script = TextAbstraction::GetCharacterScript( character );
275 } // end while( !endOfText && ( TextAbstraction::COMMON == script ) )
279 // Last characters of the text are 'white spaces'.
280 // There is nothing else to do. Just add the remaining characters to the last script after this bucle.
284 // Check if it is the first character of a paragraph.
285 if( isFirstScriptToBeSet &&
286 ( TextAbstraction::UNKNOWN != script ) &&
287 ( TextAbstraction::COMMON != script ) &&
288 ( TextAbstraction::EMOJI != script ) )
290 // Sets the direction of the first valid script.
291 isParagraphRTL = TextAbstraction::IsRightToLeftScript( script );
292 isFirstScriptToBeSet = false;
295 if( ( script != currentScriptRun.script ) &&
296 ( TextAbstraction::COMMON != script ) )
298 // Current run needs to be stored and a new one initialized.
300 if( ( isParagraphRTL == TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) ) &&
301 ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
303 // Previous script has the same direction than the first script of the paragraph.
304 // All the previously skipped characters need to be added to the previous script before it's stored.
305 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
306 numberOfAllScriptCharacters = 0u;
308 else if( ( TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) == TextAbstraction::IsRightToLeftScript( script ) ) &&
309 ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
311 // Current script and previous one have the same direction.
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::UNKNOWN == currentScriptRun.script ) &&
317 ( TextAbstraction::EMOJI == script ) )
319 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
320 numberOfAllScriptCharacters = 0u;
323 if( 0u != currentScriptRun.characterRun.numberOfCharacters )
325 // Store the script run.
326 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
330 // Initialize the new one.
331 currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
332 currentScriptRun.characterRun.numberOfCharacters = numberOfAllScriptCharacters + 1u; // Adds the white spaces which are at the begining of the script.
333 currentScriptRun.script = script;
334 numberOfAllScriptCharacters = 0u;
338 if( TextAbstraction::UNKNOWN != currentScriptRun.script )
340 // Adds white spaces between characters.
341 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
342 numberOfAllScriptCharacters = 0u;
345 // Add one more character to the run.
346 ++currentScriptRun.characterRun.numberOfCharacters;
350 // Add remaining characters into the last script.
351 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
353 if( 0u != currentScriptRun.characterRun.numberOfCharacters )
355 // Store the last run.
356 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
360 if( scriptIndex < scripts.Count() )
362 // Update the indices of the next script runs.
363 const ScriptRun& run = *( scripts.Begin() + scriptIndex - 1u );
364 CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
366 for( Vector<ScriptRun>::Iterator it = scripts.Begin() + scriptIndex,
367 endIt = scripts.End();
371 ScriptRun& run = *it;
372 run.characterRun.characterIndex = nextCharacterIndex;
373 nextCharacterIndex += run.characterRun.numberOfCharacters;
378 void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
379 const Vector<ScriptRun>& scripts,
380 const Vector<FontDescriptionRun>& fontDescriptions,
381 const TextAbstraction::FontDescription& defaultFontDescription,
382 TextAbstraction::PointSize26Dot6 defaultFontPointSize,
383 CharacterIndex startIndex,
384 Length numberOfCharacters,
385 Vector<FontRun>& fonts )
387 DALI_LOG_INFO( gLogFilter, Debug::General, "-->MultilanguageSupport::ValidateFonts\n" );
389 if( 0u == numberOfCharacters )
391 DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
392 // Nothing to do if there are no characters.
396 // Find the first index where to insert the font run.
397 FontRunIndex fontIndex = 0u;
398 if( 0u != startIndex )
400 for( Vector<FontRun>::ConstIterator it = fonts.Begin(),
405 const FontRun& run = *it;
406 if( startIndex < run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
414 // Traverse the characters and validate/set the fonts.
417 DefaultFonts** defaultFontPerScriptCacheBuffer = mDefaultFontPerScriptCache.Begin();
418 ValidateFontsPerScript** validFontsPerScriptCacheBuffer = mValidFontsPerScriptCache.Begin();
420 // Stores the validated font runs.
421 fonts.Reserve( fontDescriptions.Count() );
423 // Initializes a validated font run.
424 FontRun currentFontRun;
425 currentFontRun.characterRun.characterIndex = startIndex;
426 currentFontRun.characterRun.numberOfCharacters = 0u;
427 currentFontRun.fontId = 0u;
429 // Get the font client.
430 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
432 const Character* const textBuffer = text.Begin();
434 // Iterators of the script runs.
435 Vector<ScriptRun>::ConstIterator scriptRunIt = scripts.Begin();
436 Vector<ScriptRun>::ConstIterator scriptRunEndIt = scripts.End();
437 bool isNewParagraphCharacter = false;
439 bool isPreviousEmojiScript = false;
441 // Description of fallback font which is selected at current iteration.
442 TextAbstraction::FontDescription selectedFontDescription;
444 CharacterIndex lastCharacter = startIndex + numberOfCharacters;
445 for( Length index = startIndex; index < lastCharacter; ++index )
447 // Get the current character.
448 const Character character = *( textBuffer + index );
449 bool needSoftwareBoldening = false;
450 bool needSoftwareItalic = false;
452 // new description for current character
453 TextAbstraction::FontDescription currentFontDescription;
454 TextAbstraction::PointSize26Dot6 currentFontPointSize = defaultFontPointSize;
455 bool isDefaultFont = true;
456 MergeFontDescriptions( fontDescriptions,
457 defaultFontDescription,
458 defaultFontPointSize,
460 currentFontDescription,
461 currentFontPointSize,
464 // Get the font for the current character.
465 FontId fontId = fontClient.GetFontId( currentFontDescription, currentFontPointSize );
467 // Get the script for the current character.
468 Script script = GetScript( index,
474 Dali::TextAbstraction::FontDescription description;
475 fontClient.GetDescription( fontId, description );
477 DALI_LOG_INFO( gLogFilter,
479 " Initial font set\n Character : %x, Script : %s, Font : %s \n",
481 Dali::TextAbstraction::ScriptName[script],
482 description.path.c_str() );
486 // Validate whether the current character is supported by the given font.
487 bool isValidFont = false;
489 // Check first in the cache of default fonts per script and size.
491 FontId cachedDefaultFontId = 0u;
492 DefaultFonts* defaultFonts = *( defaultFontPerScriptCacheBuffer + script );
493 if( NULL != defaultFonts )
495 // This cache stores fall-back fonts.
496 cachedDefaultFontId = defaultFonts->FindFont( fontClient,
497 currentFontDescription,
498 currentFontPointSize );
501 // Whether the cached default font is valid.
502 const bool isValidCachedDefaultFont = 0u != cachedDefaultFontId;
504 // The font is valid if it matches with the default one for the current script and size and it's different than zero.
505 isValidFont = isValidCachedDefaultFont && ( fontId == cachedDefaultFontId );
509 // Check if the font supports the character.
510 isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
513 bool isCommonScript = false;
514 bool isEmojiScript = TextAbstraction::EMOJI == script;
516 if( isEmojiScript && !isPreviousEmojiScript )
518 if( 0u != currentFontRun.characterRun.numberOfCharacters )
520 // Store the font run.
521 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
525 // Initialize the new one.
526 currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
527 currentFontRun.characterRun.numberOfCharacters = 0u;
528 currentFontRun.fontId = fontId;
531 // If the given font is not valid, it means either:
532 // - there is no cached font for the current script yet or,
533 // - the user has set a different font than the default one for the current script or,
534 // - the platform default font is different than the default font for the current script.
536 // Need to check if the given font supports the current character.
537 if( !isValidFont ) // (1)
539 // Whether the current character is common for all scripts (i.e. white spaces, ...)
541 // Is not desirable to cache fonts for the common script.
543 // i.e. Consider the text " हिंदी", the 'white space' has assigned the DEVANAGARI script.
544 // The user may have set a font or the platform's default is used.
546 // As the 'white space' is the first character, no font is cached so the font validation
547 // retrieves a glyph from the given font.
549 // Many fonts support 'white spaces' so probably the font set by the user or the platform's default
550 // supports the 'white space'. However, that font may not support the DEVANAGARI script.
551 isCommonScript = TextAbstraction::IsCommonScript( character );
553 // Check in the valid fonts cache.
554 ValidateFontsPerScript* validateFontsPerScript = *( validFontsPerScriptCacheBuffer + script );
556 if( NULL != validateFontsPerScript )
558 // This cache stores valid fonts set by the user.
559 isValidFont = validateFontsPerScript->IsValidFont( fontId );
561 // It may happen that a validated font for a script doesn't have all the glyphs for that script.
562 // i.e a font validated for the CJK script may contain glyphs for the chinese language but not for the Japanese.
565 // Checks if the current character is supported by the font is needed.
566 isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
570 if( !isValidFont ) // (2)
572 // The selected font is not stored in any cache.
574 // Checks if the current character is supported by the selected font.
575 isValidFont = fontClient.IsCharacterSupportedByFont( fontId, character );
577 // Emojis are present in many monochrome fonts; prefer color by default.
581 const GlyphIndex glyphIndex = fontClient.GetGlyphIndex( fontId, character );
583 // For color emojis, the font is valid if the glyph is a color glyph (the bitmap is RGBA).
584 isValidFont = fontClient.IsColorGlyph( fontId, glyphIndex );
587 // If there is a valid font, cache it.
588 if( isValidFont && !isCommonScript )
590 if( NULL == validateFontsPerScript )
592 validateFontsPerScript = new ValidateFontsPerScript();
594 *( validFontsPerScriptCacheBuffer + script ) = validateFontsPerScript;
597 validateFontsPerScript->mValidFonts.PushBack( fontId );
600 if( !isValidFont && ( fontId != cachedDefaultFontId ) && ( !TextAbstraction::IsNewParagraph( character ) )) // (3)
602 // The selected font by the user or the platform's default font has failed to validate the character.
604 // Checks if the previously discarted cached default font supports the character.
605 bool isValidCachedFont = false;
606 if( isValidCachedDefaultFont )
608 isValidCachedFont = fontClient.IsCharacterSupportedByFont( cachedDefaultFontId, character );
611 if( isValidCachedFont )
613 // Use the cached default font for the script if there is one.
614 fontId = cachedDefaultFontId;
618 // There is no valid cached default font for the script.
620 DefaultFonts* defaultFontsPerScript = NULL;
622 // Emojis are present in many monochrome fonts; prefer color by default.
623 const bool preferColor = ( TextAbstraction::EMOJI == script );
625 // Find a fallback-font.
626 fontId = fontClient.FindFallbackFont( character,
627 currentFontDescription,
628 currentFontPointSize,
633 fontId = fontClient.FindDefaultFont( UTF32_A, currentFontPointSize );
636 if ( !isCommonScript && (script != TextAbstraction::UNKNOWN) )
638 // Cache the font if it is not an unknown script
639 if( NULL == defaultFontsPerScript )
641 defaultFontsPerScript = *( defaultFontPerScriptCacheBuffer + script );
643 if( NULL == defaultFontsPerScript )
645 defaultFontsPerScript = new DefaultFonts();
646 *( defaultFontPerScriptCacheBuffer + script ) = defaultFontsPerScript;
649 defaultFontsPerScript->Cache( currentFontDescription, fontId );
652 } // !isValidFont (3)
653 } // !isValidFont (2)
654 } // !isValidFont (1)
658 Dali::TextAbstraction::FontDescription description;
659 fontClient.GetDescription( fontId, description );
660 DALI_LOG_INFO( gLogFilter,
662 " Validated font set\n Character : %x, Script : %s, Font : %s \n",
664 Dali::TextAbstraction::ScriptName[script],
665 description.path.c_str() );
669 if( fontId != currentFontRun.fontId )
671 fontClient.GetDescription(fontId,selectedFontDescription);
674 // Developer sets bold to character but selected font cannot support it
675 needSoftwareBoldening = ( currentFontDescription.weight >= TextAbstraction::FontWeight::BOLD ) && ( selectedFontDescription.weight < TextAbstraction::FontWeight::BOLD );
677 // Developer sets italic to character but selected font cannot support it
678 needSoftwareItalic = ( currentFontDescription.slant == TextAbstraction::FontSlant::ITALIC ) && ( selectedFontDescription.slant < TextAbstraction::FontSlant::ITALIC );
680 // The font is now validated.
681 if( ( fontId != currentFontRun.fontId ) ||
682 isNewParagraphCharacter ||
683 // If font id is same as previous but style is diffrent, initialize new one
684 ( ( fontId == currentFontRun.fontId ) && ( ( needSoftwareBoldening != currentFontRun.softwareBold ) || ( needSoftwareItalic != currentFontRun.softwareItalic ) ) ) )
686 // Current run needs to be stored and a new one initialized.
688 if( 0u != currentFontRun.characterRun.numberOfCharacters )
690 // Store the font run.
691 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
695 // Initialize the new one.
696 currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
697 currentFontRun.characterRun.numberOfCharacters = 0u;
698 currentFontRun.fontId = fontId;
699 currentFontRun.softwareItalic = needSoftwareItalic;
700 currentFontRun.softwareBold = needSoftwareBoldening;
703 // Add one more character to the run.
704 ++currentFontRun.characterRun.numberOfCharacters;
706 // Whether the current character is a new paragraph character.
707 isNewParagraphCharacter = TextAbstraction::IsNewParagraph( character );
708 isPreviousEmojiScript = isEmojiScript;
709 } // end traverse characters.
711 if( 0u != currentFontRun.characterRun.numberOfCharacters )
713 // Store the last run.
714 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
718 if( fontIndex < fonts.Count() )
720 // Update the indices of the next font runs.
721 const FontRun& run = *( fonts.Begin() + fontIndex - 1u );
722 CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
724 for( Vector<FontRun>::Iterator it = fonts.Begin() + fontIndex,
731 run.characterRun.characterIndex = nextCharacterIndex;
732 nextCharacterIndex += run.characterRun.numberOfCharacters;
736 DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
739 } // namespace Internal
743 } // namespace Toolkit