Changes required after WeakHandle moved to Public API in Core
[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) 2017 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 public: // from Visual::Base
118
119   /**
120    * @copydoc Visual::Base::GetHeightForWidth()
121    */
122   virtual float GetHeightForWidth( float width );
123
124   /**
125    * @copydoc Visual::Base::GetNaturalSize()
126    */
127   virtual void GetNaturalSize( Vector2& naturalSize );
128
129   /**
130    * @copydoc Visual::Base::CreatePropertyMap()
131    */
132   virtual void DoCreatePropertyMap( Property::Map& map ) const;
133
134   /**
135    * @copydoc Visual::Base::CreateInstancePropertyMap
136    */
137   virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
138
139 protected:
140
141   /**
142    * @brief Constructor.
143    *
144    * @param[in] factoryCache The VisualFactoryCache object
145    */
146   TextVisual( VisualFactoryCache& factoryCache );
147
148   /**
149    * @brief A reference counted object may only be deleted by calling Unreference().
150    */
151   virtual ~TextVisual();
152
153   // from Visual::Base
154
155   /**
156    * @copydoc Visual::Base::DoSetProperties()
157    */
158   virtual void DoSetProperties( const Property::Map& propertyMap );
159
160   /**
161    * @copydoc Visual::Base::DoSetOnStage()
162    */
163   virtual void DoSetOnStage( Actor& actor );
164
165   /**
166    * @copydoc Visual::Base::DoSetOffStage()
167    */
168   virtual void DoSetOffStage( Actor& actor );
169
170   /**
171    * @copydoc Visual::Base::OnSetTransform
172    */
173   virtual void OnSetTransform();
174
175 private:
176   /**
177    * @brief Set the individual property to the given value.
178    *
179    * @param[in] index The index key used to reference this value within the initial property map.
180    *
181    * @param[in] propertyValue The value to set.
182    */
183   void DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue );
184
185   /**
186    * @brief Updates the text's renderer.
187    */
188   void UpdateRenderer();
189
190   /**
191    * @brief Removes the texture set from the renderer.
192    */
193   void RemoveTextureSet();
194
195   /**
196    * Get the texture of the text for rendering.
197    * @param[in] size The texture size.
198    * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
199    * @param[in] containsEmoji Whether the text contains emoji.
200    * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
201    */
202   TextureSet GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsEmoji, bool styleEnabled );
203
204   /**
205    * Get the text rendering shader.
206    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
207    * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
208    * @param[in] containsEmoji Whether the text contains emoji.
209    * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
210    */
211   Shader GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsEmoji, bool styleEnabled );
212
213   /**
214    * @brief Retrieve the text's controller.
215    * @param[in] visual The text visual.
216    * @return The text controller
217    */
218   static TextVisual& GetVisualObject( Toolkit::Visual::Base visual )
219   {
220     return static_cast<TextVisual&>( visual.GetBaseObject() );
221   };
222
223 private:
224
225   /**
226    * Used as an alternative to boolean so that it is obvious whether the text contains single or multiple text colors, and emoji and styles.
227    */
228   struct TextType
229   {
230     enum Type
231     {
232       SINGLE_COLOR_TEXT = 0, ///< The text contains single color only.
233       MULTI_COLOR_TEXT = 1,  ///< The text contains multiple colors.
234       NO_EMOJI = 0,          ///< The text contains no emoji.
235       HAS_EMOJI = 1,         ///< The text contains emoji.
236       NO_STYLES = 0,         ///< The text contains contains no styles.
237       HAS_SYLES = 1          ///< The text contains contains styles.
238     };
239   };
240
241 private:
242   Text::ControllerPtr mController;                        ///< The text's controller.
243   Text::TypesetterPtr mTypesetter;                        ///< The text's typesetter.
244   WeakHandle<Actor>   mControl;                           ///< The control where the renderer is added.
245   Property::Index     mAnimatableTextColorPropertyIndex;  ///< The index of animatable text color property registered by the control.
246   bool                mRendererUpdateNeeded:1;            ///< The flag to indicate whether the renderer needs to be updated.
247 };
248
249 } // namespace Internal
250
251 } // namespace Toolkit
252
253 } // namespace Dali
254
255 #endif /* DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H */