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>
34 #if defined(DEBUG_ENABLED)
35 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_MULTI_LANGUAGE_SUPPORT");
38 const Dali::Toolkit::Text::Character UTF32_A = 0x0041;
48 * @brief Retrieves the font Id from the font run for a given character's @p index.
50 * If the character's index exceeds the current font run it increases the iterator to get the next one.
52 * @param[in] index The character's index.
53 * @param[in,out] fontRunIt Iterator to the current font run.
54 * @param[in] fontRunEndIt Iterator to one after the last font run.
56 * @return The font id.
58 FontId GetFontId( Length index,
59 Vector<FontRun>::ConstIterator& fontRunIt,
60 const Vector<FontRun>::ConstIterator& fontRunEndIt )
64 if( fontRunIt != fontRunEndIt )
66 const FontRun& fontRun = *fontRunIt;
68 if( ( index >= fontRun.characterRun.characterIndex ) &&
69 ( index < fontRun.characterRun.characterIndex + fontRun.characterRun.numberOfCharacters ) )
71 fontId = fontRun.fontId;
74 if( index + 1u == fontRun.characterRun.characterIndex + fontRun.characterRun.numberOfCharacters )
76 // All the characters of the current run have been traversed. Get the next one for the next iteration.
85 * @brief Retrieves the script Id from the script run for a given character's @p index.
87 * If the character's index exceeds the current script run it increases the iterator to get the next one.
89 * @param[in] index The character's index.
90 * @param[in,out] scriptRunIt Iterator to the current font run.
91 * @param[in] scriptRunEndIt Iterator to one after the last script run.
95 Script GetScript( Length index,
96 Vector<ScriptRun>::ConstIterator& scriptRunIt,
97 const Vector<ScriptRun>::ConstIterator& scriptRunEndIt )
99 Script script = TextAbstraction::UNKNOWN;
101 if( scriptRunIt != scriptRunEndIt )
103 const ScriptRun& scriptRun = *scriptRunIt;
105 if( ( index >= scriptRun.characterRun.characterIndex ) &&
106 ( index < scriptRun.characterRun.characterIndex + scriptRun.characterRun.numberOfCharacters ) )
108 script = scriptRun.script;
111 if( index + 1u == scriptRun.characterRun.characterIndex + scriptRun.characterRun.numberOfCharacters )
113 // All the characters of the current run have been traversed. Get the next one for the next iteration.
121 bool ValidateFontsPerScript::FindValidFont( FontId fontId ) const
123 for( Vector<FontId>::ConstIterator it = mValidFonts.Begin(),
124 endIt = mValidFonts.End();
137 MultilanguageSupport::MultilanguageSupport()
138 : mDefaultFontPerScriptCache(),
139 mValidFontsPerScriptCache()
141 // Initializes the default font cache to zero (invalid font).
142 // Reserves space to cache the default fonts and access them with the script as an index.
143 mDefaultFontPerScriptCache.Resize( TextAbstraction::UNKNOWN, 0u );
145 // Initializes the valid fonts cache to NULL (no valid fonts).
146 // Reserves space to cache the valid fonts and access them with the script as an index.
147 mValidFontsPerScriptCache.Resize( TextAbstraction::UNKNOWN, NULL );
150 MultilanguageSupport::~MultilanguageSupport()
152 // Destroy the valid fonts per script cache.
154 for( Vector<ValidateFontsPerScript*>::Iterator it = mValidFontsPerScriptCache.Begin(),
155 endIt = mValidFontsPerScriptCache.End();
163 Text::MultilanguageSupport MultilanguageSupport::Get()
165 Text::MultilanguageSupport multilanguageSupportHandle;
167 SingletonService service( SingletonService::Get() );
170 // Check whether the singleton is already created
171 Dali::BaseHandle handle = service.GetSingleton( typeid( Text::MultilanguageSupport ) );
174 // If so, downcast the handle
175 MultilanguageSupport* impl = dynamic_cast< Internal::MultilanguageSupport* >( handle.GetObjectPtr() );
176 multilanguageSupportHandle = Text::MultilanguageSupport( impl );
178 else // create and register the object
180 multilanguageSupportHandle = Text::MultilanguageSupport( new MultilanguageSupport );
181 service.Register( typeid( multilanguageSupportHandle ), multilanguageSupportHandle );
185 return multilanguageSupportHandle;
188 void MultilanguageSupport::SetScripts( const Vector<Character>& text,
189 Vector<ScriptRun>& scripts )
191 const Length numberOfCharacters = text.Count();
193 if( 0u == numberOfCharacters )
195 // Nothing to do if there are no characters.
199 // Stores the current script run.
200 ScriptRun currentScriptRun;
201 currentScriptRun.characterRun.characterIndex = 0u;
202 currentScriptRun.characterRun.numberOfCharacters = 0u;
203 currentScriptRun.script = TextAbstraction::UNKNOWN;
205 // Reserve some space to reduce the number of reallocations.
206 scripts.Reserve( numberOfCharacters << 2u );
208 // Whether the first valid script needs to be set.
209 bool isFirstScriptToBeSet = true;
211 // Whether the first valid script is a right to left script.
212 bool isParagraphRTL = false;
214 // Count the number of characters which are valid for all scripts. i.e. white spaces or '\n'.
215 Length numberOfAllScriptCharacters = 0u;
217 // Pointers to the text and break info buffers.
218 const Character* const textBuffer = text.Begin();
220 // Traverse all characters and set the scripts.
221 for( Length index = 0u; index < numberOfCharacters; ++index )
223 Character character = *( textBuffer + index );
225 // Get the script of the character.
226 Script script = TextAbstraction::GetCharacterScript( character );
228 // Some characters (like white spaces) are valid for many scripts. The rules to set a script
230 // - If they are at the begining of a paragraph they get the script of the first character with
231 // a defined script. If they are at the end, they get the script of the last one.
232 // - If they are between two scripts with the same direction, they get the script of the previous
233 // character with a defined script. If the two scripts have different directions, they get the
234 // script of the first character of the paragraph with a defined script.
236 // Skip those characters valid for many scripts like white spaces or '\n'.
237 bool endOfText = index == numberOfCharacters;
239 ( TextAbstraction::COMMON == script ) )
241 // Count all these characters to be added into a script.
242 ++numberOfAllScriptCharacters;
244 if( TextAbstraction::IsNewParagraph( character ) )
246 // The character is a new paragraph.
247 // To know when there is a new paragraph is needed because if there is a white space
248 // between two scripts with different directions, it is added to the script with
249 // the same direction than the first script of the paragraph.
250 isFirstScriptToBeSet = true;
252 // Characters common to all scripts at the end of the paragraph are added to the last script (if the last one is not unknown).
253 if( TextAbstraction::UNKNOWN != currentScriptRun.script )
255 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
256 numberOfAllScriptCharacters = 0u;
260 // Get the next character.
262 endOfText = index == numberOfCharacters;
265 character = *( textBuffer + index );
266 script = TextAbstraction::GetCharacterScript( character );
272 // Last characters of the text are 'white spaces'.
273 // There is nothing else to do. Just add the remaining characters to the last script after this bucle.
277 // Check if it is the first character of a paragraph.
278 if( isFirstScriptToBeSet &&
279 ( TextAbstraction::UNKNOWN != script ) &&
280 ( TextAbstraction::COMMON != script ) )
282 // Sets the direction of the first valid script.
283 isParagraphRTL = TextAbstraction::IsRightToLeftScript( script );
284 isFirstScriptToBeSet = false;
287 if( ( script != currentScriptRun.script ) &&
288 ( TextAbstraction::COMMON != script ) )
290 // Current run needs to be stored and a new one initialized.
292 if( ( isParagraphRTL == TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) ) &&
293 ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
295 // Previous script has the same direction than the first script of the paragraph.
296 // All the previously skipped characters need to be added to the previous script before it's stored.
297 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
298 numberOfAllScriptCharacters = 0u;
300 else if( ( TextAbstraction::IsRightToLeftScript( currentScriptRun.script ) == TextAbstraction::IsRightToLeftScript( script ) ) &&
301 ( TextAbstraction::UNKNOWN != currentScriptRun.script ) )
303 // Current script and previous one have the same direction.
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;
309 if( 0u != currentScriptRun.characterRun.numberOfCharacters )
311 // Store the script run.
312 scripts.PushBack( currentScriptRun );
315 // Initialize the new one.
316 currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
317 currentScriptRun.characterRun.numberOfCharacters = numberOfAllScriptCharacters + 1u; // Adds the white spaces which are at the begining of the script.
318 currentScriptRun.script = script;
319 numberOfAllScriptCharacters = 0u;
323 if( TextAbstraction::UNKNOWN != currentScriptRun.script )
325 // Adds white spaces between characters.
326 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
327 numberOfAllScriptCharacters = 0u;
330 // Add one more character to the run.
331 ++currentScriptRun.characterRun.numberOfCharacters;
335 // Add remaining characters into the last script.
336 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
338 DALI_ASSERT_DEBUG( ( 0u != currentScriptRun.characterRun.numberOfCharacters ) && "MultilanguageSupport::SetScripts() Trying to insert a script run with zero characters." );
340 if( TextAbstraction::UNKNOWN == currentScriptRun.script )
342 // There are only white spaces in the last script. Set the latin script.
343 currentScriptRun.script = TextAbstraction::LATIN;
346 // Store the last run.
347 scripts.PushBack( currentScriptRun );
350 void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
351 const Vector<ScriptRun>& scripts,
352 Vector<FontRun>& fonts )
354 DALI_LOG_INFO( gLogFilter, Debug::General, "-->MultilanguageSupport::ValidateFonts\n" );
355 const Length numberOfCharacters = text.Count();
357 if( 0u == numberOfCharacters )
359 DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
360 // Nothing to do if there are no characters.
364 // Copy the fonts set by application developers.
365 const Length numberOfFontRuns = fonts.Count();
366 const Vector<FontRun> userSetFonts = fonts;
369 // Traverse the characters and validate/set the fonts.
372 FontId* defaultFontPerScriptCacheBuffer = mDefaultFontPerScriptCache.Begin();
373 ValidateFontsPerScript** validFontsPerScriptCacheBuffer = mValidFontsPerScriptCache.Begin();
375 // Stores the validated font runs.
376 fonts.Reserve( numberOfFontRuns );
378 // Initializes a validated font run.
379 FontRun currentFontRun;
380 currentFontRun.characterRun.characterIndex = 0u;
381 currentFontRun.characterRun.numberOfCharacters = 0u;
382 currentFontRun.fontId = 0u;
383 currentFontRun.isDefault = false;
385 // Get the font client.
386 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
388 // Iterators of the font and script runs.
389 Vector<FontRun>::ConstIterator fontRunIt = userSetFonts.Begin();
390 Vector<FontRun>::ConstIterator fontRunEndIt = userSetFonts.End();
391 Vector<ScriptRun>::ConstIterator scriptRunIt = scripts.Begin();
392 Vector<ScriptRun>::ConstIterator scriptRunEndIt = scripts.End();
394 for( Length index = 0u; index < numberOfCharacters; ++index )
396 // Get the character.
397 const Character character = *( text.Begin() + index );
399 // Get the font for the character.
400 FontId fontId = GetFontId( index,
404 // Get the script for the character.
405 Script script = GetScript( index,
411 Dali::TextAbstraction::FontDescription description;
412 fontClient.GetDescription( fontId, description );
414 DALI_LOG_INFO( gLogFilter,
416 " Initial font set\n Character : %x, Script : %s, Font : %s \n",
418 Dali::TextAbstraction::ScriptName[script],
419 description.path.c_str() );
423 if( TextAbstraction::UNKNOWN == script )
425 DALI_LOG_WARNING( "MultilanguageSupport::ValidateFonts. Unknown script!" );
426 script = TextAbstraction::LATIN;
429 // Whether the font being validated is a default one not set by the user.
430 const bool isDefault = ( 0u == fontId );
431 FontId preferredFont = fontId;
433 DALI_LOG_INFO( gLogFilter,
435 " Is a default font : %s\n",
436 ( isDefault ? "true" : "false" ) );
438 // The default font point size.
439 PointSize26Dot6 pointSize = TextAbstraction::FontClient::DEFAULT_POINT_SIZE;
443 // Validate if the font set by the user supports the character.
445 // Check first in the caches.
447 // The user may have set the default font. Check it. Otherwise check in the valid fonts cache.
448 if( fontId != *( defaultFontPerScriptCacheBuffer + script ) )
450 // Check in the valid fonts cache.
451 ValidateFontsPerScript* validateFontsPerScript = *( validFontsPerScriptCacheBuffer + script );
453 if( NULL == validateFontsPerScript )
455 validateFontsPerScript = new ValidateFontsPerScript();
457 *( validFontsPerScriptCacheBuffer + script ) = validateFontsPerScript;
460 if( NULL != validateFontsPerScript )
462 if( !validateFontsPerScript->FindValidFont( fontId ) )
464 // Use the font client to validate the font.
465 GlyphIndex glyphIndex = fontClient.GetGlyphIndex( fontId, character );
467 // Emojis are present in many monochrome fonts; prefer color by default.
468 if( TextAbstraction::EMOJI == script &&
471 BufferImage bitmap = fontClient.CreateBitmap( fontId, glyphIndex );
473 Pixel::BGRA8888 != bitmap.GetPixelFormat() )
479 if( 0u == glyphIndex )
481 // Get the point size of the current font. It will be used to get a default font id.
482 pointSize = fontClient.GetPointSize( fontId );
484 // The font is not valid. Set to zero and a default one will be set.
489 // Add the font to the valid font cache.
491 // At this point the validated font supports the given character. However, characters
492 // common for all scripts, like white spaces or new paragraph characters, need to be
493 // processed differently.
495 // i.e. A white space can have assigned a DEVANAGARI script but the font assigned may not
496 // support none of the DEVANAGARI glyphs. This font can't be added to the cache as a valid
497 // font for the DEVANAGARI script but the COMMON one.
498 if( TextAbstraction::IsCommonScript( character ) )
500 validateFontsPerScript = *( validFontsPerScriptCacheBuffer + TextAbstraction::COMMON );
502 if( NULL == validateFontsPerScript )
504 validateFontsPerScript = new ValidateFontsPerScript();
506 *( validFontsPerScriptCacheBuffer + TextAbstraction::COMMON ) = validateFontsPerScript;
510 validateFontsPerScript->mValidFonts.PushBack( fontId );
517 // The font has not been validated. Find a default one.
520 // The character has no font assigned. Get a default one from the cache
521 fontId = *( defaultFontPerScriptCacheBuffer + script );
523 // If the cache has not a default font, get one from the font client.
526 // Emojis are present in many monochrome fonts; prefer color by default.
527 bool preferColor = ( TextAbstraction::EMOJI == script );
529 // Find a fallback-font.
530 fontId = fontClient.FindFallbackFont( preferredFont, character, pointSize, preferColor );
532 // If the system does not support a suitable font, fallback to Latin
535 fontId = *( defaultFontPerScriptCacheBuffer + TextAbstraction::LATIN );
539 fontId = fontClient.FindDefaultFont( UTF32_A, pointSize );
543 *( defaultFontPerScriptCacheBuffer + script ) = fontId;
549 Dali::TextAbstraction::FontDescription description;
550 fontClient.GetDescription( fontId, description );
551 DALI_LOG_INFO( gLogFilter,
553 " Validated font set\n Character : %x, Script : %s, Font : %s \n",
555 Dali::TextAbstraction::ScriptName[script],
556 description.path.c_str() );
560 // The font is now validated.
562 if( ( fontId != currentFontRun.fontId ) ||
563 ( isDefault != currentFontRun.isDefault ) )
565 // Current run needs to be stored and a new one initialized.
567 if( 0u != currentFontRun.characterRun.numberOfCharacters )
569 // Store the font run.
570 fonts.PushBack( currentFontRun );
573 // Initialize the new one.
574 currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
575 currentFontRun.characterRun.numberOfCharacters = 0u;
576 currentFontRun.fontId = fontId;
577 currentFontRun.isDefault = isDefault;
580 // Add one more character to the run.
581 ++currentFontRun.characterRun.numberOfCharacters;
584 if( 0u != currentFontRun.characterRun.numberOfCharacters )
586 // Store the last run.
587 fonts.PushBack( currentFontRun );
589 DALI_LOG_INFO( gLogFilter, Debug::General, "<--MultilanguageSupport::ValidateFonts\n" );
592 } // namespace Internal
596 } // namespace Toolkit