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 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 #include <dali/public-api/actors/camera-actor.h>
23 #include <dali/public-api/actors/image-actor.h>
24 #include <dali/public-api/actors/layer.h>
25 #include <dali/public-api/images/glyph-image.h>
26 #include <dali/public-api/render-tasks/render-task.h>
29 #include <dali-toolkit/public-api/controls/control-impl.h>
30 #include <dali-toolkit/public-api/controls/text-view/text-view.h>
31 #include <dali-toolkit/internal/controls/text-view/text-actor-cache.h>
32 #include <dali-toolkit/internal/controls/text-view/text-view-processor-types.h>
44 * TextView is a custom control for text aligning and multiline support
46 class TextView : public Control
51 * Internal exceed policy with the valid combinations.
55 Original, ///< Original size (even if it exceeds the witdh or the height).
56 OriginalFade, ///< Original size if it exceeds the width but faded if it exceeds the height.
57 OriginalShrink, ///< Shrunk if it exceeds the height.
58 SplitOriginal, ///< Split if it exceeds the width but no action if it exceeds the height.
59 SplitFade, ///< Split if it exceeds the width and faded if it exceeds the height.
60 SplitShrink, ///< Split if it exceeds the width and shrunk if it exceeds the height.
61 SplitEllipsizeEnd, ///< Split if it exceeds the width and ellipsize if it exceeds the height.
62 Fade, ///< Faded if it exceeds any boundary.
63 FadeOriginal, ///< Faded if it exceeds the width but no action if it exceeds the height.
64 ShrinkOriginal, ///< Shrunk if it exceeds the width but no action if it exceeds the height.
65 ShrinkFade, ///< Shrunk if it exceeds the width and faded if it exceeds the height.
66 Shrink, ///< Shrunk if it exceeds any boundary.
67 EllipsizeEndOriginal, ///< Ellipsized by the end if it exceeds the width but no action if it exceeds the height.
68 EllipsizeEnd ///< Ellipsized by the end if it exceeds the width and/or the height.
71 // Between two OnRelayout methods, several calls to InsertTextAt, RemoveTextFrom or SetText can happen.
72 // TextViewProcessorMetadata stores the type of operation. A vector stores all operations between two OnRelayout calls.
74 enum TextViewProcessorMetadataType
76 TextSet, ///< Sets new text.
77 TextInserted, ///< Inserts text into current text.
78 TextReplaced, ///< Replaces some text from current text.
79 TextRemoved, ///< Removes some text from current text.
80 NewLineHeight, ///< Sets a new line height offset.
81 NewStyle ///< Sets a new style to the whole text.
85 * Stores info about which data structures need to be modified when the OnRelayout() method is called
87 struct TextViewProcessorMetadata
89 TextViewProcessorMetadata();
91 TextViewProcessorMetadataType mType; ///< Stores the type of operation.
92 std::size_t mPosition; ///< Character position within the text.
93 std::size_t mNumberOfCharacters; ///< Number of characters to be removed/replaced.
94 MarkupProcessor::StyledTextArray mText; ///< The new text.
95 TextStyle::Mask mStyleMask; ///< The style mask.
99 * Defines which operations have to be done in the relayout process.
101 enum RelayoutOperationMask
103 NO_RELAYOUT = 0x0, ///< Does nothing.
104 RELAYOUT_REMOVE_TEXT_ACTORS = 0x1, ///< Removes current text-actors from the text-view.
105 RELAYOUT_SIZE_POSITION = 0x2, ///< Calculates size and position of the text but it doesn't calculate alignment.
106 RELAYOUT_ALIGNMENT = 0x4, ///< Aligns the whole text.
107 RELAYOUT_VISIBILITY = 0x8, ///< Calculates the visibility.
108 RELAYOUT_INITIALIZE_TEXT_ACTORS = 0x10, ///< Initialize text-actors (create handles).
109 RELAYOUT_TEXT_ACTOR_UPDATE = 0x20, ///< Updates text-actors (set size, position, style, ...)
110 RELAYOUT_INSERT_TO_TEXT_VIEW = 0x40, ///< Adds the text-actors to the text-view.
111 RELAYOUT_ALL = 0xFF ///< Does all operations.
114 //////////////////////////////////////////////
117 * Create a new TextView.
118 * @return A smart-pointer to the newly allocated TextView.
120 static Toolkit::TextView New();
123 * @copydoc SetText( const std::string& text )
125 void SetText( const std::string& text );
128 * @copydoc SetText( const StyledTextArray& text )
130 void SetText( const MarkupProcessor::StyledTextArray& text );
133 * @copydoc InsertTextAt( std::size_t position, const std::string& text )
135 void InsertTextAt( std::size_t position, const std::string& text );
138 * @copydoc InsertTextAt( std::size_t position, const std::string& text )
140 void InsertTextAt( std::size_t position, const MarkupProcessor::StyledTextArray& text );
143 * @copydoc RemoveTextFrom( std::size_t position, std::size_t numberOfCharacters )
145 void RemoveTextFrom( std::size_t position, std::size_t numberOfCharacters );
148 * @copydoc ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const std::string& text )
150 void ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const std::string& text );
153 * @copydoc ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const std::string& text )
155 void ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const MarkupProcessor::StyledTextArray& text );
160 std::string GetText() const;
163 * @copydoc SetLineHeightOffset()
165 void SetLineHeightOffset( PointSize offset );
168 * @copydoc GetLineHeightOffset()
170 PointSize GetLineHeightOffset() const;
173 * @copydoc SetStyleToCurrentText()
175 void SetStyleToCurrentText( const TextStyle& style, TextStyle::Mask mask );
178 * @copydoc SetTextAlignment( Toolkit::Alignment::Type align )
180 void SetTextAlignment( Toolkit::Alignment::Type align );
183 * @copydoc GetTextAlignment()
185 Toolkit::Alignment::Type GetTextAlignment() const;
188 * @copydoc SetMultilinePolicy()
190 void SetMultilinePolicy( Toolkit::TextView::MultilinePolicy policy );
193 * @copydoc GetMultilinePolicy()
195 Toolkit::TextView::MultilinePolicy GetMultilinePolicy() const;
198 * @copydoc SetWidthExceedPolicy()
200 void SetWidthExceedPolicy( Toolkit::TextView::ExceedPolicy policy );
203 * @copydoc GetWidthExceedPolicy()
205 Toolkit::TextView::ExceedPolicy GetWidthExceedPolicy() const;
208 * @copydoc SetHeightExceedPolicy()
210 void SetHeightExceedPolicy( Toolkit::TextView::ExceedPolicy policy );
213 * @copydoc GetHeightExceedPolicy()
215 Toolkit::TextView::ExceedPolicy GetHeightExceedPolicy() const;
218 * @copydoc SetLineJustification()
220 void SetLineJustification( Toolkit::TextView::LineJustification justification );
223 * @copydoc GetLineJustification()
225 Toolkit::TextView::LineJustification GetLineJustification() const;
228 * @copydoc SetFadeBoundary()
230 void SetFadeBoundary( const Toolkit::TextView::FadeBoundary& fadeBoundary );
233 * @copydoc GetFadeBoundary()
235 const Toolkit::TextView::FadeBoundary& GetFadeBoundary() const;
238 * @copydoc SetEllipsizeText()
240 void SetEllipsizeText( const std::string& ellipsizeText );
243 * @copydoc SetEllipsizeText()
245 void SetEllipsizeText( const MarkupProcessor::StyledTextArray& ellipsizeText );
248 * @copydoc SetEllipsizeText()
250 void SetEllipsizeText( const Text& ellipsizeText, const Vector<TextStyle*>& ellipsizeStyles );
253 * @copydoc GetEllipsizeText()
255 std::string GetEllipsizeText() const;
258 * Checks if relayout the text is needed. If it is, it relais out the text
259 * by calling DoRelayOut().
261 void GetTextLayoutInfo();
264 * Calls GetTextLayoutInfo() and fills the given data structure.
266 * @see GetTextLayoutInfo()
268 void GetTextLayoutInfo( Toolkit::TextView::TextLayoutInfo& textLayoutInfo );
271 * @copydoc SetSortModifier()
273 void SetSortModifier( float depthOffset );
276 * @copydoc SetSnapshotModeEnabled()
278 void SetSnapshotModeEnabled( bool enable );
281 * @copydoc IsSnapshotModeEnabled()
283 bool IsSnapshotModeEnabled() const;
286 * @brief Sets whether markup processing should be carried out.
288 * @param[in] enable whether markup processing is carried out or not.
290 void SetMarkupProcessingEnabled( bool enable );
293 * @brief Returns whether markup processing is enabled or not
295 * @return true is markup processing is enabled
297 bool IsMarkupProcessingEnabled() const;
300 * @copydoc SetScrollEnabled()
302 void SetScrollEnabled( bool enable );
305 * @copydoc IsScrollEnabled()
307 bool IsScrollEnabled() const;
310 * @copydoc SetScrollPosition()
311 * @see DoSetScrollPosition()
313 void SetScrollPosition( const Vector2& position );
316 * @copydoc GetScrollPosition()
318 const Vector2& GetScrollPosition() const;
321 * @copydoc IsScrollPositionTrimmed()
323 bool IsScrollPositionTrimmed() const;
326 * @copydoc ScrolledSignal()
328 Toolkit::TextView::ScrolledSignalType& ScrolledSignal();
331 * Connects a callback function with the object's signals.
332 * @param[in] object The object providing the signal.
333 * @param[in] tracker Used to disconnect the signal.
334 * @param[in] signalName The signal to connect to.
335 * @param[in] functor A newly allocated FunctorDelegate.
336 * @return True if the signal was connected.
337 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
339 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
344 * Called when a property of an object of this type is set.
345 * @param[in] object The object whose property is set.
346 * @param[in] index The property index.
347 * @param[in] value The new property value.
349 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
352 * Called to retrieve a property of an object of this type.
353 * @param[in] object The object whose property is to be retrieved.
354 * @param[in] index The property index.
355 * @return The current value of the property.
357 static Property::Value GetProperty( BaseObject* object, Property::Index index );
360 private: // From Control
363 * @copydoc Toolkit::Control::OnInitialize()
365 virtual void OnInitialize();
368 * @copydoc Toolkit::Control::OnFontChange( )
370 virtual void OnFontChange( bool defaultFontChange, bool defaultFontSizeChange );
373 * @copydoc Toolkit::Control::OnControlSizeSet()
375 virtual void OnControlSizeSet( const Vector3& size );
378 * @copydoc Toolkit::Control::OnRelayout()
380 virtual void OnRelayout( const 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, etc.
430 void PerformTextViewProcessorOperations();
433 * Optimizes some text-view processor operations.
435 void OptimizeTextViewProcessorOperations();
438 * Synchronously relays-out all text-actors.
440 * Perform text-view-processor operations, sets the new size and position of text-actors and adds them to the text-view.
442 * @param[in] textViewSize The new text-view's size.
443 * @param[in] relayoutOperationMask Defines which operations need to be done in the relayout process.
445 void DoRelayOut( const Size& textViewSize, RelayoutOperationMask relayoutOperationMask );
448 * Process Snapshot. It refresh the render-task in order to generate a new snapshot image.
450 * ProcessSnapshot is called from OnRelayout() only if text has been relaid out.
451 * It creates a new image buffer only if the size of the text has changed.
453 * @param[in] textViewSize The new text-view's size.
455 void ProcessSnapshot( const Size& textViewSize );
458 * Aligns the offscreen rendering camera actor to cover the whole text and the resulting image actor accordingly with the text view's alignment.
460 * @param[in] textViewSize The text view's size.
461 * @param[in] offscreenSize The offscreen's frame buffer's size.
463 void AlignOffscreenCameraActor( const Size& textViewSize, const Size& offscreenSize );
466 * Callback called when the render task has processed.
468 * It makes the root actor invisible.
470 * @param[in] renderTask The processed render task.
472 void RenderTaskFinished( Dali::RenderTask& renderTask );
475 * Destroys offscreen rendering resources.
477 * It disconects the render task finished signal from the TextView::RenderTaskFinished() method,
478 * removes the render task from the render task list and resets the offscreen camera actor, root actor,
479 * image actor and the frame buffer.
481 void DestroyOffscreenRenderingResources();
484 * Called when text-view is scrolled.
486 * Sets the new scroll position. @see DoSetScrollPosition()
488 * @param[in] actor Handle of the text-view.
489 * @param[in] gesture Data structure with the parameters of the gesture.
491 void OnTextPan( Actor actor, const PanGesture& gesture );
494 * Ensures the text-view's boundaries are fully covered of text.
496 * i.e. if the text-view's size is 100x100 and the text's size is 150x100, the scroll position
497 * can be in the range -50,0 and 50,0.
499 void TrimScrollPosition();
502 * Called from SetScrollPosition() and OnTextPan()
504 * Updates the stored scroll position ensuring the text-view is always covered with text by calling
505 * TrimScrollPosition(), calculates the difference with the previous one and emits the Toolkit::TextView::SignalScrolled() signal.
507 * @param[in] position The new scroll position.
509 void DoSetScrollPosition( const Vector2& position );
512 * Combines width and height exceed policies.
514 * This method is a big switch() which combines two exceed policies into one.
515 * The aim is avoid this switch inside the relayout code.
517 * i.e. Width policy = Split. Height policy = Original. Internally the policy is SplitOriginal.
519 void CombineExceedPolicies();
522 * Retrieves the text-view's root actor which stores all text-actors.
523 * It could be the text-view itself or an actor used in the snapshot mode.
525 * @return the root actor.
527 Actor GetRootActor() const;
530 * Creates the ellipsize text layout.
532 void CreateEllipsizeLayout();
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.
602 * Default destructor.
609 LayoutParameters( Toolkit::TextView::MultilinePolicy multilinePolicy,
610 Toolkit::TextView::ExceedPolicy widthExceedPolicy,
611 Toolkit::TextView::ExceedPolicy heightExceedPolicy,
612 Toolkit::Alignment::Type alignment,
613 Toolkit::TextView::LineJustification lineJustification,
614 float lineHeightOffset,
615 bool markUpEnabled );
620 LayoutParameters( const LayoutParameters& layoutParameters );
623 * Assignment operator.
625 LayoutParameters& operator=( const LayoutParameters& layoutParameters );
627 Toolkit::TextView::MultilinePolicy mMultilinePolicy; ///< Stores the multiline policy.
628 TextView::ExceedPolicy mExceedPolicy; ///< Stores a combination of both policies.
629 Toolkit::TextView::ExceedPolicy mWidthExceedPolicy; ///< Stores the text width exceed policy.
630 Toolkit::TextView::ExceedPolicy mHeightExceedPolicy; ///< Stores the text height exceed policy.
631 Toolkit::Alignment::Type mHorizontalAlignment; ///< Stores the horizontal alignment for the whole text.
632 Toolkit::Alignment::Type mVerticalAlignment; ///< Stores the vertical alignment for the whole text.
633 Toolkit::TextView::LineJustification mLineJustification; ///< Stores the line justification.
634 float mLineHeightOffset; ///< Line height offset to be addded to the font line height (measured in PointSize).
635 bool mMarkUpEnabled:1; ///< Is markup string scanning enabled.
639 * Some parameters which affects the text view visualization.
641 struct VisualParameters
644 * Default constructor.
651 VisualParameters( const VisualParameters& visualParameters );
654 * Assignment operator.
656 VisualParameters& operator=( const VisualParameters& visualParameters );
658 Toolkit::TextView::FadeBoundary mFadeBoundary; ///< Fade boundary used in fade mode.
659 float mSortModifier; ///< Stores the sort modifier for all text-actors.
660 Vector2 mCameraScrollPosition; ///< The scroll offset.
661 bool mSnapshotModeEnabled:1; ///< Whether text-view is rendered offscreen.
662 bool mScrollEnabled:1; ///< Whether the text scroll is enabled.
663 bool mScrollPositionTrimmed:1; ///< Whether the last scroll position set was trimmed.
667 * The results of the relayout process.
672 * Default constructor.
679 RelayoutData( const RelayoutData& relayoutData );
682 * Assignment operator.
684 RelayoutData& operator=( const RelayoutData& relayoutData );
686 Size mTextViewSize; ///< The text-view's size used to relaid-out the text.
687 float mShrinkFactor; ///< Shrink factor used when the exceed policy contains ShrinkToFit.
688 TextViewProcessor::TextLayoutInfo mTextLayoutInfo; ///< Stores metrics, layout info (size, direction, type of word) and text-actor info for the whole text.
689 std::vector<int> mCharacterLogicalToVisualMap; ///< Reorder map that stores each character's visual (output) index according to its logical (input) index
690 std::vector<int> mCharacterVisualToLogicalMap; ///< Reorder map that stores each character's logical (input) index according to its visual (output) index
691 std::vector<RenderableActor> mGlyphActors; ///< Stores handles of those text-actors which are currently added to the text-view.
692 std::vector<RenderableActor> mEllipsizedGlyphActors; ///< Stores handles of those text-actors which are used to ellipsize the text.
693 Toolkit::TextView::CharacterLayoutInfoContainer mCharacterLayoutInfoTable; ///< Stores layout info per character sorted by the character's visual index.
694 Toolkit::TextView::LineLayoutInfoContainer mLines; ///< Stores an index to the first character of each line.
695 Size mTextSizeForRelayoutOption; ///< Stores the text size after relayout.
696 TextActorCache mTextActorCache; ///< Stores previously created text-actors to be reused.
701 MarkupProcessor::StyledTextArray mCurrentStyledText; ///< text currently displayed by the view
702 std::vector<TextViewProcessorMetadata> mTextViewProcessorOperations; ///< Stores all relayout operations which arrive between two consecutive OnRelayout() calls.
704 LayoutParameters mLayoutParameters; ///< Stores some layout parameters in a struct. To be passed in layout functions.
705 VisualParameters mVisualParameters; ///< Some parameters which afects text-view visualization.
706 RelayoutData mRelayoutData; ///< struct with text-view's data structures used to pass all of them in one parameter.
707 RelayoutOperationMask mRelayoutOperations; ///< Which relayout operations have to be done.
709 Layer mOffscreenRootActor; ///< Root actor for offscreen rendering.
710 ImageActor mOffscreenImageActor; ///< Image actor for offscreen rendering.
711 CameraActor mOffscreenCameraActor; ///< Camera actor for offscreen rendering.
712 Size mCurrentOffscreenSize; ///< Current used ofscreen size.
713 FrameBufferImage mFrameBufferImage; ///< Frame buffer used for offscreen rendering.
714 RenderTask mRenderTask; ///< Used to generate an offscreen rendering.
716 PanGestureDetector mPanGestureDetector; ///< Pan gesture for text scrolling.
719 * Helper class used to prevent the modification of some members.
737 bool mLockPreviousSnapshotMode; ///< Whether previous stored snapshot mode should be modified.
738 bool mPreviousSnapshotModeEnabled:1; ///< Stores the previous snapshot mode value.
739 bool mMarkUpEnabled:1; ///< enable to scan for mark-up
741 Toolkit::TextView::ScrolledSignalType mScrolledSignal; ///< Signal emitted when text is scrolled.
744 } // namespace Internal
746 // Helpers for public-api forwarding methods
748 inline Internal::TextView& GetImpl( TextView& textView )
750 DALI_ASSERT_ALWAYS( textView );
752 RefObject& handle = textView.GetImplementation();
754 return static_cast< Internal::TextView& >( handle );
757 inline const Internal::TextView& GetImpl( const TextView& textView )
759 DALI_ASSERT_ALWAYS( textView );
761 const RefObject& handle = textView.GetImplementation();
763 return static_cast< const Internal::TextView& >( handle );
766 } // namespace Toolkit
770 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__