2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
22 #include <dali/devel-api/text-abstraction/font-client.h>
23 #include <dali/public-api/common/constants.h>
27 #include <dali-toolkit/devel-api/controls/text-controls/text-label-devel.h>
28 #include <dali-toolkit/internal/text/rendering/view-model.h>
39 * @brief Data struct used to set the buffer of the glyph's bitmap into the final bitmap's buffer.
43 Devel::PixelBuffer bitmapBuffer; ///< The buffer of the whole bitmap. The format is RGBA8888.
44 Vector2* position; ///< The position of the glyph.
45 TextAbstraction::FontClient::GlyphBufferData glyphBitmap; ///< The glyph's bitmap.
46 unsigned int width; ///< The bitmap's width.
47 unsigned int height; ///< The bitmap's height.
48 int horizontalOffset; ///< The horizontal offset to be added to the 'x' glyph's position.
49 int verticalOffset; ///< The vertical offset to be added to the 'y' glyph's position.
53 * @brief Sets the glyph's buffer into the bitmap's buffer.
55 * @param[in] data Struct which contains the glyph's data and the bitmap's data.
56 * @param[in] position The position of the glyph.
57 * @param[in] color The color of the glyph.
58 * @param[in] style The style of the text.
59 * @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).
61 void TypesetGlyph(GlyphData& data,
62 const Vector2* const position,
63 const Vector4* const color,
64 Typesetter::Style style,
65 Pixel::Format pixelFormat)
67 if((0u == data.glyphBitmap.width) || (0u == data.glyphBitmap.height))
69 // Nothing to do if the width or height of the buffer is zero.
73 const int widthMinusOne = static_cast<int>(data.width - 1u);
74 const int heightMinusOne = static_cast<int>(data.height - 1u);
76 if(Pixel::RGBA8888 == pixelFormat)
78 // Whether the given glyph is a color one.
79 const bool isColorGlyph = data.glyphBitmap.isColorEmoji || data.glyphBitmap.isColorBitmap;
80 const uint32_t glyphPixelSize = Pixel::GetBytesPerPixel(data.glyphBitmap.format);
81 const uint32_t alphaIndex = glyphPixelSize - 1u;
82 const bool swapChannelsBR = Pixel::BGRA8888 == data.glyphBitmap.format;
84 // Pointer to the color glyph if there is one.
85 const uint32_t* const colorGlyphBuffer = isColorGlyph ? reinterpret_cast<uint32_t*>(data.glyphBitmap.buffer) : NULL;
87 // Initial vertical offset.
88 const int yOffset = data.verticalOffset + position->y;
90 uint32_t* bitmapBuffer = reinterpret_cast<uint32_t*>(data.bitmapBuffer.GetBuffer());
92 // Traverse the pixels of the glyph line per line.
93 for(int lineIndex = 0, glyphHeight = static_cast<int>(data.glyphBitmap.height); lineIndex < glyphHeight; ++lineIndex)
95 const int yOffsetIndex = yOffset + lineIndex;
96 if((0 > yOffsetIndex) || (yOffsetIndex > heightMinusOne))
98 // Do not write out of bounds.
102 const int verticalOffset = yOffsetIndex * data.width;
103 const int xOffset = data.horizontalOffset + position->x;
104 const int glyphBufferOffset = lineIndex * static_cast<int>(data.glyphBitmap.width);
105 for(int index = 0, glyphWidth = static_cast<int>(data.glyphBitmap.width); index < glyphWidth; ++index)
107 const int xOffsetIndex = xOffset + index;
108 if((0 > xOffsetIndex) || (xOffsetIndex > widthMinusOne))
110 // Don't write out of bounds.
116 // Retrieves the color from the color glyph.
117 uint32_t packedColorGlyph = *(colorGlyphBuffer + glyphBufferOffset + index);
118 uint8_t* packedColorGlyphBuffer = reinterpret_cast<uint8_t*>(&packedColorGlyph);
120 // Update the alpha channel.
121 if(Typesetter::STYLE_MASK == style || Typesetter::STYLE_OUTLINE == style) // Outline not shown for color glyph
123 // Create an alpha mask for color glyph.
124 *(packedColorGlyphBuffer + 3u) = 0u;
125 *(packedColorGlyphBuffer + 2u) = 0u;
126 *(packedColorGlyphBuffer + 1u) = 0u;
127 *packedColorGlyphBuffer = 0u;
131 const uint8_t colorAlpha = static_cast<uint8_t>(color->a * static_cast<float>(*(packedColorGlyphBuffer + 3u)));
132 *(packedColorGlyphBuffer + 3u) = colorAlpha;
134 if(Typesetter::STYLE_SHADOW == style)
136 // The shadow of color glyph needs to have the shadow color.
137 *(packedColorGlyphBuffer + 2u) = static_cast<uint8_t>(color->b * colorAlpha);
138 *(packedColorGlyphBuffer + 1u) = static_cast<uint8_t>(color->g * colorAlpha);
139 *packedColorGlyphBuffer = static_cast<uint8_t>(color->r * colorAlpha);
145 std::swap(*packedColorGlyphBuffer, *(packedColorGlyphBuffer + 2u)); // Swap B and R.
148 *(packedColorGlyphBuffer + 2u) = (*(packedColorGlyphBuffer + 2u) * colorAlpha / 255);
149 *(packedColorGlyphBuffer + 1u) = (*(packedColorGlyphBuffer + 1u) * colorAlpha / 255);
150 *packedColorGlyphBuffer = (*(packedColorGlyphBuffer)*colorAlpha / 255);
152 if(data.glyphBitmap.isColorBitmap)
154 *(packedColorGlyphBuffer + 2u) = static_cast<uint8_t>(*(packedColorGlyphBuffer + 2u) * color->b);
155 *(packedColorGlyphBuffer + 1u) = static_cast<uint8_t>(*(packedColorGlyphBuffer + 1u) * color->g);
156 *packedColorGlyphBuffer = static_cast<uint8_t>(*packedColorGlyphBuffer * color->r);
161 // Set the color into the final pixel buffer.
162 *(bitmapBuffer + verticalOffset + xOffsetIndex) = packedColorGlyph;
166 // Pack the given color into a 32bit buffer. The alpha channel will be updated later for each pixel.
167 // The format is RGBA8888.
168 uint32_t packedColor = 0u;
169 uint8_t* packedColorBuffer = reinterpret_cast<uint8_t*>(&packedColor);
171 // Update the alpha channel.
172 const uint8_t alpha = *(data.glyphBitmap.buffer + glyphPixelSize * (glyphBufferOffset + index) + alphaIndex);
174 // Copy non-transparent pixels only
177 // Check alpha of overlapped pixels
178 uint32_t& currentColor = *(bitmapBuffer + verticalOffset + xOffsetIndex);
179 uint8_t* packedCurrentColorBuffer = reinterpret_cast<uint8_t*>(¤tColor);
181 // For any pixel overlapped with the pixel in previous glyphs, make sure we don't
182 // overwrite a previous bigger alpha with a smaller alpha (in order to avoid
183 // semi-transparent gaps between joint glyphs with overlapped pixels, which could
184 // happen, for example, in the RTL text when we copy glyphs from right to left).
185 uint8_t currentAlpha = *(packedCurrentColorBuffer + 3u);
186 currentAlpha = std::max(currentAlpha, alpha);
188 // Color is pre-muliplied with its alpha.
189 *(packedColorBuffer + 3u) = static_cast<uint8_t>(color->a * currentAlpha);
190 *(packedColorBuffer + 2u) = static_cast<uint8_t>(color->b * currentAlpha);
191 *(packedColorBuffer + 1u) = static_cast<uint8_t>(color->g * currentAlpha);
192 *(packedColorBuffer) = static_cast<uint8_t>(color->r * currentAlpha);
194 // Set the color into the final pixel buffer.
195 currentColor = packedColor;
203 // Whether the given glyph is a color one.
204 const bool isColorGlyph = data.glyphBitmap.isColorEmoji || data.glyphBitmap.isColorBitmap;
205 const uint32_t glyphPixelSize = Pixel::GetBytesPerPixel(data.glyphBitmap.format);
206 const uint32_t alphaIndex = glyphPixelSize - 1u;
208 // Initial vertical offset.
209 const int yOffset = data.verticalOffset + position->y;
211 uint8_t* bitmapBuffer = reinterpret_cast<uint8_t*>(data.bitmapBuffer.GetBuffer());
213 // Traverse the pixels of the glyph line per line.
214 for(int lineIndex = 0, glyphHeight = static_cast<int>(data.glyphBitmap.height); lineIndex < glyphHeight; ++lineIndex)
216 const int yOffsetIndex = yOffset + lineIndex;
217 if((0 > yOffsetIndex) || (yOffsetIndex > heightMinusOne))
219 // Do not write out of bounds.
223 const int verticalOffset = yOffsetIndex * data.width;
224 const int xOffset = data.horizontalOffset + position->x;
225 const int glyphBufferOffset = lineIndex * static_cast<int>(data.glyphBitmap.width);
226 for(int index = 0, glyphWidth = static_cast<int>(data.glyphBitmap.width); index < glyphWidth; ++index)
228 const int xOffsetIndex = xOffset + index;
229 if((0 > xOffsetIndex) || (xOffsetIndex > widthMinusOne))
231 // Don't write out of bounds.
237 // Update the alpha channel.
238 const uint8_t alpha = *(data.glyphBitmap.buffer + glyphPixelSize * (glyphBufferOffset + index) + alphaIndex);
240 // Copy non-transparent pixels only
243 // Check alpha of overlapped pixels
244 uint8_t& currentAlpha = *(bitmapBuffer + verticalOffset + xOffsetIndex);
246 // For any pixel overlapped with the pixel in previous glyphs, make sure we don't
247 // overwrite a previous bigger alpha with a smaller alpha (in order to avoid
248 // semi-transparent gaps between joint glyphs with overlapped pixels, which could
249 // happen, for example, in the RTL text when we copy glyphs from right to left).
250 currentAlpha = std::max(currentAlpha, alpha);
258 bool IsGlyphUnderlined(GlyphIndex index,
259 const Vector<GlyphRun>& underlineRuns)
261 for(Vector<GlyphRun>::ConstIterator it = underlineRuns.Begin(),
262 endIt = underlineRuns.End();
266 const GlyphRun& run = *it;
268 if((run.glyphIndex <= index) && (index < run.glyphIndex + run.numberOfGlyphs))
277 /// Helper method to fetch the underline metrics for the specified font glyph
278 void FetchFontUnderlineMetrics(
279 TextAbstraction::FontClient& fontClient,
280 const GlyphInfo* const glyphInfo,
281 float& currentUnderlinePosition,
282 const float underlineHeight,
283 float& currentUnderlineThickness,
284 float& maxUnderlineThickness,
285 FontId& lastUnderlinedFontId)
287 FontMetrics fontMetrics;
288 fontClient.GetFontMetrics(glyphInfo->fontId, fontMetrics);
289 currentUnderlinePosition = ceil(fabsf(fontMetrics.underlinePosition));
290 const float descender = ceil(fabsf(fontMetrics.descender));
292 if(fabsf(underlineHeight) < Math::MACHINE_EPSILON_1000)
294 currentUnderlineThickness = fontMetrics.underlineThickness;
296 // Ensure underline will be at least a pixel high
297 if(currentUnderlineThickness < 1.0f)
299 currentUnderlineThickness = 1.0f;
303 currentUnderlineThickness = ceil(currentUnderlineThickness);
307 // The underline thickness should be the max underline thickness of all glyphs of the line.
308 if(currentUnderlineThickness > maxUnderlineThickness)
310 maxUnderlineThickness = currentUnderlineThickness;
313 // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
314 if(currentUnderlinePosition > descender)
316 currentUnderlinePosition = descender;
319 if(fabsf(currentUnderlinePosition) < Math::MACHINE_EPSILON_1000)
321 // Move offset down by one ( EFL behavior )
322 currentUnderlinePosition = 1.0f;
325 lastUnderlinedFontId = glyphInfo->fontId;
328 /// Draws the specified color to the pixel buffer
329 void WriteColorToPixelBuffer(
330 GlyphData& glyphData,
331 uint32_t* bitmapBuffer,
332 const Vector4& color,
333 const unsigned int x,
334 const unsigned int y)
336 // Always RGBA image for text with styles
337 uint32_t pixel = *(bitmapBuffer + y * glyphData.width + x);
338 uint8_t* pixelBuffer = reinterpret_cast<uint8_t*>(&pixel);
340 // Write the color to the pixel buffer
341 uint8_t colorAlpha = static_cast<uint8_t>(color.a * 255.f);
342 *(pixelBuffer + 3u) = colorAlpha;
343 *(pixelBuffer + 2u) = static_cast<uint8_t>(color.b * colorAlpha);
344 *(pixelBuffer + 1u) = static_cast<uint8_t>(color.g * colorAlpha);
345 *(pixelBuffer) = static_cast<uint8_t>(color.r * colorAlpha);
347 *(bitmapBuffer + y * glyphData.width + x) = pixel;
350 /// Draws the specified underline color to the buffer
352 const Vector4& underlineColor,
353 const unsigned int bufferWidth,
354 const unsigned int bufferHeight,
355 GlyphData& glyphData,
356 const float baseline,
357 const float currentUnderlinePosition,
358 const float maxUnderlineThickness,
359 const float lineExtentLeft,
360 const float lineExtentRight)
362 int underlineYOffset = glyphData.verticalOffset + baseline + currentUnderlinePosition;
363 uint32_t* bitmapBuffer = reinterpret_cast<uint32_t*>(glyphData.bitmapBuffer.GetBuffer());
365 for(unsigned int y = underlineYOffset; y < underlineYOffset + maxUnderlineThickness; y++)
367 if(y > bufferHeight - 1)
369 // Do not write out of bounds.
373 for(unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++)
375 if(x > bufferWidth - 1)
377 // Do not write out of bounds.
381 WriteColorToPixelBuffer(glyphData, bitmapBuffer, underlineColor, x, y);
386 /// Draws the background color to the buffer
387 void DrawBackgroundColor(
388 Vector4 backgroundColor,
389 const unsigned int bufferWidth,
390 const unsigned int bufferHeight,
391 GlyphData& glyphData,
392 const float baseline,
394 const float lineExtentLeft,
395 const float lineExtentRight)
397 uint32_t* bitmapBuffer = reinterpret_cast<uint32_t*>(glyphData.bitmapBuffer.GetBuffer());
399 for(int y = glyphData.verticalOffset + baseline - line.ascender; y < glyphData.verticalOffset + baseline - line.descender; y++)
401 if((y < 0) || (y > static_cast<int>(bufferHeight - 1)))
403 // Do not write out of bounds.
407 for(int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++)
409 if((x < 0) || (x > static_cast<int>(bufferWidth - 1)))
411 // Do not write out of bounds.
415 WriteColorToPixelBuffer(glyphData, bitmapBuffer, backgroundColor, x, y);
420 Devel::PixelBuffer DrawGlyphsBackground(const ViewModel* model, Devel::PixelBuffer& buffer, const unsigned int bufferWidth, const unsigned int bufferHeight, bool ignoreHorizontalAlignment, int horizontalOffset, int verticalOffset)
422 // Retrieve lines, glyphs, positions and colors from the view model.
423 const Length modelNumberOfLines = model->GetNumberOfLines();
424 const LineRun* const modelLinesBuffer = model->GetLines();
425 const Length numberOfGlyphs = model->GetNumberOfGlyphs();
426 const GlyphInfo* const glyphsBuffer = model->GetGlyphs();
427 const Vector2* const positionBuffer = model->GetLayout();
428 const Vector4* const backgroundColorsBuffer = model->GetBackgroundColors();
429 const ColorIndex* const backgroundColorIndicesBuffer = model->GetBackgroundColorIndices();
431 // Create and initialize the pixel buffer.
433 glyphData.verticalOffset = verticalOffset;
434 glyphData.width = bufferWidth;
435 glyphData.height = bufferHeight;
436 glyphData.bitmapBuffer = buffer;
437 glyphData.horizontalOffset = 0;
439 ColorIndex prevBackgroundColorIndex = 0;
440 ColorIndex backgroundColorIndex = 0;
442 // Traverses the lines of the text.
443 for(LineIndex lineIndex = 0u; lineIndex < modelNumberOfLines; ++lineIndex)
445 const LineRun& line = *(modelLinesBuffer + lineIndex);
447 // Sets the horizontal offset of the line.
448 glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast<int>(line.alignmentOffset);
449 glyphData.horizontalOffset += horizontalOffset;
451 // Increases the vertical offset with the line's ascender.
452 glyphData.verticalOffset += static_cast<int>(line.ascender);
454 // Include line spacing after first line
457 glyphData.verticalOffset += static_cast<int>(line.lineSpacing);
460 float left = bufferWidth;
462 float baseline = 0.0f;
464 // Traverses the glyphs of the line.
465 const GlyphIndex endGlyphIndex = std::min(numberOfGlyphs, line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs);
466 for(GlyphIndex glyphIndex = line.glyphRun.glyphIndex; glyphIndex < endGlyphIndex; ++glyphIndex)
468 // Retrieve the glyph's info.
469 const GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
471 if((glyphInfo->width < Math::MACHINE_EPSILON_1000) ||
472 (glyphInfo->height < Math::MACHINE_EPSILON_1000))
474 // Nothing to do if default background color, the glyph's width or height is zero.
478 backgroundColorIndex = (nullptr == backgroundColorsBuffer) ? 0u : *(backgroundColorIndicesBuffer + glyphIndex);
480 if((backgroundColorIndex != prevBackgroundColorIndex) &&
481 (prevBackgroundColorIndex != 0u))
483 const Vector4& backgroundColor = *(backgroundColorsBuffer + prevBackgroundColorIndex - 1u);
484 DrawBackgroundColor(backgroundColor, bufferWidth, bufferHeight, glyphData, baseline, line, left, right);
487 if(backgroundColorIndex == 0u)
489 prevBackgroundColorIndex = backgroundColorIndex;
490 //if background color is the default do nothing
494 // Retrieves the glyph's position.
495 const Vector2* const position = positionBuffer + glyphIndex;
497 if(baseline < position->y + glyphInfo->yBearing)
499 baseline = position->y + glyphInfo->yBearing;
502 // Calculate the positions of leftmost and rightmost glyphs in the current line
503 if((position->x < left) || (backgroundColorIndex != prevBackgroundColorIndex))
505 left = position->x - glyphInfo->xBearing;
508 if(position->x + glyphInfo->width > right)
510 right = position->x - glyphInfo->xBearing + glyphInfo->advance;
513 prevBackgroundColorIndex = backgroundColorIndex;
516 //draw last background at line end if not default
517 if(backgroundColorIndex != 0u)
519 const Vector4& backgroundColor = *(backgroundColorsBuffer + backgroundColorIndex - 1u);
520 DrawBackgroundColor(backgroundColor, bufferWidth, bufferHeight, glyphData, baseline, line, left, right);
523 // Increases the vertical offset with the line's descender.
524 glyphData.verticalOffset += static_cast<int>(-line.descender);
527 return glyphData.bitmapBuffer;
532 TypesetterPtr Typesetter::New(const ModelInterface* const model)
534 return TypesetterPtr(new Typesetter(model));
537 ViewModel* Typesetter::GetViewModel()
542 Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth, const unsigned int bufferHeight, Pixel::Format pixelFormat)
544 Devel::PixelBuffer imageBuffer = Devel::PixelBuffer::New(bufferWidth, bufferHeight, pixelFormat);
546 if(Pixel::RGBA8888 == pixelFormat)
548 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
549 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
550 memset(imageBuffer.GetBuffer(), 0u, bufferSizeChar);
554 memset(imageBuffer.GetBuffer(), 0, bufferWidth * bufferHeight);
560 PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirection::Type textDirection, RenderBehaviour behaviour, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat)
562 // @todo. This initial implementation for a TextLabel has only one visible page.
564 // Elides the text if needed.
565 mModel->ElideGlyphs();
567 // Retrieves the layout size.
568 const Size& layoutSize = mModel->GetLayoutSize();
570 const int outlineWidth = static_cast<int>(mModel->GetOutlineWidth());
572 // Set the offset for the horizontal alignment according to the text direction and outline width.
575 switch(mModel->GetHorizontalAlignment())
577 case HorizontalAlignment::BEGIN:
582 case HorizontalAlignment::CENTER:
584 penX += (textDirection == Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT) ? -outlineWidth : outlineWidth;
587 case HorizontalAlignment::END:
589 penX += (textDirection == Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT) ? -outlineWidth * 2 : outlineWidth * 2;
594 // Set the offset for the vertical alignment.
597 switch(mModel->GetVerticalAlignment())
599 case VerticalAlignment::TOP:
604 case VerticalAlignment::CENTER:
606 penY = static_cast<int>(0.5f * (size.height - layoutSize.height));
607 penY = penY < 0.f ? 0.f : penY;
610 case VerticalAlignment::BOTTOM:
612 penY = static_cast<int>(size.height - layoutSize.height);
617 // Calculate vertical line alignment
618 switch(mModel->GetVerticalLineAlignment())
620 case DevelText::VerticalLineAlignment::TOP:
624 case DevelText::VerticalLineAlignment::MIDDLE:
626 const auto& line = *mModel->GetLines();
627 penY -= line.descender;
628 penY += static_cast<int>(line.lineSpacing * 0.5f + line.descender);
631 case DevelText::VerticalLineAlignment::BOTTOM:
633 const auto& line = *mModel->GetLines();
634 const auto lineHeight = line.ascender + (-line.descender) + line.lineSpacing;
635 penY += static_cast<int>(lineHeight - (line.ascender - line.descender));
640 // Generate the image buffers of the text for each different style first,
641 // then combine all of them together as one final image buffer. We try to
642 // do all of these in CPU only, so that once the final texture is generated,
643 // no calculation is needed in GPU during each frame.
645 const unsigned int bufferWidth = static_cast<unsigned int>(size.width);
646 const unsigned int bufferHeight = static_cast<unsigned int>(size.height);
648 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
649 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
651 //Elided text in ellipsis at START could start on index greater than 0
652 auto startIndexOfGlyphs = mModel->GetStartIndexOfElidedGlyphs();
653 auto endIndexOfGlyphs = mModel->GetEndIndexOfElidedGlyphs();
655 Devel::PixelBuffer imageBuffer;
657 if(RENDER_MASK == behaviour)
659 // Generate the image buffer as an alpha mask for color glyphs.
660 imageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_MASK, ignoreHorizontalAlignment, pixelFormat, penX, penY, startIndexOfGlyphs, endIndexOfGlyphs);
662 else if(RENDER_NO_TEXT == behaviour)
664 // Generate an empty image buffer so that it can been combined with the image buffers for styles
665 imageBuffer = Devel::PixelBuffer::New(bufferWidth, bufferHeight, Pixel::RGBA8888);
666 memset(imageBuffer.GetBuffer(), 0u, bufferSizeChar);
670 // Generate the image buffer for the text with no style.
671 imageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_NONE, ignoreHorizontalAlignment, pixelFormat, penX, penY, startIndexOfGlyphs, endIndexOfGlyphs);
674 if((RENDER_NO_STYLES != behaviour) && (RENDER_MASK != behaviour))
676 // Generate the outline if enabled
677 const uint16_t outlineWidth = mModel->GetOutlineWidth();
678 if(outlineWidth != 0u)
680 // Create the image buffer for outline
681 Devel::PixelBuffer outlineImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_OUTLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, startIndexOfGlyphs, endIndexOfGlyphs);
683 // Combine the two buffers
684 imageBuffer = CombineImageBuffer(imageBuffer, outlineImageBuffer, bufferWidth, bufferHeight);
687 // @todo. Support shadow and underline for partial text later on.
689 // Generate the shadow if enabled
690 const Vector2& shadowOffset = mModel->GetShadowOffset();
691 if(fabsf(shadowOffset.x) > Math::MACHINE_EPSILON_1 || fabsf(shadowOffset.y) > Math::MACHINE_EPSILON_1)
693 // Create the image buffer for shadow
694 Devel::PixelBuffer shadowImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_SHADOW, ignoreHorizontalAlignment, pixelFormat, penX, penY, startIndexOfGlyphs, endIndexOfGlyphs);
696 // Check whether it will be a soft shadow
697 const float& blurRadius = mModel->GetShadowBlurRadius();
699 if(blurRadius > Math::MACHINE_EPSILON_1)
701 shadowImageBuffer.ApplyGaussianBlur(blurRadius);
704 // Combine the two buffers
705 imageBuffer = CombineImageBuffer(imageBuffer, shadowImageBuffer, bufferWidth, bufferHeight);
708 // Generate the underline if enabled
709 const bool underlineEnabled = mModel->IsUnderlineEnabled();
712 // Create the image buffer for underline
713 Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, startIndexOfGlyphs, endIndexOfGlyphs);
715 // Combine the two buffers
716 imageBuffer = CombineImageBuffer(imageBuffer, underlineImageBuffer, bufferWidth, bufferHeight);
719 // Generate the background if enabled
720 const bool backgroundEnabled = mModel->IsBackgroundEnabled();
721 const bool backgroundMarkupSet = mModel->IsMarkupBackgroundColorSet();
722 if(backgroundEnabled || backgroundMarkupSet)
724 Devel::PixelBuffer backgroundImageBuffer;
726 if(backgroundEnabled)
728 backgroundImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_BACKGROUND, ignoreHorizontalAlignment, pixelFormat, penX, penY, startIndexOfGlyphs, endIndexOfGlyphs);
732 backgroundImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, pixelFormat);
735 if(backgroundMarkupSet)
737 DrawGlyphsBackground(mModel, backgroundImageBuffer, bufferWidth, bufferHeight, ignoreHorizontalAlignment, penX, penY);
740 // Combine the two buffers
741 imageBuffer = CombineImageBuffer(imageBuffer, backgroundImageBuffer, bufferWidth, bufferHeight);
746 imageBuffer = ApplyMarkupProcessorOnPixelBuffer(imageBuffer, bufferWidth, bufferHeight, ignoreHorizontalAlignment, pixelFormat, penX, penY);
749 // Create the final PixelData for the combined image buffer
750 PixelData pixelData = Devel::PixelBuffer::Convert(imageBuffer);
755 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)
757 // Retrieve lines, glyphs, positions and colors from the view model.
758 const Length modelNumberOfLines = mModel->GetNumberOfLines();
759 const LineRun* const modelLinesBuffer = mModel->GetLines();
760 const GlyphInfo* const glyphsBuffer = mModel->GetGlyphs();
761 const Vector2* const positionBuffer = mModel->GetLayout();
762 const Vector4* const colorsBuffer = mModel->GetColors();
763 const ColorIndex* const colorIndexBuffer = mModel->GetColorIndices();
764 const GlyphInfo* hyphens = mModel->GetHyphens();
765 const Length* hyphenIndices = mModel->GetHyphenIndices();
766 const Length hyphensCount = mModel->GetHyphensCount();
768 // Elided text info. Indices according to elided text and Ellipsis position.
769 const auto startIndexOfGlyphs = mModel->GetStartIndexOfElidedGlyphs();
770 const auto endIndexOfGlyphs = mModel->GetEndIndexOfElidedGlyphs();
771 const auto firstMiddleIndexOfElidedGlyphs = mModel->GetFirstMiddleIndexOfElidedGlyphs();
772 const auto secondMiddleIndexOfElidedGlyphs = mModel->GetSecondMiddleIndexOfElidedGlyphs();
773 const auto ellipsisPosition = mModel->GetEllipsisPosition();
775 // Whether to use the default color.
776 const bool useDefaultColor = (NULL == colorsBuffer);
777 const Vector4& defaultColor = mModel->GetDefaultColor();
779 // Create and initialize the pixel buffer.
781 glyphData.verticalOffset = verticalOffset;
782 glyphData.width = bufferWidth;
783 glyphData.height = bufferHeight;
784 glyphData.bitmapBuffer = CreateImageBuffer(bufferWidth, bufferHeight, pixelFormat);
785 glyphData.horizontalOffset = 0;
787 // Get a handle of the font client. Used to retrieve the bitmaps of the glyphs.
788 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
789 Length hyphenIndex = 0;
791 // Traverses the lines of the text.
792 for(LineIndex lineIndex = 0u; lineIndex < modelNumberOfLines; ++lineIndex)
794 const LineRun& line = *(modelLinesBuffer + lineIndex);
796 // Sets the horizontal offset of the line.
797 glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast<int>(line.alignmentOffset);
798 glyphData.horizontalOffset += horizontalOffset;
800 // Increases the vertical offset with the line's ascender.
801 glyphData.verticalOffset += static_cast<int>(line.ascender);
803 // Include line spacing after first line
806 glyphData.verticalOffset += static_cast<int>(line.lineSpacing);
809 // Retrieves the glyph's outline width
810 float outlineWidth = static_cast<float>(mModel->GetOutlineWidth());
812 if(style == Typesetter::STYLE_OUTLINE)
814 glyphData.horizontalOffset -= outlineWidth;
817 // Only need to add the vertical outline offset for the first line
818 glyphData.verticalOffset -= outlineWidth;
821 else if(style == Typesetter::STYLE_SHADOW)
823 const Vector2& shadowOffset = mModel->GetShadowOffset();
824 glyphData.horizontalOffset += shadowOffset.x - outlineWidth; // if outline enabled then shadow should offset from outline
828 // Only need to add the vertical shadow offset for first line
829 glyphData.verticalOffset += shadowOffset.y - outlineWidth;
833 const bool underlineEnabled = mModel->IsUnderlineEnabled();
834 const Vector4& underlineColor = mModel->GetUnderlineColor();
835 const float underlineHeight = mModel->GetUnderlineHeight();
837 // Get the underline runs.
838 const Length numberOfUnderlineRuns = mModel->GetNumberOfUnderlineRuns();
839 Vector<GlyphRun> underlineRuns;
840 underlineRuns.Resize(numberOfUnderlineRuns);
841 mModel->GetUnderlineRuns(underlineRuns.Begin(), 0u, numberOfUnderlineRuns);
843 bool thereAreUnderlinedGlyphs = false;
845 float currentUnderlinePosition = 0.0f;
846 float currentUnderlineThickness = underlineHeight;
847 float maxUnderlineThickness = currentUnderlineThickness;
849 FontId lastUnderlinedFontId = 0;
851 float lineExtentLeft = bufferWidth;
852 float lineExtentRight = 0.0f;
853 float baseline = 0.0f;
854 bool addHyphen = false;
856 // Traverses the glyphs of the line.
857 const GlyphIndex startGlyphIndex = std::max(line.glyphRun.glyphIndex, startIndexOfGlyphs);
858 GlyphIndex endGlyphIndex = (line.isSplitToTwoHalves ? line.glyphRunSecondHalf.glyphIndex + line.glyphRunSecondHalf.numberOfGlyphs : line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs) - 1u;
859 endGlyphIndex = std::min(endGlyphIndex, endIndexOfGlyphs);
861 for(GlyphIndex glyphIndex = startGlyphIndex; glyphIndex <= endGlyphIndex; ++glyphIndex)
863 if(glyphIndex < fromGlyphIndex || glyphIndex > toGlyphIndex)
865 // Ignore any glyph that out of the specified range
869 //To handle START case of ellipsis, the first glyph has been shifted
870 //glyphIndex represent indices in whole glyphs but elidedGlyphIndex represents indices in elided Glyphs
871 GlyphIndex elidedGlyphIndex = glyphIndex - startIndexOfGlyphs;
873 //To handle MIDDLE case of ellipsis, the first glyph in the second half of line has been shifted and skip the removed glyph from middle.
874 if(ellipsisPosition == DevelText::EllipsisPosition::MIDDLE)
876 if(glyphIndex > firstMiddleIndexOfElidedGlyphs &&
877 glyphIndex < secondMiddleIndexOfElidedGlyphs)
879 // Ignore any glyph that removed for MIDDLE ellipsis
882 if(glyphIndex >= secondMiddleIndexOfElidedGlyphs)
884 elidedGlyphIndex -= (secondMiddleIndexOfElidedGlyphs - firstMiddleIndexOfElidedGlyphs - 1u);
888 // Retrieve the glyph's info.
889 const GlyphInfo* glyphInfo;
891 if(addHyphen && hyphens)
893 glyphInfo = hyphens + hyphenIndex;
898 glyphInfo = glyphsBuffer + elidedGlyphIndex;
901 if((glyphInfo->width < Math::MACHINE_EPSILON_1000) ||
902 (glyphInfo->height < Math::MACHINE_EPSILON_1000))
904 // Nothing to do if the glyph's width or height is zero.
908 const bool underlineGlyph = underlineEnabled || IsGlyphUnderlined(glyphIndex, underlineRuns);
909 thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || underlineGlyph;
911 // Are we still using the same fontId as previous
912 if(underlineGlyph && (glyphInfo->fontId != lastUnderlinedFontId))
914 // We need to fetch fresh font underline metrics
915 FetchFontUnderlineMetrics(fontClient, glyphInfo, currentUnderlinePosition, underlineHeight, currentUnderlineThickness, maxUnderlineThickness, lastUnderlinedFontId);
918 // Retrieves the glyph's position.
919 Vector2 position = *(positionBuffer + elidedGlyphIndex);
923 GlyphInfo tempInfo = *(glyphsBuffer + elidedGlyphIndex);
924 position.x = position.x + tempInfo.advance - tempInfo.xBearing + glyphInfo->xBearing;
925 position.y = -glyphInfo->yBearing;
928 if(baseline < position.y + glyphInfo->yBearing)
930 baseline = position.y + glyphInfo->yBearing;
933 // Calculate the positions of leftmost and rightmost glyphs in the current line
934 if(position.x < lineExtentLeft)
936 lineExtentLeft = position.x;
939 if(position.x + glyphInfo->width > lineExtentRight)
941 lineExtentRight = position.x + glyphInfo->width;
944 // Retrieves the glyph's color.
945 const ColorIndex colorIndex = useDefaultColor ? 0u : *(colorIndexBuffer + glyphIndex);
948 if(style == Typesetter::STYLE_SHADOW)
950 color = mModel->GetShadowColor();
952 else if(style == Typesetter::STYLE_OUTLINE)
954 color = mModel->GetOutlineColor();
958 color = (useDefaultColor || (0u == colorIndex)) ? defaultColor : *(colorsBuffer + (colorIndex - 1u));
966 // Retrieves the glyph's bitmap.
967 glyphData.glyphBitmap.buffer = NULL;
968 glyphData.glyphBitmap.width = glyphInfo->width; // Desired width and height.
969 glyphData.glyphBitmap.height = glyphInfo->height;
971 if(style != Typesetter::STYLE_OUTLINE && style != Typesetter::STYLE_SHADOW)
973 // Don't render outline for other styles
977 if(style != Typesetter::STYLE_UNDERLINE)
979 fontClient.CreateBitmap(glyphInfo->fontId,
981 glyphInfo->isItalicRequired,
982 glyphInfo->isBoldRequired,
983 glyphData.glyphBitmap,
984 static_cast<int>(outlineWidth));
987 // Sets the glyph's bitmap into the bitmap of the whole text.
988 if(NULL != glyphData.glyphBitmap.buffer)
990 if(style == Typesetter::STYLE_OUTLINE)
992 // Set the position offset for the current glyph
993 glyphData.horizontalOffset -= glyphData.glyphBitmap.outlineOffsetX;
994 glyphData.verticalOffset -= glyphData.glyphBitmap.outlineOffsetY;
997 // Set the buffer of the glyph's bitmap into the final bitmap's buffer
998 TypesetGlyph(glyphData,
1004 if(style == Typesetter::STYLE_OUTLINE)
1006 // Reset the position offset for the next glyph
1007 glyphData.horizontalOffset += glyphData.glyphBitmap.outlineOffsetX;
1008 glyphData.verticalOffset += glyphData.glyphBitmap.outlineOffsetY;
1011 // delete the glyphBitmap.buffer as it is now copied into glyphData.bitmapBuffer
1012 delete[] glyphData.glyphBitmap.buffer;
1013 glyphData.glyphBitmap.buffer = NULL;
1018 while((hyphenIndex < hyphensCount) && (glyphIndex > hyphenIndices[hyphenIndex]))
1023 addHyphen = ((hyphenIndex < hyphensCount) && ((glyphIndex + 1) == hyphenIndices[hyphenIndex]));
1031 // Draw the underline from the leftmost glyph to the rightmost glyph
1032 if(thereAreUnderlinedGlyphs && style == Typesetter::STYLE_UNDERLINE)
1034 DrawUnderline(underlineColor, bufferWidth, bufferHeight, glyphData, baseline, currentUnderlinePosition, maxUnderlineThickness, lineExtentLeft, lineExtentRight);
1037 // Draw the background color from the leftmost glyph to the rightmost glyph
1038 if(style == Typesetter::STYLE_BACKGROUND)
1040 DrawBackgroundColor(mModel->GetBackgroundColor(), bufferWidth, bufferHeight, glyphData, baseline, line, lineExtentLeft, lineExtentRight);
1043 // Increases the vertical offset with the line's descender.
1044 glyphData.verticalOffset += static_cast<int>(-line.descender);
1047 return glyphData.bitmapBuffer;
1050 Devel::PixelBuffer Typesetter::CombineImageBuffer(Devel::PixelBuffer topPixelBuffer, Devel::PixelBuffer bottomPixelBuffer, const unsigned int bufferWidth, const unsigned int bufferHeight)
1052 unsigned char* topBuffer = topPixelBuffer.GetBuffer();
1053 unsigned char* bottomBuffer = bottomPixelBuffer.GetBuffer();
1055 Devel::PixelBuffer combinedPixelBuffer;
1057 if(topBuffer == NULL && bottomBuffer == NULL)
1059 // Nothing to do if both buffers are empty.
1060 return combinedPixelBuffer;
1063 if(topBuffer == NULL)
1065 // Nothing to do if topBuffer is empty.
1066 return bottomPixelBuffer;
1069 if(bottomBuffer == NULL)
1071 // Nothing to do if bottomBuffer is empty.
1072 return topPixelBuffer;
1075 // Always combine two RGBA images
1076 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
1077 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
1079 combinedPixelBuffer = Devel::PixelBuffer::New(bufferWidth, bufferHeight, Pixel::RGBA8888);
1080 uint8_t* combinedBuffer = reinterpret_cast<uint8_t*>(combinedPixelBuffer.GetBuffer());
1081 memset(combinedBuffer, 0u, bufferSizeChar);
1083 for(unsigned int pixelIndex = 0; pixelIndex < bufferSizeInt; pixelIndex++)
1085 // If the alpha of the pixel in either buffer is not fully opaque, blend the two pixels.
1086 // Otherwise, copy pixel from topBuffer to combinedBuffer.
1088 unsigned int alphaBuffer1 = topBuffer[pixelIndex * 4 + 3];
1090 if(alphaBuffer1 != 255)
1092 // At least one pixel is not fully opaque
1093 // "Over" blend the the pixel from topBuffer with the pixel in bottomBuffer
1094 combinedBuffer[pixelIndex * 4] = topBuffer[pixelIndex * 4] + (bottomBuffer[pixelIndex * 4] * (255 - topBuffer[pixelIndex * 4 + 3]) / 255);
1095 combinedBuffer[pixelIndex * 4 + 1] = topBuffer[pixelIndex * 4 + 1] + (bottomBuffer[pixelIndex * 4 + 1] * (255 - topBuffer[pixelIndex * 4 + 3]) / 255);
1096 combinedBuffer[pixelIndex * 4 + 2] = topBuffer[pixelIndex * 4 + 2] + (bottomBuffer[pixelIndex * 4 + 2] * (255 - topBuffer[pixelIndex * 4 + 3]) / 255);
1097 combinedBuffer[pixelIndex * 4 + 3] = topBuffer[pixelIndex * 4 + 3] + (bottomBuffer[pixelIndex * 4 + 3] * (255 - topBuffer[pixelIndex * 4 + 3]) / 255);
1101 // Copy the pixel from topBuffer to combinedBuffer
1102 combinedBuffer[pixelIndex * 4] = topBuffer[pixelIndex * 4];
1103 combinedBuffer[pixelIndex * 4 + 1] = topBuffer[pixelIndex * 4 + 1];
1104 combinedBuffer[pixelIndex * 4 + 2] = topBuffer[pixelIndex * 4 + 2];
1105 combinedBuffer[pixelIndex * 4 + 3] = topBuffer[pixelIndex * 4 + 3];
1109 return combinedPixelBuffer;
1112 Devel::PixelBuffer Typesetter::ApplyMarkupProcessorOnPixelBuffer(Devel::PixelBuffer topPixelBuffer, const unsigned int bufferWidth, const unsigned int bufferHeight, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, int horizontalOffset, int verticalOffset)
1114 // Apply the markup-Processor if enabled
1115 const bool markupProcessorEnabled = mModel->IsMarkupProcessorEnabled();
1116 if(markupProcessorEnabled)
1118 // Underline-tags (this is for Markup case)
1119 // Get the underline runs.
1120 const Length numberOfUnderlineRuns = mModel->GetNumberOfUnderlineRuns();
1121 Vector<GlyphRun> underlineRuns;
1122 underlineRuns.Resize(numberOfUnderlineRuns);
1123 mModel->GetUnderlineRuns(underlineRuns.Begin(), 0u, numberOfUnderlineRuns);
1125 // Iterate on the consecutive underlined glyph run and connect them into one chunk of underlined characters.
1126 Vector<GlyphRun>::ConstIterator itGlyphRun = underlineRuns.Begin();
1127 Vector<GlyphRun>::ConstIterator endItGlyphRun = underlineRuns.End();
1128 GlyphIndex startGlyphIndex, endGlyphIndex;
1130 //The outer loop to iterate on the separated chunks of underlined glyph runs
1131 while(itGlyphRun != endItGlyphRun)
1133 startGlyphIndex = itGlyphRun->glyphIndex;
1134 endGlyphIndex = startGlyphIndex;
1135 //The inner loop to make a connected underline for the consecutive characters
1138 endGlyphIndex += itGlyphRun->numberOfGlyphs;
1140 } while(itGlyphRun != endItGlyphRun && itGlyphRun->glyphIndex == endGlyphIndex);
1144 // Create the image buffer for underline
1145 Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, horizontalOffset, verticalOffset, startGlyphIndex, endGlyphIndex);
1146 // Combine the two buffers
1147 topPixelBuffer = CombineImageBuffer(topPixelBuffer, underlineImageBuffer, bufferWidth, bufferHeight);
1151 return topPixelBuffer;
1154 Typesetter::Typesetter(const ModelInterface* const model)
1155 : mModel(new ViewModel(model))
1159 Typesetter::~Typesetter()
1166 } // namespace Toolkit