1 #ifndef DALI_TOOLKIT_INTERNAL_TEXT_LABEL_H
2 #define DALI_TOOLKIT_INTERNAL_TEXT_LABEL_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/devel-api/actors/actor-devel.h>
23 #include <dali/devel-api/atspi-interfaces/hypertext.h>
24 #include <dali/devel-api/atspi-interfaces/text.h>
25 #include <dali/public-api/object/property-map.h>
28 #include <dali-toolkit/devel-api/text/spanned.h>
29 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
30 #include <dali-toolkit/internal/controls/text-controls/common-text-utils.h>
31 #include <dali-toolkit/internal/text/async-text/async-text-loader.h>
32 #include <dali-toolkit/internal/text/controller/text-controller.h>
33 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
34 #include <dali-toolkit/internal/text/text-anchor-control-interface.h>
35 #include <dali-toolkit/internal/text/text-control-interface.h>
36 #include <dali-toolkit/internal/text/text-scroller-interface.h>
37 #include <dali-toolkit/internal/text/text-scroller.h>
38 #include <dali-toolkit/internal/visuals/text/text-visual.h>
39 #include <dali-toolkit/public-api/controls/control-impl.h>
40 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
48 * @brief A control which renders a short text string.
50 class TextLabel : public Control, public Text::ControlInterface, public Text::ScrollerInterface, public Text::AnchorControlInterface, public Text::AsyncTextInterface
54 * @copydoc Dali::Toollkit::TextLabel::New()
55 * @param[in] additionalBehaviour custom behavior flags for this TextLabel. Default is CONTROL_BEHAVIOUR_DEFAULT
57 static Toolkit::TextLabel New(ControlBehaviour additionalBehaviour = ControlBehaviour::CONTROL_BEHAVIOUR_DEFAULT);
62 * @brief Called when a property of an object of this type is set.
64 * @param[in] object The object whose property is set.
65 * @param[in] index The property index.
66 * @param[in] value The new property value.
68 static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
71 * @brief Called to retrieve a property of an object of this type.
73 * @param[in] object The object whose property is to be retrieved.
74 * @param[in] index The property index.
75 * @return The current value of the property.
77 static Property::Value GetProperty(BaseObject* object, Property::Index index);
80 * @copydoc Dali::Toollkit::TextLabel::AnchorClickedSignal()
82 DevelTextLabel::AnchorClickedSignalType& AnchorClickedSignal();
85 * @copydoc Dali::Toollkit::TextLabel::TextFitChangedSignal()
87 DevelTextLabel::TextFitChangedSignalType& TextFitChangedSignal();
90 * @copydoc Dali::Toollkit::TextLabel::AsyncTextRenderedSignal()
92 DevelTextLabel::AsyncTextRenderedSignalType& AsyncTextRenderedSignal();
95 * @copydoc Dali::Toollkit::TextLabel::AsyncNaturalSizeComputedSignal()
97 DevelTextLabel::AsyncNaturalSizeComputedSignalType& AsyncNaturalSizeComputedSignal();
100 * @copydoc Dali::Toollkit::TextLabel::AsyncHeightForWidthComputedSignal()
102 DevelTextLabel::AsyncHeightForWidthComputedSignalType& AsyncHeightForWidthComputedSignal();
105 * Connects a callback function with the object's signals.
106 * @param[in] object The object providing the signal.
107 * @param[in] tracker Used to disconnect the signal.
108 * @param[in] signalName The signal to connect to.
109 * @param[in] functor A newly allocated FunctorDelegate.
110 * @return True if the signal was connected.
111 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
113 static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
116 * @brief Gets text controller
118 * @return The text controller
120 Text::ControllerPtr GetTextController();
123 * @brief Get the rendered size of a specific text range.
124 * if the requested text is at multilines, multiple sizes will be returned for each text located in a separate line.
125 * if a line contains characters with different directions, multiple sizes will be returned for each block of contiguous characters with the same direction.
127 * @param[in] startIndex start index of the text requested to calculate size for.
128 * @param[in] endIndex end index(included) of the text requested to calculate size for.
129 * @return list of sizes of the reuested text.
131 Vector<Vector2> GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const;
134 * @brief Get the top/left rendered position of a specific text range.
135 * if the requested text is at multilines, multiple positions will be returned for each text located in a separate line.
136 * if a line contains characters with different directions, multiple positions will be returned for each block of contiguous characters with the same direction.
138 * @param[in] startIndex start index of the text requested to get position to.
139 * @param[in] endIndex end index(included) of the text requested to get position to.
140 * @return list of positions of the requested text.
142 Vector<Vector2> GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const;
145 * @brief Get the line bounding rectangle.
146 * if the requested index is out of range or the line is not yet rendered, a rect of {0, 0, 0, 0} is returned.
148 * @param[in] lineIndex line index to which we want to calculate the geometry for.
149 * @return bounding rectangle.
151 Rect<float> GetLineBoundingRectangle(const uint32_t lineIndex) const;
154 * @brief Get the character bounding rectangle.
155 * If the text is not yet rendered or the index > text.Count(); a rect of {0, 0, 0, 0} is returned.
157 * @param[in] charIndex character index to which we want to calculate the geometry for.
158 * @return bounding rectangle.
160 Rect<float> GetCharacterBoundingRectangle(const uint32_t charIndex) const;
163 * @brief Get the character index.
164 * If the text is not yet rendered or the textis empty, -1 is returned.
166 * @param[in] visualX visual x position.
167 * @param[in] visualY visual y position.
168 * @return character index.
170 int GetCharacterIndexAtPosition(float visualX, float visualY) const;
173 * @brief Gets the bounding box of a specific text range.
175 * @param[in] startIndex start index of the text requested to get bounding box to.
176 * @param[in] endIndex end index(included) of the text requested to get bounding box to.
177 * @return bounding box of the requested text.
179 Rect<> GetTextBoundingRectangle(uint32_t startIndex, uint32_t endIndex) const;
182 * @brief Set the @p spannedText into current textLabel
183 * the spanned text contains content (text) and format (spans with ranges)
184 * the text is copied into text-controller and the spans are applied on ranges
186 * @param[in] spannedText the text with spans.
188 void SetSpannedText(const Text::Spanned& spannedText);
191 * @brief Set text fit array to text label.
193 * @param[in] enable Whether the text fit array is enabled or not.
194 * @param[in] fitOptions list of the fit options.
196 void SetTextFitArray(const bool enable, std::vector<Toolkit::DevelTextLabel::FitOption>& fitOptions);
199 * @brief Get the text fit array of text label.
201 * @return list of the fit options.
203 std::vector<Toolkit::DevelTextLabel::FitOption>& GetTextFitArray();
206 * @brief Whether the text fit array is enabled or not.
208 * @return True if the text fit array is enabled.
210 bool IsTextFitArrayEnabled() const;
213 * @brief Gets the locale.
215 std::string GetLocale();
218 * @brief Set removing front inset to text label.
220 * @param[in] remove Whether front inset of text label has to be removed or not.
222 void SetRemoveFrontInset(const bool remove);
225 * @brief Whether front inset of text label is removed or not.
227 * @return True if the front inset of text label is removed.
229 bool IsRemoveFrontInset() const;
232 * @brief Set removing back inset to text label.
234 * @param[in] remove Whether back inset of text label has to be removed or not.
236 void SetRemoveBackInset(const bool remove);
239 * @brief Whether back inset of text label is removed or not.
241 * @return True if the back inset of text label is removed.
243 bool IsRemoveBackInset() const;
246 * @brief Enable control's background
248 * @param[in] enable Whether to enable the background of control.
250 void EnableControlBackground(const bool enable);
253 * @brief A method that requests asynchronous rendering of text with a fixed size.
255 * @param[in] width The width of text to render.
256 * @param[in] height The height of text to render.
258 void RequestAsyncRenderWithFixedSize(float width, float height);
261 * @brief Requests asynchronous text rendering with a fixed width.
262 * The height is determined by the content of the text when rendered with the given width.
263 * The result will be the same as the height returned by GetHeightForWidth.
264 * If the heightConstraint is given, the maximum height will be the heightConstraint.
266 * @param[in] width The width of text to render.
267 * @param[in] heightConstraint The maximum available height of text to render.
269 void RequestAsyncRenderWithFixedWidth(float width, float heightConstraint);
272 * @brief Requests asynchronous rendering with the maximum available width using the given widthConstraint.
274 * If the width of the text content is smaller than the widthConstraint, the width will be determined by the width of the text.
275 * If the width of the text content is larger than the widthConstraint, the width will be determined by the widthConstraint.
276 * The height is determined by the content of the text when rendered with the given width.
277 * In this case, the result will be the same as the height returned by GetHeightForWidth.
278 * If the heightConstraint is given, the maximum height will be the heightConstraint.
280 * @param[in] widthConstraint The maximum available width of text to render.
282 void RequestAsyncRenderWithConstraint(float widthConstraint, float heightConstraint);
285 * @brief Requests asynchronous text natural size computation.
287 void RequestAsyncNaturalSize();
290 * @brief Requests asynchronous computation of the height of the text based on the given width.
291 * @param[in] width The width of text to compute.
293 void RequestAsyncHeightForWidth(float width);
296 private: // From Control
298 * @copydoc Control::OnInitialize()
300 void OnInitialize() override;
303 * @copydoc Toolkit::Internal::Control::CreateAccessibleObject()
305 DevelControl::ControlAccessible* CreateAccessibleObject() override;
308 * @copydoc Control::OnStyleChange()
310 void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
313 * @copydoc Control::OnRelayout()
315 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
318 * @copydoc Control::OnTap()
320 void OnTap(const TapGesture& tap) override;
323 * @copydoc Control::GetNaturalSize()
325 Vector3 GetNaturalSize() override;
328 * @copydoc Control::GetHeightForWidth()
330 float GetHeightForWidth(float width) override;
333 * @copydoc Control::OnPropertySet()
335 void OnPropertySet(Property::Index index, const Property::Value& propertyValue) override;
338 * @copydoc Control::OnSceneConnection()
340 void OnSceneConnection(int depth) override;
343 * @copydoc Control::OnSceneDisconnection()
345 void OnSceneDisconnection() override;
347 // From ControlInterface
350 * @copydoc Text::ControlInterface::RequestTextRelayout()
352 void RequestTextRelayout() override;
354 private: // from TextScroller
356 * @copydoc Text::ScrollerInterface::ScrollingFinished()
358 void ScrollingFinished() override;
360 public: // From AnchorControlInterface
362 * @copydoc Text::AnchorControlInterface::AnchorClicked()
364 void AnchorClicked(const std::string& href) override;
366 private: // from AsyncTextInterface
369 * @copydoc Text::AsyncTextInterface::AsyncSetupAutoScroll()
371 void AsyncSetupAutoScroll(Text::AsyncTextRenderInfo renderInfo) override;
374 * @copydoc Text::AsyncTextInterface::AsyncTextFitChanged()
376 void AsyncTextFitChanged(float pointSize) override;
379 * @copydoc Text::AsyncTextInterface::AsyncLoadComplete()
381 void AsyncLoadComplete(Text::AsyncTextRenderInfo renderInfo);
384 * @copydoc Text::AsyncTextInterface::AsyncSizeComputed()
386 void AsyncSizeComputed(Text::AsyncTextRenderInfo renderInfo);
389 private: // Implementation
391 * Construct a new TextLabel.
393 * @param[in] additionalBehaviour additional behaviour flags for this TextLabel
395 TextLabel(ControlBehaviour additionalBehaviour);
398 * A reference counted object may only be deleted by calling Unreference()
400 virtual ~TextLabel();
403 // Undefined copy constructor and assignment operators
404 TextLabel(const TextLabel&);
405 TextLabel& operator=(const TextLabel& rhs);
408 * @brief Get the AsyncTextParameters
409 * All properties of the text label needed to render the text are stored and returned in the parameter.
411 * @param[in] requestType Type to request asynchronous computation.
412 * @param[in] contentSize The size of the text content requested by relayout excluding padding.
413 * @param[in] padding The size of the label's padding.
414 * @param[in] layoutDirection The layout direction.
415 * @return The parameters for async text render.
417 Text::AsyncTextParameters GetAsyncTextParameters(const Text::Async::RequestType requestType, const Vector2& contentSize, const Extents& padding, const Dali::LayoutDirection::Type layoutDirection);
420 * @brief Set up Autoscrolling
422 void SetUpAutoScrolling();
425 * Creates a text-scroller if one has not been created.
426 * @return The text scroller.
428 Text::TextScrollerPtr GetTextScroller()
432 mTextScroller = Text::TextScroller::New(*this);
434 return mTextScroller;
438 * @brief Callback function for when the layout is changed.
439 * @param[in] actor The actor whose layoutDirection is changed.
440 * @param[in] type The layoutDirection.
442 void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
445 * @brief Callback function for when the locale is changed.
446 * @param[in] locale The new system locale.
448 void OnLocaleChanged(std::string locale);
451 * @brief Emits TextFitChanged signal.
453 void EmitTextFitChangedSignal();
456 * @brief Emits AsyncTextRendered signal.
458 void EmitAsyncTextRenderedSignal(float width, float height);
461 * @brief Emits AsyncNaturalSizeComputed signal.
463 void EmitAsyncNaturalSizeComputedSignal(float width, float height);
466 * @brief Emits AsyncHeightForWidthComputed signal.
468 void EmitAsyncHeightForWidthComputedSignal(float width, float height);
470 void OnAccessibilityStatusChanged();
473 Text::ControllerPtr mController;
474 Text::TextScrollerPtr mTextScroller;
476 Toolkit::Visual::Base mVisual;
478 std::vector<Toolkit::TextAnchor> mAnchorActors;
481 Toolkit::DevelTextLabel::AnchorClickedSignalType mAnchorClickedSignal;
482 Toolkit::DevelTextLabel::TextFitChangedSignalType mTextFitChangedSignal;
483 Toolkit::DevelTextLabel::AsyncTextRenderedSignalType mAsyncTextRenderedSignal;
484 Toolkit::DevelTextLabel::AsyncNaturalSizeComputedSignalType mAsyncNaturalSizeComputedSignal;
485 Toolkit::DevelTextLabel::AsyncHeightForWidthComputedSignalType mAsyncHeightForWidthComputedSignal;
491 int mRenderingBackend;
493 bool mTextUpdateNeeded : 1;
494 bool mLastAutoScrollEnabled : 1;
495 bool mControlBackgroundEnabeld : 1;
497 bool mIsAsyncRenderNeeded : 1; // true if a render request is required in ASYNC_AUTO mode, otherwise false.
498 bool mIsSizeChanged : 1; // whether the size has been changed or not.
499 bool mIsManualRender : 1; // whether an async manual render has been requested, returns false when completed.
500 bool mIsManualRendered : 1; // whether an async manual render has been completed, returns false on the next relayout.
501 bool mManualRendered : 1;
505 * @brief This structure is to connect TextLabel with Accessible functions.
507 class TextLabelAccessible : public TextControlAccessible
510 using TextControlAccessible::TextControlAccessible;
513 * @copydoc Dali::Accessibility::Text::GetNameRaw()
515 std::string GetNameRaw() const override;
518 * @copydoc Dali::Accessibility::Text::GetNamePropertyIndex()
520 Property::Index GetNamePropertyIndex() override;
524 * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetTextAnchors()
526 const std::vector<Toolkit::TextAnchor>& GetTextAnchors() const override;
529 * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetTextController()
531 Toolkit::Text::ControllerPtr GetTextController() const override;
535 } // namespace Internal
537 // Helpers for public-api forwarding methods
539 inline Toolkit::Internal::TextLabel& GetImpl(Toolkit::TextLabel& textLabel)
541 DALI_ASSERT_ALWAYS(textLabel);
543 Dali::RefObject& handle = textLabel.GetImplementation();
545 return static_cast<Toolkit::Internal::TextLabel&>(handle);
548 inline const Toolkit::Internal::TextLabel& GetImpl(const Toolkit::TextLabel& textLabel)
550 DALI_ASSERT_ALWAYS(textLabel);
552 const Dali::RefObject& handle = textLabel.GetImplementation();
554 return static_cast<const Toolkit::Internal::TextLabel&>(handle);
557 } // namespace Toolkit
561 #endif // DALI_TOOLKIT_INTERNAL_TEXT_LABEL_H