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/public-api/math/vector2.h>
24 #include <dali/devel-api/text-abstraction/font-client.h>
25 #include <dali/integration-api/debug.h>
28 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
29 #include <dali-toolkit/internal/text/bidirectional-line-info-run.h>
43 #if defined(DEBUG_ENABLED)
44 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_LAYOUT");
47 const float MAX_FLOAT = std::numeric_limits<float>::max();
48 const bool RTL = true;
53 * @brief Stores temporary layout info of the line.
61 numberOfCharacters( 0u ),
65 wsLengthEndOfLine( 0.f ),
67 descender( MAX_FLOAT )
78 numberOfCharacters = 0u;
82 wsLengthEndOfLine = 0.f;
84 descender = MAX_FLOAT;
87 GlyphIndex glyphIndex; ///< Index of the first glyph to be laid-out.
88 CharacterIndex characterIndex; ///< Index of the first character to be laid-out.
89 Length numberOfGlyphs; ///< The number of glyph which fit in one line.
90 Length numberOfCharacters; ///< The number of characters which fit in one line.
91 float length; ///< The addition of the advance metric of all the glyphs which fit in one line.
92 float extraBearing; ///< The extra width to be added to the line's length when the bearing of the first glyph is negative.
93 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.
94 float wsLengthEndOfLine; ///< The length of the white spaces at the end of the line.
95 float ascender; ///< The maximum ascender of all fonts in the line.
96 float descender; ///< The minimum descender of all fonts in the line.
99 struct LayoutEngine::Impl
102 : mLayout( LayoutEngine::SINGLE_LINE_BOX ),
103 mHorizontalAlignment( LayoutEngine::HORIZONTAL_ALIGN_BEGIN ),
104 mVerticalAlignment( LayoutEngine::VERTICAL_ALIGN_TOP ),
105 mEllipsisEnabled( false )
107 mFontClient = TextAbstraction::FontClient::Get();
111 * @brief Updates the line ascender and descender with the metrics of a new font.
113 * @param[in] fontId The id of the new font.
114 * @param[in,out] lineLayout The line layout.
116 void UpdateLineHeight( FontId fontId, LineLayout& lineLayout )
118 Text::FontMetrics fontMetrics;
119 mFontClient.GetFontMetrics( fontId, fontMetrics );
121 // Sets the maximum ascender.
122 if( fontMetrics.ascender > lineLayout.ascender )
124 lineLayout.ascender = fontMetrics.ascender;
127 // Sets the minimum descender.
128 if( fontMetrics.descender < lineLayout.descender )
130 lineLayout.descender = fontMetrics.descender;
135 * @brief Merges a temporary line layout into the line layout.
137 * @param[in,out] lineLayout The line layout.
138 * @param[in] tmpLineLayout A temporary line layout.
140 void MergeLineLayout( LineLayout& lineLayout,
141 const LineLayout& tmpLineLayout )
143 lineLayout.numberOfCharacters += tmpLineLayout.numberOfCharacters;
144 lineLayout.numberOfGlyphs += tmpLineLayout.numberOfGlyphs;
145 lineLayout.length += tmpLineLayout.length;
147 if( 0.f < tmpLineLayout.length )
149 lineLayout.length += lineLayout.wsLengthEndOfLine;
151 lineLayout.wsLengthEndOfLine = tmpLineLayout.wsLengthEndOfLine;
155 lineLayout.wsLengthEndOfLine += tmpLineLayout.wsLengthEndOfLine;
158 if( tmpLineLayout.ascender > lineLayout.ascender )
160 lineLayout.ascender = tmpLineLayout.ascender;
163 if( tmpLineLayout.descender < lineLayout.descender )
165 lineLayout.descender = tmpLineLayout.descender;
170 * Retrieves the line layout for a given box width.
172 * @note This method lais out text as it were left to right. At this point is not possible to reorder the line
173 * because the number of characters of the line is not known (one of the responsabilities of this method
174 * is calculate that). Due to glyph's 'x' bearing, width and advance, when right to left or mixed right to left
175 * and left to right text is laid out, it can be small differences in the line length. One solution is to
176 * reorder and re-lay out the text after this method and add or remove one extra glyph if needed. However,
177 * this method calculates which are the first and last glyphs of the line (the ones that causes the
178 * differences). This is a good point to check if there is problems with the text exceeding the boundaries
179 * of the control when there is right to left text.
181 * @param[in] parameters The layout parameters.
182 * @param[out] lineLayout The line layout.
183 * @param[in,out] paragraphDirection in: the current paragraph's direction, out: the next paragraph's direction. Is set after a must break.
184 * @param[in] completelyFill Whether to completely fill the line ( even if the last word exceeds the boundaries ).
186 void GetLineLayoutForBox( const LayoutParameters& parameters,
187 LineLayout& lineLayout,
188 CharacterDirection& paragraphDirection,
189 bool completelyFill )
191 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->GetLineLayoutForBox\n" );
192 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " initial glyph index : %d\n", lineLayout.glyphIndex );
193 // Stores temporary line layout which has not been added to the final line layout.
194 LineLayout tmpLineLayout;
196 const bool isMultiline = mLayout == MULTI_LINE_BOX;
197 const GlyphIndex lastGlyphIndex = parameters.totalNumberOfGlyphs - 1u;
199 // If the first glyph has a negative bearing its absolute value needs to be added to the line length.
200 // In the case the line starts with a right to left character, if the width is longer than the advance,
201 // the difference needs to be added to the line length.
202 const GlyphInfo& glyphInfo = *( parameters.glyphsBuffer + lineLayout.glyphIndex );
204 // Set the direction of the first character of the line.
205 lineLayout.characterIndex = *( parameters.glyphsToCharactersBuffer + lineLayout.glyphIndex );
206 const CharacterDirection firstCharacterDirection = ( NULL == parameters.characterDirectionBuffer ) ? false : *( parameters.characterDirectionBuffer + lineLayout.characterIndex );
207 CharacterDirection previousCharacterDirection = firstCharacterDirection;
209 const float extraWidth = glyphInfo.xBearing + glyphInfo.width - glyphInfo.advance;
210 float tmpExtraWidth = ( 0.f < extraWidth ) ? extraWidth : 0.f;
212 float tmpExtraBearing = ( 0.f > glyphInfo.xBearing ) ? -glyphInfo.xBearing : 0.f;
214 tmpLineLayout.length += 1.f; // Added one unit to give some space to the cursor.
216 // Calculate the line height if there is no characters.
217 FontId lastFontId = glyphInfo.fontId;
218 UpdateLineHeight( lastFontId, tmpLineLayout );
220 bool oneWordLaidOut = false;
222 for( GlyphIndex glyphIndex = lineLayout.glyphIndex;
223 glyphIndex < parameters.totalNumberOfGlyphs;
226 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " glyph index : %d\n", glyphIndex );
227 const bool isLastGlyph = glyphIndex == lastGlyphIndex;
229 // Get the glyph info.
230 const GlyphInfo& glyphInfo = *( parameters.glyphsBuffer + glyphIndex );
232 // Check if the font of the current glyph is the same of the previous one.
233 // If it's different the ascender and descender need to be updated.
234 if( lastFontId != glyphInfo.fontId )
236 UpdateLineHeight( glyphInfo.fontId, tmpLineLayout );
237 lastFontId = glyphInfo.fontId;
240 // Get the character indices for the current glyph. The last character index is needed
241 // because there are glyphs formed by more than one character but their break info is
242 // given only for the last character.
243 const Length charactersPerGlyph = *( parameters.charactersPerGlyphBuffer + glyphIndex );
244 const CharacterIndex characterFirstIndex = *( parameters.glyphsToCharactersBuffer + glyphIndex );
245 const CharacterIndex characterLastIndex = characterFirstIndex + ( ( 1u > charactersPerGlyph ) ? 0u : charactersPerGlyph - 1u );
247 // Get the line break info for the current character.
248 const LineBreakInfo lineBreakInfo = *( parameters.lineBreakInfoBuffer + characterLastIndex );
250 // Get the word break info for the current character.
251 const WordBreakInfo wordBreakInfo = *( parameters.wordBreakInfoBuffer + characterLastIndex );
253 // Increase the number of characters.
254 tmpLineLayout.numberOfCharacters += charactersPerGlyph;
256 // Increase the number of glyphs.
257 tmpLineLayout.numberOfGlyphs++;
259 // Check whether is a white space.
260 const Character character = *( parameters.textBuffer + characterFirstIndex );
261 const bool isWhiteSpace = TextAbstraction::IsWhiteSpace( character );
263 // Used to restore the temporal line layout when a single word does not fit in the control's width and is split by character.
264 const float previousTmpLineLength = tmpLineLayout.length;
265 const float previousTmpExtraBearing = tmpExtraBearing;
266 const float previousTmpExtraWidth = tmpExtraWidth;
268 // Get the character's direction.
269 const CharacterDirection characterDirection = ( NULL == parameters.characterDirectionBuffer ) ? false : *( parameters.characterDirectionBuffer + characterFirstIndex );
271 // Increase the accumulated length.
274 // Add the length to the length of white spaces at the end of the line.
275 tmpLineLayout.wsLengthEndOfLine += glyphInfo.advance; // The advance is used as the width is always zero for the white spaces.
279 // Add as well any previous white space length.
280 tmpLineLayout.length += tmpLineLayout.wsLengthEndOfLine + glyphInfo.advance;
282 // An extra space may be added to the line for the first and last glyph of the line.
283 // If the bearing of the first glyph is negative, its positive value needs to be added.
284 // If the bearing plus the width of the last glyph is greater than the advance, the difference
285 // needs to be added.
287 if( characterDirection == paragraphDirection )
289 if( RTL == characterDirection )
300 tmpExtraBearing = ( 0.f > glyphInfo.xBearing ) ? -glyphInfo.xBearing : 0.f;
313 const float extraWidth = glyphInfo.xBearing + glyphInfo.width - glyphInfo.advance;
314 tmpExtraWidth = ( 0.f < extraWidth ) ? extraWidth : 0.f;
319 if( characterDirection != previousCharacterDirection )
321 if( RTL == characterDirection )
326 const float extraWidth = glyphInfo.xBearing + glyphInfo.width - glyphInfo.advance;
327 tmpExtraWidth = ( 0.f < extraWidth ) ? extraWidth : 0.f;
334 tmpExtraBearing = ( 0.f > glyphInfo.xBearing ) ? -glyphInfo.xBearing : 0.f;
337 else if( characterDirection == firstCharacterDirection )
339 if( RTL == characterDirection )
345 tmpExtraBearing = ( 0.f > glyphInfo.xBearing ) ? -glyphInfo.xBearing : 0.f;
353 const float extraWidth = glyphInfo.xBearing + glyphInfo.width - glyphInfo.advance;
354 tmpExtraWidth = ( 0.f < extraWidth ) ? extraWidth : 0.f;
359 // Clear the white space length at the end of the line.
360 tmpLineLayout.wsLengthEndOfLine = 0.f;
363 // Check if the accumulated length fits in the width of the box.
364 if( ( completelyFill || isMultiline ) && !isWhiteSpace &&
365 ( tmpExtraBearing + lineLayout.length + lineLayout.wsLengthEndOfLine + tmpLineLayout.length + tmpExtraWidth > parameters.boundingBox.width ) )
367 // Current word does not fit in the box's width.
368 if( !oneWordLaidOut || completelyFill )
370 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " Break the word by character\n" );
372 // The word's with doesn't fit in the control's with. It needs to be split by character.
373 if( tmpLineLayout.numberOfGlyphs > 0u )
375 tmpLineLayout.numberOfCharacters -= charactersPerGlyph;
376 --tmpLineLayout.numberOfGlyphs;
377 tmpLineLayout.length = previousTmpLineLength;
378 tmpExtraBearing = previousTmpExtraBearing;
379 tmpExtraWidth = previousTmpExtraWidth;
382 // Add part of the word to the line layout.
383 MergeLineLayout( lineLayout, tmpLineLayout );
387 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " Current word does not fit.\n" );
390 lineLayout.extraBearing = tmpExtraBearing;
391 lineLayout.extraWidth = tmpExtraWidth;
393 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--GetLineLayoutForBox.\n" );
398 if( ( isMultiline || isLastGlyph ) &&
399 ( TextAbstraction::LINE_MUST_BREAK == lineBreakInfo ) )
401 // Must break the line. Update the line layout and return.
402 MergeLineLayout( lineLayout, tmpLineLayout );
404 // Set the next paragraph's direction.
406 ( NULL != parameters.characterDirectionBuffer ) )
408 paragraphDirection = *( parameters.characterDirectionBuffer + 1u + characterLastIndex );
411 lineLayout.extraBearing = tmpExtraBearing;
412 lineLayout.extraWidth = tmpExtraWidth;
414 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " Must break\n" );
415 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--GetLineLayoutForBox\n" );
420 ( TextAbstraction::WORD_BREAK == wordBreakInfo ) )
422 oneWordLaidOut = true;
423 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " One word laid out\n" );
425 // Current glyph is the last one of the current word.
426 // Add the temporal layout to the current one.
427 MergeLineLayout( lineLayout, tmpLineLayout );
429 tmpLineLayout.Clear();
432 previousCharacterDirection = characterDirection;
435 lineLayout.extraBearing = tmpExtraBearing;
436 lineLayout.extraWidth = tmpExtraWidth;
438 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--GetLineLayoutForBox\n" );
441 void SetGlyphPositions( const GlyphInfo* const glyphsBuffer,
442 Length numberOfGlyphs,
444 Vector2* glyphPositionsBuffer )
446 // Traverse the glyphs and set the positions.
448 // Check if the x bearing of the first character is negative.
449 // If it has a negative x bearing, it will exceed the boundaries of the actor,
450 // so the penX position needs to be moved to the right.
452 const GlyphInfo& glyph = *glyphsBuffer;
453 float penX = ( 0.f > glyph.xBearing ) ? -glyph.xBearing : 0.f;
454 penX += 1.f; // Added one unit to give some space to the cursor.
456 for( GlyphIndex i = 0u; i < numberOfGlyphs; ++i )
458 const GlyphInfo& glyph = *( glyphsBuffer + i );
459 Vector2& position = *( glyphPositionsBuffer + i );
461 position.x = penX + glyph.xBearing;
462 position.y = penY - glyph.yBearing;
464 penX += glyph.advance;
468 bool LayoutText( const LayoutParameters& layoutParameters,
469 Vector<Vector2>& glyphPositions,
470 Vector<LineRun>& lines,
473 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->LayoutText\n" );
474 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " box size %f, %f\n", layoutParameters.boundingBox.width, layoutParameters.boundingBox.height );
476 // Set the first paragraph's direction.
477 CharacterDirection paragraphDirection = ( NULL != layoutParameters.characterDirectionBuffer ) ? *layoutParameters.characterDirectionBuffer : !RTL;
480 for( GlyphIndex index = 0u; index < layoutParameters.totalNumberOfGlyphs; )
482 CharacterDirection currentParagraphDirection = paragraphDirection;
484 // Get the layout for the line.
486 layout.glyphIndex = index;
487 GetLineLayoutForBox( layoutParameters,
492 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " glyph index %d\n", layout.glyphIndex );
493 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " character index %d\n", layout.characterIndex );
494 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " number of glyphs %d\n", layout.numberOfGlyphs );
495 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " number of characters %d\n", layout.numberOfCharacters );
496 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " length %f\n", layout.length );
498 if( 0u == layout.numberOfGlyphs )
500 // The width is too small and no characters are laid-out.
501 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--LayoutText width too small!\n\n" );
505 // Set the line position. Discard if ellipsis is enabled and the position exceeds the boundaries
507 penY += layout.ascender;
509 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " pen y %f\n", penY );
510 if( mEllipsisEnabled &&
511 ( ( penY - layout.descender > layoutParameters.boundingBox.height ) ||
512 ( ( mLayout == SINGLE_LINE_BOX ) &&
513 ( layout.extraBearing + layout.length + layout.extraWidth > layoutParameters.boundingBox.width ) ) ) )
515 // Do not layout more lines if ellipsis is enabled.
517 // The last line needs to be completely filled with characters.
518 // Part of a word may be used.
520 const Length numberOfLines = lines.Count();
523 LineLayout ellipsisLayout;
524 if( 0u != numberOfLines )
526 // Get the last line and layout it again with the 'completelyFill' flag to true.
527 lineRun = *( lines.Begin() + ( numberOfLines - 1u ) );
529 penY -= layout.ascender - lineRun.descender;
531 ellipsisLayout.glyphIndex = lineRun.glyphIndex;
535 lineRun.glyphIndex = 0u;
536 ellipsisLayout.glyphIndex = 0u;
539 GetLineLayoutForBox( layoutParameters,
541 currentParagraphDirection,
544 lineRun.numberOfGlyphs = ellipsisLayout.numberOfGlyphs;
545 lineRun.characterRun.characterIndex = ellipsisLayout.characterIndex;
546 lineRun.characterRun.numberOfCharacters = ellipsisLayout.numberOfCharacters;
547 lineRun.width = ellipsisLayout.length;
548 lineRun.extraLength = ( ellipsisLayout.wsLengthEndOfLine > 0.f ) ? ellipsisLayout.wsLengthEndOfLine - ellipsisLayout.extraWidth : 0.f;
549 lineRun.ascender = ellipsisLayout.ascender;
550 lineRun.descender = ellipsisLayout.descender;
551 lineRun.direction = !RTL;
552 lineRun.ellipsis = true;
554 actualSize.width = layoutParameters.boundingBox.width;
555 actualSize.height += ( lineRun.ascender + -lineRun.descender );
557 SetGlyphPositions( layoutParameters.glyphsBuffer + lineRun.glyphIndex,
558 ellipsisLayout.numberOfGlyphs,
560 glyphPositions.Begin() + lineRun.glyphIndex );
562 if( 0u != numberOfLines )
564 // Set the last line with the ellipsis layout.
565 *( lines.Begin() + ( numberOfLines - 1u ) ) = lineRun;
570 lines.PushBack( lineRun );
577 const bool isLastLine = index + layout.numberOfGlyphs == layoutParameters.totalNumberOfGlyphs;
580 lineRun.glyphIndex = index;
581 lineRun.numberOfGlyphs = layout.numberOfGlyphs;
582 lineRun.characterRun.characterIndex = layout.characterIndex;
583 lineRun.characterRun.numberOfCharacters = layout.numberOfCharacters;
584 if( isLastLine && !layoutParameters.isLastNewParagraph )
586 const float width = layout.extraBearing + layout.length + layout.extraWidth + layout.wsLengthEndOfLine;
587 if( MULTI_LINE_BOX == mLayout )
589 lineRun.width = ( width > layoutParameters.boundingBox.width ) ? layoutParameters.boundingBox.width : width;
593 lineRun.width = width;
596 lineRun.extraLength = 0.f;
600 lineRun.width = layout.extraBearing + layout.length + layout.extraWidth;
601 lineRun.extraLength = ( layout.wsLengthEndOfLine > 0.f ) ? layout.wsLengthEndOfLine - layout.extraWidth : 0.f;
603 lineRun.ascender = layout.ascender;
604 lineRun.descender = layout.descender;
605 lineRun.direction = !RTL;
606 lineRun.ellipsis = false;
608 lines.PushBack( lineRun );
610 // Update the actual size.
611 if( lineRun.width > actualSize.width )
613 actualSize.width = lineRun.width;
616 actualSize.height += ( lineRun.ascender + -lineRun.descender );
618 SetGlyphPositions( layoutParameters.glyphsBuffer + index,
619 layout.numberOfGlyphs,
621 glyphPositions.Begin() + index );
623 penY += -layout.descender;
625 // Increase the glyph index.
626 index += layout.numberOfGlyphs;
629 layoutParameters.isLastNewParagraph &&
630 ( mLayout == MULTI_LINE_BOX ) )
632 // Need to add a new line with no characters but with height to increase the actualSize.height
633 const GlyphInfo& glyphInfo = *( layoutParameters.glyphsBuffer + layoutParameters.totalNumberOfGlyphs - 1u );
635 Text::FontMetrics fontMetrics;
636 mFontClient.GetFontMetrics( glyphInfo.fontId, fontMetrics );
639 lineRun.glyphIndex = 0u;
640 lineRun.numberOfGlyphs = 0u;
641 lineRun.characterRun.characterIndex = 0u;
642 lineRun.characterRun.numberOfCharacters = 0u;
644 lineRun.ascender = fontMetrics.ascender;
645 lineRun.descender = fontMetrics.descender;
646 lineRun.extraLength = 0.f;
647 lineRun.alignmentOffset = 0.f;
648 lineRun.direction = !RTL;
649 lineRun.ellipsis = false;
651 actualSize.height += ( lineRun.ascender + -lineRun.descender );
653 lines.PushBack( lineRun );
656 } // end for() traversing glyphs.
658 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--LayoutText\n\n" );
663 void ReLayoutRightToLeftLines( const LayoutParameters& layoutParameters,
664 Vector<Vector2>& glyphPositions )
666 // Traverses the paragraphs with right to left characters.
667 for( LineIndex lineIndex = 0u; lineIndex < layoutParameters.numberOfBidirectionalInfoRuns; ++lineIndex )
669 const BidirectionalLineInfoRun& bidiLine = *( layoutParameters.lineBidirectionalInfoRunsBuffer + lineIndex );
671 const CharacterIndex characterVisualIndex = bidiLine.characterRun.characterIndex + *bidiLine.visualToLogicalMap;
672 const GlyphInfo& glyph = *( layoutParameters.glyphsBuffer + *( layoutParameters.charactersToGlyphsBuffer + characterVisualIndex ) );
674 float penX = ( 0.f > glyph.xBearing ) ? -glyph.xBearing : 0.f;
675 penX += 1.f; // Added one unit to give some space to the cursor.
677 Vector2* glyphPositionsBuffer = glyphPositions.Begin();
679 // Traverses the characters of the right to left paragraph.
680 for( CharacterIndex characterLogicalIndex = 0u;
681 characterLogicalIndex < bidiLine.characterRun.numberOfCharacters;
682 ++characterLogicalIndex )
684 // Convert the character in the logical order into the character in the visual order.
685 const CharacterIndex characterVisualIndex = bidiLine.characterRun.characterIndex + *( bidiLine.visualToLogicalMap + characterLogicalIndex );
687 // Get the number of glyphs of the character.
688 const Length numberOfGlyphs = *( layoutParameters.glyphsPerCharacterBuffer + characterVisualIndex );
690 for( GlyphIndex index = 0u; index < numberOfGlyphs; ++index )
692 // Convert the character in the visual order into the glyph in the visual order.
693 const GlyphIndex glyphIndex = *( layoutParameters.charactersToGlyphsBuffer + characterVisualIndex ) + index;
695 DALI_ASSERT_DEBUG( 0u <= glyphIndex && glyphIndex < layoutParameters.totalNumberOfGlyphs );
697 const GlyphInfo& glyph = *( layoutParameters.glyphsBuffer + glyphIndex );
698 Vector2& position = *( glyphPositionsBuffer + glyphIndex );
700 position.x = penX + glyph.xBearing;
701 penX += glyph.advance;
707 void Align( const Size& layoutSize,
708 Vector<LineRun>& lines )
710 // Traverse all lines and align the glyphs.
712 for( Vector<LineRun>::Iterator it = lines.Begin(), endIt = lines.End();
717 const bool isLastLine = lines.End() == it + 1u;
719 // Calculate the alignment offset accordingly with the align option,
720 // the box width, line length, and the paragraphs direction.
721 CalculateHorizontalAlignment( layoutSize.width,
727 void CalculateHorizontalAlignment( float boxWidth,
731 line.alignmentOffset = 0.f;
732 const bool isRTL = RTL == line.direction;
733 float lineLength = line.width;
735 HorizontalAlignment alignment = mHorizontalAlignment;
737 ( HORIZONTAL_ALIGN_CENTER != alignment ) )
739 if( HORIZONTAL_ALIGN_BEGIN == alignment )
741 alignment = HORIZONTAL_ALIGN_END;
745 alignment = HORIZONTAL_ALIGN_BEGIN;
751 case HORIZONTAL_ALIGN_BEGIN:
753 line.alignmentOffset = 0.f;
757 // 'Remove' the white spaces at the end of the line (which are at the beginning in visual order)
758 line.alignmentOffset -= line.extraLength;
762 line.alignmentOffset += std::min( line.extraLength, boxWidth - lineLength );
767 case HORIZONTAL_ALIGN_CENTER:
769 if( isLastLine && !isRTL )
771 lineLength += line.extraLength;
772 if( lineLength > boxWidth )
774 lineLength = boxWidth;
775 line.alignmentOffset = 0.f;
780 line.alignmentOffset = 0.5f * ( boxWidth - lineLength );
784 line.alignmentOffset -= line.extraLength;
788 line.alignmentOffset += 0.5f * std::min( line.extraLength, boxWidth - lineLength );
792 line.alignmentOffset = floorf( line.alignmentOffset ); // try to avoid pixel alignment.
795 case HORIZONTAL_ALIGN_END:
797 if( isLastLine && !isRTL )
799 lineLength += line.extraLength;
800 if( lineLength > boxWidth )
802 line.alignmentOffset = 0.f;
809 lineLength += line.extraLength;
812 line.alignmentOffset = boxWidth - lineLength;
818 LayoutEngine::Layout mLayout;
819 LayoutEngine::HorizontalAlignment mHorizontalAlignment;
820 LayoutEngine::VerticalAlignment mVerticalAlignment;
822 TextAbstraction::FontClient mFontClient;
824 bool mEllipsisEnabled:1;
827 LayoutEngine::LayoutEngine()
830 mImpl = new LayoutEngine::Impl();
833 LayoutEngine::~LayoutEngine()
838 void LayoutEngine::SetLayout( Layout layout )
840 mImpl->mLayout = layout;
843 unsigned int LayoutEngine::GetLayout() const
845 return mImpl->mLayout;
848 void LayoutEngine::SetTextEllipsisEnabled( bool enabled )
850 mImpl->mEllipsisEnabled = enabled;
853 bool LayoutEngine::GetTextEllipsisEnabled() const
855 return mImpl->mEllipsisEnabled;
858 void LayoutEngine::SetHorizontalAlignment( HorizontalAlignment alignment )
860 mImpl->mHorizontalAlignment = alignment;
863 LayoutEngine::HorizontalAlignment LayoutEngine::GetHorizontalAlignment() const
865 return mImpl->mHorizontalAlignment;
868 void LayoutEngine::SetVerticalAlignment( VerticalAlignment alignment )
870 mImpl->mVerticalAlignment = alignment;
873 LayoutEngine::VerticalAlignment LayoutEngine::GetVerticalAlignment() const
875 return mImpl->mVerticalAlignment;
878 bool LayoutEngine::LayoutText( const LayoutParameters& layoutParameters,
879 Vector<Vector2>& glyphPositions,
880 Vector<LineRun>& lines,
883 return mImpl->LayoutText( layoutParameters,
889 void LayoutEngine::ReLayoutRightToLeftLines( const LayoutParameters& layoutParameters,
890 Vector<Vector2>& glyphPositions )
892 mImpl->ReLayoutRightToLeftLines( layoutParameters,
896 void LayoutEngine::Align( const Size& layoutSize,
897 Vector<LineRun>& lines )
899 mImpl->Align( layoutSize,
905 } // namespace Toolkit