2 * Copyright (c) 2021 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-impl.h>
31 #if defined(DEBUG_ENABLED)
32 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
35 constexpr float MAX_FLOAT = std::numeric_limits<float>::max();
37 float ConvertToEven(float value)
39 int intValue(static_cast<int>(value));
40 return static_cast<float>(intValue + (intValue & 1));
51 Size Controller::Relayouter::CalculateLayoutSizeOnRequiredControllerSize(Controller& controller, const Size& requestedControllerSize, const OperationsMask& requestedOperationsMask, bool restoreLinesAndGlyphPositions)
53 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->CalculateLayoutSizeOnRequiredControllerSize\n");
54 Size calculatedLayoutSize;
56 Controller::Impl& impl = *controller.mImpl;
57 ModelPtr& model = impl.mModel;
58 VisualModelPtr& visualModel = model->mVisualModel;
60 // Store the pending operations mask so that it can be restored later on with no modifications made on it
61 // while getting the natural size were reflected on the original mask.
62 OperationsMask operationsPendingBackUp = static_cast<OperationsMask>(impl.mOperationsPending);
64 // This is a hotfix for side effect on Scrolling, LineWrap and Invalid position of cursor in TextEditor after calling CalculateLayoutSizeOnRequiredControllerSize.
65 // The number of lines and glyph-positions inside visualModel have been changed by calling DoRelayout with requestedControllerSize.
66 // Store the mLines and mGlyphPositions from visualModel so that they can be restored later on with no modifications made on them.
67 //TODO: Refactor "DoRelayout" and extract common code of size calculation without modifying attributes of mVisualModel, and then blah, blah, etc.
68 Vector<LineRun> linesBackup = visualModel->mLines;
69 Vector<Vector2> glyphPositionsBackup = visualModel->mGlyphPositions;
71 // Operations that can be done only once until the text changes.
72 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>(CONVERT_TO_UTF32 |
80 // Set the update info to relayout the whole text.
81 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
82 if((0 == textUpdateInfo.mNumberOfCharactersToAdd) &&
83 (0 == textUpdateInfo.mPreviousNumberOfCharacters) &&
84 ((visualModel->mControlSize.width < Math::MACHINE_EPSILON_1000) || (visualModel->mControlSize.height < Math::MACHINE_EPSILON_1000)))
86 textUpdateInfo.mNumberOfCharactersToAdd = model->mLogicalModel->mText.Count();
88 textUpdateInfo.mParagraphCharacterIndex = 0u;
89 textUpdateInfo.mRequestedNumberOfCharacters = model->mLogicalModel->mText.Count();
91 // Make sure the model is up-to-date before layouting
92 impl.UpdateModel(onlyOnceOperations);
94 // Get a reference to the pending operations member
95 OperationsMask& operationsPending = impl.mOperationsPending;
97 // Layout the text for the new width.
98 operationsPending = static_cast<OperationsMask>(operationsPending | requestedOperationsMask);
100 // Store the actual control's size to restore later.
101 const Size actualControlSize = visualModel->mControlSize;
103 DoRelayout(controller,
104 requestedControllerSize,
105 static_cast<OperationsMask>(onlyOnceOperations |
106 requestedOperationsMask),
107 calculatedLayoutSize);
109 // Clear the update info. This info will be set the next time the text is updated.
110 textUpdateInfo.Clear();
111 textUpdateInfo.mClearAll = true;
113 // Restore the actual control's size.
114 visualModel->mControlSize = actualControlSize;
115 // Restore the previously backed-up pending operations' mask without the only once operations.
116 impl.mOperationsPending = static_cast<OperationsMask>(operationsPendingBackUp & ~onlyOnceOperations);
118 // Restore the previously backed-up mLines and mGlyphPositions from visualModel.
119 if(restoreLinesAndGlyphPositions)
121 visualModel->mLines = linesBackup;
122 visualModel->mGlyphPositions = glyphPositionsBackup;
125 return calculatedLayoutSize;
128 Vector3 Controller::Relayouter::GetNaturalSize(Controller& controller)
130 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n");
131 Vector3 naturalSizeVec3;
133 // Make sure the model is up-to-date before layouting
134 controller.ProcessModifyEvents();
136 Controller::Impl& impl = *controller.mImpl;
137 ModelPtr& model = impl.mModel;
138 VisualModelPtr& visualModel = model->mVisualModel;
140 if(impl.mRecalculateNaturalSize)
144 // Layout the text for the new width.
145 OperationsMask requestedOperationsMask = static_cast<OperationsMask>(LAYOUT | REORDER);
146 Size sizeMaxWidthAndMaxHeight = Size(MAX_FLOAT, MAX_FLOAT);
148 naturalSize = CalculateLayoutSizeOnRequiredControllerSize(controller, sizeMaxWidthAndMaxHeight, requestedOperationsMask, true);
150 // Stores the natural size to avoid recalculate it again
151 // unless the text/style changes.
152 visualModel->SetNaturalSize(naturalSize);
153 naturalSizeVec3 = naturalSize;
155 impl.mRecalculateNaturalSize = false;
157 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSizeVec3.x, naturalSizeVec3.y, naturalSizeVec3.z);
161 naturalSizeVec3 = visualModel->GetNaturalSize();
163 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSizeVec3.x, naturalSizeVec3.y, naturalSizeVec3.z);
166 naturalSizeVec3.x = ConvertToEven(naturalSizeVec3.x);
167 naturalSizeVec3.y = ConvertToEven(naturalSizeVec3.y);
169 return naturalSizeVec3;
172 bool Controller::Relayouter::CheckForTextFit(Controller& controller, float pointSize, const Size& layoutSize)
175 Controller::Impl& impl = *controller.mImpl;
176 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
177 impl.mFontDefaults->mFitPointSize = pointSize;
178 impl.mFontDefaults->sizeDefined = true;
179 impl.ClearFontData();
181 // Operations that can be done only once until the text changes.
182 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>(CONVERT_TO_UTF32 |
190 textUpdateInfo.mParagraphCharacterIndex = 0u;
191 textUpdateInfo.mRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
193 // Make sure the model is up-to-date before layouting
194 impl.UpdateModel(onlyOnceOperations);
196 DoRelayout(controller,
197 Size(layoutSize.width, MAX_FLOAT),
198 static_cast<OperationsMask>(onlyOnceOperations | LAYOUT),
201 // Clear the update info. This info will be set the next time the text is updated.
202 textUpdateInfo.Clear();
203 textUpdateInfo.mClearAll = true;
205 if(textSize.width > layoutSize.width || textSize.height > layoutSize.height)
212 void Controller::Relayouter::FitPointSizeforLayout(Controller& controller, const Size& layoutSize)
214 Controller::Impl& impl = *controller.mImpl;
216 const OperationsMask operations = impl.mOperationsPending;
217 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations) || impl.mTextFitContentSize != layoutSize)
219 ModelPtr& model = impl.mModel;
221 bool actualellipsis = model->mElideEnabled;
222 float minPointSize = impl.mTextFitMinSize;
223 float maxPointSize = impl.mTextFitMaxSize;
224 float pointInterval = impl.mTextFitStepSize;
225 float currentFitPointSize = impl.mFontDefaults->mFitPointSize;
227 model->mElideEnabled = false;
228 Vector<float> pointSizeArray;
231 if(pointInterval < 1.f)
233 impl.mTextFitStepSize = pointInterval = 1.0f;
236 pointSizeArray.Reserve(static_cast<unsigned int>(ceil((maxPointSize - minPointSize) / pointInterval)));
238 for(float i = minPointSize; i < maxPointSize; i += pointInterval)
240 pointSizeArray.PushBack(i);
243 pointSizeArray.PushBack(maxPointSize);
245 int bestSizeIndex = 0;
246 int min = bestSizeIndex + 1;
247 int max = pointSizeArray.Size() - 1;
250 int destI = (min + max) / 2;
252 if(CheckForTextFit(controller, pointSizeArray[destI], layoutSize))
264 model->mElideEnabled = actualellipsis;
265 if(currentFitPointSize != pointSizeArray[bestSizeIndex])
267 impl.mTextFitChanged = true;
269 impl.mFontDefaults->mFitPointSize = pointSizeArray[bestSizeIndex];
270 impl.mFontDefaults->sizeDefined = true;
271 impl.ClearFontData();
275 float Controller::Relayouter::GetHeightForWidth(Controller& controller, float width)
277 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", &controller, width);
279 // Make sure the model is up-to-date before layouting
280 controller.ProcessModifyEvents();
282 Controller::Impl& impl = *controller.mImpl;
283 ModelPtr& model = impl.mModel;
284 VisualModelPtr& visualModel = model->mVisualModel;
285 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
289 if(fabsf(width - visualModel->mControlSize.width) > Math::MACHINE_EPSILON_1000 ||
290 textUpdateInfo.mFullRelayoutNeeded ||
291 textUpdateInfo.mClearAll)
293 // Layout the text for the new width.
294 OperationsMask requestedOperationsMask = static_cast<OperationsMask>(LAYOUT);
295 Size sizeRequestedWidthAndMaxHeight = Size(width, MAX_FLOAT);
297 // Skip restore, because if GetHeightForWidth called before rendering and layouting then visualModel->mControlSize will be zero which will make LineCount zero.
298 // The implementation of Get LineCount property depends on calling GetHeightForWidth then read mLines.Count() from visualModel direct.
299 // If the LineCount property is requested before rendering and layouting then the value will be zero, which is incorrect.
300 // So we will not restore the previously backed-up mLines and mGlyphPositions from visualModel in such case.
301 // Another case to skip restore is when the requested width equals the Control's width which means the caller need to update the old values.
302 // For example, when the text is changed.
303 bool restoreLinesAndGlyphPositions = (visualModel->mControlSize.width > 0 && visualModel->mControlSize.height > 0) && (visualModel->mControlSize.width != width);
305 layoutSize = CalculateLayoutSizeOnRequiredControllerSize(controller, sizeRequestedWidthAndMaxHeight, requestedOperationsMask, restoreLinesAndGlyphPositions);
307 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height);
311 layoutSize = visualModel->GetLayoutSize();
312 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height);
315 return layoutSize.height;
318 Controller::UpdateTextType Controller::Relayouter::Relayout(Controller& controller, const Size& size, Dali::LayoutDirection::Type layoutDirection)
320 Controller::Impl& impl = *controller.mImpl;
321 ModelPtr& model = impl.mModel;
322 VisualModelPtr& visualModel = model->mVisualModel;
323 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
325 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", &controller, size.width, size.height, impl.mIsAutoScrollEnabled ? "true" : "false");
327 UpdateTextType updateTextType = NONE_UPDATED;
329 if((size.width < Math::MACHINE_EPSILON_1000) || (size.height < Math::MACHINE_EPSILON_1000))
331 if(0u != visualModel->mGlyphPositions.Count())
333 visualModel->mGlyphPositions.Clear();
334 updateTextType = MODEL_UPDATED;
337 // Clear the update info. This info will be set the next time the text is updated.
338 textUpdateInfo.Clear();
340 // Not worth to relayout if width or height is equal to zero.
341 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n");
343 return updateTextType;
346 // Whether a new size has been set.
347 const bool newSize = (size != visualModel->mControlSize);
349 // Get a reference to the pending operations member
350 OperationsMask& operationsPending = impl.mOperationsPending;
354 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", visualModel->mControlSize.width, visualModel->mControlSize.height);
356 if((0 == textUpdateInfo.mNumberOfCharactersToAdd) &&
357 (0 == textUpdateInfo.mPreviousNumberOfCharacters) &&
358 ((visualModel->mControlSize.width < Math::MACHINE_EPSILON_1000) || (visualModel->mControlSize.height < Math::MACHINE_EPSILON_1000)))
360 textUpdateInfo.mNumberOfCharactersToAdd = model->mLogicalModel->mText.Count();
363 // Layout operations that need to be done if the size changes.
364 operationsPending = static_cast<OperationsMask>(operationsPending |
369 // Set the update info to relayout the whole text.
370 textUpdateInfo.mFullRelayoutNeeded = true;
371 textUpdateInfo.mCharacterIndex = 0u;
373 // Store the size used to layout the text.
374 visualModel->mControlSize = size;
377 // Whether there are modify events.
378 if(0u != impl.mModifyEvents.Count())
380 // Style operations that need to be done if the text is modified.
381 operationsPending = static_cast<OperationsMask>(operationsPending | COLOR);
384 // Set the update info to elide the text.
385 if(model->mElideEnabled ||
386 ((NULL != impl.mEventData) && impl.mEventData->mIsPlaceholderElideEnabled))
388 // Update Text layout for applying elided
389 operationsPending = static_cast<OperationsMask>(operationsPending |
394 textUpdateInfo.mFullRelayoutNeeded = true;
395 textUpdateInfo.mCharacterIndex = 0u;
398 if(impl.mLayoutDirection != layoutDirection)
400 // Clear the update info. This info will be set the next time the text is updated.
401 textUpdateInfo.mClearAll = true;
402 // Apply modifications to the model
403 // Shape the text again is needed because characters like '()[]{}' have to be mirrored and the glyphs generated again.
404 operationsPending = static_cast<OperationsMask>(operationsPending |
412 impl.mLayoutDirection = layoutDirection;
415 // Make sure the model is up-to-date before layouting.
416 controller.ProcessModifyEvents();
417 bool updated = impl.UpdateModel(operationsPending);
421 updated = DoRelayout(controller, size, operationsPending, layoutSize) || updated;
425 updateTextType = MODEL_UPDATED;
428 // Do not re-do any operation until something changes.
429 operationsPending = NO_OPERATION;
430 model->mScrollPositionLast = model->mScrollPosition;
432 // Whether the text control is editable
433 const bool isEditable = NULL != impl.mEventData;
435 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
437 if(newSize && isEditable)
439 offset = model->mScrollPosition;
442 if(!isEditable || !controller.IsMultiLineEnabled())
444 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
445 controller.CalculateVerticalOffset(size);
452 // If there is a new size, the scroll position needs to be clamped.
453 impl.ClampHorizontalScroll(layoutSize);
455 // Update the decorator's positions is needed if there is a new size.
456 impl.mEventData->mDecorator->UpdatePositions(model->mScrollPosition - offset);
459 // Move the cursor, grab handle etc.
460 if(impl.ProcessInputEvents())
462 updateTextType = static_cast<UpdateTextType>(updateTextType | DECORATOR_UPDATED);
466 // Clear the update info. This info will be set the next time the text is updated.
467 textUpdateInfo.Clear();
468 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout\n");
470 return updateTextType;
473 bool Controller::Relayouter::DoRelayout(Controller& controller, const Size& size, OperationsMask operationsRequired, Size& layoutSize)
475 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", &controller, size.width, size.height);
476 bool viewUpdated(false);
478 Controller::Impl& impl = *controller.mImpl;
480 // Calculate the operations to be done.
481 const OperationsMask operations = static_cast<OperationsMask>(impl.mOperationsPending & operationsRequired);
483 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
484 const CharacterIndex startIndex = textUpdateInfo.mParagraphCharacterIndex;
485 const Length requestedNumberOfCharacters = textUpdateInfo.mRequestedNumberOfCharacters;
487 // Get the current layout size.
488 VisualModelPtr& visualModel = impl.mModel->mVisualModel;
489 layoutSize = visualModel->GetLayoutSize();
491 if(NO_OPERATION != (LAYOUT & operations))
493 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
495 // Some vectors with data needed to layout and reorder may be void
496 // after the first time the text has been laid out.
497 // Fill the vectors again.
499 // Calculate the number of glyphs to layout.
500 const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
501 const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
502 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
503 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
505 const CharacterIndex lastIndex = startIndex + ((requestedNumberOfCharacters > 0u) ? requestedNumberOfCharacters - 1u : 0u);
506 const GlyphIndex startGlyphIndex = textUpdateInfo.mStartGlyphIndex;
508 // Make sure the index is not out of bound
509 if(charactersToGlyph.Count() != glyphsPerCharacter.Count() ||
510 requestedNumberOfCharacters > charactersToGlyph.Count() ||
511 (lastIndex > charactersToGlyph.Count() && charactersToGlyph.Count() > 0u))
513 std::string currentText;
514 controller.GetText(currentText);
516 DALI_LOG_ERROR("Controller::DoRelayout: Attempting to access invalid buffer\n");
517 DALI_LOG_ERROR("Current text is: %s\n", currentText.c_str());
518 DALI_LOG_ERROR("startIndex: %u, lastIndex: %u, requestedNumberOfCharacters: %u, charactersToGlyph.Count = %lu, glyphsPerCharacter.Count = %lu\n", startIndex, lastIndex, requestedNumberOfCharacters, charactersToGlyph.Count(), glyphsPerCharacter.Count());
523 const Length numberOfGlyphs = (requestedNumberOfCharacters > 0u) ? *(charactersToGlyphBuffer + lastIndex) + *(glyphsPerCharacterBuffer + lastIndex) - startGlyphIndex : 0u;
524 const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
526 if(0u == totalNumberOfGlyphs)
528 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
530 visualModel->SetLayoutSize(Size::ZERO);
533 // Nothing else to do if there is no glyphs.
534 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n");
538 // Set the layout parameters.
539 Layout::Parameters layoutParameters(size, impl.mModel);
541 // Resize the vector of positions to have the same size than the vector of glyphs.
542 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
543 glyphPositions.Resize(totalNumberOfGlyphs);
545 // Whether the last character is a new paragraph character.
546 const Character* const textBuffer = impl.mModel->mLogicalModel->mText.Begin();
547 textUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph(*(textBuffer + (impl.mModel->mLogicalModel->mText.Count() - 1u)));
548 layoutParameters.isLastNewParagraph = textUpdateInfo.mIsLastCharacterNewParagraph;
550 // The initial glyph and the number of glyphs to layout.
551 layoutParameters.startGlyphIndex = startGlyphIndex;
552 layoutParameters.numberOfGlyphs = numberOfGlyphs;
553 layoutParameters.startLineIndex = textUpdateInfo.mStartLineIndex;
554 layoutParameters.estimatedNumberOfLines = textUpdateInfo.mEstimatedNumberOfLines;
556 // Update the ellipsis
557 bool elideTextEnabled = impl.mModel->mElideEnabled;
558 auto ellipsisPosition = impl.mModel->mEllipsisPosition;
560 if(NULL != impl.mEventData)
562 if(impl.mEventData->mPlaceholderEllipsisFlag && impl.IsShowingPlaceholderText())
564 elideTextEnabled = impl.mEventData->mIsPlaceholderElideEnabled;
566 else if(EventData::INACTIVE != impl.mEventData->mState)
568 // Disable ellipsis when editing
569 elideTextEnabled = false;
572 // Reset the scroll position in inactive state
573 if(elideTextEnabled && (impl.mEventData->mState == EventData::INACTIVE))
575 impl.ResetScrollPosition();
579 // Update the visual model.
580 bool isAutoScrollEnabled = impl.mIsAutoScrollEnabled;
582 viewUpdated = impl.mLayoutEngine.LayoutText(layoutParameters,
587 impl.mIsAutoScrollEnabled = isAutoScrollEnabled;
589 viewUpdated = viewUpdated || (newLayoutSize != layoutSize);
593 layoutSize = newLayoutSize;
595 if(NO_OPERATION != (UPDATE_DIRECTION & operations))
597 impl.mIsTextDirectionRTL = false;
600 if((NO_OPERATION != (UPDATE_DIRECTION & operations)) && !visualModel->mLines.Empty())
602 impl.mIsTextDirectionRTL = visualModel->mLines[0u].direction;
605 // Sets the layout size.
606 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
608 visualModel->SetLayoutSize(layoutSize);
613 if(NO_OPERATION != (ALIGN & operations))
615 // The laid-out lines.
616 Vector<LineRun>& lines = visualModel->mLines;
618 CharacterIndex alignStartIndex = startIndex;
619 Length alignRequestedNumberOfCharacters = requestedNumberOfCharacters;
621 // the whole text needs to be full aligned.
622 // If you do not do a full aligned, only the last line of the multiline input is aligned.
623 if(impl.mEventData && impl.mEventData->mUpdateAlignment)
625 alignStartIndex = 0u;
626 alignRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
627 impl.mEventData->mUpdateAlignment = false;
630 // Need to align with the control's size as the text may contain lines
631 // starting either with left to right text or right to left.
632 impl.mLayoutEngine.Align(size,
634 alignRequestedNumberOfCharacters,
635 impl.mModel->mHorizontalAlignment,
637 impl.mModel->mAlignmentOffset,
638 impl.mLayoutDirection,
639 (impl.mModel->mMatchLayoutDirection != DevelText::MatchLayoutDirection::CONTENTS));
643 #if defined(DEBUG_ENABLED)
644 std::string currentText;
645 controller.GetText(currentText);
646 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Controller::DoRelayout [%p] mImpl->mIsTextDirectionRTL[%s] [%s]\n", &controller, (impl.mIsTextDirectionRTL) ? "true" : "false", currentText.c_str());
648 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", (viewUpdated ? "true" : "false"));
652 void Controller::Relayouter::CalculateVerticalOffset(Controller& controller, const Size& controlSize)
654 Controller::Impl& impl = *controller.mImpl;
655 ModelPtr& model = impl.mModel;
656 VisualModelPtr& visualModel = model->mVisualModel;
657 Size layoutSize = model->mVisualModel->GetLayoutSize();
658 Size oldLayoutSize = layoutSize;
660 bool needRecalc = false;
661 float defaultFontLineHeight = impl.GetDefaultFontLineHeight();
663 if(fabsf(layoutSize.height) < Math::MACHINE_EPSILON_1000)
665 // Get the line height of the default font.
666 layoutSize.height = defaultFontLineHeight;
669 // Whether the text control is editable
670 const bool isEditable = NULL != impl.mEventData;
671 if(isEditable && layoutSize.height != defaultFontLineHeight && impl.IsShowingPlaceholderText())
673 // This code prevents the wrong positioning of cursor when the layout size is bigger/smaller than defaultFontLineHeight.
674 // This situation occurs when the size of placeholder text is different from the default text.
675 layoutSize.height = defaultFontLineHeight;
679 switch(model->mVerticalAlignment)
681 case VerticalAlignment::TOP:
683 model->mScrollPosition.y = 0.f;
687 case VerticalAlignment::CENTER:
689 model->mScrollPosition.y = floorf(0.5f * (controlSize.height - layoutSize.height)); // try to avoid pixel alignment.
690 if(needRecalc) offsetY = floorf(0.5f * (layoutSize.height - oldLayoutSize.height));
693 case VerticalAlignment::BOTTOM:
695 model->mScrollPosition.y = controlSize.height - layoutSize.height;
696 if(needRecalc) offsetY = layoutSize.height - oldLayoutSize.height;
703 // Update glyphPositions according to recalculation.
704 const Length positionCount = visualModel->mGlyphPositions.Count();
705 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
706 for(Length index = 0u; index < positionCount; index++)
708 glyphPositions[index].y += offsetY;
715 } // namespace Toolkit