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 index );
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 );
414 * Construct a new TextView.
419 * A reference counted object may only be deleted by calling Unreference()
426 TextView( const TextView& );
429 TextView& operator=( const TextView& rhs );
432 * Executes synchronously relayout operations such as set, insert, remove or replace text, or
433 * split groups of characters, etc.
435 void PerformTextViewProcessorOperations();
438 * Optimizes some text-view processor operations.
440 void OptimizeTextViewProcessorOperations();
443 * Synchronously relays-out all text-actors.
445 * Perform text-view-processor operations, sets the new size and position of text-actors and adds them to the text-view.
447 * @param[in] textViewSize The new text-view's size.
448 * @param[in] relayoutOperationMask Defines which operations need to be done in the relayout process.
450 void DoRelayOut( const Size& textViewSize, RelayoutOperationMask relayoutOperationMask );
453 * Process Snapshot. It refresh the render-task in order to generate a new snapshot image.
455 * ProcessSnapshot is called from OnRelaidOut() only if text has been relaid out.
456 * It creates a new image buffer only if the size of the text has changed.
458 * @param[in] textViewSize The new text-view's size.
460 void ProcessSnapshot( const Size& textViewSize );
463 * Aligns the offscreen rendering camera actor to cover the whole text and the resulting image actor accordingly with the text view's alignment.
465 * @param[in] textViewSize The text view's size.
466 * @param[in] offscreenSize The offscreen's frame buffer's size.
468 void AlignOffscreenCameraActor( const Size& textViewSize, const Size& offscreenSize );
471 * Callback called when the render task has processed.
473 * It makes the root actor invisible.
475 * @param[in] renderTask The processed render task.
477 void RenderTaskFinished( Dali::RenderTask& renderTask );
480 * Destroys offscreen rendering resources.
482 * It disconects the render task finished signal from the TextView::RenderTaskFinished() method,
483 * removes the render task from the render task list and resets the offscreen camera actor, root actor,
484 * image actor and the frame buffer.
486 void DestroyOffscreenRenderingResources();
489 * Called when text-view is scrolled.
491 * Sets the new scroll position. @see DoSetScrollPosition()
493 * @param[in] actor Handle of the text-view.
494 * @param[in] gesture Data structure with the parameters of the gesture.
496 void OnTextPan( Actor actor, PanGesture gesture );
499 * Ensures the text-view's boundaries are fully covered of text.
501 * i.e. if the text-view's size is 100x100 and the text's size is 150x100, the scroll position
502 * can be in the range -50,0 and 50,0.
504 void TrimScrollPosition();
507 * Called from SetScrollPosition() and OnTextPan()
509 * Updates the stored scroll position ensuring the text-view is always covered with text by calling
510 * TrimScrollPosition(), calculates the difference with the previous one and emits the Toolkit::TextView::SignalScrolled() signal.
512 * @param[in] position The new scroll position.
514 void DoSetScrollPosition( const Vector2& position );
517 * Combines width and height exceed policies.
519 * This method is a big switch() which combines two exceed policies into one.
520 * The aim is avoid this switch inside the relayout code.
522 * i.e. Width policy = Split. Height policy = Original. Internally the policy is SplitOriginal.
524 void CombineExceedPolicies();
527 * Retrieves the text-view's root actor which stores all text-actors.
528 * It could be the text-view itself or an actor used in the snapshot mode.
530 * @return the root actor.
532 Actor GetRootActor() const;
535 * Handle SetProperty for markup processing.
536 * @param[in] propertyValue The new property value.
538 void OnMarkupEnabledPeopertySet( Property::Value propertyValue );
541 * Handles SetProperty for multiline policy.
542 * @param[in] propertyValue The new property value.
544 void OnMultilinePolicyPropertySet( Property::Value propertyValue );
547 * Handles SetProperty for width exceed policy.
548 * @param[in] propertyValue The new property value.
550 void OnWidthExceedPolicyPropertySet( Property::Value propertyValue );
553 * Handles SetProperty for height exceed policy.
554 * @param[in] propertyValue The new property value.
556 void OnHeightExceedPolicyPropertySet( Property::Value propertyValue );
559 * Handles SetProperty for line justification.
560 * @param[in] propertyValue The new property value.
562 void OnLineJustificationPropertySet( Property::Value propertyValue );
565 * Handles SetProperty for fade boundary.
566 * @param[in] propertyValue The new property value.
568 void OnFadeBoundaryPropertySet( 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 );
578 * Handles GetProperty for horizontal alignment property.
579 * @return The property value of horizontal alignment.
581 std::string OnHorizontalAlignmentPropertyGet();
584 * Handles GetProperty for vertical alignment property.
585 * @return The property value of vertical alignment.
587 std::string OnVerticalAlignmentPropertyGet();
592 * The parameters which affects the layout of the text.
594 struct LayoutParameters
597 * Default constructor.
604 LayoutParameters( Toolkit::TextView::MultilinePolicy multilinePolicy,
605 Toolkit::TextView::ExceedPolicy widthExceedPolicy,
606 Toolkit::TextView::ExceedPolicy heightExceedPolicy,
607 Toolkit::Alignment::Type alignment,
608 Toolkit::TextView::LineJustification lineJustification,
609 float lineHeightOffset,
610 const std::string& ellipsizeText,
611 const bool markUpEnabled );
616 LayoutParameters( const LayoutParameters& layoutParameters );
619 * Assignment operator.
621 LayoutParameters& operator=( const LayoutParameters& layoutParameters );
623 Toolkit::TextView::MultilinePolicy mMultilinePolicy; ///< Stores the multiline policy.
624 TextView::ExceedPolicy mExceedPolicy; ///< Stores a combination of both policies;
625 Toolkit::TextView::ExceedPolicy mWidthExceedPolicy; ///< Stores the text width exceed policy.
626 Toolkit::TextView::ExceedPolicy mHeightExceedPolicy; ///< Stores the text height exceed policy.
627 Toolkit::Alignment::Type mHorizontalAlignment; ///< Stores the horizontal alignment for the whole text.
628 Toolkit::Alignment::Type mVerticalAlignment; ///< Stores the vertical alignment for the whole text.
629 Toolkit::TextView::LineJustification mLineJustification; ///< Stores the line justification.
630 float mLineHeightOffset; ///< Line height offset to be addded to the font line height (measured in PointSize).
631 MarkupProcessor::StyledTextArray mEllipsizeText; ///< Stores the ellipsize text
632 bool mMarkUpEnabled:1; ///< Is markup string scanning enabled
636 * Some parameters which affects the text view visualization.
638 struct VisualParameters
641 * Default constructor.
648 VisualParameters( const VisualParameters& visualParameters );
651 * Assignment operator.
653 VisualParameters& operator=( const VisualParameters& visualParameters );
655 Toolkit::TextView::FadeBoundary mFadeBoundary; ///< Fade boundary used in fade mode.
656 float mSortModifier; ///< Stores the sort modifier for all text-actors.
657 Vector2 mCameraScrollPosition; ///< The scroll offset.
658 bool mSnapshotModeEnabled:1; ///< Whether text-view is rendered offscreen.
659 bool mScrollEnabled:1; ///< Whether the text scroll is enabled.
660 bool mScrollPositionTrimmed:1; ///< Whether the last scroll position set was trimmed.
664 * Temporary data used to calculate line justification.
666 struct LineJustificationInfo
668 TextViewProcessor::TextInfoIndices mIndices; ///< Indices to the first character of the new line.
669 float mLineLength; ///< Length of the line (or portion of line).
673 * The results of the relayout process.
678 * Default constructor.
685 RelayoutData( const RelayoutData& relayoutData );
688 * Assignment operator.
690 RelayoutData& operator=( const RelayoutData& relayoutData );
692 Size mTextViewSize; ///< The text-view's size used to relaid-out the text.
693 float mShrinkFactor; ///< Shrink factor used when the exceed policy contains ShrinkToFit.
694 TextViewProcessor::TextLayoutInfo mTextLayoutInfo; ///< Stores metrics, layout info (size, direction, type of word) and text-actor info for the whole text.
695 std::vector<int> mCharacterLogicalToVisualMap; ///< Reorder map that stores each character's visual (output) index according to its logical (input) index
696 std::vector<int> mCharacterVisualToLogicalMap; ///< Reorder map that stores each character's logical (input) index according to its visual (output) index
697 std::vector<RenderableActor> mGlyphActors; ///< Stores handles of those text-actors which are currently added to the text-view.
698 std::vector<RenderableActor> mEllipsizedGlyphActors; ///< Stores handles of those text-actors which are used to ellipsize the text.
699 Toolkit::TextView::CharacterLayoutInfoContainer mCharacterLayoutInfoTable; ///< Stores layout info per character sorted by the character's visual index.
700 Toolkit::TextView::LineLayoutInfoContainer mLines; ///< Stores an index to the first character of each line.
701 Size mTextSizeForRelayoutOption; ///< Stores the text size after relayout.
702 std::vector<LineJustificationInfo> mLineJustificationInfo; ///< Stores justification info per line.
703 TextActorCache mTextActorCache; ///< Stores previously created text-actors to be reused.
708 MarkupProcessor::StyledTextArray mCurrentStyledText; ///< text currently displayed by the view
709 std::vector<TextViewProcessorMetadata> mTextViewProcessorOperations; ///< Stores all relayout operations which arrive between two consecutive OnRelaidOut() calls.
711 LayoutParameters mLayoutParameters; ///< Stores some layout parameters in a struct. To be passed in layout functions.
712 VisualParameters mVisualParameters; ///< Some parameters which afects text-view visualization.
713 RelayoutData mRelayoutData; ///< struct with text-view's data structures used to pass all of them in one parameter.
714 RelayoutOperationMask mRelayoutOperations; ///< Which relayout operations have to be done.
716 Layer mOffscreenRootActor; ///< Root actor for offscreen rendering.
717 ImageActor mOffscreenImageActor; ///< Image actor for offscreen rendering.
718 CameraActor mOffscreenCameraActor; ///< Camera actor for offscreen rendering.
719 Size mCurrentOffscreenSize; ///< Current used ofscreen size.
720 FrameBufferImage mFrameBufferImage; ///< Frame buffer used for offscreen rendering.
721 RenderTask mRenderTask; ///< Used to generate an offscreen rendering.
723 PanGestureDetector mPanGestureDetector; ///< Pan gesture for text scrolling.
726 * Helper class used to prevent the modification of some members.
744 bool mLockPreviousSnapshotMode; ///< Whether previous stored snapshot mode should be modified.
745 bool mPreviousSnapshotModeEnabled:1; ///< Stores the previous snapshot mode value.
746 bool mMarkUpEnabled:1; ///< enable to scan for mark-up
748 Toolkit::TextView::ScrolledSignalV2 mScrolledSignalV2; ///< Signal emitted when text is scrolled.
751 } // namespace Internal
753 // Helpers for public-api forwarding methods
755 inline Internal::TextView& GetImpl( TextView& textView )
757 DALI_ASSERT_ALWAYS( textView );
759 RefObject& handle = textView.GetImplementation();
761 return static_cast< Internal::TextView& >( handle );
764 inline const Internal::TextView& GetImpl( const TextView& textView )
766 DALI_ASSERT_ALWAYS( textView );
768 const RefObject& handle = textView.GetImplementation();
770 return static_cast< const Internal::TextView& >( handle );
773 } // namespace Toolkit
777 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__