2 * Copyright (c) 2017 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 float ViewModel::GetOutlineWidth() const
208 return mModel->GetOutlineWidth();
211 void ViewModel::ElideGlyphs()
213 mIsTextElided = false;
215 if( mModel->IsTextElideEnabled() )
217 const Length numberOfLines = mModel->GetNumberOfLines();
218 if( 0u != numberOfLines )
220 const LineRun* const lines = mModel->GetLines();
222 const LineRun& lastLine = *( lines + ( numberOfLines - 1u ) );
223 const Length numberOfLaidOutGlyphs = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
225 if( lastLine.ellipsis && ( 0u != numberOfLaidOutGlyphs ) )
227 mIsTextElided = true;
228 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
230 const GlyphInfo* const glyphs = mModel->GetGlyphs();
231 const Vector2* const positions = mModel->GetLayout();
233 // Copy the glyphs to be elided.
234 mElidedGlyphs.Resize( numberOfLaidOutGlyphs );
235 mElidedLayout.Resize( numberOfLaidOutGlyphs );
237 GlyphInfo* elidedGlyphsBuffer = mElidedGlyphs.Begin();
238 Vector2* elidedPositionsBuffer = mElidedLayout.Begin();
240 memcpy( elidedGlyphsBuffer, glyphs, numberOfLaidOutGlyphs * sizeof( GlyphInfo ) );
241 memcpy( elidedPositionsBuffer, positions, numberOfLaidOutGlyphs * sizeof( Vector2 ) );
243 const Size& controlSize = mModel->GetControlSize();
245 if( ( 1u == numberOfLines ) &&
246 ( lastLine.ascender - lastLine.descender > controlSize.height ) )
248 // Get the first glyph which is going to be replaced and the ellipsis glyph.
249 GlyphInfo& glyphToRemove = *elidedGlyphsBuffer;
250 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
252 // Change the 'x' and 'y' position of the ellipsis glyph.
253 Vector2& position = *elidedPositionsBuffer;
255 position.x = ellipsisGlyph.xBearing;
256 position.y = -lastLine.ascender + controlSize.height - ellipsisGlyph.yBearing;
258 // Replace the glyph by the ellipsis glyph and resize the buffers.
259 glyphToRemove = ellipsisGlyph;
261 mElidedGlyphs.Resize( 1u );
262 mElidedLayout.Resize( 1u );
267 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
268 float firstPenX = 0.f; // Used if rtl text is elided.
270 bool firstPenSet = false;
272 // Add the ellipsis glyph.
273 bool inserted = false;
274 float removedGlypsWidth = 0.f;
275 Length numberOfRemovedGlyphs = 0u;
276 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
278 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
281 const GlyphInfo& glyphToRemove = *( elidedGlyphsBuffer + index );
283 if( 0u != glyphToRemove.fontId )
285 // i.e. The font id of the glyph shaped from the '\n' character is zero.
287 // Need to reshape the glyph as the font may be different in size.
288 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
292 const Vector2& position = *( elidedPositionsBuffer + index );
294 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
295 penY = position.y + glyphToRemove.yBearing;
297 // Calculates the first penX which will be used if rtl text is elided.
298 firstPenX = position.x - glyphToRemove.xBearing;
299 if( firstPenX < -ellipsisGlyph.xBearing )
301 // Avoids to exceed the bounding box when rtl text is elided.
302 firstPenX = -ellipsisGlyph.xBearing;
305 removedGlypsWidth = -ellipsisGlyph.xBearing;
310 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
312 // Calculate the width of the ellipsis glyph and check if it fits.
313 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
315 if( ellipsisGlyphWidth < removedGlypsWidth )
317 GlyphInfo& glyphInfo = *( elidedGlyphsBuffer + index );
318 Vector2& position = *( elidedPositionsBuffer + index );
319 position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
321 // Replace the glyph by the ellipsis glyph.
322 glyphInfo = ellipsisGlyph;
324 // Change the 'x' and 'y' position of the ellipsis glyph.
326 if( position.x > firstPenX )
328 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
331 position.x += ellipsisGlyph.xBearing;
332 position.y = penY - ellipsisGlyph.yBearing;
346 // No space for the ellipsis.
349 ++numberOfRemovedGlyphs;
351 } // while( !inserted )
353 // 'Removes' all the glyphs after the ellipsis glyph.
354 const Length numberOfGlyphs = numberOfLaidOutGlyphs - numberOfRemovedGlyphs;
355 mElidedGlyphs.Resize( numberOfGlyphs );
356 mElidedLayout.Resize( numberOfGlyphs );
364 } // namespace Toolkit