1 #ifndef DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H
2 #define DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H
5 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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>
37 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
47 Vector<GlyphInfo> glyph;
48 Vector<Vector2> position;
53 typedef IntrusivePtr<VisualModel> VisualModelPtr;
56 * @brief A visual text model contains layout specific information.
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.
62 class VisualModel : public RefObject
66 * @brief Create a new instance of a VisualModel.
68 * @return A pointer to a new VisualModel.
70 static VisualModelPtr New();
75 * @brief Creates the character to glyph conversion table.
77 * @pre The glyphs per character table needs to be created first.
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.
83 void CreateCharacterToGlyphTable(CharacterIndex startIndex,
84 GlyphIndex startGlyphIndex,
85 Length numberOfCharacters);
88 * @brief Creates an array containing the number of glyphs per character.
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.
94 void CreateGlyphsPerCharacterTable(CharacterIndex startIndex,
95 GlyphIndex startGlyphIndex,
96 Length numberOfCharacters);
99 * @brief Retrieves glyphs in the given buffer.
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.
106 void GetGlyphs(GlyphInfo* glyphs,
107 GlyphIndex glyphIndex,
108 Length numberOfGlyphs) const;
110 // Position interface
113 * @brief Retrieves the glyph positions.
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.
120 void GetGlyphPositions(Vector2* glyphPositions,
121 GlyphIndex glyphIndex,
122 Length numberOfGlyphs) const;
127 * @brief Retrieves the total number of lines.
129 * @return The number of lines.
131 Length GetTotalNumberOfLines() const;
134 * @brief Retrieves the number of lines and the index to the first line where the given range of glyphs is laid out.
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.
141 void GetNumberOfLines(GlyphIndex glyphIndex,
142 Length numberOfGlyphs,
143 LineIndex& firstLine,
144 Length& numberOfLines) const;
147 * @brief Retrieves the line index where the glyph is laid-out.
149 * @param[in] glyphIndex The glyph's index.
151 * @return The line index.
153 LineIndex GetLineOfGlyph(GlyphIndex glyphIndex);
156 * @brief Retrieves the lines where the given range of glyphs is laid out.
158 * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
160 * @param[out] lines Pointer to a buffer where the lines are copied.
161 * @param[in] glyphIndex Index to the first glyphs of the range.
162 * @param[in] numberOfGlyphs Number of glyphs in the range.
164 void GetLinesOfGlyphRange(LineRun* lines,
165 GlyphIndex glyphIndex,
166 Length numberOfGlyphs) const;
169 * @brief Retrieves the line index where the character is laid-out.
171 * @param[in] characterIndex The character's index.
173 * @return The line index.
175 LineIndex GetLineOfCharacter(CharacterIndex characterIndex);
180 * @brief Retrieves the underline runs.
182 * @param[out] underlineRuns Pointer to a buffer where the underline runs are copied.
183 * @param[in] index Index of the first underline run to be copied.
184 * @param[in] numberOfRuns Number of underline runs to be copied.
186 void GetUnderlineRuns(UnderlinedGlyphRun* underlineRuns,
187 UnderlineRunIndex index,
188 Length numberOfRuns) const;
193 * @brief Sets the natural size.
195 * @param[in] size The text's natural size.
197 void SetNaturalSize(const Vector2& size);
200 * @brief Retrieves the natural size.
202 * @return The text's natural size.
204 const Vector2& GetNaturalSize() const;
207 * @brief Sets the text's layout size.
209 * @param[in] size The text's size.
211 void SetLayoutSize(const Vector2& size);
214 * @brief Retrieves the text's layout size.
216 * @return The text's size.
218 const Vector2& GetLayoutSize() const;
221 * @brief Set the text's color
223 * @param[in] textColor The text's color
225 void SetTextColor(const Vector4& textColor);
228 * @brief Retrieve the text's color
230 * @return The text's color
232 const Vector4& GetTextColor() const;
235 * @brief Sets the text's shadow offset.
237 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
239 void SetShadowOffset(const Vector2& shadowOffset);
242 * @brief Retrieves the text's shadow offset.
244 * @return The text's shadow offset, 0,0 indicates no shadow.
246 const Vector2& GetShadowOffset() const;
249 * @brief Sets the text's shadow color.
251 * @param[in] shadowColor The shadow color.
253 void SetShadowColor(const Vector4& shadowColor);
256 * @brief Retrieves the text's shadow color.
258 * @return The text's shadow color.
260 const Vector4& GetShadowColor() const;
263 * @brief Set the shadow blur radius.
265 * @param[in] shadowBlurRadius The shadow blur radius, 0,0 indicates no blur.
267 void SetShadowBlurRadius(const float& shadowBlurRadius);
270 * @brief Retrieve the shadow blur radius.
272 * @return The shadow blur radius.
274 const float& GetShadowBlurRadius() const;
277 * @brief Sets the text's underline color.
279 * @param[in] color The text's underline color.
281 void SetUnderlineColor(const Vector4& color);
284 * @brief Retrieves the text's underline color.
286 * @return The text's underline color.
288 const Vector4& GetUnderlineColor() const;
291 * @brief Sets the text underline flag.
293 * @param[in] enabled true if underlined.
295 void SetUnderlineEnabled(bool enabled);
298 * @brief Returns whether the text is underlined or not.
300 * @return underline state.
302 bool IsUnderlineEnabled() const;
305 * @brief Clear the caches.
310 * @brief Set the override used for underline height, 0 indicates height will be come from font metrics
312 * @param[in] height The height in pixels of the underline
314 void SetUnderlineHeight(float height);
317 * @brief Set the override used for underline type.
319 * @param[in] underlineType The type of the underline.
321 void SetUnderlineType(Text::Underline::Type type);
324 * @brief Set the override used for the width of the dashes of the dashed underline.
326 * @param[in] width width of the dashes.
328 void SetDashedUnderlineWidth(float width);
331 * @brief Set the override used for the gap between the dashes of the dashed underline.
333 * @param[in] gap gap between the dashes.
335 void SetDashedUnderlineGap(float gap);
338 * @brief Retrieves the underline height override
340 * @return Returns the override height for an underline, 0 indicates that font metrics will determine the height
342 float GetUnderlineHeight() const;
345 * @brief Retrieves the underline type override.
347 * @return Returns the override type for the underline.
349 Text::Underline::Type GetUnderlineType() const;
352 * @brief Retrieves the dashed underline width.
354 * @return Returns the override width for the dashed underline.
356 float GetDashedUnderlineWidth() const;
359 * @brief Retrieves the dashed underline gap.
361 * @return Returns the override gap for the dashed underline.
363 float GetDashedUnderlineGap() const;
366 * @brief Retrieves the number of underline runs.
368 * @return The number of underline runs.
370 Length GetNumberOfUnderlineRuns() const;
373 * @brief Set the outline color.
375 * @param[in] color color of outline.
377 void SetOutlineColor(const Vector4& color);
380 * @brief Retrieve the outline color.
382 * @return The outline color.
384 const Vector4& GetOutlineColor() const;
387 * @brief Set the outline width
389 * @param[in] width The width in pixels of the outline, 0 indicates no outline
391 void SetOutlineWidth(uint16_t width);
394 * @brief Retrieves the width of an outline
396 * @return The width of the outline.
398 uint16_t GetOutlineWidth() const;
401 * @brief Sets the text's background color.
403 * @param[in] color The text's background color.
405 void SetBackgroundColor(const Vector4& color);
408 * @brief Retrieves the text's background color.
410 * @return The text's background color.
412 const Vector4& GetBackgroundColor() const;
415 * @brief Sets whether the text has a background or not.
417 * @param[in] enabled true if the text has a background.
419 void SetBackgroundEnabled(bool enabled);
422 * @brief Returns whether the text has a background or not.
424 * @return whether the text has a background or not.
426 bool IsBackgroundEnabled() const;
429 * @brief Sets whether the text has a markup-processor or not.
431 * @param[in] enabled true if the text has a markup-processor.
433 void SetMarkupProcessorEnabled(bool enabled);
436 * @brief Returns whether the text has a markup-processor or not.
438 * @return whether the text has a markup-processor or not.
440 bool IsMarkupProcessorEnabled() const;
443 * @brief Sets ellipsis position
444 * @param[in] ellipsisPosition The ellipsis position for the text
446 void SetEllipsisPosition(Toolkit::DevelText::EllipsisPosition::Type ellipsisPosition);
449 * @brief Retrieves ellipsis position for text.
451 * @return The ellipsis position.
453 Toolkit::DevelText::EllipsisPosition::Type GetEllipsisPosition() const;
456 * @brief Enable or disable the text elide.
458 * @param[in] enabled Whether to enable the text elide.
460 void SetTextElideEnabled(bool enabled);
463 * @brief Whether the text elide property is enabled.
465 * @return @e true if the text elide property is enabled, @e false otherwise.
467 bool IsTextElideEnabled() const;
470 * @brief Sets the start index of laid-out glyphs.
472 * @param[in] startIndexOfElidedGlyphs The start index of laid-out glyphs.
474 void SetStartIndexOfElidedGlyphs(GlyphIndex startIndexOfElidedGlyphs);
477 * @brief Sets the end index of elided glyphs.
479 * @param[in] endIndexOfElidedGlyphs The end index of elided glyphs.
481 void SetEndIndexOfElidedGlyphs(GlyphIndex endIndexOfElidedGlyphs);
484 * @brief Sets the first middle index of elided glyphs, index before ellipsis of middle.
486 * @param[in] firstMiddleIndexOfElidedGlyphs The first middle index of elided glyphs, index before ellipsis of middle.
488 void SetFirstMiddleIndexOfElidedGlyphs(GlyphIndex firstMiddleIndexOfElidedGlyphs);
491 * @brief Sets the second middle index of elided glyphs, index of ellipsis of middle.
493 * @param[in] secondMiddleIndexOfElidedGlyphs The second middle index of elided glyphs, index of ellipsis of middle.
495 void SetSecondMiddleIndexOfElidedGlyphs(GlyphIndex secondMiddleIndexOfElidedGlyphs);
498 * @brief Retrieves the start index of laid-out glyphs.
500 * @return The start index of laid-out glyphs.
502 GlyphIndex GetStartIndexOfElidedGlyphs() const;
505 * @brief Retrieves the end index of laid-out glyphs.
507 * @return The end index of laid-out glyphs.
509 GlyphIndex GetEndIndexOfElidedGlyphs() const;
512 * @brief Retrieves the first middle index of elided glyphs, index before ellipsis of middle.
514 * @return The first middle index of elided glyphs, index before ellipsis of middle.
516 GlyphIndex GetFirstMiddleIndexOfElidedGlyphs() const;
519 * @brief Retrieves the second middle index of elided glyphs, index of ellipsis of middle.
521 * @return The second middle index of elided glyphs, index of ellipsis of middle.
523 GlyphIndex GetSecondMiddleIndexOfElidedGlyphs() const;
526 * @brief Sets the text's strikethrough color.
528 * @param[in] color The text's strikethrough color.
530 void SetStrikethroughColor(const Vector4& color);
533 * @brief Retrieves the text's strikethrough color.
535 * @return The text's strikethrough color.
537 const Vector4& GetStrikethroughColor() const;
540 * @brief Sets the text strikethrough flag.
542 * @param[in] enabled true if strikethrough.
544 void SetStrikethroughEnabled(bool enabled);
547 * @brief Returns whether the text is strikethrough or not.
549 * @return strikethrough state.
551 bool IsStrikethroughEnabled() const;
554 * @brief Set the override used for strikethrough height, 0 indicates height will be come from font metrics
556 * @param[in] height The height in pixels of the strikethrough
558 void SetStrikethroughHeight(float height);
561 * @brief Retrieves the strikethrough height override
563 * @return Returns the override height for a strikethrough, 0 indicates that font metrics will determine the height
565 float GetStrikethroughHeight() const;
567 * @brief Set the override used for character spacing.
569 * @note A positive value will make the characters far apart (expanded) and a negative value will bring them closer (condensed).
571 * @param[in] characterSpacing The character spacing.
573 void SetCharacterSpacing(float characterSpacing);
576 * @brief Retrieves the characterSpacing.
578 * @note A positive value will make the characters far apart (expanded) and a negative value will bring them closer (condensed).
580 * @return Returns the characterSpacing.
582 const float GetCharacterSpacing() const;
585 * @brief Retrieves the Glyphs to Characters Array.
587 * @return The GlyphsToCharacters.
589 const Vector<CharacterIndex>& GetGlyphsToCharacters() const;
592 * @brief Retrieves the strikethrough runs.
594 * @param[out] strikethroughRuns Pointer to a buffer where the strikethrough runs are copied.
595 * @param[in] index Index of the first strikethrough run to be copied.
596 * @param[in] numberOfRuns Number of strikethrough runs to be copied.
598 void GetStrikethroughRuns(StrikethroughGlyphRun* strikethroughRuns,
599 StrikethroughRunIndex index,
600 Length numberOfRuns) const;
603 * @brief Retrieves the number of strikethrough runs.
605 * @return The number of strikethrough runs.
607 Length GetNumberOfStrikethroughRuns() const;
610 * @brief Retrieves the number of character-spacing glyph runs.
612 * @return The number of character-spacing glyph runs.
614 Length GetNumberOfCharacterSpacingGlyphRuns() const;
617 * @brief Retrieves the reference for character-spacing glyph runs.
619 * @return The reference for character-spacing glyph runs.
621 const Vector<CharacterSpacingGlyphRun>& GetCharacterSpacingGlyphRuns() const;
625 * @brief A reference counted object may only be deleted by calling Unreference().
627 virtual ~VisualModel();
631 * @brief Private constructor.
636 VisualModel(const VisualModel& handle);
639 VisualModel& operator=(const VisualModel& handle);
642 Vector<GlyphInfo> mGlyphs; ///< For each glyph, the font's id, glyph's index within the font and glyph's metrics.
643 Vector<CharacterIndex> mGlyphsToCharacters; ///< For each glyph, the index of the first character.
644 Vector<GlyphIndex> mCharactersToGlyph; ///< For each character, the index of the first glyph.
645 Vector<Length> mCharactersPerGlyph; ///< For each glyph, the number of characters that form the glyph.
646 Vector<Length> mGlyphsPerCharacter; ///< For each character, the number of glyphs that are shaped.
647 Vector<Vector2> mGlyphPositions; ///< For each glyph, the position.
648 Vector<LineRun> mLines; ///< The laid out lines.
649 Vector<UnderlinedGlyphRun> mUnderlineRuns; ///< Runs of glyphs that are underlined.
650 Vector<Vector4> mColors; ///< Colors of the glyphs.
651 Vector<ColorIndex> mColorIndices; ///< Indices to the vector of colors for each glyphs.
652 Vector<Vector4> mBackgroundColors; ///< Background colors of the glyphs.
653 Vector<ColorIndex> mBackgroundColorIndices; ///< Indices to the vector of background colors for each glyphs.
654 Vector4 mTextColor; ///< The text color
655 Vector4 mShadowColor; ///< Color of drop shadow
656 Vector4 mUnderlineColor; ///< Color of underline
657 Vector4 mOutlineColor; ///< Color of outline
658 Vector4 mBackgroundColor; ///< Color of text background
659 Vector4 mStrikethroughColor; ///< Color of text background
660 Size mControlSize; ///< The size of the UI control.
661 Vector2 mShadowOffset; ///< Offset for drop shadow, 0 indicates no shadow
662 float mUnderlineHeight; ///< Fixed height for underline to override font metrics.
663 float mStrikethroughHeight; ///< Fixed height for strikethrough to override font metrics.
664 Text::Underline::Type mUnderlineType; ///< The type of the underline.
665 float mDashedUnderlineWidth; ///< The width of the dashes of the dashed underline.
666 float mDashedUnderlineGap; ///< The gap between the dashes of the dashed underline.
667 float mShadowBlurRadius; ///< Blur radius of shadow, 0 indicates no blur.
668 uint16_t mOutlineWidth; ///< Width of outline.
669 Vector<StrikethroughGlyphRun> mStrikethroughRuns; ///< Runs of glyphs that have strikethrough.
670 Vector<CharacterSpacingGlyphRun> mCharacterSpacingRuns; ///< Runs of glyphs that have character-spacing.
673 Size mNaturalSize; ///< Size of the text with no line wrapping.
674 Size mLayoutSize; ///< Size of the laid-out text considering the layout properties set.
676 // Caches to increase performance in some consecutive operations.
677 LineIndex mCachedLineIndex; ///< Used to increase performance in consecutive calls to GetLineOfGlyph() or GetLineOfCharacter() with consecutive glyphs or characters.
679 DevelText::EllipsisPosition::Type mEllipsisPosition; ///< Where is the location the text elide
680 GlyphIndex mStartIndexOfElidedGlyphs; ///< The start index of elided glyphs.
681 GlyphIndex mEndIndexOfElidedGlyphs; ///< The end index of elided glyphs.
682 GlyphIndex mFirstMiddleIndexOfElidedGlyphs; ///< The first end index of elided glyphs, index before ellipsis of middle.
683 GlyphIndex mSecondMiddleIndexOfElidedGlyphs; ///< The first end index of elided glyphs, index of ellipsis of middle.
684 bool mTextElideEnabled : 1; ///< Whether the text's elide is enabled.
687 bool mUnderlineEnabled : 1; ///< Underline enabled flag
688 bool mUnderlineColorSet : 1; ///< Has the underline color been explicitly set?
689 bool mBackgroundEnabled : 1; ///< Background enabled flag
690 bool mMarkupProcessorEnabled : 1; ///< Markup-processor enabled flag
691 HyphenInfo mHyphen; ///< Contains hyphen glyph info & the character index to draw hyphen after.
692 bool mStrikethroughEnabled : 1; ///< Strikethrough enabled flag
693 float mCharacterSpacing; ///< Contains the value of the character spacing.
698 } // namespace Toolkit
702 #endif // DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H