2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/text/rendering/atlas/text-atlas-renderer.h>
22 #include <dali/devel-api/text-abstraction/font-client.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/animation/constraints.h>
25 #include <dali/public-api/rendering/geometry.h>
26 #include <dali/public-api/rendering/renderer.h>
29 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
30 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
31 #include <dali-toolkit/internal/text/glyph-run.h>
32 #include <dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager.h>
33 #include <dali-toolkit/internal/text/rendering/atlas/atlas-mesh-factory.h>
34 #include <dali-toolkit/internal/text/text-view.h>
37 using namespace Dali::Toolkit;
38 using namespace Dali::Toolkit::Text;
42 #if defined(DEBUG_ENABLED)
43 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_RENDERING");
46 const float ZERO(0.0f);
47 const float HALF(0.5f);
48 const float ONE(1.0f);
49 const uint32_t DOUBLE_PIXEL_PADDING = 4u; //Padding will be added twice to Atlas
50 const uint16_t NO_OUTLINE = 0u;
53 struct AtlasRenderer::Impl
69 AtlasManager::Mesh2D mMesh;
73 * brief Struct used to generate the underline mesh.
74 * There is one Extent per line of text.
82 mUnderlinePosition(0.0f),
83 mUnderlineThickness(0.0f),
92 float mUnderlinePosition;
93 float mUnderlineThickness;
94 uint32_t mMeshRecordIndex;
95 uint32_t mUnderlineChunkId;
102 mNeededBlockWidth(0),
103 mNeededBlockHeight(0)
108 uint32_t mNeededBlockWidth;
109 uint32_t mNeededBlockHeight;
121 Text::GlyphIndex mIndex;
124 struct TextCacheEntry
137 Text::GlyphIndex mIndex;
139 uint16_t mOutlineWidth;
147 mGlyphManager = AtlasGlyphManager::Get();
148 mFontClient = TextAbstraction::FontClient::Get();
150 mQuadVertexFormat["aPosition"] = Property::VECTOR2;
151 mQuadVertexFormat["aTexCoord"] = Property::VECTOR2;
152 mQuadVertexFormat["aColor"] = Property::VECTOR4;
155 bool IsGlyphUnderlined(GlyphIndex index,
156 const Vector<GlyphRun>& underlineRuns)
158 for(Vector<GlyphRun>::ConstIterator it = underlineRuns.Begin(),
159 endIt = underlineRuns.End();
163 const GlyphRun& run = *it;
165 if((run.glyphIndex <= index) && (index < run.glyphIndex + run.numberOfGlyphs))
174 void CacheGlyph(const GlyphInfo& glyph, FontId lastFontId, const AtlasGlyphManager::GlyphStyle& style, AtlasManager::AtlasSlot& slot)
176 const Size& defaultTextAtlasSize = mFontClient.GetDefaultTextAtlasSize(); //Retrieve default size of text-atlas-block from font-client.
177 const Size& maximumTextAtlasSize = mFontClient.GetMaximumTextAtlasSize(); //Retrieve maximum size of text-atlas-block from font-client.
179 const bool glyphNotCached = !mGlyphManager.IsCached(glyph.fontId, glyph.index, style, slot); // Check FontGlyphRecord vector for entry with glyph index and fontId
181 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "AddGlyphs fontID[%u] glyphIndex[%u] [%s]\n", glyph.fontId, glyph.index, (glyphNotCached) ? "not cached" : "cached");
185 MaxBlockSize& blockSize = mBlockSizes[0u];
187 if(lastFontId != glyph.fontId)
190 // Looks through all stored block sizes until finds the one which mataches required glyph font it. Ensures new atlas block size will match existing for same font id.
191 // CalculateBlocksSize() above ensures a block size entry exists.
192 for(std::vector<MaxBlockSize>::const_iterator it = mBlockSizes.begin(),
193 endIt = mBlockSizes.end();
197 const MaxBlockSize& blockSizeEntry = *it;
198 if(blockSizeEntry.mFontId == glyph.fontId)
200 blockSize = mBlockSizes[index];
205 // Create a new image for the glyph
208 // Whether the glyph is an outline.
209 const bool isOutline = 0u != style.outline;
211 // Whether the current glyph is a color one.
212 const bool isColorGlyph = mFontClient.IsColorGlyph(glyph.fontId, glyph.index);
214 if(!isOutline || (isOutline && !isColorGlyph))
216 // Retrieve the emoji's bitmap.
217 TextAbstraction::FontClient::GlyphBufferData glyphBufferData;
218 glyphBufferData.width = isColorGlyph ? glyph.width : 0; // Desired width and height.
219 glyphBufferData.height = isColorGlyph ? glyph.height : 0;
221 mFontClient.CreateBitmap(glyph.fontId,
223 glyph.isItalicRequired,
224 glyph.isBoldRequired,
228 // Create the pixel data.
229 bitmap = PixelData::New(glyphBufferData.buffer,
230 glyphBufferData.width * glyphBufferData.height * GetBytesPerPixel(glyphBufferData.format),
231 glyphBufferData.width,
232 glyphBufferData.height,
233 glyphBufferData.format,
234 PixelData::DELETE_ARRAY);
238 // Ensure that the next image will fit into the current block size
239 if(bitmap.GetWidth() > blockSize.mNeededBlockWidth)
241 blockSize.mNeededBlockWidth = bitmap.GetWidth();
244 if(bitmap.GetHeight() > blockSize.mNeededBlockHeight)
246 blockSize.mNeededBlockHeight = bitmap.GetHeight();
249 // If CheckAtlas in AtlasManager::Add can't fit the bitmap in the current atlas it will create a new atlas
251 // Setting the block size and size of new atlas does not mean a new one will be created. An existing atlas may still surffice.
252 uint32_t default_width = defaultTextAtlasSize.width;
253 uint32_t default_height = defaultTextAtlasSize.height;
256 (blockSize.mNeededBlockWidth >= (default_width - (DOUBLE_PIXEL_PADDING + 1u)) ||
257 blockSize.mNeededBlockHeight >= (default_height - (DOUBLE_PIXEL_PADDING + 1u))) &&
258 (default_width < maximumTextAtlasSize.width &&
259 default_height < maximumTextAtlasSize.height))
261 default_width <<= 1u;
262 default_height <<= 1u;
264 mGlyphManager.SetNewAtlasSize(default_width,
266 blockSize.mNeededBlockWidth,
267 blockSize.mNeededBlockHeight);
269 // Locate a new slot for our glyph
270 mGlyphManager.Add(glyph, style, bitmap, slot); // slot will be 0 is glyph not added
276 // We have 2+ copies of the same glyph
277 mGlyphManager.AdjustReferenceCount(glyph.fontId, glyph.index, style, 1); //increment
281 void GenerateMesh(const GlyphInfo& glyph,
282 const Vector2& position,
283 const Vector4& color,
285 AtlasManager::AtlasSlot& slot,
287 float currentUnderlinePosition,
288 float currentUnderlineThickness,
289 std::vector<MeshRecord>& meshContainer,
290 Vector<TextCacheEntry>& newTextCache,
291 Vector<Extent>& extents,
292 uint32_t underlineChunkId)
294 // Generate mesh data for this quad, plugging in our supplied position
295 AtlasManager::Mesh2D newMesh;
296 mGlyphManager.GenerateMeshData(slot.mImageId, position, newMesh);
298 TextCacheEntry textCacheEntry;
299 textCacheEntry.mFontId = glyph.fontId;
300 textCacheEntry.mImageId = slot.mImageId;
301 textCacheEntry.mIndex = glyph.index;
302 textCacheEntry.mOutlineWidth = outline;
303 textCacheEntry.isItalic = glyph.isItalicRequired;
304 textCacheEntry.isBold = glyph.isBoldRequired;
306 newTextCache.PushBack(textCacheEntry);
308 AtlasManager::Vertex2D* verticesBuffer = newMesh.mVertices.Begin();
310 for(unsigned int index = 0u, size = newMesh.mVertices.Count();
314 AtlasManager::Vertex2D& vertex = *(verticesBuffer + index);
316 // Set the color of the vertex.
317 vertex.mColor = color;
320 // Find an existing mesh data object to attach to ( or create a new one, if we can't find one using the same atlas)
321 StitchTextMesh(meshContainer,
324 position.y + glyph.yBearing,
326 currentUnderlinePosition,
327 currentUnderlineThickness,
332 void CreateActors(const std::vector<MeshRecord>& meshContainer,
333 const Size& textSize,
334 const Vector4& color,
335 const Vector4& shadowColor,
336 const Vector2& shadowOffset,
338 Property::Index animatablePropertyIndex,
343 // Create a container actor to act as a common parent for text and shadow, to avoid color inheritence issues.
344 mActor = Actor::New();
345 mActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
346 mActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
347 mActor.SetProperty(Actor::Property::SIZE, textSize);
348 mActor.SetProperty(Actor::Property::COLOR_MODE, USE_OWN_MULTIPLY_PARENT_COLOR);
351 for(std::vector<MeshRecord>::const_iterator it = meshContainer.begin(),
352 endIt = meshContainer.end();
356 const MeshRecord& meshRecord = *it;
358 Actor actor = CreateMeshActor(textControl, animatablePropertyIndex, color, meshRecord, textSize, STYLE_NORMAL);
360 // Whether the actor has renderers.
361 const bool hasRenderer = actor.GetRendererCount() > 0u;
363 // Create an effect if necessary
367 // Change the color of the vertices.
368 for(Vector<AtlasManager::Vertex2D>::Iterator vIt = meshRecord.mMesh.mVertices.Begin(),
369 vEndIt = meshRecord.mMesh.mVertices.End();
373 AtlasManager::Vertex2D& vertex = *vIt;
375 vertex.mColor = shadowColor;
378 Actor shadowActor = CreateMeshActor(textControl, animatablePropertyIndex, color, meshRecord, textSize, STYLE_DROP_SHADOW);
379 #if defined(DEBUG_ENABLED)
380 shadowActor.SetProperty(Dali::Actor::Property::NAME, "Text Shadow renderable actor");
382 // Offset shadow in x and y
383 shadowActor.RegisterProperty("uOffset", shadowOffset);
384 Dali::Renderer renderer(shadowActor.GetRendererAt(0));
385 int depthIndex = renderer.GetProperty<int>(Dali::Renderer::Property::DEPTH_INDEX);
386 renderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, depthIndex - 1);
387 mActor.Add(shadowActor);
397 void AddGlyphs(Text::ViewInterface& view,
399 Property::Index animatablePropertyIndex,
400 const Vector<Vector2>& positions,
401 const Vector<GlyphInfo>& glyphs,
402 const Vector4& defaultColor,
403 const Vector4* const colorsBuffer,
404 const ColorIndex* const colorIndicesBuffer,
408 AtlasManager::AtlasSlot slot;
412 AtlasManager::AtlasSlot slotOutline;
413 slotOutline.mImageId = 0u;
414 slotOutline.mAtlasId = 0u;
416 std::vector<MeshRecord> meshContainer;
417 std::vector<MeshRecord> meshContainerOutline;
418 Vector<Extent> extents;
421 const Vector2& textSize(view.GetLayoutSize());
422 const Vector2 halfTextSize(textSize * 0.5f);
423 const Vector2& shadowOffset(view.GetShadowOffset());
424 const Vector4& shadowColor(view.GetShadowColor());
425 const bool underlineEnabled = view.IsUnderlineEnabled();
426 const Vector4& underlineColor(view.GetUnderlineColor());
427 const float underlineHeight = view.GetUnderlineHeight();
428 const uint16_t outlineWidth = view.GetOutlineWidth();
429 const Vector4& outlineColor(view.GetOutlineColor());
430 const bool isOutline = 0u != outlineWidth;
431 const GlyphInfo* hyphens = view.GetHyphens();
432 const Length* hyphenIndices = view.GetHyphenIndices();
433 const Length hyphensCount = view.GetHyphensCount();
435 // Elided text info. Indices according to elided text.
436 const auto startIndexOfGlyphs = view.GetStartIndexOfElidedGlyphs();
437 const auto firstMiddleIndexOfElidedGlyphs = view.GetFirstMiddleIndexOfElidedGlyphs();
438 const auto secondMiddleIndexOfElidedGlyphs = view.GetSecondMiddleIndexOfElidedGlyphs();
440 const bool useDefaultColor = (NULL == colorsBuffer);
442 // Get the underline runs.
443 const Length numberOfUnderlineRuns = view.GetNumberOfUnderlineRuns();
444 Vector<GlyphRun> underlineRuns;
445 underlineRuns.Resize(numberOfUnderlineRuns);
446 view.GetUnderlineRuns(underlineRuns.Begin(),
448 numberOfUnderlineRuns);
450 bool thereAreUnderlinedGlyphs = false;
452 float currentUnderlinePosition = ZERO;
453 float currentUnderlineThickness = underlineHeight;
454 FontId lastFontId = 0;
455 FontId lastUnderlinedFontId = 0;
456 Style style = STYLE_NORMAL;
458 if(fabsf(shadowOffset.x) > Math::MACHINE_EPSILON_1 || fabsf(shadowOffset.y) > Math::MACHINE_EPSILON_1)
460 style = STYLE_DROP_SHADOW;
463 CalculateBlocksSize(glyphs);
465 // Avoid emptying mTextCache (& removing references) until after incremented references for the new text
466 Vector<TextCacheEntry> newTextCache;
467 const GlyphInfo* const glyphsBuffer = glyphs.Begin();
468 const Vector2* const positionsBuffer = positions.Begin();
469 const Vector2 lineOffsetPosition(minLineOffset, 0.f);
470 uint32_t hyphenIndex = 0;
472 //For septated underlined chunks. (this is for Markup case)
473 uint32_t underlineChunkId = 0u; // give id for each chunk.
474 bool isPreUnderlined = false; // status of underlined for previous glyph.
476 //Skip hyphenIndices less than startIndexOfGlyphs or between two middle of elided text
479 while((hyphenIndex < hyphensCount) && (hyphenIndices[hyphenIndex] < startIndexOfGlyphs ||
480 (hyphenIndices[hyphenIndex] > firstMiddleIndexOfElidedGlyphs && hyphenIndices[hyphenIndex] < secondMiddleIndexOfElidedGlyphs)))
486 for(uint32_t i = 0, glyphSize = glyphs.Size(); i < glyphSize; ++i)
489 bool addHyphen = ((hyphenIndex < hyphensCount) && hyphenIndices && ((i + startIndexOfGlyphs) == hyphenIndices[hyphenIndex]));
490 if(addHyphen && hyphens)
492 glyph = hyphens[hyphenIndex];
497 glyph = *(glyphsBuffer + i);
500 const bool isGlyphUnderlined = underlineEnabled || IsGlyphUnderlined(i, underlineRuns);
501 thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || isGlyphUnderlined;
503 // No operation for white space
504 if(glyph.width && glyph.height)
506 // Are we still using the same fontId as previous
507 if(isGlyphUnderlined && (glyph.fontId != lastUnderlinedFontId))
509 // We need to fetch fresh font underline metrics
510 FontMetrics fontMetrics;
511 mFontClient.GetFontMetrics(glyph.fontId, fontMetrics);
512 currentUnderlinePosition = ceil(fabsf(fontMetrics.underlinePosition));
513 const float descender = ceil(fabsf(fontMetrics.descender));
515 if(fabsf(underlineHeight) < Math::MACHINE_EPSILON_1000)
517 currentUnderlineThickness = fontMetrics.underlineThickness;
519 // Ensure underline will be at least a pixel high
520 if(currentUnderlineThickness < ONE)
522 currentUnderlineThickness = ONE;
526 currentUnderlineThickness = ceil(currentUnderlineThickness);
530 // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
531 if(currentUnderlinePosition > descender)
533 currentUnderlinePosition = descender;
536 if(fabsf(currentUnderlinePosition) < Math::MACHINE_EPSILON_1000)
538 // Move offset down by one ( EFL behavior )
539 currentUnderlinePosition = ONE;
542 lastUnderlinedFontId = glyph.fontId;
546 AtlasGlyphManager::GlyphStyle style;
547 style.isItalic = glyph.isItalicRequired;
548 style.isBold = glyph.isBoldRequired;
550 // Retrieves and caches the glyph's bitmap.
551 CacheGlyph(glyph, lastFontId, style, slot);
553 // Retrieves and caches the outline glyph's bitmap.
556 style.outline = outlineWidth;
557 CacheGlyph(glyph, lastFontId, style, slotOutline);
560 // Move the origin (0,0) of the mesh to the center of the actor
561 Vector2 position = *(positionsBuffer + i);
565 GlyphInfo tempInfo = *(glyphsBuffer + i);
566 position.x = position.x + tempInfo.advance - tempInfo.xBearing + glyph.xBearing;
567 position.y += tempInfo.yBearing - glyph.yBearing;
570 position = Vector2(roundf(position.x), position.y) - halfTextSize - lineOffsetPosition; // roundf() avoids pixel alignment issues.
572 if(0u != slot.mImageId) // invalid slot id, glyph has failed to be added to atlas
574 Vector2 positionPlusOutlineOffset = position;
577 // Add an offset to the text.
578 const float outlineWidthOffset = static_cast<float>(outlineWidth);
579 positionPlusOutlineOffset += Vector2(outlineWidthOffset, outlineWidthOffset);
582 // Get the color of the character.
583 const ColorIndex colorIndex = useDefaultColor ? 0u : *(colorIndicesBuffer + i);
584 const Vector4& color = (useDefaultColor || (0u == colorIndex)) ? defaultColor : *(colorsBuffer + colorIndex - 1u);
587 positionPlusOutlineOffset,
592 currentUnderlinePosition,
593 currentUnderlineThickness,
599 lastFontId = glyph.fontId; // Prevents searching for existing blocksizes when string of the same fontId.
602 if(isOutline && (0u != slotOutline.mImageId)) // invalid slot id, glyph has failed to be added to atlas
610 currentUnderlinePosition,
611 currentUnderlineThickness,
612 meshContainerOutline,
618 //The new underlined chunk. Add new id if they are not consecutive indices (this is for Markup case)
619 // Examples: "Hello <u>World</u> Hello <u>World</u>", "<u>World</u> Hello <u>World</u>", "<u> World</u> Hello <u>World</u>"
620 if(isPreUnderlined && (isPreUnderlined != isGlyphUnderlined))
624 //Keep status of underlined for previous glyph to check consecutive indices
625 isPreUnderlined = isGlyphUnderlined;
634 // Now remove references for the old text
636 mTextCache.Swap(newTextCache);
638 if(thereAreUnderlinedGlyphs)
640 // Check to see if any of the text needs an underline
641 GenerateUnderlines(meshContainer, extents, underlineColor);
644 // For each MeshData object, create a mesh actor and add to the renderable actor
645 bool isShadowDrawn = false;
646 if(!meshContainerOutline.empty())
648 const bool drawShadow = STYLE_DROP_SHADOW == style;
649 CreateActors(meshContainerOutline,
655 animatablePropertyIndex,
658 isShadowDrawn = drawShadow;
661 // For each MeshData object, create a mesh actor and add to the renderable actor
662 if(!meshContainer.empty())
664 const bool drawShadow = !isShadowDrawn && (STYLE_DROP_SHADOW == style);
665 CreateActors(meshContainer,
671 animatablePropertyIndex,
675 #if defined(DEBUG_ENABLED)
676 Toolkit::AtlasGlyphManager::Metrics metrics = mGlyphManager.GetMetrics();
677 DALI_LOG_INFO(gLogFilter, Debug::General, "TextAtlasRenderer::GlyphManager::GlyphCount: %i, AtlasCount: %i, TextureMemoryUse: %iK\n", metrics.mGlyphCount, metrics.mAtlasMetrics.mAtlasCount, metrics.mAtlasMetrics.mTextureMemoryUsed / 1024);
679 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "%s\n", metrics.mVerboseGlyphCounts.c_str());
681 for(uint32_t i = 0; i < metrics.mAtlasMetrics.mAtlasCount; ++i)
683 DALI_LOG_INFO(gLogFilter, Debug::Verbose, " Atlas [%i] %sPixels: %s Size: %ix%i, BlockSize: %ix%i, BlocksUsed: %i/%i\n", i + 1, i > 8 ? "" : " ", metrics.mAtlasMetrics.mAtlasMetrics[i].mPixelFormat == Pixel::L8 ? "L8 " : "BGRA", metrics.mAtlasMetrics.mAtlasMetrics[i].mSize.mWidth, metrics.mAtlasMetrics.mAtlasMetrics[i].mSize.mHeight, metrics.mAtlasMetrics.mAtlasMetrics[i].mSize.mBlockWidth, metrics.mAtlasMetrics.mAtlasMetrics[i].mSize.mBlockHeight, metrics.mAtlasMetrics.mAtlasMetrics[i].mBlocksUsed, metrics.mAtlasMetrics.mAtlasMetrics[i].mTotalBlocks);
690 for(Vector<TextCacheEntry>::Iterator oldTextIter = mTextCache.Begin(); oldTextIter != mTextCache.End(); ++oldTextIter)
692 AtlasGlyphManager::GlyphStyle style;
693 style.outline = oldTextIter->mOutlineWidth;
694 style.isItalic = oldTextIter->isItalic;
695 style.isBold = oldTextIter->isBold;
696 mGlyphManager.AdjustReferenceCount(oldTextIter->mFontId, oldTextIter->mIndex, style, -1 /*decrement*/);
698 mTextCache.Resize(0);
701 Actor CreateMeshActor(Actor textControl, Property::Index animatablePropertyIndex, const Vector4& defaultColor, const MeshRecord& meshRecord, const Vector2& actorSize, Style style)
703 VertexBuffer quadVertices = VertexBuffer::New(mQuadVertexFormat);
704 quadVertices.SetData(const_cast<AtlasManager::Vertex2D*>(&meshRecord.mMesh.mVertices[0]), meshRecord.mMesh.mVertices.Size());
706 Geometry quadGeometry = Geometry::New();
707 quadGeometry.AddVertexBuffer(quadVertices);
708 quadGeometry.SetIndexBuffer(&meshRecord.mMesh.mIndices[0], meshRecord.mMesh.mIndices.Size());
710 TextureSet textureSet(mGlyphManager.GetTextures(meshRecord.mAtlasId));
712 // Choose the shader to use.
713 const bool isColorShader = (STYLE_DROP_SHADOW != style) && (Pixel::BGRA8888 == mGlyphManager.GetPixelFormat(meshRecord.mAtlasId));
717 // The glyph is an emoji and is not a shadow.
720 mShaderRgba = Shader::New(SHADER_TEXT_ATLAS_SHADER_VERT, SHADER_TEXT_ATLAS_RGBA_SHADER_FRAG);
722 shader = mShaderRgba;
726 // The glyph is text or a shadow.
729 mShaderL8 = Shader::New(SHADER_TEXT_ATLAS_SHADER_VERT, SHADER_TEXT_ATLAS_L8_SHADER_FRAG);
734 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "defaultColor[%f, %f, %f, %f ]\n", defaultColor.r, defaultColor.g, defaultColor.b, defaultColor.a);
736 Dali::Property::Index shaderTextColorIndex = shader.RegisterProperty("textColorAnimatable", defaultColor);
738 if(animatablePropertyIndex != Property::INVALID_INDEX)
740 // create constraint for the animatable text's color Property with textColorAnimatable in the shader.
741 if(shaderTextColorIndex)
743 Constraint constraint = Constraint::New<Vector4>(shader, shaderTextColorIndex, EqualToConstraint());
744 constraint.AddSource(Source(textControl, animatablePropertyIndex));
750 // If not animating the text colour then set to 1's so shader uses the current vertex color
751 shader.RegisterProperty("textColorAnimatable", Vector4(1.0, 1.0, 1.0, 1.0));
754 Dali::Renderer renderer = Dali::Renderer::New(quadGeometry, shader);
755 renderer.SetTextures(textureSet);
756 renderer.SetProperty(Dali::Renderer::Property::BLEND_MODE, BlendMode::ON);
757 renderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, DepthIndex::CONTENT + mDepth);
759 Actor actor = Actor::New();
760 #if defined(DEBUG_ENABLED)
761 actor.SetProperty(Dali::Actor::Property::NAME, "Text renderable actor");
763 actor.AddRenderer(renderer);
764 // Keep all of the origins aligned
765 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
766 actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
767 actor.SetProperty(Actor::Property::SIZE, actorSize);
768 actor.RegisterProperty("uOffset", Vector2::ZERO);
769 actor.SetProperty(Actor::Property::COLOR_MODE, USE_OWN_MULTIPLY_PARENT_COLOR);
774 void StitchTextMesh(std::vector<MeshRecord>& meshContainer,
775 AtlasManager::Mesh2D& newMesh,
776 Vector<Extent>& extents,
779 float underlinePosition,
780 float underlineThickness,
781 AtlasManager::AtlasSlot& slot,
782 uint32_t underlineChunkId)
786 float left = newMesh.mVertices[0].mPosition.x;
787 float right = newMesh.mVertices[1].mPosition.x;
789 // Check to see if there's a mesh data object that references the same atlas ?
791 for(std::vector<MeshRecord>::iterator mIt = meshContainer.begin(),
792 mEndIt = meshContainer.end();
796 if(slot.mAtlasId == mIt->mAtlasId)
798 // Append the mesh to the existing mesh and adjust any extents
799 Toolkit::Internal::AtlasMeshFactory::AppendMesh(mIt->mMesh, newMesh);
803 AdjustExtents(extents,
818 // No mesh data object currently exists that references this atlas, so create a new one
819 MeshRecord meshRecord;
820 meshRecord.mAtlasId = slot.mAtlasId;
821 meshRecord.mMesh = newMesh;
822 meshContainer.push_back(meshRecord);
826 // Adjust extents for this new meshrecord
827 AdjustExtents(extents,
829 meshContainer.size() - 1u,
840 void AdjustExtents(Vector<Extent>& extents,
841 std::vector<MeshRecord>& meshRecords,
846 float underlinePosition,
847 float underlineThickness,
848 uint32_t underlineChunkId)
850 bool foundExtent = false;
851 for(Vector<Extent>::Iterator eIt = extents.Begin(),
852 eEndIt = extents.End();
856 if(Equals(baseLine, eIt->mBaseLine) && underlineChunkId == eIt->mUnderlineChunkId)
859 if(left < eIt->mLeft)
863 if(right > eIt->mRight)
868 if(underlinePosition > eIt->mUnderlinePosition)
870 eIt->mUnderlinePosition = underlinePosition;
872 if(underlineThickness > eIt->mUnderlineThickness)
874 eIt->mUnderlineThickness = underlineThickness;
882 extent.mRight = right;
883 extent.mBaseLine = baseLine;
884 extent.mUnderlinePosition = underlinePosition;
885 extent.mUnderlineThickness = underlineThickness;
886 extent.mMeshRecordIndex = index;
887 extent.mUnderlineChunkId = underlineChunkId;
888 extents.PushBack(extent);
892 void CalculateBlocksSize(const Vector<GlyphInfo>& glyphs)
894 for(Vector<GlyphInfo>::ConstIterator glyphIt = glyphs.Begin(),
895 glyphEndIt = glyphs.End();
896 glyphIt != glyphEndIt;
899 const FontId fontId = (*glyphIt).fontId;
900 bool foundFont = false;
902 for(std::vector<MaxBlockSize>::const_iterator blockIt = mBlockSizes.begin(),
903 blockEndIt = mBlockSizes.end();
904 blockIt != blockEndIt;
907 if((*blockIt).mFontId == fontId) // Different size fonts will have a different fontId
909 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Text::AtlasRenderer::CalculateBlocksSize match found fontID(%u) glyphIndex(%u)\n", fontId, (*glyphIt).index);
917 FontMetrics fontMetrics;
918 mFontClient.GetFontMetrics(fontId, fontMetrics);
920 MaxBlockSize maxBlockSize;
921 maxBlockSize.mNeededBlockWidth = static_cast<uint32_t>(fontMetrics.height);
922 maxBlockSize.mNeededBlockHeight = maxBlockSize.mNeededBlockWidth;
923 maxBlockSize.mFontId = fontId;
924 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Text::AtlasRenderer::CalculateBlocksSize New font with no matched blocksize, setting blocksize[%u]\n", maxBlockSize.mNeededBlockWidth);
925 mBlockSizes.push_back(maxBlockSize);
930 void GenerateUnderlines(std::vector<MeshRecord>& meshRecords,
931 Vector<Extent>& extents,
932 const Vector4& underlineColor)
934 AtlasManager::Mesh2D newMesh;
935 unsigned short faceIndex = 0;
936 for(Vector<Extent>::ConstIterator eIt = extents.Begin(),
937 eEndIt = extents.End();
941 AtlasManager::Vertex2D vert;
942 uint32_t index = eIt->mMeshRecordIndex;
943 Vector2 uv = mGlyphManager.GetAtlasSize(meshRecords[index].mAtlasId);
945 // Make sure we don't hit texture edge for single pixel texture ( filled pixel is in top left of every atlas )
946 float u = HALF / uv.x;
947 float v = HALF / uv.y;
948 float thickness = eIt->mUnderlineThickness;
949 float baseLine = eIt->mBaseLine + eIt->mUnderlinePosition - (thickness * HALF);
950 float tlx = eIt->mLeft;
951 float brx = eIt->mRight;
953 vert.mPosition.x = tlx;
954 vert.mPosition.y = baseLine;
955 vert.mTexCoords.x = ZERO;
956 vert.mTexCoords.y = ZERO;
957 vert.mColor = underlineColor;
958 newMesh.mVertices.PushBack(vert);
960 vert.mPosition.x = brx;
961 vert.mPosition.y = baseLine;
962 vert.mTexCoords.x = u;
963 vert.mColor = underlineColor;
964 newMesh.mVertices.PushBack(vert);
966 vert.mPosition.x = tlx;
967 vert.mPosition.y = baseLine + thickness;
968 vert.mTexCoords.x = ZERO;
969 vert.mTexCoords.y = v;
970 vert.mColor = underlineColor;
971 newMesh.mVertices.PushBack(vert);
973 vert.mPosition.x = brx;
974 vert.mPosition.y = baseLine + thickness;
975 vert.mTexCoords.x = u;
976 vert.mColor = underlineColor;
977 newMesh.mVertices.PushBack(vert);
979 // Six indices in counter clockwise winding
980 newMesh.mIndices.PushBack(faceIndex + 1u);
981 newMesh.mIndices.PushBack(faceIndex);
982 newMesh.mIndices.PushBack(faceIndex + 2u);
983 newMesh.mIndices.PushBack(faceIndex + 2u);
984 newMesh.mIndices.PushBack(faceIndex + 3u);
985 newMesh.mIndices.PushBack(faceIndex + 1u);
988 Toolkit::Internal::AtlasMeshFactory::AppendMesh(meshRecords[index].mMesh, newMesh);
992 Actor mActor; ///< The actor parent which renders the text
993 AtlasGlyphManager mGlyphManager; ///< Glyph Manager to handle upload and caching
994 TextAbstraction::FontClient mFontClient; ///< The font client used to supply glyph information
995 Shader mShaderL8; ///< The shader for glyphs and emoji's shadows.
996 Shader mShaderRgba; ///< The shader for emojis.
997 std::vector<MaxBlockSize> mBlockSizes; ///< Maximum size needed to contain a glyph in a block within a new atlas
998 Vector<TextCacheEntry> mTextCache; ///< Caches data from previous render
999 Property::Map mQuadVertexFormat; ///< Describes the vertex format for text
1000 int mDepth; ///< DepthIndex passed by control when connect to stage
1003 Text::RendererPtr AtlasRenderer::New()
1005 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Text::AtlasRenderer::New()\n");
1007 return Text::RendererPtr(new AtlasRenderer());
1010 Actor AtlasRenderer::Render(Text::ViewInterface& view,
1012 Property::Index animatablePropertyIndex,
1013 float& alignmentOffset,
1016 DALI_LOG_INFO(gLogFilter, Debug::General, "Text::AtlasRenderer::Render()\n");
1018 UnparentAndReset(mImpl->mActor);
1020 Length numberOfGlyphs = view.GetNumberOfGlyphs();
1022 if(numberOfGlyphs > 0u)
1024 Vector<GlyphInfo> glyphs;
1025 glyphs.Resize(numberOfGlyphs);
1027 Vector<Vector2> positions;
1028 positions.Resize(numberOfGlyphs);
1030 numberOfGlyphs = view.GetGlyphs(glyphs.Begin(),
1036 glyphs.Resize(numberOfGlyphs);
1037 positions.Resize(numberOfGlyphs);
1039 const Vector4* const colorsBuffer = view.GetColors();
1040 const ColorIndex* const colorIndicesBuffer = view.GetColorIndices();
1041 const Vector4& defaultColor = view.GetTextColor();
1043 mImpl->AddGlyphs(view,
1045 animatablePropertyIndex,
1054 /* In the case where AddGlyphs does not create a renderable Actor for example when glyphs are all whitespace create a new Actor. */
1055 /* This renderable actor is used to position the text, other "decorations" can rely on there always being an Actor regardless of it is whitespace or regular text. */
1058 mImpl->mActor = Actor::New();
1062 return mImpl->mActor;
1065 AtlasRenderer::AtlasRenderer()
1070 AtlasRenderer::~AtlasRenderer()
1072 mImpl->RemoveText();