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,
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 );
136 const GlyphIndex lastLaidOutGlyphIndex = glyphIndex + numberOfLaidOutGlyphs;
138 for( Vector<ColorGlyphRun>::ConstIterator it = mImpl->mVisualModel->mColorRuns.Begin(),
139 endIt = mImpl->mVisualModel->mColorRuns.End();
143 const ColorGlyphRun& colorGlyphRun = *it;
144 const GlyphIndex lastGlyphIndex = colorGlyphRun.glyphRun.glyphIndex + colorGlyphRun.glyphRun.numberOfGlyphs;
146 if( ( colorGlyphRun.glyphRun.glyphIndex < lastLaidOutGlyphIndex ) &&
147 ( glyphIndex < lastGlyphIndex ) )
149 for( GlyphIndex index = glyphIndex < colorGlyphRun.glyphRun.glyphIndex ? colorGlyphRun.glyphRun.glyphIndex : glyphIndex,
150 endIndex = lastLaidOutGlyphIndex < lastGlyphIndex ? lastLaidOutGlyphIndex : lastGlyphIndex;
154 *( glyphColors + index - glyphIndex ) = colorGlyphRun.color;
159 // Get the lines for the given range of glyphs.
160 // The lines contain the alignment offset which needs to be added to the glyph's position.
161 LineIndex firstLine = 0u;
162 Length numberOfLines = 0u;
163 mImpl->mVisualModel->GetNumberOfLines( glyphIndex,
164 numberOfLaidOutGlyphs,
168 Vector<LineRun> lines;
169 lines.Resize( numberOfLines );
170 LineRun* lineBuffer = lines.Begin();
172 mImpl->mVisualModel->GetLinesOfGlyphRange( lineBuffer,
174 numberOfLaidOutGlyphs );
176 // Get the first line for the given glyph range.
177 LineIndex lineIndex = firstLine;
178 LineRun* line = lineBuffer + lineIndex;
180 // Index of the last glyph of the line.
181 GlyphIndex lastGlyphIndexOfLine = line->glyphRun.glyphIndex + line->glyphRun.numberOfGlyphs - 1u;
183 // Add the alignment offset to the glyph's position.
184 for( Length index = 0u; index < numberOfLaidOutGlyphs; ++index )
186 ( *( glyphPositions + index ) ).x += line->alignmentOffset;
188 if( lastGlyphIndexOfLine == index )
190 // Get the next line.
193 if( lineIndex < numberOfLines )
195 line = lineBuffer + lineIndex;
196 lastGlyphIndexOfLine = line->glyphRun.glyphIndex + line->glyphRun.numberOfGlyphs - 1u;
201 if( 1u == numberOfLaidOutGlyphs )
203 // not a point try to do ellipsis with only one laid out character.
204 return numberOfLaidOutGlyphs;
207 if( lastLine.ellipsis )
209 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
210 float firstPenX = 0.f; // Used if rtl text is elided.
212 bool firstPenSet = false;
214 // Add the ellipsis glyph.
215 bool inserted = false;
216 float removedGlypsWidth = 0.f;
217 Length numberOfRemovedGlyphs = 0u;
218 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
220 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
223 const GlyphInfo& glyphToRemove = *( glyphs + index );
225 if( 0u != glyphToRemove.fontId )
227 // i.e. The font id of the glyph shaped from the '\n' character is zero.
229 // Need to reshape the glyph as the font may be different in size.
230 const GlyphInfo& ellipsisGlyph = mImpl->mFontClient.GetEllipsisGlyph( mImpl->mFontClient.GetPointSize( glyphToRemove.fontId ) );
234 const Vector2& position = *( glyphPositions + index );
236 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
237 penY = position.y + glyphToRemove.yBearing;
239 // Calculates the first penX which will be used if rtl text is elided.
240 firstPenX = position.x - glyphToRemove.xBearing;
241 if( firstPenX < -ellipsisGlyph.xBearing )
243 // Avoids to exceed the bounding box when rtl text is elided.
244 firstPenX = -ellipsisGlyph.xBearing;
247 removedGlypsWidth = -ellipsisGlyph.xBearing;
252 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
254 // Calculate the width of the ellipsis glyph and check if it fits.
255 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
256 if( ellipsisGlyphWidth < removedGlypsWidth )
258 GlyphInfo& glyphInfo = *( glyphs + index );
259 Vector2& position = *( glyphPositions + index );
260 position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
262 // Replace the glyph by the ellipsis glyph.
263 glyphInfo = ellipsisGlyph;
265 // Change the 'x' and 'y' position of the ellipsis glyph.
267 if( position.x > firstPenX )
269 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
272 position.x += ellipsisGlyph.xBearing;
273 position.y = penY - ellipsisGlyph.yBearing;
287 // No space for the ellipsis.
290 ++numberOfRemovedGlyphs;
294 // 'Removes' all the glyphs after the ellipsis glyph.
295 numberOfLaidOutGlyphs -= numberOfRemovedGlyphs;
301 return numberOfLaidOutGlyphs;
304 const Vector4& View::GetTextColor() const
306 if( mImpl->mVisualModel )
308 return mImpl->mVisualModel->GetTextColor();
310 return Vector4::ZERO;
313 const Vector2& View::GetShadowOffset() const
315 if( mImpl->mVisualModel )
317 return mImpl->mVisualModel->GetShadowOffset();
319 return Vector2::ZERO;
322 const Vector4& View::GetShadowColor() const
324 if( mImpl->mVisualModel )
326 return mImpl->mVisualModel->GetShadowColor();
328 return Vector4::ZERO;
331 const Vector4& View::GetUnderlineColor() const
333 if( mImpl->mVisualModel )
335 return mImpl->mVisualModel->GetUnderlineColor();
337 return Vector4::ZERO;
340 bool View::IsUnderlineEnabled() const
342 if( mImpl->mVisualModel )
344 return mImpl->mVisualModel->IsUnderlineEnabled();
349 float View::GetUnderlineHeight() const
351 if( mImpl->mVisualModel )
353 return mImpl->mVisualModel->GetUnderlineHeight();
358 Length View::GetNumberOfUnderlineRuns() const
360 if( mImpl->mVisualModel )
362 return mImpl->mVisualModel->mUnderlineRuns.Count();
368 void View::GetUnderlineRuns( GlyphRun* underlineRuns,
369 UnderlineRunIndex index,
370 Length numberOfRuns ) const
372 if( mImpl->mVisualModel )
374 mImpl->mVisualModel->GetUnderlineRuns( underlineRuns,
382 } // namespace Toolkit