2 * Copyright (c) 2016 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 void ViewModel::ElideGlyphs()
148 mIsTextElided = false;
150 if( mModel->IsTextElideEnabled() )
152 const Length numberOfLines = mModel->GetNumberOfLines();
153 if( 0u != numberOfLines )
155 const LineRun* const lines = mModel->GetLines();
157 const LineRun& lastLine = *( lines + ( numberOfLines - 1u ) );
158 const Length numberOfLaidOutGlyphs = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
160 if( lastLine.ellipsis && ( 0u != numberOfLaidOutGlyphs ) )
162 mIsTextElided = true;
163 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
165 const GlyphInfo* const glyphs = mModel->GetGlyphs();
166 const Vector2* const positions = mModel->GetLayout();
168 // Copy the glyphs to be elided.
169 mElidedGlyphs.Resize( numberOfLaidOutGlyphs );
170 mElidedLayout.Resize( numberOfLaidOutGlyphs );
172 GlyphInfo* elidedGlyphsBuffer = mElidedGlyphs.Begin();
173 Vector2* elidedPositionsBuffer = mElidedLayout.Begin();
175 memcpy( elidedGlyphsBuffer, glyphs, numberOfLaidOutGlyphs * sizeof( GlyphInfo ) );
176 memcpy( elidedPositionsBuffer, positions, numberOfLaidOutGlyphs * sizeof( Vector2 ) );
178 const Size& controlSize = mModel->GetControlSize();
180 if( ( 1u == numberOfLines ) &&
181 ( lastLine.ascender - lastLine.descender > controlSize.height ) )
183 // Get the first glyph which is going to be replaced and the ellipsis glyph.
184 GlyphInfo& glyphToRemove = *elidedGlyphsBuffer;
185 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
187 // Change the 'x' and 'y' position of the ellipsis glyph.
188 Vector2& position = *elidedPositionsBuffer;
190 position.x = ellipsisGlyph.xBearing;
191 position.y = -lastLine.ascender + controlSize.height - ellipsisGlyph.yBearing;
193 // Replace the glyph by the ellipsis glyph and resize the buffers.
194 glyphToRemove = ellipsisGlyph;
196 mElidedGlyphs.Resize( 1u );
197 mElidedLayout.Resize( 1u );
202 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
203 float firstPenX = 0.f; // Used if rtl text is elided.
205 bool firstPenSet = false;
207 // Add the ellipsis glyph.
208 bool inserted = false;
209 float removedGlypsWidth = 0.f;
210 Length numberOfRemovedGlyphs = 0u;
211 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
213 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
216 const GlyphInfo& glyphToRemove = *( elidedGlyphsBuffer + index );
218 if( 0u != glyphToRemove.fontId )
220 // i.e. The font id of the glyph shaped from the '\n' character is zero.
222 // Need to reshape the glyph as the font may be different in size.
223 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
227 const Vector2& position = *( elidedPositionsBuffer + index );
229 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
230 penY = position.y + glyphToRemove.yBearing;
232 // Calculates the first penX which will be used if rtl text is elided.
233 firstPenX = position.x - glyphToRemove.xBearing;
234 if( firstPenX < -ellipsisGlyph.xBearing )
236 // Avoids to exceed the bounding box when rtl text is elided.
237 firstPenX = -ellipsisGlyph.xBearing;
240 removedGlypsWidth = -ellipsisGlyph.xBearing;
245 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
247 // Calculate the width of the ellipsis glyph and check if it fits.
248 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
250 if( ellipsisGlyphWidth < removedGlypsWidth )
252 GlyphInfo& glyphInfo = *( elidedGlyphsBuffer + index );
253 Vector2& position = *( elidedPositionsBuffer + index );
254 position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
256 // Replace the glyph by the ellipsis glyph.
257 glyphInfo = ellipsisGlyph;
259 // Change the 'x' and 'y' position of the ellipsis glyph.
261 if( position.x > firstPenX )
263 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
266 position.x += ellipsisGlyph.xBearing;
267 position.y = penY - ellipsisGlyph.yBearing;
281 // No space for the ellipsis.
284 ++numberOfRemovedGlyphs;
286 } // while( !inserted )
288 // 'Removes' all the glyphs after the ellipsis glyph.
289 const Length numberOfGlyphs = numberOfLaidOutGlyphs - numberOfRemovedGlyphs;
290 mElidedGlyphs.Resize( numberOfGlyphs );
291 mElidedLayout.Resize( numberOfGlyphs );
299 } // namespace Toolkit