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 const Vector2& View::GetControlSize() const
60 if ( mImpl->mVisualModel )
62 return mImpl->mVisualModel->mControlSize;
68 Length View::GetGlyphs( GlyphInfo* glyphs,
69 Vector2* glyphPositions,
70 GlyphIndex glyphIndex,
71 Length numberOfGlyphs ) const
73 Length numberOfLaidOutGlyphs = 0u;
75 if( mImpl->mVisualModel )
77 // If ellipsis is enabled, the number of glyphs the layout engine has laid out may be less than 'numberOfGlyphs'.
78 // Check the last laid out line to know if the layout engine elided some text.
80 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
81 if( numberOfLines > 0u )
83 const LineRun& lastLine = *( mImpl->mVisualModel->mLines.Begin() + ( numberOfLines - 1u ) );
85 // If ellipsis is enabled, calculate the number of laid out glyphs.
86 // Otherwise use the given number of glyphs.
87 if( lastLine.ellipsis )
89 numberOfLaidOutGlyphs = lastLine.glyphIndex + lastLine.numberOfGlyphs;
93 numberOfLaidOutGlyphs = numberOfGlyphs;
96 // Retrieve from the visual model the glyphs and positions.
97 mImpl->mVisualModel->GetGlyphs( glyphs,
99 numberOfLaidOutGlyphs );
101 mImpl->mVisualModel->GetGlyphPositions( glyphPositions,
103 numberOfLaidOutGlyphs );
105 // Get the lines for the given range of glyphs.
106 // The lines contain the alignment offset which needs to be added to the glyph's position.
107 LineIndex firstLine = 0u;
108 Length numberOfLines = 0u;
109 mImpl->mVisualModel->GetNumberOfLines( glyphIndex,
110 numberOfLaidOutGlyphs,
114 Vector<LineRun> lines;
115 lines.Resize( numberOfLines );
116 LineRun* lineBuffer = lines.Begin();
118 mImpl->mVisualModel->GetLinesOfGlyphRange( lineBuffer,
120 numberOfLaidOutGlyphs );
122 // Get the first line for the given glyph range.
123 LineIndex lineIndex = firstLine;
124 LineRun* line = lineBuffer + lineIndex;
126 // Index of the last glyph of the line.
127 GlyphIndex lastGlyphIndexOfLine = line->glyphIndex + line->numberOfGlyphs - 1u;
129 // Add the alignment offset to the glyph's position.
130 for( Length index = 0u; index < numberOfLaidOutGlyphs; ++index )
132 ( *( glyphPositions + index ) ).x += line->alignmentOffset;
134 if( lastGlyphIndexOfLine == index )
136 // Get the next line.
139 if( lineIndex < numberOfLines )
141 line = lineBuffer + lineIndex;
142 lastGlyphIndexOfLine = line->glyphIndex + line->numberOfGlyphs - 1u;
147 if( 1u == numberOfLaidOutGlyphs )
149 // not a point try to do ellipsis with only one laid out character.
150 return numberOfLaidOutGlyphs;
153 if( lastLine.ellipsis )
155 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
156 float firstPenX = 0.f; // Used if rtl text is elided.
158 bool firstPenSet = false;
160 // Add the ellipsis glyph.
161 bool inserted = false;
162 float removedGlypsWidth = 0.f;
163 Length numberOfRemovedGlyphs = 0u;
164 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
166 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
169 const GlyphInfo& glyphToRemove = *( glyphs + index );
171 if( 0u != glyphToRemove.fontId )
173 // i.e. The font id of the glyph shaped from the '\n' character is zero.
175 // Need to reshape the glyph as the font may be different in size.
176 const GlyphInfo& ellipsisGlyph = mImpl->mFontClient.GetEllipsisGlyph( mImpl->mFontClient.GetPointSize( glyphToRemove.fontId ) );
180 const Vector2& position = *( glyphPositions + index );
182 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
183 penY = position.y + glyphToRemove.yBearing;
185 // Calculates the first penX which will be used if rtl text is elided.
186 firstPenX = position.x - glyphToRemove.xBearing;
187 if( firstPenX < -ellipsisGlyph.xBearing )
189 // Avoids to exceed the bounding box when rtl text is elided.
190 firstPenX = -ellipsisGlyph.xBearing;
193 removedGlypsWidth = -ellipsisGlyph.xBearing;
198 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
200 // Calculate the width of the ellipsis glyph and check if it fits.
201 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
202 if( ellipsisGlyphWidth < removedGlypsWidth )
204 GlyphInfo& glyphInfo = *( glyphs + index );
205 Vector2& position = *( glyphPositions + index );
206 position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
208 // Replace the glyph by the ellipsis glyph.
209 glyphInfo = ellipsisGlyph;
211 // Change the 'x' and 'y' position of the ellipsis glyph.
213 if( position.x > firstPenX )
215 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
218 position.x += ellipsisGlyph.xBearing;
219 position.y = penY - ellipsisGlyph.yBearing;
233 // No space for the ellipsis.
236 ++numberOfRemovedGlyphs;
240 // 'Removes' all the glyphs after the ellipsis glyph.
241 numberOfLaidOutGlyphs -= numberOfRemovedGlyphs;
246 return numberOfLaidOutGlyphs;
249 const Vector4& View::GetTextColor() const
251 if ( mImpl->mVisualModel )
253 VisualModel& model = *mImpl->mVisualModel;
254 return model.GetTextColor();
256 return Vector4::ZERO;
259 const Vector2& View::GetShadowOffset() const
261 if ( mImpl->mVisualModel )
263 VisualModel& model = *mImpl->mVisualModel;
264 return model.GetShadowOffset();
266 return Vector2::ZERO;
269 const Vector4& View::GetShadowColor() const
271 if ( mImpl->mVisualModel )
273 VisualModel& model = *mImpl->mVisualModel;
274 return model.GetShadowColor();
276 return Vector4::ZERO;
279 const Vector4& View::GetUnderlineColor() const
281 if ( mImpl->mVisualModel )
283 VisualModel& model = *mImpl->mVisualModel;
284 return model.GetUnderlineColor();
286 return Vector4::ZERO;
289 bool View::IsUnderlineEnabled() const
291 if ( mImpl->mVisualModel )
293 VisualModel& model = *mImpl->mVisualModel;
294 return model.IsUnderlineEnabled();
299 float View::GetUnderlineHeight() const
301 if ( mImpl->mVisualModel )
303 VisualModel& model = *mImpl->mVisualModel;
304 return model.GetUnderlineHeight();
309 Length View::GetNumberOfGlyphs() const
311 if( mImpl->mVisualModel )
313 VisualModel& model = *mImpl->mVisualModel;
315 const Length glyphCount = model.mGlyphs.Count();
316 const Length positionCount = model.mGlyphPositions.Count();
318 DALI_ASSERT_DEBUG( positionCount <= glyphCount && "Invalid glyph positions in Model" );
320 return (positionCount < glyphCount) ? positionCount : glyphCount;
328 } // namespace Toolkit