1 #ifndef __DALI_TOOLKIT_TEXT_VIEW_H__
2 #define __DALI_TOOLKIT_TEXT_VIEW_H__
5 * Copyright (c) 2014 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 * @addtogroup CAPI_DALI_TOOLKIT_TEXT_VIEW_MODULE
30 #include <dali-toolkit/public-api/controls/alignment/alignment.h>
31 #include <dali-toolkit/public-api/markup-processor/markup-processor.h>
33 namespace Dali DALI_IMPORT_API
39 namespace Internal DALI_INTERNAL
45 * @brief The TextView class is a UI Dali::Toolkit::Control designed to extend the capabilities of the basic Dali::TextActor.
47 * It provides support for multi-line wrapping, multi-language font detection, text alignment, scrolling and styling.
49 * See the \link text-view Text View \endlink page of the Programming Guide for more details and examples.
51 class TextView : public Control
56 static const char* const SIGNAL_TEXT_SCROLLED; ///< Signal emitted when the scroll position changes. @see ScrolledSignal()
59 static const Property::Index PROPERTY_MARKUP_ENABLED; ///< name "markup-enabled", @see SetMarkupProcessingEnabled(), type BOOLEAN
60 static const Property::Index PROPERTY_TEXT; ///< name "text", @see SetText(), type STRING
61 static const Property::Index PROPERTY_MULTILINE_POLICY; ///< name "multiline-policy", @see SetMultilinePolicy(), type STRING
62 static const Property::Index PROPERTY_WIDTH_EXCEED_POLICY; ///< name "width-exceed-policy", @see SetWidthExceedPolicy(), type STRING
63 static const Property::Index PROPERTY_HEIGHT_EXCEED_POLICY; ///< name "height-exceed-policy", @see SetHeightExceedPolicy(), type STRING
64 static const Property::Index PROPERTY_LINE_JUSTIFICATION; ///< name "line-justification", @see SetLineJustification(), type STRING
65 static const Property::Index PROPERTY_FADE_BOUNDARY; ///< name "fade-boundary", @see SetFadeBoundary(), type VECTOR4
66 static const Property::Index PROPERTY_LINE_HEIGHT_OFFSET; ///< name "line-height-offset", @see SetLineHeightOffset(), type FLOAT
67 static const Property::Index PROPERTY_HORIZONTAL_ALIGNMENT; ///< name "horizontal-alignment", @see SetTextAlignment(), type STRING
68 static const Property::Index PROPERTY_VERTICAL_ALIGNMENT; ///< name "vertical-alignment", @see SetTextAlignment(), type STRING
71 * @brief Structure used to retrieve Layout info per character.
73 struct CharacterLayoutInfo
76 * @brief Default constructor.
78 * Initializes all members to their default values.
80 CharacterLayoutInfo();
83 * @brief Empty destructor.
85 * @note Added to increase coverage.
87 ~CharacterLayoutInfo();
90 * @brief Copy constructor.
92 CharacterLayoutInfo( const CharacterLayoutInfo& characterLayoutInfo );
95 * @brief Assignment operator.
97 CharacterLayoutInfo& operator=( const CharacterLayoutInfo& character );
100 * @brief Constructor.
102 * @param[in] size of the character.
103 * @param[in] position of the character.
104 * @param[in] isNewLineChar Whether the character is a new line character.
105 * @param[in] isRightToLeftCharacter Whether is a right to left character.
106 * @param[in] visible Whether is visible.
107 * @param[in] descender of the character (distance from the base line to the bottom of the character.)
109 CharacterLayoutInfo( const Size& size,
110 const Vector3& position,
112 bool isRightToLeftCharacter,
116 Size mSize; ///< Size of the group of characters.
117 Vector3 mPosition; ///< Position of the character within the text view.
118 bool mIsNewLineChar:1; ///< Whether this character represent a new line.
119 bool mIsRightToLeftCharacter:1; ///< Whether it's a right-to-left character.
120 bool mIsVisible:1; ///< Whether this character is visible or not.
121 float mDescender; ///< The character's descender which is the distance from the baseline to the bottom of the character
124 typedef std::vector<CharacterLayoutInfo> CharacterLayoutInfoContainer; ///< Container of layout info per character.
127 * @brief Stores some info about a laid-out line.
129 struct LineLayoutInfo
131 std::size_t mCharacterGlobalIndex; ///< Global index within the whole text of the first character of current laid-out line.
132 Size mSize; ///< Size of the current laid-out line.
133 float mAscender; ///< The max ascender of the current laid-out line.
136 typedef std::vector<LineLayoutInfo> LineLayoutInfoContainer; ///< Container of layout info per line.
139 * @brief How text is laid out.
141 struct TextLayoutInfo
144 * @brief Default constructor.
149 * @brief Empty destructor.
151 * @note Added to increase coverage.
156 * @brief Copy constructor.
158 TextLayoutInfo( const TextLayoutInfo& textLayoutInfo );
161 * @brief Assignment operator.
163 TextLayoutInfo& operator=( const TextLayoutInfo& textLayoutInfo );
165 CharacterLayoutInfoContainer mCharacterLayoutInfoTable; ///< The table of character's positions and sizes sorted by the character's visual index.
166 LineLayoutInfoContainer mLines; ///< For each laid-out line, it stores an index to the first character of the line.
167 std::vector<int> mCharacterLogicalToVisualMap; ///< The map to store the character's logical (input) index according to its visual (reordered) index.
168 std::vector<int> mCharacterVisualToLogicalMap; ///< The map to store the character's visual (reordered) index according to its logical (input) index.
169 Size mTextSize; ///< Text size after relayout.
170 Vector2 mScrollOffset; ///< Scroll's position.
174 * @brief This structure represents a fade boundary.
176 * If the Exceed policy is set to Fade all text which does not fit within the text-view fade boundary is faded out. Text which exceeds the text-view boundary becomes invisible.
177 * The \e left, \e right, \e top and \e bottom values are positive, in pixels and set the distances between the text-view and fade boundaries.
182 * @brief Default constructor.
184 * Initializes all values to 0. It means no fade boundary.
189 * @brief Constructor.
191 * Initializes the fade boundary with the given values.
193 * @param[in] left value in pixels.
194 * @param[in] right value in pixels.
195 * @param[in] top value in pixels.
196 * @param[in] bottom value in pixels.
198 FadeBoundary( PixelSize left, PixelSize right, PixelSize top, PixelSize bottom );
200 PixelSize mLeft; ///< The left fade boundary
201 PixelSize mRight; ///< The right fade boundary
202 PixelSize mTop; ///< The top fade boundary
203 PixelSize mBottom; ///< The bottom fade bounday
207 * @brief Define how to wrap the text in lines.
209 * \e SplitByNewLineChar will wrap the text in lines when a '\\n' character or a \<br /\> is found.
210 * \e SplitByWord has effect only when TextView size is assigned.
211 * It will wrap the text in lines when a '\\n' character or a \<br /\> is found or if a line exceeds the TextView's boundary. This option won't split a word in two.
212 * \e SplitByChar has effect only when TextView size is assigned.
213 * It will wrap the text in lines when a '\\n' character or a \<br /\> is found or if a line exceeds the TextView's boundary. This option might split a word in two.
214 * The default value is \e SplitByNewLineChar.
218 SplitByNewLineChar, ///< Text lines will wrap when '\\n' character is found.
219 SplitByWord, ///< Text lines will wrap by word or if '\\n' character is found. It has effect only when TextView size is assigned.
220 SplitByChar ///< Text lines will wrap by char or if '\\n' character is found. It has effect only when TextView size is assigned.
224 * @brief Define how to display the text when it doesn't fit inside the TextView after the line wrap.
226 * The default value is \e Original.
230 Original, ///< Will display the text in its original size. If a line, a word or a character is bigger than the TextView size it may exceed its boundary.
231 Truncate, ///< @deprecated. Use Fade instead.
232 Fade, ///< Will display the text in its original size. It won't display the text which exceeds the TextView boundary. It fades the text out.
233 Split, ///< Will split the text in a new line.
234 ShrinkToFit, ///< Will shrink the text to fit the TextView boundary.
235 EllipsizeEnd ///< Will ellipsize the text by the end.
239 * @brief Define how to justify lines inside the text area.
241 * The default value is \e Left.
243 enum LineJustification
245 Left, ///< Justify to the left.
246 Center, ///< Centered.
247 Right, ///< Justify to the right.
248 Justified ///< Line justified.
253 * @brief Create a TextView handle; this can be initialised with TextView::New().
255 * Calling member functions with an uninitialised Dali::Object handle is not allowed.
260 * @brief Copy constructor.
262 * Creates another handle that points to the same real object
263 * @param[in] handle The handle to copy from
265 TextView( const TextView& handle );
268 * @brief Assignment operator.
270 * Changes this handle to point to another real object
271 * @param[in] handle The handle to copy from
272 * @return a reference to this
274 TextView& operator=( const TextView& handle );
277 * @brief Create a TextView control with no text.
279 * @return A handle the TextView control.
281 static TextView New();
284 * @brief Create a TextView control.
286 * @param[in] text to display.
287 * @return A handle the TextView control.
289 static TextView New( const std::string& text );
292 * @brief Create a TextView control with styled text.
294 * @param[in] text The text with style to display.
295 * @return A handle the TextView control.
297 static TextView New( const MarkupProcessor::StyledTextArray& text );
300 * @brief Downcast an Object handle to TextView.
302 * If handle points to a TextView the
303 * downcast produces valid handle. If not the returned handle is left uninitialized.
304 * @param[in] handle Handle to an object
305 * @return handle to a TextView or an uninitialized handle
307 static TextView DownCast( BaseHandle handle );
312 * This is non-virtual since derived Handle types must not contain data or virtual methods.
317 * @brief Replace the current text with a new text string.
319 * @param[in] text to display. The string may contain style tags.
321 void SetText( const std::string& text );
324 * @brief Replace the current text with a new text string with style.
326 * @param[in] text The text with style to display.
328 void SetText( const MarkupProcessor::StyledTextArray& text );
331 * @brief Inserts the given text in the specified position.
333 * @param[in] position Where the given text is going to be added.
334 * @param[in] text The text to be added.
336 void InsertTextAt( std::size_t position, const std::string& text );
339 * @brief Inserts the given text with style in the specified position.
341 * @param[in] position Where the given text is going to be added.
342 * @param[in] text The text with style to be added.
344 void InsertTextAt( std::size_t position, const MarkupProcessor::StyledTextArray& text );
347 * @brief Replaces part of the text.
349 * It removes the specified number of characters from the given position and inserts the given text in the same specified position.
351 * @param[in] position of the first character to be removed and Where the given text is going to be added.
352 * @param[in] numberOfCharacters number of characters to be removed.
353 * @param[in] text The text to be added.
355 void ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const std::string& text );
358 * @brief Replaces part of the text.
360 * It removes the specified number of characters from the given position and inserts the given text with style in the same specified position.
362 * @param[in] position of the first character to be removed and Where the given text is going to be added.
363 * @param[in] numberOfCharacters number of characters to be removed.
364 * @param[in] text The text with style to be added.
366 void ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const MarkupProcessor::StyledTextArray& text );
369 * @brief Removes the specified number of characters from the given position.
371 * @param[in] position of the first character to be removed.
372 * @param[in] numberOfCharacters number of characters to be removed.
374 void RemoveTextFrom( std::size_t position, std::size_t numberOfCharacters );
377 * @brief Get the currently displayed text.
379 * @return The currently displayed text.
381 std::string GetText() const;
384 * @brief Sets a line height offset.
386 * The line height offset will be added to the font line height.
387 * @param [in] offset The height offset in PointSize units.
389 void SetLineHeightOffset( PointSize offset );
392 * @brief Retrieves the line height offset.
394 * @return The line height offset in PointSize units.
396 PointSize GetLineHeightOffset() const;
399 * @brief Sets the given style to the current text.
401 * By default all style settings are applied but a bit mask could be used to modify only certain style settings.
402 * @note TextView doesn't store a copy of the given style, it applies the given style to the current text only.
403 * Subsequent calls to SetText() will override any style set by this method.
404 * @param[in] style The given style
405 * @param[in] mask The bit mask.
407 void SetStyleToCurrentText( const TextStyle& style, TextStyle::Mask mask = TextStyle::ALL );
410 * @brief Set the current text alignment.
412 * Default alignment is (HorizontalCenter | VerticalCenter)
413 * @param[in] align The new alignment option.
415 void SetTextAlignment( Alignment::Type align );
418 * @brief Get the current text alignment combined into a single value.
420 * The values can be tested by using the & operator
421 * and the desired flag. e.g. if (GetTextAlignment() & HorizontalCentre) ...
422 * @return the combined alignment
424 Alignment::Type GetTextAlignment() const;
427 * @brief Sets how to wrap the text in lines policy.
429 * @param policy The multi-line policy. \e SplitByNewLineChar is set by default.
431 void SetMultilinePolicy( MultilinePolicy policy );
434 * @brief Gets the wrap in lines policy.
436 * @return The multi-line policy.
438 MultilinePolicy GetMultilinePolicy() const;
441 * @brief Sets how to display the text inside the TextView when it exceeds the text-view's width.
443 * @param policy The exceed policy. Original is set by default.
445 void SetWidthExceedPolicy( ExceedPolicy policy );
448 * @brief Gets the width exceed policy.
450 * @return The exceed policy.
452 ExceedPolicy GetWidthExceedPolicy() const;
455 * @brief Sets how to display the text inside the TextView when it exceeds the text-view's height.
457 * @param policy The exceed policy. Original is set by default.
459 void SetHeightExceedPolicy( ExceedPolicy policy );
462 * @brief Gets the height exceed policy.
464 * @return The exceed policy.
466 ExceedPolicy GetHeightExceedPolicy() const;
469 * @brief Sets how to justify lines inside the text area.
471 * @param justification The line justification. Left is set by default.
473 void SetLineJustification( LineJustification justification );
476 * @brief Gets the line justification.
478 * @return The line justification.
480 LineJustification GetLineJustification() const;
483 * @brief Sets a fade boundary.
487 * @param[in] fadeBoundary The given fade boundary.
489 void SetFadeBoundary( const FadeBoundary& fadeBoundary );
492 * @brief Retrieves the fade boundary.
496 * @return The fade boundary.
498 const FadeBoundary& GetFadeBoundary() const;
501 * @brief Sets the ellipsize text.
503 * @param[in] ellipsizeText The new text. The string may contain style tags. By default the ellipsize text is '...'
505 void SetEllipsizeText( const std::string& ellipsizeText );
508 * @brief Sets the ellipsize text with style.
510 * @param[in] ellipsizeText The new text with its style.
512 void SetEllipsizeText( const MarkupProcessor::StyledTextArray& ellipsizeText );
515 * @brief Retrieves the ellipsize text.
517 * @return The ellipsize text.
519 std::string GetEllipsizeText() const;
522 * @brief A mechanism to retrieve layout information from the TextView.
524 * It produces a vector of CharcterLayoutInfo structures which describe the size and position of each character,
525 * two vectors which maps the logical and visual positions of the characters in a bidirectional text, the size
526 * of the whole laid-out text and the scroll offset value.
528 * @see TextLayoutInfo.
530 * @param[out] textLayoutInfo A structure with text layout information.
532 void GetTextLayoutInfo( TextLayoutInfo& textLayoutInfo );
535 * @brief Allows modification of text-actor's position in the depth sort algorithm.
537 * @see Dali::RenderableActor::SetSortModifier()
538 * @param [in] depthOffset the offset to be given to the internal text-actors. Positive values pushing it further back.
540 void SetSortModifier( float depthOffset );
543 * @brief Sets whether text-view renders text using a previously generated snapshot.
545 * Rendering long text using a snapshot may increase performance. The default value is \e false (render without using a snapshot).
547 * @param[in] enable Whether text-view is using a snapshot to render text.
549 void SetSnapshotModeEnabled( bool enable );
552 * @brief Retrieves whether text-view is using a snapshot to render text.
554 * @return \e true if text-view is using a snapshot to render text, otherwhise it returns \e false.
556 bool IsSnapshotModeEnabled() const;
559 * @brief Sets whether markup processing should be carried out.
561 * To use markup, applications need to SetMarkupProcessingEnabled first, then SetText().
564 * @param[in] enable whether markup processing is carried out or not.
566 void SetMarkupProcessingEnabled( bool enable );
569 * @brief Retrieves whether text-view is processing markup text
571 * @return \e true if text-view markup processing is enabled, otherwhise it returns \e false.
573 bool IsMarkupProcessingEnabled() const;
576 * @brief Enables or disables the text scroll.
578 * When scroll is enabled, snapshot mode will be enabled automatically. Equally, if scroll is disabled
579 * the snapshot mode is restored to the previous value.
581 * @param[in] enable Whether to enable the text scroll.
583 void SetScrollEnabled( bool enable );
586 * @brief Retrieves whether the text scroll is enabled.
588 * @return \e true if the scroll is enabled.
590 bool IsScrollEnabled() const;
593 * @brief Sets a new scroll position.
595 * The new scroll position set could be trimmed if the text doesn't cover the whole text-view.
596 * i.e. If a text-view is 100x100 and a text is 200x100 a scroll position beyond 50x0 will be trimmed to 50x0.
598 * Call IsScrollPositionTrimmed() to know if the last scroll position set has been trimmed.
600 * A signal is emitted. @see ScrolledSignal().
602 * @param[in] position The new scroll position.
604 void SetScrollPosition( const Vector2& position );
607 * @brief Recrieves current scroll position.
609 * @return The scroll position.
611 const Vector2& GetScrollPosition() const;
614 * @brief Whether the last scroll position set was trimmed.
616 * @return \e true if the last scroll position set was trimmed, otherwise \e false.
618 bool IsScrollPositionTrimmed() const;
621 /// @brief Signal types
622 typedef SignalV2< void ( TextView textView, Vector2 scrollDelta ) > ScrolledSignalV2;
625 * @brief Signal emitted when the text is scrolled inside the text-view.
627 * A callback with the following prototype can be connected to this signal.
629 * Callback(TextView textView, Vector2 scrollDelta)
631 * \e textView is the handle of the text-view emitting the signal.
632 * \e scrollDelta is the differente of the current scroll position with the previous one.
633 * @return The signal to connect to
635 ScrolledSignalV2& ScrolledSignal();
637 public: // Not intended for application developers
640 * @brief Creates a handle using the Toolkit::Internal implementation.
641 * @note Not intended for application developers
643 * @param[in] implementation The Control implementation.
645 TextView( Internal::TextView& implementation );
648 * @brief Allows the creation of this Control from an Internal::CustomActor pointer.
649 * @note Not intended for application developers
651 * @param[in] internal A pointer to the internal CustomActor.
653 TextView( Dali::Internal::CustomActor* internal );
656 } // namespace Toolkit
663 #endif // __DALI_TOOLKIT_ITEM_VIEW_H__