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/layouts/layout-engine.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/devel-api/text-abstraction/font-client.h>
27 #include <dali-toolkit/internal/text/bidirectional-line-info-run.h>
28 #include <dali-toolkit/internal/text/cursor-helper-functions.h>
29 #include <dali-toolkit/internal/text/glyph-metrics-helper.h>
30 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
44 #if defined(DEBUG_ENABLED)
45 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_LAYOUT");
48 const float MAX_FLOAT = std::numeric_limits<float>::max();
49 const bool RTL = true;
50 const float CURSOR_WIDTH = 1.f;
51 const float LINE_SPACING= 0.f;
56 * @brief Stores temporary layout info of the line.
64 numberOfCharacters( 0u ),
68 wsLengthEndOfLine( 0.f ),
70 descender( MAX_FLOAT )
81 numberOfCharacters = 0u;
85 wsLengthEndOfLine = 0.f;
87 descender = MAX_FLOAT;
90 GlyphIndex glyphIndex; ///< Index of the first glyph to be laid-out.
91 CharacterIndex characterIndex; ///< Index of the first character to be laid-out.
92 Length numberOfGlyphs; ///< The number of glyph which fit in one line.
93 Length numberOfCharacters; ///< The number of characters which fit in one line.
94 float length; ///< The addition of the advance metric of all the glyphs which fit in one line.
95 float extraBearing; ///< The extra width to be added to the line's length when the bearing of the first glyph is negative.
96 float extraWidth; ///< The extra width to be added to the line's length when the bearing + width of the last glyph is greater than the advance.
97 float wsLengthEndOfLine; ///< The length of the white spaces at the end of the line.
98 float ascender; ///< The maximum ascender of all fonts in the line.
99 float descender; ///< The minimum descender of all fonts in the line.
102 struct LayoutEngine::Impl
105 : mLayout( LayoutEngine::SINGLE_LINE_BOX ),
106 mHorizontalAlignment( LayoutEngine::HORIZONTAL_ALIGN_BEGIN ),
107 mVerticalAlignment( LayoutEngine::VERTICAL_ALIGN_TOP ),
108 mCursorWidth( CURSOR_WIDTH ),
109 mDefaultLineSpacing( LINE_SPACING ),
110 mEllipsisEnabled( false )
115 * @brief Updates the line ascender and descender with the metrics of a new font.
117 * @param[in] fontId The id of the new font.
118 * @param[in,out] lineLayout The line layout.
120 void UpdateLineHeight( FontId fontId, LineLayout& lineLayout )
122 Text::FontMetrics fontMetrics;
123 mMetrics->GetFontMetrics( fontId, fontMetrics );
125 // Sets the maximum ascender.
126 if( fontMetrics.ascender > lineLayout.ascender )
128 lineLayout.ascender = fontMetrics.ascender;
131 // Sets the minimum descender.
132 if( fontMetrics.descender < lineLayout.descender )
134 lineLayout.descender = fontMetrics.descender;
139 * @brief Merges a temporary line layout into the line layout.
141 * @param[in,out] lineLayout The line layout.
142 * @param[in] tmpLineLayout A temporary line layout.
144 void MergeLineLayout( LineLayout& lineLayout,
145 const LineLayout& tmpLineLayout )
147 lineLayout.numberOfCharacters += tmpLineLayout.numberOfCharacters;
148 lineLayout.numberOfGlyphs += tmpLineLayout.numberOfGlyphs;
149 lineLayout.length += tmpLineLayout.length;
151 if( 0.f < tmpLineLayout.length )
153 lineLayout.length += lineLayout.wsLengthEndOfLine;
155 lineLayout.wsLengthEndOfLine = tmpLineLayout.wsLengthEndOfLine;
159 lineLayout.wsLengthEndOfLine += tmpLineLayout.wsLengthEndOfLine;
162 if( tmpLineLayout.ascender > lineLayout.ascender )
164 lineLayout.ascender = tmpLineLayout.ascender;
167 if( tmpLineLayout.descender < lineLayout.descender )
169 lineLayout.descender = tmpLineLayout.descender;
174 * Retrieves the line layout for a given box width.
176 * @note This method lais out text as it were left to right. At this point is not possible to reorder the line
177 * because the number of characters of the line is not known (one of the responsabilities of this method
178 * is calculate that). Due to glyph's 'x' bearing, width and advance, when right to left or mixed right to left
179 * and left to right text is laid-out, it can be small differences in the line length. One solution is to
180 * reorder and re-lay out the text after this method and add or remove one extra glyph if needed. However,
181 * this method calculates which are the first and last glyphs of the line (the ones that causes the
182 * differences). This is a good point to check if there is problems with the text exceeding the boundaries
183 * of the control when there is right to left text.
185 * @param[in] parameters The layout parameters.
186 * @param[out] lineLayout The line layout.
187 * @param[in,out] paragraphDirection in: the current paragraph's direction, out: the next paragraph's direction. Is set after a must break.
188 * @param[in] completelyFill Whether to completely fill the line ( even if the last word exceeds the boundaries ).
190 void GetLineLayoutForBox( const LayoutParameters& parameters,
191 LineLayout& lineLayout,
192 CharacterDirection& paragraphDirection,
193 bool completelyFill )
195 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->GetLineLayoutForBox\n" );
196 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " initial glyph index : %d\n", lineLayout.glyphIndex );
197 // Stores temporary line layout which has not been added to the final line layout.
198 LineLayout tmpLineLayout;
200 const bool isMultiline = mLayout == MULTI_LINE_BOX;
202 // The last glyph to be laid-out.
203 const GlyphIndex lastGlyphOfParagraphPlusOne = parameters.startGlyphIndex + parameters.numberOfGlyphs;
205 // If the first glyph has a negative bearing its absolute value needs to be added to the line length.
206 // In the case the line starts with a right to left character, if the width is longer than the advance,
207 // the difference needs to be added to the line length.
209 // Check whether the first glyph comes from a character that is shaped in multiple glyphs.
210 const Length numberOfGLyphsInGroup = GetNumberOfGlyphsOfGroup( lineLayout.glyphIndex,
211 lastGlyphOfParagraphPlusOne,
212 parameters.charactersPerGlyphBuffer );
214 GlyphMetrics glyphMetrics;
215 GetGlyphsMetrics( lineLayout.glyphIndex,
216 numberOfGLyphsInGroup,
218 parameters.glyphsBuffer,
221 // Set the direction of the first character of the line.
222 lineLayout.characterIndex = *( parameters.glyphsToCharactersBuffer + lineLayout.glyphIndex );
223 const CharacterDirection firstCharacterDirection = ( NULL == parameters.characterDirectionBuffer ) ? false : *( parameters.characterDirectionBuffer + lineLayout.characterIndex );
224 CharacterDirection previousCharacterDirection = firstCharacterDirection;
226 const float extraWidth = glyphMetrics.xBearing + glyphMetrics.width - glyphMetrics.advance;
227 float tmpExtraWidth = ( 0.f < extraWidth ) ? extraWidth : 0.f;
229 float tmpExtraBearing = ( 0.f > glyphMetrics.xBearing ) ? -glyphMetrics.xBearing : 0.f;
231 tmpLineLayout.length += mCursorWidth; // Added to give some space to the cursor.
233 // Calculate the line height if there is no characters.
234 FontId lastFontId = glyphMetrics.fontId;
235 UpdateLineHeight( lastFontId, tmpLineLayout );
237 bool oneWordLaidOut = false;
239 for( GlyphIndex glyphIndex = lineLayout.glyphIndex;
240 glyphIndex < lastGlyphOfParagraphPlusOne; )
242 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " glyph index : %d\n", glyphIndex );
244 // Check whether this glyph comes from a character that is shaped in multiple glyphs.
245 const Length numberOfGLyphsInGroup = GetNumberOfGlyphsOfGroup( glyphIndex,
246 lastGlyphOfParagraphPlusOne,
247 parameters.charactersPerGlyphBuffer );
249 GlyphMetrics glyphMetrics;
250 GetGlyphsMetrics( glyphIndex,
251 numberOfGLyphsInGroup,
253 parameters.glyphsBuffer,
256 const bool isLastGlyph = glyphIndex + numberOfGLyphsInGroup == parameters.totalNumberOfGlyphs;
258 // Check if the font of the current glyph is the same of the previous one.
259 // If it's different the ascender and descender need to be updated.
260 if( lastFontId != glyphMetrics.fontId )
262 UpdateLineHeight( glyphMetrics.fontId, tmpLineLayout );
263 lastFontId = glyphMetrics.fontId;
266 // Get the character indices for the current glyph. The last character index is needed
267 // because there are glyphs formed by more than one character but their break info is
268 // given only for the last character.
269 const Length charactersPerGlyph = *( parameters.charactersPerGlyphBuffer + glyphIndex + numberOfGLyphsInGroup - 1u );
270 const bool hasCharacters = charactersPerGlyph > 0u;
271 const CharacterIndex characterFirstIndex = *( parameters.glyphsToCharactersBuffer + glyphIndex );
272 const CharacterIndex characterLastIndex = characterFirstIndex + ( hasCharacters ? charactersPerGlyph - 1u : 0u );
274 // Get the line break info for the current character.
275 const LineBreakInfo lineBreakInfo = hasCharacters ? *( parameters.lineBreakInfoBuffer + characterLastIndex ) : TextAbstraction::LINE_NO_BREAK;
277 // Get the word break info for the current character.
278 const WordBreakInfo wordBreakInfo = *( parameters.wordBreakInfoBuffer + characterLastIndex );
280 // Increase the number of characters.
281 tmpLineLayout.numberOfCharacters += charactersPerGlyph;
283 // Increase the number of glyphs.
284 tmpLineLayout.numberOfGlyphs += numberOfGLyphsInGroup;
286 // Check whether is a white space.
287 const Character character = *( parameters.textBuffer + characterFirstIndex );
288 const bool isWhiteSpace = TextAbstraction::IsWhiteSpace( character );
290 // Used to restore the temporal line layout when a single word does not fit in the control's width and is split by character.
291 const float previousTmpLineLength = tmpLineLayout.length;
292 const float previousTmpExtraBearing = tmpExtraBearing;
293 const float previousTmpExtraWidth = tmpExtraWidth;
295 // Get the character's direction.
296 const CharacterDirection characterDirection = ( NULL == parameters.characterDirectionBuffer ) ? false : *( parameters.characterDirectionBuffer + characterFirstIndex );
298 // Increase the accumulated length.
301 // Add the length to the length of white spaces at the end of the line.
302 tmpLineLayout.wsLengthEndOfLine += glyphMetrics.advance; // The advance is used as the width is always zero for the white spaces.
306 // Add as well any previous white space length.
307 tmpLineLayout.length += tmpLineLayout.wsLengthEndOfLine + glyphMetrics.advance;
309 // An extra space may be added to the line for the first and last glyph of the line.
310 // If the bearing of the first glyph is negative, its positive value needs to be added.
311 // If the bearing plus the width of the last glyph is greater than the advance, the difference
312 // needs to be added.
314 if( characterDirection == paragraphDirection )
316 if( RTL == characterDirection )
327 tmpExtraBearing = ( 0.f > glyphMetrics.xBearing ) ? -glyphMetrics.xBearing : 0.f;
340 const float extraWidth = glyphMetrics.xBearing + glyphMetrics.width - glyphMetrics.advance;
341 tmpExtraWidth = ( 0.f < extraWidth ) ? extraWidth : 0.f;
346 if( characterDirection != previousCharacterDirection )
348 if( RTL == characterDirection )
353 const float extraWidth = glyphMetrics.xBearing + glyphMetrics.width - glyphMetrics.advance;
354 tmpExtraWidth = ( 0.f < extraWidth ) ? extraWidth : 0.f;
361 tmpExtraBearing = ( 0.f > glyphMetrics.xBearing ) ? -glyphMetrics.xBearing : 0.f;
364 else if( characterDirection == firstCharacterDirection )
366 if( RTL == characterDirection )
372 tmpExtraBearing = ( 0.f > glyphMetrics.xBearing ) ? -glyphMetrics.xBearing : 0.f;
380 const float extraWidth = glyphMetrics.xBearing + glyphMetrics.width - glyphMetrics.advance;
381 tmpExtraWidth = ( 0.f < extraWidth ) ? extraWidth : 0.f;
386 // Clear the white space length at the end of the line.
387 tmpLineLayout.wsLengthEndOfLine = 0.f;
390 // Check if the accumulated length fits in the width of the box.
391 if( ( completelyFill || isMultiline ) && !isWhiteSpace &&
392 ( tmpExtraBearing + lineLayout.length + lineLayout.wsLengthEndOfLine + tmpLineLayout.length + tmpExtraWidth > parameters.boundingBox.width ) )
394 // Current word does not fit in the box's width.
395 if( !oneWordLaidOut || completelyFill )
397 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " Break the word by character\n" );
399 // The word doesn't fit in the control's width. It needs to be split by character.
400 if( tmpLineLayout.numberOfGlyphs > 0u )
402 tmpLineLayout.numberOfCharacters -= charactersPerGlyph;
403 tmpLineLayout.numberOfGlyphs -= numberOfGLyphsInGroup;
404 tmpLineLayout.length = previousTmpLineLength;
405 tmpExtraBearing = previousTmpExtraBearing;
406 tmpExtraWidth = previousTmpExtraWidth;
409 // Add part of the word to the line layout.
410 MergeLineLayout( lineLayout, tmpLineLayout );
414 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " Current word does not fit.\n" );
417 lineLayout.extraBearing = tmpExtraBearing;
418 lineLayout.extraWidth = tmpExtraWidth;
420 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--GetLineLayoutForBox.\n" );
425 if( ( isMultiline || isLastGlyph ) &&
426 ( TextAbstraction::LINE_MUST_BREAK == lineBreakInfo ) )
428 // Must break the line. Update the line layout and return.
429 MergeLineLayout( lineLayout, tmpLineLayout );
431 // Set the next paragraph's direction.
433 ( NULL != parameters.characterDirectionBuffer ) )
435 paragraphDirection = *( parameters.characterDirectionBuffer + 1u + characterLastIndex );
438 lineLayout.extraBearing = tmpExtraBearing;
439 lineLayout.extraWidth = tmpExtraWidth;
441 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " Must break\n" );
442 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--GetLineLayoutForBox\n" );
448 ( TextAbstraction::WORD_BREAK == wordBreakInfo ) )
450 oneWordLaidOut = true;
451 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " One word laid-out\n" );
453 // Current glyph is the last one of the current word.
454 // Add the temporal layout to the current one.
455 MergeLineLayout( lineLayout, tmpLineLayout );
457 tmpLineLayout.Clear();
460 previousCharacterDirection = characterDirection;
461 glyphIndex += numberOfGLyphsInGroup;
464 lineLayout.extraBearing = tmpExtraBearing;
465 lineLayout.extraWidth = tmpExtraWidth;
467 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--GetLineLayoutForBox\n" );
470 void SetGlyphPositions( const GlyphInfo* const glyphsBuffer,
471 Length numberOfGlyphs,
472 Vector2* glyphPositionsBuffer )
474 // Traverse the glyphs and set the positions.
476 // Check if the x bearing of the first character is negative.
477 // If it has a negative x bearing, it will exceed the boundaries of the actor,
478 // so the penX position needs to be moved to the right.
480 const GlyphInfo& glyph = *glyphsBuffer;
481 float penX = ( 0.f > glyph.xBearing ) ? -glyph.xBearing : 0.f;
483 for( GlyphIndex i = 0u; i < numberOfGlyphs; ++i )
485 const GlyphInfo& glyph = *( glyphsBuffer + i );
486 Vector2& position = *( glyphPositionsBuffer + i );
488 position.x = penX + glyph.xBearing;
489 position.y = -glyph.yBearing;
491 penX += glyph.advance;
496 * @brief Resizes the line buffer.
498 * @param[in,out] lines The vector of lines. Used when the layout is created from scratch.
499 * @param[in,out] newLines The vector of lines used instead of @p lines when the layout is updated.
500 * @param[in,out] linesCapacity The capacity of the vector (either lines or newLines).
501 * @param[in] updateCurrentBuffer Whether the layout is updated.
503 * @return Pointer to either lines or newLines.
505 LineRun* ResizeLinesBuffer( Vector<LineRun>& lines,
506 Vector<LineRun>& newLines,
507 Length& linesCapacity,
508 bool updateCurrentBuffer )
510 LineRun* linesBuffer = NULL;
511 // Reserve more space for the next lines.
513 if( updateCurrentBuffer )
515 newLines.Resize( linesCapacity );
516 linesBuffer = newLines.Begin();
520 lines.Resize( linesCapacity );
521 linesBuffer = lines.Begin();
528 * Ellipsis a line if it exceeds the width's of the bounding box.
530 * @param[in] layoutParameters The parameters needed to layout the text.
531 * @param[in] layout The line layout.
532 * @param[in,out] layoutSize The text's layout size.
533 * @param[in,out] linesBuffer Pointer to the line's buffer.
534 * @param[in,out] glyphPositionsBuffer Pointer to the position's buffer.
535 * @param[in,out] numberOfLines The number of laid-out lines.
536 * @param[in] penY The vertical layout position.
537 * @param[in] currentParagraphDirection The current paragraph's direction.
539 * return Whether the line is ellipsized.
541 bool EllipsisLine( const LayoutParameters& layoutParameters,
542 const LineLayout& layout,
544 LineRun* linesBuffer,
545 Vector2* glyphPositionsBuffer,
546 Length& numberOfLines,
548 CharacterDirection currentParagraphDirection )
550 const bool ellipsis = ( ( penY - layout.descender > layoutParameters.boundingBox.height ) ||
551 ( ( mLayout == SINGLE_LINE_BOX ) &&
552 ( layout.extraBearing + layout.length + layout.extraWidth > layoutParameters.boundingBox.width ) ) );
556 // Do not layout more lines if ellipsis is enabled.
558 // The last line needs to be completely filled with characters.
559 // Part of a word may be used.
561 LineRun* lineRun = NULL;
562 LineLayout ellipsisLayout;
563 if( 0u != numberOfLines )
565 // Get the last line and layout it again with the 'completelyFill' flag to true.
566 lineRun = linesBuffer + ( numberOfLines - 1u );
568 penY -= layout.ascender - lineRun->descender;
570 ellipsisLayout.glyphIndex = lineRun->glyphRun.glyphIndex;
574 // At least there is space reserved for one line.
575 lineRun = linesBuffer;
577 lineRun->glyphRun.glyphIndex = 0u;
578 ellipsisLayout.glyphIndex = 0u;
583 GetLineLayoutForBox( layoutParameters,
585 currentParagraphDirection,
588 lineRun->glyphRun.numberOfGlyphs = ellipsisLayout.numberOfGlyphs;
589 lineRun->characterRun.characterIndex = ellipsisLayout.characterIndex;
590 lineRun->characterRun.numberOfCharacters = ellipsisLayout.numberOfCharacters;
591 lineRun->width = ellipsisLayout.length;
592 lineRun->extraLength = ( ellipsisLayout.wsLengthEndOfLine > 0.f ) ? ellipsisLayout.wsLengthEndOfLine - ellipsisLayout.extraWidth : 0.f;
593 lineRun->ascender = ellipsisLayout.ascender;
594 lineRun->descender = ellipsisLayout.descender;
595 lineRun->direction = !RTL;
596 lineRun->ellipsis = true;
598 layoutSize.width = layoutParameters.boundingBox.width;
599 layoutSize.height += ( lineRun->ascender + -lineRun->descender );
601 SetGlyphPositions( layoutParameters.glyphsBuffer + lineRun->glyphRun.glyphIndex,
602 ellipsisLayout.numberOfGlyphs,
603 glyphPositionsBuffer + lineRun->glyphRun.glyphIndex - layoutParameters.startGlyphIndex );
610 * @brief Updates the text layout with a new laid-out line.
612 * @param[in] layoutParameters The parameters needed to layout the text.
613 * @param[in] layout The line layout.
614 * @param[in,out] layoutSize The text's layout size.
615 * @param[in,out] linesBuffer Pointer to the line's buffer.
616 * @param[in] index Index to the vector of glyphs.
617 * @param[in,out] numberOfLines The number of laid-out lines.
618 * @param[in] isLastLine Whether the laid-out line is the last one.
620 void UpdateTextLayout( const LayoutParameters& layoutParameters,
621 const LineLayout& layout,
623 LineRun* linesBuffer,
625 Length& numberOfLines,
628 LineRun& lineRun = *( linesBuffer + numberOfLines );
631 lineRun.glyphRun.glyphIndex = index;
632 lineRun.glyphRun.numberOfGlyphs = layout.numberOfGlyphs;
633 lineRun.characterRun.characterIndex = layout.characterIndex;
634 lineRun.characterRun.numberOfCharacters = layout.numberOfCharacters;
635 if( isLastLine && !layoutParameters.isLastNewParagraph )
637 const float width = layout.extraBearing + layout.length + layout.extraWidth + layout.wsLengthEndOfLine;
638 if( MULTI_LINE_BOX == mLayout )
640 lineRun.width = ( width > layoutParameters.boundingBox.width ) ? layoutParameters.boundingBox.width : width;
644 lineRun.width = width;
647 lineRun.extraLength = 0.f;
651 lineRun.width = layout.extraBearing + layout.length + layout.extraWidth;
652 lineRun.extraLength = ( layout.wsLengthEndOfLine > 0.f ) ? layout.wsLengthEndOfLine - layout.extraWidth : 0.f;
654 lineRun.ascender = layout.ascender;
655 lineRun.descender = layout.descender;
656 lineRun.direction = !RTL;
657 lineRun.ellipsis = false;
659 // Update the actual size.
660 if( lineRun.width > layoutSize.width )
662 layoutSize.width = lineRun.width;
665 layoutSize.height += ( lineRun.ascender + -lineRun.descender );
669 * @brief Updates the text layout with the last laid-out line.
671 * @param[in] layoutParameters The parameters needed to layout the text.
672 * @param[in] characterIndex The character index of the line.
673 * @param[in] glyphIndex The glyph index of the line.
674 * @param[in,out] layoutSize The text's layout size.
675 * @param[in,out] linesBuffer Pointer to the line's buffer.
676 * @param[in,out] numberOfLines The number of laid-out lines.
678 void UpdateTextLayout( const LayoutParameters& layoutParameters,
679 CharacterIndex characterIndex,
680 GlyphIndex glyphIndex,
682 LineRun* linesBuffer,
683 Length& numberOfLines )
685 // Need to add a new line with no characters but with height to increase the layoutSize.height
686 const GlyphInfo& glyphInfo = *( layoutParameters.glyphsBuffer + layoutParameters.totalNumberOfGlyphs - 1u );
688 Text::FontMetrics fontMetrics;
689 mMetrics->GetFontMetrics( glyphInfo.fontId, fontMetrics );
691 LineRun& lineRun = *( linesBuffer + numberOfLines );
694 lineRun.glyphRun.glyphIndex = glyphIndex;
695 lineRun.glyphRun.numberOfGlyphs = 0u;
696 lineRun.characterRun.characterIndex = characterIndex;
697 lineRun.characterRun.numberOfCharacters = 0u;
699 lineRun.ascender = fontMetrics.ascender;
700 lineRun.descender = fontMetrics.descender;
701 lineRun.extraLength = 0.f;
702 lineRun.alignmentOffset = 0.f;
703 lineRun.direction = !RTL;
704 lineRun.ellipsis = false;
706 layoutSize.height += ( lineRun.ascender + -lineRun.descender );
710 * @brief Updates the text's layout size adding the size of the previously laid-out lines.
712 * @param[in] lines The vector of lines (before the new laid-out lines are inserted).
713 * @param[in,out] layoutSize The text's layout size.
715 void UpdateLayoutSize( const Vector<LineRun>& lines,
718 for( Vector<LineRun>::ConstIterator it = lines.Begin(),
723 const LineRun& line = *it;
725 if( line.width > layoutSize.width )
727 layoutSize.width = line.width;
730 layoutSize.height += ( line.ascender + -line.descender );
735 * @brief Updates the indices of the character and glyph runs of the lines before the new lines are inserted.
737 * @param[in] layoutParameters The parameters needed to layout the text.
738 * @param[in,out] lines The vector of lines (before the new laid-out lines are inserted).
739 * @param[in] characterOffset The offset to be added to the runs of characters.
740 * @param[in] glyphOffset The offset to be added to the runs of glyphs.
742 void UpdateLineIndexOffsets( const LayoutParameters& layoutParameters,
743 Vector<LineRun>& lines,
744 Length characterOffset,
747 // Update the glyph and character runs.
748 for( Vector<LineRun>::Iterator it = lines.Begin() + layoutParameters.startLineIndex,
755 line.glyphRun.glyphIndex = glyphOffset;
756 line.characterRun.characterIndex = characterOffset;
758 glyphOffset += line.glyphRun.numberOfGlyphs;
759 characterOffset += line.characterRun.numberOfCharacters;
763 bool LayoutText( const LayoutParameters& layoutParameters,
764 Vector<Vector2>& glyphPositions,
765 Vector<LineRun>& lines,
768 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->LayoutText\n" );
769 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " box size %f, %f\n", layoutParameters.boundingBox.width, layoutParameters.boundingBox.height );
771 if( 0u == layoutParameters.numberOfGlyphs )
773 // Add an extra line if the last character is a new paragraph character and the last line doesn't have zero characters.
774 if( layoutParameters.isLastNewParagraph )
776 Length numberOfLines = lines.Count();
777 if( 0u != numberOfLines )
779 const LineRun& lastLine = *( lines.End() - 1u );
781 if( 0u != lastLine.characterRun.numberOfCharacters )
783 // Need to add a new line with no characters but with height to increase the layoutSize.height
785 Initialize( newLine );
786 lines.PushBack( newLine );
788 UpdateTextLayout( layoutParameters,
789 lastLine.characterRun.characterIndex + lastLine.characterRun.numberOfCharacters,
790 lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs,
798 // Calculates the layout size.
799 UpdateLayoutSize( lines,
802 // Nothing else do if there are no glyphs to layout.
806 const GlyphIndex lastGlyphPlusOne = layoutParameters.startGlyphIndex + layoutParameters.numberOfGlyphs;
808 // In a previous layout, an extra line with no characters may have been added if the text ended with a new paragraph character.
809 // This extra line needs to be removed.
810 if( 0u != lines.Count() )
812 Vector<LineRun>::Iterator lastLine = lines.End() - 1u;
814 if( ( 0u == lastLine->characterRun.numberOfCharacters ) &&
815 ( lastGlyphPlusOne == layoutParameters.totalNumberOfGlyphs ) )
817 lines.Remove( lastLine );
821 // Set the first paragraph's direction.
822 CharacterDirection paragraphDirection = ( NULL != layoutParameters.characterDirectionBuffer ) ? *layoutParameters.characterDirectionBuffer : !RTL;
824 // Whether the layout is being updated or set from scratch.
825 const bool updateCurrentBuffer = layoutParameters.numberOfGlyphs < layoutParameters.totalNumberOfGlyphs;
827 Vector2* glyphPositionsBuffer = NULL;
828 Vector<Vector2> newGlyphPositions;
830 LineRun* linesBuffer = NULL;
831 Vector<LineRun> newLines;
833 // Estimate the number of lines.
834 Length linesCapacity = std::max( 1u, layoutParameters.estimatedNumberOfLines );
835 Length numberOfLines = 0u;
837 if( updateCurrentBuffer )
839 newGlyphPositions.Resize( layoutParameters.numberOfGlyphs );
840 glyphPositionsBuffer = newGlyphPositions.Begin();
842 newLines.Resize( linesCapacity );
843 linesBuffer = newLines.Begin();
847 glyphPositionsBuffer = glyphPositions.Begin();
849 lines.Resize( linesCapacity );
850 linesBuffer = lines.Begin();
853 float penY = CalculateLineOffset( lines,
854 layoutParameters.startLineIndex );
856 for( GlyphIndex index = layoutParameters.startGlyphIndex; index < lastGlyphPlusOne; )
858 CharacterDirection currentParagraphDirection = paragraphDirection;
860 // Get the layout for the line.
862 layout.glyphIndex = index;
863 GetLineLayoutForBox( layoutParameters,
868 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " glyph index %d\n", layout.glyphIndex );
869 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " character index %d\n", layout.characterIndex );
870 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " number of glyphs %d\n", layout.numberOfGlyphs );
871 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " number of characters %d\n", layout.numberOfCharacters );
872 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " length %f\n", layout.length );
874 if( 0u == layout.numberOfGlyphs )
876 // The width is too small and no characters are laid-out.
877 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--LayoutText width too small!\n\n" );
879 lines.Resize( numberOfLines );
883 // Set the line position. Discard if ellipsis is enabled and the position exceeds the boundaries
885 penY += layout.ascender;
887 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " pen y %f\n", penY );
889 bool ellipsis = false;
890 if( mEllipsisEnabled )
892 // Does the ellipsis of the last line.
893 ellipsis = EllipsisLine( layoutParameters,
897 glyphPositionsBuffer,
900 currentParagraphDirection );
905 // No more lines to layout.
910 // Whether the last line has been laid-out.
911 const bool isLastLine = index + layout.numberOfGlyphs == layoutParameters.totalNumberOfGlyphs;
913 if( numberOfLines == linesCapacity )
915 // Reserve more space for the next lines.
916 linesBuffer = ResizeLinesBuffer( lines,
919 updateCurrentBuffer );
922 // Updates the current text's layout with the line's layout.
923 UpdateTextLayout( layoutParameters,
931 const GlyphIndex nextIndex = index + layout.numberOfGlyphs;
933 if( ( nextIndex == layoutParameters.totalNumberOfGlyphs ) &&
934 layoutParameters.isLastNewParagraph &&
935 ( mLayout == MULTI_LINE_BOX ) )
937 // The last character of the text is a new paragraph character.
938 // An extra line with no characters is added to increase the text's height
939 // in order to place the cursor.
941 if( numberOfLines == linesCapacity )
943 // Reserve more space for the next lines.
944 linesBuffer = ResizeLinesBuffer( lines,
947 updateCurrentBuffer );
950 UpdateTextLayout( layoutParameters,
951 layout.characterIndex + layout.numberOfCharacters,
952 index + layout.numberOfGlyphs,
956 } // whether to add a last line.
958 // Sets the positions of the glyphs.
959 SetGlyphPositions( layoutParameters.glyphsBuffer + index,
960 layout.numberOfGlyphs,
961 glyphPositionsBuffer + index - layoutParameters.startGlyphIndex );
963 // Updates the vertical pen's position.
964 penY += -layout.descender;
966 // Increase the glyph index.
969 } // end for() traversing glyphs.
971 if( updateCurrentBuffer )
973 glyphPositions.Insert( glyphPositions.Begin() + layoutParameters.startGlyphIndex,
974 newGlyphPositions.Begin(),
975 newGlyphPositions.End() );
976 glyphPositions.Resize( layoutParameters.totalNumberOfGlyphs );
978 newLines.Resize( numberOfLines );
980 // Current text's layout size adds only the newly laid-out lines.
981 // Updates the layout size with the previously laid-out lines.
982 UpdateLayoutSize( lines,
985 if( 0u != newLines.Count() )
987 const LineRun& lastLine = *( newLines.End() - 1u );
989 const Length characterOffset = lastLine.characterRun.characterIndex + lastLine.characterRun.numberOfCharacters;
990 const Length glyphOffset = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
992 // Update the indices of the runs before the new laid-out lines are inserted.
993 UpdateLineIndexOffsets( layoutParameters,
999 lines.Insert( lines.Begin() + layoutParameters.startLineIndex,
1006 lines.Resize( numberOfLines );
1009 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--LayoutText\n\n" );
1014 void ReLayoutRightToLeftLines( const LayoutParameters& layoutParameters,
1015 CharacterIndex startIndex,
1016 Length numberOfCharacters,
1017 Vector<Vector2>& glyphPositions )
1019 const CharacterIndex lastCharacterIndex = startIndex + numberOfCharacters;
1021 // Traverses the paragraphs with right to left characters.
1022 for( LineIndex lineIndex = 0u; lineIndex < layoutParameters.numberOfBidirectionalInfoRuns; ++lineIndex )
1024 const BidirectionalLineInfoRun& bidiLine = *( layoutParameters.lineBidirectionalInfoRunsBuffer + lineIndex );
1026 if( startIndex >= bidiLine.characterRun.characterIndex + bidiLine.characterRun.numberOfCharacters )
1028 // Do not reorder the line if it has been already reordered.
1032 if( bidiLine.characterRun.characterIndex >= lastCharacterIndex )
1034 // Do not reorder the lines after the last requested character.
1038 const CharacterIndex characterVisualIndex = bidiLine.characterRun.characterIndex + *bidiLine.visualToLogicalMap;
1039 const GlyphInfo& glyph = *( layoutParameters.glyphsBuffer + *( layoutParameters.charactersToGlyphsBuffer + characterVisualIndex ) );
1041 float penX = ( 0.f > glyph.xBearing ) ? -glyph.xBearing : 0.f;
1043 Vector2* glyphPositionsBuffer = glyphPositions.Begin();
1045 // Traverses the characters of the right to left paragraph.
1046 for( CharacterIndex characterLogicalIndex = 0u;
1047 characterLogicalIndex < bidiLine.characterRun.numberOfCharacters;
1048 ++characterLogicalIndex )
1050 // Convert the character in the logical order into the character in the visual order.
1051 const CharacterIndex characterVisualIndex = bidiLine.characterRun.characterIndex + *( bidiLine.visualToLogicalMap + characterLogicalIndex );
1053 // Get the number of glyphs of the character.
1054 const Length numberOfGlyphs = *( layoutParameters.glyphsPerCharacterBuffer + characterVisualIndex );
1056 for( GlyphIndex index = 0u; index < numberOfGlyphs; ++index )
1058 // Convert the character in the visual order into the glyph in the visual order.
1059 const GlyphIndex glyphIndex = *( layoutParameters.charactersToGlyphsBuffer + characterVisualIndex ) + index;
1061 DALI_ASSERT_DEBUG( 0u <= glyphIndex && glyphIndex < layoutParameters.totalNumberOfGlyphs );
1063 const GlyphInfo& glyph = *( layoutParameters.glyphsBuffer + glyphIndex );
1064 Vector2& position = *( glyphPositionsBuffer + glyphIndex );
1066 position.x = penX + glyph.xBearing;
1067 penX += glyph.advance;
1073 void Align( const Size& size,
1074 CharacterIndex startIndex,
1075 Length numberOfCharacters,
1076 Vector<LineRun>& lines )
1078 const CharacterIndex lastCharacterPlusOne = startIndex + numberOfCharacters;
1080 // Traverse all lines and align the glyphs.
1081 for( Vector<LineRun>::Iterator it = lines.Begin(), endIt = lines.End();
1085 LineRun& line = *it;
1087 if( line.characterRun.characterIndex < startIndex )
1089 // Do not align lines which have already been aligned.
1093 if( line.characterRun.characterIndex >= lastCharacterPlusOne )
1095 // Do not align lines beyond the last laid-out character.
1099 // Calculate the line's alignment offset accordingly with the align option,
1100 // the box width, line length, and the paragraph's direction.
1101 CalculateHorizontalAlignment( size.width,
1106 void CalculateHorizontalAlignment( float boxWidth,
1109 line.alignmentOffset = 0.f;
1110 const bool isRTL = RTL == line.direction;
1111 float lineLength = line.width;
1113 HorizontalAlignment alignment = mHorizontalAlignment;
1116 // Swap the alignment type if the line is right to left.
1119 case HORIZONTAL_ALIGN_BEGIN:
1121 alignment = HORIZONTAL_ALIGN_END;
1124 case HORIZONTAL_ALIGN_CENTER:
1129 case HORIZONTAL_ALIGN_END:
1131 alignment = HORIZONTAL_ALIGN_BEGIN;
1137 // Calculate the horizontal line offset.
1140 case HORIZONTAL_ALIGN_BEGIN:
1142 line.alignmentOffset = 0.f;
1146 // 'Remove' the white spaces at the end of the line (which are at the beginning in visual order)
1147 line.alignmentOffset -= line.extraLength;
1151 case HORIZONTAL_ALIGN_CENTER:
1153 line.alignmentOffset = 0.5f * ( boxWidth - lineLength );
1157 line.alignmentOffset -= line.extraLength;
1160 line.alignmentOffset = floorf( line.alignmentOffset ); // try to avoid pixel alignment.
1163 case HORIZONTAL_ALIGN_END:
1167 lineLength += line.extraLength;
1170 line.alignmentOffset = boxWidth - lineLength;
1176 void Initialize( LineRun& line )
1178 line.glyphRun.glyphIndex = 0u;
1179 line.glyphRun.numberOfGlyphs = 0u;
1180 line.characterRun.characterIndex = 0u;
1181 line.characterRun.numberOfCharacters = 0u;
1183 line.ascender = 0.f;
1184 line.descender = 0.f;
1185 line.extraLength = 0.f;
1186 line.alignmentOffset = 0.f;
1187 line.direction = !RTL;
1188 line.ellipsis = false;
1191 LayoutEngine::Layout mLayout;
1192 LayoutEngine::HorizontalAlignment mHorizontalAlignment;
1193 LayoutEngine::VerticalAlignment mVerticalAlignment;
1195 float mDefaultLineSpacing;
1197 IntrusivePtr<Metrics> mMetrics;
1199 bool mEllipsisEnabled:1;
1202 LayoutEngine::LayoutEngine()
1205 mImpl = new LayoutEngine::Impl();
1208 LayoutEngine::~LayoutEngine()
1213 void LayoutEngine::SetMetrics( MetricsPtr& metrics )
1215 mImpl->mMetrics = metrics;
1218 void LayoutEngine::SetLayout( Layout layout )
1220 mImpl->mLayout = layout;
1223 LayoutEngine::Layout LayoutEngine::GetLayout() const
1225 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "GetLayout[%d]\n", mImpl->mLayout);
1226 return mImpl->mLayout;
1229 void LayoutEngine::SetTextEllipsisEnabled( bool enabled )
1231 DALI_LOG_INFO( gLogFilter, Debug::General, "-->LayoutEngine::SetTextEllipsisEnabled[%s]\n", (enabled)?"true":"false" );
1232 mImpl->mEllipsisEnabled = enabled;
1235 bool LayoutEngine::GetTextEllipsisEnabled() const
1237 return mImpl->mEllipsisEnabled;
1240 void LayoutEngine::SetHorizontalAlignment( HorizontalAlignment alignment )
1242 mImpl->mHorizontalAlignment = alignment;
1245 LayoutEngine::HorizontalAlignment LayoutEngine::GetHorizontalAlignment() const
1247 return mImpl->mHorizontalAlignment;
1250 void LayoutEngine::SetVerticalAlignment( VerticalAlignment alignment )
1252 mImpl->mVerticalAlignment = alignment;
1255 LayoutEngine::VerticalAlignment LayoutEngine::GetVerticalAlignment() const
1257 return mImpl->mVerticalAlignment;
1260 void LayoutEngine::SetCursorWidth( int width )
1262 mImpl->mCursorWidth = static_cast<float>( width );
1265 int LayoutEngine::GetCursorWidth() const
1267 return static_cast<int>( mImpl->mCursorWidth );
1270 bool LayoutEngine::LayoutText( const LayoutParameters& layoutParameters,
1271 Vector<Vector2>& glyphPositions,
1272 Vector<LineRun>& lines,
1275 return mImpl->LayoutText( layoutParameters,
1281 void LayoutEngine::ReLayoutRightToLeftLines( const LayoutParameters& layoutParameters,
1282 CharacterIndex startIndex,
1283 Length numberOfCharacters,
1284 Vector<Vector2>& glyphPositions )
1286 mImpl->ReLayoutRightToLeftLines( layoutParameters,
1292 void LayoutEngine::Align( const Size& size,
1293 CharacterIndex startIndex,
1294 Length numberOfCharacters,
1295 Vector<LineRun>& lines )
1303 void LayoutEngine::SetDefaultLineSpacing( float lineSpacing )
1305 mImpl->mDefaultLineSpacing = lineSpacing;
1308 float LayoutEngine::GetDefaultLineSpacing() const
1310 return mImpl->mDefaultLineSpacing;
1315 } // namespace Toolkit