9593fd8a917ca25e4db7a9b26dd6f60b14f76a46
[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    * Retrieves the number of glyph positions set.
187    *
188    * @note This may be less than the number of glyphs in the model.
189    * @return The number of glyphs.
190    */
191   Length GetNumberOfGlyphPositions() const;
192
193   /**
194    * @brief Retrieves the glyph positions.
195    *
196    * @pre The size of the @p positions buffer needs to be big enough to copy the @p numberOfGlyphs positions.
197    * @param[out] glyphPositions Pointer to a buffer where the glyph positions are copied.
198    * @param[in] glyphIndex Index to the first glyph position.
199    * @param[in] numberOfGlyphs The number of positions to be copied.
200    */
201   void GetGlyphPositions( Vector2* glyphPositions,
202                           GlyphIndex glyphIndex,
203                           Length numberOfGlyphs ) const;
204
205   /**
206    * Retrieve the glyph's position of the given glyph.
207    *
208    * @param[in] glyphIndex Index to the glyph.
209    *
210    * @return The glyph's position.
211    */
212   const Vector2& GetGlyphPosition( GlyphIndex glyphIndex ) const;
213
214   // Line interface.
215
216   /**
217    * Sets the lines.
218    *
219    * Replaces any lines previously set.
220    *
221    * Every line is an item run containing the index to the first glyph of the line and the number of glyphs.
222    *
223    * @note If the number of lines is zero or the pointer is NULL, the lines buffer is cleared.
224    *
225    * @param[in] lines Pointer to a buffer containing all the line runs.
226    * @param[in] numberOfLines The number of lines in the buffer.
227    */
228   void SetLines( const LineRun* const lines,
229                  Length numberOfLines );
230
231   /**
232    * Retrieves the number of lines of the whole text.
233    *
234    * @return The number of lines.
235    */
236   Length GetNumberOfLines() const;
237
238   /**
239    * Retrieves lines.
240    *
241    * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
242    *
243    * @param[out] lines Pointer to a buffer where the lines are copied.
244    * @param[in] lineIndex Index to the first line.
245    * @param[in] numberOfLines Number of lines to be copied.
246    */
247   void GetLines( LineRun* lines,
248                  LineIndex lineIndex,
249                  Length numberOfLines ) const;
250
251   /**
252    * Retrieves the number of lines where the given range of glyphs is laid out.
253    *
254    * @param[in] glyphIndex Index to the first glyph.
255    * @param[in] numberOfGlyphs The number of glyph.
256    *
257    * @return The number of lines.
258    */
259   Length GetNumberOfLines( GlyphIndex glyphIndex,
260                            Length numberOfGlyphs ) const;
261   /**
262    * Retrieves the lines where the given range of glyphs is laid out.
263    *
264    * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
265    *
266    * @param[out] lines Pointer to a buffer where the lines are copied.
267    * @param[in] glyphIndex Index to the first glyphs of the range.
268    * @param[in] numberOfGlyphs Number of glyphs in the range.
269    */
270   void GetLinesOfGlyphRange( LineRun* lines,
271                              GlyphIndex glyphIndex,
272                              Length numberOfGlyphs ) const;
273
274   // Size interface
275
276   /**
277    * Sets the natural size.
278    *
279    * @param[in] size The text's natural size.
280    */
281   void SetNaturalSize( const Vector2& size  );
282
283   /**
284    * Retrieves the natural size.
285    *
286    * @return The text's natural size.
287    */
288   const Vector2& GetNaturalSize() const;
289
290   /**
291    * Sets the text's actual size after it has been laid out.
292    *
293    * @param[in] size The text's size.
294    */
295   void SetActualSize( const Vector2& size );
296
297   /**
298    * Retrieves the text's actual size after it has been laid out.
299    *
300    * @return The text's size.
301    */
302   const Vector2& GetActualSize() const;
303
304 protected:
305
306   /**
307    * @brief A reference counted object may only be deleted by calling Unreference().
308    */
309   virtual ~VisualModel();
310
311 private:
312
313   /**
314    * @brief Private constructor.
315    */
316   VisualModel();
317
318   // Undefined
319   VisualModel( const VisualModel& handle );
320
321   // Undefined
322   VisualModel& operator=( const VisualModel& handle );
323
324 private:
325
326   struct Impl;
327   Impl* mImpl;
328 };
329 } // namespace Text
330
331 } // namespace Toolkit
332
333 } // namespace Dali
334
335 #endif // __DALI_TOOLKIT_TEXT_VISUAL_MODEL_H__