(TextController) Reformatted to reduce LOC
[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 // DEVEL INCLUDES
33 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
34
35 namespace Dali
36 {
37 namespace Toolkit
38 {
39 namespace Text
40 {
41 struct HyphenInfo
42 {
43   Vector<GlyphInfo> glyph;
44   Vector<Vector2>   position;
45   Vector<Length>    index;
46 };
47
48 class VisualModel;
49 typedef IntrusivePtr<VisualModel> VisualModelPtr;
50
51 /**
52  * @brief A visual text model contains layout specific information.
53  *
54  * This includes:
55  * - A series of glyphs in visual order i.e. after the bidirectional reordering.
56  * - The position of each glyph within a 2D bounding box.
57  */
58 class VisualModel : public RefObject
59 {
60 public:
61   /**
62    * @brief Create a new instance of a VisualModel.
63    *
64    * @return A pointer to a new VisualModel.
65    */
66   static VisualModelPtr New();
67
68   // Glyph interface.
69
70   /**
71    * @brief Creates the character to glyph conversion table.
72    *
73    * @pre The glyphs per character table needs to be created first.
74    *
75    * @param[in] startIndex The character from where the conversion table is created.
76    * @param[in] startGlyphIndex The glyph from where the conversion table is created.
77    * @param[in] numberOfCharacters The number of characters.
78    */
79   void CreateCharacterToGlyphTable(CharacterIndex startIndex,
80                                    GlyphIndex     startGlyphIndex,
81                                    Length         numberOfCharacters);
82
83   /**
84    * @brief Creates an array containing the number of glyphs per character.
85    *
86    * @param[in] startIndex The character from where the table is created.
87    * @param[in] startGlyphIndex The glyph from where the conversion table is created.
88    * @param[in] numberOfCharacters The number of characters.
89    */
90   void CreateGlyphsPerCharacterTable(CharacterIndex startIndex,
91                                      GlyphIndex     startGlyphIndex,
92                                      Length         numberOfCharacters);
93
94   /**
95    * @brief Retrieves glyphs in the given buffer.
96    *
97    * The size of the @p glyphs buffer needs to be big enough to copy the @p numberOfGlyphs.
98    * @param[out] glyphs Pointer to a buffer where the glyphs are copied.
99    * @param[in] glyphIndex Index to the first glyph.
100    * @param[in] numberOfGlyphs Number of glyphs to be copied.
101    */
102   void GetGlyphs(GlyphInfo* glyphs,
103                  GlyphIndex glyphIndex,
104                  Length     numberOfGlyphs) const;
105
106   // Position interface
107
108   /**
109    * @brief Retrieves the glyph positions.
110    *
111    * @pre The size of the @p positions buffer needs to be big enough to copy the @p numberOfGlyphs positions.
112    * @param[out] glyphPositions Pointer to a buffer where the glyph positions are copied.
113    * @param[in] glyphIndex Index to the first glyph position.
114    * @param[in] numberOfGlyphs The number of positions to be copied.
115    */
116   void GetGlyphPositions(Vector2*   glyphPositions,
117                          GlyphIndex glyphIndex,
118                          Length     numberOfGlyphs) const;
119
120   // Line interface.
121
122   /**
123    * @brief Retrieves the number of lines and the index to the first line where the given range of glyphs is laid out.
124    *
125    * @param[in] glyphIndex Index to the first glyph.
126    * @param[in] numberOfGlyphs The number of glyph.
127    * @param[out] firstLine Index to the line containing the glyph index.
128    * @param[out] numberOfLines The number of lines.
129    */
130   void GetNumberOfLines(GlyphIndex glyphIndex,
131                         Length     numberOfGlyphs,
132                         LineIndex& firstLine,
133                         Length&    numberOfLines) const;
134
135   /**
136    * @brief Retrieves the lines where the given range of glyphs is laid out.
137    *
138    * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
139    *
140    * @param[out] lines Pointer to a buffer where the lines are copied.
141    * @param[in] glyphIndex Index to the first glyphs of the range.
142    * @param[in] numberOfGlyphs Number of glyphs in the range.
143    */
144   void GetLinesOfGlyphRange(LineRun*   lines,
145                             GlyphIndex glyphIndex,
146                             Length     numberOfGlyphs) const;
147
148   /**
149    * @brief Retrieves the line index where the character is laid-out.
150    *
151    * @param[in] characterIndex The character's index.
152    *
153    * @return The line index.
154    */
155   LineIndex GetLineOfCharacter(CharacterIndex characterIndex);
156
157   // Underline runs
158
159   /**
160    * @brief Retrieves the underline runs.
161    *
162    * @param[out] underlineRuns Pointer to a buffer where the underline runs are copied.
163    * @param[in] index Index of the first underline run to be copied.
164    * @param[in] numberOfRuns Number of underline runs to be copied.
165    */
166   void GetUnderlineRuns(GlyphRun*         underlineRuns,
167                         UnderlineRunIndex index,
168                         Length            numberOfRuns) const;
169
170   // Size interface
171
172   /**
173    * @brief Sets the natural size.
174    *
175    * @param[in] size The text's natural size.
176    */
177   void SetNaturalSize(const Vector2& size);
178
179   /**
180    * @brief Retrieves the natural size.
181    *
182    * @return The text's natural size.
183    */
184   const Vector2& GetNaturalSize() const;
185
186   /**
187    * @brief Sets the text's layout size.
188    *
189    * @param[in] size The text's size.
190    */
191   void SetLayoutSize(const Vector2& size);
192
193   /**
194    * @brief Retrieves the text's layout size.
195    *
196    * @return The text's size.
197    */
198   const Vector2& GetLayoutSize() const;
199
200   /**
201    * @brief Set the text's color
202    *
203    * @param[in] textColor The text's color
204    */
205   void SetTextColor(const Vector4& textColor);
206
207   /**
208    * @brief Retrieve the text's color
209    *
210    * @return The text's color
211    */
212   const Vector4& GetTextColor() const;
213
214   /**
215    * @brief Sets the text's shadow offset.
216    *
217    * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
218    */
219   void SetShadowOffset(const Vector2& shadowOffset);
220
221   /**
222    * @brief Retrieves the text's shadow offset.
223    *
224    * @return The text's shadow offset, 0,0 indicates no shadow.
225    */
226   const Vector2& GetShadowOffset() const;
227
228   /**
229    * @brief Sets the text's shadow color.
230    *
231    * @param[in] shadowColor The shadow color.
232    */
233   void SetShadowColor(const Vector4& shadowColor);
234
235   /**
236    * @brief Retrieves the text's shadow color.
237    *
238    * @return The text's shadow color.
239    */
240   const Vector4& GetShadowColor() const;
241
242   /**
243    * @brief Set the shadow blur radius.
244    *
245    * @param[in] shadowBlurRadius The shadow blur radius, 0,0 indicates no blur.
246    */
247   void SetShadowBlurRadius(const float& shadowBlurRadius);
248
249   /**
250    * @brief Retrieve the shadow blur radius.
251    *
252    * @return The shadow blur radius.
253    */
254   const float& GetShadowBlurRadius() const;
255
256   /**
257    * @brief Sets the text's underline color.
258    *
259    * @param[in] color The text's underline color.
260    */
261   void SetUnderlineColor(const Vector4& color);
262
263   /**
264    * @brief Retrieves the text's underline color.
265    *
266    * @return The text's underline color.
267    */
268   const Vector4& GetUnderlineColor() const;
269
270   /**
271    * @brief Sets the text underline flag.
272    *
273    * @param[in] enabled true if underlined.
274    */
275   void SetUnderlineEnabled(bool enabled);
276
277   /**
278    * @brief Returns whether the text is underlined or not.
279    *
280    * @return underline state.
281    */
282   bool IsUnderlineEnabled() const;
283
284   /**
285    * @brief Clear the caches.
286    */
287   void ClearCaches();
288
289   /**
290    * @brief Set the override used for underline height, 0 indicates height will be come from font metrics
291    *
292    * @param[in] height The height in pixels of the underline
293    */
294   void SetUnderlineHeight(float height);
295
296   /**
297    * @brief Retrieves the underline height override
298    *
299    * @return Returns the override height for an underline, 0 indicates that font metrics will determine the height
300    */
301   float GetUnderlineHeight() const;
302
303   /**
304    * @brief Retrieves the number of underline runs.
305    *
306    * @return The number of underline runs.
307    */
308   Length GetNumberOfUnderlineRuns() const;
309
310   /**
311    * @brief Set the outline color.
312    *
313    * @param[in] color color of outline.
314    */
315   void SetOutlineColor(const Vector4& color);
316
317   /**
318    * @brief Retrieve the outline color.
319    *
320    * @return The outline color.
321    */
322   const Vector4& GetOutlineColor() const;
323
324   /**
325    * @brief Set the outline width
326    *
327    * @param[in] width The width in pixels of the outline, 0 indicates no outline
328    */
329   void SetOutlineWidth(uint16_t width);
330
331   /**
332    * @brief Retrieves the width of an outline
333    *
334    * @return The width of the outline.
335    */
336   uint16_t GetOutlineWidth() const;
337
338   /**
339    * @brief Sets the text's background color.
340    *
341    * @param[in] color The text's background color.
342    */
343   void SetBackgroundColor(const Vector4& color);
344
345   /**
346    * @brief Retrieves the text's background color.
347    *
348    * @return The text's background color.
349    */
350   const Vector4& GetBackgroundColor() const;
351
352   /**
353    * @brief Sets whether the text has a background or not.
354    *
355    * @param[in] enabled true if the text has a background.
356    */
357   void SetBackgroundEnabled(bool enabled);
358
359   /**
360    * @brief Returns whether the text has a background or not.
361    *
362    * @return whether the text has a background or not.
363    */
364   bool IsBackgroundEnabled() const;
365
366   /**
367    * @brief Sets whether the text has a markup-processor or not.
368    *
369    * @param[in] enabled true if the text has a markup-processor.
370    */
371   void SetMarkupProcessorEnabled(bool enabled);
372
373   /**
374    * @brief Returns whether the text has a markup-processor or not.
375    *
376    * @return whether the text has a markup-processor or not.
377    */
378   bool IsMarkupProcessorEnabled() const;
379
380   /**
381    * @brief Sets ellipsis position
382    * @param[in] ellipsisPosition The ellipsis position for the text
383    */
384   void SetEllipsisPosition(Toolkit::DevelText::EllipsisPosition::Type ellipsisPosition);
385
386   /**
387    * @brief Retrieves ellipsis position for text.
388    *
389    * @return The ellipsis position.
390    */
391   Toolkit::DevelText::EllipsisPosition::Type GetEllipsisPosition() const;
392
393   /**
394    * @brief Enable or disable the text elide.
395    *
396    * @param[in] enabled Whether to enable the text elide.
397    */
398   void SetTextElideEnabled(bool enabled);
399
400   /**
401    * @brief Whether the text elide property is enabled.
402    *
403    * @return @e true if the text elide property is enabled, @e false otherwise.
404    */
405   bool IsTextElideEnabled() const;
406
407   /**
408    * @brief Sets the start index of laid-out glyphs.
409    *
410    * @param[in] startIndexOfElidedGlyphs The start index of laid-out glyphs.
411    */
412   void SetStartIndexOfElidedGlyphs(GlyphIndex startIndexOfElidedGlyphs);
413
414   /**
415    * @brief Sets the end index of elided glyphs.
416    *
417    * @param[in] endIndexOfElidedGlyphs The end index of elided glyphs.
418    */
419   void SetEndIndexOfElidedGlyphs(GlyphIndex endIndexOfElidedGlyphs);
420
421   /**
422    * @brief Sets the first middle index of elided glyphs, index before ellipsis of middle.
423    *
424    * @param[in] firstMiddleIndexOfElidedGlyphs The first middle index of elided glyphs, index before ellipsis of middle.
425    */
426   void SetFirstMiddleIndexOfElidedGlyphs(GlyphIndex firstMiddleIndexOfElidedGlyphs);
427
428   /**
429    * @brief Sets the second middle index of elided glyphs, index of ellipsis of middle.
430    *
431    * @param[in] secondMiddleIndexOfElidedGlyphs The second middle index of elided glyphs, index of ellipsis of middle.
432    */
433   void SetSecondMiddleIndexOfElidedGlyphs(GlyphIndex secondMiddleIndexOfElidedGlyphs);
434
435   /**
436    * @brief Retrieves the start index of laid-out glyphs.
437    *
438    * @return The start index of laid-out glyphs.
439    */
440   GlyphIndex GetStartIndexOfElidedGlyphs() const;
441
442   /**
443    * @brief Retrieves the end index of laid-out glyphs.
444    *
445    * @return The end index of laid-out glyphs.
446    */
447   GlyphIndex GetEndIndexOfElidedGlyphs() const;
448
449   /**
450    * @brief Retrieves the first middle index of elided glyphs, index before ellipsis of middle.
451    *
452    * @return The first middle index of elided glyphs, index before ellipsis of middle.
453    */
454   GlyphIndex GetFirstMiddleIndexOfElidedGlyphs() const;
455
456   /**
457    * @brief Retrieves the second middle index of elided glyphs, index of ellipsis of middle.
458    *
459    * @return The second middle index of elided glyphs, index of ellipsis of middle.
460    */
461   GlyphIndex GetSecondMiddleIndexOfElidedGlyphs() const;
462
463 protected:
464   /**
465    * @brief A reference counted object may only be deleted by calling Unreference().
466    */
467   virtual ~VisualModel();
468
469 private:
470   /**
471    * @brief Private constructor.
472    */
473   VisualModel();
474
475   // Undefined
476   VisualModel(const VisualModel& handle);
477
478   // Undefined
479   VisualModel& operator=(const VisualModel& handle);
480
481 public:
482   Vector<GlyphInfo>      mGlyphs;                 ///< For each glyph, the font's id, glyph's index within the font and glyph's metrics.
483   Vector<CharacterIndex> mGlyphsToCharacters;     ///< For each glyph, the index of the first character.
484   Vector<GlyphIndex>     mCharactersToGlyph;      ///< For each character, the index of the first glyph.
485   Vector<Length>         mCharactersPerGlyph;     ///< For each glyph, the number of characters that form the glyph.
486   Vector<Length>         mGlyphsPerCharacter;     ///< For each character, the number of glyphs that are shaped.
487   Vector<Vector2>        mGlyphPositions;         ///< For each glyph, the position.
488   Vector<LineRun>        mLines;                  ///< The laid out lines.
489   Vector<GlyphRun>       mUnderlineRuns;          ///< Runs of glyphs that are underlined.
490   Vector<Vector4>        mColors;                 ///< Colors of the glyphs.
491   Vector<ColorIndex>     mColorIndices;           ///< Indices to the vector of colors for each glyphs.
492   Vector<Vector4>        mBackgroundColors;       ///< Background colors of the glyphs.
493   Vector<ColorIndex>     mBackgroundColorIndices; ///< Indices to the vector of background colors for each glyphs.
494
495   Vector4  mTextColor;        ///< The text color
496   Vector4  mShadowColor;      ///< Color of drop shadow
497   Vector4  mUnderlineColor;   ///< Color of underline
498   Vector4  mOutlineColor;     ///< Color of outline
499   Vector4  mBackgroundColor;  ///< Color of text background
500   Size     mControlSize;      ///< The size of the UI control.
501   Vector2  mShadowOffset;     ///< Offset for drop shadow, 0 indicates no shadow
502   float    mUnderlineHeight;  ///< Fixed height for underline to override font metrics.
503   float    mShadowBlurRadius; ///< Blur radius of shadow, 0 indicates no blur.
504   uint16_t mOutlineWidth;     ///< Width of outline.
505
506 private:
507   Size mNaturalSize; ///< Size of the text with no line wrapping.
508   Size mLayoutSize;  ///< Size of the laid-out text considering the layout properties set.
509
510   // Caches to increase performance in some consecutive operations.
511   LineIndex mCachedLineIndex; ///< Used to increase performance in consecutive calls to GetLineOfGlyph() or GetLineOfCharacter() with consecutive glyphs or characters.
512
513   DevelText::EllipsisPosition::Type mEllipsisPosition;                ///< Where is the location the text elide
514   GlyphIndex                        mStartIndexOfElidedGlyphs;        ///< The start index of elided glyphs.
515   GlyphIndex                        mEndIndexOfElidedGlyphs;          ///< The end index of elided glyphs.
516   GlyphIndex                        mFirstMiddleIndexOfElidedGlyphs;  ///< The first end index of elided glyphs, index before ellipsis of middle.
517   GlyphIndex                        mSecondMiddleIndexOfElidedGlyphs; ///< The first end index of elided glyphs, index of ellipsis of middle.
518   bool                              mTextElideEnabled : 1;            ///< Whether the text's elide is enabled.
519
520 public:
521   bool       mUnderlineEnabled : 1;       ///< Underline enabled flag
522   bool       mUnderlineColorSet : 1;      ///< Has the underline color been explicitly set?
523   bool       mBackgroundEnabled : 1;      ///< Background enabled flag
524   bool       mMarkupProcessorEnabled : 1; ///< Markup-processor enabled flag
525   HyphenInfo mHyphen;                     ///< Contains hyphen glyph info & the character index to draw hyphen after.
526 };
527
528 } // namespace Text
529
530 } // namespace Toolkit
531
532 } // namespace Dali
533
534 #endif // DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H