Add support for text geometry
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-label-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_TEXT_LABEL_H
2 #define DALI_TOOLKIT_INTERNAL_TEXT_LABEL_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/object/property-map.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
26 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
27 #include <dali-toolkit/internal/text/text-anchor-control-interface.h>
28 #include <dali-toolkit/internal/text/text-control-interface.h>
29 #include <dali-toolkit/internal/text/text-controller.h>
30 #include <dali-toolkit/internal/text/text-scroller-interface.h>
31 #include <dali-toolkit/internal/text/text-scroller.h>
32 #include <dali-toolkit/internal/visuals/text/text-visual.h>
33 #include <dali-toolkit/public-api/controls/control-impl.h>
34 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
35
36 namespace Dali
37 {
38 namespace Toolkit
39 {
40 namespace Internal
41 {
42 /**
43  * @brief A control which renders a short text string.
44  */
45 class TextLabel : public Control, public Text::ControlInterface, public Text::ScrollerInterface, public Text::AnchorControlInterface
46 {
47 public:
48   /**
49    * @copydoc Dali::Toollkit::TextLabel::New()
50    */
51   static Toolkit::TextLabel New();
52
53   // Properties
54
55   /**
56    * @brief Called when a property of an object of this type is set.
57    *
58    * @param[in] object The object whose property is set.
59    * @param[in] index The property index.
60    * @param[in] value The new property value.
61    */
62   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
63
64   /**
65    * @brief Called to retrieve a property of an object of this type.
66    *
67    * @param[in] object The object whose property is to be retrieved.
68    * @param[in] index The property index.
69    * @return The current value of the property.
70    */
71   static Property::Value GetProperty(BaseObject* object, Property::Index index);
72
73   /**
74    * @copydoc Dali::Toollkit::TextLabel::AnchorClickedSignal()
75    */
76   DevelTextLabel::AnchorClickedSignalType& AnchorClickedSignal();
77
78   /**
79    * @copydoc Dali::Toollkit::TextLabel::TextFitChangedSignal()
80    */
81   DevelTextLabel::TextFitChangedSignalType& TextFitChangedSignal();
82
83   /**
84    * Connects a callback function with the object's signals.
85    * @param[in] object The object providing the signal.
86    * @param[in] tracker Used to disconnect the signal.
87    * @param[in] signalName The signal to connect to.
88    * @param[in] functor A newly allocated FunctorDelegate.
89    * @return True if the signal was connected.
90    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
91    */
92   static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
93
94   /**
95    * @brief Gets text controller
96    *
97    * @return The text controller
98    */
99   Text::ControllerPtr GetTextController();
100
101   /**
102    * @brief Get the rendered size of a specific text range.
103    * if the requested text is at multilines, multiple sizes will be returned for each text located in a separate line.
104    * if a line contains characters with different directions, multiple sizes will be returned for each block of contiguous characters with the same direction.
105    *
106    * @param[in] startIndex start index of the text requested to calculate size for.
107    * @param[in] endIndex end index(included) of the text requested to calculate size for.
108    * @return list of sizes of the reuested text.
109    */
110   Vector<Vector2> GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const;
111
112   /**
113    * @brief Get the top/left rendered position of a specific text range.
114    * if the requested text is at multilines, multiple positions will be returned for each text located in a separate line.
115    * if a line contains characters with different directions, multiple positions will be returned for each block of contiguous characters with the same direction.
116    *
117    * @param[in] startIndex start index of the text requested to get position to.
118    * @param[in] endIndex end index(included) of the text requested to get position to.
119    * @return list of positions of the requested text.
120    */
121   Vector<Vector2> GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const;
122
123 private: // From Control
124   /**
125    * @copydoc Control::OnInitialize()
126    */
127   void OnInitialize() override;
128
129   /**
130    * @copydoc Control::OnStyleChange()
131    */
132   void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
133
134   /**
135    * @copydoc Control::OnRelayout()
136    */
137   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
138
139   /**
140    * @copydoc Control::OnTap()
141    */
142   void OnTap(const TapGesture& tap) override;
143
144   /**
145    * @copydoc Control::GetNaturalSize()
146    */
147   Vector3 GetNaturalSize() override;
148
149   /**
150    * @copydoc Control::GetHeightForWidth()
151    */
152   float GetHeightForWidth(float width) override;
153
154   /**
155    * @copydoc Control::OnPropertySet()
156    */
157   void OnPropertySet(Property::Index index, const Property::Value& propertyValue) override;
158
159   // From ControlInterface
160
161   /**
162    * @copydoc Text::ControlInterface::RequestTextRelayout()
163    */
164   void RequestTextRelayout() override;
165
166 private: // from TextScroller
167   /**
168    * @copydoc Text::ScrollerInterface::ScrollingFinished()
169    */
170   void ScrollingFinished() override;
171
172 public: // From AnchorControlInterface
173   /**
174    * @copydoc Text::AnchorControlInterface::AnchorClicked()
175    */
176   void AnchorClicked(const std::string& href) override;
177
178 private: // Implementation
179   /**
180    * Construct a new TextLabel.
181    */
182   TextLabel();
183
184   /**
185    * A reference counted object may only be deleted by calling Unreference()
186    */
187   virtual ~TextLabel();
188
189 private:
190   // Undefined copy constructor and assignment operators
191   TextLabel(const TextLabel&);
192   TextLabel& operator=(const TextLabel& rhs);
193
194   /**
195    * @brief Set up Autoscrolling
196    */
197   void SetUpAutoScrolling();
198
199   /**
200    * Creates a text-scroller if one has not been created.
201    * @return The text scroller.
202    */
203   Text::TextScrollerPtr GetTextScroller()
204   {
205     if(!mTextScroller)
206     {
207       mTextScroller = Text::TextScroller::New(*this);
208     }
209     return mTextScroller;
210   }
211
212   /**
213    * @brief Callback function for when the layout is changed.
214    * @param[in] actor The actor whose layoutDirection is changed.
215    * @param[in] type  The layoutDirection.
216    */
217   void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
218
219   /**
220    * @brief Emits TextFitChanged signal.
221    */
222   void EmitTextFitChangedSignal();
223   void OnAccessibilityStatusChanged();
224
225 private: // Data
226   Text::ControllerPtr   mController;
227   Text::TextScrollerPtr mTextScroller;
228
229   Toolkit::Visual::Base mVisual;
230
231   std::vector<Toolkit::TextAnchor> mAnchorActors;
232
233   // Signals
234   Toolkit::DevelTextLabel::AnchorClickedSignalType  mAnchorClickedSignal;
235   Toolkit::DevelTextLabel::TextFitChangedSignalType mTextFitChangedSignal;
236
237   int  mRenderingBackend;
238   bool mTextUpdateNeeded : 1;
239
240 protected:
241   /**
242    * @brief This structure is to connect TextLabel with Accessible functions.
243    */
244   struct AccessibleImpl : public DevelControl::AccessibleImpl,
245                           public virtual Dali::Accessibility::Text,
246                           public virtual Dali::Accessibility::Hypertext
247   {
248     using DevelControl::AccessibleImpl::AccessibleImpl;
249
250     /**
251      * @copydoc Dali::Accessibility::Text::GetText()
252      */
253     std::string GetText(size_t startOffset, size_t endOffset) override;
254
255     /**
256      * @copydoc Dali::Accessibility::Text::GetCharacterCount()
257      */
258     size_t GetCharacterCount() override;
259
260     /**
261      * @copydoc Dali::Accessibility::Text::GetCursorOffset()
262      */
263     size_t GetCursorOffset() override;
264
265     /**
266      * @copydoc Dali::Accessibility::Text::SetCursorOffset()
267      */
268     bool SetCursorOffset(size_t offset) override;
269
270     /**
271      * @copydoc Dali::Accessibility::Text::GetTextAtOffset()
272      */
273     Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
274
275     /**
276      * @copydoc Dali::Accessibility::Text::GetRangeOfSelection()
277      */
278     Accessibility::Range GetRangeOfSelection(size_t selectionIndex) override;
279
280     /**
281      * @copydoc Dali::Accessibility::Text::RemoveSelection()
282      */
283     bool RemoveSelection(size_t selectionIndex) override;
284
285     /**
286      * @copydoc Dali::Accessibility::Text::SetRangeOfSelection()
287      */
288     bool SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset) override;
289
290     /**
291      * @copydoc Dali::Accessibility::Text::GetNameRaw()
292      */
293     std::string GetNameRaw() override;
294
295     /**
296      * @copydoc Dali::Accessibility::Text::GetNamePropertyIndex()
297      */
298     Property::Index GetNamePropertyIndex() override;
299
300     /**
301      * @copydoc Dali::Accessibility::Hypertext::GetLink()
302      */
303     Accessibility::Hyperlink* GetLink(int32_t linkIndex) const override;
304
305     /**
306      * @copydoc Dali::Accessibility::Hypertext::GetLinkIndex()
307      */
308     int32_t GetLinkIndex(int32_t characterOffset) const override;
309
310     /**
311      * @copydoc Dali::Accessibility::Hypertext::GetLinkCount()
312      */
313     int32_t GetLinkCount() const override;
314   };
315 };
316
317 } // namespace Internal
318
319 // Helpers for public-api forwarding methods
320
321 inline Toolkit::Internal::TextLabel& GetImpl(Toolkit::TextLabel& textLabel)
322 {
323   DALI_ASSERT_ALWAYS(textLabel);
324
325   Dali::RefObject& handle = textLabel.GetImplementation();
326
327   return static_cast<Toolkit::Internal::TextLabel&>(handle);
328 }
329
330 inline const Toolkit::Internal::TextLabel& GetImpl(const Toolkit::TextLabel& textLabel)
331 {
332   DALI_ASSERT_ALWAYS(textLabel);
333
334   const Dali::RefObject& handle = textLabel.GetImplementation();
335
336   return static_cast<const Toolkit::Internal::TextLabel&>(handle);
337 }
338
339 } // namespace Toolkit
340
341 } // namespace Dali
342
343 #endif // DALI_TOOLKIT_INTERNAL_TEXT_LABEL_H