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/common/dali-vector.h>
24 #include <dali/public-api/actors/text-actor.h>
25 #include <dali-toolkit/public-api/controls/control-impl.h>
26 #include <dali-toolkit/public-api/controls/text-view/text-view.h>
27 #include "text-actor-cache.h"
28 #include "text-view-processor-types.h"
40 * TextView is a custom control for text aligning and multiline support
42 class TextView : public ControlImpl
49 TEXTVIEW_PROPERTY_START_INDEX = ControlImpl::CONTROL_PROPERTY_END_INDEX + 1,
50 TEXTVIEW_PROPERTY_END_INDEX = TEXTVIEW_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices
54 * Internal exceed policy with the valid combinations.
58 Original, ///< Original size (even if it exceeds the witdh or the height).
59 OriginalFade, ///< Original size if it exceeds the width but faded if it exceeds the height.
60 OriginalShrink, ///< Shrunk if it exceeds the height.
61 SplitOriginal, ///< Split if it exceeds the width but no action if it exceeds the height.
62 SplitFade, ///< Split if it exceeds the width and faded if it exceeds the height.
63 SplitShrink, ///< Split if it exceeds the width and shrunk 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 SetFont( const Font newFont )
168 void SetFont( const Font newFont );
171 * @copydoc SetLineHeightOffset()
173 void SetLineHeightOffset( PointSize offset );
176 * @copydoc GetLineHeightOffset()
178 PointSize GetLineHeightOffset() const;
181 * @copydoc SetStyleToCurrentText()
183 void SetStyleToCurrentText( const TextStyle& style, TextStyle::Mask mask );
186 * @copydoc SetTextAlignment( Toolkit::Alignment::Type align )
188 void SetTextAlignment( Toolkit::Alignment::Type align );
191 * @copydoc GetTextAlignment()
193 Toolkit::Alignment::Type GetTextAlignment() const;
196 * @copydoc SetMultilinePolicy()
198 void SetMultilinePolicy( Toolkit::TextView::MultilinePolicy policy );
201 * @copydoc GetMultilinePolicy()
203 Toolkit::TextView::MultilinePolicy GetMultilinePolicy() const;
206 * @copydoc SetWidthExceedPolicy()
208 void SetWidthExceedPolicy( Toolkit::TextView::ExceedPolicy policy );
211 * @copydoc GetWidthExceedPolicy()
213 Toolkit::TextView::ExceedPolicy GetWidthExceedPolicy() const;
216 * @copydoc SetHeightExceedPolicy()
218 void SetHeightExceedPolicy( Toolkit::TextView::ExceedPolicy policy );
221 * @copydoc GetHeightExceedPolicy()
223 Toolkit::TextView::ExceedPolicy GetHeightExceedPolicy() const;
226 * @copydoc SetLineJustification()
228 void SetLineJustification( Toolkit::TextView::LineJustification justification );
231 * @copydoc GetLineJustification()
233 Toolkit::TextView::LineJustification GetLineJustification() const;
236 * @copydoc SetFadeBoundary()
238 void SetFadeBoundary( const Toolkit::TextView::FadeBoundary& fadeBoundary );
241 * @copydoc GetFadeBoundary()
243 const Toolkit::TextView::FadeBoundary& GetFadeBoundary() const;
246 * @copydoc SetEllipsizeText()
248 void SetEllipsizeText( const std::string& ellipsizeText );
251 * @copydoc SetEllipsizeText()
253 void SetEllipsizeText( const MarkupProcessor::StyledTextArray& ellipsizeText );
256 * @copydoc GetEllipsizeText()
258 std::string GetEllipsizeText() const;
261 * Checks if relayout the text is needed. If it is, it relais out the text
262 * by calling DoRelayOut().
264 void GetTextLayoutInfo();
267 * Calls GetTextLayoutInfo() and fills the given data structure.
269 * @see GetTextLayoutInfo()
271 void GetTextLayoutInfo( Toolkit::TextView::TextLayoutInfo& textLayoutInfo );
274 * @copydoc SetSortModifier()
276 void SetSortModifier( float depthOffset );
279 * @copydoc SetSnapshotModeEnabled()
281 void SetSnapshotModeEnabled( bool enable );
284 * @copydoc IsSnapshotModeEnabled()
286 bool IsSnapshotModeEnabled() const;
289 * @brief Sets whether markup processing should be carried out.
291 * @param[in] enable whether markup processing is carried out or not.
293 void SetMarkupProcessingEnabled( bool enable );
296 * @brief Returns whether markup processing is enabled or not
298 * @return true is markup processing is enabled
300 bool IsMarkupProcessingEnabled() const;
303 * @copydoc SetScrollEnabled()
305 void SetScrollEnabled( bool enable );
308 * @copydoc IsScrollEnabled()
310 bool IsScrollEnabled() const;
313 * @copydoc SetScrollPosition()
314 * @see DoSetScrollPosition()
316 void SetScrollPosition( const Vector2& position );
319 * @copydoc GetScrollPosition()
321 const Vector2& GetScrollPosition() const;
324 * @copydoc IsScrollPositionTrimmed()
326 bool IsScrollPositionTrimmed() const;
329 * @copydoc ScrolledSignal()
331 Toolkit::TextView::ScrolledSignalV2& ScrolledSignal();
334 * Connects a callback function with the object's signals.
335 * @param[in] object The object providing the signal.
336 * @param[in] tracker Used to disconnect the signal.
337 * @param[in] signalName The signal to connect to.
338 * @param[in] functor A newly allocated FunctorDelegate.
339 * @return True if the signal was connected.
340 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
342 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
347 * Called when a property of an object of this type is set.
348 * @param[in] object The object whose property is set.
349 * @param[in] index The property index.
350 * @param[in] value The new property value.
352 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
355 * Called to retrieve a property of an object of this type.
356 * @param[in] object The object whose property is to be retrieved.
357 * @param[in] index The property index.
358 * @return The current value of the property.
360 static Property::Value GetProperty( BaseObject* object, Property::Index index );
363 private: // From ControlImpl
366 * @copydoc Toolkit::Control::OnInitialize()
368 virtual void OnInitialize();
371 * @copydoc Toolkit::Control::OnStyleChange()
373 virtual void OnStyleChange( StyleChange change );
376 * @copydoc Toolkit::Control::OnControlSizeSet()
378 virtual void OnControlSizeSet( const Vector3& size );
381 * @copydoc Toolkit::Control::OnRelaidOut()
383 * Removes text-actor and calls DoRelayOut()..
385 virtual void OnRelaidOut( Vector2 size, ActorSizeContainer& container );
388 * Retrieves the text-view's natural size.
390 * @return The natural size.
392 virtual Vector3 GetNaturalSize();
395 * Retrieves the text-view's \e height for a given \e width.
397 * @param[in] width The given \e width.
399 * @return The \e height for the given \e width.
401 virtual float GetHeightForWidth( float width );
404 * Retrieves the text-view's \e width for a given \e height.
406 * @param[in] height The given \e height.
408 * @return The \e width for the given \e height.
410 virtual float GetWidthForHeight( float height );
415 * Construct a new TextView.
420 * A reference counted object may only be deleted by calling Unreference()
427 TextView( const TextView& );
430 TextView& operator=( const TextView& rhs );
433 * Executes synchronously relayout operations such as set, insert, remove or replace text, or
434 * split groups of characters, etc.
436 void PerformTextViewProcessorOperations();
439 * Optimizes some text-view processor operations.
441 void OptimizeTextViewProcessorOperations();
444 * Synchronously relays-out all text-actors.
446 * Perform text-view-processor operations, sets the new size and position of text-actors and adds them to the text-view.
448 * @param[in] textViewSize The new text-view's size.
449 * @param[in] relayoutOperationMask Defines which operations need to be done in the relayout process.
451 void DoRelayOut( const Size& textViewSize, RelayoutOperationMask relayoutOperationMask );
454 * Process Snapshot. It refresh the render-task in order to generate a new snapshot image.
456 * ProcessSnapshot is called from OnRelaidOut() only if text has been relaid out.
457 * It creates a new image buffer only if the size of the text has changed.
459 * @param[in] textViewSize The new text-view's size.
461 void ProcessSnapshot( const Size& textViewSize );
464 * Aligns the offscreen rendering camera actor to cover the whole text and the resulting image actor accordingly with the text view's alignment.
466 * @param[in] textViewSize The text view's size.
467 * @param[in] offscreenSize The offscreen's frame buffer's size.
469 void AlignOffscreenCameraActor( const Size& textViewSize, const Size& offscreenSize );
472 * Callback called when the render task has processed.
474 * It makes the root actor invisible.
476 * @param[in] renderTask The processed render task.
478 void RenderTaskFinished( Dali::RenderTask& renderTask );
481 * Destroys offscreen rendering resources.
483 * It disconects the render task finished signal from the TextView::RenderTaskFinished() method,
484 * removes the render task from the render task list and resets the offscreen camera actor, root actor,
485 * image actor and the frame buffer.
487 void DestroyOffscreenRenderingResources();
490 * Called when text-view is scrolled.
492 * Sets the new scroll position. @see DoSetScrollPosition()
494 * @param[in] actor Handle of the text-view.
495 * @param[in] gesture Data structure with the parameters of the gesture.
497 void OnTextPan( Actor actor, PanGesture gesture );
500 * Ensures the text-view's boundaries are fully covered of text.
502 * i.e. if the text-view's size is 100x100 and the text's size is 150x100, the scroll position
503 * can be in the range -50,0 and 50,0.
505 void TrimScrollPosition();
508 * Called from SetScrollPosition() and OnTextPan()
510 * Updates the stored scroll position ensuring the text-view is always covered with text by calling
511 * TrimScrollPosition(), calculates the difference with the previous one and emits the Toolkit::TextView::SignalScrolled() signal.
513 * @param[in] position The new scroll position.
515 void DoSetScrollPosition( const Vector2& position );
518 * Combines width and height exceed policies.
520 * This method is a big switch() which combines two exceed policies into one.
521 * The aim is avoid this switch inside the relayout code.
523 * i.e. Width policy = Split. Height policy = Original. Internally the policy is SplitOriginal.
525 void CombineExceedPolicies();
528 * Retrieves the text-view's root actor which stores all text-actors.
529 * It could be the text-view itself or an actor used in the snapshot mode.
531 * @return the root actor.
533 Actor GetRootActor() const;
536 * Handle SetProperty for markup processing.
537 * @param[in] propertyValue The new property value.
539 void OnMarkupEnabledPeopertySet( Property::Value propertyValue );
542 * Handles SetProperty for multiline policy.
543 * @param[in] propertyValue The new property value.
545 void OnMultilinePolicyPropertySet( Property::Value propertyValue );
548 * Handles SetProperty for width exceed policy.
549 * @param[in] propertyValue The new property value.
551 void OnWidthExceedPolicyPropertySet( Property::Value propertyValue );
554 * Handles SetProperty for height exceed policy.
555 * @param[in] propertyValue The new property value.
557 void OnHeightExceedPolicyPropertySet( Property::Value propertyValue );
560 * Handles SetProperty for line justification.
561 * @param[in] propertyValue The new property value.
563 void OnLineJustificationPropertySet( Property::Value propertyValue );
566 * Handles SetProperty for fade boundary.
567 * @param[in] propertyValue The new property value.
569 void OnFadeBoundaryPropertySet( Property::Value propertyValue );
572 * Handles SetProperty for alignment property.
573 * @param[in] propertyIndex The property index.
574 * @param[in] propertyValue The new property value.
576 void OnAlignmentPropertySet( Property::Index propertyIndex, Property::Value propertyValue );
579 * Handles GetProperty for horizontal alignment property.
580 * @return The property value of horizontal alignment.
582 std::string OnHorizontalAlignmentPropertyGet();
585 * Handles GetProperty for vertical alignment property.
586 * @return The property value of vertical alignment.
588 std::string OnVerticalAlignmentPropertyGet();
593 * The parameters which affects the layout of the text.
595 struct LayoutParameters
598 * Default constructor.
605 LayoutParameters( Toolkit::TextView::MultilinePolicy multilinePolicy,
606 Toolkit::TextView::ExceedPolicy widthExceedPolicy,
607 Toolkit::TextView::ExceedPolicy heightExceedPolicy,
608 Toolkit::Alignment::Type alignment,
609 Toolkit::TextView::LineJustification lineJustification,
610 float lineHeightOffset,
611 const std::string& ellipsizeText,
612 const bool markUpEnabled );
617 LayoutParameters( const LayoutParameters& layoutParameters );
620 * Assignment operator.
622 LayoutParameters& operator=( const LayoutParameters& layoutParameters );
624 Toolkit::TextView::MultilinePolicy mMultilinePolicy; ///< Stores the multiline policy.
625 TextView::ExceedPolicy mExceedPolicy; ///< Stores a combination of both policies;
626 Toolkit::TextView::ExceedPolicy mWidthExceedPolicy; ///< Stores the text width exceed policy.
627 Toolkit::TextView::ExceedPolicy mHeightExceedPolicy; ///< Stores the text height exceed policy.
628 Toolkit::Alignment::Type mHorizontalAlignment; ///< Stores the horizontal alignment for the whole text.
629 Toolkit::Alignment::Type mVerticalAlignment; ///< Stores the vertical alignment for the whole text.
630 Toolkit::TextView::LineJustification mLineJustification; ///< Stores the line justification.
631 float mLineHeightOffset; ///< Line height offset to be addded to the font line height (measured in PointSize).
632 MarkupProcessor::StyledTextArray mEllipsizeText; ///< Stores the ellipsize text
633 bool mMarkUpEnabled:1; ///< Is markup string scanning enabled
637 * Some parameters which affects the text view visualization.
639 struct VisualParameters
642 * Default constructor.
649 VisualParameters( const VisualParameters& visualParameters );
652 * Assignment operator.
654 VisualParameters& operator=( const VisualParameters& visualParameters );
656 Toolkit::TextView::FadeBoundary mFadeBoundary; ///< Fade boundary used in fade mode.
657 float mSortModifier; ///< Stores the sort modifier for all text-actors.
658 Vector2 mCameraScrollPosition; ///< The scroll offset.
659 bool mSnapshotModeEnabled:1; ///< Whether text-view is rendered offscreen.
660 bool mScrollEnabled:1; ///< Whether the text scroll is enabled.
661 bool mScrollPositionTrimmed:1; ///< Whether the last scroll position set was trimmed.
665 * Temporary data used to calculate line justification.
667 struct LineJustificationInfo
669 TextViewProcessor::TextInfoIndices mIndices; ///< Indices to the first character of the new line.
670 float mLineLength; ///< Length of the line (or portion of line).
674 * The results of the relayout process.
679 * Default constructor.
686 RelayoutData( const RelayoutData& relayoutData );
689 * Assignment operator.
691 RelayoutData& operator=( const RelayoutData& relayoutData );
693 Size mTextViewSize; ///< The text-view's size used to relaid-out the text.
694 float mShrinkFactor; ///< Shrink factor used when the exceed policy contains ShrinkToFit.
695 TextViewProcessor::TextLayoutInfo mTextLayoutInfo; ///< Stores metrics, layout info (size, direction, type of word) and text-actor info for the whole text.
696 std::vector<int> mCharacterLogicalToVisualMap; ///< Reorder map that stores each character's visual (output) index according to its logical (input) index
697 std::vector<int> mCharacterVisualToLogicalMap; ///< Reorder map that stores each character's logical (input) index according to its visual (output) index
698 std::vector<RenderableActor> mGlyphActors; ///< Stores handles of those text-actors which are currently added to the text-view.
699 std::vector<RenderableActor> mEllipsizedGlyphActors; ///< Stores handles of those text-actors which are used to ellipsize the text.
700 Toolkit::TextView::CharacterLayoutInfoContainer mCharacterLayoutInfoTable; ///< Stores layout info per character sorted by the character's visual index.
701 Toolkit::TextView::LineLayoutInfoContainer mLines; ///< Stores an index to the first character of each line.
702 Size mTextSizeForRelayoutOption; ///< Stores the text size after relayout.
703 std::vector<LineJustificationInfo> mLineJustificationInfo; ///< Stores justification info per line.
704 TextActorCache mTextActorCache; ///< Stores previously created text-actors to be reused.
709 MarkupProcessor::StyledTextArray mCurrentStyledText; ///< text currently displayed by the view
710 std::vector<TextViewProcessorMetadata> mTextViewProcessorOperations; ///< Stores all relayout operations which arrive between two consecutive OnRelaidOut() calls.
712 LayoutParameters mLayoutParameters; ///< Stores some layout parameters in a struct. To be passed in layout functions.
713 VisualParameters mVisualParameters; ///< Some parameters which afects text-view visualization.
714 RelayoutData mRelayoutData; ///< struct with text-view's data structures used to pass all of them in one parameter.
715 RelayoutOperationMask mRelayoutOperations; ///< Which relayout operations have to be done.
717 Layer mOffscreenRootActor; ///< Root actor for offscreen rendering.
718 ImageActor mOffscreenImageActor; ///< Image actor for offscreen rendering.
719 CameraActor mOffscreenCameraActor; ///< Camera actor for offscreen rendering.
720 Size mCurrentOffscreenSize; ///< Current used ofscreen size.
721 FrameBufferImage mFrameBufferImage; ///< Frame buffer used for offscreen rendering.
722 RenderTask mRenderTask; ///< Used to generate an offscreen rendering.
724 PanGestureDetector mPanGestureDetector; ///< Pan gesture for text scrolling.
727 * Helper class used to prevent the modification of some members.
745 bool mLockPreviousSnapshotMode; ///< Whether previous stored snapshot mode should be modified.
746 bool mPreviousSnapshotModeEnabled:1; ///< Stores the previous snapshot mode value.
747 bool mMarkUpEnabled:1; ///< enable to scan for mark-up
749 Toolkit::TextView::ScrolledSignalV2 mScrolledSignalV2; ///< Signal emitted when text is scrolled.
752 } // namespace Internal
754 // Helpers for public-api forwarding methods
756 inline Internal::TextView& GetImpl( TextView& textView )
758 DALI_ASSERT_ALWAYS( textView );
760 RefObject& handle = textView.GetImplementation();
762 return static_cast< Internal::TextView& >( handle );
765 inline const Internal::TextView& GetImpl( const TextView& textView )
767 DALI_ASSERT_ALWAYS( textView );
769 const RefObject& handle = textView.GetImplementation();
771 return static_cast< const Internal::TextView& >( handle );
774 } // namespace Toolkit
778 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__