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,
88 GlyphIndex glyphIndex,
89 Length numberOfGlyphs ) const
91 Length numberOfLaidOutGlyphs = 0u;
93 if( mImpl->mVisualModel )
95 // If ellipsis is enabled, the number of glyphs the layout engine has laid out may be less than 'numberOfGlyphs'.
96 // Check the last laid out line to know if the layout engine elided some text.
98 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
99 if( numberOfLines > 0u )
101 const LineRun& lastLine = *( mImpl->mVisualModel->mLines.Begin() + ( numberOfLines - 1u ) );
103 // If ellipsis is enabled, calculate the number of laid out glyphs.
104 // Otherwise use the given number of glyphs.
105 if( lastLine.ellipsis )
107 numberOfLaidOutGlyphs = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
111 numberOfLaidOutGlyphs = numberOfGlyphs;
114 if( 0u < numberOfLaidOutGlyphs )
116 // Retrieve from the visual model the glyphs and positions.
117 mImpl->mVisualModel->GetGlyphs( glyphs,
119 numberOfLaidOutGlyphs );
121 mImpl->mVisualModel->GetGlyphPositions( glyphPositions,
123 numberOfLaidOutGlyphs );
125 // Get the lines for the given range of glyphs.
126 // The lines contain the alignment offset which needs to be added to the glyph's position.
127 LineIndex firstLine = 0u;
128 Length numberOfLines = 0u;
129 mImpl->mVisualModel->GetNumberOfLines( glyphIndex,
130 numberOfLaidOutGlyphs,
134 Vector<LineRun> lines;
135 lines.Resize( numberOfLines );
136 LineRun* lineBuffer = lines.Begin();
138 mImpl->mVisualModel->GetLinesOfGlyphRange( lineBuffer,
140 numberOfLaidOutGlyphs );
142 // Get the first line for the given glyph range.
143 LineIndex lineIndex = firstLine;
144 LineRun* line = lineBuffer + lineIndex;
146 // Index of the last glyph of the line.
147 GlyphIndex lastGlyphIndexOfLine = line->glyphRun.glyphIndex + line->glyphRun.numberOfGlyphs - 1u;
149 // Add the alignment offset to the glyph's position.
150 for( Length index = 0u; index < numberOfLaidOutGlyphs; ++index )
152 ( *( glyphPositions + index ) ).x += line->alignmentOffset;
154 if( lastGlyphIndexOfLine == index )
156 // Get the next line.
159 if( lineIndex < numberOfLines )
161 line = lineBuffer + lineIndex;
162 lastGlyphIndexOfLine = line->glyphRun.glyphIndex + line->glyphRun.numberOfGlyphs - 1u;
167 if( 1u == numberOfLaidOutGlyphs )
169 // not a point try to do ellipsis with only one laid out character.
170 return numberOfLaidOutGlyphs;
173 if( lastLine.ellipsis )
175 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
176 float firstPenX = 0.f; // Used if rtl text is elided.
178 bool firstPenSet = false;
180 // Add the ellipsis glyph.
181 bool inserted = false;
182 float removedGlypsWidth = 0.f;
183 Length numberOfRemovedGlyphs = 0u;
184 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
186 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
189 const GlyphInfo& glyphToRemove = *( glyphs + index );
191 if( 0u != glyphToRemove.fontId )
193 // i.e. The font id of the glyph shaped from the '\n' character is zero.
195 // Need to reshape the glyph as the font may be different in size.
196 const GlyphInfo& ellipsisGlyph = mImpl->mFontClient.GetEllipsisGlyph( mImpl->mFontClient.GetPointSize( glyphToRemove.fontId ) );
200 const Vector2& position = *( glyphPositions + index );
202 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
203 penY = position.y + glyphToRemove.yBearing;
205 // Calculates the first penX which will be used if rtl text is elided.
206 firstPenX = position.x - glyphToRemove.xBearing;
207 if( firstPenX < -ellipsisGlyph.xBearing )
209 // Avoids to exceed the bounding box when rtl text is elided.
210 firstPenX = -ellipsisGlyph.xBearing;
213 removedGlypsWidth = -ellipsisGlyph.xBearing;
218 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
220 // Calculate the width of the ellipsis glyph and check if it fits.
221 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
222 if( ellipsisGlyphWidth < removedGlypsWidth )
224 GlyphInfo& glyphInfo = *( glyphs + index );
225 Vector2& position = *( glyphPositions + index );
226 position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
228 // Replace the glyph by the ellipsis glyph.
229 glyphInfo = ellipsisGlyph;
231 // Change the 'x' and 'y' position of the ellipsis glyph.
233 if( position.x > firstPenX )
235 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
238 position.x += ellipsisGlyph.xBearing;
239 position.y = penY - ellipsisGlyph.yBearing;
253 // No space for the ellipsis.
256 ++numberOfRemovedGlyphs;
260 // 'Removes' all the glyphs after the ellipsis glyph.
261 numberOfLaidOutGlyphs -= numberOfRemovedGlyphs;
267 return numberOfLaidOutGlyphs;
270 const Vector4& View::GetTextColor() const
272 if( mImpl->mVisualModel )
274 return mImpl->mVisualModel->GetTextColor();
276 return Vector4::ZERO;
279 const Vector2& View::GetShadowOffset() const
281 if( mImpl->mVisualModel )
283 return mImpl->mVisualModel->GetShadowOffset();
285 return Vector2::ZERO;
288 const Vector4& View::GetShadowColor() const
290 if( mImpl->mVisualModel )
292 return mImpl->mVisualModel->GetShadowColor();
294 return Vector4::ZERO;
297 const Vector4& View::GetUnderlineColor() const
299 if( mImpl->mVisualModel )
301 return mImpl->mVisualModel->GetUnderlineColor();
303 return Vector4::ZERO;
306 bool View::IsUnderlineEnabled() const
308 if( mImpl->mVisualModel )
310 return mImpl->mVisualModel->IsUnderlineEnabled();
315 float View::GetUnderlineHeight() const
317 if( mImpl->mVisualModel )
319 return mImpl->mVisualModel->GetUnderlineHeight();
324 Length View::GetNumberOfUnderlineRuns() const
326 if( mImpl->mVisualModel )
328 return mImpl->mVisualModel->mUnderlineRuns.Count();
334 void View::GetUnderlineRuns( GlyphRun* underlineRuns,
335 UnderlineRunIndex index,
336 Length numberOfRuns ) const
338 if( mImpl->mVisualModel )
340 mImpl->mVisualModel->GetUnderlineRuns( underlineRuns,
348 } // namespace Toolkit