Update keyboard focus direction enum for Control
[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/math/vector4.h>
25 #include <dali/public-api/object/ref-object.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/internal/text/line-run.h>
29
30 namespace Dali
31 {
32
33 struct Vector2;
34
35 namespace Toolkit
36 {
37
38 namespace Text
39 {
40
41 struct LineRun;
42 class VisualModel;
43 typedef IntrusivePtr<VisualModel> VisualModelPtr;
44
45 /**
46  * @brief A visual text model contains layout specific information.
47  *
48  * This includes:
49  * - A series of glyphs in visual order i.e. after the bidirectional reordering.
50  * - The position of each glyph within a 2D bounding box.
51  */
52 class VisualModel : public RefObject
53 {
54 public:
55
56   /**
57    * @brief Create a new instance of a VisualModel.
58    *
59    * @return A pointer to a new VisualModel.
60    */
61   static VisualModelPtr New();
62
63   // Glyph interface.
64
65   /**
66    * @brief Replaces any glyphs previously set.
67    *
68    * @note If the number of glyphs is zero, all buffers are cleared.
69    * @note If one pointer is NULL and the number of glyphs is not zero, the buffer is not touched.
70    *
71    * @param[in] glyphs An array of glyphs in the visual order.
72    * @param[in] characterIndices An array containing the first character in the logical model that each glyph relates to.
73    * @param[in] charactersPerGlyph An array containing the number of characters per glyph.
74    * @param[in] numberOfGlyphs The number of glyphs.
75    */
76   void SetGlyphs( const GlyphInfo* glyphs,
77                   const CharacterIndex* characterIndices,
78                   const Length* charactersPerGlyph,
79                   Length numberOfGlyphs );
80
81   /**
82    * @brief Creates the character to glyph conversion table.
83    *
84    * @pre The glyphs per character table needs to be created first.
85    *
86    * @param[in] numberOfCharacters The number of characters.
87    */
88   void CreateCharacterToGlyphTable( Length numberOfCharacters = 0u );
89
90   /**
91    * @brief Creates an array containing the number of glyphs per character.
92    *
93    * @param[in] numberOfCharacters The number of characters.
94    */
95   void CreateGlyphsPerCharacterTable( Length numberOfCharacters = 0u );
96
97   /**
98    * @brief Retrieves the number of glyphs.
99    *
100    * @return The number of glyphs.
101    */
102   Length GetNumberOfGlyphs() const;
103
104   /**
105    * @brief Retrieves glyphs in the given buffer.
106    *
107    * The size of the @p glyphs buffer needs to be big enough to copy the @p numberOfGlyphs.
108    * @param[out] glyphs Pointer to a buffer where the glyphs are copied.
109    * @param[in] glyphIndex Index to the first glyph.
110    * @param[in] numberOfGlyphs Number of glyphs to be copied.
111    */
112   void GetGlyphs( GlyphInfo* glyphs,
113                   GlyphIndex glyphIndex,
114                   Length numberOfGlyphs ) const;
115
116   /**
117    * @brief Retrieves a glyph.
118    *
119    * @param[in] glyphIndex Index to a glyph.
120    *
121    * @return A glyph.
122    */
123   const GlyphInfo& GetGlyphInfo( GlyphIndex glyphIndex ) const;
124
125   /**
126    * @brief Replaces glyphs.
127    *
128    * If the @p numberOfGlyphsToRemove is zero, this operation is like an insert.
129    * If the @p numberOfGlyphsToInsert is zero, this operation is like a remove.
130    *
131    * @param[in] glyphIndex Where to replace the glyphs.
132    * @param[in] numberOfGlyphsToRemove The number of glyphs to be removed.
133    * @param[in] glyphs Pointer to a buffer with the new glyphs.
134    * @param[in] numberOfCharacters Pointer to a buffer with the number of characters per glyph.
135    * @param[in] numberOfGlyphsToInsert The number of new glyphs in the buffer.
136    */
137   void ReplaceGlyphs( GlyphIndex glyphIndex,
138                       Length numberOfGlyphsToRemove,
139                       const GlyphInfo* const glyphs,
140                       const Length* const numberOfCharacters,
141                       Length numberOfGlyphsToInsert );
142
143   // Character <--> Glyph conversion
144
145   /**
146    * @brief Retrieves the first character in the logical model which a glyph represents.
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] glyphIndex The glyph index.
151    * @return The character index.
152    */
153   CharacterIndex GetCharacterIndex( GlyphIndex glyphIndex ) const;
154
155   /**
156    * @brief Query the number of characters the glyph represents.
157    *
158    * @param[in] glyphIndex The glyph index.
159    * @return The number of characters represented by the glyph.
160    */
161   Length GetCharactersPerGlyph( GlyphIndex glyphIndex ) const;
162
163   /**
164    * @brief Retrieves the first glyph in the visual model which represents a given character.
165    *
166    * @note After shaping several characters may be represented by the same glyph.
167    * Alternatively several glyphs may be required to display a character.
168    * @param[in] characterIndex The character index.
169    * @return The glyph index.
170    */
171   GlyphIndex GetGlyphIndex( CharacterIndex characterIndex ) const;
172
173   /**
174    * @brief Retrieves the whole or part of the character to glyph conversion map.
175    *
176    * The size of the buffer needs to be big enough to copy the @p numberOfCharacters.
177    *
178    * @param[out] characterToGlyphMap Pointer to a buffer where the conversion map is copied.
179    * @param[in] characterIndex Index to the first character.
180    * @param[in] numberOfCharacters The number of characters.
181    */
182   void GetCharacterToGlyphMap( GlyphIndex* characterToGlyphMap,
183                                CharacterIndex characterIndex,
184                                Length numberOfCharacters ) const;
185
186   /**
187    * @brief Retrieves the whole or part of the glyph to character conversion map.
188    *
189    * The size of the buffer needs to be big enough to copy the @p numberOfGlyphs.
190    *
191    * @param[out] glyphToCharacter Pointer to a buffer where the conversion map is copied.
192    * @param[in] glyphIndex Index to the first glyph.
193    * @param[in] numberOfGlyphs The number of glyphs.
194    */
195   void GetGlyphToCharacterMap( CharacterIndex* glyphToCharacter,
196                                GlyphIndex glyphIndex,
197                                Length numberOfGlyphs ) const;
198
199   /**
200    * @brief Retrieves for each glyph the number of characters the glyph represents.
201    *
202    * @param[out] charactersPerGlyph Pointer to a buffer where the number of characters for each glyph are copied.
203    * @param[in] glyphIndex Index to the first glyph.
204    * @param[in] numberOfGlyphs The number of glyphs.
205    */
206   void GetCharactersPerGlyphMap( Length* charactersPerGlyph,
207                                  GlyphIndex glyphIndex,
208                                  Length numberOfGlyphs ) const;
209
210   /**
211    * @brief Retrieves for each character the number of glyphs the character is shaped.
212    *
213    * @param[out] glyphsPerCharacter Pointer to a buffer where the number of glyphs for each character are copied.
214    * @param[in] characterIndex Index to the first character.
215    * @param[in] numberOfCharacters The number of characters.
216    */
217   void GetGlyphsPerCharacterMap( Length* glyphsPerCharacter,
218                                  CharacterIndex characterIndex,
219                                  Length numberOfCharacters ) const;
220
221   // Position interface
222
223   /**
224    * @brief Replaces any glyph positions previously set.
225    *
226    * @note If the number of glyphs is zero the position buffer is cleared.
227    *
228    * @param[in] glyphPositions An array of visual positions for each glyph.
229    * @param[in] numberOfGlyphs The number of positions.
230    */
231   void SetGlyphPositions( const Vector2* glyphPositions,
232                           Length numberOfGlyphs );
233
234   /**
235    * @brief Retrieves the number of glyph positions set.
236    *
237    * @note This may be less than the number of glyphs in the model.
238    * @return The number of glyphs.
239    */
240   Length GetNumberOfGlyphPositions() const;
241
242   /**
243    * @brief Retrieves the glyph positions.
244    *
245    * @pre The size of the @p positions buffer needs to be big enough to copy the @p numberOfGlyphs positions.
246    * @param[out] glyphPositions Pointer to a buffer where the glyph positions are copied.
247    * @param[in] glyphIndex Index to the first glyph position.
248    * @param[in] numberOfGlyphs The number of positions to be copied.
249    */
250   void GetGlyphPositions( Vector2* glyphPositions,
251                           GlyphIndex glyphIndex,
252                           Length numberOfGlyphs ) const;
253
254   /**
255    * @brief Retrieve the glyph's position of the given glyph.
256    *
257    * @param[in] glyphIndex Index to the glyph.
258    *
259    * @return The glyph's position.
260    */
261   const Vector2& GetGlyphPosition( GlyphIndex glyphIndex ) const;
262
263   /**
264    * @brief Replaces glyph's positions.
265    *
266    * If the @p numberOfGlyphsToRemove is zero, this operation is like an insert.
267    * If the @p numberOfGlyphsToInsert is zero, this operation is like a remove.
268    *
269    * @param[in] glyphIndex Where to replace the glyph's positions.
270    * @param[in] numberOfGlyphsToRemove The number of glyph's positions to be removed.
271    * @param[in] positions Pointer to a buffer with the new glyph's positions.
272    * @param[in] numberOfGlyphsToInsert The number of new glyph's positions in the buffer.
273    */
274   void ReplaceGlyphPositions( GlyphIndex glyphIndex,
275                               Length numberOfGlyphsToRemove,
276                               const Vector2* const positions,
277                               Length numberOfGlyphsToInsert );
278
279   // Line interface.
280
281   /**
282    * @brief Sets the lines.
283    *
284    * Replaces any lines previously set.
285    *
286    * Every line is an item run containing the index to the first glyph of the line and the number of glyphs.
287    *
288    * @note If the number of lines is zero or the pointer is NULL, the lines buffer is cleared.
289    *
290    * @param[in] lines Pointer to a buffer containing all the line runs.
291    * @param[in] numberOfLines The number of lines in the buffer.
292    */
293   void SetLines( const LineRun* const lines,
294                  Length numberOfLines );
295
296   /**
297    * @brief Retrieves the number of lines of the whole text.
298    *
299    * @return The number of lines.
300    */
301   Length GetNumberOfLines() const;
302
303   /**
304    * @brief Retrieves lines.
305    *
306    * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
307    *
308    * @param[out] lines Pointer to a buffer where the lines are copied.
309    * @param[in] lineIndex Index to the first line.
310    * @param[in] numberOfLines Number of lines to be copied.
311    */
312   void GetLines( LineRun* lines,
313                  LineIndex lineIndex,
314                  Length numberOfLines ) const;
315
316   /**
317    * @brief Retrieves the number of lines and the index to the first line where the given range of glyphs is laid out.
318    *
319    * @param[in] glyphIndex Index to the first glyph.
320    * @param[in] numberOfGlyphs The number of glyph.
321    * @param[out] firstLine Index to the line containing the glyph index.
322    * @param[out] numberOfLines The number of lines.
323    */
324   void GetNumberOfLines( GlyphIndex glyphIndex,
325                          Length numberOfGlyphs,
326                          LineIndex& firstLine,
327                          Length& numberOfLines ) const;
328   /**
329    * @brief Retrieves the lines where the given range of glyphs is laid out.
330    *
331    * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
332    *
333    * @param[out] lines Pointer to a buffer where the lines are copied.
334    * @param[in] glyphIndex Index to the first glyphs of the range.
335    * @param[in] numberOfGlyphs Number of glyphs in the range.
336    */
337   void GetLinesOfGlyphRange( LineRun* lines,
338                              GlyphIndex glyphIndex,
339                              Length numberOfGlyphs ) const;
340
341   /**
342    * @brief Retrieves the line index where the glyph is laid-out.
343    *
344    * @param[in] glyphIndex The glyph's index.
345    *
346    * @return The line index.
347    */
348   LineIndex GetLineOfGlyph( GlyphIndex glyphIndex );
349
350   /**
351    * @brief Retrieves the line index where the character is laid-out.
352    *
353    * @param[in] characterIndex The character's index.
354    *
355    * @return The line index.
356    */
357   LineIndex GetLineOfCharacter( CharacterIndex characterIndex );
358
359   /**
360    * @brief Replaces lines for the given range of glyphs.
361    *
362    * If the @p numberOfGlyphsToRemove is zero, this operation is like an insert.
363    * If the @p numberOfGlyphsToInsert is zero, this operation is like a remove.
364    *
365    * @param[in] glyphIndex Index of the first glyph where to replace the line info.
366    * @param[in] numberOfGlyphsToRemove The number of glyphs to be the line info removed.
367    * @param[in] lines Pointer to a buffer with the lines.
368    * @param[in] numberOfGlyphsToInsert The number of characters to be the line info inserted.
369    */
370   void ReplaceLines( GlyphIndex glyphIndex,
371                      Length numberOfGlyphsToRemove,
372                      const LineRun* const lines,
373                      Length numberOfGlyphsToInsert );
374
375   // Size interface
376
377   /**
378    * @brief Sets the natural size.
379    *
380    * @param[in] size The text's natural size.
381    */
382   void SetNaturalSize( const Vector2& size  );
383
384   /**
385    * @brief Retrieves the natural size.
386    *
387    * @return The text's natural size.
388    */
389   const Vector2& GetNaturalSize() const;
390
391   /**
392    * @brief Sets the text's actual size after it has been laid out.
393    *
394    * @param[in] size The text's size.
395    */
396   void SetActualSize( const Vector2& size );
397
398   /**
399    * @brief Retrieves the text's actual size after it has been laid out.
400    *
401    * @return The text's size.
402    */
403   const Vector2& GetActualSize() const;
404
405   /**
406    * @brief Set the text's color
407    *
408    * @param[in] textColor The text's color
409    */
410   void SetTextColor( const Vector4& textColor );
411
412   /**
413    * @brief Retrieve the text's color
414    *
415    * @return The text's color
416    */
417   const Vector4& GetTextColor() const;
418
419   /**
420    * @brief Sets the text's shadow offset.
421    *
422    * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
423    */
424   void SetShadowOffset( const Vector2& shadowOffset );
425
426   /**
427    * @brief Retrieves the text's shadow offset.
428    *
429    * @return The text's shadow offset, 0,0 indicates no shadow.
430    */
431   const Vector2& GetShadowOffset() const;
432
433   /**
434    * @brief Sets the text's shadow color.
435    *
436    * @param[in] shadowColor The shadow color.
437    */
438   void SetShadowColor( const Vector4& shadowColor );
439
440   /**
441    * @brief Retrieves the text's shadow color.
442    *
443    * @return The text's shadow color.
444    */
445   const Vector4& GetShadowColor() const;
446
447   /**
448    * @brief Sets the text's underline color.
449    *
450    * @param[in] color The text's underline color.
451    */
452   void SetUnderlineColor( const Vector4& color );
453
454   /**
455    * @brief Retrieves the text's underline color.
456    *
457    * @return The text's underline color.
458    */
459   const Vector4& GetUnderlineColor() const;
460
461   /**
462    * @brief Sets the text underline flag.
463    *
464    * @param[in] enabled true if underlined.
465    */
466   void SetUnderlineEnabled( bool enabled );
467
468   /**
469    * @brief Returns whether the text is underlined or not.
470    *
471    * @return underline state.
472    */
473   bool IsUnderlineEnabled() const;
474
475   /**
476    * @brief Clear the caches.
477    */
478   void ClearCaches();
479
480   /**
481    * @brief Set the override used for underline height, 0 indicates height will be come from font metrics
482    *
483    * @param[in] height The height in pixels of the underline
484    */
485   void SetUnderlineHeight( float height );
486
487   /**
488    * @brief Retrieves the underline height override
489    *
490    * @return Returns the override height for an underline, 0 indicates that font metrics will determine the height
491    */
492   float GetUnderlineHeight() const;
493
494 protected:
495
496   /**
497    * @brief A reference counted object may only be deleted by calling Unreference().
498    */
499   virtual ~VisualModel();
500
501 private:
502
503   /**
504    * @brief Private constructor.
505    */
506   VisualModel();
507
508   // Undefined
509   VisualModel( const VisualModel& handle );
510
511   // Undefined
512   VisualModel& operator=( const VisualModel& handle );
513
514 public:
515
516   Vector<GlyphInfo>      mGlyphs;               ///< For each glyph, the font's id, glyph's index within the font and glyph's metrics.
517   Vector<CharacterIndex> mGlyphsToCharacters;   ///< For each glyph, the index of the first character.
518   Vector<GlyphIndex>     mCharactersToGlyph;    ///< For each character, the index of the first glyph.
519   Vector<Length>         mCharactersPerGlyph;   ///< For each glyph, the number of characters that form the glyph.
520   Vector<Length>         mGlyphsPerCharacter;   ///< For each character, the number of glyphs that are shaped.
521   Vector<Vector2>        mGlyphPositions;       ///< For each glyph, the position.
522   Vector<LineRun>        mLines;                ///< The laid out lines.
523
524   Vector4                mTextColor;            ///< The text color
525   Vector4                mShadowColor;          ///< Color of drop shadow
526   Vector4                mUnderlineColor;       ///< Color of underline
527   Vector2                mShadowOffset;         ///< Offset for drop shadow, 0 indicates no shadow
528   float                  mUnderlineHeight;      ///< Fixed height for underline to override font metrics.
529
530
531
532 private:
533
534   Size                   mNaturalSize;        ///< Size of the text with no line wrapping.
535   Size                   mActualSize;         ///< Size of the laid-out text considering the layout properties set.
536
537   // Caches to increase performance in some consecutive operations.
538   LineIndex mCachedLineIndex; ///< Used to increase performance in consecutive calls to GetLineOfGlyph() or GetLineOfCharacter() with consecutive glyphs or characters.
539
540 public:
541
542   bool                   mUnderlineEnabled:1;   ///< Underline enabled flag
543   bool                   mUnderlineColorSet:1;  ///< Has the underline color been explicitly set?
544
545 };
546
547 } // namespace Text
548
549 } // namespace Toolkit
550
551 } // namespace Dali
552
553 #endif // __DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H__