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 // Get the lines for the given range of glyphs.
96 // The lines contain the alignment offset which needs to be added to the glyph's position.
97 LineIndex firstLine = 0u;
98 Length numberOfLines = 0u;
99 mImpl->mVisualModel->GetNumberOfLines( glyphIndex,
100 numberOfLaidOutGlyphs,
104 Vector<LineRun> lines;
105 lines.Resize( numberOfLines );
106 LineRun* lineBuffer = lines.Begin();
108 mImpl->mVisualModel->GetLinesOfGlyphRange( lineBuffer,
110 numberOfLaidOutGlyphs );
112 // Get the first line for the given glyph range.
113 LineIndex lineIndex = firstLine;
114 LineRun* line = lineBuffer + lineIndex;
116 // Index of the last glyph of the line.
117 GlyphIndex lastGlyphIndexOfLine = line->glyphIndex + line->numberOfGlyphs - 1u;
119 // Add the alignment offset to the glyph's position.
120 for( Length index = 0u; index < numberOfLaidOutGlyphs; ++index )
122 ( *( glyphPositions + index ) ).x += line->alignmentOffset;
124 if( lastGlyphIndexOfLine == index )
126 // Get the next line.
129 if( lineIndex < numberOfLines )
131 line = lineBuffer + lineIndex;
132 lastGlyphIndexOfLine = line->glyphIndex + line->numberOfGlyphs - 1u;
137 if( 1u == numberOfLaidOutGlyphs )
139 // not a point try to do ellipsis with only one laid out character.
140 return numberOfLaidOutGlyphs;
143 if( lastLine.ellipsis )
145 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
146 float firstPenX = 0.f; // Used if rtl text is elided.
148 bool firstPenSet = false;
150 // Add the ellipsis glyph.
151 bool inserted = false;
152 float removedGlypsWidth = 0.f;
153 Length numberOfRemovedGlyphs = 0u;
154 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
156 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
159 const GlyphInfo& glyphToRemove = *( glyphs + index );
161 if( 0u != glyphToRemove.fontId )
163 // i.e. The font id of the glyph shaped from the '\n' character is zero.
165 // Need to reshape the glyph as the font may be different in size.
166 const GlyphInfo& ellipsisGlyph = mImpl->mFontClient.GetEllipsisGlyph( mImpl->mFontClient.GetPointSize( glyphToRemove.fontId ) );
170 const Vector2& position = *( glyphPositions + index );
172 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
173 penY = position.y + glyphToRemove.yBearing;
175 // Calculates the first penX which will be used if rtl text is elided.
176 firstPenX = position.x - glyphToRemove.xBearing;
177 if( firstPenX < -ellipsisGlyph.xBearing )
179 // Avoids to exceed the bounding box when rtl text is elided.
180 firstPenX = -ellipsisGlyph.xBearing;
183 removedGlypsWidth = -ellipsisGlyph.xBearing;
188 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
190 // Calculate the width of the ellipsis glyph and check if it fits.
191 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
192 if( ellipsisGlyphWidth < removedGlypsWidth )
194 GlyphInfo& glyphInfo = *( glyphs + index );
195 Vector2& position = *( glyphPositions + index );
196 position.x -= glyphInfo.xBearing;
198 // Replace the glyph by the ellipsis glyph.
199 glyphInfo = ellipsisGlyph;
201 // Change the 'x' and 'y' position of the ellipsis glyph.
203 if( position.x > firstPenX )
205 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
208 position.x += ellipsisGlyph.xBearing;
209 position.y = penY - ellipsisGlyph.yBearing;
223 // No space for the ellipsis.
226 ++numberOfRemovedGlyphs;
230 // 'Removes' all the glyphs after the ellipsis glyph.
231 numberOfLaidOutGlyphs -= numberOfRemovedGlyphs;
236 return numberOfLaidOutGlyphs;
239 const Vector4& View::GetTextColor() const
241 if ( mImpl->mVisualModel )
243 VisualModel& model = *mImpl->mVisualModel;
244 return model.GetTextColor();
246 return Vector4::ZERO;
249 const Vector2& View::GetShadowOffset() const
251 if ( mImpl->mVisualModel )
253 VisualModel& model = *mImpl->mVisualModel;
254 return model.GetShadowOffset();
256 return Vector2::ZERO;
259 const Vector4& View::GetShadowColor() const
261 if ( mImpl->mVisualModel )
263 VisualModel& model = *mImpl->mVisualModel;
264 return model.GetShadowColor();
266 return Vector4::ZERO;
269 const Vector4& View::GetUnderlineColor() const
271 if ( mImpl->mVisualModel )
273 VisualModel& model = *mImpl->mVisualModel;
274 return model.GetUnderlineColor();
276 return Vector4::ZERO;
279 bool View::IsUnderlineEnabled() const
281 if ( mImpl->mVisualModel )
283 VisualModel& model = *mImpl->mVisualModel;
284 return model.IsUnderlineEnabled();
289 float View::GetUnderlineHeight() const
291 if ( mImpl->mVisualModel )
293 VisualModel& model = *mImpl->mVisualModel;
294 return model.GetUnderlineHeight();
299 Length View::GetNumberOfGlyphs() const
301 if( mImpl->mVisualModel )
303 VisualModel& model = *mImpl->mVisualModel;
305 Length glyphCount = model.GetNumberOfGlyphs();
306 Length positionCount = model.GetNumberOfGlyphPositions();
308 DALI_ASSERT_DEBUG( positionCount <= glyphCount && "Invalid glyph positions in Model" );
310 return (positionCount < glyphCount) ? positionCount : glyphCount;
318 } // namespace Toolkit