2 * Copyright (c) 2021 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>
22 #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>
31 #if defined(DEBUG_ENABLED)
32 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
35 constexpr float MAX_FLOAT = std::numeric_limits<float>::max();
37 float ConvertToEven(float value)
39 int intValue(static_cast<int>(value));
40 return static_cast<float>(intValue + (intValue & 1));
51 Vector3 Controller::Relayouter::GetNaturalSize(Controller& controller)
53 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n");
56 // Make sure the model is up-to-date before layouting
57 controller.ProcessModifyEvents();
59 Controller::Impl& impl = *controller.mImpl;
60 ModelPtr& model = impl.mModel;
61 VisualModelPtr& visualModel = model->mVisualModel;
62 if(impl.mRecalculateNaturalSize)
64 // Operations that can be done only once until the text changes.
65 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>(CONVERT_TO_UTF32 |
73 // Set the update info to relayout the whole text.
74 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
75 textUpdateInfo.mParagraphCharacterIndex = 0u;
76 textUpdateInfo.mRequestedNumberOfCharacters = model->mLogicalModel->mText.Count();
78 // Make sure the model is up-to-date before layouting
79 impl.UpdateModel(onlyOnceOperations);
81 // Get a reference to the pending operations member
82 OperationsMask& operationsPending = impl.mOperationsPending;
84 // Layout the text for the new width.
85 operationsPending = static_cast<OperationsMask>(operationsPending | LAYOUT | REORDER);
87 // Store the actual control's size to restore later.
88 const Size actualControlSize = visualModel->mControlSize;
90 DoRelayout(controller,
91 Size(MAX_FLOAT, MAX_FLOAT),
92 static_cast<OperationsMask>(onlyOnceOperations |
94 naturalSize.GetVectorXY());
96 // Do not do again the only once operations.
97 operationsPending = static_cast<OperationsMask>(operationsPending & ~onlyOnceOperations);
99 // Do the size related operations again.
100 const OperationsMask sizeOperations = static_cast<OperationsMask>(LAYOUT |
103 operationsPending = static_cast<OperationsMask>(operationsPending | sizeOperations);
105 // Stores the natural size to avoid recalculate it again
106 // unless the text/style changes.
107 visualModel->SetNaturalSize(naturalSize.GetVectorXY());
109 impl.mRecalculateNaturalSize = false;
111 // Clear the update info. This info will be set the next time the text is updated.
112 textUpdateInfo.Clear();
113 textUpdateInfo.mClearAll = true;
115 // Restore the actual control's size.
116 visualModel->mControlSize = actualControlSize;
118 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z);
122 naturalSize = visualModel->GetNaturalSize();
124 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z);
127 naturalSize.x = ConvertToEven(naturalSize.x);
128 naturalSize.y = ConvertToEven(naturalSize.y);
133 bool Controller::Relayouter::CheckForTextFit(Controller& controller, float pointSize, const Size& layoutSize)
136 Controller::Impl& impl = *controller.mImpl;
137 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
138 impl.mFontDefaults->mFitPointSize = pointSize;
139 impl.mFontDefaults->sizeDefined = true;
140 controller.ClearFontData();
142 // Operations that can be done only once until the text changes.
143 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>(CONVERT_TO_UTF32 |
151 textUpdateInfo.mParagraphCharacterIndex = 0u;
152 textUpdateInfo.mRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
154 // Make sure the model is up-to-date before layouting
155 impl.UpdateModel(onlyOnceOperations);
157 DoRelayout(controller,
158 Size(layoutSize.width, MAX_FLOAT),
159 static_cast<OperationsMask>(onlyOnceOperations | LAYOUT),
162 // Clear the update info. This info will be set the next time the text is updated.
163 textUpdateInfo.Clear();
164 textUpdateInfo.mClearAll = true;
166 if(textSize.width > layoutSize.width || textSize.height > layoutSize.height)
173 void Controller::Relayouter::FitPointSizeforLayout(Controller& controller, const Size& layoutSize)
175 Controller::Impl& impl = *controller.mImpl;
177 const OperationsMask operations = impl.mOperationsPending;
178 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations) || impl.mTextFitContentSize != layoutSize)
180 ModelPtr& model = impl.mModel;
182 bool actualellipsis = model->mElideEnabled;
183 float minPointSize = impl.mTextFitMinSize;
184 float maxPointSize = impl.mTextFitMaxSize;
185 float pointInterval = impl.mTextFitStepSize;
187 model->mElideEnabled = false;
188 Vector<float> pointSizeArray;
191 if(pointInterval < 1.f)
193 impl.mTextFitStepSize = pointInterval = 1.0f;
196 pointSizeArray.Reserve(static_cast<unsigned int>(ceil((maxPointSize - minPointSize) / pointInterval)));
198 for(float i = minPointSize; i < maxPointSize; i += pointInterval)
200 pointSizeArray.PushBack(i);
203 pointSizeArray.PushBack(maxPointSize);
205 int bestSizeIndex = 0;
206 int min = bestSizeIndex + 1;
207 int max = pointSizeArray.Size() - 1;
210 int destI = (min + max) / 2;
212 if(CheckForTextFit(controller, pointSizeArray[destI], layoutSize))
224 model->mElideEnabled = actualellipsis;
225 impl.mFontDefaults->mFitPointSize = pointSizeArray[bestSizeIndex];
226 impl.mFontDefaults->sizeDefined = true;
227 controller.ClearFontData();
231 float Controller::Relayouter::GetHeightForWidth(Controller& controller, float width)
233 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", &controller, width);
234 // Make sure the model is up-to-date before layouting
235 controller.ProcessModifyEvents();
237 Controller::Impl& impl = *controller.mImpl;
238 ModelPtr& model = impl.mModel;
239 VisualModelPtr& visualModel = model->mVisualModel;
240 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
243 if(fabsf(width - visualModel->mControlSize.width) > Math::MACHINE_EPSILON_1000 ||
244 textUpdateInfo.mFullRelayoutNeeded ||
245 textUpdateInfo.mClearAll)
247 // Operations that can be done only once until the text changes.
248 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>(CONVERT_TO_UTF32 |
256 // Set the update info to relayout the whole text.
257 textUpdateInfo.mParagraphCharacterIndex = 0u;
258 textUpdateInfo.mRequestedNumberOfCharacters = model->mLogicalModel->mText.Count();
260 // Make sure the model is up-to-date before layouting
261 impl.UpdateModel(onlyOnceOperations);
263 // Get a reference to the pending operations member
264 OperationsMask& operationsPending = impl.mOperationsPending;
266 // Layout the text for the new width.
267 operationsPending = static_cast<OperationsMask>(operationsPending | LAYOUT);
269 // Store the actual control's width.
270 const float actualControlWidth = visualModel->mControlSize.width;
272 DoRelayout(controller,
273 Size(width, MAX_FLOAT),
274 static_cast<OperationsMask>(onlyOnceOperations |
278 // Do not do again the only once operations.
279 operationsPending = static_cast<OperationsMask>(operationsPending & ~onlyOnceOperations);
281 // Do the size related operations again.
282 const OperationsMask sizeOperations = static_cast<OperationsMask>(LAYOUT |
286 operationsPending = static_cast<OperationsMask>(operationsPending | sizeOperations);
288 // Clear the update info. This info will be set the next time the text is updated.
289 textUpdateInfo.Clear();
290 textUpdateInfo.mClearAll = true;
292 // Restore the actual control's width.
293 visualModel->mControlSize.width = actualControlWidth;
295 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height);
299 layoutSize = visualModel->GetLayoutSize();
300 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height);
303 return layoutSize.height;
306 Controller::UpdateTextType Controller::Relayouter::Relayout(Controller& controller, const Size& size, Dali::LayoutDirection::Type layoutDirection)
308 Controller::Impl& impl = *controller.mImpl;
309 ModelPtr& model = impl.mModel;
310 VisualModelPtr& visualModel = model->mVisualModel;
311 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
313 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", &controller, size.width, size.height, impl.mIsAutoScrollEnabled ? "true" : "false");
315 UpdateTextType updateTextType = NONE_UPDATED;
317 if((size.width < Math::MACHINE_EPSILON_1000) || (size.height < Math::MACHINE_EPSILON_1000))
319 if(0u != visualModel->mGlyphPositions.Count())
321 visualModel->mGlyphPositions.Clear();
322 updateTextType = MODEL_UPDATED;
325 // Clear the update info. This info will be set the next time the text is updated.
326 textUpdateInfo.Clear();
328 // Not worth to relayout if width or height is equal to zero.
329 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n");
331 return updateTextType;
334 // Whether a new size has been set.
335 const bool newSize = (size != visualModel->mControlSize);
337 // Get a reference to the pending operations member
338 OperationsMask& operationsPending = impl.mOperationsPending;
342 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", visualModel->mControlSize.width, visualModel->mControlSize.height);
344 if((0 == textUpdateInfo.mNumberOfCharactersToAdd) &&
345 (0 == textUpdateInfo.mPreviousNumberOfCharacters) &&
346 ((visualModel->mControlSize.width < Math::MACHINE_EPSILON_1000) || (visualModel->mControlSize.height < Math::MACHINE_EPSILON_1000)))
348 textUpdateInfo.mNumberOfCharactersToAdd = model->mLogicalModel->mText.Count();
351 // Layout operations that need to be done if the size changes.
352 operationsPending = static_cast<OperationsMask>(operationsPending |
357 // Set the update info to relayout the whole text.
358 textUpdateInfo.mFullRelayoutNeeded = true;
359 textUpdateInfo.mCharacterIndex = 0u;
361 // Store the size used to layout the text.
362 visualModel->mControlSize = size;
365 // Whether there are modify events.
366 if(0u != impl.mModifyEvents.Count())
368 // Style operations that need to be done if the text is modified.
369 operationsPending = static_cast<OperationsMask>(operationsPending | COLOR);
372 // Set the update info to elide the text.
373 if(model->mElideEnabled ||
374 ((NULL != impl.mEventData) && impl.mEventData->mIsPlaceholderElideEnabled))
376 // Update Text layout for applying elided
377 operationsPending = static_cast<OperationsMask>(operationsPending |
382 textUpdateInfo.mFullRelayoutNeeded = true;
383 textUpdateInfo.mCharacterIndex = 0u;
386 if(model->mMatchSystemLanguageDirection && impl.mLayoutDirection != layoutDirection)
388 // Clear the update info. This info will be set the next time the text is updated.
389 textUpdateInfo.mClearAll = true;
390 // Apply modifications to the model
391 // Shape the text again is needed because characters like '()[]{}' have to be mirrored and the glyphs generated again.
392 operationsPending = static_cast<OperationsMask>(operationsPending |
399 impl.mLayoutDirection = layoutDirection;
402 // Make sure the model is up-to-date before layouting.
403 controller.ProcessModifyEvents();
404 bool updated = impl.UpdateModel(operationsPending);
408 updated = DoRelayout(controller, size, operationsPending, layoutSize) || updated;
412 updateTextType = MODEL_UPDATED;
415 // Do not re-do any operation until something changes.
416 operationsPending = NO_OPERATION;
417 model->mScrollPositionLast = model->mScrollPosition;
419 // Whether the text control is editable
420 const bool isEditable = NULL != impl.mEventData;
422 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
424 if(newSize && isEditable)
426 offset = model->mScrollPosition;
429 if(!isEditable || !controller.IsMultiLineEnabled())
431 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
432 controller.CalculateVerticalOffset(size);
439 // If there is a new size, the scroll position needs to be clamped.
440 impl.ClampHorizontalScroll(layoutSize);
442 // Update the decorator's positions is needed if there is a new size.
443 impl.mEventData->mDecorator->UpdatePositions(model->mScrollPosition - offset);
446 // Move the cursor, grab handle etc.
447 if(impl.ProcessInputEvents())
449 updateTextType = static_cast<UpdateTextType>(updateTextType | DECORATOR_UPDATED);
453 // Clear the update info. This info will be set the next time the text is updated.
454 textUpdateInfo.Clear();
455 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout\n");
457 return updateTextType;
460 bool Controller::Relayouter::DoRelayout(Controller& controller, const Size& size, OperationsMask operationsRequired, Size& layoutSize)
462 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", &controller, size.width, size.height);
463 bool viewUpdated(false);
465 Controller::Impl& impl = *controller.mImpl;
467 // Calculate the operations to be done.
468 const OperationsMask operations = static_cast<OperationsMask>(impl.mOperationsPending & operationsRequired);
470 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
471 const CharacterIndex startIndex = textUpdateInfo.mParagraphCharacterIndex;
472 const Length requestedNumberOfCharacters = textUpdateInfo.mRequestedNumberOfCharacters;
474 // Get the current layout size.
475 VisualModelPtr& visualModel = impl.mModel->mVisualModel;
476 layoutSize = visualModel->GetLayoutSize();
478 if(NO_OPERATION != (LAYOUT & operations))
480 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
482 // Some vectors with data needed to layout and reorder may be void
483 // after the first time the text has been laid out.
484 // Fill the vectors again.
486 // Calculate the number of glyphs to layout.
487 const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
488 const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
489 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
490 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
492 const CharacterIndex lastIndex = startIndex + ((requestedNumberOfCharacters > 0u) ? requestedNumberOfCharacters - 1u : 0u);
493 const GlyphIndex startGlyphIndex = textUpdateInfo.mStartGlyphIndex;
495 // Make sure the index is not out of bound
496 if(charactersToGlyph.Count() != glyphsPerCharacter.Count() ||
497 requestedNumberOfCharacters > charactersToGlyph.Count() ||
498 (lastIndex > charactersToGlyph.Count() && charactersToGlyph.Count() > 0u))
500 std::string currentText;
501 controller.GetText(currentText);
503 DALI_LOG_ERROR("Controller::DoRelayout: Attempting to access invalid buffer\n");
504 DALI_LOG_ERROR("Current text is: %s\n", currentText.c_str());
505 DALI_LOG_ERROR("startIndex: %u, lastIndex: %u, requestedNumberOfCharacters: %u, charactersToGlyph.Count = %lu, glyphsPerCharacter.Count = %lu\n", startIndex, lastIndex, requestedNumberOfCharacters, charactersToGlyph.Count(), glyphsPerCharacter.Count());
510 const Length numberOfGlyphs = (requestedNumberOfCharacters > 0u) ? *(charactersToGlyphBuffer + lastIndex) + *(glyphsPerCharacterBuffer + lastIndex) - startGlyphIndex : 0u;
511 const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
513 if(0u == totalNumberOfGlyphs)
515 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
517 visualModel->SetLayoutSize(Size::ZERO);
520 // Nothing else to do if there is no glyphs.
521 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n");
525 // Set the layout parameters.
526 Layout::Parameters layoutParameters(size, impl.mModel);
528 // Resize the vector of positions to have the same size than the vector of glyphs.
529 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
530 glyphPositions.Resize(totalNumberOfGlyphs);
532 // Whether the last character is a new paragraph character.
533 const Character* const textBuffer = impl.mModel->mLogicalModel->mText.Begin();
534 textUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph(*(textBuffer + (impl.mModel->mLogicalModel->mText.Count() - 1u)));
535 layoutParameters.isLastNewParagraph = textUpdateInfo.mIsLastCharacterNewParagraph;
537 // The initial glyph and the number of glyphs to layout.
538 layoutParameters.startGlyphIndex = startGlyphIndex;
539 layoutParameters.numberOfGlyphs = numberOfGlyphs;
540 layoutParameters.startLineIndex = textUpdateInfo.mStartLineIndex;
541 layoutParameters.estimatedNumberOfLines = textUpdateInfo.mEstimatedNumberOfLines;
543 // Update the ellipsis
544 bool elideTextEnabled = impl.mModel->mElideEnabled;
546 if(NULL != impl.mEventData)
548 if(impl.mEventData->mPlaceholderEllipsisFlag && impl.IsShowingPlaceholderText())
550 elideTextEnabled = impl.mEventData->mIsPlaceholderElideEnabled;
552 else if(EventData::INACTIVE != impl.mEventData->mState)
554 // Disable ellipsis when editing
555 elideTextEnabled = false;
558 // Reset the scroll position in inactive state
559 if(elideTextEnabled && (impl.mEventData->mState == EventData::INACTIVE))
561 controller.ResetScrollPosition();
565 // Update the visual model.
566 bool isAutoScrollEnabled = impl.mIsAutoScrollEnabled;
568 viewUpdated = impl.mLayoutEngine.LayoutText(layoutParameters,
571 isAutoScrollEnabled);
572 impl.mIsAutoScrollEnabled = isAutoScrollEnabled;
574 viewUpdated = viewUpdated || (newLayoutSize != layoutSize);
578 layoutSize = newLayoutSize;
580 if(NO_OPERATION != (UPDATE_DIRECTION & operations))
582 impl.mIsTextDirectionRTL = false;
585 if((NO_OPERATION != (UPDATE_DIRECTION & operations)) && !visualModel->mLines.Empty())
587 impl.mIsTextDirectionRTL = visualModel->mLines[0u].direction;
590 // Sets the layout size.
591 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
593 visualModel->SetLayoutSize(layoutSize);
598 if(NO_OPERATION != (ALIGN & operations))
600 // The laid-out lines.
601 Vector<LineRun>& lines = visualModel->mLines;
603 CharacterIndex alignStartIndex = startIndex;
604 Length alignRequestedNumberOfCharacters = requestedNumberOfCharacters;
606 // the whole text needs to be full aligned.
607 // If you do not do a full aligned, only the last line of the multiline input is aligned.
608 if(impl.mEventData && impl.mEventData->mUpdateAlignment)
610 alignStartIndex = 0u;
611 alignRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
612 impl.mEventData->mUpdateAlignment = false;
615 // Need to align with the control's size as the text may contain lines
616 // starting either with left to right text or right to left.
617 impl.mLayoutEngine.Align(size,
619 alignRequestedNumberOfCharacters,
620 impl.mModel->mHorizontalAlignment,
622 impl.mModel->mAlignmentOffset,
623 impl.mLayoutDirection,
624 impl.mModel->mMatchSystemLanguageDirection);
628 #if defined(DEBUG_ENABLED)
629 std::string currentText;
630 controller.GetText(currentText);
631 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Controller::DoRelayout [%p] mImpl->mIsTextDirectionRTL[%s] [%s]\n", &controller, (impl.mIsTextDirectionRTL) ? "true" : "false", currentText.c_str());
633 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", (viewUpdated ? "true" : "false"));
637 void Controller::Relayouter::CalculateVerticalOffset(Controller& controller, const Size& controlSize)
639 Controller::Impl& impl = *controller.mImpl;
640 ModelPtr& model = impl.mModel;
641 Size layoutSize = model->mVisualModel->GetLayoutSize();
643 if(fabsf(layoutSize.height) < Math::MACHINE_EPSILON_1000)
645 // Get the line height of the default font.
646 layoutSize.height = impl.GetDefaultFontLineHeight();
649 switch(model->mVerticalAlignment)
651 case VerticalAlignment::TOP:
653 model->mScrollPosition.y = 0.f;
656 case VerticalAlignment::CENTER:
658 model->mScrollPosition.y = floorf(0.5f * (controlSize.height - layoutSize.height)); // try to avoid pixel alignment.
661 case VerticalAlignment::BOTTOM:
663 model->mScrollPosition.y = controlSize.height - layoutSize.height;
671 } // namespace Toolkit