Fix corner radius shader of the ImageVisual
[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) 2019 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/object/weak-handle.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
27 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
28 #include <dali-toolkit/internal/text/text-controller.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
38
39 class TextVisual;
40 typedef IntrusivePtr< TextVisual > TextVisualPtr;
41
42 /**
43  * The visual which renders text
44  *
45  * The following properties are optional:
46  *
47  * | %Property Name      | Type    |
48  * |---------------------|---------|
49  * | renderingBackend    | INTEGER |
50  * | text                | STRING  |
51  * | fontFamily          | STRING  |
52  * | fontStyle           | STRING  |
53  * | pointSize           | FLOAT   |
54  * | multiLine           | BOOLEAN |
55  * | horizontalAlignment | STRING  |
56  * | verticalAlignment   | STRING  |
57  * | textColor           | VECTOR4 |
58  * | enableMarkup        | BOOLEAN |
59  * | enableAutoScroll    | BOOLEAN |
60  * | autoScrollSpeed     | INTEGER |
61  * | autoScrollLoopCount | INTEGER |
62  * | autoScrollGap       | INTEGER |
63  * | lineSpacing         | FLOAT   |
64  * | underline           | STRING  |
65  * | shadow              | STRING  |
66  * | outline             | STRING  |
67  *
68  */
69 class TextVisual : public Visual::Base
70 {
71 public:
72
73   /**
74    * @brief Create a new text visual.
75    *
76    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
77    * @param[in] properties A Property::Map containing settings for this visual
78    * @return A smart-pointer to the newly allocated visual.
79    */
80   static TextVisualPtr New( VisualFactoryCache& factoryCache, const Property::Map& properties );
81
82   /**
83    * @brief Converts all strings keys in property map to index keys.  Property Map can then be merged correctly.
84    * @param[in,out] propertyMap containing string keys or a mix of strings and indexes. Will be changed to index keys.
85    */
86   static void ConvertStringKeysToIndexKeys( Property::Map& propertyMap );
87
88   /**
89    * @brief Retrieve the text's controller.
90    * @param[in] visual The text visual.
91    * @return The text controller
92    */
93   static Text::ControllerPtr GetController( Toolkit::Visual::Base visual )
94   {
95     return GetVisualObject( visual ).mController;
96   };
97
98   /**
99    * @brief Set the index of the animatable text color property.
100    * @param[in] visual The text visual.
101    * @param[in] animatablePropertyIndex The index of the animatable property
102    */
103   static void SetAnimatableTextColorProperty( Toolkit::Visual::Base visual, Property::Index animatablePropertyIndex )
104   {
105     GetVisualObject( visual ).mAnimatableTextColorPropertyIndex = animatablePropertyIndex;
106   };
107
108   /**
109    * @brief Set the flag to trigger the textures to be initialized and renderer to be added to the control.
110    * @param[in] visual The text visual.
111    */
112   static void EnableRendererUpdate( Toolkit::Visual::Base visual )
113   {
114     GetVisualObject( visual ).mRendererUpdateNeeded = true;
115   };
116
117   /**
118    * @brief Instantly updates the renderer
119    * @param[in] visual The text visual.
120    */
121   static void UpdateRenderer( Toolkit::Visual::Base visual )
122   {
123     GetVisualObject( visual ).UpdateRenderer();
124   };
125
126 public: // from Visual::Base
127
128   /**
129    * @copydoc Visual::Base::GetHeightForWidth()
130    */
131   float GetHeightForWidth( float width ) override;
132
133   /**
134    * @copydoc Visual::Base::GetNaturalSize()
135    */
136   void GetNaturalSize( Vector2& naturalSize ) override;
137
138   /**
139    * @copydoc Visual::Base::CreatePropertyMap()
140    */
141   void DoCreatePropertyMap( Property::Map& map ) const override;
142
143   /**
144    * @copydoc Visual::Base::CreateInstancePropertyMap
145    */
146   void DoCreateInstancePropertyMap( Property::Map& map ) const override;
147
148 protected:
149
150   /**
151    * @brief Constructor.
152    *
153    * @param[in] factoryCache The VisualFactoryCache object
154    */
155   TextVisual( VisualFactoryCache& factoryCache );
156
157   /**
158    * @brief A reference counted object may only be deleted by calling Unreference().
159    */
160   virtual ~TextVisual();
161
162   // from Visual::Base
163
164   /**
165    * @copydoc Visual::Base::DoSetProperties()
166    */
167   void DoSetProperties( const Property::Map& propertyMap ) override;
168
169   /**
170    * @copydoc Visual::Base::DoSetOnStage()
171    */
172   void DoSetOnStage( Actor& actor ) override;
173
174   /**
175    * @copydoc Visual::Base::DoSetOffStage()
176    */
177   void DoSetOffStage( Actor& actor ) override;
178
179   /**
180    * @copydoc Visual::Base::OnSetTransform
181    */
182   void OnSetTransform() override;
183
184 private:
185   /**
186    * @brief Set the individual property to the given value.
187    *
188    * @param[in] index The index key used to reference this value within the initial property map.
189    *
190    * @param[in] propertyValue The value to set.
191    */
192   void DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue );
193
194   /**
195    * @brief Updates the text's renderer.
196    */
197   void UpdateRenderer();
198
199   /**
200    * @brief Removes the texture set from the renderer.
201    */
202   void RemoveTextureSet();
203
204   /**
205    * Get the texture of the text for rendering.
206    * @param[in] size The texture size.
207    * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
208    * @param[in] containsColorGlyph Whether the text contains color glyph.
209    * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
210    */
211   TextureSet GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled );
212
213   /**
214    * Get the text rendering shader.
215    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
216    * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
217    * @param[in] containsColorGlyph Whether the text contains color glyph.
218    * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
219    */
220   Shader GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled );
221
222   /**
223    * @brief Retrieve the TextVisual object.
224    * @param[in] visual A handle to the TextVisual
225    * @return The TextVisual object
226    */
227   static TextVisual& GetVisualObject( Toolkit::Visual::Base visual )
228   {
229     return static_cast< TextVisual& >( Toolkit::GetImplementation( visual ).GetVisualObject() );
230   };
231
232 private:
233
234   /**
235    * Used as an alternative to boolean so that it is obvious whether the text contains single or multiple text colors, and emoji and styles.
236    */
237   struct TextType
238   {
239     enum Type
240     {
241       SINGLE_COLOR_TEXT = 0, ///< The text contains single color only.
242       MULTI_COLOR_TEXT = 1,  ///< The text contains multiple colors.
243       NO_EMOJI = 0,          ///< The text contains no emoji.
244       HAS_EMOJI = 1,         ///< The text contains emoji.
245       NO_STYLES = 0,         ///< The text contains contains no styles.
246       HAS_SYLES = 1          ///< The text contains contains styles.
247     };
248   };
249
250 private:
251   Text::ControllerPtr mController;                        ///< The text's controller.
252   Text::TypesetterPtr mTypesetter;                        ///< The text's typesetter.
253   WeakHandle<Actor>   mControl;                           ///< The control where the renderer is added.
254   Property::Index     mAnimatableTextColorPropertyIndex;  ///< The index of animatable text color property registered by the control.
255   bool                mRendererUpdateNeeded:1;            ///< The flag to indicate whether the renderer needs to be updated.
256 };
257
258 } // namespace Internal
259
260 } // namespace Toolkit
261
262 } // namespace Dali
263
264 #endif /* DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H */