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/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-event-handler.h>
28 #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));
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 // Set the update info to relayout the whole text.
71 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
72 if((0 == textUpdateInfo.mNumberOfCharactersToAdd) &&
73 (0 == textUpdateInfo.mPreviousNumberOfCharacters) &&
74 ((visualModel->mControlSize.width < Math::MACHINE_EPSILON_1000) || (visualModel->mControlSize.height < Math::MACHINE_EPSILON_1000)))
76 textUpdateInfo.mNumberOfCharactersToAdd = model->mLogicalModel->mText.Count();
78 textUpdateInfo.mParagraphCharacterIndex = 0u;
79 textUpdateInfo.mRequestedNumberOfCharacters = model->mLogicalModel->mText.Count();
81 // This is to keep Index to the first character to be updated.
82 // Then restore it after calling Clear method.
83 auto updateInfoCharIndexBackup = textUpdateInfo.mCharacterIndex;
85 // Get a reference to the pending operations member
86 OperationsMask& operationsPending = impl.mOperationsPending;
88 // Layout the text for the new width.
89 // Apply the pending operations, requested operations and the only once operations.
90 // Then remove onlyOnceOperations
91 operationsPending = static_cast<OperationsMask>(operationsPending | requestedOperationsMask | onlyOnceOperations);
93 // Make sure the model is up-to-date before layouting
94 impl.UpdateModel(static_cast<OperationsMask>(operationsPending & ~UPDATE_LAYOUT_SIZE));
96 // Store the actual control's size to restore later.
97 const Size actualControlSize = visualModel->mControlSize;
100 requestedControllerSize,
101 static_cast<OperationsMask>(operationsPending & ~UPDATE_LAYOUT_SIZE),
102 calculatedLayoutSize);
104 // Clear the update info. This info will be set the next time the text is updated.
105 textUpdateInfo.Clear();
107 //TODO: Refactor "DoRelayout" and extract common code of size calculation without modifying attributes of mVisualModel,
108 //TODO: then calculate GlyphPositions. Lines, Size, Layout for Natural-Size
109 //TODO: and utilize the values in OperationsPending and TextUpdateInfo without changing the original one.
110 //TODO: Also it will improve performance because there is no need todo FullRelyout on the next need for layouting.
112 // FullRelayoutNeeded should be true because DoRelayout is MAX_FLOAT, MAX_FLOAT.
113 // By this no need to take backup and restore it.
114 textUpdateInfo.mFullRelayoutNeeded = true;
116 // Restore mCharacterIndex. Because "Clear" set it to the maximum integer.
117 // The "CalculateTextUpdateIndices" does not work proprely because the mCharacterIndex will be greater than mPreviousNumberOfCharacters.
118 // Which apply an assumption to update only the last paragraph. That could cause many of out of index crashes.
119 textUpdateInfo.mCharacterIndex = updateInfoCharIndexBackup;
121 // Do not do again the only once operations.
122 operationsPending = static_cast<OperationsMask>(operationsPending & ~onlyOnceOperations);
124 // Do the size related operations again.
126 const OperationsMask sizeOperations = static_cast<OperationsMask>(LAYOUT |
130 operationsPending = static_cast<OperationsMask>(operationsPending | sizeOperations);
132 // Restore the actual control's size.
133 visualModel->mControlSize = actualControlSize;
135 return calculatedLayoutSize;
138 Vector3 Controller::Relayouter::GetNaturalSize(Controller& controller)
140 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n");
141 Vector3 naturalSizeVec3;
143 // Make sure the model is up-to-date before layouting
144 EventHandler::ProcessModifyEvents(controller);
146 Controller::Impl& impl = *controller.mImpl;
147 ModelPtr& model = impl.mModel;
148 VisualModelPtr& visualModel = model->mVisualModel;
150 if(impl.mRecalculateNaturalSize)
154 // Layout the text for the new width.
155 OperationsMask requestedOperationsMask = static_cast<OperationsMask>(LAYOUT | REORDER);
156 Size sizeMaxWidthAndMaxHeight = Size(MAX_FLOAT, MAX_FLOAT);
158 naturalSize = CalculateLayoutSizeOnRequiredControllerSize(controller, sizeMaxWidthAndMaxHeight, requestedOperationsMask);
160 // Stores the natural size to avoid recalculate it again
161 // unless the text/style changes.
162 visualModel->SetNaturalSize(naturalSize);
163 naturalSizeVec3 = naturalSize;
165 impl.mRecalculateNaturalSize = false;
167 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSizeVec3.x, naturalSizeVec3.y, naturalSizeVec3.z);
171 naturalSizeVec3 = visualModel->GetNaturalSize();
173 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSizeVec3.x, naturalSizeVec3.y, naturalSizeVec3.z);
176 naturalSizeVec3.x = ConvertToEven(naturalSizeVec3.x);
177 naturalSizeVec3.y = ConvertToEven(naturalSizeVec3.y);
179 return naturalSizeVec3;
182 bool Controller::Relayouter::CheckForTextFit(Controller& controller, float pointSize, const Size& layoutSize)
185 Controller::Impl& impl = *controller.mImpl;
186 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
187 impl.mFontDefaults->mFitPointSize = pointSize;
188 impl.mFontDefaults->sizeDefined = true;
189 impl.ClearFontData();
191 // Operations that can be done only once until the text changes.
192 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>(CONVERT_TO_UTF32 |
200 textUpdateInfo.mParagraphCharacterIndex = 0u;
201 textUpdateInfo.mRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
203 // Make sure the model is up-to-date before layouting
204 impl.UpdateModel(onlyOnceOperations);
207 Size(layoutSize.width, MAX_FLOAT),
208 static_cast<OperationsMask>(onlyOnceOperations | LAYOUT),
211 // Clear the update info. This info will be set the next time the text is updated.
212 textUpdateInfo.Clear();
213 textUpdateInfo.mClearAll = true;
215 if(textSize.width > layoutSize.width || textSize.height > layoutSize.height)
222 void Controller::Relayouter::FitPointSizeforLayout(Controller& controller, const Size& layoutSize)
224 Controller::Impl& impl = *controller.mImpl;
226 const OperationsMask operations = impl.mOperationsPending;
227 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations) || impl.mTextFitContentSize != layoutSize)
229 ModelPtr& model = impl.mModel;
231 bool actualellipsis = model->mElideEnabled;
232 float minPointSize = impl.mTextFitMinSize;
233 float maxPointSize = impl.mTextFitMaxSize;
234 float pointInterval = impl.mTextFitStepSize;
235 float currentFitPointSize = impl.mFontDefaults->mFitPointSize;
237 model->mElideEnabled = false;
238 Vector<float> pointSizeArray;
241 if(pointInterval < 1.f)
243 impl.mTextFitStepSize = pointInterval = 1.0f;
246 pointSizeArray.Reserve(static_cast<unsigned int>(ceil((maxPointSize - minPointSize) / pointInterval)));
248 for(float i = minPointSize; i < maxPointSize; i += pointInterval)
250 pointSizeArray.PushBack(i);
253 pointSizeArray.PushBack(maxPointSize);
255 int bestSizeIndex = 0;
256 int min = bestSizeIndex + 1;
257 int max = pointSizeArray.Size() - 1;
260 int destI = (min + max) / 2;
262 if(CheckForTextFit(controller, pointSizeArray[destI], layoutSize))
274 model->mElideEnabled = actualellipsis;
275 if(currentFitPointSize != pointSizeArray[bestSizeIndex])
277 impl.mTextFitChanged = true;
279 impl.mFontDefaults->mFitPointSize = pointSizeArray[bestSizeIndex];
280 impl.mFontDefaults->sizeDefined = true;
281 impl.ClearFontData();
285 float Controller::Relayouter::GetHeightForWidth(Controller& controller, float width)
287 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", &controller, width);
289 // Make sure the model is up-to-date before layouting
290 EventHandler::ProcessModifyEvents(controller);
292 Controller::Impl& impl = *controller.mImpl;
293 ModelPtr& model = impl.mModel;
294 VisualModelPtr& visualModel = model->mVisualModel;
295 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
299 if(fabsf(width - visualModel->mControlSize.width) > Math::MACHINE_EPSILON_1000 ||
300 textUpdateInfo.mFullRelayoutNeeded ||
301 textUpdateInfo.mClearAll)
303 // Layout the text for the new width.
304 OperationsMask requestedOperationsMask = static_cast<OperationsMask>(LAYOUT);
305 Size sizeRequestedWidthAndMaxHeight = Size(width, MAX_FLOAT);
307 layoutSize = CalculateLayoutSizeOnRequiredControllerSize(controller, sizeRequestedWidthAndMaxHeight, requestedOperationsMask);
309 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height);
313 layoutSize = visualModel->GetLayoutSize();
314 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height);
317 return layoutSize.height;
320 Controller::UpdateTextType Controller::Relayouter::Relayout(Controller& controller, const Size& size, Dali::LayoutDirection::Type layoutDirection)
322 Controller::Impl& impl = *controller.mImpl;
323 ModelPtr& model = impl.mModel;
324 VisualModelPtr& visualModel = model->mVisualModel;
325 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
327 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", &controller, size.width, size.height, impl.mIsAutoScrollEnabled ? "true" : "false");
329 UpdateTextType updateTextType = NONE_UPDATED;
331 if((size.width < Math::MACHINE_EPSILON_1000) || (size.height < Math::MACHINE_EPSILON_1000))
333 if(0u != visualModel->mGlyphPositions.Count())
335 visualModel->mGlyphPositions.Clear();
336 updateTextType = MODEL_UPDATED;
339 // Clear the update info. This info will be set the next time the text is updated.
340 textUpdateInfo.Clear();
342 // Not worth to relayout if width or height is equal to zero.
343 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n");
345 return updateTextType;
348 // Whether a new size has been set.
349 const bool newSize = (size != visualModel->mControlSize);
351 // Get a reference to the pending operations member
352 OperationsMask& operationsPending = impl.mOperationsPending;
356 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", visualModel->mControlSize.width, visualModel->mControlSize.height);
358 if((0 == textUpdateInfo.mNumberOfCharactersToAdd) &&
359 (0 == textUpdateInfo.mPreviousNumberOfCharacters) &&
360 ((visualModel->mControlSize.width < Math::MACHINE_EPSILON_1000) || (visualModel->mControlSize.height < Math::MACHINE_EPSILON_1000)))
362 textUpdateInfo.mNumberOfCharactersToAdd = model->mLogicalModel->mText.Count();
365 // Layout operations that need to be done if the size changes.
366 operationsPending = static_cast<OperationsMask>(operationsPending |
371 // Set the update info to relayout the whole text.
372 textUpdateInfo.mFullRelayoutNeeded = true;
373 textUpdateInfo.mCharacterIndex = 0u;
375 // Store the size used to layout the text.
376 visualModel->mControlSize = size;
379 // Whether there are modify events.
380 if(0u != impl.mModifyEvents.Count())
382 // Style operations that need to be done if the text is modified.
383 operationsPending = static_cast<OperationsMask>(operationsPending | COLOR);
386 // Set the update info to elide the text.
387 if(model->mElideEnabled ||
388 ((NULL != impl.mEventData) && impl.mEventData->mIsPlaceholderElideEnabled))
390 // Update Text layout for applying elided
391 operationsPending = static_cast<OperationsMask>(operationsPending |
396 textUpdateInfo.mFullRelayoutNeeded = true;
397 textUpdateInfo.mCharacterIndex = 0u;
400 bool layoutDirectionChanged = false;
401 if(impl.mLayoutDirection != layoutDirection)
403 // Flag to indicate that the layout direction has changed.
404 layoutDirectionChanged = true;
405 // Clear the update info. This info will be set the next time the text is updated.
406 textUpdateInfo.mClearAll = true;
407 // Apply modifications to the model
408 // Shape the text again is needed because characters like '()[]{}' have to be mirrored and the glyphs generated again.
409 operationsPending = static_cast<OperationsMask>(operationsPending |
417 impl.mLayoutDirection = layoutDirection;
420 // Make sure the model is up-to-date before layouting.
421 EventHandler::ProcessModifyEvents(controller);
422 bool updated = impl.UpdateModel(operationsPending);
426 updated = DoRelayout(impl, size, operationsPending, layoutSize) || updated;
430 updateTextType = MODEL_UPDATED;
433 // Do not re-do any operation until something changes.
434 operationsPending = NO_OPERATION;
435 model->mScrollPositionLast = model->mScrollPosition;
437 // Whether the text control is editable
438 const bool isEditable = NULL != impl.mEventData;
440 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
442 if(newSize && isEditable)
444 offset = model->mScrollPosition;
447 if(!isEditable || !controller.IsMultiLineEnabled())
449 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
450 CalculateVerticalOffset(controller, size);
455 if(newSize || layoutDirectionChanged)
457 // If there is a new size or layout direction is changed, the scroll position needs to be clamped.
458 impl.ClampHorizontalScroll(layoutSize);
460 // Update the decorator's positions is needed if there is a new size.
461 impl.mEventData->mDecorator->UpdatePositions(model->mScrollPosition - offset);
463 // All decorator elements need to be updated.
464 if(EventData::IsEditingState(impl.mEventData->mState))
466 impl.mEventData->mScrollAfterUpdatePosition = true;
467 impl.mEventData->mUpdateCursorPosition = true;
468 impl.mEventData->mUpdateGrabHandlePosition = true;
470 else if(impl.mEventData->mState == EventData::SELECTING)
472 impl.mEventData->mUpdateHighlightBox = true;
476 // Move the cursor, grab handle etc.
477 if(impl.ProcessInputEvents())
479 updateTextType = static_cast<UpdateTextType>(updateTextType | DECORATOR_UPDATED);
483 // Clear the update info. This info will be set the next time the text is updated.
484 textUpdateInfo.Clear();
485 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout\n");
487 return updateTextType;
490 bool Controller::Relayouter::DoRelayout(Controller::Impl& impl, const Size& size, OperationsMask operationsRequired, Size& layoutSize)
492 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::Relayouter::DoRelayout %p size %f,%f\n", &impl, size.width, size.height);
493 bool viewUpdated(false);
495 // Calculate the operations to be done.
496 const OperationsMask operations = static_cast<OperationsMask>(impl.mOperationsPending & operationsRequired);
498 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
499 const CharacterIndex startIndex = textUpdateInfo.mParagraphCharacterIndex;
500 const Length requestedNumberOfCharacters = textUpdateInfo.mRequestedNumberOfCharacters;
502 // Get the current layout size.
503 VisualModelPtr& visualModel = impl.mModel->mVisualModel;
504 layoutSize = visualModel->GetLayoutSize();
506 if(NO_OPERATION != (LAYOUT & operations))
508 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
510 // Some vectors with data needed to layout and reorder may be void
511 // after the first time the text has been laid out.
512 // Fill the vectors again.
514 // Calculate the number of glyphs to layout.
515 const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
516 const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
517 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
518 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
520 const CharacterIndex lastIndex = startIndex + ((requestedNumberOfCharacters > 0u) ? requestedNumberOfCharacters - 1u : 0u);
521 const GlyphIndex startGlyphIndex = textUpdateInfo.mStartGlyphIndex;
523 // Make sure the index is not out of bound
524 if(charactersToGlyph.Count() != glyphsPerCharacter.Count() ||
525 requestedNumberOfCharacters > charactersToGlyph.Count() ||
526 (lastIndex > charactersToGlyph.Count() && charactersToGlyph.Count() > 0u))
528 std::string currentText;
529 impl.GetText(currentText);
531 DALI_LOG_ERROR("Controller::DoRelayout: Attempting to access invalid buffer\n");
532 DALI_LOG_ERROR("Current text is: %s\n", currentText.c_str());
533 DALI_LOG_ERROR("startIndex: %u, lastIndex: %u, requestedNumberOfCharacters: %u, charactersToGlyph.Count = %lu, glyphsPerCharacter.Count = %lu\n", startIndex, lastIndex, requestedNumberOfCharacters, charactersToGlyph.Count(), glyphsPerCharacter.Count());
538 const Length numberOfGlyphs = (requestedNumberOfCharacters > 0u) ? *(charactersToGlyphBuffer + lastIndex) + *(glyphsPerCharacterBuffer + lastIndex) - startGlyphIndex : 0u;
539 const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
541 if(0u == totalNumberOfGlyphs)
543 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
545 visualModel->SetLayoutSize(Size::ZERO);
548 // Nothing else to do if there is no glyphs.
549 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n");
553 // Set the layout parameters.
554 Layout::Parameters layoutParameters(size, impl.mModel);
556 // Resize the vector of positions to have the same size than the vector of glyphs.
557 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
558 glyphPositions.Resize(totalNumberOfGlyphs);
560 // Whether the last character is a new paragraph character.
561 const Character* const textBuffer = impl.mModel->mLogicalModel->mText.Begin();
562 textUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph(*(textBuffer + (impl.mModel->mLogicalModel->mText.Count() - 1u)));
563 layoutParameters.isLastNewParagraph = textUpdateInfo.mIsLastCharacterNewParagraph;
565 // The initial glyph and the number of glyphs to layout.
566 layoutParameters.startGlyphIndex = startGlyphIndex;
567 layoutParameters.numberOfGlyphs = numberOfGlyphs;
568 layoutParameters.startLineIndex = textUpdateInfo.mStartLineIndex;
569 layoutParameters.estimatedNumberOfLines = textUpdateInfo.mEstimatedNumberOfLines;
571 // Update the ellipsis
572 bool elideTextEnabled = impl.mModel->mElideEnabled;
573 auto ellipsisPosition = impl.mModel->mEllipsisPosition;
575 if(NULL != impl.mEventData)
577 if(impl.mEventData->mPlaceholderEllipsisFlag && impl.IsShowingPlaceholderText())
579 elideTextEnabled = impl.mEventData->mIsPlaceholderElideEnabled;
581 else if(EventData::INACTIVE != impl.mEventData->mState)
583 // Disable ellipsis when editing
584 elideTextEnabled = false;
587 // Reset the scroll position in inactive state
588 if(elideTextEnabled && (impl.mEventData->mState == EventData::INACTIVE))
590 impl.ResetScrollPosition();
594 // Update the visual model.
595 bool isAutoScrollEnabled = impl.mIsAutoScrollEnabled;
597 viewUpdated = impl.mLayoutEngine.LayoutText(layoutParameters,
602 impl.mIsAutoScrollEnabled = isAutoScrollEnabled;
604 viewUpdated = viewUpdated || (newLayoutSize != layoutSize);
608 layoutSize = newLayoutSize;
610 if(NO_OPERATION != (UPDATE_DIRECTION & operations))
612 impl.mIsTextDirectionRTL = false;
615 if((NO_OPERATION != (UPDATE_DIRECTION & operations)) && !visualModel->mLines.Empty())
617 impl.mIsTextDirectionRTL = visualModel->mLines[0u].direction;
620 // Sets the layout size.
621 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
623 visualModel->SetLayoutSize(layoutSize);
628 if(NO_OPERATION != (ALIGN & operations))
630 DoRelayoutHorizontalAlignment(impl, size, startIndex, requestedNumberOfCharacters);
633 #if defined(DEBUG_ENABLED)
634 std::string currentText;
635 impl.GetText(currentText);
636 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Controller::Relayouter::DoRelayout [%p] mImpl->mIsTextDirectionRTL[%s] [%s]\n", &impl, (impl.mIsTextDirectionRTL) ? "true" : "false", currentText.c_str());
638 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayouter::DoRelayout, view updated %s\n", (viewUpdated ? "true" : "false"));
642 void Controller::Relayouter::DoRelayoutHorizontalAlignment(Controller::Impl& impl,
644 const CharacterIndex startIndex,
645 const Length requestedNumberOfCharacters)
648 VisualModelPtr& visualModel = impl.mModel->mVisualModel;
650 // The laid-out lines.
651 Vector<LineRun>& lines = visualModel->mLines;
653 CharacterIndex alignStartIndex = startIndex;
654 Length alignRequestedNumberOfCharacters = requestedNumberOfCharacters;
656 // the whole text needs to be full aligned.
657 // If you do not do a full aligned, only the last line of the multiline input is aligned.
658 if(impl.mEventData && impl.mEventData->mUpdateAlignment)
660 alignStartIndex = 0u;
661 alignRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
662 impl.mEventData->mUpdateAlignment = false;
665 // If there is no BoundedParagraphRuns then apply the alignment of controller.
666 // Check whether the layout is single line. It's needed to apply one alignment for single-line.
667 // In single-line layout case we need to check whether to follow the alignment of controller or the first BoundedParagraph.
668 // Apply BoundedParagraph's alignment if and only if there is one BoundedParagraph contains all characters. Otherwise follow controller's alignment.
669 const bool isFollowControllerAlignment = ((impl.mModel->GetNumberOfBoundedParagraphRuns() == 0u) ||
670 ((Layout::Engine::SINGLE_LINE_BOX == impl.mLayoutEngine.GetLayout()) &&
671 (impl.mModel->GetBoundedParagraphRuns()[0].characterRun.numberOfCharacters != impl.mModel->mLogicalModel->mText.Count())));
673 if(isFollowControllerAlignment)
675 // Need to align with the control's size as the text may contain lines
676 // starting either with left to right text or right to left.
677 impl.mLayoutEngine.Align(size,
679 alignRequestedNumberOfCharacters,
680 impl.mModel->mHorizontalAlignment,
682 impl.mModel->mAlignmentOffset,
683 impl.mLayoutDirection,
684 (impl.mModel->mMatchLayoutDirection != DevelText::MatchLayoutDirection::CONTENTS));
688 //Override the controller horizontal-alignment by horizontal-alignment of bounded paragraph.
689 const Length& numberOfBoundedParagraphRuns = impl.mModel->GetNumberOfBoundedParagraphRuns();
690 const Vector<BoundedParagraphRun>& boundedParagraphRuns = impl.mModel->GetBoundedParagraphRuns();
691 const CharacterIndex alignEndIndex = alignStartIndex + alignRequestedNumberOfCharacters - 1u;
693 Length alignIndex = alignStartIndex;
694 Length boundedParagraphRunIndex = 0u;
696 while(alignIndex <= alignEndIndex && boundedParagraphRunIndex < numberOfBoundedParagraphRuns)
698 //BP: BoundedParagraph
699 const BoundedParagraphRun& boundedParagraphRun = boundedParagraphRuns[boundedParagraphRunIndex];
700 const CharacterIndex& characterStartIndexBP = boundedParagraphRun.characterRun.characterIndex;
701 const Length& numberOfCharactersBP = boundedParagraphRun.characterRun.numberOfCharacters;
702 const CharacterIndex characterEndIndexBP = characterStartIndexBP + numberOfCharactersBP - 1u;
704 CharacterIndex decidedAlignStartIndex = alignIndex;
705 Length decidedAlignNumberOfCharacters = alignEndIndex - alignIndex + 1u;
706 Text::HorizontalAlignment::Type decidedHorizontalAlignment = impl.mModel->mHorizontalAlignment;
709 * Shortcuts to explain indexes cases:
711 * AS: Alignment Start Index
712 * AE: Alignment End Index
713 * PS: Paragraph Start Index
714 * PE: Paragraph End Index
715 * B: BoundedParagraph Alignment
720 if(alignIndex < characterStartIndexBP && characterStartIndexBP <= alignEndIndex) /// AS.MMMMMM.PS--------AE
722 // Alignment from "Alignment Start Index" to index before "Paragraph Start Index" according to "Model Alignment"
723 decidedAlignStartIndex = alignIndex;
724 decidedAlignNumberOfCharacters = characterStartIndexBP - alignIndex;
725 decidedHorizontalAlignment = impl.mModel->mHorizontalAlignment;
727 // Need to re-heck the case of current bounded paragraph
728 alignIndex = characterStartIndexBP; // Shift AS to be PS
730 else if((characterStartIndexBP <= alignIndex && alignIndex <= characterEndIndexBP) || /// ---PS.BBBBBBB.AS.BBBBBBB.PE---
731 (characterStartIndexBP <= alignEndIndex && alignEndIndex <= characterEndIndexBP)) /// ---PS.BBBBBB.AE.BBBBBBB.PE---
733 // Alignment from "Paragraph Start Index" to "Paragraph End Index" according to "BoundedParagraph Alignment"
734 decidedAlignStartIndex = characterStartIndexBP;
735 decidedAlignNumberOfCharacters = numberOfCharactersBP;
736 decidedHorizontalAlignment = boundedParagraphRun.horizontalAlignmentDefined ? boundedParagraphRun.horizontalAlignment : impl.mModel->mHorizontalAlignment;
738 alignIndex = characterEndIndexBP + 1u; // Shift AS to be after PE direct
739 boundedParagraphRunIndex++; // Align then check the case of next bounded paragraph
743 boundedParagraphRunIndex++; // Check the case of next bounded paragraph
747 impl.mLayoutEngine.Align(size,
748 decidedAlignStartIndex,
749 decidedAlignNumberOfCharacters,
750 decidedHorizontalAlignment,
752 impl.mModel->mAlignmentOffset,
753 impl.mLayoutDirection,
754 (impl.mModel->mMatchLayoutDirection != DevelText::MatchLayoutDirection::CONTENTS));
757 //Align the remaining that is not aligned
758 if(alignIndex <= alignEndIndex)
760 impl.mLayoutEngine.Align(size,
762 (alignEndIndex - alignIndex + 1u),
763 impl.mModel->mHorizontalAlignment,
765 impl.mModel->mAlignmentOffset,
766 impl.mLayoutDirection,
767 (impl.mModel->mMatchLayoutDirection != DevelText::MatchLayoutDirection::CONTENTS));
772 void Controller::Relayouter::CalculateVerticalOffset(Controller& controller, const Size& controlSize)
774 Controller::Impl& impl = *controller.mImpl;
775 ModelPtr& model = impl.mModel;
776 VisualModelPtr& visualModel = model->mVisualModel;
777 Size layoutSize = model->mVisualModel->GetLayoutSize();
778 Size oldLayoutSize = layoutSize;
780 bool needRecalc = false;
781 float defaultFontLineHeight = impl.GetDefaultFontLineHeight();
783 if(fabsf(layoutSize.height) < Math::MACHINE_EPSILON_1000)
785 // Get the line height of the default font.
786 layoutSize.height = defaultFontLineHeight;
789 // Whether the text control is editable
790 const bool isEditable = NULL != impl.mEventData;
791 if(isEditable && layoutSize.height != defaultFontLineHeight && impl.IsShowingPlaceholderText())
793 // This code prevents the wrong positioning of cursor when the layout size is bigger/smaller than defaultFontLineHeight.
794 // This situation occurs when the size of placeholder text is different from the default text.
795 layoutSize.height = defaultFontLineHeight;
799 switch(model->mVerticalAlignment)
801 case VerticalAlignment::TOP:
803 model->mScrollPosition.y = 0.f;
807 case VerticalAlignment::CENTER:
809 model->mScrollPosition.y = floorf(0.5f * (controlSize.height - layoutSize.height)); // try to avoid pixel alignment.
810 if(needRecalc) offsetY = floorf(0.5f * (layoutSize.height - oldLayoutSize.height));
813 case VerticalAlignment::BOTTOM:
815 model->mScrollPosition.y = controlSize.height - layoutSize.height;
816 if(needRecalc) offsetY = layoutSize.height - oldLayoutSize.height;
823 // Update glyphPositions according to recalculation.
824 const Length positionCount = visualModel->mGlyphPositions.Count();
825 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
826 for(Length index = 0u; index < positionCount; index++)
828 glyphPositions[index].y += offsetY;
835 } // namespace Toolkit