2 * Copyright (c) 2022 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/controller/text-controller-relayouter.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/trace.h>
24 #include <dali/public-api/math/math-utils.h>
28 #include <dali-toolkit/internal/text/controller/text-controller-event-handler.h>
29 #include <dali-toolkit/internal/text/controller/text-controller-impl.h>
30 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
34 #if defined(DEBUG_ENABLED)
35 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
38 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_TEXT_PERFORMANCE_MARKER, false);
39 DALI_INIT_TRACE_FILTER(gTraceFilter2, DALI_TRACE_PERFORMANCE_MARKER, false);
41 constexpr float MAX_FLOAT = std::numeric_limits<float>::max();
43 float ConvertToEven(float value)
45 int intValue(static_cast<int>(value));
46 return static_cast<float>(intValue + (intValue & 1));
57 Size Controller::Relayouter::CalculateLayoutSizeOnRequiredControllerSize(Controller& controller, const Size& requestedControllerSize, const OperationsMask& requestedOperationsMask)
59 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->CalculateLayoutSizeOnRequiredControllerSize\n");
60 Size calculatedLayoutSize;
62 Controller::Impl& impl = *controller.mImpl;
63 ModelPtr& model = impl.mModel;
64 VisualModelPtr& visualModel = model->mVisualModel;
66 // Operations that can be done only once until the text changes.
67 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>(CONVERT_TO_UTF32 |
75 const OperationsMask sizeOperations = static_cast<OperationsMask>(LAYOUT | ALIGN | REORDER);
77 // Set the update info to relayout the whole text.
78 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
79 if((0 == textUpdateInfo.mNumberOfCharactersToAdd) &&
80 (0 == textUpdateInfo.mPreviousNumberOfCharacters) &&
81 ((visualModel->mControlSize.width < Math::MACHINE_EPSILON_1000) || (visualModel->mControlSize.height < Math::MACHINE_EPSILON_1000)))
83 textUpdateInfo.mNumberOfCharactersToAdd = model->mLogicalModel->mText.Count();
85 textUpdateInfo.mParagraphCharacterIndex = 0u;
86 textUpdateInfo.mRequestedNumberOfCharacters = model->mLogicalModel->mText.Count();
88 // Get a reference to the pending operations member
89 OperationsMask& operationsPending = impl.mOperationsPending;
91 // Store the actual control's size to restore later.
92 const Size actualControlSize = visualModel->mControlSize;
94 // This is to keep Index to the first character to be updated.
95 // Then restore it after calling Clear method.
96 auto updateInfoCharIndexBackup = textUpdateInfo.mCharacterIndex;
98 // Whether the text control is editable
99 const bool isEditable = NULL != impl.mEventData;
103 if(NO_OPERATION != (VALIDATE_FONTS & operationsPending) &&
104 textUpdateInfo.mCharacterIndex == static_cast<CharacterIndex>(-1))
106 impl.ClearFontData();
107 updateInfoCharIndexBackup = textUpdateInfo.mCharacterIndex;
110 impl.UpdateModel(onlyOnceOperations);
112 // Layout the text for the new width.
113 operationsPending = static_cast<OperationsMask>(operationsPending | requestedOperationsMask);
116 requestedControllerSize,
117 static_cast<OperationsMask>(onlyOnceOperations | requestedOperationsMask),
118 calculatedLayoutSize);
120 textUpdateInfo.Clear();
121 textUpdateInfo.mClearAll = true;
123 // Do not do again the only once operations.
124 operationsPending = static_cast<OperationsMask>(operationsPending & ~onlyOnceOperations);
128 // Layout the text for the new width.
129 // Apply the pending operations, requested operations and the only once operations.
130 // Then remove onlyOnceOperations
131 operationsPending = static_cast<OperationsMask>(operationsPending | requestedOperationsMask | onlyOnceOperations);
133 // Make sure the model is up-to-date before layouting
134 impl.UpdateModel(static_cast<OperationsMask>(operationsPending & ~UPDATE_LAYOUT_SIZE));
137 requestedControllerSize,
138 static_cast<OperationsMask>(operationsPending & ~UPDATE_LAYOUT_SIZE),
139 calculatedLayoutSize);
141 // Clear the update info. This info will be set the next time the text is updated.
142 textUpdateInfo.Clear();
144 //TODO: Refactor "DoRelayout" and extract common code of size calculation without modifying attributes of mVisualModel,
145 //TODO: then calculate GlyphPositions. Lines, Size, Layout for Natural-Size
146 //TODO: and utilize the values in OperationsPending and TextUpdateInfo without changing the original one.
147 //TODO: Also it will improve performance because there is no need todo FullRelyout on the next need for layouting.
150 // FullRelayoutNeeded should be true because DoRelayout is MAX_FLOAT, MAX_FLOAT.
151 // By this no need to take backup and restore it.
152 textUpdateInfo.mFullRelayoutNeeded = true;
154 // Restore mCharacterIndex. Because "Clear" set it to the maximum integer.
155 // The "CalculateTextUpdateIndices" does not work proprely because the mCharacterIndex will be greater than mPreviousNumberOfCharacters.
156 // Which apply an assumption to update only the last paragraph. That could cause many of out of index crashes.
157 textUpdateInfo.mCharacterIndex = updateInfoCharIndexBackup;
159 // Do the size related operations again.
160 operationsPending = static_cast<OperationsMask>(operationsPending | sizeOperations);
162 // Restore the actual control's size.
163 visualModel->mControlSize = actualControlSize;
165 return calculatedLayoutSize;
168 Vector3 Controller::Relayouter::GetNaturalSize(Controller& controller)
170 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n");
171 DALI_TRACE_SCOPE(gTraceFilter, "DALI_TEXT_GET_NATURAL_SIZE");
172 Vector3 naturalSizeVec3;
174 // Make sure the model is up-to-date before layouting
175 EventHandler::ProcessModifyEvents(controller);
177 Controller::Impl& impl = *controller.mImpl;
178 ModelPtr& model = impl.mModel;
179 VisualModelPtr& visualModel = model->mVisualModel;
181 if(impl.mRecalculateNaturalSize)
185 // Layout the text for the new width.
186 OperationsMask requestedOperationsMask = static_cast<OperationsMask>(LAYOUT | REORDER);
187 Size sizeMaxWidthAndMaxHeight = Size(MAX_FLOAT, MAX_FLOAT);
189 naturalSize = CalculateLayoutSizeOnRequiredControllerSize(controller, sizeMaxWidthAndMaxHeight, requestedOperationsMask);
191 // Stores the natural size to avoid recalculate it again
192 // unless the text/style changes.
193 visualModel->SetNaturalSize(naturalSize);
194 naturalSizeVec3 = naturalSize;
196 impl.mRecalculateNaturalSize = false;
198 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSizeVec3.x, naturalSizeVec3.y, naturalSizeVec3.z);
202 naturalSizeVec3 = visualModel->GetNaturalSize();
204 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSizeVec3.x, naturalSizeVec3.y, naturalSizeVec3.z);
207 naturalSizeVec3.x = ConvertToEven(naturalSizeVec3.x);
208 naturalSizeVec3.y = ConvertToEven(naturalSizeVec3.y);
210 return naturalSizeVec3;
213 bool Controller::Relayouter::CheckForTextFit(Controller& controller, float pointSize, const Size& layoutSize)
216 Controller::Impl& impl = *controller.mImpl;
217 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
218 impl.mFontDefaults->mFitPointSize = pointSize;
219 impl.mFontDefaults->sizeDefined = true;
220 impl.ClearFontData();
222 // Operations that can be done only once until the text changes.
223 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>(CONVERT_TO_UTF32 |
231 textUpdateInfo.mParagraphCharacterIndex = 0u;
232 textUpdateInfo.mRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
234 // Make sure the model is up-to-date before layouting
235 impl.UpdateModel(onlyOnceOperations);
238 Size(layoutSize.width, MAX_FLOAT),
239 static_cast<OperationsMask>(onlyOnceOperations | LAYOUT),
242 // Clear the update info. This info will be set the next time the text is updated.
243 textUpdateInfo.Clear();
244 textUpdateInfo.mClearAll = true;
246 if(textSize.width > layoutSize.width || textSize.height > layoutSize.height)
253 void Controller::Relayouter::FitPointSizeforLayout(Controller& controller, const Size& layoutSize)
255 Controller::Impl& impl = *controller.mImpl;
257 const OperationsMask operations = impl.mOperationsPending;
258 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations) || impl.mTextFitContentSize != layoutSize)
260 DALI_TRACE_SCOPE(gTraceFilter, "DALI_TEXT_FIT_LAYOUT");
261 ModelPtr& model = impl.mModel;
263 bool actualellipsis = model->mElideEnabled;
264 float minPointSize = impl.mTextFitMinSize;
265 float maxPointSize = impl.mTextFitMaxSize;
266 float pointInterval = impl.mTextFitStepSize;
267 float currentFitPointSize = impl.mFontDefaults->mFitPointSize;
268 float currentDefaultLineSize = impl.mLayoutEngine.GetDefaultLineSize();
269 // Instead of using the LineSize of the current TextLabel, the LineSize set in TextFit is used.
270 impl.SetDefaultLineSize(impl.mTextFitLineSize);
272 model->mElideEnabled = false;
275 if(pointInterval < 1.f)
277 impl.mTextFitStepSize = pointInterval = 1.0f;
279 uint32_t pointSizeRange = static_cast<uint32_t>(ceil((maxPointSize - minPointSize) / pointInterval));
281 // Ensure minPointSize + pointSizeRange * pointInverval >= maxPointSize
282 while(minPointSize + static_cast<float>(pointSizeRange) * pointInterval < maxPointSize)
287 uint32_t bestSizeIndex = 0;
288 uint32_t minIndex = bestSizeIndex + 1u;
289 uint32_t maxIndex = pointSizeRange + 1u;
291 bool bestSizeUpdatedLatest = false;
292 // Find best size as binary search.
293 // Range format as [l r). (left closed, right opened)
294 // It mean, we already check all i < l is valid, and r <= i is invalid.
295 // Below binary search will check m = (l+r)/2 point.
296 // Search area sperate as [l m) or [m+1 r)
298 // Basically, we can assume that 0 (minPointSize) is always valid.
299 // Now, we will check [1 pointSizeRange] range s.t. pointSizeRange mean the maxPointSize
300 while(minIndex < maxIndex)
302 uint32_t testIndex = minIndex + ((maxIndex - minIndex) >> 1u);
303 const float testPointSize = std::min(maxPointSize, minPointSize + static_cast<float>(testIndex) * pointInterval);
305 if(CheckForTextFit(controller, testPointSize, layoutSize))
307 bestSizeUpdatedLatest = true;
309 bestSizeIndex = testIndex;
310 minIndex = testIndex + 1u;
314 bestSizeUpdatedLatest = false;
315 maxIndex = testIndex;
318 const float bestPointSize = std::min(maxPointSize, minPointSize + static_cast<float>(bestSizeIndex) * pointInterval);
320 // Best point size was not updated. re-run so the TextFit should be fitted really.
321 if(!bestSizeUpdatedLatest)
323 CheckForTextFit(controller, bestPointSize, layoutSize);
326 model->mElideEnabled = actualellipsis;
327 if(!Dali::Equals(currentFitPointSize, bestPointSize))
329 impl.mTextFitChanged = true;
331 // Revert back to the original TextLabel LineSize.
332 impl.SetDefaultLineSize(currentDefaultLineSize);
333 impl.mFontDefaults->mFitPointSize = bestPointSize;
334 impl.mFontDefaults->sizeDefined = true;
335 impl.ClearFontData();
339 float Controller::Relayouter::GetHeightForWidth(Controller& controller, float width)
341 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", &controller, width);
342 DALI_TRACE_SCOPE(gTraceFilter, "DALI_TEXT_GET_HEIGHT_FOR_WIDTH");
344 // Make sure the model is up-to-date before layouting
345 EventHandler::ProcessModifyEvents(controller);
347 Controller::Impl& impl = *controller.mImpl;
348 ModelPtr& model = impl.mModel;
349 VisualModelPtr& visualModel = model->mVisualModel;
350 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
354 if(fabsf(width - visualModel->mControlSize.width) > Math::MACHINE_EPSILON_1000 ||
355 textUpdateInfo.mFullRelayoutNeeded ||
356 textUpdateInfo.mClearAll)
358 // Layout the text for the new width.
359 OperationsMask requestedOperationsMask = static_cast<OperationsMask>(LAYOUT);
360 Size sizeRequestedWidthAndMaxHeight = Size(width, MAX_FLOAT);
362 layoutSize = CalculateLayoutSizeOnRequiredControllerSize(controller, sizeRequestedWidthAndMaxHeight, requestedOperationsMask);
364 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height);
368 layoutSize = visualModel->GetLayoutSize();
369 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height);
372 return layoutSize.height;
375 Controller::UpdateTextType Controller::Relayouter::Relayout(Controller& controller, const Size& size, Dali::LayoutDirection::Type layoutDirection)
377 Controller::Impl& impl = *controller.mImpl;
378 ModelPtr& model = impl.mModel;
379 VisualModelPtr& visualModel = model->mVisualModel;
380 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
382 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", &controller, size.width, size.height, impl.mIsAutoScrollEnabled ? "true" : "false");
383 DALI_TRACE_SCOPE(gTraceFilter, "DALI_TEXT_RELAYOUT");
385 UpdateTextType updateTextType = NONE_UPDATED;
387 if((size.width < Math::MACHINE_EPSILON_1000) || (size.height < Math::MACHINE_EPSILON_1000))
389 if(0u != visualModel->mGlyphPositions.Count())
391 visualModel->mGlyphPositions.Clear();
392 updateTextType = MODEL_UPDATED;
395 // Clear the update info. This info will be set the next time the text is updated.
396 textUpdateInfo.Clear();
398 // Not worth to relayout if width or height is equal to zero.
399 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n");
401 return updateTextType;
404 // Whether a new size has been set.
405 const bool newSize = (size != visualModel->mControlSize);
407 // Get a reference to the pending operations member
408 OperationsMask& operationsPending = impl.mOperationsPending;
412 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", visualModel->mControlSize.width, visualModel->mControlSize.height);
414 if((0 == textUpdateInfo.mNumberOfCharactersToAdd) &&
415 (0 == textUpdateInfo.mPreviousNumberOfCharacters) &&
416 ((visualModel->mControlSize.width < Math::MACHINE_EPSILON_1000) || (visualModel->mControlSize.height < Math::MACHINE_EPSILON_1000)))
418 textUpdateInfo.mNumberOfCharactersToAdd = model->mLogicalModel->mText.Count();
421 // Layout operations that need to be done if the size changes.
422 operationsPending = static_cast<OperationsMask>(operationsPending |
427 // Set the update info to relayout the whole text.
428 textUpdateInfo.mFullRelayoutNeeded = true;
429 textUpdateInfo.mCharacterIndex = 0u;
431 // Store the size used to layout the text.
432 visualModel->mControlSize = size;
435 // Whether there are modify events.
436 if(0u != impl.mModifyEvents.Count())
438 // Style operations that need to be done if the text is modified.
439 operationsPending = static_cast<OperationsMask>(operationsPending | COLOR);
442 // Set the update info to elide the text.
443 if(model->mElideEnabled ||
444 ((NULL != impl.mEventData) && impl.mEventData->mIsPlaceholderElideEnabled))
446 // Update Text layout for applying elided
447 operationsPending = static_cast<OperationsMask>(operationsPending |
452 textUpdateInfo.mFullRelayoutNeeded = true;
453 textUpdateInfo.mCharacterIndex = 0u;
456 bool layoutDirectionChanged = false;
457 if(impl.mLayoutDirection != layoutDirection)
459 // Flag to indicate that the layout direction has changed.
460 layoutDirectionChanged = true;
461 // Clear the update info. This info will be set the next time the text is updated.
462 textUpdateInfo.mClearAll = true;
463 // Apply modifications to the model
464 // Shape the text again is needed because characters like '()[]{}' have to be mirrored and the glyphs generated again.
465 operationsPending = static_cast<OperationsMask>(operationsPending |
473 impl.mLayoutDirection = layoutDirection;
476 // Make sure the model is up-to-date before layouting.
477 EventHandler::ProcessModifyEvents(controller);
478 bool updated = impl.UpdateModel(operationsPending);
482 updated = DoRelayout(impl, size, operationsPending, layoutSize) || updated;
486 updateTextType = MODEL_UPDATED;
489 // Do not re-do any operation until something changes.
490 operationsPending = NO_OPERATION;
491 model->mScrollPositionLast = model->mScrollPosition;
493 // Whether the text control is editable
494 const bool isEditable = NULL != impl.mEventData;
496 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
498 if(newSize && isEditable)
500 offset = model->mScrollPosition;
503 if(!isEditable || !controller.IsMultiLineEnabled())
505 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
506 CalculateVerticalOffset(impl, size);
510 // If layoutSize is bigger than size, vertical align has no meaning.
511 if(layoutSize.y < size.y)
513 CalculateVerticalOffset(impl, size);
516 impl.mEventData->mScrollAfterDelete = false;
523 if(newSize || layoutDirectionChanged)
525 // If there is a new size or layout direction is changed, the scroll position needs to be clamped.
526 impl.ClampHorizontalScroll(layoutSize);
528 // Update the decorator's positions is needed if there is a new size.
529 impl.mEventData->mDecorator->UpdatePositions(model->mScrollPosition - offset);
531 // All decorator elements need to be updated.
532 if(EventData::IsEditingState(impl.mEventData->mState))
534 impl.mEventData->mScrollAfterUpdatePosition = true;
535 impl.mEventData->mUpdateCursorPosition = true;
536 impl.mEventData->mUpdateGrabHandlePosition = true;
538 else if(impl.mEventData->mState == EventData::SELECTING)
540 impl.mEventData->mUpdateHighlightBox = true;
544 // Move the cursor, grab handle etc.
545 if(impl.ProcessInputEvents())
547 updateTextType = static_cast<UpdateTextType>(updateTextType | DECORATOR_UPDATED);
551 // Clear the update info. This info will be set the next time the text is updated.
552 textUpdateInfo.Clear();
553 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout\n");
555 return updateTextType;
558 bool Controller::Relayouter::DoRelayout(Controller::Impl& impl, const Size& size, OperationsMask operationsRequired, Size& layoutSize)
560 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::Relayouter::DoRelayout %p size %f,%f\n", &impl, size.width, size.height);
561 DALI_TRACE_SCOPE(gTraceFilter2, "DALI_TEXT_DORELAYOUT");
562 bool viewUpdated(false);
564 // Calculate the operations to be done.
565 const OperationsMask operations = static_cast<OperationsMask>(impl.mOperationsPending & operationsRequired);
567 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
568 const CharacterIndex startIndex = textUpdateInfo.mParagraphCharacterIndex;
569 const Length requestedNumberOfCharacters = textUpdateInfo.mRequestedNumberOfCharacters;
571 // Get the current layout size.
572 VisualModelPtr& visualModel = impl.mModel->mVisualModel;
573 layoutSize = visualModel->GetLayoutSize();
575 if(NO_OPERATION != (LAYOUT & operations))
577 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
579 // Some vectors with data needed to layout and reorder may be void
580 // after the first time the text has been laid out.
581 // Fill the vectors again.
583 // Calculate the number of glyphs to layout.
584 const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
585 const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
586 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
587 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
589 const CharacterIndex lastIndex = startIndex + ((requestedNumberOfCharacters > 0u) ? requestedNumberOfCharacters - 1u : 0u);
590 const GlyphIndex startGlyphIndex = textUpdateInfo.mStartGlyphIndex;
592 // Make sure the index is not out of bound
593 if(charactersToGlyph.Count() != glyphsPerCharacter.Count() ||
594 requestedNumberOfCharacters > charactersToGlyph.Count() ||
595 (lastIndex > charactersToGlyph.Count() && charactersToGlyph.Count() > 0u))
597 std::string currentText;
598 impl.GetText(currentText);
600 DALI_LOG_ERROR("Controller::DoRelayout: Attempting to access invalid buffer\n");
601 DALI_LOG_ERROR("Current text is: %s\n", currentText.c_str());
602 DALI_LOG_ERROR("startIndex: %u, lastIndex: %u, requestedNumberOfCharacters: %u, charactersToGlyph.Count = %lu, glyphsPerCharacter.Count = %lu\n", startIndex, lastIndex, requestedNumberOfCharacters, charactersToGlyph.Count(), glyphsPerCharacter.Count());
607 const Length numberOfGlyphs = (requestedNumberOfCharacters > 0u) ? *(charactersToGlyphBuffer + lastIndex) + *(glyphsPerCharacterBuffer + lastIndex) - startGlyphIndex : 0u;
608 const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
610 if(0u == totalNumberOfGlyphs)
612 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
614 visualModel->SetLayoutSize(Size::ZERO);
617 // Nothing else to do if there is no glyphs.
618 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n");
622 // Set the layout parameters.
623 Layout::Parameters layoutParameters(size, impl.mModel);
625 // Resize the vector of positions to have the same size than the vector of glyphs.
626 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
627 glyphPositions.Resize(totalNumberOfGlyphs);
629 // Whether the last character is a new paragraph character.
630 const Character* const textBuffer = impl.mModel->mLogicalModel->mText.Begin();
631 textUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph(*(textBuffer + (impl.mModel->mLogicalModel->mText.Count() - 1u)));
632 layoutParameters.isLastNewParagraph = textUpdateInfo.mIsLastCharacterNewParagraph;
634 // The initial glyph and the number of glyphs to layout.
635 layoutParameters.startGlyphIndex = startGlyphIndex;
636 layoutParameters.numberOfGlyphs = numberOfGlyphs;
637 layoutParameters.startLineIndex = textUpdateInfo.mStartLineIndex;
638 layoutParameters.estimatedNumberOfLines = textUpdateInfo.mEstimatedNumberOfLines;
640 // Update the ellipsis
641 bool elideTextEnabled = impl.mModel->mElideEnabled;
642 auto ellipsisPosition = impl.mModel->mEllipsisPosition;
644 if(NULL != impl.mEventData)
646 if(impl.mEventData->mPlaceholderEllipsisFlag && impl.IsShowingPlaceholderText())
648 elideTextEnabled = impl.mEventData->mIsPlaceholderElideEnabled;
650 else if(EventData::INACTIVE != impl.mEventData->mState)
652 // Disable ellipsis when editing
653 elideTextEnabled = false;
656 // Reset the scroll position in inactive state
657 if(elideTextEnabled && (impl.mEventData->mState == EventData::INACTIVE))
659 impl.ResetScrollPosition();
663 // Update the visual model.
664 bool isAutoScrollEnabled = impl.mIsAutoScrollEnabled;
665 bool isAutoScrollMaxTextureExceeded = impl.mIsAutoScrollMaxTextureExceeded;
666 bool isHiddenInputEnabled = false;
667 if(impl.mHiddenInput && impl.mEventData != nullptr && impl.mHiddenInput->GetHideMode() != Toolkit::HiddenInput::Mode::HIDE_NONE)
669 isHiddenInputEnabled = true;
673 viewUpdated = impl.mLayoutEngine.LayoutText(layoutParameters,
677 isAutoScrollMaxTextureExceeded,
678 isHiddenInputEnabled,
680 impl.mIsAutoScrollEnabled = isAutoScrollEnabled;
682 viewUpdated = viewUpdated || (newLayoutSize != layoutSize);
686 layoutSize = newLayoutSize;
688 if(NO_OPERATION != (UPDATE_DIRECTION & operations))
690 impl.mIsTextDirectionRTL = false;
693 if((NO_OPERATION != (UPDATE_DIRECTION & operations)) && !visualModel->mLines.Empty())
695 impl.mIsTextDirectionRTL = visualModel->mLines[0u].direction;
698 // Sets the layout size.
699 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
701 visualModel->SetLayoutSize(layoutSize);
706 if(NO_OPERATION != (ALIGN & operations))
708 DoRelayoutHorizontalAlignment(impl, size, startIndex, requestedNumberOfCharacters);
711 #if defined(DEBUG_ENABLED)
712 std::string currentText;
713 impl.GetText(currentText);
714 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Controller::Relayouter::DoRelayout [%p] mImpl->mIsTextDirectionRTL[%s] [%s]\n", &impl, (impl.mIsTextDirectionRTL) ? "true" : "false", currentText.c_str());
716 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayouter::DoRelayout, view updated %s\n", (viewUpdated ? "true" : "false"));
720 void Controller::Relayouter::DoRelayoutHorizontalAlignment(Controller::Impl& impl,
722 const CharacterIndex startIndex,
723 const Length requestedNumberOfCharacters)
726 VisualModelPtr& visualModel = impl.mModel->mVisualModel;
728 // The laid-out lines.
729 Vector<LineRun>& lines = visualModel->mLines;
731 CharacterIndex alignStartIndex = startIndex;
732 Length alignRequestedNumberOfCharacters = requestedNumberOfCharacters;
734 // the whole text needs to be full aligned.
735 // If you do not do a full aligned, only the last line of the multiline input is aligned.
736 if(impl.mEventData && impl.mEventData->mUpdateAlignment)
738 alignStartIndex = 0u;
739 alignRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
740 impl.mEventData->mUpdateAlignment = false;
743 // If there is no BoundedParagraphRuns then apply the alignment of controller.
744 // Check whether the layout is single line. It's needed to apply one alignment for single-line.
745 // In single-line layout case we need to check whether to follow the alignment of controller or the first BoundedParagraph.
746 // Apply BoundedParagraph's alignment if and only if there is one BoundedParagraph contains all characters. Otherwise follow controller's alignment.
747 const bool isFollowControllerAlignment = ((impl.mModel->GetNumberOfBoundedParagraphRuns() == 0u) ||
748 ((Layout::Engine::SINGLE_LINE_BOX == impl.mLayoutEngine.GetLayout()) &&
749 (impl.mModel->GetBoundedParagraphRuns()[0].characterRun.numberOfCharacters != impl.mModel->mLogicalModel->mText.Count())));
751 if(isFollowControllerAlignment)
753 // Need to align with the control's size as the text may contain lines
754 // starting either with left to right text or right to left.
755 impl.mLayoutEngine.Align(size,
757 alignRequestedNumberOfCharacters,
758 impl.mModel->mHorizontalAlignment,
760 impl.mModel->mAlignmentOffset,
761 impl.mLayoutDirection,
762 (impl.mModel->mMatchLayoutDirection != DevelText::MatchLayoutDirection::CONTENTS));
766 //Override the controller horizontal-alignment by horizontal-alignment of bounded paragraph.
767 const Length& numberOfBoundedParagraphRuns = impl.mModel->GetNumberOfBoundedParagraphRuns();
768 const Vector<BoundedParagraphRun>& boundedParagraphRuns = impl.mModel->GetBoundedParagraphRuns();
769 const CharacterIndex alignEndIndex = alignStartIndex + alignRequestedNumberOfCharacters - 1u;
771 Length alignIndex = alignStartIndex;
772 Length boundedParagraphRunIndex = 0u;
774 while(alignIndex <= alignEndIndex && boundedParagraphRunIndex < numberOfBoundedParagraphRuns)
776 //BP: BoundedParagraph
777 const BoundedParagraphRun& boundedParagraphRun = boundedParagraphRuns[boundedParagraphRunIndex];
778 const CharacterIndex& characterStartIndexBP = boundedParagraphRun.characterRun.characterIndex;
779 const Length& numberOfCharactersBP = boundedParagraphRun.characterRun.numberOfCharacters;
780 const CharacterIndex characterEndIndexBP = characterStartIndexBP + numberOfCharactersBP - 1u;
782 CharacterIndex decidedAlignStartIndex = alignIndex;
783 Length decidedAlignNumberOfCharacters = alignEndIndex - alignIndex + 1u;
784 Text::HorizontalAlignment::Type decidedHorizontalAlignment = impl.mModel->mHorizontalAlignment;
787 * Shortcuts to explain indexes cases:
789 * AS: Alignment Start Index
790 * AE: Alignment End Index
791 * PS: Paragraph Start Index
792 * PE: Paragraph End Index
793 * B: BoundedParagraph Alignment
798 if(alignIndex < characterStartIndexBP && characterStartIndexBP <= alignEndIndex) /// AS.MMMMMM.PS--------AE
800 // Alignment from "Alignment Start Index" to index before "Paragraph Start Index" according to "Model Alignment"
801 decidedAlignStartIndex = alignIndex;
802 decidedAlignNumberOfCharacters = characterStartIndexBP - alignIndex;
803 decidedHorizontalAlignment = impl.mModel->mHorizontalAlignment;
805 // Need to re-heck the case of current bounded paragraph
806 alignIndex = characterStartIndexBP; // Shift AS to be PS
808 else if((characterStartIndexBP <= alignIndex && alignIndex <= characterEndIndexBP) || /// ---PS.BBBBBBB.AS.BBBBBBB.PE---
809 (characterStartIndexBP <= alignEndIndex && alignEndIndex <= characterEndIndexBP)) /// ---PS.BBBBBB.AE.BBBBBBB.PE---
811 // Alignment from "Paragraph Start Index" to "Paragraph End Index" according to "BoundedParagraph Alignment"
812 decidedAlignStartIndex = characterStartIndexBP;
813 decidedAlignNumberOfCharacters = numberOfCharactersBP;
814 decidedHorizontalAlignment = boundedParagraphRun.horizontalAlignmentDefined ? boundedParagraphRun.horizontalAlignment : impl.mModel->mHorizontalAlignment;
816 alignIndex = characterEndIndexBP + 1u; // Shift AS to be after PE direct
817 boundedParagraphRunIndex++; // Align then check the case of next bounded paragraph
821 boundedParagraphRunIndex++; // Check the case of next bounded paragraph
825 impl.mLayoutEngine.Align(size,
826 decidedAlignStartIndex,
827 decidedAlignNumberOfCharacters,
828 decidedHorizontalAlignment,
830 impl.mModel->mAlignmentOffset,
831 impl.mLayoutDirection,
832 (impl.mModel->mMatchLayoutDirection != DevelText::MatchLayoutDirection::CONTENTS));
835 //Align the remaining that is not aligned
836 if(alignIndex <= alignEndIndex)
838 impl.mLayoutEngine.Align(size,
840 (alignEndIndex - alignIndex + 1u),
841 impl.mModel->mHorizontalAlignment,
843 impl.mModel->mAlignmentOffset,
844 impl.mLayoutDirection,
845 (impl.mModel->mMatchLayoutDirection != DevelText::MatchLayoutDirection::CONTENTS));
850 void Controller::Relayouter::CalculateVerticalOffset(Controller::Impl& impl, const Size& controlSize)
852 ModelPtr& model = impl.mModel;
853 VisualModelPtr& visualModel = model->mVisualModel;
854 Size layoutSize = model->mVisualModel->GetLayoutSize();
855 Size oldLayoutSize = layoutSize;
857 bool needRecalc = false;
858 float defaultFontLineHeight = impl.GetDefaultFontLineHeight();
860 if(fabsf(layoutSize.height) < Math::MACHINE_EPSILON_1000)
862 // Get the line height of the default font.
863 layoutSize.height = defaultFontLineHeight;
866 // Whether the text control is editable
867 const bool isEditable = NULL != impl.mEventData;
868 if(isEditable && !Dali::Equals(layoutSize.height, defaultFontLineHeight) && impl.IsShowingPlaceholderText())
870 // This code prevents the wrong positioning of cursor when the layout size is bigger/smaller than defaultFontLineHeight.
871 // This situation occurs when the size of placeholder text is different from the default text.
872 layoutSize.height = defaultFontLineHeight;
876 switch(model->mVerticalAlignment)
878 case VerticalAlignment::TOP:
880 model->mScrollPosition.y = 0.f;
884 case VerticalAlignment::CENTER:
886 model->mScrollPosition.y = floorf(0.5f * (controlSize.height - layoutSize.height)); // try to avoid pixel alignment.
887 if(needRecalc) offsetY = floorf(0.5f * (layoutSize.height - oldLayoutSize.height));
890 case VerticalAlignment::BOTTOM:
892 model->mScrollPosition.y = controlSize.height - layoutSize.height;
893 if(needRecalc) offsetY = layoutSize.height - oldLayoutSize.height;
900 // Update glyphPositions according to recalculation.
901 const Length positionCount = visualModel->mGlyphPositions.Count();
902 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
903 for(Length index = 0u; index < positionCount; index++)
905 glyphPositions[index].y += offsetY;
912 } // namespace Toolkit