+/// 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)
+ {
+ currentUnderlineThickness = fontMetrics.underlineThickness;
+
+ // Ensure underline will be at least a pixel high
+ if(currentUnderlineThickness < 1.0f)
+ {
+ currentUnderlineThickness = 1.0f;
+ }
+ else
+ {
+ currentUnderlineThickness = ceil(currentUnderlineThickness);
+ }
+ }
+
+ if(fabsf(strikethroughHeight) < Math::MACHINE_EPSILON_1000)
+ {
+ // Ensure strikethrough will be at least a pixel high
+ if(currentStrikethroughThickness < 1.0f)
+ {
+ currentStrikethroughThickness = 1.0f;
+ }
+ else
+ {
+ currentStrikethroughThickness = ceil(currentStrikethroughThickness);
+ }
+ }
+
+ // The underline thickness should be the max underline thickness of all glyphs of the line.
+ if(currentUnderlineThickness > maxUnderlineThickness)
+ {
+ maxUnderlineThickness = currentUnderlineThickness;
+ }
+
+ // 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;
+ }
+
+ if(fabsf(currentUnderlinePosition) < Math::MACHINE_EPSILON_1000)
+ {
+ // Move offset down by one ( EFL behavior )
+ currentUnderlinePosition = 1.0f;
+ }
+
+ lastlinedFontId = glyphInfo->fontId;
+}
+
+/// 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);
+
+ // 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);
+
+ *(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,
+ const Text::Underline::Type underlineType,
+ const float dashedUnderlineWidth,
+ const float dashedUnderlineGap,
+ const LineRun& line)
+{
+ int underlineYOffset = glyphData.verticalOffset + baseline + currentUnderlinePosition;
+ uint32_t* bitmapBuffer = reinterpret_cast<uint32_t*>(glyphData.bitmapBuffer.GetBuffer());
+
+ for(unsigned int y = underlineYOffset; y < underlineYOffset + maxUnderlineThickness; y++)
+ {
+ if(y > bufferHeight - 1)
+ {
+ // 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)
+ {
+ // 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;
+ }
+ }
+ }
+ else
+ {
+ for(unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++)
+ {
+ if(x > bufferWidth - 1)
+ {
+ // Do not write out of bounds.
+ break;
+ }
+ WriteColorToPixelBuffer(glyphData, bitmapBuffer, underlineColor, x, y);
+ }
+ }
+ }
+ if(underlineType == Text::Underline::DOUBLE)
+ {
+ int secondUnderlineYOffset = glyphData.verticalOffset - line.descender - maxUnderlineThickness;
+ for(unsigned int y = secondUnderlineYOffset; y < secondUnderlineYOffset + maxUnderlineThickness; y++)
+ {
+ if(y > bufferHeight - 1)
+ {
+ // Do not write out of bounds.
+ break;
+ }
+ for(unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++)
+ {
+ if(x > bufferWidth - 1)
+ {
+ // Do not write out of bounds.
+ break;
+ }
+ WriteColorToPixelBuffer(glyphData, bitmapBuffer, underlineColor, x, y);
+ }
+ }
+ }
+}
+
+/// 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)
+{
+ 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++)
+ {
+ if((y < 0) || (y > static_cast<int>(bufferHeight - 1)))
+ {
+ // Do not write out of bounds.
+ continue;
+ }
+
+ for(int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++)
+ {
+ if((x < 0) || (x > static_cast<int>(bufferWidth - 1)))
+ {
+ // Do not write out of bounds.
+ continue;
+ }
+
+ WriteColorToPixelBuffer(glyphData, bitmapBuffer, backgroundColor, x, y);
+ }
+ }
+}
+
+Devel::PixelBuffer DrawGlyphsBackground(const ViewModel* model, Devel::PixelBuffer& buffer, const unsigned int bufferWidth, const unsigned int bufferHeight, bool ignoreHorizontalAlignment, int horizontalOffset, int verticalOffset)
+{
+ // Retrieve lines, glyphs, positions and colors from the view model.
+ const Length modelNumberOfLines = model->GetNumberOfLines();
+ const LineRun* const modelLinesBuffer = model->GetLines();
+ const Length numberOfGlyphs = model->GetNumberOfGlyphs();
+ const GlyphInfo* const glyphsBuffer = model->GetGlyphs();
+ const Vector2* const positionBuffer = model->GetLayout();
+ const Vector4* const backgroundColorsBuffer = model->GetBackgroundColors();
+ const ColorIndex* const backgroundColorIndicesBuffer = model->GetBackgroundColorIndices();
+
+ // Create and initialize the pixel buffer.
+ GlyphData glyphData;
+ glyphData.verticalOffset = verticalOffset;
+ glyphData.width = bufferWidth;
+ glyphData.height = bufferHeight;
+ glyphData.bitmapBuffer = buffer;
+ glyphData.horizontalOffset = 0;
+
+ ColorIndex prevBackgroundColorIndex = 0;
+ ColorIndex backgroundColorIndex = 0;
+
+ // 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 += 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);
+ }
+
+ float left = bufferWidth;
+ float right = 0.0f;
+ float baseline = 0.0f;
+
+ // Traverses the glyphs of the line.
+ const GlyphIndex endGlyphIndex = std::min(numberOfGlyphs, line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs);
+ for(GlyphIndex glyphIndex = line.glyphRun.glyphIndex; glyphIndex < endGlyphIndex; ++glyphIndex)
+ {
+ // Retrieve the glyph's info.
+ const GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
+
+ if((glyphInfo->width < Math::MACHINE_EPSILON_1000) ||
+ (glyphInfo->height < Math::MACHINE_EPSILON_1000))
+ {
+ // Nothing to do if default background color, the glyph's width or height is zero.
+ continue;
+ }
+
+ backgroundColorIndex = (nullptr == backgroundColorsBuffer) ? 0u : *(backgroundColorIndicesBuffer + glyphIndex);
+
+ if((backgroundColorIndex != prevBackgroundColorIndex) &&
+ (prevBackgroundColorIndex != 0u))
+ {
+ const Vector4& backgroundColor = *(backgroundColorsBuffer + prevBackgroundColorIndex - 1u);
+ DrawBackgroundColor(backgroundColor, bufferWidth, bufferHeight, glyphData, baseline, line, left, right);
+ }
+
+ if(backgroundColorIndex == 0u)
+ {
+ prevBackgroundColorIndex = backgroundColorIndex;
+ //if background color is the default do nothing
+ continue;
+ }
+
+ // Retrieves the glyph's position.
+ const Vector2* const position = positionBuffer + glyphIndex;
+
+ if(baseline < position->y + glyphInfo->yBearing)
+ {
+ baseline = position->y + glyphInfo->yBearing;
+ }
+
+ // Calculate the positions of leftmost and rightmost glyphs in the current line
+ if((position->x < left) || (backgroundColorIndex != prevBackgroundColorIndex))
+ {
+ left = position->x - glyphInfo->xBearing;
+ }
+
+ if(position->x + glyphInfo->width > right)
+ {
+ right = position->x - glyphInfo->xBearing + glyphInfo->advance;
+ }
+
+ prevBackgroundColorIndex = backgroundColorIndex;
+ }
+
+ //draw last background at line end if not default
+ if(backgroundColorIndex != 0u)
+ {
+ const Vector4& backgroundColor = *(backgroundColorsBuffer + backgroundColorIndex - 1u);
+ DrawBackgroundColor(backgroundColor, bufferWidth, bufferHeight, glyphData, baseline, line, left, right);
+ }
+
+ // Increases the vertical offset with the line's descender.
+ glyphData.verticalOffset += static_cast<int>(-line.descender);
+ }
+
+ 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)
+{
+ uint32_t* bitmapBuffer = reinterpret_cast<uint32_t*>(glyphData.bitmapBuffer.GetBuffer());
+
+ for(unsigned int y = strikethroughStartingYPosition; y < strikethroughStartingYPosition + maxStrikethroughThickness; y++)
+ {
+ if(y > bufferHeight - 1)
+ {
+ // Do not write out of bounds.
+ break;
+ }
+
+ for(unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++)
+ {
+ if(x > bufferWidth - 1)
+ {
+ // Do not write out of bounds.
+ break;
+ }
+
+ WriteColorToPixelBuffer(glyphData, bitmapBuffer, strikethroughColor, x, y);
+ }
+ }
+}
+