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