Merge "TextModel interface" into new_text
[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 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/intrusive-ptr.h>
23 #include <dali/public-api/object/ref-object.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/text/text-definitions.h>
27
28 namespace Dali
29 {
30
31 struct Vector2;
32
33 namespace Toolkit
34 {
35
36 namespace Text
37 {
38
39 struct LineRun;
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 Replaces any glyphs previously set.
65    *
66    * @note If the number of glyphs is zero, all buffers are cleared.
67    * @note If one pointer is NULL and the number of glyphs is not zero, the buffer is not touched.
68    *
69    * @param[in] glyphs An array of glyphs in the visual order.
70    * @param[in] characterIndices An array containing the first character in the logical model that each glyph relates to.
71    * @param[in] charactersPerGlyph An array containing the number of characters per glyph.
72    * @param[in] numberOfGlyphs The number of glyphs.
73    */
74   void SetGlyphs( const GlyphInfo* glyphs,
75                   const CharacterIndex* characterIndices,
76                   const Length* charactersPerGlyph,
77                   Length numberOfGlyphs );
78
79   /**
80    * Retrieves the number of glyphs.
81    *
82    * @return The number of glyphs.
83    */
84   Length GetNumberOfGlyphs() const;
85
86   /**
87    * @brief Retrieves glyphs in the given buffer.
88    *
89    * The size of the @p glyphs buffer needs to be big enough to copy the @p numberOfGlyphs.
90    * @param[out] glyphs Pointer to a buffer where the glyphs are copied.
91    * @param[in] glyphIndex Index to the first glyph.
92    * @param[in] numberOfGlyphs Number of glyphs to be copied.
93    */
94   void GetGlyphs( GlyphInfo* glyphs,
95                   GlyphIndex glyphIndex,
96                   Length numberOfGlyphs ) const;
97
98   /**
99    * Retrieves a glyph.
100    *
101    * @param[in] glyphIndex Index to a glyph.
102    *
103    * @return A glyph.
104    */
105   const GlyphInfo& GetGlyphInfo( GlyphIndex glyphIndex ) const;
106
107   /**
108    * Replaces glyphs.
109    *
110    * If the @p numberOfGlyphsToRemove is zero, this operation is like an insert.
111    * If the @p numberOfGlyphsToInsert is zero, this operation is like a remove.
112    *
113    * @param[in] glyphIndex Where to replace the glyphs.
114    * @param[in] numberOfGlyphsToRemove The number of glyphs to be removed.
115    * @param[in] glyphs Pointer to a buffer with the new glyphs.
116    * @param[in] numberOfCharacters Pointer to a buffer with the number of characters per glyph.
117    * @param[in] numberOfGlyphsToInsert The number of new glyphs in the buffer.
118    */
119   void ReplaceGlyphs( GlyphIndex glyphIndex,
120                       Length numberOfGlyphsToRemove,
121                       const GlyphInfo* const glyphs,
122                       const Length* const numberOfCharacters,
123                       Length numberOfGlyphsToInsert );
124
125   // Character <--> Glyph conversion
126
127   /**
128    * @brief Retrieves the first character in the logical model which a glyph represents.
129    *
130    * @note After shaping several characters may be represented by the same glyph.
131    * Alternatively several glyphs may be required to display a character.
132    * @param[in] glyphIndex The glyph index.
133    * @return The character index.
134    */
135   CharacterIndex GetCharacterIndex( GlyphIndex glyphIndex ) const;
136
137   /**
138    * @brief Query the number of characters the glyph represents.
139    *
140    * @param[in] glyphIndex The glyph index.
141    * @return The number of characters represented by the glyph.
142    */
143   Length GetCharactersPerGlyph( GlyphIndex glyphIndex ) const;
144
145   /**
146    * Retrieves the first glyph in the visual model which represents a given character.
147    *
148    * @note After shaping several characters may be represented by the same glyph.
149    * Alternatively several glyphs may be required to display a character.
150    * @param[in] characterIndex The character index.
151    * @return The glyph index.
152    */
153   GlyphIndex GetGlyphIndex( CharacterIndex characterIndex ) const;
154
155   /**
156    * Retrieves the whole or part of the character to glyph conversion map.
157    *
158    * The size of the buffer needs to be big enough to copy the @p numberOfCharacters.
159    *
160    * @param[out] characterToGlyphMap Pointer to a buffer where the conversion map is copied.
161    * @param[in] characterIndex Index to the first character.
162    * @param[in] numberOfCharacters The number of characters.
163    */
164   void GetCharacterToGlyphMap( GlyphIndex* characterToGlyphMap,
165                                CharacterIndex characterIndex,
166                                Length numberOfCharacters ) const;
167
168   /**
169    * Retrieves the whole or part of the glyph to character conversion map.
170    *
171    * The size of the buffer needs to be big enough to copy the @p numberOfGlyphs.
172    *
173    * @param[out] glyphToCharacter Pointer to a buffer where the conversion map is copied.
174    * @param[in] glyphIndex Index to the first glyph.
175    * @param[in] numberOfGlyphs The number of glyphs.
176    */
177   void GetGlyphToCharacterMap( CharacterIndex* glyphToCharacter,
178                                GlyphIndex glyphIndex,
179                                Length numberOfGlyphs ) const;
180
181   /**
182    * Retrieves for each glyph the number of characters the glyph represents.
183    *
184    * @param[out] charactersPerGlyph Pointer to a buffer where the number of characters for each glyph are copied.
185    * @param[in] glyphIndex Index to the first glyph.
186    * @param[in] numberOfGlyphs The number of glyphs.
187    */
188   void GetCharactersPerGlyphMap( Length* charactersPerGlyph,
189                                  GlyphIndex glyphIndex,
190                                  Length numberOfGlyphs ) const;
191
192   /**
193    * Retrieves for each character the number of glyphs the character is shaped.
194    *
195    * @param[out] glyphsPerCharacter Pointer to a buffer where the number of glyphs for each character are copied.
196    * @param[in] characterIndex Index to the first character.
197    * @param[in] numberOfCharacters The number of characters.
198    */
199   void GetGlyphsPerCharacterMap( Length* glyphsPerCharacter,
200                                  CharacterIndex characterIndex,
201                                  Length numberOfCharacters ) const;
202
203   // Position interface
204
205   /**
206    * @brief Replaces any glyph positions previously set.
207    *
208    * @note If the number of glyphs is zero the position buffer is cleared.
209    *
210    * @param[in] glyphPositions An array of visual positions for each glyph.
211    * @param[in] numberOfGlyphs The number of positions.
212    */
213   void SetGlyphPositions( const Vector2* glyphPositions,
214                           Length numberOfGlyphs );
215
216   /**
217    * Retrieves the number of glyph positions set.
218    *
219    * @note This may be less than the number of glyphs in the model.
220    * @return The number of glyphs.
221    */
222   Length GetNumberOfGlyphPositions() const;
223
224   /**
225    * @brief Retrieves the glyph positions.
226    *
227    * @pre The size of the @p positions buffer needs to be big enough to copy the @p numberOfGlyphs positions.
228    * @param[out] glyphPositions Pointer to a buffer where the glyph positions are copied.
229    * @param[in] glyphIndex Index to the first glyph position.
230    * @param[in] numberOfGlyphs The number of positions to be copied.
231    */
232   void GetGlyphPositions( Vector2* glyphPositions,
233                           GlyphIndex glyphIndex,
234                           Length numberOfGlyphs ) const;
235
236   /**
237    * Retrieve the glyph's position of the given glyph.
238    *
239    * @param[in] glyphIndex Index to the glyph.
240    *
241    * @return The glyph's position.
242    */
243   const Vector2& GetGlyphPosition( GlyphIndex glyphIndex ) const;
244
245   /**
246    * Replaces glyph's positions.
247    *
248    * If the @p numberOfGlyphsToRemove is zero, this operation is like an insert.
249    * If the @p numberOfGlyphsToInsert is zero, this operation is like a remove.
250    *
251    * @param[in] glyphIndex Where to replace the glyph's positions.
252    * @param[in] numberOfGlyphsToRemove The number of glyph's positions to be removed.
253    * @param[in] positions Pointer to a buffer with the new glyph's positions.
254    * @param[in] numberOfGlyphsToInsert The number of new glyph's positions in the buffer.
255    */
256   void ReplaceGlyphPositions( GlyphIndex glyphIndex,
257                               Length numberOfGlyphsToRemove,
258                               const Vector2* const positions,
259                               Length numberOfGlyphsToInsert );
260
261   // Line interface.
262
263   /**
264    * Sets the lines.
265    *
266    * Replaces any lines previously set.
267    *
268    * Every line is an item run containing the index to the first glyph of the line and the number of glyphs.
269    *
270    * @note If the number of lines is zero or the pointer is NULL, the lines buffer is cleared.
271    *
272    * @param[in] lines Pointer to a buffer containing all the line runs.
273    * @param[in] numberOfLines The number of lines in the buffer.
274    */
275   void SetLines( const LineRun* const lines,
276                  Length numberOfLines );
277
278   /**
279    * Retrieves the number of lines of the whole text.
280    *
281    * @return The number of lines.
282    */
283   Length GetNumberOfLines() const;
284
285   /**
286    * Retrieves lines.
287    *
288    * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
289    *
290    * @param[out] lines Pointer to a buffer where the lines are copied.
291    * @param[in] lineIndex Index to the first line.
292    * @param[in] numberOfLines Number of lines to be copied.
293    */
294   void GetLines( LineRun* lines,
295                  LineIndex lineIndex,
296                  Length numberOfLines ) const;
297
298   /**
299    * Retrieves the number of lines where the given range of glyphs is laid out.
300    *
301    * @param[in] glyphIndex Index to the first glyph.
302    * @param[in] numberOfGlyphs The number of glyph.
303    *
304    * @return The number of lines.
305    */
306   Length GetNumberOfLines( GlyphIndex glyphIndex,
307                            Length numberOfGlyphs ) const;
308   /**
309    * Retrieves the lines where the given range of glyphs is laid out.
310    *
311    * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
312    *
313    * @param[out] lines Pointer to a buffer where the lines are copied.
314    * @param[in] glyphIndex Index to the first glyphs of the range.
315    * @param[in] numberOfGlyphs Number of glyphs in the range.
316    */
317   void GetLinesOfGlyphRange( LineRun* lines,
318                              GlyphIndex glyphIndex,
319                              Length numberOfGlyphs ) const;
320
321   /**
322    * Replaces lines for the given range of glyphs.
323    *
324    * If the @p numberOfGlyphsToRemove is zero, this operation is like an insert.
325    * If the @p numberOfGlyphsToInsert is zero, this operation is like a remove.
326    *
327    * @param[in] glyphIndex Index of the first glyph where to replace the line info.
328    * @param[in] numberOfGlyphsToRemove The number of glyphs to be the line info removed.
329    * @param[in] lines Pointer to a buffer with the lines.
330    * @param[in] numberOfGlyphsToInsert The number of characters to be the line info inserted.
331    */
332   void ReplaceLines( GlyphIndex glyphIndex,
333                      Length numberOfGlyphsToRemove,
334                      const LineRun* const lines,
335                      Length numberOfGlyphsToInsert );
336
337   // Size interface
338
339   /**
340    * Sets the natural size.
341    *
342    * @param[in] size The text's natural size.
343    */
344   void SetNaturalSize( const Vector2& size  );
345
346   /**
347    * Retrieves the natural size.
348    *
349    * @return The text's natural size.
350    */
351   const Vector2& GetNaturalSize() const;
352
353   /**
354    * Sets the text's actual size after it has been laid out.
355    *
356    * @param[in] size The text's size.
357    */
358   void SetActualSize( const Vector2& size );
359
360   /**
361    * Retrieves the text's actual size after it has been laid out.
362    *
363    * @return The text's size.
364    */
365   const Vector2& GetActualSize() const;
366
367 protected:
368
369   /**
370    * @brief A reference counted object may only be deleted by calling Unreference().
371    */
372   virtual ~VisualModel();
373
374 private:
375
376   /**
377    * @brief Private constructor.
378    */
379   VisualModel();
380
381   // Undefined
382   VisualModel( const VisualModel& handle );
383
384   // Undefined
385   VisualModel& operator=( const VisualModel& handle );
386
387 private:
388
389   struct Impl;
390   Impl* mImpl;
391 };
392 } // namespace Text
393
394 } // namespace Toolkit
395
396 } // namespace Dali
397
398 #endif // __DALI_TOOLKIT_TEXT_VISUAL_MODEL_H__