2 * Copyright (c) 2017 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,
98 GlyphIndex glyphIndex,
99 Length numberOfGlyphs ) const
101 Length numberOfLaidOutGlyphs = 0u;
103 if( mImpl->mVisualModel )
105 // If ellipsis is enabled, the number of glyphs the layout engine has laid out may be less than 'numberOfGlyphs'.
106 // Check the last laid out line to know if the layout engine elided some text.
108 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
109 if( numberOfLines > 0u )
111 const LineRun& lastLine = *( mImpl->mVisualModel->mLines.Begin() + ( numberOfLines - 1u ) );
113 // If ellipsis is enabled, calculate the number of laid out glyphs.
114 // Otherwise use the given number of glyphs.
115 if( lastLine.ellipsis )
117 numberOfLaidOutGlyphs = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
121 numberOfLaidOutGlyphs = numberOfGlyphs;
124 if( 0u < numberOfLaidOutGlyphs )
126 // Retrieve from the visual model the glyphs and positions.
127 mImpl->mVisualModel->GetGlyphs( glyphs,
129 numberOfLaidOutGlyphs );
131 mImpl->mVisualModel->GetGlyphPositions( glyphPositions,
133 numberOfLaidOutGlyphs );
135 // Get the lines for the given range of glyphs.
136 // The lines contain the alignment offset which needs to be added to the glyph's position.
137 LineIndex firstLine = 0u;
138 Length numberOfLines = 0u;
139 mImpl->mVisualModel->GetNumberOfLines( glyphIndex,
140 numberOfLaidOutGlyphs,
144 Vector<LineRun> lines;
145 lines.Resize( numberOfLines );
146 LineRun* lineBuffer = lines.Begin();
148 mImpl->mVisualModel->GetLinesOfGlyphRange( lineBuffer,
150 numberOfLaidOutGlyphs );
152 // Get the first line for the given glyph range.
153 LineIndex lineIndex = firstLine;
154 LineRun* line = lineBuffer + lineIndex;
156 // Index of the last glyph of the line.
157 GlyphIndex lastGlyphIndexOfLine = line->glyphRun.glyphIndex + line->glyphRun.numberOfGlyphs - 1u;
159 // Add the alignment offset to the glyph's position.
161 minLineOffset = line->alignmentOffset;
162 float penY = line->ascender;
163 for( Length index = 0u; index < numberOfLaidOutGlyphs; ++index )
165 Vector2& position = *( glyphPositions + index );
166 position.x += line->alignmentOffset;
169 if( lastGlyphIndexOfLine == index )
171 penY += -line->descender;
173 // Get the next line.
176 if( lineIndex < numberOfLines )
178 line = lineBuffer + lineIndex;
179 minLineOffset = std::min( minLineOffset, line->alignmentOffset );
181 lastGlyphIndexOfLine = line->glyphRun.glyphIndex + line->glyphRun.numberOfGlyphs - 1u;
183 penY += line->ascender;
188 if( 1u == numberOfLaidOutGlyphs )
190 // not a point try to do ellipsis with only one laid out character.
191 return numberOfLaidOutGlyphs;
194 if( lastLine.ellipsis )
196 if( ( 1u == numberOfLines ) &&
197 ( lastLine.ascender - lastLine.descender > mImpl->mVisualModel->mControlSize.height ) )
199 // Get the first glyph which is going to be replaced and the ellipsis glyph.
200 GlyphInfo& glyphInfo = *glyphs;
201 const GlyphInfo& ellipsisGlyph = mImpl->mFontClient.GetEllipsisGlyph( mImpl->mFontClient.GetPointSize( glyphInfo.fontId ) );
203 // Change the 'x' and 'y' position of the ellipsis glyph.
204 Vector2& position = *glyphPositions;
205 position.x = ellipsisGlyph.xBearing;
206 position.y = mImpl->mVisualModel->mControlSize.height - ellipsisGlyph.yBearing;
208 // Replace the glyph by the ellipsis glyph.
209 glyphInfo = ellipsisGlyph;
214 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
215 float firstPenX = 0.f; // Used if rtl text is elided.
217 bool firstPenSet = false;
219 // Add the ellipsis glyph.
220 bool inserted = false;
221 float removedGlypsWidth = 0.f;
222 Length numberOfRemovedGlyphs = 0u;
223 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
225 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
228 const GlyphInfo& glyphToRemove = *( glyphs + index );
230 if( 0u != glyphToRemove.fontId )
232 // i.e. The font id of the glyph shaped from the '\n' character is zero.
234 // Need to reshape the glyph as the font may be different in size.
235 const GlyphInfo& ellipsisGlyph = mImpl->mFontClient.GetEllipsisGlyph( mImpl->mFontClient.GetPointSize( glyphToRemove.fontId ) );
239 const Vector2& position = *( glyphPositions + index );
241 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
242 penY = position.y + glyphToRemove.yBearing;
244 // Calculates the first penX which will be used if rtl text is elided.
245 firstPenX = position.x - glyphToRemove.xBearing;
246 if( firstPenX < -ellipsisGlyph.xBearing )
248 // Avoids to exceed the bounding box when rtl text is elided.
249 firstPenX = -ellipsisGlyph.xBearing;
252 removedGlypsWidth = -ellipsisGlyph.xBearing;
257 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
259 // Calculate the width of the ellipsis glyph and check if it fits.
260 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
261 if( ellipsisGlyphWidth < removedGlypsWidth )
263 GlyphInfo& glyphInfo = *( glyphs + index );
264 Vector2& position = *( glyphPositions + index );
265 position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
267 // Replace the glyph by the ellipsis glyph.
268 glyphInfo = ellipsisGlyph;
270 // Change the 'x' and 'y' position of the ellipsis glyph.
272 if( position.x > firstPenX )
274 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
277 position.x += ellipsisGlyph.xBearing;
278 position.y = penY - ellipsisGlyph.yBearing;
292 // No space for the ellipsis.
295 ++numberOfRemovedGlyphs;
299 // 'Removes' all the glyphs after the ellipsis glyph.
300 numberOfLaidOutGlyphs -= numberOfRemovedGlyphs;
306 return numberOfLaidOutGlyphs;
309 const Vector4* const View::GetColors() const
311 if( mImpl->mVisualModel )
313 return mImpl->mVisualModel->mColors.Begin();
319 const ColorIndex* const View::GetColorIndices() const
321 if( mImpl->mVisualModel )
323 return mImpl->mVisualModel->mColorIndices.Begin();
329 const Vector4& View::GetTextColor() const
331 if( mImpl->mVisualModel )
333 return mImpl->mVisualModel->GetTextColor();
335 return Vector4::ZERO;
338 const Vector2& View::GetShadowOffset() const
340 if( mImpl->mVisualModel )
342 return mImpl->mVisualModel->GetShadowOffset();
344 return Vector2::ZERO;
347 const Vector4& View::GetShadowColor() const
349 if( mImpl->mVisualModel )
351 return mImpl->mVisualModel->GetShadowColor();
353 return Vector4::ZERO;
356 const Vector4& View::GetUnderlineColor() const
358 if( mImpl->mVisualModel )
360 return mImpl->mVisualModel->GetUnderlineColor();
362 return Vector4::ZERO;
365 bool View::IsUnderlineEnabled() const
367 if( mImpl->mVisualModel )
369 return mImpl->mVisualModel->IsUnderlineEnabled();
374 float View::GetUnderlineHeight() const
376 if( mImpl->mVisualModel )
378 return mImpl->mVisualModel->GetUnderlineHeight();
383 Length View::GetNumberOfUnderlineRuns() const
385 if( mImpl->mVisualModel )
387 return mImpl->mVisualModel->GetNumberOfUnderlineRuns();
393 void View::GetUnderlineRuns( GlyphRun* underlineRuns,
394 UnderlineRunIndex index,
395 Length numberOfRuns ) const
397 if( mImpl->mVisualModel )
399 mImpl->mVisualModel->GetUnderlineRuns( underlineRuns,
407 } // namespace Toolkit