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