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>
26 #include <dali-toolkit/internal/text/controller/text-controller-event-handler.h>
27 #include <dali-toolkit/internal/text/controller/text-controller-impl.h>
28 #include <dali-toolkit/internal/text/layouts/layout-parameters.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));
52 Size Controller::Relayouter::CalculateLayoutSizeOnRequiredControllerSize(Controller& controller, const Size& requestedControllerSize, const OperationsMask& requestedOperationsMask)
54 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->CalculateLayoutSizeOnRequiredControllerSize\n");
55 Size calculatedLayoutSize;
57 Controller::Impl& impl = *controller.mImpl;
58 ModelPtr& model = impl.mModel;
59 VisualModelPtr& visualModel = model->mVisualModel;
61 // Operations that can be done only once until the text changes.
62 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>(CONVERT_TO_UTF32 |
70 const OperationsMask sizeOperations = static_cast<OperationsMask>(LAYOUT | ALIGN | REORDER);
72 // Set the update info to relayout the whole text.
73 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
74 if((0 == textUpdateInfo.mNumberOfCharactersToAdd) &&
75 (0 == textUpdateInfo.mPreviousNumberOfCharacters) &&
76 ((visualModel->mControlSize.width < Math::MACHINE_EPSILON_1000) || (visualModel->mControlSize.height < Math::MACHINE_EPSILON_1000)))
78 textUpdateInfo.mNumberOfCharactersToAdd = model->mLogicalModel->mText.Count();
80 textUpdateInfo.mParagraphCharacterIndex = 0u;
81 textUpdateInfo.mRequestedNumberOfCharacters = model->mLogicalModel->mText.Count();
83 // Get a reference to the pending operations member
84 OperationsMask& operationsPending = impl.mOperationsPending;
86 // Store the actual control's size to restore later.
87 const Size actualControlSize = visualModel->mControlSize;
89 // This is to keep Index to the first character to be updated.
90 // Then restore it after calling Clear method.
91 auto updateInfoCharIndexBackup = textUpdateInfo.mCharacterIndex;
93 // Whether the text control is editable
94 const bool isEditable = NULL != impl.mEventData;
98 impl.UpdateModel(onlyOnceOperations);
100 // Layout the text for the new width.
101 operationsPending = static_cast<OperationsMask>(operationsPending | requestedOperationsMask);
104 requestedControllerSize,
105 static_cast<OperationsMask>(onlyOnceOperations | requestedOperationsMask),
106 calculatedLayoutSize);
108 textUpdateInfo.Clear();
109 textUpdateInfo.mClearAll = true;
111 // Do not do again the only once operations.
112 operationsPending = static_cast<OperationsMask>(operationsPending & ~onlyOnceOperations);
116 // Layout the text for the new width.
117 // Apply the pending operations, requested operations and the only once operations.
118 // Then remove onlyOnceOperations
119 operationsPending = static_cast<OperationsMask>(operationsPending | requestedOperationsMask | onlyOnceOperations);
121 // Make sure the model is up-to-date before layouting
122 impl.UpdateModel(static_cast<OperationsMask>(operationsPending & ~UPDATE_LAYOUT_SIZE));
125 requestedControllerSize,
126 static_cast<OperationsMask>(operationsPending & ~UPDATE_LAYOUT_SIZE),
127 calculatedLayoutSize);
129 // Clear the update info. This info will be set the next time the text is updated.
130 textUpdateInfo.Clear();
132 //TODO: Refactor "DoRelayout" and extract common code of size calculation without modifying attributes of mVisualModel,
133 //TODO: then calculate GlyphPositions. Lines, Size, Layout for Natural-Size
134 //TODO: and utilize the values in OperationsPending and TextUpdateInfo without changing the original one.
135 //TODO: Also it will improve performance because there is no need todo FullRelyout on the next need for layouting.
138 // FullRelayoutNeeded should be true because DoRelayout is MAX_FLOAT, MAX_FLOAT.
139 // By this no need to take backup and restore it.
140 textUpdateInfo.mFullRelayoutNeeded = true;
142 // Restore mCharacterIndex. Because "Clear" set it to the maximum integer.
143 // The "CalculateTextUpdateIndices" does not work proprely because the mCharacterIndex will be greater than mPreviousNumberOfCharacters.
144 // Which apply an assumption to update only the last paragraph. That could cause many of out of index crashes.
145 textUpdateInfo.mCharacterIndex = updateInfoCharIndexBackup;
147 // Do the size related operations again.
148 operationsPending = static_cast<OperationsMask>(operationsPending | sizeOperations);
150 // Restore the actual control's size.
151 visualModel->mControlSize = actualControlSize;
153 return calculatedLayoutSize;
156 Vector3 Controller::Relayouter::GetNaturalSize(Controller& controller)
158 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n");
159 Vector3 naturalSizeVec3;
161 // Make sure the model is up-to-date before layouting
162 EventHandler::ProcessModifyEvents(controller);
164 Controller::Impl& impl = *controller.mImpl;
165 ModelPtr& model = impl.mModel;
166 VisualModelPtr& visualModel = model->mVisualModel;
168 if(impl.mRecalculateNaturalSize)
172 // Layout the text for the new width.
173 OperationsMask requestedOperationsMask = static_cast<OperationsMask>(LAYOUT | REORDER);
174 Size sizeMaxWidthAndMaxHeight = Size(MAX_FLOAT, MAX_FLOAT);
176 naturalSize = CalculateLayoutSizeOnRequiredControllerSize(controller, sizeMaxWidthAndMaxHeight, requestedOperationsMask);
178 // Stores the natural size to avoid recalculate it again
179 // unless the text/style changes.
180 visualModel->SetNaturalSize(naturalSize);
181 naturalSizeVec3 = naturalSize;
183 impl.mRecalculateNaturalSize = false;
185 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSizeVec3.x, naturalSizeVec3.y, naturalSizeVec3.z);
189 naturalSizeVec3 = visualModel->GetNaturalSize();
191 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSizeVec3.x, naturalSizeVec3.y, naturalSizeVec3.z);
194 naturalSizeVec3.x = ConvertToEven(naturalSizeVec3.x);
195 naturalSizeVec3.y = ConvertToEven(naturalSizeVec3.y);
197 return naturalSizeVec3;
200 bool Controller::Relayouter::CheckForTextFit(Controller& controller, float pointSize, const Size& layoutSize)
203 Controller::Impl& impl = *controller.mImpl;
204 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
205 impl.mFontDefaults->mFitPointSize = pointSize;
206 impl.mFontDefaults->sizeDefined = true;
207 impl.ClearFontData();
209 // Operations that can be done only once until the text changes.
210 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>(CONVERT_TO_UTF32 |
218 textUpdateInfo.mParagraphCharacterIndex = 0u;
219 textUpdateInfo.mRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
221 // Make sure the model is up-to-date before layouting
222 impl.UpdateModel(onlyOnceOperations);
225 Size(layoutSize.width, MAX_FLOAT),
226 static_cast<OperationsMask>(onlyOnceOperations | LAYOUT),
229 // Clear the update info. This info will be set the next time the text is updated.
230 textUpdateInfo.Clear();
231 textUpdateInfo.mClearAll = true;
233 if(textSize.width >= layoutSize.width || textSize.height >= layoutSize.height)
240 void Controller::Relayouter::FitPointSizeforLayout(Controller& controller, const Size& layoutSize)
242 Controller::Impl& impl = *controller.mImpl;
244 const OperationsMask operations = impl.mOperationsPending;
245 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations) || impl.mTextFitContentSize != layoutSize)
247 ModelPtr& model = impl.mModel;
249 bool actualellipsis = model->mElideEnabled;
250 float minPointSize = impl.mTextFitMinSize;
251 float maxPointSize = impl.mTextFitMaxSize;
252 float pointInterval = impl.mTextFitStepSize;
253 float currentFitPointSize = impl.mFontDefaults->mFitPointSize;
255 model->mElideEnabled = false;
258 if(pointInterval < 1.f)
260 impl.mTextFitStepSize = pointInterval = 1.0f;
262 uint32_t pointSizeRange = static_cast<uint32_t>(ceil((maxPointSize - minPointSize) / pointInterval));
264 // Ensure minPointSize + pointSizeRange * pointInverval >= maxPointSize
265 while(minPointSize + static_cast<float>(pointSizeRange) * pointInterval < maxPointSize)
270 uint32_t bestSizeIndex = 0;
271 uint32_t minIndex = bestSizeIndex + 1u;
272 uint32_t maxIndex = pointSizeRange + 1u;
274 bool bestSizeUpdatedLatest = false;
275 // Find best size as binary search.
276 // Range format as [l r). (left closed, right opened)
277 // It mean, we already check all i < l is valid, and r <= i is invalid.
278 // Below binary search will check m = (l+r)/2 point.
279 // Search area sperate as [l m) or [m+1 r)
281 // Basically, we can assume that 0 (minPointSize) is always valid.
282 // Now, we will check [1 pointSizeRange] range s.t. pointSizeRange mean the maxPointSize
283 while(minIndex < maxIndex)
285 uint32_t testIndex = minIndex + ((maxIndex - minIndex) >> 1u);
286 const float testPointSize = std::min(maxPointSize, minPointSize + static_cast<float>(testIndex) * pointInterval);
288 if(CheckForTextFit(controller, testPointSize, layoutSize))
290 bestSizeUpdatedLatest = true;
292 bestSizeIndex = testIndex;
293 minIndex = testIndex + 1u;
297 bestSizeUpdatedLatest = false;
298 maxIndex = testIndex;
301 const float bestPointSize = std::min(maxPointSize, minPointSize + static_cast<float>(bestSizeIndex) * pointInterval);
303 // Best point size was not updated. re-run so the TextFit should be fitted really.
304 if(!bestSizeUpdatedLatest)
306 CheckForTextFit(controller, bestPointSize, layoutSize);
309 model->mElideEnabled = actualellipsis;
310 if(currentFitPointSize != bestPointSize)
312 impl.mTextFitChanged = true;
314 impl.mFontDefaults->mFitPointSize = bestPointSize;
315 impl.mFontDefaults->sizeDefined = true;
316 impl.ClearFontData();
320 float Controller::Relayouter::GetHeightForWidth(Controller& controller, float width)
322 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", &controller, width);
324 // Make sure the model is up-to-date before layouting
325 EventHandler::ProcessModifyEvents(controller);
327 Controller::Impl& impl = *controller.mImpl;
328 ModelPtr& model = impl.mModel;
329 VisualModelPtr& visualModel = model->mVisualModel;
330 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
334 if(fabsf(width - visualModel->mControlSize.width) > Math::MACHINE_EPSILON_1000 ||
335 textUpdateInfo.mFullRelayoutNeeded ||
336 textUpdateInfo.mClearAll)
338 // Layout the text for the new width.
339 OperationsMask requestedOperationsMask = static_cast<OperationsMask>(LAYOUT);
340 Size sizeRequestedWidthAndMaxHeight = Size(width, MAX_FLOAT);
342 layoutSize = CalculateLayoutSizeOnRequiredControllerSize(controller, sizeRequestedWidthAndMaxHeight, requestedOperationsMask);
344 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height);
348 layoutSize = visualModel->GetLayoutSize();
349 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height);
352 return layoutSize.height;
355 Controller::UpdateTextType Controller::Relayouter::Relayout(Controller& controller, const Size& size, Dali::LayoutDirection::Type layoutDirection)
357 Controller::Impl& impl = *controller.mImpl;
358 ModelPtr& model = impl.mModel;
359 VisualModelPtr& visualModel = model->mVisualModel;
360 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
362 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", &controller, size.width, size.height, impl.mIsAutoScrollEnabled ? "true" : "false");
364 UpdateTextType updateTextType = NONE_UPDATED;
366 if((size.width < Math::MACHINE_EPSILON_1000) || (size.height < Math::MACHINE_EPSILON_1000))
368 if(0u != visualModel->mGlyphPositions.Count())
370 visualModel->mGlyphPositions.Clear();
371 updateTextType = MODEL_UPDATED;
374 // Clear the update info. This info will be set the next time the text is updated.
375 textUpdateInfo.Clear();
377 // Not worth to relayout if width or height is equal to zero.
378 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n");
380 return updateTextType;
383 // Whether a new size has been set.
384 const bool newSize = (size != visualModel->mControlSize);
386 // Get a reference to the pending operations member
387 OperationsMask& operationsPending = impl.mOperationsPending;
391 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", visualModel->mControlSize.width, visualModel->mControlSize.height);
393 if((0 == textUpdateInfo.mNumberOfCharactersToAdd) &&
394 (0 == textUpdateInfo.mPreviousNumberOfCharacters) &&
395 ((visualModel->mControlSize.width < Math::MACHINE_EPSILON_1000) || (visualModel->mControlSize.height < Math::MACHINE_EPSILON_1000)))
397 textUpdateInfo.mNumberOfCharactersToAdd = model->mLogicalModel->mText.Count();
400 // Layout operations that need to be done if the size changes.
401 operationsPending = static_cast<OperationsMask>(operationsPending |
406 // Set the update info to relayout the whole text.
407 textUpdateInfo.mFullRelayoutNeeded = true;
408 textUpdateInfo.mCharacterIndex = 0u;
410 // Store the size used to layout the text.
411 visualModel->mControlSize = size;
414 // Whether there are modify events.
415 if(0u != impl.mModifyEvents.Count())
417 // Style operations that need to be done if the text is modified.
418 operationsPending = static_cast<OperationsMask>(operationsPending | COLOR);
421 // Set the update info to elide the text.
422 if(model->mElideEnabled ||
423 ((NULL != impl.mEventData) && impl.mEventData->mIsPlaceholderElideEnabled))
425 // Update Text layout for applying elided
426 operationsPending = static_cast<OperationsMask>(operationsPending |
431 textUpdateInfo.mFullRelayoutNeeded = true;
432 textUpdateInfo.mCharacterIndex = 0u;
435 bool layoutDirectionChanged = false;
436 if(impl.mLayoutDirection != layoutDirection)
438 // Flag to indicate that the layout direction has changed.
439 layoutDirectionChanged = true;
440 // Clear the update info. This info will be set the next time the text is updated.
441 textUpdateInfo.mClearAll = true;
442 // Apply modifications to the model
443 // Shape the text again is needed because characters like '()[]{}' have to be mirrored and the glyphs generated again.
444 operationsPending = static_cast<OperationsMask>(operationsPending |
452 impl.mLayoutDirection = layoutDirection;
455 // Make sure the model is up-to-date before layouting.
456 EventHandler::ProcessModifyEvents(controller);
457 bool updated = impl.UpdateModel(operationsPending);
461 updated = DoRelayout(impl, size, operationsPending, layoutSize) || updated;
465 updateTextType = MODEL_UPDATED;
468 // Do not re-do any operation until something changes.
469 operationsPending = NO_OPERATION;
470 model->mScrollPositionLast = model->mScrollPosition;
472 // Whether the text control is editable
473 const bool isEditable = NULL != impl.mEventData;
475 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
477 if(newSize && isEditable)
479 offset = model->mScrollPosition;
482 if(!isEditable || !controller.IsMultiLineEnabled())
484 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
485 CalculateVerticalOffset(impl, size);
489 // If layoutSize is bigger than size, vertical align has no meaning.
490 if(layoutSize.y < size.y)
492 CalculateVerticalOffset(impl, size);
495 impl.mEventData->mScrollAfterDelete = false;
502 if(newSize || layoutDirectionChanged)
504 // If there is a new size or layout direction is changed, the scroll position needs to be clamped.
505 impl.ClampHorizontalScroll(layoutSize);
507 // Update the decorator's positions is needed if there is a new size.
508 impl.mEventData->mDecorator->UpdatePositions(model->mScrollPosition - offset);
510 // All decorator elements need to be updated.
511 if(EventData::IsEditingState(impl.mEventData->mState))
513 impl.mEventData->mScrollAfterUpdatePosition = true;
514 impl.mEventData->mUpdateCursorPosition = true;
515 impl.mEventData->mUpdateGrabHandlePosition = true;
517 else if(impl.mEventData->mState == EventData::SELECTING)
519 impl.mEventData->mUpdateHighlightBox = true;
523 // Move the cursor, grab handle etc.
524 if(impl.ProcessInputEvents())
526 updateTextType = static_cast<UpdateTextType>(updateTextType | DECORATOR_UPDATED);
530 // Clear the update info. This info will be set the next time the text is updated.
531 textUpdateInfo.Clear();
532 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout\n");
534 return updateTextType;
537 bool Controller::Relayouter::DoRelayout(Controller::Impl& impl, const Size& size, OperationsMask operationsRequired, Size& layoutSize)
539 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::Relayouter::DoRelayout %p size %f,%f\n", &impl, size.width, size.height);
540 bool viewUpdated(false);
542 // Calculate the operations to be done.
543 const OperationsMask operations = static_cast<OperationsMask>(impl.mOperationsPending & operationsRequired);
545 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
546 const CharacterIndex startIndex = textUpdateInfo.mParagraphCharacterIndex;
547 const Length requestedNumberOfCharacters = textUpdateInfo.mRequestedNumberOfCharacters;
549 // Get the current layout size.
550 VisualModelPtr& visualModel = impl.mModel->mVisualModel;
551 layoutSize = visualModel->GetLayoutSize();
553 if(NO_OPERATION != (LAYOUT & operations))
555 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
557 // Some vectors with data needed to layout and reorder may be void
558 // after the first time the text has been laid out.
559 // Fill the vectors again.
561 // Calculate the number of glyphs to layout.
562 const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
563 const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
564 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
565 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
567 const CharacterIndex lastIndex = startIndex + ((requestedNumberOfCharacters > 0u) ? requestedNumberOfCharacters - 1u : 0u);
568 const GlyphIndex startGlyphIndex = textUpdateInfo.mStartGlyphIndex;
570 // Make sure the index is not out of bound
571 if(charactersToGlyph.Count() != glyphsPerCharacter.Count() ||
572 requestedNumberOfCharacters > charactersToGlyph.Count() ||
573 (lastIndex > charactersToGlyph.Count() && charactersToGlyph.Count() > 0u))
575 std::string currentText;
576 impl.GetText(currentText);
578 DALI_LOG_ERROR("Controller::DoRelayout: Attempting to access invalid buffer\n");
579 DALI_LOG_ERROR("Current text is: %s\n", currentText.c_str());
580 DALI_LOG_ERROR("startIndex: %u, lastIndex: %u, requestedNumberOfCharacters: %u, charactersToGlyph.Count = %lu, glyphsPerCharacter.Count = %lu\n", startIndex, lastIndex, requestedNumberOfCharacters, charactersToGlyph.Count(), glyphsPerCharacter.Count());
585 const Length numberOfGlyphs = (requestedNumberOfCharacters > 0u) ? *(charactersToGlyphBuffer + lastIndex) + *(glyphsPerCharacterBuffer + lastIndex) - startGlyphIndex : 0u;
586 const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
588 if(0u == totalNumberOfGlyphs)
590 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
592 visualModel->SetLayoutSize(Size::ZERO);
595 // Nothing else to do if there is no glyphs.
596 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n");
600 // Set the layout parameters.
601 Layout::Parameters layoutParameters(size, impl.mModel);
603 // Resize the vector of positions to have the same size than the vector of glyphs.
604 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
605 glyphPositions.Resize(totalNumberOfGlyphs);
607 // Whether the last character is a new paragraph character.
608 const Character* const textBuffer = impl.mModel->mLogicalModel->mText.Begin();
609 textUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph(*(textBuffer + (impl.mModel->mLogicalModel->mText.Count() - 1u)));
610 layoutParameters.isLastNewParagraph = textUpdateInfo.mIsLastCharacterNewParagraph;
612 // The initial glyph and the number of glyphs to layout.
613 layoutParameters.startGlyphIndex = startGlyphIndex;
614 layoutParameters.numberOfGlyphs = numberOfGlyphs;
615 layoutParameters.startLineIndex = textUpdateInfo.mStartLineIndex;
616 layoutParameters.estimatedNumberOfLines = textUpdateInfo.mEstimatedNumberOfLines;
618 // Update the ellipsis
619 bool elideTextEnabled = impl.mModel->mElideEnabled;
620 auto ellipsisPosition = impl.mModel->mEllipsisPosition;
622 if(NULL != impl.mEventData)
624 if(impl.mEventData->mPlaceholderEllipsisFlag && impl.IsShowingPlaceholderText())
626 elideTextEnabled = impl.mEventData->mIsPlaceholderElideEnabled;
628 else if(EventData::INACTIVE != impl.mEventData->mState)
630 // Disable ellipsis when editing
631 elideTextEnabled = false;
634 // Reset the scroll position in inactive state
635 if(elideTextEnabled && (impl.mEventData->mState == EventData::INACTIVE))
637 impl.ResetScrollPosition();
641 // Update the visual model.
642 bool isAutoScrollEnabled = impl.mIsAutoScrollEnabled;
643 bool isAutoScrollMaxTextureExceeded = impl.mIsAutoScrollMaxTextureExceeded;
644 bool isHiddenInputEnabled = false;
645 if(impl.mHiddenInput && impl.mEventData != nullptr && impl.mHiddenInput->GetHideMode() != Toolkit::HiddenInput::Mode::HIDE_NONE)
647 isHiddenInputEnabled = true;
651 viewUpdated = impl.mLayoutEngine.LayoutText(layoutParameters,
655 isAutoScrollMaxTextureExceeded,
656 isHiddenInputEnabled,
658 impl.mIsAutoScrollEnabled = isAutoScrollEnabled;
660 viewUpdated = viewUpdated || (newLayoutSize != layoutSize);
664 layoutSize = newLayoutSize;
666 if(NO_OPERATION != (UPDATE_DIRECTION & operations))
668 impl.mIsTextDirectionRTL = false;
671 if((NO_OPERATION != (UPDATE_DIRECTION & operations)) && !visualModel->mLines.Empty())
673 impl.mIsTextDirectionRTL = visualModel->mLines[0u].direction;
676 // Sets the layout size.
677 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
679 visualModel->SetLayoutSize(layoutSize);
684 if(NO_OPERATION != (ALIGN & operations))
686 DoRelayoutHorizontalAlignment(impl, size, startIndex, requestedNumberOfCharacters);
689 #if defined(DEBUG_ENABLED)
690 std::string currentText;
691 impl.GetText(currentText);
692 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Controller::Relayouter::DoRelayout [%p] mImpl->mIsTextDirectionRTL[%s] [%s]\n", &impl, (impl.mIsTextDirectionRTL) ? "true" : "false", currentText.c_str());
694 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayouter::DoRelayout, view updated %s\n", (viewUpdated ? "true" : "false"));
698 void Controller::Relayouter::DoRelayoutHorizontalAlignment(Controller::Impl& impl,
700 const CharacterIndex startIndex,
701 const Length requestedNumberOfCharacters)
704 VisualModelPtr& visualModel = impl.mModel->mVisualModel;
706 // The laid-out lines.
707 Vector<LineRun>& lines = visualModel->mLines;
709 CharacterIndex alignStartIndex = startIndex;
710 Length alignRequestedNumberOfCharacters = requestedNumberOfCharacters;
712 // the whole text needs to be full aligned.
713 // If you do not do a full aligned, only the last line of the multiline input is aligned.
714 if(impl.mEventData && impl.mEventData->mUpdateAlignment)
716 alignStartIndex = 0u;
717 alignRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
718 impl.mEventData->mUpdateAlignment = false;
721 // If there is no BoundedParagraphRuns then apply the alignment of controller.
722 // Check whether the layout is single line. It's needed to apply one alignment for single-line.
723 // In single-line layout case we need to check whether to follow the alignment of controller or the first BoundedParagraph.
724 // Apply BoundedParagraph's alignment if and only if there is one BoundedParagraph contains all characters. Otherwise follow controller's alignment.
725 const bool isFollowControllerAlignment = ((impl.mModel->GetNumberOfBoundedParagraphRuns() == 0u) ||
726 ((Layout::Engine::SINGLE_LINE_BOX == impl.mLayoutEngine.GetLayout()) &&
727 (impl.mModel->GetBoundedParagraphRuns()[0].characterRun.numberOfCharacters != impl.mModel->mLogicalModel->mText.Count())));
729 if(isFollowControllerAlignment)
731 // Need to align with the control's size as the text may contain lines
732 // starting either with left to right text or right to left.
733 impl.mLayoutEngine.Align(size,
735 alignRequestedNumberOfCharacters,
736 impl.mModel->mHorizontalAlignment,
738 impl.mModel->mAlignmentOffset,
739 impl.mLayoutDirection,
740 (impl.mModel->mMatchLayoutDirection != DevelText::MatchLayoutDirection::CONTENTS));
744 //Override the controller horizontal-alignment by horizontal-alignment of bounded paragraph.
745 const Length& numberOfBoundedParagraphRuns = impl.mModel->GetNumberOfBoundedParagraphRuns();
746 const Vector<BoundedParagraphRun>& boundedParagraphRuns = impl.mModel->GetBoundedParagraphRuns();
747 const CharacterIndex alignEndIndex = alignStartIndex + alignRequestedNumberOfCharacters - 1u;
749 Length alignIndex = alignStartIndex;
750 Length boundedParagraphRunIndex = 0u;
752 while(alignIndex <= alignEndIndex && boundedParagraphRunIndex < numberOfBoundedParagraphRuns)
754 //BP: BoundedParagraph
755 const BoundedParagraphRun& boundedParagraphRun = boundedParagraphRuns[boundedParagraphRunIndex];
756 const CharacterIndex& characterStartIndexBP = boundedParagraphRun.characterRun.characterIndex;
757 const Length& numberOfCharactersBP = boundedParagraphRun.characterRun.numberOfCharacters;
758 const CharacterIndex characterEndIndexBP = characterStartIndexBP + numberOfCharactersBP - 1u;
760 CharacterIndex decidedAlignStartIndex = alignIndex;
761 Length decidedAlignNumberOfCharacters = alignEndIndex - alignIndex + 1u;
762 Text::HorizontalAlignment::Type decidedHorizontalAlignment = impl.mModel->mHorizontalAlignment;
765 * Shortcuts to explain indexes cases:
767 * AS: Alignment Start Index
768 * AE: Alignment End Index
769 * PS: Paragraph Start Index
770 * PE: Paragraph End Index
771 * B: BoundedParagraph Alignment
776 if(alignIndex < characterStartIndexBP && characterStartIndexBP <= alignEndIndex) /// AS.MMMMMM.PS--------AE
778 // Alignment from "Alignment Start Index" to index before "Paragraph Start Index" according to "Model Alignment"
779 decidedAlignStartIndex = alignIndex;
780 decidedAlignNumberOfCharacters = characterStartIndexBP - alignIndex;
781 decidedHorizontalAlignment = impl.mModel->mHorizontalAlignment;
783 // Need to re-heck the case of current bounded paragraph
784 alignIndex = characterStartIndexBP; // Shift AS to be PS
786 else if((characterStartIndexBP <= alignIndex && alignIndex <= characterEndIndexBP) || /// ---PS.BBBBBBB.AS.BBBBBBB.PE---
787 (characterStartIndexBP <= alignEndIndex && alignEndIndex <= characterEndIndexBP)) /// ---PS.BBBBBB.AE.BBBBBBB.PE---
789 // Alignment from "Paragraph Start Index" to "Paragraph End Index" according to "BoundedParagraph Alignment"
790 decidedAlignStartIndex = characterStartIndexBP;
791 decidedAlignNumberOfCharacters = numberOfCharactersBP;
792 decidedHorizontalAlignment = boundedParagraphRun.horizontalAlignmentDefined ? boundedParagraphRun.horizontalAlignment : impl.mModel->mHorizontalAlignment;
794 alignIndex = characterEndIndexBP + 1u; // Shift AS to be after PE direct
795 boundedParagraphRunIndex++; // Align then check the case of next bounded paragraph
799 boundedParagraphRunIndex++; // Check the case of next bounded paragraph
803 impl.mLayoutEngine.Align(size,
804 decidedAlignStartIndex,
805 decidedAlignNumberOfCharacters,
806 decidedHorizontalAlignment,
808 impl.mModel->mAlignmentOffset,
809 impl.mLayoutDirection,
810 (impl.mModel->mMatchLayoutDirection != DevelText::MatchLayoutDirection::CONTENTS));
813 //Align the remaining that is not aligned
814 if(alignIndex <= alignEndIndex)
816 impl.mLayoutEngine.Align(size,
818 (alignEndIndex - alignIndex + 1u),
819 impl.mModel->mHorizontalAlignment,
821 impl.mModel->mAlignmentOffset,
822 impl.mLayoutDirection,
823 (impl.mModel->mMatchLayoutDirection != DevelText::MatchLayoutDirection::CONTENTS));
828 void Controller::Relayouter::CalculateVerticalOffset(Controller::Impl& impl, const Size& controlSize)
830 ModelPtr& model = impl.mModel;
831 VisualModelPtr& visualModel = model->mVisualModel;
832 Size layoutSize = model->mVisualModel->GetLayoutSize();
833 Size oldLayoutSize = layoutSize;
835 bool needRecalc = false;
836 float defaultFontLineHeight = impl.GetDefaultFontLineHeight();
838 if(fabsf(layoutSize.height) < Math::MACHINE_EPSILON_1000)
840 // Get the line height of the default font.
841 layoutSize.height = defaultFontLineHeight;
844 // Whether the text control is editable
845 const bool isEditable = NULL != impl.mEventData;
846 if(isEditable && layoutSize.height != defaultFontLineHeight && impl.IsShowingPlaceholderText())
848 // This code prevents the wrong positioning of cursor when the layout size is bigger/smaller than defaultFontLineHeight.
849 // This situation occurs when the size of placeholder text is different from the default text.
850 layoutSize.height = defaultFontLineHeight;
854 switch(model->mVerticalAlignment)
856 case VerticalAlignment::TOP:
858 model->mScrollPosition.y = 0.f;
862 case VerticalAlignment::CENTER:
864 model->mScrollPosition.y = floorf(0.5f * (controlSize.height - layoutSize.height)); // try to avoid pixel alignment.
865 if(needRecalc) offsetY = floorf(0.5f * (layoutSize.height - oldLayoutSize.height));
868 case VerticalAlignment::BOTTOM:
870 model->mScrollPosition.y = controlSize.height - layoutSize.height;
871 if(needRecalc) offsetY = layoutSize.height - oldLayoutSize.height;
878 // Update glyphPositions according to recalculation.
879 const Length positionCount = visualModel->mGlyphPositions.Count();
880 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
881 for(Length index = 0u; index < positionCount; index++)
883 glyphPositions[index].y += offsetY;
890 } // namespace Toolkit