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)
42 ViewModel::~ViewModel()
46 const Size& ViewModel::GetControlSize() const
48 return mModel->GetControlSize();
51 const Size& ViewModel::GetLayoutSize() const
53 return mModel->GetLayoutSize();
56 const Vector2& ViewModel::GetScrollPosition() const
58 return mModel->GetScrollPosition();
61 HorizontalAlignment::Type ViewModel::GetHorizontalAlignment() const
63 return mModel->GetHorizontalAlignment();
66 VerticalAlignment::Type ViewModel::GetVerticalAlignment() const
68 return mModel->GetVerticalAlignment();
71 DevelText::VerticalLineAlignment::Type ViewModel::GetVerticalLineAlignment() const
73 return mModel->GetVerticalLineAlignment();
76 bool ViewModel::IsTextElideEnabled() const
78 return mModel->IsTextElideEnabled();
81 Length ViewModel::GetNumberOfLines() const
83 return mModel->GetNumberOfLines();
86 const LineRun* const ViewModel::GetLines() const
88 return mModel->GetLines();
91 Length ViewModel::GetNumberOfScripts() const
93 return mModel->GetNumberOfScripts();
96 const ScriptRun* const ViewModel::GetScriptRuns() const
98 return mModel->GetScriptRuns();
101 Length ViewModel::GetNumberOfGlyphs() const
103 if(mIsTextElided && mModel->IsTextElideEnabled())
105 return mElidedGlyphs.Count();
109 return mModel->GetNumberOfGlyphs();
115 const GlyphInfo* const ViewModel::GetGlyphs() const
117 if(mIsTextElided && mModel->IsTextElideEnabled())
119 return mElidedGlyphs.Begin();
123 return mModel->GetGlyphs();
129 const Vector2* const ViewModel::GetLayout() const
131 if(mIsTextElided && mModel->IsTextElideEnabled())
133 return mElidedLayout.Begin();
137 return mModel->GetLayout();
143 const Vector4* const ViewModel::GetColors() const
145 return mModel->GetColors();
148 const ColorIndex* const ViewModel::GetColorIndices() const
150 return mModel->GetColorIndices();
153 const Vector4* const ViewModel::GetBackgroundColors() const
155 return mModel->GetBackgroundColors();
158 const ColorIndex* const ViewModel::GetBackgroundColorIndices() const
160 return mModel->GetBackgroundColorIndices();
163 bool const ViewModel::IsMarkupBackgroundColorSet() const
165 return mModel->IsMarkupBackgroundColorSet();
168 const Vector4& ViewModel::GetDefaultColor() const
170 return mModel->GetDefaultColor();
173 const Vector2& ViewModel::GetShadowOffset() const
175 return mModel->GetShadowOffset();
178 const Vector4& ViewModel::GetShadowColor() const
180 return mModel->GetShadowColor();
183 const float& ViewModel::GetShadowBlurRadius() const
185 return mModel->GetShadowBlurRadius();
188 const Vector4& ViewModel::GetUnderlineColor() const
190 return mModel->GetUnderlineColor();
193 bool ViewModel::IsUnderlineEnabled() const
195 return mModel->IsUnderlineEnabled();
198 float ViewModel::GetUnderlineHeight() const
200 return mModel->GetUnderlineHeight();
203 Length ViewModel::GetNumberOfUnderlineRuns() const
205 return mModel->GetNumberOfUnderlineRuns();
208 void ViewModel::GetUnderlineRuns(GlyphRun* underlineRuns, UnderlineRunIndex index, Length numberOfRuns) const
210 mModel->GetUnderlineRuns(underlineRuns, index, numberOfRuns);
213 const Vector4& ViewModel::GetOutlineColor() const
215 return mModel->GetOutlineColor();
218 uint16_t ViewModel::GetOutlineWidth() const
220 return mModel->GetOutlineWidth();
223 const Vector4& ViewModel::GetBackgroundColor() const
225 return mModel->GetBackgroundColor();
228 bool ViewModel::IsBackgroundEnabled() const
230 return mModel->IsBackgroundEnabled();
233 bool ViewModel::IsMarkupProcessorEnabled() const
235 return mModel->IsMarkupProcessorEnabled();
238 const GlyphInfo* ViewModel::GetHyphens() const
240 return mModel->GetHyphens();
243 const Length* ViewModel::GetHyphenIndices() const
245 return mModel->GetHyphenIndices();
248 Length ViewModel::GetHyphensCount() const
250 return mModel->GetHyphensCount();
253 void ViewModel::ElideGlyphs()
255 mIsTextElided = false;
257 if(mModel->IsTextElideEnabled())
259 const Length numberOfLines = mModel->GetNumberOfLines();
260 if(0u != numberOfLines)
262 const LineRun* const lines = mModel->GetLines();
264 const LineRun& lastLine = *(lines + (numberOfLines - 1u));
265 const Length numberOfLaidOutGlyphs = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
267 if(lastLine.ellipsis && (0u != numberOfLaidOutGlyphs))
269 mIsTextElided = true;
270 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
272 const GlyphInfo* const glyphs = mModel->GetGlyphs();
273 const Vector2* const positions = mModel->GetLayout();
275 // Copy the glyphs to be elided.
276 mElidedGlyphs.Resize(numberOfLaidOutGlyphs);
277 mElidedLayout.Resize(numberOfLaidOutGlyphs);
279 GlyphInfo* elidedGlyphsBuffer = mElidedGlyphs.Begin();
280 Vector2* elidedPositionsBuffer = mElidedLayout.Begin();
282 memcpy(elidedGlyphsBuffer, glyphs, numberOfLaidOutGlyphs * sizeof(GlyphInfo));
283 memcpy(elidedPositionsBuffer, positions, numberOfLaidOutGlyphs * sizeof(Vector2));
285 const Size& controlSize = mModel->GetControlSize();
287 if((1u == numberOfLines) &&
288 (lastLine.ascender - lastLine.descender > controlSize.height))
290 // Get the first glyph which is going to be replaced and the ellipsis glyph.
291 GlyphInfo& glyphToRemove = *elidedGlyphsBuffer;
292 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph(fontClient.GetPointSize(glyphToRemove.fontId));
294 // Change the 'x' and 'y' position of the ellipsis glyph.
295 Vector2& position = *elidedPositionsBuffer;
297 position.x = ellipsisGlyph.xBearing;
298 position.y = -lastLine.ascender + controlSize.height - ellipsisGlyph.yBearing;
300 // Replace the glyph by the ellipsis glyph and resize the buffers.
301 glyphToRemove = ellipsisGlyph;
303 mElidedGlyphs.Resize(1u);
304 mElidedLayout.Resize(1u);
309 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
310 float firstPenX = 0.f; // Used if rtl text is elided.
312 bool firstPenSet = false;
314 // Add the ellipsis glyph.
315 bool inserted = false;
316 float removedGlypsWidth = 0.f;
317 Length numberOfRemovedGlyphs = 0u;
318 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
320 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
323 const GlyphInfo& glyphToRemove = *(elidedGlyphsBuffer + index);
325 if(0u != glyphToRemove.fontId)
327 // i.e. The font id of the glyph shaped from the '\n' character is zero.
329 // Need to reshape the glyph as the font may be different in size.
330 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph(fontClient.GetPointSize(glyphToRemove.fontId));
332 if(!firstPenSet || EqualsZero(glyphToRemove.advance))
334 const Vector2& position = *(elidedPositionsBuffer + index);
336 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
337 penY = position.y + glyphToRemove.yBearing;
339 // Calculates the first penX which will be used if rtl text is elided.
340 firstPenX = position.x - glyphToRemove.xBearing;
341 if(firstPenX < -ellipsisGlyph.xBearing)
343 // Avoids to exceed the bounding box when rtl text is elided.
344 firstPenX = -ellipsisGlyph.xBearing;
347 removedGlypsWidth = -ellipsisGlyph.xBearing;
349 if(!EqualsZero(firstPenX))
355 removedGlypsWidth += std::min(glyphToRemove.advance, (glyphToRemove.xBearing + glyphToRemove.width));
357 // Calculate the width of the ellipsis glyph and check if it fits.
358 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
360 // If it is the last glyph to remove, add the ellipsis glyph without checking its width.
361 if((ellipsisGlyphWidth < removedGlypsWidth) || (index == 0u))
363 GlyphInfo& glyphInfo = *(elidedGlyphsBuffer + index);
364 Vector2& position = *(elidedPositionsBuffer + index);
365 position.x -= (0.f > glyphInfo.xBearing) ? glyphInfo.xBearing : 0.f;
367 // Replace the glyph by the ellipsis glyph.
368 glyphInfo = ellipsisGlyph;
370 // Change the 'x' and 'y' position of the ellipsis glyph.
371 if(position.x > firstPenX)
373 position.x = firstPenX;
374 if(ellipsisGlyphWidth < removedGlypsWidth)
376 position.x += removedGlypsWidth - ellipsisGlyphWidth;
380 position.x += ellipsisGlyph.xBearing;
381 position.y = penY - ellipsisGlyph.yBearing;
395 // No space for the ellipsis.
398 ++numberOfRemovedGlyphs;
400 } // while( !inserted )
402 // 'Removes' all the glyphs after the ellipsis glyph.
403 const Length numberOfGlyphs = numberOfLaidOutGlyphs - numberOfRemovedGlyphs;
404 mElidedGlyphs.Resize(numberOfGlyphs);
405 mElidedLayout.Resize(numberOfGlyphs);
413 } // namespace Toolkit