2 * Copyright (c) 2022 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/scripting/enum-helper.h>
23 #include <dali/devel-api/text-abstraction/font-client.h>
24 #include <dali/devel-api/text-abstraction/text-renderer-layout-helper.h>
25 #include <dali/devel-api/text-abstraction/text-renderer.h>
26 #include <dali/integration-api/debug.h>
31 #include <dali-toolkit/internal/text/bidirectional-support.h>
32 #include <dali-toolkit/internal/text/character-set-conversion.h>
33 #include <dali-toolkit/internal/text/color-segmentation.h>
34 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
35 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
36 #include <dali-toolkit/internal/text/markup-processor.h>
37 #include <dali-toolkit/internal/text/multi-language-support.h>
38 #include <dali-toolkit/internal/text/segmentation.h>
39 #include <dali-toolkit/internal/text/shaper.h>
40 #include <dali-toolkit/internal/text/text-enumerations-impl.h>
41 #include <dali-toolkit/internal/text/text-font-style.h>
42 #include <dali-toolkit/internal/text/text-model.h>
43 #include <dali-toolkit/internal/text/glyph-metrics-helper.h>
47 using namespace TextAbstraction;
58 * @brief The text's layout.
62 SINGLELINE, ///< The text is laid out on a single line.
63 MULTILINE, ///< The text is laid out in multiple lines.
64 CIRCULAR, ///< The text is laid out on a single line following a circular path.
69 namespace CircularAlignment
72 * @brief The enumerations for the circular alignment.
76 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).
77 CENTER, ///< The text is centered within the arc.
78 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).
81 } // namespace CircularAlignment
83 const float TO_POINT_26_DOT_6 = 64.f;
84 const float TO_FLOAT = 1.f / 255.f;
85 const float TO_UCHAR = 255.f;
86 const bool RTL = true;
87 const float TWO_PI = 2.f * Dali::Math::PI; ///< 360 degrees in radians
88 const float RAD_135 = Math::PI_2 + Math::PI_4; ///< 135 degrees in radians;
89 const float RAD_225 = RAD_135 + Math::PI_2; ///< 225 degrees in radians;
90 const float RAD_270 = 3.f * Math::PI_2; ///< 270 degrees in radians;
91 const float RAD_315 = RAD_225 + Math::PI_2; ///< 315 degrees in radians;
92 const float MAX_INT = std::numeric_limits<int>::max();
94 DALI_ENUM_TO_STRING_TABLE_BEGIN(LAYOUT_TYPE)
95 DALI_ENUM_TO_STRING_WITH_SCOPE(DevelText::Layout, SINGLELINE)
96 DALI_ENUM_TO_STRING_WITH_SCOPE(DevelText::Layout, MULTILINE)
97 DALI_ENUM_TO_STRING_WITH_SCOPE(DevelText::Layout, CIRCULAR)
98 DALI_ENUM_TO_STRING_TABLE_END(LAYOUT_TYPE)
100 DALI_ENUM_TO_STRING_TABLE_BEGIN(CIRCULAR_ALIGNMENT_TYPE)
101 DALI_ENUM_TO_STRING_WITH_SCOPE(DevelText::CircularAlignment, BEGIN)
102 DALI_ENUM_TO_STRING_WITH_SCOPE(DevelText::CircularAlignment, CENTER)
103 DALI_ENUM_TO_STRING_WITH_SCOPE(DevelText::CircularAlignment, END)
104 DALI_ENUM_TO_STRING_TABLE_END(CIRCULAR_ALIGNMENT_TYPE)
106 struct InternalDataModel
108 InternalDataModel(FontClient& fontClient,
110 Text::ModelPtr textModel)
111 : fontClient(fontClient),
113 textModel(textModel),
114 numberOfCharacters{0u},
115 isTextMirrored{false},
118 layoutEngine.SetMetrics(metrics);
121 FontClient& fontClient;
123 Text::Layout::Engine layoutEngine; ///< The layout engine.
124 Text::ModelPtr textModel; ///< Pointer to the text's model.
125 Vector<ColorBlendingMode> blendingMode; ///< How embedded items and bitmap font glyphs are blended with color text.
126 Vector<bool> isEmoji; ///< Whether the glyph is an emoji.
128 Vector<Character> mirroredUtf32Characters; // The utf32Characters Characters but mirrored if there are RTL text.
130 Length numberOfCharacters; // The number of characters (not glyphs!).
131 bool isTextMirrored; // Whether the text has been mirrored.
133 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);
147 void ShapeTextPreprocess(const RendererParameters& textParameters, TextAbstraction::TextRenderer::Parameters& rendererParameters, InternalDataModel& internalDataModel)
149 MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
150 const uint8_t* utf8 = NULL; // pointer to the first character of the text (encoded in utf8)
151 Length textSize = 0u; // The length of the utf8 string.
153 Length& numberOfCharacters = internalDataModel.numberOfCharacters;
154 Vector<Character>& mirroredUtf32Characters = internalDataModel.mirroredUtf32Characters;
155 Text::ModelPtr& textModel = internalDataModel.textModel;
157 Vector<Character>& utf32Characters = textModel->mLogicalModel->mText; // Characters encoded in utf32.
158 Vector<LineBreakInfo>& lineBreakInfo = textModel->mLogicalModel->mLineBreakInfo; // The line break info.
159 Vector<ScriptRun>& scripts = textModel->mLogicalModel->mScriptRuns; // Charactes's script.
160 Vector<FontDescriptionRun>& fontDescriptionRuns = textModel->mLogicalModel->mFontDescriptionRuns; // Desired font descriptions.
161 Vector<FontRun>& validFonts = textModel->mLogicalModel->mFontRuns; // Validated fonts.
162 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = textModel->mLogicalModel->mBidirectionalParagraphInfo; // The bidirectional info per paragraph.
163 Vector<CharacterDirection>& directions = textModel->mLogicalModel->mCharacterDirections; // Character's directions.
164 Vector<ColorRun>& colorRuns = textModel->mLogicalModel->mColorRuns; // colors of the text.
166 // the default font's description.
167 FontDescription defaultFontDescription;
168 PointSize26Dot6 defaultPointSize = FontClient::DEFAULT_POINT_SIZE;
170 ////////////////////////////////////////////////////////////////////////////////
171 // Process the markup string if the mark-up processor is enabled.
172 ////////////////////////////////////////////////////////////////////////////////
174 MarkupProcessData markupProcessData(colorRuns,
176 textModel->mLogicalModel->mEmbeddedItems,
177 textModel->mLogicalModel->mAnchors,
178 textModel->mLogicalModel->mUnderlinedCharacterRuns,
179 textModel->mLogicalModel->mBackgroundColorRuns,
180 textModel->mLogicalModel->mStrikethroughCharacterRuns);
182 if(textParameters.markupEnabled)
184 ProcessMarkupString(textParameters.text, markupProcessData);
185 textSize = markupProcessData.markupProcessedText.size();
187 // This is a bit horrible but std::string returns a (signed) char*
188 utf8 = reinterpret_cast<const uint8_t*>(markupProcessData.markupProcessedText.c_str());
192 textSize = textParameters.text.size();
194 // This is a bit horrible but std::string returns a (signed) char*
195 utf8 = reinterpret_cast<const uint8_t*>(textParameters.text.c_str());
198 ////////////////////////////////////////////////////////////////////////////////
199 // Convert from utf8 to utf32
200 ////////////////////////////////////////////////////////////////////////////////
202 utf32Characters.Resize(textSize);
204 // Transform a text array encoded in utf8 into an array encoded in utf32.
205 // It returns the actual number of characters.
206 numberOfCharacters = Utf8ToUtf32(utf8, textSize, utf32Characters.Begin());
207 utf32Characters.Resize(numberOfCharacters);
209 ////////////////////////////////////////////////////////////////////////////////
210 // Retrieve the Line and Word Break Info.
211 ////////////////////////////////////////////////////////////////////////////////
213 lineBreakInfo.Resize(numberOfCharacters, LINE_NO_BREAK);
215 SetLineBreakInfo(utf32Characters,
220 ////////////////////////////////////////////////////////////////////////////////
221 // Retrieve the script runs.
222 ////////////////////////////////////////////////////////////////////////////////
224 multilanguageSupport.SetScripts(utf32Characters,
229 // Check if there are emojis.
230 // If there are an RGBA8888 pixel format is needed.
231 for(const auto& run : scripts)
233 if(run.script == TextAbstraction::Script::EMOJI)
235 rendererParameters.pixelFormat = TextAbstraction::TextRenderer::Parameters::RGBA8888;
240 ////////////////////////////////////////////////////////////////////////////////
241 // Retrieve the font runs.
242 ////////////////////////////////////////////////////////////////////////////////
244 // Set the description font run with the given text parameters.
245 FontDescriptionRun fontDescriptionRun;
246 fontDescriptionRun.characterRun.characterIndex = 0u;
247 fontDescriptionRun.characterRun.numberOfCharacters = numberOfCharacters;
249 fontDescriptionRun.familyLength = 0u;
250 fontDescriptionRun.familyName = nullptr;
251 fontDescriptionRun.familyDefined = !textParameters.fontFamily.empty();
252 if(fontDescriptionRun.familyDefined)
254 // The allocated memory will be freed when the logical model is destroyed.
255 fontDescriptionRun.familyLength = textParameters.fontFamily.size();
256 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
257 memcpy(fontDescriptionRun.familyName, textParameters.fontFamily.c_str(), fontDescriptionRun.familyLength);
260 fontDescriptionRun.weightDefined = !textParameters.fontWeight.empty();
261 if(fontDescriptionRun.weightDefined)
263 fontDescriptionRun.weight = StringToWeight(textParameters.fontWeight.c_str());
266 fontDescriptionRun.widthDefined = !textParameters.fontWidth.empty();
267 if(fontDescriptionRun.widthDefined)
269 fontDescriptionRun.width = StringToWidth(textParameters.fontWidth.c_str());
272 fontDescriptionRun.slantDefined = !textParameters.fontSlant.empty();
273 if(fontDescriptionRun.slantDefined)
275 fontDescriptionRun.slant = StringToSlant(textParameters.fontSlant.c_str());
278 fontDescriptionRun.sizeDefined = !EqualsZero(textParameters.fontSize);
279 if(fontDescriptionRun.sizeDefined)
281 fontDescriptionRun.size = static_cast<unsigned int>(textParameters.fontSize * TO_POINT_26_DOT_6);
284 fontDescriptionRuns.PushBack(fontDescriptionRun);
286 // Validates the fonts. If there is a character with no assigned font it sets a default one.
287 // After this call, fonts are validated.
288 multilanguageSupport.ValidateFonts(utf32Characters,
291 defaultFontDescription,
297 ////////////////////////////////////////////////////////////////////////////////
298 // Retrieve the Bidirectional info.
299 ////////////////////////////////////////////////////////////////////////////////
301 bidirectionalInfo.Reserve(1u);
303 SetBidirectionalInfo(utf32Characters,
310 const bool hasBidirectionalText = 0u != bidirectionalInfo.Count();
311 if(hasBidirectionalText)
313 // Only set the character directions if there is right to left characters.
314 GetCharactersDirection(bidirectionalInfo,
320 // This paragraph has right to left text. Some characters may need to be mirrored.
321 // TODO: consider if the mirrored string can be stored as well.
323 internalDataModel.isTextMirrored = GetMirroredText(utf32Characters,
328 mirroredUtf32Characters);
332 void ShapeText(TextAbstraction::TextRenderer::Parameters& rendererParameters, Vector<EmbeddedItemInfo>& embeddedItemLayout, InternalDataModel& internalDataModel)
334 Vector<GlyphIndex> newParagraphGlyphs; // Glyphs for the new paragraph characters.
335 const Length numberOfCharacters = internalDataModel.numberOfCharacters;
336 const bool isTextMirrored = internalDataModel.isTextMirrored;
337 Text::ModelPtr& textModel = internalDataModel.textModel;
338 const Vector<Character>& mirroredUtf32Characters = internalDataModel.mirroredUtf32Characters;
339 FontClient& fontClient = internalDataModel.fontClient;
340 const Vector<Character>& utf32Characters = textModel->mLogicalModel->mText; // Characters encoded in utf32.
341 const Vector<LineBreakInfo>& lineBreakInfo = textModel->mLogicalModel->mLineBreakInfo; // The line break info.
342 const Vector<ScriptRun>& scripts = textModel->mLogicalModel->mScriptRuns; // Charactes's script.
343 const Vector<FontRun>& validFonts = textModel->mLogicalModel->mFontRuns; // Validated fonts.
345 Vector<CharacterIndex>& glyphsToCharacters = textModel->mVisualModel->mGlyphsToCharacters; // Glyphs to character map.
346 Vector<Length>& charactersPerGlyph = textModel->mVisualModel->mCharactersPerGlyph; // Number of characters per glyph.
348 ////////////////////////////////////////////////////////////////////////////////
349 // Retrieve the glyphs. Text shaping
350 ////////////////////////////////////////////////////////////////////////////////
352 const Vector<Character>& textToShape = isTextMirrored ? mirroredUtf32Characters : utf32Characters;
354 newParagraphGlyphs.Reserve(1u);
357 ShapeText(textToShape,
364 rendererParameters.glyphs,
369 // Create the 'number of glyphs' per character and the glyph to character conversion tables.
370 textModel->mVisualModel->CreateGlyphsPerCharacterTable(0u, 0u, numberOfCharacters);
371 textModel->mVisualModel->CreateCharacterToGlyphTable(0u, 0u, numberOfCharacters);
373 internalDataModel.numberOfGlyphs = rendererParameters.glyphs.Count();
375 // Once the text has been shaped and the glyphs created it's possible to replace the font id of those glyphs
376 // that represent an image or an item and create the embedded item layout info.
377 // Note: the position of the embedded item can't be set until the text is laid-out.
378 embeddedItemLayout.Reserve(textModel->mLogicalModel->mEmbeddedItems.Count());
379 for(const auto& item : textModel->mLogicalModel->mEmbeddedItems)
381 // Get the glyph that matches with the character index.
382 const GlyphIndex glyphIndex = textModel->mVisualModel->mCharactersToGlyph[item.characterIndex];
383 GlyphInfo& glyph = rendererParameters.glyphs[glyphIndex];
386 Pixel::Format pixelFormat = Pixel::A8;
387 TextAbstraction::FontClient::EmbeddedItemDescription description = {std::string(item.url, item.urlLength), item.width, item.height, item.colorBlendingMode};
388 glyph.index = fontClient.CreateEmbeddedItem(description, pixelFormat); // Set here an index to an item.
390 if((Pixel::RGBA8888 == pixelFormat) || (Pixel::BGRA8888 == pixelFormat))
392 rendererParameters.pixelFormat = TextAbstraction::TextRenderer::Parameters::RGBA8888;
395 // 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.
396 if(description.url.empty())
398 EmbeddedItemInfo embeddedInfo =
403 Size(static_cast<float>(item.width), static_cast<float>(item.height)),
404 Size(static_cast<float>(item.width), static_cast<float>(item.height)),
406 item.colorBlendingMode};
408 embeddedItemLayout.PushBack(embeddedInfo);
413 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;
495 void Align(const RendererParameters& textParameters, TextAbstraction::TextRenderer::Parameters& rendererParameters, Vector<EmbeddedItemInfo>& embeddedItemLayout, InternalDataModel& internalDataModel, const Size& newLayoutSize)
497 Text::Layout::Engine& layoutEngine = internalDataModel.layoutEngine;
498 Text::ModelPtr& textModel = internalDataModel.textModel;
499 const Length numberOfCharacters = internalDataModel.numberOfCharacters;
500 Size& textLayoutArea = internalDataModel.textLayoutArea;
502 Vector<LineRun>& lines = textModel->mVisualModel->mLines; // The laid out lines.
504 ////////////////////////////////////////////////////////////////////////////////
506 ////////////////////////////////////////////////////////////////////////////////
508 HorizontalAlignment::Type horizontalAlignment = Toolkit::HorizontalAlignment::CENTER;
509 HorizontalAlignment::Type horizontalCircularAlignment = Toolkit::HorizontalAlignment::CENTER;
510 VerticalAlignment::Type verticalAlignment = VerticalAlignment::CENTER;
511 CircularAlignment::Type circularAlignment = CircularAlignment::BEGIN;
513 Layout::Type layout = Layout::SINGLELINE;
515 // Sets the alignment
516 Property::Value horizontalAlignmentStr(textParameters.horizontalAlignment);
517 GetHorizontalAlignmentEnumeration(horizontalAlignmentStr, horizontalAlignment);
518 horizontalCircularAlignment = horizontalAlignment;
520 Property::Value verticalAlignmentStr(textParameters.verticalAlignment);
521 GetVerticalAlignmentEnumeration(verticalAlignmentStr, verticalAlignment);
523 Property::Value circularAlignmentStr(textParameters.circularAlignment);
524 GetCircularAlignmentEnumeration(circularAlignmentStr, circularAlignment);
526 Property::Value layoutStr(textParameters.layout);
527 GetLayoutEnumeration(layoutStr, layout);
529 // Whether the layout is circular.
530 const bool isCircularTextLayout = (Layout::CIRCULAR == layout);
531 const bool isClockwise = isCircularTextLayout && (0.f < textParameters.incrementAngle);
533 // Convert CircularAlignment to HorizontalAlignment.
534 if(isCircularTextLayout)
536 switch(circularAlignment)
538 case CircularAlignment::BEGIN:
540 horizontalCircularAlignment = Toolkit::HorizontalAlignment::BEGIN;
543 case CircularAlignment::CENTER:
545 horizontalCircularAlignment = Toolkit::HorizontalAlignment::CENTER;
548 case CircularAlignment::END:
550 horizontalCircularAlignment = Toolkit::HorizontalAlignment::END;
555 textModel->mHorizontalAlignment = isCircularTextLayout ? horizontalCircularAlignment : horizontalAlignment;
557 // Retrieve the line of text to know the direction and the width. @todo multi-line
558 const LineRun& line = lines[0u];
560 if(isCircularTextLayout)
562 // Set the circular alignment.
563 rendererParameters.circularLayout = isClockwise ? TextRenderer::Parameters::CLOCKWISE : TextRenderer::Parameters::COUNTER_CLOCKWISE;
565 // Update the text's height to be used by the ellipsis code.
566 textLayoutArea.height = newLayoutSize.height;
568 // Set the size of the text laid out on a straight horizontal line.
569 rendererParameters.circularWidth = static_cast<unsigned int>(newLayoutSize.width);
570 rendererParameters.circularHeight = static_cast<unsigned int>(newLayoutSize.height);
572 // Calculate the center of the circular text according the horizontal and vertical alingments and the radius.
573 switch(horizontalAlignment)
575 case HorizontalAlignment::BEGIN:
577 rendererParameters.centerX = static_cast<int>(textParameters.radius);
580 case HorizontalAlignment::CENTER:
582 rendererParameters.centerX = static_cast<int>(textParameters.textWidth / 2u);
585 case HorizontalAlignment::END:
587 rendererParameters.centerX = static_cast<int>(textParameters.textWidth) - static_cast<int>(textParameters.radius);
592 switch(verticalAlignment)
594 case VerticalAlignment::TOP:
596 rendererParameters.centerY = static_cast<int>(textParameters.radius);
599 case VerticalAlignment::CENTER:
601 rendererParameters.centerY = static_cast<int>(textParameters.textHeight / 2u);
604 case VerticalAlignment::BOTTOM:
606 rendererParameters.centerY = static_cast<int>(textParameters.textHeight) - static_cast<int>(textParameters.radius);
611 // Calculate the beginning angle according with the given horizontal alignment.
612 const bool isRTL = RTL == line.direction;
614 CircularAlignment::Type alignment = circularAlignment;
617 // Swap the alignment type if the line is right to left.
620 case CircularAlignment::BEGIN:
622 alignment = CircularAlignment::END;
625 case CircularAlignment::CENTER:
630 case CircularAlignment::END:
632 alignment = CircularAlignment::BEGIN;
638 float angleOffset = 0.f;
642 case CircularAlignment::BEGIN:
647 case CircularAlignment::CENTER:
649 const bool isNeg = textParameters.incrementAngle < 0.f;
650 const float textWidth = static_cast<float>(rendererParameters.circularWidth);
651 angleOffset = (isNeg ? -0.5f : 0.5f) * (textLayoutArea.width - textWidth) / static_cast<float>(rendererParameters.radius);
654 case CircularAlignment::END:
656 const bool isNeg = textParameters.incrementAngle < 0.f;
657 const float textWidth = static_cast<float>(rendererParameters.circularWidth);
658 angleOffset = (isNeg ? -1.f : 1.f) * (textLayoutArea.width - textWidth) / static_cast<float>(rendererParameters.radius);
663 // Update the beginning angle with the calculated offset.
664 rendererParameters.beginAngle = Radian(Degree(textParameters.beginAngle)) + angleOffset;
666 // Set the vertical position of the glyphs.
667 for(auto& position : rendererParameters.positions)
674 // Calculate the vertical offset according with the given alignment.
677 switch(verticalAlignment)
679 case VerticalAlignment::TOP:
681 penY = line.ascender;
684 case VerticalAlignment::CENTER:
686 penY = line.ascender + 0.5f * (textLayoutArea.height - (line.ascender - line.descender));
689 case VerticalAlignment::BOTTOM:
691 penY = textLayoutArea.height;
696 // Calculate the horizontal offset according with the given alignment.
697 float alignmentOffset = 0.f;
698 layoutEngine.Align(textLayoutArea,
704 Dali::LayoutDirection::LEFT_TO_RIGHT,
707 // Update the position of the glyphs with the calculated offsets.
708 for(auto& position : rendererParameters.positions)
710 position.x += line.alignmentOffset;
715 // Cairo adds the bearing to the position of the glyph
716 // that has already been added by the DALi's layout engine,
717 // so it's needed to be removed here.
718 for(unsigned int index = 0u; index < rendererParameters.glyphs.Count(); ++index)
720 const GlyphInfo& glyph = rendererParameters.glyphs[index];
721 Vector2& position = rendererParameters.positions[index];
723 position.x -= glyph.xBearing;
726 // Set the position of the embedded items (if there is any).
727 EmbeddedItemInfo* embeddedItemLayoutBuffer = embeddedItemLayout.Begin();
729 for(Length index = 0u, endIndex = embeddedItemLayout.Count(); index < endIndex; ++index)
731 EmbeddedItemInfo& embeddedItem = *(embeddedItemLayoutBuffer + index);
733 embeddedItem.position = rendererParameters.positions[embeddedItem.glyphIndex];
735 if(isCircularTextLayout)
737 // Calculate the new position of the embedded item in the circular path.
739 // Center of the bitmap.
740 const float halfWidth = 0.5f * embeddedItem.size.width;
741 const float halfHeight = 0.5f * embeddedItem.size.height;
742 double centerX = static_cast<double>(embeddedItem.position.x + halfWidth);
743 double centerY = static_cast<double>(embeddedItem.position.y - halfHeight);
745 Dali::TextAbstraction::CircularTextParameters circularTextParameters;
747 circularTextParameters.radius = static_cast<double>(rendererParameters.radius);
748 circularTextParameters.invRadius = 1.0 / circularTextParameters.radius;
749 circularTextParameters.beginAngle = static_cast<double>(-rendererParameters.beginAngle + Dali::Math::PI_2);
750 circularTextParameters.centerX = 0.5f * static_cast<double>(textParameters.textWidth);
751 circularTextParameters.centerY = 0.5f * static_cast<double>(textParameters.textHeight);
753 // Calculate the rotation angle.
754 float radians = rendererParameters.beginAngle;
757 radians += static_cast<float>(circularTextParameters.invRadius * centerX);
762 radians -= static_cast<float>(circularTextParameters.invRadius * centerX);
763 radians = -radians + Dali::Math::PI;
765 embeddedItem.angle = Degree(Radian(radians));
767 Dali::TextAbstraction::TransformToArc(circularTextParameters, centerX, centerY);
769 // Recalculate the size of the embedded item after the rotation to position it correctly.
770 float width = embeddedItem.size.width;
771 float height = embeddedItem.size.height;
773 // Transform the input angle into the range [0..2PI]
774 radians = fmod(radians, TWO_PI);
775 radians += (radians < 0.f) ? TWO_PI : 0.f;
777 // Does the same operations than rotate by shear.
778 if((radians > Math::PI_4) && (radians <= RAD_135))
780 std::swap(width, height);
781 radians -= Math::PI_2;
783 else if((radians > RAD_135) && (radians <= RAD_225))
787 else if((radians > RAD_225) && (radians <= RAD_315))
789 std::swap(width, height);
793 if(fabs(radians) > Dali::Math::MACHINE_EPSILON_10)
795 const float angleSinus = fabs(sin(radians));
796 const float angleCosinus = cos(radians);
798 // Calculate the rotated image dimensions.
799 embeddedItem.rotatedSize.height = width * angleSinus + height * angleCosinus;
800 embeddedItem.rotatedSize.width = height * angleSinus + width * angleCosinus + 1.f;
803 embeddedItem.position.x = floor(static_cast<float>(centerX) - 0.5f * embeddedItem.rotatedSize.width);
804 embeddedItem.position.y = floor(static_cast<float>(centerY) - 0.5f * embeddedItem.rotatedSize.height);
808 embeddedItem.position.y -= embeddedItem.size.height;
813 void Ellipsis(const RendererParameters& textParameters, TextAbstraction::TextRenderer::Parameters& rendererParameters, Vector<EmbeddedItemInfo>& embeddedItemLayout, InternalDataModel& internalDataModel)
815 Text::ModelPtr& textModel = internalDataModel.textModel;
816 FontClient& fontClient = internalDataModel.fontClient;
818 Vector<LineRun>& lines = textModel->mVisualModel->mLines; // The laid out lines.
819 Vector<bool>& isEmoji = internalDataModel.isEmoji;
820 const Size textLayoutArea = internalDataModel.textLayoutArea;
821 const float characterSpacing = textModel->mVisualModel->GetCharacterSpacing();
822 float calculatedAdvance = 0.f;
823 Vector<CharacterIndex>& glyphToCharacterMap = textModel->mVisualModel->mGlyphsToCharacters;
824 const CharacterIndex* glyphToCharacterMapBuffer = glyphToCharacterMap.Begin();
825 ////////////////////////////////////////////////////////////////////////////////
826 // Ellipsis the text.
827 ////////////////////////////////////////////////////////////////////////////////
829 if(textParameters.ellipsisEnabled)
831 const LineRun& line = lines[0u]; // TODO: multi-line
835 Length finalNumberOfGlyphs = 0u;
837 if((GetLineHeight(line)) > textLayoutArea.height)
839 // The height of the line is bigger than the height of the text area.
840 // Show the ellipsis glyph even if it doesn't fit in the text area.
841 // If no text is rendered then issues are rised and it may be a while
842 // until is find out that the text area is too small.
844 // Get the first glyph which is going to be replaced and the ellipsis glyph.
845 GlyphInfo& glyphInfo = rendererParameters.glyphs[0u];
846 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph(fontClient.GetPointSize(glyphInfo.fontId));
848 // Change the 'x' and 'y' position of the ellipsis glyph.
849 Vector2& position = rendererParameters.positions[0u];
850 position.x = ellipsisGlyph.xBearing;
851 position.y = textLayoutArea.height - ellipsisGlyph.yBearing;
853 // Replace the glyph by the ellipsis glyph.
854 glyphInfo = ellipsisGlyph;
856 // Set the final number of glyphs
857 finalNumberOfGlyphs = 1u;
861 // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
862 float firstPenX = 0.f; // Used if rtl text is elided.
863 bool firstPenSet = false;
865 // Add the ellipsis glyph.
866 bool inserted = false;
867 float removedGlypsWidth = 0.f;
868 Length numberOfRemovedGlyphs = 0u;
869 if(line.glyphRun.numberOfGlyphs > 0u)
871 GlyphIndex index = line.glyphRun.numberOfGlyphs - 1u;
873 GlyphInfo* glyphs = rendererParameters.glyphs.Begin();
874 Vector2* glyphPositions = rendererParameters.positions.Begin();
878 // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
881 const GlyphInfo& glyphToRemove = *(glyphs + index);
883 if(0u != glyphToRemove.fontId)
885 // i.e. The font id of the glyph shaped from the '\n' character is zero.
887 // Need to reshape the glyph as the font may be different in size.
888 const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph(fontClient.GetPointSize(glyphToRemove.fontId));
892 const Vector2& position = *(glyphPositions + index);
894 // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
897 // Calculates the first penX which will be used if rtl text is elided.
898 firstPenX = position.x - glyphToRemove.xBearing;
899 if(firstPenX < -ellipsisGlyph.xBearing)
901 // Avoids to exceed the bounding box when rtl text is elided.
902 firstPenX = -ellipsisGlyph.xBearing;
905 removedGlypsWidth = -ellipsisGlyph.xBearing;
910 calculatedAdvance = GetCalculatedAdvance(*(textModel->mLogicalModel->mText.Begin() + (*(glyphToCharacterMapBuffer + index))), characterSpacing, glyphToRemove.advance);
911 removedGlypsWidth += std::min(calculatedAdvance, (glyphToRemove.xBearing + glyphToRemove.width));
913 // Calculate the width of the ellipsis glyph and check if it fits.
914 const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
915 if(ellipsisGlyphWidth < removedGlypsWidth)
917 GlyphInfo& glyphInfo = *(glyphs + index);
918 Vector2& position = *(glyphPositions + index);
919 position.x -= (0.f > glyphInfo.xBearing) ? glyphInfo.xBearing : 0.f;
921 // Replace the glyph by the ellipsis glyph.
922 glyphInfo = ellipsisGlyph;
924 // Update the isEmoji vector
925 isEmoji[index] = false;
927 // Change the 'x' and 'y' position of the ellipsis glyph.
929 if(position.x > firstPenX)
931 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
934 position.x += ellipsisGlyph.xBearing;
949 // No space for the ellipsis.
952 ++numberOfRemovedGlyphs;
955 // Set the final number of glyphs
956 finalNumberOfGlyphs = line.glyphRun.numberOfGlyphs - numberOfRemovedGlyphs;
960 // Resize the number of glyphs/positions
961 rendererParameters.glyphs.Resize(finalNumberOfGlyphs);
962 rendererParameters.positions.Resize(finalNumberOfGlyphs);
964 // Remove from the embedded items those exceding the last laid out glyph.
965 embeddedItemLayout.Erase(std::remove_if(embeddedItemLayout.Begin(),
966 embeddedItemLayout.End(),
967 [finalNumberOfGlyphs](const EmbeddedItemInfo& item) {
968 return item.glyphIndex >= finalNumberOfGlyphs;
970 embeddedItemLayout.End());
976 Size LayoutText(const RendererParameters& textParameters, TextAbstraction::TextRenderer::Parameters& rendererParameters, Vector<EmbeddedItemInfo>& embeddedItemLayout, InternalDataModel& internalDataModel)
978 ////////////////////////////////////////////////////////////////////////////////
980 ////////////////////////////////////////////////////////////////////////////////
981 Text::ModelPtr& textModel = internalDataModel.textModel;
982 Text::Layout::Engine& layoutEngine = internalDataModel.layoutEngine;
983 FontClient& fontClient = internalDataModel.fontClient;
984 const Length numberOfGlyphs = internalDataModel.numberOfGlyphs;
985 const bool isTextMirrored = internalDataModel.isTextMirrored;
986 const Vector<Character>& mirroredUtf32Characters = internalDataModel.mirroredUtf32Characters;
987 const Length numberOfCharacters = internalDataModel.numberOfCharacters;
988 const auto ellipsisPosition = textModel->mEllipsisPosition;
990 Layout::Type layout = Layout::SINGLELINE;
992 Property::Value layoutStr(textParameters.layout);
993 GetLayoutEnumeration(layoutStr, layout);
995 // Whether the layout is multi-line.
996 const Text::Layout::Engine::Type horizontalLayout = (Layout::MULTILINE == layout) ? Text::Layout::Engine::MULTI_LINE_BOX : Text::Layout::Engine::SINGLE_LINE_BOX;
997 layoutEngine.SetLayout(horizontalLayout); // TODO: multi-line.
999 // Set minimun line size
1000 layoutEngine.SetDefaultLineSize(textParameters.minLineSize);
1002 // Whether the layout is circular.
1003 const bool isCircularTextLayout = (Layout::CIRCULAR == layout);
1004 const bool isClockwise = isCircularTextLayout && (0.f < textParameters.incrementAngle);
1006 // Calculates the max ascender or the max descender.
1007 // Is used to calculate the radius of the base line of the text.
1008 float maxAscenderDescender = 0.f;
1009 if(isCircularTextLayout)
1011 FontId currentFontId = 0u;
1012 for(const auto& glyph : rendererParameters.glyphs)
1014 if(currentFontId != glyph.fontId)
1016 currentFontId = glyph.fontId;
1017 FontMetrics metrics;
1018 fontClient.GetFontMetrics(currentFontId, metrics);
1019 maxAscenderDescender = std::max(maxAscenderDescender, isClockwise ? metrics.ascender : metrics.descender);
1023 const unsigned int radius = textParameters.radius - static_cast<unsigned int>(maxAscenderDescender);
1025 // Set the layout parameters.
1026 Size textLayoutArea = Size(static_cast<float>(textParameters.textWidth),
1027 static_cast<float>(textParameters.textHeight));
1030 Extents padding = textParameters.padding;
1031 internalDataModel.textLayoutArea = Size(textLayoutArea.x - (padding.start + padding.end), textLayoutArea.y - (padding.top + padding.bottom));
1033 if(isCircularTextLayout)
1035 // In a circular layout, the length of the text area depends on the radius.
1036 rendererParameters.radius = radius;
1037 internalDataModel.textLayoutArea.width = fabs(Radian(Degree(textParameters.incrementAngle)) * static_cast<float>(rendererParameters.radius));
1039 // Resize the vector of positions to have the same size than the vector of glyphs.
1040 rendererParameters.positions.Resize(numberOfGlyphs);
1042 textModel->mLineWrapMode = Text::LineWrap::WORD;
1043 textModel->mIgnoreSpacesAfterText = false;
1044 Text::Layout::Parameters layoutParameters(internalDataModel.textLayoutArea,
1047 // Whether the last character is a new paragraph character.
1048 const Vector<Character>& textToShape = isTextMirrored ? mirroredUtf32Characters : textModel->mLogicalModel->mText;
1049 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph(textToShape[numberOfCharacters - 1u]);
1051 // The initial glyph and the number of glyphs to layout.
1052 layoutParameters.startGlyphIndex = 0u;
1053 layoutParameters.numberOfGlyphs = numberOfGlyphs;
1054 layoutParameters.startLineIndex = 0u;
1055 layoutParameters.estimatedNumberOfLines = 1u;
1056 layoutParameters.interGlyphExtraAdvance = 0.f;
1058 // Update the visual model.
1060 bool isAutoScrollEnabled = false;
1061 layoutEngine.LayoutText(layoutParameters,
1063 textParameters.ellipsisEnabled,
1064 isAutoScrollEnabled,
1067 return newLayoutSize;
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);
1083 Devel::PixelBuffer Render(const RendererParameters& textParameters, Vector<EmbeddedItemInfo>& embeddedItemLayout)
1085 if(textParameters.text.empty())
1087 Dali::Devel::PixelBuffer pixelBuffer = Dali::Devel::PixelBuffer::New(textParameters.textWidth,
1088 textParameters.textHeight,
1089 Dali::Pixel::RGBA8888);
1091 const unsigned int bufferSize = textParameters.textWidth * textParameters.textHeight * Dali::Pixel::GetBytesPerPixel(Dali::Pixel::RGBA8888);
1092 unsigned char* buffer = pixelBuffer.GetBuffer();
1093 memset(buffer, 0, bufferSize);
1098 FontClient fontClient = FontClient::Get();
1100 // Use this to access FontClient i.e. to get down-scaled Emoji metrics.
1101 metrics = Metrics::New(fontClient);
1103 Text::ModelPtr textModel = Text::Model::New();
1104 InternalDataModel internalData(fontClient, metrics, textModel);
1106 TextAbstraction::TextRenderer::Parameters rendererParameters(internalData.textModel->mVisualModel->mGlyphs,
1107 internalData.textModel->mVisualModel->mGlyphPositions,
1108 internalData.textModel->mVisualModel->mColors,
1109 internalData.textModel->mVisualModel->mColorIndices,
1110 internalData.blendingMode,
1111 internalData.isEmoji);
1113 rendererParameters.width = textParameters.textWidth;
1114 rendererParameters.height = textParameters.textHeight;
1115 rendererParameters.pixelFormat = TextAbstraction::TextRenderer::Parameters::RGBA8888; // @note: At the moment all textures are generated RGBA8888
1117 ////////////////////////////////////////////////////////////////////////////////
1118 // Process the markup string if the mark-up processor is enabled.
1119 ////////////////////////////////////////////////////////////////////////////////
1120 ShapeTextPreprocess(textParameters, rendererParameters, internalData);
1122 ////////////////////////////////////////////////////////////////////////////////
1123 // Retrieve the glyphs. Text shaping
1124 ////////////////////////////////////////////////////////////////////////////////
1125 ShapeText(rendererParameters, embeddedItemLayout, internalData);
1127 ////////////////////////////////////////////////////////////////////////////////
1128 // Retrieve the glyph's metrics.
1129 ////////////////////////////////////////////////////////////////////////////////
1131 metrics->GetGlyphMetrics(rendererParameters.glyphs.Begin(), internalData.numberOfGlyphs);
1133 ////////////////////////////////////////////////////////////////////////////////
1134 // Set the color runs in glyphs.
1135 ////////////////////////////////////////////////////////////////////////////////
1136 SetColorSegmentation(textParameters, internalData);
1138 ////////////////////////////////////////////////////////////////////////////////
1139 // Set the isEmoji Vector
1140 ////////////////////////////////////////////////////////////////////////////////
1141 SetEmojiVector(internalData);
1143 ////////////////////////////////////////////////////////////////////////////////
1145 ////////////////////////////////////////////////////////////////////////////////
1146 Size newLayoutSize = LayoutText(textParameters, rendererParameters, embeddedItemLayout, internalData);
1148 ////////////////////////////////////////////////////////////////////////////////
1150 ////////////////////////////////////////////////////////////////////////////////
1151 Align(textParameters, rendererParameters, embeddedItemLayout, internalData, newLayoutSize);
1153 ////////////////////////////////////////////////////////////////////////////////
1154 // Ellipsis the text.
1155 ////////////////////////////////////////////////////////////////////////////////
1156 Ellipsis(textParameters, rendererParameters, embeddedItemLayout, internalData);
1158 ////////////////////////////////////////////////////////////////////////////////
1160 ////////////////////////////////////////////////////////////////////////////////
1161 return RenderText(textParameters, rendererParameters);
1164 Devel::PixelBuffer CreateShadow(const ShadowParameters& shadowParameters)
1166 // The size of the pixel data.
1167 const int width = static_cast<int>(shadowParameters.input.GetWidth());
1168 const int height = static_cast<int>(shadowParameters.input.GetHeight());
1170 // The shadow's offset.
1171 const int xOffset = static_cast<int>(shadowParameters.offset.x);
1172 const int yOffset = static_cast<int>(shadowParameters.offset.y);
1174 // The size in bytes of the pixel of the input's buffer.
1175 const Pixel::Format inputFormat = shadowParameters.input.GetPixelFormat();
1176 const unsigned int inputPixelSize = Pixel::GetBytesPerPixel(inputFormat);
1177 const bool isA8 = Pixel::A8 == inputFormat;
1179 // Creates the output pixel buffer.
1180 Devel::PixelBuffer outputPixelBuffer = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
1182 // Clear the output buffer
1183 unsigned char* outputPixelBufferPtr = outputPixelBuffer.GetBuffer();
1184 memset(outputPixelBufferPtr, 0, width * height * Pixel::GetBytesPerPixel(Pixel::RGBA8888));
1186 // Gets the buffer of the input pixel buffer.
1187 const unsigned char* const inputPixelBuffer = shadowParameters.input.GetBuffer();
1189 float textColor[4u];
1192 memcpy(textColor, shadowParameters.textColor.AsFloat(), 4u * sizeof(float));
1194 const float* const shadowColor = shadowParameters.color.AsFloat();
1196 // Traverse the input pixel buffer and write the text on the foreground and the shadow on the background.
1197 for(int rowIndex = 0; rowIndex < height; ++rowIndex)
1199 // Calculates the rowIndex to the input pixel buffer for the shadow and whether it's within the boundaries.
1200 const int yOffsetIndex = rowIndex - yOffset;
1201 const bool isValidRowIndex = ((yOffsetIndex >= 0) && (yOffsetIndex < height));
1203 const int rows = rowIndex * width;
1204 const int offsetRows = yOffsetIndex * width;
1205 for(int columnIndex = 0; columnIndex < width; ++columnIndex)
1207 // Index to the input buffer to retrieve the alpha value of the foreground text.
1208 const unsigned int index = inputPixelSize * static_cast<unsigned int>(rows + columnIndex);
1210 // Build the index to the input buffer to retrieve the alpha value of the background shadow.
1211 unsigned int shadowIndex = 0u;
1212 bool isValidShadowIndex = false;
1215 const int xOffsetIndex = columnIndex - xOffset;
1216 isValidShadowIndex = ((xOffsetIndex >= 0) && (xOffsetIndex < width));
1218 if(isValidShadowIndex)
1220 shadowIndex = inputPixelSize * static_cast<unsigned int>(offsetRows + xOffsetIndex);
1224 // If the input buffer is an alpha mask, retrieve the values for the foreground text and the background shadow.
1225 // If not retrieve the color.
1226 float inputShadowOffsetAlphaValue = 1.f;
1227 float inputAlphaValue = 1.f;
1230 // Retrieve the alpha value for the shadow.
1231 inputShadowOffsetAlphaValue = isValidShadowIndex ? (static_cast<float>(*(inputPixelBuffer + shadowIndex)) / 255.f) : 0.f;
1233 // Retrieve the alpha value for the text.
1234 inputAlphaValue = static_cast<float>(*(inputPixelBuffer + index)) / 255.f;
1238 // The input buffer is not an alpha mask. Retrieve the color.
1239 textColor[0u] = TO_FLOAT * static_cast<float>(*(inputPixelBuffer + index + 0u));
1240 textColor[1u] = TO_FLOAT * static_cast<float>(*(inputPixelBuffer + index + 1u));
1241 textColor[2u] = TO_FLOAT * static_cast<float>(*(inputPixelBuffer + index + 2u));
1242 textColor[3u] = TO_FLOAT * static_cast<float>(*(inputPixelBuffer + index + 3u));
1243 inputAlphaValue = textColor[3u];
1244 inputShadowOffsetAlphaValue = isValidShadowIndex ? TO_FLOAT * static_cast<float>(*(inputPixelBuffer + shadowIndex + 3u)) : 0.f;
1247 // Build the output color.
1248 float outputColor[4u];
1250 if(shadowParameters.blendShadow)
1252 // Blend the shadow's color with the text's color on top
1253 const float textAlpha = textColor[3u] * inputAlphaValue;
1254 const float shadowAlpha = shadowColor[3u] * inputShadowOffsetAlphaValue;
1256 // Blends the alpha.
1257 outputColor[3u] = 1.f - ((1.f - textAlpha) * (1.f - shadowAlpha));
1258 const bool isOutputAlphaZero = outputColor[3u] < Dali::Math::MACHINE_EPSILON_1000;
1259 if(isOutputAlphaZero)
1261 std::fill(outputColor, outputColor + 4u, 0.f);
1265 // Blends the RGB components.
1266 float shadowComponent = 0.f;
1267 float textComponent = 0.f;
1269 shadowComponent = shadowColor[0u] * inputShadowOffsetAlphaValue;
1270 textComponent = textColor[0u] * inputAlphaValue;
1271 outputColor[0u] = (textComponent * textAlpha / outputColor[3u]) + (shadowComponent * shadowAlpha * (1.f - textAlpha) / outputColor[3u]);
1273 shadowComponent = shadowColor[1u] * inputShadowOffsetAlphaValue;
1274 textComponent = textColor[1u] * inputAlphaValue;
1275 outputColor[1u] = (textComponent * textAlpha / outputColor[3u]) + (shadowComponent * shadowAlpha * (1.f - textAlpha) / outputColor[3u]);
1277 shadowComponent = shadowColor[2u] * inputShadowOffsetAlphaValue;
1278 textComponent = textColor[2u] * inputAlphaValue;
1279 outputColor[2u] = (textComponent * textAlpha / outputColor[3u]) + (shadowComponent * shadowAlpha * (1.f - textAlpha) / outputColor[3u]);
1285 std::fill(outputColor, outputColor + 4u, 0.f);
1287 const float textAlpha = textColor[3u];
1288 const float shadowAlpha = shadowColor[3u] * inputShadowOffsetAlphaValue;
1290 // Write shadow first.
1291 if(shadowAlpha > Dali::Math::MACHINE_EPSILON_1000)
1293 outputColor[0u] = shadowColor[0u] * inputShadowOffsetAlphaValue;
1294 outputColor[1u] = shadowColor[1u] * inputShadowOffsetAlphaValue;
1295 outputColor[2u] = shadowColor[2u] * inputShadowOffsetAlphaValue;
1296 outputColor[3u] = shadowAlpha;
1299 // Write character on top.
1300 if(textAlpha > Dali::Math::MACHINE_EPSILON_1000)
1302 outputColor[0u] = textColor[0u];
1303 outputColor[1u] = textColor[1u];
1304 outputColor[2u] = textColor[2u];
1305 outputColor[3u] = textAlpha;
1309 // Write the color into the output pixel buffer.
1310 const unsigned int outputIndex = 4u * (rows + columnIndex);
1311 *(outputPixelBufferPtr + outputIndex + 0u) = static_cast<unsigned char>(TO_UCHAR * outputColor[0u]);
1312 *(outputPixelBufferPtr + outputIndex + 1u) = static_cast<unsigned char>(TO_UCHAR * outputColor[1u]);
1313 *(outputPixelBufferPtr + outputIndex + 2u) = static_cast<unsigned char>(TO_UCHAR * outputColor[2u]);
1314 *(outputPixelBufferPtr + outputIndex + 3u) = static_cast<unsigned char>(TO_UCHAR * outputColor[3u]);
1318 // Returns the pixel buffer.
1319 return outputPixelBuffer;
1322 Devel::PixelBuffer ConvertToRgba8888(Devel::PixelBuffer pixelBuffer, const Vector4& color, bool multiplyByAlpha)
1324 if(Dali::Pixel::A8 != pixelBuffer.GetPixelFormat())
1330 const unsigned int width = pixelBuffer.GetWidth();
1331 const unsigned int height = pixelBuffer.GetHeight();
1332 Devel::PixelBuffer newPixelBuffer = Devel::PixelBuffer::New(width, height, Dali::Pixel::RGBA8888);
1334 unsigned char* dstBuffer = newPixelBuffer.GetBuffer();
1335 const unsigned char* const srcBuffer = pixelBuffer.GetBuffer();
1337 const unsigned char r = static_cast<unsigned char>(TO_UCHAR * color.r);
1338 const unsigned char g = static_cast<unsigned char>(TO_UCHAR * color.g);
1339 const unsigned char b = static_cast<unsigned char>(TO_UCHAR * color.b);
1341 unsigned char dstColor[4];
1342 for(unsigned int j = 0u; j < height; ++j)
1344 const unsigned int lineIndex = j * width;
1345 for(unsigned int i = 0u; i < width; ++i)
1347 const unsigned int srcIndex = lineIndex + i;
1349 const float srcAlpha = static_cast<float>(*(srcBuffer + srcIndex));
1353 dstColor[0u] = static_cast<unsigned char>(srcAlpha * color.r);
1354 dstColor[1u] = static_cast<unsigned char>(srcAlpha * color.g);
1355 dstColor[2u] = static_cast<unsigned char>(srcAlpha * color.b);
1356 dstColor[3u] = static_cast<unsigned char>(srcAlpha * color.a);
1363 dstColor[3u] = static_cast<unsigned char>(srcAlpha);
1366 const unsigned int dstIndex = srcIndex * 4u;
1367 memcpy(dstBuffer + dstIndex, dstColor, 4u);
1371 return newPixelBuffer;
1374 void UpdateBuffer(Devel::PixelBuffer src, Devel::PixelBuffer dst, unsigned int x, unsigned int y, bool blend)
1376 const Dali::Pixel::Format pixelFormat = dst.GetPixelFormat();
1377 if(src.GetPixelFormat() != pixelFormat)
1379 DALI_LOG_ERROR("PixelBuffer::SetBuffer. The pixel format of the new data must be the same of the current pixel buffer.");
1383 const unsigned int srcWidth = src.GetWidth();
1384 const unsigned int srcHeight = src.GetHeight();
1385 const unsigned int dstWidth = dst.GetWidth();
1386 const unsigned int dstHeight = dst.GetHeight();
1388 if((x > dstWidth) ||
1390 (x + srcWidth > dstWidth) ||
1391 (y + srcHeight > dstHeight))
1393 DALI_LOG_ERROR("PixelBuffer::SetBuffer. The source pixel buffer is out of the boundaries of the destination pixel buffer.");
1397 const unsigned int bytesPerPixel = Dali::Pixel::GetBytesPerPixel(pixelFormat);
1398 // Ignore when pixelFormat is invalid or contain float
1399 if(bytesPerPixel == 0u || bytesPerPixel == 6u || bytesPerPixel == 12u)
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 Dali::Property::Array RenderForLastIndex(RendererParameters& textParameters)
1476 Property::Array offsetValues;
1477 if(textParameters.text.empty())
1479 return offsetValues;
1481 FontClient fontClient = FontClient::Get();
1483 metrics = Metrics::New(fontClient);
1485 Text::ModelPtr textModel = Text::Model::New();
1486 InternalDataModel internalData(fontClient, metrics, textModel);
1488 TextAbstraction::TextRenderer::Parameters rendererParameters(textModel->mVisualModel->mGlyphs,
1489 textModel->mVisualModel->mGlyphPositions,
1490 textModel->mVisualModel->mColors,
1491 textModel->mVisualModel->mColorIndices,
1492 internalData.blendingMode,
1493 internalData.isEmoji);
1495 rendererParameters.width = textParameters.textWidth;
1496 rendererParameters.height = textParameters.textHeight;
1498 ////////////////////////////////////////////////////////////////////////////////
1499 // Process the markup string if the mark-up processor is enabled.
1500 ////////////////////////////////////////////////////////////////////////////////
1501 ShapeTextPreprocess(textParameters, rendererParameters, internalData);
1503 ////////////////////////////////////////////////////////////////////////////////
1504 // Retrieve the glyphs. Text shaping
1505 ////////////////////////////////////////////////////////////////////////////////
1506 Dali::Vector<Dali::Toolkit::DevelText::EmbeddedItemInfo> embeddedItemLayout;
1507 ShapeText(rendererParameters, embeddedItemLayout, internalData);
1509 ////////////////////////////////////////////////////////////////////////////////
1510 // Retrieve the glyph's metrics.
1511 ////////////////////////////////////////////////////////////////////////////////
1512 metrics->GetGlyphMetrics(rendererParameters.glyphs.Begin(), internalData.numberOfGlyphs);
1514 ////////////////////////////////////////////////////////////////////////////////
1516 ////////////////////////////////////////////////////////////////////////////////
1517 int boundingBox = textParameters.textHeight - (textParameters.padding.top + textParameters.padding.bottom);
1518 textParameters.textHeight = MAX_INT; // layout for the entire area.
1519 LayoutText(textParameters, rendererParameters, embeddedItemLayout, internalData);
1521 ////////////////////////////////////////////////////////////////////////////////
1522 // Calculation last character index
1523 ////////////////////////////////////////////////////////////////////////////////
1524 Vector<LineRun>& lines = internalData.textModel->mVisualModel->mLines;
1525 unsigned int numberOfLines = lines.Count();
1526 int numberOfCharacters = 0;
1528 float lineSize = internalData.layoutEngine.GetDefaultLineSize();
1529 float lineOffset = 0.f;
1530 for(unsigned int index = 0u; index < numberOfLines; ++index)
1532 const LineRun& line = *(lines.Begin() + index);
1533 numberOfCharacters += line.characterRun.numberOfCharacters;
1535 lineOffset = lineSize > 0.f ? lineSize : GetLineHeight(line);
1537 if((penY + lineOffset) > boundingBox)
1539 offsetValues.PushBack(numberOfCharacters);
1545 // add remain character index
1546 offsetValues.PushBack(numberOfCharacters);
1549 return offsetValues;
1552 Dali::Property::Array GetLastCharacterIndex(RendererParameters& textParameters)
1554 Dali::Property::Array offsetValues = Toolkit::DevelText::RenderForLastIndex(textParameters);
1555 return offsetValues;
1558 } // namespace DevelText
1560 } // namespace Toolkit