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