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
53 TEXTVIEW_PROPERTY_START_INDEX = Control::CONTROL_PROPERTY_END_INDEX + 1,
54 TEXTVIEW_PROPERTY_END_INDEX = TEXTVIEW_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices
58 * Internal exceed policy with the valid combinations.
62 Original, ///< Original size (even if it exceeds the witdh or the height).
63 OriginalFade, ///< Original size if it exceeds the width but faded if it exceeds the height.
64 OriginalShrink, ///< Shrunk if it exceeds the height.
65 SplitOriginal, ///< Split if it exceeds the width but no action if it exceeds the height.
66 SplitFade, ///< Split if it exceeds the width and faded if it exceeds the height.
67 SplitShrink, ///< Split if it exceeds the width and shrunk if it exceeds the height.
68 SplitEllipsizeEnd, ///< Split if it exceeds the width and ellipsize if it exceeds the height.
69 Fade, ///< Faded if it exceeds any boundary.
70 FadeOriginal, ///< Faded if it exceeds the width but no action if it exceeds the height.
71 ShrinkOriginal, ///< Shrunk if it exceeds the width but no action if it exceeds the height.
72 ShrinkFade, ///< Shrunk if it exceeds the width and faded if it exceeds the height.
73 Shrink, ///< Shrunk if it exceeds any boundary.
74 EllipsizeEndOriginal, ///< Ellipsized by the end if it exceeds the width but no action if it exceeds the height.
75 EllipsizeEnd ///< Ellipsized by the end if it exceeds the width and/or the height.
78 // Between two OnRelaidOut methods, several calls to InsertTextAt, RemoveTextFrom or SetText can happen.
79 // TextViewProcessorMetadata stores the type of operation. A vector stores all operations between two OnRelaidOut calls.
81 enum TextViewProcessorMetadataType
83 TextSet, ///< Sets new text.
84 TextInserted, ///< Inserts text into current text.
85 TextReplaced, ///< Replaces some text from current text.
86 TextRemoved, ///< Removes some text from current text.
87 NewLineHeight, ///< Sets a new line height offset.
88 NewStyle ///< Sets a new style to the whole text.
92 * Stores info about which data structures need to be modified when the OnRelaidOut() method is called
94 struct TextViewProcessorMetadata
96 TextViewProcessorMetadata();
98 TextViewProcessorMetadataType mType; ///< Stores the type of operation.
99 std::size_t mPosition; ///< Character position within the text.
100 std::size_t mNumberOfCharacters; ///< Number of characters to be removed/replaced.
101 MarkupProcessor::StyledTextArray mText; ///< The new text.
102 TextStyle::Mask mStyleMask; ///< The style mask.
106 * Defines which operations have to be done in the relayout process.
108 enum RelayoutOperationMask
110 NO_RELAYOUT = 0x0, ///< Does nothing.
111 RELAYOUT_REMOVE_TEXT_ACTORS = 0x1, ///< Removes current text-actors from the text-view.
112 RELAYOUT_SIZE_POSITION = 0x2, ///< Calculates size and position of the text but it doesn't calculate alignment.
113 RELAYOUT_ALIGNMENT = 0x4, ///< Aligns the whole text.
114 RELAYOUT_VISIBILITY = 0x8, ///< Calculates the visibility.
115 RELAYOUT_INITIALIZE_TEXT_ACTORS = 0x10, ///< Initialize text-actors (create handles).
116 RELAYOUT_TEXT_ACTOR_UPDATE = 0x20, ///< Updates text-actors (set size, position, style, ...)
117 RELAYOUT_INSERT_TO_TEXT_VIEW = 0x40, ///< Adds the text-actors to the text-view.
118 RELAYOUT_ALL = 0xFF ///< Does all operations.
121 //////////////////////////////////////////////
124 * Create a new TextView.
125 * @return A smart-pointer to the newly allocated TextView.
127 static Toolkit::TextView New();
130 * @copydoc SetText( const std::string& text )
132 void SetText( const std::string& text );
135 * @copydoc SetText( const StyledTextArray& text )
137 void SetText( const MarkupProcessor::StyledTextArray& text );
140 * @copydoc InsertTextAt( std::size_t position, const std::string& text )
142 void InsertTextAt( std::size_t position, const std::string& text );
145 * @copydoc InsertTextAt( std::size_t position, const std::string& text )
147 void InsertTextAt( std::size_t position, const MarkupProcessor::StyledTextArray& text );
150 * @copydoc RemoveTextFrom( std::size_t position, std::size_t numberOfCharacters )
152 void RemoveTextFrom( std::size_t position, std::size_t numberOfCharacters );
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 std::string& text );
160 * @copydoc ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const std::string& text )
162 void ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const MarkupProcessor::StyledTextArray& text );
167 std::string GetText() const;
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 SetEllipsizeText()
257 void SetEllipsizeText( const Text& ellipsizeText, const Vector<TextStyle*>& ellipsizeStyles );
260 * @copydoc GetEllipsizeText()
262 std::string GetEllipsizeText() const;
265 * Checks if relayout the text is needed. If it is, it relais out the text
266 * by calling DoRelayOut().
268 void GetTextLayoutInfo();
271 * Calls GetTextLayoutInfo() and fills the given data structure.
273 * @see GetTextLayoutInfo()
275 void GetTextLayoutInfo( Toolkit::TextView::TextLayoutInfo& textLayoutInfo );
278 * @copydoc SetSortModifier()
280 void SetSortModifier( float depthOffset );
283 * @copydoc SetSnapshotModeEnabled()
285 void SetSnapshotModeEnabled( bool enable );
288 * @copydoc IsSnapshotModeEnabled()
290 bool IsSnapshotModeEnabled() const;
293 * @brief Sets whether markup processing should be carried out.
295 * @param[in] enable whether markup processing is carried out or not.
297 void SetMarkupProcessingEnabled( bool enable );
300 * @brief Returns whether markup processing is enabled or not
302 * @return true is markup processing is enabled
304 bool IsMarkupProcessingEnabled() const;
307 * @copydoc SetScrollEnabled()
309 void SetScrollEnabled( bool enable );
312 * @copydoc IsScrollEnabled()
314 bool IsScrollEnabled() const;
317 * @copydoc SetScrollPosition()
318 * @see DoSetScrollPosition()
320 void SetScrollPosition( const Vector2& position );
323 * @copydoc GetScrollPosition()
325 const Vector2& GetScrollPosition() const;
328 * @copydoc IsScrollPositionTrimmed()
330 bool IsScrollPositionTrimmed() const;
333 * @copydoc ScrolledSignal()
335 Toolkit::TextView::ScrolledSignalV2& ScrolledSignal();
338 * Connects a callback function with the object's signals.
339 * @param[in] object The object providing the signal.
340 * @param[in] tracker Used to disconnect the signal.
341 * @param[in] signalName The signal to connect to.
342 * @param[in] functor A newly allocated FunctorDelegate.
343 * @return True if the signal was connected.
344 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
346 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
351 * Called when a property of an object of this type is set.
352 * @param[in] object The object whose property is set.
353 * @param[in] index The property index.
354 * @param[in] value The new property value.
356 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
359 * Called to retrieve a property of an object of this type.
360 * @param[in] object The object whose property is to be retrieved.
361 * @param[in] index The property index.
362 * @return The current value of the property.
364 static Property::Value GetProperty( BaseObject* object, Property::Index index );
367 private: // From Control
370 * @copydoc Toolkit::Control::OnInitialize()
372 virtual void OnInitialize();
375 * @copydoc Toolkit::Control::OnFontChange( )
377 virtual void OnFontChange( bool defaultFontChange, bool defaultFontSizeChange );
380 * @copydoc Toolkit::Control::OnControlSizeSet()
382 virtual void OnControlSizeSet( const Vector3& size );
385 * @copydoc Toolkit::Control::OnRelaidOut()
387 * Removes text-actor and calls DoRelayOut()..
389 virtual void OnRelaidOut( Vector2 size, ActorSizeContainer& container );
392 * Retrieves the text-view's natural size.
394 * @return The natural size.
396 virtual Vector3 GetNaturalSize();
399 * Retrieves the text-view's \e height for a given \e width.
401 * @param[in] width The given \e width.
403 * @return The \e height for the given \e width.
405 virtual float GetHeightForWidth( float width );
408 * Retrieves the text-view's \e width for a given \e height.
410 * @param[in] height The given \e height.
412 * @return The \e width for the given \e height.
414 virtual float GetWidthForHeight( float height );
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, etc.
439 void PerformTextViewProcessorOperations();
442 * Optimizes some text-view processor operations.
444 void OptimizeTextViewProcessorOperations();
447 * Synchronously relays-out all text-actors.
449 * Perform text-view-processor operations, sets the new size and position of text-actors and adds them to the text-view.
451 * @param[in] textViewSize The new text-view's size.
452 * @param[in] relayoutOperationMask Defines which operations need to be done in the relayout process.
454 void DoRelayOut( const Size& textViewSize, RelayoutOperationMask relayoutOperationMask );
457 * Process Snapshot. It refresh the render-task in order to generate a new snapshot image.
459 * ProcessSnapshot is called from OnRelaidOut() only if text has been relaid out.
460 * It creates a new image buffer only if the size of the text has changed.
462 * @param[in] textViewSize The new text-view's size.
464 void ProcessSnapshot( const Size& textViewSize );
467 * Aligns the offscreen rendering camera actor to cover the whole text and the resulting image actor accordingly with the text view's alignment.
469 * @param[in] textViewSize The text view's size.
470 * @param[in] offscreenSize The offscreen's frame buffer's size.
472 void AlignOffscreenCameraActor( const Size& textViewSize, const Size& offscreenSize );
475 * Callback called when the render task has processed.
477 * It makes the root actor invisible.
479 * @param[in] renderTask The processed render task.
481 void RenderTaskFinished( Dali::RenderTask& renderTask );
484 * Destroys offscreen rendering resources.
486 * It disconects the render task finished signal from the TextView::RenderTaskFinished() method,
487 * removes the render task from the render task list and resets the offscreen camera actor, root actor,
488 * image actor and the frame buffer.
490 void DestroyOffscreenRenderingResources();
493 * Called when text-view is scrolled.
495 * Sets the new scroll position. @see DoSetScrollPosition()
497 * @param[in] actor Handle of the text-view.
498 * @param[in] gesture Data structure with the parameters of the gesture.
500 void OnTextPan( Actor actor, const PanGesture& gesture );
503 * Ensures the text-view's boundaries are fully covered of text.
505 * i.e. if the text-view's size is 100x100 and the text's size is 150x100, the scroll position
506 * can be in the range -50,0 and 50,0.
508 void TrimScrollPosition();
511 * Called from SetScrollPosition() and OnTextPan()
513 * Updates the stored scroll position ensuring the text-view is always covered with text by calling
514 * TrimScrollPosition(), calculates the difference with the previous one and emits the Toolkit::TextView::SignalScrolled() signal.
516 * @param[in] position The new scroll position.
518 void DoSetScrollPosition( const Vector2& position );
521 * Combines width and height exceed policies.
523 * This method is a big switch() which combines two exceed policies into one.
524 * The aim is avoid this switch inside the relayout code.
526 * i.e. Width policy = Split. Height policy = Original. Internally the policy is SplitOriginal.
528 void CombineExceedPolicies();
531 * Retrieves the text-view's root actor which stores all text-actors.
532 * It could be the text-view itself or an actor used in the snapshot mode.
534 * @return the root actor.
536 Actor GetRootActor() const;
539 * Creates the ellipsize text layout.
541 void CreateEllipsizeLayout();
544 * Handle SetProperty for markup processing.
545 * @param[in] propertyValue The new property value.
547 void OnMarkupEnabledPeopertySet( Property::Value propertyValue );
550 * Handles SetProperty for multiline policy.
551 * @param[in] propertyValue The new property value.
553 void OnMultilinePolicyPropertySet( Property::Value propertyValue );
556 * Handles SetProperty for width exceed policy.
557 * @param[in] propertyValue The new property value.
559 void OnWidthExceedPolicyPropertySet( Property::Value propertyValue );
562 * Handles SetProperty for height exceed policy.
563 * @param[in] propertyValue The new property value.
565 void OnHeightExceedPolicyPropertySet( Property::Value propertyValue );
568 * Handles SetProperty for line justification.
569 * @param[in] propertyValue The new property value.
571 void OnLineJustificationPropertySet( Property::Value propertyValue );
574 * Handles SetProperty for fade boundary.
575 * @param[in] propertyValue The new property value.
577 void OnFadeBoundaryPropertySet( Property::Value propertyValue );
580 * Handles SetProperty for alignment property.
581 * @param[in] propertyIndex The property index.
582 * @param[in] propertyValue The new property value.
584 void OnAlignmentPropertySet( Property::Index propertyIndex, Property::Value propertyValue );
587 * Handles GetProperty for horizontal alignment property.
588 * @return The property value of horizontal alignment.
590 std::string OnHorizontalAlignmentPropertyGet();
593 * Handles GetProperty for vertical alignment property.
594 * @return The property value of vertical alignment.
596 std::string OnVerticalAlignmentPropertyGet();
601 * The parameters which affects the layout of the text.
603 struct LayoutParameters
606 * Default constructor.
611 * Default destructor.
618 LayoutParameters( Toolkit::TextView::MultilinePolicy multilinePolicy,
619 Toolkit::TextView::ExceedPolicy widthExceedPolicy,
620 Toolkit::TextView::ExceedPolicy heightExceedPolicy,
621 Toolkit::Alignment::Type alignment,
622 Toolkit::TextView::LineJustification lineJustification,
623 float lineHeightOffset,
624 bool markUpEnabled );
629 LayoutParameters( const LayoutParameters& layoutParameters );
632 * Assignment operator.
634 LayoutParameters& operator=( const LayoutParameters& layoutParameters );
636 Toolkit::TextView::MultilinePolicy mMultilinePolicy; ///< Stores the multiline policy.
637 TextView::ExceedPolicy mExceedPolicy; ///< Stores a combination of both policies.
638 Toolkit::TextView::ExceedPolicy mWidthExceedPolicy; ///< Stores the text width exceed policy.
639 Toolkit::TextView::ExceedPolicy mHeightExceedPolicy; ///< Stores the text height exceed policy.
640 Toolkit::Alignment::Type mHorizontalAlignment; ///< Stores the horizontal alignment for the whole text.
641 Toolkit::Alignment::Type mVerticalAlignment; ///< Stores the vertical alignment for the whole text.
642 Toolkit::TextView::LineJustification mLineJustification; ///< Stores the line justification.
643 float mLineHeightOffset; ///< Line height offset to be addded to the font line height (measured in PointSize).
644 bool mMarkUpEnabled:1; ///< Is markup string scanning enabled.
648 * Some parameters which affects the text view visualization.
650 struct VisualParameters
653 * Default constructor.
660 VisualParameters( const VisualParameters& visualParameters );
663 * Assignment operator.
665 VisualParameters& operator=( const VisualParameters& visualParameters );
667 Toolkit::TextView::FadeBoundary mFadeBoundary; ///< Fade boundary used in fade mode.
668 float mSortModifier; ///< Stores the sort modifier for all text-actors.
669 Vector2 mCameraScrollPosition; ///< The scroll offset.
670 bool mSnapshotModeEnabled:1; ///< Whether text-view is rendered offscreen.
671 bool mScrollEnabled:1; ///< Whether the text scroll is enabled.
672 bool mScrollPositionTrimmed:1; ///< Whether the last scroll position set was trimmed.
676 * The results of the relayout process.
681 * Default constructor.
688 RelayoutData( const RelayoutData& relayoutData );
691 * Assignment operator.
693 RelayoutData& operator=( const RelayoutData& relayoutData );
695 Size mTextViewSize; ///< The text-view's size used to relaid-out the text.
696 float mShrinkFactor; ///< Shrink factor used when the exceed policy contains ShrinkToFit.
697 TextViewProcessor::TextLayoutInfo mTextLayoutInfo; ///< Stores metrics, layout info (size, direction, type of word) and text-actor info for the whole text.
698 std::vector<int> mCharacterLogicalToVisualMap; ///< Reorder map that stores each character's visual (output) index according to its logical (input) index
699 std::vector<int> mCharacterVisualToLogicalMap; ///< Reorder map that stores each character's logical (input) index according to its visual (output) index
700 std::vector<RenderableActor> mGlyphActors; ///< Stores handles of those text-actors which are currently added to the text-view.
701 std::vector<RenderableActor> mEllipsizedGlyphActors; ///< Stores handles of those text-actors which are used to ellipsize the text.
702 Toolkit::TextView::CharacterLayoutInfoContainer mCharacterLayoutInfoTable; ///< Stores layout info per character sorted by the character's visual index.
703 Toolkit::TextView::LineLayoutInfoContainer mLines; ///< Stores an index to the first character of each line.
704 Size mTextSizeForRelayoutOption; ///< Stores the text size after relayout.
705 TextActorCache mTextActorCache; ///< Stores previously created text-actors to be reused.
710 MarkupProcessor::StyledTextArray mCurrentStyledText; ///< text currently displayed by the view
711 std::vector<TextViewProcessorMetadata> mTextViewProcessorOperations; ///< Stores all relayout operations which arrive between two consecutive OnRelaidOut() calls.
713 LayoutParameters mLayoutParameters; ///< Stores some layout parameters in a struct. To be passed in layout functions.
714 VisualParameters mVisualParameters; ///< Some parameters which afects text-view visualization.
715 RelayoutData mRelayoutData; ///< struct with text-view's data structures used to pass all of them in one parameter.
716 RelayoutOperationMask mRelayoutOperations; ///< Which relayout operations have to be done.
718 Layer mOffscreenRootActor; ///< Root actor for offscreen rendering.
719 ImageActor mOffscreenImageActor; ///< Image actor for offscreen rendering.
720 CameraActor mOffscreenCameraActor; ///< Camera actor for offscreen rendering.
721 Size mCurrentOffscreenSize; ///< Current used ofscreen size.
722 FrameBufferImage mFrameBufferImage; ///< Frame buffer used for offscreen rendering.
723 RenderTask mRenderTask; ///< Used to generate an offscreen rendering.
725 PanGestureDetector mPanGestureDetector; ///< Pan gesture for text scrolling.
728 * Helper class used to prevent the modification of some members.
746 bool mLockPreviousSnapshotMode; ///< Whether previous stored snapshot mode should be modified.
747 bool mPreviousSnapshotModeEnabled:1; ///< Stores the previous snapshot mode value.
748 bool mMarkUpEnabled:1; ///< enable to scan for mark-up
750 Toolkit::TextView::ScrolledSignalV2 mScrolledSignalV2; ///< Signal emitted when text is scrolled.
753 } // namespace Internal
755 // Helpers for public-api forwarding methods
757 inline Internal::TextView& GetImpl( TextView& textView )
759 DALI_ASSERT_ALWAYS( textView );
761 RefObject& handle = textView.GetImplementation();
763 return static_cast< Internal::TextView& >( handle );
766 inline const Internal::TextView& GetImpl( const TextView& textView )
768 DALI_ASSERT_ALWAYS( textView );
770 const RefObject& handle = textView.GetImplementation();
772 return static_cast< const Internal::TextView& >( handle );
775 } // namespace Toolkit
779 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__