Fixes for multi-language.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / multi-language-support-impl.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/multi-language-support-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <memory.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
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/public-api/text/logical-model.h>
29 #include <dali-toolkit/public-api/text/font-run.h>
30 #include <dali-toolkit/public-api/text/script.h>
31 #include <dali-toolkit/public-api/text/script-run.h>
32
33 namespace Dali
34 {
35
36 namespace Toolkit
37 {
38
39 namespace Text
40 {
41
42 namespace Internal
43 {
44
45 /**
46  * @brief Retrieves the font Id from the font run for a given character's @p index.
47  *
48  * If the character's index exceeds the current font run it increases the iterator to get the next one.
49  *
50  * @param[in] index The character's index.
51  * @param[in,out] fontRunIt Iterator to the current font run.
52  * @param[in] fontRunEndIt Iterator to one after the last font run.
53  *
54  * @return The font id.
55  */
56 FontId GetFontId( Length index,
57                   Vector<FontRun>::ConstIterator& fontRunIt,
58                   const Vector<FontRun>::ConstIterator& fontRunEndIt )
59 {
60   FontId fontId = 0u;
61
62   if( fontRunIt != fontRunEndIt )
63   {
64     const FontRun& fontRun = *fontRunIt;
65
66     if( ( index >= fontRun.characterRun.characterIndex ) &&
67         ( index < fontRun.characterRun.characterIndex + fontRun.characterRun.numberOfCharacters ) )
68     {
69       fontId = fontRun.fontId;
70     }
71
72     if( index + 1u == fontRun.characterRun.characterIndex + fontRun.characterRun.numberOfCharacters )
73     {
74       // All the characters of the current run have been traversed. Get the next one for the next iteration.
75       ++fontRunIt;
76     }
77   }
78
79   return fontId;
80 }
81
82 /**
83  * @brief Retrieves the script Id from the script run for a given character's @p index.
84  *
85  * If the character's index exceeds the current script run it increases the iterator to get the next one.
86  *
87  * @param[in] index The character's index.
88  * @param[in,out] scriptRunIt Iterator to the current font run.
89  * @param[in] scriptRunEndIt Iterator to one after the last script run.
90  *
91  * @return The script.
92  */
93 Script GetScript( Length index,
94                   Vector<ScriptRun>::ConstIterator& scriptRunIt,
95                   const Vector<ScriptRun>::ConstIterator& scriptRunEndIt )
96 {
97   Script script = TextAbstraction::UNKNOWN;
98
99   if( scriptRunIt != scriptRunEndIt )
100   {
101     const ScriptRun& scriptRun = *scriptRunIt;
102
103     if( ( index >= scriptRun.characterRun.characterIndex ) &&
104         ( index < scriptRun.characterRun.characterIndex + scriptRun.characterRun.numberOfCharacters ) )
105     {
106       script = scriptRun.script;
107     }
108
109     if( index + 1u == scriptRun.characterRun.characterIndex + scriptRun.characterRun.numberOfCharacters )
110     {
111       // All the characters of the current run have been traversed. Get the next one for the next iteration.
112       ++scriptRunIt;
113     }
114   }
115
116   return script;
117 }
118
119 /**
120  * @brief Whether the character is valid for all scripts. i.e. the white space.
121  *
122  * @param[in] character The character.
123  *
124  * @return @e true if the character is valid for all scripts.
125  */
126 bool IsValidForAllScripts( Character character )
127 {
128   return ( IsWhiteSpace( character )         ||
129            IsZeroWidthNonJoiner( character ) ||
130            IsZeroWidthJoiner( character )    ||
131            IsZeroWidthSpace( character )     ||
132            IsLeftToRightMark( character )    ||
133            IsRightToLeftMark( character )    ||
134            IsThinSpace( character ) );
135 }
136
137 bool ValidateFontsPerScript::FindValidFont( FontId fontId ) const
138 {
139   for( Vector<FontId>::ConstIterator it = mValidFonts.Begin(),
140          endIt = mValidFonts.End();
141        it != endIt;
142        ++it )
143   {
144     if( fontId == *it )
145     {
146       return true;
147     }
148   }
149
150   return false;
151 }
152
153 MultilanguageSupport::MultilanguageSupport()
154 : mDefaultFontPerScriptCache(),
155   mValidFontsPerScriptCache()
156 {
157   // Initializes the default font cache to zero (invalid font).
158   // Reserves space to cache the default fonts and access them with the script as an index.
159   mDefaultFontPerScriptCache.Resize( TextAbstraction::UNKNOWN, 0u );
160
161   // Initializes the valid fonts cache to NULL (no valid fonts).
162   // Reserves space to cache the valid fonts and access them with the script as an index.
163   mValidFontsPerScriptCache.Resize( TextAbstraction::UNKNOWN, NULL );
164 }
165
166 MultilanguageSupport::~MultilanguageSupport()
167 {
168   // Destroy the valid fonts per script cache.
169
170   for( Vector<ValidateFontsPerScript*>::Iterator it = mValidFontsPerScriptCache.Begin(),
171          endIt = mValidFontsPerScriptCache.End();
172        it != endIt;
173        ++it )
174   {
175     delete *it;
176   }
177 }
178
179 Text::MultilanguageSupport MultilanguageSupport::Get()
180 {
181   Text::MultilanguageSupport multilanguageSupportHandle;
182
183   SingletonService service( SingletonService::Get() );
184   if( service )
185   {
186     // Check whether the singleton is already created
187     Dali::BaseHandle handle = service.GetSingleton( typeid( Text::MultilanguageSupport ) );
188     if( handle )
189     {
190       // If so, downcast the handle
191       MultilanguageSupport* impl = dynamic_cast< Internal::MultilanguageSupport* >( handle.GetObjectPtr() );
192       multilanguageSupportHandle = Text::MultilanguageSupport( impl );
193     }
194     else // create and register the object
195     {
196       multilanguageSupportHandle = Text::MultilanguageSupport( new MultilanguageSupport );
197       service.Register( typeid( multilanguageSupportHandle ), multilanguageSupportHandle );
198     }
199   }
200
201   return multilanguageSupportHandle;
202 }
203
204 void MultilanguageSupport::SetScripts( const Vector<Character>& text,
205                                        const Vector<LineBreakInfo>& lineBreakInfo,
206                                        Vector<ScriptRun>& scripts )
207 {
208   const Length numberOfCharacters = text.Count();
209
210   if( 0u == numberOfCharacters )
211   {
212     // Nothing to do if there are no characters.
213     return;
214   }
215
216   // Stores the current script run.
217   ScriptRun currentScriptRun;
218   currentScriptRun.characterRun.characterIndex = 0u;
219   currentScriptRun.characterRun.numberOfCharacters = 0u;
220   currentScriptRun.script = TextAbstraction::UNKNOWN;
221
222   // Reserve some space to reduce the number of reallocations.
223   scripts.Reserve( numberOfCharacters << 2u );
224
225   // Whether the first valid script need to be set.
226   bool firstValidScript = true;
227
228   // Whether the first valid script is a right to left script.
229   bool isParagraphRTL = false;
230
231   // Count the number of characters which are valid for all scripts. i.e. white spaces or '\n'.
232   Length numberOfAllScriptCharacters = 0u;
233
234   // Pointers to the text and break info buffers.
235   const Character* textBuffer = text.Begin();
236   const LineBreakInfo* breakInfoBuffer = lineBreakInfo.Begin();
237
238   // Traverse all characters and set the scripts.
239   for( Length index = 0u; index < numberOfCharacters; ++index )
240   {
241     Character character = *( textBuffer + index );
242     LineBreakInfo breakInfo = *( breakInfoBuffer + index );
243
244     // Some characters (like white spaces) are valid for many scripts. The rules to set a script
245     // for them are:
246     // - If they are at the begining of a paragraph they get the script of the first character with
247     //   a defined script. If they are at the end, they get the script of the last one.
248     // - If they are between two scripts with the same direction, they get the script of the previous
249     //   character with a defined script. If the two scripts have different directions, they get the
250     //   script of the first character of the paragraph with a defined script.
251
252     // Skip those characters valid for many scripts like white spaces or '\n'.
253     bool endOfText = index == numberOfCharacters;
254     while( !endOfText &&
255            IsValidForAllScripts( character ) )
256     {
257       // Count all these characters to be added into a script.
258       ++numberOfAllScriptCharacters;
259
260       if( TextAbstraction::LINE_MUST_BREAK == breakInfo )
261       {
262         // The next character is a new paragraph.
263         // Know when there is a new paragraph is needed because if there is a white space
264         // between two scripts with different directions, it is added to the script with
265         // the same direction than the first script of the paragraph.
266         firstValidScript = true;
267         isParagraphRTL = false;
268       }
269
270       // Get the next character.
271       ++index;
272       endOfText = index == numberOfCharacters;
273       if( !endOfText )
274       {
275         character = *( textBuffer + index );
276         breakInfo = *( breakInfoBuffer + index );
277       }
278     }
279
280     if( endOfText )
281     {
282       // Last characters of the text are 'white spaces'.
283       // There is nothing else to do. Just add the remaining characters to the last script after this bucle.
284       break;
285     }
286
287     // Get the script of the character.
288     Script script = GetCharacterScript( character );
289
290     // Check if it is the first character of a paragraph.
291     if( firstValidScript &&
292         ( TextAbstraction::UNKNOWN != script ) )
293     {
294       // Sets the direction of the first valid script.
295       isParagraphRTL = ( TextAbstraction::ARABIC == script );
296       firstValidScript = false;
297     }
298
299     if( script != currentScriptRun.script )
300     {
301       // Current run needs to be stored and a new one initialized.
302
303       if( isParagraphRTL != ( TextAbstraction::ARABIC == script ) )
304       {
305         // Current script has different direction than the first script of the paragraph.
306         // All the previously skipped characters need to be added to the previous script before it's stored.
307         currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
308         numberOfAllScriptCharacters = 0u;
309       }
310
311       if( 0u != currentScriptRun.characterRun.numberOfCharacters )
312       {
313         // Store the script run.
314         scripts.PushBack( currentScriptRun );
315       }
316
317       // Initialize the new one.
318       currentScriptRun.characterRun.characterIndex = currentScriptRun.characterRun.characterIndex + currentScriptRun.characterRun.numberOfCharacters;
319       currentScriptRun.characterRun.numberOfCharacters = numberOfAllScriptCharacters; // Adds the white spaces which are at the begining of the script.
320       currentScriptRun.script = script;
321       numberOfAllScriptCharacters = 0u;
322     }
323     else
324     {
325       // Adds white spaces between characters.
326       currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
327       numberOfAllScriptCharacters = 0u;
328     }
329
330     if( TextAbstraction::LINE_MUST_BREAK == breakInfo )
331     {
332       // The next character is a new paragraph.
333       firstValidScript = true;
334       isParagraphRTL = false;
335     }
336
337     // Add one more character to the run.
338     ++currentScriptRun.characterRun.numberOfCharacters;
339   }
340
341   // Add remaining characters into the last script.
342   currentScriptRun.characterRun.numberOfCharacters += numberOfAllScriptCharacters;
343   if( 0u != currentScriptRun.characterRun.numberOfCharacters )
344   {
345     if( TextAbstraction::UNKNOWN == currentScriptRun.script )
346     {
347       // There are only white spaces in the last script. Set the latin script.
348       currentScriptRun.script = TextAbstraction::LATIN;
349     }
350
351     // Store the last run.
352     scripts.PushBack( currentScriptRun );
353   }
354 }
355
356 void MultilanguageSupport::ValidateFonts( const Vector<Character>& text,
357                                           const Vector<ScriptRun>& scripts,
358                                           Vector<FontRun>& fonts )
359 {
360   const Length numberOfCharacters = text.Count();
361
362   if( 0u == numberOfCharacters )
363   {
364     // Nothing to do if there are no characters.
365     return;
366   }
367
368   // Copy the fonts set by application developers.
369   const Length numberOfFontRuns = fonts.Count();
370   const Vector<FontRun> definedFonts = fonts;
371   fonts.Clear();
372
373   // Traverse the characters and validate/set the fonts.
374
375   // Get the caches.
376   FontId* defaultFontPerScriptCacheBuffer = mDefaultFontPerScriptCache.Begin();
377   ValidateFontsPerScript** validFontsPerScriptCacheBuffer = mValidFontsPerScriptCache.Begin();
378
379   // Stores the validated font runs.
380   fonts.Reserve( numberOfFontRuns );
381
382   // Initializes a validated font run.
383   FontRun currentFontRun;
384   currentFontRun.characterRun.characterIndex = 0u;
385   currentFontRun.characterRun.numberOfCharacters = 0u;
386   currentFontRun.fontId = 0u;
387   currentFontRun.isDefault = false;
388
389   // Get the font client.
390   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
391
392   // Iterators of the font and script runs.
393   Vector<FontRun>::ConstIterator fontRunIt = definedFonts.Begin();
394   Vector<FontRun>::ConstIterator fontRunEndIt = definedFonts.End();
395   Vector<ScriptRun>::ConstIterator scriptRunIt = scripts.Begin();
396   Vector<ScriptRun>::ConstIterator scriptRunEndIt = scripts.End();
397
398   for( Length index = 0u; index < numberOfCharacters; ++index )
399   {
400     // Get the character.
401     const Character character = *( text.Begin() + index );
402
403     // Get the font for the character.
404     FontId fontId = GetFontId( index,
405                                fontRunIt,
406                                fontRunEndIt );
407
408     // Get the script for the character.
409     Script script = GetScript( index,
410                                scriptRunIt,
411                                scriptRunEndIt );
412
413     if( TextAbstraction::UNKNOWN == script )
414     {
415       DALI_LOG_WARNING( "MultilanguageSupport::ValidateFonts. Unknown script!" );
416       script = TextAbstraction::LATIN;
417     }
418
419     // Whether the font being validated is a default one not set by the user.
420     const bool isDefault = ( 0u == fontId );
421
422     // The default font point size.
423     PointSize26Dot6 pointSize = TextAbstraction::FontClient::DEFAULT_POINT_SIZE;
424
425     if( !isDefault )
426     {
427       // Validate if the font set by the user supports the character.
428
429       // Check first in the caches.
430
431       // The user may have set the default font. Check it. Otherwise check in the valid fonts cache.
432       if( fontId != *( defaultFontPerScriptCacheBuffer + script ) )
433       {
434         // Check in the valid fonts cache.
435         ValidateFontsPerScript* validateFontsPerScript = *( validFontsPerScriptCacheBuffer + script );
436         if( NULL != validateFontsPerScript )
437         {
438           if( !validateFontsPerScript->FindValidFont( fontId ) )
439           {
440             // Use the font client to validate the font.
441             const GlyphIndex glyphIndex = fontClient.GetGlyphIndex( fontId, character );
442
443             if( 0u == glyphIndex )
444             {
445               // Get the point size of the current font. It will be used to get a default font id.
446               pointSize = fontClient.GetPointSize( fontId );
447
448               // The font is not valid. Set to zero and a default one will be set.
449               fontId = 0u;
450             }
451             else
452             {
453               // Add the font to the valid font cache.
454               validateFontsPerScript->mValidFonts.PushBack( fontId );
455             }
456           }
457         }
458         else
459         {
460           // Use the font client to validate the font.
461           const GlyphIndex glyphIndex = fontClient.GetGlyphIndex( fontId, character );
462
463           if( 0u == glyphIndex )
464           {
465             // Get the point size of the current font. It will be used to get a default font id.
466             pointSize = fontClient.GetPointSize( fontId );
467
468             // The font is not valid. Set to zero and a default one will be set.
469             fontId = 0u;
470           }
471           else
472           {
473             // Add the font to the valid font cache.
474             validateFontsPerScript = new ValidateFontsPerScript();
475             *( validFontsPerScriptCacheBuffer + script ) = validateFontsPerScript;
476
477             validateFontsPerScript->mValidFonts.PushBack( fontId );
478           }
479         }
480       }
481     }
482
483     // The font has not been validated. Find a default one.
484     if( 0u == fontId )
485     {
486       // The character has no font assigned. Get a default one from the cache
487       fontId = *( defaultFontPerScriptCacheBuffer + script );
488
489       // If the cache has not a default font, get one from the font client.
490       if( 0u == fontId )
491       {
492         // Find a default font.
493         fontId = fontClient.FindDefaultFont( character, pointSize );
494
495         // Cache the font.
496         *( defaultFontPerScriptCacheBuffer + script ) = fontId;
497       }
498     }
499
500     // The font is now validated.
501
502     if( ( fontId != currentFontRun.fontId ) ||
503         ( isDefault != currentFontRun.isDefault ) )
504     {
505       // Current run needs to be stored and a new one initialized.
506
507       if( 0u != currentFontRun.characterRun.numberOfCharacters )
508       {
509         // Store the font run.
510         fonts.PushBack( currentFontRun );
511       }
512
513       // Initialize the new one.
514       currentFontRun.characterRun.characterIndex = currentFontRun.characterRun.characterIndex + currentFontRun.characterRun.numberOfCharacters;
515       currentFontRun.characterRun.numberOfCharacters = 0u;
516       currentFontRun.fontId = fontId;
517       currentFontRun.isDefault = isDefault;
518     }
519
520     // Add one more character to the run.
521     ++currentFontRun.characterRun.numberOfCharacters;
522   }
523
524   if( 0u != currentFontRun.characterRun.numberOfCharacters )
525   {
526     // Store the last run.
527     fonts.PushBack( currentFontRun );
528   }
529 }
530
531 } // namespace Internal
532
533 } // namespace Text
534
535 } // namespace Toolkit
536
537 } // namespace Dali