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,
67 const TextAbstraction::FontDescription& description,
68 PointSize26Dot6 size ) const
70 for( Vector<FontId>::ConstIterator it = mFonts.Begin(),
75 const FontId fontId = *it;
76 TextAbstraction::FontDescription fontDescription;
77 fontClient.GetDescription( fontId, fontDescription );
79 if( ( size == fontClient.GetPointSize( fontId ) ) &&
80 ( description.weight == fontDescription.weight ) &&
81 ( description.width == fontDescription.width ) &&
82 ( description.slant == fontDescription.slant ) )
91 MultilanguageSupport::MultilanguageSupport()
92 : mDefaultFontPerScriptCache(),
93 mValidFontsPerScriptCache()
95 // Initializes the default font cache to zero (invalid font).
96 // Reserves space to cache the default fonts and access them with the script as an index.
97 mDefaultFontPerScriptCache.Resize( TextAbstraction::UNKNOWN, NULL );
99 // Initializes the valid fonts cache to NULL (no valid fonts).
100 // Reserves space to cache the valid fonts and access them with the script as an index.
101 mValidFontsPerScriptCache.Resize( TextAbstraction::UNKNOWN, NULL );
104 MultilanguageSupport::~MultilanguageSupport()
106 // Destroy the default font per script cache.
107 for( Vector<DefaultFonts*>::Iterator it = mDefaultFontPerScriptCache.Begin(),
108 endIt = mDefaultFontPerScriptCache.End();
115 // Destroy the valid fonts per script cache.
116 for( Vector<ValidateFontsPerScript*>::Iterator it = mValidFontsPerScriptCache.Begin(),
117 endIt = mValidFontsPerScriptCache.End();
125 Text::MultilanguageSupport MultilanguageSupport::Get()
127 Text::MultilanguageSupport multilanguageSupportHandle;
129 SingletonService service( SingletonService::Get() );
132 // Check whether the singleton is already created
133 Dali::BaseHandle handle = service.GetSingleton( typeid( Text::MultilanguageSupport ) );
136 // If so, downcast the handle
137 MultilanguageSupport* impl = dynamic_cast< Internal::MultilanguageSupport* >( handle.GetObjectPtr() );
138 multilanguageSupportHandle = Text::MultilanguageSupport( impl );
140 else // create and register the object
142 multilanguageSupportHandle = Text::MultilanguageSupport( new MultilanguageSupport );
143 service.Register( typeid( multilanguageSupportHandle ), multilanguageSupportHandle );
147 return multilanguageSupportHandle;
150 void MultilanguageSupport::SetScripts( const Vector<Character>& text,
151 CharacterIndex startIndex,
152 Length numberOfCharacters,
153 Vector<ScriptRun>& scripts )
155 if( 0u == numberOfCharacters )
157 // Nothing to do if there are no characters.
161 // Find the first index where to insert the script.
162 ScriptRunIndex scriptIndex = 0u;
163 if( 0u != startIndex )
165 for( Vector<ScriptRun>::ConstIterator it = scripts.Begin(),
166 endIt = scripts.End();
168 ++it, ++scriptIndex )
170 const ScriptRun& run = *it;
171 if( startIndex < run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
179 // Stores the current script run.
180 ScriptRun currentScriptRun;
181 currentScriptRun.characterRun.characterIndex = startIndex;
182 currentScriptRun.characterRun.numberOfCharacters = 0u;
183 currentScriptRun.script = TextAbstraction::UNKNOWN;
185 // Reserve some space to reduce the number of reallocations.
186 scripts.Reserve( text.Count() << 2u );
188 // Whether the first valid script needs to be set.
189 bool isFirstScriptToBeSet = true;
191 // Whether the first valid script is a right to left script.
192 bool isParagraphRTL = false;
194 // Count the number of characters which are valid for all scripts. i.e. white spaces or '\n'.
195 Length numberOfAllScriptCharacters = 0u;
197 // Pointers to the text buffer.
198 const Character* const textBuffer = text.Begin();
200 // Traverse all characters and set the scripts.
201 const Length lastCharacter = startIndex + numberOfCharacters;
202 for( Length index = startIndex; index < lastCharacter; ++index )
204 Character character = *( textBuffer + index );
206 // Get the script of the character.
207 Script script = TextAbstraction::GetCharacterScript( character );
209 // Some characters (like white spaces) are valid for many scripts. The rules to set a script
211 // - If they are at the begining of a paragraph they get the script of the first character with
212 // a defined script. If they are at the end, they get the script of the last one.
213 // - If they are between two scripts with the same direction, they get the script of the previous
214 // character with a defined script. If the two scripts have different directions, they get the
215 // script of the first character of the paragraph with a defined script.
217 // Skip those characters valid for many scripts like white spaces or '\n'.
218 bool endOfText = index == lastCharacter;
220 ( TextAbstraction::COMMON == script ) )
222 if( TextAbstraction::EMOJI == currentScriptRun.script )
224 // Emojis doesn't mix well with characters common to all scripts. Insert the emoji run.
225 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
228 // Initialize the new one.
229 currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
230 currentScriptRun.characterRun.numberOfCharacters = 0u;
231 currentScriptRun.script = TextAbstraction::UNKNOWN;
232 numberOfAllScriptCharacters = 0u;
235 // Count all these characters to be added into a script.
236 ++numberOfAllScriptCharacters;
238 if( TextAbstraction::IsNewParagraph( character ) )
240 // The character is a new paragraph.
241 // To know when there is a new paragraph is needed because if there is a white space
242 // between two scripts with different directions, it is added to the script with
243 // the same direction than the first script of the paragraph.
244 isFirstScriptToBeSet = true;
246 // Characters common to all scripts at the end of the paragraph are added to the last script.
247 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
249 // Store the script run.
250 if( TextAbstraction::UNKNOWN == currentScriptRun.script )
252 currentScriptRun.script = TextAbstraction::LATIN;
254 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
257 // Initialize the new one.
258 currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
259 currentScriptRun.characterRun.numberOfCharacters = 0u;
260 currentScriptRun.script = TextAbstraction::UNKNOWN;
261 numberOfAllScriptCharacters = 0u;
264 // Get the next character.
266 endOfText = index == lastCharacter;
269 character = *( textBuffer + index );
270 script = TextAbstraction::GetCharacterScript( character );
272 } // end while( !endOfText && ( TextAbstraction::COMMON == script ) )
276 // Last characters of the text are 'white spaces'.
277 // There is nothing else to do. Just add the remaining characters to the last script after this bucle.
281 // Check if it is the first character of a paragraph.
282 if( isFirstScriptToBeSet &&
283 ( TextAbstraction::UNKNOWN != script ) &&
284 ( TextAbstraction::COMMON != script ) &&
285 ( TextAbstraction::EMOJI != script ) )
287 // Sets the direction of the first valid script.
288 isParagraphRTL = TextAbstraction::IsRightToLeftScript( script );
289 isFirstScriptToBeSet = false;
292 if( ( script != currentScriptRun.script ) &&
293 ( TextAbstraction::COMMON != script ) )
295 // Current run needs to be stored and a new one initialized.
297 if( ( isParagraphRTL == TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) ) &&
298 ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
300 // Previous script has the same direction than the first script of the paragraph.
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::IsRightToLeftScript( currentScriptRun.script ) == TextAbstraction::IsRightToLeftScript( script ) ) &&
306 ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
308 // Current script and previous one have the same direction.
309 // All the previously skipped characters need to be added to the previous script before it's stored.
310 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
311 numberOfAllScriptCharacters = 0u;
313 else if( ( TextAbstraction::UNKNOWN == currentScriptRun.script ) &&
314 ( TextAbstraction::EMOJI == script ) )
316 currentScriptRun.script = TextAbstraction::LATIN;
317 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
318 numberOfAllScriptCharacters = 0u;
321 if( 0u != currentScriptRun.characterRun.numberOfCharacters )
323 // Store the script run.
324 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
328 // Initialize the new one.
329 currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
330 currentScriptRun.characterRun.numberOfCharacters = numberOfAllScriptCharacters + 1u; // Adds the white spaces which are at the begining of the script.
331 currentScriptRun.script = script;
332 numberOfAllScriptCharacters = 0u;
336 if( TextAbstraction::UNKNOWN != currentScriptRun.script )
338 // Adds white spaces between characters.
339 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
340 numberOfAllScriptCharacters = 0u;
343 // Add one more character to the run.
344 ++currentScriptRun.characterRun.numberOfCharacters;
348 // Add remaining characters into the last script.
349 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
351 if( 0u != currentScriptRun.characterRun.numberOfCharacters )
353 if( TextAbstraction::UNKNOWN == currentScriptRun.script )
355 // There are only white spaces in the last script. Set the latin script.
356 currentScriptRun.script = TextAbstraction::LATIN;
359 // Store the last run.
360 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
364 if( scriptIndex < scripts.Count() )
366 // Update the indices of the next script runs.
367 const ScriptRun& run = *( scripts.Begin() + scriptIndex - 1u );
368 CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
370 for( Vector<ScriptRun>::Iterator it = scripts.Begin() + scriptIndex,
371 endIt = scripts.End();
375 ScriptRun& run = *it;
376 run.characterRun.characterIndex = nextCharacterIndex;
377 nextCharacterIndex += run.characterRun.numberOfCharacters;
382 void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
383 const Vector<ScriptRun>& scripts,
384 const Vector<FontDescriptionRun>& fontDescriptions,
385 const TextAbstraction::FontDescription& defaultFontDescription,
386 TextAbstraction::PointSize26Dot6 defaultFontPointSize,
387 CharacterIndex startIndex,
388 Length numberOfCharacters,
389 Vector<FontRun>& fonts )
391 DALI_LOG_INFO( gLogFilter, Debug::General, "-->MultilanguageSupport::ValidateFonts\n" );
393 if( 0u == numberOfCharacters )
395 DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
396 // Nothing to do if there are no characters.
400 // Find the first index where to insert the font run.
401 FontRunIndex fontIndex = 0u;
402 if( 0u != startIndex )
404 for( Vector<FontRun>::ConstIterator it = fonts.Begin(),
409 const FontRun& run = *it;
410 if( startIndex < run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
418 // Traverse the characters and validate/set the fonts.
421 DefaultFonts** defaultFontPerScriptCacheBuffer = mDefaultFontPerScriptCache.Begin();
422 ValidateFontsPerScript** validFontsPerScriptCacheBuffer = mValidFontsPerScriptCache.Begin();
424 // Stores the validated font runs.
425 fonts.Reserve( fontDescriptions.Count() );
427 // Initializes a validated font run.
428 FontRun currentFontRun;
429 currentFontRun.characterRun.characterIndex = startIndex;
430 currentFontRun.characterRun.numberOfCharacters = 0u;
431 currentFontRun.fontId = 0u;
433 // Get the font client.
434 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
436 const Character* const textBuffer = text.Begin();
438 // Iterators of the script runs.
439 Vector<ScriptRun>::ConstIterator scriptRunIt = scripts.Begin();
440 Vector<ScriptRun>::ConstIterator scriptRunEndIt = scripts.End();
441 bool isNewParagraphCharacter = false;
443 FontId currentFontId = 0u;
444 FontId previousFontId = 0u;
445 bool isPreviousEmojiScript = false;
447 // Whether it's the first set of characters to be validated.
448 // Used in case the paragraph starts with characters common to all scripts.
449 bool isFirstSetToBeValidated = true;
451 CharacterIndex lastCharacter = startIndex + numberOfCharacters;
452 for( Length index = startIndex; index < lastCharacter; ++index )
454 // Get the current character.
455 const Character character = *( textBuffer + index );
457 TextAbstraction::FontDescription currentFontDescription;
458 TextAbstraction::PointSize26Dot6 currentFontPointSize = defaultFontPointSize;
459 bool isDefaultFont = true;
460 MergeFontDescriptions( fontDescriptions,
461 defaultFontDescription,
462 defaultFontPointSize,
464 currentFontDescription,
465 currentFontPointSize,
468 // Get the font for the current character.
469 FontId fontId = fontClient.GetFontId( currentFontDescription, currentFontPointSize );
470 currentFontId = fontId;
472 // Get the script for the current character.
473 const Script script = GetScript( index,
479 Dali::TextAbstraction::FontDescription description;
480 fontClient.GetDescription( fontId, description );
482 DALI_LOG_INFO( gLogFilter,
484 " Initial font set\n Character : %x, Script : %s, Font : %s \n",
486 Dali::TextAbstraction::ScriptName[script],
487 description.path.c_str() );
491 // Validate whether the current character is supported by the given font.
492 bool isValidFont = false;
494 // Check first in the cache of default fonts per script and size.
496 DefaultFonts* defaultFonts = *( defaultFontPerScriptCacheBuffer + script );
497 FontId cachedDefaultFontId = 0u;
498 if( NULL != defaultFonts )
500 cachedDefaultFontId = defaultFonts->FindFont( fontClient,
501 currentFontDescription,
502 currentFontPointSize );
505 // Whether the cached default font is valid.
506 const bool isValidCachedDefaultFont = 0u != cachedDefaultFontId;
508 // The font is valid if it matches with the default one for the current script and size and it's different than zero.
509 isValidFont = isValidCachedDefaultFont && ( fontId == cachedDefaultFontId );
511 bool isCommonScript = false;
512 bool isEmojiScript = TextAbstraction::EMOJI == script;
514 if( isEmojiScript && !isPreviousEmojiScript )
516 if( 0u != currentFontRun.characterRun.numberOfCharacters )
518 // Store the font run.
519 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
523 // Initialize the new one.
524 currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
525 currentFontRun.characterRun.numberOfCharacters = 0u;
526 currentFontRun.fontId = fontId;
530 // If the given font is not valid, it means either:
531 // - there is no cached font for the current script yet or,
532 // - the user has set a different font than the default one for the current script or,
533 // - the platform default font is different than the default font for the current script.
535 // Need to check if the given font supports the current character.
536 if( !isValidFont ) // (1)
538 // Whether the current character is common for all scripts (i.e. white spaces, ...)
540 // Is not desirable to cache fonts for the common script.
542 // i.e. Consider the text " हिंदी", the 'white space' has assigned the DEVANAGARI script.
543 // The user may have set a font or the platform's default is used.
545 // As the 'white space' is the first character, no font is cached so the font validation
546 // retrieves a glyph from the given font.
548 // Many fonts support 'white spaces' so probably the font set by the user or the platform's default
549 // supports the 'white space'. However, that font may not support the DEVANAGARI script.
550 isCommonScript = TextAbstraction::IsCommonScript( character );
554 if( isValidCachedDefaultFont &&
555 ( isDefaultFont || ( currentFontId == previousFontId ) ) &&
558 // At this point the character common for all scripts has no font assigned.
559 // If there is a valid previously cached default font for it, use that one.
560 fontId = cachedDefaultFontId;
566 // Check in the valid fonts cache.
567 ValidateFontsPerScript* validateFontsPerScript = *( validFontsPerScriptCacheBuffer + script );
569 if( NULL != validateFontsPerScript )
571 isValidFont = validateFontsPerScript->IsValidFont( fontId );
574 if( !isValidFont ) // (2)
576 // Use the font client to validate the font.
577 const GlyphIndex glyphIndex = fontClient.GetGlyphIndex( fontId, character );
579 // The font is valid if there is a glyph for that character.
580 isValidFont = 0u != glyphIndex;
582 // Emojis are present in many monochrome fonts; prefer color by default.
586 const PixelData bitmap = fontClient.CreateBitmap( fontId, glyphIndex );
588 // For color emojis, the font is valid if the bitmap is RGBA.
589 isValidFont = bitmap && ( Pixel::BGRA8888 == bitmap.GetPixelFormat() );
592 // If there is a valid font, cache it.
595 if( NULL == validateFontsPerScript )
597 validateFontsPerScript = new ValidateFontsPerScript();
599 *( validFontsPerScriptCacheBuffer + script ) = validateFontsPerScript;
602 validateFontsPerScript->mValidFonts.PushBack( fontId );
605 if( !isValidFont ) // (3)
607 // The given font has not been validated.
609 if( isValidCachedDefaultFont )
611 // Use the cached default font for the script if there is one.
612 fontId = cachedDefaultFontId;
616 // There is no valid cached default font for the script.
618 DefaultFonts* defaultFontsPerScript = NULL;
620 // Emojis are present in many monochrome fonts; prefer color by default.
621 const bool preferColor = ( TextAbstraction::EMOJI == script );
623 // Find a fallback-font.
624 fontId = fontClient.FindFallbackFont( character,
625 currentFontDescription,
626 currentFontPointSize,
631 // If the system does not support a suitable font, fallback to Latin
632 defaultFontsPerScript = *( defaultFontPerScriptCacheBuffer + TextAbstraction::LATIN );
633 if( NULL != defaultFontsPerScript )
635 fontId = defaultFontsPerScript->FindFont( fontClient,
636 currentFontDescription,
637 currentFontPointSize );
643 fontId = fontClient.FindDefaultFont( UTF32_A, currentFontPointSize );
647 if( NULL == defaultFontsPerScript )
649 defaultFontsPerScript = *( defaultFontPerScriptCacheBuffer + script );
651 if( NULL == defaultFontsPerScript )
653 defaultFontsPerScript = new DefaultFonts();
654 *( defaultFontPerScriptCacheBuffer + script ) = defaultFontsPerScript;
657 defaultFontsPerScript->mFonts.PushBack( fontId );
659 } // !isValidFont (3)
660 } // !isValidFont (2)
662 } // !isValidFont (1)
666 Dali::TextAbstraction::FontDescription description;
667 fontClient.GetDescription( fontId, description );
668 DALI_LOG_INFO( gLogFilter,
670 " Validated font set\n Character : %x, Script : %s, Font : %s \n",
672 Dali::TextAbstraction::ScriptName[script],
673 description.path.c_str() );
677 if( isFirstSetToBeValidated && !isCommonScript )
679 currentFontRun.fontId = fontId;
680 isFirstSetToBeValidated = false;
683 // The font is now validated.
684 if( ( fontId != currentFontRun.fontId ) ||
685 isNewParagraphCharacter )
687 // Current run needs to be stored and a new one initialized.
689 if( 0u != currentFontRun.characterRun.numberOfCharacters )
691 // Store the font run.
692 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
696 // Initialize the new one.
697 currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
698 currentFontRun.characterRun.numberOfCharacters = 0u;
699 currentFontRun.fontId = fontId;
701 if( isNewParagraphCharacter )
703 isFirstSetToBeValidated = true;
707 // Add one more character to the run.
708 ++currentFontRun.characterRun.numberOfCharacters;
710 // Whether the current character is a new paragraph character.
711 isNewParagraphCharacter = TextAbstraction::IsNewParagraph( character );
712 previousFontId = currentFontId;
713 isPreviousEmojiScript = isEmojiScript;
714 } // end traverse characters.
716 if( 0u != currentFontRun.characterRun.numberOfCharacters )
718 // Store the last run.
719 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
723 if( fontIndex < fonts.Count() )
725 // Update the indices of the next font runs.
726 const FontRun& run = *( fonts.Begin() + fontIndex - 1u );
727 CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
729 for( Vector<FontRun>::Iterator it = fonts.Begin() + fontIndex,
736 run.characterRun.characterIndex = nextCharacterIndex;
737 nextCharacterIndex += run.characterRun.numberOfCharacters;
741 DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
744 } // namespace Internal
748 } // namespace Toolkit