2 * Copyright (c) 2019 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/devel-api/text/text-utils-devel.h>
22 #include <dali/devel-api/text-abstraction/font-client.h>
23 #include <dali/devel-api/text-abstraction/text-renderer.h>
24 #include <dali/devel-api/text-abstraction/text-renderer-layout-helper.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/devel-api/scripting/enum-helper.h>
30 #include <dali-toolkit/internal/text/bidirectional-support.h>
31 #include <dali-toolkit/internal/text/character-set-conversion.h>
32 #include <dali-toolkit/internal/text/color-segmentation.h>
33 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
34 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
35 #include <dali-toolkit/internal/text/markup-processor.h>
36 #include <dali-toolkit/internal/text/multi-language-support.h>
37 #include <dali-toolkit/internal/text/segmentation.h>
38 #include <dali-toolkit/internal/text/shaper.h>
39 #include <dali-toolkit/internal/text/text-enumerations-impl.h>
40 #include <dali-toolkit/internal/text/text-font-style.h>
41 #include <dali-toolkit/internal/text/text-model.h>
46 using namespace TextAbstraction;
59 * @brief The text's layout.
63 SINGLELINE, ///< The text is laid out on a single line.
64 MULTILINE, ///< The text is laid out in multiple lines.
65 CIRCULAR, ///< The text is laid out on a single line following a circular path.
70 namespace CircularAlignment
74 * @brief The enumerations for the circular alignment.
78 BEGIN, ///< The text is aligned to the @p begin angle of the arc (or to the @p begin+increment if it's a RTL text).
79 CENTER, ///< The text is centered within the arc.
80 END, ///< The text is aligned to the @p begin+increment angle of the arc (or to the @p begin if it's a RTL text).
83 } // namespace CircularAlignment
85 const float TO_POINT_26_DOT_6 = 64.f;
86 const float TO_FLOAT = 1.f / 255.f;
87 const float TO_UCHAR = 255.f;
88 const bool RTL = true;
89 const float TWO_PI = 2.f * Dali::Math::PI; ///< 360 degrees in radians
90 const float RAD_135 = Math::PI_2 + Math::PI_4; ///< 135 degrees in radians;
91 const float RAD_225 = RAD_135 + Math::PI_2; ///< 225 degrees in radians;
92 const float RAD_270 = 3.f * Math::PI_2; ///< 270 degrees in radians;
93 const float RAD_315 = RAD_225 + Math::PI_2; ///< 315 degrees in radians;
95 DALI_ENUM_TO_STRING_TABLE_BEGIN( LAYOUT_TYPE )
96 DALI_ENUM_TO_STRING_WITH_SCOPE( DevelText::Layout, SINGLELINE )
97 DALI_ENUM_TO_STRING_WITH_SCOPE( DevelText::Layout, MULTILINE )
98 DALI_ENUM_TO_STRING_WITH_SCOPE( DevelText::Layout, CIRCULAR )
99 DALI_ENUM_TO_STRING_TABLE_END( LAYOUT_TYPE )
101 DALI_ENUM_TO_STRING_TABLE_BEGIN( CIRCULAR_ALIGNMENT_TYPE )
102 DALI_ENUM_TO_STRING_WITH_SCOPE( DevelText::CircularAlignment, BEGIN )
103 DALI_ENUM_TO_STRING_WITH_SCOPE( DevelText::CircularAlignment, CENTER )
104 DALI_ENUM_TO_STRING_WITH_SCOPE( DevelText::CircularAlignment, END )
105 DALI_ENUM_TO_STRING_TABLE_END( CIRCULAR_ALIGNMENT_TYPE )
107 bool GetLayoutEnumeration(const Property::Value& propertyValue, DevelText::Layout::Type& layout)
109 return Scripting::GetEnumerationProperty(propertyValue, LAYOUT_TYPE_TABLE, LAYOUT_TYPE_TABLE_COUNT, layout);
112 bool GetCircularAlignmentEnumeration(const Property::Value& propertyValue, DevelText::CircularAlignment::Type& circularAlignment)
114 return Scripting::GetEnumerationProperty(propertyValue, CIRCULAR_ALIGNMENT_TYPE_TABLE, CIRCULAR_ALIGNMENT_TYPE_TABLE_COUNT, circularAlignment);
117 Devel::PixelBuffer Render( const RendererParameters& textParameters, Vector<EmbeddedItemInfo>& embeddedItemLayout )
119 if( textParameters.text.empty() )
121 Dali::Devel::PixelBuffer pixelBuffer = Dali::Devel::PixelBuffer::New( textParameters.textWidth,
122 textParameters.textHeight,
123 Dali::Pixel::RGBA8888 );
125 const unsigned int bufferSize = textParameters.textWidth * textParameters.textHeight * Dali::Pixel::GetBytesPerPixel(Dali::Pixel::RGBA8888);
126 unsigned char* buffer = pixelBuffer.GetBuffer();
127 memset(buffer, 0, bufferSize);
132 MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
133 FontClient fontClient = FontClient::Get();
135 Text::Layout::Engine layoutEngine; ///< The layout engine.
136 Text::ModelPtr textModel = Text::Model::New(); ///< Pointer to the text's model.
137 Vector<ColorBlendingMode> blendingMode; ///< How embedded items and bitmap font glyphs are blended with color text.
138 Vector<bool> isEmoji; ///< Whether the glyph is an emoji.
140 // Use this to access FontClient i.e. to get down-scaled Emoji metrics.
141 metrics = Metrics::New( fontClient );
142 layoutEngine.SetMetrics( metrics );
144 TextAbstraction::TextRenderer::Parameters rendererParameters( textModel->mVisualModel->mGlyphs,
145 textModel->mVisualModel->mGlyphPositions,
146 textModel->mVisualModel->mColors,
147 textModel->mVisualModel->mColorIndices,
151 rendererParameters.width = textParameters.textWidth;
152 rendererParameters.height = textParameters.textHeight;
153 rendererParameters.pixelFormat = TextAbstraction::TextRenderer::Parameters::RGBA8888; // @note: At the moment all textures are generated RGBA8888
155 Vector<Character>& utf32Characters = textModel->mLogicalModel->mText; // Characters encoded in utf32.
156 Vector<Character> mirroredUtf32Characters; // The utf32Characters Characters but mirrored if there are RTL text.
157 Vector<LineBreakInfo>& lineBreakInfo = textModel->mLogicalModel->mLineBreakInfo; // The line break info.
158 Vector<ScriptRun>& scripts = textModel->mLogicalModel->mScriptRuns; // Charactes's script.
159 Vector<FontDescriptionRun>& fontDescriptionRuns = textModel->mLogicalModel->mFontDescriptionRuns; // Desired font descriptions.
160 Vector<FontRun>& validFonts = textModel->mLogicalModel->mFontRuns; // Validated fonts.
161 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = textModel->mLogicalModel->mBidirectionalParagraphInfo; // The bidirectional info per paragraph.
162 //Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = textModel->mLogicalModel->mBidirectionalLineInfo; // The bidirectional info per line.
163 Vector<CharacterDirection>& directions = textModel->mLogicalModel->mCharacterDirections; // Character's directions.
164 Vector<ColorRun>& colorRuns = textModel->mLogicalModel->mColorRuns; // colors of the text.
166 Vector<CharacterIndex>& glyphsToCharacters = textModel->mVisualModel->mGlyphsToCharacters; // Glyphs to character map.
167 Vector<GlyphIndex>& charactersToGlyph = textModel->mVisualModel->mCharactersToGlyph; // Characters to glyphs map.
168 Vector<Length>& charactersPerGlyph = textModel->mVisualModel->mCharactersPerGlyph; // Number of characters per glyph.
169 Vector<Length>& glyphsPerCharacter = textModel->mVisualModel->mGlyphsPerCharacter; // The number of glyphs that are shaped.
170 Vector<LineRun>& lines = textModel->mVisualModel->mLines; // The laid out lines.
172 Vector<GlyphIndex> newParagraphGlyphs; // Glyphs for the new paragraph characters.
174 // the default font's description.
175 FontDescription defaultFontDescription;
176 PointSize26Dot6 defaultPointSize = FontClient::DEFAULT_POINT_SIZE;
178 Length numberOfCharacters = 0u; // The number of characters (not glyphs!).
179 bool isTextMirrored = false; // Whether the text has been mirrored.
181 const uint8_t* utf8 = NULL; // pointer to the first character of the text (encoded in utf8)
182 Length textSize = 0u; // The length of the utf8 string.
184 ////////////////////////////////////////////////////////////////////////////////
185 // Process the markup string if the mark-up processor is enabled.
186 ////////////////////////////////////////////////////////////////////////////////
188 MarkupProcessData markupProcessData( colorRuns,
190 textModel->mLogicalModel->mEmbeddedItems );
192 if (textParameters.markupEnabled)
194 ProcessMarkupString(textParameters.text, markupProcessData);
195 textSize = markupProcessData.markupProcessedText.size();
197 // This is a bit horrible but std::string returns a (signed) char*
198 utf8 = reinterpret_cast<const uint8_t*>(markupProcessData.markupProcessedText.c_str());
202 textSize = textParameters.text.size();
204 // This is a bit horrible but std::string returns a (signed) char*
205 utf8 = reinterpret_cast<const uint8_t*>(textParameters.text.c_str());
208 ////////////////////////////////////////////////////////////////////////////////
209 // Convert from utf8 to utf32
210 ////////////////////////////////////////////////////////////////////////////////
212 utf32Characters.Resize(textSize);
214 // Transform a text array encoded in utf8 into an array encoded in utf32.
215 // It returns the actual number of characters.
216 numberOfCharacters = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
217 utf32Characters.Resize( numberOfCharacters );
219 ////////////////////////////////////////////////////////////////////////////////
220 // Retrieve the Line and Word Break Info.
221 ////////////////////////////////////////////////////////////////////////////////
223 lineBreakInfo.Resize( numberOfCharacters, LINE_NO_BREAK );
225 SetLineBreakInfo( utf32Characters,
230 ////////////////////////////////////////////////////////////////////////////////
231 // Retrieve the script runs.
232 ////////////////////////////////////////////////////////////////////////////////
234 multilanguageSupport.SetScripts( utf32Characters,
239 // Check if there are emojis.
240 // If there are an RGBA8888 pixel format is needed.
241 for( const auto& run : scripts )
243 if( run.script == TextAbstraction::Script::EMOJI )
245 rendererParameters.pixelFormat = TextAbstraction::TextRenderer::Parameters::RGBA8888;
250 ////////////////////////////////////////////////////////////////////////////////
251 // Retrieve the font runs.
252 ////////////////////////////////////////////////////////////////////////////////
254 // Set the description font run with the given text parameters.
255 FontDescriptionRun fontDescriptionRun;
256 fontDescriptionRun.characterRun.characterIndex = 0u;
257 fontDescriptionRun.characterRun.numberOfCharacters = numberOfCharacters;
259 fontDescriptionRun.familyLength = 0u;
260 fontDescriptionRun.familyName = nullptr;
261 fontDescriptionRun.familyDefined = !textParameters.fontFamily.empty();
262 if( fontDescriptionRun.familyDefined )
264 // The allocated memory will be freed when the logical model is destroyed.
265 fontDescriptionRun.familyLength = textParameters.fontFamily.size();
266 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
267 memcpy( fontDescriptionRun.familyName, textParameters.fontFamily.c_str(), fontDescriptionRun.familyLength );
270 fontDescriptionRun.weightDefined = !textParameters.fontWeight.empty();
271 if( fontDescriptionRun.weightDefined )
273 fontDescriptionRun.weight = StringToWeight( textParameters.fontWeight.c_str() );
276 fontDescriptionRun.widthDefined = !textParameters.fontWidth.empty();
277 if( fontDescriptionRun.widthDefined )
279 fontDescriptionRun.width = StringToWidth( textParameters.fontWidth.c_str() );
282 fontDescriptionRun.slantDefined = !textParameters.fontSlant.empty();
283 if( fontDescriptionRun.slantDefined )
285 fontDescriptionRun.slant = StringToSlant( textParameters.fontSlant.c_str() );
288 fontDescriptionRun.sizeDefined = !EqualsZero( textParameters.fontSize );
289 if( fontDescriptionRun.sizeDefined )
291 fontDescriptionRun.size = static_cast<unsigned int>( textParameters.fontSize * TO_POINT_26_DOT_6 );
294 fontDescriptionRuns.PushBack( fontDescriptionRun );
296 // Validates the fonts. If there is a character with no assigned font it sets a default one.
297 // After this call, fonts are validated.
298 multilanguageSupport.ValidateFonts( utf32Characters,
301 defaultFontDescription,
307 ////////////////////////////////////////////////////////////////////////////////
308 // Retrieve the Bidirectional info.
309 ////////////////////////////////////////////////////////////////////////////////
311 bidirectionalInfo.Reserve( 1u );
313 SetBidirectionalInfo( utf32Characters,
320 const bool hasBidirectionalText = 0u != bidirectionalInfo.Count();
321 if( hasBidirectionalText )
323 // Only set the character directions if there is right to left characters.
324 GetCharactersDirection( bidirectionalInfo,
330 // This paragraph has right to left text. Some characters may need to be mirrored.
331 // TODO: consider if the mirrored string can be stored as well.
333 isTextMirrored = GetMirroredText( utf32Characters,
338 mirroredUtf32Characters );
341 ////////////////////////////////////////////////////////////////////////////////
342 // Retrieve the glyphs. Text shaping
343 ////////////////////////////////////////////////////////////////////////////////
345 const Vector<Character>& textToShape = isTextMirrored ? mirroredUtf32Characters : utf32Characters;
347 newParagraphGlyphs.Reserve( 1u );
350 ShapeText( textToShape,
357 rendererParameters.glyphs,
360 newParagraphGlyphs );
362 // Create the 'number of glyphs' per character and the glyph to character conversion tables.
363 textModel->mVisualModel->CreateGlyphsPerCharacterTable( 0u, 0u, numberOfCharacters );
364 textModel->mVisualModel->CreateCharacterToGlyphTable( 0u, 0u, numberOfCharacters );
366 const Length numberOfGlyphs = rendererParameters.glyphs.Count();
368 // Once the text has been shaped and the glyphs created it's possible to replace the font id of those glyphs
369 // that represent an image or an item and create the embedded item layout info.
370 // Note: the position of the embedded item can't be set until the text is laid-out.
371 embeddedItemLayout.Reserve( textModel->mLogicalModel->mEmbeddedItems.Count() );
372 for( const auto& item : textModel->mLogicalModel->mEmbeddedItems )
374 // Get the glyph that matches with the character index.
375 const GlyphIndex glyphIndex = textModel->mVisualModel->mCharactersToGlyph[item.characterIndex];
376 GlyphInfo& glyph = rendererParameters.glyphs[glyphIndex];
379 Pixel::Format pixelFormat = Pixel::A8;
380 TextAbstraction::FontClient::EmbeddedItemDescription description = { std::string( item.url, item.urlLength ), item.width, item.height, item.colorBlendingMode };
381 glyph.index = fontClient.CreateEmbeddedItem( description, pixelFormat ); // Set here an index to an item.
383 if( ( Pixel::RGBA8888 == pixelFormat ) || ( Pixel::BGRA8888 == pixelFormat ) )
385 rendererParameters.pixelFormat = TextAbstraction::TextRenderer::Parameters::RGBA8888;
388 // If the url is empty the item is going to be added after the text is rendered. It's needed to store the layout here.
389 if( description.url.empty() )
391 EmbeddedItemInfo embeddedInfo =
396 Size( static_cast<float>( item.width ), static_cast<float>( item.height ) ),
397 Size( static_cast<float>( item.width ), static_cast<float>( item.height ) ),
399 item.colorBlendingMode
402 embeddedItemLayout.PushBack( embeddedInfo );
406 ////////////////////////////////////////////////////////////////////////////////
407 // Retrieve the glyph's metrics.
408 ////////////////////////////////////////////////////////////////////////////////
410 metrics->GetGlyphMetrics( rendererParameters.glyphs.Begin(), numberOfGlyphs );
412 ////////////////////////////////////////////////////////////////////////////////
413 // Set the color runs in glyphs.
414 ////////////////////////////////////////////////////////////////////////////////
416 SetColorSegmentationInfo( colorRuns,
422 textModel->mVisualModel->mColors,
423 textModel->mVisualModel->mColorIndices );
425 // Insert the default color at the beginning of the vector.
426 textModel->mVisualModel->mColors.Insert( textModel->mVisualModel->mColors.Begin(),textParameters.textColor );
428 // Set how the embedded items are blended with text color.
429 blendingMode.Resize( numberOfGlyphs, textParameters.isTextColorSet ? ColorBlendingMode::MULTIPLY : ColorBlendingMode::NONE );
431 if( !textParameters.isTextColorSet )
433 // Traverse the color runs.
434 for( const auto& run : colorRuns )
436 const GlyphIndex firstGlyph = textModel->mVisualModel->mCharactersToGlyph[run.characterRun.characterIndex];
437 const CharacterIndex lastCharacter = run.characterRun.characterIndex + run.characterRun.numberOfCharacters - 1u;
438 const GlyphIndex lastGlyphPlusOne = textModel->mVisualModel->mCharactersToGlyph[lastCharacter] + textModel->mVisualModel->mGlyphsPerCharacter[lastCharacter];
440 for( GlyphIndex index = firstGlyph; index < lastGlyphPlusOne; ++index )
442 blendingMode[index] = ColorBlendingMode::MULTIPLY;
447 // Traverse the embedded items and update the blending mode vector.
448 for( const auto& item : textModel->mLogicalModel->mEmbeddedItems )
450 const GlyphIndex glyphIndex = textModel->mVisualModel->mCharactersToGlyph[item.characterIndex];
451 blendingMode[glyphIndex] = item.colorBlendingMode;
454 ////////////////////////////////////////////////////////////////////////////////
455 // Set the isEmoji Vector
456 ////////////////////////////////////////////////////////////////////////////////
458 isEmoji.Resize( numberOfGlyphs, false );
460 for( const auto& run : scripts )
462 if( run.script == TextAbstraction::Script::EMOJI )
464 const GlyphIndex firstGlyph = textModel->mVisualModel->mCharactersToGlyph[run.characterRun.characterIndex];
465 const CharacterIndex lastCharacter = run.characterRun.characterIndex + run.characterRun.numberOfCharacters - 1u;
466 const GlyphIndex lastGlyphPlusOne = textModel->mVisualModel->mCharactersToGlyph[lastCharacter] + textModel->mVisualModel->mGlyphsPerCharacter[lastCharacter];
468 for( GlyphIndex index = firstGlyph; index < lastGlyphPlusOne; ++index )
470 isEmoji[index] = true;
475 ////////////////////////////////////////////////////////////////////////////////
477 ////////////////////////////////////////////////////////////////////////////////
479 // Sets the alignment
480 HorizontalAlignment::Type horizontalAlignment = Toolkit::HorizontalAlignment::CENTER;
481 HorizontalAlignment::Type horizontalCircularAlignment = Toolkit::HorizontalAlignment::CENTER;
482 VerticalAlignment::Type verticalAlignment = VerticalAlignment::CENTER;
483 Layout::Type layout = Layout::SINGLELINE;
484 CircularAlignment::Type circularAlignment = CircularAlignment::BEGIN;
486 Property::Value horizontalAlignmentStr( textParameters.horizontalAlignment );
487 GetHorizontalAlignmentEnumeration( horizontalAlignmentStr, horizontalAlignment );
488 horizontalCircularAlignment = horizontalAlignment;
490 Property::Value verticalAlignmentStr( textParameters.verticalAlignment );
491 GetVerticalAlignmentEnumeration( verticalAlignmentStr, verticalAlignment );
493 Property::Value layoutStr( textParameters.layout );
494 GetLayoutEnumeration( layoutStr, layout );
496 Property::Value circularAlignmentStr( textParameters.circularAlignment );
497 GetCircularAlignmentEnumeration( circularAlignmentStr, circularAlignment );
499 // Whether the layout is multi-line.
500 const Text::Layout::Engine::Type horizontalLayout = ( Layout::MULTILINE == layout ) ? Text::Layout::Engine::MULTI_LINE_BOX : Text::Layout::Engine::SINGLE_LINE_BOX;
501 layoutEngine.SetLayout( horizontalLayout ); // TODO: multi-line.
504 // Whether the layout is circular.
505 const bool isCircularTextLayout = (Layout::CIRCULAR == layout);
506 const bool isClockwise = isCircularTextLayout && ( 0.f < textParameters.incrementAngle );
508 // Calculates the max ascender or the max descender.
509 // Is used to calculate the radius of the base line of the text.
510 float maxAscenderDescender = 0.f;
511 if( isCircularTextLayout )
513 FontId currentFontId = 0u;
514 for( const auto& glyph : rendererParameters.glyphs )
516 if( currentFontId != glyph.fontId )
518 currentFontId = glyph.fontId;
520 fontClient.GetFontMetrics(currentFontId, metrics);
521 maxAscenderDescender = std::max( maxAscenderDescender, isClockwise ? metrics.ascender : metrics.descender );
525 const unsigned int radius = textParameters.radius - static_cast<unsigned int>( maxAscenderDescender );
527 // Convert CircularAlignment to HorizontalAlignment.
528 if( isCircularTextLayout )
530 switch( circularAlignment )
532 case CircularAlignment::BEGIN:
534 horizontalCircularAlignment = Toolkit::HorizontalAlignment::BEGIN;
537 case CircularAlignment::CENTER:
539 horizontalCircularAlignment = Toolkit::HorizontalAlignment::CENTER;
542 case CircularAlignment::END:
544 horizontalCircularAlignment = Toolkit::HorizontalAlignment::END;
550 // Set the layout parameters.
551 Size textLayoutArea( static_cast<float>( textParameters.textWidth ),
552 static_cast<float>( textParameters.textHeight ) );
554 if( isCircularTextLayout )
556 // In a circular layout, the length of the text area depends on the radius.
557 rendererParameters.radius = radius;
558 textLayoutArea.width = fabs( Radian( Degree( textParameters.incrementAngle ) ) * static_cast<float>( rendererParameters.radius ) );
561 textModel->mHorizontalAlignment = isCircularTextLayout ? horizontalCircularAlignment : horizontalAlignment;
562 textModel->mLineWrapMode = LineWrap::WORD;
563 textModel->mIgnoreSpacesAfterText = false;
564 textModel->mMatchSystemLanguageDirection = false;
565 Text::Layout::Parameters layoutParameters( textLayoutArea,
568 // Resize the vector of positions to have the same size than the vector of glyphs.
569 rendererParameters.positions.Resize( numberOfGlyphs );
571 // Whether the last character is a new paragraph character.
572 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( textToShape[numberOfCharacters - 1u] );
574 // The initial glyph and the number of glyphs to layout.
575 layoutParameters.startGlyphIndex = 0u;
576 layoutParameters.numberOfGlyphs = numberOfGlyphs;
577 layoutParameters.startLineIndex = 0u;
578 layoutParameters.estimatedNumberOfLines = 1u;
579 layoutParameters.interGlyphExtraAdvance = 0.f;
581 // Update the visual model.
583 bool isAutoScrollEnabled = false;
584 layoutEngine.LayoutText( layoutParameters,
586 textParameters.ellipsisEnabled,
587 isAutoScrollEnabled );
589 ////////////////////////////////////////////////////////////////////////////////
591 ////////////////////////////////////////////////////////////////////////////////
593 // Retrieve the line of text to know the direction and the width. @todo multi-line
594 const LineRun& line = lines[0u];
596 if( isCircularTextLayout )
598 // Set the circular alignment.
599 rendererParameters.circularLayout = isClockwise ? TextRenderer::Parameters::CLOCKWISE : TextRenderer::Parameters::COUNTER_CLOCKWISE;
601 // Update the text's height to be used by the ellipsis code.
602 textLayoutArea.height = newLayoutSize.height;
604 // Set the size of the text laid out on a straight horizontal line.
605 rendererParameters.circularWidth = static_cast<unsigned int>( newLayoutSize.width );
606 rendererParameters.circularHeight = static_cast<unsigned int>( newLayoutSize.height );
608 // Calculate the center of the circular text according the horizontal and vertical alingments and the radius.
609 switch( horizontalAlignment )
611 case HorizontalAlignment::BEGIN:
613 rendererParameters.centerX = static_cast<int>(textParameters.radius);
616 case HorizontalAlignment::CENTER:
618 rendererParameters.centerX = static_cast<int>( textParameters.textWidth / 2u );
621 case HorizontalAlignment::END:
623 rendererParameters.centerX = static_cast<int>( textParameters.textWidth ) - static_cast<int>(textParameters.radius);
628 switch( verticalAlignment )
630 case VerticalAlignment::TOP:
632 rendererParameters.centerY = static_cast<int>(textParameters.radius);
635 case VerticalAlignment::CENTER:
637 rendererParameters.centerY = static_cast<int>( textParameters.textHeight / 2u );
640 case VerticalAlignment::BOTTOM:
642 rendererParameters.centerY = static_cast<int>( textParameters.textHeight ) - static_cast<int>(textParameters.radius);
647 // Calculate the beginning angle according with the given horizontal alignment.
648 const bool isRTL = RTL == line.direction;
650 CircularAlignment::Type alignment = circularAlignment;
653 // Swap the alignment type if the line is right to left.
656 case CircularAlignment::BEGIN:
658 alignment = CircularAlignment::END;
661 case CircularAlignment::CENTER:
666 case CircularAlignment::END:
668 alignment = CircularAlignment::BEGIN;
674 float angleOffset = 0.f;
678 case CircularAlignment::BEGIN:
683 case CircularAlignment::CENTER:
685 const bool isNeg = textParameters.incrementAngle < 0.f;
686 const float textWidth = static_cast<float>( rendererParameters.circularWidth );
687 angleOffset = ( isNeg ? -0.5f : 0.5f ) * ( textLayoutArea.width - textWidth ) / static_cast<float>( radius );
690 case CircularAlignment::END:
692 const bool isNeg = textParameters.incrementAngle < 0.f;
693 const float textWidth = static_cast<float>( rendererParameters.circularWidth );
694 angleOffset = ( isNeg ? -1.f : 1.f ) * ( textLayoutArea.width - textWidth ) / static_cast<float>( radius );
699 // Update the beginning angle with the calculated offset.
700 rendererParameters.beginAngle = Radian( Degree( textParameters.beginAngle ) ) + angleOffset;
702 // Set the vertical position of the glyphs.
703 for( auto& position : rendererParameters.positions )
710 // Calculate the vertical offset according with the given alignment.
713 switch( verticalAlignment )
715 case VerticalAlignment::TOP:
717 penY = line.ascender;
720 case VerticalAlignment::CENTER:
722 penY = line.ascender + 0.5f * ( textLayoutArea.height - ( line.ascender - line.descender ) );
725 case VerticalAlignment::BOTTOM:
727 penY = textLayoutArea.height;
732 // Calculate the horizontal offset according with the given alignment.
733 float alignmentOffset = 0.f;
734 layoutEngine.Align( textLayoutArea,
740 Dali::LayoutDirection::LEFT_TO_RIGHT,
743 // Update the position of the glyphs with the calculated offsets.
744 for( auto& position : rendererParameters.positions )
746 position.x += line.alignmentOffset;
751 // Cairo adds the bearing to the position of the glyph
752 // that has already been added by the DALi's layout engine,
753 // so it's needed to be removed here.
754 for( unsigned int index = 0u; index < rendererParameters.glyphs.Count(); ++index )
756 const GlyphInfo& glyph = rendererParameters.glyphs[index];
757 Vector2& position = rendererParameters.positions[index];
759 position.x -= glyph.xBearing;
762 // Set the position of the embedded items (if there is any).
763 EmbeddedItemInfo* embeddedItemLayoutBuffer = embeddedItemLayout.Begin();
765 for( Length index = 0u, endIndex = embeddedItemLayout.Count(); index < endIndex; ++index )
767 EmbeddedItemInfo& embeddedItem = *( embeddedItemLayoutBuffer + index );
769 embeddedItem.position = rendererParameters.positions[embeddedItem.glyphIndex];
771 if( isCircularTextLayout )
773 // Calculate the new position of the embedded item in the circular path.
775 // Center of the bitmap.
776 const float halfWidth = 0.5f * embeddedItem.size.width;
777 const float halfHeight = 0.5f * embeddedItem.size.height;
778 double centerX = static_cast<double>( embeddedItem.position.x + halfWidth );
779 double centerY = static_cast<double>(embeddedItem.position.y - halfHeight);
781 Dali::TextAbstraction::CircularTextParameters circularTextParameters;
783 circularTextParameters.radius = static_cast<double>( radius );
784 circularTextParameters.invRadius = 1.0 / circularTextParameters.radius;
785 circularTextParameters.beginAngle = static_cast<double>( -rendererParameters.beginAngle + Dali::Math::PI_2 );
786 circularTextParameters.centerX = 0.5f * static_cast<double>( textParameters.textWidth );
787 circularTextParameters.centerY = 0.5f * static_cast<double>( textParameters.textHeight );
789 // Calculate the rotation angle.
790 float radians = rendererParameters.beginAngle;
793 radians += static_cast<float>( circularTextParameters.invRadius * centerX );
798 radians -= static_cast<float>( circularTextParameters.invRadius * centerX );
799 radians = -radians + Dali::Math::PI;
801 embeddedItem.angle = Degree( Radian( radians ) );
803 Dali::TextAbstraction::TransformToArc( circularTextParameters, centerX, centerY );
805 // Recalculate the size of the embedded item after the rotation to position it correctly.
806 float width = embeddedItem.size.width;
807 float height = embeddedItem.size.height;
809 // Transform the input angle into the range [0..2PI]
810 radians = fmod( radians, TWO_PI );
811 radians += ( radians < 0.f ) ? TWO_PI : 0.f;
813 // Does the same operations than rotate by shear.
814 if( ( radians > Math::PI_4 ) && ( radians <= RAD_135 ) )
816 std::swap( width, height );
817 radians -= Math::PI_2;
819 else if( ( radians > RAD_135 ) && ( radians <= RAD_225 ) )
823 else if( ( radians > RAD_225 ) && ( radians <= RAD_315 ) )
825 std::swap( width, height );
829 if( fabs( radians ) > Dali::Math::MACHINE_EPSILON_10 )
831 const float angleSinus = fabs( sin( radians ) );
832 const float angleCosinus = cos( radians );
834 // Calculate the rotated image dimensions.
835 embeddedItem.rotatedSize.height = width * angleSinus + height * angleCosinus;
836 embeddedItem.rotatedSize.width = height * angleSinus + width * angleCosinus + 1.f;
839 embeddedItem.position.x = floor( static_cast<float>( centerX ) - 0.5f * embeddedItem.rotatedSize.width );
840 embeddedItem.position.y = floor( static_cast<float>( centerY ) - 0.5f * embeddedItem.rotatedSize.height );
844 embeddedItem.position.y -= embeddedItem.size.height;
848 ////////////////////////////////////////////////////////////////////////////////
849 // Ellipsis the text.
850 ////////////////////////////////////////////////////////////////////////////////
852 if( textParameters.ellipsisEnabled )
854 const LineRun& line = lines[0u]; // TODO: multi-line
858 Length finalNumberOfGlyphs = 0u;
860 if( ( line.ascender - line.descender ) > textLayoutArea.height )
862 // The height of the line is bigger than the height of the text area.
863 // Show the ellipsis glyph even if it doesn't fit in the text area.
864 // If no text is rendered then issues are rised and it may be a while
865 // until is find out that the text area is too small.
867 // Get the first glyph which is going to be replaced and the ellipsis glyph.
868 GlyphInfo& glyphInfo = rendererParameters.glyphs[0u];
869 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphInfo.fontId ) );
871 // Change the 'x' and 'y' position of the ellipsis glyph.
872 Vector2& position = rendererParameters.positions[0u];
873 position.x = ellipsisGlyph.xBearing;
874 position.y = textLayoutArea.height - ellipsisGlyph.yBearing;
876 // Replace the glyph by the ellipsis glyph.
877 glyphInfo = ellipsisGlyph;
879 // Set the final number of glyphs
880 finalNumberOfGlyphs = 1u;
885 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
886 float firstPenX = 0.f; // Used if rtl text is elided.
887 bool firstPenSet = false;
889 // Add the ellipsis glyph.
890 bool inserted = false;
891 float removedGlypsWidth = 0.f;
892 Length numberOfRemovedGlyphs = 0u;
893 if (line.glyphRun.numberOfGlyphs > 0u)
895 GlyphIndex index = line.glyphRun.numberOfGlyphs - 1u;
897 GlyphInfo* glyphs = rendererParameters.glyphs.Begin();
898 Vector2* glyphPositions = rendererParameters.positions.Begin();
902 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
905 const GlyphInfo& glyphToRemove = *( glyphs + index );
907 if( 0u != glyphToRemove.fontId )
909 // i.e. The font id of the glyph shaped from the '\n' character is zero.
911 // Need to reshape the glyph as the font may be different in size.
912 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
916 const Vector2& position = *( glyphPositions + index );
918 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
921 // Calculates the first penX which will be used if rtl text is elided.
922 firstPenX = position.x - glyphToRemove.xBearing;
923 if( firstPenX < -ellipsisGlyph.xBearing )
925 // Avoids to exceed the bounding box when rtl text is elided.
926 firstPenX = -ellipsisGlyph.xBearing;
929 removedGlypsWidth = -ellipsisGlyph.xBearing;
934 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
936 // Calculate the width of the ellipsis glyph and check if it fits.
937 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
938 if( ellipsisGlyphWidth < removedGlypsWidth )
940 GlyphInfo& glyphInfo = *( glyphs + index );
941 Vector2& position = *( glyphPositions + index );
942 position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
944 // Replace the glyph by the ellipsis glyph.
945 glyphInfo = ellipsisGlyph;
947 // Update the isEmoji vector
948 isEmoji[index] = false;
950 // Change the 'x' and 'y' position of the ellipsis glyph.
952 if( position.x > firstPenX )
954 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
957 position.x += ellipsisGlyph.xBearing;
972 // No space for the ellipsis.
975 ++numberOfRemovedGlyphs;
978 // Set the final number of glyphs
979 finalNumberOfGlyphs = line.glyphRun.numberOfGlyphs - numberOfRemovedGlyphs;
983 // Resize the number of glyphs/positions
984 rendererParameters.glyphs.Resize( finalNumberOfGlyphs );
985 rendererParameters.positions.Resize( finalNumberOfGlyphs );
987 // Remove from the embedded items those exceding the last laid out glyph.
988 embeddedItemLayout.Erase( std::remove_if( embeddedItemLayout.Begin(),
989 embeddedItemLayout.End(),
990 [finalNumberOfGlyphs]( const EmbeddedItemInfo& item )
992 return item.glyphIndex >= finalNumberOfGlyphs;
994 embeddedItemLayout.End() );
999 ////////////////////////////////////////////////////////////////////////////////
1001 ////////////////////////////////////////////////////////////////////////////////
1003 rendererParameters.width = textParameters.textWidth;
1004 rendererParameters.height = textParameters.textHeight;
1006 TextAbstraction::TextRenderer renderer = TextAbstraction::TextRenderer::Get();
1007 return renderer.Render( rendererParameters );
1010 Devel::PixelBuffer CreateShadow( const ShadowParameters& shadowParameters )
1012 // The size of the pixel data.
1013 const int width = static_cast<int>(shadowParameters.input.GetWidth());
1014 const int height = static_cast<int>(shadowParameters.input.GetHeight());
1016 // The shadow's offset.
1017 const int xOffset = static_cast<int>( shadowParameters.offset.x );
1018 const int yOffset = static_cast<int>( shadowParameters.offset.y );
1020 // The size in bytes of the pixel of the input's buffer.
1021 const Pixel::Format inputFormat = shadowParameters.input.GetPixelFormat();
1022 const unsigned int inputPixelSize = Pixel::GetBytesPerPixel( inputFormat );
1023 const bool isA8 = Pixel::A8 == inputFormat;
1025 // Creates the output pixel buffer.
1026 Devel::PixelBuffer outputPixelBuffer = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
1028 // Clear the output buffer
1029 unsigned char* outputPixelBufferPtr = outputPixelBuffer.GetBuffer();
1030 memset(outputPixelBufferPtr, 0, width * height * Pixel::GetBytesPerPixel(Pixel::RGBA8888));
1032 // Gets the buffer of the input pixel buffer.
1033 const unsigned char* const inputPixelBuffer = shadowParameters.input.GetBuffer();
1035 float textColor[4u];
1038 memcpy(textColor, shadowParameters.textColor.AsFloat(), 4u * sizeof(float));
1040 const float* const shadowColor = shadowParameters.color.AsFloat();
1042 // Traverse the input pixel buffer and write the text on the foreground and the shadow on the background.
1043 for (int rowIndex = 0; rowIndex < height; ++rowIndex)
1045 // Calculates the rowIndex to the input pixel buffer for the shadow and whether it's within the boundaries.
1046 const int yOffsetIndex = rowIndex - yOffset;
1047 const bool isValidRowIndex = ((yOffsetIndex >= 0) && (yOffsetIndex < height));
1049 const int rows = rowIndex * width;
1050 const int offsetRows = yOffsetIndex * width;
1051 for (int columnIndex = 0; columnIndex < width; ++columnIndex)
1053 // Index to the input buffer to retrieve the alpha value of the foreground text.
1054 const unsigned int index = inputPixelSize * static_cast<unsigned int>(rows + columnIndex);
1056 // Build the index to the input buffer to retrieve the alpha value of the background shadow.
1057 unsigned int shadowIndex = 0u;
1058 bool isValidShadowIndex = false;
1059 if (isValidRowIndex)
1061 const int xOffsetIndex = columnIndex - xOffset;
1062 isValidShadowIndex = ((xOffsetIndex >= 0) && (xOffsetIndex < width));
1064 if (isValidShadowIndex)
1066 shadowIndex = inputPixelSize * static_cast<unsigned int>(offsetRows + xOffsetIndex);
1070 // If the input buffer is an alpha mask, retrieve the values for the foreground text and the background shadow.
1071 // If not retrieve the color.
1072 float inputShadowOffsetAlphaValue = 1.f;
1073 float inputAlphaValue = 1.f;
1076 // Retrieve the alpha value for the shadow.
1077 inputShadowOffsetAlphaValue = isValidShadowIndex ? (static_cast<float>(*(inputPixelBuffer + shadowIndex)) / 255.f) : 0.f;
1079 // Retrieve the alpha value for the text.
1080 inputAlphaValue = static_cast<float>(*(inputPixelBuffer + index)) / 255.f;
1084 // The input buffer is not an alpha mask. Retrieve the color.
1085 textColor[0u] = TO_FLOAT * static_cast<float>( *(inputPixelBuffer + index + 0u) );
1086 textColor[1u] = TO_FLOAT * static_cast<float>( *(inputPixelBuffer + index + 1u) );
1087 textColor[2u] = TO_FLOAT * static_cast<float>( *(inputPixelBuffer + index + 2u) );
1088 textColor[3u] = TO_FLOAT * static_cast<float>( *(inputPixelBuffer + index + 3u) );
1089 inputAlphaValue = textColor[3u];
1090 inputShadowOffsetAlphaValue = isValidShadowIndex ? TO_FLOAT * static_cast<float>( *(inputPixelBuffer + shadowIndex + 3u) ) : 0.f;
1093 // Build the output color.
1094 float outputColor[4u];
1096 if( shadowParameters.blendShadow )
1098 // Blend the shadow's color with the text's color on top
1099 const float textAlpha = textColor[3u] * inputAlphaValue;
1100 const float shadowAlpha = shadowColor[3u] * inputShadowOffsetAlphaValue;
1102 // Blends the alpha.
1103 outputColor[3u] = 1.f - ((1.f - textAlpha) * (1.f - shadowAlpha));
1104 const bool isOutputAlphaZero = outputColor[3u] < Dali::Math::MACHINE_EPSILON_1000;
1105 if( isOutputAlphaZero )
1107 std::fill( outputColor, outputColor + 4u, 0.f );
1111 // Blends the RGB components.
1112 float shadowComponent = 0.f;
1113 float textComponent = 0.f;
1115 shadowComponent = shadowColor[0u] * inputShadowOffsetAlphaValue;
1116 textComponent = textColor[0u] * inputAlphaValue;
1117 outputColor[0u] = (textComponent * textAlpha / outputColor[3u]) + (shadowComponent * shadowAlpha * (1.f - textAlpha) / outputColor[3u]);
1119 shadowComponent = shadowColor[1u] * inputShadowOffsetAlphaValue;
1120 textComponent = textColor[1u] * inputAlphaValue;
1121 outputColor[1u] = (textComponent * textAlpha / outputColor[3u]) + (shadowComponent * shadowAlpha * (1.f - textAlpha) / outputColor[3u]);
1123 shadowComponent = shadowColor[2u] * inputShadowOffsetAlphaValue;
1124 textComponent = textColor[2u] * inputAlphaValue;
1125 outputColor[2u] = (textComponent * textAlpha / outputColor[3u]) + (shadowComponent * shadowAlpha * (1.f - textAlpha) / outputColor[3u]);
1131 std::fill( outputColor, outputColor + 4u, 0.f );
1133 const float textAlpha = textColor[3u];
1134 const float shadowAlpha = shadowColor[3u] * inputShadowOffsetAlphaValue;
1136 // Write shadow first.
1137 if( shadowAlpha > Dali::Math::MACHINE_EPSILON_1000 )
1139 outputColor[0u] = shadowColor[0u] * inputShadowOffsetAlphaValue;
1140 outputColor[1u] = shadowColor[1u] * inputShadowOffsetAlphaValue;
1141 outputColor[2u] = shadowColor[2u] * inputShadowOffsetAlphaValue;
1142 outputColor[3u] = shadowAlpha;
1145 // Write character on top.
1146 if( textAlpha > Dali::Math::MACHINE_EPSILON_1000 )
1148 outputColor[0u] = textColor[0u];
1149 outputColor[1u] = textColor[1u];
1150 outputColor[2u] = textColor[2u];
1151 outputColor[3u] = textAlpha;
1155 // Write the color into the output pixel buffer.
1156 const unsigned int outputIndex = 4u * (rows + columnIndex);
1157 *(outputPixelBufferPtr + outputIndex + 0u) = static_cast<unsigned char>( TO_UCHAR * outputColor[0u] );
1158 *(outputPixelBufferPtr + outputIndex + 1u) = static_cast<unsigned char>( TO_UCHAR * outputColor[1u] );
1159 *(outputPixelBufferPtr + outputIndex + 2u) = static_cast<unsigned char>( TO_UCHAR * outputColor[2u] );
1160 *(outputPixelBufferPtr + outputIndex + 3u) = static_cast<unsigned char>( TO_UCHAR * outputColor[3u] );
1164 // Returns the pixel buffer.
1165 return outputPixelBuffer;
1168 Devel::PixelBuffer ConvertToRgba8888(Devel::PixelBuffer pixelBuffer, const Vector4& color, bool multiplyByAlpha)
1170 if (Dali::Pixel::A8 != pixelBuffer.GetPixelFormat())
1176 const unsigned int width = pixelBuffer.GetWidth();
1177 const unsigned int height = pixelBuffer.GetHeight();
1178 Devel::PixelBuffer newPixelBuffer = Devel::PixelBuffer::New( width, height, Dali::Pixel::RGBA8888 );
1180 unsigned char* dstBuffer = newPixelBuffer.GetBuffer();
1181 const unsigned char* const srcBuffer = pixelBuffer.GetBuffer();
1183 const unsigned char r = static_cast<unsigned char>( TO_UCHAR * color.r );
1184 const unsigned char g = static_cast<unsigned char>( TO_UCHAR * color.g );
1185 const unsigned char b = static_cast<unsigned char>( TO_UCHAR * color.b );
1187 unsigned char dstColor[4];
1188 for( unsigned int j = 0u; j < height; ++j )
1190 const unsigned int lineIndex = j * width;
1191 for( unsigned int i=0u; i < width; ++i )
1193 const unsigned int srcIndex = lineIndex + i;
1195 const float srcAlpha = static_cast<float>( *( srcBuffer + srcIndex ) );
1197 if( multiplyByAlpha )
1199 dstColor[0u] = static_cast<unsigned char>( srcAlpha * color.r );
1200 dstColor[1u] = static_cast<unsigned char>( srcAlpha * color.g );
1201 dstColor[2u] = static_cast<unsigned char>( srcAlpha * color.b );
1202 dstColor[3u] = static_cast<unsigned char>( srcAlpha * color.a );
1209 dstColor[3u] = static_cast<unsigned char>( srcAlpha );
1212 const unsigned int dstIndex = srcIndex * 4u;
1213 memcpy( dstBuffer + dstIndex, dstColor, 4u );
1217 return newPixelBuffer;
1220 void UpdateBuffer(Devel::PixelBuffer src, Devel::PixelBuffer dst, unsigned int x, unsigned int y, bool blend)
1222 const Dali::Pixel::Format pixelFormat = dst.GetPixelFormat();
1223 if( src.GetPixelFormat() != pixelFormat )
1225 DALI_LOG_ERROR("PixelBuffer::SetBuffer. The pixel format of the new data must be the same of the current pixel buffer.");
1229 const unsigned int srcWidth = src.GetWidth();
1230 const unsigned int srcHeight = src.GetHeight();
1231 const unsigned int dstWidth = dst.GetWidth();
1232 const unsigned int dstHeight = dst.GetHeight();
1234 if( ( x > dstWidth ) ||
1235 ( y > dstHeight ) ||
1236 ( x + srcWidth > dstWidth ) ||
1237 ( y + srcHeight > dstHeight ) )
1239 DALI_LOG_ERROR("PixelBuffer::SetBuffer. The source pixel buffer is out of the boundaries of the destination pixel buffer.");
1243 const unsigned int bytesPerPixel = Dali::Pixel::GetBytesPerPixel(pixelFormat);
1244 if( bytesPerPixel == 0u || bytesPerPixel == 12u || bytesPerPixel == 24u )
1248 const unsigned int alphaIndex = bytesPerPixel - 1u;
1250 const unsigned char* const srcBuffer = src.GetBuffer();
1251 unsigned char* dstBuffer = dst.GetBuffer();
1255 const unsigned int currentLineSize = dstWidth * bytesPerPixel;
1256 const unsigned int newLineSize = srcWidth * bytesPerPixel;
1257 unsigned char* currentBuffer = dstBuffer + (y * dstWidth + x) * bytesPerPixel;
1258 for (unsigned int j = 0u; j < srcHeight; ++j)
1260 memcpy(currentBuffer + j * currentLineSize, srcBuffer + j * newLineSize, newLineSize);
1265 float outputColor[4u];
1267 // Blend the src pixel buffer with the dst pixel buffer as background.
1269 // fgColor, fgAlpha, bgColor, bgAlpha
1271 // alpha = 1 - ( 1 - fgAlpha ) * ( 1 - bgAlpha )
1272 // color = ( fgColor * fgAlpha / alpha ) + ( bgColor * bgAlpha * ( 1 - fgAlpha ) / alpha )
1274 // Jump till the 'x,y' position
1275 const unsigned int dstWidthBytes = dstWidth * bytesPerPixel;
1276 dstBuffer += ( y * dstWidthBytes + x * bytesPerPixel );
1278 for (unsigned int j = 0u; j < srcHeight; ++j)
1280 const unsigned int srcLineIndex = j * srcWidth;
1281 for (unsigned int i = 0u; i < srcWidth; ++i)
1283 const float srcAlpha = TO_FLOAT * static_cast<float>( *( srcBuffer + bytesPerPixel * ( srcLineIndex + i ) + alphaIndex ) );
1284 const float dstAlpha = TO_FLOAT * static_cast<float>( *(dstBuffer + i*bytesPerPixel + alphaIndex) );
1286 // Blends the alpha channel.
1287 const float oneMinusSrcAlpha = 1.f - srcAlpha;
1288 outputColor[alphaIndex] = 1.f - (oneMinusSrcAlpha * (1.f - dstAlpha));
1290 // Blends the RGB channels.
1291 const bool isOutputAlphaZero = outputColor[alphaIndex] < Dali::Math::MACHINE_EPSILON_1000;
1292 if( isOutputAlphaZero )
1294 std::fill( outputColor, outputColor + bytesPerPixel, 0.f );
1298 const float srcAlphaOverOutputAlpha = srcAlpha / outputColor[alphaIndex]; // fgAlpha / alpha
1299 const float dstAlphaOneMinusSrcAlphaOverOutputAlpha = dstAlpha * oneMinusSrcAlpha / outputColor[alphaIndex]; // bgAlpha * ( 1 - fgAlpha ) / alpha
1300 for (unsigned int index = 0u; index < alphaIndex; ++index)
1302 const float dstComponent = TO_FLOAT * static_cast<float>( *( dstBuffer + i * bytesPerPixel + index ) ) * dstAlpha;
1303 const float srcComponent = TO_FLOAT * static_cast<float>(*(srcBuffer + bytesPerPixel * (srcLineIndex + i) + index) ) * srcAlpha;
1304 outputColor[index] = ( srcComponent * srcAlphaOverOutputAlpha ) + ( dstComponent * dstAlphaOneMinusSrcAlphaOverOutputAlpha );
1308 for (unsigned int index = 0u; index < bytesPerPixel; ++index)
1310 *(dstBuffer + i * bytesPerPixel + index) = static_cast<unsigned char>( TO_UCHAR * outputColor[index] );
1314 dstBuffer += dstWidthBytes;
1319 } // namespace DevelText
1321 } // namespace Toolkit