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>
33 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
43 Vector<GlyphInfo> glyph;
44 Vector<Vector2> position;
49 typedef IntrusivePtr<VisualModel> VisualModelPtr;
52 * @brief A visual text model contains layout specific information.
55 * - A series of glyphs in visual order i.e. after the bidirectional reordering.
56 * - The position of each glyph within a 2D bounding box.
58 class VisualModel : public RefObject
62 * @brief Create a new instance of a VisualModel.
64 * @return A pointer to a new VisualModel.
66 static VisualModelPtr New();
71 * @brief Creates the character to glyph conversion table.
73 * @pre The glyphs per character table needs to be created first.
75 * @param[in] startIndex The character from where the conversion table is created.
76 * @param[in] startGlyphIndex The glyph from where the conversion table is created.
77 * @param[in] numberOfCharacters The number of characters.
79 void CreateCharacterToGlyphTable(CharacterIndex startIndex,
80 GlyphIndex startGlyphIndex,
81 Length numberOfCharacters);
84 * @brief Creates an array containing the number of glyphs per character.
86 * @param[in] startIndex The character from where the table is created.
87 * @param[in] startGlyphIndex The glyph from where the conversion table is created.
88 * @param[in] numberOfCharacters The number of characters.
90 void CreateGlyphsPerCharacterTable(CharacterIndex startIndex,
91 GlyphIndex startGlyphIndex,
92 Length numberOfCharacters);
95 * @brief Retrieves glyphs in the given buffer.
97 * The size of the @p glyphs buffer needs to be big enough to copy the @p numberOfGlyphs.
98 * @param[out] glyphs Pointer to a buffer where the glyphs are copied.
99 * @param[in] glyphIndex Index to the first glyph.
100 * @param[in] numberOfGlyphs Number of glyphs to be copied.
102 void GetGlyphs(GlyphInfo* glyphs,
103 GlyphIndex glyphIndex,
104 Length numberOfGlyphs) const;
106 // Position interface
109 * @brief Retrieves the glyph positions.
111 * @pre The size of the @p positions buffer needs to be big enough to copy the @p numberOfGlyphs positions.
112 * @param[out] glyphPositions Pointer to a buffer where the glyph positions are copied.
113 * @param[in] glyphIndex Index to the first glyph position.
114 * @param[in] numberOfGlyphs The number of positions to be copied.
116 void GetGlyphPositions(Vector2* glyphPositions,
117 GlyphIndex glyphIndex,
118 Length numberOfGlyphs) const;
123 * @brief Retrieves the total number of lines.
125 * @return The number of lines.
127 Length GetTotalNumberOfLines() const;
130 * @brief Retrieves the number of lines and the index to the first line where the given range of glyphs is laid out.
132 * @param[in] glyphIndex Index to the first glyph.
133 * @param[in] numberOfGlyphs The number of glyph.
134 * @param[out] firstLine Index to the line containing the glyph index.
135 * @param[out] numberOfLines The number of lines.
137 void GetNumberOfLines(GlyphIndex glyphIndex,
138 Length numberOfGlyphs,
139 LineIndex& firstLine,
140 Length& numberOfLines) const;
143 * @brief Retrieves the lines where the given range of glyphs is laid out.
145 * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
147 * @param[out] lines Pointer to a buffer where the lines are copied.
148 * @param[in] glyphIndex Index to the first glyphs of the range.
149 * @param[in] numberOfGlyphs Number of glyphs in the range.
151 void GetLinesOfGlyphRange(LineRun* lines,
152 GlyphIndex glyphIndex,
153 Length numberOfGlyphs) const;
156 * @brief Retrieves the line index where the character is laid-out.
158 * @param[in] characterIndex The character's index.
160 * @return The line index.
162 LineIndex GetLineOfCharacter(CharacterIndex characterIndex);
167 * @brief Retrieves the underline runs.
169 * @param[out] underlineRuns Pointer to a buffer where the underline runs are copied.
170 * @param[in] index Index of the first underline run to be copied.
171 * @param[in] numberOfRuns Number of underline runs to be copied.
173 void GetUnderlineRuns(GlyphRun* underlineRuns,
174 UnderlineRunIndex index,
175 Length numberOfRuns) const;
180 * @brief Sets the natural size.
182 * @param[in] size The text's natural size.
184 void SetNaturalSize(const Vector2& size);
187 * @brief Retrieves the natural size.
189 * @return The text's natural size.
191 const Vector2& GetNaturalSize() const;
194 * @brief Sets the text's layout size.
196 * @param[in] size The text's size.
198 void SetLayoutSize(const Vector2& size);
201 * @brief Retrieves the text's layout size.
203 * @return The text's size.
205 const Vector2& GetLayoutSize() const;
208 * @brief Set the text's color
210 * @param[in] textColor The text's color
212 void SetTextColor(const Vector4& textColor);
215 * @brief Retrieve the text's color
217 * @return The text's color
219 const Vector4& GetTextColor() const;
222 * @brief Sets the text's shadow offset.
224 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
226 void SetShadowOffset(const Vector2& shadowOffset);
229 * @brief Retrieves the text's shadow offset.
231 * @return The text's shadow offset, 0,0 indicates no shadow.
233 const Vector2& GetShadowOffset() const;
236 * @brief Sets the text's shadow color.
238 * @param[in] shadowColor The shadow color.
240 void SetShadowColor(const Vector4& shadowColor);
243 * @brief Retrieves the text's shadow color.
245 * @return The text's shadow color.
247 const Vector4& GetShadowColor() const;
250 * @brief Set the shadow blur radius.
252 * @param[in] shadowBlurRadius The shadow blur radius, 0,0 indicates no blur.
254 void SetShadowBlurRadius(const float& shadowBlurRadius);
257 * @brief Retrieve the shadow blur radius.
259 * @return The shadow blur radius.
261 const float& GetShadowBlurRadius() const;
264 * @brief Sets the text's underline color.
266 * @param[in] color The text's underline color.
268 void SetUnderlineColor(const Vector4& color);
271 * @brief Retrieves the text's underline color.
273 * @return The text's underline color.
275 const Vector4& GetUnderlineColor() const;
278 * @brief Sets the text underline flag.
280 * @param[in] enabled true if underlined.
282 void SetUnderlineEnabled(bool enabled);
285 * @brief Returns whether the text is underlined or not.
287 * @return underline state.
289 bool IsUnderlineEnabled() const;
292 * @brief Clear the caches.
297 * @brief Set the override used for underline height, 0 indicates height will be come from font metrics
299 * @param[in] height The height in pixels of the underline
301 void SetUnderlineHeight(float height);
304 * @brief Retrieves the underline height override
306 * @return Returns the override height for an underline, 0 indicates that font metrics will determine the height
308 float GetUnderlineHeight() const;
311 * @brief Retrieves the number of underline runs.
313 * @return The number of underline runs.
315 Length GetNumberOfUnderlineRuns() const;
318 * @brief Set the outline color.
320 * @param[in] color color of outline.
322 void SetOutlineColor(const Vector4& color);
325 * @brief Retrieve the outline color.
327 * @return The outline color.
329 const Vector4& GetOutlineColor() const;
332 * @brief Set the outline width
334 * @param[in] width The width in pixels of the outline, 0 indicates no outline
336 void SetOutlineWidth(uint16_t width);
339 * @brief Retrieves the width of an outline
341 * @return The width of the outline.
343 uint16_t GetOutlineWidth() const;
346 * @brief Sets the text's background color.
348 * @param[in] color The text's background color.
350 void SetBackgroundColor(const Vector4& color);
353 * @brief Retrieves the text's background color.
355 * @return The text's background color.
357 const Vector4& GetBackgroundColor() const;
360 * @brief Sets whether the text has a background or not.
362 * @param[in] enabled true if the text has a background.
364 void SetBackgroundEnabled(bool enabled);
367 * @brief Returns whether the text has a background or not.
369 * @return whether the text has a background or not.
371 bool IsBackgroundEnabled() const;
374 * @brief Sets whether the text has a markup-processor or not.
376 * @param[in] enabled true if the text has a markup-processor.
378 void SetMarkupProcessorEnabled(bool enabled);
381 * @brief Returns whether the text has a markup-processor or not.
383 * @return whether the text has a markup-processor or not.
385 bool IsMarkupProcessorEnabled() const;
388 * @brief Sets ellipsis position
389 * @param[in] ellipsisPosition The ellipsis position for the text
391 void SetEllipsisPosition(Toolkit::DevelText::EllipsisPosition::Type ellipsisPosition);
394 * @brief Retrieves ellipsis position for text.
396 * @return The ellipsis position.
398 Toolkit::DevelText::EllipsisPosition::Type GetEllipsisPosition() const;
401 * @brief Enable or disable the text elide.
403 * @param[in] enabled Whether to enable the text elide.
405 void SetTextElideEnabled(bool enabled);
408 * @brief Whether the text elide property is enabled.
410 * @return @e true if the text elide property is enabled, @e false otherwise.
412 bool IsTextElideEnabled() const;
415 * @brief Sets the start index of laid-out glyphs.
417 * @param[in] startIndexOfElidedGlyphs The start index of laid-out glyphs.
419 void SetStartIndexOfElidedGlyphs(GlyphIndex startIndexOfElidedGlyphs);
422 * @brief Sets the end index of elided glyphs.
424 * @param[in] endIndexOfElidedGlyphs The end index of elided glyphs.
426 void SetEndIndexOfElidedGlyphs(GlyphIndex endIndexOfElidedGlyphs);
429 * @brief Sets the first middle index of elided glyphs, index before ellipsis of middle.
431 * @param[in] firstMiddleIndexOfElidedGlyphs The first middle index of elided glyphs, index before ellipsis of middle.
433 void SetFirstMiddleIndexOfElidedGlyphs(GlyphIndex firstMiddleIndexOfElidedGlyphs);
436 * @brief Sets the second middle index of elided glyphs, index of ellipsis of middle.
438 * @param[in] secondMiddleIndexOfElidedGlyphs The second middle index of elided glyphs, index of ellipsis of middle.
440 void SetSecondMiddleIndexOfElidedGlyphs(GlyphIndex secondMiddleIndexOfElidedGlyphs);
443 * @brief Retrieves the start index of laid-out glyphs.
445 * @return The start index of laid-out glyphs.
447 GlyphIndex GetStartIndexOfElidedGlyphs() const;
450 * @brief Retrieves the end index of laid-out glyphs.
452 * @return The end index of laid-out glyphs.
454 GlyphIndex GetEndIndexOfElidedGlyphs() const;
457 * @brief Retrieves the first middle index of elided glyphs, index before ellipsis of middle.
459 * @return The first middle index of elided glyphs, index before ellipsis of middle.
461 GlyphIndex GetFirstMiddleIndexOfElidedGlyphs() const;
464 * @brief Retrieves the second middle index of elided glyphs, index of ellipsis of middle.
466 * @return The second middle index of elided glyphs, index of ellipsis of middle.
468 GlyphIndex GetSecondMiddleIndexOfElidedGlyphs() const;
472 * @brief A reference counted object may only be deleted by calling Unreference().
474 virtual ~VisualModel();
478 * @brief Private constructor.
483 VisualModel(const VisualModel& handle);
486 VisualModel& operator=(const VisualModel& handle);
489 Vector<GlyphInfo> mGlyphs; ///< For each glyph, the font's id, glyph's index within the font and glyph's metrics.
490 Vector<CharacterIndex> mGlyphsToCharacters; ///< For each glyph, the index of the first character.
491 Vector<GlyphIndex> mCharactersToGlyph; ///< For each character, the index of the first glyph.
492 Vector<Length> mCharactersPerGlyph; ///< For each glyph, the number of characters that form the glyph.
493 Vector<Length> mGlyphsPerCharacter; ///< For each character, the number of glyphs that are shaped.
494 Vector<Vector2> mGlyphPositions; ///< For each glyph, the position.
495 Vector<LineRun> mLines; ///< The laid out lines.
496 Vector<GlyphRun> mUnderlineRuns; ///< Runs of glyphs that are underlined.
497 Vector<Vector4> mColors; ///< Colors of the glyphs.
498 Vector<ColorIndex> mColorIndices; ///< Indices to the vector of colors for each glyphs.
499 Vector<Vector4> mBackgroundColors; ///< Background colors of the glyphs.
500 Vector<ColorIndex> mBackgroundColorIndices; ///< Indices to the vector of background colors for each glyphs.
502 Vector4 mTextColor; ///< The text color
503 Vector4 mShadowColor; ///< Color of drop shadow
504 Vector4 mUnderlineColor; ///< Color of underline
505 Vector4 mOutlineColor; ///< Color of outline
506 Vector4 mBackgroundColor; ///< Color of text background
507 Size mControlSize; ///< The size of the UI control.
508 Vector2 mShadowOffset; ///< Offset for drop shadow, 0 indicates no shadow
509 float mUnderlineHeight; ///< Fixed height for underline to override font metrics.
510 float mShadowBlurRadius; ///< Blur radius of shadow, 0 indicates no blur.
511 uint16_t mOutlineWidth; ///< Width of outline.
514 Size mNaturalSize; ///< Size of the text with no line wrapping.
515 Size mLayoutSize; ///< Size of the laid-out text considering the layout properties set.
517 // Caches to increase performance in some consecutive operations.
518 LineIndex mCachedLineIndex; ///< Used to increase performance in consecutive calls to GetLineOfGlyph() or GetLineOfCharacter() with consecutive glyphs or characters.
520 DevelText::EllipsisPosition::Type mEllipsisPosition; ///< Where is the location the text elide
521 GlyphIndex mStartIndexOfElidedGlyphs; ///< The start index of elided glyphs.
522 GlyphIndex mEndIndexOfElidedGlyphs; ///< The end index of elided glyphs.
523 GlyphIndex mFirstMiddleIndexOfElidedGlyphs; ///< The first end index of elided glyphs, index before ellipsis of middle.
524 GlyphIndex mSecondMiddleIndexOfElidedGlyphs; ///< The first end index of elided glyphs, index of ellipsis of middle.
525 bool mTextElideEnabled : 1; ///< Whether the text's elide is enabled.
528 bool mUnderlineEnabled : 1; ///< Underline enabled flag
529 bool mUnderlineColorSet : 1; ///< Has the underline color been explicitly set?
530 bool mBackgroundEnabled : 1; ///< Background enabled flag
531 bool mMarkupProcessorEnabled : 1; ///< Markup-processor enabled flag
532 HyphenInfo mHyphen; ///< Contains hyphen glyph info & the character index to draw hyphen after.
537 } // namespace Toolkit
541 #endif // DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H