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 if( 0u != glyphToRemove.fontId )
121 // i.e. The font id of the glyph shaped from the '\n' character is zero.
123 // Need to reshape the glyph as the font may be different in size.
124 const GlyphInfo& ellipsisGlyph = mImpl->mFontClient.GetEllipsisGlyph( mImpl->mFontClient.GetPointSize( glyphToRemove.fontId ) );
128 const Vector2& position = *( glyphPositions + index );
130 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
131 penY = position.y + glyphToRemove.yBearing;
133 // Calculates the first penX which will be used if rtl text is elided.
134 firstPenX = position.x - glyphToRemove.xBearing;
135 if( firstPenX < -ellipsisGlyph.xBearing )
137 // Avoids to exceed the bounding box when rtl text is elided.
138 firstPenX = -ellipsisGlyph.xBearing;
141 removedGlypsWidth = -ellipsisGlyph.xBearing;
146 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
148 // Calculate the width of the ellipsis glyph and check if it fits.
149 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
150 if( ellipsisGlyphWidth < removedGlypsWidth )
152 GlyphInfo& glyphInfo = *( glyphs + index );
153 Vector2& position = *( glyphPositions + index );
154 position.x -= glyphInfo.xBearing;
156 // Replace the glyph by the ellipsis glyph.
157 glyphInfo = ellipsisGlyph;
159 // Change the 'x' and 'y' position of the ellipsis glyph.
161 if( position.x > firstPenX )
163 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
166 position.x += ellipsisGlyph.xBearing;
167 position.y = penY - ellipsisGlyph.yBearing;
181 // No space for the ellipsis.
184 ++numberOfRemovedGlyphs;
188 // 'Removes' all the glyphs after the ellipsis glyph.
189 numberOfLaidOutGlyphs -= numberOfRemovedGlyphs;
194 return numberOfLaidOutGlyphs;
197 const Vector4& View::GetTextColor() const
199 if ( mImpl->mVisualModel )
201 VisualModel& model = *mImpl->mVisualModel;
202 return model.GetTextColor();
204 return Vector4::ZERO;
207 const Vector2& View::GetShadowOffset() const
209 if ( mImpl->mVisualModel )
211 VisualModel& model = *mImpl->mVisualModel;
212 return model.GetShadowOffset();
214 return Vector2::ZERO;
217 const Vector4& View::GetShadowColor() const
219 if ( mImpl->mVisualModel )
221 VisualModel& model = *mImpl->mVisualModel;
222 return model.GetShadowColor();
224 return Vector4::ZERO;
227 const Vector4& View::GetUnderlineColor() const
229 if ( mImpl->mVisualModel )
231 VisualModel& model = *mImpl->mVisualModel;
232 return model.GetUnderlineColor();
234 return Vector4::ZERO;
237 bool View::IsUnderlineEnabled() const
239 if ( mImpl->mVisualModel )
241 VisualModel& model = *mImpl->mVisualModel;
242 return model.IsUnderlineEnabled();
247 float View::GetUnderlineHeight() const
249 if ( mImpl->mVisualModel )
251 VisualModel& model = *mImpl->mVisualModel;
252 return model.GetUnderlineHeight();
257 Length View::GetNumberOfGlyphs() const
259 if( mImpl->mVisualModel )
261 VisualModel& model = *mImpl->mVisualModel;
263 Length glyphCount = model.GetNumberOfGlyphs();
264 Length positionCount = model.GetNumberOfGlyphPositions();
266 DALI_ASSERT_DEBUG( positionCount <= glyphCount && "Invalid glyph positions in Model" );
268 return (positionCount < glyphCount) ? positionCount : glyphCount;
276 } // namespace Toolkit