Merge "Add a callback for navigation policy in web view." into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / text / text-visual.h
1 #ifndef DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H
3
4 /*
5  * Copyright (c) 2021 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/animation/constraint.h>
23 #include <dali/public-api/object/base-object.h>
24 #include <dali/public-api/object/weak-handle.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
28 #include <dali-toolkit/internal/text/text-controller.h>
29 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
30
31 namespace Dali
32 {
33 namespace Toolkit
34 {
35 namespace Internal
36 {
37 class TextVisual;
38 typedef IntrusivePtr<TextVisual> TextVisualPtr;
39
40 /**
41  * The visual which renders text
42  *
43  * The following properties are optional:
44  *
45  * | %Property Name      | Type    |
46  * |---------------------|---------|
47  * | renderingBackend    | INTEGER |
48  * | text                | STRING  |
49  * | fontFamily          | STRING  |
50  * | fontStyle           | STRING  |
51  * | pointSize           | FLOAT   |
52  * | multiLine           | BOOLEAN |
53  * | horizontalAlignment | STRING  |
54  * | verticalAlignment   | STRING  |
55  * | textColor           | VECTOR4 |
56  * | enableMarkup        | BOOLEAN |
57  * | enableAutoScroll    | BOOLEAN |
58  * | autoScrollSpeed     | INTEGER |
59  * | autoScrollLoopCount | INTEGER |
60  * | autoScrollGap       | INTEGER |
61  * | lineSpacing         | FLOAT   |
62  * | underline           | STRING  |
63  * | shadow              | STRING  |
64  * | outline             | STRING  |
65  *
66  */
67 class TextVisual : public Visual::Base
68 {
69 public:
70   /**
71    * @brief Create a new text visual.
72    *
73    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
74    * @param[in] properties A Property::Map containing settings for this visual
75    * @return A smart-pointer to the newly allocated visual.
76    */
77   static TextVisualPtr New(VisualFactoryCache& factoryCache, const Property::Map& properties);
78
79   /**
80    * @brief Converts all strings keys in property map to index keys.  Property Map can then be merged correctly.
81    * @param[in] propertyMap containing string keys or a mix of strings and indexes.
82    * @return Property::Map containing index keys.
83    */
84   static Property::Map ConvertStringKeysToIndexKeys(const Property::Map& propertyMap);
85
86   /**
87    * @brief Retrieve the text's controller.
88    * @param[in] visual The text visual.
89    * @return The text controller
90    */
91   static Text::ControllerPtr GetController(Toolkit::Visual::Base visual)
92   {
93     return GetVisualObject(visual).mController;
94   };
95
96   /**
97    * @brief Set the index of the animatable text color property.
98    * @param[in] visual The text visual.
99    * @param[in] animatablePropertyIndex The index of the animatable property
100    */
101   static void SetAnimatableTextColorProperty(Toolkit::Visual::Base visual, Property::Index animatablePropertyIndex)
102   {
103     GetVisualObject(visual).mAnimatableTextColorPropertyIndex = animatablePropertyIndex;
104   };
105
106   /**
107    * @brief Set the flag to trigger the textures to be initialized and renderer to be added to the control.
108    * @param[in] visual The text visual.
109    */
110   static void EnableRendererUpdate(Toolkit::Visual::Base visual)
111   {
112     GetVisualObject(visual).mRendererUpdateNeeded = true;
113   };
114
115   /**
116    * @brief Instantly updates the renderer
117    * @param[in] visual The text visual.
118    */
119   static void UpdateRenderer(Toolkit::Visual::Base visual)
120   {
121     GetVisualObject(visual).UpdateRenderer();
122   };
123
124 public: // from Visual::Base
125   /**
126    * @copydoc Visual::Base::GetHeightForWidth()
127    */
128   float GetHeightForWidth(float width) override;
129
130   /**
131    * @copydoc Visual::Base::GetNaturalSize()
132    */
133   void GetNaturalSize(Vector2& naturalSize) override;
134
135   /**
136    * @copydoc Visual::Base::CreatePropertyMap()
137    */
138   void DoCreatePropertyMap(Property::Map& map) const override;
139
140   /**
141    * @copydoc Visual::Base::CreateInstancePropertyMap
142    */
143   void DoCreateInstancePropertyMap(Property::Map& map) const override;
144
145 protected:
146   /**
147    * @brief Constructor.
148    *
149    * @param[in] factoryCache The VisualFactoryCache object
150    */
151   TextVisual(VisualFactoryCache& factoryCache);
152
153   /**
154    * @brief A reference counted object may only be deleted by calling Unreference().
155    */
156   virtual ~TextVisual();
157
158   /**
159    * @copydoc Visual::Base::OnInitialize
160    */
161   void OnInitialize() override;
162
163   // from Visual::Base
164
165   /**
166    * @copydoc Visual::Base::DoSetProperties()
167    */
168   void DoSetProperties(const Property::Map& propertyMap) override;
169
170   /**
171    * @copydoc Visual::Base::DoSetOnScene()
172    */
173   void DoSetOnScene(Actor& actor) override;
174
175   /**
176    * @copydoc Visual::Base::DoSetOffScene()
177    */
178   void DoSetOffScene(Actor& actor) override;
179
180   /**
181    * @copydoc Visual::Base::OnSetTransform
182    */
183   void OnSetTransform() override;
184
185 private:
186   struct TilingInfo
187   {
188     unsigned char* textBuffer;
189     unsigned char* styleBuffer;
190     unsigned char* maskBuffer;
191     int            width;
192     int            height;
193     Pixel::Format  textPixelFormat;
194     int            offsetPosition;
195     Vector2        offSet;
196
197     TilingInfo(int width, int height, Pixel::Format textPixelFormat)
198     : textBuffer(NULL),
199       styleBuffer(NULL),
200       maskBuffer(NULL),
201       width(width),
202       height(height),
203       textPixelFormat(textPixelFormat),
204       offsetPosition(0),
205       offSet(0.f, 0.f)
206     {
207     }
208
209     ~TilingInfo()
210     {
211       if(textBuffer)
212       {
213         free(textBuffer);
214       }
215       if(styleBuffer)
216       {
217         free(styleBuffer);
218       }
219       if(maskBuffer)
220       {
221         free(maskBuffer);
222       }
223     }
224   };
225
226   /**
227    * @brief Set the individual property to the given value.
228    *
229    * @param[in] index The index key used to reference this value within the initial property map.
230    *
231    * @param[in] propertyValue The value to set.
232    */
233   void DoSetProperty(Dali::Property::Index index, const Dali::Property::Value& propertyValue);
234
235   /**
236    * @brief Updates the text's renderer.
237    */
238   void UpdateRenderer();
239
240   /**
241    * @brief Removes the text's renderer.
242    */
243   void RemoveRenderer(Actor& actor);
244
245   /**
246    * @brief Create a texture in textureSet and add it.
247    * @param[in] textureSet The textureSet to which the texture will be added.
248    * @param[in] data The PixelData to be uploaded to texture
249    * @param[in] sampler The sampler.
250    * @param[in] textureSetIndex The Index of TextureSet.
251    */
252   void AddTexture(TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex);
253
254   /**
255    * @brief Convert the buffer to pixelData.
256    * @param[in] buffer The Buffer to be converted to pixelData.
257    * @param[in] width The width of pixel data.
258    * @param[in] height The height of pixel data.
259    * @param[in] offsetPosition The The buffer's start position.
260    * @param[in] textPixelFormat The PixelForma of text.
261    */
262   PixelData ConvertToPixelData(unsigned char* buffer, int width, int height, int offsetPosition, const Pixel::Format textPixelFormat);
263
264   /**
265    * @brief Create the text's texture.
266    * @param[in] info This is the information you need to create a Tiling.
267    * @param[in] renderer The renderer to which the TextureSet will be added.
268    * @param[in] sampler The sampler.
269    * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
270    * @param[in] containsColorGlyph Whether the text contains color glyph.
271    * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
272    * @param[in] isOverlayStyle Whether the style needs to overlay on the text (e.g. strikethrough, underline, etc.).
273    */
274   void CreateTextureSet(TilingInfo& info, Renderer& renderer, Sampler& sampler, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle);
275
276   /**
277    * Create renderer of the text for rendering.
278    * @param[in] actor The actor.
279    * @param[in] size The texture size.
280    * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
281    * @param[in] containsColorGlyph Whether the text contains color glyph.
282    * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
283    * @param[in] isOverlayStyle Whether the style needs to overlay on the text (e.g. strikethrough, underline, etc.).
284    */
285   void AddRenderer(Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle);
286
287   /**
288    * Get the texture of the text for rendering.
289    * @param[in] size The texture size.
290    * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
291    * @param[in] containsColorGlyph Whether the text contains color glyph.
292    * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
293    * @param[in] isOverlayStyle Whether the style needs to overlay on the text (e.g. strikethrough, underline, etc.).
294    */
295   TextureSet GetTextTexture(const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle);
296
297   /**
298    * Get the text rendering shader.
299    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
300    * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
301    * @param[in] containsColorGlyph Whether the text contains color glyph.
302    * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
303    */
304   Shader GetTextShader(VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled);
305
306   /**
307    * @brief Retrieve the TextVisual object.
308    * @param[in] visual A handle to the TextVisual
309    * @return The TextVisual object
310    */
311   static TextVisual& GetVisualObject(Toolkit::Visual::Base visual)
312   {
313     return static_cast<TextVisual&>(Toolkit::GetImplementation(visual).GetVisualObject());
314   };
315
316 private:
317   typedef std::vector<Renderer> RendererContainer;
318
319   /**
320    * Used as an alternative to boolean so that it is obvious whether the text contains single or multiple text colors, and emoji and styles.
321    */
322   struct TextType
323   {
324     enum Type
325     {
326       SINGLE_COLOR_TEXT = 0, ///< The text contains single color only.
327       MULTI_COLOR_TEXT  = 1, ///< The text contains multiple colors.
328       NO_EMOJI          = 0, ///< The text contains no emoji.
329       HAS_EMOJI         = 1, ///< The text contains emoji.
330       NO_STYLES         = 0, ///< The text contains contains no styles.
331       HAS_SYLES         = 1  ///< The text contains contains styles.
332     };
333   };
334
335 private:
336   Text::ControllerPtr mController;                       ///< The text's controller.
337   Text::TypesetterPtr mTypesetter;                       ///< The text's typesetter.
338   WeakHandle<Actor>   mControl;                          ///< The control where the renderer is added.
339   Constraint          mColorConstraint{};                ///< Color constraint
340   Constraint          mOpacityConstraint{};              ///< Opacity constraint
341   Property::Index     mAnimatableTextColorPropertyIndex; ///< The index of animatable text color property registered by the control.
342   Property::Index     mTextColorAnimatableIndex;         ///< The index of uTextColorAnimatable property.
343   bool                mRendererUpdateNeeded : 1;         ///< The flag to indicate whether the renderer needs to be updated.
344   RendererContainer   mRendererList;
345 };
346
347 } // namespace Internal
348
349 } // namespace Toolkit
350
351 } // namespace Dali
352
353 #endif /* DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H */