[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / multi-language-support-impl.h
1 #ifndef DALI_TOOLKIT_TEXT_MULTI_LANGUAGE_SUPPORT_IMPL_H
2 #define DALI_TOOLKIT_TEXT_MULTI_LANGUAGE_SUPPORT_IMPL_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/object/base-object.h>
23 #include <dali/public-api/signals/connection-tracker.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/text/multi-language-support.h>
27
28 namespace Dali
29 {
30 namespace TextAbstraction
31 {
32 //Forward declaration
33 class FontClient;
34 } // namespace TextAbstraction
35
36 namespace Toolkit
37 {
38 namespace Text
39 {
40 namespace Internal
41 {
42 /**
43  * @brief Stores valid font ids per script.
44  */
45 struct ValidateFontsPerScript
46 {
47   /**
48    * Default constructor.
49    */
50   ValidateFontsPerScript()
51   : mValidFonts()
52   {
53   }
54
55   /**
56    * Default destructor.
57    */
58   ~ValidateFontsPerScript()
59   {
60   }
61
62   /**
63    * @brief Whether the given @p fontId is in the vector of valid fonts.
64    *
65    * @param[in] fontId The font id.
66    *
67    * @return @e true if the font is in the vector of valid fonts.
68    */
69   bool IsValidFont(FontId fontId) const;
70
71   /**
72    * @brief Cache the given @p fontId in the vector of valid fonts.
73    * @note If cache size is big enough, we might remove some caches.
74    *
75    * @param[in] fontId The font id.
76    */
77   void Cache(FontId fontId);
78
79   Vector<FontId> mValidFonts;
80 };
81
82 /**
83  * @brief Stores default font ids per script. It can be different sizes for a default font family.
84  */
85 struct DefaultFonts
86 {
87   struct CacheItem
88   {
89     TextAbstraction::FontDescription description;
90     FontId                           fontId;
91   };
92
93   /**
94    * Default constructor.
95    */
96   DefaultFonts()
97   : mFonts()
98   {
99   }
100
101   /**
102    * Default destructor.
103    */
104   ~DefaultFonts()
105   {
106   }
107
108   /**
109    * @brief Finds a default font for the given @p size.
110    *
111    * @param[in] fontClient The font client.
112    * @param[in] description The font's description.
113    * @param[in] size The given size.
114    *
115    * @return The font id of a default font for the given @p size. If there isn't any font cached it returns 0.
116    */
117   FontId FindFont(TextAbstraction::FontClient&            fontClient,
118                   const TextAbstraction::FontDescription& description,
119                   PointSize26Dot6                         size) const;
120
121   /**
122    * @brief Cache a default font for the given @p size.
123    * @note If cache size is big enough, we might remove some caches.
124    *
125    * @param[in] description The font's description.
126    * @param[in] fontId The font id.
127    */
128   void Cache(const TextAbstraction::FontDescription& description, FontId fontId);
129
130   std::vector<CacheItem> mFonts;
131 };
132
133 /**
134  * @brief Multi-language support implementation. @see Text::MultilanguageSupport.
135  */
136 class MultilanguageSupport : public BaseObject, public ConnectionTracker
137 {
138 public:
139   /**
140    * Constructor
141    */
142   MultilanguageSupport();
143
144   /**
145    * Destructor
146    *
147    * This is non-virtual since derived Handle types must not contain data or virtual methods.
148    */
149   ~MultilanguageSupport();
150
151   /**
152    * @copydoc Dali::MultilanguageSupport::Get()
153    */
154   static Text::MultilanguageSupport Get();
155
156   /**
157    * @copydoc Dali::MultilanguageSupport::SetScripts()
158    */
159   void SetScripts(const Vector<Character>& text,
160                   CharacterIndex           startIndex,
161                   Length                   numberOfCharacters,
162                   Vector<ScriptRun>&       scripts);
163
164   /**
165    * @copydoc Dali::MultilanguageSupport::ValidateFonts()
166    */
167   void ValidateFonts(const Vector<Character>&                text,
168                      const Vector<ScriptRun>&                scripts,
169                      const Vector<FontDescriptionRun>&       fontDescriptions,
170                      const TextAbstraction::FontDescription& defaultFontDescription,
171                      TextAbstraction::PointSize26Dot6        defaultFontPointSize,
172                      float                                   fontSizeScale,
173                      CharacterIndex                          startIndex,
174                      Length                                  numberOfCharacters,
175                      Vector<FontRun>&                        fonts);
176
177   /**
178    * @brief Callback function for when the locale is changed.
179    * @param[in] locale The new system locale.
180    */
181   void OnLocaleChanged(std::string locale);
182
183   /**
184    * @brief Clear font caches when locale changed.
185    */
186   void ClearCache();
187
188   /**
189    * @brief Gets the locale.
190    */
191   std::string GetLocale();
192
193 private:
194   Vector<DefaultFonts*>           mDefaultFontPerScriptCache; ///< Caches default fonts for a script.
195   Vector<ValidateFontsPerScript*> mValidFontsPerScriptCache;  ///< Caches valid fonts for a script.
196
197   std::string mLocale;
198
199   //Methods
200
201   /**
202  * @brief Add the current script to scripts and create new script.
203  *
204  * @param[in] requestedScript The script of the new script run.
205  * @param[in] isRightToLeft The direction of the new script run.
206  * @param[in] addScriptCharactersToNewScript Whether to add the pending characters to the new script run or to the current script run.
207  * @param[inout] currentScriptRun The current character script run and it will be updated it to the new script run.
208  * @param[inout] numberOfAllScriptCharacters The pending characters.
209  * @param[inout] scripts The list of scripts.
210  * @param[inout] scriptIndex The current index of scripts.
211  *
212  */
213   void AddCurrentScriptAndCreatNewScript(const Script       requestedScript,
214                                          const bool         isRightToLeft,
215                                          const bool         addScriptCharactersToNewScript,
216                                          ScriptRun&         currentScriptRun,
217                                          Length&            numberOfAllScriptCharacters,
218                                          Vector<ScriptRun>& scripts,
219                                          ScriptRunIndex&    scriptIndex);
220 };
221
222 } // namespace Internal
223
224 inline static Internal::MultilanguageSupport& GetImplementation(MultilanguageSupport& multilanguageSupport)
225 {
226   DALI_ASSERT_ALWAYS(multilanguageSupport && "multi-language handle is empty");
227   BaseObject& handle = multilanguageSupport.GetBaseObject();
228   return static_cast<Internal::MultilanguageSupport&>(handle);
229 }
230
231 inline static const Internal::MultilanguageSupport& GetImplementation(const MultilanguageSupport& multilanguageSupport)
232 {
233   DALI_ASSERT_ALWAYS(multilanguageSupport && "multi-language handle is empty");
234   const BaseObject& handle = multilanguageSupport.GetBaseObject();
235   return static_cast<const Internal::MultilanguageSupport&>(handle);
236 }
237
238 } // namespace Text
239
240 } // namespace Toolkit
241
242 } // namespace Dali
243
244 #endif // DALI_TOOLKIT_TEXT_MULTI_LANGUAGE_SUPPORT_IMPL_H