2 * Copyright (c) 2021 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/devel-api/text-abstraction/font-client.h>
23 #include <dali/public-api/math/vector2.h>
33 VisualModelPtr mVisualModel;
34 TextAbstraction::FontClient mFontClient; ///< Handle to the font client.
40 mImpl = new View::Impl();
42 mImpl->mFontClient = TextAbstraction::FontClient::Get();
50 void View::SetVisualModel(VisualModelPtr visualModel)
52 mImpl->mVisualModel = visualModel;
55 const Vector2& View::GetControlSize() const
57 if(mImpl->mVisualModel)
59 return mImpl->mVisualModel->mControlSize;
65 const Vector2& View::GetLayoutSize() const
67 if(mImpl->mVisualModel)
69 return mImpl->mVisualModel->GetLayoutSize();
75 Length View::GetNumberOfGlyphs() const
77 if(mImpl->mVisualModel)
79 const VisualModel& model = *mImpl->mVisualModel;
81 const Length glyphCount = model.mGlyphs.Count();
82 const Length positionCount = model.mGlyphPositions.Count();
84 DALI_ASSERT_DEBUG(positionCount <= glyphCount && "Invalid glyph positions in Model");
86 return (positionCount < glyphCount) ? positionCount : glyphCount;
92 Length View::GetGlyphs(GlyphInfo* glyphs,
93 Vector2* glyphPositions,
95 GlyphIndex glyphIndex,
96 Length numberOfGlyphs) const
98 Length numberOfLaidOutGlyphs = 0u;
100 if(mImpl->mVisualModel)
102 // If ellipsis is enabled, the number of glyphs the layout engine has laid out may be less than 'numberOfGlyphs'.
103 // Check the last laid out line to know if the layout engine elided some text.
105 const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
106 if(numberOfLines > 0u)
108 const LineRun& lastLine = *(mImpl->mVisualModel->mLines.Begin() + (numberOfLines - 1u));
110 // If ellipsis is enabled, calculate the number of laid out glyphs.
111 // Otherwise use the given number of glyphs.
112 if(lastLine.ellipsis)
114 numberOfLaidOutGlyphs = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
118 numberOfLaidOutGlyphs = numberOfGlyphs;
121 if(0u < numberOfLaidOutGlyphs)
123 // Retrieve from the visual model the glyphs and positions.
124 mImpl->mVisualModel->GetGlyphs(glyphs,
126 numberOfLaidOutGlyphs);
128 mImpl->mVisualModel->GetGlyphPositions(glyphPositions,
130 numberOfLaidOutGlyphs);
132 // Get the lines for the given range of glyphs.
133 // The lines contain the alignment offset which needs to be added to the glyph's position.
134 LineIndex firstLine = 0u;
135 Length numberOfLines = 0u;
136 mImpl->mVisualModel->GetNumberOfLines(glyphIndex,
137 numberOfLaidOutGlyphs,
141 Vector<LineRun> lines;
142 lines.Resize(numberOfLines);
143 LineRun* lineBuffer = lines.Begin();
145 mImpl->mVisualModel->GetLinesOfGlyphRange(lineBuffer,
147 numberOfLaidOutGlyphs);
149 // Get the first line for the given glyph range.
150 LineIndex lineIndex = firstLine;
151 LineRun* line = lineBuffer + lineIndex;
153 // Index of the last glyph of the line.
154 GlyphIndex lastGlyphIndexOfLine = line->glyphRun.glyphIndex + line->glyphRun.numberOfGlyphs - 1u;
156 // Add the alignment offset to the glyph's position.
158 minLineOffset = line->alignmentOffset;
159 float penY = line->ascender;
160 for(Length index = 0u; index < numberOfLaidOutGlyphs; ++index)
162 Vector2& position = *(glyphPositions + index);
163 position.x += line->alignmentOffset;
166 if(lastGlyphIndexOfLine == index)
168 penY += -line->descender;
170 // Get the next line.
173 if(lineIndex < numberOfLines)
175 line = lineBuffer + lineIndex;
176 minLineOffset = std::min(minLineOffset, line->alignmentOffset);
178 lastGlyphIndexOfLine = line->glyphRun.glyphIndex + line->glyphRun.numberOfGlyphs - 1u;
180 penY += line->ascender;
185 if(1u == numberOfLaidOutGlyphs)
187 // not a point try to do ellipsis with only one laid out character.
188 return numberOfLaidOutGlyphs;
191 if(lastLine.ellipsis)
193 if((1u == numberOfLines) &&
194 (lastLine.ascender - lastLine.descender > mImpl->mVisualModel->mControlSize.height))
196 // Get the first glyph which is going to be replaced and the ellipsis glyph.
197 GlyphInfo& glyphInfo = *glyphs;
198 const GlyphInfo& ellipsisGlyph = mImpl->mFontClient.GetEllipsisGlyph(mImpl->mFontClient.GetPointSize(glyphInfo.fontId));
200 // Change the 'x' and 'y' position of the ellipsis glyph.
201 Vector2& position = *glyphPositions;
202 position.x = ellipsisGlyph.xBearing;
203 position.y = mImpl->mVisualModel->mControlSize.height - ellipsisGlyph.yBearing;
205 // Replace the glyph by the ellipsis glyph.
206 glyphInfo = ellipsisGlyph;
211 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
212 float firstPenX = 0.f; // Used if rtl text is elided.
214 bool firstPenSet = false;
216 // Add the ellipsis glyph.
217 bool inserted = false;
218 float removedGlypsWidth = 0.f;
219 Length numberOfRemovedGlyphs = 0u;
220 GlyphIndex index = numberOfLaidOutGlyphs - 1u;
222 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
225 const GlyphInfo& glyphToRemove = *(glyphs + index);
227 if(0u != glyphToRemove.fontId)
229 // i.e. The font id of the glyph shaped from the '\n' character is zero.
231 // Need to reshape the glyph as the font may be different in size.
232 const GlyphInfo& ellipsisGlyph = mImpl->mFontClient.GetEllipsisGlyph(mImpl->mFontClient.GetPointSize(glyphToRemove.fontId));
236 const Vector2& position = *(glyphPositions + index);
238 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
239 penY = position.y + glyphToRemove.yBearing;
241 // Calculates the first penX which will be used if rtl text is elided.
242 firstPenX = position.x - glyphToRemove.xBearing;
243 if(firstPenX < -ellipsisGlyph.xBearing)
245 // Avoids to exceed the bounding box when rtl text is elided.
246 firstPenX = -ellipsisGlyph.xBearing;
249 removedGlypsWidth = -ellipsisGlyph.xBearing;
254 removedGlypsWidth += std::min(glyphToRemove.advance, (glyphToRemove.xBearing + glyphToRemove.width));
256 // Calculate the width of the ellipsis glyph and check if it fits.
257 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
258 if(ellipsisGlyphWidth < removedGlypsWidth)
260 GlyphInfo& glyphInfo = *(glyphs + index);
261 Vector2& position = *(glyphPositions + index);
262 position.x -= (0.f > glyphInfo.xBearing) ? glyphInfo.xBearing : 0.f;
264 // Replace the glyph by the ellipsis glyph.
265 glyphInfo = ellipsisGlyph;
267 // Change the 'x' and 'y' position of the ellipsis glyph.
269 if(position.x > firstPenX)
271 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
274 position.x += ellipsisGlyph.xBearing;
275 position.y = penY - ellipsisGlyph.yBearing;
289 // No space for the ellipsis.
292 ++numberOfRemovedGlyphs;
296 // 'Removes' all the glyphs after the ellipsis glyph.
297 numberOfLaidOutGlyphs -= numberOfRemovedGlyphs;
303 return numberOfLaidOutGlyphs;
306 const Vector4* const View::GetColors() const
308 if(mImpl->mVisualModel)
310 return mImpl->mVisualModel->mColors.Begin();
316 const ColorIndex* const View::GetColorIndices() const
318 if(mImpl->mVisualModel)
320 return mImpl->mVisualModel->mColorIndices.Begin();
326 const Vector4* const View::GetBackgroundColors() const
328 if(mImpl->mVisualModel)
330 return mImpl->mVisualModel->mBackgroundColors.Begin();
336 const ColorIndex* const View::GetBackgroundColorIndices() const
338 if(mImpl->mVisualModel)
340 return mImpl->mVisualModel->mBackgroundColorIndices.Begin();
346 const Vector4& View::GetTextColor() const
348 if(mImpl->mVisualModel)
350 return mImpl->mVisualModel->GetTextColor();
352 return Vector4::ZERO;
355 const Vector2& View::GetShadowOffset() const
357 if(mImpl->mVisualModel)
359 return mImpl->mVisualModel->GetShadowOffset();
361 return Vector2::ZERO;
364 const Vector4& View::GetShadowColor() const
366 if(mImpl->mVisualModel)
368 return mImpl->mVisualModel->GetShadowColor();
370 return Vector4::ZERO;
373 const Vector4& View::GetUnderlineColor() const
375 if(mImpl->mVisualModel)
377 return mImpl->mVisualModel->GetUnderlineColor();
379 return Vector4::ZERO;
382 bool View::IsUnderlineEnabled() const
384 if(mImpl->mVisualModel)
386 return mImpl->mVisualModel->IsUnderlineEnabled();
391 const GlyphInfo* View::GetHyphens() const
393 if(mImpl->mVisualModel)
395 return mImpl->mVisualModel->mHyphen.glyph.Begin();
401 const Length* View::GetHyphenIndices() const
403 if(mImpl->mVisualModel)
405 return mImpl->mVisualModel->mHyphen.index.Begin();
411 Length View::GetHyphensCount() const
413 if(mImpl->mVisualModel)
415 return mImpl->mVisualModel->mHyphen.glyph.Size();
420 float View::GetUnderlineHeight() const
422 if(mImpl->mVisualModel)
424 return mImpl->mVisualModel->GetUnderlineHeight();
429 Length View::GetNumberOfUnderlineRuns() const
431 if(mImpl->mVisualModel)
433 return mImpl->mVisualModel->GetNumberOfUnderlineRuns();
439 void View::GetUnderlineRuns(GlyphRun* underlineRuns,
440 UnderlineRunIndex index,
441 Length numberOfRuns) const
443 if(mImpl->mVisualModel)
445 mImpl->mVisualModel->GetUnderlineRuns(underlineRuns,
451 const Vector4& View::GetOutlineColor() const
453 if(mImpl->mVisualModel)
455 return mImpl->mVisualModel->GetOutlineColor();
457 return Vector4::ZERO;
460 uint16_t View::GetOutlineWidth() const
462 if(mImpl->mVisualModel)
464 return mImpl->mVisualModel->GetOutlineWidth();
471 } // namespace Toolkit