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/public-api/text/text-enumerations.h>
35 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
45 Vector<GlyphInfo> glyph;
46 Vector<Vector2> position;
51 typedef IntrusivePtr<VisualModel> VisualModelPtr;
54 * @brief A visual text model contains layout specific information.
57 * - A series of glyphs in visual order i.e. after the bidirectional reordering.
58 * - The position of each glyph within a 2D bounding box.
60 class VisualModel : public RefObject
64 * @brief Create a new instance of a VisualModel.
66 * @return A pointer to a new VisualModel.
68 static VisualModelPtr New();
73 * @brief Creates the character to glyph conversion table.
75 * @pre The glyphs per character table needs to be created first.
77 * @param[in] startIndex The character from where the conversion table is created.
78 * @param[in] startGlyphIndex The glyph from where the conversion table is created.
79 * @param[in] numberOfCharacters The number of characters.
81 void CreateCharacterToGlyphTable(CharacterIndex startIndex,
82 GlyphIndex startGlyphIndex,
83 Length numberOfCharacters);
86 * @brief Creates an array containing the number of glyphs per character.
88 * @param[in] startIndex The character from where the table is created.
89 * @param[in] startGlyphIndex The glyph from where the conversion table is created.
90 * @param[in] numberOfCharacters The number of characters.
92 void CreateGlyphsPerCharacterTable(CharacterIndex startIndex,
93 GlyphIndex startGlyphIndex,
94 Length numberOfCharacters);
97 * @brief Retrieves glyphs in the given buffer.
99 * The size of the @p glyphs buffer needs to be big enough to copy the @p numberOfGlyphs.
100 * @param[out] glyphs Pointer to a buffer where the glyphs are copied.
101 * @param[in] glyphIndex Index to the first glyph.
102 * @param[in] numberOfGlyphs Number of glyphs to be copied.
104 void GetGlyphs(GlyphInfo* glyphs,
105 GlyphIndex glyphIndex,
106 Length numberOfGlyphs) const;
108 // Position interface
111 * @brief Retrieves the glyph positions.
113 * @pre The size of the @p positions buffer needs to be big enough to copy the @p numberOfGlyphs positions.
114 * @param[out] glyphPositions Pointer to a buffer where the glyph positions are copied.
115 * @param[in] glyphIndex Index to the first glyph position.
116 * @param[in] numberOfGlyphs The number of positions to be copied.
118 void GetGlyphPositions(Vector2* glyphPositions,
119 GlyphIndex glyphIndex,
120 Length numberOfGlyphs) const;
125 * @brief Retrieves the total number of lines.
127 * @return The number of lines.
129 Length GetTotalNumberOfLines() const;
132 * @brief Retrieves the number of lines and the index to the first line where the given range of glyphs is laid out.
134 * @param[in] glyphIndex Index to the first glyph.
135 * @param[in] numberOfGlyphs The number of glyph.
136 * @param[out] firstLine Index to the line containing the glyph index.
137 * @param[out] numberOfLines The number of lines.
139 void GetNumberOfLines(GlyphIndex glyphIndex,
140 Length numberOfGlyphs,
141 LineIndex& firstLine,
142 Length& numberOfLines) const;
145 * @brief Retrieves the lines where the given range of glyphs is laid out.
147 * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
149 * @param[out] lines Pointer to a buffer where the lines are copied.
150 * @param[in] glyphIndex Index to the first glyphs of the range.
151 * @param[in] numberOfGlyphs Number of glyphs in the range.
153 void GetLinesOfGlyphRange(LineRun* lines,
154 GlyphIndex glyphIndex,
155 Length numberOfGlyphs) const;
158 * @brief Retrieves the line index where the character is laid-out.
160 * @param[in] characterIndex The character's index.
162 * @return The line index.
164 LineIndex GetLineOfCharacter(CharacterIndex characterIndex);
169 * @brief Retrieves the underline runs.
171 * @param[out] underlineRuns Pointer to a buffer where the underline runs are copied.
172 * @param[in] index Index of the first underline run to be copied.
173 * @param[in] numberOfRuns Number of underline runs to be copied.
175 void GetUnderlineRuns(GlyphRun* underlineRuns,
176 UnderlineRunIndex index,
177 Length numberOfRuns) const;
182 * @brief Sets the natural size.
184 * @param[in] size The text's natural size.
186 void SetNaturalSize(const Vector2& size);
189 * @brief Retrieves the natural size.
191 * @return The text's natural size.
193 const Vector2& GetNaturalSize() const;
196 * @brief Sets the text's layout size.
198 * @param[in] size The text's size.
200 void SetLayoutSize(const Vector2& size);
203 * @brief Retrieves the text's layout size.
205 * @return The text's size.
207 const Vector2& GetLayoutSize() const;
210 * @brief Set the text's color
212 * @param[in] textColor The text's color
214 void SetTextColor(const Vector4& textColor);
217 * @brief Retrieve the text's color
219 * @return The text's color
221 const Vector4& GetTextColor() const;
224 * @brief Sets the text's shadow offset.
226 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
228 void SetShadowOffset(const Vector2& shadowOffset);
231 * @brief Retrieves the text's shadow offset.
233 * @return The text's shadow offset, 0,0 indicates no shadow.
235 const Vector2& GetShadowOffset() const;
238 * @brief Sets the text's shadow color.
240 * @param[in] shadowColor The shadow color.
242 void SetShadowColor(const Vector4& shadowColor);
245 * @brief Retrieves the text's shadow color.
247 * @return The text's shadow color.
249 const Vector4& GetShadowColor() const;
252 * @brief Set the shadow blur radius.
254 * @param[in] shadowBlurRadius The shadow blur radius, 0,0 indicates no blur.
256 void SetShadowBlurRadius(const float& shadowBlurRadius);
259 * @brief Retrieve the shadow blur radius.
261 * @return The shadow blur radius.
263 const float& GetShadowBlurRadius() const;
266 * @brief Sets the text's underline color.
268 * @param[in] color The text's underline color.
270 void SetUnderlineColor(const Vector4& color);
273 * @brief Retrieves the text's underline color.
275 * @return The text's underline color.
277 const Vector4& GetUnderlineColor() const;
280 * @brief Sets the text underline flag.
282 * @param[in] enabled true if underlined.
284 void SetUnderlineEnabled(bool enabled);
287 * @brief Returns whether the text is underlined or not.
289 * @return underline state.
291 bool IsUnderlineEnabled() const;
294 * @brief Clear the caches.
299 * @brief Set the override used for underline height, 0 indicates height will be come from font metrics
301 * @param[in] height The height in pixels of the underline
303 void SetUnderlineHeight(float height);
306 * @brief Set the override used for underline type.
308 * @param[in] underlineType The type of the underline.
310 void SetUnderlineType(Text::Underline::Type type);
313 * @brief Set the override used for the width of the dashes of the dashed underline.
315 * @param[in] width width of the dashes.
317 void SetDashedUnderlineWidth(float width);
320 * @brief Set the override used for the gap between the dashes of the dashed underline.
322 * @param[in] gap gap between the dashes.
324 void SetDashedUnderlineGap(float gap);
327 * @brief Retrieves the underline height override
329 * @return Returns the override height for an underline, 0 indicates that font metrics will determine the height
331 float GetUnderlineHeight() const;
334 * @brief Retrieves the underline type override.
336 * @return Returns the override type for the underline.
338 Text::Underline::Type GetUnderlineType() const;
341 * @brief Retrieves the dashed underline width.
343 * @return Returns the override width for the dashed underline.
345 float GetDashedUnderlineWidth() const;
348 * @brief Retrieves the dashed underline gap.
350 * @return Returns the override gap for the dashed underline.
352 float GetDashedUnderlineGap() const;
355 * @brief Retrieves the number of underline runs.
357 * @return The number of underline runs.
359 Length GetNumberOfUnderlineRuns() const;
362 * @brief Set the outline color.
364 * @param[in] color color of outline.
366 void SetOutlineColor(const Vector4& color);
369 * @brief Retrieve the outline color.
371 * @return The outline color.
373 const Vector4& GetOutlineColor() const;
376 * @brief Set the outline width
378 * @param[in] width The width in pixels of the outline, 0 indicates no outline
380 void SetOutlineWidth(uint16_t width);
383 * @brief Retrieves the width of an outline
385 * @return The width of the outline.
387 uint16_t GetOutlineWidth() const;
390 * @brief Sets the text's background color.
392 * @param[in] color The text's background color.
394 void SetBackgroundColor(const Vector4& color);
397 * @brief Retrieves the text's background color.
399 * @return The text's background color.
401 const Vector4& GetBackgroundColor() const;
404 * @brief Sets whether the text has a background or not.
406 * @param[in] enabled true if the text has a background.
408 void SetBackgroundEnabled(bool enabled);
411 * @brief Returns whether the text has a background or not.
413 * @return whether the text has a background or not.
415 bool IsBackgroundEnabled() const;
418 * @brief Sets whether the text has a markup-processor or not.
420 * @param[in] enabled true if the text has a markup-processor.
422 void SetMarkupProcessorEnabled(bool enabled);
425 * @brief Returns whether the text has a markup-processor or not.
427 * @return whether the text has a markup-processor or not.
429 bool IsMarkupProcessorEnabled() const;
432 * @brief Sets ellipsis position
433 * @param[in] ellipsisPosition The ellipsis position for the text
435 void SetEllipsisPosition(Toolkit::DevelText::EllipsisPosition::Type ellipsisPosition);
438 * @brief Retrieves ellipsis position for text.
440 * @return The ellipsis position.
442 Toolkit::DevelText::EllipsisPosition::Type GetEllipsisPosition() const;
445 * @brief Enable or disable the text elide.
447 * @param[in] enabled Whether to enable the text elide.
449 void SetTextElideEnabled(bool enabled);
452 * @brief Whether the text elide property is enabled.
454 * @return @e true if the text elide property is enabled, @e false otherwise.
456 bool IsTextElideEnabled() const;
459 * @brief Sets the start index of laid-out glyphs.
461 * @param[in] startIndexOfElidedGlyphs The start index of laid-out glyphs.
463 void SetStartIndexOfElidedGlyphs(GlyphIndex startIndexOfElidedGlyphs);
466 * @brief Sets the end index of elided glyphs.
468 * @param[in] endIndexOfElidedGlyphs The end index of elided glyphs.
470 void SetEndIndexOfElidedGlyphs(GlyphIndex endIndexOfElidedGlyphs);
473 * @brief Sets the first middle index of elided glyphs, index before ellipsis of middle.
475 * @param[in] firstMiddleIndexOfElidedGlyphs The first middle index of elided glyphs, index before ellipsis of middle.
477 void SetFirstMiddleIndexOfElidedGlyphs(GlyphIndex firstMiddleIndexOfElidedGlyphs);
480 * @brief Sets the second middle index of elided glyphs, index of ellipsis of middle.
482 * @param[in] secondMiddleIndexOfElidedGlyphs The second middle index of elided glyphs, index of ellipsis of middle.
484 void SetSecondMiddleIndexOfElidedGlyphs(GlyphIndex secondMiddleIndexOfElidedGlyphs);
487 * @brief Retrieves the start index of laid-out glyphs.
489 * @return The start index of laid-out glyphs.
491 GlyphIndex GetStartIndexOfElidedGlyphs() const;
494 * @brief Retrieves the end index of laid-out glyphs.
496 * @return The end index of laid-out glyphs.
498 GlyphIndex GetEndIndexOfElidedGlyphs() const;
501 * @brief Retrieves the first middle index of elided glyphs, index before ellipsis of middle.
503 * @return The first middle index of elided glyphs, index before ellipsis of middle.
505 GlyphIndex GetFirstMiddleIndexOfElidedGlyphs() const;
508 * @brief Retrieves the second middle index of elided glyphs, index of ellipsis of middle.
510 * @return The second middle index of elided glyphs, index of ellipsis of middle.
512 GlyphIndex GetSecondMiddleIndexOfElidedGlyphs() const;
515 * @brief Sets the text's strikethrough color.
517 * @param[in] color The text's strikethrough color.
519 void SetStrikethroughColor(const Vector4& color);
522 * @brief Retrieves the text's strikethrough color.
524 * @return The text's strikethrough color.
526 const Vector4& GetStrikethroughColor() const;
529 * @brief Sets the text strikethrough flag.
531 * @param[in] enabled true if strikethrough.
533 void SetStrikethroughEnabled(bool enabled);
536 * @brief Returns whether the text is strikethrough or not.
538 * @return strikethrough state.
540 bool IsStrikethroughEnabled() const;
543 * @brief Set the override used for strikethrough height, 0 indicates height will be come from font metrics
545 * @param[in] height The height in pixels of the strikethrough
547 void SetStrikethroughHeight(float height);
550 * @brief Retrieves the strikethrough height override
552 * @return Returns the override height for a strikethrough, 0 indicates that font metrics will determine the height
554 float GetStrikethroughHeight() const;
556 * @brief Set the override used for character spacing.
558 * @note A positive value will make the characters far apart (expanded) and a negative value will bring them closer (condensed).
560 * @param[in] characterSpacing The character spacing.
562 void SetCharacterSpacing(float characterSpacing);
565 * @brief Retrieves the characterSpacing.
567 * @note A positive value will make the characters far apart (expanded) and a negative value will bring them closer (condensed).
569 * @return Returns the characterSpacing.
571 const float GetCharacterSpacing() const;
574 * @brief Retrieves the Glyphs to Characters Array.
576 * @return The GlyphsToCharacters.
578 const Vector<CharacterIndex>& GetGlyphsToCharacters() const;
581 * @brief Retrieves the strikethrough runs.
583 * @param[out] strikethroughRuns Pointer to a buffer where the strikethrough runs are copied.
584 * @param[in] index Index of the first strikethrough run to be copied.
585 * @param[in] numberOfRuns Number of strikethrough runs to be copied.
587 void GetStrikethroughRuns(StrikethroughGlyphRun* strikethroughRuns,
588 StrikethroughRunIndex index,
589 Length numberOfRuns) const;
592 * @brief Retrieves the number of strikethrough runs.
594 * @return The number of strikethrough runs.
596 Length GetNumberOfStrikethroughRuns() const;
600 * @brief A reference counted object may only be deleted by calling Unreference().
602 virtual ~VisualModel();
606 * @brief Private constructor.
611 VisualModel(const VisualModel& handle);
614 VisualModel& operator=(const VisualModel& handle);
617 Vector<GlyphInfo> mGlyphs; ///< For each glyph, the font's id, glyph's index within the font and glyph's metrics.
618 Vector<CharacterIndex> mGlyphsToCharacters; ///< For each glyph, the index of the first character.
619 Vector<GlyphIndex> mCharactersToGlyph; ///< For each character, the index of the first glyph.
620 Vector<Length> mCharactersPerGlyph; ///< For each glyph, the number of characters that form the glyph.
621 Vector<Length> mGlyphsPerCharacter; ///< For each character, the number of glyphs that are shaped.
622 Vector<Vector2> mGlyphPositions; ///< For each glyph, the position.
623 Vector<LineRun> mLines; ///< The laid out lines.
624 Vector<GlyphRun> mUnderlineRuns; ///< Runs of glyphs that are underlined.
625 Vector<Vector4> mColors; ///< Colors of the glyphs.
626 Vector<ColorIndex> mColorIndices; ///< Indices to the vector of colors for each glyphs.
627 Vector<Vector4> mBackgroundColors; ///< Background colors of the glyphs.
628 Vector<ColorIndex> mBackgroundColorIndices; ///< Indices to the vector of background colors for each glyphs.
629 Vector4 mTextColor; ///< The text color
630 Vector4 mShadowColor; ///< Color of drop shadow
631 Vector4 mUnderlineColor; ///< Color of underline
632 Vector4 mOutlineColor; ///< Color of outline
633 Vector4 mBackgroundColor; ///< Color of text background
634 Vector4 mStrikethroughColor; ///< Color of text background
635 Size mControlSize; ///< The size of the UI control.
636 Vector2 mShadowOffset; ///< Offset for drop shadow, 0 indicates no shadow
637 float mUnderlineHeight; ///< Fixed height for underline to override font metrics.
638 float mStrikethroughHeight; ///< Fixed height for strikethrough to override font metrics.
639 Text::Underline::Type mUnderlineType; ///< The type of the underline.
640 float mDashedUnderlineWidth; ///< The width of the dashes of the dashed underline.
641 float mDashedUnderlineGap; ///< The gap between the dashes of the dashed underline.
642 float mShadowBlurRadius; ///< Blur radius of shadow, 0 indicates no blur.
643 uint16_t mOutlineWidth; ///< Width of outline.
644 Vector<StrikethroughGlyphRun> mStrikethroughRuns; ///< Runs of glyphs that have strikethrough.
647 Size mNaturalSize; ///< Size of the text with no line wrapping.
648 Size mLayoutSize; ///< Size of the laid-out text considering the layout properties set.
650 // Caches to increase performance in some consecutive operations.
651 LineIndex mCachedLineIndex; ///< Used to increase performance in consecutive calls to GetLineOfGlyph() or GetLineOfCharacter() with consecutive glyphs or characters.
653 DevelText::EllipsisPosition::Type mEllipsisPosition; ///< Where is the location the text elide
654 GlyphIndex mStartIndexOfElidedGlyphs; ///< The start index of elided glyphs.
655 GlyphIndex mEndIndexOfElidedGlyphs; ///< The end index of elided glyphs.
656 GlyphIndex mFirstMiddleIndexOfElidedGlyphs; ///< The first end index of elided glyphs, index before ellipsis of middle.
657 GlyphIndex mSecondMiddleIndexOfElidedGlyphs; ///< The first end index of elided glyphs, index of ellipsis of middle.
658 bool mTextElideEnabled : 1; ///< Whether the text's elide is enabled.
661 bool mUnderlineEnabled : 1; ///< Underline enabled flag
662 bool mUnderlineColorSet : 1; ///< Has the underline color been explicitly set?
663 bool mBackgroundEnabled : 1; ///< Background enabled flag
664 bool mMarkupProcessorEnabled : 1; ///< Markup-processor enabled flag
665 HyphenInfo mHyphen; ///< Contains hyphen glyph info & the character index to draw hyphen after.
666 bool mStrikethroughEnabled : 1; ///< Strikethrough enabled flag
667 float mCharacterSpacing; ///< Contains the value of the character spacing.
672 } // namespace Toolkit
676 #endif // DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H