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 // Retrieve from the visual model the glyphs and positions.
114 mImpl->mVisualModel->GetGlyphs( glyphs,
116 numberOfLaidOutGlyphs );
118 mImpl->mVisualModel->GetGlyphPositions( glyphPositions,
120 numberOfLaidOutGlyphs );
122 // Get the lines for the given range of glyphs.
123 // The lines contain the alignment offset which needs to be added to the glyph's position.
124 LineIndex firstLine = 0u;
125 Length numberOfLines = 0u;
126 mImpl->mVisualModel->GetNumberOfLines( glyphIndex,
127 numberOfLaidOutGlyphs,
131 Vector<LineRun> lines;
132 lines.Resize( numberOfLines );
133 LineRun* lineBuffer = lines.Begin();
135 mImpl->mVisualModel->GetLinesOfGlyphRange( lineBuffer,
137 numberOfLaidOutGlyphs );
139 // Get the first line for the given glyph range.
140 LineIndex lineIndex = firstLine;
141 LineRun* line = lineBuffer + lineIndex;
143 // Index of the last glyph of the line.
144 GlyphIndex lastGlyphIndexOfLine = line->glyphRun.glyphIndex + line->glyphRun.numberOfGlyphs - 1u;
146 // Add the alignment offset to the glyph's position.
147 for( Length index = 0u; index < numberOfLaidOutGlyphs; ++index )
149 ( *( glyphPositions + index ) ).x += line->alignmentOffset;
151 if( lastGlyphIndexOfLine == index )
153 // Get the next line.
156 if( lineIndex < numberOfLines )
158 line = lineBuffer + lineIndex;
159 lastGlyphIndexOfLine = line->glyphRun.glyphIndex + line->glyphRun.numberOfGlyphs - 1u;
164 if( 1u == numberOfLaidOutGlyphs )
166 // not a point try to do ellipsis with only one laid out character.
167 return numberOfLaidOutGlyphs;
170 if( lastLine.ellipsis )
172 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
173 float firstPenX = 0.f; // Used if rtl text is elided.
175 bool firstPenSet = false;
177 // Add the ellipsis glyph.
178 bool inserted = false;
179 float removedGlypsWidth = 0.f;
180 Length numberOfRemovedGlyphs = 0u;
181 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
183 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
186 const GlyphInfo& glyphToRemove = *( glyphs + index );
188 if( 0u != glyphToRemove.fontId )
190 // i.e. The font id of the glyph shaped from the '\n' character is zero.
192 // Need to reshape the glyph as the font may be different in size.
193 const GlyphInfo& ellipsisGlyph = mImpl->mFontClient.GetEllipsisGlyph( mImpl->mFontClient.GetPointSize( glyphToRemove.fontId ) );
197 const Vector2& position = *( glyphPositions + index );
199 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
200 penY = position.y + glyphToRemove.yBearing;
202 // Calculates the first penX which will be used if rtl text is elided.
203 firstPenX = position.x - glyphToRemove.xBearing;
204 if( firstPenX < -ellipsisGlyph.xBearing )
206 // Avoids to exceed the bounding box when rtl text is elided.
207 firstPenX = -ellipsisGlyph.xBearing;
210 removedGlypsWidth = -ellipsisGlyph.xBearing;
215 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
217 // Calculate the width of the ellipsis glyph and check if it fits.
218 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
219 if( ellipsisGlyphWidth < removedGlypsWidth )
221 GlyphInfo& glyphInfo = *( glyphs + index );
222 Vector2& position = *( glyphPositions + index );
223 position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
225 // Replace the glyph by the ellipsis glyph.
226 glyphInfo = ellipsisGlyph;
228 // Change the 'x' and 'y' position of the ellipsis glyph.
230 if( position.x > firstPenX )
232 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
235 position.x += ellipsisGlyph.xBearing;
236 position.y = penY - ellipsisGlyph.yBearing;
250 // No space for the ellipsis.
253 ++numberOfRemovedGlyphs;
257 // 'Removes' all the glyphs after the ellipsis glyph.
258 numberOfLaidOutGlyphs -= numberOfRemovedGlyphs;
263 return numberOfLaidOutGlyphs;
266 const Vector4& View::GetTextColor() const
268 if( mImpl->mVisualModel )
270 return mImpl->mVisualModel->GetTextColor();
272 return Vector4::ZERO;
275 const Vector2& View::GetShadowOffset() const
277 if( mImpl->mVisualModel )
279 return mImpl->mVisualModel->GetShadowOffset();
281 return Vector2::ZERO;
284 const Vector4& View::GetShadowColor() const
286 if( mImpl->mVisualModel )
288 return mImpl->mVisualModel->GetShadowColor();
290 return Vector4::ZERO;
293 const Vector4& View::GetUnderlineColor() const
295 if( mImpl->mVisualModel )
297 return mImpl->mVisualModel->GetUnderlineColor();
299 return Vector4::ZERO;
302 bool View::IsUnderlineEnabled() const
304 if( mImpl->mVisualModel )
306 return mImpl->mVisualModel->IsUnderlineEnabled();
311 float View::GetUnderlineHeight() const
313 if( mImpl->mVisualModel )
315 return mImpl->mVisualModel->GetUnderlineHeight();
320 Length View::GetNumberOfUnderlineRuns() const
322 if( mImpl->mVisualModel )
324 return mImpl->mVisualModel->mUnderlineRuns.Count();
330 void View::GetUnderlineRuns( GlyphRun* underlineRuns,
331 UnderlineRunIndex index,
332 Length numberOfRuns ) const
334 if( mImpl->mVisualModel )
336 mImpl->mVisualModel->GetUnderlineRuns( underlineRuns,
344 } // namespace Toolkit