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::GetNumberOfGlyphs() const
70 if( mImpl->mVisualModel )
72 const VisualModel& model = *mImpl->mVisualModel;
74 const Length glyphCount = model.mGlyphs.Count();
75 const Length positionCount = model.mGlyphPositions.Count();
77 DALI_ASSERT_DEBUG( positionCount <= glyphCount && "Invalid glyph positions in Model" );
79 return (positionCount < glyphCount) ? positionCount : glyphCount;
85 Length View::GetGlyphs( GlyphInfo* glyphs,
86 Vector2* glyphPositions,
87 GlyphIndex glyphIndex,
88 Length numberOfGlyphs ) const
90 Length numberOfLaidOutGlyphs = 0u;
92 if( mImpl->mVisualModel )
94 // If ellipsis is enabled, the number of glyphs the layout engine has laid out may be less than 'numberOfGlyphs'.
95 // Check the last laid out line to know if the layout engine elided some text.
97 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
98 if( numberOfLines > 0u )
100 const LineRun& lastLine = *( mImpl->mVisualModel->mLines.Begin() + ( numberOfLines - 1u ) );
102 // If ellipsis is enabled, calculate the number of laid out glyphs.
103 // Otherwise use the given number of glyphs.
104 if( lastLine.ellipsis )
106 numberOfLaidOutGlyphs = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
110 numberOfLaidOutGlyphs = numberOfGlyphs;
113 if( 0u < numberOfLaidOutGlyphs )
115 // Retrieve from the visual model the glyphs and positions.
116 mImpl->mVisualModel->GetGlyphs( glyphs,
118 numberOfLaidOutGlyphs );
120 mImpl->mVisualModel->GetGlyphPositions( glyphPositions,
122 numberOfLaidOutGlyphs );
124 // Get the lines for the given range of glyphs.
125 // The lines contain the alignment offset which needs to be added to the glyph's position.
126 LineIndex firstLine = 0u;
127 Length numberOfLines = 0u;
128 mImpl->mVisualModel->GetNumberOfLines( glyphIndex,
129 numberOfLaidOutGlyphs,
133 Vector<LineRun> lines;
134 lines.Resize( numberOfLines );
135 LineRun* lineBuffer = lines.Begin();
137 mImpl->mVisualModel->GetLinesOfGlyphRange( lineBuffer,
139 numberOfLaidOutGlyphs );
141 // Get the first line for the given glyph range.
142 LineIndex lineIndex = firstLine;
143 LineRun* line = lineBuffer + lineIndex;
145 // Index of the last glyph of the line.
146 GlyphIndex lastGlyphIndexOfLine = line->glyphRun.glyphIndex + line->glyphRun.numberOfGlyphs - 1u;
148 // Add the alignment offset to the glyph's position.
149 for( Length index = 0u; index < numberOfLaidOutGlyphs; ++index )
151 ( *( glyphPositions + index ) ).x += line->alignmentOffset;
153 if( lastGlyphIndexOfLine == index )
155 // Get the next line.
158 if( lineIndex < numberOfLines )
160 line = lineBuffer + lineIndex;
161 lastGlyphIndexOfLine = line->glyphRun.glyphIndex + line->glyphRun.numberOfGlyphs - 1u;
166 if( 1u == numberOfLaidOutGlyphs )
168 // not a point try to do ellipsis with only one laid out character.
169 return numberOfLaidOutGlyphs;
172 if( lastLine.ellipsis )
174 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
175 float firstPenX = 0.f; // Used if rtl text is elided.
177 bool firstPenSet = false;
179 // Add the ellipsis glyph.
180 bool inserted = false;
181 float removedGlypsWidth = 0.f;
182 Length numberOfRemovedGlyphs = 0u;
183 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
185 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
188 const GlyphInfo& glyphToRemove = *( glyphs + index );
190 if( 0u != glyphToRemove.fontId )
192 // i.e. The font id of the glyph shaped from the '\n' character is zero.
194 // Need to reshape the glyph as the font may be different in size.
195 const GlyphInfo& ellipsisGlyph = mImpl->mFontClient.GetEllipsisGlyph( mImpl->mFontClient.GetPointSize( glyphToRemove.fontId ) );
199 const Vector2& position = *( glyphPositions + index );
201 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
202 penY = position.y + glyphToRemove.yBearing;
204 // Calculates the first penX which will be used if rtl text is elided.
205 firstPenX = position.x - glyphToRemove.xBearing;
206 if( firstPenX < -ellipsisGlyph.xBearing )
208 // Avoids to exceed the bounding box when rtl text is elided.
209 firstPenX = -ellipsisGlyph.xBearing;
212 removedGlypsWidth = -ellipsisGlyph.xBearing;
217 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
219 // Calculate the width of the ellipsis glyph and check if it fits.
220 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
221 if( ellipsisGlyphWidth < removedGlypsWidth )
223 GlyphInfo& glyphInfo = *( glyphs + index );
224 Vector2& position = *( glyphPositions + index );
225 position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
227 // Replace the glyph by the ellipsis glyph.
228 glyphInfo = ellipsisGlyph;
230 // Change the 'x' and 'y' position of the ellipsis glyph.
232 if( position.x > firstPenX )
234 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
237 position.x += ellipsisGlyph.xBearing;
238 position.y = penY - ellipsisGlyph.yBearing;
252 // No space for the ellipsis.
255 ++numberOfRemovedGlyphs;
259 // 'Removes' all the glyphs after the ellipsis glyph.
260 numberOfLaidOutGlyphs -= numberOfRemovedGlyphs;
266 return numberOfLaidOutGlyphs;
269 const Vector4& View::GetTextColor() const
271 if( mImpl->mVisualModel )
273 return mImpl->mVisualModel->GetTextColor();
275 return Vector4::ZERO;
278 const Vector2& View::GetShadowOffset() const
280 if( mImpl->mVisualModel )
282 return mImpl->mVisualModel->GetShadowOffset();
284 return Vector2::ZERO;
287 const Vector4& View::GetShadowColor() const
289 if( mImpl->mVisualModel )
291 return mImpl->mVisualModel->GetShadowColor();
293 return Vector4::ZERO;
296 const Vector4& View::GetUnderlineColor() const
298 if( mImpl->mVisualModel )
300 return mImpl->mVisualModel->GetUnderlineColor();
302 return Vector4::ZERO;
305 bool View::IsUnderlineEnabled() const
307 if( mImpl->mVisualModel )
309 return mImpl->mVisualModel->IsUnderlineEnabled();
314 float View::GetUnderlineHeight() const
316 if( mImpl->mVisualModel )
318 return mImpl->mVisualModel->GetUnderlineHeight();
323 Length View::GetNumberOfUnderlineRuns() const
325 if( mImpl->mVisualModel )
327 return mImpl->mVisualModel->mUnderlineRuns.Count();
333 void View::GetUnderlineRuns( GlyphRun* underlineRuns,
334 UnderlineRunIndex index,
335 Length numberOfRuns ) const
337 if( mImpl->mVisualModel )
339 mImpl->mVisualModel->GetUnderlineRuns( underlineRuns,
347 } // namespace Toolkit