1 #ifndef DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H
2 #define DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H
5 * Copyright (c) 2021 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/public-api/text/text-enumerations.h>
34 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
44 Vector<GlyphInfo> glyph;
45 Vector<Vector2> position;
50 typedef IntrusivePtr<VisualModel> VisualModelPtr;
53 * @brief A visual text model contains layout specific information.
56 * - A series of glyphs in visual order i.e. after the bidirectional reordering.
57 * - The position of each glyph within a 2D bounding box.
59 class VisualModel : public RefObject
63 * @brief Create a new instance of a VisualModel.
65 * @return A pointer to a new VisualModel.
67 static VisualModelPtr New();
72 * @brief Creates the character to glyph conversion table.
74 * @pre The glyphs per character table needs to be created first.
76 * @param[in] startIndex The character from where the conversion table is created.
77 * @param[in] startGlyphIndex The glyph from where the conversion table is created.
78 * @param[in] numberOfCharacters The number of characters.
80 void CreateCharacterToGlyphTable(CharacterIndex startIndex,
81 GlyphIndex startGlyphIndex,
82 Length numberOfCharacters);
85 * @brief Creates an array containing the number of glyphs per character.
87 * @param[in] startIndex The character from where the table is created.
88 * @param[in] startGlyphIndex The glyph from where the conversion table is created.
89 * @param[in] numberOfCharacters The number of characters.
91 void CreateGlyphsPerCharacterTable(CharacterIndex startIndex,
92 GlyphIndex startGlyphIndex,
93 Length numberOfCharacters);
96 * @brief Retrieves glyphs in the given buffer.
98 * The size of the @p glyphs buffer needs to be big enough to copy the @p numberOfGlyphs.
99 * @param[out] glyphs Pointer to a buffer where the glyphs are copied.
100 * @param[in] glyphIndex Index to the first glyph.
101 * @param[in] numberOfGlyphs Number of glyphs to be copied.
103 void GetGlyphs(GlyphInfo* glyphs,
104 GlyphIndex glyphIndex,
105 Length numberOfGlyphs) const;
107 // Position interface
110 * @brief Retrieves the glyph positions.
112 * @pre The size of the @p positions buffer needs to be big enough to copy the @p numberOfGlyphs positions.
113 * @param[out] glyphPositions Pointer to a buffer where the glyph positions are copied.
114 * @param[in] glyphIndex Index to the first glyph position.
115 * @param[in] numberOfGlyphs The number of positions to be copied.
117 void GetGlyphPositions(Vector2* glyphPositions,
118 GlyphIndex glyphIndex,
119 Length numberOfGlyphs) const;
124 * @brief Retrieves the total number of lines.
126 * @return The number of lines.
128 Length GetTotalNumberOfLines() const;
131 * @brief Retrieves the number of lines and the index to the first line where the given range of glyphs is laid out.
133 * @param[in] glyphIndex Index to the first glyph.
134 * @param[in] numberOfGlyphs The number of glyph.
135 * @param[out] firstLine Index to the line containing the glyph index.
136 * @param[out] numberOfLines The number of lines.
138 void GetNumberOfLines(GlyphIndex glyphIndex,
139 Length numberOfGlyphs,
140 LineIndex& firstLine,
141 Length& numberOfLines) const;
144 * @brief Retrieves the lines where the given range of glyphs is laid out.
146 * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
148 * @param[out] lines Pointer to a buffer where the lines are copied.
149 * @param[in] glyphIndex Index to the first glyphs of the range.
150 * @param[in] numberOfGlyphs Number of glyphs in the range.
152 void GetLinesOfGlyphRange(LineRun* lines,
153 GlyphIndex glyphIndex,
154 Length numberOfGlyphs) const;
157 * @brief Retrieves the line index where the character is laid-out.
159 * @param[in] characterIndex The character's index.
161 * @return The line index.
163 LineIndex GetLineOfCharacter(CharacterIndex characterIndex);
168 * @brief Retrieves the underline runs.
170 * @param[out] underlineRuns Pointer to a buffer where the underline runs are copied.
171 * @param[in] index Index of the first underline run to be copied.
172 * @param[in] numberOfRuns Number of underline runs to be copied.
174 void GetUnderlineRuns(GlyphRun* underlineRuns,
175 UnderlineRunIndex index,
176 Length numberOfRuns) const;
181 * @brief Sets the natural size.
183 * @param[in] size The text's natural size.
185 void SetNaturalSize(const Vector2& size);
188 * @brief Retrieves the natural size.
190 * @return The text's natural size.
192 const Vector2& GetNaturalSize() const;
195 * @brief Sets the text's layout size.
197 * @param[in] size The text's size.
199 void SetLayoutSize(const Vector2& size);
202 * @brief Retrieves the text's layout size.
204 * @return The text's size.
206 const Vector2& GetLayoutSize() const;
209 * @brief Set the text's color
211 * @param[in] textColor The text's color
213 void SetTextColor(const Vector4& textColor);
216 * @brief Retrieve the text's color
218 * @return The text's color
220 const Vector4& GetTextColor() const;
223 * @brief Sets the text's shadow offset.
225 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
227 void SetShadowOffset(const Vector2& shadowOffset);
230 * @brief Retrieves the text's shadow offset.
232 * @return The text's shadow offset, 0,0 indicates no shadow.
234 const Vector2& GetShadowOffset() const;
237 * @brief Sets the text's shadow color.
239 * @param[in] shadowColor The shadow color.
241 void SetShadowColor(const Vector4& shadowColor);
244 * @brief Retrieves the text's shadow color.
246 * @return The text's shadow color.
248 const Vector4& GetShadowColor() const;
251 * @brief Set the shadow blur radius.
253 * @param[in] shadowBlurRadius The shadow blur radius, 0,0 indicates no blur.
255 void SetShadowBlurRadius(const float& shadowBlurRadius);
258 * @brief Retrieve the shadow blur radius.
260 * @return The shadow blur radius.
262 const float& GetShadowBlurRadius() const;
265 * @brief Sets the text's underline color.
267 * @param[in] color The text's underline color.
269 void SetUnderlineColor(const Vector4& color);
272 * @brief Retrieves the text's underline color.
274 * @return The text's underline color.
276 const Vector4& GetUnderlineColor() const;
279 * @brief Sets the text underline flag.
281 * @param[in] enabled true if underlined.
283 void SetUnderlineEnabled(bool enabled);
286 * @brief Returns whether the text is underlined or not.
288 * @return underline state.
290 bool IsUnderlineEnabled() const;
293 * @brief Clear the caches.
298 * @brief Set the override used for underline height, 0 indicates height will be come from font metrics
300 * @param[in] height The height in pixels of the underline
302 void SetUnderlineHeight(float height);
305 * @brief Set the override used for underline type.
307 * @param[in] underlineType The type of the underline.
309 void SetUnderlineType(Text::Underline::Type type);
312 * @brief Set the override used for the width of the dashes of the dashed underline.
314 * @param[in] width width of the dashes.
316 void SetDashedUnderlineWidth(float width);
319 * @brief Set the override used for the gap between the dashes of the dashed underline.
321 * @param[in] gap gap between the dashes.
323 void SetDashedUnderlineGap(float gap);
326 * @brief Retrieves the underline height override
328 * @return Returns the override height for an underline, 0 indicates that font metrics will determine the height
330 float GetUnderlineHeight() const;
333 * @brief Retrieves the underline type override.
335 * @return Returns the override type for the underline.
337 Text::Underline::Type GetUnderlineType() const;
340 * @brief Retrieves the dashed underline width.
342 * @return Returns the override width for the dashed underline.
344 float GetDashedUnderlineWidth() const;
347 * @brief Retrieves the dashed underline gap.
349 * @return Returns the override gap for the dashed underline.
351 float GetDashedUnderlineGap() const;
354 * @brief Retrieves the number of underline runs.
356 * @return The number of underline runs.
358 Length GetNumberOfUnderlineRuns() const;
361 * @brief Set the outline color.
363 * @param[in] color color of outline.
365 void SetOutlineColor(const Vector4& color);
368 * @brief Retrieve the outline color.
370 * @return The outline color.
372 const Vector4& GetOutlineColor() const;
375 * @brief Set the outline width
377 * @param[in] width The width in pixels of the outline, 0 indicates no outline
379 void SetOutlineWidth(uint16_t width);
382 * @brief Retrieves the width of an outline
384 * @return The width of the outline.
386 uint16_t GetOutlineWidth() const;
389 * @brief Sets the text's background color.
391 * @param[in] color The text's background color.
393 void SetBackgroundColor(const Vector4& color);
396 * @brief Retrieves the text's background color.
398 * @return The text's background color.
400 const Vector4& GetBackgroundColor() const;
403 * @brief Sets whether the text has a background or not.
405 * @param[in] enabled true if the text has a background.
407 void SetBackgroundEnabled(bool enabled);
410 * @brief Returns whether the text has a background or not.
412 * @return whether the text has a background or not.
414 bool IsBackgroundEnabled() const;
417 * @brief Sets whether the text has a markup-processor or not.
419 * @param[in] enabled true if the text has a markup-processor.
421 void SetMarkupProcessorEnabled(bool enabled);
424 * @brief Returns whether the text has a markup-processor or not.
426 * @return whether the text has a markup-processor or not.
428 bool IsMarkupProcessorEnabled() const;
431 * @brief Sets ellipsis position
432 * @param[in] ellipsisPosition The ellipsis position for the text
434 void SetEllipsisPosition(Toolkit::DevelText::EllipsisPosition::Type ellipsisPosition);
437 * @brief Retrieves ellipsis position for text.
439 * @return The ellipsis position.
441 Toolkit::DevelText::EllipsisPosition::Type GetEllipsisPosition() const;
444 * @brief Enable or disable the text elide.
446 * @param[in] enabled Whether to enable the text elide.
448 void SetTextElideEnabled(bool enabled);
451 * @brief Whether the text elide property is enabled.
453 * @return @e true if the text elide property is enabled, @e false otherwise.
455 bool IsTextElideEnabled() const;
458 * @brief Sets the start index of laid-out glyphs.
460 * @param[in] startIndexOfElidedGlyphs The start index of laid-out glyphs.
462 void SetStartIndexOfElidedGlyphs(GlyphIndex startIndexOfElidedGlyphs);
465 * @brief Sets the end index of elided glyphs.
467 * @param[in] endIndexOfElidedGlyphs The end index of elided glyphs.
469 void SetEndIndexOfElidedGlyphs(GlyphIndex endIndexOfElidedGlyphs);
472 * @brief Sets the first middle index of elided glyphs, index before ellipsis of middle.
474 * @param[in] firstMiddleIndexOfElidedGlyphs The first middle index of elided glyphs, index before ellipsis of middle.
476 void SetFirstMiddleIndexOfElidedGlyphs(GlyphIndex firstMiddleIndexOfElidedGlyphs);
479 * @brief Sets the second middle index of elided glyphs, index of ellipsis of middle.
481 * @param[in] secondMiddleIndexOfElidedGlyphs The second middle index of elided glyphs, index of ellipsis of middle.
483 void SetSecondMiddleIndexOfElidedGlyphs(GlyphIndex secondMiddleIndexOfElidedGlyphs);
486 * @brief Retrieves the start index of laid-out glyphs.
488 * @return The start index of laid-out glyphs.
490 GlyphIndex GetStartIndexOfElidedGlyphs() const;
493 * @brief Retrieves the end index of laid-out glyphs.
495 * @return The end index of laid-out glyphs.
497 GlyphIndex GetEndIndexOfElidedGlyphs() const;
500 * @brief Retrieves the first middle index of elided glyphs, index before ellipsis of middle.
502 * @return The first middle index of elided glyphs, index before ellipsis of middle.
504 GlyphIndex GetFirstMiddleIndexOfElidedGlyphs() const;
507 * @brief Retrieves the second middle index of elided glyphs, index of ellipsis of middle.
509 * @return The second middle index of elided glyphs, index of ellipsis of middle.
511 GlyphIndex GetSecondMiddleIndexOfElidedGlyphs() const;
514 * @brief Sets the text's strikethrough color.
516 * @param[in] color The text's strikethrough color.
518 void SetStrikethroughColor(const Vector4& color);
521 * @brief Retrieves the text's strikethrough color.
523 * @return The text's strikethrough color.
525 const Vector4& GetStrikethroughColor() const;
528 * @brief Sets the text strikethrough flag.
530 * @param[in] enabled true if strikethrough.
532 void SetStrikethroughEnabled(bool enabled);
535 * @brief Returns whether the text is strikethrough or not.
537 * @return strikethrough state.
539 bool IsStrikethroughEnabled() const;
542 * @brief Set the override used for strikethrough height, 0 indicates height will be come from font metrics
544 * @param[in] height The height in pixels of the strikethrough
546 void SetStrikethroughHeight(float height);
549 * @brief Retrieves the strikethrough height override
551 * @return Returns the override height for a strikethrough, 0 indicates that font metrics will determine the height
553 float GetStrikethroughHeight() const;
557 * @brief A reference counted object may only be deleted by calling Unreference().
559 virtual ~VisualModel();
563 * @brief Private constructor.
568 VisualModel(const VisualModel& handle);
571 VisualModel& operator=(const VisualModel& handle);
574 Vector<GlyphInfo> mGlyphs; ///< For each glyph, the font's id, glyph's index within the font and glyph's metrics.
575 Vector<CharacterIndex> mGlyphsToCharacters; ///< For each glyph, the index of the first character.
576 Vector<GlyphIndex> mCharactersToGlyph; ///< For each character, the index of the first glyph.
577 Vector<Length> mCharactersPerGlyph; ///< For each glyph, the number of characters that form the glyph.
578 Vector<Length> mGlyphsPerCharacter; ///< For each character, the number of glyphs that are shaped.
579 Vector<Vector2> mGlyphPositions; ///< For each glyph, the position.
580 Vector<LineRun> mLines; ///< The laid out lines.
581 Vector<GlyphRun> mUnderlineRuns; ///< Runs of glyphs that are underlined.
582 Vector<Vector4> mColors; ///< Colors of the glyphs.
583 Vector<ColorIndex> mColorIndices; ///< Indices to the vector of colors for each glyphs.
584 Vector<Vector4> mBackgroundColors; ///< Background colors of the glyphs.
585 Vector<ColorIndex> mBackgroundColorIndices; ///< Indices to the vector of background colors for each glyphs.
586 Vector4 mTextColor; ///< The text color
587 Vector4 mShadowColor; ///< Color of drop shadow
588 Vector4 mUnderlineColor; ///< Color of underline
589 Vector4 mOutlineColor; ///< Color of outline
590 Vector4 mBackgroundColor; ///< Color of text background
591 Vector4 mStrikethroughColor; ///< Color of text background
592 Size mControlSize; ///< The size of the UI control.
593 Vector2 mShadowOffset; ///< Offset for drop shadow, 0 indicates no shadow
594 float mUnderlineHeight; ///< Fixed height for underline to override font metrics.
595 float mStrikethroughHeight; ///< Fixed height for strikethrough to override font metrics.
596 Text::Underline::Type mUnderlineType; ///< The type of the underline.
597 float mDashedUnderlineWidth; ///< The width of the dashes of the dashed underline.
598 float mDashedUnderlineGap; ///< The gap between the dashes of the dashed underline.
599 float mShadowBlurRadius; ///< Blur radius of shadow, 0 indicates no blur.
600 uint16_t mOutlineWidth; ///< Width of outline.
603 Size mNaturalSize; ///< Size of the text with no line wrapping.
604 Size mLayoutSize; ///< Size of the laid-out text considering the layout properties set.
606 // Caches to increase performance in some consecutive operations.
607 LineIndex mCachedLineIndex; ///< Used to increase performance in consecutive calls to GetLineOfGlyph() or GetLineOfCharacter() with consecutive glyphs or characters.
609 DevelText::EllipsisPosition::Type mEllipsisPosition; ///< Where is the location the text elide
610 GlyphIndex mStartIndexOfElidedGlyphs; ///< The start index of elided glyphs.
611 GlyphIndex mEndIndexOfElidedGlyphs; ///< The end index of elided glyphs.
612 GlyphIndex mFirstMiddleIndexOfElidedGlyphs; ///< The first end index of elided glyphs, index before ellipsis of middle.
613 GlyphIndex mSecondMiddleIndexOfElidedGlyphs; ///< The first end index of elided glyphs, index of ellipsis of middle.
614 bool mTextElideEnabled : 1; ///< Whether the text's elide is enabled.
617 bool mUnderlineEnabled : 1; ///< Underline enabled flag
618 bool mUnderlineColorSet : 1; ///< Has the underline color been explicitly set?
619 bool mBackgroundEnabled : 1; ///< Background enabled flag
620 bool mMarkupProcessorEnabled : 1; ///< Markup-processor enabled flag
621 HyphenInfo mHyphen; ///< Contains hyphen glyph info & the character index to draw hyphen after.
622 bool mStrikethroughEnabled : 1; ///< Strikethrough enabled flag
627 } // namespace Toolkit
631 #endif // DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H