Merge "Dummy graphics controller for test suite" into devel/graphics
[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/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] propertyMap containing string keys or a mix of strings and indexes.
85    * @return Property::Map containing index keys.
86    */
87   static Property::Map ConvertStringKeysToIndexKeys( const Property::Map& propertyMap );
88
89   /**
90    * @brief Retrieve the text's controller.
91    * @param[in] visual The text visual.
92    * @return The text controller
93    */
94   static Text::ControllerPtr GetController( Toolkit::Visual::Base visual )
95   {
96     return GetVisualObject( visual ).mController;
97   };
98
99   /**
100    * @brief Set the index of the animatable text color property.
101    * @param[in] visual The text visual.
102    * @param[in] animatablePropertyIndex The index of the animatable property
103    */
104   static void SetAnimatableTextColorProperty( Toolkit::Visual::Base visual, Property::Index animatablePropertyIndex )
105   {
106     GetVisualObject( visual ).mAnimatableTextColorPropertyIndex = animatablePropertyIndex;
107   };
108
109   /**
110    * @brief Set the flag to trigger the textures to be initialized and renderer to be added to the control.
111    * @param[in] visual The text visual.
112    */
113   static void EnableRendererUpdate( Toolkit::Visual::Base visual )
114   {
115     GetVisualObject( visual ).mRendererUpdateNeeded = true;
116   };
117
118   /**
119    * @brief Instantly updates the renderer
120    * @param[in] visual The text visual.
121    */
122   static void UpdateRenderer( Toolkit::Visual::Base visual )
123   {
124     GetVisualObject( visual ).UpdateRenderer();
125   };
126
127 public: // from Visual::Base
128
129   /**
130    * @copydoc Visual::Base::GetHeightForWidth()
131    */
132   float GetHeightForWidth( float width ) override;
133
134   /**
135    * @copydoc Visual::Base::GetNaturalSize()
136    */
137   void GetNaturalSize( Vector2& naturalSize ) override;
138
139   /**
140    * @copydoc Visual::Base::CreatePropertyMap()
141    */
142   void DoCreatePropertyMap( Property::Map& map ) const override;
143
144   /**
145    * @copydoc Visual::Base::CreateInstancePropertyMap
146    */
147   void DoCreateInstancePropertyMap( Property::Map& map ) const override;
148
149 protected:
150
151   /**
152    * @brief Constructor.
153    *
154    * @param[in] factoryCache The VisualFactoryCache object
155    */
156   TextVisual( VisualFactoryCache& factoryCache );
157
158   /**
159    * @brief A reference counted object may only be deleted by calling Unreference().
160    */
161   virtual ~TextVisual();
162
163   /**
164    * @copydoc Visual::Base::OnInitialize
165    */
166   void OnInitialize() override;
167
168   // from Visual::Base
169
170   /**
171    * @copydoc Visual::Base::DoSetProperties()
172    */
173   void DoSetProperties( const Property::Map& propertyMap ) override;
174
175   /**
176    * @copydoc Visual::Base::DoSetOnScene()
177    */
178   void DoSetOnScene( Actor& actor ) override;
179
180   /**
181    * @copydoc Visual::Base::DoSetOffScene()
182    */
183   void DoSetOffScene( Actor& actor ) override;
184
185   /**
186    * @copydoc Visual::Base::OnSetTransform
187    */
188   void OnSetTransform() override;
189
190 private:
191
192   struct TilingInfo
193   {
194     unsigned char* textBuffer;
195     unsigned char* styleBuffer;
196     unsigned char* maskBuffer;
197     int width;
198     int height;
199     Pixel::Format textPixelFormat;
200     int offsetPosition;
201     Vector2 offSet;
202
203     TilingInfo( int width, int height, Pixel::Format textPixelFormat )
204     : textBuffer( NULL ),
205       styleBuffer( NULL ),
206       maskBuffer( NULL ),
207       width( width ),
208       height( height ),
209       textPixelFormat( textPixelFormat ),
210       offsetPosition( 0 ),
211       offSet( 0.f, 0.f )
212     {
213     }
214
215     ~TilingInfo()
216     {
217       if( textBuffer )
218       {
219         free( textBuffer );
220       }
221       if( styleBuffer )
222       {
223         free( styleBuffer );
224       }
225       if( maskBuffer )
226       {
227         free( maskBuffer );
228       }
229     }
230
231   };
232
233   /**
234    * @brief Set the individual property to the given value.
235    *
236    * @param[in] index The index key used to reference this value within the initial property map.
237    *
238    * @param[in] propertyValue The value to set.
239    */
240   void DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue );
241
242   /**
243    * @brief Updates the text's renderer.
244    */
245   void UpdateRenderer();
246
247   /**
248    * @brief Removes the text's renderer.
249    */
250   void RemoveRenderer( Actor& actor );
251
252   /**
253    * @brief Create a texture in textureSet and add it.
254    * @param[in] textureSet The textureSet to which the texture will be added.
255    * @param[in] data The PixelData to be uploaded to texture
256    * @param[in] sampler The sampler.
257    * @param[in] textureSetIndex The Index of TextureSet.
258    */
259   void AddTexture( TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex );
260
261   /**
262    * @brief Convert the buffer to pixelData.
263    * @param[in] buffer The Buffer to be converted to pixelData.
264    * @param[in] width The width of pixel data.
265    * @param[in] height The height of pixel data.
266    * @param[in] offsetPosition The The buffer's start position.
267    * @param[in] textPixelFormat The PixelForma of text.
268    */
269   PixelData ConvertToPixelData( unsigned char* buffer, int width, int height, int offsetPosition, const Pixel::Format textPixelFormat );
270
271   /**
272    * @brief Create the text's texture.
273    * @param[in] info This is the information you need to create a Tiling.
274    * @param[in] renderer The renderer to which the TextureSet will be added.
275    * @param[in] sampler The sampler.
276    * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
277    * @param[in] containsColorGlyph Whether the text contains color glyph.
278    * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
279    */
280   void CreateTextureSet( TilingInfo& info, Renderer& renderer, Sampler& sampler,  bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled );
281
282   /**
283    * Create renderer of the text for rendering.
284    * @param[in] actor The actor.
285    * @param[in] size The texture size.
286    * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
287    * @param[in] containsColorGlyph Whether the text contains color glyph.
288    * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
289    */
290   void AddRenderer( Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled );
291
292
293   /**
294    * Get the texture of the text for rendering.
295    * @param[in] size The texture size.
296    * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
297    * @param[in] containsColorGlyph Whether the text contains color glyph.
298    * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
299    */
300   TextureSet GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled );
301
302   /**
303    * Get the text rendering shader.
304    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
305    * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
306    * @param[in] containsColorGlyph Whether the text contains color glyph.
307    * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
308    */
309   Shader GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled );
310
311   /**
312    * @brief Retrieve the TextVisual object.
313    * @param[in] visual A handle to the TextVisual
314    * @return The TextVisual object
315    */
316   static TextVisual& GetVisualObject( Toolkit::Visual::Base visual )
317   {
318     return static_cast< TextVisual& >( Toolkit::GetImplementation( visual ).GetVisualObject() );
319   };
320
321 private:
322
323   typedef std::vector< Renderer > RendererContainer;
324
325   /**
326    * Used as an alternative to boolean so that it is obvious whether the text contains single or multiple text colors, and emoji and styles.
327    */
328   struct TextType
329   {
330     enum Type
331     {
332       SINGLE_COLOR_TEXT = 0, ///< The text contains single color only.
333       MULTI_COLOR_TEXT = 1,  ///< The text contains multiple colors.
334       NO_EMOJI = 0,          ///< The text contains no emoji.
335       HAS_EMOJI = 1,         ///< The text contains emoji.
336       NO_STYLES = 0,         ///< The text contains contains no styles.
337       HAS_SYLES = 1          ///< The text contains contains styles.
338     };
339   };
340
341
342 private:
343   Text::ControllerPtr mController;                        ///< The text's controller.
344   Text::TypesetterPtr mTypesetter;                        ///< The text's typesetter.
345   WeakHandle<Actor>   mControl;                           ///< The control where the renderer is added.
346   Property::Index     mAnimatableTextColorPropertyIndex;  ///< The index of animatable text color property registered by the control.
347   bool                mRendererUpdateNeeded:1;            ///< The flag to indicate whether the renderer needs to be updated.
348   RendererContainer   mRendererList;
349 };
350
351 } // namespace Internal
352
353 } // namespace Toolkit
354
355 } // namespace Dali
356
357 #endif /* DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H */