2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/text/text-controller-relayouter.h>
23 #include <dali/integration-api/debug.h>
26 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
27 #include <dali-toolkit/internal/text/text-controller-impl.h>
32 #if defined(DEBUG_ENABLED)
33 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
36 constexpr float MAX_FLOAT = std::numeric_limits<float>::max();
38 float ConvertToEven( float value )
40 int intValue(static_cast<int>( value ));
41 return static_cast<float>( intValue + ( intValue & 1 ) );
55 Vector3 Controller::Relayouter::GetNaturalSize(Controller& controller)
57 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
60 // Make sure the model is up-to-date before layouting
61 controller.ProcessModifyEvents();
63 Controller::Impl& impl = *controller.mImpl;
64 ModelPtr& model = impl.mModel;
65 VisualModelPtr& visualModel = model->mVisualModel;
66 if( impl.mRecalculateNaturalSize )
68 // Operations that can be done only once until the text changes.
69 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
77 // Set the update info to relayout the whole text.
78 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
79 textUpdateInfo.mParagraphCharacterIndex = 0u;
80 textUpdateInfo.mRequestedNumberOfCharacters = model->mLogicalModel->mText.Count();
82 // Make sure the model is up-to-date before layouting
83 impl.UpdateModel( onlyOnceOperations );
85 // Get a reference to the pending operations member
86 OperationsMask& operationsPending = impl.mOperationsPending;
88 // Layout the text for the new width.
89 operationsPending = static_cast<OperationsMask>( operationsPending | LAYOUT | REORDER );
91 // Store the actual control's size to restore later.
92 const Size actualControlSize = visualModel->mControlSize;
94 DoRelayout( controller,
95 Size( MAX_FLOAT, MAX_FLOAT ),
96 static_cast<OperationsMask>( onlyOnceOperations |
98 naturalSize.GetVectorXY() );
100 // Do not do again the only once operations.
101 operationsPending = static_cast<OperationsMask>( operationsPending & ~onlyOnceOperations );
103 // Do the size related operations again.
104 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
107 operationsPending = static_cast<OperationsMask>( operationsPending | sizeOperations );
109 // Stores the natural size to avoid recalculate it again
110 // unless the text/style changes.
111 visualModel->SetNaturalSize( naturalSize.GetVectorXY() );
113 impl.mRecalculateNaturalSize = false;
115 // Clear the update info. This info will be set the next time the text is updated.
116 textUpdateInfo.Clear();
117 textUpdateInfo.mClearAll = true;
119 // Restore the actual control's size.
120 visualModel->mControlSize = actualControlSize;
122 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
126 naturalSize = visualModel->GetNaturalSize();
128 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
131 naturalSize.x = ConvertToEven( naturalSize.x );
132 naturalSize.y = ConvertToEven( naturalSize.y );
137 bool Controller::Relayouter::CheckForTextFit(Controller& controller, float pointSize, const Size& layoutSize)
140 Controller::Impl& impl = *controller.mImpl;
141 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
142 impl.mFontDefaults->mFitPointSize = pointSize;
143 impl.mFontDefaults->sizeDefined = true;
144 controller.ClearFontData();
146 // Operations that can be done only once until the text changes.
147 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
155 textUpdateInfo.mParagraphCharacterIndex = 0u;
156 textUpdateInfo.mRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
158 // Make sure the model is up-to-date before layouting
159 impl.UpdateModel( onlyOnceOperations );
161 DoRelayout( controller,
162 Size( layoutSize.width, MAX_FLOAT ),
163 static_cast<OperationsMask>( onlyOnceOperations | LAYOUT),
166 // Clear the update info. This info will be set the next time the text is updated.
167 textUpdateInfo.Clear();
168 textUpdateInfo.mClearAll = true;
170 if( textSize.width > layoutSize.width || textSize.height > layoutSize.height )
177 float Controller::Relayouter::GetHeightForWidth(Controller& controller, float width)
179 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", &controller, width );
180 // Make sure the model is up-to-date before layouting
181 controller.ProcessModifyEvents();
183 Controller::Impl& impl = *controller.mImpl;
184 ModelPtr& model = impl.mModel;
185 VisualModelPtr& visualModel = model->mVisualModel;
186 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
189 if( fabsf( width - visualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 ||
190 textUpdateInfo.mFullRelayoutNeeded ||
191 textUpdateInfo.mClearAll )
193 // Operations that can be done only once until the text changes.
194 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
202 // Set the update info to relayout the whole text.
203 textUpdateInfo.mParagraphCharacterIndex = 0u;
204 textUpdateInfo.mRequestedNumberOfCharacters = model->mLogicalModel->mText.Count();
206 // Make sure the model is up-to-date before layouting
207 impl.UpdateModel( onlyOnceOperations );
209 // Get a reference to the pending operations member
210 OperationsMask& operationsPending = impl.mOperationsPending;
212 // Layout the text for the new width.
213 operationsPending = static_cast<OperationsMask>( operationsPending | LAYOUT );
215 // Store the actual control's width.
216 const float actualControlWidth = visualModel->mControlSize.width;
218 DoRelayout( controller,
219 Size( width, MAX_FLOAT ),
220 static_cast<OperationsMask>( onlyOnceOperations |
224 // Do not do again the only once operations.
225 operationsPending = static_cast<OperationsMask>( operationsPending & ~onlyOnceOperations );
227 // Do the size related operations again.
228 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
232 operationsPending = static_cast<OperationsMask>( operationsPending | sizeOperations );
234 // Clear the update info. This info will be set the next time the text is updated.
235 textUpdateInfo.Clear();
236 textUpdateInfo.mClearAll = true;
238 // Restore the actual control's width.
239 visualModel->mControlSize.width = actualControlWidth;
241 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
245 layoutSize = visualModel->GetLayoutSize();
246 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
249 return layoutSize.height;
252 Controller::UpdateTextType Controller::Relayouter::Relayout(Controller& controller, const Size& size, Dali::LayoutDirection::Type layoutDirection)
254 Controller::Impl& impl = *controller.mImpl;
255 ModelPtr& model = impl.mModel;
256 VisualModelPtr& visualModel = model->mVisualModel;
257 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
259 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", &controller, size.width, size.height, impl.mIsAutoScrollEnabled ?"true":"false" );
261 UpdateTextType updateTextType = NONE_UPDATED;
263 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
265 if( 0u != visualModel->mGlyphPositions.Count() )
267 visualModel->mGlyphPositions.Clear();
268 updateTextType = MODEL_UPDATED;
271 // Clear the update info. This info will be set the next time the text is updated.
272 textUpdateInfo.Clear();
274 // Not worth to relayout if width or height is equal to zero.
275 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
277 return updateTextType;
280 // Whether a new size has been set.
281 const bool newSize = ( size != visualModel->mControlSize );
283 // Get a reference to the pending operations member
284 OperationsMask& operationsPending = impl.mOperationsPending;
288 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", visualModel->mControlSize.width, visualModel->mControlSize.height );
290 if( ( 0 == textUpdateInfo.mNumberOfCharactersToAdd ) &&
291 ( 0 == textUpdateInfo.mPreviousNumberOfCharacters ) &&
292 ( ( visualModel->mControlSize.width < Math::MACHINE_EPSILON_1000 ) || ( visualModel->mControlSize.height < Math::MACHINE_EPSILON_1000 ) ) )
294 textUpdateInfo.mNumberOfCharactersToAdd = model->mLogicalModel->mText.Count();
297 // Layout operations that need to be done if the size changes.
298 operationsPending = static_cast<OperationsMask>( operationsPending |
303 // Set the update info to relayout the whole text.
304 textUpdateInfo.mFullRelayoutNeeded = true;
305 textUpdateInfo.mCharacterIndex = 0u;
307 // Store the size used to layout the text.
308 visualModel->mControlSize = size;
311 // Whether there are modify events.
312 if( 0u != impl.mModifyEvents.Count() )
314 // Style operations that need to be done if the text is modified.
315 operationsPending = static_cast<OperationsMask>( operationsPending | COLOR );
318 // Set the update info to elide the text.
319 if( model->mElideEnabled ||
320 ( ( NULL != impl.mEventData ) && impl.mEventData->mIsPlaceholderElideEnabled ) )
322 // Update Text layout for applying elided
323 operationsPending = static_cast<OperationsMask>( operationsPending |
328 textUpdateInfo.mFullRelayoutNeeded = true;
329 textUpdateInfo.mCharacterIndex = 0u;
332 if( model->mMatchSystemLanguageDirection && impl.mLayoutDirection != layoutDirection )
334 // Clear the update info. This info will be set the next time the text is updated.
335 textUpdateInfo.mClearAll = true;
336 // Apply modifications to the model
337 // Shape the text again is needed because characters like '()[]{}' have to be mirrored and the glyphs generated again.
338 operationsPending = static_cast<OperationsMask>( operationsPending |
345 impl.mLayoutDirection = layoutDirection;
348 // Make sure the model is up-to-date before layouting.
349 controller.ProcessModifyEvents();
350 bool updated = impl.UpdateModel( operationsPending );
354 updated = DoRelayout( controller, size, operationsPending, layoutSize ) || updated;
359 updateTextType = MODEL_UPDATED;
362 // Do not re-do any operation until something changes.
363 operationsPending = NO_OPERATION;
364 model->mScrollPositionLast = model->mScrollPosition;
366 // Whether the text control is editable
367 const bool isEditable = NULL != impl.mEventData;
369 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
371 if( newSize && isEditable )
373 offset = model->mScrollPosition;
376 if( !isEditable || !controller.IsMultiLineEnabled() )
378 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
379 controller.CalculateVerticalOffset( size );
386 // If there is a new size, the scroll position needs to be clamped.
387 impl.ClampHorizontalScroll( layoutSize );
389 // Update the decorator's positions is needed if there is a new size.
390 impl.mEventData->mDecorator->UpdatePositions( model->mScrollPosition - offset );
393 // Move the cursor, grab handle etc.
394 if( impl.ProcessInputEvents() )
396 updateTextType = static_cast<UpdateTextType>( updateTextType | DECORATOR_UPDATED );
400 // Clear the update info. This info will be set the next time the text is updated.
401 textUpdateInfo.Clear();
402 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
404 return updateTextType;
407 bool Controller::Relayouter::DoRelayout(Controller& controller, const Size& size, OperationsMask operationsRequired, Size& layoutSize )
409 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", &controller, size.width, size.height );
410 bool viewUpdated( false );
412 Controller::Impl& impl = *controller.mImpl;
414 // Calculate the operations to be done.
415 const OperationsMask operations = static_cast<OperationsMask>( impl.mOperationsPending & operationsRequired );
417 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
418 const CharacterIndex startIndex = textUpdateInfo.mParagraphCharacterIndex;
419 const Length requestedNumberOfCharacters = textUpdateInfo.mRequestedNumberOfCharacters;
421 // Get the current layout size.
422 VisualModelPtr& visualModel = impl.mModel->mVisualModel;
423 layoutSize = visualModel->GetLayoutSize();
425 if( NO_OPERATION != ( LAYOUT & operations ) )
427 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
429 // Some vectors with data needed to layout and reorder may be void
430 // after the first time the text has been laid out.
431 // Fill the vectors again.
433 // Calculate the number of glyphs to layout.
434 const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
435 const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
436 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
437 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
439 const CharacterIndex lastIndex = startIndex + ( ( requestedNumberOfCharacters > 0u ) ? requestedNumberOfCharacters - 1u : 0u );
440 const GlyphIndex startGlyphIndex = textUpdateInfo.mStartGlyphIndex;
442 // Make sure the index is not out of bound
443 if ( charactersToGlyph.Count() != glyphsPerCharacter.Count() ||
444 requestedNumberOfCharacters > charactersToGlyph.Count() ||
445 ( lastIndex > charactersToGlyph.Count() && charactersToGlyph.Count() > 0u ) )
447 std::string currentText;
448 controller.GetText( currentText );
450 DALI_LOG_ERROR( "Controller::DoRelayout: Attempting to access invalid buffer\n" );
451 DALI_LOG_ERROR( "Current text is: %s\n", currentText.c_str() );
452 DALI_LOG_ERROR( "startIndex: %u, lastIndex: %u, requestedNumberOfCharacters: %u, charactersToGlyph.Count = %lu, glyphsPerCharacter.Count = %lu\n", startIndex, lastIndex, requestedNumberOfCharacters, charactersToGlyph.Count(), glyphsPerCharacter.Count());
457 const Length numberOfGlyphs = ( requestedNumberOfCharacters > 0u ) ? *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex : 0u;
458 const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
460 if( 0u == totalNumberOfGlyphs )
462 if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) )
464 visualModel->SetLayoutSize( Size::ZERO );
467 // Nothing else to do if there is no glyphs.
468 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
472 // Set the layout parameters.
473 Layout::Parameters layoutParameters( size, impl.mModel);
475 // Resize the vector of positions to have the same size than the vector of glyphs.
476 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
477 glyphPositions.Resize( totalNumberOfGlyphs );
479 // Whether the last character is a new paragraph character.
480 const Character* const textBuffer = impl.mModel->mLogicalModel->mText.Begin();
481 textUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( impl.mModel->mLogicalModel->mText.Count() - 1u ) ) );
482 layoutParameters.isLastNewParagraph = textUpdateInfo.mIsLastCharacterNewParagraph;
484 // The initial glyph and the number of glyphs to layout.
485 layoutParameters.startGlyphIndex = startGlyphIndex;
486 layoutParameters.numberOfGlyphs = numberOfGlyphs;
487 layoutParameters.startLineIndex = textUpdateInfo.mStartLineIndex;
488 layoutParameters.estimatedNumberOfLines = textUpdateInfo.mEstimatedNumberOfLines;
490 // Update the ellipsis
491 bool elideTextEnabled = impl.mModel->mElideEnabled;
493 if( NULL != impl.mEventData )
495 if( impl.mEventData->mPlaceholderEllipsisFlag && impl.IsShowingPlaceholderText() )
497 elideTextEnabled = impl.mEventData->mIsPlaceholderElideEnabled;
499 else if( EventData::INACTIVE != impl.mEventData->mState )
501 // Disable ellipsis when editing
502 elideTextEnabled = false;
505 // Reset the scroll position in inactive state
506 if( elideTextEnabled && ( impl.mEventData->mState == EventData::INACTIVE ) )
508 controller.ResetScrollPosition();
512 // Update the visual model.
513 bool isAutoScrollEnabled = impl.mIsAutoScrollEnabled;
515 viewUpdated = impl.mLayoutEngine.LayoutText( layoutParameters,
518 isAutoScrollEnabled );
519 impl.mIsAutoScrollEnabled = isAutoScrollEnabled;
521 viewUpdated = viewUpdated || ( newLayoutSize != layoutSize );
525 layoutSize = newLayoutSize;
527 if( NO_OPERATION != ( UPDATE_DIRECTION & operations ) )
529 impl.mIsTextDirectionRTL = false;
532 if ( ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) ) && !visualModel->mLines.Empty() )
534 impl.mIsTextDirectionRTL = visualModel->mLines[0u].direction;
537 // Sets the layout size.
538 if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) )
540 visualModel->SetLayoutSize( layoutSize );
545 if( NO_OPERATION != ( ALIGN & operations ) )
547 // The laid-out lines.
548 Vector<LineRun>& lines = visualModel->mLines;
550 CharacterIndex alignStartIndex = startIndex;
551 Length alignRequestedNumberOfCharacters = requestedNumberOfCharacters;
553 // the whole text needs to be full aligned.
554 // If you do not do a full aligned, only the last line of the multiline input is aligned.
555 if( impl.mEventData && impl.mEventData->mUpdateAlignment )
557 alignStartIndex = 0u;
558 alignRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
559 impl.mEventData->mUpdateAlignment = false;
562 // Need to align with the control's size as the text may contain lines
563 // starting either with left to right text or right to left.
564 impl.mLayoutEngine.Align( size,
566 alignRequestedNumberOfCharacters,
567 impl.mModel->mHorizontalAlignment,
569 impl.mModel->mAlignmentOffset,
570 impl.mLayoutDirection,
571 impl.mModel->mMatchSystemLanguageDirection );
575 #if defined(DEBUG_ENABLED)
576 std::string currentText;
577 controller.GetText( currentText );
578 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::DoRelayout [%p] mImpl->mIsTextDirectionRTL[%s] [%s]\n", &controller, (impl.mIsTextDirectionRTL)?"true":"false", currentText.c_str() );
580 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
584 void Controller::Relayouter::CalculateVerticalOffset(Controller& controller, const Size& controlSize)
586 Controller::Impl& impl = *controller.mImpl;
587 ModelPtr& model = impl.mModel;
588 Size layoutSize = model->mVisualModel->GetLayoutSize();
590 if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
592 // Get the line height of the default font.
593 layoutSize.height = impl.GetDefaultFontLineHeight();
596 switch( model->mVerticalAlignment )
598 case VerticalAlignment::TOP:
600 model->mScrollPosition.y = 0.f;
603 case VerticalAlignment::CENTER:
605 model->mScrollPosition.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
608 case VerticalAlignment::BOTTOM:
610 model->mScrollPosition.y = controlSize.height - layoutSize.height;
618 } // namespace Toolkit