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