52d2d311b86f78badcdcbd8d14928e07b45d4573
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / visual-model-impl.h
1 #ifndef __DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H__
2 #define __DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H__
3
4 /*
5  * Copyright (c) 2015 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/common/dali-vector.h>
23 #include <dali/public-api/common/intrusive-ptr.h>
24 #include <dali/public-api/math/vector2.h>
25 #include <dali/public-api/math/vector4.h>
26 #include <dali/public-api/object/ref-object.h>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/internal/text/line-run.h>
30
31 namespace Dali
32 {
33
34 namespace Toolkit
35 {
36
37 namespace Text
38 {
39
40 class VisualModel;
41 typedef IntrusivePtr<VisualModel> VisualModelPtr;
42
43 /**
44  * @brief A visual text model contains layout specific information.
45  *
46  * This includes:
47  * - A series of glyphs in visual order i.e. after the bidirectional reordering.
48  * - The position of each glyph within a 2D bounding box.
49  */
50 class VisualModel : public RefObject
51 {
52 public:
53
54   /**
55    * @brief Create a new instance of a VisualModel.
56    *
57    * @return A pointer to a new VisualModel.
58    */
59   static VisualModelPtr New();
60
61   // Glyph interface.
62
63   /**
64    * @brief Creates the character to glyph conversion table.
65    *
66    * @pre The glyphs per character table needs to be created first.
67    *
68    * @param[in] numberOfCharacters The number of characters.
69    */
70   void CreateCharacterToGlyphTable( Length numberOfCharacters = 0u );
71
72   /**
73    * @brief Creates an array containing the number of glyphs per character.
74    *
75    * @param[in] numberOfCharacters The number of characters.
76    */
77   void CreateGlyphsPerCharacterTable( Length numberOfCharacters = 0u );
78
79   /**
80    * @brief Retrieves glyphs in the given buffer.
81    *
82    * The size of the @p glyphs buffer needs to be big enough to copy the @p numberOfGlyphs.
83    * @param[out] glyphs Pointer to a buffer where the glyphs are copied.
84    * @param[in] glyphIndex Index to the first glyph.
85    * @param[in] numberOfGlyphs Number of glyphs to be copied.
86    */
87   void GetGlyphs( GlyphInfo* glyphs,
88                   GlyphIndex glyphIndex,
89                   Length numberOfGlyphs ) const;
90
91   // Position interface
92
93   /**
94    * @brief Retrieves the glyph positions.
95    *
96    * @pre The size of the @p positions buffer needs to be big enough to copy the @p numberOfGlyphs positions.
97    * @param[out] glyphPositions Pointer to a buffer where the glyph positions are copied.
98    * @param[in] glyphIndex Index to the first glyph position.
99    * @param[in] numberOfGlyphs The number of positions to be copied.
100    */
101   void GetGlyphPositions( Vector2* glyphPositions,
102                           GlyphIndex glyphIndex,
103                           Length numberOfGlyphs ) const;
104
105   // Line interface.
106
107   /**
108    * @brief Retrieves the number of lines and the index to the first line where the given range of glyphs is laid out.
109    *
110    * @param[in] glyphIndex Index to the first glyph.
111    * @param[in] numberOfGlyphs The number of glyph.
112    * @param[out] firstLine Index to the line containing the glyph index.
113    * @param[out] numberOfLines The number of lines.
114    */
115   void GetNumberOfLines( GlyphIndex glyphIndex,
116                          Length numberOfGlyphs,
117                          LineIndex& firstLine,
118                          Length& numberOfLines ) const;
119
120   /**
121    * @brief Retrieves the lines where the given range of glyphs is laid out.
122    *
123    * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
124    *
125    * @param[out] lines Pointer to a buffer where the lines are copied.
126    * @param[in] glyphIndex Index to the first glyphs of the range.
127    * @param[in] numberOfGlyphs Number of glyphs in the range.
128    */
129   void GetLinesOfGlyphRange( LineRun* lines,
130                              GlyphIndex glyphIndex,
131                              Length numberOfGlyphs ) const;
132
133   /**
134    * @brief Retrieves the line index where the character is laid-out.
135    *
136    * @param[in] characterIndex The character's index.
137    *
138    * @return The line index.
139    */
140   LineIndex GetLineOfCharacter( CharacterIndex characterIndex );
141
142   // Underline runs
143
144   /**
145    * @brief Retrieves the underline runs.
146    *
147    * @param[out] underlineRuns Pointer to a buffer where the underline runs are copied.
148    * @param[in] index Index of the first underline run to be copied.
149    * @param[in] numberOfRuns Number of underline runs to be copied.
150    */
151   void GetUnderlineRuns( GlyphRun* underlineRuns,
152                          UnderlineRunIndex index,
153                          Length numberOfRuns ) const;
154
155   // Size interface
156
157   /**
158    * @brief Sets the natural size.
159    *
160    * @param[in] size The text's natural size.
161    */
162   void SetNaturalSize( const Vector2& size  );
163
164   /**
165    * @brief Retrieves the natural size.
166    *
167    * @return The text's natural size.
168    */
169   const Vector2& GetNaturalSize() const;
170
171   /**
172    * @brief Sets the text's actual size after it has been laid out.
173    *
174    * @param[in] size The text's size.
175    */
176   void SetActualSize( const Vector2& size );
177
178   /**
179    * @brief Retrieves the text's actual size after it has been laid out.
180    *
181    * @return The text's size.
182    */
183   const Vector2& GetActualSize() const;
184
185   /**
186    * @brief Set the text's color
187    *
188    * @param[in] textColor The text's color
189    */
190   void SetTextColor( const Vector4& textColor );
191
192   /**
193    * @brief Retrieve the text's color
194    *
195    * @return The text's color
196    */
197   const Vector4& GetTextColor() const;
198
199   /**
200    * @brief Sets the text's shadow offset.
201    *
202    * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
203    */
204   void SetShadowOffset( const Vector2& shadowOffset );
205
206   /**
207    * @brief Retrieves the text's shadow offset.
208    *
209    * @return The text's shadow offset, 0,0 indicates no shadow.
210    */
211   const Vector2& GetShadowOffset() const;
212
213   /**
214    * @brief Sets the text's shadow color.
215    *
216    * @param[in] shadowColor The shadow color.
217    */
218   void SetShadowColor( const Vector4& shadowColor );
219
220   /**
221    * @brief Retrieves the text's shadow color.
222    *
223    * @return The text's shadow color.
224    */
225   const Vector4& GetShadowColor() const;
226
227   /**
228    * @brief Sets the text's underline color.
229    *
230    * @param[in] color The text's underline color.
231    */
232   void SetUnderlineColor( const Vector4& color );
233
234   /**
235    * @brief Retrieves the text's underline color.
236    *
237    * @return The text's underline color.
238    */
239   const Vector4& GetUnderlineColor() const;
240
241   /**
242    * @brief Sets the text underline flag.
243    *
244    * @param[in] enabled true if underlined.
245    */
246   void SetUnderlineEnabled( bool enabled );
247
248   /**
249    * @brief Returns whether the text is underlined or not.
250    *
251    * @return underline state.
252    */
253   bool IsUnderlineEnabled() const;
254
255   /**
256    * @brief Clear the caches.
257    */
258   void ClearCaches();
259
260   /**
261    * @brief Set the override used for underline height, 0 indicates height will be come from font metrics
262    *
263    * @param[in] height The height in pixels of the underline
264    */
265   void SetUnderlineHeight( float height );
266
267   /**
268    * @brief Retrieves the underline height override
269    *
270    * @return Returns the override height for an underline, 0 indicates that font metrics will determine the height
271    */
272   float GetUnderlineHeight() const;
273
274 protected:
275
276   /**
277    * @brief A reference counted object may only be deleted by calling Unreference().
278    */
279   virtual ~VisualModel();
280
281 private:
282
283   /**
284    * @brief Private constructor.
285    */
286   VisualModel();
287
288   // Undefined
289   VisualModel( const VisualModel& handle );
290
291   // Undefined
292   VisualModel& operator=( const VisualModel& handle );
293
294 public:
295
296   Vector<GlyphInfo>      mGlyphs;               ///< For each glyph, the font's id, glyph's index within the font and glyph's metrics.
297   Vector<CharacterIndex> mGlyphsToCharacters;   ///< For each glyph, the index of the first character.
298   Vector<GlyphIndex>     mCharactersToGlyph;    ///< For each character, the index of the first glyph.
299   Vector<Length>         mCharactersPerGlyph;   ///< For each glyph, the number of characters that form the glyph.
300   Vector<Length>         mGlyphsPerCharacter;   ///< For each character, the number of glyphs that are shaped.
301   Vector<Vector2>        mGlyphPositions;       ///< For each glyph, the position.
302   Vector<LineRun>        mLines;                ///< The laid out lines.
303   Vector<GlyphRun>       mUnderlineRuns;        ///< Runs of glyphs that are underlined.
304
305   Vector2                mControlSize;           ///< The size of the UI control the decorator is adding it's decorations to.
306   Vector4                mTextColor;            ///< The text color
307   Vector4                mShadowColor;          ///< Color of drop shadow
308   Vector4                mUnderlineColor;       ///< Color of underline
309   Vector2                mShadowOffset;         ///< Offset for drop shadow, 0 indicates no shadow
310   float                  mUnderlineHeight;      ///< Fixed height for underline to override font metrics.
311
312 private:
313
314   Size                   mNaturalSize;        ///< Size of the text with no line wrapping.
315   Size                   mActualSize;         ///< Size of the laid-out text considering the layout properties set.
316
317   // Caches to increase performance in some consecutive operations.
318   LineIndex mCachedLineIndex; ///< Used to increase performance in consecutive calls to GetLineOfGlyph() or GetLineOfCharacter() with consecutive glyphs or characters.
319
320 public:
321
322   bool                   mUnderlineEnabled:1;   ///< Underline enabled flag
323   bool                   mUnderlineColorSet:1;  ///< Has the underline color been explicitly set?
324
325 };
326
327 } // namespace Text
328
329 } // namespace Toolkit
330
331 } // namespace Dali
332
333 #endif // __DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H__