1 #ifndef __DALI_TOOLKIT_INTERNAL_TEXT_VIEW_H__
2 #define __DALI_TOOLKIT_INTERNAL_TEXT_VIEW_H__
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
21 #include <dali/public-api/common/dali-vector.h>
23 #include <dali/public-api/actors/text-actor.h>
24 #include <dali-toolkit/public-api/controls/control-impl.h>
25 #include <dali-toolkit/public-api/controls/text-view/text-view.h>
26 #include "text-actor-cache.h"
27 #include "text-view-processor-types.h"
39 * TextView is a custom control for text aligning and multiline support
41 class TextView : public ControlImpl
48 TEXTVIEW_PROPERTY_START_INDEX = ControlImpl::CONTROL_PROPERTY_END_INDEX + 1,
49 TEXTVIEW_PROPERTY_END_INDEX = TEXTVIEW_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices
53 * Internal exceed policy with the valid combinations.
57 Original, ///< Original size (even if it exceeds the witdh or the height).
58 OriginalFade, ///< Original size if it exceeds the width but faded if it exceeds the height.
59 OriginalShrink, ///< Shrunk if it exceeds the height.
60 SplitOriginal, ///< Split if it exceeds the width but no action if it exceeds the height.
61 SplitFade, ///< Split if it exceeds the width and faded if it exceeds the height.
62 SplitShrink, ///< Split if it exceeds the width and shrunk if it exceeds the height.
63 Fade, ///< Faded if it exceeds any boundary.
64 FadeOriginal, ///< Faded if it exceeds the width but no action if it exceeds the height.
65 ShrinkOriginal, ///< Shrunk if it exceeds the width but no action if it exceeds the height.
66 ShrinkFade, ///< Shrunk if it exceeds the width and faded if it exceeds the height.
67 Shrink, ///< Shrunk if it exceeds any boundary.
68 EllipsizeEndOriginal, ///< Ellipsized by the end if it exceeds the width but no action if it exceeds the height.
69 EllipsizeEnd ///< Ellipsized by the end if it exceeds the width and/or the height.
72 // Between two OnRelaidOut methods, several calls to InsertTextAt, RemoveTextFrom or SetText can happen.
73 // TextViewProcessorMetadata stores the type of operation. A vector stores all operations between two OnRelaidOut calls.
75 enum TextViewProcessorMetadataType
77 TextSet, ///< Sets new text.
78 TextInserted, ///< Inserts text into current text.
79 TextReplaced, ///< Replaces some text from current text.
80 TextRemoved, ///< Removes some text from current text.
81 NewLineHeight, ///< Sets a new line height offset.
82 NewStyle ///< Sets a new style to the whole text.
86 * Stores info about which data structures need to be modified when the OnRelaidOut() method is called
88 struct TextViewProcessorMetadata
90 TextViewProcessorMetadata();
92 TextViewProcessorMetadataType mType; ///< Stores the type of operation.
93 std::size_t mPosition; ///< Character position within the text.
94 std::size_t mNumberOfCharacters; ///< Number of characters to be removed/ replaced.
95 MarkupProcessor::StyledTextArray mText; ///< The new text.
96 TextStyle::Mask mStyleMask; ///< The style mask.
100 * Defines which operations have to be done in the relayout process.
102 enum RelayoutOperationMask
104 NO_RELAYOUT = 0x0, ///< Does nothing.
105 RELAYOUT_REMOVE_TEXT_ACTORS = 0x1, ///< Removes current text-actors from the text-view.
106 RELAYOUT_SIZE_POSITION = 0x2, ///< Calculates size and position of the text but it doesn't calculate alignment.
107 RELAYOUT_ALIGNMENT = 0x4, ///< Aligns the whole text.
108 RELAYOUT_VISIBILITY = 0x8, ///< Calculates the visibility.
109 RELAYOUT_INITIALIZE_TEXT_ACTORS = 0x10, ///< Initialize text-actors (create handles).
110 RELAYOUT_TEXT_ACTOR_UPDATE = 0x20, ///< Updates text-actors (set size, position, style, ...)
111 RELAYOUT_INSERT_TO_TEXT_VIEW = 0x40, ///< Adds the text-actors to the text-view.
112 RELAYOUT_INSERT_TO_TEXT_ACTOR_LIST = 0x80, ///< Inserts the text-actors to the text-actor list.
113 RELAYOUT_ALL = 0xFF ///< Does all operations.
116 //////////////////////////////////////////////
119 * Create a new TextView.
120 * @return A smart-pointer to the newly allocated TextView.
122 static Toolkit::TextView New();
125 * @copydoc SetText( const std::string& text )
127 void SetText( const std::string& text );
130 * @copydoc SetText( const StyledTextArray& text )
132 void SetText( const MarkupProcessor::StyledTextArray& text );
135 * @copydoc InsertTextAt( std::size_t position, const std::string& text )
137 void InsertTextAt( std::size_t position, const std::string& text );
140 * @copydoc InsertTextAt( std::size_t position, const std::string& text )
142 void InsertTextAt( std::size_t position, const MarkupProcessor::StyledTextArray& text );
145 * @copydoc RemoveTextFrom( std::size_t position, std::size_t numberOfCharacters )
147 void RemoveTextFrom( std::size_t position, std::size_t numberOfCharacters );
150 * @copydoc ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const std::string& text )
152 void ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const std::string& text );
155 * @copydoc ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const std::string& text )
157 void ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const MarkupProcessor::StyledTextArray& text );
162 std::string GetText() const;
165 * @copydoc SetFont( const Font newFont )
167 void SetFont( const Font newFont );
170 * @copydoc SetLineHeightOffset()
172 void SetLineHeightOffset( PointSize offset );
175 * @copydoc GetLineHeightOffset()
177 PointSize GetLineHeightOffset() const;
180 * @copydoc SetStyleToCurrentText()
182 void SetStyleToCurrentText( const TextStyle& style, TextStyle::Mask mask );
185 * @copydoc SetTextAlignment( Toolkit::Alignment::Type align )
187 void SetTextAlignment( Toolkit::Alignment::Type align );
190 * @copydoc GetTextAlignment()
192 Toolkit::Alignment::Type GetTextAlignment() const;
195 * @copydoc SetMultilinePolicy()
197 void SetMultilinePolicy( Toolkit::TextView::MultilinePolicy policy );
200 * @copydoc GetMultilinePolicy()
202 Toolkit::TextView::MultilinePolicy GetMultilinePolicy() const;
205 * @copydoc SetWidthExceedPolicy()
207 void SetWidthExceedPolicy( Toolkit::TextView::ExceedPolicy policy );
210 * @copydoc GetWidthExceedPolicy()
212 Toolkit::TextView::ExceedPolicy GetWidthExceedPolicy() const;
215 * @copydoc SetHeightExceedPolicy()
217 void SetHeightExceedPolicy( Toolkit::TextView::ExceedPolicy policy );
220 * @copydoc GetHeightExceedPolicy()
222 Toolkit::TextView::ExceedPolicy GetHeightExceedPolicy() const;
225 * @copydoc SetLineJustification()
227 void SetLineJustification( Toolkit::TextView::LineJustification justification );
230 * @copydoc GetLineJustification()
232 Toolkit::TextView::LineJustification GetLineJustification() const;
235 * @copydoc SetFadeBoundary()
237 void SetFadeBoundary( const Toolkit::TextView::FadeBoundary& fadeBoundary );
240 * @copydoc GetFadeBoundary()
242 const Toolkit::TextView::FadeBoundary& GetFadeBoundary() const;
245 * @copydoc SetEllipsizeText()
247 void SetEllipsizeText( const std::string& ellipsizeText );
250 * @copydoc SetEllipsizeText()
252 void SetEllipsizeText( const MarkupProcessor::StyledTextArray& ellipsizeText );
255 * @copydoc GetEllipsizeText()
257 std::string GetEllipsizeText() const;
260 * Checks if relayout the text is needed. If it is, it relais out the text
261 * by calling DoRelayOut().
263 void GetTextLayoutInfo();
266 * Calls GetTextLayoutInfo() and fills the given data structure.
268 * @see GetTextLayoutInfo()
270 void GetTextLayoutInfo( Toolkit::TextView::TextLayoutInfo& textLayoutInfo );
273 * @copydoc SetSortModifier()
275 void SetSortModifier( float depthOffset );
278 * @copydoc SetSnapshotModeEnabled()
280 void SetSnapshotModeEnabled( bool enable );
283 * @copydoc IsSnapshotModeEnabled()
285 bool IsSnapshotModeEnabled() const;
288 * @brief Sets whether markup processing should be carried out.
290 * @param[in] enable whether markup processing is carried out or not.
292 void SetMarkupProcessingEnabled( bool enable );
295 * @brief Returns whether markup processing is enabled or not
297 * @return true is markup processing is enabled
299 bool IsMarkupProcessingEnabled() const;
302 * @copydoc SetScrollEnabled()
304 void SetScrollEnabled( bool enable );
307 * @copydoc IsScrollEnabled()
309 bool IsScrollEnabled() const;
312 * @copydoc SetScrollPosition()
313 * @see DoSetScrollPosition()
315 void SetScrollPosition( const Vector2& position );
318 * @copydoc GetScrollPosition()
320 const Vector2& GetScrollPosition() const;
323 * @copydoc IsScrollPositionTrimmed()
325 bool IsScrollPositionTrimmed() const;
328 * @copydoc ScrolledSignal()
330 Toolkit::TextView::ScrolledSignalV2& ScrolledSignal();
333 * Connects a callback function with the object's signals.
334 * @param[in] object The object providing the signal.
335 * @param[in] tracker Used to disconnect the signal.
336 * @param[in] signalName The signal to connect to.
337 * @param[in] functor A newly allocated FunctorDelegate.
338 * @return True if the signal was connected.
339 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
341 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
346 * Called when a property of an object of this type is set.
347 * @param[in] object The object whose property is set.
348 * @param[in] index The property index.
349 * @param[in] value The new property value.
351 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
354 * Called to retrieve a property of an object of this type.
355 * @param[in] object The object whose property is to be retrieved.
356 * @param[in] index The property index.
357 * @return The current value of the property.
359 static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
362 private: // From ControlImpl
365 * @copydoc Toolkit::Control::OnInitialize()
367 virtual void OnInitialize();
370 * @copydoc Toolkit::Control::OnStyleChange()
372 virtual void OnStyleChange( StyleChange change );
375 * @copydoc Toolkit::Control::OnControlSizeSet()
377 virtual void OnControlSizeSet( const Vector3& size );
380 * @copydoc Toolkit::Control::OnRelaidOut()
382 * Removes text-actor and calls DoRelayOut()..
384 virtual void OnRelaidOut( Vector2 size, ActorSizeContainer& container );
387 * Retrieves the text-view's natural size.
389 * @return The natural size.
391 virtual Vector3 GetNaturalSize();
394 * Retrieves the text-view's \e height for a given \e width.
396 * @param[in] width The given \e width.
398 * @return The \e height for the given \e width.
400 virtual float GetHeightForWidth( float width );
403 * Retrieves the text-view's \e width for a given \e height.
405 * @param[in] height The given \e height.
407 * @return The \e width for the given \e height.
409 virtual float GetWidthForHeight( float height );
412 * @copydoc Dali::CustomActorImpl::OnPropertySet()
414 virtual void OnPropertySet( Property::Index index, Property::Value propertyValue );
419 * Construct a new TextView.
424 * A reference counted object may only be deleted by calling Unreference()
431 TextView( const TextView& );
434 TextView& operator=( const TextView& rhs );
437 * Executes synchronously relayout operations such as set, insert, remove or replace text, or
438 * split groups of characters, etc.
440 void PerformTextViewProcessorOperations();
443 * Optimizes some text-view processor operations.
445 void OptimizeTextViewProcessorOperations();
448 * Synchronously relays-out all text-actors.
450 * Perform text-view-processor operations, sets the new size and position of text-actors and adds them to the text-view.
452 * @param[in] textViewSize The new text-view's size.
453 * @param[in] relayoutOperationMask Defines which operations need to be done in the relayout process.
455 void DoRelayOut( const Size& textViewSize, RelayoutOperationMask relayoutOperationMask );
458 * Process Snapshot. It refresh the render-task in order to generate a new snapshot image.
460 * ProcessSnapshot is called from OnRelaidOut() only if text has been relaid out.
461 * It creates a new image buffer only if the size of the text has changed.
463 * @param[in] textViewSize The new text-view's size.
465 void ProcessSnapshot( const Size& textViewSize );
468 * Aligns the offscreen rendering camera actor to cover the whole text and the resulting image actor accordingly with the text view's alignment.
470 * @param[in] textViewSize The text view's size.
471 * @param[in] offscreenSize The offscreen's frame buffer's size.
473 void AlignOffscreenCameraActor( const Size& textViewSize, const Size& offscreenSize );
476 * Callback called when the render task has processed.
478 * It makes the root actor invisible.
480 * @param[in] renderTask The processed render task.
482 void RenderTaskFinished( Dali::RenderTask& renderTask );
485 * Destroys offscreen rendering resources.
487 * It disconects the render task finished signal from the TextView::RenderTaskFinished() method,
488 * removes the render task from the render task list and resets the offscreen camera actor, root actor,
489 * image actor and the frame buffer.
491 void DestroyOffscreenRenderingResources();
494 * Called when text-view is scrolled.
496 * Sets the new scroll position. @see DoSetScrollPosition()
498 * @param[in] actor Handle of the text-view.
499 * @param[in] gesture Data structure with the parameters of the gesture.
501 void OnTextPan( Actor actor, PanGesture gesture );
504 * Ensures the text-view's boundaries are fully covered of text.
506 * i.e. if the text-view's size is 100x100 and the text's size is 150x100, the scroll position
507 * can be in the range -50,0 and 50,0.
509 void TrimScrollPosition();
512 * Called from SetScrollPosition() and OnTextPan()
514 * Updates the stored scroll position ensuring the text-view is always covered with text by calling
515 * TrimScrollPosition(), calculates the difference with the previous one and emits the Toolkit::TextView::SignalScrolled() signal.
517 * @param[in] position The new scroll position.
519 void DoSetScrollPosition( const Vector2& position );
522 * Combines width and height exceed policies.
524 * This method is a big switch() which combines two exceed policies into one.
525 * The aim is avoid this switch inside the relayout code.
527 * i.e. Width policy = Split. Height policy = Original. Internally the policy is SplitOriginal.
529 void CombineExceedPolicies();
532 * Retrieves the text-view's root actor which stores all text-actors.
533 * It could be the text-view itself or an actor used in the snapshot mode.
535 * @return the root actor.
537 Actor GetRootActor() const;
540 * Handles SetProperty for multiline policy.
541 * @param[in] propertyValue The new property value.
543 void OnMultilinePolicyPropertySet( Property::Value propertyValue );
546 * Handles SetProperty for width exceed policy.
547 * @param[in] propertyValue The new property value.
549 void OnWidthExceedPolicyPropertySet( Property::Value propertyValue );
552 * Handles SetProperty for height exceed policy.
553 * @param[in] propertyValue The new property value.
555 void OnHeightExceedPolicyPropertySet( Property::Value propertyValue );
558 * Handles SetProperty for line justification.
559 * @param[in] propertyValue The new property value.
561 void OnLineJustificationPropertySet( Property::Value propertyValue );
564 * Handles SetProperty for fade boundary.
565 * @param[in] propertyIndex The property index.
566 * @param[in] propertyValue The new property value.
568 void OnFadeBoundaryPropertySet( Property::Index propertyIndex, Property::Value propertyValue );
571 * Handles SetProperty for alignment property.
572 * @param[in] propertyIndex The property index.
573 * @param[in] propertyValue The new property value.
575 void OnAlignmentPropertySet( Property::Index propertyIndex, Property::Value propertyValue );
580 * The parameters which affects the layout of the text.
582 struct LayoutParameters
585 * Default constructor.
592 LayoutParameters( Toolkit::TextView::MultilinePolicy multilinePolicy,
593 Toolkit::TextView::ExceedPolicy widthExceedPolicy,
594 Toolkit::TextView::ExceedPolicy heightExceedPolicy,
595 Toolkit::Alignment::Type alignment,
596 Toolkit::TextView::LineJustification lineJustification,
597 float lineHeightOffset,
598 const std::string& ellipsizeText,
599 const bool markUpEnabled );
604 LayoutParameters( const LayoutParameters& layoutParameters );
607 * Assignment operator.
609 LayoutParameters& operator=( const LayoutParameters& layoutParameters );
611 Toolkit::TextView::MultilinePolicy mMultilinePolicy; ///< Stores the multiline policy.
612 TextView::ExceedPolicy mExceedPolicy; ///< Stores a combination of both policies;
613 Toolkit::TextView::ExceedPolicy mWidthExceedPolicy; ///< Stores the text width exceed policy.
614 Toolkit::TextView::ExceedPolicy mHeightExceedPolicy; ///< Stores the text height exceed policy.
615 Toolkit::Alignment::Type mHorizontalAlignment; ///< Stores the horizontal alignment for the whole text.
616 Toolkit::Alignment::Type mVerticalAlignment; ///< Stores the vertical alignment for the whole text.
617 Toolkit::TextView::LineJustification mLineJustification; ///< Stores the line justification.
618 float mLineHeightOffset; ///< Line height offset to be addded to the font line height (measured in PointSize).
619 MarkupProcessor::StyledTextArray mEllipsizeText; ///< Stores the ellipsize text
620 bool mMarkUpEnabled:1; ///< Is markup string scanning enabled
624 * Some parameters which affects the text view visualization.
626 struct VisualParameters
629 * Default constructor.
636 VisualParameters( const VisualParameters& visualParameters );
639 * Assignment operator.
641 VisualParameters& operator=( const VisualParameters& visualParameters );
643 Toolkit::TextView::FadeBoundary mFadeBoundary; ///< Fade boundary used in fade mode.
644 float mSortModifier; ///< Stores the sort modifier for all text-actors.
645 Vector2 mCameraScrollPosition; ///< The scroll offset.
646 bool mSnapshotModeEnabled:1; ///< Whether text-view is rendered offscreen.
647 bool mScrollEnabled:1; ///< Whether the text scroll is enabled.
648 bool mScrollPositionTrimmed:1; ///< Whether the last scroll position set was trimmed.
652 * Temporary data used to calculate line justification.
654 struct LineJustificationInfo
656 TextViewProcessor::TextInfoIndices mIndices; ///< Indices to the first character of the new line.
657 float mLineLength; ///< Length of the line (or portion of line).
661 * The results of the relayout process.
666 * Default constructor.
673 RelayoutData( const RelayoutData& relayoutData );
676 * Assignment operator.
678 RelayoutData& operator=( const RelayoutData& relayoutData );
680 Size mTextViewSize; ///< The text-view's size used to relaid-out the text.
681 float mShrinkFactor; ///< Shrink factor used when the exceed policy contains ShrinkToFit.
682 TextViewProcessor::TextLayoutInfo mTextLayoutInfo; ///< Stores metrics, layout info (size, direction, type of word) and text-actor info for the whole text.
683 std::vector<int> mCharacterLogicalToVisualMap; ///< Reorder map that stores each character's visual (output) index according to its logical (input) index
684 std::vector<int> mCharacterVisualToLogicalMap; ///< Reorder map that stores each character's logical (input) index according to its visual (output) index
685 std::vector<RenderableActor> mGlyphActors; ///< Stores handles of those text-actors which are currently added to the text-view.
686 std::vector<RenderableActor> mEllipsizedGlyphActors; ///< Stores handles of those text-actors which are used to ellipsize the text.
687 Toolkit::TextView::CharacterLayoutInfoContainer mCharacterLayoutInfoTable; ///< Stores layout info per character sorted by the character's visual index.
688 Toolkit::TextView::LineLayoutInfoContainer mLines; ///< Stores an index to the first character of each line.
689 Size mTextSizeForRelayoutOption; ///< Stores the text size after relayout.
690 std::vector<LineJustificationInfo> mLineJustificationInfo; ///< Stores justification info per line.
691 TextActorCache mTextActorCache; ///< Stores previously created text-actors to be reused.
696 MarkupProcessor::StyledTextArray mCurrentStyledText; ///< text currently displayed by the view
697 std::vector<TextViewProcessorMetadata> mTextViewProcessorOperations; ///< Stores all relayout operations which arrive between two consecutive OnRelaidOut() calls.
699 LayoutParameters mLayoutParameters; ///< Stores some layout parameters in a struct. To be passed in layout functions.
700 VisualParameters mVisualParameters; ///< Some parameters which afects text-view visualization.
701 RelayoutData mRelayoutData; ///< struct with text-view's data structures used to pass all of them in one parameter.
702 RelayoutOperationMask mRelayoutOperations; ///< Which relayout operations have to be done.
704 Layer mOffscreenRootActor; ///< Root actor for offscreen rendering.
705 ImageActor mOffscreenImageActor; ///< Image actor for offscreen rendering.
706 CameraActor mOffscreenCameraActor; ///< Camera actor for offscreen rendering.
707 Size mCurrentOffscreenSize; ///< Current used ofscreen size.
708 FrameBufferImage mFrameBufferImage; ///< Frame buffer used for offscreen rendering.
709 RenderTask mRenderTask; ///< Used to generate an offscreen rendering.
711 PanGestureDetector mPanGestureDetector; ///< Pan gesture for text scrolling.
714 * Helper class used to prevent the modification of some members.
732 bool mLockPreviousSnapshotMode; ///< Whether previous stored snapshot mode should be modified.
733 bool mPreviousSnapshotModeEnabled:1; ///< Stores the previous snapshot mode value.
734 bool mMarkUpEnabled:1; ///< enable to scan for mark-up
736 Toolkit::TextView::ScrolledSignalV2 mScrolledSignalV2; ///< Signal emitted when text is scrolled.
738 Property::Index mPropertyText; ///< Property index for text.
739 Property::Index mPropertyMultilinePolicy; ///< Property index for multiline policy.
740 Property::Index mPropertyWidthExceedPolicy; ///< Property index for width exceed policy.
741 Property::Index mPropertyHeightExceedPolicy; ///< Property index for height exceed policy.
742 Property::Index mPropertyLineJustification; ///< Property index for line justification policy.
743 Property::Index mPropertyFadeBoundaryLeft; ///< Property index for Left fade boundary.
744 Property::Index mPropertyFadeBoundaryRight; ///< Property index for Right fade boundary.
745 Property::Index mPropertyFadeBoundaryTop; ///< Property index for Top fade boundary.
746 Property::Index mPropertyFadeBoundaryBottom; ///< Property index for Bottom fade boundary.
747 Property::Index mPropertyLineHeightOffset; ///< Property index for Line height offset.
748 Property::Index mPropertyHorizontalAlignment; ///< Property index for Horizontal alignment.
749 Property::Index mPropertyVerticalAlignment; ///< Property index for Vertical alignment.
752 } // namespace Internal
754 // Helpers for public-api forwarding methods
756 inline Internal::TextView& GetImpl( TextView& textView )
758 DALI_ASSERT_ALWAYS( textView );
760 RefObject& handle = textView.GetImplementation();
762 return static_cast< Internal::TextView& >( handle );
765 inline const Internal::TextView& GetImpl( const TextView& textView )
767 DALI_ASSERT_ALWAYS( textView );
769 const RefObject& handle = textView.GetImplementation();
771 return static_cast< const Internal::TextView& >( handle );
774 } // namespace Toolkit
778 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__