X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali-toolkit%2Finternal%2Ftext%2Flayouts%2Flayout-engine.cpp;h=cd51c8d1618daac9cbc02e449cac557ba7f75a14;hb=d00a250741411c386d988e7ac34525cf94a1918e;hp=8816e333d88fbb5d80103654dc908a6711a4ad04;hpb=008e8f30624bc5abdfc707fdafd31b0540fa53d0;p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git diff --git a/dali-toolkit/internal/text/layouts/layout-engine.cpp b/dali-toolkit/internal/text/layouts/layout-engine.cpp index 8816e33..cd51c8d 100644 --- a/dali-toolkit/internal/text/layouts/layout-engine.cpp +++ b/dali-toolkit/internal/text/layouts/layout-engine.cpp @@ -25,6 +25,7 @@ // INTERNAL INCLUDES #include +#include #include #include @@ -47,26 +48,7 @@ namespace const float MAX_FLOAT = std::numeric_limits::max(); const bool RTL = true; const float CURSOR_WIDTH = 1.f; - -Length CountParagraphs( const LayoutParameters& layoutParameters ) -{ - Length numberOfParagraphs = 0u; - - const CharacterIndex startCharacterIndex = *( layoutParameters.glyphsToCharactersBuffer + layoutParameters.startGlyphIndex ); - - const GlyphIndex lastGlyphIndex = layoutParameters.startGlyphIndex + layoutParameters.numberOfGlyphs - 1u; - const CharacterIndex lastCharacterIndexPlusOne = *( layoutParameters.glyphsToCharactersBuffer + lastGlyphIndex ) + *( layoutParameters.charactersPerGlyphBuffer + lastGlyphIndex ); - - for( CharacterIndex index = startCharacterIndex; index < lastCharacterIndexPlusOne; ++index ) - { - if( TextAbstraction::LINE_MUST_BREAK == *( layoutParameters.lineBreakInfoBuffer + index ) ) - { - ++numberOfParagraphs; - } - } - - return numberOfParagraphs; -} +const float LINE_SPACING= 0.f; } //namespace @@ -124,6 +106,7 @@ struct LayoutEngine::Impl mHorizontalAlignment( LayoutEngine::HORIZONTAL_ALIGN_BEGIN ), mVerticalAlignment( LayoutEngine::VERTICAL_ALIGN_TOP ), mCursorWidth( CURSOR_WIDTH ), + mDefaultLineSpacing( LINE_SPACING ), mEllipsisEnabled( false ) { } @@ -484,37 +467,8 @@ struct LayoutEngine::Impl DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--GetLineLayoutForBox\n" ); } - /** - * @brief Calculates the vertical offset to add to the new laid-out glyphs. - * - * @pre @p lineIndex must be between 0 and the number of lines (both inclusive). - * - * @param[in] lines The previously laid-out lines. - * @param[in] lineIndex Index to the line where the new laid-out lines are inserted. - * - * @return The vertical offset of the lines starting from the beginning to the line @p lineIndex. - */ - float SetParagraphOffset( const Vector& lines, - LineIndex lineIndex ) - { - float offset = 0.f; - - for( Vector::ConstIterator it = lines.Begin(), - endIt = lines.Begin() + lineIndex; - it != endIt; - ++it ) - { - const LineRun& line = *it; - - offset += line.ascender + -line.descender; - } - - return offset; - } - void SetGlyphPositions( const GlyphInfo* const glyphsBuffer, Length numberOfGlyphs, - float penY, Vector2* glyphPositionsBuffer ) { // Traverse the glyphs and set the positions. @@ -532,7 +486,7 @@ struct LayoutEngine::Impl Vector2& position = *( glyphPositionsBuffer + i ); position.x = penX + glyph.xBearing; - position.y = penY - glyph.yBearing; + position.y = -glyph.yBearing; penX += glyph.advance; } @@ -646,7 +600,6 @@ struct LayoutEngine::Impl SetGlyphPositions( layoutParameters.glyphsBuffer + lineRun->glyphRun.glyphIndex, ellipsisLayout.numberOfGlyphs, - penY, glyphPositionsBuffer + lineRun->glyphRun.glyphIndex - layoutParameters.startGlyphIndex ); } @@ -716,17 +669,17 @@ struct LayoutEngine::Impl * @brief Updates the text layout with the last laid-out line. * * @param[in] layoutParameters The parameters needed to layout the text. - * @param[in] layout The line layout. + * @param[in] characterIndex The character index of the line. + * @param[in] glyphIndex The glyph index of the line. * @param[in,out] layoutSize The text's layout size. * @param[in,out] linesBuffer Pointer to the line's buffer. - * @param[in] index Index to the vector of glyphs. * @param[in,out] numberOfLines The number of laid-out lines. */ void UpdateTextLayout( const LayoutParameters& layoutParameters, - const LineLayout& layout, + CharacterIndex characterIndex, + GlyphIndex glyphIndex, Size& layoutSize, LineRun* linesBuffer, - GlyphIndex index, Length& numberOfLines ) { // Need to add a new line with no characters but with height to increase the layoutSize.height @@ -738,9 +691,9 @@ struct LayoutEngine::Impl LineRun& lineRun = *( linesBuffer + numberOfLines ); ++numberOfLines; - lineRun.glyphRun.glyphIndex = index + layout.numberOfGlyphs; + lineRun.glyphRun.glyphIndex = glyphIndex; lineRun.glyphRun.numberOfGlyphs = 0u; - lineRun.characterRun.characterIndex = layout.characterIndex + layout.numberOfCharacters; + lineRun.characterRun.characterIndex = characterIndex; lineRun.characterRun.numberOfCharacters = 0u; lineRun.width = 0.f; lineRun.ascender = fontMetrics.ascender; @@ -817,10 +770,54 @@ struct LayoutEngine::Impl if( 0u == layoutParameters.numberOfGlyphs ) { - // Nothing to do if there are no glyphs to layout. + // Add an extra line if the last character is a new paragraph character and the last line doesn't have zero characters. + if( layoutParameters.isLastNewParagraph ) + { + Length numberOfLines = lines.Count(); + if( 0u != numberOfLines ) + { + const LineRun& lastLine = *( lines.End() - 1u ); + + if( 0u != lastLine.characterRun.numberOfCharacters ) + { + // Need to add a new line with no characters but with height to increase the layoutSize.height + LineRun newLine; + Initialize( newLine ); + lines.PushBack( newLine ); + + UpdateTextLayout( layoutParameters, + lastLine.characterRun.characterIndex + lastLine.characterRun.numberOfCharacters, + lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs, + layoutSize, + lines.Begin(), + numberOfLines ); + } + } + } + + // Calculates the layout size. + UpdateLayoutSize( lines, + layoutSize ); + + // Nothing else do if there are no glyphs to layout. return false; } + const GlyphIndex lastGlyphPlusOne = layoutParameters.startGlyphIndex + layoutParameters.numberOfGlyphs; + + // In a previous layout, an extra line with no characters may have been added if the text ended with a new paragraph character. + // This extra line needs to be removed. + if( 0u != lines.Count() ) + { + Vector::Iterator lastLine = lines.End() - 1u; + + if( ( 0u == lastLine->characterRun.numberOfCharacters ) && + ( lastGlyphPlusOne == layoutParameters.totalNumberOfGlyphs ) ) + { + lines.Remove( lastLine ); + } + } + // Set the first paragraph's direction. CharacterDirection paragraphDirection = ( NULL != layoutParameters.characterDirectionBuffer ) ? *layoutParameters.characterDirectionBuffer : !RTL; @@ -834,8 +831,7 @@ struct LayoutEngine::Impl Vector newLines; // Estimate the number of lines. - // TODO: In a next patch the paragraphs are properly managed and this can be removed. - Length linesCapacity = CountParagraphs( layoutParameters ); + Length linesCapacity = std::max( 1u, layoutParameters.estimatedNumberOfLines ); Length numberOfLines = 0u; if( updateCurrentBuffer ) @@ -854,10 +850,9 @@ struct LayoutEngine::Impl linesBuffer = lines.Begin(); } - float penY = SetParagraphOffset( lines, - layoutParameters.startLineIndex ); + float penY = CalculateLineOffset( lines, + layoutParameters.startLineIndex ); - const GlyphIndex lastGlyphPlusOne = layoutParameters.startGlyphIndex + layoutParameters.numberOfGlyphs; for( GlyphIndex index = layoutParameters.startGlyphIndex; index < lastGlyphPlusOne; ) { CharacterDirection currentParagraphDirection = paragraphDirection; @@ -933,7 +928,9 @@ struct LayoutEngine::Impl numberOfLines, isLastLine ); - if( isLastLine && + const GlyphIndex nextIndex = index + layout.numberOfGlyphs; + + if( ( nextIndex == layoutParameters.totalNumberOfGlyphs ) && layoutParameters.isLastNewParagraph && ( mLayout == MULTI_LINE_BOX ) ) { @@ -951,25 +948,23 @@ struct LayoutEngine::Impl } UpdateTextLayout( layoutParameters, - layout, + layout.characterIndex + layout.numberOfCharacters, + index + layout.numberOfGlyphs, layoutSize, linesBuffer, - index, numberOfLines ); } // whether to add a last line. // Sets the positions of the glyphs. SetGlyphPositions( layoutParameters.glyphsBuffer + index, layout.numberOfGlyphs, - penY, glyphPositionsBuffer + index - layoutParameters.startGlyphIndex ); // Updates the vertical pen's position. penY += -layout.descender; // Increase the glyph index. - index += layout.numberOfGlyphs; - + index = nextIndex; } // no ellipsis } // end for() traversing glyphs. @@ -978,6 +973,7 @@ struct LayoutEngine::Impl glyphPositions.Insert( glyphPositions.Begin() + layoutParameters.startGlyphIndex, newGlyphPositions.Begin(), newGlyphPositions.End() ); + glyphPositions.Resize( layoutParameters.totalNumberOfGlyphs ); newLines.Resize( numberOfLines ); @@ -1177,10 +1173,26 @@ struct LayoutEngine::Impl } } + void Initialize( LineRun& line ) + { + line.glyphRun.glyphIndex = 0u; + line.glyphRun.numberOfGlyphs = 0u; + line.characterRun.characterIndex = 0u; + line.characterRun.numberOfCharacters = 0u; + line.width = 0.f; + line.ascender = 0.f; + line.descender = 0.f; + line.extraLength = 0.f; + line.alignmentOffset = 0.f; + line.direction = !RTL; + line.ellipsis = false; + } + LayoutEngine::Layout mLayout; LayoutEngine::HorizontalAlignment mHorizontalAlignment; LayoutEngine::VerticalAlignment mVerticalAlignment; float mCursorWidth; + float mDefaultLineSpacing; IntrusivePtr mMetrics; @@ -1210,11 +1222,13 @@ void LayoutEngine::SetLayout( Layout layout ) LayoutEngine::Layout LayoutEngine::GetLayout() const { + DALI_LOG_INFO( gLogFilter, Debug::Verbose, "GetLayout[%d]\n", mImpl->mLayout); return mImpl->mLayout; } void LayoutEngine::SetTextEllipsisEnabled( bool enabled ) { + DALI_LOG_INFO( gLogFilter, Debug::General, "-->LayoutEngine::SetTextEllipsisEnabled[%s]\n", (enabled)?"true":"false" ); mImpl->mEllipsisEnabled = enabled; } @@ -1286,6 +1300,16 @@ void LayoutEngine::Align( const Size& size, lines ); } +void LayoutEngine::SetDefaultLineSpacing( float lineSpacing ) +{ + mImpl->mDefaultLineSpacing = lineSpacing; +} + +float LayoutEngine::GetDefaultLineSpacing() const +{ + return mImpl->mDefaultLineSpacing; +} + } // namespace Text } // namespace Toolkit