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 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 const Vector4& ViewModel::GetOutlineColor() const
193 return mModel->GetOutlineColor();
196 float ViewModel::GetOutlineWidth() const
198 return mModel->GetOutlineWidth();
201 void ViewModel::ElideGlyphs()
203 mIsTextElided = false;
205 if( mModel->IsTextElideEnabled() )
207 const Length numberOfLines = mModel->GetNumberOfLines();
208 if( 0u != numberOfLines )
210 const LineRun* const lines = mModel->GetLines();
212 const LineRun& lastLine = *( lines + ( numberOfLines - 1u ) );
213 const Length numberOfLaidOutGlyphs = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
215 if( lastLine.ellipsis && ( 0u != numberOfLaidOutGlyphs ) )
217 mIsTextElided = true;
218 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
220 const GlyphInfo* const glyphs = mModel->GetGlyphs();
221 const Vector2* const positions = mModel->GetLayout();
223 // Copy the glyphs to be elided.
224 mElidedGlyphs.Resize( numberOfLaidOutGlyphs );
225 mElidedLayout.Resize( numberOfLaidOutGlyphs );
227 GlyphInfo* elidedGlyphsBuffer = mElidedGlyphs.Begin();
228 Vector2* elidedPositionsBuffer = mElidedLayout.Begin();
230 memcpy( elidedGlyphsBuffer, glyphs, numberOfLaidOutGlyphs * sizeof( GlyphInfo ) );
231 memcpy( elidedPositionsBuffer, positions, numberOfLaidOutGlyphs * sizeof( Vector2 ) );
233 const Size& controlSize = mModel->GetControlSize();
235 if( ( 1u == numberOfLines ) &&
236 ( lastLine.ascender - lastLine.descender > controlSize.height ) )
238 // Get the first glyph which is going to be replaced and the ellipsis glyph.
239 GlyphInfo& glyphToRemove = *elidedGlyphsBuffer;
240 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
242 // Change the 'x' and 'y' position of the ellipsis glyph.
243 Vector2& position = *elidedPositionsBuffer;
245 position.x = ellipsisGlyph.xBearing;
246 position.y = -lastLine.ascender + controlSize.height - ellipsisGlyph.yBearing;
248 // Replace the glyph by the ellipsis glyph and resize the buffers.
249 glyphToRemove = ellipsisGlyph;
251 mElidedGlyphs.Resize( 1u );
252 mElidedLayout.Resize( 1u );
257 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
258 float firstPenX = 0.f; // Used if rtl text is elided.
260 bool firstPenSet = false;
262 // Add the ellipsis glyph.
263 bool inserted = false;
264 float removedGlypsWidth = 0.f;
265 Length numberOfRemovedGlyphs = 0u;
266 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
268 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
271 const GlyphInfo& glyphToRemove = *( elidedGlyphsBuffer + index );
273 if( 0u != glyphToRemove.fontId )
275 // i.e. The font id of the glyph shaped from the '\n' character is zero.
277 // Need to reshape the glyph as the font may be different in size.
278 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
282 const Vector2& position = *( elidedPositionsBuffer + index );
284 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
285 penY = position.y + glyphToRemove.yBearing;
287 // Calculates the first penX which will be used if rtl text is elided.
288 firstPenX = position.x - glyphToRemove.xBearing;
289 if( firstPenX < -ellipsisGlyph.xBearing )
291 // Avoids to exceed the bounding box when rtl text is elided.
292 firstPenX = -ellipsisGlyph.xBearing;
295 removedGlypsWidth = -ellipsisGlyph.xBearing;
300 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
302 // Calculate the width of the ellipsis glyph and check if it fits.
303 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
305 if( ellipsisGlyphWidth < removedGlypsWidth )
307 GlyphInfo& glyphInfo = *( elidedGlyphsBuffer + index );
308 Vector2& position = *( elidedPositionsBuffer + index );
309 position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
311 // Replace the glyph by the ellipsis glyph.
312 glyphInfo = ellipsisGlyph;
314 // Change the 'x' and 'y' position of the ellipsis glyph.
316 if( position.x > firstPenX )
318 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
321 position.x += ellipsisGlyph.xBearing;
322 position.y = penY - ellipsisGlyph.yBearing;
336 // No space for the ellipsis.
339 ++numberOfRemovedGlyphs;
341 } // while( !inserted )
343 // 'Removes' all the glyphs after the ellipsis glyph.
344 const Length numberOfGlyphs = numberOfLaidOutGlyphs - numberOfRemovedGlyphs;
345 mElidedGlyphs.Resize( numberOfGlyphs );
346 mElidedLayout.Resize( numberOfGlyphs );
354 } // namespace Toolkit