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);
97 requestedControllerSize,
98 static_cast<OperationsMask>(onlyOnceOperations | requestedOperationsMask),
99 calculatedLayoutSize);
101 textUpdateInfo.Clear();
102 textUpdateInfo.mClearAll = true;
104 // Do not do again the only once operations.
105 operationsPending = static_cast<OperationsMask>(operationsPending & ~onlyOnceOperations);
109 // This is to keep Index to the first character to be updated.
110 // Then restore it after calling Clear method.
111 auto updateInfoCharIndexBackup = textUpdateInfo.mCharacterIndex;
113 // Layout the text for the new width.
114 // Apply the pending operations, requested operations and the only once operations.
115 // Then remove onlyOnceOperations
116 operationsPending = static_cast<OperationsMask>(operationsPending | requestedOperationsMask | onlyOnceOperations);
118 // Make sure the model is up-to-date before layouting
119 impl.UpdateModel(static_cast<OperationsMask>(operationsPending & ~UPDATE_LAYOUT_SIZE));
122 requestedControllerSize,
123 static_cast<OperationsMask>(operationsPending & ~UPDATE_LAYOUT_SIZE),
124 calculatedLayoutSize);
126 // Clear the update info. This info will be set the next time the text is updated.
127 textUpdateInfo.Clear();
129 //TODO: Refactor "DoRelayout" and extract common code of size calculation without modifying attributes of mVisualModel,
130 //TODO: then calculate GlyphPositions. Lines, Size, Layout for Natural-Size
131 //TODO: and utilize the values in OperationsPending and TextUpdateInfo without changing the original one.
132 //TODO: Also it will improve performance because there is no need todo FullRelyout on the next need for layouting.
134 // FullRelayoutNeeded should be true because DoRelayout is MAX_FLOAT, MAX_FLOAT.
135 // By this no need to take backup and restore it.
136 textUpdateInfo.mFullRelayoutNeeded = true;
138 // Restore mCharacterIndex. Because "Clear" set it to the maximum integer.
139 // The "CalculateTextUpdateIndices" does not work proprely because the mCharacterIndex will be greater than mPreviousNumberOfCharacters.
140 // Which apply an assumption to update only the last paragraph. That could cause many of out of index crashes.
141 textUpdateInfo.mCharacterIndex = updateInfoCharIndexBackup;
144 // Do the size related operations again.
145 operationsPending = static_cast<OperationsMask>(operationsPending | sizeOperations);
147 // Restore the actual control's size.
148 visualModel->mControlSize = actualControlSize;
150 return calculatedLayoutSize;
153 Vector3 Controller::Relayouter::GetNaturalSize(Controller& controller)
155 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n");
156 Vector3 naturalSizeVec3;
158 // Make sure the model is up-to-date before layouting
159 EventHandler::ProcessModifyEvents(controller);
161 Controller::Impl& impl = *controller.mImpl;
162 ModelPtr& model = impl.mModel;
163 VisualModelPtr& visualModel = model->mVisualModel;
165 if(impl.mRecalculateNaturalSize)
169 // Layout the text for the new width.
170 OperationsMask requestedOperationsMask = static_cast<OperationsMask>(LAYOUT | REORDER);
171 Size sizeMaxWidthAndMaxHeight = Size(MAX_FLOAT, MAX_FLOAT);
173 naturalSize = CalculateLayoutSizeOnRequiredControllerSize(controller, sizeMaxWidthAndMaxHeight, requestedOperationsMask);
175 // Stores the natural size to avoid recalculate it again
176 // unless the text/style changes.
177 visualModel->SetNaturalSize(naturalSize);
178 naturalSizeVec3 = naturalSize;
180 impl.mRecalculateNaturalSize = false;
182 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSizeVec3.x, naturalSizeVec3.y, naturalSizeVec3.z);
186 naturalSizeVec3 = visualModel->GetNaturalSize();
188 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSizeVec3.x, naturalSizeVec3.y, naturalSizeVec3.z);
191 naturalSizeVec3.x = ConvertToEven(naturalSizeVec3.x);
192 naturalSizeVec3.y = ConvertToEven(naturalSizeVec3.y);
194 return naturalSizeVec3;
197 bool Controller::Relayouter::CheckForTextFit(Controller& controller, float pointSize, const Size& layoutSize)
200 Controller::Impl& impl = *controller.mImpl;
201 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
202 impl.mFontDefaults->mFitPointSize = pointSize;
203 impl.mFontDefaults->sizeDefined = true;
204 impl.ClearFontData();
206 // Operations that can be done only once until the text changes.
207 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>(CONVERT_TO_UTF32 |
215 textUpdateInfo.mParagraphCharacterIndex = 0u;
216 textUpdateInfo.mRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
218 // Make sure the model is up-to-date before layouting
219 impl.UpdateModel(onlyOnceOperations);
222 Size(layoutSize.width, MAX_FLOAT),
223 static_cast<OperationsMask>(onlyOnceOperations | LAYOUT),
226 // Clear the update info. This info will be set the next time the text is updated.
227 textUpdateInfo.Clear();
228 textUpdateInfo.mClearAll = true;
230 if(textSize.width > layoutSize.width || textSize.height > layoutSize.height)
237 void Controller::Relayouter::FitPointSizeforLayout(Controller& controller, const Size& layoutSize)
239 Controller::Impl& impl = *controller.mImpl;
241 const OperationsMask operations = impl.mOperationsPending;
242 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations) || impl.mTextFitContentSize != layoutSize)
244 ModelPtr& model = impl.mModel;
246 bool actualellipsis = model->mElideEnabled;
247 float minPointSize = impl.mTextFitMinSize;
248 float maxPointSize = impl.mTextFitMaxSize;
249 float pointInterval = impl.mTextFitStepSize;
250 float currentFitPointSize = impl.mFontDefaults->mFitPointSize;
252 model->mElideEnabled = false;
253 Vector<float> pointSizeArray;
256 if(pointInterval < 1.f)
258 impl.mTextFitStepSize = pointInterval = 1.0f;
261 pointSizeArray.Reserve(static_cast<unsigned int>(ceil((maxPointSize - minPointSize) / pointInterval)));
263 for(float i = minPointSize; i < maxPointSize; i += pointInterval)
265 pointSizeArray.PushBack(i);
268 pointSizeArray.PushBack(maxPointSize);
270 int bestSizeIndex = 0;
271 int min = bestSizeIndex + 1;
272 int max = pointSizeArray.Size() - 1;
275 int destI = (min + max) / 2;
277 if(CheckForTextFit(controller, pointSizeArray[destI], layoutSize))
289 model->mElideEnabled = actualellipsis;
290 if(currentFitPointSize != pointSizeArray[bestSizeIndex])
292 impl.mTextFitChanged = true;
294 impl.mFontDefaults->mFitPointSize = pointSizeArray[bestSizeIndex];
295 impl.mFontDefaults->sizeDefined = true;
296 impl.ClearFontData();
300 float Controller::Relayouter::GetHeightForWidth(Controller& controller, float width)
302 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", &controller, width);
304 // Make sure the model is up-to-date before layouting
305 EventHandler::ProcessModifyEvents(controller);
307 Controller::Impl& impl = *controller.mImpl;
308 ModelPtr& model = impl.mModel;
309 VisualModelPtr& visualModel = model->mVisualModel;
310 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
314 if(fabsf(width - visualModel->mControlSize.width) > Math::MACHINE_EPSILON_1000 ||
315 textUpdateInfo.mFullRelayoutNeeded ||
316 textUpdateInfo.mClearAll)
318 // Layout the text for the new width.
319 OperationsMask requestedOperationsMask = static_cast<OperationsMask>(LAYOUT);
320 Size sizeRequestedWidthAndMaxHeight = Size(width, MAX_FLOAT);
322 layoutSize = CalculateLayoutSizeOnRequiredControllerSize(controller, sizeRequestedWidthAndMaxHeight, requestedOperationsMask);
324 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height);
328 layoutSize = visualModel->GetLayoutSize();
329 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height);
332 return layoutSize.height;
335 Controller::UpdateTextType Controller::Relayouter::Relayout(Controller& controller, const Size& size, Dali::LayoutDirection::Type layoutDirection)
337 Controller::Impl& impl = *controller.mImpl;
338 ModelPtr& model = impl.mModel;
339 VisualModelPtr& visualModel = model->mVisualModel;
340 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
342 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", &controller, size.width, size.height, impl.mIsAutoScrollEnabled ? "true" : "false");
344 UpdateTextType updateTextType = NONE_UPDATED;
346 if((size.width < Math::MACHINE_EPSILON_1000) || (size.height < Math::MACHINE_EPSILON_1000))
348 if(0u != visualModel->mGlyphPositions.Count())
350 visualModel->mGlyphPositions.Clear();
351 updateTextType = MODEL_UPDATED;
354 // Clear the update info. This info will be set the next time the text is updated.
355 textUpdateInfo.Clear();
357 // Not worth to relayout if width or height is equal to zero.
358 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n");
360 return updateTextType;
363 // Whether a new size has been set.
364 const bool newSize = (size != visualModel->mControlSize);
366 // Get a reference to the pending operations member
367 OperationsMask& operationsPending = impl.mOperationsPending;
371 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", visualModel->mControlSize.width, visualModel->mControlSize.height);
373 if((0 == textUpdateInfo.mNumberOfCharactersToAdd) &&
374 (0 == textUpdateInfo.mPreviousNumberOfCharacters) &&
375 ((visualModel->mControlSize.width < Math::MACHINE_EPSILON_1000) || (visualModel->mControlSize.height < Math::MACHINE_EPSILON_1000)))
377 textUpdateInfo.mNumberOfCharactersToAdd = model->mLogicalModel->mText.Count();
380 // Layout operations that need to be done if the size changes.
381 operationsPending = static_cast<OperationsMask>(operationsPending |
386 // Set the update info to relayout the whole text.
387 textUpdateInfo.mFullRelayoutNeeded = true;
388 textUpdateInfo.mCharacterIndex = 0u;
390 // Store the size used to layout the text.
391 visualModel->mControlSize = size;
394 // Whether there are modify events.
395 if(0u != impl.mModifyEvents.Count())
397 // Style operations that need to be done if the text is modified.
398 operationsPending = static_cast<OperationsMask>(operationsPending | COLOR);
401 // Set the update info to elide the text.
402 if(model->mElideEnabled ||
403 ((NULL != impl.mEventData) && impl.mEventData->mIsPlaceholderElideEnabled))
405 // Update Text layout for applying elided
406 operationsPending = static_cast<OperationsMask>(operationsPending |
411 textUpdateInfo.mFullRelayoutNeeded = true;
412 textUpdateInfo.mCharacterIndex = 0u;
415 bool layoutDirectionChanged = false;
416 if(impl.mLayoutDirection != layoutDirection)
418 // Flag to indicate that the layout direction has changed.
419 layoutDirectionChanged = true;
420 // Clear the update info. This info will be set the next time the text is updated.
421 textUpdateInfo.mClearAll = true;
422 // Apply modifications to the model
423 // Shape the text again is needed because characters like '()[]{}' have to be mirrored and the glyphs generated again.
424 operationsPending = static_cast<OperationsMask>(operationsPending |
432 impl.mLayoutDirection = layoutDirection;
435 // Make sure the model is up-to-date before layouting.
436 EventHandler::ProcessModifyEvents(controller);
437 bool updated = impl.UpdateModel(operationsPending);
441 updated = DoRelayout(impl, size, operationsPending, layoutSize) || updated;
445 updateTextType = MODEL_UPDATED;
448 // Do not re-do any operation until something changes.
449 operationsPending = NO_OPERATION;
450 model->mScrollPositionLast = model->mScrollPosition;
452 // Whether the text control is editable
453 const bool isEditable = NULL != impl.mEventData;
455 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
457 if(newSize && isEditable)
459 offset = model->mScrollPosition;
462 if(!isEditable || !controller.IsMultiLineEnabled())
464 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
465 CalculateVerticalOffset(impl, size);
469 // If layoutSize is bigger than size, vertical align has no meaning.
470 if(layoutSize.y < size.y)
472 CalculateVerticalOffset(impl, size);
475 impl.mEventData->mScrollAfterDelete = false;
482 if(newSize || layoutDirectionChanged)
484 // If there is a new size or layout direction is changed, the scroll position needs to be clamped.
485 impl.ClampHorizontalScroll(layoutSize);
487 // Update the decorator's positions is needed if there is a new size.
488 impl.mEventData->mDecorator->UpdatePositions(model->mScrollPosition - offset);
490 // All decorator elements need to be updated.
491 if(EventData::IsEditingState(impl.mEventData->mState))
493 impl.mEventData->mScrollAfterUpdatePosition = true;
494 impl.mEventData->mUpdateCursorPosition = true;
495 impl.mEventData->mUpdateGrabHandlePosition = true;
497 else if(impl.mEventData->mState == EventData::SELECTING)
499 impl.mEventData->mUpdateHighlightBox = true;
503 // Move the cursor, grab handle etc.
504 if(impl.ProcessInputEvents())
506 updateTextType = static_cast<UpdateTextType>(updateTextType | DECORATOR_UPDATED);
510 // Clear the update info. This info will be set the next time the text is updated.
511 textUpdateInfo.Clear();
512 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout\n");
514 return updateTextType;
517 bool Controller::Relayouter::DoRelayout(Controller::Impl& impl, const Size& size, OperationsMask operationsRequired, Size& layoutSize)
519 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::Relayouter::DoRelayout %p size %f,%f\n", &impl, size.width, size.height);
520 bool viewUpdated(false);
522 // Calculate the operations to be done.
523 const OperationsMask operations = static_cast<OperationsMask>(impl.mOperationsPending & operationsRequired);
525 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
526 const CharacterIndex startIndex = textUpdateInfo.mParagraphCharacterIndex;
527 const Length requestedNumberOfCharacters = textUpdateInfo.mRequestedNumberOfCharacters;
529 // Get the current layout size.
530 VisualModelPtr& visualModel = impl.mModel->mVisualModel;
531 layoutSize = visualModel->GetLayoutSize();
533 if(NO_OPERATION != (LAYOUT & operations))
535 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
537 // Some vectors with data needed to layout and reorder may be void
538 // after the first time the text has been laid out.
539 // Fill the vectors again.
541 // Calculate the number of glyphs to layout.
542 const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
543 const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
544 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
545 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
547 const CharacterIndex lastIndex = startIndex + ((requestedNumberOfCharacters > 0u) ? requestedNumberOfCharacters - 1u : 0u);
548 const GlyphIndex startGlyphIndex = textUpdateInfo.mStartGlyphIndex;
550 // Make sure the index is not out of bound
551 if(charactersToGlyph.Count() != glyphsPerCharacter.Count() ||
552 requestedNumberOfCharacters > charactersToGlyph.Count() ||
553 (lastIndex > charactersToGlyph.Count() && charactersToGlyph.Count() > 0u))
555 std::string currentText;
556 impl.GetText(currentText);
558 DALI_LOG_ERROR("Controller::DoRelayout: Attempting to access invalid buffer\n");
559 DALI_LOG_ERROR("Current text is: %s\n", currentText.c_str());
560 DALI_LOG_ERROR("startIndex: %u, lastIndex: %u, requestedNumberOfCharacters: %u, charactersToGlyph.Count = %lu, glyphsPerCharacter.Count = %lu\n", startIndex, lastIndex, requestedNumberOfCharacters, charactersToGlyph.Count(), glyphsPerCharacter.Count());
565 const Length numberOfGlyphs = (requestedNumberOfCharacters > 0u) ? *(charactersToGlyphBuffer + lastIndex) + *(glyphsPerCharacterBuffer + lastIndex) - startGlyphIndex : 0u;
566 const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
568 if(0u == totalNumberOfGlyphs)
570 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
572 visualModel->SetLayoutSize(Size::ZERO);
575 // Nothing else to do if there is no glyphs.
576 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n");
580 // Set the layout parameters.
581 Layout::Parameters layoutParameters(size, impl.mModel);
583 // Resize the vector of positions to have the same size than the vector of glyphs.
584 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
585 glyphPositions.Resize(totalNumberOfGlyphs);
587 // Whether the last character is a new paragraph character.
588 const Character* const textBuffer = impl.mModel->mLogicalModel->mText.Begin();
589 textUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph(*(textBuffer + (impl.mModel->mLogicalModel->mText.Count() - 1u)));
590 layoutParameters.isLastNewParagraph = textUpdateInfo.mIsLastCharacterNewParagraph;
592 // The initial glyph and the number of glyphs to layout.
593 layoutParameters.startGlyphIndex = startGlyphIndex;
594 layoutParameters.numberOfGlyphs = numberOfGlyphs;
595 layoutParameters.startLineIndex = textUpdateInfo.mStartLineIndex;
596 layoutParameters.estimatedNumberOfLines = textUpdateInfo.mEstimatedNumberOfLines;
598 // Update the ellipsis
599 bool elideTextEnabled = impl.mModel->mElideEnabled;
600 auto ellipsisPosition = impl.mModel->mEllipsisPosition;
602 if(NULL != impl.mEventData)
604 if(impl.mEventData->mPlaceholderEllipsisFlag && impl.IsShowingPlaceholderText())
606 elideTextEnabled = impl.mEventData->mIsPlaceholderElideEnabled;
608 else if(EventData::INACTIVE != impl.mEventData->mState)
610 // Disable ellipsis when editing
611 elideTextEnabled = false;
614 // Reset the scroll position in inactive state
615 if(elideTextEnabled && (impl.mEventData->mState == EventData::INACTIVE))
617 impl.ResetScrollPosition();
621 // Update the visual model.
622 bool isAutoScrollEnabled = impl.mIsAutoScrollEnabled;
624 viewUpdated = impl.mLayoutEngine.LayoutText(layoutParameters,
629 impl.mIsAutoScrollEnabled = isAutoScrollEnabled;
631 viewUpdated = viewUpdated || (newLayoutSize != layoutSize);
635 layoutSize = newLayoutSize;
637 if(NO_OPERATION != (UPDATE_DIRECTION & operations))
639 impl.mIsTextDirectionRTL = false;
642 if((NO_OPERATION != (UPDATE_DIRECTION & operations)) && !visualModel->mLines.Empty())
644 impl.mIsTextDirectionRTL = visualModel->mLines[0u].direction;
647 // Sets the layout size.
648 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
650 visualModel->SetLayoutSize(layoutSize);
655 if(NO_OPERATION != (ALIGN & operations))
657 DoRelayoutHorizontalAlignment(impl, size, startIndex, requestedNumberOfCharacters);
660 #if defined(DEBUG_ENABLED)
661 std::string currentText;
662 impl.GetText(currentText);
663 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Controller::Relayouter::DoRelayout [%p] mImpl->mIsTextDirectionRTL[%s] [%s]\n", &impl, (impl.mIsTextDirectionRTL) ? "true" : "false", currentText.c_str());
665 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayouter::DoRelayout, view updated %s\n", (viewUpdated ? "true" : "false"));
669 void Controller::Relayouter::DoRelayoutHorizontalAlignment(Controller::Impl& impl,
671 const CharacterIndex startIndex,
672 const Length requestedNumberOfCharacters)
675 VisualModelPtr& visualModel = impl.mModel->mVisualModel;
677 // The laid-out lines.
678 Vector<LineRun>& lines = visualModel->mLines;
680 CharacterIndex alignStartIndex = startIndex;
681 Length alignRequestedNumberOfCharacters = requestedNumberOfCharacters;
683 // the whole text needs to be full aligned.
684 // If you do not do a full aligned, only the last line of the multiline input is aligned.
685 if(impl.mEventData && impl.mEventData->mUpdateAlignment)
687 alignStartIndex = 0u;
688 alignRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
689 impl.mEventData->mUpdateAlignment = false;
692 // If there is no BoundedParagraphRuns then apply the alignment of controller.
693 // Check whether the layout is single line. It's needed to apply one alignment for single-line.
694 // In single-line layout case we need to check whether to follow the alignment of controller or the first BoundedParagraph.
695 // Apply BoundedParagraph's alignment if and only if there is one BoundedParagraph contains all characters. Otherwise follow controller's alignment.
696 const bool isFollowControllerAlignment = ((impl.mModel->GetNumberOfBoundedParagraphRuns() == 0u) ||
697 ((Layout::Engine::SINGLE_LINE_BOX == impl.mLayoutEngine.GetLayout()) &&
698 (impl.mModel->GetBoundedParagraphRuns()[0].characterRun.numberOfCharacters != impl.mModel->mLogicalModel->mText.Count())));
700 if(isFollowControllerAlignment)
702 // Need to align with the control's size as the text may contain lines
703 // starting either with left to right text or right to left.
704 impl.mLayoutEngine.Align(size,
706 alignRequestedNumberOfCharacters,
707 impl.mModel->mHorizontalAlignment,
709 impl.mModel->mAlignmentOffset,
710 impl.mLayoutDirection,
711 (impl.mModel->mMatchLayoutDirection != DevelText::MatchLayoutDirection::CONTENTS));
715 //Override the controller horizontal-alignment by horizontal-alignment of bounded paragraph.
716 const Length& numberOfBoundedParagraphRuns = impl.mModel->GetNumberOfBoundedParagraphRuns();
717 const Vector<BoundedParagraphRun>& boundedParagraphRuns = impl.mModel->GetBoundedParagraphRuns();
718 const CharacterIndex alignEndIndex = alignStartIndex + alignRequestedNumberOfCharacters - 1u;
720 Length alignIndex = alignStartIndex;
721 Length boundedParagraphRunIndex = 0u;
723 while(alignIndex <= alignEndIndex && boundedParagraphRunIndex < numberOfBoundedParagraphRuns)
725 //BP: BoundedParagraph
726 const BoundedParagraphRun& boundedParagraphRun = boundedParagraphRuns[boundedParagraphRunIndex];
727 const CharacterIndex& characterStartIndexBP = boundedParagraphRun.characterRun.characterIndex;
728 const Length& numberOfCharactersBP = boundedParagraphRun.characterRun.numberOfCharacters;
729 const CharacterIndex characterEndIndexBP = characterStartIndexBP + numberOfCharactersBP - 1u;
731 CharacterIndex decidedAlignStartIndex = alignIndex;
732 Length decidedAlignNumberOfCharacters = alignEndIndex - alignIndex + 1u;
733 Text::HorizontalAlignment::Type decidedHorizontalAlignment = impl.mModel->mHorizontalAlignment;
736 * Shortcuts to explain indexes cases:
738 * AS: Alignment Start Index
739 * AE: Alignment End Index
740 * PS: Paragraph Start Index
741 * PE: Paragraph End Index
742 * B: BoundedParagraph Alignment
747 if(alignIndex < characterStartIndexBP && characterStartIndexBP <= alignEndIndex) /// AS.MMMMMM.PS--------AE
749 // Alignment from "Alignment Start Index" to index before "Paragraph Start Index" according to "Model Alignment"
750 decidedAlignStartIndex = alignIndex;
751 decidedAlignNumberOfCharacters = characterStartIndexBP - alignIndex;
752 decidedHorizontalAlignment = impl.mModel->mHorizontalAlignment;
754 // Need to re-heck the case of current bounded paragraph
755 alignIndex = characterStartIndexBP; // Shift AS to be PS
757 else if((characterStartIndexBP <= alignIndex && alignIndex <= characterEndIndexBP) || /// ---PS.BBBBBBB.AS.BBBBBBB.PE---
758 (characterStartIndexBP <= alignEndIndex && alignEndIndex <= characterEndIndexBP)) /// ---PS.BBBBBB.AE.BBBBBBB.PE---
760 // Alignment from "Paragraph Start Index" to "Paragraph End Index" according to "BoundedParagraph Alignment"
761 decidedAlignStartIndex = characterStartIndexBP;
762 decidedAlignNumberOfCharacters = numberOfCharactersBP;
763 decidedHorizontalAlignment = boundedParagraphRun.horizontalAlignmentDefined ? boundedParagraphRun.horizontalAlignment : impl.mModel->mHorizontalAlignment;
765 alignIndex = characterEndIndexBP + 1u; // Shift AS to be after PE direct
766 boundedParagraphRunIndex++; // Align then check the case of next bounded paragraph
770 boundedParagraphRunIndex++; // Check the case of next bounded paragraph
774 impl.mLayoutEngine.Align(size,
775 decidedAlignStartIndex,
776 decidedAlignNumberOfCharacters,
777 decidedHorizontalAlignment,
779 impl.mModel->mAlignmentOffset,
780 impl.mLayoutDirection,
781 (impl.mModel->mMatchLayoutDirection != DevelText::MatchLayoutDirection::CONTENTS));
784 //Align the remaining that is not aligned
785 if(alignIndex <= alignEndIndex)
787 impl.mLayoutEngine.Align(size,
789 (alignEndIndex - alignIndex + 1u),
790 impl.mModel->mHorizontalAlignment,
792 impl.mModel->mAlignmentOffset,
793 impl.mLayoutDirection,
794 (impl.mModel->mMatchLayoutDirection != DevelText::MatchLayoutDirection::CONTENTS));
799 void Controller::Relayouter::CalculateVerticalOffset(Controller::Impl& impl, const Size& controlSize)
801 ModelPtr& model = impl.mModel;
802 VisualModelPtr& visualModel = model->mVisualModel;
803 Size layoutSize = model->mVisualModel->GetLayoutSize();
804 Size oldLayoutSize = layoutSize;
806 bool needRecalc = false;
807 float defaultFontLineHeight = impl.GetDefaultFontLineHeight();
809 if(fabsf(layoutSize.height) < Math::MACHINE_EPSILON_1000)
811 // Get the line height of the default font.
812 layoutSize.height = defaultFontLineHeight;
815 // Whether the text control is editable
816 const bool isEditable = NULL != impl.mEventData;
817 if(isEditable && layoutSize.height != defaultFontLineHeight && impl.IsShowingPlaceholderText())
819 // This code prevents the wrong positioning of cursor when the layout size is bigger/smaller than defaultFontLineHeight.
820 // This situation occurs when the size of placeholder text is different from the default text.
821 layoutSize.height = defaultFontLineHeight;
825 switch(model->mVerticalAlignment)
827 case VerticalAlignment::TOP:
829 model->mScrollPosition.y = 0.f;
833 case VerticalAlignment::CENTER:
835 model->mScrollPosition.y = floorf(0.5f * (controlSize.height - layoutSize.height)); // try to avoid pixel alignment.
836 if(needRecalc) offsetY = floorf(0.5f * (layoutSize.height - oldLayoutSize.height));
839 case VerticalAlignment::BOTTOM:
841 model->mScrollPosition.y = controlSize.height - layoutSize.height;
842 if(needRecalc) offsetY = layoutSize.height - oldLayoutSize.height;
849 // Update glyphPositions according to recalculation.
850 const Length positionCount = visualModel->mGlyphPositions.Count();
851 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
852 for(Length index = 0u; index < positionCount; index++)
854 glyphPositions[index].y += offsetY;
861 } // namespace Toolkit