Merge "(Vector) Use one EventThreadCallback" into 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
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/internal/text/multi-language-support.h>
26
27 namespace Dali
28 {
29 namespace TextAbstraction
30 {
31 //Forward declaration
32 class FontClient;
33 } // namespace TextAbstraction
34
35 namespace Toolkit
36 {
37 namespace Text
38 {
39 namespace Internal
40 {
41 /**
42  * @brief Stores valid font ids per script.
43  */
44 struct ValidateFontsPerScript
45 {
46   /**
47    * Default constructor.
48    */
49   ValidateFontsPerScript()
50   : mValidFonts()
51   {
52   }
53
54   /**
55    * Default destructor.
56    */
57   ~ValidateFontsPerScript()
58   {
59   }
60
61   /**
62    * @brief Whether the given @p fontId is in the vector of valid fonts.
63    *
64    * @param[in] fontId The font id.
65    *
66    * @return @e true if the font is in the vector of valid fonts.
67    */
68   bool IsValidFont(FontId fontId) const;
69
70   /**
71    * @brief Cache the given @p fontId in the vector of valid fonts.
72    * @note If cache size is big enough, we might remove some caches.
73    *
74    * @param[in] fontId The font id.
75    */
76   void Cache(FontId fontId);
77
78   Vector<FontId> mValidFonts;
79 };
80
81 /**
82  * @brief Stores default font ids per script. It can be different sizes for a default font family.
83  */
84 struct DefaultFonts
85 {
86   struct CacheItem
87   {
88     TextAbstraction::FontDescription description;
89     FontId                           fontId;
90   };
91
92   /**
93    * Default constructor.
94    */
95   DefaultFonts()
96   : mFonts()
97   {
98   }
99
100   /**
101    * Default destructor.
102    */
103   ~DefaultFonts()
104   {
105   }
106
107   /**
108    * @brief Finds a default font for the given @p size.
109    *
110    * @param[in] fontClient The font client.
111    * @param[in] description The font's description.
112    * @param[in] size The given size.
113    *
114    * @return The font id of a default font for the given @p size. If there isn't any font cached it returns 0.
115    */
116   FontId FindFont(TextAbstraction::FontClient&            fontClient,
117                   const TextAbstraction::FontDescription& description,
118                   PointSize26Dot6                         size) const;
119
120   /**
121    * @brief Cache a default font for the given @p size.
122    * @note If cache size is big enough, we might remove some caches.
123    *
124    * @param[in] description The font's description.
125    * @param[in] fontId The font id.
126    */
127   void Cache(const TextAbstraction::FontDescription& description, FontId fontId);
128
129   std::vector<CacheItem> mFonts;
130 };
131
132 /**
133  * @brief Multi-language support implementation. @see Text::MultilanguageSupport.
134  */
135 class MultilanguageSupport : public BaseObject
136 {
137 public:
138   /**
139    * Constructor
140    */
141   MultilanguageSupport();
142
143   /**
144    * Destructor
145    *
146    * This is non-virtual since derived Handle types must not contain data or virtual methods.
147    */
148   ~MultilanguageSupport();
149
150   /**
151    * @copydoc Dali::MultilanguageSupport::Get()
152    */
153   static Text::MultilanguageSupport Get();
154
155   /**
156    * @copydoc Dali::MultilanguageSupport::SetScripts()
157    */
158   void SetScripts(const Vector<Character>& text,
159                   CharacterIndex           startIndex,
160                   Length                   numberOfCharacters,
161                   Vector<ScriptRun>&       scripts);
162
163   /**
164    * @copydoc Dali::MultilanguageSupport::ValidateFonts()
165    */
166   void ValidateFonts(const Vector<Character>&                text,
167                      const Vector<ScriptRun>&                scripts,
168                      const Vector<FontDescriptionRun>&       fontDescriptions,
169                      const TextAbstraction::FontDescription& defaultFontDescription,
170                      TextAbstraction::PointSize26Dot6        defaultFontPointSize,
171                      float                                   fontSizeScale,
172                      CharacterIndex                          startIndex,
173                      Length                                  numberOfCharacters,
174                      Vector<FontRun>&                        fonts);
175
176 private:
177   Vector<DefaultFonts*>           mDefaultFontPerScriptCache; ///< Caches default fonts for a script.
178   Vector<ValidateFontsPerScript*> mValidFontsPerScriptCache;  ///< Caches valid fonts for a script.
179
180   //Methods
181
182   /**
183  * @brief Add the current script to scripts and create new script.
184  *
185  * @param[in] requestedScript The script of the new script run.
186  * @param[in] isRightToLeft The direction of the new script run.
187  * @param[in] addScriptCharactersToNewScript Whether to add the pending characters to the new script run or to the current script run.
188  * @param[inout] currentScriptRun The current character script run and it will be updated it to the new script run.
189  * @param[inout] numberOfAllScriptCharacters The pending characters.
190  * @param[inout] scripts The list of scripts.
191  * @param[inout] scriptIndex The current index of scripts.
192  *
193  */
194   void AddCurrentScriptAndCreatNewScript(const Script       requestedScript,
195                                          const bool         isRightToLeft,
196                                          const bool         addScriptCharactersToNewScript,
197                                          ScriptRun&         currentScriptRun,
198                                          Length&            numberOfAllScriptCharacters,
199                                          Vector<ScriptRun>& scripts,
200                                          ScriptRunIndex&    scriptIndex);
201 };
202
203 } // namespace Internal
204
205 inline static Internal::MultilanguageSupport& GetImplementation(MultilanguageSupport& multilanguageSupport)
206 {
207   DALI_ASSERT_ALWAYS(multilanguageSupport && "multi-language handle is empty");
208   BaseObject& handle = multilanguageSupport.GetBaseObject();
209   return static_cast<Internal::MultilanguageSupport&>(handle);
210 }
211
212 inline static const Internal::MultilanguageSupport& GetImplementation(const MultilanguageSupport& multilanguageSupport)
213 {
214   DALI_ASSERT_ALWAYS(multilanguageSupport && "multi-language handle is empty");
215   const BaseObject& handle = multilanguageSupport.GetBaseObject();
216   return static_cast<const Internal::MultilanguageSupport&>(handle);
217 }
218
219 } // namespace Text
220
221 } // namespace Toolkit
222
223 } // namespace Dali
224
225 #endif // DALI_TOOLKIT_TEXT_MULTI_LANGUAGE_SUPPORT_IMPL_H