Updated all header files to new format
[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) 2021 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/vector2.h>
25 #include <dali/public-api/math/vector4.h>
26 #include <dali/public-api/object/ref-object.h>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/internal/text/color-run.h>
30 #include <dali-toolkit/internal/text/line-run.h>
31
32 namespace Dali
33 {
34 namespace Toolkit
35 {
36 namespace Text
37 {
38 class VisualModel;
39 typedef IntrusivePtr<VisualModel> VisualModelPtr;
40
41 /**
42  * @brief A visual text model contains layout specific information.
43  *
44  * This includes:
45  * - A series of glyphs in visual order i.e. after the bidirectional reordering.
46  * - The position of each glyph within a 2D bounding box.
47  */
48 class VisualModel : public RefObject
49 {
50 public:
51   /**
52    * @brief Create a new instance of a VisualModel.
53    *
54    * @return A pointer to a new VisualModel.
55    */
56   static VisualModelPtr New();
57
58   // Glyph interface.
59
60   /**
61    * @brief Creates the character to glyph conversion table.
62    *
63    * @pre The glyphs per character table needs to be created first.
64    *
65    * @param[in] startIndex The character from where the conversion table is created.
66    * @param[in] startGlyphIndex The glyph from where the conversion table is created.
67    * @param[in] numberOfCharacters The number of characters.
68    */
69   void CreateCharacterToGlyphTable(CharacterIndex startIndex,
70                                    GlyphIndex     startGlyphIndex,
71                                    Length         numberOfCharacters);
72
73   /**
74    * @brief Creates an array containing the number of glyphs per character.
75    *
76    * @param[in] startIndex The character from where the table is created.
77    * @param[in] startGlyphIndex The glyph from where the conversion table is created.
78    * @param[in] numberOfCharacters The number of characters.
79    */
80   void CreateGlyphsPerCharacterTable(CharacterIndex startIndex,
81                                      GlyphIndex     startGlyphIndex,
82                                      Length         numberOfCharacters);
83
84   /**
85    * @brief Retrieves glyphs in the given buffer.
86    *
87    * The size of the @p glyphs buffer needs to be big enough to copy the @p numberOfGlyphs.
88    * @param[out] glyphs Pointer to a buffer where the glyphs are copied.
89    * @param[in] glyphIndex Index to the first glyph.
90    * @param[in] numberOfGlyphs Number of glyphs to be copied.
91    */
92   void GetGlyphs(GlyphInfo* glyphs,
93                  GlyphIndex glyphIndex,
94                  Length     numberOfGlyphs) const;
95
96   // Position interface
97
98   /**
99    * @brief Retrieves the glyph positions.
100    *
101    * @pre The size of the @p positions buffer needs to be big enough to copy the @p numberOfGlyphs positions.
102    * @param[out] glyphPositions Pointer to a buffer where the glyph positions are copied.
103    * @param[in] glyphIndex Index to the first glyph position.
104    * @param[in] numberOfGlyphs The number of positions to be copied.
105    */
106   void GetGlyphPositions(Vector2*   glyphPositions,
107                          GlyphIndex glyphIndex,
108                          Length     numberOfGlyphs) const;
109
110   // Line interface.
111
112   /**
113    * @brief Retrieves the number of lines and the index to the first line where the given range of glyphs is laid out.
114    *
115    * @param[in] glyphIndex Index to the first glyph.
116    * @param[in] numberOfGlyphs The number of glyph.
117    * @param[out] firstLine Index to the line containing the glyph index.
118    * @param[out] numberOfLines The number of lines.
119    */
120   void GetNumberOfLines(GlyphIndex glyphIndex,
121                         Length     numberOfGlyphs,
122                         LineIndex& firstLine,
123                         Length&    numberOfLines) const;
124
125   /**
126    * @brief Retrieves the lines where the given range of glyphs is laid out.
127    *
128    * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
129    *
130    * @param[out] lines Pointer to a buffer where the lines are copied.
131    * @param[in] glyphIndex Index to the first glyphs of the range.
132    * @param[in] numberOfGlyphs Number of glyphs in the range.
133    */
134   void GetLinesOfGlyphRange(LineRun*   lines,
135                             GlyphIndex glyphIndex,
136                             Length     numberOfGlyphs) const;
137
138   /**
139    * @brief Retrieves the line index where the character is laid-out.
140    *
141    * @param[in] characterIndex The character's index.
142    *
143    * @return The line index.
144    */
145   LineIndex GetLineOfCharacter(CharacterIndex characterIndex);
146
147   // Underline runs
148
149   /**
150    * @brief Retrieves the underline runs.
151    *
152    * @param[out] underlineRuns Pointer to a buffer where the underline runs are copied.
153    * @param[in] index Index of the first underline run to be copied.
154    * @param[in] numberOfRuns Number of underline runs to be copied.
155    */
156   void GetUnderlineRuns(GlyphRun*         underlineRuns,
157                         UnderlineRunIndex index,
158                         Length            numberOfRuns) const;
159
160   // Size interface
161
162   /**
163    * @brief Sets the natural size.
164    *
165    * @param[in] size The text's natural size.
166    */
167   void SetNaturalSize(const Vector2& size);
168
169   /**
170    * @brief Retrieves the natural size.
171    *
172    * @return The text's natural size.
173    */
174   const Vector2& GetNaturalSize() const;
175
176   /**
177    * @brief Sets the text's layout size.
178    *
179    * @param[in] size The text's size.
180    */
181   void SetLayoutSize(const Vector2& size);
182
183   /**
184    * @brief Retrieves the text's layout size.
185    *
186    * @return The text's size.
187    */
188   const Vector2& GetLayoutSize() const;
189
190   /**
191    * @brief Set the text's color
192    *
193    * @param[in] textColor The text's color
194    */
195   void SetTextColor(const Vector4& textColor);
196
197   /**
198    * @brief Retrieve the text's color
199    *
200    * @return The text's color
201    */
202   const Vector4& GetTextColor() const;
203
204   /**
205    * @brief Sets the text's shadow offset.
206    *
207    * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
208    */
209   void SetShadowOffset(const Vector2& shadowOffset);
210
211   /**
212    * @brief Retrieves the text's shadow offset.
213    *
214    * @return The text's shadow offset, 0,0 indicates no shadow.
215    */
216   const Vector2& GetShadowOffset() const;
217
218   /**
219    * @brief Sets the text's shadow color.
220    *
221    * @param[in] shadowColor The shadow color.
222    */
223   void SetShadowColor(const Vector4& shadowColor);
224
225   /**
226    * @brief Retrieves the text's shadow color.
227    *
228    * @return The text's shadow color.
229    */
230   const Vector4& GetShadowColor() const;
231
232   /**
233    * @brief Set the shadow blur radius.
234    *
235    * @param[in] shadowBlurRadius The shadow blur radius, 0,0 indicates no blur.
236    */
237   void SetShadowBlurRadius(const float& shadowBlurRadius);
238
239   /**
240    * @brief Retrieve the shadow blur radius.
241    *
242    * @return The shadow blur radius.
243    */
244   const float& GetShadowBlurRadius() const;
245
246   /**
247    * @brief Sets the text's underline color.
248    *
249    * @param[in] color The text's underline color.
250    */
251   void SetUnderlineColor(const Vector4& color);
252
253   /**
254    * @brief Retrieves the text's underline color.
255    *
256    * @return The text's underline color.
257    */
258   const Vector4& GetUnderlineColor() const;
259
260   /**
261    * @brief Sets the text underline flag.
262    *
263    * @param[in] enabled true if underlined.
264    */
265   void SetUnderlineEnabled(bool enabled);
266
267   /**
268    * @brief Returns whether the text is underlined or not.
269    *
270    * @return underline state.
271    */
272   bool IsUnderlineEnabled() const;
273
274   /**
275    * @brief Clear the caches.
276    */
277   void ClearCaches();
278
279   /**
280    * @brief Set the override used for underline height, 0 indicates height will be come from font metrics
281    *
282    * @param[in] height The height in pixels of the underline
283    */
284   void SetUnderlineHeight(float height);
285
286   /**
287    * @brief Retrieves the underline height override
288    *
289    * @return Returns the override height for an underline, 0 indicates that font metrics will determine the height
290    */
291   float GetUnderlineHeight() const;
292
293   /**
294    * @brief Retrieves the number of underline runs.
295    *
296    * @return The number of underline runs.
297    */
298   Length GetNumberOfUnderlineRuns() const;
299
300   /**
301    * @brief Set the outline color.
302    *
303    * @param[in] color color of outline.
304    */
305   void SetOutlineColor(const Vector4& color);
306
307   /**
308    * @brief Retrieve the outline color.
309    *
310    * @return The outline color.
311    */
312   const Vector4& GetOutlineColor() const;
313
314   /**
315    * @brief Set the outline width
316    *
317    * @param[in] width The width in pixels of the outline, 0 indicates no outline
318    */
319   void SetOutlineWidth(uint16_t width);
320
321   /**
322    * @brief Retrieves the width of an outline
323    *
324    * @return The width of the outline.
325    */
326   uint16_t GetOutlineWidth() const;
327
328   /**
329    * @brief Sets the text's background color.
330    *
331    * @param[in] color The text's background color.
332    */
333   void SetBackgroundColor(const Vector4& color);
334
335   /**
336    * @brief Retrieves the text's background color.
337    *
338    * @return The text's background color.
339    */
340   const Vector4& GetBackgroundColor() const;
341
342   /**
343    * @brief Sets whether the text has a background or not.
344    *
345    * @param[in] enabled true if the text has a background.
346    */
347   void SetBackgroundEnabled(bool enabled);
348
349   /**
350    * @brief Returns whether the text has a background or not.
351    *
352    * @return whether the text has a background or not.
353    */
354   bool IsBackgroundEnabled() const;
355
356 protected:
357   /**
358    * @brief A reference counted object may only be deleted by calling Unreference().
359    */
360   virtual ~VisualModel();
361
362 private:
363   /**
364    * @brief Private constructor.
365    */
366   VisualModel();
367
368   // Undefined
369   VisualModel(const VisualModel& handle);
370
371   // Undefined
372   VisualModel& operator=(const VisualModel& handle);
373
374 public:
375   Vector<GlyphInfo>      mGlyphs;                 ///< For each glyph, the font's id, glyph's index within the font and glyph's metrics.
376   Vector<CharacterIndex> mGlyphsToCharacters;     ///< For each glyph, the index of the first character.
377   Vector<GlyphIndex>     mCharactersToGlyph;      ///< For each character, the index of the first glyph.
378   Vector<Length>         mCharactersPerGlyph;     ///< For each glyph, the number of characters that form the glyph.
379   Vector<Length>         mGlyphsPerCharacter;     ///< For each character, the number of glyphs that are shaped.
380   Vector<Vector2>        mGlyphPositions;         ///< For each glyph, the position.
381   Vector<LineRun>        mLines;                  ///< The laid out lines.
382   Vector<GlyphRun>       mUnderlineRuns;          ///< Runs of glyphs that are underlined.
383   Vector<Vector4>        mColors;                 ///< Colors of the glyphs.
384   Vector<ColorIndex>     mColorIndices;           ///< Indices to the vector of colors for each glyphs.
385   Vector<Vector4>        mBackgroundColors;       ///< Background colors of the glyphs.
386   Vector<ColorIndex>     mBackgroundColorIndices; ///< Indices to the vector of background colors for each glyphs.
387
388   Vector4  mTextColor;        ///< The text color
389   Vector4  mShadowColor;      ///< Color of drop shadow
390   Vector4  mUnderlineColor;   ///< Color of underline
391   Vector4  mOutlineColor;     ///< Color of outline
392   Vector4  mBackgroundColor;  ///< Color of text background
393   Size     mControlSize;      ///< The size of the UI control.
394   Vector2  mShadowOffset;     ///< Offset for drop shadow, 0 indicates no shadow
395   float    mUnderlineHeight;  ///< Fixed height for underline to override font metrics.
396   float    mShadowBlurRadius; ///< Blur radius of shadow, 0 indicates no blur.
397   uint16_t mOutlineWidth;     ///< Width of outline.
398
399 private:
400   Size mNaturalSize; ///< Size of the text with no line wrapping.
401   Size mLayoutSize;  ///< Size of the laid-out text considering the layout properties set.
402
403   // Caches to increase performance in some consecutive operations.
404   LineIndex mCachedLineIndex; ///< Used to increase performance in consecutive calls to GetLineOfGlyph() or GetLineOfCharacter() with consecutive glyphs or characters.
405
406 public:
407   bool mUnderlineEnabled : 1;  ///< Underline enabled flag
408   bool mUnderlineColorSet : 1; ///< Has the underline color been explicitly set?
409   bool mBackgroundEnabled : 1; ///< Background enabled flag
410 };
411
412 } // namespace Text
413
414 } // namespace Toolkit
415
416 } // namespace Dali
417
418 #endif // DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H