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/rendering/view-model.h>
22 #include <dali/devel-api/text-abstraction/font-client.h>
26 #include <dali-toolkit/internal/text/line-run.h>
34 ViewModel::ViewModel(const ModelInterface* const model)
39 mStartIndexOfElidedGlyphs(0u),
40 mEndIndexOfElidedGlyphs(0u),
41 mFirstMiddleIndexOfElidedGlyphs(0u),
42 mSecondMiddleIndexOfElidedGlyphs(0u)
46 ViewModel::~ViewModel()
50 const Size& ViewModel::GetControlSize() const
52 return mModel->GetControlSize();
55 const Size& ViewModel::GetLayoutSize() const
57 return mModel->GetLayoutSize();
60 const Vector2& ViewModel::GetScrollPosition() const
62 return mModel->GetScrollPosition();
65 HorizontalAlignment::Type ViewModel::GetHorizontalAlignment() const
67 return mModel->GetHorizontalAlignment();
70 VerticalAlignment::Type ViewModel::GetVerticalAlignment() const
72 return mModel->GetVerticalAlignment();
75 DevelText::VerticalLineAlignment::Type ViewModel::GetVerticalLineAlignment() const
77 return mModel->GetVerticalLineAlignment();
80 DevelText::EllipsisPosition::Type ViewModel::GetEllipsisPosition() const
82 return mModel->GetEllipsisPosition();
85 bool ViewModel::IsTextElideEnabled() const
87 return mModel->IsTextElideEnabled();
90 Length ViewModel::GetNumberOfLines() const
92 return mModel->GetNumberOfLines();
95 const LineRun* const ViewModel::GetLines() const
97 return mModel->GetLines();
100 Length ViewModel::GetNumberOfScripts() const
102 return mModel->GetNumberOfScripts();
105 const ScriptRun* const ViewModel::GetScriptRuns() const
107 return mModel->GetScriptRuns();
110 Length ViewModel::GetNumberOfGlyphs() const
112 if(mIsTextElided && mModel->IsTextElideEnabled())
114 return mElidedGlyphs.Count();
118 return mModel->GetNumberOfGlyphs();
124 GlyphIndex ViewModel::GetStartIndexOfElidedGlyphs() const
126 if(mIsTextElided && mModel->IsTextElideEnabled())
128 return mStartIndexOfElidedGlyphs;
131 return mModel->GetStartIndexOfElidedGlyphs();
134 GlyphIndex ViewModel::GetEndIndexOfElidedGlyphs() const
136 if(mIsTextElided && mModel->IsTextElideEnabled())
138 return mEndIndexOfElidedGlyphs;
141 return mModel->GetEndIndexOfElidedGlyphs();
144 GlyphIndex ViewModel::GetFirstMiddleIndexOfElidedGlyphs() const
146 if(mIsTextElided && mModel->IsTextElideEnabled())
148 return mFirstMiddleIndexOfElidedGlyphs;
151 return mModel->GetFirstMiddleIndexOfElidedGlyphs();
154 GlyphIndex ViewModel::GetSecondMiddleIndexOfElidedGlyphs() const
156 if(mIsTextElided && mModel->IsTextElideEnabled())
158 return mSecondMiddleIndexOfElidedGlyphs;
161 return mModel->GetSecondMiddleIndexOfElidedGlyphs();
164 const GlyphInfo* const ViewModel::GetGlyphs() const
166 if(mIsTextElided && mModel->IsTextElideEnabled())
168 return mElidedGlyphs.Begin();
172 return mModel->GetGlyphs();
178 const Vector2* const ViewModel::GetLayout() const
180 if(mIsTextElided && mModel->IsTextElideEnabled())
182 return mElidedLayout.Begin();
186 return mModel->GetLayout();
192 const Vector4* const ViewModel::GetColors() const
194 return mModel->GetColors();
197 const ColorIndex* const ViewModel::GetColorIndices() const
199 return mModel->GetColorIndices();
202 const Vector4* const ViewModel::GetBackgroundColors() const
204 return mModel->GetBackgroundColors();
207 const ColorIndex* const ViewModel::GetBackgroundColorIndices() const
209 return mModel->GetBackgroundColorIndices();
212 bool const ViewModel::IsMarkupBackgroundColorSet() const
214 return mModel->IsMarkupBackgroundColorSet();
217 const Vector4& ViewModel::GetDefaultColor() const
219 return mModel->GetDefaultColor();
222 const Vector2& ViewModel::GetShadowOffset() const
224 return mModel->GetShadowOffset();
227 const Vector4& ViewModel::GetShadowColor() const
229 return mModel->GetShadowColor();
232 const float& ViewModel::GetShadowBlurRadius() const
234 return mModel->GetShadowBlurRadius();
237 const Vector4& ViewModel::GetUnderlineColor() const
239 return mModel->GetUnderlineColor();
242 bool ViewModel::IsUnderlineEnabled() const
244 return mModel->IsUnderlineEnabled();
247 float ViewModel::GetUnderlineHeight() const
249 return mModel->GetUnderlineHeight();
252 Length ViewModel::GetNumberOfUnderlineRuns() const
254 return mModel->GetNumberOfUnderlineRuns();
257 void ViewModel::GetUnderlineRuns(GlyphRun* underlineRuns, UnderlineRunIndex index, Length numberOfRuns) const
259 mModel->GetUnderlineRuns(underlineRuns, index, numberOfRuns);
262 const Vector4& ViewModel::GetOutlineColor() const
264 return mModel->GetOutlineColor();
267 uint16_t ViewModel::GetOutlineWidth() const
269 return mModel->GetOutlineWidth();
272 const Vector4& ViewModel::GetBackgroundColor() const
274 return mModel->GetBackgroundColor();
277 bool ViewModel::IsBackgroundEnabled() const
279 return mModel->IsBackgroundEnabled();
282 bool ViewModel::IsMarkupProcessorEnabled() const
284 return mModel->IsMarkupProcessorEnabled();
287 const GlyphInfo* ViewModel::GetHyphens() const
289 return mModel->GetHyphens();
292 const Length* ViewModel::GetHyphenIndices() const
294 return mModel->GetHyphenIndices();
297 Length ViewModel::GetHyphensCount() const
299 return mModel->GetHyphensCount();
302 void ViewModel::ElideGlyphs()
304 mIsTextElided = false;
305 mStartIndexOfElidedGlyphs = mFirstMiddleIndexOfElidedGlyphs = mSecondMiddleIndexOfElidedGlyphs = 0;
306 mEndIndexOfElidedGlyphs = mModel->GetNumberOfGlyphs() - 1u;
308 auto ellipsisPosition = GetEllipsisPosition();
310 if(IsTextElideEnabled())
312 const Length numberOfLines = mModel->GetNumberOfLines();
313 if(0u != numberOfLines)
315 const LineRun* const lines = mModel->GetLines();
317 //Get line of ellipsis
318 const LineRun* ellipsisLine = nullptr;
319 const LineRun* ellipsisNextLine = nullptr;
321 for(Length lineIndex = 0; lineIndex < numberOfLines; lineIndex++)
323 const LineRun* line = (lines + lineIndex);
327 if(lineIndex < numberOfLines - 1u)
329 ellipsisNextLine = (lines + lineIndex + 1u);
335 // Check if there is a line contains Ellipsis.
336 // Then find total number of glyphs and total number of laid out glyphs.
337 // Check where to set Ellipsis glyph in line.
338 // Determine index of Ellipsis glyph and how many glyphs should be replaced by Ellipsis glyph, according to width of Ellipsis glyph.
339 if(ellipsisLine != nullptr)
341 // Total number of glyphs.
342 const Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
343 // Total number of laid out glyphs.
344 Length numberOfActualLaidOutGlyphs = 0u;
346 // Accumulate laid out glyphs for each line to find total number of laid out glyphs.
347 for(Length lineIndex = 0u; lineIndex < numberOfLines; lineIndex++)
349 numberOfActualLaidOutGlyphs += lines[lineIndex].glyphRun.numberOfGlyphs + lines[lineIndex].glyphRunSecondHalf.numberOfGlyphs;
352 // Make sure there are laid out glyphs.
353 if(0u != numberOfActualLaidOutGlyphs)
355 // There are elided glyphs.
356 mIsTextElided = true;
357 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
359 // Retrieve the whole glyphs and their positions.
360 const GlyphInfo* const glyphs = mModel->GetGlyphs();
361 const Vector2* const positions = mModel->GetLayout();
363 // Copy the glyphs to be elided.
364 mElidedGlyphs.Resize(numberOfGlyphs);
365 mElidedLayout.Resize(numberOfGlyphs);
366 GlyphInfo* elidedGlyphsBuffer = mElidedGlyphs.Begin();
367 Vector2* elidedPositionsBuffer = mElidedLayout.Begin();
369 memcpy(elidedGlyphsBuffer, glyphs, numberOfGlyphs * sizeof(GlyphInfo));
370 memcpy(elidedPositionsBuffer, positions, numberOfGlyphs * sizeof(Vector2));
372 const Size& controlSize = mModel->GetControlSize();
374 // Set index where to set Ellipsis according to the selected position of Ellipsis.
375 // Start with this index to replace its glyph by Ellipsis, if the width is not enough, then remove more glyphs.
376 GlyphIndex startIndexOfEllipsis = 0u;
377 if(ellipsisPosition == DevelText::EllipsisPosition::START)
379 // It's the fisrt glyph in line.
380 startIndexOfEllipsis = ellipsisLine->glyphRun.glyphIndex;
382 else if(ellipsisPosition == DevelText::EllipsisPosition::MIDDLE)
384 // It's the second middle of the line in case the line split to two halves.
385 // Otherwise it's It's the last glyph in line (line before all removed lines).
386 startIndexOfEllipsis = ellipsisLine->isSplitToTwoHalves ? (ellipsisLine->glyphRunSecondHalf.glyphIndex) : (ellipsisLine->glyphRun.glyphIndex + ellipsisLine->glyphRun.numberOfGlyphs - 1u);
388 else // DevelText::EllipsisPosition::END
390 // It's the last glyph in line.
391 startIndexOfEllipsis = ellipsisLine->glyphRun.glyphIndex + ellipsisLine->glyphRun.numberOfGlyphs - 1u;
394 // When the hight is not enough then show one glyph and that should be the first laid out glyph.
395 if((1u == numberOfLines) &&
396 (ellipsisLine->ascender - ellipsisLine->descender > controlSize.height))
398 // Replace the first glyph with ellipsis glyph
399 auto indexOfFirstGlyph = (ellipsisPosition == DevelText::EllipsisPosition::START) ? startIndexOfEllipsis : 0u;
401 // Regardless where the location of ellipsis,in-case the hight of line is greater than control's height
402 // then replace the first glyph with ellipsis glyph.
404 // Get the first glyph which is going to be replaced and the ellipsis glyph.
405 GlyphInfo& glyphToRemove = *(elidedGlyphsBuffer + indexOfFirstGlyph);
406 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph(fontClient.GetPointSize(glyphToRemove.fontId));
408 // Change the 'x' and 'y' position of the ellipsis glyph.
409 Vector2& position = *(elidedPositionsBuffer + indexOfFirstGlyph);
411 position.x = ellipsisGlyph.xBearing;
412 position.y = -ellipsisLine->ascender + controlSize.height - ellipsisGlyph.yBearing;
414 // Replace the glyph by the ellipsis glyph and resize the buffers.
415 glyphToRemove = ellipsisGlyph;
417 mElidedGlyphs.Resize(1u);
418 mElidedLayout.Resize(1u);
420 mEndIndexOfElidedGlyphs = mStartIndexOfElidedGlyphs = mFirstMiddleIndexOfElidedGlyphs = mSecondMiddleIndexOfElidedGlyphs = indexOfFirstGlyph;
425 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
426 float firstPenX = 0.f; // Used if rtl text is elided.
428 bool firstPenSet = false;
430 // Add the ellipsis glyph.
431 bool inserted = false;
432 float removedGlypsWidth = 0.f;
433 Length numberOfRemovedGlyphs = 0u;
434 GlyphIndex indexOfEllipsis = startIndexOfEllipsis;
436 // Tail Mode: start by the end of line.
437 bool isTailMode = (ellipsisPosition == DevelText::EllipsisPosition::END) ||
438 (ellipsisPosition == DevelText::EllipsisPosition::MIDDLE && numberOfLines != 1u);
440 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
443 const GlyphInfo& glyphToRemove = *(elidedGlyphsBuffer + indexOfEllipsis);
445 if(0u != glyphToRemove.fontId)
447 // i.e. The font id of the glyph shaped from the '\n' character is zero.
449 // Need to reshape the glyph as the font may be different in size.
450 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph(fontClient.GetPointSize(glyphToRemove.fontId));
454 const Vector2& position = *(elidedPositionsBuffer + indexOfEllipsis);
456 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
457 penY = position.y + glyphToRemove.yBearing;
459 // Calculates the first penX which will be used if rtl text is elided.
460 firstPenX = position.x - glyphToRemove.xBearing;
461 if(firstPenX < -ellipsisGlyph.xBearing)
463 // Avoids to exceed the bounding box when rtl text is elided.
464 firstPenX = -ellipsisGlyph.xBearing;
467 removedGlypsWidth = -ellipsisGlyph.xBearing;
472 removedGlypsWidth += std::min(glyphToRemove.advance, (glyphToRemove.xBearing + glyphToRemove.width));
474 // Calculate the width of the ellipsis glyph and check if it fits.
475 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
477 // If it is the last glyph to remove, add the ellipsis glyph without checking its width.
478 if((ellipsisGlyphWidth < removedGlypsWidth) || (isTailMode ? (indexOfEllipsis == 0u) : (indexOfEllipsis == numberOfGlyphs - 1u)))
480 GlyphInfo& glyphInfo = *(elidedGlyphsBuffer + indexOfEllipsis);
481 Vector2& position = *(elidedPositionsBuffer + indexOfEllipsis);
482 position.x -= (0.f > glyphInfo.xBearing) ? glyphInfo.xBearing : 0.f;
484 // Replace the glyph by the ellipsis glyph.
485 glyphInfo = ellipsisGlyph;
487 // Change the 'x' and 'y' position of the ellipsis glyph.
488 if(position.x > firstPenX)
490 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
493 position.x += ellipsisGlyph.xBearing;
494 position.y = penY - ellipsisGlyph.yBearing;
502 if(!isTailMode && indexOfEllipsis < numberOfGlyphs - 1u)
504 // Tail Mode: remove glyphs from startIndexOfEllipsis then decrement indexOfEllipsis, until arrive to index zero.
507 else if(isTailMode && indexOfEllipsis > 0u)
509 // Not Tail Mode: remove glyphs from startIndexOfEllipsis then increase indexOfEllipsis, until arrive to last index (numberOfGlyphs - 1u).
514 // No space for the ellipsis.
517 ++numberOfRemovedGlyphs;
519 } // while( !inserted )
521 //Reduce size, shift glyphs and start from ellipsis glyph
522 Length numberOfElidedGlyphs = numberOfActualLaidOutGlyphs - numberOfRemovedGlyphs;
523 mElidedGlyphs.Resize(numberOfElidedGlyphs);
524 mElidedLayout.Resize(numberOfElidedGlyphs);
526 if(ellipsisPosition == DevelText::EllipsisPosition::START)
528 // 'Shifts' glyphs after ellipsis glyph and 'Removes' before ellipsis glyph
529 memcpy(elidedGlyphsBuffer, elidedGlyphsBuffer + indexOfEllipsis, numberOfElidedGlyphs * sizeof(GlyphInfo));
530 memcpy(elidedPositionsBuffer, elidedPositionsBuffer + indexOfEllipsis, numberOfElidedGlyphs * sizeof(Vector2));
532 mStartIndexOfElidedGlyphs = mFirstMiddleIndexOfElidedGlyphs = mSecondMiddleIndexOfElidedGlyphs = indexOfEllipsis;
534 else if(ellipsisPosition == DevelText::EllipsisPosition::MIDDLE)
536 // 'Shifts and connects' glyphs before and after ellipsis glyph and 'Removes' in-between.
537 bool isOnlySecondHalf = false;
541 mFirstMiddleIndexOfElidedGlyphs = indexOfEllipsis;
542 if(ellipsisNextLine != nullptr)
544 mSecondMiddleIndexOfElidedGlyphs = ellipsisNextLine->glyphRun.glyphIndex;
548 mEndIndexOfElidedGlyphs = mSecondMiddleIndexOfElidedGlyphs = mFirstMiddleIndexOfElidedGlyphs;
553 mFirstMiddleIndexOfElidedGlyphs = (ellipsisLine->glyphRun.numberOfGlyphs > 0u) ? (ellipsisLine->glyphRun.glyphIndex + ellipsisLine->glyphRun.numberOfGlyphs - 1u) : (ellipsisLine->glyphRun.glyphIndex);
554 mSecondMiddleIndexOfElidedGlyphs = indexOfEllipsis;
555 isOnlySecondHalf = ellipsisLine->glyphRun.numberOfGlyphs == 0u && ellipsisLine->glyphRunSecondHalf.numberOfGlyphs > 0u;
560 Length numberOfSecondHalfGlyphs = numberOfElidedGlyphs - mFirstMiddleIndexOfElidedGlyphs;
562 //Copy elided glyphs after the ellipsis glyph.
563 memcpy(elidedGlyphsBuffer + mFirstMiddleIndexOfElidedGlyphs, elidedGlyphsBuffer + mSecondMiddleIndexOfElidedGlyphs, numberOfSecondHalfGlyphs * sizeof(GlyphInfo));
564 memcpy(elidedPositionsBuffer + mFirstMiddleIndexOfElidedGlyphs, elidedPositionsBuffer + mSecondMiddleIndexOfElidedGlyphs, numberOfSecondHalfGlyphs * sizeof(Vector2));
568 Length numberOfSecondHalfGlyphs = numberOfElidedGlyphs - mFirstMiddleIndexOfElidedGlyphs + 1u;
570 //Copy elided glyphs after the ellipsis glyph.
571 memcpy(elidedGlyphsBuffer + mFirstMiddleIndexOfElidedGlyphs + 1u, elidedGlyphsBuffer + mSecondMiddleIndexOfElidedGlyphs, numberOfSecondHalfGlyphs * sizeof(GlyphInfo));
572 memcpy(elidedPositionsBuffer + mFirstMiddleIndexOfElidedGlyphs + 1u, elidedPositionsBuffer + mSecondMiddleIndexOfElidedGlyphs, numberOfSecondHalfGlyphs * sizeof(Vector2));
575 else // DevelText::EllipsisPosition::END
577 // 'Removes' all the glyphs after the ellipsis glyph.
578 mEndIndexOfElidedGlyphs = indexOfEllipsis;
588 } // namespace Toolkit