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/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    * 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    * 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    * 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    * 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    * 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    * 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    * 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    * 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    * 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    * 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    * 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    * 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    * Retrieves the number of lines of the whole text.
298    *
299    * @return The number of lines.
300    */
301   Length GetNumberOfLines() const;
302
303   /**
304    * 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    * 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    * 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    * Replaces lines for the given range of glyphs.
343    *
344    * If the @p numberOfGlyphsToRemove is zero, this operation is like an insert.
345    * If the @p numberOfGlyphsToInsert is zero, this operation is like a remove.
346    *
347    * @param[in] glyphIndex Index of the first glyph where to replace the line info.
348    * @param[in] numberOfGlyphsToRemove The number of glyphs to be the line info removed.
349    * @param[in] lines Pointer to a buffer with the lines.
350    * @param[in] numberOfGlyphsToInsert The number of characters to be the line info inserted.
351    */
352   void ReplaceLines( GlyphIndex glyphIndex,
353                      Length numberOfGlyphsToRemove,
354                      const LineRun* const lines,
355                      Length numberOfGlyphsToInsert );
356
357   // Size interface
358
359   /**
360    * Sets the natural size.
361    *
362    * @param[in] size The text's natural size.
363    */
364   void SetNaturalSize( const Vector2& size  );
365
366   /**
367    * Retrieves the natural size.
368    *
369    * @return The text's natural size.
370    */
371   const Vector2& GetNaturalSize() const;
372
373   /**
374    * Sets the text's actual size after it has been laid out.
375    *
376    * @param[in] size The text's size.
377    */
378   void SetActualSize( const Vector2& size );
379
380   /**
381    * Retrieves the text's actual size after it has been laid out.
382    *
383    * @return The text's size.
384    */
385   const Vector2& GetActualSize() const;
386
387   /**
388    * @brief Set the text's color
389    *
390    * @param[in] textColor The text's color
391    */
392   void SetTextColor( const Vector4& textColor );
393
394   /**
395    * @brief Retrieve the text's color
396    *
397    * @return The text's color
398    */
399   const Vector4& GetTextColor() const;
400
401   /**
402    * @brief Sets the text's shadow offset.
403    *
404    * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
405    */
406   void SetShadowOffset( const Vector2& shadowOffset );
407
408   /**
409    * @brief Retrieves the text's shadow offset.
410    *
411    * @return The text's shadow offset, 0,0 indicates no shadow.
412    */
413   const Vector2& GetShadowOffset() const;
414
415   /**
416    * @brief Sets the text's shadow color.
417    *
418    * @param[in] shadowColor The shadow color.
419    */
420   void SetShadowColor( const Vector4& shadowColor );
421
422   /**
423    * @brief Retrieves the text's shadow color.
424    *
425    * @return The text's shadow color.
426    */
427   const Vector4& GetShadowColor() const;
428
429   /**
430    * @brief Sets the text's underline color.
431    *
432    * @param[in] color The text's underline color.
433    */
434   void SetUnderlineColor( const Vector4& color );
435
436   /**
437    * @brief Retrieves the text's underline color.
438    *
439    * @return The text's underline color.
440    */
441   const Vector4& GetUnderlineColor() const;
442
443   /**
444    * @brief Sets the text underline flag.
445    *
446    * @param[in] enabled true if underlined.
447    */
448   void SetUnderlineEnabled( bool enabled );
449
450   /**
451    * @brief Returns whether the text is underlined or not.
452    *
453    * @return underline state.
454    */
455   bool IsUnderlineEnabled() const;
456
457 protected:
458
459   /**
460    * @brief A reference counted object may only be deleted by calling Unreference().
461    */
462   virtual ~VisualModel();
463
464 private:
465
466   /**
467    * @brief Private constructor.
468    */
469   VisualModel();
470
471   // Undefined
472   VisualModel( const VisualModel& handle );
473
474   // Undefined
475   VisualModel& operator=( const VisualModel& handle );
476
477 public:
478
479   Vector<GlyphInfo>      mGlyphs;               ///< For each glyph, the font's id, glyph's index within the font and glyph's metrics.
480   Vector<CharacterIndex> mGlyphsToCharacters;   ///< For each glyph, the index of the first character.
481   Vector<GlyphIndex>     mCharactersToGlyph;    ///< For each character, the index of the first glyph.
482   Vector<Length>         mCharactersPerGlyph;   ///< For each glyph, the number of characters that form the glyph.
483   Vector<Length>         mGlyphsPerCharacter;   ///< For each character, the number of glyphs that are shaped.
484   Vector<Vector2>        mGlyphPositions;       ///< For each glyph, the position.
485   Vector<LineRun>        mLines;                ///< The laid out lines.
486
487   Vector4                mTextColor;            ///< The text color
488   Vector2                mShadowOffset;         ///< Offset for drop shadow, 0 indicates no shadow
489   Vector4                mShadowColor;          ///< Color of drop shadow
490   Vector4                mUnderlineColor;       ///< Color of underline
491   bool                   mUnderlineEnabled:1;   ///< Underline enabled flag
492   bool                   mUnderlineColorSet:1;  ///< Has the underline color been explicitly set?
493
494 private:
495
496   Size                   mNaturalSize;        ///< Size of the text with no line wrapping.
497   Size                   mActualSize;         ///< Size of the laid-out text considering the layout properties set.
498 };
499
500 } // namespace Text
501
502 } // namespace Toolkit
503
504 } // namespace Dali
505
506 #endif // __DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H__