2 * Copyright (c) 2015 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, PointSize26Dot6 size ) const
68 for( Vector<FontId>::ConstIterator it = mFonts.Begin(),
73 const FontId fontId = *it;
74 if( size == fontClient.GetPointSize( fontId ) )
83 MultilanguageSupport::MultilanguageSupport()
84 : mDefaultFontPerScriptCache(),
85 mValidFontsPerScriptCache()
87 // Initializes the default font cache to zero (invalid font).
88 // Reserves space to cache the default fonts and access them with the script as an index.
89 mDefaultFontPerScriptCache.Resize( TextAbstraction::UNKNOWN, NULL );
91 // Initializes the valid fonts cache to NULL (no valid fonts).
92 // Reserves space to cache the valid fonts and access them with the script as an index.
93 mValidFontsPerScriptCache.Resize( TextAbstraction::UNKNOWN, NULL );
96 MultilanguageSupport::~MultilanguageSupport()
98 // Destroy the default font per script cache.
99 for( Vector<DefaultFonts*>::Iterator it = mDefaultFontPerScriptCache.Begin(),
100 endIt = mDefaultFontPerScriptCache.End();
107 // Destroy the valid fonts per script cache.
108 for( Vector<ValidateFontsPerScript*>::Iterator it = mValidFontsPerScriptCache.Begin(),
109 endIt = mValidFontsPerScriptCache.End();
117 Text::MultilanguageSupport MultilanguageSupport::Get()
119 Text::MultilanguageSupport multilanguageSupportHandle;
121 SingletonService service( SingletonService::Get() );
124 // Check whether the singleton is already created
125 Dali::BaseHandle handle = service.GetSingleton( typeid( Text::MultilanguageSupport ) );
128 // If so, downcast the handle
129 MultilanguageSupport* impl = dynamic_cast< Internal::MultilanguageSupport* >( handle.GetObjectPtr() );
130 multilanguageSupportHandle = Text::MultilanguageSupport( impl );
132 else // create and register the object
134 multilanguageSupportHandle = Text::MultilanguageSupport( new MultilanguageSupport );
135 service.Register( typeid( multilanguageSupportHandle ), multilanguageSupportHandle );
139 return multilanguageSupportHandle;
142 void MultilanguageSupport::SetScripts( const Vector<Character>& text,
143 CharacterIndex startIndex,
144 Length numberOfCharacters,
145 Vector<ScriptRun>& scripts )
147 if( 0u == numberOfCharacters )
149 // Nothing to do if there are no characters.
153 // Find the first index where to insert the script.
154 ScriptRunIndex scriptIndex = 0u;
155 if( 0u != startIndex )
157 for( Vector<ScriptRun>::ConstIterator it = scripts.Begin(),
158 endIt = scripts.End();
160 ++it, ++scriptIndex )
162 const ScriptRun& run = *it;
163 if( startIndex < run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
171 // Stores the current script run.
172 ScriptRun currentScriptRun;
173 currentScriptRun.characterRun.characterIndex = startIndex;
174 currentScriptRun.characterRun.numberOfCharacters = 0u;
175 currentScriptRun.script = TextAbstraction::UNKNOWN;
177 // Reserve some space to reduce the number of reallocations.
178 scripts.Reserve( text.Count() << 2u );
180 // Whether the first valid script needs to be set.
181 bool isFirstScriptToBeSet = true;
183 // Whether the first valid script is a right to left script.
184 bool isParagraphRTL = false;
186 // Count the number of characters which are valid for all scripts. i.e. white spaces or '\n'.
187 Length numberOfAllScriptCharacters = 0u;
189 // Pointers to the text buffer.
190 const Character* const textBuffer = text.Begin();
192 // Traverse all characters and set the scripts.
193 const Length lastCharacter = startIndex + numberOfCharacters;
194 for( Length index = startIndex; index < lastCharacter; ++index )
196 Character character = *( textBuffer + index );
198 // Get the script of the character.
199 Script script = TextAbstraction::GetCharacterScript( character );
201 // Some characters (like white spaces) are valid for many scripts. The rules to set a script
203 // - If they are at the begining of a paragraph they get the script of the first character with
204 // a defined script. If they are at the end, they get the script of the last one.
205 // - If they are between two scripts with the same direction, they get the script of the previous
206 // character with a defined script. If the two scripts have different directions, they get the
207 // script of the first character of the paragraph with a defined script.
209 // Skip those characters valid for many scripts like white spaces or '\n'.
210 bool endOfText = index == lastCharacter;
212 ( TextAbstraction::COMMON == script ) )
214 if( TextAbstraction::EMOJI == currentScriptRun.script )
216 // Emojis doesn't mix well with characters common to all scripts. Insert the emoji run.
217 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
220 // Initialize the new one.
221 currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
222 currentScriptRun.characterRun.numberOfCharacters = 0u;
223 currentScriptRun.script = TextAbstraction::UNKNOWN;
224 numberOfAllScriptCharacters = 0u;
227 // Count all these characters to be added into a script.
228 ++numberOfAllScriptCharacters;
230 if( TextAbstraction::IsNewParagraph( character ) )
232 // The character is a new paragraph.
233 // To know when there is a new paragraph is needed because if there is a white space
234 // between two scripts with different directions, it is added to the script with
235 // the same direction than the first script of the paragraph.
236 isFirstScriptToBeSet = true;
238 // Characters common to all scripts at the end of the paragraph are added to the last script.
239 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
241 // Store the script run.
242 if( TextAbstraction::UNKNOWN == currentScriptRun.script )
244 currentScriptRun.script = TextAbstraction::LATIN;
246 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
249 // Initialize the new one.
250 currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
251 currentScriptRun.characterRun.numberOfCharacters = 0u;
252 currentScriptRun.script = TextAbstraction::UNKNOWN;
253 numberOfAllScriptCharacters = 0u;
256 // Get the next character.
258 endOfText = index == lastCharacter;
261 character = *( textBuffer + index );
262 script = TextAbstraction::GetCharacterScript( character );
264 } // end while( !endOfText && ( TextAbstraction::COMMON == script ) )
268 // Last characters of the text are 'white spaces'.
269 // There is nothing else to do. Just add the remaining characters to the last script after this bucle.
273 // Check if it is the first character of a paragraph.
274 if( isFirstScriptToBeSet &&
275 ( TextAbstraction::UNKNOWN != script ) &&
276 ( TextAbstraction::COMMON != script ) &&
277 ( TextAbstraction::EMOJI != script ) )
279 // Sets the direction of the first valid script.
280 isParagraphRTL = TextAbstraction::IsRightToLeftScript( script );
281 isFirstScriptToBeSet = false;
284 if( ( script != currentScriptRun.script ) &&
285 ( TextAbstraction::COMMON != script ) )
287 // Current run needs to be stored and a new one initialized.
289 if( ( isParagraphRTL == TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) ) &&
290 ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
292 // Previous script has the same direction than the first script of the paragraph.
293 // All the previously skipped characters need to be added to the previous script before it's stored.
294 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
295 numberOfAllScriptCharacters = 0u;
297 else if( ( TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) == TextAbstraction::IsRightToLeftScript( script ) ) &&
298 ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
300 // Current script and previous one have the same direction.
301 // All the previously skipped characters need to be added to the previous script before it's stored.
302 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
303 numberOfAllScriptCharacters = 0u;
305 else if( ( TextAbstraction::UNKNOWN == currentScriptRun.script ) &&
306 ( TextAbstraction::EMOJI == script ) )
308 currentScriptRun.script = TextAbstraction::LATIN;
309 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
310 numberOfAllScriptCharacters = 0u;
313 if( 0u != currentScriptRun.characterRun.numberOfCharacters )
315 // Store the script run.
316 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
320 // Initialize the new one.
321 currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
322 currentScriptRun.characterRun.numberOfCharacters = numberOfAllScriptCharacters + 1u; // Adds the white spaces which are at the begining of the script.
323 currentScriptRun.script = script;
324 numberOfAllScriptCharacters = 0u;
328 if( TextAbstraction::UNKNOWN != currentScriptRun.script )
330 // Adds white spaces between characters.
331 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
332 numberOfAllScriptCharacters = 0u;
335 // Add one more character to the run.
336 ++currentScriptRun.characterRun.numberOfCharacters;
340 // Add remaining characters into the last script.
341 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
343 if( 0u != currentScriptRun.characterRun.numberOfCharacters )
345 if( TextAbstraction::UNKNOWN == currentScriptRun.script )
347 // There are only white spaces in the last script. Set the latin script.
348 currentScriptRun.script = TextAbstraction::LATIN;
351 // Store the last run.
352 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
356 if( scriptIndex < scripts.Count() )
358 // Update the indices of the next script runs.
359 const ScriptRun& run = *( scripts.Begin() + scriptIndex - 1u );
360 CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
362 for( Vector<ScriptRun>::Iterator it = scripts.Begin() + scriptIndex,
363 endIt = scripts.End();
367 ScriptRun& run = *it;
368 run.characterRun.characterIndex = nextCharacterIndex;
369 nextCharacterIndex += run.characterRun.numberOfCharacters;
374 void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
375 const Vector<ScriptRun>& scripts,
376 const Vector<FontDescriptionRun>& fontDescriptions,
377 FontId defaultFontId,
378 CharacterIndex startIndex,
379 Length numberOfCharacters,
380 Vector<FontRun>& fonts )
382 DALI_LOG_INFO( gLogFilter, Debug::General, "-->MultilanguageSupport::ValidateFonts\n" );
384 if( 0u == numberOfCharacters )
386 DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
387 // Nothing to do if there are no characters.
391 // Find the first index where to insert the font run.
392 FontRunIndex fontIndex = 0u;
393 if( 0u != startIndex )
395 for( Vector<FontRun>::ConstIterator it = fonts.Begin(),
400 const FontRun& run = *it;
401 if( startIndex < run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
409 // Traverse the characters and validate/set the fonts.
412 DefaultFonts** defaultFontPerScriptCacheBuffer = mDefaultFontPerScriptCache.Begin();
413 ValidateFontsPerScript** validFontsPerScriptCacheBuffer = mValidFontsPerScriptCache.Begin();
415 // Stores the validated font runs.
416 fonts.Reserve( fontDescriptions.Count() );
418 // Initializes a validated font run.
419 FontRun currentFontRun;
420 currentFontRun.characterRun.characterIndex = startIndex;
421 currentFontRun.characterRun.numberOfCharacters = 0u;
422 currentFontRun.fontId = 0u;
424 // Get the font client.
425 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
427 // Get the default font description and default size.
428 TextAbstraction::FontDescription defaultFontDescription;
429 TextAbstraction::PointSize26Dot6 defaultPointSize = TextAbstraction::FontClient::DEFAULT_POINT_SIZE;
430 if( defaultFontId > 0u )
432 fontClient.GetDescription( defaultFontId, defaultFontDescription );
433 defaultPointSize = fontClient.GetPointSize( defaultFontId );
436 // Merge font descriptions
437 Vector<FontId> fontIds;
438 fontIds.Resize( numberOfCharacters, defaultFontId );
439 Vector<bool> isDefaultFont;
440 isDefaultFont.Resize( numberOfCharacters, true );
441 MergeFontDescriptions( fontDescriptions,
444 defaultFontDescription,
447 numberOfCharacters );
449 const Character* const textBuffer = text.Begin();
450 const FontId* const fontIdsBuffer = fontIds.Begin();
451 const bool* const isDefaultFontBuffer = isDefaultFont.Begin();
452 Vector<ScriptRun>::ConstIterator scriptRunIt = scripts.Begin();
453 Vector<ScriptRun>::ConstIterator scriptRunEndIt = scripts.End();
454 bool isNewParagraphCharacter = false;
456 PointSize26Dot6 currentPointSize = defaultPointSize;
457 FontId currentFontId = 0u;
458 FontId previousFontId = 0u;
459 bool isPreviousEmojiScript = false;
461 // Whether it's the first set of characters to be validated.
462 // Used in case the paragraph starts with characters common to all scripts.
463 bool isFirstSetToBeValidated = true;
465 CharacterIndex lastCharacter = startIndex + numberOfCharacters;
466 for( Length index = startIndex; index < lastCharacter; ++index )
468 // Get the current character.
469 const Character character = *( textBuffer + index );
471 // Get the font for the current character.
472 FontId fontId = *( fontIdsBuffer + index - startIndex );
474 // Whether the font being validated for the current character is a default one not set by the user.
475 const bool isDefault = *( isDefaultFontBuffer + index - startIndex );
477 // Get the script for the current character.
478 const Script script = GetScript( index,
482 // Get the current point size.
483 if( currentFontId != fontId )
485 currentPointSize = fontClient.GetPointSize( fontId );
486 currentFontId = fontId;
491 Dali::TextAbstraction::FontDescription description;
492 fontClient.GetDescription( fontId, description );
494 DALI_LOG_INFO( gLogFilter,
496 " Initial font set\n Character : %x, Script : %s, Font : %s \n",
498 Dali::TextAbstraction::ScriptName[script],
499 description.path.c_str() );
503 // Validate whether the current character is supported by the given font.
504 bool isValidFont = false;
506 // Check first in the cache of default fonts per script and size.
508 DefaultFonts* defaultFonts = *( defaultFontPerScriptCacheBuffer + script );
509 FontId cachedDefaultFontId = 0u;
510 if( NULL != defaultFonts )
512 cachedDefaultFontId = defaultFonts->FindFont( fontClient, currentPointSize );
515 // Whether the cached default font is valid.
516 const bool isValidCachedDefaultFont = 0u != cachedDefaultFontId;
518 // The font is valid if it matches with the default one for the current script and size and it's different than zero.
519 isValidFont = isValidCachedDefaultFont && ( fontId == cachedDefaultFontId );
521 bool isCommonScript = false;
522 bool isEmojiScript = TextAbstraction::EMOJI == script;
524 if( isEmojiScript && !isPreviousEmojiScript )
526 if( 0u != currentFontRun.characterRun.numberOfCharacters )
528 // Store the font run.
529 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
533 // Initialize the new one.
534 currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
535 currentFontRun.characterRun.numberOfCharacters = 0u;
536 currentFontRun.fontId = fontId;
540 // If the given font is not valid, it means either:
541 // - there is no cached font for the current script yet or,
542 // - the user has set a different font than the default one for the current script or,
543 // - the platform default font is different than the default font for the current script.
545 // Need to check if the given font supports the current character.
546 if( !isValidFont ) // (1)
548 // Whether the current character is common for all scripts (i.e. white spaces, ...)
550 // Is not desirable to cache fonts for the common script.
552 // i.e. Consider the text " हिंदी", the 'white space' has assigned the DEVANAGARI script.
553 // The user may have set a font or the platform's default is used.
555 // As the 'white space' is the first character, no font is cached so the font validation
556 // retrieves a glyph from the given font.
558 // Many fonts support 'white spaces' so probably the font set by the user or the platform's default
559 // supports the 'white space'. However, that font may not support the DEVANAGARI script.
560 isCommonScript = TextAbstraction::IsCommonScript( character );
564 if( isValidCachedDefaultFont &&
565 ( isDefault || ( currentFontId == previousFontId ) ) &&
568 // At this point the character common for all scripts has no font assigned.
569 // If there is a valid previously cached default font for it, use that one.
570 fontId = cachedDefaultFontId;
576 // Check in the valid fonts cache.
577 ValidateFontsPerScript* validateFontsPerScript = *( validFontsPerScriptCacheBuffer + script );
579 if( NULL != validateFontsPerScript )
581 isValidFont = validateFontsPerScript->IsValidFont( fontId );
584 if( !isValidFont ) // (2)
586 // Use the font client to validate the font.
587 const GlyphIndex glyphIndex = fontClient.GetGlyphIndex( fontId, character );
589 // The font is valid if there is a glyph for that character.
590 isValidFont = 0u != glyphIndex;
592 // Emojis are present in many monochrome fonts; prefer color by default.
596 const BufferImage bitmap = fontClient.CreateBitmap( fontId, glyphIndex );
598 // For color emojis, the font is valid if the bitmap is RGBA.
599 isValidFont = bitmap && ( Pixel::BGRA8888 == bitmap.GetPixelFormat() );
602 // If there is a valid font, cache it.
605 if( NULL == validateFontsPerScript )
607 validateFontsPerScript = new ValidateFontsPerScript();
609 *( validFontsPerScriptCacheBuffer + script ) = validateFontsPerScript;
612 validateFontsPerScript->mValidFonts.PushBack( fontId );
615 if( !isValidFont ) // (3)
617 // The given font has not been validated.
619 if( isValidCachedDefaultFont )
621 // Use the cached default font for the script if there is one.
622 fontId = cachedDefaultFontId;
626 // There is no valid cached default font for the script.
628 DefaultFonts* defaultFontsPerScript = NULL;
630 // Emojis are present in many monochrome fonts; prefer color by default.
631 const bool preferColor = ( TextAbstraction::EMOJI == script );
633 // Find a fallback-font.
634 fontId = fontClient.FindFallbackFont( currentFontId, character, currentPointSize, preferColor );
638 // If the system does not support a suitable font, fallback to Latin
639 defaultFontsPerScript = *( defaultFontPerScriptCacheBuffer + TextAbstraction::LATIN );
640 if( NULL != defaultFontsPerScript )
642 fontId = defaultFontsPerScript->FindFont( fontClient, currentPointSize );
648 fontId = fontClient.FindDefaultFont( UTF32_A, currentPointSize );
652 if( NULL == defaultFontsPerScript )
654 defaultFontsPerScript = *( defaultFontPerScriptCacheBuffer + script );
656 if( NULL == defaultFontsPerScript )
658 defaultFontsPerScript = new DefaultFonts();
659 *( defaultFontPerScriptCacheBuffer + script ) = defaultFontsPerScript;
662 defaultFontsPerScript->mFonts.PushBack( fontId );
664 } // !isValidFont (3)
665 } // !isValidFont (2)
667 } // !isValidFont (1)
671 Dali::TextAbstraction::FontDescription description;
672 fontClient.GetDescription( fontId, description );
673 DALI_LOG_INFO( gLogFilter,
675 " Validated font set\n Character : %x, Script : %s, Font : %s \n",
677 Dali::TextAbstraction::ScriptName[script],
678 description.path.c_str() );
682 if( isFirstSetToBeValidated && !isCommonScript )
684 currentFontRun.fontId = fontId;
685 isFirstSetToBeValidated = false;
688 // The font is now validated.
689 if( ( fontId != currentFontRun.fontId ) ||
690 isNewParagraphCharacter )
692 // Current run needs to be stored and a new one initialized.
694 if( 0u != currentFontRun.characterRun.numberOfCharacters )
696 // Store the font run.
697 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
701 // Initialize the new one.
702 currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
703 currentFontRun.characterRun.numberOfCharacters = 0u;
704 currentFontRun.fontId = fontId;
706 if( isNewParagraphCharacter )
708 isFirstSetToBeValidated = true;
712 // Add one more character to the run.
713 ++currentFontRun.characterRun.numberOfCharacters;
715 // Whether the current character is a new paragraph character.
716 isNewParagraphCharacter = TextAbstraction::IsNewParagraph( character );
717 previousFontId = currentFontId;
718 isPreviousEmojiScript = isEmojiScript;
719 } // end traverse characters.
721 if( 0u != currentFontRun.characterRun.numberOfCharacters )
723 // Store the last run.
724 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
728 if( fontIndex < fonts.Count() )
730 // Update the indices of the next font runs.
731 const FontRun& run = *( fonts.Begin() + fontIndex - 1u );
732 CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
734 for( Vector<FontRun>::Iterator it = fonts.Begin() + fontIndex,
741 run.characterRun.characterIndex = nextCharacterIndex;
742 nextCharacterIndex += run.characterRun.numberOfCharacters;
746 DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
749 } // namespace Internal
753 } // namespace Toolkit