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-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, bool restoreLinesAndGlyphPositions)
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 // Store the pending operations mask so that it can be restored later on with no modifications made on it
62 // while getting the natural size were reflected on the original mask.
63 OperationsMask operationsPendingBackUp = static_cast<OperationsMask>(impl.mOperationsPending);
65 // This is a hotfix for side effect on Scrolling, LineWrap and Invalid position of cursor in TextEditor after calling CalculateLayoutSizeOnRequiredControllerSize.
66 // The number of lines and glyph-positions inside visualModel have been changed by calling DoRelayout with requestedControllerSize.
67 // Store the mLines and mGlyphPositions from visualModel so that they can be restored later on with no modifications made on them.
68 //TODO: Refactor "DoRelayout" and extract common code of size calculation without modifying attributes of mVisualModel, and then blah, blah, etc.
69 Vector<LineRun> linesBackup = visualModel->mLines;
70 Vector<Vector2> glyphPositionsBackup = visualModel->mGlyphPositions;
72 // Operations that can be done only once until the text changes.
73 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>(CONVERT_TO_UTF32 |
81 // Set the update info to relayout the whole text.
82 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
83 if((0 == textUpdateInfo.mNumberOfCharactersToAdd) &&
84 (0 == textUpdateInfo.mPreviousNumberOfCharacters) &&
85 ((visualModel->mControlSize.width < Math::MACHINE_EPSILON_1000) || (visualModel->mControlSize.height < Math::MACHINE_EPSILON_1000)))
87 textUpdateInfo.mNumberOfCharactersToAdd = model->mLogicalModel->mText.Count();
89 textUpdateInfo.mParagraphCharacterIndex = 0u;
90 textUpdateInfo.mRequestedNumberOfCharacters = model->mLogicalModel->mText.Count();
92 // Make sure the model is up-to-date before layouting
93 impl.UpdateModel(onlyOnceOperations);
95 // Get a reference to the pending operations member
96 OperationsMask& operationsPending = impl.mOperationsPending;
98 // Layout the text for the new width.
99 operationsPending = static_cast<OperationsMask>(operationsPending | requestedOperationsMask);
101 // Store the actual control's size to restore later.
102 const Size actualControlSize = visualModel->mControlSize;
105 requestedControllerSize,
106 static_cast<OperationsMask>(onlyOnceOperations |
107 requestedOperationsMask),
108 calculatedLayoutSize);
110 // Clear the update info. This info will be set the next time the text is updated.
111 textUpdateInfo.Clear();
112 textUpdateInfo.mClearAll = true;
114 // Restore the actual control's size.
115 visualModel->mControlSize = actualControlSize;
116 // Restore the previously backed-up pending operations' mask without the only once operations.
117 impl.mOperationsPending = static_cast<OperationsMask>(operationsPendingBackUp & ~onlyOnceOperations);
119 // Restore the previously backed-up mLines and mGlyphPositions from visualModel.
120 if(restoreLinesAndGlyphPositions)
122 visualModel->mLines = linesBackup;
123 visualModel->mGlyphPositions = glyphPositionsBackup;
126 return calculatedLayoutSize;
129 Vector3 Controller::Relayouter::GetNaturalSize(Controller& controller)
131 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n");
132 Vector3 naturalSizeVec3;
134 // Make sure the model is up-to-date before layouting
135 EventHandler::ProcessModifyEvents(controller);
137 Controller::Impl& impl = *controller.mImpl;
138 ModelPtr& model = impl.mModel;
139 VisualModelPtr& visualModel = model->mVisualModel;
141 if(impl.mRecalculateNaturalSize)
145 // Layout the text for the new width.
146 OperationsMask requestedOperationsMask = static_cast<OperationsMask>(LAYOUT | REORDER);
147 Size sizeMaxWidthAndMaxHeight = Size(MAX_FLOAT, MAX_FLOAT);
149 naturalSize = CalculateLayoutSizeOnRequiredControllerSize(controller, sizeMaxWidthAndMaxHeight, requestedOperationsMask, true);
151 // Stores the natural size to avoid recalculate it again
152 // unless the text/style changes.
153 visualModel->SetNaturalSize(naturalSize);
154 naturalSizeVec3 = naturalSize;
156 impl.mRecalculateNaturalSize = false;
158 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSizeVec3.x, naturalSizeVec3.y, naturalSizeVec3.z);
162 naturalSizeVec3 = visualModel->GetNaturalSize();
164 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSizeVec3.x, naturalSizeVec3.y, naturalSizeVec3.z);
167 naturalSizeVec3.x = ConvertToEven(naturalSizeVec3.x);
168 naturalSizeVec3.y = ConvertToEven(naturalSizeVec3.y);
170 return naturalSizeVec3;
173 bool Controller::Relayouter::CheckForTextFit(Controller& controller, float pointSize, const Size& layoutSize)
176 Controller::Impl& impl = *controller.mImpl;
177 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
178 impl.mFontDefaults->mFitPointSize = pointSize;
179 impl.mFontDefaults->sizeDefined = true;
180 impl.ClearFontData();
182 // Operations that can be done only once until the text changes.
183 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>(CONVERT_TO_UTF32 |
191 textUpdateInfo.mParagraphCharacterIndex = 0u;
192 textUpdateInfo.mRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
194 // Make sure the model is up-to-date before layouting
195 impl.UpdateModel(onlyOnceOperations);
198 Size(layoutSize.width, MAX_FLOAT),
199 static_cast<OperationsMask>(onlyOnceOperations | LAYOUT),
202 // Clear the update info. This info will be set the next time the text is updated.
203 textUpdateInfo.Clear();
204 textUpdateInfo.mClearAll = true;
206 if(textSize.width > layoutSize.width || textSize.height > layoutSize.height)
213 void Controller::Relayouter::FitPointSizeforLayout(Controller& controller, const Size& layoutSize)
215 Controller::Impl& impl = *controller.mImpl;
217 const OperationsMask operations = impl.mOperationsPending;
218 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations) || impl.mTextFitContentSize != layoutSize)
220 ModelPtr& model = impl.mModel;
222 bool actualellipsis = model->mElideEnabled;
223 float minPointSize = impl.mTextFitMinSize;
224 float maxPointSize = impl.mTextFitMaxSize;
225 float pointInterval = impl.mTextFitStepSize;
226 float currentFitPointSize = impl.mFontDefaults->mFitPointSize;
228 model->mElideEnabled = false;
229 Vector<float> pointSizeArray;
232 if(pointInterval < 1.f)
234 impl.mTextFitStepSize = pointInterval = 1.0f;
237 pointSizeArray.Reserve(static_cast<unsigned int>(ceil((maxPointSize - minPointSize) / pointInterval)));
239 for(float i = minPointSize; i < maxPointSize; i += pointInterval)
241 pointSizeArray.PushBack(i);
244 pointSizeArray.PushBack(maxPointSize);
246 int bestSizeIndex = 0;
247 int min = bestSizeIndex + 1;
248 int max = pointSizeArray.Size() - 1;
251 int destI = (min + max) / 2;
253 if(CheckForTextFit(controller, pointSizeArray[destI], layoutSize))
265 model->mElideEnabled = actualellipsis;
266 if(currentFitPointSize != pointSizeArray[bestSizeIndex])
268 impl.mTextFitChanged = true;
270 impl.mFontDefaults->mFitPointSize = pointSizeArray[bestSizeIndex];
271 impl.mFontDefaults->sizeDefined = true;
272 impl.ClearFontData();
276 float Controller::Relayouter::GetHeightForWidth(Controller& controller, float width)
278 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", &controller, width);
280 // Make sure the model is up-to-date before layouting
281 EventHandler::ProcessModifyEvents(controller);
283 Controller::Impl& impl = *controller.mImpl;
284 ModelPtr& model = impl.mModel;
285 VisualModelPtr& visualModel = model->mVisualModel;
286 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
290 if(fabsf(width - visualModel->mControlSize.width) > Math::MACHINE_EPSILON_1000 ||
291 textUpdateInfo.mFullRelayoutNeeded ||
292 textUpdateInfo.mClearAll)
294 // Layout the text for the new width.
295 OperationsMask requestedOperationsMask = static_cast<OperationsMask>(LAYOUT);
296 Size sizeRequestedWidthAndMaxHeight = Size(width, MAX_FLOAT);
298 // Skip restore, because if GetHeightForWidth called before rendering and layouting then visualModel->mControlSize will be zero which will make LineCount zero.
299 // The implementation of Get LineCount property depends on calling GetHeightForWidth then read mLines.Count() from visualModel direct.
300 // If the LineCount property is requested before rendering and layouting then the value will be zero, which is incorrect.
301 // So we will not restore the previously backed-up mLines and mGlyphPositions from visualModel in such case.
302 // 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.
303 // For example, when the text is changed.
304 bool restoreLinesAndGlyphPositions = (visualModel->mControlSize.width > 0 && visualModel->mControlSize.height > 0) && (visualModel->mControlSize.width != width);
306 layoutSize = CalculateLayoutSizeOnRequiredControllerSize(controller, sizeRequestedWidthAndMaxHeight, requestedOperationsMask, restoreLinesAndGlyphPositions);
308 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height);
312 layoutSize = visualModel->GetLayoutSize();
313 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height);
316 return layoutSize.height;
319 Controller::UpdateTextType Controller::Relayouter::Relayout(Controller& controller, const Size& size, Dali::LayoutDirection::Type layoutDirection)
321 Controller::Impl& impl = *controller.mImpl;
322 ModelPtr& model = impl.mModel;
323 VisualModelPtr& visualModel = model->mVisualModel;
324 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
326 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", &controller, size.width, size.height, impl.mIsAutoScrollEnabled ? "true" : "false");
328 UpdateTextType updateTextType = NONE_UPDATED;
330 if((size.width < Math::MACHINE_EPSILON_1000) || (size.height < Math::MACHINE_EPSILON_1000))
332 if(0u != visualModel->mGlyphPositions.Count())
334 visualModel->mGlyphPositions.Clear();
335 updateTextType = MODEL_UPDATED;
338 // Clear the update info. This info will be set the next time the text is updated.
339 textUpdateInfo.Clear();
341 // Not worth to relayout if width or height is equal to zero.
342 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n");
344 return updateTextType;
347 // Whether a new size has been set.
348 const bool newSize = (size != visualModel->mControlSize);
350 // Get a reference to the pending operations member
351 OperationsMask& operationsPending = impl.mOperationsPending;
355 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", visualModel->mControlSize.width, visualModel->mControlSize.height);
357 if((0 == textUpdateInfo.mNumberOfCharactersToAdd) &&
358 (0 == textUpdateInfo.mPreviousNumberOfCharacters) &&
359 ((visualModel->mControlSize.width < Math::MACHINE_EPSILON_1000) || (visualModel->mControlSize.height < Math::MACHINE_EPSILON_1000)))
361 textUpdateInfo.mNumberOfCharactersToAdd = model->mLogicalModel->mText.Count();
364 // Layout operations that need to be done if the size changes.
365 operationsPending = static_cast<OperationsMask>(operationsPending |
370 // Set the update info to relayout the whole text.
371 textUpdateInfo.mFullRelayoutNeeded = true;
372 textUpdateInfo.mCharacterIndex = 0u;
374 // Store the size used to layout the text.
375 visualModel->mControlSize = size;
378 // Whether there are modify events.
379 if(0u != impl.mModifyEvents.Count())
381 // Style operations that need to be done if the text is modified.
382 operationsPending = static_cast<OperationsMask>(operationsPending | COLOR);
385 // Set the update info to elide the text.
386 if(model->mElideEnabled ||
387 ((NULL != impl.mEventData) && impl.mEventData->mIsPlaceholderElideEnabled))
389 // Update Text layout for applying elided
390 operationsPending = static_cast<OperationsMask>(operationsPending |
395 textUpdateInfo.mFullRelayoutNeeded = true;
396 textUpdateInfo.mCharacterIndex = 0u;
399 if(impl.mLayoutDirection != layoutDirection)
401 // Clear the update info. This info will be set the next time the text is updated.
402 textUpdateInfo.mClearAll = true;
403 // Apply modifications to the model
404 // Shape the text again is needed because characters like '()[]{}' have to be mirrored and the glyphs generated again.
405 operationsPending = static_cast<OperationsMask>(operationsPending |
413 impl.mLayoutDirection = layoutDirection;
416 // Make sure the model is up-to-date before layouting.
417 EventHandler::ProcessModifyEvents(controller);
418 bool updated = impl.UpdateModel(operationsPending);
422 updated = DoRelayout(impl, size, operationsPending, layoutSize) || updated;
426 updateTextType = MODEL_UPDATED;
429 // Do not re-do any operation until something changes.
430 operationsPending = NO_OPERATION;
431 model->mScrollPositionLast = model->mScrollPosition;
433 // Whether the text control is editable
434 const bool isEditable = NULL != impl.mEventData;
436 // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
438 if(newSize && isEditable)
440 offset = model->mScrollPosition;
443 if(!isEditable || !controller.IsMultiLineEnabled())
445 // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
446 CalculateVerticalOffset(controller, size);
453 // If there is a new size, the scroll position needs to be clamped.
454 impl.ClampHorizontalScroll(layoutSize);
456 // Update the decorator's positions is needed if there is a new size.
457 impl.mEventData->mDecorator->UpdatePositions(model->mScrollPosition - offset);
460 // Move the cursor, grab handle etc.
461 if(impl.ProcessInputEvents())
463 updateTextType = static_cast<UpdateTextType>(updateTextType | DECORATOR_UPDATED);
467 // Clear the update info. This info will be set the next time the text is updated.
468 textUpdateInfo.Clear();
469 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayout\n");
471 return updateTextType;
474 bool Controller::Relayouter::DoRelayout(Controller::Impl& impl, const Size& size, OperationsMask operationsRequired, Size& layoutSize)
476 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::Relayouter::DoRelayout %p size %f,%f\n", &impl, size.width, size.height);
477 bool viewUpdated(false);
479 // Calculate the operations to be done.
480 const OperationsMask operations = static_cast<OperationsMask>(impl.mOperationsPending & operationsRequired);
482 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
483 const CharacterIndex startIndex = textUpdateInfo.mParagraphCharacterIndex;
484 const Length requestedNumberOfCharacters = textUpdateInfo.mRequestedNumberOfCharacters;
486 // Get the current layout size.
487 VisualModelPtr& visualModel = impl.mModel->mVisualModel;
488 layoutSize = visualModel->GetLayoutSize();
490 if(NO_OPERATION != (LAYOUT & operations))
492 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
494 // Some vectors with data needed to layout and reorder may be void
495 // after the first time the text has been laid out.
496 // Fill the vectors again.
498 // Calculate the number of glyphs to layout.
499 const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
500 const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
501 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
502 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
504 const CharacterIndex lastIndex = startIndex + ((requestedNumberOfCharacters > 0u) ? requestedNumberOfCharacters - 1u : 0u);
505 const GlyphIndex startGlyphIndex = textUpdateInfo.mStartGlyphIndex;
507 // Make sure the index is not out of bound
508 if(charactersToGlyph.Count() != glyphsPerCharacter.Count() ||
509 requestedNumberOfCharacters > charactersToGlyph.Count() ||
510 (lastIndex > charactersToGlyph.Count() && charactersToGlyph.Count() > 0u))
512 std::string currentText;
513 impl.GetText(currentText);
515 DALI_LOG_ERROR("Controller::DoRelayout: Attempting to access invalid buffer\n");
516 DALI_LOG_ERROR("Current text is: %s\n", currentText.c_str());
517 DALI_LOG_ERROR("startIndex: %u, lastIndex: %u, requestedNumberOfCharacters: %u, charactersToGlyph.Count = %lu, glyphsPerCharacter.Count = %lu\n", startIndex, lastIndex, requestedNumberOfCharacters, charactersToGlyph.Count(), glyphsPerCharacter.Count());
522 const Length numberOfGlyphs = (requestedNumberOfCharacters > 0u) ? *(charactersToGlyphBuffer + lastIndex) + *(glyphsPerCharacterBuffer + lastIndex) - startGlyphIndex : 0u;
523 const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
525 if(0u == totalNumberOfGlyphs)
527 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
529 visualModel->SetLayoutSize(Size::ZERO);
532 // Nothing else to do if there is no glyphs.
533 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n");
537 // Set the layout parameters.
538 Layout::Parameters layoutParameters(size, impl.mModel);
540 // Resize the vector of positions to have the same size than the vector of glyphs.
541 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
542 glyphPositions.Resize(totalNumberOfGlyphs);
544 // Whether the last character is a new paragraph character.
545 const Character* const textBuffer = impl.mModel->mLogicalModel->mText.Begin();
546 textUpdateInfo.mIsLastCharacterNewParagraph = TextAbstraction::IsNewParagraph(*(textBuffer + (impl.mModel->mLogicalModel->mText.Count() - 1u)));
547 layoutParameters.isLastNewParagraph = textUpdateInfo.mIsLastCharacterNewParagraph;
549 // The initial glyph and the number of glyphs to layout.
550 layoutParameters.startGlyphIndex = startGlyphIndex;
551 layoutParameters.numberOfGlyphs = numberOfGlyphs;
552 layoutParameters.startLineIndex = textUpdateInfo.mStartLineIndex;
553 layoutParameters.estimatedNumberOfLines = textUpdateInfo.mEstimatedNumberOfLines;
555 // Update the ellipsis
556 bool elideTextEnabled = impl.mModel->mElideEnabled;
557 auto ellipsisPosition = impl.mModel->mEllipsisPosition;
559 if(NULL != impl.mEventData)
561 if(impl.mEventData->mPlaceholderEllipsisFlag && impl.IsShowingPlaceholderText())
563 elideTextEnabled = impl.mEventData->mIsPlaceholderElideEnabled;
565 else if(EventData::INACTIVE != impl.mEventData->mState)
567 // Disable ellipsis when editing
568 elideTextEnabled = false;
571 // Reset the scroll position in inactive state
572 if(elideTextEnabled && (impl.mEventData->mState == EventData::INACTIVE))
574 impl.ResetScrollPosition();
578 // Update the visual model.
579 bool isAutoScrollEnabled = impl.mIsAutoScrollEnabled;
581 viewUpdated = impl.mLayoutEngine.LayoutText(layoutParameters,
586 impl.mIsAutoScrollEnabled = isAutoScrollEnabled;
588 viewUpdated = viewUpdated || (newLayoutSize != layoutSize);
592 layoutSize = newLayoutSize;
594 if(NO_OPERATION != (UPDATE_DIRECTION & operations))
596 impl.mIsTextDirectionRTL = false;
599 if((NO_OPERATION != (UPDATE_DIRECTION & operations)) && !visualModel->mLines.Empty())
601 impl.mIsTextDirectionRTL = visualModel->mLines[0u].direction;
604 // Sets the layout size.
605 if(NO_OPERATION != (UPDATE_LAYOUT_SIZE & operations))
607 visualModel->SetLayoutSize(layoutSize);
612 if(NO_OPERATION != (ALIGN & operations))
614 // The laid-out lines.
615 Vector<LineRun>& lines = visualModel->mLines;
617 CharacterIndex alignStartIndex = startIndex;
618 Length alignRequestedNumberOfCharacters = requestedNumberOfCharacters;
620 // the whole text needs to be full aligned.
621 // If you do not do a full aligned, only the last line of the multiline input is aligned.
622 if(impl.mEventData && impl.mEventData->mUpdateAlignment)
624 alignStartIndex = 0u;
625 alignRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
626 impl.mEventData->mUpdateAlignment = false;
629 // Need to align with the control's size as the text may contain lines
630 // starting either with left to right text or right to left.
631 impl.mLayoutEngine.Align(size,
633 alignRequestedNumberOfCharacters,
634 impl.mModel->mHorizontalAlignment,
636 impl.mModel->mAlignmentOffset,
637 impl.mLayoutDirection,
638 (impl.mModel->mMatchLayoutDirection != DevelText::MatchLayoutDirection::CONTENTS));
642 #if defined(DEBUG_ENABLED)
643 std::string currentText;
644 impl.GetText(currentText);
645 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Controller::Relayouter::DoRelayout [%p] mImpl->mIsTextDirectionRTL[%s] [%s]\n", &impl, (impl.mIsTextDirectionRTL) ? "true" : "false", currentText.c_str());
647 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "<--Controller::Relayouter::DoRelayout, view updated %s\n", (viewUpdated ? "true" : "false"));
651 void Controller::Relayouter::CalculateVerticalOffset(Controller& controller, const Size& controlSize)
653 Controller::Impl& impl = *controller.mImpl;
654 ModelPtr& model = impl.mModel;
655 VisualModelPtr& visualModel = model->mVisualModel;
656 Size layoutSize = model->mVisualModel->GetLayoutSize();
657 Size oldLayoutSize = layoutSize;
659 bool needRecalc = false;
660 float defaultFontLineHeight = impl.GetDefaultFontLineHeight();
662 if(fabsf(layoutSize.height) < Math::MACHINE_EPSILON_1000)
664 // Get the line height of the default font.
665 layoutSize.height = defaultFontLineHeight;
668 // Whether the text control is editable
669 const bool isEditable = NULL != impl.mEventData;
670 if(isEditable && layoutSize.height != defaultFontLineHeight && impl.IsShowingPlaceholderText())
672 // This code prevents the wrong positioning of cursor when the layout size is bigger/smaller than defaultFontLineHeight.
673 // This situation occurs when the size of placeholder text is different from the default text.
674 layoutSize.height = defaultFontLineHeight;
678 switch(model->mVerticalAlignment)
680 case VerticalAlignment::TOP:
682 model->mScrollPosition.y = 0.f;
686 case VerticalAlignment::CENTER:
688 model->mScrollPosition.y = floorf(0.5f * (controlSize.height - layoutSize.height)); // try to avoid pixel alignment.
689 if(needRecalc) offsetY = floorf(0.5f * (layoutSize.height - oldLayoutSize.height));
692 case VerticalAlignment::BOTTOM:
694 model->mScrollPosition.y = controlSize.height - layoutSize.height;
695 if(needRecalc) offsetY = layoutSize.height - oldLayoutSize.height;
702 // Update glyphPositions according to recalculation.
703 const Length positionCount = visualModel->mGlyphPositions.Count();
704 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
705 for(Length index = 0u; index < positionCount; index++)
707 glyphPositions[index].y += offsetY;
714 } // namespace Toolkit