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