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 number of lines and the index to the first line where the given range of glyphs is laid out.
125 * @param[in] glyphIndex Index to the first glyph.
126 * @param[in] numberOfGlyphs The number of glyph.
127 * @param[out] firstLine Index to the line containing the glyph index.
128 * @param[out] numberOfLines The number of lines.
130 void GetNumberOfLines(GlyphIndex glyphIndex,
131 Length numberOfGlyphs,
132 LineIndex& firstLine,
133 Length& numberOfLines) const;
136 * @brief Retrieves the lines where the given range of glyphs is laid out.
138 * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
140 * @param[out] lines Pointer to a buffer where the lines are copied.
141 * @param[in] glyphIndex Index to the first glyphs of the range.
142 * @param[in] numberOfGlyphs Number of glyphs in the range.
144 void GetLinesOfGlyphRange(LineRun* lines,
145 GlyphIndex glyphIndex,
146 Length numberOfGlyphs) const;
149 * @brief Retrieves the line index where the character is laid-out.
151 * @param[in] characterIndex The character's index.
153 * @return The line index.
155 LineIndex GetLineOfCharacter(CharacterIndex characterIndex);
160 * @brief Retrieves the underline runs.
162 * @param[out] underlineRuns Pointer to a buffer where the underline runs are copied.
163 * @param[in] index Index of the first underline run to be copied.
164 * @param[in] numberOfRuns Number of underline runs to be copied.
166 void GetUnderlineRuns(GlyphRun* underlineRuns,
167 UnderlineRunIndex index,
168 Length numberOfRuns) const;
173 * @brief Sets the natural size.
175 * @param[in] size The text's natural size.
177 void SetNaturalSize(const Vector2& size);
180 * @brief Retrieves the natural size.
182 * @return The text's natural size.
184 const Vector2& GetNaturalSize() const;
187 * @brief Sets the text's layout size.
189 * @param[in] size The text's size.
191 void SetLayoutSize(const Vector2& size);
194 * @brief Retrieves the text's layout size.
196 * @return The text's size.
198 const Vector2& GetLayoutSize() const;
201 * @brief Set the text's color
203 * @param[in] textColor The text's color
205 void SetTextColor(const Vector4& textColor);
208 * @brief Retrieve the text's color
210 * @return The text's color
212 const Vector4& GetTextColor() const;
215 * @brief Sets the text's shadow offset.
217 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
219 void SetShadowOffset(const Vector2& shadowOffset);
222 * @brief Retrieves the text's shadow offset.
224 * @return The text's shadow offset, 0,0 indicates no shadow.
226 const Vector2& GetShadowOffset() const;
229 * @brief Sets the text's shadow color.
231 * @param[in] shadowColor The shadow color.
233 void SetShadowColor(const Vector4& shadowColor);
236 * @brief Retrieves the text's shadow color.
238 * @return The text's shadow color.
240 const Vector4& GetShadowColor() const;
243 * @brief Set the shadow blur radius.
245 * @param[in] shadowBlurRadius The shadow blur radius, 0,0 indicates no blur.
247 void SetShadowBlurRadius(const float& shadowBlurRadius);
250 * @brief Retrieve the shadow blur radius.
252 * @return The shadow blur radius.
254 const float& GetShadowBlurRadius() const;
257 * @brief Sets the text's underline color.
259 * @param[in] color The text's underline color.
261 void SetUnderlineColor(const Vector4& color);
264 * @brief Retrieves the text's underline color.
266 * @return The text's underline color.
268 const Vector4& GetUnderlineColor() const;
271 * @brief Sets the text underline flag.
273 * @param[in] enabled true if underlined.
275 void SetUnderlineEnabled(bool enabled);
278 * @brief Returns whether the text is underlined or not.
280 * @return underline state.
282 bool IsUnderlineEnabled() const;
285 * @brief Clear the caches.
290 * @brief Set the override used for underline height, 0 indicates height will be come from font metrics
292 * @param[in] height The height in pixels of the underline
294 void SetUnderlineHeight(float height);
297 * @brief Retrieves the underline height override
299 * @return Returns the override height for an underline, 0 indicates that font metrics will determine the height
301 float GetUnderlineHeight() const;
304 * @brief Retrieves the number of underline runs.
306 * @return The number of underline runs.
308 Length GetNumberOfUnderlineRuns() const;
311 * @brief Set the outline color.
313 * @param[in] color color of outline.
315 void SetOutlineColor(const Vector4& color);
318 * @brief Retrieve the outline color.
320 * @return The outline color.
322 const Vector4& GetOutlineColor() const;
325 * @brief Set the outline width
327 * @param[in] width The width in pixels of the outline, 0 indicates no outline
329 void SetOutlineWidth(uint16_t width);
332 * @brief Retrieves the width of an outline
334 * @return The width of the outline.
336 uint16_t GetOutlineWidth() const;
339 * @brief Sets the text's background color.
341 * @param[in] color The text's background color.
343 void SetBackgroundColor(const Vector4& color);
346 * @brief Retrieves the text's background color.
348 * @return The text's background color.
350 const Vector4& GetBackgroundColor() const;
353 * @brief Sets whether the text has a background or not.
355 * @param[in] enabled true if the text has a background.
357 void SetBackgroundEnabled(bool enabled);
360 * @brief Returns whether the text has a background or not.
362 * @return whether the text has a background or not.
364 bool IsBackgroundEnabled() const;
367 * @brief Sets whether the text has a markup-processor or not.
369 * @param[in] enabled true if the text has a markup-processor.
371 void SetMarkupProcessorEnabled(bool enabled);
374 * @brief Returns whether the text has a markup-processor or not.
376 * @return whether the text has a markup-processor or not.
378 bool IsMarkupProcessorEnabled() const;
381 * @brief Sets ellipsis position
382 * @param[in] ellipsisPosition The ellipsis position for the text
384 void SetEllipsisPosition(Toolkit::DevelText::EllipsisPosition::Type ellipsisPosition);
387 * @brief Retrieves ellipsis position for text.
389 * @return The ellipsis position.
391 Toolkit::DevelText::EllipsisPosition::Type GetEllipsisPosition() const;
394 * @brief Enable or disable the text elide.
396 * @param[in] enabled Whether to enable the text elide.
398 void SetTextElideEnabled(bool enabled);
401 * @brief Whether the text elide property is enabled.
403 * @return @e true if the text elide property is enabled, @e false otherwise.
405 bool IsTextElideEnabled() const;
408 * @brief Sets the start index of laid-out glyphs.
410 * @param[in] startIndexOfElidedGlyphs The start index of laid-out glyphs.
412 void SetStartIndexOfElidedGlyphs(GlyphIndex startIndexOfElidedGlyphs);
415 * @brief Sets the end index of elided glyphs.
417 * @param[in] endIndexOfElidedGlyphs The end index of elided glyphs.
419 void SetEndIndexOfElidedGlyphs(GlyphIndex endIndexOfElidedGlyphs);
422 * @brief Sets the first middle index of elided glyphs, index before ellipsis of middle.
424 * @param[in] firstMiddleIndexOfElidedGlyphs The first middle index of elided glyphs, index before ellipsis of middle.
426 void SetFirstMiddleIndexOfElidedGlyphs(GlyphIndex firstMiddleIndexOfElidedGlyphs);
429 * @brief Sets the second middle index of elided glyphs, index of ellipsis of middle.
431 * @param[in] secondMiddleIndexOfElidedGlyphs The second middle index of elided glyphs, index of ellipsis of middle.
433 void SetSecondMiddleIndexOfElidedGlyphs(GlyphIndex secondMiddleIndexOfElidedGlyphs);
436 * @brief Retrieves the start index of laid-out glyphs.
438 * @return The start index of laid-out glyphs.
440 GlyphIndex GetStartIndexOfElidedGlyphs() const;
443 * @brief Retrieves the end index of laid-out glyphs.
445 * @return The end index of laid-out glyphs.
447 GlyphIndex GetEndIndexOfElidedGlyphs() const;
450 * @brief Retrieves the first middle index of elided glyphs, index before ellipsis of middle.
452 * @return The first middle index of elided glyphs, index before ellipsis of middle.
454 GlyphIndex GetFirstMiddleIndexOfElidedGlyphs() const;
457 * @brief Retrieves the second middle index of elided glyphs, index of ellipsis of middle.
459 * @return The second middle index of elided glyphs, index of ellipsis of middle.
461 GlyphIndex GetSecondMiddleIndexOfElidedGlyphs() const;
465 * @brief A reference counted object may only be deleted by calling Unreference().
467 virtual ~VisualModel();
471 * @brief Private constructor.
476 VisualModel(const VisualModel& handle);
479 VisualModel& operator=(const VisualModel& handle);
482 Vector<GlyphInfo> mGlyphs; ///< For each glyph, the font's id, glyph's index within the font and glyph's metrics.
483 Vector<CharacterIndex> mGlyphsToCharacters; ///< For each glyph, the index of the first character.
484 Vector<GlyphIndex> mCharactersToGlyph; ///< For each character, the index of the first glyph.
485 Vector<Length> mCharactersPerGlyph; ///< For each glyph, the number of characters that form the glyph.
486 Vector<Length> mGlyphsPerCharacter; ///< For each character, the number of glyphs that are shaped.
487 Vector<Vector2> mGlyphPositions; ///< For each glyph, the position.
488 Vector<LineRun> mLines; ///< The laid out lines.
489 Vector<GlyphRun> mUnderlineRuns; ///< Runs of glyphs that are underlined.
490 Vector<Vector4> mColors; ///< Colors of the glyphs.
491 Vector<ColorIndex> mColorIndices; ///< Indices to the vector of colors for each glyphs.
492 Vector<Vector4> mBackgroundColors; ///< Background colors of the glyphs.
493 Vector<ColorIndex> mBackgroundColorIndices; ///< Indices to the vector of background colors for each glyphs.
495 Vector4 mTextColor; ///< The text color
496 Vector4 mShadowColor; ///< Color of drop shadow
497 Vector4 mUnderlineColor; ///< Color of underline
498 Vector4 mOutlineColor; ///< Color of outline
499 Vector4 mBackgroundColor; ///< Color of text background
500 Size mControlSize; ///< The size of the UI control.
501 Vector2 mShadowOffset; ///< Offset for drop shadow, 0 indicates no shadow
502 float mUnderlineHeight; ///< Fixed height for underline to override font metrics.
503 float mShadowBlurRadius; ///< Blur radius of shadow, 0 indicates no blur.
504 uint16_t mOutlineWidth; ///< Width of outline.
507 Size mNaturalSize; ///< Size of the text with no line wrapping.
508 Size mLayoutSize; ///< Size of the laid-out text considering the layout properties set.
510 // Caches to increase performance in some consecutive operations.
511 LineIndex mCachedLineIndex; ///< Used to increase performance in consecutive calls to GetLineOfGlyph() or GetLineOfCharacter() with consecutive glyphs or characters.
513 DevelText::EllipsisPosition::Type mEllipsisPosition; ///< Where is the location the text elide
514 GlyphIndex mStartIndexOfElidedGlyphs; ///< The start index of elided glyphs.
515 GlyphIndex mEndIndexOfElidedGlyphs; ///< The end index of elided glyphs.
516 GlyphIndex mFirstMiddleIndexOfElidedGlyphs; ///< The first end index of elided glyphs, index before ellipsis of middle.
517 GlyphIndex mSecondMiddleIndexOfElidedGlyphs; ///< The first end index of elided glyphs, index of ellipsis of middle.
518 bool mTextElideEnabled : 1; ///< Whether the text's elide is enabled.
521 bool mUnderlineEnabled : 1; ///< Underline enabled flag
522 bool mUnderlineColorSet : 1; ///< Has the underline color been explicitly set?
523 bool mBackgroundEnabled : 1; ///< Background enabled flag
524 bool mMarkupProcessorEnabled : 1; ///< Markup-processor enabled flag
525 HyphenInfo mHyphen; ///< Contains hyphen glyph info & the character index to draw hyphen after.
530 } // namespace Toolkit
534 #endif // DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H