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 Control
48 TEXTVIEW_PROPERTY_START_INDEX = Control::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 SplitEllipsizeEnd, ///< Split if it exceeds the width and ellipsize if it exceeds the height.
64 Fade, ///< Faded if it exceeds any boundary.
65 FadeOriginal, ///< Faded if it exceeds the width but no action if it exceeds the height.
66 ShrinkOriginal, ///< Shrunk if it exceeds the width but no action if it exceeds the height.
67 ShrinkFade, ///< Shrunk if it exceeds the width and faded if it exceeds the height.
68 Shrink, ///< Shrunk if it exceeds any boundary.
69 EllipsizeEndOriginal, ///< Ellipsized by the end if it exceeds the width but no action if it exceeds the height.
70 EllipsizeEnd ///< Ellipsized by the end if it exceeds the width and/or the height.
73 // Between two OnRelaidOut methods, several calls to InsertTextAt, RemoveTextFrom or SetText can happen.
74 // TextViewProcessorMetadata stores the type of operation. A vector stores all operations between two OnRelaidOut calls.
76 enum TextViewProcessorMetadataType
78 TextSet, ///< Sets new text.
79 TextInserted, ///< Inserts text into current text.
80 TextReplaced, ///< Replaces some text from current text.
81 TextRemoved, ///< Removes some text from current text.
82 NewLineHeight, ///< Sets a new line height offset.
83 NewStyle ///< Sets a new style to the whole text.
87 * Stores info about which data structures need to be modified when the OnRelaidOut() method is called
89 struct TextViewProcessorMetadata
91 TextViewProcessorMetadata();
93 TextViewProcessorMetadataType mType; ///< Stores the type of operation.
94 std::size_t mPosition; ///< Character position within the text.
95 std::size_t mNumberOfCharacters; ///< Number of characters to be removed/ replaced.
96 MarkupProcessor::StyledTextArray mText; ///< The new text.
97 TextStyle::Mask mStyleMask; ///< The style mask.
101 * Defines which operations have to be done in the relayout process.
103 enum RelayoutOperationMask
105 NO_RELAYOUT = 0x0, ///< Does nothing.
106 RELAYOUT_REMOVE_TEXT_ACTORS = 0x1, ///< Removes current text-actors from the text-view.
107 RELAYOUT_SIZE_POSITION = 0x2, ///< Calculates size and position of the text but it doesn't calculate alignment.
108 RELAYOUT_ALIGNMENT = 0x4, ///< Aligns the whole text.
109 RELAYOUT_VISIBILITY = 0x8, ///< Calculates the visibility.
110 RELAYOUT_INITIALIZE_TEXT_ACTORS = 0x10, ///< Initialize text-actors (create handles).
111 RELAYOUT_TEXT_ACTOR_UPDATE = 0x20, ///< Updates text-actors (set size, position, style, ...)
112 RELAYOUT_INSERT_TO_TEXT_VIEW = 0x40, ///< Adds the text-actors to the text-view.
113 RELAYOUT_INSERT_TO_TEXT_ACTOR_LIST = 0x80, ///< Inserts the text-actors to the text-actor list.
114 RELAYOUT_ALL = 0xFF ///< Does all operations.
117 //////////////////////////////////////////////
120 * Create a new TextView.
121 * @return A smart-pointer to the newly allocated TextView.
123 static Toolkit::TextView New();
126 * @copydoc SetText( const std::string& text )
128 void SetText( const std::string& text );
131 * @copydoc SetText( const StyledTextArray& text )
133 void SetText( const MarkupProcessor::StyledTextArray& text );
136 * @copydoc InsertTextAt( std::size_t position, const std::string& text )
138 void InsertTextAt( std::size_t position, const std::string& text );
141 * @copydoc InsertTextAt( std::size_t position, const std::string& text )
143 void InsertTextAt( std::size_t position, const MarkupProcessor::StyledTextArray& text );
146 * @copydoc RemoveTextFrom( std::size_t position, std::size_t numberOfCharacters )
148 void RemoveTextFrom( std::size_t position, std::size_t numberOfCharacters );
151 * @copydoc ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const std::string& text )
153 void ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const std::string& text );
156 * @copydoc ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const std::string& text )
158 void ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const MarkupProcessor::StyledTextArray& text );
163 std::string GetText() const;
166 * @copydoc SetLineHeightOffset()
168 void SetLineHeightOffset( PointSize offset );
171 * @copydoc GetLineHeightOffset()
173 PointSize GetLineHeightOffset() const;
176 * @copydoc SetStyleToCurrentText()
178 void SetStyleToCurrentText( const TextStyle& style, TextStyle::Mask mask );
181 * @copydoc SetTextAlignment( Toolkit::Alignment::Type align )
183 void SetTextAlignment( Toolkit::Alignment::Type align );
186 * @copydoc GetTextAlignment()
188 Toolkit::Alignment::Type GetTextAlignment() const;
191 * @copydoc SetMultilinePolicy()
193 void SetMultilinePolicy( Toolkit::TextView::MultilinePolicy policy );
196 * @copydoc GetMultilinePolicy()
198 Toolkit::TextView::MultilinePolicy GetMultilinePolicy() const;
201 * @copydoc SetWidthExceedPolicy()
203 void SetWidthExceedPolicy( Toolkit::TextView::ExceedPolicy policy );
206 * @copydoc GetWidthExceedPolicy()
208 Toolkit::TextView::ExceedPolicy GetWidthExceedPolicy() const;
211 * @copydoc SetHeightExceedPolicy()
213 void SetHeightExceedPolicy( Toolkit::TextView::ExceedPolicy policy );
216 * @copydoc GetHeightExceedPolicy()
218 Toolkit::TextView::ExceedPolicy GetHeightExceedPolicy() const;
221 * @copydoc SetLineJustification()
223 void SetLineJustification( Toolkit::TextView::LineJustification justification );
226 * @copydoc GetLineJustification()
228 Toolkit::TextView::LineJustification GetLineJustification() const;
231 * @copydoc SetFadeBoundary()
233 void SetFadeBoundary( const Toolkit::TextView::FadeBoundary& fadeBoundary );
236 * @copydoc GetFadeBoundary()
238 const Toolkit::TextView::FadeBoundary& GetFadeBoundary() const;
241 * @copydoc SetEllipsizeText()
243 void SetEllipsizeText( const std::string& ellipsizeText );
246 * @copydoc SetEllipsizeText()
248 void SetEllipsizeText( const MarkupProcessor::StyledTextArray& ellipsizeText );
251 * @copydoc GetEllipsizeText()
253 std::string GetEllipsizeText() const;
256 * Checks if relayout the text is needed. If it is, it relais out the text
257 * by calling DoRelayOut().
259 void GetTextLayoutInfo();
262 * Calls GetTextLayoutInfo() and fills the given data structure.
264 * @see GetTextLayoutInfo()
266 void GetTextLayoutInfo( Toolkit::TextView::TextLayoutInfo& textLayoutInfo );
269 * @copydoc SetSortModifier()
271 void SetSortModifier( float depthOffset );
274 * @copydoc SetSnapshotModeEnabled()
276 void SetSnapshotModeEnabled( bool enable );
279 * @copydoc IsSnapshotModeEnabled()
281 bool IsSnapshotModeEnabled() const;
284 * @brief Sets whether markup processing should be carried out.
286 * @param[in] enable whether markup processing is carried out or not.
288 void SetMarkupProcessingEnabled( bool enable );
291 * @brief Returns whether markup processing is enabled or not
293 * @return true is markup processing is enabled
295 bool IsMarkupProcessingEnabled() const;
298 * @copydoc SetScrollEnabled()
300 void SetScrollEnabled( bool enable );
303 * @copydoc IsScrollEnabled()
305 bool IsScrollEnabled() const;
308 * @copydoc SetScrollPosition()
309 * @see DoSetScrollPosition()
311 void SetScrollPosition( const Vector2& position );
314 * @copydoc GetScrollPosition()
316 const Vector2& GetScrollPosition() const;
319 * @copydoc IsScrollPositionTrimmed()
321 bool IsScrollPositionTrimmed() const;
324 * @copydoc ScrolledSignal()
326 Toolkit::TextView::ScrolledSignalV2& ScrolledSignal();
329 * Connects a callback function with the object's signals.
330 * @param[in] object The object providing the signal.
331 * @param[in] tracker Used to disconnect the signal.
332 * @param[in] signalName The signal to connect to.
333 * @param[in] functor A newly allocated FunctorDelegate.
334 * @return True if the signal was connected.
335 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
337 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
342 * Called when a property of an object of this type is set.
343 * @param[in] object The object whose property is set.
344 * @param[in] index The property index.
345 * @param[in] value The new property value.
347 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
350 * Called to retrieve a property of an object of this type.
351 * @param[in] object The object whose property is to be retrieved.
352 * @param[in] index The property index.
353 * @return The current value of the property.
355 static Property::Value GetProperty( BaseObject* object, Property::Index index );
358 private: // From Control
361 * @copydoc Toolkit::Control::OnInitialize()
363 virtual void OnInitialize();
366 * @copydoc Toolkit::Control::OnFontChange( )
368 virtual void OnFontChange( bool defaultFontChange, bool defaultFontSizeChange );
371 * @copydoc Toolkit::Control::OnControlSizeSet()
373 virtual void OnControlSizeSet( const Vector3& size );
376 * @copydoc Toolkit::Control::OnRelaidOut()
378 * Removes text-actor and calls DoRelayOut()..
380 virtual void OnRelaidOut( Vector2 size, ActorSizeContainer& container );
383 * Retrieves the text-view's natural size.
385 * @return The natural size.
387 virtual Vector3 GetNaturalSize();
390 * Retrieves the text-view's \e height for a given \e width.
392 * @param[in] width The given \e width.
394 * @return The \e height for the given \e width.
396 virtual float GetHeightForWidth( float width );
399 * Retrieves the text-view's \e width for a given \e height.
401 * @param[in] height The given \e height.
403 * @return The \e width for the given \e height.
405 virtual float GetWidthForHeight( float height );
410 * Construct a new TextView.
415 * A reference counted object may only be deleted by calling Unreference()
422 TextView( const TextView& );
425 TextView& operator=( const TextView& rhs );
428 * Executes synchronously relayout operations such as set, insert, remove or replace text, or
429 * split groups of characters, etc.
431 void PerformTextViewProcessorOperations();
434 * Optimizes some text-view processor operations.
436 void OptimizeTextViewProcessorOperations();
439 * Synchronously relays-out all text-actors.
441 * Perform text-view-processor operations, sets the new size and position of text-actors and adds them to the text-view.
443 * @param[in] textViewSize The new text-view's size.
444 * @param[in] relayoutOperationMask Defines which operations need to be done in the relayout process.
446 void DoRelayOut( const Size& textViewSize, RelayoutOperationMask relayoutOperationMask );
449 * Process Snapshot. It refresh the render-task in order to generate a new snapshot image.
451 * ProcessSnapshot is called from OnRelaidOut() only if text has been relaid out.
452 * It creates a new image buffer only if the size of the text has changed.
454 * @param[in] textViewSize The new text-view's size.
456 void ProcessSnapshot( const Size& textViewSize );
459 * Aligns the offscreen rendering camera actor to cover the whole text and the resulting image actor accordingly with the text view's alignment.
461 * @param[in] textViewSize The text view's size.
462 * @param[in] offscreenSize The offscreen's frame buffer's size.
464 void AlignOffscreenCameraActor( const Size& textViewSize, const Size& offscreenSize );
467 * Callback called when the render task has processed.
469 * It makes the root actor invisible.
471 * @param[in] renderTask The processed render task.
473 void RenderTaskFinished( Dali::RenderTask& renderTask );
476 * Destroys offscreen rendering resources.
478 * It disconects the render task finished signal from the TextView::RenderTaskFinished() method,
479 * removes the render task from the render task list and resets the offscreen camera actor, root actor,
480 * image actor and the frame buffer.
482 void DestroyOffscreenRenderingResources();
485 * Called when text-view is scrolled.
487 * Sets the new scroll position. @see DoSetScrollPosition()
489 * @param[in] actor Handle of the text-view.
490 * @param[in] gesture Data structure with the parameters of the gesture.
492 void OnTextPan( Actor actor, PanGesture gesture );
495 * Ensures the text-view's boundaries are fully covered of text.
497 * i.e. if the text-view's size is 100x100 and the text's size is 150x100, the scroll position
498 * can be in the range -50,0 and 50,0.
500 void TrimScrollPosition();
503 * Called from SetScrollPosition() and OnTextPan()
505 * Updates the stored scroll position ensuring the text-view is always covered with text by calling
506 * TrimScrollPosition(), calculates the difference with the previous one and emits the Toolkit::TextView::SignalScrolled() signal.
508 * @param[in] position The new scroll position.
510 void DoSetScrollPosition( const Vector2& position );
513 * Combines width and height exceed policies.
515 * This method is a big switch() which combines two exceed policies into one.
516 * The aim is avoid this switch inside the relayout code.
518 * i.e. Width policy = Split. Height policy = Original. Internally the policy is SplitOriginal.
520 void CombineExceedPolicies();
523 * Retrieves the text-view's root actor which stores all text-actors.
524 * It could be the text-view itself or an actor used in the snapshot mode.
526 * @return the root actor.
528 Actor GetRootActor() const;
531 * Handle SetProperty for markup processing.
532 * @param[in] propertyValue The new property value.
534 void OnMarkupEnabledPeopertySet( Property::Value propertyValue );
537 * Handles SetProperty for multiline policy.
538 * @param[in] propertyValue The new property value.
540 void OnMultilinePolicyPropertySet( Property::Value propertyValue );
543 * Handles SetProperty for width exceed policy.
544 * @param[in] propertyValue The new property value.
546 void OnWidthExceedPolicyPropertySet( Property::Value propertyValue );
549 * Handles SetProperty for height exceed policy.
550 * @param[in] propertyValue The new property value.
552 void OnHeightExceedPolicyPropertySet( Property::Value propertyValue );
555 * Handles SetProperty for line justification.
556 * @param[in] propertyValue The new property value.
558 void OnLineJustificationPropertySet( Property::Value propertyValue );
561 * Handles SetProperty for fade boundary.
562 * @param[in] propertyValue The new property value.
564 void OnFadeBoundaryPropertySet( Property::Value propertyValue );
567 * Handles SetProperty for alignment property.
568 * @param[in] propertyIndex The property index.
569 * @param[in] propertyValue The new property value.
571 void OnAlignmentPropertySet( Property::Index propertyIndex, Property::Value propertyValue );
574 * Handles GetProperty for horizontal alignment property.
575 * @return The property value of horizontal alignment.
577 std::string OnHorizontalAlignmentPropertyGet();
580 * Handles GetProperty for vertical alignment property.
581 * @return The property value of vertical alignment.
583 std::string OnVerticalAlignmentPropertyGet();
588 * The parameters which affects the layout of the text.
590 struct LayoutParameters
593 * Default constructor.
600 LayoutParameters( Toolkit::TextView::MultilinePolicy multilinePolicy,
601 Toolkit::TextView::ExceedPolicy widthExceedPolicy,
602 Toolkit::TextView::ExceedPolicy heightExceedPolicy,
603 Toolkit::Alignment::Type alignment,
604 Toolkit::TextView::LineJustification lineJustification,
605 float lineHeightOffset,
606 const std::string& ellipsizeText,
607 bool markUpEnabled );
612 LayoutParameters( const LayoutParameters& layoutParameters );
615 * Assignment operator.
617 LayoutParameters& operator=( const LayoutParameters& layoutParameters );
619 Toolkit::TextView::MultilinePolicy mMultilinePolicy; ///< Stores the multiline policy.
620 TextView::ExceedPolicy mExceedPolicy; ///< Stores a combination of both policies;
621 Toolkit::TextView::ExceedPolicy mWidthExceedPolicy; ///< Stores the text width exceed policy.
622 Toolkit::TextView::ExceedPolicy mHeightExceedPolicy; ///< Stores the text height exceed policy.
623 Toolkit::Alignment::Type mHorizontalAlignment; ///< Stores the horizontal alignment for the whole text.
624 Toolkit::Alignment::Type mVerticalAlignment; ///< Stores the vertical alignment for the whole text.
625 Toolkit::TextView::LineJustification mLineJustification; ///< Stores the line justification.
626 float mLineHeightOffset; ///< Line height offset to be addded to the font line height (measured in PointSize).
627 MarkupProcessor::StyledTextArray mEllipsizeText; ///< Stores the ellipsize text
628 bool mMarkUpEnabled:1; ///< Is markup string scanning enabled
632 * Some parameters which affects the text view visualization.
634 struct VisualParameters
637 * Default constructor.
644 VisualParameters( const VisualParameters& visualParameters );
647 * Assignment operator.
649 VisualParameters& operator=( const VisualParameters& visualParameters );
651 Toolkit::TextView::FadeBoundary mFadeBoundary; ///< Fade boundary used in fade mode.
652 float mSortModifier; ///< Stores the sort modifier for all text-actors.
653 Vector2 mCameraScrollPosition; ///< The scroll offset.
654 bool mSnapshotModeEnabled:1; ///< Whether text-view is rendered offscreen.
655 bool mScrollEnabled:1; ///< Whether the text scroll is enabled.
656 bool mScrollPositionTrimmed:1; ///< Whether the last scroll position set was trimmed.
660 * Temporary data used to calculate line justification.
662 struct LineJustificationInfo
664 TextViewProcessor::TextInfoIndices mIndices; ///< Indices to the first character of the new line.
665 float mLineLength; ///< Length of the line (or portion of line).
669 * The results of the relayout process.
674 * Default constructor.
681 RelayoutData( const RelayoutData& relayoutData );
684 * Assignment operator.
686 RelayoutData& operator=( const RelayoutData& relayoutData );
688 Size mTextViewSize; ///< The text-view's size used to relaid-out the text.
689 float mShrinkFactor; ///< Shrink factor used when the exceed policy contains ShrinkToFit.
690 TextViewProcessor::TextLayoutInfo mTextLayoutInfo; ///< Stores metrics, layout info (size, direction, type of word) and text-actor info for the whole text.
691 std::vector<int> mCharacterLogicalToVisualMap; ///< Reorder map that stores each character's visual (output) index according to its logical (input) index
692 std::vector<int> mCharacterVisualToLogicalMap; ///< Reorder map that stores each character's logical (input) index according to its visual (output) index
693 std::vector<RenderableActor> mGlyphActors; ///< Stores handles of those text-actors which are currently added to the text-view.
694 std::vector<RenderableActor> mEllipsizedGlyphActors; ///< Stores handles of those text-actors which are used to ellipsize the text.
695 Toolkit::TextView::CharacterLayoutInfoContainer mCharacterLayoutInfoTable; ///< Stores layout info per character sorted by the character's visual index.
696 Toolkit::TextView::LineLayoutInfoContainer mLines; ///< Stores an index to the first character of each line.
697 Size mTextSizeForRelayoutOption; ///< Stores the text size after relayout.
698 std::vector<LineJustificationInfo> mLineJustificationInfo; ///< Stores justification info per line.
699 TextActorCache mTextActorCache; ///< Stores previously created text-actors to be reused.
704 MarkupProcessor::StyledTextArray mCurrentStyledText; ///< text currently displayed by the view
705 std::vector<TextViewProcessorMetadata> mTextViewProcessorOperations; ///< Stores all relayout operations which arrive between two consecutive OnRelaidOut() calls.
707 LayoutParameters mLayoutParameters; ///< Stores some layout parameters in a struct. To be passed in layout functions.
708 VisualParameters mVisualParameters; ///< Some parameters which afects text-view visualization.
709 RelayoutData mRelayoutData; ///< struct with text-view's data structures used to pass all of them in one parameter.
710 RelayoutOperationMask mRelayoutOperations; ///< Which relayout operations have to be done.
712 Layer mOffscreenRootActor; ///< Root actor for offscreen rendering.
713 ImageActor mOffscreenImageActor; ///< Image actor for offscreen rendering.
714 CameraActor mOffscreenCameraActor; ///< Camera actor for offscreen rendering.
715 Size mCurrentOffscreenSize; ///< Current used ofscreen size.
716 FrameBufferImage mFrameBufferImage; ///< Frame buffer used for offscreen rendering.
717 RenderTask mRenderTask; ///< Used to generate an offscreen rendering.
719 PanGestureDetector mPanGestureDetector; ///< Pan gesture for text scrolling.
722 * Helper class used to prevent the modification of some members.
740 bool mLockPreviousSnapshotMode; ///< Whether previous stored snapshot mode should be modified.
741 bool mPreviousSnapshotModeEnabled:1; ///< Stores the previous snapshot mode value.
742 bool mMarkUpEnabled:1; ///< enable to scan for mark-up
744 Toolkit::TextView::ScrolledSignalV2 mScrolledSignalV2; ///< Signal emitted when text is scrolled.
747 } // namespace Internal
749 // Helpers for public-api forwarding methods
751 inline Internal::TextView& GetImpl( TextView& textView )
753 DALI_ASSERT_ALWAYS( textView );
755 RefObject& handle = textView.GetImplementation();
757 return static_cast< Internal::TextView& >( handle );
760 inline const Internal::TextView& GetImpl( const TextView& textView )
762 DALI_ASSERT_ALWAYS( textView );
764 const RefObject& handle = textView.GetImplementation();
766 return static_cast< const Internal::TextView& >( handle );
769 } // namespace Toolkit
773 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__