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(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 |
401 impl.mLayoutDirection = layoutDirection;
404 // Make sure the model is up-to-date before layouting.
405 controller.ProcessModifyEvents();
406 bool updated = impl.UpdateModel(operationsPending);
410 updated = DoRelayout(controller, size, operationsPending, layoutSize) || updated;
414 updateTextType = MODEL_UPDATED;
417 // Do not re-do any operation until something changes.
418 operationsPending = NO_OPERATION;
419 model->mScrollPositionLast = model->mScrollPosition;
421 // Whether the text control is editable
422 const bool isEditable = NULL != impl.mEventData;
424 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
426 if(newSize && isEditable)
428 offset = model->mScrollPosition;
431 if(!isEditable || !controller.IsMultiLineEnabled())
433 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
434 controller.CalculateVerticalOffset(size);
441 // If there is a new size, the scroll position needs to be clamped.
442 impl.ClampHorizontalScroll(layoutSize);
444 // Update the decorator's positions is needed if there is a new size.
445 impl.mEventData->mDecorator->UpdatePositions(model->mScrollPosition - offset);
448 // Move the cursor, grab handle etc.
449 if(impl.ProcessInputEvents())
451 updateTextType = static_cast<UpdateTextType>(updateTextType | DECORATOR_UPDATED);
455 // Clear the update info. This info will be set the next time the text is updated.
456 textUpdateInfo.Clear();
457 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout\n");
459 return updateTextType;
462 bool Controller::Relayouter::DoRelayout(Controller& controller, const Size& size, OperationsMask operationsRequired, Size& layoutSize)
464 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", &controller, size.width, size.height);
465 bool viewUpdated(false);
467 Controller::Impl& impl = *controller.mImpl;
469 // Calculate the operations to be done.
470 const OperationsMask operations = static_cast<OperationsMask>(impl.mOperationsPending & operationsRequired);
472 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
473 const CharacterIndex startIndex = textUpdateInfo.mParagraphCharacterIndex;
474 const Length requestedNumberOfCharacters = textUpdateInfo.mRequestedNumberOfCharacters;
476 // Get the current layout size.
477 VisualModelPtr& visualModel = impl.mModel->mVisualModel;
478 layoutSize = visualModel->GetLayoutSize();
480 if(NO_OPERATION != (LAYOUT & operations))
482 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
484 // Some vectors with data needed to layout and reorder may be void
485 // after the first time the text has been laid out.
486 // Fill the vectors again.
488 // Calculate the number of glyphs to layout.
489 const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
490 const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
491 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
492 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
494 const CharacterIndex lastIndex = startIndex + ((requestedNumberOfCharacters > 0u) ? requestedNumberOfCharacters - 1u : 0u);
495 const GlyphIndex startGlyphIndex = textUpdateInfo.mStartGlyphIndex;
497 // Make sure the index is not out of bound
498 if(charactersToGlyph.Count() != glyphsPerCharacter.Count() ||
499 requestedNumberOfCharacters > charactersToGlyph.Count() ||
500 (lastIndex > charactersToGlyph.Count() && charactersToGlyph.Count() > 0u))
502 std::string currentText;
503 controller.GetText(currentText);
505 DALI_LOG_ERROR("Controller::DoRelayout: Attempting to access invalid buffer\n");
506 DALI_LOG_ERROR("Current text is: %s\n", currentText.c_str());
507 DALI_LOG_ERROR("startIndex: %u, lastIndex: %u, requestedNumberOfCharacters: %u, charactersToGlyph.Count = %lu, glyphsPerCharacter.Count = %lu\n", startIndex, lastIndex, requestedNumberOfCharacters, charactersToGlyph.Count(), glyphsPerCharacter.Count());
512 const Length numberOfGlyphs = (requestedNumberOfCharacters > 0u) ? *(charactersToGlyphBuffer + lastIndex) + *(glyphsPerCharacterBuffer + lastIndex) - startGlyphIndex : 0u;
513 const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
515 if(0u == totalNumberOfGlyphs)
517 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
519 visualModel->SetLayoutSize(Size::ZERO);
522 // Nothing else to do if there is no glyphs.
523 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n");
527 // Set the layout parameters.
528 Layout::Parameters layoutParameters(size, impl.mModel);
530 // Resize the vector of positions to have the same size than the vector of glyphs.
531 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
532 glyphPositions.Resize(totalNumberOfGlyphs);
534 // Whether the last character is a new paragraph character.
535 const Character* const textBuffer = impl.mModel->mLogicalModel->mText.Begin();
536 textUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph(*(textBuffer + (impl.mModel->mLogicalModel->mText.Count() - 1u)));
537 layoutParameters.isLastNewParagraph = textUpdateInfo.mIsLastCharacterNewParagraph;
539 // The initial glyph and the number of glyphs to layout.
540 layoutParameters.startGlyphIndex = startGlyphIndex;
541 layoutParameters.numberOfGlyphs = numberOfGlyphs;
542 layoutParameters.startLineIndex = textUpdateInfo.mStartLineIndex;
543 layoutParameters.estimatedNumberOfLines = textUpdateInfo.mEstimatedNumberOfLines;
545 // Update the ellipsis
546 bool elideTextEnabled = impl.mModel->mElideEnabled;
548 if(NULL != impl.mEventData)
550 if(impl.mEventData->mPlaceholderEllipsisFlag && impl.IsShowingPlaceholderText())
552 elideTextEnabled = impl.mEventData->mIsPlaceholderElideEnabled;
554 else if(EventData::INACTIVE != impl.mEventData->mState)
556 // Disable ellipsis when editing
557 elideTextEnabled = false;
560 // Reset the scroll position in inactive state
561 if(elideTextEnabled && (impl.mEventData->mState == EventData::INACTIVE))
563 controller.ResetScrollPosition();
567 // Update the visual model.
568 bool isAutoScrollEnabled = impl.mIsAutoScrollEnabled;
570 viewUpdated = impl.mLayoutEngine.LayoutText(layoutParameters,
573 isAutoScrollEnabled);
574 impl.mIsAutoScrollEnabled = isAutoScrollEnabled;
576 viewUpdated = viewUpdated || (newLayoutSize != layoutSize);
580 layoutSize = newLayoutSize;
582 if(NO_OPERATION != (UPDATE_DIRECTION & operations))
584 impl.mIsTextDirectionRTL = false;
587 if((NO_OPERATION != (UPDATE_DIRECTION & operations)) && !visualModel->mLines.Empty())
589 impl.mIsTextDirectionRTL = visualModel->mLines[0u].direction;
592 // Sets the layout size.
593 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
595 visualModel->SetLayoutSize(layoutSize);
600 if(NO_OPERATION != (ALIGN & operations))
602 // The laid-out lines.
603 Vector<LineRun>& lines = visualModel->mLines;
605 CharacterIndex alignStartIndex = startIndex;
606 Length alignRequestedNumberOfCharacters = requestedNumberOfCharacters;
608 // the whole text needs to be full aligned.
609 // If you do not do a full aligned, only the last line of the multiline input is aligned.
610 if(impl.mEventData && impl.mEventData->mUpdateAlignment)
612 alignStartIndex = 0u;
613 alignRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
614 impl.mEventData->mUpdateAlignment = false;
617 // Need to align with the control's size as the text may contain lines
618 // starting either with left to right text or right to left.
619 impl.mLayoutEngine.Align(size,
621 alignRequestedNumberOfCharacters,
622 impl.mModel->mHorizontalAlignment,
624 impl.mModel->mAlignmentOffset,
625 impl.mLayoutDirection,
626 (impl.mModel->mMatchLayoutDirection != DevelText::MatchLayoutDirection::CONTENTS));
630 #if defined(DEBUG_ENABLED)
631 std::string currentText;
632 controller.GetText(currentText);
633 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Controller::DoRelayout [%p] mImpl->mIsTextDirectionRTL[%s] [%s]\n", &controller, (impl.mIsTextDirectionRTL) ? "true" : "false", currentText.c_str());
635 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", (viewUpdated ? "true" : "false"));
639 void Controller::Relayouter::CalculateVerticalOffset(Controller& controller, const Size& controlSize)
641 Controller::Impl& impl = *controller.mImpl;
642 ModelPtr& model = impl.mModel;
643 VisualModelPtr& visualModel = model->mVisualModel;
644 Size layoutSize = model->mVisualModel->GetLayoutSize();
645 Size oldLayoutSize = layoutSize;
647 bool needRecalc = false;
648 float defaultFontLineHeight = impl.GetDefaultFontLineHeight();
650 if(fabsf(layoutSize.height) < Math::MACHINE_EPSILON_1000)
652 // Get the line height of the default font.
653 layoutSize.height = defaultFontLineHeight;
656 // Whether the text control is editable
657 const bool isEditable = NULL != impl.mEventData;
658 if(isEditable && layoutSize.height != defaultFontLineHeight && impl.IsShowingPlaceholderText())
660 // This code prevents the wrong positioning of cursor when the layout size is bigger/smaller than defaultFontLineHeight.
661 // This situation occurs when the size of placeholder text is different from the default text.
662 layoutSize.height = defaultFontLineHeight;
666 switch(model->mVerticalAlignment)
668 case VerticalAlignment::TOP:
670 model->mScrollPosition.y = 0.f;
674 case VerticalAlignment::CENTER:
676 model->mScrollPosition.y = floorf(0.5f * (controlSize.height - layoutSize.height)); // try to avoid pixel alignment.
677 if(needRecalc) offsetY = floorf(0.5f * (layoutSize.height - oldLayoutSize.height));
680 case VerticalAlignment::BOTTOM:
682 model->mScrollPosition.y = controlSize.height - layoutSize.height;
683 if(needRecalc) offsetY = layoutSize.height - oldLayoutSize.height;
690 // Update glyphPositions according to recalculation.
691 const Length positionCount = visualModel->mGlyphPositions.Count();
692 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
693 for(Length index = 0u; index < positionCount; index++)
695 glyphPositions[index].y += offsetY;
702 } // namespace Toolkit