Merge "Changed Core::Update to add current time and vsync times as parameters. Moved...
[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/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 #include <dali/integration-api/text-array.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    * @return A smart-pointer to the newly allocated TextAttachment.
61    */
62   static TextAttachmentPtr New( const SceneGraph::Node& parentNode, const Integration::TextArray& text, FontPointer font );
63
64   /**
65    * Set the text label displayed by the attachment
66    * @param [in] text The new text label
67    */
68   void SetText(const Integration::TextArray& text);
69
70   /**
71    * Get the text label displayed by the attachment
72    * @return The text label
73    */
74   const Integration::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    * Set the text color
107    * @param[in] color The text color
108    */
109   void SetTextColor(const Vector4& color);
110
111   /**
112    * Get the text color
113    * @return The text color
114    */
115   Vector4 GetTextColor() const;
116
117   /**
118    * Resets to default the text color.
119    */
120   void ResetTextColor();
121
122   /**
123    * @copydoc Dali::TextActor::SetWeight()
124    */
125   void SetWeight( TextStyle::Weight weight );
126
127   /**
128    * @copydoc Dali::TextActor::GetWeight()
129    */
130   TextStyle::Weight GetWeight() const;
131
132   /**
133    * Resets to default the text weight.
134    */
135   void ResetWeight();
136
137   /**
138    * @copydoc Dali::TextActor::SetSmoothEdge(const float)
139    */
140   void SetSmoothEdge( float smoothEdge );
141
142   /**
143    * Retrieve the smooth edge value.
144    * @return the smooth edge value.
145    */
146   float GetSmoothEdge() const;
147
148   /**
149    * Resets to default the smooth edge.
150    */
151   void ResetSmoothEdge();
152
153   /**
154    * Enable italics on the text actor, the text will be sheared by the given angle.
155    * @param[in] angle Italics angle in radians.
156    */
157   void SetItalics( Radian angle );
158
159   /**
160    * @copydoc Dali::TextActor::GetItalics()
161    */
162   bool GetItalics() const;
163
164   /**
165    * @copydoc Dali::TextActor::GetItalicsAngle()
166    */
167   Radian GetItalicsAngle() const;
168
169   /**
170    * Resets to default the italics.
171    */
172   void ResetItalics();
173
174   /**
175    * @copydoc Dali::TextActor::SetUnderline()
176    */
177   void SetUnderline( bool enable, float thickness, float position );
178
179   /**
180    * @copydoc Dali::TextActor::GetUnderline()
181    */
182   bool GetUnderline() const;
183
184   /**
185    * @copydoc Internal::TextActor::GetUnderlineThickness()
186    */
187   float GetUnderlineThickness() const;
188
189   /**
190    * @copydoc Internal::TextActor::GetUnderlinePosition()
191    */
192   float GetUnderlinePosition() const;
193
194   /**
195    * Resets to default the text underline.
196    */
197   void ResetUnderline();
198
199   /**
200    * @copydoc Dali::TextActor::SetOutline(const bool,const Vector4&,const Vector2&)
201    */
202   void SetOutline( bool enable, const Vector4& color, const Vector2& thickness );
203
204   /**
205    * Get outline state.
206    * @return \e true if outline is enabled, otherwise \e false.
207    */
208   bool GetOutline() const;
209
210   /**
211    * Retrieve outline parameters.
212    * @param[out] color The outline color.
213    * @param[out] thickness The outline parameters.
214    */
215   void GetOutlineParams( Vector4& color, Vector2& thickness ) const;
216
217   /**
218    * Resets to default the text outline.
219    */
220   void ResetOutline();
221
222   /**
223    * @copydoc Dali::TextActor::SetGlow(const bool,const Vector4&,const float)
224    */
225   void SetGlow( bool enable, const Vector4& color, float intensity );
226
227   /**
228    * Get glow state.
229    * @return \e true if glow is enabled, otherwise \e false.
230    */
231   bool GetGlow() const;
232
233   /**
234    * Retrieve glow parameters.
235    * @param[out] color The glow color.
236    * @param[out] intensity The glow intensity.
237    */
238   void GetGlowParams( Vector4& color, float& intensity ) const;
239
240   /**
241    * Resets to default the text glow.
242    */
243   void ResetGlow();
244
245   /**
246    * @copydoc Dali::TextActor::SetShadow(const bool,const Vector4&,const Vector2&,const float)
247    */
248   void SetShadow( bool enable, const Vector4& color, const Vector2& offset, float size );
249
250   /**
251    * Retrieve the shadow state.
252    * @return \e true if shadow is enabled, otherwise \e false.
253    */
254   bool GetShadow() const;
255
256   /**
257    * Retrieve shadow parameters.
258    * @param[out] color The shadow color.
259    * @param[out] offset The shadow offset.
260    * @param[out] size The shadow size.
261    */
262   void GetShadowParams( Vector4& color, Vector2& offset, float& size ) const;
263
264   /**
265    * Resets to default the text shadow.
266    */
267   void ResetShadow();
268
269   /**
270    * @see Dali::TextActor::SetGradientColor()
271    * @see Dali::TextActor::SetGradientStartPoint()
272    * @see Dali::TextActor::SetGradientEndPoint()
273    */
274   void SetGradient( const Vector4& color, const Vector2& startPoint, const Vector2& endPoint );
275
276   /**
277    * @copydoc Dali::TextActor::GetGradientColor()
278    */
279   const Vector4& GetGradientColor() const;
280
281   /**
282    * @copydoc Dali::TextActor::GetGradientStartPoint()
283    */
284   const Vector2& GetGradientStartPoint() const;
285
286   /**
287    * @copydoc Dali::TextActor::GetGradientEndPoint()
288    */
289   const Vector2& GetGradientEndPoint() const;
290
291   /**
292    * Resets to default the text gradient.
293    */
294   void ResetGradient();
295
296   /**
297    * Retrieves the text style.
298    *
299    * @param[out] style The text style.
300    */
301   void GetTextStyle( TextStyle& style ) const;
302
303   /**
304    * Measure the natural size of a text string, as displayed in this font.
305    * @return The natural size of the text.
306    */
307   Vector3 MeasureText() const;
308
309   /**
310    * Inform the text attachment the text / font it is using has changed.
311    * The TextChanged() and TextNotRequired() functions are used to avoid duplicate
312    * requests for text. For example if the text is changed, and the font is changed
313    * then we want to avoid requesting the new text with the old font, then the new
314    * text with the new font.
315    */
316   void TextChanged();
317
318   /**
319    * @return true if all text has been loaded and is ready to display
320    */
321   bool IsTextLoaded();
322
323   /**
324    * Calculates the weighted smoothing value.
325    */
326   void CalculateWeightedSmoothing( TextStyle::Weight weight, float smoothEdge );
327
328 public: // glyph texture observer
329
330   /**
331    *  @copydoc Dali::Internal::GlyphTextureObserver::TextureResized()
332    */
333   virtual void TextureResized( const TextureIdList& oldTextureIds, unsigned int newTextureId );
334
335   /**
336    * @copydoc Dali::Internal::GlyphTextureObserver::TextureSplit()
337    */
338   virtual void TextureSplit( FontId fontId, const TextureIdList& oldTextureIds, unsigned int newTextureId );
339
340 private:
341
342   /**
343    * Check if the text has been modified
344    * @return true if the text or it's formating has been modified
345    */
346    bool IsTextModified();
347
348   /**
349    * First stage construction of a TextAttachment.
350    * @param[in] stage Used to send messages to scene-graph.
351    */
352   TextAttachment( Stage& stage );
353
354   /**
355    * @copydoc Dali::Internal::RenderableAttachment::OnStageConnection2()
356    */
357   virtual void OnStageConnection2();
358
359   /**
360    * @copydoc Dali::Internal::RenderableAttachment::OnStageDisconnection2()
361    */
362   virtual void OnStageDisconnection2();
363
364   /**
365    * @copydoc Dali::Internal::RenderableAttachment::GetSceneObject()
366    */
367   virtual const SceneGraph::RenderableAttachment& GetSceneObject() const;
368
369   /**
370    * Update any text & font changes to the on stage scene graph text attachment
371    */
372   void SetTextChanges();
373
374 protected:
375
376   /**
377    * A reference counted object may only be deleted by calling Unreference()
378    */
379   virtual ~TextAttachment();
380
381 private:
382
383   const SceneGraph::TextAttachment* mSceneObject; ///< Not owned
384
385   TextRequestHelper mTextRequestHelper;           ///< Text request helper
386   FontPointer mFont;                              ///< The font resource
387   Vector4* mTextColor;                            ///< on demand storage for text color
388   TextStyle mStyle;                               ///< on demand storage for text effects parameters ( do not store font parameters and color )
389                                                   ///< Font parameters are stored in the FontPointer member. Inside the TextStyle, font parameters and color are allocated in the same struct,
390                                                   ///< so store the text color in the style will allocate space for the font parameters as well, wasting space.
391   // Cached values for public getters
392   Integration::TextArray mText;
393
394   bool    mTextChanged:1;       ///< whether the text has changed
395   bool    mFontChanged:1;       ///< whether the font has changed
396   bool    mUnderlineChanged:1;  ///< whether the underline has changed
397   bool    mItalicsChanged:1;    ///< whether the italics has changed
398   bool    mTextureIdSet:1;      ///< flag to signify if the texture id has been set on the scene-graph text-attachment
399   unsigned int mTextureId;      ///< the texture id of the glyph atlas being used
400   Vector2 mTextSize;            ///< text natural size
401
402   OwnerPointer< TextVertexBuffer > mVertexBuffer; ///< vertex data to display the text
403 };
404
405 } // namespace Internal
406
407 } // namespace Dali
408
409 #endif // __DALI_INTERNAL_TEXT_ATTACHMENT_H__