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 struct InternalDataModel
109 InternalDataModel( FontClient& fontClient,
111 Text::ModelPtr textModel )
112 : fontClient( fontClient ),
114 textModel( textModel ),
115 numberOfCharacters{ 0u },
116 isTextMirrored{ false },
119 layoutEngine.SetMetrics( metrics );
122 FontClient& fontClient;
124 Text::Layout::Engine layoutEngine; ///< The layout engine.
125 Text::ModelPtr textModel; ///< Pointer to the text's model.
126 Vector<ColorBlendingMode> blendingMode; ///< How embedded items and bitmap font glyphs are blended with color text.
127 Vector<bool> isEmoji; ///< Whether the glyph is an emoji.
129 Vector<Character> mirroredUtf32Characters; // The utf32Characters Characters but mirrored if there are RTL text.
131 Length numberOfCharacters; // The number of characters (not glyphs!).
132 bool isTextMirrored ; // Whether the text has been mirrored.
134 Length numberOfGlyphs;
137 bool GetLayoutEnumeration(const Property::Value& propertyValue, DevelText::Layout::Type& layout)
139 return Scripting::GetEnumerationProperty(propertyValue, LAYOUT_TYPE_TABLE, LAYOUT_TYPE_TABLE_COUNT, layout);
142 bool GetCircularAlignmentEnumeration(const Property::Value& propertyValue, DevelText::CircularAlignment::Type& circularAlignment)
144 return Scripting::GetEnumerationProperty(propertyValue, CIRCULAR_ALIGNMENT_TYPE_TABLE, CIRCULAR_ALIGNMENT_TYPE_TABLE_COUNT, circularAlignment);
148 void ShapeTextPreprocess( const RendererParameters& textParameters, TextAbstraction::TextRenderer::Parameters& rendererParameters, InternalDataModel& internalDataModel )
151 MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
152 const uint8_t* utf8 = NULL; // pointer to the first character of the text (encoded in utf8)
153 Length textSize = 0u; // The length of the utf8 string.
155 Length& numberOfCharacters = internalDataModel.numberOfCharacters;
156 Vector<Character>& mirroredUtf32Characters = internalDataModel.mirroredUtf32Characters;
157 Text::ModelPtr& textModel = internalDataModel.textModel;
159 Vector<Character>& utf32Characters = textModel->mLogicalModel->mText; // Characters encoded in utf32.
160 Vector<LineBreakInfo>& lineBreakInfo = textModel->mLogicalModel->mLineBreakInfo; // The line break info.
161 Vector<ScriptRun>& scripts = textModel->mLogicalModel->mScriptRuns; // Charactes's script.
162 Vector<FontDescriptionRun>& fontDescriptionRuns = textModel->mLogicalModel->mFontDescriptionRuns; // Desired font descriptions.
163 Vector<FontRun>& validFonts = textModel->mLogicalModel->mFontRuns; // Validated fonts.
164 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = textModel->mLogicalModel->mBidirectionalParagraphInfo; // The bidirectional info per paragraph.
165 Vector<CharacterDirection>& directions = textModel->mLogicalModel->mCharacterDirections; // Character's directions.
166 Vector<ColorRun>& colorRuns = textModel->mLogicalModel->mColorRuns; // colors of the text.
168 // the default font's description.
169 FontDescription defaultFontDescription;
170 PointSize26Dot6 defaultPointSize = FontClient::DEFAULT_POINT_SIZE;
172 ////////////////////////////////////////////////////////////////////////////////
173 // Process the markup string if the mark-up processor is enabled.
174 ////////////////////////////////////////////////////////////////////////////////
176 MarkupProcessData markupProcessData( colorRuns,
178 textModel->mLogicalModel->mEmbeddedItems );
180 if (textParameters.markupEnabled)
182 ProcessMarkupString(textParameters.text, markupProcessData);
183 textSize = markupProcessData.markupProcessedText.size();
185 // This is a bit horrible but std::string returns a (signed) char*
186 utf8 = reinterpret_cast<const uint8_t*>(markupProcessData.markupProcessedText.c_str());
190 textSize = textParameters.text.size();
192 // This is a bit horrible but std::string returns a (signed) char*
193 utf8 = reinterpret_cast<const uint8_t*>(textParameters.text.c_str());
196 ////////////////////////////////////////////////////////////////////////////////
197 // Convert from utf8 to utf32
198 ////////////////////////////////////////////////////////////////////////////////
200 utf32Characters.Resize(textSize);
202 // Transform a text array encoded in utf8 into an array encoded in utf32.
203 // It returns the actual number of characters.
204 numberOfCharacters = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
205 utf32Characters.Resize( numberOfCharacters );
207 ////////////////////////////////////////////////////////////////////////////////
208 // Retrieve the Line and Word Break Info.
209 ////////////////////////////////////////////////////////////////////////////////
211 lineBreakInfo.Resize( numberOfCharacters, LINE_NO_BREAK );
213 SetLineBreakInfo( utf32Characters,
218 ////////////////////////////////////////////////////////////////////////////////
219 // Retrieve the script runs.
220 ////////////////////////////////////////////////////////////////////////////////
222 multilanguageSupport.SetScripts( utf32Characters,
227 // Check if there are emojis.
228 // If there are an RGBA8888 pixel format is needed.
229 for( const auto& run : scripts )
231 if( run.script == TextAbstraction::Script::EMOJI )
233 rendererParameters.pixelFormat = TextAbstraction::TextRenderer::Parameters::RGBA8888;
238 ////////////////////////////////////////////////////////////////////////////////
239 // Retrieve the font runs.
240 ////////////////////////////////////////////////////////////////////////////////
242 // Set the description font run with the given text parameters.
243 FontDescriptionRun fontDescriptionRun;
244 fontDescriptionRun.characterRun.characterIndex = 0u;
245 fontDescriptionRun.characterRun.numberOfCharacters = numberOfCharacters;
247 fontDescriptionRun.familyLength = 0u;
248 fontDescriptionRun.familyName = nullptr;
249 fontDescriptionRun.familyDefined = !textParameters.fontFamily.empty();
250 if( fontDescriptionRun.familyDefined )
252 // The allocated memory will be freed when the logical model is destroyed.
253 fontDescriptionRun.familyLength = textParameters.fontFamily.size();
254 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
255 memcpy( fontDescriptionRun.familyName, textParameters.fontFamily.c_str(), fontDescriptionRun.familyLength );
258 fontDescriptionRun.weightDefined = !textParameters.fontWeight.empty();
259 if( fontDescriptionRun.weightDefined )
261 fontDescriptionRun.weight = StringToWeight( textParameters.fontWeight.c_str() );
264 fontDescriptionRun.widthDefined = !textParameters.fontWidth.empty();
265 if( fontDescriptionRun.widthDefined )
267 fontDescriptionRun.width = StringToWidth( textParameters.fontWidth.c_str() );
270 fontDescriptionRun.slantDefined = !textParameters.fontSlant.empty();
271 if( fontDescriptionRun.slantDefined )
273 fontDescriptionRun.slant = StringToSlant( textParameters.fontSlant.c_str() );
276 fontDescriptionRun.sizeDefined = !EqualsZero( textParameters.fontSize );
277 if( fontDescriptionRun.sizeDefined )
279 fontDescriptionRun.size = static_cast<unsigned int>( textParameters.fontSize * TO_POINT_26_DOT_6 );
282 fontDescriptionRuns.PushBack( fontDescriptionRun );
284 // Validates the fonts. If there is a character with no assigned font it sets a default one.
285 // After this call, fonts are validated.
286 multilanguageSupport.ValidateFonts( utf32Characters,
289 defaultFontDescription,
295 ////////////////////////////////////////////////////////////////////////////////
296 // Retrieve the Bidirectional info.
297 ////////////////////////////////////////////////////////////////////////////////
299 bidirectionalInfo.Reserve( 1u );
301 SetBidirectionalInfo( utf32Characters,
308 const bool hasBidirectionalText = 0u != bidirectionalInfo.Count();
309 if( hasBidirectionalText )
311 // Only set the character directions if there is right to left characters.
312 GetCharactersDirection( bidirectionalInfo,
318 // This paragraph has right to left text. Some characters may need to be mirrored.
319 // TODO: consider if the mirrored string can be stored as well.
321 internalDataModel.isTextMirrored = GetMirroredText( utf32Characters,
326 mirroredUtf32Characters );
330 void ShapeText( TextAbstraction::TextRenderer::Parameters& rendererParameters, Vector<EmbeddedItemInfo>& embeddedItemLayout, InternalDataModel& internalDataModel )
332 Vector<GlyphIndex> newParagraphGlyphs; // Glyphs for the new paragraph characters.
333 const Length numberOfCharacters = internalDataModel.numberOfCharacters;
334 const bool isTextMirrored = internalDataModel.isTextMirrored;
335 Text::ModelPtr& textModel = internalDataModel.textModel;
336 const Vector<Character>& mirroredUtf32Characters = internalDataModel.mirroredUtf32Characters;
337 FontClient& fontClient = internalDataModel.fontClient;
338 const Vector<Character>& utf32Characters = textModel->mLogicalModel->mText; // Characters encoded in utf32.
339 const Vector<LineBreakInfo>& lineBreakInfo = textModel->mLogicalModel->mLineBreakInfo; // The line break info.
340 const Vector<ScriptRun>& scripts = textModel->mLogicalModel->mScriptRuns; // Charactes's script.
341 const Vector<FontRun>& validFonts = textModel->mLogicalModel->mFontRuns; // Validated fonts.
343 Vector<CharacterIndex>& glyphsToCharacters = textModel->mVisualModel->mGlyphsToCharacters; // Glyphs to character map.
344 Vector<Length>& charactersPerGlyph = textModel->mVisualModel->mCharactersPerGlyph; // Number of characters per glyph.
346 ////////////////////////////////////////////////////////////////////////////////
347 // Retrieve the glyphs. Text shaping
348 ////////////////////////////////////////////////////////////////////////////////
350 const Vector<Character>& textToShape = isTextMirrored ? mirroredUtf32Characters : utf32Characters;
352 newParagraphGlyphs.Reserve( 1u );
355 ShapeText( textToShape,
362 rendererParameters.glyphs,
365 newParagraphGlyphs );
367 // Create the 'number of glyphs' per character and the glyph to character conversion tables.
368 textModel->mVisualModel->CreateGlyphsPerCharacterTable( 0u, 0u, numberOfCharacters );
369 textModel->mVisualModel->CreateCharacterToGlyphTable( 0u, 0u, numberOfCharacters );
371 internalDataModel.numberOfGlyphs = rendererParameters.glyphs.Count();
373 // Once the text has been shaped and the glyphs created it's possible to replace the font id of those glyphs
374 // that represent an image or an item and create the embedded item layout info.
375 // Note: the position of the embedded item can't be set until the text is laid-out.
376 embeddedItemLayout.Reserve( textModel->mLogicalModel->mEmbeddedItems.Count() );
377 for( const auto& item : textModel->mLogicalModel->mEmbeddedItems )
379 // Get the glyph that matches with the character index.
380 const GlyphIndex glyphIndex = textModel->mVisualModel->mCharactersToGlyph[item.characterIndex];
381 GlyphInfo& glyph = rendererParameters.glyphs[glyphIndex];
384 Pixel::Format pixelFormat = Pixel::A8;
385 TextAbstraction::FontClient::EmbeddedItemDescription description = { std::string( item.url, item.urlLength ), item.width, item.height, item.colorBlendingMode };
386 glyph.index = fontClient.CreateEmbeddedItem( description, pixelFormat ); // Set here an index to an item.
388 if( ( Pixel::RGBA8888 == pixelFormat ) || ( Pixel::BGRA8888 == pixelFormat ) )
390 rendererParameters.pixelFormat = TextAbstraction::TextRenderer::Parameters::RGBA8888;
393 // 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.
394 if( description.url.empty() )
396 EmbeddedItemInfo embeddedInfo =
401 Size( static_cast<float>( item.width ), static_cast<float>( item.height ) ),
402 Size( static_cast<float>( item.width ), static_cast<float>( item.height ) ),
404 item.colorBlendingMode
407 embeddedItemLayout.PushBack( embeddedInfo );
412 void SetColorSegmentation( const RendererParameters& textParameters, InternalDataModel& internalDataModel )
415 Text::ModelPtr& textModel = internalDataModel.textModel;
416 Vector<ColorBlendingMode>& blendingMode = internalDataModel.blendingMode;
418 Vector<ColorRun>& colorRuns = textModel->mLogicalModel->mColorRuns; // colors of the text.
420 Vector<GlyphIndex>& charactersToGlyph = textModel->mVisualModel->mCharactersToGlyph; // Characters to glyphs map.
421 Vector<Length>& glyphsPerCharacter = textModel->mVisualModel->mGlyphsPerCharacter; // The number of glyphs that are shaped.
423 ////////////////////////////////////////////////////////////////////////////////
424 // Set the color runs in glyphs.
425 ////////////////////////////////////////////////////////////////////////////////
427 SetColorSegmentationInfo( colorRuns,
432 internalDataModel.numberOfCharacters,
433 textModel->mVisualModel->mColors,
434 textModel->mVisualModel->mColorIndices );
436 // Insert the default color at the beginning of the vector.
437 textModel->mVisualModel->mColors.Insert( textModel->mVisualModel->mColors.Begin(),textParameters.textColor );
439 // Set how the embedded items are blended with text color.
440 blendingMode.Resize( internalDataModel.numberOfGlyphs, textParameters.isTextColorSet ? ColorBlendingMode::MULTIPLY : ColorBlendingMode::NONE );
442 if( !textParameters.isTextColorSet )
444 // Traverse the color runs.
445 for( const auto& run : colorRuns )
447 const GlyphIndex firstGlyph = textModel->mVisualModel->mCharactersToGlyph[run.characterRun.characterIndex];
448 const CharacterIndex lastCharacter = run.characterRun.characterIndex + run.characterRun.numberOfCharacters - 1u;
449 const GlyphIndex lastGlyphPlusOne = textModel->mVisualModel->mCharactersToGlyph[lastCharacter] + textModel->mVisualModel->mGlyphsPerCharacter[lastCharacter];
451 for( GlyphIndex index = firstGlyph; index < lastGlyphPlusOne; ++index )
453 blendingMode[index] = ColorBlendingMode::MULTIPLY;
458 // Traverse the embedded items and update the blending mode vector.
459 for( const auto& item : textModel->mLogicalModel->mEmbeddedItems )
461 const GlyphIndex glyphIndex = textModel->mVisualModel->mCharactersToGlyph[item.characterIndex];
462 blendingMode[glyphIndex] = item.colorBlendingMode;
466 void SetEmojiVector( InternalDataModel& internalDataModel )
468 Vector<bool>& isEmoji = internalDataModel.isEmoji;
469 Text::ModelPtr& textModel = internalDataModel.textModel;
470 const Length numberOfGlyphs = internalDataModel.numberOfGlyphs;
472 const Vector<ScriptRun>& scripts = textModel->mLogicalModel->mScriptRuns; // Charactes's script.
473 ////////////////////////////////////////////////////////////////////////////////
474 // Set the isEmoji Vector
475 ////////////////////////////////////////////////////////////////////////////////
477 isEmoji.Resize( numberOfGlyphs, false );
479 for( const auto& run : scripts )
481 if( run.script == TextAbstraction::Script::EMOJI )
483 const GlyphIndex firstGlyph = textModel->mVisualModel->mCharactersToGlyph[run.characterRun.characterIndex];
484 const CharacterIndex lastCharacter = run.characterRun.characterIndex + run.characterRun.numberOfCharacters - 1u;
485 const GlyphIndex lastGlyphPlusOne = textModel->mVisualModel->mCharactersToGlyph[lastCharacter] + textModel->mVisualModel->mGlyphsPerCharacter[lastCharacter];
487 for( GlyphIndex index = firstGlyph; index < lastGlyphPlusOne; ++index )
489 isEmoji[index] = true;
496 void Align( const RendererParameters& textParameters, TextAbstraction::TextRenderer::Parameters& rendererParameters,
497 Vector<EmbeddedItemInfo>& embeddedItemLayout, InternalDataModel& internalDataModel,
498 Size& textLayoutArea, const Size& newLayoutSize,
499 const bool isCircularTextLayout, const bool isClockwise,
500 HorizontalAlignment::Type horizontalAlignment, VerticalAlignment::Type verticalAlignment, CircularAlignment::Type circularAlignment,
501 const unsigned int radius )
503 Text::Layout::Engine& layoutEngine = internalDataModel.layoutEngine;
504 Text::ModelPtr& textModel = internalDataModel.textModel;
505 const Length numberOfCharacters = internalDataModel.numberOfCharacters;
507 Vector<LineRun>& lines = textModel->mVisualModel->mLines; // The laid out lines.
509 ////////////////////////////////////////////////////////////////////////////////
511 ////////////////////////////////////////////////////////////////////////////////
513 // Retrieve the line of text to know the direction and the width. @todo multi-line
514 const LineRun& line = lines[0u];
516 if( isCircularTextLayout )
518 // Set the circular alignment.
519 rendererParameters.circularLayout = isClockwise ? TextRenderer::Parameters::CLOCKWISE : TextRenderer::Parameters::COUNTER_CLOCKWISE;
521 // Update the text's height to be used by the ellipsis code.
522 textLayoutArea.height = newLayoutSize.height;
524 // Set the size of the text laid out on a straight horizontal line.
525 rendererParameters.circularWidth = static_cast<unsigned int>( newLayoutSize.width );
526 rendererParameters.circularHeight = static_cast<unsigned int>( newLayoutSize.height );
528 // Calculate the center of the circular text according the horizontal and vertical alingments and the radius.
529 switch( horizontalAlignment )
531 case HorizontalAlignment::BEGIN:
533 rendererParameters.centerX = static_cast<int>(textParameters.radius);
536 case HorizontalAlignment::CENTER:
538 rendererParameters.centerX = static_cast<int>( textParameters.textWidth / 2u );
541 case HorizontalAlignment::END:
543 rendererParameters.centerX = static_cast<int>( textParameters.textWidth ) - static_cast<int>(textParameters.radius);
548 switch( verticalAlignment )
550 case VerticalAlignment::TOP:
552 rendererParameters.centerY = static_cast<int>(textParameters.radius);
555 case VerticalAlignment::CENTER:
557 rendererParameters.centerY = static_cast<int>( textParameters.textHeight / 2u );
560 case VerticalAlignment::BOTTOM:
562 rendererParameters.centerY = static_cast<int>( textParameters.textHeight ) - static_cast<int>(textParameters.radius);
567 // Calculate the beginning angle according with the given horizontal alignment.
568 const bool isRTL = RTL == line.direction;
570 CircularAlignment::Type alignment = circularAlignment;
573 // Swap the alignment type if the line is right to left.
576 case CircularAlignment::BEGIN:
578 alignment = CircularAlignment::END;
581 case CircularAlignment::CENTER:
586 case CircularAlignment::END:
588 alignment = CircularAlignment::BEGIN;
594 float angleOffset = 0.f;
598 case CircularAlignment::BEGIN:
603 case CircularAlignment::CENTER:
605 const bool isNeg = textParameters.incrementAngle < 0.f;
606 const float textWidth = static_cast<float>( rendererParameters.circularWidth );
607 angleOffset = ( isNeg ? -0.5f : 0.5f ) * ( textLayoutArea.width - textWidth ) / static_cast<float>( radius );
610 case CircularAlignment::END:
612 const bool isNeg = textParameters.incrementAngle < 0.f;
613 const float textWidth = static_cast<float>( rendererParameters.circularWidth );
614 angleOffset = ( isNeg ? -1.f : 1.f ) * ( textLayoutArea.width - textWidth ) / static_cast<float>( radius );
619 // Update the beginning angle with the calculated offset.
620 rendererParameters.beginAngle = Radian( Degree( textParameters.beginAngle ) ) + angleOffset;
622 // Set the vertical position of the glyphs.
623 for( auto& position : rendererParameters.positions )
630 // Calculate the vertical offset according with the given alignment.
633 switch( verticalAlignment )
635 case VerticalAlignment::TOP:
637 penY = line.ascender;
640 case VerticalAlignment::CENTER:
642 penY = line.ascender + 0.5f * ( textLayoutArea.height - ( line.ascender - line.descender ) );
645 case VerticalAlignment::BOTTOM:
647 penY = textLayoutArea.height;
652 // Calculate the horizontal offset according with the given alignment.
653 float alignmentOffset = 0.f;
654 layoutEngine.Align( textLayoutArea,
660 Dali::LayoutDirection::LEFT_TO_RIGHT,
663 // Update the position of the glyphs with the calculated offsets.
664 for( auto& position : rendererParameters.positions )
666 position.x += line.alignmentOffset;
671 // Cairo adds the bearing to the position of the glyph
672 // that has already been added by the DALi's layout engine,
673 // so it's needed to be removed here.
674 for( unsigned int index = 0u; index < rendererParameters.glyphs.Count(); ++index )
676 const GlyphInfo& glyph = rendererParameters.glyphs[index];
677 Vector2& position = rendererParameters.positions[index];
679 position.x -= glyph.xBearing;
682 // Set the position of the embedded items (if there is any).
683 EmbeddedItemInfo* embeddedItemLayoutBuffer = embeddedItemLayout.Begin();
685 for( Length index = 0u, endIndex = embeddedItemLayout.Count(); index < endIndex; ++index )
687 EmbeddedItemInfo& embeddedItem = *( embeddedItemLayoutBuffer + index );
689 embeddedItem.position = rendererParameters.positions[embeddedItem.glyphIndex];
691 if( isCircularTextLayout )
693 // Calculate the new position of the embedded item in the circular path.
695 // Center of the bitmap.
696 const float halfWidth = 0.5f * embeddedItem.size.width;
697 const float halfHeight = 0.5f * embeddedItem.size.height;
698 double centerX = static_cast<double>( embeddedItem.position.x + halfWidth );
699 double centerY = static_cast<double>(embeddedItem.position.y - halfHeight);
701 Dali::TextAbstraction::CircularTextParameters circularTextParameters;
703 circularTextParameters.radius = static_cast<double>( radius );
704 circularTextParameters.invRadius = 1.0 / circularTextParameters.radius;
705 circularTextParameters.beginAngle = static_cast<double>( -rendererParameters.beginAngle + Dali::Math::PI_2 );
706 circularTextParameters.centerX = 0.5f * static_cast<double>( textParameters.textWidth );
707 circularTextParameters.centerY = 0.5f * static_cast<double>( textParameters.textHeight );
709 // Calculate the rotation angle.
710 float radians = rendererParameters.beginAngle;
713 radians += static_cast<float>( circularTextParameters.invRadius * centerX );
718 radians -= static_cast<float>( circularTextParameters.invRadius * centerX );
719 radians = -radians + Dali::Math::PI;
721 embeddedItem.angle = Degree( Radian( radians ) );
723 Dali::TextAbstraction::TransformToArc( circularTextParameters, centerX, centerY );
725 // Recalculate the size of the embedded item after the rotation to position it correctly.
726 float width = embeddedItem.size.width;
727 float height = embeddedItem.size.height;
729 // Transform the input angle into the range [0..2PI]
730 radians = fmod( radians, TWO_PI );
731 radians += ( radians < 0.f ) ? TWO_PI : 0.f;
733 // Does the same operations than rotate by shear.
734 if( ( radians > Math::PI_4 ) && ( radians <= RAD_135 ) )
736 std::swap( width, height );
737 radians -= Math::PI_2;
739 else if( ( radians > RAD_135 ) && ( radians <= RAD_225 ) )
743 else if( ( radians > RAD_225 ) && ( radians <= RAD_315 ) )
745 std::swap( width, height );
749 if( fabs( radians ) > Dali::Math::MACHINE_EPSILON_10 )
751 const float angleSinus = fabs( sin( radians ) );
752 const float angleCosinus = cos( radians );
754 // Calculate the rotated image dimensions.
755 embeddedItem.rotatedSize.height = width * angleSinus + height * angleCosinus;
756 embeddedItem.rotatedSize.width = height * angleSinus + width * angleCosinus + 1.f;
759 embeddedItem.position.x = floor( static_cast<float>( centerX ) - 0.5f * embeddedItem.rotatedSize.width );
760 embeddedItem.position.y = floor( static_cast<float>( centerY ) - 0.5f * embeddedItem.rotatedSize.height );
764 embeddedItem.position.y -= embeddedItem.size.height;
770 void Ellipsis( const RendererParameters& textParameters, TextAbstraction::TextRenderer::Parameters& rendererParameters,
771 Vector<EmbeddedItemInfo>& embeddedItemLayout, Size& textLayoutArea, InternalDataModel& internalDataModel )
773 Text::ModelPtr& textModel = internalDataModel.textModel;
774 FontClient& fontClient = internalDataModel.fontClient;
776 Vector<LineRun>& lines = textModel->mVisualModel->mLines; // The laid out lines.
777 Vector<bool>& isEmoji = internalDataModel.isEmoji;
779 ////////////////////////////////////////////////////////////////////////////////
780 // Ellipsis the text.
781 ////////////////////////////////////////////////////////////////////////////////
783 if( textParameters.ellipsisEnabled )
785 const LineRun& line = lines[0u]; // TODO: multi-line
789 Length finalNumberOfGlyphs = 0u;
791 if( ( line.ascender - line.descender ) > textLayoutArea.height )
793 // The height of the line is bigger than the height of the text area.
794 // Show the ellipsis glyph even if it doesn't fit in the text area.
795 // If no text is rendered then issues are rised and it may be a while
796 // until is find out that the text area is too small.
798 // Get the first glyph which is going to be replaced and the ellipsis glyph.
799 GlyphInfo& glyphInfo = rendererParameters.glyphs[0u];
800 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphInfo.fontId ) );
802 // Change the 'x' and 'y' position of the ellipsis glyph.
803 Vector2& position = rendererParameters.positions[0u];
804 position.x = ellipsisGlyph.xBearing;
805 position.y = textLayoutArea.height - ellipsisGlyph.yBearing;
807 // Replace the glyph by the ellipsis glyph.
808 glyphInfo = ellipsisGlyph;
810 // Set the final number of glyphs
811 finalNumberOfGlyphs = 1u;
816 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
817 float firstPenX = 0.f; // Used if rtl text is elided.
818 bool firstPenSet = false;
820 // Add the ellipsis glyph.
821 bool inserted = false;
822 float removedGlypsWidth = 0.f;
823 Length numberOfRemovedGlyphs = 0u;
824 if (line.glyphRun.numberOfGlyphs > 0u)
826 GlyphIndex index = line.glyphRun.numberOfGlyphs - 1u;
828 GlyphInfo* glyphs = rendererParameters.glyphs.Begin();
829 Vector2* glyphPositions = rendererParameters.positions.Begin();
833 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
836 const GlyphInfo& glyphToRemove = *( glyphs + index );
838 if( 0u != glyphToRemove.fontId )
840 // i.e. The font id of the glyph shaped from the '\n' character is zero.
842 // Need to reshape the glyph as the font may be different in size.
843 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
847 const Vector2& position = *( glyphPositions + index );
849 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
852 // Calculates the first penX which will be used if rtl text is elided.
853 firstPenX = position.x - glyphToRemove.xBearing;
854 if( firstPenX < -ellipsisGlyph.xBearing )
856 // Avoids to exceed the bounding box when rtl text is elided.
857 firstPenX = -ellipsisGlyph.xBearing;
860 removedGlypsWidth = -ellipsisGlyph.xBearing;
865 removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
867 // Calculate the width of the ellipsis glyph and check if it fits.
868 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
869 if( ellipsisGlyphWidth < removedGlypsWidth )
871 GlyphInfo& glyphInfo = *( glyphs + index );
872 Vector2& position = *( glyphPositions + index );
873 position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
875 // Replace the glyph by the ellipsis glyph.
876 glyphInfo = ellipsisGlyph;
878 // Update the isEmoji vector
879 isEmoji[index] = false;
881 // Change the 'x' and 'y' position of the ellipsis glyph.
883 if( position.x > firstPenX )
885 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
888 position.x += ellipsisGlyph.xBearing;
903 // No space for the ellipsis.
906 ++numberOfRemovedGlyphs;
909 // Set the final number of glyphs
910 finalNumberOfGlyphs = line.glyphRun.numberOfGlyphs - numberOfRemovedGlyphs;
914 // Resize the number of glyphs/positions
915 rendererParameters.glyphs.Resize( finalNumberOfGlyphs );
916 rendererParameters.positions.Resize( finalNumberOfGlyphs );
918 // Remove from the embedded items those exceding the last laid out glyph.
919 embeddedItemLayout.Erase( std::remove_if( embeddedItemLayout.Begin(),
920 embeddedItemLayout.End(),
921 [finalNumberOfGlyphs]( const EmbeddedItemInfo& item )
923 return item.glyphIndex >= finalNumberOfGlyphs;
925 embeddedItemLayout.End() );
931 Size LayoutText( const RendererParameters& textParameters, TextAbstraction::TextRenderer::Parameters& rendererParameters,
932 Vector<EmbeddedItemInfo>& embeddedItemLayout, InternalDataModel& internalDataModel )
934 ////////////////////////////////////////////////////////////////////////////////
936 ////////////////////////////////////////////////////////////////////////////////
937 Text::ModelPtr& textModel = internalDataModel.textModel;
938 Text::Layout::Engine& layoutEngine = internalDataModel.layoutEngine;
939 FontClient& fontClient = internalDataModel.fontClient;
940 const Length numberOfGlyphs = internalDataModel.numberOfGlyphs;
941 const bool isTextMirrored = internalDataModel.isTextMirrored;
942 const Vector<Character>& mirroredUtf32Characters = internalDataModel.mirroredUtf32Characters;
943 const Length numberOfCharacters = internalDataModel.numberOfCharacters;
945 // Sets the alignment
946 HorizontalAlignment::Type horizontalAlignment = Toolkit::HorizontalAlignment::CENTER;
947 HorizontalAlignment::Type horizontalCircularAlignment = Toolkit::HorizontalAlignment::CENTER;
948 VerticalAlignment::Type verticalAlignment = VerticalAlignment::CENTER;
949 Layout::Type layout = Layout::SINGLELINE;
950 CircularAlignment::Type circularAlignment = CircularAlignment::BEGIN;
952 Property::Value horizontalAlignmentStr( textParameters.horizontalAlignment );
953 GetHorizontalAlignmentEnumeration( horizontalAlignmentStr, horizontalAlignment );
954 horizontalCircularAlignment = horizontalAlignment;
956 Property::Value verticalAlignmentStr( textParameters.verticalAlignment );
957 GetVerticalAlignmentEnumeration( verticalAlignmentStr, verticalAlignment );
959 Property::Value layoutStr( textParameters.layout );
960 GetLayoutEnumeration( layoutStr, layout );
962 Property::Value circularAlignmentStr( textParameters.circularAlignment );
963 GetCircularAlignmentEnumeration( circularAlignmentStr, circularAlignment );
965 // Whether the layout is multi-line.
966 const Text::Layout::Engine::Type horizontalLayout = ( Layout::MULTILINE == layout ) ? Text::Layout::Engine::MULTI_LINE_BOX : Text::Layout::Engine::SINGLE_LINE_BOX;
967 layoutEngine.SetLayout( horizontalLayout ); // TODO: multi-line.
970 // Whether the layout is circular.
971 const bool isCircularTextLayout = (Layout::CIRCULAR == layout);
972 const bool isClockwise = isCircularTextLayout && ( 0.f < textParameters.incrementAngle );
974 // Calculates the max ascender or the max descender.
975 // Is used to calculate the radius of the base line of the text.
976 float maxAscenderDescender = 0.f;
977 if( isCircularTextLayout )
979 FontId currentFontId = 0u;
980 for( const auto& glyph : rendererParameters.glyphs )
982 if( currentFontId != glyph.fontId )
984 currentFontId = glyph.fontId;
986 fontClient.GetFontMetrics(currentFontId, metrics);
987 maxAscenderDescender = std::max( maxAscenderDescender, isClockwise ? metrics.ascender : metrics.descender );
991 const unsigned int radius = textParameters.radius - static_cast<unsigned int>( maxAscenderDescender );
993 // Convert CircularAlignment to HorizontalAlignment.
994 if( isCircularTextLayout )
996 switch( circularAlignment )
998 case CircularAlignment::BEGIN:
1000 horizontalCircularAlignment = Toolkit::HorizontalAlignment::BEGIN;
1003 case CircularAlignment::CENTER:
1005 horizontalCircularAlignment = Toolkit::HorizontalAlignment::CENTER;
1008 case CircularAlignment::END:
1010 horizontalCircularAlignment = Toolkit::HorizontalAlignment::END;
1016 // Set the layout parameters.
1017 Size textLayoutArea( static_cast<float>( textParameters.textWidth ),
1018 static_cast<float>( textParameters.textHeight ) );
1020 if( isCircularTextLayout )
1022 // In a circular layout, the length of the text area depends on the radius.
1023 rendererParameters.radius = radius;
1024 textLayoutArea.width = fabs( Radian( Degree( textParameters.incrementAngle ) ) * static_cast<float>( rendererParameters.radius ) );
1026 // Resize the vector of positions to have the same size than the vector of glyphs.
1027 rendererParameters.positions.Resize( numberOfGlyphs );
1031 textModel->mHorizontalAlignment = isCircularTextLayout ? horizontalCircularAlignment : horizontalAlignment;
1032 textModel->mLineWrapMode = LineWrap::WORD;
1033 textModel->mIgnoreSpacesAfterText = false;
1034 textModel->mMatchSystemLanguageDirection = false;
1035 Text::Layout::Parameters layoutParameters( textLayoutArea,
1039 // Whether the last character is a new paragraph character.
1040 const Vector<Character>& textToShape = isTextMirrored ? mirroredUtf32Characters : textModel->mLogicalModel->mText;
1041 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( textToShape[numberOfCharacters - 1u] );
1043 // The initial glyph and the number of glyphs to layout.
1044 layoutParameters.startGlyphIndex = 0u;
1045 layoutParameters.numberOfGlyphs = numberOfGlyphs;
1046 layoutParameters.startLineIndex = 0u;
1047 layoutParameters.estimatedNumberOfLines = 1u;
1048 layoutParameters.interGlyphExtraAdvance = 0.f;
1050 // Update the visual model.
1052 bool isAutoScrollEnabled = false;
1053 layoutEngine.LayoutText( layoutParameters,
1055 textParameters.ellipsisEnabled,
1056 isAutoScrollEnabled );
1058 ////////////////////////////////////////////////////////////////////////////////
1060 ////////////////////////////////////////////////////////////////////////////////
1061 Align( textParameters, rendererParameters, embeddedItemLayout, internalDataModel,
1062 textLayoutArea, newLayoutSize, isCircularTextLayout, isClockwise,
1063 horizontalAlignment, verticalAlignment, circularAlignment, radius );
1065 return textLayoutArea;
1070 Devel::PixelBuffer RenderText( const RendererParameters& textParameters, TextAbstraction::TextRenderer::Parameters& rendererParameters )
1072 ////////////////////////////////////////////////////////////////////////////////
1074 ////////////////////////////////////////////////////////////////////////////////
1076 rendererParameters.width = textParameters.textWidth;
1077 rendererParameters.height = textParameters.textHeight;
1079 TextAbstraction::TextRenderer renderer = TextAbstraction::TextRenderer::Get();
1080 return renderer.Render( rendererParameters );
1084 Devel::PixelBuffer Render( const RendererParameters& textParameters, Vector<EmbeddedItemInfo>& embeddedItemLayout )
1086 if( textParameters.text.empty() )
1088 Dali::Devel::PixelBuffer pixelBuffer = Dali::Devel::PixelBuffer::New( textParameters.textWidth,
1089 textParameters.textHeight,
1090 Dali::Pixel::RGBA8888 );
1092 const unsigned int bufferSize = textParameters.textWidth * textParameters.textHeight * Dali::Pixel::GetBytesPerPixel(Dali::Pixel::RGBA8888);
1093 unsigned char* buffer = pixelBuffer.GetBuffer();
1094 memset(buffer, 0, bufferSize);
1099 FontClient fontClient = FontClient::Get();
1101 // Use this to access FontClient i.e. to get down-scaled Emoji metrics.
1102 metrics = Metrics::New( fontClient );
1104 Text::ModelPtr textModel = Text::Model::New();
1105 InternalDataModel internalData( fontClient, metrics, textModel );
1107 TextAbstraction::TextRenderer::Parameters rendererParameters( internalData.textModel->mVisualModel->mGlyphs,
1108 internalData.textModel->mVisualModel->mGlyphPositions,
1109 internalData.textModel->mVisualModel->mColors,
1110 internalData.textModel->mVisualModel->mColorIndices,
1111 internalData.blendingMode,
1112 internalData.isEmoji );
1115 rendererParameters.width = textParameters.textWidth;
1116 rendererParameters.height = textParameters.textHeight;
1117 rendererParameters.pixelFormat = TextAbstraction::TextRenderer::Parameters::RGBA8888; // @note: At the moment all textures are generated RGBA8888
1120 ////////////////////////////////////////////////////////////////////////////////
1121 // Process the markup string if the mark-up processor is enabled.
1122 ////////////////////////////////////////////////////////////////////////////////
1123 ShapeTextPreprocess( textParameters, rendererParameters, internalData );
1125 ////////////////////////////////////////////////////////////////////////////////
1126 // Retrieve the glyphs. Text shaping
1127 ////////////////////////////////////////////////////////////////////////////////
1128 ShapeText( rendererParameters, embeddedItemLayout, internalData );
1131 ////////////////////////////////////////////////////////////////////////////////
1132 // Retrieve the glyph's metrics.
1133 ////////////////////////////////////////////////////////////////////////////////
1135 metrics->GetGlyphMetrics( rendererParameters.glyphs.Begin(), internalData.numberOfGlyphs );
1137 ////////////////////////////////////////////////////////////////////////////////
1138 // Set the color runs in glyphs.
1139 ////////////////////////////////////////////////////////////////////////////////
1140 SetColorSegmentation( textParameters, internalData );
1143 ////////////////////////////////////////////////////////////////////////////////
1144 // Set the isEmoji Vector
1145 ////////////////////////////////////////////////////////////////////////////////
1146 SetEmojiVector( internalData );
1148 ////////////////////////////////////////////////////////////////////////////////
1149 // Layout the text and Align the text
1150 ////////////////////////////////////////////////////////////////////////////////
1151 Size textLayoutArea = LayoutText( textParameters, rendererParameters, embeddedItemLayout, internalData );
1153 ////////////////////////////////////////////////////////////////////////////////
1154 // Ellipsis the text.
1155 ////////////////////////////////////////////////////////////////////////////////
1156 Ellipsis( textParameters, rendererParameters, embeddedItemLayout, textLayoutArea, internalData );
1158 ////////////////////////////////////////////////////////////////////////////////
1160 ////////////////////////////////////////////////////////////////////////////////
1161 return RenderText( textParameters, rendererParameters );
1165 Devel::PixelBuffer CreateShadow( const ShadowParameters& shadowParameters )
1167 // The size of the pixel data.
1168 const int width = static_cast<int>(shadowParameters.input.GetWidth());
1169 const int height = static_cast<int>(shadowParameters.input.GetHeight());
1171 // The shadow's offset.
1172 const int xOffset = static_cast<int>( shadowParameters.offset.x );
1173 const int yOffset = static_cast<int>( shadowParameters.offset.y );
1175 // The size in bytes of the pixel of the input's buffer.
1176 const Pixel::Format inputFormat = shadowParameters.input.GetPixelFormat();
1177 const unsigned int inputPixelSize = Pixel::GetBytesPerPixel( inputFormat );
1178 const bool isA8 = Pixel::A8 == inputFormat;
1180 // Creates the output pixel buffer.
1181 Devel::PixelBuffer outputPixelBuffer = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
1183 // Clear the output buffer
1184 unsigned char* outputPixelBufferPtr = outputPixelBuffer.GetBuffer();
1185 memset(outputPixelBufferPtr, 0, width * height * Pixel::GetBytesPerPixel(Pixel::RGBA8888));
1187 // Gets the buffer of the input pixel buffer.
1188 const unsigned char* const inputPixelBuffer = shadowParameters.input.GetBuffer();
1190 float textColor[4u];
1193 memcpy(textColor, shadowParameters.textColor.AsFloat(), 4u * sizeof(float));
1195 const float* const shadowColor = shadowParameters.color.AsFloat();
1197 // Traverse the input pixel buffer and write the text on the foreground and the shadow on the background.
1198 for (int rowIndex = 0; rowIndex < height; ++rowIndex)
1200 // Calculates the rowIndex to the input pixel buffer for the shadow and whether it's within the boundaries.
1201 const int yOffsetIndex = rowIndex - yOffset;
1202 const bool isValidRowIndex = ((yOffsetIndex >= 0) && (yOffsetIndex < height));
1204 const int rows = rowIndex * width;
1205 const int offsetRows = yOffsetIndex * width;
1206 for (int columnIndex = 0; columnIndex < width; ++columnIndex)
1208 // Index to the input buffer to retrieve the alpha value of the foreground text.
1209 const unsigned int index = inputPixelSize * static_cast<unsigned int>(rows + columnIndex);
1211 // Build the index to the input buffer to retrieve the alpha value of the background shadow.
1212 unsigned int shadowIndex = 0u;
1213 bool isValidShadowIndex = false;
1214 if (isValidRowIndex)
1216 const int xOffsetIndex = columnIndex - xOffset;
1217 isValidShadowIndex = ((xOffsetIndex >= 0) && (xOffsetIndex < width));
1219 if (isValidShadowIndex)
1221 shadowIndex = inputPixelSize * static_cast<unsigned int>(offsetRows + xOffsetIndex);
1225 // If the input buffer is an alpha mask, retrieve the values for the foreground text and the background shadow.
1226 // If not retrieve the color.
1227 float inputShadowOffsetAlphaValue = 1.f;
1228 float inputAlphaValue = 1.f;
1231 // Retrieve the alpha value for the shadow.
1232 inputShadowOffsetAlphaValue = isValidShadowIndex ? (static_cast<float>(*(inputPixelBuffer + shadowIndex)) / 255.f) : 0.f;
1234 // Retrieve the alpha value for the text.
1235 inputAlphaValue = static_cast<float>(*(inputPixelBuffer + index)) / 255.f;
1239 // The input buffer is not an alpha mask. Retrieve the color.
1240 textColor[0u] = TO_FLOAT * static_cast<float>( *(inputPixelBuffer + index + 0u) );
1241 textColor[1u] = TO_FLOAT * static_cast<float>( *(inputPixelBuffer + index + 1u) );
1242 textColor[2u] = TO_FLOAT * static_cast<float>( *(inputPixelBuffer + index + 2u) );
1243 textColor[3u] = TO_FLOAT * static_cast<float>( *(inputPixelBuffer + index + 3u) );
1244 inputAlphaValue = textColor[3u];
1245 inputShadowOffsetAlphaValue = isValidShadowIndex ? TO_FLOAT * static_cast<float>( *(inputPixelBuffer + shadowIndex + 3u) ) : 0.f;
1248 // Build the output color.
1249 float outputColor[4u];
1251 if( shadowParameters.blendShadow )
1253 // Blend the shadow's color with the text's color on top
1254 const float textAlpha = textColor[3u] * inputAlphaValue;
1255 const float shadowAlpha = shadowColor[3u] * inputShadowOffsetAlphaValue;
1257 // Blends the alpha.
1258 outputColor[3u] = 1.f - ((1.f - textAlpha) * (1.f - shadowAlpha));
1259 const bool isOutputAlphaZero = outputColor[3u] < Dali::Math::MACHINE_EPSILON_1000;
1260 if( isOutputAlphaZero )
1262 std::fill( outputColor, outputColor + 4u, 0.f );
1266 // Blends the RGB components.
1267 float shadowComponent = 0.f;
1268 float textComponent = 0.f;
1270 shadowComponent = shadowColor[0u] * inputShadowOffsetAlphaValue;
1271 textComponent = textColor[0u] * inputAlphaValue;
1272 outputColor[0u] = (textComponent * textAlpha / outputColor[3u]) + (shadowComponent * shadowAlpha * (1.f - textAlpha) / outputColor[3u]);
1274 shadowComponent = shadowColor[1u] * inputShadowOffsetAlphaValue;
1275 textComponent = textColor[1u] * inputAlphaValue;
1276 outputColor[1u] = (textComponent * textAlpha / outputColor[3u]) + (shadowComponent * shadowAlpha * (1.f - textAlpha) / outputColor[3u]);
1278 shadowComponent = shadowColor[2u] * inputShadowOffsetAlphaValue;
1279 textComponent = textColor[2u] * inputAlphaValue;
1280 outputColor[2u] = (textComponent * textAlpha / outputColor[3u]) + (shadowComponent * shadowAlpha * (1.f - textAlpha) / outputColor[3u]);
1286 std::fill( outputColor, outputColor + 4u, 0.f );
1288 const float textAlpha = textColor[3u];
1289 const float shadowAlpha = shadowColor[3u] * inputShadowOffsetAlphaValue;
1291 // Write shadow first.
1292 if( shadowAlpha > Dali::Math::MACHINE_EPSILON_1000 )
1294 outputColor[0u] = shadowColor[0u] * inputShadowOffsetAlphaValue;
1295 outputColor[1u] = shadowColor[1u] * inputShadowOffsetAlphaValue;
1296 outputColor[2u] = shadowColor[2u] * inputShadowOffsetAlphaValue;
1297 outputColor[3u] = shadowAlpha;
1300 // Write character on top.
1301 if( textAlpha > Dali::Math::MACHINE_EPSILON_1000 )
1303 outputColor[0u] = textColor[0u];
1304 outputColor[1u] = textColor[1u];
1305 outputColor[2u] = textColor[2u];
1306 outputColor[3u] = textAlpha;
1310 // Write the color into the output pixel buffer.
1311 const unsigned int outputIndex = 4u * (rows + columnIndex);
1312 *(outputPixelBufferPtr + outputIndex + 0u) = static_cast<unsigned char>( TO_UCHAR * outputColor[0u] );
1313 *(outputPixelBufferPtr + outputIndex + 1u) = static_cast<unsigned char>( TO_UCHAR * outputColor[1u] );
1314 *(outputPixelBufferPtr + outputIndex + 2u) = static_cast<unsigned char>( TO_UCHAR * outputColor[2u] );
1315 *(outputPixelBufferPtr + outputIndex + 3u) = static_cast<unsigned char>( TO_UCHAR * outputColor[3u] );
1319 // Returns the pixel buffer.
1320 return outputPixelBuffer;
1323 Devel::PixelBuffer ConvertToRgba8888(Devel::PixelBuffer pixelBuffer, const Vector4& color, bool multiplyByAlpha)
1325 if (Dali::Pixel::A8 != pixelBuffer.GetPixelFormat())
1331 const unsigned int width = pixelBuffer.GetWidth();
1332 const unsigned int height = pixelBuffer.GetHeight();
1333 Devel::PixelBuffer newPixelBuffer = Devel::PixelBuffer::New( width, height, Dali::Pixel::RGBA8888 );
1335 unsigned char* dstBuffer = newPixelBuffer.GetBuffer();
1336 const unsigned char* const srcBuffer = pixelBuffer.GetBuffer();
1338 const unsigned char r = static_cast<unsigned char>( TO_UCHAR * color.r );
1339 const unsigned char g = static_cast<unsigned char>( TO_UCHAR * color.g );
1340 const unsigned char b = static_cast<unsigned char>( TO_UCHAR * color.b );
1342 unsigned char dstColor[4];
1343 for( unsigned int j = 0u; j < height; ++j )
1345 const unsigned int lineIndex = j * width;
1346 for( unsigned int i=0u; i < width; ++i )
1348 const unsigned int srcIndex = lineIndex + i;
1350 const float srcAlpha = static_cast<float>( *( srcBuffer + srcIndex ) );
1352 if( multiplyByAlpha )
1354 dstColor[0u] = static_cast<unsigned char>( srcAlpha * color.r );
1355 dstColor[1u] = static_cast<unsigned char>( srcAlpha * color.g );
1356 dstColor[2u] = static_cast<unsigned char>( srcAlpha * color.b );
1357 dstColor[3u] = static_cast<unsigned char>( srcAlpha * color.a );
1364 dstColor[3u] = static_cast<unsigned char>( srcAlpha );
1367 const unsigned int dstIndex = srcIndex * 4u;
1368 memcpy( dstBuffer + dstIndex, dstColor, 4u );
1372 return newPixelBuffer;
1375 void UpdateBuffer(Devel::PixelBuffer src, Devel::PixelBuffer dst, unsigned int x, unsigned int y, bool blend)
1377 const Dali::Pixel::Format pixelFormat = dst.GetPixelFormat();
1378 if( src.GetPixelFormat() != pixelFormat )
1380 DALI_LOG_ERROR("PixelBuffer::SetBuffer. The pixel format of the new data must be the same of the current pixel buffer.");
1384 const unsigned int srcWidth = src.GetWidth();
1385 const unsigned int srcHeight = src.GetHeight();
1386 const unsigned int dstWidth = dst.GetWidth();
1387 const unsigned int dstHeight = dst.GetHeight();
1389 if( ( x > dstWidth ) ||
1390 ( y > dstHeight ) ||
1391 ( x + srcWidth > dstWidth ) ||
1392 ( y + srcHeight > dstHeight ) )
1394 DALI_LOG_ERROR("PixelBuffer::SetBuffer. The source pixel buffer is out of the boundaries of the destination pixel buffer.");
1398 const unsigned int bytesPerPixel = Dali::Pixel::GetBytesPerPixel(pixelFormat);
1399 if( bytesPerPixel == 0u || bytesPerPixel == 12u || bytesPerPixel == 24u )
1403 const unsigned int alphaIndex = bytesPerPixel - 1u;
1405 const unsigned char* const srcBuffer = src.GetBuffer();
1406 unsigned char* dstBuffer = dst.GetBuffer();
1410 const unsigned int currentLineSize = dstWidth * bytesPerPixel;
1411 const unsigned int newLineSize = srcWidth * bytesPerPixel;
1412 unsigned char* currentBuffer = dstBuffer + (y * dstWidth + x) * bytesPerPixel;
1413 for (unsigned int j = 0u; j < srcHeight; ++j)
1415 memcpy(currentBuffer + j * currentLineSize, srcBuffer + j * newLineSize, newLineSize);
1420 float outputColor[4u];
1422 // Blend the src pixel buffer with the dst pixel buffer as background.
1424 // fgColor, fgAlpha, bgColor, bgAlpha
1426 // alpha = 1 - ( 1 - fgAlpha ) * ( 1 - bgAlpha )
1427 // color = ( fgColor * fgAlpha / alpha ) + ( bgColor * bgAlpha * ( 1 - fgAlpha ) / alpha )
1429 // Jump till the 'x,y' position
1430 const unsigned int dstWidthBytes = dstWidth * bytesPerPixel;
1431 dstBuffer += ( y * dstWidthBytes + x * bytesPerPixel );
1433 for (unsigned int j = 0u; j < srcHeight; ++j)
1435 const unsigned int srcLineIndex = j * srcWidth;
1436 for (unsigned int i = 0u; i < srcWidth; ++i)
1438 const float srcAlpha = TO_FLOAT * static_cast<float>( *( srcBuffer + bytesPerPixel * ( srcLineIndex + i ) + alphaIndex ) );
1439 const float dstAlpha = TO_FLOAT * static_cast<float>( *(dstBuffer + i*bytesPerPixel + alphaIndex) );
1441 // Blends the alpha channel.
1442 const float oneMinusSrcAlpha = 1.f - srcAlpha;
1443 outputColor[alphaIndex] = 1.f - (oneMinusSrcAlpha * (1.f - dstAlpha));
1445 // Blends the RGB channels.
1446 const bool isOutputAlphaZero = outputColor[alphaIndex] < Dali::Math::MACHINE_EPSILON_1000;
1447 if( isOutputAlphaZero )
1449 std::fill( outputColor, outputColor + bytesPerPixel, 0.f );
1453 const float srcAlphaOverOutputAlpha = srcAlpha / outputColor[alphaIndex]; // fgAlpha / alpha
1454 const float dstAlphaOneMinusSrcAlphaOverOutputAlpha = dstAlpha * oneMinusSrcAlpha / outputColor[alphaIndex]; // bgAlpha * ( 1 - fgAlpha ) / alpha
1455 for (unsigned int index = 0u; index < alphaIndex; ++index)
1457 const float dstComponent = TO_FLOAT * static_cast<float>( *( dstBuffer + i * bytesPerPixel + index ) ) * dstAlpha;
1458 const float srcComponent = TO_FLOAT * static_cast<float>(*(srcBuffer + bytesPerPixel * (srcLineIndex + i) + index) ) * srcAlpha;
1459 outputColor[index] = ( srcComponent * srcAlphaOverOutputAlpha ) + ( dstComponent * dstAlphaOneMinusSrcAlphaOverOutputAlpha );
1463 for (unsigned int index = 0u; index < bytesPerPixel; ++index)
1465 *(dstBuffer + i * bytesPerPixel + index) = static_cast<unsigned char>( TO_UCHAR * outputColor[index] );
1469 dstBuffer += dstWidthBytes;
1474 } // namespace DevelText
1476 } // namespace Toolkit