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& ViewModel::GetDefaultColor() const
158 return mModel->GetDefaultColor();
161 const Vector2& ViewModel::GetShadowOffset() const
163 return mModel->GetShadowOffset();
166 const Vector4& ViewModel::GetShadowColor() const
168 return mModel->GetShadowColor();
171 const float& ViewModel::GetShadowBlurRadius() const
173 return mModel->GetShadowBlurRadius();
176 const Vector4& ViewModel::GetUnderlineColor() const
178 return mModel->GetUnderlineColor();
181 bool ViewModel::IsUnderlineEnabled() const
183 return mModel->IsUnderlineEnabled();
186 float ViewModel::GetUnderlineHeight() const
188 return mModel->GetUnderlineHeight();
191 Length ViewModel::GetNumberOfUnderlineRuns() const
193 return mModel->GetNumberOfUnderlineRuns();
196 void ViewModel::GetUnderlineRuns( GlyphRun* underlineRuns, UnderlineRunIndex index, Length numberOfRuns ) const
198 mModel->GetUnderlineRuns( underlineRuns, index, numberOfRuns );
201 const Vector4& ViewModel::GetOutlineColor() const
203 return mModel->GetOutlineColor();
206 uint16_t ViewModel::GetOutlineWidth() const
208 return mModel->GetOutlineWidth();
211 const Vector4& ViewModel::GetBackgroundColor() const
213 return mModel->GetBackgroundColor();
216 bool ViewModel::IsBackgroundEnabled() const
218 return mModel->IsBackgroundEnabled();
221 void ViewModel::ElideGlyphs()
223 mIsTextElided = false;
225 if( mModel->IsTextElideEnabled() )
227 const Length numberOfLines = mModel->GetNumberOfLines();
228 if( 0u != numberOfLines )
230 const LineRun* const lines = mModel->GetLines();
232 const LineRun& lastLine = *( lines + ( numberOfLines - 1u ) );
233 const Length numberOfLaidOutGlyphs = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
235 if( lastLine.ellipsis && ( 0u != numberOfLaidOutGlyphs ) )
237 mIsTextElided = true;
238 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
240 const GlyphInfo* const glyphs = mModel->GetGlyphs();
241 const Vector2* const positions = mModel->GetLayout();
243 // Copy the glyphs to be elided.
244 mElidedGlyphs.Resize( numberOfLaidOutGlyphs );
245 mElidedLayout.Resize( numberOfLaidOutGlyphs );
247 GlyphInfo* elidedGlyphsBuffer = mElidedGlyphs.Begin();
248 Vector2* elidedPositionsBuffer = mElidedLayout.Begin();
250 memcpy( elidedGlyphsBuffer, glyphs, numberOfLaidOutGlyphs * sizeof( GlyphInfo ) );
251 memcpy( elidedPositionsBuffer, positions, numberOfLaidOutGlyphs * sizeof( Vector2 ) );
253 const Size& controlSize = mModel->GetControlSize();
255 if( ( 1u == numberOfLines ) &&
256 ( lastLine.ascender - lastLine.descender > controlSize.height ) )
258 // Get the first glyph which is going to be replaced and the ellipsis glyph.
259 GlyphInfo& glyphToRemove = *elidedGlyphsBuffer;
260 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
262 // Change the 'x' and 'y' position of the ellipsis glyph.
263 Vector2& position = *elidedPositionsBuffer;
265 position.x = ellipsisGlyph.xBearing;
266 position.y = -lastLine.ascender + controlSize.height - ellipsisGlyph.yBearing;
268 // Replace the glyph by the ellipsis glyph and resize the buffers.
269 glyphToRemove = ellipsisGlyph;
271 mElidedGlyphs.Resize( 1u );
272 mElidedLayout.Resize( 1u );
277 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
278 float firstPenX = 0.f; // Used if rtl text is elided.
280 bool firstPenSet = false;
282 // Add the ellipsis glyph.
283 bool inserted = false;
284 float removedGlypsWidth = 0.f;
285 Length numberOfRemovedGlyphs = 0u;
286 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
288 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
291 const GlyphInfo& glyphToRemove = *( elidedGlyphsBuffer + index );
293 if( 0u != glyphToRemove.fontId )
295 // i.e. The font id of the glyph shaped from the '\n' character is zero.
297 // Need to reshape the glyph as the font may be different in size.
298 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
302 const Vector2& position = *( elidedPositionsBuffer + index );
304 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
305 penY = position.y + glyphToRemove.yBearing;
307 // Calculates the first penX which will be used if rtl text is elided.
308 firstPenX = position.x - glyphToRemove.xBearing;
309 if( firstPenX < -ellipsisGlyph.xBearing )
311 // Avoids to exceed the bounding box when rtl text is elided.
312 firstPenX = -ellipsisGlyph.xBearing;
315 removedGlypsWidth = -ellipsisGlyph.xBearing;
320 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
322 // Calculate the width of the ellipsis glyph and check if it fits.
323 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
325 if( ellipsisGlyphWidth < removedGlypsWidth )
327 GlyphInfo& glyphInfo = *( elidedGlyphsBuffer + index );
328 Vector2& position = *( elidedPositionsBuffer + index );
329 position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
331 // Replace the glyph by the ellipsis glyph.
332 glyphInfo = ellipsisGlyph;
334 // Change the 'x' and 'y' position of the ellipsis glyph.
335 position.x += ellipsisGlyph.xBearing;
336 position.y = penY - ellipsisGlyph.yBearing;
350 // No space for the ellipsis.
353 ++numberOfRemovedGlyphs;
355 } // while( !inserted )
357 // 'Removes' all the glyphs after the ellipsis glyph.
358 const Length numberOfGlyphs = numberOfLaidOutGlyphs - numberOfRemovedGlyphs;
359 mElidedGlyphs.Resize( numberOfGlyphs );
360 mElidedLayout.Resize( numberOfGlyphs );
368 } // namespace Toolkit