TextModel - Clear the buffers if the number of items is zero.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / visual-model.h
1 #ifndef __DALI_TOOLKIT_TEXT_VISUAL_MODEL_H__
2 #define __DALI_TOOLKIT_TEXT_VISUAL_MODEL_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 // INTERNAL INCLUDES
22 #include <dali/public-api/common/intrusive-ptr.h>
23 #include <dali/public-api/object/ref-object.h>
24 #include <dali-toolkit/internal/text/text-definitions.h>
25
26 namespace Dali
27 {
28
29 struct Vector2;
30
31 namespace Toolkit
32 {
33
34 namespace Text
35 {
36
37 struct LineRun;
38 class VisualModel;
39 typedef IntrusivePtr<VisualModel> VisualModelPtr;
40
41 /**
42  * @brief A visual text model contains layout specific information.
43  *
44  * This includes:
45  * - A series of glyphs in visual order i.e. after the bidirectional reordering.
46  * - The position of each glyph within a 2D bounding box.
47  */
48 class VisualModel : public RefObject
49 {
50 public:
51
52   /**
53    * @brief Create a new instance of a VisualModel.
54    *
55    * @return A pointer to a new VisualModel.
56    */
57   static VisualModelPtr New();
58
59   // Glyph interface.
60
61   /**
62    * @brief Replaces any glyphs previously set.
63    *
64    * @note If the number of glyphs is zero, all buffers are cleared.
65    * @note If one pointer is NULL and the number of glyphs is not zero, the buffer is not touched.
66    *
67    * @param[in] glyphs An array of glyphs in the visual order.
68    * @param[in] characterIndices An array containing the first character in the logical model that each glyph relates to.
69    * @param[in] charactersPerGlyph An array containing the number of characters per glyph.
70    * @param[in] numberOfGlyphs The number of glyphs.
71    */
72   void SetGlyphs( const GlyphInfo* glyphs,
73                   const CharacterIndex* characterIndices,
74                   const Length* charactersPerGlyph,
75                   Length numberOfGlyphs );
76
77   /**
78    * Retrieves the number of glyphs.
79    *
80    * @return The number of glyphs.
81    */
82   Length GetNumberOfGlyphs() const;
83
84   /**
85    * @brief Retrieves glyphs in the given buffer.
86    *
87    * The size of the @p glyphs buffer needs to be big enough to copy the @p numberOfGlyphs.
88    * @param[out] glyphs Pointer to a buffer where the glyphs are copied.
89    * @param[in] glyphIndex Index to the first glyph.
90    * @param[in] numberOfGlyphs Number of glyphs to be copied.
91    */
92   void GetGlyphs( GlyphInfo* glyphs,
93                   GlyphIndex glyphIndex,
94                   Length numberOfGlyphs ) const;
95
96   /**
97    * Retrieves a glyph.
98    *
99    * @param[in] glyphIndex Index to a glyph.
100    *
101    * @return A glyph.
102    */
103   const GlyphInfo& GetGlyphInfo( GlyphIndex glyphIndex ) const;
104
105   // Character <--> Glyph conversion
106
107   /**
108    * @brief Retrieves the first character in the logical model which a glyph represents.
109    *
110    * @note After shaping several characters may be represented by the same glyph.
111    * Alternatively several glyphs may be required to display a character.
112    * @param[in] glyphIndex The glyph index.
113    * @return The character index.
114    */
115   CharacterIndex GetCharacterIndex( GlyphIndex glyphIndex ) const;
116
117   /**
118    * @brief Query the number of characters the glyph represents.
119    *
120    * @param[in] glyphIndex The glyph index.
121    * @return The number of characters represented by the glyph.
122    */
123   Length GetCharactersPerGlyph( GlyphIndex glyphIndex ) const;
124
125   /**
126    * Retrieves the first glyph in the visual model which represents a given character.
127    *
128    * @note After shaping several characters may be represented by the same glyph.
129    * Alternatively several glyphs may be required to display a character.
130    * @param[in] characterIndex The character index.
131    * @return The glyph index.
132    */
133   GlyphIndex GetGlyphIndex( CharacterIndex characterIndex ) const;
134
135   /**
136    * Retrieves the whole or part of the character to glyph conversion map.
137    *
138    * The size of the buffer needs to be big enough to copy the @p numberOfCharacters.
139    *
140    * @param[out] characterToGlyphMap Pointer to a buffer where the conversion map is copied.
141    * @param[in] characterIndex Index to the first character.
142    * @param[in] numberOfCharacters The number of characters.
143    */
144   void GetCharacterToGlyphMap( GlyphIndex* characterToGlyphMap,
145                                CharacterIndex characterIndex,
146                                Length numberOfCharacters ) const;
147
148   /**
149    * Retrieves for each glyph the number of characters the glyph represents.
150    *
151    * @param[out] charactersPerGlyph Pointer to a buffer where the number of characters for each glyph are copied.
152    * @param[in] glyphIndex Index to the first glyph.
153    * @param[in] numberOfGlyphs The number of glyphs.
154    */
155   void GetCharactersPerGlyphMap( Length* charactersPerGlyph,
156                                  GlyphIndex glyphIndex,
157                                  Length numberOfGlyphs ) const;
158
159   /**
160    * Retrieves the whole or part of the glyph to character conversion map.
161    *
162    * The size of the buffer needs to be big enough to copy the @p numberOfGlyphs.
163    *
164    * @param[out] glyphToCharacter Pointer to a buffer where the conversion map is copied.
165    * @param[in] glyphIndex Index to the first glyph.
166    * @param[in] numberOfGlyphs The number of glyphs.
167    */
168   void GetGlyphToCharacterMap( CharacterIndex* glyphToCharacter,
169                                GlyphIndex glyphIndex,
170                                Length numberOfGlyphs ) const;
171
172   // Position interface
173
174   /**
175    * @brief Replaces any glyph positions previously set.
176    *
177    * @note If the number of glyphs is zero the position buffer is cleared.
178    *
179    * @param[in] glyphPositions An array of visual positions for each glyph.
180    * @param[in] numberOfGlyphs The number of positions.
181    */
182   void SetGlyphPositions( const Vector2* glyphPositions,
183                           Length numberOfGlyphs );
184
185   /**
186    * @brief Retrieves the glyph positions.
187    *
188    * @pre The size of the @p positions buffer needs to be big enough to copy the @p numberOfGlyphs positions.
189    * @param[out] glyphPositions Pointer to a buffer where the glyph positions are copied.
190    * @param[in] glyphIndex Index to the first glyph position.
191    * @param[in] numberOfGlyphs The number of positions to be copied.
192    */
193   void GetGlyphPositions( Vector2* glyphPositions,
194                           GlyphIndex glyphIndex,
195                           Length numberOfGlyphs ) const;
196
197   /**
198    * Retrieve the glyph's position of the given glyph.
199    *
200    * @param[in] glyphIndex Index to the glyph.
201    *
202    * @return The glyph's position.
203    */
204   const Vector2& GetGlyphPosition( GlyphIndex glyphIndex ) const;
205
206   // Line interface.
207
208   /**
209    * Sets the lines.
210    *
211    * Replaces any lines previously set.
212    *
213    * Every line is an item run containing the index to the first glyph of the line and the number of glyphs.
214    *
215    * @note If the number of lines is zero or the pointer is NULL, the lines buffer is cleared.
216    *
217    * @param[in] lines Pointer to a buffer containing all the line runs.
218    * @param[in] numberOfLines The number of lines in the buffer.
219    */
220   void SetLines( const LineRun* const lines,
221                  Length numberOfLines );
222
223   /**
224    * Retrieves the number of lines of the whole text.
225    *
226    * @return The number of lines.
227    */
228   Length GetNumberOfLines() const;
229
230   /**
231    * Retrieves lines.
232    *
233    * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
234    *
235    * @param[out] lines Pointer to a buffer where the lines are copied.
236    * @param[in] lineIndex Index to the first line.
237    * @param[in] numberOfLines Number of lines to be copied.
238    */
239   void GetLines( LineRun* lines,
240                  LineIndex lineIndex,
241                  Length numberOfLines ) const;
242
243   /**
244    * Retrieves the number of lines where the given range of glyphs is laid out.
245    *
246    * @param[in] glyphIndex Index to the first glyph.
247    * @param[in] numberOfGlyphs The number of glyph.
248    *
249    * @return The number of lines.
250    */
251   Length GetNumberOfLines( GlyphIndex glyphIndex,
252                            Length numberOfGlyphs ) const;
253   /**
254    * Retrieves the lines where the given range of glyphs is laid out.
255    *
256    * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
257    *
258    * @param[out] lines Pointer to a buffer where the lines are copied.
259    * @param[in] glyphIndex Index to the first glyphs of the range.
260    * @param[in] numberOfGlyphs Number of glyphs in the range.
261    */
262   void GetLinesOfGlyphRange( LineRun* lines,
263                              GlyphIndex glyphIndex,
264                              Length numberOfGlyphs ) const;
265
266   // Size interface
267
268   /**
269    * Sets the natural size.
270    *
271    * @param[in] size The text's natural size.
272    */
273   void SetNaturalSize( const Vector2& size  );
274
275   /**
276    * Retrieves the natural size.
277    *
278    * @return The text's natural size.
279    */
280   const Vector2& GetNaturalSize() const;
281
282   /**
283    * Sets the text's actual size after it has been laid out.
284    *
285    * @param[in] size The text's size.
286    */
287   void SetActualSize( const Vector2& size );
288
289   /**
290    * Retrieves the text's actual size after it has been laid out.
291    *
292    * @return The text's size.
293    */
294   const Vector2& GetActualSize() const;
295
296 protected:
297
298   /**
299    * @brief A reference counted object may only be deleted by calling Unreference().
300    */
301   virtual ~VisualModel();
302
303 private:
304
305   /**
306    * @brief Private constructor.
307    */
308   VisualModel();
309
310   // Undefined
311   VisualModel( const VisualModel& handle );
312
313   // Undefined
314   VisualModel& operator=( const VisualModel& handle );
315
316 private:
317
318   struct Impl;
319   Impl* mImpl;
320 };
321 } // namespace Text
322
323 } // namespace Toolkit
324
325 } // namespace Dali
326
327 #endif // __DALI_TOOLKIT_TEXT_VISUAL_MODEL_H__