License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-core.git] / dali / internal / event / actor-attachments / text-attachment-impl.h
1 #ifndef __DALI_INTERNAL_TEXT_ATTACHMENT_H__
2 #define __DALI_INTERNAL_TEXT_ATTACHMENT_H__
3
4 /*
5  * Copyright (c) 2014 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 // INTERNAL INCLUDES
22 #include <dali/public-api/math/radian.h>
23 #include <dali/public-api/math/vector2.h>
24 #include <dali/public-api/math/vector4.h>
25 #include <dali/public-api/text/text-style.h>
26 #include <dali/internal/event/actor-attachments/renderable-attachment-impl.h>
27 #include <dali/internal/event/actor-attachments/actor-attachment-declarations.h>
28 #include <dali/internal/event/text/font-declarations.h>
29 #include <dali/internal/common/text-array.h>
30 #include <dali/internal/event/text/text-request-helper.h>
31 #include <dali/internal/event/text/resource/glyph-texture-observer.h>
32 #include <dali/internal/common/owner-pointer.h>
33
34 namespace Dali
35 {
36
37 namespace Internal
38 {
39
40 class TextParameters;
41
42 namespace SceneGraph
43 {
44 class TextAttachment;
45 class Node;
46 }
47
48 /**
49  * An attachment for rendering text from a font atlas.
50  */
51 class TextAttachment : public RenderableAttachment, public GlyphTextureObserver
52 {
53 public:
54
55   /**
56    * Create a new TextAttachment.
57    * @param[in] parentNode The node to attach a scene-object to.
58    * @param [in] text The text which will be displayed.
59    * @param [in] font The font which will be used for the text.
60    * @param [in] isLeftToRight Text is displayed from left to right if true, otherwise from right to left.
61    * @return A smart-pointer to the newly allocated TextAttachment.
62    */
63   static TextAttachmentPtr New( const SceneGraph::Node& parentNode, const TextArray& text, FontPointer font, bool isLeftToRight );
64
65   /**
66    * Set the text label displayed by the attachment
67    * @param [in] text The new text label
68    */
69   void SetText(const TextArray& text);
70
71   /**
72    * Get the text label displayed by the attachment
73    * @return The text label
74    */
75   const TextArray& GetText() const
76   {
77     // This is not animatable; the cached value is up-to-date.
78     return mText;
79   }
80
81   /**
82    * Get the natural text size
83    * @return the natural size of the text.
84    */
85   const Vector2& GetNaturalTextSize() const
86   {
87     // This is not animatable; the cached value is up-to-date.
88     return mTextSize;
89   }
90
91   /**
92    * Set the font used to display the text label displayed by the attachment
93    * @param [in] font The new font
94    */
95   void SetFont(Font& font);
96
97   /**
98    * Get the font used to display the text label displayed by the attachment
99    * @return The font currently in use
100    */
101   Font& GetFont()
102   {
103     return *mFont;
104   }
105
106   /**
107    * @copydoc Dali::TextActor::SetGradientColor()
108    */
109   void SetGradientColor( const Vector4& color );
110
111   /**
112    * @copydoc Dali::TextActor::GetGradientColor()
113    */
114   const Vector4& GetGradientColor() const;
115
116   /**
117    * @copydoc Dali::TextActor::SetGradientStartPoint()
118    */
119   void SetGradientStartPoint( const Vector2& position );
120
121   /**
122    * @copydoc Dali::TextActor::GetGradientStartPoint()
123    */
124   const Vector2& GetGradientStartPoint() const;
125
126   /**
127    * @copydoc Dali::TextActor::SetGradientEndPoint()
128    */
129   void SetGradientEndPoint( const Vector2& position );
130
131   /**
132    * @copydoc Dali::TextActor::GetGradientEndPoint()
133    */
134   const Vector2& GetGradientEndPoint() const;
135
136   /**
137    * @copydoc Dali::TextActor::SetSmoothEdge(const float)
138    */
139   void SetSmoothEdge(const float smoothEdge);
140
141   /**
142    * Retrieve the smooth edge value.
143    * @return the smooth edge value.
144    */
145   float GetSmoothEdge() const;
146
147   /**
148    * @copydoc Dali::TextActor::SetOutline(const bool,const Vector4&,const Vector2&)
149    */
150   void SetOutline( bool enable, const Vector4& color, const Vector2& thickness );
151
152   /**
153    * Get outline state.
154    * @return \e true if outline is enabled, otherwise \e false.
155    */
156   bool GetOutline() const;
157
158   /**
159    * Retrieve outline parameters.
160    * @param[out] color The outline color.
161    * @param[out] thickness The outline parameters.
162    */
163   void GetOutlineParams( Vector4& color, Vector2& thickness ) const;
164
165   /**
166    * @copydoc Dali::TextActor::SetGlow(const bool,const Vector4&,const float)
167    */
168   void SetGlow( bool enable, const Vector4& color, const float intensity);
169
170   /**
171    * Get glow state.
172    * @return \e true if glow is enabled, otherwise \e false.
173    */
174   bool GetGlow() const;
175
176   /**
177    * Retrieve glow parameters.
178    * @param[out] color The glow color.
179    * @param[out] intensity The glow intensity.
180    */
181   void GetGlowParams( Vector4& color, float& intensity ) const;
182
183   /**
184    * @copydoc Dali::TextActor::SetShadow(const bool,const Vector4&,const Vector2&,const float)
185    */
186   void SetShadow( bool enable, const Vector4& color, const Vector2& offset, const float size);
187
188   /**
189    * Retrieve the shadow state.
190    * @return \e true if shadow is enabled, otherwise \e false.
191    */
192   bool GetShadow() const;
193
194   /**
195    * Retrieve shadow parameters.
196    * @param[out] color The shadow color.
197    * @param[out] offset The shadow offset.
198    * @param[out] size The shadow size.
199    */
200   void GetShadowParams( Vector4& color, Vector2& offset, float& size ) const;
201
202   /**
203    * Set the text color
204    * @param[in] color The text color
205    */
206   void SetTextColor(const Vector4& color);
207
208   /**
209    * Get the text color
210    * @return The text color
211    */
212   Vector4 GetTextColor() const;
213
214   /**
215    * Enable italics on the text actor, the text will be sheared by the given angle.
216    * @param[in] angle Italics angle in radians.
217    */
218   void SetItalics( const Radian& angle );
219
220   /**
221    * @copydoc Dali::TextActor::GetItalics()
222    */
223   const Radian& GetItalics() const;
224
225   /**
226    * @copydoc Dali::TextActor::SetUnderline()
227    */
228   void SetUnderline( bool enable, float thickness, float position );
229
230   /**
231    * @copydoc Dali::TextActor::GetUnderline()
232    */
233   bool GetUnderline() const;
234
235   /**
236    * @copydoc Internal::TextActor::GetUnderlineThickness()
237    */
238   float GetUnderlineThickness() const;
239
240   /**
241    * @copydoc Internal::TextActor::GetUnderlinePosition()
242    */
243   float GetUnderlinePosition() const;
244
245   /**
246    * @copydoc Dali::TextActor::SetWeight()
247    */
248   void SetWeight( TextStyle::Weight weight );
249
250   /**
251    * @copydoc Dali::TextActor::GetWeight()
252    */
253   TextStyle::Weight GetWeight() const;
254
255   /**
256    * Measure the natural size of a text string, as displayed in this font.
257    * @return The natural size of the text.
258    */
259   Vector3 MeasureText() const;
260
261   /**
262    * Inform the text attachment the text / font it is using has changed.
263    * The TextChanged() and TextNotRequired() functions are used to avoid duplicate
264    * requests for text. For example if the text is changed, and the font is changed
265    * then we want to avoid requesting the new text with the old font, then the new
266    * text with the new font.
267    */
268   void TextChanged();
269
270   /**
271    * @return true if all text has been loaded and is ready to display
272    */
273   bool IsTextLoaded();
274
275 public: // glyph texture observer
276
277   /**
278    *  @copydoc Dali::Internal::GlyphTextureObserver::TextureResized()
279    */
280   virtual void TextureResized( const TextureIdList& oldTextureIds, unsigned int newTextureId );
281
282   /**
283    * @copydoc Dali::Internal::GlyphTextureObserver::TextureSplit()
284    */
285   virtual void TextureSplit( FontId fontId, const TextureIdList& oldTextureIds, unsigned int newTextureId );
286
287 private:
288
289   /**
290    * Check if the text has been modified
291    * @return true if the text or it's formating has been modified
292    */
293    bool IsTextModified();
294
295   /**
296    * Allocate a TextParameters object on this TextAttachment if one does not exist
297    */
298   void AllocateTextParameters();
299
300   /**
301    * First stage construction of a TextAttachment.
302    * @param[in] stage Used to send messages to scene-graph.
303    */
304   TextAttachment( Stage& stage );
305
306   /**
307    * @copydoc Dali::Internal::RenderableAttachment::OnStageConnection2()
308    */
309   virtual void OnStageConnection2();
310
311   /**
312    * @copydoc Dali::Internal::RenderableAttachment::OnStageDisconnection2()
313    */
314   virtual void OnStageDisconnection2();
315
316   /**
317    * @copydoc Dali::Internal::RenderableAttachment::GetSceneObject()
318    */
319   virtual const SceneGraph::RenderableAttachment& GetSceneObject() const;
320
321   /**
322    * Update any text & font changes to the on stage scene graph text attachment
323    */
324   void SetTextChanges();
325
326   /**
327    * Send new texture-id message
328    */
329   void SendTextureIdMessage();
330
331   /**
332    * Make a request to load the current text
333    * @param uniqueText the text to request to be loaded
334    */
335   void RequestText( const TextArray& uniqueText );
336
337 protected:
338
339   /**
340    * A reference counted object may only be deleted by calling Unreference()
341    */
342   virtual ~TextAttachment();
343
344 private:
345
346   const SceneGraph::TextAttachment* mSceneObject; ///< Not owned
347
348   TextRequestHelper mTextRequestHelper;  ///< Text request helper
349   FontPointer mFont; ///< The font resource
350
351   // Cached values for public getters
352   TextArray mText;
353
354   bool    mUnderlineEnabled:1;  ///< whether the text is underlined
355   bool    mIsLeftToRight:1;     ///< whether the text is left to right
356   bool    mTextChanged:1;       ///< whether the text has changed
357   bool    mFontChanged:1;       ///< whether the font has changed
358   bool    mUnderlineChanged:1;  ///< whether the underline has changed
359   bool    mItalicsChanged:1;    ///< whether the italics has changed
360   bool    mItalicsEnabled:1;    ///< whether italics are enabled
361   bool    mTextureIdSet:1;      ///< flag to signify if the texture id has been set on the scene-graph text-attachment
362   unsigned int mTextureId;      ///< the texture id of the glyph atlas being used
363   float   mSmoothing;           ///< edge smoothing for the font
364   Radian  mItalicsAngle;        ///< font italics angle
365   float mUnderlineThickness;    ///< The underline's thickness.
366   float mUnderlinePosition;     ///< The underline's position.
367   Vector2 mTextSize;            ///< text natural size
368   TextStyle::Weight mWeight;    ///< font weight
369
370   OwnerPointer< TextVertexBuffer > mVertexBuffer; ///< vertex data to display the text
371   OwnerPointer< TextParameters > mTextParameters; ///< on demand storage for text effect parameters
372   Vector4* mTextColor;                            ///< on demand storage for text color
373 };
374
375 } // namespace Internal
376
377 } // namespace Dali
378
379 #endif // __DALI_INTERNAL_TEXT_ATTACHMENT_H__