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 Layout::HorizontalAlignment ViewModel::GetHorizontalAlignment() const
66 return mModel->GetHorizontalAlignment();
69 Layout::VerticalAlignment 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 Vector4& ViewModel::GetUnderlineColor() const
168 return mModel->GetUnderlineColor();
171 bool ViewModel::IsUnderlineEnabled() const
173 return mModel->IsUnderlineEnabled();
176 float ViewModel::GetUnderlineHeight() const
178 return mModel->GetUnderlineHeight();
181 Length ViewModel::GetNumberOfUnderlineRuns() const
183 return mModel->GetNumberOfUnderlineRuns();
186 void ViewModel::GetUnderlineRuns( GlyphRun* underlineRuns, UnderlineRunIndex index, Length numberOfRuns ) const
188 mModel->GetUnderlineRuns( underlineRuns, index, numberOfRuns );
191 void ViewModel::ElideGlyphs()
193 mIsTextElided = false;
195 if( mModel->IsTextElideEnabled() )
197 const Length numberOfLines = mModel->GetNumberOfLines();
198 if( 0u != numberOfLines )
200 const LineRun* const lines = mModel->GetLines();
202 const LineRun& lastLine = *( lines + ( numberOfLines - 1u ) );
203 const Length numberOfLaidOutGlyphs = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
205 if( lastLine.ellipsis && ( 0u != numberOfLaidOutGlyphs ) )
207 mIsTextElided = true;
208 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
210 const GlyphInfo* const glyphs = mModel->GetGlyphs();
211 const Vector2* const positions = mModel->GetLayout();
213 // Copy the glyphs to be elided.
214 mElidedGlyphs.Resize( numberOfLaidOutGlyphs );
215 mElidedLayout.Resize( numberOfLaidOutGlyphs );
217 GlyphInfo* elidedGlyphsBuffer = mElidedGlyphs.Begin();
218 Vector2* elidedPositionsBuffer = mElidedLayout.Begin();
220 memcpy( elidedGlyphsBuffer, glyphs, numberOfLaidOutGlyphs * sizeof( GlyphInfo ) );
221 memcpy( elidedPositionsBuffer, positions, numberOfLaidOutGlyphs * sizeof( Vector2 ) );
223 const Size& controlSize = mModel->GetControlSize();
225 if( ( 1u == numberOfLines ) &&
226 ( lastLine.ascender - lastLine.descender > controlSize.height ) )
228 // Get the first glyph which is going to be replaced and the ellipsis glyph.
229 GlyphInfo& glyphToRemove = *elidedGlyphsBuffer;
230 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
232 // Change the 'x' and 'y' position of the ellipsis glyph.
233 Vector2& position = *elidedPositionsBuffer;
235 position.x = ellipsisGlyph.xBearing;
236 position.y = -lastLine.ascender + controlSize.height - ellipsisGlyph.yBearing;
238 // Replace the glyph by the ellipsis glyph and resize the buffers.
239 glyphToRemove = ellipsisGlyph;
241 mElidedGlyphs.Resize( 1u );
242 mElidedLayout.Resize( 1u );
247 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
248 float firstPenX = 0.f; // Used if rtl text is elided.
250 bool firstPenSet = false;
252 // Add the ellipsis glyph.
253 bool inserted = false;
254 float removedGlypsWidth = 0.f;
255 Length numberOfRemovedGlyphs = 0u;
256 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
258 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
261 const GlyphInfo& glyphToRemove = *( elidedGlyphsBuffer + index );
263 if( 0u != glyphToRemove.fontId )
265 // i.e. The font id of the glyph shaped from the '\n' character is zero.
267 // Need to reshape the glyph as the font may be different in size.
268 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
272 const Vector2& position = *( elidedPositionsBuffer + index );
274 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
275 penY = position.y + glyphToRemove.yBearing;
277 // Calculates the first penX which will be used if rtl text is elided.
278 firstPenX = position.x - glyphToRemove.xBearing;
279 if( firstPenX < -ellipsisGlyph.xBearing )
281 // Avoids to exceed the bounding box when rtl text is elided.
282 firstPenX = -ellipsisGlyph.xBearing;
285 removedGlypsWidth = -ellipsisGlyph.xBearing;
290 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
292 // Calculate the width of the ellipsis glyph and check if it fits.
293 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
295 if( ellipsisGlyphWidth < removedGlypsWidth )
297 GlyphInfo& glyphInfo = *( elidedGlyphsBuffer + index );
298 Vector2& position = *( elidedPositionsBuffer + index );
299 position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
301 // Replace the glyph by the ellipsis glyph.
302 glyphInfo = ellipsisGlyph;
304 // Change the 'x' and 'y' position of the ellipsis glyph.
306 if( position.x > firstPenX )
308 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
311 position.x += ellipsisGlyph.xBearing;
312 position.y = penY - ellipsisGlyph.yBearing;
326 // No space for the ellipsis.
329 ++numberOfRemovedGlyphs;
331 } // while( !inserted )
333 // 'Removes' all the glyphs after the ellipsis glyph.
334 const Length numberOfGlyphs = numberOfLaidOutGlyphs - numberOfRemovedGlyphs;
335 mElidedGlyphs.Resize( numberOfGlyphs );
336 mElidedLayout.Resize( numberOfGlyphs );
344 } // namespace Toolkit