/*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 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.
#include <dali-toolkit/internal/text/rendering/text-typesetter.h>
// EXTERNAL INCLUDES
+#include <cmath>
#include <dali/devel-api/text-abstraction/font-client.h>
+#include <dali/integration-api/debug.h>
+#include <dali/integration-api/trace.h>
#include <dali/public-api/common/constants.h>
+#include <dali/public-api/math/math-utils.h>
#include <memory.h>
// INTERNAL INCLUDES
#include <dali-toolkit/devel-api/controls/text-controls/text-label-devel.h>
#include <dali-toolkit/internal/text/glyph-metrics-helper.h>
+#include <dali-toolkit/internal/text/line-helper-functions.h>
#include <dali-toolkit/internal/text/rendering/styles/character-spacing-helper-functions.h>
#include <dali-toolkit/internal/text/rendering/styles/strikethrough-helper-functions.h>
#include <dali-toolkit/internal/text/rendering/styles/underline-helper-functions.h>
{
namespace
{
+DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_TEXT_PERFORMANCE_MARKER, false);
+
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<const uint32_t>(x) * y;
+ return ((xy << 15) + (xy << 7) + xy) >> 23;
+}
+
+/**
+ * @brief Fast multiply & Summation & divide by 255.
+ *
+ * @param x1 The value between [0..255]
+ * @param y1 The value between [0..255]
+ * @param x2 The value between [0..255]
+ * @param y2 The value between [0..255]
+ * @return min(255, (x1*y1)/255 + (x2*y2)/255)
+ */
+inline uint8_t MultiplyAndSummationAndNormalizeColor(const uint8_t x1, const uint8_t y1, const uint8_t x2, const uint8_t y2) noexcept
+{
+ const uint32_t xy1 = static_cast<const uint32_t>(x1) * y1;
+ const uint32_t xy2 = static_cast<const uint32_t>(x2) * y2;
+ const uint32_t res = std::min(65025u, xy1 + xy2); // 65025 is 255 * 255.
+ return ((res + ((res + 257) >> 8)) >> 8); // fast divide by 255.
+}
+
+/// Helper macro define for glyph typesetter. It will reduce some duplicated code line.
+// clang-format off
+/**
+ * @brief Prepare decode glyph bitmap data. It must be call END_GLYPH_BITMAP end of same scope.
+ */
+#define BEGIN_GLYPH_BITMAP(data) \
+{ \
+ uint32_t glyphOffet = 0u; \
+ const bool useLocalScanline = data.glyphBitmap.compressionType != TextAbstraction::GlyphBufferData::CompressionType::NO_COMPRESSION; \
+ uint8_t* __restrict__ glyphScanline = useLocalScanline ? (uint8_t*)malloc(data.glyphBitmap.width * glyphPixelSize) : data.glyphBitmap.buffer; \
+ DALI_ASSERT_ALWAYS(glyphScanline && "Glyph scanline for buffer is null!");
+
+/**
+ * @brief Macro to skip useless line fast.
+ */
+#define SKIP_GLYPH_SCANLINE(skipLine) \
+if(useLocalScanline) \
+{ \
+ for(int32_t lineIndex = 0; lineIndex < skipLine; ++lineIndex) \
+ { \
+ TextAbstraction::GlyphBufferData::DecompressScanline(data.glyphBitmap, glyphScanline, glyphOffet); \
+ } \
+} \
+else \
+{ \
+ glyphScanline += skipLine * static_cast<int32_t>(data.glyphBitmap.width * glyphPixelSize); \
+}
+
+/**
+ * @brief Prepare scanline of glyph bitmap data per each lines. It must be call END_GLYPH_SCANLINE_DECODE end of same scope.
+ */
+#define BEGIN_GLYPH_SCANLINE_DECODE(data) \
+{ \
+ if(useLocalScanline) \
+ { \
+ TextAbstraction::GlyphBufferData::DecompressScanline(data.glyphBitmap, glyphScanline, glyphOffet); \
+ }
+
+/**
+ * @brief Finalize scanline of glyph bitmap data per each lines.
+ */
+#define END_GLYPH_SCANLINE_DECODE(data) \
+ if(!useLocalScanline) \
+ { \
+ glyphScanline += data.glyphBitmap.width * glyphPixelSize; \
+ } \
+} // For ensure that we call BEGIN_GLYPH_SCANLINE_DECODE before
+
+/**
+ * @brief Finalize decode glyph bitmap data.
+ */
+#define END_GLYPH_BITMAP() \
+ if(useLocalScanline) \
+ { \
+ free(glyphScanline); \
+ } \
+} // For ensure that we call BEGIN_GLYPH_BITMAP before
+
+// clang-format on
+/// Helper macro define end.
+
/**
* @brief Data struct used to set the buffer of the glyph's bitmap into the final bitmap's buffer.
*/
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.
+ Devel::PixelBuffer bitmapBuffer; ///< The buffer of the whole bitmap. The format is RGBA8888.
+ Vector2* position; ///< The position of the glyph.
+ TextAbstraction::GlyphBufferData glyphBitmap; ///< The glyph's bitmap.
+ 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.
* @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).
*/
-void TypesetGlyph(GlyphData& data,
- const Vector2* const position,
- const Vector4* const color,
- Typesetter::Style style,
- Pixel::Format pixelFormat)
+void TypesetGlyph(GlyphData& __restrict__ data,
+ const Vector2* const __restrict__ position,
+ const Vector4* const __restrict__ color,
+ const Typesetter::Style style,
+ const Pixel::Format pixelFormat)
{
if((0u == data.glyphBitmap.width) || (0u == data.glyphBitmap.height))
{
return;
}
- const int widthMinusOne = static_cast<int>(data.width - 1u);
- const int heightMinusOne = static_cast<int>(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 glyphAlphaIndex = (glyphPixelSize > 0u) ? glyphPixelSize - 1u : 0u;
+
+ // Determinate iterator range.
+ const int32_t lineIndexRangeMin = std::max(0, -yOffset);
+ const int32_t lineIndexRangeMax = std::min(static_cast<int32_t>(data.glyphBitmap.height), static_cast<int32_t>(data.height) - yOffset);
+ const int32_t indexRangeMin = std::max(0, -xOffset);
+ const int32_t indexRangeMax = std::min(static_cast<int32_t>(data.glyphBitmap.width), static_cast<int32_t>(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;
+ uint32_t* __restrict__ bitmapBuffer = reinterpret_cast<uint32_t*>(data.bitmapBuffer.GetBuffer());
+ // Skip basic line.
+ bitmapBuffer += (lineIndexRangeMin + yOffset) * static_cast<int32_t>(data.width);
- // Pointer to the color glyph if there is one.
- const uint32_t* const colorGlyphBuffer = isColorGlyph ? reinterpret_cast<uint32_t*>(data.glyphBitmap.buffer) : NULL;
+ // 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)
+ {
+ // We can use memset here.
+ memset(bitmapBuffer + xOffset + indexRangeMin, 0, (indexRangeMax - indexRangeMin) * sizeof(uint32_t));
+ bitmapBuffer += data.width;
+ }
+ return;
+ }
+
+ const bool swapChannelsBR = Pixel::BGRA8888 == data.glyphBitmap.format;
+
+ // Precalculate input color's packed result.
+ uint32_t packedInputColor = 0u;
+ uint8_t* __restrict__ packedInputColorBuffer = reinterpret_cast<uint8_t*>(&packedInputColor);
+
+ *(packedInputColorBuffer + 3u) = static_cast<uint8_t>(color->a * 255);
+ *(packedInputColorBuffer + 2u) = static_cast<uint8_t>(color->b * 255);
+ *(packedInputColorBuffer + 1u) = static_cast<uint8_t>(color->g * 255);
+ *(packedInputColorBuffer) = static_cast<uint8_t>(color->r * 255);
- // Initial vertical offset.
- const int yOffset = data.verticalOffset + position->y;
+ // Prepare glyph bitmap
+ BEGIN_GLYPH_BITMAP(data);
- uint32_t* bitmapBuffer = reinterpret_cast<uint32_t*>(data.bitmapBuffer.GetBuffer());
+ // Skip basic line of glyph.
+ SKIP_GLYPH_SCANLINE(lineIndexRangeMin);
// Traverse the pixels of the glyph line per line.
- for(int lineIndex = 0, glyphHeight = static_cast<int>(data.glyphBitmap.height); lineIndex < glyphHeight; ++lineIndex)
+ if(isColorGlyph)
{
- const int yOffsetIndex = yOffset + lineIndex;
- if((0 > yOffsetIndex) || (yOffsetIndex > heightMinusOne))
+ for(int32_t lineIndex = lineIndexRangeMin; lineIndex < lineIndexRangeMax; ++lineIndex)
{
- // Do not write out of bounds.
- continue;
- }
+ BEGIN_GLYPH_SCANLINE_DECODE(data);
- const int verticalOffset = yOffsetIndex * data.width;
- const int xOffset = data.horizontalOffset + position->x;
- const int glyphBufferOffset = lineIndex * static_cast<int>(data.glyphBitmap.width);
- for(int index = 0, glyphWidth = static_cast<int>(data.glyphBitmap.width); index < glyphWidth; ++index)
- {
- const int xOffsetIndex = xOffset + index;
- if((0 > xOffsetIndex) || (xOffsetIndex > widthMinusOne))
+ for(int32_t index = indexRangeMin; index < indexRangeMax; ++index)
{
- // Don't write out of bounds.
- continue;
- }
+ const int32_t xOffsetIndex = xOffset + index;
- if(isColorGlyph)
- {
// Retrieves the color from the color glyph.
- uint32_t packedColorGlyph = *(colorGlyphBuffer + glyphBufferOffset + index);
- uint8_t* packedColorGlyphBuffer = reinterpret_cast<uint8_t*>(&packedColorGlyph);
+ uint32_t packedColorGlyph = *(reinterpret_cast<const uint32_t*>(glyphScanline + (index << 2)));
+ uint8_t* __restrict__ packedColorGlyphBuffer = reinterpret_cast<uint8_t*>(&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<uint8_t>(color->a * static_cast<float>(*(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<uint8_t>(color->b * colorAlpha);
- *(packedColorGlyphBuffer + 1u) = static_cast<uint8_t>(color->g * colorAlpha);
- *packedColorGlyphBuffer = static_cast<uint8_t>(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<uint8_t>(*(packedColorGlyphBuffer + 2u) * color->b);
- *(packedColorGlyphBuffer + 1u) = static_cast<uint8_t>(*(packedColorGlyphBuffer + 1u) * color->g);
- *packedColorGlyphBuffer = static_cast<uint8_t>(*packedColorGlyphBuffer * color->r);
- }
+ *(packedColorGlyphBuffer + 2u) = MultiplyAndNormalizeColor(*(packedInputColorBuffer + 2u), *(packedColorGlyphBuffer + 2u));
+ *(packedColorGlyphBuffer + 1u) = MultiplyAndNormalizeColor(*(packedInputColorBuffer + 1u), *(packedColorGlyphBuffer + 1u));
+ *packedColorGlyphBuffer = MultiplyAndNormalizeColor(*packedInputColorBuffer, *packedColorGlyphBuffer);
}
}
// Set the color into the final pixel buffer.
- *(bitmapBuffer + verticalOffset + xOffsetIndex) = packedColorGlyph;
+ *(bitmapBuffer + xOffsetIndex) = packedColorGlyph;
}
- else
- {
- // Pack the given color into a 32bit buffer. The alpha channel will be updated later for each pixel.
- // The format is RGBA8888.
- uint32_t packedColor = 0u;
- uint8_t* packedColorBuffer = reinterpret_cast<uint8_t*>(&packedColor);
+ bitmapBuffer += data.width;
+
+ END_GLYPH_SCANLINE_DECODE(data);
+ }
+ }
+ else
+ {
+ for(int32_t lineIndex = lineIndexRangeMin; lineIndex < lineIndexRangeMax; ++lineIndex)
+ {
+ BEGIN_GLYPH_SCANLINE_DECODE(data);
+
+ for(int32_t index = indexRangeMin; index < indexRangeMax; ++index)
+ {
// Update the alpha channel.
- const uint8_t alpha = *(data.glyphBitmap.buffer + glyphPixelSize * (glyphBufferOffset + index) + alphaIndex);
+ const uint8_t alpha = *(glyphScanline + index * glyphPixelSize + glyphAlphaIndex);
// Copy non-transparent pixels only
if(alpha > 0u)
{
+ const int32_t xOffsetIndex = xOffset + index;
+
// Check alpha of overlapped pixels
- uint32_t& currentColor = *(bitmapBuffer + verticalOffset + xOffsetIndex);
+ uint32_t& currentColor = *(bitmapBuffer + xOffsetIndex);
uint8_t* packedCurrentColorBuffer = reinterpret_cast<uint8_t*>(¤tColor);
// For any pixel overlapped with the pixel in previous glyphs, make sure we don't
// 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<uint8_t>(color->a * currentAlpha);
- *(packedColorBuffer + 2u) = static_cast<uint8_t>(color->b * currentAlpha);
- *(packedColorBuffer + 1u) = static_cast<uint8_t>(color->g * currentAlpha);
- *(packedColorBuffer) = static_cast<uint8_t>(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
+ {
+ // Pack the given color into a 32bit buffer. The alpha channel will be updated later for each pixel.
+ // The format is RGBA8888.
+ uint32_t packedColor = 0u;
+ uint8_t* __restrict__ packedColorBuffer = reinterpret_cast<uint8_t*>(&packedColor);
+
+ // 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;
+ }
}
}
+
+ bitmapBuffer += data.width;
+
+ END_GLYPH_SCANLINE_DECODE(data);
}
}
+
+ END_GLYPH_BITMAP();
}
- else
+ else // Pixel::L8
{
- // 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;
+ // Below codes required only if not color glyph.
+ if(!isColorGlyph)
+ {
+ uint8_t* __restrict__ bitmapBuffer = data.bitmapBuffer.GetBuffer();
+ // Skip basic line.
+ bitmapBuffer += (lineIndexRangeMin + yOffset) * static_cast<int32_t>(data.width);
- // Initial vertical offset.
- const int yOffset = data.verticalOffset + position->y;
+ // Prepare glyph bitmap
+ BEGIN_GLYPH_BITMAP(data);
- uint8_t* bitmapBuffer = reinterpret_cast<uint8_t*>(data.bitmapBuffer.GetBuffer());
+ // Skip basic line of glyph.
+ SKIP_GLYPH_SCANLINE(lineIndexRangeMin);
- // Traverse the pixels of the glyph line per line.
- for(int lineIndex = 0, glyphHeight = static_cast<int>(data.glyphBitmap.height); lineIndex < glyphHeight; ++lineIndex)
- {
- const int yOffsetIndex = yOffset + lineIndex;
- if((0 > yOffsetIndex) || (yOffsetIndex > heightMinusOne))
+ // Traverse the pixels of the glyph line per line.
+ for(int32_t lineIndex = lineIndexRangeMin; lineIndex < lineIndexRangeMax; ++lineIndex)
{
- // Do not write out of bounds.
- continue;
- }
+ BEGIN_GLYPH_SCANLINE_DECODE(data);
- const int verticalOffset = yOffsetIndex * data.width;
- const int xOffset = data.horizontalOffset + position->x;
- const int glyphBufferOffset = lineIndex * static_cast<int>(data.glyphBitmap.width);
- for(int index = 0, glyphWidth = static_cast<int>(data.glyphBitmap.width); index < glyphWidth; ++index)
- {
- const int xOffsetIndex = xOffset + index;
- if((0 > xOffsetIndex) || (xOffsetIndex > widthMinusOne))
+ for(int32_t index = indexRangeMin; index < indexRangeMax; ++index)
{
- // Don't write out of bounds.
- continue;
- }
+ const int32_t xOffsetIndex = xOffset + index;
- if(!isColorGlyph)
- {
// Update the alpha channel.
- const uint8_t alpha = *(data.glyphBitmap.buffer + glyphPixelSize * (glyphBufferOffset + index) + alphaIndex);
+ const uint8_t alpha = *(glyphScanline + index * glyphPixelSize + glyphAlphaIndex);
// Copy non-transparent pixels only
if(alpha > 0u)
{
// Check alpha of overlapped pixels
- uint8_t& currentAlpha = *(bitmapBuffer + verticalOffset + xOffsetIndex);
+ uint8_t& currentAlpha = *(bitmapBuffer + xOffsetIndex);
// For any pixel overlapped with the pixel in previous glyphs, make sure we don't
// overwrite a previous bigger alpha with a smaller alpha (in order to avoid
currentAlpha = std::max(currentAlpha, alpha);
}
}
- }
- }
- }
-}
-/// 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<uint8_t*>(&pixel);
+ bitmapBuffer += data.width;
- // Write the color to the pixel buffer
- uint8_t colorAlpha = static_cast<uint8_t>(color.a * 255.f);
- *(pixelBuffer + 3u) = colorAlpha;
- *(pixelBuffer + 2u) = static_cast<uint8_t>(color.b * colorAlpha);
- *(pixelBuffer + 1u) = static_cast<uint8_t>(color.g * colorAlpha);
- *(pixelBuffer) = static_cast<uint8_t>(color.r * colorAlpha);
+ END_GLYPH_SCANLINE_DECODE(data);
+ }
- *(bitmapBuffer + y * glyphData.width + x) = pixel;
+ END_GLYPH_BITMAP();
+ }
+ }
}
/// Draws the specified underline color to the buffer
void DrawUnderline(
- const unsigned int bufferWidth,
- const unsigned int bufferHeight,
+ const uint32_t bufferWidth,
+ const uint32_t bufferHeight,
GlyphData& glyphData,
const float baseline,
const float currentUnderlinePosition,
const float dashedUnderlineWidth = currentUnderlineProperties.dashWidthDefined ? currentUnderlineProperties.dashWidth : commonUnderlineProperties.dashWidth;
const float dashedUnderlineGap = currentUnderlineProperties.dashGapDefined ? currentUnderlineProperties.dashGap : commonUnderlineProperties.dashGap;
- int underlineYOffset = glyphData.verticalOffset + baseline + currentUnderlinePosition;
- uint32_t* bitmapBuffer = reinterpret_cast<uint32_t*>(glyphData.bitmapBuffer.GetBuffer());
+ int32_t underlineYOffset = glyphData.verticalOffset + baseline + currentUnderlinePosition;
+
+ const uint32_t yRangeMin = underlineYOffset;
+ const uint32_t yRangeMax = std::min(bufferHeight, underlineYOffset + static_cast<uint32_t>(maxUnderlineHeight));
+ const uint32_t xRangeMin = static_cast<uint32_t>(glyphData.horizontalOffset + lineExtentLeft);
+ const uint32_t xRangeMax = std::min(bufferWidth, static_cast<uint32_t>(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((underlineType != Text::Underline::DOUBLE && yRangeMax <= yRangeMin) || xRangeMax <= xRangeMin)
+ {
+ return;
+ }
+
+ // We can optimize by memset when underlineColor.a is near zero
+ uint8_t underlineColorAlpha = static_cast<uint8_t>(underlineColor.a * 255.f);
+
+ uint32_t* bitmapBuffer = reinterpret_cast<uint32_t*>(glyphData.bitmapBuffer.GetBuffer());
+
+ // Skip yRangeMin line.
+ bitmapBuffer += yRangeMin * glyphData.width;
- for(unsigned int y = underlineYOffset; y < underlineYOffset + maxUnderlineHeight; y++)
+ // Note if underlineType is DASHED, we cannot setup color by memset.
+ if(underlineType != Text::Underline::DASHED && underlineColorAlpha == 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;
}
- if(underlineType == Text::Underline::DASHED)
+ if(underlineType == Text::Underline::DOUBLE)
{
- float dashWidth = dashedUnderlineWidth;
- float dashGap = 0;
+ int32_t secondUnderlineYOffset = underlineYOffset - ONE_AND_A_HALF * maxUnderlineHeight;
+ const uint32_t secondYRangeMin = static_cast<uint32_t>(std::max(0, secondUnderlineYOffset));
+ const uint32_t secondYRangeMax = static_cast<uint32_t>(std::max(0, std::min(static_cast<int32_t>(bufferHeight), secondUnderlineYOffset + static_cast<int32_t>(maxUnderlineHeight))));
+
+ // Rewind bitmapBuffer pointer, and skip secondYRangeMin line.
+ bitmapBuffer = reinterpret_cast<uint32_t*>(glyphData.bitmapBuffer.GetBuffer()) + yRangeMin * glyphData.width;
- for(unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++)
+ for(uint32_t y = secondYRangeMin; y < secondYRangeMax; y++)
{
- if(x > bufferWidth - 1)
- {
- // Do not write out of bounds.
- break;
- }
- if(dashGap == 0 && dashWidth > 0)
- {
- WriteColorToPixelBuffer(glyphData, bitmapBuffer, underlineColor, x, y);
- dashWidth--;
- }
- else if(dashGap < dashedUnderlineGap)
- {
- dashGap++;
- }
- else
- {
- //reset
- dashWidth = dashedUnderlineWidth;
- dashGap = 0;
- }
+ // We can use memset.
+ memset(bitmapBuffer + xRangeMin, 0, (xRangeMax - xRangeMin) * sizeof(uint32_t));
+ bitmapBuffer += glyphData.width;
}
}
- else
+ }
+ else
+ {
+ uint32_t packedUnderlineColor = 0u;
+ uint8_t* packedUnderlineColorBuffer = reinterpret_cast<uint8_t*>(&packedUnderlineColor);
+
+ // Write the color to the pixel buffer
+ *(packedUnderlineColorBuffer + 3u) = underlineColorAlpha;
+ *(packedUnderlineColorBuffer + 2u) = static_cast<uint8_t>(underlineColor.b * underlineColorAlpha);
+ *(packedUnderlineColorBuffer + 1u) = static_cast<uint8_t>(underlineColor.g * underlineColorAlpha);
+ *(packedUnderlineColorBuffer) = static_cast<uint8_t>(underlineColor.r * underlineColorAlpha);
+
+ for(uint32_t y = yRangeMin; y < yRangeMax; y++)
{
- for(unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++)
+ if(underlineType == Text::Underline::DASHED)
{
- if(x > bufferWidth - 1)
+ float dashWidth = dashedUnderlineWidth;
+ float dashGap = 0;
+
+ for(uint32_t x = xRangeMin; x < xRangeMax; x++)
{
- // Do not write out of bounds.
- break;
+ if(Dali::EqualsZero(dashGap) && 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;
+ }
}
- WriteColorToPixelBuffer(glyphData, bitmapBuffer, underlineColor, x, y);
}
- }
- }
- if(underlineType == Text::Underline::DOUBLE)
- {
- int secondUnderlineYOffset = underlineYOffset - ONE_AND_A_HALF * maxUnderlineHeight;
- for(unsigned int y = secondUnderlineYOffset; y < secondUnderlineYOffset + maxUnderlineHeight; y++)
- {
- if(y > bufferHeight - 1)
+ 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;
+ }
}
- for(unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++)
+ bitmapBuffer += glyphData.width;
+ }
+ if(underlineType == Text::Underline::DOUBLE)
+ {
+ int32_t secondUnderlineYOffset = underlineYOffset - ONE_AND_A_HALF * maxUnderlineHeight;
+ const uint32_t secondYRangeMin = static_cast<uint32_t>(std::max(0, secondUnderlineYOffset));
+ const uint32_t secondYRangeMax = static_cast<uint32_t>(std::max(0, std::min(static_cast<int32_t>(bufferHeight), secondUnderlineYOffset + static_cast<int32_t>(maxUnderlineHeight))));
+
+ // Rewind bitmapBuffer pointer, and skip secondYRangeMin line.
+ bitmapBuffer = reinterpret_cast<uint32_t*>(glyphData.bitmapBuffer.GetBuffer()) + yRangeMin * glyphData.width;
+
+ for(uint32_t y = secondYRangeMin; y < secondYRangeMax; 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] = underlineColor;
+ *(bitmapBuffer + x) = packedUnderlineColor;
}
- WriteColorToPixelBuffer(glyphData, bitmapBuffer, underlineColor, x, y);
+ 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<int32_t>(glyphData.verticalOffset + baseline - line.ascender));
+ const int32_t yRangeMax = std::min(static_cast<int32_t>(bufferHeight), static_cast<int32_t>(glyphData.verticalOffset + baseline - line.descender));
+ const int32_t xRangeMin = std::max(0, static_cast<int32_t>(glyphData.horizontalOffset + lineExtentLeft));
+ const int32_t xRangeMax = std::min(static_cast<int32_t>(bufferWidth), static_cast<int32_t>(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<uint8_t>(backgroundColor.a * 255.f);
+
uint32_t* bitmapBuffer = reinterpret_cast<uint32_t*>(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<int>(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<uint8_t*>(&packedBackgroundColor);
+
+ // Write the color to the pixel buffer
+ *(packedBackgroundColorBuffer + 3u) = backgroundColorAlpha;
+ *(packedBackgroundColorBuffer + 2u) = static_cast<uint8_t>(backgroundColor.b * backgroundColorAlpha);
+ *(packedBackgroundColorBuffer + 1u) = static_cast<uint8_t>(backgroundColor.g * backgroundColorAlpha);
+ *(packedBackgroundColorBuffer) = static_cast<uint8_t>(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<int>(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, const bool ignoreHorizontalAlignment, const int32_t horizontalOffset, const int32_t verticalOffset)
{
// Retrieve lines, glyphs, positions and colors from the view model.
const Length modelNumberOfLines = model->GetNumberOfLines();
const Vector2* const positionBuffer = model->GetLayout();
const Vector4* const backgroundColorsBuffer = model->GetBackgroundColors();
const ColorIndex* const backgroundColorIndicesBuffer = model->GetBackgroundColorIndices();
+ const bool removeFrontInset = model->IsRemoveFrontInset();
+ const bool removeBackInset = model->IsRemoveBackInset();
+
+ const DevelText::VerticalLineAlignment::Type verLineAlign = model->GetVerticalLineAlignment();
// Create and initialize the pixel buffer.
GlyphData glyphData;
const LineRun& line = *(modelLinesBuffer + lineIndex);
// Sets the horizontal offset of the line.
- glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast<int>(line.alignmentOffset);
+ glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast<int32_t>(line.alignmentOffset);
glyphData.horizontalOffset += horizontalOffset;
// Increases the vertical offset with the line's ascender.
- glyphData.verticalOffset += static_cast<int>(line.ascender);
-
- // Include line spacing after first line
- if(lineIndex > 0u)
- {
- glyphData.verticalOffset += static_cast<int>(line.lineSpacing);
- }
+ glyphData.verticalOffset += static_cast<int32_t>(line.ascender + GetPreOffsetVerticalLineAlignment(line, verLineAlign));
float left = bufferWidth;
float right = 0.0f;
}
// Calculate the positions of leftmost and rightmost glyphs in the current line
- if((position->x < left) || (backgroundColorIndex != prevBackgroundColorIndex))
+ if(removeFrontInset)
{
- left = position->x - glyphInfo->xBearing;
+ if((position->x < left) || (backgroundColorIndex != prevBackgroundColorIndex))
+ {
+ left = position->x;
+ }
+ }
+ else
+ {
+ const float originPositionLeft = position->x - glyphInfo->xBearing;
+ if((originPositionLeft < left) || (backgroundColorIndex != prevBackgroundColorIndex))
+ {
+ left = originPositionLeft;
+ }
}
- if(position->x + glyphInfo->width > right)
+ if(removeBackInset)
+ {
+ if(position->x + glyphInfo->width > right)
+ {
+ right = position->x + glyphInfo->width;
+ }
+ }
+ else
{
- right = position->x - glyphInfo->xBearing + glyphInfo->advance;
+ const float originPositionRight = position->x - glyphInfo->xBearing + glyphInfo->advance;
+ if(originPositionRight > right)
+ {
+ right = originPositionRight;
+ }
}
prevBackgroundColorIndex = backgroundColorIndex;
}
// Increases the vertical offset with the line's descender.
- glyphData.verticalOffset += static_cast<int>(-line.descender);
+ glyphData.verticalOffset += static_cast<int32_t>(-line.descender + GetPostOffsetVerticalLineAlignment(line, verLineAlign));
}
return glyphData.bitmapBuffer;
}
/// Draws the specified strikethrough color to the buffer
-void DrawStrikethrough(const unsigned int bufferWidth,
- const unsigned int bufferHeight,
+void DrawStrikethrough(const uint32_t bufferWidth,
+ const uint32_t bufferHeight,
GlyphData& glyphData,
const float baseline,
const float strikethroughStartingYPosition,
{
const Vector4& strikethroughColor = currentStrikethroughProperties.colorDefined ? currentStrikethroughProperties.color : commonStrikethroughProperties.color;
+ const uint32_t yRangeMin = static_cast<uint32_t>(strikethroughStartingYPosition);
+ const uint32_t yRangeMax = std::min(bufferHeight, static_cast<uint32_t>(strikethroughStartingYPosition + maxStrikethroughHeight));
+ const uint32_t xRangeMin = static_cast<uint32_t>(glyphData.horizontalOffset + lineExtentLeft);
+ const uint32_t xRangeMax = std::min(bufferWidth, static_cast<uint32_t>(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<uint8_t>(strikethroughColor.a * 255.f);
+
uint32_t* bitmapBuffer = reinterpret_cast<uint32_t*>(glyphData.bitmapBuffer.GetBuffer());
- for(unsigned int y = strikethroughStartingYPosition; y < strikethroughStartingYPosition + maxStrikethroughHeight; 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<uint8_t*>(&packedStrikethroughColor);
- for(unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++)
+ // Write the color to the pixel buffer
+ *(packedStrikethroughColorBuffer + 3u) = strikethroughColorAlpha;
+ *(packedStrikethroughColorBuffer + 2u) = static_cast<uint8_t>(strikethroughColor.b * strikethroughColorAlpha);
+ *(packedStrikethroughColorBuffer + 1u) = static_cast<uint8_t>(strikethroughColor.g * strikethroughColorAlpha);
+ *(packedStrikethroughColorBuffer) = static_cast<uint8_t>(strikethroughColor.r * strikethroughColorAlpha);
+
+ 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;
}
+ bitmapBuffer += glyphData.width;
+ }
+ }
+}
+
+/**
+ * @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 uint32_t bufferSizeInt = bufferWidth * bufferHeight;
+ const size_t bufferSizeChar = sizeof(uint32_t) * static_cast<std::size_t>(bufferSizeInt);
+ memset(imageBuffer.GetBuffer(), 0, bufferSizeChar);
+ }
+ else
+ {
+ memset(imageBuffer.GetBuffer(), 0, static_cast<std::size_t>(bufferWidth * bufferHeight));
+ }
+
+ return imageBuffer;
+}
- WriteColorToPixelBuffer(glyphData, bitmapBuffer, strikethroughColor, x, y);
+/**
+ * @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& __restrict__ topPixelBuffer, Devel::PixelBuffer& __restrict__ 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<uint32_t*>(topPixelBuffer.GetBuffer());
+ uint32_t* bottomBuffer = reinterpret_cast<uint32_t*>(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* __restrict__ combinedBuffer = storeResultIntoTop ? topBuffer : bottomBuffer;
+ uint8_t* __restrict__ topAlphaBufferPointer = reinterpret_cast<uint8_t*>(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* __restrict__ blendedBottomBufferColorBuffer = reinterpret_cast<uint8_t*>(&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);
}
}
return mModel;
}
-Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth, const unsigned int bufferHeight, Pixel::Format pixelFormat)
+void Typesetter::SetFontClient(TextAbstraction::FontClient& fontClient)
{
- Devel::PixelBuffer imageBuffer = Devel::PixelBuffer::New(bufferWidth, bufferHeight, pixelFormat);
+ mFontClient = fontClient;
+}
- if(Pixel::RGBA8888 == pixelFormat)
- {
- const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
- const unsigned int bufferSizeChar = 4u * bufferSizeInt;
- memset(imageBuffer.GetBuffer(), 0u, bufferSizeChar);
- }
- else
- {
- memset(imageBuffer.GetBuffer(), 0, bufferWidth * bufferHeight);
- }
+PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirection::Type textDirection, RenderBehaviour behaviour, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat)
+{
+ Devel::PixelBuffer result = RenderWithPixelBuffer(size, textDirection, behaviour, ignoreHorizontalAlignment, pixelFormat);
+ PixelData pixelData = Devel::PixelBuffer::Convert(result);
- return imageBuffer;
+ return pixelData;
}
-PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirection::Type textDirection, RenderBehaviour behaviour, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat)
+PixelData Typesetter::RenderWithCutout(const Vector2& size, Toolkit::DevelText::TextDirection::Type textDirection, Devel::PixelBuffer mask, RenderBehaviour behaviour, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, float originAlpha)
{
+ Devel::PixelBuffer result = RenderWithPixelBuffer(size, textDirection, behaviour, ignoreHorizontalAlignment, pixelFormat);
+ SetMaskForImageBuffer(mask, result, size.width, size.height, originAlpha);
+
+ PixelData pixelData = Devel::PixelBuffer::Convert(result);
+
+ return pixelData;
+}
+
+Devel::PixelBuffer Typesetter::RenderWithPixelBuffer(const Vector2& size, Toolkit::DevelText::TextDirection::Type textDirection, RenderBehaviour behaviour, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat)
+{
+ DALI_TRACE_SCOPE(gTraceFilter, "DALI_TEXT_RENDERING_TYPESETTER");
// @todo. This initial implementation for a TextLabel has only one visible page.
// Elides the text if needed.
- mModel->ElideGlyphs();
+ mModel->ElideGlyphs(mFontClient);
// Retrieves the layout size.
const Size& layoutSize = mModel->GetLayoutSize();
-
- const int outlineWidth = static_cast<int>(mModel->GetOutlineWidth());
+ const int32_t outlineWidth = static_cast<int32_t>(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())
{
case HorizontalAlignment::BEGIN:
}
// Set the offset for the vertical alignment.
- int penY = 0u;
-
+ int32_t penY = 0u;
switch(mModel->GetVerticalAlignment())
{
case VerticalAlignment::TOP:
}
case VerticalAlignment::CENTER:
{
- penY = static_cast<int>(0.5f * (size.height - layoutSize.height));
+ penY = static_cast<int32_t>(std::round(0.5f * (size.height - layoutSize.height)));
penY = penY < 0.f ? 0.f : penY;
+
break;
}
case VerticalAlignment::BOTTOM:
{
- penY = static_cast<int>(size.height - layoutSize.height);
+ penY = static_cast<int32_t>(size.height - layoutSize.height);
break;
}
}
- // Calculate vertical line alignment
- switch(mModel->GetVerticalLineAlignment())
+ const bool isCutoutEnabled = mModel->IsCutoutEnabled();
+ if(isCutoutEnabled)
{
- case DevelText::VerticalLineAlignment::TOP:
- {
- break;
- }
- case DevelText::VerticalLineAlignment::MIDDLE:
- {
- const auto& line = *mModel->GetLines();
- penY -= line.descender;
- penY += static_cast<int>(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<int>(lineHeight - (line.ascender - line.descender));
- break;
- }
+ Vector2 offset = mModel->GetOffsetWithCutout();
+ penX = offset.x;
+ penY = offset.y;
}
// Generate the image buffers of the text for each different style first,
// 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<unsigned int>(size.width);
- const unsigned int bufferHeight = static_cast<unsigned int>(size.height);
+ const uint32_t bufferWidth = static_cast<uint32_t>(size.width);
+ const uint32_t bufferHeight = static_cast<uint32_t>(size.height);
- const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
- const unsigned int bufferSizeChar = 4u * bufferSizeInt;
+ const uint32_t bufferSizeInt = bufferWidth * bufferHeight;
+ const size_t bufferSizeChar = sizeof(uint32_t) * static_cast<std::size_t>(bufferSizeInt);
//Elided text in ellipsis at START could start on index greater than 0
auto startIndexOfGlyphs = mModel->GetStartIndexOfElidedGlyphs();
{
// Generate the outline if enabled
const uint16_t outlineWidth = mModel->GetOutlineWidth();
- if(outlineWidth != 0u && RENDER_OVERLAY_STYLE != behaviour)
+ const float outlineAlpha = mModel->GetOutlineColor().a;
+ if(outlineWidth != 0u && fabsf(outlineAlpha) > Math::MACHINE_EPSILON_1 && RENDER_OVERLAY_STYLE != behaviour)
{
// Create the image buffer for outline
Devel::PixelBuffer outlineImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_OUTLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, startIndexOfGlyphs, endIndexOfGlyphs);
+ const float& blurRadius = mModel->GetOutlineBlurRadius();
+
+ if(blurRadius > Math::MACHINE_EPSILON_1)
+ {
+ outlineImageBuffer.ApplyGaussianBlur(blurRadius);
+ }
+
// 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.
+ // @todo. Support shadow for partial text later on.
// Generate the shadow if enabled
const Vector2& shadowOffset = mModel->GetShadowOffset();
- if(RENDER_OVERLAY_STYLE != behaviour && (fabsf(shadowOffset.x) > Math::MACHINE_EPSILON_1 || fabsf(shadowOffset.y) > Math::MACHINE_EPSILON_1))
+ const float shadowAlpha = mModel->GetShadowColor().a;
+ if(RENDER_OVERLAY_STYLE != behaviour && fabsf(shadowAlpha) > Math::MACHINE_EPSILON_1 && (fabsf(shadowOffset.x) > Math::MACHINE_EPSILON_1 || fabsf(shadowOffset.y) > Math::MACHINE_EPSILON_1))
{
// Create the image buffer for shadow
Devel::PixelBuffer shadowImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_SHADOW, ignoreHorizontalAlignment, pixelFormat, penX, penY, startIndexOfGlyphs, endIndexOfGlyphs);
}
// Combine the two buffers
- imageBuffer = CombineImageBuffer(imageBuffer, shadowImageBuffer, bufferWidth, bufferHeight);
- }
-
- // Generate the underline if enabled
- const bool underlineEnabled = mModel->IsUnderlineEnabled();
- if(underlineEnabled && RENDER_OVERLAY_STYLE == behaviour)
- {
- // Create the image buffer for underline
- 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, shadowImageBuffer, bufferWidth, bufferHeight, true);
}
// Generate the background if enabled
}
else
{
- backgroundImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, pixelFormat);
+ backgroundImageBuffer = CreateTransparentImageBuffer(bufferWidth, bufferHeight, pixelFormat);
}
if(backgroundMarkupSet)
}
// Combine the two buffers
- imageBuffer = CombineImageBuffer(imageBuffer, backgroundImageBuffer, bufferWidth, bufferHeight);
+ CombineImageBuffer(imageBuffer, backgroundImageBuffer, bufferWidth, bufferHeight, true);
}
- // Generate the strikethrough if enabled
- const bool strikethroughEnabled = mModel->IsStrikethroughEnabled();
- if(strikethroughEnabled && RENDER_OVERLAY_STYLE == behaviour)
+ // Generate the background_with_mask if enabled
+ const bool backgroundWithCutoutEnabled = mModel->IsBackgroundWithCutoutEnabled();
+ if((backgroundWithCutoutEnabled) && RENDER_OVERLAY_STYLE != behaviour)
{
- // Create the image buffer for strikethrough
- Devel::PixelBuffer strikethroughImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_STRIKETHROUGH, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, endIndexOfGlyphs);
+ Devel::PixelBuffer backgroundImageBuffer;
+
+ backgroundImageBuffer = CreateFullBackgroundBuffer(bufferWidth, bufferHeight, mModel->GetBackgroundColorWithCutout());
// Combine the two buffers
- imageBuffer = CombineImageBuffer(imageBuffer, strikethroughImageBuffer, bufferWidth, bufferHeight);
+ CombineImageBuffer(imageBuffer, backgroundImageBuffer, bufferWidth, bufferHeight, true);
}
- // Markup-Processor
+ if(RENDER_OVERLAY_STYLE == behaviour)
+ {
+ if(mModel->IsUnderlineEnabled())
+ {
+ // Create the image buffer for underline
+ Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, startIndexOfGlyphs, endIndexOfGlyphs);
+
+ // Combine the two buffers
+ CombineImageBuffer(imageBuffer, underlineImageBuffer, bufferWidth, bufferHeight, true);
+ }
- imageBuffer = ApplyMarkupProcessorOnPixelBuffer(imageBuffer, bufferWidth, bufferHeight, ignoreHorizontalAlignment, pixelFormat, penX, penY);
+ if(mModel->IsStrikethroughEnabled())
+ {
+ // Create the image buffer for strikethrough
+ Devel::PixelBuffer strikethroughImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_STRIKETHROUGH, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, endIndexOfGlyphs);
+
+ // Combine the two buffers
+ CombineImageBuffer(imageBuffer, strikethroughImageBuffer, bufferWidth, bufferHeight, true);
+ }
+
+ // Markup-Processor for overlay styles
+ if(mModel->IsMarkupProcessorEnabled() || mModel->IsSpannedTextPlaced())
+ {
+ if(mModel->IsMarkupUnderlineSet())
+ {
+ imageBuffer = ApplyUnderlineMarkupImageBuffer(imageBuffer, bufferWidth, bufferHeight, ignoreHorizontalAlignment, pixelFormat, penX, penY);
+ }
+
+ if(mModel->IsMarkupStrikethroughSet())
+ {
+ imageBuffer = ApplyStrikethroughMarkupImageBuffer(imageBuffer, bufferWidth, bufferHeight, ignoreHorizontalAlignment, pixelFormat, penX, penY);
+ }
+ }
+ }
}
- // Create the final PixelData for the combined image buffer
- PixelData pixelData = Devel::PixelBuffer::Convert(imageBuffer);
+ return imageBuffer;
+}
+
+Devel::PixelBuffer Typesetter::CreateFullBackgroundBuffer(const uint32_t bufferWidth, const uint32_t bufferHeight, const Vector4& backgroundColor)
+{
+ const uint32_t bufferSizeInt = bufferWidth * bufferHeight;
+ uint8_t backgroundColorAlpha = static_cast<uint8_t>(backgroundColor.a * 255.f);
- return pixelData;
+ Devel::PixelBuffer buffer = Devel::PixelBuffer::New(bufferWidth, bufferHeight, Pixel::RGBA8888);
+
+ uint32_t* bitmapBuffer = reinterpret_cast<uint32_t*>(buffer.GetBuffer());
+
+ uint32_t packedBackgroundColor = 0u;
+ uint8_t* packedBackgroundColorBuffer = reinterpret_cast<uint8_t*>(&packedBackgroundColor);
+
+ // Write the color to the pixel buffer
+ *(packedBackgroundColorBuffer + 3u) = backgroundColorAlpha;
+ *(packedBackgroundColorBuffer + 2u) = static_cast<uint8_t>(backgroundColor.b * backgroundColorAlpha);
+ *(packedBackgroundColorBuffer + 1u) = static_cast<uint8_t>(backgroundColor.g * backgroundColorAlpha);
+ *(packedBackgroundColorBuffer) = static_cast<uint8_t>(backgroundColor.r * backgroundColorAlpha);
+
+ std::fill(bitmapBuffer, bitmapBuffer + bufferSizeInt, packedBackgroundColor);
+
+ return buffer;
}
-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, const Typesetter::Style style, const bool ignoreHorizontalAlignment, const Pixel::Format pixelFormat, const int32_t horizontalOffset, const int32_t verticalOffset, const GlyphIndex fromGlyphIndex, const GlyphIndex toGlyphIndex)
{
// Retrieve lines, glyphs, positions and colors from the view model.
- const Length modelNumberOfLines = mModel->GetNumberOfLines();
- const LineRun* const modelLinesBuffer = mModel->GetLines();
- const GlyphInfo* const glyphsBuffer = mModel->GetGlyphs();
- const Vector2* const positionBuffer = mModel->GetLayout();
- const Vector4* const colorsBuffer = mModel->GetColors();
- const ColorIndex* const colorIndexBuffer = mModel->GetColorIndices();
- const GlyphInfo* hyphens = mModel->GetHyphens();
- const Length* hyphenIndices = mModel->GetHyphenIndices();
- const Length hyphensCount = mModel->GetHyphensCount();
+ const Length modelNumberOfLines = mModel->GetNumberOfLines();
+ const LineRun* const __restrict__ modelLinesBuffer = mModel->GetLines();
+ const GlyphInfo* const __restrict__ glyphsBuffer = mModel->GetGlyphs();
+ const Vector2* const __restrict__ positionBuffer = mModel->GetLayout();
+ const Vector4* const __restrict__ colorsBuffer = mModel->GetColors();
+ const ColorIndex* const __restrict__ colorIndexBuffer = mModel->GetColorIndices();
+ const GlyphInfo* __restrict__ hyphens = mModel->GetHyphens();
+ const Length* __restrict__ hyphenIndices = mModel->GetHyphenIndices();
+ const Length hyphensCount = mModel->GetHyphensCount();
+ const bool removeFrontInset = mModel->IsRemoveFrontInset();
+ const bool removeBackInset = mModel->IsRemoveBackInset();
+ const bool cutoutEnabled = mModel->IsCutoutEnabled();
// Elided text info. Indices according to elided text and Ellipsis position.
const auto startIndexOfGlyphs = mModel->GetStartIndexOfElidedGlyphs();
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<CharacterIndex>& glyphToCharacterMap = mModel->GetGlyphsToCharacters();
- const CharacterIndex* glyphToCharacterMapBuffer = glyphToCharacterMap.Begin();
+ const Character* __restrict__ textBuffer = mModel->GetTextBuffer();
+ float calculatedAdvance = 0.f;
+ const Vector<CharacterIndex>& __restrict__ glyphToCharacterMap = mModel->GetGlyphsToCharacters();
+ const CharacterIndex* __restrict__ 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<int>(line.alignmentOffset);
+ glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast<int32_t>(line.alignmentOffset);
glyphData.horizontalOffset += horizontalOffset;
// Increases the vertical offset with the line's ascender.
- glyphData.verticalOffset += static_cast<int>(line.ascender);
-
- // Include line spacing after first line
- if(lineIndex > 0u)
- {
- glyphData.verticalOffset += static_cast<int>(line.lineSpacing);
- }
+ glyphData.verticalOffset += static_cast<int32_t>(line.ascender + GetPreOffsetVerticalLineAlignment(line, verLineAlign));
// Retrieves the glyph's outline width
float outlineWidth = static_cast<float>(mModel->GetOutlineWidth());
if(style == Typesetter::STYLE_OUTLINE)
{
+ const Vector2& outlineOffset = mModel->GetOutlineOffset();
+
glyphData.horizontalOffset -= outlineWidth;
+ glyphData.horizontalOffset += outlineOffset.x;
if(lineIndex == 0u)
{
// Only need to add the vertical outline offset for the first line
glyphData.verticalOffset -= outlineWidth;
+ glyphData.verticalOffset += outlineOffset.y;
}
}
else if(style == Typesetter::STYLE_SHADOW)
const float modelCharacterSpacing = mModel->GetCharacterSpacing();
// Get the character-spacing runs.
- const Vector<CharacterSpacingGlyphRun>& characterSpacingGlyphRuns = mModel->GetCharacterSpacingGlyphRuns();
+ const Vector<CharacterSpacingGlyphRun>& __restrict__ characterSpacingGlyphRuns = mModel->GetCharacterSpacingGlyphRuns();
// Aggregate underline-style-properties from mModel
const UnderlineStyleProperties modelUnderlineProperties{mModel->GetUnderlineType(),
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;
{
// We need to fetch fresh font underline metrics
FontMetrics fontMetrics;
- fontClient.GetFontMetrics(glyphInfo->fontId, fontMetrics);
+ mFontClient.GetFontMetrics(glyphInfo->fontId, fontMetrics);
//The currentUnderlinePosition will be used for both Underline and/or Strikethrough
currentUnderlinePosition = FetchUnderlinePositionFromFontMetrics(fontMetrics);
}
// Calculate the positions of leftmost and rightmost glyphs in the current line
- if(position.x < lineExtentLeft)
+ if(removeFrontInset)
{
- lineExtentLeft = position.x;
+ if(position.x < lineExtentLeft)
+ {
+ lineExtentLeft = position.x;
+ }
+ }
+ else
+ {
+ const float originPositionLeft = position.x - glyphInfo->xBearing;
+ if(originPositionLeft < lineExtentLeft)
+ {
+ lineExtentLeft = originPositionLeft;
+ }
}
- if(position.x + glyphInfo->width > lineExtentRight)
+ if(removeBackInset)
{
- lineExtentRight = position.x + glyphInfo->width;
+ if(position.x + glyphInfo->width > lineExtentRight)
+ {
+ lineExtentRight = position.x + glyphInfo->width;
+ }
+ }
+ else
+ {
+ const float originPositionRight = position.x - glyphInfo->xBearing + glyphInfo->advance;
+ if(originPositionRight > lineExtentRight)
+ {
+ lineExtentRight = originPositionRight;
+ }
}
// Retrieves the glyph's color.
color = (useDefaultColor || (0u == colorIndex)) ? defaultColor : *(colorsBuffer + (colorIndex - 1u));
}
+ if(style == Typesetter::STYLE_NONE && cutoutEnabled)
+ {
+ // Temporarily adjust the transparency to 1.f
+ color.a = 1.f;
+ }
+
// Premultiply alpha
color.r *= color.a;
color.g *= color.a;
if(style != Typesetter::STYLE_UNDERLINE && style != Typesetter::STYLE_STRIKETHROUGH)
{
- fontClient.CreateBitmap(glyphInfo->fontId,
- glyphInfo->index,
- glyphInfo->isItalicRequired,
- glyphInfo->isBoldRequired,
- glyphData.glyphBitmap,
- static_cast<int>(outlineWidth));
+ mFontClient.CreateBitmap(glyphInfo->fontId,
+ glyphInfo->index,
+ glyphInfo->isItalicRequired,
+ glyphInfo->isBoldRequired,
+ glyphData.glyphBitmap,
+ static_cast<int32_t>(outlineWidth));
}
// Sets the glyph's bitmap into the bitmap of the whole text.
glyphData.verticalOffset += glyphData.glyphBitmap.outlineOffsetY;
}
- // delete the glyphBitmap.buffer as it is now copied into glyphData.bitmapBuffer
- delete[] glyphData.glyphBitmap.buffer;
+ // free the glyphBitmap.buffer if it is owner of buffer
+ if(glyphData.glyphBitmap.isBufferOwned)
+ {
+ free(glyphData.glyphBitmap.buffer);
+ glyphData.glyphBitmap.isBufferOwned = false;
+ }
glyphData.glyphBitmap.buffer = NULL;
}
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<int>(-line.descender);
+ // Increases the vertical offset with the line's descender & line spacing.
+ glyphData.verticalOffset += static_cast<int32_t>(-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)
-{
- unsigned char* topBuffer = topPixelBuffer.GetBuffer();
- unsigned char* bottomBuffer = bottomPixelBuffer.GetBuffer();
-
- Devel::PixelBuffer combinedPixelBuffer;
-
- if(topBuffer == NULL && bottomBuffer == NULL)
- {
- // Nothing to do if both buffers are empty.
- return combinedPixelBuffer;
- }
-
- if(topBuffer == NULL)
- {
- // Nothing to do if topBuffer is empty.
- return bottomPixelBuffer;
- }
-
- if(bottomBuffer == NULL)
- {
- // Nothing to do if bottomBuffer is empty.
- return topPixelBuffer;
- }
-
- // 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<uint8_t*>(combinedPixelBuffer.GetBuffer());
- memset(combinedBuffer, 0u, bufferSizeChar);
-
- for(unsigned int 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.
-
- unsigned int alphaBuffer1 = topBuffer[pixelIndex * 4 + 3];
-
- 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];
- }
- }
-
- return combinedPixelBuffer;
-}
-
-Devel::PixelBuffer Typesetter::ApplyUnderlineMarkupImageBuffer(Devel::PixelBuffer topPixelBuffer, const unsigned int bufferWidth, const unsigned int bufferHeight, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, int horizontalOffset, int verticalOffset)
+Devel::PixelBuffer Typesetter::ApplyUnderlineMarkupImageBuffer(Devel::PixelBuffer topPixelBuffer, const uint32_t bufferWidth, const uint32_t bufferHeight, const bool ignoreHorizontalAlignment, const Pixel::Format pixelFormat, const int32_t horizontalOffset, const int32_t verticalOffset)
{
// Underline-tags (this is for Markup case)
// Get the underline runs.
// 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(underlineImageBuffer, topPixelBuffer, bufferWidth, bufferHeight);
+ // Result pixel buffer will be stored into topPixelBuffer.
+ CombineImageBuffer(underlineImageBuffer, topPixelBuffer, bufferWidth, bufferHeight, false);
itGlyphRun++;
}
return topPixelBuffer;
}
-Devel::PixelBuffer Typesetter::ApplyStrikethroughMarkupImageBuffer(Devel::PixelBuffer topPixelBuffer, const unsigned int bufferWidth, const unsigned int bufferHeight, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, int horizontalOffset, int verticalOffset)
+Devel::PixelBuffer Typesetter::ApplyStrikethroughMarkupImageBuffer(Devel::PixelBuffer topPixelBuffer, const uint32_t bufferWidth, const uint32_t bufferHeight, const bool ignoreHorizontalAlignment, const Pixel::Format pixelFormat, const int32_t horizontalOffset, const int32_t verticalOffset)
{
// strikethrough-tags (this is for Markup case)
// Get the strikethrough runs.
// 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
- topPixelBuffer = CombineImageBuffer(strikethroughImageBuffer, topPixelBuffer, bufferWidth, bufferHeight);
+ // Result pixel buffer will be stored into topPixelBuffer.
+ CombineImageBuffer(strikethroughImageBuffer, topPixelBuffer, bufferWidth, bufferHeight, false);
itGlyphRun++;
}
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)
+void Typesetter::SetMaskForImageBuffer(Devel::PixelBuffer& __restrict__ topPixelBuffer, Devel::PixelBuffer& __restrict__ bottomPixelBuffer, const uint32_t bufferWidth, const uint32_t bufferHeight, float originAlpha)
{
- // Apply the markup-Processor if enabled
- const bool markupProcessorEnabled = mModel->IsMarkupProcessorEnabled();
- if(markupProcessorEnabled)
- {
- topPixelBuffer = ApplyUnderlineMarkupImageBuffer(topPixelBuffer, bufferWidth, bufferHeight, ignoreHorizontalAlignment, pixelFormat, horizontalOffset, verticalOffset);
+ // Assume that we always combine two RGBA images
+ // Jump with 4bytes for optimize runtime.
+ uint32_t* topBuffer = reinterpret_cast<uint32_t*>(topPixelBuffer.GetBuffer());
+ uint32_t* bottomBuffer = reinterpret_cast<uint32_t*>(bottomPixelBuffer.GetBuffer());
- topPixelBuffer = ApplyStrikethroughMarkupImageBuffer(topPixelBuffer, bufferWidth, bufferHeight, ignoreHorizontalAlignment, pixelFormat, horizontalOffset, verticalOffset);
+ if(topBuffer == NULL || bottomBuffer == NULL)
+ {
+ // Nothing to do if one of both buffers are empty.
+ return;
}
- return topPixelBuffer;
+ const uint32_t bufferSizeInt = bufferWidth * bufferHeight;
+
+ for(uint32_t pixelIndex = 0; pixelIndex < bufferSizeInt; ++pixelIndex)
+ {
+ uint32_t topBufferColor = *(topBuffer);
+ uint32_t bottomBufferColor = *(bottomBuffer);
+ uint8_t* __restrict__ topBufferColorBuffer = reinterpret_cast<uint8_t*>(&topBufferColor);
+ uint8_t* __restrict__ bottomBufferColorBuffer = reinterpret_cast<uint8_t*>(&bottomBufferColor);
+
+ // Return the transparency of the text to original.
+ uint8_t originAlphaInt = originAlpha * 255;
+
+ uint8_t topAlpha = topBufferColorBuffer[3];
+ uint8_t bottomAlpha = 255 - topAlpha;
+
+ // Manual blending.
+ bottomBufferColorBuffer[0] = MultiplyAndSummationAndNormalizeColor(topBufferColorBuffer[0], originAlphaInt, bottomBufferColorBuffer[0], bottomAlpha);
+ bottomBufferColorBuffer[1] = MultiplyAndSummationAndNormalizeColor(topBufferColorBuffer[1], originAlphaInt, bottomBufferColorBuffer[1], bottomAlpha);
+ bottomBufferColorBuffer[2] = MultiplyAndSummationAndNormalizeColor(topBufferColorBuffer[2], originAlphaInt, bottomBufferColorBuffer[2], bottomAlpha);
+ bottomBufferColorBuffer[3] = MultiplyAndSummationAndNormalizeColor(topBufferColorBuffer[3], originAlphaInt, bottomBufferColorBuffer[3], bottomAlpha);
+
+ *(bottomBuffer) = bottomBufferColor;
+
+ // Increase each buffer's pointer.
+ ++topBuffer;
+ ++bottomBuffer;
+ }
}
Typesetter::Typesetter(const ModelInterface* const model)
-: mModel(new ViewModel(model))
+: mModel(new ViewModel(model)),
+ mFontClient()
{
+ // Default font client set.
+ mFontClient = TextAbstraction::FontClient::Get();
}
Typesetter::~Typesetter()