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/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>
36 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
46 Vector<GlyphInfo> glyph;
47 Vector<Vector2> position;
52 typedef IntrusivePtr<VisualModel> VisualModelPtr;
55 * @brief A visual text model contains layout specific information.
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.
61 class VisualModel : public RefObject
65 * @brief Create a new instance of a VisualModel.
67 * @return A pointer to a new VisualModel.
69 static VisualModelPtr New();
74 * @brief Creates the character to glyph conversion table.
76 * @pre The glyphs per character table needs to be created first.
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.
82 void CreateCharacterToGlyphTable(CharacterIndex startIndex,
83 GlyphIndex startGlyphIndex,
84 Length numberOfCharacters);
87 * @brief Creates an array containing the number of glyphs per character.
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.
93 void CreateGlyphsPerCharacterTable(CharacterIndex startIndex,
94 GlyphIndex startGlyphIndex,
95 Length numberOfCharacters);
98 * @brief Retrieves glyphs in the given buffer.
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.
105 void GetGlyphs(GlyphInfo* glyphs,
106 GlyphIndex glyphIndex,
107 Length numberOfGlyphs) const;
109 // Position interface
112 * @brief Retrieves the glyph positions.
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.
119 void GetGlyphPositions(Vector2* glyphPositions,
120 GlyphIndex glyphIndex,
121 Length numberOfGlyphs) const;
126 * @brief Retrieves the total number of lines.
128 * @return The number of lines.
130 Length GetTotalNumberOfLines() const;
133 * @brief Retrieves the number of lines and the index to the first line where the given range of glyphs is laid out.
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.
140 void GetNumberOfLines(GlyphIndex glyphIndex,
141 Length numberOfGlyphs,
142 LineIndex& firstLine,
143 Length& numberOfLines) const;
146 * @brief Retrieves the lines where the given range of glyphs is laid out.
148 * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
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.
154 void GetLinesOfGlyphRange(LineRun* lines,
155 GlyphIndex glyphIndex,
156 Length numberOfGlyphs) const;
159 * @brief Retrieves the line index where the character is laid-out.
161 * @param[in] characterIndex The character's index.
163 * @return The line index.
165 LineIndex GetLineOfCharacter(CharacterIndex characterIndex);
170 * @brief Retrieves the underline runs.
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.
176 void GetUnderlineRuns(UnderlinedGlyphRun* underlineRuns,
177 UnderlineRunIndex index,
178 Length numberOfRuns) const;
183 * @brief Sets the natural size.
185 * @param[in] size The text's natural size.
187 void SetNaturalSize(const Vector2& size);
190 * @brief Retrieves the natural size.
192 * @return The text's natural size.
194 const Vector2& GetNaturalSize() const;
197 * @brief Sets the text's layout size.
199 * @param[in] size The text's size.
201 void SetLayoutSize(const Vector2& size);
204 * @brief Retrieves the text's layout size.
206 * @return The text's size.
208 const Vector2& GetLayoutSize() const;
211 * @brief Set the text's color
213 * @param[in] textColor The text's color
215 void SetTextColor(const Vector4& textColor);
218 * @brief Retrieve the text's color
220 * @return The text's color
222 const Vector4& GetTextColor() const;
225 * @brief Sets the text's shadow offset.
227 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
229 void SetShadowOffset(const Vector2& shadowOffset);
232 * @brief Retrieves the text's shadow offset.
234 * @return The text's shadow offset, 0,0 indicates no shadow.
236 const Vector2& GetShadowOffset() const;
239 * @brief Sets the text's shadow color.
241 * @param[in] shadowColor The shadow color.
243 void SetShadowColor(const Vector4& shadowColor);
246 * @brief Retrieves the text's shadow color.
248 * @return The text's shadow color.
250 const Vector4& GetShadowColor() const;
253 * @brief Set the shadow blur radius.
255 * @param[in] shadowBlurRadius The shadow blur radius, 0,0 indicates no blur.
257 void SetShadowBlurRadius(const float& shadowBlurRadius);
260 * @brief Retrieve the shadow blur radius.
262 * @return The shadow blur radius.
264 const float& GetShadowBlurRadius() const;
267 * @brief Sets the text's underline color.
269 * @param[in] color The text's underline color.
271 void SetUnderlineColor(const Vector4& color);
274 * @brief Retrieves the text's underline color.
276 * @return The text's underline color.
278 const Vector4& GetUnderlineColor() const;
281 * @brief Sets the text underline flag.
283 * @param[in] enabled true if underlined.
285 void SetUnderlineEnabled(bool enabled);
288 * @brief Returns whether the text is underlined or not.
290 * @return underline state.
292 bool IsUnderlineEnabled() const;
295 * @brief Clear the caches.
300 * @brief Set the override used for underline height, 0 indicates height will be come from font metrics
302 * @param[in] height The height in pixels of the underline
304 void SetUnderlineHeight(float height);
307 * @brief Set the override used for underline type.
309 * @param[in] underlineType The type of the underline.
311 void SetUnderlineType(Text::Underline::Type type);
314 * @brief Set the override used for the width of the dashes of the dashed underline.
316 * @param[in] width width of the dashes.
318 void SetDashedUnderlineWidth(float width);
321 * @brief Set the override used for the gap between the dashes of the dashed underline.
323 * @param[in] gap gap between the dashes.
325 void SetDashedUnderlineGap(float gap);
328 * @brief Retrieves the underline height override
330 * @return Returns the override height for an underline, 0 indicates that font metrics will determine the height
332 float GetUnderlineHeight() const;
335 * @brief Retrieves the underline type override.
337 * @return Returns the override type for the underline.
339 Text::Underline::Type GetUnderlineType() const;
342 * @brief Retrieves the dashed underline width.
344 * @return Returns the override width for the dashed underline.
346 float GetDashedUnderlineWidth() const;
349 * @brief Retrieves the dashed underline gap.
351 * @return Returns the override gap for the dashed underline.
353 float GetDashedUnderlineGap() const;
356 * @brief Retrieves the number of underline runs.
358 * @return The number of underline runs.
360 Length GetNumberOfUnderlineRuns() const;
363 * @brief Set the outline color.
365 * @param[in] color color of outline.
367 void SetOutlineColor(const Vector4& color);
370 * @brief Retrieve the outline color.
372 * @return The outline color.
374 const Vector4& GetOutlineColor() const;
377 * @brief Set the outline width
379 * @param[in] width The width in pixels of the outline, 0 indicates no outline
381 void SetOutlineWidth(uint16_t width);
384 * @brief Retrieves the width of an outline
386 * @return The width of the outline.
388 uint16_t GetOutlineWidth() const;
391 * @brief Sets the text's background color.
393 * @param[in] color The text's background color.
395 void SetBackgroundColor(const Vector4& color);
398 * @brief Retrieves the text's background color.
400 * @return The text's background color.
402 const Vector4& GetBackgroundColor() const;
405 * @brief Sets whether the text has a background or not.
407 * @param[in] enabled true if the text has a background.
409 void SetBackgroundEnabled(bool enabled);
412 * @brief Returns whether the text has a background or not.
414 * @return whether the text has a background or not.
416 bool IsBackgroundEnabled() const;
419 * @brief Sets whether the text has a markup-processor or not.
421 * @param[in] enabled true if the text has a markup-processor.
423 void SetMarkupProcessorEnabled(bool enabled);
426 * @brief Returns whether the text has a markup-processor or not.
428 * @return whether the text has a markup-processor or not.
430 bool IsMarkupProcessorEnabled() const;
433 * @brief Sets ellipsis position
434 * @param[in] ellipsisPosition The ellipsis position for the text
436 void SetEllipsisPosition(Toolkit::DevelText::EllipsisPosition::Type ellipsisPosition);
439 * @brief Retrieves ellipsis position for text.
441 * @return The ellipsis position.
443 Toolkit::DevelText::EllipsisPosition::Type GetEllipsisPosition() const;
446 * @brief Enable or disable the text elide.
448 * @param[in] enabled Whether to enable the text elide.
450 void SetTextElideEnabled(bool enabled);
453 * @brief Whether the text elide property is enabled.
455 * @return @e true if the text elide property is enabled, @e false otherwise.
457 bool IsTextElideEnabled() const;
460 * @brief Sets the start index of laid-out glyphs.
462 * @param[in] startIndexOfElidedGlyphs The start index of laid-out glyphs.
464 void SetStartIndexOfElidedGlyphs(GlyphIndex startIndexOfElidedGlyphs);
467 * @brief Sets the end index of elided glyphs.
469 * @param[in] endIndexOfElidedGlyphs The end index of elided glyphs.
471 void SetEndIndexOfElidedGlyphs(GlyphIndex endIndexOfElidedGlyphs);
474 * @brief Sets the first middle index of elided glyphs, index before ellipsis of middle.
476 * @param[in] firstMiddleIndexOfElidedGlyphs The first middle index of elided glyphs, index before ellipsis of middle.
478 void SetFirstMiddleIndexOfElidedGlyphs(GlyphIndex firstMiddleIndexOfElidedGlyphs);
481 * @brief Sets the second middle index of elided glyphs, index of ellipsis of middle.
483 * @param[in] secondMiddleIndexOfElidedGlyphs The second middle index of elided glyphs, index of ellipsis of middle.
485 void SetSecondMiddleIndexOfElidedGlyphs(GlyphIndex secondMiddleIndexOfElidedGlyphs);
488 * @brief Retrieves the start index of laid-out glyphs.
490 * @return The start index of laid-out glyphs.
492 GlyphIndex GetStartIndexOfElidedGlyphs() const;
495 * @brief Retrieves the end index of laid-out glyphs.
497 * @return The end index of laid-out glyphs.
499 GlyphIndex GetEndIndexOfElidedGlyphs() const;
502 * @brief Retrieves the first middle index of elided glyphs, index before ellipsis of middle.
504 * @return The first middle index of elided glyphs, index before ellipsis of middle.
506 GlyphIndex GetFirstMiddleIndexOfElidedGlyphs() const;
509 * @brief Retrieves the second middle index of elided glyphs, index of ellipsis of middle.
511 * @return The second middle index of elided glyphs, index of ellipsis of middle.
513 GlyphIndex GetSecondMiddleIndexOfElidedGlyphs() const;
516 * @brief Sets the text's strikethrough color.
518 * @param[in] color The text's strikethrough color.
520 void SetStrikethroughColor(const Vector4& color);
523 * @brief Retrieves the text's strikethrough color.
525 * @return The text's strikethrough color.
527 const Vector4& GetStrikethroughColor() const;
530 * @brief Sets the text strikethrough flag.
532 * @param[in] enabled true if strikethrough.
534 void SetStrikethroughEnabled(bool enabled);
537 * @brief Returns whether the text is strikethrough or not.
539 * @return strikethrough state.
541 bool IsStrikethroughEnabled() const;
544 * @brief Set the override used for strikethrough height, 0 indicates height will be come from font metrics
546 * @param[in] height The height in pixels of the strikethrough
548 void SetStrikethroughHeight(float height);
551 * @brief Retrieves the strikethrough height override
553 * @return Returns the override height for a strikethrough, 0 indicates that font metrics will determine the height
555 float GetStrikethroughHeight() const;
557 * @brief Set the override used for character spacing.
559 * @note A positive value will make the characters far apart (expanded) and a negative value will bring them closer (condensed).
561 * @param[in] characterSpacing The character spacing.
563 void SetCharacterSpacing(float characterSpacing);
566 * @brief Retrieves the characterSpacing.
568 * @note A positive value will make the characters far apart (expanded) and a negative value will bring them closer (condensed).
570 * @return Returns the characterSpacing.
572 const float GetCharacterSpacing() const;
575 * @brief Retrieves the Glyphs to Characters Array.
577 * @return The GlyphsToCharacters.
579 const Vector<CharacterIndex>& GetGlyphsToCharacters() const;
582 * @brief Retrieves the strikethrough runs.
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.
588 void GetStrikethroughRuns(StrikethroughGlyphRun* strikethroughRuns,
589 StrikethroughRunIndex index,
590 Length numberOfRuns) const;
593 * @brief Retrieves the number of strikethrough runs.
595 * @return The number of strikethrough runs.
597 Length GetNumberOfStrikethroughRuns() const;
601 * @brief A reference counted object may only be deleted by calling Unreference().
603 virtual ~VisualModel();
607 * @brief Private constructor.
612 VisualModel(const VisualModel& handle);
615 VisualModel& operator=(const VisualModel& handle);
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.
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.
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.
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.
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.
673 } // namespace Toolkit
677 #endif // DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H