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 const Vector2& View::GetLayoutSize() const
70 if ( mImpl->mVisualModel )
72 return mImpl->mVisualModel->GetLayoutSize();
78 Length View::GetNumberOfGlyphs() const
80 if( mImpl->mVisualModel )
82 const VisualModel& model = *mImpl->mVisualModel;
84 const Length glyphCount = model.mGlyphs.Count();
85 const Length positionCount = model.mGlyphPositions.Count();
87 DALI_ASSERT_DEBUG( positionCount <= glyphCount && "Invalid glyph positions in Model" );
89 return (positionCount < glyphCount) ? positionCount : glyphCount;
95 Length View::GetGlyphs( GlyphInfo* glyphs,
96 Vector2* glyphPositions,
97 GlyphIndex glyphIndex,
98 Length numberOfGlyphs ) const
100 Length numberOfLaidOutGlyphs = 0u;
102 if( mImpl->mVisualModel )
104 // If ellipsis is enabled, the number of glyphs the layout engine has laid out may be less than 'numberOfGlyphs'.
105 // Check the last laid out line to know if the layout engine elided some text.
107 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
108 if( numberOfLines > 0u )
110 const LineRun& lastLine = *( mImpl->mVisualModel->mLines.Begin() + ( numberOfLines - 1u ) );
112 // If ellipsis is enabled, calculate the number of laid out glyphs.
113 // Otherwise use the given number of glyphs.
114 if( lastLine.ellipsis )
116 numberOfLaidOutGlyphs = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
120 numberOfLaidOutGlyphs = numberOfGlyphs;
123 if( 0u < numberOfLaidOutGlyphs )
125 // Retrieve from the visual model the glyphs and positions.
126 mImpl->mVisualModel->GetGlyphs( glyphs,
128 numberOfLaidOutGlyphs );
130 mImpl->mVisualModel->GetGlyphPositions( glyphPositions,
132 numberOfLaidOutGlyphs );
134 // Get the lines for the given range of glyphs.
135 // The lines contain the alignment offset which needs to be added to the glyph's position.
136 LineIndex firstLine = 0u;
137 Length numberOfLines = 0u;
138 mImpl->mVisualModel->GetNumberOfLines( glyphIndex,
139 numberOfLaidOutGlyphs,
143 Vector<LineRun> lines;
144 lines.Resize( numberOfLines );
145 LineRun* lineBuffer = lines.Begin();
147 mImpl->mVisualModel->GetLinesOfGlyphRange( lineBuffer,
149 numberOfLaidOutGlyphs );
151 // Get the first line for the given glyph range.
152 LineIndex lineIndex = firstLine;
153 LineRun* line = lineBuffer + lineIndex;
155 // Index of the last glyph of the line.
156 GlyphIndex lastGlyphIndexOfLine = line->glyphRun.glyphIndex + line->glyphRun.numberOfGlyphs - 1u;
158 // Add the alignment offset to the glyph's position.
160 float penY = line->ascender;
161 for( Length index = 0u; index < numberOfLaidOutGlyphs; ++index )
163 Vector2& position = *( glyphPositions + index );
164 position.x += line->alignmentOffset;
167 if( lastGlyphIndexOfLine == index )
169 penY += -line->descender;
171 // Get the next line.
174 if( lineIndex < numberOfLines )
176 line = lineBuffer + lineIndex;
178 lastGlyphIndexOfLine = line->glyphRun.glyphIndex + line->glyphRun.numberOfGlyphs - 1u;
180 penY += line->ascender;
185 if( 1u == numberOfLaidOutGlyphs )
187 // not a point try to do ellipsis with only one laid out character.
188 return numberOfLaidOutGlyphs;
191 if( lastLine.ellipsis )
193 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
194 float firstPenX = 0.f; // Used if rtl text is elided.
196 bool firstPenSet = false;
198 // Add the ellipsis glyph.
199 bool inserted = false;
200 float removedGlypsWidth = 0.f;
201 Length numberOfRemovedGlyphs = 0u;
202 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
204 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
207 const GlyphInfo& glyphToRemove = *( glyphs + index );
209 if( 0u != glyphToRemove.fontId )
211 // i.e. The font id of the glyph shaped from the '\n' character is zero.
213 // Need to reshape the glyph as the font may be different in size.
214 const GlyphInfo& ellipsisGlyph = mImpl->mFontClient.GetEllipsisGlyph( mImpl->mFontClient.GetPointSize( glyphToRemove.fontId ) );
218 const Vector2& position = *( glyphPositions + index );
220 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
221 penY = position.y + glyphToRemove.yBearing;
223 // Calculates the first penX which will be used if rtl text is elided.
224 firstPenX = position.x - glyphToRemove.xBearing;
225 if( firstPenX < -ellipsisGlyph.xBearing )
227 // Avoids to exceed the bounding box when rtl text is elided.
228 firstPenX = -ellipsisGlyph.xBearing;
231 removedGlypsWidth = -ellipsisGlyph.xBearing;
236 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
238 // Calculate the width of the ellipsis glyph and check if it fits.
239 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
240 if( ellipsisGlyphWidth < removedGlypsWidth )
242 GlyphInfo& glyphInfo = *( glyphs + index );
243 Vector2& position = *( glyphPositions + index );
244 position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
246 // Replace the glyph by the ellipsis glyph.
247 glyphInfo = ellipsisGlyph;
249 // Change the 'x' and 'y' position of the ellipsis glyph.
251 if( position.x > firstPenX )
253 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
256 position.x += ellipsisGlyph.xBearing;
257 position.y = penY - ellipsisGlyph.yBearing;
271 // No space for the ellipsis.
274 ++numberOfRemovedGlyphs;
278 // 'Removes' all the glyphs after the ellipsis glyph.
279 numberOfLaidOutGlyphs -= numberOfRemovedGlyphs;
285 return numberOfLaidOutGlyphs;
288 const Vector4* const View::GetColors() const
290 if( mImpl->mVisualModel )
292 return mImpl->mVisualModel->mColors.Begin();
298 const ColorIndex* const View::GetColorIndices() const
300 if( mImpl->mVisualModel )
302 return mImpl->mVisualModel->mColorIndices.Begin();
308 const Vector4& View::GetTextColor() const
310 if( mImpl->mVisualModel )
312 return mImpl->mVisualModel->GetTextColor();
314 return Vector4::ZERO;
317 const Vector2& View::GetShadowOffset() const
319 if( mImpl->mVisualModel )
321 return mImpl->mVisualModel->GetShadowOffset();
323 return Vector2::ZERO;
326 const Vector4& View::GetShadowColor() const
328 if( mImpl->mVisualModel )
330 return mImpl->mVisualModel->GetShadowColor();
332 return Vector4::ZERO;
335 const Vector4& View::GetUnderlineColor() const
337 if( mImpl->mVisualModel )
339 return mImpl->mVisualModel->GetUnderlineColor();
341 return Vector4::ZERO;
344 bool View::IsUnderlineEnabled() const
346 if( mImpl->mVisualModel )
348 return mImpl->mVisualModel->IsUnderlineEnabled();
353 float View::GetUnderlineHeight() const
355 if( mImpl->mVisualModel )
357 return mImpl->mVisualModel->GetUnderlineHeight();
362 Length View::GetNumberOfUnderlineRuns() const
364 if( mImpl->mVisualModel )
366 return mImpl->mVisualModel->mUnderlineRuns.Count();
372 void View::GetUnderlineRuns( GlyphRun* underlineRuns,
373 UnderlineRunIndex index,
374 Length numberOfRuns ) const
376 if( mImpl->mVisualModel )
378 mImpl->mVisualModel->GetUnderlineRuns( underlineRuns,
386 } // namespace Toolkit