2 * Copyright (c) 2019 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>
23 #include <dali/devel-api/text-abstraction/font-client.h>
26 #include <dali-toolkit/internal/text/line-run.h>
37 ViewModel::ViewModel( const ModelInterface* const model )
41 mIsTextElided( false )
45 ViewModel::~ViewModel()
49 const Size& ViewModel::GetControlSize() const
51 return mModel->GetControlSize();
54 const Size& ViewModel::GetLayoutSize() const
56 return mModel->GetLayoutSize();
59 const Vector2& ViewModel::GetScrollPosition() const
61 return mModel->GetScrollPosition();
64 HorizontalAlignment::Type ViewModel::GetHorizontalAlignment() const
66 return mModel->GetHorizontalAlignment();
69 VerticalAlignment::Type ViewModel::GetVerticalAlignment() const
71 return mModel->GetVerticalAlignment();
74 DevelText::VerticalLineAlignment::Type ViewModel::GetVerticalLineAlignment() const
76 return mModel->GetVerticalLineAlignment();
79 bool ViewModel::IsTextElideEnabled() const
81 return mModel->IsTextElideEnabled();
84 Length ViewModel::GetNumberOfLines() const
86 return mModel->GetNumberOfLines();
89 const LineRun* const ViewModel::GetLines() const
91 return mModel->GetLines();
94 Length ViewModel::GetNumberOfScripts() const
96 return mModel->GetNumberOfScripts();
99 const ScriptRun* const ViewModel::GetScriptRuns() const
101 return mModel->GetScriptRuns();
104 Length ViewModel::GetNumberOfGlyphs() const
106 if( mIsTextElided && mModel->IsTextElideEnabled() )
108 return mElidedGlyphs.Count();
112 return mModel->GetNumberOfGlyphs();
118 const GlyphInfo* const ViewModel::GetGlyphs() const
120 if( mIsTextElided && mModel->IsTextElideEnabled() )
122 return mElidedGlyphs.Begin();
126 return mModel->GetGlyphs();
132 const Vector2* const ViewModel::GetLayout() const
134 if( mIsTextElided && mModel->IsTextElideEnabled() )
136 return mElidedLayout.Begin();
140 return mModel->GetLayout();
146 const Vector4* const ViewModel::GetColors() const
148 return mModel->GetColors();
151 const ColorIndex* const ViewModel::GetColorIndices() const
153 return mModel->GetColorIndices();
156 const Vector4* const ViewModel::GetBackgroundColors() const
158 return mModel->GetBackgroundColors();
161 const ColorIndex* const ViewModel::GetBackgroundColorIndices() const
163 return mModel->GetBackgroundColorIndices();
166 const Vector4& ViewModel::GetDefaultColor() const
168 return mModel->GetDefaultColor();
171 const Vector2& ViewModel::GetShadowOffset() const
173 return mModel->GetShadowOffset();
176 const Vector4& ViewModel::GetShadowColor() const
178 return mModel->GetShadowColor();
181 const float& ViewModel::GetShadowBlurRadius() const
183 return mModel->GetShadowBlurRadius();
186 const Vector4& ViewModel::GetUnderlineColor() const
188 return mModel->GetUnderlineColor();
191 bool ViewModel::IsUnderlineEnabled() const
193 return mModel->IsUnderlineEnabled();
196 float ViewModel::GetUnderlineHeight() const
198 return mModel->GetUnderlineHeight();
201 Length ViewModel::GetNumberOfUnderlineRuns() const
203 return mModel->GetNumberOfUnderlineRuns();
206 void ViewModel::GetUnderlineRuns( GlyphRun* underlineRuns, UnderlineRunIndex index, Length numberOfRuns ) const
208 mModel->GetUnderlineRuns( underlineRuns, index, numberOfRuns );
211 const Vector4& ViewModel::GetOutlineColor() const
213 return mModel->GetOutlineColor();
216 uint16_t ViewModel::GetOutlineWidth() const
218 return mModel->GetOutlineWidth();
221 const Vector4& ViewModel::GetBackgroundColor() const
223 return mModel->GetBackgroundColor();
226 bool ViewModel::IsBackgroundEnabled() const
228 return mModel->IsBackgroundEnabled();
231 void ViewModel::ElideGlyphs()
233 mIsTextElided = false;
235 if( mModel->IsTextElideEnabled() )
237 const Length numberOfLines = mModel->GetNumberOfLines();
238 if( 0u != numberOfLines )
240 const LineRun* const lines = mModel->GetLines();
242 const LineRun& lastLine = *( lines + ( numberOfLines - 1u ) );
243 const Length numberOfLaidOutGlyphs = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
245 if( lastLine.ellipsis && ( 0u != numberOfLaidOutGlyphs ) )
247 mIsTextElided = true;
248 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
250 const GlyphInfo* const glyphs = mModel->GetGlyphs();
251 const Vector2* const positions = mModel->GetLayout();
253 // Copy the glyphs to be elided.
254 mElidedGlyphs.Resize( numberOfLaidOutGlyphs );
255 mElidedLayout.Resize( numberOfLaidOutGlyphs );
257 GlyphInfo* elidedGlyphsBuffer = mElidedGlyphs.Begin();
258 Vector2* elidedPositionsBuffer = mElidedLayout.Begin();
260 memcpy( elidedGlyphsBuffer, glyphs, numberOfLaidOutGlyphs * sizeof( GlyphInfo ) );
261 memcpy( elidedPositionsBuffer, positions, numberOfLaidOutGlyphs * sizeof( Vector2 ) );
263 const Size& controlSize = mModel->GetControlSize();
265 if( ( 1u == numberOfLines ) &&
266 ( lastLine.ascender - lastLine.descender > controlSize.height ) )
268 // Get the first glyph which is going to be replaced and the ellipsis glyph.
269 GlyphInfo& glyphToRemove = *elidedGlyphsBuffer;
270 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
272 // Change the 'x' and 'y' position of the ellipsis glyph.
273 Vector2& position = *elidedPositionsBuffer;
275 position.x = ellipsisGlyph.xBearing;
276 position.y = -lastLine.ascender + controlSize.height - ellipsisGlyph.yBearing;
278 // Replace the glyph by the ellipsis glyph and resize the buffers.
279 glyphToRemove = ellipsisGlyph;
281 mElidedGlyphs.Resize( 1u );
282 mElidedLayout.Resize( 1u );
287 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
288 float firstPenX = 0.f; // Used if rtl text is elided.
290 bool firstPenSet = false;
292 // Add the ellipsis glyph.
293 bool inserted = false;
294 float removedGlypsWidth = 0.f;
295 Length numberOfRemovedGlyphs = 0u;
296 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
298 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
301 const GlyphInfo& glyphToRemove = *( elidedGlyphsBuffer + index );
303 if( 0u != glyphToRemove.fontId )
305 // i.e. The font id of the glyph shaped from the '\n' character is zero.
307 // Need to reshape the glyph as the font may be different in size.
308 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
310 if( !firstPenSet || EqualsZero( glyphToRemove.advance ) )
312 const Vector2& position = *( elidedPositionsBuffer + index );
314 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
315 penY = position.y + glyphToRemove.yBearing;
317 // Calculates the first penX which will be used if rtl text is elided.
318 firstPenX = position.x - glyphToRemove.xBearing;
319 if( firstPenX < -ellipsisGlyph.xBearing )
321 // Avoids to exceed the bounding box when rtl text is elided.
322 firstPenX = -ellipsisGlyph.xBearing;
325 removedGlypsWidth = -ellipsisGlyph.xBearing;
327 if( !EqualsZero( firstPenX ) )
333 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
335 // Calculate the width of the ellipsis glyph and check if it fits.
336 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
338 if( ellipsisGlyphWidth < removedGlypsWidth )
340 GlyphInfo& glyphInfo = *( elidedGlyphsBuffer + index );
341 Vector2& position = *( elidedPositionsBuffer + index );
342 position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
344 // Replace the glyph by the ellipsis glyph.
345 glyphInfo = ellipsisGlyph;
347 // Change the 'x' and 'y' position of the ellipsis glyph.
348 if( position.x > firstPenX )
350 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
353 position.x += ellipsisGlyph.xBearing;
354 position.y = penY - ellipsisGlyph.yBearing;
368 // No space for the ellipsis.
371 ++numberOfRemovedGlyphs;
373 } // while( !inserted )
375 // 'Removes' all the glyphs after the ellipsis glyph.
376 const Length numberOfGlyphs = numberOfLaidOutGlyphs - numberOfRemovedGlyphs;
377 mElidedGlyphs.Resize( numberOfGlyphs );
378 mElidedLayout.Resize( numberOfGlyphs );
386 } // namespace Toolkit