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>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/adaptor-framework/singleton-service.h>
25 #include <dali/public-api/text-abstraction/font-client.h>
26 #include <dali/public-api/text-abstraction/script.h>
29 #include <dali-toolkit/internal/text/logical-model.h>
30 #include <dali-toolkit/internal/text/font-run.h>
31 #include <dali-toolkit/internal/text/script-run.h>
32 #include <dali-toolkit/internal/text/text-io.h>
42 #if defined(DEBUG_ENABLED)
43 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_MULTI_LANGUAGE_SUPPORT");
46 const Dali::Toolkit::Text::Character UTF32_A = 0x0041;
56 * @brief Retrieves the font Id from the font run for a given character's @p index.
58 * If the character's index exceeds the current font run it increases the iterator to get the next one.
60 * @param[in] index The character's index.
61 * @param[in,out] fontRunIt Iterator to the current font run.
62 * @param[in] fontRunEndIt Iterator to one after the last font run.
64 * @return The font id.
66 FontId GetFontId( Length index,
67 Vector<FontRun>::ConstIterator& fontRunIt,
68 const Vector<FontRun>::ConstIterator& fontRunEndIt )
72 if( fontRunIt != fontRunEndIt )
74 const FontRun& fontRun = *fontRunIt;
76 if( ( index >= fontRun.characterRun.characterIndex ) &&
77 ( index < fontRun.characterRun.characterIndex + fontRun.characterRun.numberOfCharacters ) )
79 fontId = fontRun.fontId;
82 if( index + 1u == fontRun.characterRun.characterIndex + fontRun.characterRun.numberOfCharacters )
84 // All the characters of the current run have been traversed. Get the next one for the next iteration.
93 * @brief Retrieves the script Id from the script run for a given character's @p index.
95 * If the character's index exceeds the current script run it increases the iterator to get the next one.
97 * @param[in] index The character's index.
98 * @param[in,out] scriptRunIt Iterator to the current font run.
99 * @param[in] scriptRunEndIt Iterator to one after the last script run.
101 * @return The script.
103 Script GetScript( Length index,
104 Vector<ScriptRun>::ConstIterator& scriptRunIt,
105 const Vector<ScriptRun>::ConstIterator& scriptRunEndIt )
107 Script script = TextAbstraction::UNKNOWN;
109 if( scriptRunIt != scriptRunEndIt )
111 const ScriptRun& scriptRun = *scriptRunIt;
113 if( ( index >= scriptRun.characterRun.characterIndex ) &&
114 ( index < scriptRun.characterRun.characterIndex + scriptRun.characterRun.numberOfCharacters ) )
116 script = scriptRun.script;
119 if( index + 1u == scriptRun.characterRun.characterIndex + scriptRun.characterRun.numberOfCharacters )
121 // All the characters of the current run have been traversed. Get the next one for the next iteration.
130 * @brief Whether the character is valid for all scripts. i.e. the white space.
132 * @param[in] character The character.
134 * @return @e true if the character is valid for all scripts.
136 bool IsValidForAllScripts( Character character )
138 return ( TextAbstraction::IsWhiteSpace( character ) ||
139 TextAbstraction::IsZeroWidthNonJoiner( character ) ||
140 TextAbstraction::IsZeroWidthJoiner( character ) ||
141 TextAbstraction::IsZeroWidthSpace( character ) ||
142 TextAbstraction::IsLeftToRightMark( character ) ||
143 TextAbstraction::IsRightToLeftMark( character ) ||
144 TextAbstraction::IsThinSpace( character ) );
147 bool ValidateFontsPerScript::FindValidFont( FontId fontId ) const
149 for( Vector<FontId>::ConstIterator it = mValidFonts.Begin(),
150 endIt = mValidFonts.End();
163 MultilanguageSupport::MultilanguageSupport()
164 : mDefaultFontPerScriptCache(),
165 mValidFontsPerScriptCache()
167 // Initializes the default font cache to zero (invalid font).
168 // Reserves space to cache the default fonts and access them with the script as an index.
169 mDefaultFontPerScriptCache.Resize( TextAbstraction::UNKNOWN, 0u );
171 // Initializes the valid fonts cache to NULL (no valid fonts).
172 // Reserves space to cache the valid fonts and access them with the script as an index.
173 mValidFontsPerScriptCache.Resize( TextAbstraction::UNKNOWN, NULL );
176 MultilanguageSupport::~MultilanguageSupport()
178 // Destroy the valid fonts per script cache.
180 for( Vector<ValidateFontsPerScript*>::Iterator it = mValidFontsPerScriptCache.Begin(),
181 endIt = mValidFontsPerScriptCache.End();
189 Text::MultilanguageSupport MultilanguageSupport::Get()
191 Text::MultilanguageSupport multilanguageSupportHandle;
193 SingletonService service( SingletonService::Get() );
196 // Check whether the singleton is already created
197 Dali::BaseHandle handle = service.GetSingleton( typeid( Text::MultilanguageSupport ) );
200 // If so, downcast the handle
201 MultilanguageSupport* impl = dynamic_cast< Internal::MultilanguageSupport* >( handle.GetObjectPtr() );
202 multilanguageSupportHandle = Text::MultilanguageSupport( impl );
204 else // create and register the object
206 multilanguageSupportHandle = Text::MultilanguageSupport( new MultilanguageSupport );
207 service.Register( typeid( multilanguageSupportHandle ), multilanguageSupportHandle );
211 return multilanguageSupportHandle;
214 void MultilanguageSupport::SetScripts( const Vector<Character>& text,
215 const Vector<LineBreakInfo>& lineBreakInfo,
216 Vector<ScriptRun>& scripts )
218 const Length numberOfCharacters = text.Count();
220 if( 0u == numberOfCharacters )
222 // Nothing to do if there are no characters.
226 // Stores the current script run.
227 ScriptRun currentScriptRun;
228 currentScriptRun.characterRun.characterIndex = 0u;
229 currentScriptRun.characterRun.numberOfCharacters = 0u;
230 currentScriptRun.script = TextAbstraction::UNKNOWN;
232 // Reserve some space to reduce the number of reallocations.
233 scripts.Reserve( numberOfCharacters << 2u );
235 // Whether the first valid script need to be set.
236 bool firstValidScript = true;
238 // Whether the first valid script is a right to left script.
239 bool isParagraphRTL = false;
241 // Count the number of characters which are valid for all scripts. i.e. white spaces or '\n'.
242 Length numberOfAllScriptCharacters = 0u;
244 // Pointers to the text and break info buffers.
245 const Character* textBuffer = text.Begin();
246 const LineBreakInfo* breakInfoBuffer = lineBreakInfo.Begin();
248 // Traverse all characters and set the scripts.
249 for( Length index = 0u; index < numberOfCharacters; ++index )
251 Character character = *( textBuffer + index );
252 LineBreakInfo breakInfo = *( breakInfoBuffer + index );
254 // Some characters (like white spaces) are valid for many scripts. The rules to set a script
256 // - If they are at the begining of a paragraph they get the script of the first character with
257 // a defined script. If they are at the end, they get the script of the last one.
258 // - If they are between two scripts with the same direction, they get the script of the previous
259 // character with a defined script. If the two scripts have different directions, they get the
260 // script of the first character of the paragraph with a defined script.
262 // Skip those characters valid for many scripts like white spaces or '\n'.
263 bool endOfText = index == numberOfCharacters;
265 IsValidForAllScripts( character ) )
267 // Count all these characters to be added into a script.
268 ++numberOfAllScriptCharacters;
270 if( TextAbstraction::LINE_MUST_BREAK == breakInfo )
272 // The next character is a new paragraph.
273 // Know when there is a new paragraph is needed because if there is a white space
274 // between two scripts with different directions, it is added to the script with
275 // the same direction than the first script of the paragraph.
276 firstValidScript = true;
277 isParagraphRTL = false;
280 // Get the next character.
282 endOfText = index == numberOfCharacters;
285 character = *( textBuffer + index );
286 breakInfo = *( breakInfoBuffer + index );
292 // Last characters of the text are 'white spaces'.
293 // There is nothing else to do. Just add the remaining characters to the last script after this bucle.
297 // Get the script of the character.
298 Script script = TextAbstraction::GetCharacterScript( character );
300 // Check if it is the first character of a paragraph.
301 if( firstValidScript &&
302 ( TextAbstraction::UNKNOWN != script ) )
304 // Sets the direction of the first valid script.
305 isParagraphRTL = TextAbstraction::IsRightToLeftScript( script );
306 firstValidScript = false;
309 if( script != currentScriptRun.script )
311 // Current run needs to be stored and a new one initialized.
313 if( isParagraphRTL != TextAbstraction::IsRightToLeftScript( script ) )
315 // Current script has different direction than the first script of the paragraph.
316 // All the previously skipped characters need to be added to the previous script before it's stored.
317 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
318 numberOfAllScriptCharacters = 0u;
321 if( 0u != currentScriptRun.characterRun.numberOfCharacters )
323 // Store the script run.
324 scripts.PushBack( currentScriptRun );
327 // Initialize the new one.
328 currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
329 currentScriptRun.characterRun.numberOfCharacters = numberOfAllScriptCharacters; // Adds the white spaces which are at the begining of the script.
330 currentScriptRun.script = script;
331 numberOfAllScriptCharacters = 0u;
335 // Adds white spaces between characters.
336 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
337 numberOfAllScriptCharacters = 0u;
340 if( TextAbstraction::LINE_MUST_BREAK == breakInfo )
342 // The next character is a new paragraph.
343 firstValidScript = true;
344 isParagraphRTL = false;
347 // Add one more character to the run.
348 ++currentScriptRun.characterRun.numberOfCharacters;
351 // Add remaining characters into the last script.
352 currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
353 if( 0u != currentScriptRun.characterRun.numberOfCharacters )
355 if( TextAbstraction::UNKNOWN == currentScriptRun.script )
357 // There are only white spaces in the last script. Set the latin script.
358 currentScriptRun.script = TextAbstraction::LATIN;
361 // Store the last run.
362 scripts.PushBack( currentScriptRun );
366 void MultilanguageSupport::ReplaceScripts( LogicalModel& model,
367 CharacterIndex characterIndex,
368 Length numberOfCharactersToRemove,
369 Length numberOfCharactersToInsert )
373 void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
374 const Vector<ScriptRun>& scripts,
375 Vector<FontRun>& fonts )
377 const Length numberOfCharacters = text.Count();
379 if( 0u == numberOfCharacters )
381 // Nothing to do if there are no characters.
385 // Copy the fonts set by application developers.
386 const Length numberOfFontRuns = fonts.Count();
387 const Vector<FontRun> definedFonts = fonts;
390 // Traverse the characters and validate/set the fonts.
393 FontId* defaultFontPerScriptCacheBuffer = mDefaultFontPerScriptCache.Begin();
394 ValidateFontsPerScript** validFontsPerScriptCacheBuffer = mValidFontsPerScriptCache.Begin();
396 // Stores the validated font runs.
397 fonts.Reserve( numberOfFontRuns );
399 // Initializes a validated font run.
400 FontRun currentFontRun;
401 currentFontRun.characterRun.characterIndex = 0u;
402 currentFontRun.characterRun.numberOfCharacters = 0u;
403 currentFontRun.fontId = 0u;
404 currentFontRun.isDefault = false;
406 // Get the font client.
407 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
409 // Iterators of the font and script runs.
410 Vector<FontRun>::ConstIterator fontRunIt = definedFonts.Begin();
411 Vector<FontRun>::ConstIterator fontRunEndIt = definedFonts.End();
412 Vector<ScriptRun>::ConstIterator scriptRunIt = scripts.Begin();
413 Vector<ScriptRun>::ConstIterator scriptRunEndIt = scripts.End();
415 for( Length index = 0u; index < numberOfCharacters; ++index )
417 // Get the character.
418 const Character character = *( text.Begin() + index );
420 // Get the font for the character.
421 FontId fontId = GetFontId( index,
425 // Get the script for the character.
426 Script script = GetScript( index,
430 if( TextAbstraction::UNKNOWN == script )
432 DALI_LOG_WARNING( "MultilanguageSupport::ValidateFonts. Unknown script!" );
433 script = TextAbstraction::LATIN;
436 // Whether the font being validated is a default one not set by the user.
437 const bool isDefault = ( 0u == fontId );
439 // The default font point size.
440 PointSize26Dot6 pointSize = TextAbstraction::FontClient::DEFAULT_POINT_SIZE;
444 // Validate if the font set by the user supports the character.
446 // Check first in the caches.
448 // The user may have set the default font. Check it. Otherwise check in the valid fonts cache.
449 if( fontId != *( defaultFontPerScriptCacheBuffer + script ) )
451 // Check in the valid fonts cache.
452 ValidateFontsPerScript* validateFontsPerScript = *( validFontsPerScriptCacheBuffer + script );
453 if( NULL != validateFontsPerScript )
455 if( !validateFontsPerScript->FindValidFont( fontId ) )
457 // Use the font client to validate the font.
458 const GlyphIndex glyphIndex = fontClient.GetGlyphIndex( fontId, character );
460 if( 0u == glyphIndex )
462 // Get the point size of the current font. It will be used to get a default font id.
463 pointSize = fontClient.GetPointSize( fontId );
465 // The font is not valid. Set to zero and a default one will be set.
470 // Add the font to the valid font cache.
471 validateFontsPerScript->mValidFonts.PushBack( fontId );
477 // Use the font client to validate the font.
478 const GlyphIndex glyphIndex = fontClient.GetGlyphIndex( fontId, character );
480 if( 0u == glyphIndex )
482 // Get the point size of the current font. It will be used to get a default font id.
483 pointSize = fontClient.GetPointSize( fontId );
485 // The font is not valid. Set to zero and a default one will be set.
488 else if( !IsValidForAllScripts( character ) )
490 // Add the font to the valid font cache.
491 validateFontsPerScript = new ValidateFontsPerScript();
492 *( validFontsPerScriptCacheBuffer + script ) = validateFontsPerScript;
494 validateFontsPerScript->mValidFonts.PushBack( fontId );
500 // The font has not been validated. Find a default one.
503 // The character has no font assigned. Get a default one from the cache
504 fontId = *( defaultFontPerScriptCacheBuffer + script );
506 // If the cache has not a default font, get one from the font client.
509 // Emojis are present in many monochrome fonts; prefer color by default.
510 bool preferColor = ( TextAbstraction::EMOJI == script );
512 // Find a default font.
513 fontId = fontClient.FindDefaultFont( character, pointSize, preferColor );
515 // If the system does not support a suitable font, fallback to Latin
518 fontId = *( defaultFontPerScriptCacheBuffer + TextAbstraction::LATIN );
522 fontId = fontClient.FindDefaultFont( UTF32_A, pointSize );
526 Dali::TextAbstraction::FontDescription description;
527 fontClient.GetDescription( fontId, description );
528 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Script: %s; Selected font: %s\n", Dali::TextAbstraction::ScriptName[script], description.path.c_str() );
531 *( defaultFontPerScriptCacheBuffer + script ) = fontId;
535 // The font is now validated.
537 if( ( fontId != currentFontRun.fontId ) ||
538 ( isDefault != currentFontRun.isDefault ) )
540 // Current run needs to be stored and a new one initialized.
542 if( 0u != currentFontRun.characterRun.numberOfCharacters )
544 // Store the font run.
545 fonts.PushBack( currentFontRun );
548 // Initialize the new one.
549 currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
550 currentFontRun.characterRun.numberOfCharacters = 0u;
551 currentFontRun.fontId = fontId;
552 currentFontRun.isDefault = isDefault;
555 // Add one more character to the run.
556 ++currentFontRun.characterRun.numberOfCharacters;
559 if( 0u != currentFontRun.characterRun.numberOfCharacters )
561 // Store the last run.
562 fonts.PushBack( currentFontRun );
566 void MultilanguageSupport::ValidateFonts( LogicalModel& model,
567 CharacterIndex characterIndex,
568 Length numberOfCharactersToRemove,
569 Length numberOfCharactersToInsert )
573 } // namespace Internal
577 } // namespace Toolkit