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 // Count all these characters to be added into a script.
215 ++numberOfAllScriptCharacters;
217 if( TextAbstraction::IsNewParagraph( character ) )
219 // The character is a new paragraph.
220 // To know when there is a new paragraph is needed because if there is a white space
221 // between two scripts with different directions, it is added to the script with
222 // the same direction than the first script of the paragraph.
223 isFirstScriptToBeSet = true;
225 // Characters common to all scripts at the end of the paragraph are added to the last script.
226 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
228 // Store the script run.
229 if( TextAbstraction::UNKNOWN == currentScriptRun.script )
231 currentScriptRun.script = TextAbstraction::LATIN;
233 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
236 // Initialize the new one.
237 currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
238 currentScriptRun.characterRun.numberOfCharacters = 0u;
239 currentScriptRun.script = TextAbstraction::UNKNOWN;
240 numberOfAllScriptCharacters = 0u;
243 // Get the next character.
245 endOfText = index == lastCharacter;
248 character = *( textBuffer + index );
249 script = TextAbstraction::GetCharacterScript( character );
255 // Last characters of the text are 'white spaces'.
256 // There is nothing else to do. Just add the remaining characters to the last script after this bucle.
260 // Check if it is the first character of a paragraph.
261 if( isFirstScriptToBeSet &&
262 ( TextAbstraction::UNKNOWN != script ) &&
263 ( TextAbstraction::COMMON != script ) )
265 // Sets the direction of the first valid script.
266 isParagraphRTL = TextAbstraction::IsRightToLeftScript( script );
267 isFirstScriptToBeSet = false;
270 if( ( script != currentScriptRun.script ) &&
271 ( TextAbstraction::COMMON != script ) )
273 // Current run needs to be stored and a new one initialized.
275 if( ( isParagraphRTL == TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) ) &&
276 ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
278 // Previous script has the same direction than the first script of the paragraph.
279 // All the previously skipped characters need to be added to the previous script before it's stored.
280 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
281 numberOfAllScriptCharacters = 0u;
283 else if( ( TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) == TextAbstraction::IsRightToLeftScript( script ) ) &&
284 ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
286 // Current script and previous one have the same direction.
287 // All the previously skipped characters need to be added to the previous script before it's stored.
288 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
289 numberOfAllScriptCharacters = 0u;
292 if( 0u != currentScriptRun.characterRun.numberOfCharacters )
294 // Store the script run.
295 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
299 // Initialize the new one.
300 currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
301 currentScriptRun.characterRun.numberOfCharacters = numberOfAllScriptCharacters + 1u; // Adds the white spaces which are at the begining of the script.
302 currentScriptRun.script = script;
303 numberOfAllScriptCharacters = 0u;
307 if( TextAbstraction::UNKNOWN != currentScriptRun.script )
309 // Adds white spaces between characters.
310 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
311 numberOfAllScriptCharacters = 0u;
314 // Add one more character to the run.
315 ++currentScriptRun.characterRun.numberOfCharacters;
319 // Add remaining characters into the last script.
320 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
322 if( 0u != currentScriptRun.characterRun.numberOfCharacters )
324 if( TextAbstraction::UNKNOWN == currentScriptRun.script )
326 // There are only white spaces in the last script. Set the latin script.
327 currentScriptRun.script = TextAbstraction::LATIN;
330 // Store the last run.
331 scripts.Insert( scripts.Begin() + scriptIndex, currentScriptRun );
335 if( scriptIndex < scripts.Count() )
337 // Update the indices of the next script runs.
338 const ScriptRun& run = *( scripts.Begin() + scriptIndex - 1u );
339 CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
341 for( Vector<ScriptRun>::Iterator it = scripts.Begin() + scriptIndex,
342 endIt = scripts.End();
346 ScriptRun& run = *it;
347 run.characterRun.characterIndex = nextCharacterIndex;
348 nextCharacterIndex += run.characterRun.numberOfCharacters;
353 void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
354 const Vector<ScriptRun>& scripts,
355 const Vector<FontDescriptionRun>& fontDescriptions,
356 FontId defaultFontId,
357 CharacterIndex startIndex,
358 Length numberOfCharacters,
359 Vector<FontRun>& fonts )
361 DALI_LOG_INFO( gLogFilter, Debug::General, "-->MultilanguageSupport::ValidateFonts\n" );
363 if( 0u == numberOfCharacters )
365 DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
366 // Nothing to do if there are no characters.
370 // Find the first index where to insert the font run.
371 FontRunIndex fontIndex = 0u;
372 if( 0u != startIndex )
374 for( Vector<FontRun>::ConstIterator it = fonts.Begin(),
379 const FontRun& run = *it;
380 if( startIndex < run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
388 // Traverse the characters and validate/set the fonts.
391 DefaultFonts** defaultFontPerScriptCacheBuffer = mDefaultFontPerScriptCache.Begin();
392 ValidateFontsPerScript** validFontsPerScriptCacheBuffer = mValidFontsPerScriptCache.Begin();
394 // Stores the validated font runs.
395 fonts.Reserve( fontDescriptions.Count() );
397 // Initializes a validated font run.
398 FontRun currentFontRun;
399 currentFontRun.characterRun.characterIndex = startIndex;
400 currentFontRun.characterRun.numberOfCharacters = 0u;
401 currentFontRun.fontId = 0u;
403 // Get the font client.
404 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
406 // Get the default font description and default size.
407 TextAbstraction::FontDescription defaultFontDescription;
408 TextAbstraction::PointSize26Dot6 defaultPointSize = TextAbstraction::FontClient::DEFAULT_POINT_SIZE;
409 if( defaultFontId > 0u )
411 fontClient.GetDescription( defaultFontId, defaultFontDescription );
412 defaultPointSize = fontClient.GetPointSize( defaultFontId );
415 // Merge font descriptions
416 Vector<FontId> fontIds;
417 fontIds.Resize( numberOfCharacters, defaultFontId );
418 Vector<bool> isDefaultFont;
419 isDefaultFont.Resize( numberOfCharacters, true );
420 MergeFontDescriptions( fontDescriptions,
423 defaultFontDescription,
426 numberOfCharacters );
428 const Character* const textBuffer = text.Begin();
429 const FontId* const fontIdsBuffer = fontIds.Begin();
430 const bool* const isDefaultFontBuffer = isDefaultFont.Begin();
431 Vector<ScriptRun>::ConstIterator scriptRunIt = scripts.Begin();
432 Vector<ScriptRun>::ConstIterator scriptRunEndIt = scripts.End();
433 bool isNewParagraphCharacter = false;
435 PointSize26Dot6 currentPointSize = defaultPointSize;
436 FontId currentFontId = 0u;
437 FontId previousFontId = 0u;
439 // Whether it's the first set of characters to be validated.
440 // Used in case the paragraph starts with characters common to all scripts.
441 bool isFirstSetToBeValidated = true;
443 CharacterIndex lastCharacter = startIndex + numberOfCharacters;
444 for( Length index = startIndex; index < lastCharacter; ++index )
446 // Get the current character.
447 const Character character = *( textBuffer + index );
449 // Get the font for the current character.
450 FontId fontId = *( fontIdsBuffer + index - startIndex );
452 // Whether the font being validated for the current character is a default one not set by the user.
453 const bool isDefault = *( isDefaultFontBuffer + index - startIndex );
455 // Get the script for the current character.
456 const Script script = GetScript( index,
460 // Get the current point size.
461 if( currentFontId != fontId )
463 currentPointSize = fontClient.GetPointSize( fontId );
464 currentFontId = fontId;
469 Dali::TextAbstraction::FontDescription description;
470 fontClient.GetDescription( fontId, description );
472 DALI_LOG_INFO( gLogFilter,
474 " Initial font set\n Character : %x, Script : %s, Font : %s \n",
476 Dali::TextAbstraction::ScriptName[script],
477 description.path.c_str() );
481 // Validate whether the current character is supported by the given font.
482 bool isValidFont = false;
484 // Check first in the cache of default fonts per script and size.
486 DefaultFonts* defaultFonts = *( defaultFontPerScriptCacheBuffer + script );
487 FontId cachedDefaultFontId = 0u;
488 if( NULL != defaultFonts )
490 cachedDefaultFontId = defaultFonts->FindFont( fontClient, currentPointSize );
493 // Whether the cached default font is valid.
494 const bool isValidCachedDefaultFont = 0u != cachedDefaultFontId;
496 // The font is valid if it matches with the default one for the current script and size and it's different than zero.
497 isValidFont = isValidCachedDefaultFont && ( fontId == cachedDefaultFontId );
499 bool isCommonScript = false;
501 // If the given font is not valid, it means either:
502 // - there is no cached font for the current script yet or,
503 // - the user has set a different font than the default one for the current script or,
504 // - the platform default font is different than the default font for the current script.
506 // Need to check if the given font supports the current character.
507 if( !isValidFont ) // (1)
509 // Whether the current character is common for all scripts (i.e. white spaces, ...)
511 // Is not desirable to cache fonts for the common script.
513 // i.e. Consider the text " हिंदी", the 'white space' has assigned the DEVANAGARI script.
514 // The user may have set a font or the platform's default is used.
516 // As the 'white space' is the first character, no font is cached so the font validation
517 // retrieves a glyph from the given font.
519 // Many fonts support 'white spaces' so probably the font set by the user or the platform's default
520 // supports the 'white space'. However, that font may not support the DEVANAGARI script.
521 isCommonScript = TextAbstraction::IsCommonScript( character );
525 if( isValidCachedDefaultFont &&
526 ( isDefault || ( currentFontId == previousFontId ) ) )
528 // At this point the character common for all scripts has no font assigned.
529 // If there is a valid previously cached default font for it, use that one.
530 fontId = cachedDefaultFontId;
536 // Check in the valid fonts cache.
537 ValidateFontsPerScript* validateFontsPerScript = *( validFontsPerScriptCacheBuffer + script );
539 if( NULL != validateFontsPerScript )
541 isValidFont = validateFontsPerScript->IsValidFont( fontId );
544 if( !isValidFont ) // (2)
546 // Use the font client to validate the font.
547 const GlyphIndex glyphIndex = fontClient.GetGlyphIndex( fontId, character );
549 // The font is valid if there is a glyph for that character.
550 isValidFont = 0u != glyphIndex;
552 // Emojis are present in many monochrome fonts; prefer color by default.
554 ( TextAbstraction::EMOJI == script ) )
556 const BufferImage bitmap = fontClient.CreateBitmap( fontId, glyphIndex );
558 // For color emojis, the font is valid if the bitmap is RGBA.
559 isValidFont = bitmap && ( Pixel::BGRA8888 == bitmap.GetPixelFormat() );
562 // If there is a valid font, cache it.
565 if( NULL == validateFontsPerScript )
567 validateFontsPerScript = new ValidateFontsPerScript();
569 *( validFontsPerScriptCacheBuffer + script ) = validateFontsPerScript;
572 validateFontsPerScript->mValidFonts.PushBack( fontId );
575 if( !isValidFont ) // (3)
577 // The given font has not been validated.
579 if( isValidCachedDefaultFont )
581 // Use the cached default font for the script if there is one.
582 fontId = cachedDefaultFontId;
586 // There is no valid cached default font for the script.
588 DefaultFonts* defaultFontsPerScript = NULL;
590 // Emojis are present in many monochrome fonts; prefer color by default.
591 const bool preferColor = ( TextAbstraction::EMOJI == script );
593 // Find a fallback-font.
594 fontId = fontClient.FindFallbackFont( currentFontId, character, currentPointSize, preferColor );
598 // If the system does not support a suitable font, fallback to Latin
599 defaultFontsPerScript = *( defaultFontPerScriptCacheBuffer + TextAbstraction::LATIN );
600 if( NULL != defaultFontsPerScript )
602 fontId = defaultFontsPerScript->FindFont( fontClient, currentPointSize );
608 fontId = fontClient.FindDefaultFont( UTF32_A, currentPointSize );
612 if( NULL == defaultFontsPerScript )
614 defaultFontsPerScript = new DefaultFonts();
615 *( defaultFontPerScriptCacheBuffer + script ) = defaultFontsPerScript;
617 defaultFontsPerScript->mFonts.PushBack( fontId );
619 } // !isValidFont (3)
620 } // !isValidFont (2)
622 } // !isValidFont (1)
626 Dali::TextAbstraction::FontDescription description;
627 fontClient.GetDescription( fontId, description );
628 DALI_LOG_INFO( gLogFilter,
630 " Validated font set\n Character : %x, Script : %s, Font : %s \n",
632 Dali::TextAbstraction::ScriptName[script],
633 description.path.c_str() );
637 if( isFirstSetToBeValidated && !isCommonScript )
639 currentFontRun.fontId = fontId;
640 isFirstSetToBeValidated = false;
643 // The font is now validated.
644 if( ( fontId != currentFontRun.fontId ) ||
645 isNewParagraphCharacter )
647 // Current run needs to be stored and a new one initialized.
649 if( 0u != currentFontRun.characterRun.numberOfCharacters )
651 // Store the font run.
652 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
656 // Initialize the new one.
657 currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
658 currentFontRun.characterRun.numberOfCharacters = 0u;
659 currentFontRun.fontId = fontId;
661 if( isNewParagraphCharacter )
663 isFirstSetToBeValidated = true;
667 // Add one more character to the run.
668 ++currentFontRun.characterRun.numberOfCharacters;
670 // Whether the current character is a new paragraph character.
671 isNewParagraphCharacter = TextAbstraction::IsNewParagraph( character );
672 previousFontId = currentFontId;
673 } // end traverse characters.
675 if( 0u != currentFontRun.characterRun.numberOfCharacters )
677 // Store the last run.
678 fonts.Insert( fonts.Begin() + fontIndex, currentFontRun );
682 if( fontIndex < fonts.Count() )
684 // Update the indices of the next font runs.
685 const FontRun& run = *( fonts.Begin() + fontIndex - 1u );
686 CharacterIndex nextCharacterIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters;
688 for( Vector<FontRun>::Iterator it = fonts.Begin() + fontIndex,
695 run.characterRun.characterIndex = nextCharacterIndex;
696 nextCharacterIndex += run.characterRun.numberOfCharacters;
700 DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
703 } // namespace Internal
707 } // namespace Toolkit