2 * Copyright (c) 2015 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/text-view.h>
22 #include <dali/public-api/math/vector2.h>
23 #include <dali/devel-api/text-abstraction/font-client.h>
36 VisualModelPtr mVisualModel;
37 TextAbstraction::FontClient mFontClient; ///< Handle to the font client.
43 mImpl = new View::Impl();
45 mImpl->mFontClient = TextAbstraction::FontClient::Get();
53 void View::SetVisualModel( VisualModelPtr visualModel )
55 mImpl->mVisualModel = visualModel;
58 Length View::GetGlyphs( GlyphInfo* glyphs,
59 Vector2* glyphPositions,
60 GlyphIndex glyphIndex,
61 Length numberOfGlyphs ) const
63 Length numberOfLaidOutGlyphs = 0u;
65 if( mImpl->mVisualModel )
67 // If ellipsis is enabled, the number of glyphs the layout engine has laid out may be less than 'numberOfGlyphs'.
68 // Check the last laid out line to know if the layout engine elided some text.
70 const Length numberOfLines = mImpl->mVisualModel->GetNumberOfLines();
71 if( numberOfLines > 0u )
73 const LineRun& lastLine = *( mImpl->mVisualModel->mLines.Begin() + ( numberOfLines - 1u ) );
75 // If ellipsis is enabled, calculate the number of laid out glyphs.
76 // Otherwise use the given number of glyphs.
77 if( lastLine.ellipsis )
79 numberOfLaidOutGlyphs = lastLine.glyphIndex + lastLine.numberOfGlyphs;
83 numberOfLaidOutGlyphs = numberOfGlyphs;
86 // Retrieve from the visual model the glyphs and positions.
87 mImpl->mVisualModel->GetGlyphs( glyphs,
89 numberOfLaidOutGlyphs );
91 mImpl->mVisualModel->GetGlyphPositions( glyphPositions,
93 numberOfLaidOutGlyphs );
95 if( 1u == numberOfLaidOutGlyphs )
97 // not a point try to do ellipsis with only one laid out character.
98 return numberOfLaidOutGlyphs;
101 if( lastLine.ellipsis )
103 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
104 float firstPenX = 0.f; // Used if rtl text is elided.
106 bool firstPenSet = false;
108 // Add the ellipsis glyph.
109 bool inserted = false;
110 float removedGlypsWidth = 0.f;
111 Length numberOfRemovedGlyphs = 0u;
112 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
114 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
117 const GlyphInfo& glyphToRemove = *( glyphs + index );
119 // Need to reshape the glyph as the font may be different in size.
120 const GlyphInfo& ellipsisGlyph = mImpl->mFontClient.GetEllipsisGlyph( mImpl->mFontClient.GetPointSize( glyphToRemove.fontId ) );
124 const Vector2& position = *( glyphPositions + index );
126 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
127 penY = position.y + glyphToRemove.yBearing;
129 // Calculates the first penX which will be used if rtl text is elided.
130 firstPenX = position.x - glyphToRemove.xBearing;
131 if( firstPenX < -ellipsisGlyph.xBearing )
133 // Avoids to exceed the bounding box when rtl text is elided.
134 firstPenX = -ellipsisGlyph.xBearing;
137 removedGlypsWidth = -ellipsisGlyph.xBearing;
142 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
144 // Calculate the width of the ellipsis glyph and check if it fits.
145 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
146 if( ellipsisGlyphWidth < removedGlypsWidth )
148 GlyphInfo& glyphInfo = *( glyphs + index );
149 Vector2& position = *( glyphPositions + index );
150 position.x -= glyphInfo.xBearing;
152 // Replace the glyph by the ellipsis glyph.
153 glyphInfo = ellipsisGlyph;
155 // Change the 'x' and 'y' position of the ellipsis glyph.
157 if( position.x > firstPenX )
159 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
162 position.x += ellipsisGlyph.xBearing;
163 position.y = penY - ellipsisGlyph.yBearing;
175 // No space for the ellipsis.
178 ++numberOfRemovedGlyphs;
182 // 'Removes' all the glyphs after the ellipsis glyph.
183 numberOfLaidOutGlyphs -= numberOfRemovedGlyphs;
188 return numberOfLaidOutGlyphs;
191 const Vector4& View::GetTextColor() const
193 if ( mImpl->mVisualModel )
195 VisualModel& model = *mImpl->mVisualModel;
196 return model.GetTextColor();
198 return Vector4::ZERO;
201 const Vector2& View::GetShadowOffset() const
203 if ( mImpl->mVisualModel )
205 VisualModel& model = *mImpl->mVisualModel;
206 return model.GetShadowOffset();
208 return Vector2::ZERO;
211 const Vector4& View::GetShadowColor() const
213 if ( mImpl->mVisualModel )
215 VisualModel& model = *mImpl->mVisualModel;
216 return model.GetShadowColor();
218 return Vector4::ZERO;
221 const Vector4& View::GetUnderlineColor() const
223 if ( mImpl->mVisualModel )
225 VisualModel& model = *mImpl->mVisualModel;
226 return model.GetUnderlineColor();
228 return Vector4::ZERO;
231 bool View::IsUnderlineEnabled() const
233 if ( mImpl->mVisualModel )
235 VisualModel& model = *mImpl->mVisualModel;
236 return model.IsUnderlineEnabled();
241 float View::GetUnderlineHeight() const
243 if ( mImpl->mVisualModel )
245 VisualModel& model = *mImpl->mVisualModel;
246 return model.GetUnderlineHeight();
251 Length View::GetNumberOfGlyphs() const
253 if( mImpl->mVisualModel )
255 VisualModel& model = *mImpl->mVisualModel;
257 Length glyphCount = model.GetNumberOfGlyphs();
258 Length positionCount = model.GetNumberOfGlyphPositions();
260 DALI_ASSERT_DEBUG( positionCount <= glyphCount && "Invalid glyph positions in Model" );
262 return (positionCount < glyphCount) ? positionCount : glyphCount;
270 } // namespace Toolkit