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 textUpdateInfo.mParagraphCharacterIndex = 0u;
83 textUpdateInfo.mRequestedNumberOfCharacters = model->mLogicalModel->mText.Count();
85 // Make sure the model is up-to-date before layouting
86 impl.UpdateModel(onlyOnceOperations);
88 // Get a reference to the pending operations member
89 OperationsMask& operationsPending = impl.mOperationsPending;
91 // Layout the text for the new width.
92 operationsPending = static_cast<OperationsMask>(operationsPending | requestedOperationsMask);
94 // Store the actual control's size to restore later.
95 const Size actualControlSize = visualModel->mControlSize;
97 DoRelayout(controller,
98 requestedControllerSize,
99 static_cast<OperationsMask>(onlyOnceOperations |
100 requestedOperationsMask),
101 calculatedLayoutSize);
103 // Clear the update info. This info will be set the next time the text is updated.
104 textUpdateInfo.Clear();
105 textUpdateInfo.mClearAll = true;
107 // Restore the actual control's size.
108 visualModel->mControlSize = actualControlSize;
109 // Restore the previously backed-up pending operations' mask without the only once operations.
110 impl.mOperationsPending = static_cast<OperationsMask>(operationsPendingBackUp & ~onlyOnceOperations);
112 // Restore the previously backed-up mLines and mGlyphPositions from visualModel.
113 if(restoreLinesAndGlyphPositions)
115 visualModel->mLines = linesBackup;
116 visualModel->mGlyphPositions = glyphPositionsBackup;
119 return calculatedLayoutSize;
122 Vector3 Controller::Relayouter::GetNaturalSize(Controller& controller)
124 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n");
125 Vector3 naturalSizeVec3;
127 // Make sure the model is up-to-date before layouting
128 controller.ProcessModifyEvents();
130 Controller::Impl& impl = *controller.mImpl;
131 ModelPtr& model = impl.mModel;
132 VisualModelPtr& visualModel = model->mVisualModel;
134 if(impl.mRecalculateNaturalSize)
138 // Layout the text for the new width.
139 OperationsMask requestedOperationsMask = static_cast<OperationsMask>(LAYOUT | REORDER);
140 Size sizeMaxWidthAndMaxHeight = Size(MAX_FLOAT, MAX_FLOAT);
142 naturalSize = CalculateLayoutSizeOnRequiredControllerSize(controller, sizeMaxWidthAndMaxHeight, requestedOperationsMask, true);
144 // Stores the natural size to avoid recalculate it again
145 // unless the text/style changes.
146 visualModel->SetNaturalSize(naturalSize);
147 naturalSizeVec3 = naturalSize;
149 impl.mRecalculateNaturalSize = false;
151 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSizeVec3.x, naturalSizeVec3.y, naturalSizeVec3.z);
155 naturalSizeVec3 = visualModel->GetNaturalSize();
157 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSizeVec3.x, naturalSizeVec3.y, naturalSizeVec3.z);
160 naturalSizeVec3.x = ConvertToEven(naturalSizeVec3.x);
161 naturalSizeVec3.y = ConvertToEven(naturalSizeVec3.y);
163 return naturalSizeVec3;
166 bool Controller::Relayouter::CheckForTextFit(Controller& controller, float pointSize, const Size& layoutSize)
169 Controller::Impl& impl = *controller.mImpl;
170 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
171 impl.mFontDefaults->mFitPointSize = pointSize;
172 impl.mFontDefaults->sizeDefined = true;
173 controller.ClearFontData();
175 // Operations that can be done only once until the text changes.
176 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>(CONVERT_TO_UTF32 |
184 textUpdateInfo.mParagraphCharacterIndex = 0u;
185 textUpdateInfo.mRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
187 // Make sure the model is up-to-date before layouting
188 impl.UpdateModel(onlyOnceOperations);
190 DoRelayout(controller,
191 Size(layoutSize.width, MAX_FLOAT),
192 static_cast<OperationsMask>(onlyOnceOperations | LAYOUT),
195 // Clear the update info. This info will be set the next time the text is updated.
196 textUpdateInfo.Clear();
197 textUpdateInfo.mClearAll = true;
199 if(textSize.width > layoutSize.width || textSize.height > layoutSize.height)
206 void Controller::Relayouter::FitPointSizeforLayout(Controller& controller, const Size& layoutSize)
208 Controller::Impl& impl = *controller.mImpl;
210 const OperationsMask operations = impl.mOperationsPending;
211 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations) || impl.mTextFitContentSize != layoutSize)
213 ModelPtr& model = impl.mModel;
215 bool actualellipsis = model->mElideEnabled;
216 float minPointSize = impl.mTextFitMinSize;
217 float maxPointSize = impl.mTextFitMaxSize;
218 float pointInterval = impl.mTextFitStepSize;
220 model->mElideEnabled = false;
221 Vector<float> pointSizeArray;
224 if(pointInterval < 1.f)
226 impl.mTextFitStepSize = pointInterval = 1.0f;
229 pointSizeArray.Reserve(static_cast<unsigned int>(ceil((maxPointSize - minPointSize) / pointInterval)));
231 for(float i = minPointSize; i < maxPointSize; i += pointInterval)
233 pointSizeArray.PushBack(i);
236 pointSizeArray.PushBack(maxPointSize);
238 int bestSizeIndex = 0;
239 int min = bestSizeIndex + 1;
240 int max = pointSizeArray.Size() - 1;
243 int destI = (min + max) / 2;
245 if(CheckForTextFit(controller, pointSizeArray[destI], layoutSize))
257 model->mElideEnabled = actualellipsis;
258 impl.mFontDefaults->mFitPointSize = pointSizeArray[bestSizeIndex];
259 impl.mFontDefaults->sizeDefined = true;
260 controller.ClearFontData();
264 float Controller::Relayouter::GetHeightForWidth(Controller& controller, float width)
266 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", &controller, width);
268 // Make sure the model is up-to-date before layouting
269 controller.ProcessModifyEvents();
271 Controller::Impl& impl = *controller.mImpl;
272 ModelPtr& model = impl.mModel;
273 VisualModelPtr& visualModel = model->mVisualModel;
274 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
278 if(fabsf(width - visualModel->mControlSize.width) > Math::MACHINE_EPSILON_1000 ||
279 textUpdateInfo.mFullRelayoutNeeded ||
280 textUpdateInfo.mClearAll)
282 // Layout the text for the new width.
283 OperationsMask requestedOperationsMask = static_cast<OperationsMask>(LAYOUT);
284 Size sizeRequestedWidthAndMaxHeight = Size(width, MAX_FLOAT);
286 // Skip restore, because if GetHeightForWidth called before rendering and layouting then visualModel->mControlSize will be zero which will make LineCount zero.
287 // The implementation of Get LineCount property depends on calling GetHeightForWidth then read mLines.Count() from visualModel direct.
288 // If the LineCount property is requested before rendering and layouting then the value will be zero, which is incorrect.
289 // So we will not restore the previously backed-up mLines and mGlyphPositions from visualModel in such case.
290 // 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.
291 // For example, when the text is changed.
292 bool restoreLinesAndGlyphPositions = (visualModel->mControlSize.width > 0 && visualModel->mControlSize.height > 0) && (visualModel->mControlSize.width != width);
294 layoutSize = CalculateLayoutSizeOnRequiredControllerSize(controller, sizeRequestedWidthAndMaxHeight, requestedOperationsMask, restoreLinesAndGlyphPositions);
296 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height);
300 layoutSize = visualModel->GetLayoutSize();
301 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height);
304 return layoutSize.height;
307 Controller::UpdateTextType Controller::Relayouter::Relayout(Controller& controller, const Size& size, Dali::LayoutDirection::Type layoutDirection)
309 Controller::Impl& impl = *controller.mImpl;
310 ModelPtr& model = impl.mModel;
311 VisualModelPtr& visualModel = model->mVisualModel;
312 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
314 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", &controller, size.width, size.height, impl.mIsAutoScrollEnabled ? "true" : "false");
316 UpdateTextType updateTextType = NONE_UPDATED;
318 if((size.width < Math::MACHINE_EPSILON_1000) || (size.height < Math::MACHINE_EPSILON_1000))
320 if(0u != visualModel->mGlyphPositions.Count())
322 visualModel->mGlyphPositions.Clear();
323 updateTextType = MODEL_UPDATED;
326 // Clear the update info. This info will be set the next time the text is updated.
327 textUpdateInfo.Clear();
329 // Not worth to relayout if width or height is equal to zero.
330 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n");
332 return updateTextType;
335 // Whether a new size has been set.
336 const bool newSize = (size != visualModel->mControlSize);
338 // Get a reference to the pending operations member
339 OperationsMask& operationsPending = impl.mOperationsPending;
343 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", visualModel->mControlSize.width, visualModel->mControlSize.height);
345 if((0 == textUpdateInfo.mNumberOfCharactersToAdd) &&
346 (0 == textUpdateInfo.mPreviousNumberOfCharacters) &&
347 ((visualModel->mControlSize.width < Math::MACHINE_EPSILON_1000) || (visualModel->mControlSize.height < Math::MACHINE_EPSILON_1000)))
349 textUpdateInfo.mNumberOfCharactersToAdd = model->mLogicalModel->mText.Count();
352 // Layout operations that need to be done if the size changes.
353 operationsPending = static_cast<OperationsMask>(operationsPending |
358 // Set the update info to relayout the whole text.
359 textUpdateInfo.mFullRelayoutNeeded = true;
360 textUpdateInfo.mCharacterIndex = 0u;
362 // Store the size used to layout the text.
363 visualModel->mControlSize = size;
366 // Whether there are modify events.
367 if(0u != impl.mModifyEvents.Count())
369 // Style operations that need to be done if the text is modified.
370 operationsPending = static_cast<OperationsMask>(operationsPending | COLOR);
373 // Set the update info to elide the text.
374 if(model->mElideEnabled ||
375 ((NULL != impl.mEventData) && impl.mEventData->mIsPlaceholderElideEnabled))
377 // Update Text layout for applying elided
378 operationsPending = static_cast<OperationsMask>(operationsPending |
383 textUpdateInfo.mFullRelayoutNeeded = true;
384 textUpdateInfo.mCharacterIndex = 0u;
387 if(model->mMatchSystemLanguageDirection && impl.mLayoutDirection != layoutDirection)
389 // Clear the update info. This info will be set the next time the text is updated.
390 textUpdateInfo.mClearAll = true;
391 // Apply modifications to the model
392 // Shape the text again is needed because characters like '()[]{}' have to be mirrored and the glyphs generated again.
393 operationsPending = static_cast<OperationsMask>(operationsPending |
400 impl.mLayoutDirection = layoutDirection;
403 // Make sure the model is up-to-date before layouting.
404 controller.ProcessModifyEvents();
405 bool updated = impl.UpdateModel(operationsPending);
409 updated = DoRelayout(controller, size, operationsPending, layoutSize) || updated;
413 updateTextType = MODEL_UPDATED;
416 // Do not re-do any operation until something changes.
417 operationsPending = NO_OPERATION;
418 model->mScrollPositionLast = model->mScrollPosition;
420 // Whether the text control is editable
421 const bool isEditable = NULL != impl.mEventData;
423 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
425 if(newSize && isEditable)
427 offset = model->mScrollPosition;
430 if(!isEditable || !controller.IsMultiLineEnabled())
432 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
433 controller.CalculateVerticalOffset(size);
440 // If there is a new size, the scroll position needs to be clamped.
441 impl.ClampHorizontalScroll(layoutSize);
443 // Update the decorator's positions is needed if there is a new size.
444 impl.mEventData->mDecorator->UpdatePositions(model->mScrollPosition - offset);
447 // Move the cursor, grab handle etc.
448 if(impl.ProcessInputEvents())
450 updateTextType = static_cast<UpdateTextType>(updateTextType | DECORATOR_UPDATED);
454 // Clear the update info. This info will be set the next time the text is updated.
455 textUpdateInfo.Clear();
456 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout\n");
458 return updateTextType;
461 bool Controller::Relayouter::DoRelayout(Controller& controller, const Size& size, OperationsMask operationsRequired, Size& layoutSize)
463 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", &controller, size.width, size.height);
464 bool viewUpdated(false);
466 Controller::Impl& impl = *controller.mImpl;
468 // Calculate the operations to be done.
469 const OperationsMask operations = static_cast<OperationsMask>(impl.mOperationsPending & operationsRequired);
471 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
472 const CharacterIndex startIndex = textUpdateInfo.mParagraphCharacterIndex;
473 const Length requestedNumberOfCharacters = textUpdateInfo.mRequestedNumberOfCharacters;
475 // Get the current layout size.
476 VisualModelPtr& visualModel = impl.mModel->mVisualModel;
477 layoutSize = visualModel->GetLayoutSize();
479 if(NO_OPERATION != (LAYOUT & operations))
481 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
483 // Some vectors with data needed to layout and reorder may be void
484 // after the first time the text has been laid out.
485 // Fill the vectors again.
487 // Calculate the number of glyphs to layout.
488 const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
489 const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
490 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
491 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
493 const CharacterIndex lastIndex = startIndex + ((requestedNumberOfCharacters > 0u) ? requestedNumberOfCharacters - 1u : 0u);
494 const GlyphIndex startGlyphIndex = textUpdateInfo.mStartGlyphIndex;
496 // Make sure the index is not out of bound
497 if(charactersToGlyph.Count() != glyphsPerCharacter.Count() ||
498 requestedNumberOfCharacters > charactersToGlyph.Count() ||
499 (lastIndex > charactersToGlyph.Count() && charactersToGlyph.Count() > 0u))
501 std::string currentText;
502 controller.GetText(currentText);
504 DALI_LOG_ERROR("Controller::DoRelayout: Attempting to access invalid buffer\n");
505 DALI_LOG_ERROR("Current text is: %s\n", currentText.c_str());
506 DALI_LOG_ERROR("startIndex: %u, lastIndex: %u, requestedNumberOfCharacters: %u, charactersToGlyph.Count = %lu, glyphsPerCharacter.Count = %lu\n", startIndex, lastIndex, requestedNumberOfCharacters, charactersToGlyph.Count(), glyphsPerCharacter.Count());
511 const Length numberOfGlyphs = (requestedNumberOfCharacters > 0u) ? *(charactersToGlyphBuffer + lastIndex) + *(glyphsPerCharacterBuffer + lastIndex) - startGlyphIndex : 0u;
512 const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
514 if(0u == totalNumberOfGlyphs)
516 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
518 visualModel->SetLayoutSize(Size::ZERO);
521 // Nothing else to do if there is no glyphs.
522 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n");
526 // Set the layout parameters.
527 Layout::Parameters layoutParameters(size, impl.mModel);
529 // Resize the vector of positions to have the same size than the vector of glyphs.
530 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
531 glyphPositions.Resize(totalNumberOfGlyphs);
533 // Whether the last character is a new paragraph character.
534 const Character* const textBuffer = impl.mModel->mLogicalModel->mText.Begin();
535 textUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph(*(textBuffer + (impl.mModel->mLogicalModel->mText.Count() - 1u)));
536 layoutParameters.isLastNewParagraph = textUpdateInfo.mIsLastCharacterNewParagraph;
538 // The initial glyph and the number of glyphs to layout.
539 layoutParameters.startGlyphIndex = startGlyphIndex;
540 layoutParameters.numberOfGlyphs = numberOfGlyphs;
541 layoutParameters.startLineIndex = textUpdateInfo.mStartLineIndex;
542 layoutParameters.estimatedNumberOfLines = textUpdateInfo.mEstimatedNumberOfLines;
544 // Update the ellipsis
545 bool elideTextEnabled = impl.mModel->mElideEnabled;
547 if(NULL != impl.mEventData)
549 if(impl.mEventData->mPlaceholderEllipsisFlag && impl.IsShowingPlaceholderText())
551 elideTextEnabled = impl.mEventData->mIsPlaceholderElideEnabled;
553 else if(EventData::INACTIVE != impl.mEventData->mState)
555 // Disable ellipsis when editing
556 elideTextEnabled = false;
559 // Reset the scroll position in inactive state
560 if(elideTextEnabled && (impl.mEventData->mState == EventData::INACTIVE))
562 controller.ResetScrollPosition();
566 // Update the visual model.
567 bool isAutoScrollEnabled = impl.mIsAutoScrollEnabled;
569 viewUpdated = impl.mLayoutEngine.LayoutText(layoutParameters,
572 isAutoScrollEnabled);
573 impl.mIsAutoScrollEnabled = isAutoScrollEnabled;
575 viewUpdated = viewUpdated || (newLayoutSize != layoutSize);
579 layoutSize = newLayoutSize;
581 if(NO_OPERATION != (UPDATE_DIRECTION & operations))
583 impl.mIsTextDirectionRTL = false;
586 if((NO_OPERATION != (UPDATE_DIRECTION & operations)) && !visualModel->mLines.Empty())
588 impl.mIsTextDirectionRTL = visualModel->mLines[0u].direction;
591 // Sets the layout size.
592 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
594 visualModel->SetLayoutSize(layoutSize);
599 if(NO_OPERATION != (ALIGN & operations))
601 // The laid-out lines.
602 Vector<LineRun>& lines = visualModel->mLines;
604 CharacterIndex alignStartIndex = startIndex;
605 Length alignRequestedNumberOfCharacters = requestedNumberOfCharacters;
607 // the whole text needs to be full aligned.
608 // If you do not do a full aligned, only the last line of the multiline input is aligned.
609 if(impl.mEventData && impl.mEventData->mUpdateAlignment)
611 alignStartIndex = 0u;
612 alignRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
613 impl.mEventData->mUpdateAlignment = false;
616 // Need to align with the control's size as the text may contain lines
617 // starting either with left to right text or right to left.
618 impl.mLayoutEngine.Align(size,
620 alignRequestedNumberOfCharacters,
621 impl.mModel->mHorizontalAlignment,
623 impl.mModel->mAlignmentOffset,
624 impl.mLayoutDirection,
625 impl.mModel->mMatchSystemLanguageDirection);
629 #if defined(DEBUG_ENABLED)
630 std::string currentText;
631 controller.GetText(currentText);
632 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Controller::DoRelayout [%p] mImpl->mIsTextDirectionRTL[%s] [%s]\n", &controller, (impl.mIsTextDirectionRTL) ? "true" : "false", currentText.c_str());
634 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", (viewUpdated ? "true" : "false"));
638 void Controller::Relayouter::CalculateVerticalOffset(Controller& controller, const Size& controlSize)
640 Controller::Impl& impl = *controller.mImpl;
641 ModelPtr& model = impl.mModel;
642 VisualModelPtr& visualModel = model->mVisualModel;
643 Size layoutSize = model->mVisualModel->GetLayoutSize();
644 Size oldLayoutSize = layoutSize;
646 bool needRecalc = false;
647 float defaultFontLineHeight = impl.GetDefaultFontLineHeight();
649 if(fabsf(layoutSize.height) < Math::MACHINE_EPSILON_1000)
651 // Get the line height of the default font.
652 layoutSize.height = defaultFontLineHeight;
655 // Whether the text control is editable
656 const bool isEditable = NULL != impl.mEventData;
657 if(isEditable && layoutSize.height != defaultFontLineHeight && impl.IsShowingPlaceholderText())
659 // This code prevents the wrong positioning of cursor when the layout size is bigger/smaller than defaultFontLineHeight.
660 // This situation occurs when the size of placeholder text is different from the default text.
661 layoutSize.height = defaultFontLineHeight;
665 switch(model->mVerticalAlignment)
667 case VerticalAlignment::TOP:
669 model->mScrollPosition.y = 0.f;
673 case VerticalAlignment::CENTER:
675 model->mScrollPosition.y = floorf(0.5f * (controlSize.height - layoutSize.height)); // try to avoid pixel alignment.
676 if(needRecalc) offsetY = floorf(0.5f * (layoutSize.height - oldLayoutSize.height));
679 case VerticalAlignment::BOTTOM:
681 model->mScrollPosition.y = controlSize.height - layoutSize.height;
682 if(needRecalc) offsetY = layoutSize.height - oldLayoutSize.height;
689 // Update glyphPositions according to recalculation.
690 const Length positionCount = visualModel->mGlyphPositions.Count();
691 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
692 for(Length index = 0u; index < positionCount; index++)
694 glyphPositions[index].y += offsetY;
701 } // namespace Toolkit