2 * Copyright (c) 2022 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>
27 #include <dali-toolkit/internal/text/glyph-metrics-helper.h>
35 ViewModel::ViewModel(const ModelInterface* const model)
40 mStartIndexOfElidedGlyphs(0u),
41 mEndIndexOfElidedGlyphs(0u),
42 mFirstMiddleIndexOfElidedGlyphs(0u),
43 mSecondMiddleIndexOfElidedGlyphs(0u)
47 ViewModel::~ViewModel()
51 const Size& ViewModel::GetControlSize() const
53 return mModel->GetControlSize();
56 const Size& ViewModel::GetLayoutSize() const
58 return mModel->GetLayoutSize();
61 const Vector2& ViewModel::GetScrollPosition() const
63 return mModel->GetScrollPosition();
66 HorizontalAlignment::Type ViewModel::GetHorizontalAlignment() const
68 return mModel->GetHorizontalAlignment();
71 VerticalAlignment::Type ViewModel::GetVerticalAlignment() const
73 return mModel->GetVerticalAlignment();
76 DevelText::VerticalLineAlignment::Type ViewModel::GetVerticalLineAlignment() const
78 return mModel->GetVerticalLineAlignment();
81 DevelText::EllipsisPosition::Type ViewModel::GetEllipsisPosition() const
83 return mModel->GetEllipsisPosition();
86 bool ViewModel::IsTextElideEnabled() const
88 return mModel->IsTextElideEnabled();
91 Length ViewModel::GetNumberOfLines() const
93 return mModel->GetNumberOfLines();
96 const LineRun* const ViewModel::GetLines() const
98 return mModel->GetLines();
101 Length ViewModel::GetNumberOfScripts() const
103 return mModel->GetNumberOfScripts();
106 const ScriptRun* const ViewModel::GetScriptRuns() const
108 return mModel->GetScriptRuns();
111 Length ViewModel::GetNumberOfGlyphs() const
113 if(mIsTextElided && mModel->IsTextElideEnabled())
115 return mElidedGlyphs.Count();
119 return mModel->GetNumberOfGlyphs();
125 GlyphIndex ViewModel::GetStartIndexOfElidedGlyphs() const
127 if(mIsTextElided && mModel->IsTextElideEnabled())
129 return mStartIndexOfElidedGlyphs;
132 return mModel->GetStartIndexOfElidedGlyphs();
135 GlyphIndex ViewModel::GetEndIndexOfElidedGlyphs() const
137 if(mIsTextElided && mModel->IsTextElideEnabled())
139 return mEndIndexOfElidedGlyphs;
142 return mModel->GetEndIndexOfElidedGlyphs();
145 GlyphIndex ViewModel::GetFirstMiddleIndexOfElidedGlyphs() const
147 if(mIsTextElided && mModel->IsTextElideEnabled())
149 return mFirstMiddleIndexOfElidedGlyphs;
152 return mModel->GetFirstMiddleIndexOfElidedGlyphs();
155 GlyphIndex ViewModel::GetSecondMiddleIndexOfElidedGlyphs() const
157 if(mIsTextElided && mModel->IsTextElideEnabled())
159 return mSecondMiddleIndexOfElidedGlyphs;
162 return mModel->GetSecondMiddleIndexOfElidedGlyphs();
165 const GlyphInfo* const ViewModel::GetGlyphs() const
167 if(mIsTextElided && mModel->IsTextElideEnabled())
169 return mElidedGlyphs.Begin();
173 return mModel->GetGlyphs();
179 const Vector2* const ViewModel::GetLayout() const
181 if(mIsTextElided && mModel->IsTextElideEnabled())
183 return mElidedLayout.Begin();
187 return mModel->GetLayout();
193 const Vector4* const ViewModel::GetColors() const
195 return mModel->GetColors();
198 const ColorIndex* const ViewModel::GetColorIndices() const
200 return mModel->GetColorIndices();
203 const Vector4* const ViewModel::GetBackgroundColors() const
205 return mModel->GetBackgroundColors();
208 const ColorIndex* const ViewModel::GetBackgroundColorIndices() const
210 return mModel->GetBackgroundColorIndices();
213 bool const ViewModel::IsMarkupBackgroundColorSet() const
215 return mModel->IsMarkupBackgroundColorSet();
218 const Vector4& ViewModel::GetDefaultColor() const
220 return mModel->GetDefaultColor();
223 const Vector2& ViewModel::GetShadowOffset() const
225 return mModel->GetShadowOffset();
228 const Vector4& ViewModel::GetShadowColor() const
230 return mModel->GetShadowColor();
233 const float& ViewModel::GetShadowBlurRadius() const
235 return mModel->GetShadowBlurRadius();
238 const Vector4& ViewModel::GetUnderlineColor() const
240 return mModel->GetUnderlineColor();
243 bool ViewModel::IsUnderlineEnabled() const
245 return mModel->IsUnderlineEnabled();
248 float ViewModel::GetUnderlineHeight() const
250 return mModel->GetUnderlineHeight();
253 Text::Underline::Type ViewModel::GetUnderlineType() const
255 return mModel->GetUnderlineType();
258 float ViewModel::GetDashedUnderlineWidth() const
260 return mModel->GetDashedUnderlineWidth();
263 float ViewModel::GetDashedUnderlineGap() const
265 return mModel->GetDashedUnderlineGap();
268 Length ViewModel::GetNumberOfUnderlineRuns() const
270 return mModel->GetNumberOfUnderlineRuns();
273 void ViewModel::GetUnderlineRuns(GlyphRun* underlineRuns, UnderlineRunIndex index, Length numberOfRuns) const
275 mModel->GetUnderlineRuns(underlineRuns, index, numberOfRuns);
278 const Vector4& ViewModel::GetOutlineColor() const
280 return mModel->GetOutlineColor();
283 uint16_t ViewModel::GetOutlineWidth() const
285 return mModel->GetOutlineWidth();
288 const Vector4& ViewModel::GetBackgroundColor() const
290 return mModel->GetBackgroundColor();
293 bool ViewModel::IsBackgroundEnabled() const
295 return mModel->IsBackgroundEnabled();
298 bool ViewModel::IsMarkupProcessorEnabled() const
300 return mModel->IsMarkupProcessorEnabled();
303 const GlyphInfo* ViewModel::GetHyphens() const
305 return mModel->GetHyphens();
308 const Length* ViewModel::GetHyphenIndices() const
310 return mModel->GetHyphenIndices();
313 Length ViewModel::GetHyphensCount() const
315 return mModel->GetHyphensCount();
318 const float ViewModel::GetCharacterSpacing() const
320 return mModel->GetCharacterSpacing();
323 const Character* ViewModel::GetTextBuffer() const
325 return mModel->GetTextBuffer();
328 const Vector<CharacterIndex>& ViewModel::GetGlyphsToCharacters() const
330 return mModel->GetGlyphsToCharacters();
333 void ViewModel::ElideGlyphs()
335 mIsTextElided = false;
336 mStartIndexOfElidedGlyphs = mFirstMiddleIndexOfElidedGlyphs = mSecondMiddleIndexOfElidedGlyphs = 0;
337 mEndIndexOfElidedGlyphs = mModel->GetNumberOfGlyphs() - 1u;
339 auto ellipsisPosition = GetEllipsisPosition();
340 auto characterSpacing = GetCharacterSpacing();
341 const Character* textBuffer = GetTextBuffer();
342 const Vector<CharacterIndex>& glyphToCharacterMap = GetGlyphsToCharacters();
343 const CharacterIndex* glyphToCharacterMapBuffer = glyphToCharacterMap.Begin();
344 float calculatedAdvance = 0.f;
346 if(IsTextElideEnabled())
348 const Length numberOfLines = mModel->GetNumberOfLines();
349 if(0u != numberOfLines)
351 const LineRun* const lines = mModel->GetLines();
353 //Get line of ellipsis
354 const LineRun* ellipsisLine = nullptr;
355 const LineRun* ellipsisNextLine = nullptr;
357 for(Length lineIndex = 0; lineIndex < numberOfLines; lineIndex++)
359 const LineRun* line = (lines + lineIndex);
363 if(lineIndex < numberOfLines - 1u)
365 ellipsisNextLine = (lines + lineIndex + 1u);
371 // Check if there is a line contains Ellipsis.
372 // Then find total number of glyphs and total number of laid out glyphs.
373 // Check where to set Ellipsis glyph in line.
374 // Determine index of Ellipsis glyph and how many glyphs should be replaced by Ellipsis glyph, according to width of Ellipsis glyph.
375 if(ellipsisLine != nullptr)
377 // Total number of glyphs.
378 const Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
379 // Total number of laid out glyphs.
380 Length numberOfActualLaidOutGlyphs = 0u;
382 // Accumulate laid out glyphs for each line to find total number of laid out glyphs.
383 for(Length lineIndex = 0u; lineIndex < numberOfLines; lineIndex++)
385 numberOfActualLaidOutGlyphs += lines[lineIndex].glyphRun.numberOfGlyphs + lines[lineIndex].glyphRunSecondHalf.numberOfGlyphs;
388 // Make sure there are laid out glyphs.
389 if(0u != numberOfActualLaidOutGlyphs)
391 // There are elided glyphs.
392 mIsTextElided = true;
393 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
395 // Retrieve the whole glyphs and their positions.
396 const GlyphInfo* const glyphs = mModel->GetGlyphs();
397 const Vector2* const positions = mModel->GetLayout();
399 // Copy the glyphs to be elided.
400 mElidedGlyphs.Resize(numberOfGlyphs);
401 mElidedLayout.Resize(numberOfGlyphs);
402 GlyphInfo* elidedGlyphsBuffer = mElidedGlyphs.Begin();
403 Vector2* elidedPositionsBuffer = mElidedLayout.Begin();
405 memcpy(elidedGlyphsBuffer, glyphs, numberOfGlyphs * sizeof(GlyphInfo));
406 memcpy(elidedPositionsBuffer, positions, numberOfGlyphs * sizeof(Vector2));
408 const Size& controlSize = mModel->GetControlSize();
410 // Set index where to set Ellipsis according to the selected position of Ellipsis.
411 // Start with this index to replace its glyph by Ellipsis, if the width is not enough, then remove more glyphs.
412 GlyphIndex startIndexOfEllipsis = 0u;
413 if(ellipsisPosition == DevelText::EllipsisPosition::START)
415 // It's the fisrt glyph in line.
416 startIndexOfEllipsis = ellipsisLine->glyphRun.glyphIndex;
418 else if(ellipsisPosition == DevelText::EllipsisPosition::MIDDLE)
420 // It's the second middle of the line in case the line split to two halves.
421 // Otherwise it's It's the last glyph in line (line before all removed lines).
422 startIndexOfEllipsis = ellipsisLine->isSplitToTwoHalves ? (ellipsisLine->glyphRunSecondHalf.glyphIndex) : (ellipsisLine->glyphRun.glyphIndex + ellipsisLine->glyphRun.numberOfGlyphs - 1u);
424 else // DevelText::EllipsisPosition::END
426 // It's the last glyph in line.
427 startIndexOfEllipsis = ellipsisLine->glyphRun.glyphIndex + ellipsisLine->glyphRun.numberOfGlyphs - 1u;
430 // When the hight is not enough then show one glyph and that should be the first laid out glyph.
431 if((1u == numberOfLines) &&
432 (ellipsisLine->ascender - ellipsisLine->descender > controlSize.height))
434 // Replace the first glyph with ellipsis glyph
435 auto indexOfFirstGlyph = (ellipsisPosition == DevelText::EllipsisPosition::START) ? startIndexOfEllipsis : 0u;
437 // Regardless where the location of ellipsis,in-case the hight of line is greater than control's height
438 // then replace the first glyph with ellipsis glyph.
440 // Get the first glyph which is going to be replaced and the ellipsis glyph.
441 GlyphInfo& glyphToRemove = *(elidedGlyphsBuffer + indexOfFirstGlyph);
442 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph(fontClient.GetPointSize(glyphToRemove.fontId));
444 // Change the 'x' and 'y' position of the ellipsis glyph.
445 Vector2& position = *(elidedPositionsBuffer + indexOfFirstGlyph);
447 position.x = ellipsisGlyph.xBearing;
448 position.y = -ellipsisLine->ascender + controlSize.height - ellipsisGlyph.yBearing;
450 // Replace the glyph by the ellipsis glyph and resize the buffers.
451 glyphToRemove = ellipsisGlyph;
453 mElidedGlyphs.Resize(1u);
454 mElidedLayout.Resize(1u);
456 mEndIndexOfElidedGlyphs = mStartIndexOfElidedGlyphs = mFirstMiddleIndexOfElidedGlyphs = mSecondMiddleIndexOfElidedGlyphs = indexOfFirstGlyph;
461 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
462 float firstPenX = 0.f; // Used if rtl text is elided.
464 bool firstPenSet = false;
466 // Add the ellipsis glyph.
467 bool inserted = false;
468 float removedGlypsWidth = 0.f;
469 Length numberOfRemovedGlyphs = 0u;
470 GlyphIndex indexOfEllipsis = startIndexOfEllipsis;
472 // Tail Mode: start by the end of line.
473 bool isTailMode = (ellipsisPosition == DevelText::EllipsisPosition::END) ||
474 (ellipsisPosition == DevelText::EllipsisPosition::MIDDLE && numberOfLines != 1u);
476 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
479 const GlyphInfo& glyphToRemove = *(elidedGlyphsBuffer + indexOfEllipsis);
481 if(0u != glyphToRemove.fontId)
483 // i.e. The font id of the glyph shaped from the '\n' character is zero.
485 // Need to reshape the glyph as the font may be different in size.
486 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph(fontClient.GetPointSize(glyphToRemove.fontId));
488 if(!firstPenSet || EqualsZero(glyphToRemove.advance))
490 const Vector2& position = *(elidedPositionsBuffer + indexOfEllipsis);
492 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
493 penY = position.y + glyphToRemove.yBearing;
495 // Calculates the first penX which will be used if rtl text is elided.
496 firstPenX = position.x - glyphToRemove.xBearing;
497 if(firstPenX < -ellipsisGlyph.xBearing)
499 // Avoids to exceed the bounding box when rtl text is elided.
500 firstPenX = -ellipsisGlyph.xBearing;
503 removedGlypsWidth = -ellipsisGlyph.xBearing;
508 calculatedAdvance = GetCalculatedAdvance(*(textBuffer + (*(glyphToCharacterMapBuffer + indexOfEllipsis))), characterSpacing, glyphToRemove.advance);
509 removedGlypsWidth += std::min(calculatedAdvance, (glyphToRemove.xBearing + glyphToRemove.width));
511 // Calculate the width of the ellipsis glyph and check if it fits.
512 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
514 // If it is the last glyph to remove, add the ellipsis glyph without checking its width.
515 if((ellipsisGlyphWidth < removedGlypsWidth) || (isTailMode ? (indexOfEllipsis == 0u) : (indexOfEllipsis == numberOfGlyphs - 1u)))
517 GlyphInfo& glyphInfo = *(elidedGlyphsBuffer + indexOfEllipsis);
518 Vector2& position = *(elidedPositionsBuffer + indexOfEllipsis);
519 position.x -= (0.f > glyphInfo.xBearing) ? glyphInfo.xBearing : 0.f;
521 // Replace the glyph by the ellipsis glyph.
522 glyphInfo = ellipsisGlyph;
524 // Change the 'x' and 'y' position of the ellipsis glyph.
525 if(position.x > firstPenX)
527 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
530 position.x += ellipsisGlyph.xBearing;
531 position.y = penY - ellipsisGlyph.yBearing;
539 if(!isTailMode && indexOfEllipsis < numberOfGlyphs - 1u)
541 // Tail Mode: remove glyphs from startIndexOfEllipsis then decrement indexOfEllipsis, until arrive to index zero.
544 else if(isTailMode && indexOfEllipsis > 0u)
546 // Not Tail Mode: remove glyphs from startIndexOfEllipsis then increase indexOfEllipsis, until arrive to last index (numberOfGlyphs - 1u).
551 // No space for the ellipsis.
554 ++numberOfRemovedGlyphs;
556 } // while( !inserted )
558 //Reduce size, shift glyphs and start from ellipsis glyph
559 Length numberOfElidedGlyphs = numberOfActualLaidOutGlyphs - numberOfRemovedGlyphs;
560 mElidedGlyphs.Resize(numberOfElidedGlyphs);
561 mElidedLayout.Resize(numberOfElidedGlyphs);
563 if(ellipsisPosition == DevelText::EllipsisPosition::START)
565 // 'Shifts' glyphs after ellipsis glyph and 'Removes' before ellipsis glyph
566 memcpy(elidedGlyphsBuffer, elidedGlyphsBuffer + indexOfEllipsis, numberOfElidedGlyphs * sizeof(GlyphInfo));
567 memcpy(elidedPositionsBuffer, elidedPositionsBuffer + indexOfEllipsis, numberOfElidedGlyphs * sizeof(Vector2));
569 mStartIndexOfElidedGlyphs = mFirstMiddleIndexOfElidedGlyphs = mSecondMiddleIndexOfElidedGlyphs = indexOfEllipsis;
571 else if(ellipsisPosition == DevelText::EllipsisPosition::MIDDLE)
573 // 'Shifts and connects' glyphs before and after ellipsis glyph and 'Removes' in-between.
574 bool isOnlySecondHalf = false;
578 mFirstMiddleIndexOfElidedGlyphs = indexOfEllipsis;
579 if(ellipsisNextLine != nullptr)
581 mSecondMiddleIndexOfElidedGlyphs = ellipsisNextLine->glyphRun.glyphIndex;
585 mEndIndexOfElidedGlyphs = mSecondMiddleIndexOfElidedGlyphs = mFirstMiddleIndexOfElidedGlyphs;
590 mFirstMiddleIndexOfElidedGlyphs = (ellipsisLine->glyphRun.numberOfGlyphs > 0u) ? (ellipsisLine->glyphRun.glyphIndex + ellipsisLine->glyphRun.numberOfGlyphs - 1u) : (ellipsisLine->glyphRun.glyphIndex);
591 mSecondMiddleIndexOfElidedGlyphs = indexOfEllipsis;
592 isOnlySecondHalf = ellipsisLine->glyphRun.numberOfGlyphs == 0u && ellipsisLine->glyphRunSecondHalf.numberOfGlyphs > 0u;
597 Length numberOfSecondHalfGlyphs = numberOfElidedGlyphs - mFirstMiddleIndexOfElidedGlyphs;
599 //Copy elided glyphs after the ellipsis glyph.
600 memcpy(elidedGlyphsBuffer + mFirstMiddleIndexOfElidedGlyphs, elidedGlyphsBuffer + mSecondMiddleIndexOfElidedGlyphs, numberOfSecondHalfGlyphs * sizeof(GlyphInfo));
601 memcpy(elidedPositionsBuffer + mFirstMiddleIndexOfElidedGlyphs, elidedPositionsBuffer + mSecondMiddleIndexOfElidedGlyphs, numberOfSecondHalfGlyphs * sizeof(Vector2));
605 Length numberOfSecondHalfGlyphs = numberOfElidedGlyphs - mFirstMiddleIndexOfElidedGlyphs + 1u;
607 //Copy elided glyphs after the ellipsis glyph.
608 memcpy(elidedGlyphsBuffer + mFirstMiddleIndexOfElidedGlyphs + 1u, elidedGlyphsBuffer + mSecondMiddleIndexOfElidedGlyphs, numberOfSecondHalfGlyphs * sizeof(GlyphInfo));
609 memcpy(elidedPositionsBuffer + mFirstMiddleIndexOfElidedGlyphs + 1u, elidedPositionsBuffer + mSecondMiddleIndexOfElidedGlyphs, numberOfSecondHalfGlyphs * sizeof(Vector2));
612 else // DevelText::EllipsisPosition::END
614 // 'Removes' all the glyphs after the ellipsis glyph.
615 mEndIndexOfElidedGlyphs = indexOfEllipsis;
623 float ViewModel::GetStrikethroughHeight() const
625 return mModel->GetStrikethroughHeight();
628 const Vector4& ViewModel::GetStrikethroughColor() const
630 return mModel->GetStrikethroughColor();
633 bool ViewModel::IsStrikethroughEnabled() const
635 return mModel->IsStrikethroughEnabled();
638 Length ViewModel::GetNumberOfStrikethroughRuns() const
640 return mModel->GetNumberOfStrikethroughRuns();
643 void ViewModel::GetStrikethroughRuns(StrikethroughGlyphRun* strikethroughRuns, StrikethroughRunIndex index, Length numberOfRuns) const
645 mModel->GetStrikethroughRuns(strikethroughRuns, index, numberOfRuns);
650 } // namespace Toolkit