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 bool ViewModel::IsTextElideEnabled() const
76 return mModel->IsTextElideEnabled();
79 Length ViewModel::GetNumberOfLines() const
81 return mModel->GetNumberOfLines();
84 const LineRun* const ViewModel::GetLines() const
86 return mModel->GetLines();
89 Length ViewModel::GetNumberOfScripts() const
91 return mModel->GetNumberOfScripts();
94 const ScriptRun* const ViewModel::GetScriptRuns() const
96 return mModel->GetScriptRuns();
99 Length ViewModel::GetNumberOfGlyphs() const
101 if( mIsTextElided && mModel->IsTextElideEnabled() )
103 return mElidedGlyphs.Count();
107 return mModel->GetNumberOfGlyphs();
113 const GlyphInfo* const ViewModel::GetGlyphs() const
115 if( mIsTextElided && mModel->IsTextElideEnabled() )
117 return mElidedGlyphs.Begin();
121 return mModel->GetGlyphs();
127 const Vector2* const ViewModel::GetLayout() const
129 if( mIsTextElided && mModel->IsTextElideEnabled() )
131 return mElidedLayout.Begin();
135 return mModel->GetLayout();
141 const Vector4* const ViewModel::GetColors() const
143 return mModel->GetColors();
146 const ColorIndex* const ViewModel::GetColorIndices() const
148 return mModel->GetColorIndices();
151 const Vector4& ViewModel::GetDefaultColor() const
153 return mModel->GetDefaultColor();
156 const Vector2& ViewModel::GetShadowOffset() const
158 return mModel->GetShadowOffset();
161 const Vector4& ViewModel::GetShadowColor() const
163 return mModel->GetShadowColor();
166 const float& ViewModel::GetShadowBlurRadius() const
168 return mModel->GetShadowBlurRadius();
171 const Vector4& ViewModel::GetUnderlineColor() const
173 return mModel->GetUnderlineColor();
176 bool ViewModel::IsUnderlineEnabled() const
178 return mModel->IsUnderlineEnabled();
181 float ViewModel::GetUnderlineHeight() const
183 return mModel->GetUnderlineHeight();
186 Length ViewModel::GetNumberOfUnderlineRuns() const
188 return mModel->GetNumberOfUnderlineRuns();
191 void ViewModel::GetUnderlineRuns( GlyphRun* underlineRuns, UnderlineRunIndex index, Length numberOfRuns ) const
193 mModel->GetUnderlineRuns( underlineRuns, index, numberOfRuns );
196 const Vector4& ViewModel::GetOutlineColor() const
198 return mModel->GetOutlineColor();
201 float ViewModel::GetOutlineWidth() const
203 return mModel->GetOutlineWidth();
206 void ViewModel::ElideGlyphs()
208 mIsTextElided = false;
210 if( mModel->IsTextElideEnabled() )
212 const Length numberOfLines = mModel->GetNumberOfLines();
213 if( 0u != numberOfLines )
215 const LineRun* const lines = mModel->GetLines();
217 const LineRun& lastLine = *( lines + ( numberOfLines - 1u ) );
218 const Length numberOfLaidOutGlyphs = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
220 if( lastLine.ellipsis && ( 0u != numberOfLaidOutGlyphs ) )
222 mIsTextElided = true;
223 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
225 const GlyphInfo* const glyphs = mModel->GetGlyphs();
226 const Vector2* const positions = mModel->GetLayout();
228 // Copy the glyphs to be elided.
229 mElidedGlyphs.Resize( numberOfLaidOutGlyphs );
230 mElidedLayout.Resize( numberOfLaidOutGlyphs );
232 GlyphInfo* elidedGlyphsBuffer = mElidedGlyphs.Begin();
233 Vector2* elidedPositionsBuffer = mElidedLayout.Begin();
235 memcpy( elidedGlyphsBuffer, glyphs, numberOfLaidOutGlyphs * sizeof( GlyphInfo ) );
236 memcpy( elidedPositionsBuffer, positions, numberOfLaidOutGlyphs * sizeof( Vector2 ) );
238 const Size& controlSize = mModel->GetControlSize();
240 if( ( 1u == numberOfLines ) &&
241 ( lastLine.ascender - lastLine.descender > controlSize.height ) )
243 // Get the first glyph which is going to be replaced and the ellipsis glyph.
244 GlyphInfo& glyphToRemove = *elidedGlyphsBuffer;
245 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
247 // Change the 'x' and 'y' position of the ellipsis glyph.
248 Vector2& position = *elidedPositionsBuffer;
250 position.x = ellipsisGlyph.xBearing;
251 position.y = -lastLine.ascender + controlSize.height - ellipsisGlyph.yBearing;
253 // Replace the glyph by the ellipsis glyph and resize the buffers.
254 glyphToRemove = ellipsisGlyph;
256 mElidedGlyphs.Resize( 1u );
257 mElidedLayout.Resize( 1u );
262 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
263 float firstPenX = 0.f; // Used if rtl text is elided.
265 bool firstPenSet = false;
267 // Add the ellipsis glyph.
268 bool inserted = false;
269 float removedGlypsWidth = 0.f;
270 Length numberOfRemovedGlyphs = 0u;
271 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
273 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
276 const GlyphInfo& glyphToRemove = *( elidedGlyphsBuffer + index );
278 if( 0u != glyphToRemove.fontId )
280 // i.e. The font id of the glyph shaped from the '\n' character is zero.
282 // Need to reshape the glyph as the font may be different in size.
283 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
287 const Vector2& position = *( elidedPositionsBuffer + index );
289 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
290 penY = position.y + glyphToRemove.yBearing;
292 // Calculates the first penX which will be used if rtl text is elided.
293 firstPenX = position.x - glyphToRemove.xBearing;
294 if( firstPenX < -ellipsisGlyph.xBearing )
296 // Avoids to exceed the bounding box when rtl text is elided.
297 firstPenX = -ellipsisGlyph.xBearing;
300 removedGlypsWidth = -ellipsisGlyph.xBearing;
305 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
307 // Calculate the width of the ellipsis glyph and check if it fits.
308 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
310 if( ellipsisGlyphWidth < removedGlypsWidth )
312 GlyphInfo& glyphInfo = *( elidedGlyphsBuffer + index );
313 Vector2& position = *( elidedPositionsBuffer + index );
314 position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
316 // Replace the glyph by the ellipsis glyph.
317 glyphInfo = ellipsisGlyph;
319 // Change the 'x' and 'y' position of the ellipsis glyph.
321 if( position.x > firstPenX )
323 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
326 position.x += ellipsisGlyph.xBearing;
327 position.y = penY - ellipsisGlyph.yBearing;
341 // No space for the ellipsis.
344 ++numberOfRemovedGlyphs;
346 } // while( !inserted )
348 // 'Removes' all the glyphs after the ellipsis glyph.
349 const Length numberOfGlyphs = numberOfLaidOutGlyphs - numberOfRemovedGlyphs;
350 mElidedGlyphs.Resize( numberOfGlyphs );
351 mElidedLayout.Resize( numberOfGlyphs );
359 } // namespace Toolkit