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 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 // Whether the text control is editable
90 const bool isEditable = NULL != impl.mEventData;
94 impl.UpdateModel(onlyOnceOperations);
96 if(impl.mIsAutoScrollEnabled)
98 // Layout the text for the new width.
99 operationsPending = static_cast<OperationsMask>(operationsPending | requestedOperationsMask);
103 requestedControllerSize,
104 static_cast<OperationsMask>(onlyOnceOperations | requestedOperationsMask),
105 calculatedLayoutSize);
107 textUpdateInfo.Clear();
108 textUpdateInfo.mClearAll = true;
110 // Do not do again the only once operations.
111 operationsPending = static_cast<OperationsMask>(operationsPending & ~onlyOnceOperations);
115 // This is to keep Index to the first character to be updated.
116 // Then restore it after calling Clear method.
117 auto updateInfoCharIndexBackup = textUpdateInfo.mCharacterIndex;
119 // Layout the text for the new width.
120 // Apply the pending operations, requested operations and the only once operations.
121 // Then remove onlyOnceOperations
122 operationsPending = static_cast<OperationsMask>(operationsPending | requestedOperationsMask | onlyOnceOperations);
124 // Make sure the model is up-to-date before layouting
125 impl.UpdateModel(static_cast<OperationsMask>(operationsPending & ~UPDATE_LAYOUT_SIZE));
128 requestedControllerSize,
129 static_cast<OperationsMask>(operationsPending & ~UPDATE_LAYOUT_SIZE),
130 calculatedLayoutSize);
132 // Clear the update info. This info will be set the next time the text is updated.
133 textUpdateInfo.Clear();
135 //TODO: Refactor "DoRelayout" and extract common code of size calculation without modifying attributes of mVisualModel,
136 //TODO: then calculate GlyphPositions. Lines, Size, Layout for Natural-Size
137 //TODO: and utilize the values in OperationsPending and TextUpdateInfo without changing the original one.
138 //TODO: Also it will improve performance because there is no need todo FullRelyout on the next need for layouting.
140 // FullRelayoutNeeded should be true because DoRelayout is MAX_FLOAT, MAX_FLOAT.
141 // By this no need to take backup and restore it.
142 textUpdateInfo.mFullRelayoutNeeded = true;
144 // Restore mCharacterIndex. Because "Clear" set it to the maximum integer.
145 // The "CalculateTextUpdateIndices" does not work proprely because the mCharacterIndex will be greater than mPreviousNumberOfCharacters.
146 // Which apply an assumption to update only the last paragraph. That could cause many of out of index crashes.
147 textUpdateInfo.mCharacterIndex = updateInfoCharIndexBackup;
150 // Do the size related operations again.
151 operationsPending = static_cast<OperationsMask>(operationsPending | sizeOperations);
153 // Restore the actual control's size.
154 visualModel->mControlSize = actualControlSize;
156 return calculatedLayoutSize;
159 Vector3 Controller::Relayouter::GetNaturalSize(Controller& controller)
161 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n");
162 Vector3 naturalSizeVec3;
164 // Make sure the model is up-to-date before layouting
165 EventHandler::ProcessModifyEvents(controller);
167 Controller::Impl& impl = *controller.mImpl;
168 ModelPtr& model = impl.mModel;
169 VisualModelPtr& visualModel = model->mVisualModel;
171 if(impl.mRecalculateNaturalSize)
175 // Layout the text for the new width.
176 OperationsMask requestedOperationsMask = static_cast<OperationsMask>(LAYOUT | REORDER);
177 Size sizeMaxWidthAndMaxHeight = Size(MAX_FLOAT, MAX_FLOAT);
179 naturalSize = CalculateLayoutSizeOnRequiredControllerSize(controller, sizeMaxWidthAndMaxHeight, requestedOperationsMask);
181 // Stores the natural size to avoid recalculate it again
182 // unless the text/style changes.
183 visualModel->SetNaturalSize(naturalSize);
184 naturalSizeVec3 = naturalSize;
186 impl.mRecalculateNaturalSize = false;
188 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSizeVec3.x, naturalSizeVec3.y, naturalSizeVec3.z);
192 naturalSizeVec3 = visualModel->GetNaturalSize();
194 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSizeVec3.x, naturalSizeVec3.y, naturalSizeVec3.z);
197 naturalSizeVec3.x = ConvertToEven(naturalSizeVec3.x);
198 naturalSizeVec3.y = ConvertToEven(naturalSizeVec3.y);
200 return naturalSizeVec3;
203 bool Controller::Relayouter::CheckForTextFit(Controller& controller, float pointSize, const Size& layoutSize)
206 Controller::Impl& impl = *controller.mImpl;
207 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
208 impl.mFontDefaults->mFitPointSize = pointSize;
209 impl.mFontDefaults->sizeDefined = true;
210 impl.ClearFontData();
212 // Operations that can be done only once until the text changes.
213 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>(CONVERT_TO_UTF32 |
221 textUpdateInfo.mParagraphCharacterIndex = 0u;
222 textUpdateInfo.mRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
224 // Make sure the model is up-to-date before layouting
225 impl.UpdateModel(onlyOnceOperations);
228 Size(layoutSize.width, MAX_FLOAT),
229 static_cast<OperationsMask>(onlyOnceOperations | LAYOUT),
232 // Clear the update info. This info will be set the next time the text is updated.
233 textUpdateInfo.Clear();
234 textUpdateInfo.mClearAll = true;
236 if(textSize.width > layoutSize.width || textSize.height > layoutSize.height)
243 void Controller::Relayouter::FitPointSizeforLayout(Controller& controller, const Size& layoutSize)
245 Controller::Impl& impl = *controller.mImpl;
247 const OperationsMask operations = impl.mOperationsPending;
248 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations) || impl.mTextFitContentSize != layoutSize)
250 ModelPtr& model = impl.mModel;
252 bool actualellipsis = model->mElideEnabled;
253 float minPointSize = impl.mTextFitMinSize;
254 float maxPointSize = impl.mTextFitMaxSize;
255 float pointInterval = impl.mTextFitStepSize;
256 float currentFitPointSize = impl.mFontDefaults->mFitPointSize;
258 model->mElideEnabled = false;
259 Vector<float> pointSizeArray;
262 if(pointInterval < 1.f)
264 impl.mTextFitStepSize = pointInterval = 1.0f;
267 pointSizeArray.Reserve(static_cast<unsigned int>(ceil((maxPointSize - minPointSize) / pointInterval)));
269 for(float i = minPointSize; i < maxPointSize; i += pointInterval)
271 pointSizeArray.PushBack(i);
274 pointSizeArray.PushBack(maxPointSize);
276 int bestSizeIndex = 0;
277 int min = bestSizeIndex + 1;
278 int max = pointSizeArray.Size() - 1;
281 int destI = (min + max) / 2;
283 if(CheckForTextFit(controller, pointSizeArray[destI], layoutSize))
295 model->mElideEnabled = actualellipsis;
296 if(currentFitPointSize != pointSizeArray[bestSizeIndex])
298 impl.mTextFitChanged = true;
300 impl.mFontDefaults->mFitPointSize = pointSizeArray[bestSizeIndex];
301 impl.mFontDefaults->sizeDefined = true;
302 impl.ClearFontData();
306 float Controller::Relayouter::GetHeightForWidth(Controller& controller, float width)
308 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", &controller, width);
310 // Make sure the model is up-to-date before layouting
311 EventHandler::ProcessModifyEvents(controller);
313 Controller::Impl& impl = *controller.mImpl;
314 ModelPtr& model = impl.mModel;
315 VisualModelPtr& visualModel = model->mVisualModel;
316 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
320 if(fabsf(width - visualModel->mControlSize.width) > Math::MACHINE_EPSILON_1000 ||
321 textUpdateInfo.mFullRelayoutNeeded ||
322 textUpdateInfo.mClearAll)
324 // Layout the text for the new width.
325 OperationsMask requestedOperationsMask = static_cast<OperationsMask>(LAYOUT);
326 Size sizeRequestedWidthAndMaxHeight = Size(width, MAX_FLOAT);
328 layoutSize = CalculateLayoutSizeOnRequiredControllerSize(controller, sizeRequestedWidthAndMaxHeight, requestedOperationsMask);
330 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height);
334 layoutSize = visualModel->GetLayoutSize();
335 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height);
338 return layoutSize.height;
341 Controller::UpdateTextType Controller::Relayouter::Relayout(Controller& controller, const Size& size, Dali::LayoutDirection::Type layoutDirection)
343 Controller::Impl& impl = *controller.mImpl;
344 ModelPtr& model = impl.mModel;
345 VisualModelPtr& visualModel = model->mVisualModel;
346 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
348 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", &controller, size.width, size.height, impl.mIsAutoScrollEnabled ? "true" : "false");
350 UpdateTextType updateTextType = NONE_UPDATED;
352 if((size.width < Math::MACHINE_EPSILON_1000) || (size.height < Math::MACHINE_EPSILON_1000))
354 if(0u != visualModel->mGlyphPositions.Count())
356 visualModel->mGlyphPositions.Clear();
357 updateTextType = MODEL_UPDATED;
360 // Clear the update info. This info will be set the next time the text is updated.
361 textUpdateInfo.Clear();
363 // Not worth to relayout if width or height is equal to zero.
364 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n");
366 return updateTextType;
369 // Whether a new size has been set.
370 const bool newSize = (size != visualModel->mControlSize);
372 // Get a reference to the pending operations member
373 OperationsMask& operationsPending = impl.mOperationsPending;
377 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", visualModel->mControlSize.width, visualModel->mControlSize.height);
379 if((0 == textUpdateInfo.mNumberOfCharactersToAdd) &&
380 (0 == textUpdateInfo.mPreviousNumberOfCharacters) &&
381 ((visualModel->mControlSize.width < Math::MACHINE_EPSILON_1000) || (visualModel->mControlSize.height < Math::MACHINE_EPSILON_1000)))
383 textUpdateInfo.mNumberOfCharactersToAdd = model->mLogicalModel->mText.Count();
386 // Layout operations that need to be done if the size changes.
387 operationsPending = static_cast<OperationsMask>(operationsPending |
392 // Set the update info to relayout the whole text.
393 textUpdateInfo.mFullRelayoutNeeded = true;
394 textUpdateInfo.mCharacterIndex = 0u;
396 // Store the size used to layout the text.
397 visualModel->mControlSize = size;
400 // Whether there are modify events.
401 if(0u != impl.mModifyEvents.Count())
403 // Style operations that need to be done if the text is modified.
404 operationsPending = static_cast<OperationsMask>(operationsPending | COLOR);
407 // Set the update info to elide the text.
408 if(model->mElideEnabled ||
409 ((NULL != impl.mEventData) && impl.mEventData->mIsPlaceholderElideEnabled))
411 // Update Text layout for applying elided
412 operationsPending = static_cast<OperationsMask>(operationsPending |
417 textUpdateInfo.mFullRelayoutNeeded = true;
418 textUpdateInfo.mCharacterIndex = 0u;
421 bool layoutDirectionChanged = false;
422 if(impl.mLayoutDirection != layoutDirection)
424 // Flag to indicate that the layout direction has changed.
425 layoutDirectionChanged = true;
426 // Clear the update info. This info will be set the next time the text is updated.
427 textUpdateInfo.mClearAll = true;
428 // Apply modifications to the model
429 // Shape the text again is needed because characters like '()[]{}' have to be mirrored and the glyphs generated again.
430 operationsPending = static_cast<OperationsMask>(operationsPending |
438 impl.mLayoutDirection = layoutDirection;
441 // Make sure the model is up-to-date before layouting.
442 EventHandler::ProcessModifyEvents(controller);
443 bool updated = impl.UpdateModel(operationsPending);
447 updated = DoRelayout(impl, size, operationsPending, layoutSize) || updated;
451 updateTextType = MODEL_UPDATED;
454 // Do not re-do any operation until something changes.
455 operationsPending = NO_OPERATION;
456 model->mScrollPositionLast = model->mScrollPosition;
458 // Whether the text control is editable
459 const bool isEditable = NULL != impl.mEventData;
461 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
463 if(newSize && isEditable)
465 offset = model->mScrollPosition;
468 if(!isEditable || !controller.IsMultiLineEnabled())
470 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
471 CalculateVerticalOffset(impl, size);
475 // If layoutSize is bigger than size, vertical align has no meaning.
476 if(layoutSize.y < size.y)
478 CalculateVerticalOffset(impl, size);
481 impl.mEventData->mScrollAfterDelete = false;
488 if(newSize || layoutDirectionChanged)
490 // If there is a new size or layout direction is changed, the scroll position needs to be clamped.
491 impl.ClampHorizontalScroll(layoutSize);
493 // Update the decorator's positions is needed if there is a new size.
494 impl.mEventData->mDecorator->UpdatePositions(model->mScrollPosition - offset);
496 // All decorator elements need to be updated.
497 if(EventData::IsEditingState(impl.mEventData->mState))
499 impl.mEventData->mScrollAfterUpdatePosition = true;
500 impl.mEventData->mUpdateCursorPosition = true;
501 impl.mEventData->mUpdateGrabHandlePosition = true;
503 else if(impl.mEventData->mState == EventData::SELECTING)
505 impl.mEventData->mUpdateHighlightBox = true;
509 // Move the cursor, grab handle etc.
510 if(impl.ProcessInputEvents())
512 updateTextType = static_cast<UpdateTextType>(updateTextType | DECORATOR_UPDATED);
516 // Clear the update info. This info will be set the next time the text is updated.
517 textUpdateInfo.Clear();
518 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout\n");
520 return updateTextType;
523 bool Controller::Relayouter::DoRelayout(Controller::Impl& impl, const Size& size, OperationsMask operationsRequired, Size& layoutSize)
525 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::Relayouter::DoRelayout %p size %f,%f\n", &impl, size.width, size.height);
526 bool viewUpdated(false);
528 // Calculate the operations to be done.
529 const OperationsMask operations = static_cast<OperationsMask>(impl.mOperationsPending & operationsRequired);
531 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
532 const CharacterIndex startIndex = textUpdateInfo.mParagraphCharacterIndex;
533 const Length requestedNumberOfCharacters = textUpdateInfo.mRequestedNumberOfCharacters;
535 // Get the current layout size.
536 VisualModelPtr& visualModel = impl.mModel->mVisualModel;
537 layoutSize = visualModel->GetLayoutSize();
539 if(NO_OPERATION != (LAYOUT & operations))
541 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
543 // Some vectors with data needed to layout and reorder may be void
544 // after the first time the text has been laid out.
545 // Fill the vectors again.
547 // Calculate the number of glyphs to layout.
548 const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
549 const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
550 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
551 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
553 const CharacterIndex lastIndex = startIndex + ((requestedNumberOfCharacters > 0u) ? requestedNumberOfCharacters - 1u : 0u);
554 const GlyphIndex startGlyphIndex = textUpdateInfo.mStartGlyphIndex;
556 // Make sure the index is not out of bound
557 if(charactersToGlyph.Count() != glyphsPerCharacter.Count() ||
558 requestedNumberOfCharacters > charactersToGlyph.Count() ||
559 (lastIndex > charactersToGlyph.Count() && charactersToGlyph.Count() > 0u))
561 std::string currentText;
562 impl.GetText(currentText);
564 DALI_LOG_ERROR("Controller::DoRelayout: Attempting to access invalid buffer\n");
565 DALI_LOG_ERROR("Current text is: %s\n", currentText.c_str());
566 DALI_LOG_ERROR("startIndex: %u, lastIndex: %u, requestedNumberOfCharacters: %u, charactersToGlyph.Count = %lu, glyphsPerCharacter.Count = %lu\n", startIndex, lastIndex, requestedNumberOfCharacters, charactersToGlyph.Count(), glyphsPerCharacter.Count());
571 const Length numberOfGlyphs = (requestedNumberOfCharacters > 0u) ? *(charactersToGlyphBuffer + lastIndex) + *(glyphsPerCharacterBuffer + lastIndex) - startGlyphIndex : 0u;
572 const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
574 if(0u == totalNumberOfGlyphs)
576 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
578 visualModel->SetLayoutSize(Size::ZERO);
581 // Nothing else to do if there is no glyphs.
582 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n");
586 // Set the layout parameters.
587 Layout::Parameters layoutParameters(size, impl.mModel);
589 // Resize the vector of positions to have the same size than the vector of glyphs.
590 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
591 glyphPositions.Resize(totalNumberOfGlyphs);
593 // Whether the last character is a new paragraph character.
594 const Character* const textBuffer = impl.mModel->mLogicalModel->mText.Begin();
595 textUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph(*(textBuffer + (impl.mModel->mLogicalModel->mText.Count() - 1u)));
596 layoutParameters.isLastNewParagraph = textUpdateInfo.mIsLastCharacterNewParagraph;
598 // The initial glyph and the number of glyphs to layout.
599 layoutParameters.startGlyphIndex = startGlyphIndex;
600 layoutParameters.numberOfGlyphs = numberOfGlyphs;
601 layoutParameters.startLineIndex = textUpdateInfo.mStartLineIndex;
602 layoutParameters.estimatedNumberOfLines = textUpdateInfo.mEstimatedNumberOfLines;
604 // Update the ellipsis
605 bool elideTextEnabled = impl.mModel->mElideEnabled;
606 auto ellipsisPosition = impl.mModel->mEllipsisPosition;
608 if(NULL != impl.mEventData)
610 if(impl.mEventData->mPlaceholderEllipsisFlag && impl.IsShowingPlaceholderText())
612 elideTextEnabled = impl.mEventData->mIsPlaceholderElideEnabled;
614 else if(EventData::INACTIVE != impl.mEventData->mState)
616 // Disable ellipsis when editing
617 elideTextEnabled = false;
620 // Reset the scroll position in inactive state
621 if(elideTextEnabled && (impl.mEventData->mState == EventData::INACTIVE))
623 impl.ResetScrollPosition();
627 // Update the visual model.
628 bool isAutoScrollEnabled = impl.mIsAutoScrollEnabled;
629 bool isAutoScrollMaxTextureExceeded = impl.mIsAutoScrollMaxTextureExceeded;
632 viewUpdated = impl.mLayoutEngine.LayoutText(layoutParameters,
636 isAutoScrollMaxTextureExceeded,
638 impl.mIsAutoScrollEnabled = isAutoScrollEnabled;
640 viewUpdated = viewUpdated || (newLayoutSize != layoutSize);
644 layoutSize = newLayoutSize;
646 if(NO_OPERATION != (UPDATE_DIRECTION & operations))
648 impl.mIsTextDirectionRTL = false;
651 if((NO_OPERATION != (UPDATE_DIRECTION & operations)) && !visualModel->mLines.Empty())
653 impl.mIsTextDirectionRTL = visualModel->mLines[0u].direction;
656 // Sets the layout size.
657 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
659 visualModel->SetLayoutSize(layoutSize);
664 if(NO_OPERATION != (ALIGN & operations))
666 DoRelayoutHorizontalAlignment(impl, size, startIndex, requestedNumberOfCharacters);
669 #if defined(DEBUG_ENABLED)
670 std::string currentText;
671 impl.GetText(currentText);
672 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Controller::Relayouter::DoRelayout [%p] mImpl->mIsTextDirectionRTL[%s] [%s]\n", &impl, (impl.mIsTextDirectionRTL) ? "true" : "false", currentText.c_str());
674 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayouter::DoRelayout, view updated %s\n", (viewUpdated ? "true" : "false"));
678 void Controller::Relayouter::DoRelayoutHorizontalAlignment(Controller::Impl& impl,
680 const CharacterIndex startIndex,
681 const Length requestedNumberOfCharacters)
684 VisualModelPtr& visualModel = impl.mModel->mVisualModel;
686 // The laid-out lines.
687 Vector<LineRun>& lines = visualModel->mLines;
689 CharacterIndex alignStartIndex = startIndex;
690 Length alignRequestedNumberOfCharacters = requestedNumberOfCharacters;
692 // the whole text needs to be full aligned.
693 // If you do not do a full aligned, only the last line of the multiline input is aligned.
694 if(impl.mEventData && impl.mEventData->mUpdateAlignment)
696 alignStartIndex = 0u;
697 alignRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
698 impl.mEventData->mUpdateAlignment = false;
701 // If there is no BoundedParagraphRuns then apply the alignment of controller.
702 // Check whether the layout is single line. It's needed to apply one alignment for single-line.
703 // In single-line layout case we need to check whether to follow the alignment of controller or the first BoundedParagraph.
704 // Apply BoundedParagraph's alignment if and only if there is one BoundedParagraph contains all characters. Otherwise follow controller's alignment.
705 const bool isFollowControllerAlignment = ((impl.mModel->GetNumberOfBoundedParagraphRuns() == 0u) ||
706 ((Layout::Engine::SINGLE_LINE_BOX == impl.mLayoutEngine.GetLayout()) &&
707 (impl.mModel->GetBoundedParagraphRuns()[0].characterRun.numberOfCharacters != impl.mModel->mLogicalModel->mText.Count())));
709 if(isFollowControllerAlignment)
711 // Need to align with the control's size as the text may contain lines
712 // starting either with left to right text or right to left.
713 impl.mLayoutEngine.Align(size,
715 alignRequestedNumberOfCharacters,
716 impl.mModel->mHorizontalAlignment,
718 impl.mModel->mAlignmentOffset,
719 impl.mLayoutDirection,
720 (impl.mModel->mMatchLayoutDirection != DevelText::MatchLayoutDirection::CONTENTS));
724 //Override the controller horizontal-alignment by horizontal-alignment of bounded paragraph.
725 const Length& numberOfBoundedParagraphRuns = impl.mModel->GetNumberOfBoundedParagraphRuns();
726 const Vector<BoundedParagraphRun>& boundedParagraphRuns = impl.mModel->GetBoundedParagraphRuns();
727 const CharacterIndex alignEndIndex = alignStartIndex + alignRequestedNumberOfCharacters - 1u;
729 Length alignIndex = alignStartIndex;
730 Length boundedParagraphRunIndex = 0u;
732 while(alignIndex <= alignEndIndex && boundedParagraphRunIndex < numberOfBoundedParagraphRuns)
734 //BP: BoundedParagraph
735 const BoundedParagraphRun& boundedParagraphRun = boundedParagraphRuns[boundedParagraphRunIndex];
736 const CharacterIndex& characterStartIndexBP = boundedParagraphRun.characterRun.characterIndex;
737 const Length& numberOfCharactersBP = boundedParagraphRun.characterRun.numberOfCharacters;
738 const CharacterIndex characterEndIndexBP = characterStartIndexBP + numberOfCharactersBP - 1u;
740 CharacterIndex decidedAlignStartIndex = alignIndex;
741 Length decidedAlignNumberOfCharacters = alignEndIndex - alignIndex + 1u;
742 Text::HorizontalAlignment::Type decidedHorizontalAlignment = impl.mModel->mHorizontalAlignment;
745 * Shortcuts to explain indexes cases:
747 * AS: Alignment Start Index
748 * AE: Alignment End Index
749 * PS: Paragraph Start Index
750 * PE: Paragraph End Index
751 * B: BoundedParagraph Alignment
756 if(alignIndex < characterStartIndexBP && characterStartIndexBP <= alignEndIndex) /// AS.MMMMMM.PS--------AE
758 // Alignment from "Alignment Start Index" to index before "Paragraph Start Index" according to "Model Alignment"
759 decidedAlignStartIndex = alignIndex;
760 decidedAlignNumberOfCharacters = characterStartIndexBP - alignIndex;
761 decidedHorizontalAlignment = impl.mModel->mHorizontalAlignment;
763 // Need to re-heck the case of current bounded paragraph
764 alignIndex = characterStartIndexBP; // Shift AS to be PS
766 else if((characterStartIndexBP <= alignIndex && alignIndex <= characterEndIndexBP) || /// ---PS.BBBBBBB.AS.BBBBBBB.PE---
767 (characterStartIndexBP <= alignEndIndex && alignEndIndex <= characterEndIndexBP)) /// ---PS.BBBBBB.AE.BBBBBBB.PE---
769 // Alignment from "Paragraph Start Index" to "Paragraph End Index" according to "BoundedParagraph Alignment"
770 decidedAlignStartIndex = characterStartIndexBP;
771 decidedAlignNumberOfCharacters = numberOfCharactersBP;
772 decidedHorizontalAlignment = boundedParagraphRun.horizontalAlignmentDefined ? boundedParagraphRun.horizontalAlignment : impl.mModel->mHorizontalAlignment;
774 alignIndex = characterEndIndexBP + 1u; // Shift AS to be after PE direct
775 boundedParagraphRunIndex++; // Align then check the case of next bounded paragraph
779 boundedParagraphRunIndex++; // Check the case of next bounded paragraph
783 impl.mLayoutEngine.Align(size,
784 decidedAlignStartIndex,
785 decidedAlignNumberOfCharacters,
786 decidedHorizontalAlignment,
788 impl.mModel->mAlignmentOffset,
789 impl.mLayoutDirection,
790 (impl.mModel->mMatchLayoutDirection != DevelText::MatchLayoutDirection::CONTENTS));
793 //Align the remaining that is not aligned
794 if(alignIndex <= alignEndIndex)
796 impl.mLayoutEngine.Align(size,
798 (alignEndIndex - alignIndex + 1u),
799 impl.mModel->mHorizontalAlignment,
801 impl.mModel->mAlignmentOffset,
802 impl.mLayoutDirection,
803 (impl.mModel->mMatchLayoutDirection != DevelText::MatchLayoutDirection::CONTENTS));
808 void Controller::Relayouter::CalculateVerticalOffset(Controller::Impl& impl, const Size& controlSize)
810 ModelPtr& model = impl.mModel;
811 VisualModelPtr& visualModel = model->mVisualModel;
812 Size layoutSize = model->mVisualModel->GetLayoutSize();
813 Size oldLayoutSize = layoutSize;
815 bool needRecalc = false;
816 float defaultFontLineHeight = impl.GetDefaultFontLineHeight();
818 if(fabsf(layoutSize.height) < Math::MACHINE_EPSILON_1000)
820 // Get the line height of the default font.
821 layoutSize.height = defaultFontLineHeight;
824 // Whether the text control is editable
825 const bool isEditable = NULL != impl.mEventData;
826 if(isEditable && layoutSize.height != defaultFontLineHeight && impl.IsShowingPlaceholderText())
828 // This code prevents the wrong positioning of cursor when the layout size is bigger/smaller than defaultFontLineHeight.
829 // This situation occurs when the size of placeholder text is different from the default text.
830 layoutSize.height = defaultFontLineHeight;
834 switch(model->mVerticalAlignment)
836 case VerticalAlignment::TOP:
838 model->mScrollPosition.y = 0.f;
842 case VerticalAlignment::CENTER:
844 model->mScrollPosition.y = floorf(0.5f * (controlSize.height - layoutSize.height)); // try to avoid pixel alignment.
845 if(needRecalc) offsetY = floorf(0.5f * (layoutSize.height - oldLayoutSize.height));
848 case VerticalAlignment::BOTTOM:
850 model->mScrollPosition.y = controlSize.height - layoutSize.height;
851 if(needRecalc) offsetY = layoutSize.height - oldLayoutSize.height;
858 // Update glyphPositions according to recalculation.
859 const Length positionCount = visualModel->mGlyphPositions.Count();
860 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
861 for(Length index = 0u; index < positionCount; index++)
863 glyphPositions[index].y += offsetY;
870 } // namespace Toolkit