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 Control
49 TEXTVIEW_PROPERTY_START_INDEX = Control::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 SplitEllipsizeEnd, ///< Split if it exceeds the width and ellipsize if it exceeds the height.
65 Fade, ///< Faded if it exceeds any boundary.
66 FadeOriginal, ///< Faded if it exceeds the width but no action if it exceeds the height.
67 ShrinkOriginal, ///< Shrunk if it exceeds the width but no action if it exceeds the height.
68 ShrinkFade, ///< Shrunk if it exceeds the width and faded if it exceeds the height.
69 Shrink, ///< Shrunk if it exceeds any boundary.
70 EllipsizeEndOriginal, ///< Ellipsized by the end if it exceeds the width but no action if it exceeds the height.
71 EllipsizeEnd ///< Ellipsized by the end if it exceeds the width and/or the height.
74 // Between two OnRelaidOut methods, several calls to InsertTextAt, RemoveTextFrom or SetText can happen.
75 // TextViewProcessorMetadata stores the type of operation. A vector stores all operations between two OnRelaidOut calls.
77 enum TextViewProcessorMetadataType
79 TextSet, ///< Sets new text.
80 TextInserted, ///< Inserts text into current text.
81 TextReplaced, ///< Replaces some text from current text.
82 TextRemoved, ///< Removes some text from current text.
83 NewLineHeight, ///< Sets a new line height offset.
84 NewStyle ///< Sets a new style to the whole text.
88 * Stores info about which data structures need to be modified when the OnRelaidOut() method is called
90 struct TextViewProcessorMetadata
92 TextViewProcessorMetadata();
94 TextViewProcessorMetadataType mType; ///< Stores the type of operation.
95 std::size_t mPosition; ///< Character position within the text.
96 std::size_t mNumberOfCharacters; ///< Number of characters to be removed/ replaced.
97 MarkupProcessor::StyledTextArray mText; ///< The new text.
98 TextStyle::Mask mStyleMask; ///< The style mask.
102 * Defines which operations have to be done in the relayout process.
104 enum RelayoutOperationMask
106 NO_RELAYOUT = 0x0, ///< Does nothing.
107 RELAYOUT_REMOVE_TEXT_ACTORS = 0x1, ///< Removes current text-actors from the text-view.
108 RELAYOUT_SIZE_POSITION = 0x2, ///< Calculates size and position of the text but it doesn't calculate alignment.
109 RELAYOUT_ALIGNMENT = 0x4, ///< Aligns the whole text.
110 RELAYOUT_VISIBILITY = 0x8, ///< Calculates the visibility.
111 RELAYOUT_INITIALIZE_TEXT_ACTORS = 0x10, ///< Initialize text-actors (create handles).
112 RELAYOUT_TEXT_ACTOR_UPDATE = 0x20, ///< Updates text-actors (set size, position, style, ...)
113 RELAYOUT_INSERT_TO_TEXT_VIEW = 0x40, ///< Adds the text-actors to the text-view.
114 RELAYOUT_INSERT_TO_TEXT_ACTOR_LIST = 0x80, ///< Inserts the text-actors to the text-actor list.
115 RELAYOUT_ALL = 0xFF ///< Does all operations.
118 //////////////////////////////////////////////
121 * Create a new TextView.
122 * @return A smart-pointer to the newly allocated TextView.
124 static Toolkit::TextView New();
127 * @copydoc SetText( const std::string& text )
129 void SetText( const std::string& text );
132 * @copydoc SetText( const StyledTextArray& text )
134 void SetText( const MarkupProcessor::StyledTextArray& text );
137 * @copydoc InsertTextAt( std::size_t position, const std::string& text )
139 void InsertTextAt( std::size_t position, const std::string& text );
142 * @copydoc InsertTextAt( std::size_t position, const std::string& text )
144 void InsertTextAt( std::size_t position, const MarkupProcessor::StyledTextArray& text );
147 * @copydoc RemoveTextFrom( std::size_t position, std::size_t numberOfCharacters )
149 void RemoveTextFrom( std::size_t position, std::size_t numberOfCharacters );
152 * @copydoc ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const std::string& text )
154 void ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const std::string& text );
157 * @copydoc ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const std::string& text )
159 void ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const MarkupProcessor::StyledTextArray& text );
164 std::string GetText() const;
167 * @copydoc SetLineHeightOffset()
169 void SetLineHeightOffset( PointSize offset );
172 * @copydoc GetLineHeightOffset()
174 PointSize GetLineHeightOffset() const;
177 * @copydoc SetStyleToCurrentText()
179 void SetStyleToCurrentText( const TextStyle& style, TextStyle::Mask mask );
182 * @copydoc SetTextAlignment( Toolkit::Alignment::Type align )
184 void SetTextAlignment( Toolkit::Alignment::Type align );
187 * @copydoc GetTextAlignment()
189 Toolkit::Alignment::Type GetTextAlignment() const;
192 * @copydoc SetMultilinePolicy()
194 void SetMultilinePolicy( Toolkit::TextView::MultilinePolicy policy );
197 * @copydoc GetMultilinePolicy()
199 Toolkit::TextView::MultilinePolicy GetMultilinePolicy() const;
202 * @copydoc SetWidthExceedPolicy()
204 void SetWidthExceedPolicy( Toolkit::TextView::ExceedPolicy policy );
207 * @copydoc GetWidthExceedPolicy()
209 Toolkit::TextView::ExceedPolicy GetWidthExceedPolicy() const;
212 * @copydoc SetHeightExceedPolicy()
214 void SetHeightExceedPolicy( Toolkit::TextView::ExceedPolicy policy );
217 * @copydoc GetHeightExceedPolicy()
219 Toolkit::TextView::ExceedPolicy GetHeightExceedPolicy() const;
222 * @copydoc SetLineJustification()
224 void SetLineJustification( Toolkit::TextView::LineJustification justification );
227 * @copydoc GetLineJustification()
229 Toolkit::TextView::LineJustification GetLineJustification() const;
232 * @copydoc SetFadeBoundary()
234 void SetFadeBoundary( const Toolkit::TextView::FadeBoundary& fadeBoundary );
237 * @copydoc GetFadeBoundary()
239 const Toolkit::TextView::FadeBoundary& GetFadeBoundary() const;
242 * @copydoc SetEllipsizeText()
244 void SetEllipsizeText( const std::string& ellipsizeText );
247 * @copydoc SetEllipsizeText()
249 void SetEllipsizeText( const MarkupProcessor::StyledTextArray& ellipsizeText );
252 * @copydoc GetEllipsizeText()
254 std::string GetEllipsizeText() const;
257 * Checks if relayout the text is needed. If it is, it relais out the text
258 * by calling DoRelayOut().
260 void GetTextLayoutInfo();
263 * Calls GetTextLayoutInfo() and fills the given data structure.
265 * @see GetTextLayoutInfo()
267 void GetTextLayoutInfo( Toolkit::TextView::TextLayoutInfo& textLayoutInfo );
270 * @copydoc SetSortModifier()
272 void SetSortModifier( float depthOffset );
275 * @copydoc SetSnapshotModeEnabled()
277 void SetSnapshotModeEnabled( bool enable );
280 * @copydoc IsSnapshotModeEnabled()
282 bool IsSnapshotModeEnabled() const;
285 * @brief Sets whether markup processing should be carried out.
287 * @param[in] enable whether markup processing is carried out or not.
289 void SetMarkupProcessingEnabled( bool enable );
292 * @brief Returns whether markup processing is enabled or not
294 * @return true is markup processing is enabled
296 bool IsMarkupProcessingEnabled() const;
299 * @copydoc SetScrollEnabled()
301 void SetScrollEnabled( bool enable );
304 * @copydoc IsScrollEnabled()
306 bool IsScrollEnabled() const;
309 * @copydoc SetScrollPosition()
310 * @see DoSetScrollPosition()
312 void SetScrollPosition( const Vector2& position );
315 * @copydoc GetScrollPosition()
317 const Vector2& GetScrollPosition() const;
320 * @copydoc IsScrollPositionTrimmed()
322 bool IsScrollPositionTrimmed() const;
325 * @copydoc ScrolledSignal()
327 Toolkit::TextView::ScrolledSignalV2& ScrolledSignal();
330 * Connects a callback function with the object's signals.
331 * @param[in] object The object providing the signal.
332 * @param[in] tracker Used to disconnect the signal.
333 * @param[in] signalName The signal to connect to.
334 * @param[in] functor A newly allocated FunctorDelegate.
335 * @return True if the signal was connected.
336 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
338 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
343 * Called when a property of an object of this type is set.
344 * @param[in] object The object whose property is set.
345 * @param[in] index The property index.
346 * @param[in] value The new property value.
348 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
351 * Called to retrieve a property of an object of this type.
352 * @param[in] object The object whose property is to be retrieved.
353 * @param[in] index The property index.
354 * @return The current value of the property.
356 static Property::Value GetProperty( BaseObject* object, Property::Index index );
359 private: // From Control
362 * @copydoc Toolkit::Control::OnInitialize()
364 virtual void OnInitialize();
367 * @copydoc Toolkit::Control::OnFontChange( )
369 virtual void OnFontChange( bool defaultFontChange, bool defaultFontSizeChange );
372 * @copydoc Toolkit::Control::OnControlSizeSet()
374 virtual void OnControlSizeSet( const Vector3& size );
377 * @copydoc Toolkit::Control::OnRelaidOut()
379 * Removes text-actor and calls DoRelayOut()..
381 virtual void OnRelaidOut( Vector2 size, ActorSizeContainer& container );
384 * Retrieves the text-view's natural size.
386 * @return The natural size.
388 virtual Vector3 GetNaturalSize();
391 * Retrieves the text-view's \e height for a given \e width.
393 * @param[in] width The given \e width.
395 * @return The \e height for the given \e width.
397 virtual float GetHeightForWidth( float width );
400 * Retrieves the text-view's \e width for a given \e height.
402 * @param[in] height The given \e height.
404 * @return The \e width for the given \e height.
406 virtual float GetWidthForHeight( float height );
411 * Construct a new TextView.
416 * A reference counted object may only be deleted by calling Unreference()
423 TextView( const TextView& );
426 TextView& operator=( const TextView& rhs );
429 * Executes synchronously relayout operations such as set, insert, remove or replace text, or
430 * split groups of characters, etc.
432 void PerformTextViewProcessorOperations();
435 * Optimizes some text-view processor operations.
437 void OptimizeTextViewProcessorOperations();
440 * Synchronously relays-out all text-actors.
442 * Perform text-view-processor operations, sets the new size and position of text-actors and adds them to the text-view.
444 * @param[in] textViewSize The new text-view's size.
445 * @param[in] relayoutOperationMask Defines which operations need to be done in the relayout process.
447 void DoRelayOut( const Size& textViewSize, RelayoutOperationMask relayoutOperationMask );
450 * Process Snapshot. It refresh the render-task in order to generate a new snapshot image.
452 * ProcessSnapshot is called from OnRelaidOut() only if text has been relaid out.
453 * It creates a new image buffer only if the size of the text has changed.
455 * @param[in] textViewSize The new text-view's size.
457 void ProcessSnapshot( const Size& textViewSize );
460 * Aligns the offscreen rendering camera actor to cover the whole text and the resulting image actor accordingly with the text view's alignment.
462 * @param[in] textViewSize The text view's size.
463 * @param[in] offscreenSize The offscreen's frame buffer's size.
465 void AlignOffscreenCameraActor( const Size& textViewSize, const Size& offscreenSize );
468 * Callback called when the render task has processed.
470 * It makes the root actor invisible.
472 * @param[in] renderTask The processed render task.
474 void RenderTaskFinished( Dali::RenderTask& renderTask );
477 * Destroys offscreen rendering resources.
479 * It disconects the render task finished signal from the TextView::RenderTaskFinished() method,
480 * removes the render task from the render task list and resets the offscreen camera actor, root actor,
481 * image actor and the frame buffer.
483 void DestroyOffscreenRenderingResources();
486 * Called when text-view is scrolled.
488 * Sets the new scroll position. @see DoSetScrollPosition()
490 * @param[in] actor Handle of the text-view.
491 * @param[in] gesture Data structure with the parameters of the gesture.
493 void OnTextPan( Actor actor, PanGesture gesture );
496 * Ensures the text-view's boundaries are fully covered of text.
498 * i.e. if the text-view's size is 100x100 and the text's size is 150x100, the scroll position
499 * can be in the range -50,0 and 50,0.
501 void TrimScrollPosition();
504 * Called from SetScrollPosition() and OnTextPan()
506 * Updates the stored scroll position ensuring the text-view is always covered with text by calling
507 * TrimScrollPosition(), calculates the difference with the previous one and emits the Toolkit::TextView::SignalScrolled() signal.
509 * @param[in] position The new scroll position.
511 void DoSetScrollPosition( const Vector2& position );
514 * Combines width and height exceed policies.
516 * This method is a big switch() which combines two exceed policies into one.
517 * The aim is avoid this switch inside the relayout code.
519 * i.e. Width policy = Split. Height policy = Original. Internally the policy is SplitOriginal.
521 void CombineExceedPolicies();
524 * Retrieves the text-view's root actor which stores all text-actors.
525 * It could be the text-view itself or an actor used in the snapshot mode.
527 * @return the root actor.
529 Actor GetRootActor() const;
532 * Handle SetProperty for markup processing.
533 * @param[in] propertyValue The new property value.
535 void OnMarkupEnabledPeopertySet( Property::Value propertyValue );
538 * Handles SetProperty for multiline policy.
539 * @param[in] propertyValue The new property value.
541 void OnMultilinePolicyPropertySet( Property::Value propertyValue );
544 * Handles SetProperty for width exceed policy.
545 * @param[in] propertyValue The new property value.
547 void OnWidthExceedPolicyPropertySet( Property::Value propertyValue );
550 * Handles SetProperty for height exceed policy.
551 * @param[in] propertyValue The new property value.
553 void OnHeightExceedPolicyPropertySet( Property::Value propertyValue );
556 * Handles SetProperty for line justification.
557 * @param[in] propertyValue The new property value.
559 void OnLineJustificationPropertySet( Property::Value propertyValue );
562 * Handles SetProperty for fade boundary.
563 * @param[in] propertyValue The new property value.
565 void OnFadeBoundaryPropertySet( Property::Value propertyValue );
568 * Handles SetProperty for alignment property.
569 * @param[in] propertyIndex The property index.
570 * @param[in] propertyValue The new property value.
572 void OnAlignmentPropertySet( Property::Index propertyIndex, Property::Value propertyValue );
575 * Handles GetProperty for horizontal alignment property.
576 * @return The property value of horizontal alignment.
578 std::string OnHorizontalAlignmentPropertyGet();
581 * Handles GetProperty for vertical alignment property.
582 * @return The property value of vertical alignment.
584 std::string OnVerticalAlignmentPropertyGet();
589 * The parameters which affects the layout of the text.
591 struct LayoutParameters
594 * Default constructor.
601 LayoutParameters( Toolkit::TextView::MultilinePolicy multilinePolicy,
602 Toolkit::TextView::ExceedPolicy widthExceedPolicy,
603 Toolkit::TextView::ExceedPolicy heightExceedPolicy,
604 Toolkit::Alignment::Type alignment,
605 Toolkit::TextView::LineJustification lineJustification,
606 float lineHeightOffset,
607 const std::string& ellipsizeText,
608 bool markUpEnabled );
613 LayoutParameters( const LayoutParameters& layoutParameters );
616 * Assignment operator.
618 LayoutParameters& operator=( const LayoutParameters& layoutParameters );
620 Toolkit::TextView::MultilinePolicy mMultilinePolicy; ///< Stores the multiline policy.
621 TextView::ExceedPolicy mExceedPolicy; ///< Stores a combination of both policies;
622 Toolkit::TextView::ExceedPolicy mWidthExceedPolicy; ///< Stores the text width exceed policy.
623 Toolkit::TextView::ExceedPolicy mHeightExceedPolicy; ///< Stores the text height exceed policy.
624 Toolkit::Alignment::Type mHorizontalAlignment; ///< Stores the horizontal alignment for the whole text.
625 Toolkit::Alignment::Type mVerticalAlignment; ///< Stores the vertical alignment for the whole text.
626 Toolkit::TextView::LineJustification mLineJustification; ///< Stores the line justification.
627 float mLineHeightOffset; ///< Line height offset to be addded to the font line height (measured in PointSize).
628 MarkupProcessor::StyledTextArray mEllipsizeText; ///< Stores the ellipsize text
629 bool mMarkUpEnabled:1; ///< Is markup string scanning enabled
633 * Some parameters which affects the text view visualization.
635 struct VisualParameters
638 * Default constructor.
645 VisualParameters( const VisualParameters& visualParameters );
648 * Assignment operator.
650 VisualParameters& operator=( const VisualParameters& visualParameters );
652 Toolkit::TextView::FadeBoundary mFadeBoundary; ///< Fade boundary used in fade mode.
653 float mSortModifier; ///< Stores the sort modifier for all text-actors.
654 Vector2 mCameraScrollPosition; ///< The scroll offset.
655 bool mSnapshotModeEnabled:1; ///< Whether text-view is rendered offscreen.
656 bool mScrollEnabled:1; ///< Whether the text scroll is enabled.
657 bool mScrollPositionTrimmed:1; ///< Whether the last scroll position set was trimmed.
661 * Temporary data used to calculate line justification.
663 struct LineJustificationInfo
665 TextViewProcessor::TextInfoIndices mIndices; ///< Indices to the first character of the new line.
666 float mLineLength; ///< Length of the line (or portion of line).
670 * The results of the relayout process.
675 * Default constructor.
682 RelayoutData( const RelayoutData& relayoutData );
685 * Assignment operator.
687 RelayoutData& operator=( const RelayoutData& relayoutData );
689 Size mTextViewSize; ///< The text-view's size used to relaid-out the text.
690 float mShrinkFactor; ///< Shrink factor used when the exceed policy contains ShrinkToFit.
691 TextViewProcessor::TextLayoutInfo mTextLayoutInfo; ///< Stores metrics, layout info (size, direction, type of word) and text-actor info for the whole text.
692 std::vector<int> mCharacterLogicalToVisualMap; ///< Reorder map that stores each character's visual (output) index according to its logical (input) index
693 std::vector<int> mCharacterVisualToLogicalMap; ///< Reorder map that stores each character's logical (input) index according to its visual (output) index
694 std::vector<RenderableActor> mGlyphActors; ///< Stores handles of those text-actors which are currently added to the text-view.
695 std::vector<RenderableActor> mEllipsizedGlyphActors; ///< Stores handles of those text-actors which are used to ellipsize the text.
696 Toolkit::TextView::CharacterLayoutInfoContainer mCharacterLayoutInfoTable; ///< Stores layout info per character sorted by the character's visual index.
697 Toolkit::TextView::LineLayoutInfoContainer mLines; ///< Stores an index to the first character of each line.
698 Size mTextSizeForRelayoutOption; ///< Stores the text size after relayout.
699 std::vector<LineJustificationInfo> mLineJustificationInfo; ///< Stores justification info per line.
700 TextActorCache mTextActorCache; ///< Stores previously created text-actors to be reused.
705 MarkupProcessor::StyledTextArray mCurrentStyledText; ///< text currently displayed by the view
706 std::vector<TextViewProcessorMetadata> mTextViewProcessorOperations; ///< Stores all relayout operations which arrive between two consecutive OnRelaidOut() calls.
708 LayoutParameters mLayoutParameters; ///< Stores some layout parameters in a struct. To be passed in layout functions.
709 VisualParameters mVisualParameters; ///< Some parameters which afects text-view visualization.
710 RelayoutData mRelayoutData; ///< struct with text-view's data structures used to pass all of them in one parameter.
711 RelayoutOperationMask mRelayoutOperations; ///< Which relayout operations have to be done.
713 Layer mOffscreenRootActor; ///< Root actor for offscreen rendering.
714 ImageActor mOffscreenImageActor; ///< Image actor for offscreen rendering.
715 CameraActor mOffscreenCameraActor; ///< Camera actor for offscreen rendering.
716 Size mCurrentOffscreenSize; ///< Current used ofscreen size.
717 FrameBufferImage mFrameBufferImage; ///< Frame buffer used for offscreen rendering.
718 RenderTask mRenderTask; ///< Used to generate an offscreen rendering.
720 PanGestureDetector mPanGestureDetector; ///< Pan gesture for text scrolling.
723 * Helper class used to prevent the modification of some members.
741 bool mLockPreviousSnapshotMode; ///< Whether previous stored snapshot mode should be modified.
742 bool mPreviousSnapshotModeEnabled:1; ///< Stores the previous snapshot mode value.
743 bool mMarkUpEnabled:1; ///< enable to scan for mark-up
745 Toolkit::TextView::ScrolledSignalV2 mScrolledSignalV2; ///< Signal emitted when text is scrolled.
748 } // namespace Internal
750 // Helpers for public-api forwarding methods
752 inline Internal::TextView& GetImpl( TextView& textView )
754 DALI_ASSERT_ALWAYS( textView );
756 RefObject& handle = textView.GetImplementation();
758 return static_cast< Internal::TextView& >( handle );
761 inline const Internal::TextView& GetImpl( const TextView& textView )
763 DALI_ASSERT_ALWAYS( textView );
765 const RefObject& handle = textView.GetImplementation();
767 return static_cast< const Internal::TextView& >( handle );
770 } // namespace Toolkit
774 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__