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::GetNumberOfGlyphs() const
91 if( mIsTextElided && mModel->IsTextElideEnabled() )
93 return mElidedGlyphs.Count();
97 return mModel->GetNumberOfGlyphs();
103 const GlyphInfo* const ViewModel::GetGlyphs() const
105 if( mIsTextElided && mModel->IsTextElideEnabled() )
107 return mElidedGlyphs.Begin();
111 return mModel->GetGlyphs();
117 const Vector2* const ViewModel::GetLayout() const
119 if( mIsTextElided && mModel->IsTextElideEnabled() )
121 return mElidedLayout.Begin();
125 return mModel->GetLayout();
131 const Vector4* const ViewModel::GetColors() const
133 return mModel->GetColors();
136 const ColorIndex* const ViewModel::GetColorIndices() const
138 return mModel->GetColorIndices();
141 const Vector4& ViewModel::GetDefaultColor() const
143 return mModel->GetDefaultColor();
146 const Vector2& ViewModel::GetShadowOffset() const
148 return mModel->GetShadowOffset();
151 const Vector4& ViewModel::GetShadowColor() const
153 return mModel->GetShadowColor();
156 const Vector4& ViewModel::GetUnderlineColor() const
158 return mModel->GetUnderlineColor();
161 bool ViewModel::IsUnderlineEnabled() const
163 return mModel->IsUnderlineEnabled();
166 float ViewModel::GetUnderlineHeight() const
168 return mModel->GetUnderlineHeight();
171 Length ViewModel::GetNumberOfUnderlineRuns() const
173 return mModel->GetNumberOfUnderlineRuns();
176 void ViewModel::GetUnderlineRuns( GlyphRun* underlineRuns, UnderlineRunIndex index, Length numberOfRuns ) const
178 mModel->GetUnderlineRuns( underlineRuns, index, numberOfRuns );
181 void ViewModel::ElideGlyphs()
183 mIsTextElided = false;
185 if( mModel->IsTextElideEnabled() )
187 const Length numberOfLines = mModel->GetNumberOfLines();
188 if( 0u != numberOfLines )
190 const LineRun* const lines = mModel->GetLines();
192 const LineRun& lastLine = *( lines + ( numberOfLines - 1u ) );
193 const Length numberOfLaidOutGlyphs = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
195 if( lastLine.ellipsis && ( 0u != numberOfLaidOutGlyphs ) )
197 mIsTextElided = true;
198 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
200 const GlyphInfo* const glyphs = mModel->GetGlyphs();
201 const Vector2* const positions = mModel->GetLayout();
203 // Copy the glyphs to be elided.
204 mElidedGlyphs.Resize( numberOfLaidOutGlyphs );
205 mElidedLayout.Resize( numberOfLaidOutGlyphs );
207 GlyphInfo* elidedGlyphsBuffer = mElidedGlyphs.Begin();
208 Vector2* elidedPositionsBuffer = mElidedLayout.Begin();
210 memcpy( elidedGlyphsBuffer, glyphs, numberOfLaidOutGlyphs * sizeof( GlyphInfo ) );
211 memcpy( elidedPositionsBuffer, positions, numberOfLaidOutGlyphs * sizeof( Vector2 ) );
213 const Size& controlSize = mModel->GetControlSize();
215 if( ( 1u == numberOfLines ) &&
216 ( lastLine.ascender - lastLine.descender > controlSize.height ) )
218 // Get the first glyph which is going to be replaced and the ellipsis glyph.
219 GlyphInfo& glyphToRemove = *elidedGlyphsBuffer;
220 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
222 // Change the 'x' and 'y' position of the ellipsis glyph.
223 Vector2& position = *elidedPositionsBuffer;
225 position.x = ellipsisGlyph.xBearing;
226 position.y = -lastLine.ascender + controlSize.height - ellipsisGlyph.yBearing;
228 // Replace the glyph by the ellipsis glyph and resize the buffers.
229 glyphToRemove = ellipsisGlyph;
231 mElidedGlyphs.Resize( 1u );
232 mElidedLayout.Resize( 1u );
237 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
238 float firstPenX = 0.f; // Used if rtl text is elided.
240 bool firstPenSet = false;
242 // Add the ellipsis glyph.
243 bool inserted = false;
244 float removedGlypsWidth = 0.f;
245 Length numberOfRemovedGlyphs = 0u;
246 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
248 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
251 const GlyphInfo& glyphToRemove = *( elidedGlyphsBuffer + index );
253 if( 0u != glyphToRemove.fontId )
255 // i.e. The font id of the glyph shaped from the '\n' character is zero.
257 // Need to reshape the glyph as the font may be different in size.
258 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
262 const Vector2& position = *( elidedPositionsBuffer + index );
264 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
265 penY = position.y + glyphToRemove.yBearing;
267 // Calculates the first penX which will be used if rtl text is elided.
268 firstPenX = position.x - glyphToRemove.xBearing;
269 if( firstPenX < -ellipsisGlyph.xBearing )
271 // Avoids to exceed the bounding box when rtl text is elided.
272 firstPenX = -ellipsisGlyph.xBearing;
275 removedGlypsWidth = -ellipsisGlyph.xBearing;
280 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
282 // Calculate the width of the ellipsis glyph and check if it fits.
283 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
285 if( ellipsisGlyphWidth < removedGlypsWidth )
287 GlyphInfo& glyphInfo = *( elidedGlyphsBuffer + index );
288 Vector2& position = *( elidedPositionsBuffer + index );
289 position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
291 // Replace the glyph by the ellipsis glyph.
292 glyphInfo = ellipsisGlyph;
294 // Change the 'x' and 'y' position of the ellipsis glyph.
296 if( position.x > firstPenX )
298 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
301 position.x += ellipsisGlyph.xBearing;
302 position.y = penY - ellipsisGlyph.yBearing;
316 // No space for the ellipsis.
319 ++numberOfRemovedGlyphs;
321 } // while( !inserted )
323 // 'Removes' all the glyphs after the ellipsis glyph.
324 const Length numberOfGlyphs = numberOfLaidOutGlyphs - numberOfRemovedGlyphs;
325 mElidedGlyphs.Resize( numberOfGlyphs );
326 mElidedLayout.Resize( numberOfGlyphs );
334 } // namespace Toolkit