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