X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Ftext%2Frendering%2Ftext-typesetter.cpp;h=b1502a90ec3d56d7344590dbace34b72849e2172;hp=152b7321c5360482d59a8cdda75a15ee6c84a502;hb=04ed06538a1461d2ccdbd132c4902e4572b9960a;hpb=fa834a987e844fb79531954c882581624aef0e68 diff --git a/dali-toolkit/internal/text/rendering/text-typesetter.cpp b/dali-toolkit/internal/text/rendering/text-typesetter.cpp index 152b732..b1502a9 100644 --- a/dali-toolkit/internal/text/rendering/text-typesetter.cpp +++ b/dali-toolkit/internal/text/rendering/text-typesetter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,11 @@ // INTERNAL INCLUDES #include +#include +#include +#include +#include +#include #include namespace Dali @@ -36,6 +41,21 @@ namespace Text namespace { const float HALF(0.5f); +const float ONE_AND_A_HALF(1.5f); + +/** + * @brief Fast multiply & divide by 255. It wiil be useful when we applying alpha value in color + * + * @param x The value between [0..255] + * @param y The value between [0..255] + * @return (x*y)/255 + */ +inline uint8_t MultiplyAndNormalizeColor(const uint8_t& x, const uint8_t& y) noexcept +{ + const uint32_t xy = static_cast(x) * y; + return ((xy << 15) + (xy << 7) + xy) >> 23; +} + /** * @brief Data struct used to set the buffer of the glyph's bitmap into the final bitmap's buffer. */ @@ -44,16 +64,16 @@ struct GlyphData Devel::PixelBuffer bitmapBuffer; ///< The buffer of the whole bitmap. The format is RGBA8888. Vector2* position; ///< The position of the glyph. TextAbstraction::FontClient::GlyphBufferData glyphBitmap; ///< The glyph's bitmap. - unsigned int width; ///< The bitmap's width. - unsigned int height; ///< The bitmap's height. - int horizontalOffset; ///< The horizontal offset to be added to the 'x' glyph's position. - int verticalOffset; ///< The vertical offset to be added to the 'y' glyph's position. + uint32_t width; ///< The bitmap's width. + uint32_t height; ///< The bitmap's height. + int32_t horizontalOffset; ///< The horizontal offset to be added to the 'x' glyph's position. + int32_t verticalOffset; ///< The vertical offset to be added to the 'y' glyph's position. }; /** * @brief Sets the glyph's buffer into the bitmap's buffer. * - * @param[in] data Struct which contains the glyph's data and the bitmap's data. + * @param[in, out] data Struct which contains the glyph's data and the bitmap's data. * @param[in] position The position of the glyph. * @param[in] color The color of the glyph. * @param[in] style The style of the text. @@ -71,46 +91,70 @@ void TypesetGlyph(GlyphData& data, return; } - const int widthMinusOne = static_cast(data.width - 1u); - const int heightMinusOne = static_cast(data.height - 1u); + // Initial vertical / horizontal offset. + const int32_t yOffset = data.verticalOffset + position->y; + const int32_t xOffset = data.horizontalOffset + position->x; + + // Whether the given glyph is a color one. + const bool isColorGlyph = data.glyphBitmap.isColorEmoji || data.glyphBitmap.isColorBitmap; + const uint32_t glyphPixelSize = Pixel::GetBytesPerPixel(data.glyphBitmap.format); + const uint32_t alphaIndex = glyphPixelSize - 1u; + + // Determinate iterator range. + const int32_t lineIndexRangeMin = std::max(0, -yOffset); + const int32_t lineIndexRangeMax = std::min(static_cast(data.glyphBitmap.height), static_cast(data.height) - yOffset); + const int32_t indexRangeMin = std::max(0, -xOffset); + const int32_t indexRangeMax = std::min(static_cast(data.glyphBitmap.width), static_cast(data.width) - xOffset); + + // If current glyph don't need to be rendered, just ignore. + if(lineIndexRangeMax <= lineIndexRangeMin || indexRangeMax <= indexRangeMin) + { + return; + } if(Pixel::RGBA8888 == pixelFormat) { - // Whether the given glyph is a color one. - const bool isColorGlyph = data.glyphBitmap.isColorEmoji || data.glyphBitmap.isColorBitmap; - const uint32_t glyphPixelSize = Pixel::GetBytesPerPixel(data.glyphBitmap.format); - const uint32_t alphaIndex = glyphPixelSize - 1u; - const bool swapChannelsBR = Pixel::BGRA8888 == data.glyphBitmap.format; + const bool swapChannelsBR = Pixel::BGRA8888 == data.glyphBitmap.format; + + uint32_t* bitmapBuffer = reinterpret_cast(data.bitmapBuffer.GetBuffer()); + + // Fast-cut if style is MASK or OUTLINE. Outline not shown for color glyph. + // Just overwrite transparent color and return. + if(isColorGlyph && (Typesetter::STYLE_MASK == style || Typesetter::STYLE_OUTLINE == style)) + { + for(int32_t lineIndex = lineIndexRangeMin; lineIndex < lineIndexRangeMax; ++lineIndex) + { + const int32_t yOffsetIndex = yOffset + lineIndex; + const int32_t verticalOffset = yOffsetIndex * data.width; + + // We can use memset here. + memset(bitmapBuffer + verticalOffset + xOffset + indexRangeMin, 0, (indexRangeMax - indexRangeMin) * sizeof(uint32_t)); + } + return; + } // Pointer to the color glyph if there is one. const uint32_t* const colorGlyphBuffer = isColorGlyph ? reinterpret_cast(data.glyphBitmap.buffer) : NULL; - // Initial vertical offset. - const int yOffset = data.verticalOffset + position->y; + // Precalculate input color's packed result. + uint32_t packedInputColor = 0u; + uint8_t* packedInputColorBuffer = reinterpret_cast(&packedInputColor); - uint32_t* bitmapBuffer = reinterpret_cast(data.bitmapBuffer.GetBuffer()); + *(packedInputColorBuffer + 3u) = static_cast(color->a * 255); + *(packedInputColorBuffer + 2u) = static_cast(color->b * 255); + *(packedInputColorBuffer + 1u) = static_cast(color->g * 255); + *(packedInputColorBuffer) = static_cast(color->r * 255); // Traverse the pixels of the glyph line per line. - for(int lineIndex = 0, glyphHeight = static_cast(data.glyphBitmap.height); lineIndex < glyphHeight; ++lineIndex) + for(int32_t lineIndex = lineIndexRangeMin; lineIndex < lineIndexRangeMax; ++lineIndex) { - const int yOffsetIndex = yOffset + lineIndex; - if((0 > yOffsetIndex) || (yOffsetIndex > heightMinusOne)) - { - // Do not write out of bounds. - continue; - } + const int32_t yOffsetIndex = yOffset + lineIndex; - const int verticalOffset = yOffsetIndex * data.width; - const int xOffset = data.horizontalOffset + position->x; - const int glyphBufferOffset = lineIndex * static_cast(data.glyphBitmap.width); - for(int index = 0, glyphWidth = static_cast(data.glyphBitmap.width); index < glyphWidth; ++index) + const int32_t verticalOffset = yOffsetIndex * data.width; + const int32_t glyphBufferOffset = lineIndex * static_cast(data.glyphBitmap.width); + for(int32_t index = indexRangeMin; index < indexRangeMax; ++index) { - const int xOffsetIndex = xOffset + index; - if((0 > xOffsetIndex) || (xOffsetIndex > widthMinusOne)) - { - // Don't write out of bounds. - continue; - } + const int32_t xOffsetIndex = xOffset + index; if(isColorGlyph) { @@ -119,43 +163,32 @@ void TypesetGlyph(GlyphData& data, uint8_t* packedColorGlyphBuffer = reinterpret_cast(&packedColorGlyph); // Update the alpha channel. - if(Typesetter::STYLE_MASK == style || Typesetter::STYLE_OUTLINE == style) // Outline not shown for color glyph + const uint8_t colorAlpha = MultiplyAndNormalizeColor(*(packedInputColorBuffer + 3u), *(packedColorGlyphBuffer + 3u)); + *(packedColorGlyphBuffer + 3u) = colorAlpha; + + if(Typesetter::STYLE_SHADOW == style) { - // Create an alpha mask for color glyph. - *(packedColorGlyphBuffer + 3u) = 0u; - *(packedColorGlyphBuffer + 2u) = 0u; - *(packedColorGlyphBuffer + 1u) = 0u; - *packedColorGlyphBuffer = 0u; + // The shadow of color glyph needs to have the shadow color. + *(packedColorGlyphBuffer + 2u) = MultiplyAndNormalizeColor(*(packedInputColorBuffer + 2u), colorAlpha); + *(packedColorGlyphBuffer + 1u) = MultiplyAndNormalizeColor(*(packedInputColorBuffer + 1u), colorAlpha); + *packedColorGlyphBuffer = MultiplyAndNormalizeColor(*packedInputColorBuffer, colorAlpha); } else { - const uint8_t colorAlpha = static_cast(color->a * static_cast(*(packedColorGlyphBuffer + 3u))); - *(packedColorGlyphBuffer + 3u) = colorAlpha; - - if(Typesetter::STYLE_SHADOW == style) + if(swapChannelsBR) { - // The shadow of color glyph needs to have the shadow color. - *(packedColorGlyphBuffer + 2u) = static_cast(color->b * colorAlpha); - *(packedColorGlyphBuffer + 1u) = static_cast(color->g * colorAlpha); - *packedColorGlyphBuffer = static_cast(color->r * colorAlpha); + std::swap(*packedColorGlyphBuffer, *(packedColorGlyphBuffer + 2u)); // Swap B and R. } - else + + *(packedColorGlyphBuffer + 2u) = MultiplyAndNormalizeColor(*(packedColorGlyphBuffer + 2u), colorAlpha); + *(packedColorGlyphBuffer + 1u) = MultiplyAndNormalizeColor(*(packedColorGlyphBuffer + 1u), colorAlpha); + *packedColorGlyphBuffer = MultiplyAndNormalizeColor(*packedColorGlyphBuffer, colorAlpha); + + if(data.glyphBitmap.isColorBitmap) { - if(swapChannelsBR) - { - std::swap(*packedColorGlyphBuffer, *(packedColorGlyphBuffer + 2u)); // Swap B and R. - } - - *(packedColorGlyphBuffer + 2u) = (*(packedColorGlyphBuffer + 2u) * colorAlpha / 255); - *(packedColorGlyphBuffer + 1u) = (*(packedColorGlyphBuffer + 1u) * colorAlpha / 255); - *packedColorGlyphBuffer = (*(packedColorGlyphBuffer)*colorAlpha / 255); - - if(data.glyphBitmap.isColorBitmap) - { - *(packedColorGlyphBuffer + 2u) = static_cast(*(packedColorGlyphBuffer + 2u) * color->b); - *(packedColorGlyphBuffer + 1u) = static_cast(*(packedColorGlyphBuffer + 1u) * color->g); - *packedColorGlyphBuffer = static_cast(*packedColorGlyphBuffer * color->r); - } + *(packedColorGlyphBuffer + 2u) = MultiplyAndNormalizeColor(*(packedInputColorBuffer + 2u), *(packedColorGlyphBuffer + 2u)); + *(packedColorGlyphBuffer + 1u) = MultiplyAndNormalizeColor(*(packedInputColorBuffer + 1u), *(packedColorGlyphBuffer + 1u)); + *packedColorGlyphBuffer = MultiplyAndNormalizeColor(*packedInputColorBuffer, *packedColorGlyphBuffer); } } @@ -185,15 +218,22 @@ void TypesetGlyph(GlyphData& data, // happen, for example, in the RTL text when we copy glyphs from right to left). uint8_t currentAlpha = *(packedCurrentColorBuffer + 3u); currentAlpha = std::max(currentAlpha, alpha); - - // Color is pre-muliplied with its alpha. - *(packedColorBuffer + 3u) = static_cast(color->a * currentAlpha); - *(packedColorBuffer + 2u) = static_cast(color->b * currentAlpha); - *(packedColorBuffer + 1u) = static_cast(color->g * currentAlpha); - *(packedColorBuffer) = static_cast(color->r * currentAlpha); - - // Set the color into the final pixel buffer. - currentColor = packedColor; + if(currentAlpha == 255) + { + // Fast-cut to avoid float type operation. + currentColor = packedInputColor; + } + else + { + // Color is pre-muliplied with its alpha. + *(packedColorBuffer + 3u) = MultiplyAndNormalizeColor(*(packedInputColorBuffer + 3u), currentAlpha); + *(packedColorBuffer + 2u) = MultiplyAndNormalizeColor(*(packedInputColorBuffer + 2u), currentAlpha); + *(packedColorBuffer + 1u) = MultiplyAndNormalizeColor(*(packedInputColorBuffer + 1u), currentAlpha); + *(packedColorBuffer) = MultiplyAndNormalizeColor(*packedInputColorBuffer, currentAlpha); + + // Set the color into the final pixel buffer. + currentColor = packedColor; + } } } } @@ -201,40 +241,22 @@ void TypesetGlyph(GlyphData& data, } else { - // Whether the given glyph is a color one. - const bool isColorGlyph = data.glyphBitmap.isColorEmoji || data.glyphBitmap.isColorBitmap; - const uint32_t glyphPixelSize = Pixel::GetBytesPerPixel(data.glyphBitmap.format); - const uint32_t alphaIndex = glyphPixelSize - 1u; - - // Initial vertical offset. - const int yOffset = data.verticalOffset + position->y; - - uint8_t* bitmapBuffer = reinterpret_cast(data.bitmapBuffer.GetBuffer()); - - // Traverse the pixels of the glyph line per line. - for(int lineIndex = 0, glyphHeight = static_cast(data.glyphBitmap.height); lineIndex < glyphHeight; ++lineIndex) + // Below codes required only if not color glyph. + if(!isColorGlyph) { - const int yOffsetIndex = yOffset + lineIndex; - if((0 > yOffsetIndex) || (yOffsetIndex > heightMinusOne)) - { - // Do not write out of bounds. - continue; - } + uint8_t* bitmapBuffer = reinterpret_cast(data.bitmapBuffer.GetBuffer()); - const int verticalOffset = yOffsetIndex * data.width; - const int xOffset = data.horizontalOffset + position->x; - const int glyphBufferOffset = lineIndex * static_cast(data.glyphBitmap.width); - for(int index = 0, glyphWidth = static_cast(data.glyphBitmap.width); index < glyphWidth; ++index) + // Traverse the pixels of the glyph line per line. + for(int32_t lineIndex = lineIndexRangeMin; lineIndex < lineIndexRangeMax; ++lineIndex) { - const int xOffsetIndex = xOffset + index; - if((0 > xOffsetIndex) || (xOffsetIndex > widthMinusOne)) - { - // Don't write out of bounds. - continue; - } + const int32_t yOffsetIndex = yOffset + lineIndex; - if(!isColorGlyph) + const int32_t verticalOffset = yOffsetIndex * data.width; + const int32_t glyphBufferOffset = lineIndex * static_cast(data.glyphBitmap.width); + for(int32_t index = indexRangeMin; index < indexRangeMax; ++index) { + const int32_t xOffsetIndex = xOffset + index; + // Update the alpha channel. const uint8_t alpha = *(data.glyphBitmap.buffer + glyphPixelSize * (glyphBufferOffset + index) + alphaIndex); @@ -256,191 +278,205 @@ void TypesetGlyph(GlyphData& data, } } -bool IsGlyphUnderlined(GlyphIndex index, - const Vector& underlineRuns) +/// Draws the specified underline color to the buffer +void DrawUnderline( + const uint32_t& bufferWidth, + const uint32_t& bufferHeight, + GlyphData& glyphData, + const float& baseline, + const float& currentUnderlinePosition, + const float& maxUnderlineHeight, + const float& lineExtentLeft, + const float& lineExtentRight, + const UnderlineStyleProperties& commonUnderlineProperties, + const UnderlineStyleProperties& currentUnderlineProperties, + const LineRun& line) { - for(Vector::ConstIterator it = underlineRuns.Begin(), - endIt = underlineRuns.End(); - it != endIt; - ++it) - { - const GlyphRun& run = *it; + const Vector4& underlineColor = currentUnderlineProperties.colorDefined ? currentUnderlineProperties.color : commonUnderlineProperties.color; + const Text::Underline::Type underlineType = currentUnderlineProperties.typeDefined ? currentUnderlineProperties.type : commonUnderlineProperties.type; + const float dashedUnderlineWidth = currentUnderlineProperties.dashWidthDefined ? currentUnderlineProperties.dashWidth : commonUnderlineProperties.dashWidth; + const float dashedUnderlineGap = currentUnderlineProperties.dashGapDefined ? currentUnderlineProperties.dashGap : commonUnderlineProperties.dashGap; - if((run.glyphIndex <= index) && (index < run.glyphIndex + run.numberOfGlyphs)) - { - return true; - } - } + int32_t underlineYOffset = glyphData.verticalOffset + baseline + currentUnderlinePosition; - return false; -} + const uint32_t yRangeMin = underlineYOffset; + const uint32_t yRangeMax = std::min(bufferHeight, underlineYOffset + static_cast(maxUnderlineHeight)); + const uint32_t xRangeMin = static_cast(glyphData.horizontalOffset + lineExtentLeft); + const uint32_t xRangeMax = std::min(bufferWidth, static_cast(glyphData.horizontalOffset + lineExtentRight + 1)); // Due to include last point, we add 1 here -/// Helper method to fetch the underline metrics for the specified font glyph -void FetchFontDecorationlinesMetrics( - TextAbstraction::FontClient& fontClient, - const GlyphInfo* const glyphInfo, - float& currentUnderlinePosition, - const float underlineHeight, - float& currentUnderlineThickness, - float& maxUnderlineThickness, - FontId& lastlinedFontId, - const float strikethroughHeight, - float& currentStrikethroughThickness, - float& maxStrikethroughThickness) -{ - FontMetrics fontMetrics; - fontClient.GetFontMetrics(glyphInfo->fontId, fontMetrics); - currentUnderlinePosition = ceil(fabsf(fontMetrics.underlinePosition)); - const float descender = ceil(fabsf(fontMetrics.descender)); - - if(fabsf(underlineHeight) < Math::MACHINE_EPSILON_1000) + // If current glyph don't need to be rendered, just ignore. + if((underlineType != Text::Underline::DOUBLE && yRangeMax <= yRangeMin) || xRangeMax <= xRangeMin) { - currentUnderlineThickness = fontMetrics.underlineThickness; - - // Ensure underline will be at least a pixel high - if(currentUnderlineThickness < 1.0f) - { - currentUnderlineThickness = 1.0f; - } - else - { - currentUnderlineThickness = ceil(currentUnderlineThickness); - } + return; } - if(fabsf(strikethroughHeight) < Math::MACHINE_EPSILON_1000) + // We can optimize by memset when underlineColor.a is near zero + uint8_t underlineColorAlpha = static_cast(underlineColor.a * 255.f); + + uint32_t* bitmapBuffer = reinterpret_cast(glyphData.bitmapBuffer.GetBuffer()); + + // Skip yRangeMin line. + bitmapBuffer += yRangeMin * glyphData.width; + + // Note if underlineType is DASHED, we cannot setup color by memset. + if(underlineType != Text::Underline::DASHED && underlineColorAlpha == 0) { - // Ensure strikethrough will be at least a pixel high - if(currentStrikethroughThickness < 1.0f) + for(uint32_t y = yRangeMin; y < yRangeMax; y++) { - currentStrikethroughThickness = 1.0f; + // We can use memset. + memset(bitmapBuffer + xRangeMin, 0, (xRangeMax - xRangeMin) * sizeof(uint32_t)); + bitmapBuffer += glyphData.width; } - else + if(underlineType == Text::Underline::DOUBLE) { - currentStrikethroughThickness = ceil(currentStrikethroughThickness); - } - } + int32_t secondUnderlineYOffset = underlineYOffset - ONE_AND_A_HALF * maxUnderlineHeight; + const uint32_t secondYRangeMin = static_cast(std::max(0, secondUnderlineYOffset)); + const uint32_t secondYRangeMax = static_cast(std::max(0, std::min(static_cast(bufferHeight), secondUnderlineYOffset + static_cast(maxUnderlineHeight)))); - // The underline thickness should be the max underline thickness of all glyphs of the line. - if(currentUnderlineThickness > maxUnderlineThickness) - { - maxUnderlineThickness = currentUnderlineThickness; - } + // Rewind bitmapBuffer pointer, and skip secondYRangeMin line. + bitmapBuffer = reinterpret_cast(glyphData.bitmapBuffer.GetBuffer()) + yRangeMin * glyphData.width; - // The strikethrough thickness should be the max strikethrough thickness of all glyphs of the line. - if(currentStrikethroughThickness > maxStrikethroughThickness) - { - maxStrikethroughThickness = currentStrikethroughThickness; - } - - // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font - if(currentUnderlinePosition > descender) - { - currentUnderlinePosition = descender; + for(uint32_t y = secondYRangeMin; y < secondYRangeMax; y++) + { + // We can use memset. + memset(bitmapBuffer + xRangeMin, 0, (xRangeMax - xRangeMin) * sizeof(uint32_t)); + bitmapBuffer += glyphData.width; + } + } } - - if(fabsf(currentUnderlinePosition) < Math::MACHINE_EPSILON_1000) + else { - // Move offset down by one ( EFL behavior ) - currentUnderlinePosition = 1.0f; - } - - lastlinedFontId = glyphInfo->fontId; -} + uint32_t packedUnderlineColor = 0u; + uint8_t* packedUnderlineColorBuffer = reinterpret_cast(&packedUnderlineColor); -/// Draws the specified color to the pixel buffer -void WriteColorToPixelBuffer( - GlyphData& glyphData, - uint32_t* bitmapBuffer, - const Vector4& color, - const unsigned int x, - const unsigned int y) -{ - // Always RGBA image for text with styles - uint32_t pixel = *(bitmapBuffer + y * glyphData.width + x); - uint8_t* pixelBuffer = reinterpret_cast(&pixel); - - // Write the color to the pixel buffer - uint8_t colorAlpha = static_cast(color.a * 255.f); - *(pixelBuffer + 3u) = colorAlpha; - *(pixelBuffer + 2u) = static_cast(color.b * colorAlpha); - *(pixelBuffer + 1u) = static_cast(color.g * colorAlpha); - *(pixelBuffer) = static_cast(color.r * colorAlpha); - - *(bitmapBuffer + y * glyphData.width + x) = pixel; -} - -/// Draws the specified underline color to the buffer -void DrawUnderline( - const Vector4& underlineColor, - const unsigned int bufferWidth, - const unsigned int bufferHeight, - GlyphData& glyphData, - const float baseline, - const float currentUnderlinePosition, - const float maxUnderlineThickness, - const float lineExtentLeft, - const float lineExtentRight) -{ - int underlineYOffset = glyphData.verticalOffset + baseline + currentUnderlinePosition; - uint32_t* bitmapBuffer = reinterpret_cast(glyphData.bitmapBuffer.GetBuffer()); + // Write the color to the pixel buffer + *(packedUnderlineColorBuffer + 3u) = underlineColorAlpha; + *(packedUnderlineColorBuffer + 2u) = static_cast(underlineColor.b * underlineColorAlpha); + *(packedUnderlineColorBuffer + 1u) = static_cast(underlineColor.g * underlineColorAlpha); + *(packedUnderlineColorBuffer) = static_cast(underlineColor.r * underlineColorAlpha); - for(unsigned int y = underlineYOffset; y < underlineYOffset + maxUnderlineThickness; y++) - { - if(y > bufferHeight - 1) + for(uint32_t y = yRangeMin; y < yRangeMax; y++) { - // Do not write out of bounds. - break; - } + if(underlineType == Text::Underline::DASHED) + { + float dashWidth = dashedUnderlineWidth; + float dashGap = 0; - for(unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++) - { - if(x > bufferWidth - 1) + for(uint32_t x = xRangeMin; x < xRangeMax; x++) + { + if(dashGap == 0 && dashWidth > 0) + { + // Note : this is same logic as bitmap[y][x] = underlineColor; + *(bitmapBuffer + x) = packedUnderlineColor; + dashWidth--; + } + else if(dashGap < dashedUnderlineGap) + { + dashGap++; + } + else + { + //reset + dashWidth = dashedUnderlineWidth; + dashGap = 0; + } + } + } + else { - // Do not write out of bounds. - break; + for(uint32_t x = xRangeMin; x < xRangeMax; x++) + { + // Note : this is same logic as bitmap[y][x] = underlineColor; + *(bitmapBuffer + x) = packedUnderlineColor; + } } + bitmapBuffer += glyphData.width; + } + if(underlineType == Text::Underline::DOUBLE) + { + int32_t secondUnderlineYOffset = underlineYOffset - ONE_AND_A_HALF * maxUnderlineHeight; + const uint32_t secondYRangeMin = static_cast(std::max(0, secondUnderlineYOffset)); + const uint32_t secondYRangeMax = static_cast(std::max(0, std::min(static_cast(bufferHeight), secondUnderlineYOffset + static_cast(maxUnderlineHeight)))); + + // Rewind bitmapBuffer pointer, and skip secondYRangeMin line. + bitmapBuffer = reinterpret_cast(glyphData.bitmapBuffer.GetBuffer()) + yRangeMin * glyphData.width; - WriteColorToPixelBuffer(glyphData, bitmapBuffer, underlineColor, x, y); + for(uint32_t y = secondYRangeMin; y < secondYRangeMax; y++) + { + for(uint32_t x = xRangeMin; x < xRangeMax; x++) + { + // Note : this is same logic as bitmap[y][x] = underlineColor; + *(bitmapBuffer + x) = packedUnderlineColor; + } + bitmapBuffer += glyphData.width; + } } } } /// Draws the background color to the buffer void DrawBackgroundColor( - Vector4 backgroundColor, - const unsigned int bufferWidth, - const unsigned int bufferHeight, - GlyphData& glyphData, - const float baseline, - const LineRun& line, - const float lineExtentLeft, - const float lineExtentRight) + Vector4 backgroundColor, + const uint32_t& bufferWidth, + const uint32_t& bufferHeight, + GlyphData& glyphData, + const float& baseline, + const LineRun& line, + const float& lineExtentLeft, + const float& lineExtentRight) { + const int32_t yRangeMin = std::max(0, static_cast(glyphData.verticalOffset + baseline - line.ascender)); + const int32_t yRangeMax = std::min(static_cast(bufferHeight), static_cast(glyphData.verticalOffset + baseline - line.descender)); + const int32_t xRangeMin = std::max(0, static_cast(glyphData.horizontalOffset + lineExtentLeft)); + const int32_t xRangeMax = std::min(static_cast(bufferWidth), static_cast(glyphData.horizontalOffset + lineExtentRight + 1)); // Due to include last point, we add 1 here + + // If current glyph don't need to be rendered, just ignore. + if(yRangeMax <= yRangeMin || xRangeMax <= xRangeMin) + { + return; + } + + // We can optimize by memset when backgroundColor.a is near zero + uint8_t backgroundColorAlpha = static_cast(backgroundColor.a * 255.f); + uint32_t* bitmapBuffer = reinterpret_cast(glyphData.bitmapBuffer.GetBuffer()); - for(int y = glyphData.verticalOffset + baseline - line.ascender; y < glyphData.verticalOffset + baseline - line.descender; y++) + // Skip yRangeMin line. + bitmapBuffer += yRangeMin * glyphData.width; + + if(backgroundColorAlpha == 0) { - if((y < 0) || (y > static_cast(bufferHeight - 1))) + for(int32_t y = yRangeMin; y < yRangeMax; y++) { - // Do not write out of bounds. - continue; + // We can use memset. + memset(bitmapBuffer + xRangeMin, 0, (xRangeMax - xRangeMin) * sizeof(uint32_t)); + bitmapBuffer += glyphData.width; } + } + else + { + uint32_t packedBackgroundColor = 0u; + uint8_t* packedBackgroundColorBuffer = reinterpret_cast(&packedBackgroundColor); + + // Write the color to the pixel buffer + *(packedBackgroundColorBuffer + 3u) = backgroundColorAlpha; + *(packedBackgroundColorBuffer + 2u) = static_cast(backgroundColor.b * backgroundColorAlpha); + *(packedBackgroundColorBuffer + 1u) = static_cast(backgroundColor.g * backgroundColorAlpha); + *(packedBackgroundColorBuffer) = static_cast(backgroundColor.r * backgroundColorAlpha); - for(int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++) + for(int32_t y = yRangeMin; y < yRangeMax; y++) { - if((x < 0) || (x > static_cast(bufferWidth - 1))) + for(int32_t x = xRangeMin; x < xRangeMax; x++) { - // Do not write out of bounds. - continue; + // Note : this is same logic as bitmap[y][x] = backgroundColor; + *(bitmapBuffer + x) = packedBackgroundColor; } - - WriteColorToPixelBuffer(glyphData, bitmapBuffer, backgroundColor, x, y); + bitmapBuffer += glyphData.width; } } } -Devel::PixelBuffer DrawGlyphsBackground(const ViewModel* model, Devel::PixelBuffer& buffer, const unsigned int bufferWidth, const unsigned int bufferHeight, bool ignoreHorizontalAlignment, int horizontalOffset, int verticalOffset) +Devel::PixelBuffer DrawGlyphsBackground(const ViewModel* model, Devel::PixelBuffer& buffer, const uint32_t& bufferWidth, const uint32_t& bufferHeight, bool ignoreHorizontalAlignment, int32_t horizontalOffset, int32_t verticalOffset) { // Retrieve lines, glyphs, positions and colors from the view model. const Length modelNumberOfLines = model->GetNumberOfLines(); @@ -451,6 +487,8 @@ Devel::PixelBuffer DrawGlyphsBackground(const ViewModel* model, Devel::PixelBuff const Vector4* const backgroundColorsBuffer = model->GetBackgroundColors(); const ColorIndex* const backgroundColorIndicesBuffer = model->GetBackgroundColorIndices(); + const DevelText::VerticalLineAlignment::Type verLineAlign = model->GetVerticalLineAlignment(); + // Create and initialize the pixel buffer. GlyphData glyphData; glyphData.verticalOffset = verticalOffset; @@ -468,17 +506,11 @@ Devel::PixelBuffer DrawGlyphsBackground(const ViewModel* model, Devel::PixelBuff const LineRun& line = *(modelLinesBuffer + lineIndex); // Sets the horizontal offset of the line. - glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast(line.alignmentOffset); + glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast(line.alignmentOffset); glyphData.horizontalOffset += horizontalOffset; // Increases the vertical offset with the line's ascender. - glyphData.verticalOffset += static_cast(line.ascender); - - // Include line spacing after first line - if(lineIndex > 0u) - { - glyphData.verticalOffset += static_cast(line.lineSpacing); - } + glyphData.verticalOffset += static_cast(line.ascender + GetPreOffsetVerticalLineAlignment(line, verLineAlign)); float left = bufferWidth; float right = 0.0f; @@ -544,69 +576,98 @@ Devel::PixelBuffer DrawGlyphsBackground(const ViewModel* model, Devel::PixelBuff } // Increases the vertical offset with the line's descender. - glyphData.verticalOffset += static_cast(-line.descender); + glyphData.verticalOffset += static_cast(-line.descender + GetPostOffsetVerticalLineAlignment(line, verLineAlign)); } return glyphData.bitmapBuffer; } /// Draws the specified strikethrough color to the buffer -void DrawStrikethrough( - const Vector4& strikethroughColor, - const unsigned int bufferWidth, - const unsigned int bufferHeight, - GlyphData& glyphData, - const float baseline, - const LineRun& line, - const float maxStrikethroughThickness, - const float lineExtentLeft, - const float lineExtentRight, - float strikethroughStartingYPosition) +void DrawStrikethrough(const uint32_t& bufferWidth, + const uint32_t& bufferHeight, + GlyphData& glyphData, + const float& baseline, + const float& strikethroughStartingYPosition, + const float& maxStrikethroughHeight, + const float& lineExtentLeft, + const float& lineExtentRight, + const StrikethroughStyleProperties& commonStrikethroughProperties, + const StrikethroughStyleProperties& currentStrikethroughProperties, + const LineRun& line) { + const Vector4& strikethroughColor = currentStrikethroughProperties.colorDefined ? currentStrikethroughProperties.color : commonStrikethroughProperties.color; + + const uint32_t yRangeMin = static_cast(strikethroughStartingYPosition); + const uint32_t yRangeMax = std::min(bufferHeight, static_cast(strikethroughStartingYPosition + maxStrikethroughHeight)); + const uint32_t xRangeMin = static_cast(glyphData.horizontalOffset + lineExtentLeft); + const uint32_t xRangeMax = std::min(bufferWidth, static_cast(glyphData.horizontalOffset + lineExtentRight + 1)); // Due to include last point, we add 1 here + + // If current glyph don't need to be rendered, just ignore. + if(yRangeMax <= yRangeMin || xRangeMax <= xRangeMin) + { + return; + } + + // We can optimize by memset when strikethroughColor.a is near zero + uint8_t strikethroughColorAlpha = static_cast(strikethroughColor.a * 255.f); + uint32_t* bitmapBuffer = reinterpret_cast(glyphData.bitmapBuffer.GetBuffer()); - for(unsigned int y = strikethroughStartingYPosition; y < strikethroughStartingYPosition + maxStrikethroughThickness; y++) + // Skip yRangeMin line. + bitmapBuffer += yRangeMin * glyphData.width; + + if(strikethroughColorAlpha == 0) { - if(y > bufferHeight - 1) + for(uint32_t y = yRangeMin; y < yRangeMax; y++) { - // Do not write out of bounds. - break; + // We can use memset. + memset(bitmapBuffer + xRangeMin, 0, (xRangeMax - xRangeMin) * sizeof(uint32_t)); + bitmapBuffer += glyphData.width; } + } + else + { + uint32_t packedStrikethroughColor = 0u; + uint8_t* packedStrikethroughColorBuffer = reinterpret_cast(&packedStrikethroughColor); + + // Write the color to the pixel buffer + *(packedStrikethroughColorBuffer + 3u) = strikethroughColorAlpha; + *(packedStrikethroughColorBuffer + 2u) = static_cast(strikethroughColor.b * strikethroughColorAlpha); + *(packedStrikethroughColorBuffer + 1u) = static_cast(strikethroughColor.g * strikethroughColorAlpha); + *(packedStrikethroughColorBuffer) = static_cast(strikethroughColor.r * strikethroughColorAlpha); - for(unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++) + for(uint32_t y = yRangeMin; y < yRangeMax; y++) { - if(x > bufferWidth - 1) + for(uint32_t x = xRangeMin; x < xRangeMax; x++) { - // Do not write out of bounds. - break; + // Note : this is same logic as bitmap[y][x] = strikethroughColor; + *(bitmapBuffer + x) = packedStrikethroughColor; } - - WriteColorToPixelBuffer(glyphData, bitmapBuffer, strikethroughColor, x, y); + bitmapBuffer += glyphData.width; } } } -} // namespace - -TypesetterPtr Typesetter::New(const ModelInterface* const model) -{ - return TypesetterPtr(new Typesetter(model)); -} - -ViewModel* Typesetter::GetViewModel() -{ - return mModel; -} - -Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth, const unsigned int bufferHeight, Pixel::Format pixelFormat) +/** + * @brief Create an initialized image buffer filled with transparent color. + * + * Creates the pixel data used to generate the final image with the given size. + * + * @param[in] bufferWidth The width of the image buffer. + * @param[in] bufferHeight The height of the image buffer. + * @param[in] pixelFormat The format of the pixel in the image that the text is rendered as (i.e. either Pixel::BGRA8888 or Pixel::L8). + * + * @return An image buffer. + */ +inline Devel::PixelBuffer CreateTransparentImageBuffer(const uint32_t& bufferWidth, const uint32_t& bufferHeight, const Pixel::Format& pixelFormat) { Devel::PixelBuffer imageBuffer = Devel::PixelBuffer::New(bufferWidth, bufferHeight, pixelFormat); if(Pixel::RGBA8888 == pixelFormat) { - const unsigned int bufferSizeInt = bufferWidth * bufferHeight; - const unsigned int bufferSizeChar = 4u * bufferSizeInt; - memset(imageBuffer.GetBuffer(), 0u, bufferSizeChar); + const uint32_t bufferSizeInt = bufferWidth * bufferHeight; + const uint32_t bufferSizeChar = sizeof(uint32_t) * bufferSizeInt; + memset(imageBuffer.GetBuffer(), 0, bufferSizeChar); } else { @@ -616,6 +677,125 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth, return imageBuffer; } +/** + * @brief Combine the two RGBA image buffers together. + * + * The top layer buffer will blend over the bottom layer buffer: + * - If the pixel is not fully opaque from either buffer, it will be blended with + * the pixel from the other buffer and copied to the combined buffer. + * - If the pixels from both buffers are fully opaque, the pixels from the top layer + * buffer will be copied to the combined buffer. + * + * Due to the performance issue, We need to re-use input'ed pixelBuffer memory. + * We can determine which pixelBuffer's memory is destination + * + * @param[in, out] topPixelBuffer The top layer buffer. + * @param[in, out] bottomPixelBuffer The bottom layer buffer. + * @param[in] bufferWidth The width of the image buffer. + * @param[in] bufferHeight The height of the image buffer. + * @param[in] storeResultIntoTop True if we store the combined image buffer result into topPixelBuffer. + * False if we store the combined image buffer result into bottomPixelBuffer. + * + */ +void CombineImageBuffer(Devel::PixelBuffer& topPixelBuffer, Devel::PixelBuffer& bottomPixelBuffer, const uint32_t& bufferWidth, const uint32_t& bufferHeight, bool storeResultIntoTop) +{ + // Assume that we always combine two RGBA images + // Jump with 4bytes for optimize runtime. + uint32_t* topBuffer = reinterpret_cast(topPixelBuffer.GetBuffer()); + uint32_t* bottomBuffer = reinterpret_cast(bottomPixelBuffer.GetBuffer()); + + if(topBuffer == NULL && bottomBuffer == NULL) + { + // Nothing to do if both buffers are empty. + return; + } + + if(topBuffer == NULL) + { + // Nothing to do if topBuffer is empty. + // If we need to store the result into top, change topPixelBuffer as bottomPixelBuffer. + if(storeResultIntoTop) + { + topPixelBuffer = bottomPixelBuffer; + } + return; + } + + if(bottomBuffer == NULL) + { + // Nothing to do if bottomBuffer is empty. + // If we need to store the result into bottom, change bottomPixelBuffer as topPixelBuffer. + if(!storeResultIntoTop) + { + bottomPixelBuffer = topPixelBuffer; + } + return; + } + + const uint32_t bufferSizeInt = bufferWidth * bufferHeight; + + uint32_t* combinedBuffer = storeResultIntoTop ? topBuffer : bottomBuffer; + uint8_t* topAlphaBufferPointer = reinterpret_cast(topBuffer) + 3; + + for(uint32_t pixelIndex = 0; pixelIndex < bufferSizeInt; ++pixelIndex) + { + // If the alpha of the pixel in either buffer is not fully opaque, blend the two pixels. + // Otherwise, copy pixel from topBuffer to combinedBuffer. + // Note : Be careful when we read & write into combinedBuffer. It can be write into same pointer. + + uint8_t topAlpha = *topAlphaBufferPointer; + + if(topAlpha == 0) + { + // Copy the pixel from bottomBuffer to combinedBuffer + if(storeResultIntoTop) + { + *(combinedBuffer) = *(bottomBuffer); + } + } + else if(topAlpha == 255) + { + // Copy the pixel from topBuffer to combinedBuffer + if(!storeResultIntoTop) + { + *(combinedBuffer) = *(topBuffer); + } + } + else + { + // At least one pixel is not fully opaque + // "Over" blend the the pixel from topBuffer with the pixel in bottomBuffer + uint32_t blendedBottomBufferColor = *(bottomBuffer); + uint8_t* blendedBottomBufferColorBuffer = reinterpret_cast(&blendedBottomBufferColor); + + blendedBottomBufferColorBuffer[0] = MultiplyAndNormalizeColor(blendedBottomBufferColorBuffer[0], 255 - topAlpha); + blendedBottomBufferColorBuffer[1] = MultiplyAndNormalizeColor(blendedBottomBufferColorBuffer[1], 255 - topAlpha); + blendedBottomBufferColorBuffer[2] = MultiplyAndNormalizeColor(blendedBottomBufferColorBuffer[2], 255 - topAlpha); + blendedBottomBufferColorBuffer[3] = MultiplyAndNormalizeColor(blendedBottomBufferColorBuffer[3], 255 - topAlpha); + + *(combinedBuffer) = *(topBuffer) + blendedBottomBufferColor; + } + + // Increase each buffer's pointer. + ++combinedBuffer; + ++topBuffer; + ++bottomBuffer; + topAlphaBufferPointer += sizeof(uint32_t) / sizeof(uint8_t); + } +} + +} // namespace + +TypesetterPtr Typesetter::New(const ModelInterface* const model) +{ + return TypesetterPtr(new Typesetter(model)); +} + +ViewModel* Typesetter::GetViewModel() +{ + return mModel; +} + PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirection::Type textDirection, RenderBehaviour behaviour, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat) { // @todo. This initial implementation for a TextLabel has only one visible page. @@ -626,10 +806,10 @@ PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirect // Retrieves the layout size. const Size& layoutSize = mModel->GetLayoutSize(); - const int outlineWidth = static_cast(mModel->GetOutlineWidth()); + const int32_t outlineWidth = static_cast(mModel->GetOutlineWidth()); // Set the offset for the horizontal alignment according to the text direction and outline width. - int penX = 0; + int32_t penX = 0; switch(mModel->GetHorizontalAlignment()) { @@ -651,7 +831,7 @@ PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirect } // Set the offset for the vertical alignment. - int penY = 0u; + int32_t penY = 0u; switch(mModel->GetVerticalAlignment()) { @@ -662,36 +842,13 @@ PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirect } case VerticalAlignment::CENTER: { - penY = static_cast(0.5f * (size.height - layoutSize.height)); + penY = static_cast(0.5f * (size.height - layoutSize.height)); penY = penY < 0.f ? 0.f : penY; break; } case VerticalAlignment::BOTTOM: { - penY = static_cast(size.height - layoutSize.height); - break; - } - } - - // Calculate vertical line alignment - switch(mModel->GetVerticalLineAlignment()) - { - case DevelText::VerticalLineAlignment::TOP: - { - break; - } - case DevelText::VerticalLineAlignment::MIDDLE: - { - const auto& line = *mModel->GetLines(); - penY -= line.descender; - penY += static_cast(line.lineSpacing * 0.5f + line.descender); - break; - } - case DevelText::VerticalLineAlignment::BOTTOM: - { - const auto& line = *mModel->GetLines(); - const auto lineHeight = line.ascender + (-line.descender) + line.lineSpacing; - penY += static_cast(lineHeight - (line.ascender - line.descender)); + penY = static_cast(size.height - layoutSize.height); break; } } @@ -701,11 +858,11 @@ PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirect // do all of these in CPU only, so that once the final texture is generated, // no calculation is needed in GPU during each frame. - const unsigned int bufferWidth = static_cast(size.width); - const unsigned int bufferHeight = static_cast(size.height); + const uint32_t bufferWidth = static_cast(size.width); + const uint32_t bufferHeight = static_cast(size.height); - const unsigned int bufferSizeInt = bufferWidth * bufferHeight; - const unsigned int bufferSizeChar = 4u * bufferSizeInt; + const uint32_t bufferSizeInt = bufferWidth * bufferHeight; + const uint32_t bufferSizeChar = sizeof(uint32_t) * bufferSizeInt; //Elided text in ellipsis at START could start on index greater than 0 auto startIndexOfGlyphs = mModel->GetStartIndexOfElidedGlyphs(); @@ -740,7 +897,7 @@ PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirect Devel::PixelBuffer outlineImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_OUTLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, startIndexOfGlyphs, endIndexOfGlyphs); // Combine the two buffers - imageBuffer = CombineImageBuffer(imageBuffer, outlineImageBuffer, bufferWidth, bufferHeight); + CombineImageBuffer(imageBuffer, outlineImageBuffer, bufferWidth, bufferHeight, true); } // @todo. Support shadow and underline for partial text later on. @@ -761,7 +918,7 @@ PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirect } // Combine the two buffers - imageBuffer = CombineImageBuffer(imageBuffer, shadowImageBuffer, bufferWidth, bufferHeight); + CombineImageBuffer(imageBuffer, shadowImageBuffer, bufferWidth, bufferHeight, true); } // Generate the underline if enabled @@ -772,7 +929,7 @@ PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirect Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, startIndexOfGlyphs, endIndexOfGlyphs); // Combine the two buffers - imageBuffer = CombineImageBuffer(imageBuffer, underlineImageBuffer, bufferWidth, bufferHeight); + CombineImageBuffer(imageBuffer, underlineImageBuffer, bufferWidth, bufferHeight, true); } // Generate the background if enabled @@ -788,7 +945,7 @@ PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirect } else { - backgroundImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, pixelFormat); + backgroundImageBuffer = CreateTransparentImageBuffer(bufferWidth, bufferHeight, pixelFormat); } if(backgroundMarkupSet) @@ -797,7 +954,7 @@ PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirect } // Combine the two buffers - imageBuffer = CombineImageBuffer(imageBuffer, backgroundImageBuffer, bufferWidth, bufferHeight); + CombineImageBuffer(imageBuffer, backgroundImageBuffer, bufferWidth, bufferHeight, true); } // Generate the strikethrough if enabled @@ -808,7 +965,7 @@ PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirect Devel::PixelBuffer strikethroughImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_STRIKETHROUGH, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, endIndexOfGlyphs); // Combine the two buffers - imageBuffer = CombineImageBuffer(imageBuffer, strikethroughImageBuffer, bufferWidth, bufferHeight); + CombineImageBuffer(imageBuffer, strikethroughImageBuffer, bufferWidth, bufferHeight, true); } // Markup-Processor @@ -822,7 +979,7 @@ PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirect return pixelData; } -Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth, const unsigned int bufferHeight, Typesetter::Style style, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, int horizontalOffset, int verticalOffset, GlyphIndex fromGlyphIndex, GlyphIndex toGlyphIndex) +Devel::PixelBuffer Typesetter::CreateImageBuffer(const uint32_t& bufferWidth, const uint32_t& bufferHeight, Typesetter::Style style, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, const int32_t& horizontalOffset, const int32_t& verticalOffset, GlyphIndex fromGlyphIndex, GlyphIndex toGlyphIndex) { // Retrieve lines, glyphs, positions and colors from the view model. const Length modelNumberOfLines = mModel->GetNumberOfLines(); @@ -851,30 +1008,31 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth, glyphData.verticalOffset = verticalOffset; glyphData.width = bufferWidth; glyphData.height = bufferHeight; - glyphData.bitmapBuffer = CreateImageBuffer(bufferWidth, bufferHeight, pixelFormat); + glyphData.bitmapBuffer = CreateTransparentImageBuffer(bufferWidth, bufferHeight, pixelFormat); glyphData.horizontalOffset = 0; // Get a handle of the font client. Used to retrieve the bitmaps of the glyphs. TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get(); Length hyphenIndex = 0; + const Character* textBuffer = mModel->GetTextBuffer(); + float calculatedAdvance = 0.f; + const Vector& glyphToCharacterMap = mModel->GetGlyphsToCharacters(); + const CharacterIndex* glyphToCharacterMapBuffer = glyphToCharacterMap.Begin(); + + const DevelText::VerticalLineAlignment::Type verLineAlign = mModel->GetVerticalLineAlignment(); + // Traverses the lines of the text. for(LineIndex lineIndex = 0u; lineIndex < modelNumberOfLines; ++lineIndex) { const LineRun& line = *(modelLinesBuffer + lineIndex); // Sets the horizontal offset of the line. - glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast(line.alignmentOffset); + glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast(line.alignmentOffset); glyphData.horizontalOffset += horizontalOffset; // Increases the vertical offset with the line's ascender. - glyphData.verticalOffset += static_cast(line.ascender); - - // Include line spacing after first line - if(lineIndex > 0u) - { - glyphData.verticalOffset += static_cast(line.lineSpacing); - } + glyphData.verticalOffset += static_cast(line.ascender + GetPreOffsetVerticalLineAlignment(line, verLineAlign)); // Retrieves the glyph's outline width float outlineWidth = static_cast(mModel->GetOutlineWidth()); @@ -900,31 +1058,57 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth, } } - const bool underlineEnabled = mModel->IsUnderlineEnabled(); - const Vector4& underlineColor = mModel->GetUnderlineColor(); - const float underlineHeight = mModel->GetUnderlineHeight(); - - const bool strikethroughEnabled = mModel->IsStrikethroughEnabled(); - const Vector4& strikethroughColor = mModel->GetStrikethroughColor(); - const float strikethroughHeight = mModel->GetStrikethroughHeight(); + const bool underlineEnabled = mModel->IsUnderlineEnabled(); + const bool strikethroughEnabled = mModel->IsStrikethroughEnabled(); + const float modelCharacterSpacing = mModel->GetCharacterSpacing(); + + // Get the character-spacing runs. + const Vector& characterSpacingGlyphRuns = mModel->GetCharacterSpacingGlyphRuns(); + + // Aggregate underline-style-properties from mModel + const UnderlineStyleProperties modelUnderlineProperties{mModel->GetUnderlineType(), + mModel->GetUnderlineColor(), + mModel->GetUnderlineHeight(), + mModel->GetDashedUnderlineGap(), + mModel->GetDashedUnderlineWidth(), + true, + true, + true, + true, + true}; + + // Aggregate strikethrough-style-properties from mModel + const StrikethroughStyleProperties modelStrikethroughProperties{mModel->GetStrikethroughColor(), + mModel->GetStrikethroughHeight(), + true, + true}; // Get the underline runs. - const Length numberOfUnderlineRuns = mModel->GetNumberOfUnderlineRuns(); - Vector underlineRuns; + const Length numberOfUnderlineRuns = mModel->GetNumberOfUnderlineRuns(); + Vector underlineRuns; underlineRuns.Resize(numberOfUnderlineRuns); mModel->GetUnderlineRuns(underlineRuns.Begin(), 0u, numberOfUnderlineRuns); - bool thereAreUnderlinedGlyphs = false; - bool strikethroughGlyphsExist = false; + // Get the strikethrough runs. + const Length numberOfStrikethroughRuns = mModel->GetNumberOfStrikethroughRuns(); + Vector strikethroughRuns; + strikethroughRuns.Resize(numberOfStrikethroughRuns); + mModel->GetStrikethroughRuns(strikethroughRuns.Begin(), 0u, numberOfStrikethroughRuns); + + bool thereAreUnderlinedGlyphs = false; + bool thereAreStrikethroughGlyphs = false; + + float currentUnderlinePosition = 0.0f; + float currentUnderlineHeight = modelUnderlineProperties.height; + float maxUnderlineHeight = currentUnderlineHeight; + auto currentUnderlineProperties = modelUnderlineProperties; - float currentUnderlinePosition = 0.0f; - float currentUnderlineThickness = underlineHeight; - float maxUnderlineThickness = currentUnderlineThickness; - float currentStrikethroughThickness = strikethroughHeight; - float maxStrikethroughThickness = currentStrikethroughThickness; + float currentStrikethroughHeight = modelStrikethroughProperties.height; + float maxStrikethroughHeight = currentStrikethroughHeight; + auto currentStrikethroughProperties = modelStrikethroughProperties; float strikethroughStartingYPosition = 0.0f; - FontId lastUnderlinedFontId = 0; + FontId lastFontId = 0; float lineExtentLeft = bufferWidth; float lineExtentRight = 0.0f; @@ -932,18 +1116,12 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth, bool addHyphen = false; // Traverses the glyphs of the line. - const GlyphIndex startGlyphIndex = std::max(line.glyphRun.glyphIndex, startIndexOfGlyphs); + const GlyphIndex startGlyphIndex = std::max(std::max(line.glyphRun.glyphIndex, startIndexOfGlyphs), fromGlyphIndex); GlyphIndex endGlyphIndex = (line.isSplitToTwoHalves ? line.glyphRunSecondHalf.glyphIndex + line.glyphRunSecondHalf.numberOfGlyphs : line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs) - 1u; - endGlyphIndex = std::min(endGlyphIndex, endIndexOfGlyphs); + endGlyphIndex = std::min(std::min(endGlyphIndex, endIndexOfGlyphs), toGlyphIndex); for(GlyphIndex glyphIndex = startGlyphIndex; glyphIndex <= endGlyphIndex; ++glyphIndex) { - if(glyphIndex < fromGlyphIndex || glyphIndex > toGlyphIndex) - { - // Ignore any glyph that out of the specified range - continue; - } - //To handle START case of ellipsis, the first glyph has been shifted //glyphIndex represent indices in whole glyphs but elidedGlyphIndex represents indices in elided Glyphs GlyphIndex elidedGlyphIndex = glyphIndex - startIndexOfGlyphs; @@ -983,26 +1161,52 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth, continue; } - const bool underlineGlyph = underlineEnabled || IsGlyphUnderlined(glyphIndex, underlineRuns); - thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || underlineGlyph; + Vector::ConstIterator currentUnderlinedGlyphRunIt = underlineRuns.End(); + const bool underlineGlyph = underlineEnabled || IsGlyphUnderlined(glyphIndex, underlineRuns, currentUnderlinedGlyphRunIt); + currentUnderlineProperties = GetCurrentUnderlineProperties(glyphIndex, underlineGlyph, underlineRuns, currentUnderlinedGlyphRunIt, modelUnderlineProperties); + currentUnderlineHeight = currentUnderlineProperties.height; + thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || underlineGlyph; - strikethroughGlyphsExist = strikethroughGlyphsExist || strikethroughEnabled; + Vector::ConstIterator currentStrikethroughGlyphRunIt = strikethroughRuns.End(); + const bool strikethroughGlyph = strikethroughEnabled || IsGlyphStrikethrough(glyphIndex, strikethroughRuns, currentStrikethroughGlyphRunIt); + currentStrikethroughProperties = GetCurrentStrikethroughProperties(glyphIndex, strikethroughGlyph, strikethroughRuns, currentStrikethroughGlyphRunIt, modelStrikethroughProperties); + currentStrikethroughHeight = currentStrikethroughProperties.height; + thereAreStrikethroughGlyphs = thereAreStrikethroughGlyphs || strikethroughGlyph; // Are we still using the same fontId as previous - if((strikethroughEnabled || underlineGlyph) && (glyphInfo->fontId != lastUnderlinedFontId)) + if((glyphInfo->fontId != lastFontId) && (strikethroughGlyph || underlineGlyph)) { // We need to fetch fresh font underline metrics - FetchFontDecorationlinesMetrics(fontClient, glyphInfo, currentUnderlinePosition, underlineHeight, currentUnderlineThickness, maxUnderlineThickness, lastUnderlinedFontId, strikethroughHeight, currentStrikethroughThickness, maxStrikethroughThickness); - } // underline + FontMetrics fontMetrics; + fontClient.GetFontMetrics(glyphInfo->fontId, fontMetrics); + + //The currentUnderlinePosition will be used for both Underline and/or Strikethrough + currentUnderlinePosition = FetchUnderlinePositionFromFontMetrics(fontMetrics); + + if(underlineGlyph) + { + CalcualteUnderlineHeight(fontMetrics, currentUnderlineHeight, maxUnderlineHeight); + } + + if(strikethroughGlyph) + { + CalcualteStrikethroughHeight(currentStrikethroughHeight, maxStrikethroughHeight); + } + + // Update lastFontId because fontId is changed + lastFontId = glyphInfo->fontId; // Prevents searching for existing blocksizes when string of the same fontId. + } // Retrieves the glyph's position. Vector2 position = *(positionBuffer + elidedGlyphIndex); if(addHyphen) { - GlyphInfo tempInfo = *(glyphsBuffer + elidedGlyphIndex); - position.x = position.x + tempInfo.advance - tempInfo.xBearing + glyphInfo->xBearing; - position.y = -glyphInfo->yBearing; + GlyphInfo tempInfo = *(glyphsBuffer + elidedGlyphIndex); + const float characterSpacing = GetGlyphCharacterSpacing(glyphIndex, characterSpacingGlyphRuns, modelCharacterSpacing); + calculatedAdvance = GetCalculatedAdvance(*(textBuffer + (*(glyphToCharacterMapBuffer + elidedGlyphIndex))), characterSpacing, tempInfo.advance); + position.x = position.x + calculatedAdvance - tempInfo.xBearing + glyphInfo->xBearing; + position.y = -glyphInfo->yBearing; } if(baseline < position.y + glyphInfo->yBearing) @@ -1061,7 +1265,7 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth, glyphInfo->isItalicRequired, glyphInfo->isBoldRequired, glyphData.glyphBitmap, - static_cast(outlineWidth)); + static_cast(outlineWidth)); } // Sets the glyph's bitmap into the bitmap of the whole text. @@ -1111,7 +1315,7 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth, // Draw the underline from the leftmost glyph to the rightmost glyph if(thereAreUnderlinedGlyphs && style == Typesetter::STYLE_UNDERLINE) { - DrawUnderline(underlineColor, bufferWidth, bufferHeight, glyphData, baseline, currentUnderlinePosition, maxUnderlineThickness, lineExtentLeft, lineExtentRight); + DrawUnderline(bufferWidth, bufferHeight, glyphData, baseline, currentUnderlinePosition, maxUnderlineHeight, lineExtentLeft, lineExtentRight, modelUnderlineProperties, currentUnderlineProperties, line); } // Draw the background color from the leftmost glyph to the rightmost glyph @@ -1121,119 +1325,93 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth, } // Draw the strikethrough from the leftmost glyph to the rightmost glyph - if(strikethroughGlyphsExist && style == Typesetter::STYLE_STRIKETHROUGH) + if(thereAreStrikethroughGlyphs && style == Typesetter::STYLE_STRIKETHROUGH) { //TODO : The currently implemented strikethrough creates a strikethrough on the line level. We need to create different strikethroughs the case of glyphs with different sizes. strikethroughStartingYPosition = (glyphData.verticalOffset + baseline + currentUnderlinePosition) - ((line.ascender) * HALF); // Since Free Type font doesn't contain the strikethrough-position property, strikethrough position will be calculated by moving the underline position upwards by half the value of the line height. - DrawStrikethrough(strikethroughColor, bufferWidth, bufferHeight, glyphData, baseline, line, maxStrikethroughThickness, lineExtentLeft, lineExtentRight, strikethroughStartingYPosition); + DrawStrikethrough(bufferWidth, bufferHeight, glyphData, baseline, strikethroughStartingYPosition, maxStrikethroughHeight, lineExtentLeft, lineExtentRight, modelStrikethroughProperties, currentStrikethroughProperties, line); } - // Increases the vertical offset with the line's descender. - glyphData.verticalOffset += static_cast(-line.descender); + // Increases the vertical offset with the line's descender & line spacing. + glyphData.verticalOffset += static_cast(-line.descender + GetPostOffsetVerticalLineAlignment(line, verLineAlign)); } return glyphData.bitmapBuffer; } -Devel::PixelBuffer Typesetter::CombineImageBuffer(Devel::PixelBuffer topPixelBuffer, Devel::PixelBuffer bottomPixelBuffer, const unsigned int bufferWidth, const unsigned int bufferHeight) +Devel::PixelBuffer Typesetter::ApplyUnderlineMarkupImageBuffer(Devel::PixelBuffer topPixelBuffer, const uint32_t& bufferWidth, const uint32_t& bufferHeight, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, const int32_t& horizontalOffset, const int32_t& verticalOffset) { - unsigned char* topBuffer = topPixelBuffer.GetBuffer(); - unsigned char* bottomBuffer = bottomPixelBuffer.GetBuffer(); - - Devel::PixelBuffer combinedPixelBuffer; - - if(topBuffer == NULL && bottomBuffer == NULL) + // Underline-tags (this is for Markup case) + // Get the underline runs. + const Length numberOfUnderlineRuns = mModel->GetNumberOfUnderlineRuns(); + Vector underlineRuns; + underlineRuns.Resize(numberOfUnderlineRuns); + mModel->GetUnderlineRuns(underlineRuns.Begin(), 0u, numberOfUnderlineRuns); + + // Iterate on the consecutive underlined glyph run and connect them into one chunk of underlined characters. + Vector::ConstIterator itGlyphRun = underlineRuns.Begin(); + Vector::ConstIterator endItGlyphRun = underlineRuns.End(); + GlyphIndex startGlyphIndex, endGlyphIndex; + + //The outer loop to iterate on the separated chunks of underlined glyph runs + while(itGlyphRun != endItGlyphRun) { - // Nothing to do if both buffers are empty. - return combinedPixelBuffer; - } + startGlyphIndex = itGlyphRun->glyphRun.glyphIndex; + endGlyphIndex = startGlyphIndex + itGlyphRun->glyphRun.numberOfGlyphs - 1; - if(topBuffer == NULL) - { - // Nothing to do if topBuffer is empty. - return bottomPixelBuffer; - } + // Create the image buffer for underline + Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, horizontalOffset, verticalOffset, startGlyphIndex, endGlyphIndex); + // Combine the two buffers + // Result pixel buffer will be stored into topPixelBuffer. + CombineImageBuffer(underlineImageBuffer, topPixelBuffer, bufferWidth, bufferHeight, false); - if(bottomBuffer == NULL) - { - // Nothing to do if bottomBuffer is empty. - return topPixelBuffer; + itGlyphRun++; } - // Always combine two RGBA images - const unsigned int bufferSizeInt = bufferWidth * bufferHeight; - const unsigned int bufferSizeChar = 4u * bufferSizeInt; - - combinedPixelBuffer = Devel::PixelBuffer::New(bufferWidth, bufferHeight, Pixel::RGBA8888); - uint8_t* combinedBuffer = reinterpret_cast(combinedPixelBuffer.GetBuffer()); - memset(combinedBuffer, 0u, bufferSizeChar); + return topPixelBuffer; +} - for(unsigned int pixelIndex = 0; pixelIndex < bufferSizeInt; pixelIndex++) +Devel::PixelBuffer Typesetter::ApplyStrikethroughMarkupImageBuffer(Devel::PixelBuffer topPixelBuffer, const uint32_t& bufferWidth, const uint32_t& bufferHeight, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, const int32_t& horizontalOffset, const int32_t& verticalOffset) +{ + // strikethrough-tags (this is for Markup case) + // Get the strikethrough runs. + const Length numberOfStrikethroughRuns = mModel->GetNumberOfStrikethroughRuns(); + Vector strikethroughRuns; + strikethroughRuns.Resize(numberOfStrikethroughRuns); + mModel->GetStrikethroughRuns(strikethroughRuns.Begin(), 0u, numberOfStrikethroughRuns); + + // Iterate on the consecutive strikethrough glyph run and connect them into one chunk of strikethrough characters. + Vector::ConstIterator itGlyphRun = strikethroughRuns.Begin(); + Vector::ConstIterator endItGlyphRun = strikethroughRuns.End(); + GlyphIndex startGlyphIndex, endGlyphIndex; + + //The outer loop to iterate on the separated chunks of strikethrough glyph runs + while(itGlyphRun != endItGlyphRun) { - // If the alpha of the pixel in either buffer is not fully opaque, blend the two pixels. - // Otherwise, copy pixel from topBuffer to combinedBuffer. + startGlyphIndex = itGlyphRun->glyphRun.glyphIndex; + endGlyphIndex = startGlyphIndex + itGlyphRun->glyphRun.numberOfGlyphs - 1; - unsigned int alphaBuffer1 = topBuffer[pixelIndex * 4 + 3]; + // Create the image buffer for strikethrough + Devel::PixelBuffer strikethroughImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_STRIKETHROUGH, ignoreHorizontalAlignment, pixelFormat, horizontalOffset, verticalOffset, startGlyphIndex, endGlyphIndex); + // Combine the two buffers + // Result pixel buffer will be stored into topPixelBuffer. + CombineImageBuffer(strikethroughImageBuffer, topPixelBuffer, bufferWidth, bufferHeight, false); - if(alphaBuffer1 != 255) - { - // At least one pixel is not fully opaque - // "Over" blend the the pixel from topBuffer with the pixel in bottomBuffer - combinedBuffer[pixelIndex * 4] = topBuffer[pixelIndex * 4] + (bottomBuffer[pixelIndex * 4] * (255 - topBuffer[pixelIndex * 4 + 3]) / 255); - combinedBuffer[pixelIndex * 4 + 1] = topBuffer[pixelIndex * 4 + 1] + (bottomBuffer[pixelIndex * 4 + 1] * (255 - topBuffer[pixelIndex * 4 + 3]) / 255); - combinedBuffer[pixelIndex * 4 + 2] = topBuffer[pixelIndex * 4 + 2] + (bottomBuffer[pixelIndex * 4 + 2] * (255 - topBuffer[pixelIndex * 4 + 3]) / 255); - combinedBuffer[pixelIndex * 4 + 3] = topBuffer[pixelIndex * 4 + 3] + (bottomBuffer[pixelIndex * 4 + 3] * (255 - topBuffer[pixelIndex * 4 + 3]) / 255); - } - else - { - // Copy the pixel from topBuffer to combinedBuffer - combinedBuffer[pixelIndex * 4] = topBuffer[pixelIndex * 4]; - combinedBuffer[pixelIndex * 4 + 1] = topBuffer[pixelIndex * 4 + 1]; - combinedBuffer[pixelIndex * 4 + 2] = topBuffer[pixelIndex * 4 + 2]; - combinedBuffer[pixelIndex * 4 + 3] = topBuffer[pixelIndex * 4 + 3]; - } + itGlyphRun++; } - return combinedPixelBuffer; + return topPixelBuffer; } -Devel::PixelBuffer Typesetter::ApplyMarkupProcessorOnPixelBuffer(Devel::PixelBuffer topPixelBuffer, const unsigned int bufferWidth, const unsigned int bufferHeight, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, int horizontalOffset, int verticalOffset) +Devel::PixelBuffer Typesetter::ApplyMarkupProcessorOnPixelBuffer(Devel::PixelBuffer topPixelBuffer, const uint32_t& bufferWidth, const uint32_t& bufferHeight, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, const int32_t& horizontalOffset, const int32_t& verticalOffset) { // Apply the markup-Processor if enabled const bool markupProcessorEnabled = mModel->IsMarkupProcessorEnabled(); if(markupProcessorEnabled) { - // Underline-tags (this is for Markup case) - // Get the underline runs. - const Length numberOfUnderlineRuns = mModel->GetNumberOfUnderlineRuns(); - Vector underlineRuns; - underlineRuns.Resize(numberOfUnderlineRuns); - mModel->GetUnderlineRuns(underlineRuns.Begin(), 0u, numberOfUnderlineRuns); - - // Iterate on the consecutive underlined glyph run and connect them into one chunk of underlined characters. - Vector::ConstIterator itGlyphRun = underlineRuns.Begin(); - Vector::ConstIterator endItGlyphRun = underlineRuns.End(); - GlyphIndex startGlyphIndex, endGlyphIndex; - - //The outer loop to iterate on the separated chunks of underlined glyph runs - while(itGlyphRun != endItGlyphRun) - { - startGlyphIndex = itGlyphRun->glyphIndex; - endGlyphIndex = startGlyphIndex; - //The inner loop to make a connected underline for the consecutive characters - do - { - endGlyphIndex += itGlyphRun->numberOfGlyphs; - itGlyphRun++; - } while(itGlyphRun != endItGlyphRun && itGlyphRun->glyphIndex == endGlyphIndex); + topPixelBuffer = ApplyUnderlineMarkupImageBuffer(topPixelBuffer, bufferWidth, bufferHeight, ignoreHorizontalAlignment, pixelFormat, horizontalOffset, verticalOffset); - endGlyphIndex--; - - // Create the image buffer for underline - Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, horizontalOffset, verticalOffset, startGlyphIndex, endGlyphIndex); - // Combine the two buffers - topPixelBuffer = CombineImageBuffer(topPixelBuffer, underlineImageBuffer, bufferWidth, bufferHeight); - } + topPixelBuffer = ApplyStrikethroughMarkupImageBuffer(topPixelBuffer, bufferWidth, bufferHeight, ignoreHorizontalAlignment, pixelFormat, horizontalOffset, verticalOffset); } return topPixelBuffer;