[dali_1.0.52] Merge branch 'devel/master'
[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   // Character <--> Glyph conversion
92
93   /**
94    * @brief Retrieves for each character the number of glyphs the character is shaped.
95    *
96    * @param[out] glyphsPerCharacter Pointer to a buffer where the number of glyphs for each character are copied.
97    * @param[in] characterIndex Index to the first character.
98    * @param[in] numberOfCharacters The number of characters.
99    */
100   void GetGlyphsPerCharacterMap( Length* glyphsPerCharacter,
101                                  CharacterIndex characterIndex,
102                                  Length numberOfCharacters ) const;
103
104   // Position interface
105
106   /**
107    * @brief Retrieves the glyph positions.
108    *
109    * @pre The size of the @p positions buffer needs to be big enough to copy the @p numberOfGlyphs positions.
110    * @param[out] glyphPositions Pointer to a buffer where the glyph positions are copied.
111    * @param[in] glyphIndex Index to the first glyph position.
112    * @param[in] numberOfGlyphs The number of positions to be copied.
113    */
114   void GetGlyphPositions( Vector2* glyphPositions,
115                           GlyphIndex glyphIndex,
116                           Length numberOfGlyphs ) const;
117
118   // Line interface.
119
120   /**
121    * @brief Retrieves the number of lines and the index to the first line where the given range of glyphs is laid out.
122    *
123    * @param[in] glyphIndex Index to the first glyph.
124    * @param[in] numberOfGlyphs The number of glyph.
125    * @param[out] firstLine Index to the line containing the glyph index.
126    * @param[out] numberOfLines The number of lines.
127    */
128   void GetNumberOfLines( GlyphIndex glyphIndex,
129                          Length numberOfGlyphs,
130                          LineIndex& firstLine,
131                          Length& numberOfLines ) const;
132
133   /**
134    * @brief Retrieves the lines where the given range of glyphs is laid out.
135    *
136    * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
137    *
138    * @param[out] lines Pointer to a buffer where the lines are copied.
139    * @param[in] glyphIndex Index to the first glyphs of the range.
140    * @param[in] numberOfGlyphs Number of glyphs in the range.
141    */
142   void GetLinesOfGlyphRange( LineRun* lines,
143                              GlyphIndex glyphIndex,
144                              Length numberOfGlyphs ) const;
145
146   /**
147    * @brief Retrieves the line index where the character is laid-out.
148    *
149    * @param[in] characterIndex The character's index.
150    *
151    * @return The line index.
152    */
153   LineIndex GetLineOfCharacter( CharacterIndex characterIndex );
154
155   // Underline runs
156
157   /**
158    * @brief Retrieves the underline runs.
159    *
160    * @param[out] underlineRuns Pointer to a buffer where the underline runs are copied.
161    * @param[in] index Index of the first underline run to be copied.
162    * @param[in] numberOfRuns Number of underline runs to be copied.
163    */
164   void GetUnderlineRuns( GlyphRun* underlineRuns,
165                          UnderlineRunIndex index,
166                          Length numberOfRuns ) const;
167
168   // Size interface
169
170   /**
171    * @brief Sets the natural size.
172    *
173    * @param[in] size The text's natural size.
174    */
175   void SetNaturalSize( const Vector2& size  );
176
177   /**
178    * @brief Retrieves the natural size.
179    *
180    * @return The text's natural size.
181    */
182   const Vector2& GetNaturalSize() const;
183
184   /**
185    * @brief Sets the text's actual size after it has been laid out.
186    *
187    * @param[in] size The text's size.
188    */
189   void SetActualSize( const Vector2& size );
190
191   /**
192    * @brief Retrieves the text's actual size after it has been laid out.
193    *
194    * @return The text's size.
195    */
196   const Vector2& GetActualSize() const;
197
198   /**
199    * @brief Set the text's color
200    *
201    * @param[in] textColor The text's color
202    */
203   void SetTextColor( const Vector4& textColor );
204
205   /**
206    * @brief Retrieve the text's color
207    *
208    * @return The text's color
209    */
210   const Vector4& GetTextColor() const;
211
212   /**
213    * @brief Sets the text's shadow offset.
214    *
215    * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
216    */
217   void SetShadowOffset( const Vector2& shadowOffset );
218
219   /**
220    * @brief Retrieves the text's shadow offset.
221    *
222    * @return The text's shadow offset, 0,0 indicates no shadow.
223    */
224   const Vector2& GetShadowOffset() const;
225
226   /**
227    * @brief Sets the text's shadow color.
228    *
229    * @param[in] shadowColor The shadow color.
230    */
231   void SetShadowColor( const Vector4& shadowColor );
232
233   /**
234    * @brief Retrieves the text's shadow color.
235    *
236    * @return The text's shadow color.
237    */
238   const Vector4& GetShadowColor() const;
239
240   /**
241    * @brief Sets the text's underline color.
242    *
243    * @param[in] color The text's underline color.
244    */
245   void SetUnderlineColor( const Vector4& color );
246
247   /**
248    * @brief Retrieves the text's underline color.
249    *
250    * @return The text's underline color.
251    */
252   const Vector4& GetUnderlineColor() const;
253
254   /**
255    * @brief Sets the text underline flag.
256    *
257    * @param[in] enabled true if underlined.
258    */
259   void SetUnderlineEnabled( bool enabled );
260
261   /**
262    * @brief Returns whether the text is underlined or not.
263    *
264    * @return underline state.
265    */
266   bool IsUnderlineEnabled() const;
267
268   /**
269    * @brief Clear the caches.
270    */
271   void ClearCaches();
272
273   /**
274    * @brief Set the override used for underline height, 0 indicates height will be come from font metrics
275    *
276    * @param[in] height The height in pixels of the underline
277    */
278   void SetUnderlineHeight( float height );
279
280   /**
281    * @brief Retrieves the underline height override
282    *
283    * @return Returns the override height for an underline, 0 indicates that font metrics will determine the height
284    */
285   float GetUnderlineHeight() const;
286
287 protected:
288
289   /**
290    * @brief A reference counted object may only be deleted by calling Unreference().
291    */
292   virtual ~VisualModel();
293
294 private:
295
296   /**
297    * @brief Private constructor.
298    */
299   VisualModel();
300
301   // Undefined
302   VisualModel( const VisualModel& handle );
303
304   // Undefined
305   VisualModel& operator=( const VisualModel& handle );
306
307 public:
308
309   Vector<GlyphInfo>      mGlyphs;               ///< For each glyph, the font's id, glyph's index within the font and glyph's metrics.
310   Vector<CharacterIndex> mGlyphsToCharacters;   ///< For each glyph, the index of the first character.
311   Vector<GlyphIndex>     mCharactersToGlyph;    ///< For each character, the index of the first glyph.
312   Vector<Length>         mCharactersPerGlyph;   ///< For each glyph, the number of characters that form the glyph.
313   Vector<Length>         mGlyphsPerCharacter;   ///< For each character, the number of glyphs that are shaped.
314   Vector<Vector2>        mGlyphPositions;       ///< For each glyph, the position.
315   Vector<LineRun>        mLines;                ///< The laid out lines.
316   Vector<GlyphRun>       mUnderlineRuns;        ///< Runs of glyphs that are underlined.
317
318   Vector2                mControlSize;           ///< The size of the UI control the decorator is adding it's decorations to.
319   Vector4                mTextColor;            ///< The text color
320   Vector4                mShadowColor;          ///< Color of drop shadow
321   Vector4                mUnderlineColor;       ///< Color of underline
322   Vector2                mShadowOffset;         ///< Offset for drop shadow, 0 indicates no shadow
323   float                  mUnderlineHeight;      ///< Fixed height for underline to override font metrics.
324
325 private:
326
327   Size                   mNaturalSize;        ///< Size of the text with no line wrapping.
328   Size                   mActualSize;         ///< Size of the laid-out text considering the layout properties set.
329
330   // Caches to increase performance in some consecutive operations.
331   LineIndex mCachedLineIndex; ///< Used to increase performance in consecutive calls to GetLineOfGlyph() or GetLineOfCharacter() with consecutive glyphs or characters.
332
333 public:
334
335   bool                   mUnderlineEnabled:1;   ///< Underline enabled flag
336   bool                   mUnderlineColorSet:1;  ///< Has the underline color been explicitly set?
337
338 };
339
340 } // namespace Text
341
342 } // namespace Toolkit
343
344 } // namespace Dali
345
346 #endif // __DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H__