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>
47 #if defined(DEBUG_ENABLED)
48 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_LAYOUT");
51 const float MAX_FLOAT = std::numeric_limits<float>::max();
52 const bool RTL = true;
53 const float CURSOR_WIDTH = 1.f;
54 const float LINE_SPACING= 0.f;
59 * @brief Stores temporary layout info of the line.
67 numberOfCharacters( 0u ),
71 wsLengthEndOfLine( 0.f ),
73 descender( MAX_FLOAT )
84 numberOfCharacters = 0u;
88 wsLengthEndOfLine = 0.f;
90 descender = MAX_FLOAT;
93 GlyphIndex glyphIndex; ///< Index of the first glyph to be laid-out.
94 CharacterIndex characterIndex; ///< Index of the first character to be laid-out.
95 Length numberOfGlyphs; ///< The number of glyph which fit in one line.
96 Length numberOfCharacters; ///< The number of characters which fit in one line.
97 float length; ///< The addition of the advance metric of all the glyphs which fit in one line.
98 float extraBearing; ///< The extra width to be added to the line's length when the bearing of the first glyph is negative.
99 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.
100 float wsLengthEndOfLine; ///< The length of the white spaces at the end of the line.
101 float ascender; ///< The maximum ascender of all fonts in the line.
102 float descender; ///< The minimum descender of all fonts in the line.
108 : mLayout( Layout::Engine::SINGLE_LINE_BOX ),
109 mCursorWidth( CURSOR_WIDTH ),
110 mDefaultLineSpacing( LINE_SPACING )
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 Parameters& 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;
201 const bool isWordLaidOut = parameters.lineWrapMode == Layout::LineWrap::WORD;
203 // The last glyph to be laid-out.
204 const GlyphIndex lastGlyphOfParagraphPlusOne = parameters.startGlyphIndex + parameters.numberOfGlyphs;
206 // If the first glyph has a negative bearing its absolute value needs to be added to the line length.
207 // In the case the line starts with a right to left character, if the width is longer than the advance,
208 // the difference needs to be added to the line length.
210 // Check whether the first glyph comes from a character that is shaped in multiple glyphs.
211 const Length numberOfGLyphsInGroup = GetNumberOfGlyphsOfGroup( lineLayout.glyphIndex,
212 lastGlyphOfParagraphPlusOne,
213 parameters.charactersPerGlyphBuffer );
215 GlyphMetrics glyphMetrics;
216 GetGlyphsMetrics( lineLayout.glyphIndex,
217 numberOfGLyphsInGroup,
219 parameters.glyphsBuffer,
222 // Set the direction of the first character of the line.
223 lineLayout.characterIndex = *( parameters.glyphsToCharactersBuffer + lineLayout.glyphIndex );
224 const CharacterDirection firstCharacterDirection = ( NULL == parameters.characterDirectionBuffer ) ? false : *( parameters.characterDirectionBuffer + lineLayout.characterIndex );
225 CharacterDirection previousCharacterDirection = firstCharacterDirection;
227 const float extraWidth = glyphMetrics.xBearing + glyphMetrics.width - glyphMetrics.advance;
228 float tmpExtraWidth = ( 0.f < extraWidth ) ? extraWidth : 0.f;
230 float tmpExtraBearing = ( 0.f > glyphMetrics.xBearing ) ? -glyphMetrics.xBearing : 0.f;
232 tmpLineLayout.length += mCursorWidth; // Added to give some space to the cursor.
234 // Calculate the line height if there is no characters.
235 FontId lastFontId = glyphMetrics.fontId;
236 UpdateLineHeight( lastFontId, tmpLineLayout );
238 bool oneWordLaidOut = false;
240 for( GlyphIndex glyphIndex = lineLayout.glyphIndex;
241 glyphIndex < lastGlyphOfParagraphPlusOne; )
243 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " glyph index : %d\n", glyphIndex );
245 // Check whether this glyph comes from a character that is shaped in multiple glyphs.
246 const Length numberOfGLyphsInGroup = GetNumberOfGlyphsOfGroup( glyphIndex,
247 lastGlyphOfParagraphPlusOne,
248 parameters.charactersPerGlyphBuffer );
250 GlyphMetrics glyphMetrics;
251 GetGlyphsMetrics( glyphIndex,
252 numberOfGLyphsInGroup,
254 parameters.glyphsBuffer,
257 const bool isLastGlyph = glyphIndex + numberOfGLyphsInGroup == parameters.totalNumberOfGlyphs;
259 // Check if the font of the current glyph is the same of the previous one.
260 // If it's different the ascender and descender need to be updated.
261 if( lastFontId != glyphMetrics.fontId )
263 UpdateLineHeight( glyphMetrics.fontId, tmpLineLayout );
264 lastFontId = glyphMetrics.fontId;
267 // Get the character indices for the current glyph. The last character index is needed
268 // because there are glyphs formed by more than one character but their break info is
269 // given only for the last character.
270 const Length charactersPerGlyph = *( parameters.charactersPerGlyphBuffer + glyphIndex + numberOfGLyphsInGroup - 1u );
271 const bool hasCharacters = charactersPerGlyph > 0u;
272 const CharacterIndex characterFirstIndex = *( parameters.glyphsToCharactersBuffer + glyphIndex );
273 const CharacterIndex characterLastIndex = characterFirstIndex + ( hasCharacters ? charactersPerGlyph - 1u : 0u );
275 // Get the line break info for the current character.
276 const LineBreakInfo lineBreakInfo = hasCharacters ? *( parameters.lineBreakInfoBuffer + characterLastIndex ) : TextAbstraction::LINE_NO_BREAK;
278 // Get the word break info for the current character.
279 const WordBreakInfo wordBreakInfo = *( parameters.wordBreakInfoBuffer + characterLastIndex );
281 // Increase the number of characters.
282 tmpLineLayout.numberOfCharacters += charactersPerGlyph;
284 // Increase the number of glyphs.
285 tmpLineLayout.numberOfGlyphs += numberOfGLyphsInGroup;
287 // Check whether is a white space.
288 const Character character = *( parameters.textBuffer + characterFirstIndex );
289 const bool isWhiteSpace = TextAbstraction::IsWhiteSpace( character );
291 // Used to restore the temporal line layout when a single word does not fit in the control's width and is split by character.
292 const float previousTmpLineLength = tmpLineLayout.length;
293 const float previousTmpExtraBearing = tmpExtraBearing;
294 const float previousTmpExtraWidth = tmpExtraWidth;
296 // Get the character's direction.
297 const CharacterDirection characterDirection = ( NULL == parameters.characterDirectionBuffer ) ? false : *( parameters.characterDirectionBuffer + characterFirstIndex );
299 // Increase the accumulated length.
302 // Add the length to the length of white spaces at the end of the line.
303 tmpLineLayout.wsLengthEndOfLine += glyphMetrics.advance; // The advance is used as the width is always zero for the white spaces.
307 // Add as well any previous white space length.
308 tmpLineLayout.length += tmpLineLayout.wsLengthEndOfLine + glyphMetrics.advance;
310 // An extra space may be added to the line for the first and last glyph of the line.
311 // If the bearing of the first glyph is negative, its positive value needs to be added.
312 // If the bearing plus the width of the last glyph is greater than the advance, the difference
313 // needs to be added.
315 if( characterDirection == paragraphDirection )
317 if( RTL == characterDirection )
328 tmpExtraBearing = ( 0.f > glyphMetrics.xBearing ) ? -glyphMetrics.xBearing : 0.f;
341 const float extraWidth = glyphMetrics.xBearing + glyphMetrics.width - glyphMetrics.advance;
342 tmpExtraWidth = ( 0.f < extraWidth ) ? extraWidth : 0.f;
347 if( characterDirection != previousCharacterDirection )
349 if( RTL == characterDirection )
354 const float extraWidth = glyphMetrics.xBearing + glyphMetrics.width - glyphMetrics.advance;
355 tmpExtraWidth = ( 0.f < extraWidth ) ? extraWidth : 0.f;
362 tmpExtraBearing = ( 0.f > glyphMetrics.xBearing ) ? -glyphMetrics.xBearing : 0.f;
365 else if( characterDirection == firstCharacterDirection )
367 if( RTL == characterDirection )
373 tmpExtraBearing = ( 0.f > glyphMetrics.xBearing ) ? -glyphMetrics.xBearing : 0.f;
381 const float extraWidth = glyphMetrics.xBearing + glyphMetrics.width - glyphMetrics.advance;
382 tmpExtraWidth = ( 0.f < extraWidth ) ? extraWidth : 0.f;
387 // Clear the white space length at the end of the line.
388 tmpLineLayout.wsLengthEndOfLine = 0.f;
391 // Check if the accumulated length fits in the width of the box.
392 if( ( completelyFill || isMultiline ) && !isWhiteSpace &&
393 ( tmpExtraBearing + lineLayout.length + lineLayout.wsLengthEndOfLine + tmpLineLayout.length + tmpExtraWidth > parameters.boundingBox.width ) )
395 // Current word does not fit in the box's width.
396 if( !oneWordLaidOut || completelyFill )
398 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " Break the word by character\n" );
400 // The word doesn't fit in the control's width. It needs to be split by character.
401 if( tmpLineLayout.numberOfGlyphs > 0u )
403 tmpLineLayout.numberOfCharacters -= charactersPerGlyph;
404 tmpLineLayout.numberOfGlyphs -= numberOfGLyphsInGroup;
405 tmpLineLayout.length = previousTmpLineLength;
406 tmpExtraBearing = previousTmpExtraBearing;
407 tmpExtraWidth = previousTmpExtraWidth;
410 // Add part of the word to the line layout.
411 MergeLineLayout( lineLayout, tmpLineLayout );
415 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " Current word does not fit.\n" );
418 lineLayout.extraBearing = tmpExtraBearing;
419 lineLayout.extraWidth = tmpExtraWidth;
421 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--GetLineLayoutForBox.\n" );
426 if( ( isMultiline || isLastGlyph ) &&
427 ( TextAbstraction::LINE_MUST_BREAK == lineBreakInfo ) )
429 // Must break the line. Update the line layout and return.
430 MergeLineLayout( lineLayout, tmpLineLayout );
432 // Set the next paragraph's direction.
434 ( NULL != parameters.characterDirectionBuffer ) )
436 paragraphDirection = *( parameters.characterDirectionBuffer + 1u + characterLastIndex );
439 lineLayout.extraBearing = tmpExtraBearing;
440 lineLayout.extraWidth = tmpExtraWidth;
442 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " Must break\n" );
443 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--GetLineLayoutForBox\n" );
449 ( TextAbstraction::WORD_BREAK == wordBreakInfo ) )
451 oneWordLaidOut = isWordLaidOut;
452 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " One word laid-out\n" );
454 // Current glyph is the last one of the current word.
455 // Add the temporal layout to the current one.
456 MergeLineLayout( lineLayout, tmpLineLayout );
458 tmpLineLayout.Clear();
461 previousCharacterDirection = characterDirection;
462 glyphIndex += numberOfGLyphsInGroup;
465 lineLayout.extraBearing = tmpExtraBearing;
466 lineLayout.extraWidth = tmpExtraWidth;
468 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--GetLineLayoutForBox\n" );
471 void SetGlyphPositions( const GlyphInfo* const glyphsBuffer,
472 Length numberOfGlyphs,
473 Vector2* glyphPositionsBuffer )
475 // Traverse the glyphs and set the positions.
477 // Check if the x bearing of the first character is negative.
478 // If it has a negative x bearing, it will exceed the boundaries of the actor,
479 // so the penX position needs to be moved to the right.
481 const GlyphInfo& glyph = *glyphsBuffer;
482 float penX = ( 0.f > glyph.xBearing ) ? -glyph.xBearing : 0.f;
484 for( GlyphIndex i = 0u; i < numberOfGlyphs; ++i )
486 const GlyphInfo& glyph = *( glyphsBuffer + i );
487 Vector2& position = *( glyphPositionsBuffer + i );
489 position.x = penX + glyph.xBearing;
490 position.y = -glyph.yBearing;
492 penX += glyph.advance;
497 * @brief Resizes the line buffer.
499 * @param[in,out] lines The vector of lines. Used when the layout is created from scratch.
500 * @param[in,out] newLines The vector of lines used instead of @p lines when the layout is updated.
501 * @param[in,out] linesCapacity The capacity of the vector (either lines or newLines).
502 * @param[in] updateCurrentBuffer Whether the layout is updated.
504 * @return Pointer to either lines or newLines.
506 LineRun* ResizeLinesBuffer( Vector<LineRun>& lines,
507 Vector<LineRun>& newLines,
508 Length& linesCapacity,
509 bool updateCurrentBuffer )
511 LineRun* linesBuffer = NULL;
512 // Reserve more space for the next lines.
514 if( updateCurrentBuffer )
516 newLines.Resize( linesCapacity );
517 linesBuffer = newLines.Begin();
521 lines.Resize( linesCapacity );
522 linesBuffer = lines.Begin();
529 * Ellipsis a line if it exceeds the width's of the bounding box.
531 * @param[in] layoutParameters The parameters needed to layout the text.
532 * @param[in] layout The line layout.
533 * @param[in,out] layoutSize The text's layout size.
534 * @param[in,out] linesBuffer Pointer to the line's buffer.
535 * @param[in,out] glyphPositionsBuffer Pointer to the position's buffer.
536 * @param[in,out] numberOfLines The number of laid-out lines.
537 * @param[in] penY The vertical layout position.
538 * @param[in] currentParagraphDirection The current paragraph's direction.
540 * return Whether the line is ellipsized.
542 bool EllipsisLine( const Parameters& layoutParameters,
543 const LineLayout& layout,
545 LineRun* linesBuffer,
546 Vector2* glyphPositionsBuffer,
547 Length& numberOfLines,
549 CharacterDirection currentParagraphDirection )
551 const bool ellipsis = ( ( penY - layout.descender > layoutParameters.boundingBox.height ) ||
552 ( ( mLayout == SINGLE_LINE_BOX ) &&
553 ( layout.extraBearing + layout.length + layout.extraWidth > layoutParameters.boundingBox.width ) ) );
557 // Do not layout more lines if ellipsis is enabled.
559 // The last line needs to be completely filled with characters.
560 // Part of a word may be used.
562 LineRun* lineRun = NULL;
563 LineLayout ellipsisLayout;
564 if( 0u != numberOfLines )
566 // Get the last line and layout it again with the 'completelyFill' flag to true.
567 lineRun = linesBuffer + ( numberOfLines - 1u );
569 penY -= layout.ascender - lineRun->descender;
571 ellipsisLayout.glyphIndex = lineRun->glyphRun.glyphIndex;
575 // At least there is space reserved for one line.
576 lineRun = linesBuffer;
578 lineRun->glyphRun.glyphIndex = 0u;
579 ellipsisLayout.glyphIndex = 0u;
584 GetLineLayoutForBox( layoutParameters,
586 currentParagraphDirection,
589 lineRun->glyphRun.numberOfGlyphs = ellipsisLayout.numberOfGlyphs;
590 lineRun->characterRun.characterIndex = ellipsisLayout.characterIndex;
591 lineRun->characterRun.numberOfCharacters = ellipsisLayout.numberOfCharacters;
592 lineRun->width = ellipsisLayout.length;
593 lineRun->extraLength = ( ellipsisLayout.wsLengthEndOfLine > 0.f ) ? ellipsisLayout.wsLengthEndOfLine - ellipsisLayout.extraWidth : 0.f;
594 lineRun->ascender = ellipsisLayout.ascender;
595 lineRun->descender = ellipsisLayout.descender;
596 lineRun->direction = !RTL;
597 lineRun->ellipsis = true;
599 layoutSize.width = layoutParameters.boundingBox.width;
600 if( layoutSize.height < Math::MACHINE_EPSILON_1000 )
602 layoutSize.height += ( lineRun->ascender + -lineRun->descender );
605 SetGlyphPositions( layoutParameters.glyphsBuffer + lineRun->glyphRun.glyphIndex,
606 ellipsisLayout.numberOfGlyphs,
607 glyphPositionsBuffer + lineRun->glyphRun.glyphIndex - layoutParameters.startGlyphIndex );
614 * @brief Updates the text layout with a new laid-out line.
616 * @param[in] layoutParameters The parameters needed to layout the text.
617 * @param[in] layout The line layout.
618 * @param[in,out] layoutSize The text's layout size.
619 * @param[in,out] linesBuffer Pointer to the line's buffer.
620 * @param[in] index Index to the vector of glyphs.
621 * @param[in,out] numberOfLines The number of laid-out lines.
622 * @param[in] isLastLine Whether the laid-out line is the last one.
624 void UpdateTextLayout( const Parameters& layoutParameters,
625 const LineLayout& layout,
627 LineRun* linesBuffer,
629 Length& numberOfLines,
632 LineRun& lineRun = *( linesBuffer + numberOfLines );
635 lineRun.glyphRun.glyphIndex = index;
636 lineRun.glyphRun.numberOfGlyphs = layout.numberOfGlyphs;
637 lineRun.characterRun.characterIndex = layout.characterIndex;
638 lineRun.characterRun.numberOfCharacters = layout.numberOfCharacters;
639 if( isLastLine && !layoutParameters.isLastNewParagraph )
641 const float width = layout.extraBearing + layout.length + layout.extraWidth + layout.wsLengthEndOfLine;
642 if( MULTI_LINE_BOX == mLayout )
644 lineRun.width = ( width > layoutParameters.boundingBox.width ) ? layoutParameters.boundingBox.width : width;
648 lineRun.width = width;
651 lineRun.extraLength = 0.f;
655 lineRun.width = layout.extraBearing + layout.length + layout.extraWidth;
656 lineRun.extraLength = ( layout.wsLengthEndOfLine > 0.f ) ? layout.wsLengthEndOfLine - layout.extraWidth : 0.f;
658 lineRun.ascender = layout.ascender;
659 lineRun.descender = layout.descender;
660 lineRun.direction = !RTL;
661 lineRun.ellipsis = false;
663 // Update the actual size.
664 if( lineRun.width > layoutSize.width )
666 layoutSize.width = lineRun.width;
669 layoutSize.height += ( lineRun.ascender + -lineRun.descender );
673 * @brief Updates the text layout with the last laid-out line.
675 * @param[in] layoutParameters The parameters needed to layout the text.
676 * @param[in] characterIndex The character index of the line.
677 * @param[in] glyphIndex The glyph index of the line.
678 * @param[in,out] layoutSize The text's layout size.
679 * @param[in,out] linesBuffer Pointer to the line's buffer.
680 * @param[in,out] numberOfLines The number of laid-out lines.
682 void UpdateTextLayout( const Parameters& layoutParameters,
683 CharacterIndex characterIndex,
684 GlyphIndex glyphIndex,
686 LineRun* linesBuffer,
687 Length& numberOfLines )
689 // Need to add a new line with no characters but with height to increase the layoutSize.height
690 const GlyphInfo& glyphInfo = *( layoutParameters.glyphsBuffer + layoutParameters.totalNumberOfGlyphs - 1u );
692 Text::FontMetrics fontMetrics;
693 mMetrics->GetFontMetrics( glyphInfo.fontId, fontMetrics );
695 LineRun& lineRun = *( linesBuffer + numberOfLines );
698 lineRun.glyphRun.glyphIndex = glyphIndex;
699 lineRun.glyphRun.numberOfGlyphs = 0u;
700 lineRun.characterRun.characterIndex = characterIndex;
701 lineRun.characterRun.numberOfCharacters = 0u;
703 lineRun.ascender = fontMetrics.ascender;
704 lineRun.descender = fontMetrics.descender;
705 lineRun.extraLength = 0.f;
706 lineRun.alignmentOffset = 0.f;
707 lineRun.direction = !RTL;
708 lineRun.ellipsis = false;
710 layoutSize.height += ( lineRun.ascender + -lineRun.descender );
714 * @brief Updates the text's layout size adding the size of the previously laid-out lines.
716 * @param[in] lines The vector of lines (before the new laid-out lines are inserted).
717 * @param[in,out] layoutSize The text's layout size.
719 void UpdateLayoutSize( const Vector<LineRun>& lines,
722 for( Vector<LineRun>::ConstIterator it = lines.Begin(),
727 const LineRun& line = *it;
729 if( line.width > layoutSize.width )
731 layoutSize.width = line.width;
734 layoutSize.height += ( line.ascender + -line.descender );
739 * @brief Updates the indices of the character and glyph runs of the lines before the new lines are inserted.
741 * @param[in] layoutParameters The parameters needed to layout the text.
742 * @param[in,out] lines The vector of lines (before the new laid-out lines are inserted).
743 * @param[in] characterOffset The offset to be added to the runs of characters.
744 * @param[in] glyphOffset The offset to be added to the runs of glyphs.
746 void UpdateLineIndexOffsets( const Parameters& layoutParameters,
747 Vector<LineRun>& lines,
748 Length characterOffset,
751 // Update the glyph and character runs.
752 for( Vector<LineRun>::Iterator it = lines.Begin() + layoutParameters.startLineIndex,
759 line.glyphRun.glyphIndex = glyphOffset;
760 line.characterRun.characterIndex = characterOffset;
762 glyphOffset += line.glyphRun.numberOfGlyphs;
763 characterOffset += line.characterRun.numberOfCharacters;
767 bool LayoutText( const Parameters& layoutParameters,
768 Vector<Vector2>& glyphPositions,
769 Vector<LineRun>& lines,
771 bool elideTextEnabled )
773 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->LayoutText\n" );
774 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " box size %f, %f\n", layoutParameters.boundingBox.width, layoutParameters.boundingBox.height );
776 if( 0u == layoutParameters.numberOfGlyphs )
778 // Add an extra line if the last character is a new paragraph character and the last line doesn't have zero characters.
779 if( layoutParameters.isLastNewParagraph )
781 Length numberOfLines = lines.Count();
782 if( 0u != numberOfLines )
784 const LineRun& lastLine = *( lines.End() - 1u );
786 if( 0u != lastLine.characterRun.numberOfCharacters )
788 // Need to add a new line with no characters but with height to increase the layoutSize.height
790 Initialize( newLine );
791 lines.PushBack( newLine );
793 UpdateTextLayout( layoutParameters,
794 lastLine.characterRun.characterIndex + lastLine.characterRun.numberOfCharacters,
795 lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs,
803 // Calculates the layout size.
804 UpdateLayoutSize( lines,
807 // Nothing else do if there are no glyphs to layout.
811 const GlyphIndex lastGlyphPlusOne = layoutParameters.startGlyphIndex + layoutParameters.numberOfGlyphs;
813 // In a previous layout, an extra line with no characters may have been added if the text ended with a new paragraph character.
814 // This extra line needs to be removed.
815 if( 0u != lines.Count() )
817 Vector<LineRun>::Iterator lastLine = lines.End() - 1u;
819 if( ( 0u == lastLine->characterRun.numberOfCharacters ) &&
820 ( lastGlyphPlusOne == layoutParameters.totalNumberOfGlyphs ) )
822 lines.Remove( lastLine );
826 // Set the first paragraph's direction.
827 CharacterDirection paragraphDirection = ( NULL != layoutParameters.characterDirectionBuffer ) ? *layoutParameters.characterDirectionBuffer : !RTL;
829 // Whether the layout is being updated or set from scratch.
830 const bool updateCurrentBuffer = layoutParameters.numberOfGlyphs < layoutParameters.totalNumberOfGlyphs;
832 Vector2* glyphPositionsBuffer = NULL;
833 Vector<Vector2> newGlyphPositions;
835 LineRun* linesBuffer = NULL;
836 Vector<LineRun> newLines;
838 // Estimate the number of lines.
839 Length linesCapacity = std::max( 1u, layoutParameters.estimatedNumberOfLines );
840 Length numberOfLines = 0u;
842 if( updateCurrentBuffer )
844 newGlyphPositions.Resize( layoutParameters.numberOfGlyphs );
845 glyphPositionsBuffer = newGlyphPositions.Begin();
847 newLines.Resize( linesCapacity );
848 linesBuffer = newLines.Begin();
852 glyphPositionsBuffer = glyphPositions.Begin();
854 lines.Resize( linesCapacity );
855 linesBuffer = lines.Begin();
858 float penY = CalculateLineOffset( lines,
859 layoutParameters.startLineIndex );
861 for( GlyphIndex index = layoutParameters.startGlyphIndex; index < lastGlyphPlusOne; )
863 CharacterDirection currentParagraphDirection = paragraphDirection;
865 // Get the layout for the line.
867 layout.glyphIndex = index;
868 GetLineLayoutForBox( layoutParameters,
873 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " glyph index %d\n", layout.glyphIndex );
874 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " character index %d\n", layout.characterIndex );
875 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " number of glyphs %d\n", layout.numberOfGlyphs );
876 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " number of characters %d\n", layout.numberOfCharacters );
877 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " length %f\n", layout.length );
879 if( 0u == layout.numberOfGlyphs )
881 // The width is too small and no characters are laid-out.
882 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--LayoutText width too small!\n\n" );
884 lines.Resize( numberOfLines );
888 // Set the line position. Discard if ellipsis is enabled and the position exceeds the boundaries
890 penY += layout.ascender;
892 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " pen y %f\n", penY );
894 bool ellipsis = false;
895 if( elideTextEnabled )
897 // Does the ellipsis of the last line.
898 ellipsis = EllipsisLine( layoutParameters,
902 glyphPositionsBuffer,
905 currentParagraphDirection );
910 // No more lines to layout.
915 // Whether the last line has been laid-out.
916 const bool isLastLine = index + layout.numberOfGlyphs == layoutParameters.totalNumberOfGlyphs;
918 if( numberOfLines == linesCapacity )
920 // Reserve more space for the next lines.
921 linesBuffer = ResizeLinesBuffer( lines,
924 updateCurrentBuffer );
927 // Updates the current text's layout with the line's layout.
928 UpdateTextLayout( layoutParameters,
936 const GlyphIndex nextIndex = index + layout.numberOfGlyphs;
938 if( ( nextIndex == layoutParameters.totalNumberOfGlyphs ) &&
939 layoutParameters.isLastNewParagraph &&
940 ( mLayout == MULTI_LINE_BOX ) )
942 // The last character of the text is a new paragraph character.
943 // An extra line with no characters is added to increase the text's height
944 // in order to place the cursor.
946 if( numberOfLines == linesCapacity )
948 // Reserve more space for the next lines.
949 linesBuffer = ResizeLinesBuffer( lines,
952 updateCurrentBuffer );
955 UpdateTextLayout( layoutParameters,
956 layout.characterIndex + layout.numberOfCharacters,
957 index + layout.numberOfGlyphs,
961 } // whether to add a last line.
963 // Sets the positions of the glyphs.
964 SetGlyphPositions( layoutParameters.glyphsBuffer + index,
965 layout.numberOfGlyphs,
966 glyphPositionsBuffer + index - layoutParameters.startGlyphIndex );
968 // Updates the vertical pen's position.
969 penY += -layout.descender;
971 // Increase the glyph index.
974 } // end for() traversing glyphs.
976 if( updateCurrentBuffer )
978 glyphPositions.Insert( glyphPositions.Begin() + layoutParameters.startGlyphIndex,
979 newGlyphPositions.Begin(),
980 newGlyphPositions.End() );
981 glyphPositions.Resize( layoutParameters.totalNumberOfGlyphs );
983 newLines.Resize( numberOfLines );
985 // Current text's layout size adds only the newly laid-out lines.
986 // Updates the layout size with the previously laid-out lines.
987 UpdateLayoutSize( lines,
990 if( 0u != newLines.Count() )
992 const LineRun& lastLine = *( newLines.End() - 1u );
994 const Length characterOffset = lastLine.characterRun.characterIndex + lastLine.characterRun.numberOfCharacters;
995 const Length glyphOffset = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
997 // Update the indices of the runs before the new laid-out lines are inserted.
998 UpdateLineIndexOffsets( layoutParameters,
1003 // Insert the lines.
1004 lines.Insert( lines.Begin() + layoutParameters.startLineIndex,
1011 lines.Resize( numberOfLines );
1014 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--LayoutText\n\n" );
1019 void ReLayoutRightToLeftLines( const Parameters& layoutParameters,
1020 CharacterIndex startIndex,
1021 Length numberOfCharacters,
1022 Vector<Vector2>& glyphPositions )
1024 const CharacterIndex lastCharacterIndex = startIndex + numberOfCharacters;
1026 // Traverses the paragraphs with right to left characters.
1027 for( LineIndex lineIndex = 0u; lineIndex < layoutParameters.numberOfBidirectionalInfoRuns; ++lineIndex )
1029 const BidirectionalLineInfoRun& bidiLine = *( layoutParameters.lineBidirectionalInfoRunsBuffer + lineIndex );
1031 if( startIndex >= bidiLine.characterRun.characterIndex + bidiLine.characterRun.numberOfCharacters )
1033 // Do not reorder the line if it has been already reordered.
1037 if( bidiLine.characterRun.characterIndex >= lastCharacterIndex )
1039 // Do not reorder the lines after the last requested character.
1043 const CharacterIndex characterVisualIndex = bidiLine.characterRun.characterIndex + *bidiLine.visualToLogicalMap;
1044 const GlyphInfo& glyph = *( layoutParameters.glyphsBuffer + *( layoutParameters.charactersToGlyphsBuffer + characterVisualIndex ) );
1046 float penX = ( 0.f > glyph.xBearing ) ? -glyph.xBearing : 0.f;
1048 Vector2* glyphPositionsBuffer = glyphPositions.Begin();
1050 // Traverses the characters of the right to left paragraph.
1051 for( CharacterIndex characterLogicalIndex = 0u;
1052 characterLogicalIndex < bidiLine.characterRun.numberOfCharacters;
1053 ++characterLogicalIndex )
1055 // Convert the character in the logical order into the character in the visual order.
1056 const CharacterIndex characterVisualIndex = bidiLine.characterRun.characterIndex + *( bidiLine.visualToLogicalMap + characterLogicalIndex );
1058 // Get the number of glyphs of the character.
1059 const Length numberOfGlyphs = *( layoutParameters.glyphsPerCharacterBuffer + characterVisualIndex );
1061 for( GlyphIndex index = 0u; index < numberOfGlyphs; ++index )
1063 // Convert the character in the visual order into the glyph in the visual order.
1064 const GlyphIndex glyphIndex = *( layoutParameters.charactersToGlyphsBuffer + characterVisualIndex ) + index;
1066 DALI_ASSERT_DEBUG( 0u <= glyphIndex && glyphIndex < layoutParameters.totalNumberOfGlyphs );
1068 const GlyphInfo& glyph = *( layoutParameters.glyphsBuffer + glyphIndex );
1069 Vector2& position = *( glyphPositionsBuffer + glyphIndex );
1071 position.x = penX + glyph.xBearing;
1072 penX += glyph.advance;
1078 void Align( const Size& size,
1079 CharacterIndex startIndex,
1080 Length numberOfCharacters,
1081 Text::HorizontalAlignment::Type horizontalAlignment,
1082 Vector<LineRun>& lines,
1083 float& alignmentOffset )
1085 const CharacterIndex lastCharacterPlusOne = startIndex + numberOfCharacters;
1087 alignmentOffset = MAX_FLOAT;
1088 // Traverse all lines and align the glyphs.
1089 for( Vector<LineRun>::Iterator it = lines.Begin(), endIt = lines.End();
1093 LineRun& line = *it;
1095 if( line.characterRun.characterIndex < startIndex )
1097 // Do not align lines which have already been aligned.
1101 if( line.characterRun.characterIndex >= lastCharacterPlusOne )
1103 // Do not align lines beyond the last laid-out character.
1107 // Calculate the line's alignment offset accordingly with the align option,
1108 // the box width, line length, and the paragraph's direction.
1109 CalculateHorizontalAlignment( size.width,
1110 horizontalAlignment,
1113 // Updates the alignment offset.
1114 alignmentOffset = std::min( alignmentOffset, line.alignmentOffset );
1118 void CalculateHorizontalAlignment( float boxWidth,
1119 HorizontalAlignment::Type horizontalAlignment,
1122 line.alignmentOffset = 0.f;
1123 const bool isRTL = RTL == line.direction;
1124 float lineLength = line.width;
1126 HorizontalAlignment::Type alignment = horizontalAlignment;
1129 // Swap the alignment type if the line is right to left.
1132 case HorizontalAlignment::BEGIN:
1134 alignment = HorizontalAlignment::END;
1137 case HorizontalAlignment::CENTER:
1142 case HorizontalAlignment::END:
1144 alignment = HorizontalAlignment::BEGIN;
1150 // Calculate the horizontal line offset.
1153 case HorizontalAlignment::BEGIN:
1155 line.alignmentOffset = 0.f;
1159 // 'Remove' the white spaces at the end of the line (which are at the beginning in visual order)
1160 line.alignmentOffset -= line.extraLength;
1164 case HorizontalAlignment::CENTER:
1166 line.alignmentOffset = 0.5f * ( boxWidth - lineLength );
1170 line.alignmentOffset -= line.extraLength;
1173 line.alignmentOffset = floorf( line.alignmentOffset ); // try to avoid pixel alignment.
1176 case HorizontalAlignment::END:
1180 lineLength += line.extraLength;
1183 line.alignmentOffset = boxWidth - lineLength;
1189 void Initialize( LineRun& line )
1191 line.glyphRun.glyphIndex = 0u;
1192 line.glyphRun.numberOfGlyphs = 0u;
1193 line.characterRun.characterIndex = 0u;
1194 line.characterRun.numberOfCharacters = 0u;
1196 line.ascender = 0.f;
1197 line.descender = 0.f;
1198 line.extraLength = 0.f;
1199 line.alignmentOffset = 0.f;
1200 line.direction = !RTL;
1201 line.ellipsis = false;
1206 float mDefaultLineSpacing;
1208 IntrusivePtr<Metrics> mMetrics;
1214 mImpl = new Engine::Impl();
1222 void Engine::SetMetrics( MetricsPtr& metrics )
1224 mImpl->mMetrics = metrics;
1227 void Engine::SetLayout( Type layout )
1229 mImpl->mLayout = layout;
1232 Engine::Type Engine::GetLayout() const
1234 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "GetLayout[%d]\n", mImpl->mLayout);
1235 return mImpl->mLayout;
1238 void Engine::SetCursorWidth( int width )
1240 mImpl->mCursorWidth = static_cast<float>( width );
1243 int Engine::GetCursorWidth() const
1245 return static_cast<int>( mImpl->mCursorWidth );
1248 bool Engine::LayoutText( const Parameters& layoutParameters,
1249 Vector<Vector2>& glyphPositions,
1250 Vector<LineRun>& lines,
1252 bool elideTextEnabled )
1254 return mImpl->LayoutText( layoutParameters,
1261 void Engine::ReLayoutRightToLeftLines( const Parameters& layoutParameters,
1262 CharacterIndex startIndex,
1263 Length numberOfCharacters,
1264 Vector<Vector2>& glyphPositions )
1266 mImpl->ReLayoutRightToLeftLines( layoutParameters,
1272 void Engine::Align( const Size& size,
1273 CharacterIndex startIndex,
1274 Length numberOfCharacters,
1275 Text::HorizontalAlignment::Type horizontalAlignment,
1276 Vector<LineRun>& lines,
1277 float& alignmentOffset )
1282 horizontalAlignment,
1287 void Engine::SetDefaultLineSpacing( float lineSpacing )
1289 mImpl->mDefaultLineSpacing = lineSpacing;
1292 float Engine::GetDefaultLineSpacing() const
1294 return mImpl->mDefaultLineSpacing;
1297 } // namespace Layout
1301 } // namespace Toolkit