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>
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 Text::Underline::Type ViewModel::GetUnderlineType() const
254 return mModel->GetUnderlineType();
257 float ViewModel::GetDashedUnderlineWidth() const
259 return mModel->GetDashedUnderlineWidth();
262 float ViewModel::GetDashedUnderlineGap() const
264 return mModel->GetDashedUnderlineGap();
267 Length ViewModel::GetNumberOfUnderlineRuns() const
269 return mModel->GetNumberOfUnderlineRuns();
272 void ViewModel::GetUnderlineRuns(GlyphRun* underlineRuns, UnderlineRunIndex index, Length numberOfRuns) const
274 mModel->GetUnderlineRuns(underlineRuns, index, numberOfRuns);
277 const Vector4& ViewModel::GetOutlineColor() const
279 return mModel->GetOutlineColor();
282 uint16_t ViewModel::GetOutlineWidth() const
284 return mModel->GetOutlineWidth();
287 const Vector4& ViewModel::GetBackgroundColor() const
289 return mModel->GetBackgroundColor();
292 bool ViewModel::IsBackgroundEnabled() const
294 return mModel->IsBackgroundEnabled();
297 bool ViewModel::IsMarkupProcessorEnabled() const
299 return mModel->IsMarkupProcessorEnabled();
302 const GlyphInfo* ViewModel::GetHyphens() const
304 return mModel->GetHyphens();
307 const Length* ViewModel::GetHyphenIndices() const
309 return mModel->GetHyphenIndices();
312 Length ViewModel::GetHyphensCount() const
314 return mModel->GetHyphensCount();
317 void ViewModel::ElideGlyphs()
319 mIsTextElided = false;
320 mStartIndexOfElidedGlyphs = mFirstMiddleIndexOfElidedGlyphs = mSecondMiddleIndexOfElidedGlyphs = 0;
321 mEndIndexOfElidedGlyphs = mModel->GetNumberOfGlyphs() - 1u;
323 auto ellipsisPosition = GetEllipsisPosition();
325 if(IsTextElideEnabled())
327 const Length numberOfLines = mModel->GetNumberOfLines();
328 if(0u != numberOfLines)
330 const LineRun* const lines = mModel->GetLines();
332 //Get line of ellipsis
333 const LineRun* ellipsisLine = nullptr;
334 const LineRun* ellipsisNextLine = nullptr;
336 for(Length lineIndex = 0; lineIndex < numberOfLines; lineIndex++)
338 const LineRun* line = (lines + lineIndex);
342 if(lineIndex < numberOfLines - 1u)
344 ellipsisNextLine = (lines + lineIndex + 1u);
350 // Check if there is a line contains Ellipsis.
351 // Then find total number of glyphs and total number of laid out glyphs.
352 // Check where to set Ellipsis glyph in line.
353 // Determine index of Ellipsis glyph and how many glyphs should be replaced by Ellipsis glyph, according to width of Ellipsis glyph.
354 if(ellipsisLine != nullptr)
356 // Total number of glyphs.
357 const Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
358 // Total number of laid out glyphs.
359 Length numberOfActualLaidOutGlyphs = 0u;
361 // Accumulate laid out glyphs for each line to find total number of laid out glyphs.
362 for(Length lineIndex = 0u; lineIndex < numberOfLines; lineIndex++)
364 numberOfActualLaidOutGlyphs += lines[lineIndex].glyphRun.numberOfGlyphs + lines[lineIndex].glyphRunSecondHalf.numberOfGlyphs;
367 // Make sure there are laid out glyphs.
368 if(0u != numberOfActualLaidOutGlyphs)
370 // There are elided glyphs.
371 mIsTextElided = true;
372 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
374 // Retrieve the whole glyphs and their positions.
375 const GlyphInfo* const glyphs = mModel->GetGlyphs();
376 const Vector2* const positions = mModel->GetLayout();
378 // Copy the glyphs to be elided.
379 mElidedGlyphs.Resize(numberOfGlyphs);
380 mElidedLayout.Resize(numberOfGlyphs);
381 GlyphInfo* elidedGlyphsBuffer = mElidedGlyphs.Begin();
382 Vector2* elidedPositionsBuffer = mElidedLayout.Begin();
384 memcpy(elidedGlyphsBuffer, glyphs, numberOfGlyphs * sizeof(GlyphInfo));
385 memcpy(elidedPositionsBuffer, positions, numberOfGlyphs * sizeof(Vector2));
387 const Size& controlSize = mModel->GetControlSize();
389 // Set index where to set Ellipsis according to the selected position of Ellipsis.
390 // Start with this index to replace its glyph by Ellipsis, if the width is not enough, then remove more glyphs.
391 GlyphIndex startIndexOfEllipsis = 0u;
392 if(ellipsisPosition == DevelText::EllipsisPosition::START)
394 // It's the fisrt glyph in line.
395 startIndexOfEllipsis = ellipsisLine->glyphRun.glyphIndex;
397 else if(ellipsisPosition == DevelText::EllipsisPosition::MIDDLE)
399 // It's the second middle of the line in case the line split to two halves.
400 // Otherwise it's It's the last glyph in line (line before all removed lines).
401 startIndexOfEllipsis = ellipsisLine->isSplitToTwoHalves ? (ellipsisLine->glyphRunSecondHalf.glyphIndex) : (ellipsisLine->glyphRun.glyphIndex + ellipsisLine->glyphRun.numberOfGlyphs - 1u);
403 else // DevelText::EllipsisPosition::END
405 // It's the last glyph in line.
406 startIndexOfEllipsis = ellipsisLine->glyphRun.glyphIndex + ellipsisLine->glyphRun.numberOfGlyphs - 1u;
409 // When the hight is not enough then show one glyph and that should be the first laid out glyph.
410 if((1u == numberOfLines) &&
411 (ellipsisLine->ascender - ellipsisLine->descender > controlSize.height))
413 // Replace the first glyph with ellipsis glyph
414 auto indexOfFirstGlyph = (ellipsisPosition == DevelText::EllipsisPosition::START) ? startIndexOfEllipsis : 0u;
416 // Regardless where the location of ellipsis,in-case the hight of line is greater than control's height
417 // then replace the first glyph with ellipsis glyph.
419 // Get the first glyph which is going to be replaced and the ellipsis glyph.
420 GlyphInfo& glyphToRemove = *(elidedGlyphsBuffer + indexOfFirstGlyph);
421 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph(fontClient.GetPointSize(glyphToRemove.fontId));
423 // Change the 'x' and 'y' position of the ellipsis glyph.
424 Vector2& position = *(elidedPositionsBuffer + indexOfFirstGlyph);
426 position.x = ellipsisGlyph.xBearing;
427 position.y = -ellipsisLine->ascender + controlSize.height - ellipsisGlyph.yBearing;
429 // Replace the glyph by the ellipsis glyph and resize the buffers.
430 glyphToRemove = ellipsisGlyph;
432 mElidedGlyphs.Resize(1u);
433 mElidedLayout.Resize(1u);
435 mEndIndexOfElidedGlyphs = mStartIndexOfElidedGlyphs = mFirstMiddleIndexOfElidedGlyphs = mSecondMiddleIndexOfElidedGlyphs = indexOfFirstGlyph;
440 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
441 float firstPenX = 0.f; // Used if rtl text is elided.
443 bool firstPenSet = false;
445 // Add the ellipsis glyph.
446 bool inserted = false;
447 float removedGlypsWidth = 0.f;
448 Length numberOfRemovedGlyphs = 0u;
449 GlyphIndex indexOfEllipsis = startIndexOfEllipsis;
451 // Tail Mode: start by the end of line.
452 bool isTailMode = (ellipsisPosition == DevelText::EllipsisPosition::END) ||
453 (ellipsisPosition == DevelText::EllipsisPosition::MIDDLE && numberOfLines != 1u);
455 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
458 const GlyphInfo& glyphToRemove = *(elidedGlyphsBuffer + indexOfEllipsis);
460 if(0u != glyphToRemove.fontId)
462 // i.e. The font id of the glyph shaped from the '\n' character is zero.
464 // Need to reshape the glyph as the font may be different in size.
465 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph(fontClient.GetPointSize(glyphToRemove.fontId));
467 if(!firstPenSet || EqualsZero(glyphToRemove.advance))
469 const Vector2& position = *(elidedPositionsBuffer + indexOfEllipsis);
471 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
472 penY = position.y + glyphToRemove.yBearing;
474 // Calculates the first penX which will be used if rtl text is elided.
475 firstPenX = position.x - glyphToRemove.xBearing;
476 if(firstPenX < -ellipsisGlyph.xBearing)
478 // Avoids to exceed the bounding box when rtl text is elided.
479 firstPenX = -ellipsisGlyph.xBearing;
482 removedGlypsWidth = -ellipsisGlyph.xBearing;
487 removedGlypsWidth += std::min(glyphToRemove.advance, (glyphToRemove.xBearing + glyphToRemove.width));
489 // Calculate the width of the ellipsis glyph and check if it fits.
490 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
492 // If it is the last glyph to remove, add the ellipsis glyph without checking its width.
493 if((ellipsisGlyphWidth < removedGlypsWidth) || (isTailMode ? (indexOfEllipsis == 0u) : (indexOfEllipsis == numberOfGlyphs - 1u)))
495 GlyphInfo& glyphInfo = *(elidedGlyphsBuffer + indexOfEllipsis);
496 Vector2& position = *(elidedPositionsBuffer + indexOfEllipsis);
497 position.x -= (0.f > glyphInfo.xBearing) ? glyphInfo.xBearing : 0.f;
499 // Replace the glyph by the ellipsis glyph.
500 glyphInfo = ellipsisGlyph;
502 // Change the 'x' and 'y' position of the ellipsis glyph.
503 if(position.x > firstPenX)
505 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
508 position.x += ellipsisGlyph.xBearing;
509 position.y = penY - ellipsisGlyph.yBearing;
517 if(!isTailMode && indexOfEllipsis < numberOfGlyphs - 1u)
519 // Tail Mode: remove glyphs from startIndexOfEllipsis then decrement indexOfEllipsis, until arrive to index zero.
522 else if(isTailMode && indexOfEllipsis > 0u)
524 // Not Tail Mode: remove glyphs from startIndexOfEllipsis then increase indexOfEllipsis, until arrive to last index (numberOfGlyphs - 1u).
529 // No space for the ellipsis.
532 ++numberOfRemovedGlyphs;
534 } // while( !inserted )
536 //Reduce size, shift glyphs and start from ellipsis glyph
537 Length numberOfElidedGlyphs = numberOfActualLaidOutGlyphs - numberOfRemovedGlyphs;
538 mElidedGlyphs.Resize(numberOfElidedGlyphs);
539 mElidedLayout.Resize(numberOfElidedGlyphs);
541 if(ellipsisPosition == DevelText::EllipsisPosition::START)
543 // 'Shifts' glyphs after ellipsis glyph and 'Removes' before ellipsis glyph
544 memcpy(elidedGlyphsBuffer, elidedGlyphsBuffer + indexOfEllipsis, numberOfElidedGlyphs * sizeof(GlyphInfo));
545 memcpy(elidedPositionsBuffer, elidedPositionsBuffer + indexOfEllipsis, numberOfElidedGlyphs * sizeof(Vector2));
547 mStartIndexOfElidedGlyphs = mFirstMiddleIndexOfElidedGlyphs = mSecondMiddleIndexOfElidedGlyphs = indexOfEllipsis;
549 else if(ellipsisPosition == DevelText::EllipsisPosition::MIDDLE)
551 // 'Shifts and connects' glyphs before and after ellipsis glyph and 'Removes' in-between.
552 bool isOnlySecondHalf = false;
556 mFirstMiddleIndexOfElidedGlyphs = indexOfEllipsis;
557 if(ellipsisNextLine != nullptr)
559 mSecondMiddleIndexOfElidedGlyphs = ellipsisNextLine->glyphRun.glyphIndex;
563 mEndIndexOfElidedGlyphs = mSecondMiddleIndexOfElidedGlyphs = mFirstMiddleIndexOfElidedGlyphs;
568 mFirstMiddleIndexOfElidedGlyphs = (ellipsisLine->glyphRun.numberOfGlyphs > 0u) ? (ellipsisLine->glyphRun.glyphIndex + ellipsisLine->glyphRun.numberOfGlyphs - 1u) : (ellipsisLine->glyphRun.glyphIndex);
569 mSecondMiddleIndexOfElidedGlyphs = indexOfEllipsis;
570 isOnlySecondHalf = ellipsisLine->glyphRun.numberOfGlyphs == 0u && ellipsisLine->glyphRunSecondHalf.numberOfGlyphs > 0u;
575 Length numberOfSecondHalfGlyphs = numberOfElidedGlyphs - mFirstMiddleIndexOfElidedGlyphs;
577 //Copy elided glyphs after the ellipsis glyph.
578 memcpy(elidedGlyphsBuffer + mFirstMiddleIndexOfElidedGlyphs, elidedGlyphsBuffer + mSecondMiddleIndexOfElidedGlyphs, numberOfSecondHalfGlyphs * sizeof(GlyphInfo));
579 memcpy(elidedPositionsBuffer + mFirstMiddleIndexOfElidedGlyphs, elidedPositionsBuffer + mSecondMiddleIndexOfElidedGlyphs, numberOfSecondHalfGlyphs * sizeof(Vector2));
583 Length numberOfSecondHalfGlyphs = numberOfElidedGlyphs - mFirstMiddleIndexOfElidedGlyphs + 1u;
585 //Copy elided glyphs after the ellipsis glyph.
586 memcpy(elidedGlyphsBuffer + mFirstMiddleIndexOfElidedGlyphs + 1u, elidedGlyphsBuffer + mSecondMiddleIndexOfElidedGlyphs, numberOfSecondHalfGlyphs * sizeof(GlyphInfo));
587 memcpy(elidedPositionsBuffer + mFirstMiddleIndexOfElidedGlyphs + 1u, elidedPositionsBuffer + mSecondMiddleIndexOfElidedGlyphs, numberOfSecondHalfGlyphs * sizeof(Vector2));
590 else // DevelText::EllipsisPosition::END
592 // 'Removes' all the glyphs after the ellipsis glyph.
593 mEndIndexOfElidedGlyphs = indexOfEllipsis;
601 float ViewModel::GetStrikethroughHeight() const
603 return mModel->GetStrikethroughHeight();
606 const Vector4& ViewModel::GetStrikethroughColor() const
608 return mModel->GetStrikethroughColor();
611 bool ViewModel::IsStrikethroughEnabled() const
613 return mModel->IsStrikethroughEnabled();
616 Length ViewModel::GetNumberOfStrikethroughRuns() const
618 return mModel->GetNumberOfStrikethroughRuns();
621 void ViewModel::GetStrikethroughRuns(StrikethroughGlyphRun* strikethroughRuns, StrikethroughRunIndex index, Length numberOfRuns) const
623 mModel->GetStrikethroughRuns(strikethroughRuns, index, numberOfRuns);
628 } // namespace Toolkit