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)))
259 (default_width < maximumTextAtlasSize.width &&
260 default_height < maximumTextAtlasSize.height))
262 default_width <<= 1u;
263 default_height <<= 1u;
265 mGlyphManager.SetNewAtlasSize(default_width,
267 blockSize.mNeededBlockWidth,
268 blockSize.mNeededBlockHeight);
270 // Locate a new slot for our glyph
271 mGlyphManager.Add(glyph, style, bitmap, slot); // slot will be 0 is glyph not added
277 // We have 2+ copies of the same glyph
278 mGlyphManager.AdjustReferenceCount(glyph.fontId, glyph.index, style, 1); //increment
282 void GenerateMesh(const GlyphInfo& glyph,
283 const Vector2& position,
284 const Vector4& color,
286 AtlasManager::AtlasSlot& slot,
288 float currentUnderlinePosition,
289 float currentUnderlineThickness,
290 std::vector<MeshRecord>& meshContainer,
291 Vector<TextCacheEntry>& newTextCache,
292 Vector<Extent>& extents,
293 uint32_t underlineChunkId)
295 // Generate mesh data for this quad, plugging in our supplied position
296 AtlasManager::Mesh2D newMesh;
297 mGlyphManager.GenerateMeshData(slot.mImageId, position, newMesh);
299 TextCacheEntry textCacheEntry;
300 textCacheEntry.mFontId = glyph.fontId;
301 textCacheEntry.mImageId = slot.mImageId;
302 textCacheEntry.mIndex = glyph.index;
303 textCacheEntry.mOutlineWidth = outline;
304 textCacheEntry.isItalic = glyph.isItalicRequired;
305 textCacheEntry.isBold = glyph.isBoldRequired;
307 newTextCache.PushBack(textCacheEntry);
309 AtlasManager::Vertex2D* verticesBuffer = newMesh.mVertices.Begin();
311 for(unsigned int index = 0u, size = newMesh.mVertices.Count();
315 AtlasManager::Vertex2D& vertex = *(verticesBuffer + index);
317 // Set the color of the vertex.
318 vertex.mColor = color;
321 // Find an existing mesh data object to attach to ( or create a new one, if we can't find one using the same atlas)
322 StitchTextMesh(meshContainer,
325 position.y + glyph.yBearing,
327 currentUnderlinePosition,
328 currentUnderlineThickness,
333 void CreateActors(const std::vector<MeshRecord>& meshContainer,
334 const Size& textSize,
335 const Vector4& color,
336 const Vector4& shadowColor,
337 const Vector2& shadowOffset,
339 Property::Index animatablePropertyIndex,
344 // Create a container actor to act as a common parent for text and shadow, to avoid color inheritence issues.
345 mActor = Actor::New();
346 mActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
347 mActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
348 mActor.SetProperty(Actor::Property::SIZE, textSize);
349 mActor.SetProperty(Actor::Property::COLOR_MODE, USE_OWN_MULTIPLY_PARENT_COLOR);
352 for(std::vector<MeshRecord>::const_iterator it = meshContainer.begin(),
353 endIt = meshContainer.end();
357 const MeshRecord& meshRecord = *it;
359 Actor actor = CreateMeshActor(textControl, animatablePropertyIndex, color, meshRecord, textSize, STYLE_NORMAL);
361 // Whether the actor has renderers.
362 const bool hasRenderer = actor.GetRendererCount() > 0u;
364 // Create an effect if necessary
368 // Change the color of the vertices.
369 for(Vector<AtlasManager::Vertex2D>::Iterator vIt = meshRecord.mMesh.mVertices.Begin(),
370 vEndIt = meshRecord.mMesh.mVertices.End();
374 AtlasManager::Vertex2D& vertex = *vIt;
376 vertex.mColor = shadowColor;
379 Actor shadowActor = CreateMeshActor(textControl, animatablePropertyIndex, color, meshRecord, textSize, STYLE_DROP_SHADOW);
380 #if defined(DEBUG_ENABLED)
381 shadowActor.SetProperty(Dali::Actor::Property::NAME, "Text Shadow renderable actor");
383 // Offset shadow in x and y
384 shadowActor.RegisterProperty("uOffset", shadowOffset);
385 Dali::Renderer renderer(shadowActor.GetRendererAt(0));
386 int depthIndex = renderer.GetProperty<int>(Dali::Renderer::Property::DEPTH_INDEX);
387 renderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, depthIndex - 1);
388 mActor.Add(shadowActor);
398 void AddGlyphs(Text::ViewInterface& view,
400 Property::Index animatablePropertyIndex,
401 const Vector<Vector2>& positions,
402 const Vector<GlyphInfo>& glyphs,
403 const Vector4& defaultColor,
404 const Vector4* const colorsBuffer,
405 const ColorIndex* const colorIndicesBuffer,
409 AtlasManager::AtlasSlot slot;
413 AtlasManager::AtlasSlot slotOutline;
414 slotOutline.mImageId = 0u;
415 slotOutline.mAtlasId = 0u;
417 std::vector<MeshRecord> meshContainer;
418 std::vector<MeshRecord> meshContainerOutline;
419 Vector<Extent> extents;
422 const Vector2& textSize(view.GetLayoutSize());
423 const Vector2 halfTextSize(textSize * 0.5f);
424 const Vector2& shadowOffset(view.GetShadowOffset());
425 const Vector4& shadowColor(view.GetShadowColor());
426 const bool underlineEnabled = view.IsUnderlineEnabled();
427 const Vector4& underlineColor(view.GetUnderlineColor());
428 const float underlineHeight = view.GetUnderlineHeight();
429 const uint16_t outlineWidth = view.GetOutlineWidth();
430 const Vector4& outlineColor(view.GetOutlineColor());
431 const bool isOutline = 0u != outlineWidth;
433 const bool useDefaultColor = (NULL == colorsBuffer);
435 // Get the underline runs.
436 const Length numberOfUnderlineRuns = view.GetNumberOfUnderlineRuns();
437 Vector<GlyphRun> underlineRuns;
438 underlineRuns.Resize(numberOfUnderlineRuns);
439 view.GetUnderlineRuns(underlineRuns.Begin(),
441 numberOfUnderlineRuns);
443 bool thereAreUnderlinedGlyphs = false;
445 float currentUnderlinePosition = ZERO;
446 float currentUnderlineThickness = underlineHeight;
447 FontId lastFontId = 0;
448 FontId lastUnderlinedFontId = 0;
449 Style style = STYLE_NORMAL;
451 if(fabsf(shadowOffset.x) > Math::MACHINE_EPSILON_1 || fabsf(shadowOffset.y) > Math::MACHINE_EPSILON_1)
453 style = STYLE_DROP_SHADOW;
456 CalculateBlocksSize(glyphs);
458 // Avoid emptying mTextCache (& removing references) until after incremented references for the new text
459 Vector<TextCacheEntry> newTextCache;
460 const GlyphInfo* const glyphsBuffer = glyphs.Begin();
461 const Vector2* const positionsBuffer = positions.Begin();
462 const Vector2 lineOffsetPosition(minLineOffset, 0.f);
464 //For septated underlined chunks. (this is for Markup case)
465 uint32_t underlineChunkId = 0u; // give id for each chunk.
466 bool isPreUnderlined = false; // status of underlined for previous glyph.
468 for(uint32_t i = 0, glyphSize = glyphs.Size(); i < glyphSize; ++i)
470 const GlyphInfo& glyph = *(glyphsBuffer + i);
471 const bool isGlyphUnderlined = underlineEnabled || IsGlyphUnderlined(i, underlineRuns);
472 thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || isGlyphUnderlined;
474 // No operation for white space
475 if(glyph.width && glyph.height)
477 // Are we still using the same fontId as previous
478 if(isGlyphUnderlined && (glyph.fontId != lastUnderlinedFontId))
480 // We need to fetch fresh font underline metrics
481 FontMetrics fontMetrics;
482 mFontClient.GetFontMetrics(glyph.fontId, fontMetrics);
483 currentUnderlinePosition = ceil(fabsf(fontMetrics.underlinePosition));
484 const float descender = ceil(fabsf(fontMetrics.descender));
486 if(fabsf(underlineHeight) < Math::MACHINE_EPSILON_1000)
488 currentUnderlineThickness = fontMetrics.underlineThickness;
490 // Ensure underline will be at least a pixel high
491 if(currentUnderlineThickness < ONE)
493 currentUnderlineThickness = ONE;
497 currentUnderlineThickness = ceil(currentUnderlineThickness);
501 // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
502 if(currentUnderlinePosition > descender)
504 currentUnderlinePosition = descender;
507 if(fabsf(currentUnderlinePosition) < Math::MACHINE_EPSILON_1000)
509 // Move offset down by one ( EFL behavior )
510 currentUnderlinePosition = ONE;
513 lastUnderlinedFontId = glyph.fontId;
517 AtlasGlyphManager::GlyphStyle style;
518 style.isItalic = glyph.isItalicRequired;
519 style.isBold = glyph.isBoldRequired;
521 // Retrieves and caches the glyph's bitmap.
522 CacheGlyph(glyph, lastFontId, style, slot);
524 // Retrieves and caches the outline glyph's bitmap.
527 style.outline = outlineWidth;
528 CacheGlyph(glyph, lastFontId, style, slotOutline);
531 // Move the origin (0,0) of the mesh to the center of the actor
532 const Vector2& temp = *(positionsBuffer + i);
533 const Vector2 position = Vector2(roundf(temp.x), temp.y) - halfTextSize - lineOffsetPosition; // roundf() avoids pixel alignment issues.
535 if(0u != slot.mImageId) // invalid slot id, glyph has failed to be added to atlas
537 Vector2 positionPlusOutlineOffset = position;
540 // Add an offset to the text.
541 const float outlineWidthOffset = static_cast<float>(outlineWidth);
542 positionPlusOutlineOffset += Vector2(outlineWidthOffset, outlineWidthOffset);
545 // Get the color of the character.
546 const ColorIndex colorIndex = useDefaultColor ? 0u : *(colorIndicesBuffer + i);
547 const Vector4& color = (useDefaultColor || (0u == colorIndex)) ? defaultColor : *(colorsBuffer + colorIndex - 1u);
550 positionPlusOutlineOffset,
555 currentUnderlinePosition,
556 currentUnderlineThickness,
562 lastFontId = glyph.fontId; // Prevents searching for existing blocksizes when string of the same fontId.
565 if(isOutline && (0u != slotOutline.mImageId)) // invalid slot id, glyph has failed to be added to atlas
573 currentUnderlinePosition,
574 currentUnderlineThickness,
575 meshContainerOutline,
582 //The new underlined chunk. Add new id if they are not consecutive indices (this is for Markup case)
583 // Examples: "Hello <u>World</u> Hello <u>World</u>", "<u>World</u> Hello <u>World</u>", "<u> World</u> Hello <u>World</u>"
584 if( isPreUnderlined && (isPreUnderlined != isGlyphUnderlined))
588 //Keep status of underlined for previous glyph to check consecutive indices
589 isPreUnderlined = isGlyphUnderlined;
593 // Now remove references for the old text
595 mTextCache.Swap(newTextCache);
597 if(thereAreUnderlinedGlyphs)
599 // Check to see if any of the text needs an underline
600 GenerateUnderlines(meshContainer, extents, underlineColor);
603 // For each MeshData object, create a mesh actor and add to the renderable actor
604 bool isShadowDrawn = false;
605 if(!meshContainerOutline.empty())
607 const bool drawShadow = STYLE_DROP_SHADOW == style;
608 CreateActors(meshContainerOutline,
614 animatablePropertyIndex,
617 isShadowDrawn = drawShadow;
620 // For each MeshData object, create a mesh actor and add to the renderable actor
621 if(!meshContainer.empty())
623 const bool drawShadow = !isShadowDrawn && (STYLE_DROP_SHADOW == style);
624 CreateActors(meshContainer,
630 animatablePropertyIndex,
634 #if defined(DEBUG_ENABLED)
635 Toolkit::AtlasGlyphManager::Metrics metrics = mGlyphManager.GetMetrics();
636 DALI_LOG_INFO(gLogFilter, Debug::General, "TextAtlasRenderer::GlyphManager::GlyphCount: %i, AtlasCount: %i, TextureMemoryUse: %iK\n", metrics.mGlyphCount, metrics.mAtlasMetrics.mAtlasCount, metrics.mAtlasMetrics.mTextureMemoryUsed / 1024);
638 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "%s\n", metrics.mVerboseGlyphCounts.c_str());
640 for(uint32_t i = 0; i < metrics.mAtlasMetrics.mAtlasCount; ++i)
642 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);
649 for(Vector<TextCacheEntry>::Iterator oldTextIter = mTextCache.Begin(); oldTextIter != mTextCache.End(); ++oldTextIter)
651 AtlasGlyphManager::GlyphStyle style;
652 style.outline = oldTextIter->mOutlineWidth;
653 style.isItalic = oldTextIter->isItalic;
654 style.isBold = oldTextIter->isBold;
655 mGlyphManager.AdjustReferenceCount(oldTextIter->mFontId, oldTextIter->mIndex, style, -1 /*decrement*/);
657 mTextCache.Resize(0);
660 Actor CreateMeshActor(Actor textControl, Property::Index animatablePropertyIndex, const Vector4& defaultColor, const MeshRecord& meshRecord, const Vector2& actorSize, Style style)
662 VertexBuffer quadVertices = VertexBuffer::New(mQuadVertexFormat);
663 quadVertices.SetData(const_cast<AtlasManager::Vertex2D*>(&meshRecord.mMesh.mVertices[0]), meshRecord.mMesh.mVertices.Size());
665 Geometry quadGeometry = Geometry::New();
666 quadGeometry.AddVertexBuffer(quadVertices);
667 quadGeometry.SetIndexBuffer(&meshRecord.mMesh.mIndices[0], meshRecord.mMesh.mIndices.Size());
669 TextureSet textureSet(mGlyphManager.GetTextures(meshRecord.mAtlasId));
671 // Choose the shader to use.
672 const bool isColorShader = (STYLE_DROP_SHADOW != style) && (Pixel::BGRA8888 == mGlyphManager.GetPixelFormat(meshRecord.mAtlasId));
676 // The glyph is an emoji and is not a shadow.
679 mShaderRgba = Shader::New(SHADER_TEXT_ATLAS_SHADER_VERT, SHADER_TEXT_ATLAS_RGBA_SHADER_FRAG);
681 shader = mShaderRgba;
685 // The glyph is text or a shadow.
688 mShaderL8 = Shader::New(SHADER_TEXT_ATLAS_SHADER_VERT, SHADER_TEXT_ATLAS_L8_SHADER_FRAG);
693 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "defaultColor[%f, %f, %f, %f ]\n", defaultColor.r, defaultColor.g, defaultColor.b, defaultColor.a);
695 Dali::Property::Index shaderTextColorIndex = shader.RegisterProperty("textColorAnimatable", defaultColor);
697 if(animatablePropertyIndex != Property::INVALID_INDEX)
699 // create constraint for the animatable text's color Property with textColorAnimatable in the shader.
700 if(shaderTextColorIndex)
702 Constraint constraint = Constraint::New<Vector4>(shader, shaderTextColorIndex, EqualToConstraint());
703 constraint.AddSource(Source(textControl, animatablePropertyIndex));
709 // If not animating the text colour then set to 1's so shader uses the current vertex color
710 shader.RegisterProperty("textColorAnimatable", Vector4(1.0, 1.0, 1.0, 1.0));
713 Dali::Renderer renderer = Dali::Renderer::New(quadGeometry, shader);
714 renderer.SetTextures(textureSet);
715 renderer.SetProperty(Dali::Renderer::Property::BLEND_MODE, BlendMode::ON);
716 renderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, DepthIndex::CONTENT + mDepth);
718 Actor actor = Actor::New();
719 #if defined(DEBUG_ENABLED)
720 actor.SetProperty(Dali::Actor::Property::NAME, "Text renderable actor");
722 actor.AddRenderer(renderer);
723 // Keep all of the origins aligned
724 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
725 actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
726 actor.SetProperty(Actor::Property::SIZE, actorSize);
727 actor.RegisterProperty("uOffset", Vector2::ZERO);
728 actor.SetProperty(Actor::Property::COLOR_MODE, USE_OWN_MULTIPLY_PARENT_COLOR);
733 void StitchTextMesh(std::vector<MeshRecord>& meshContainer,
734 AtlasManager::Mesh2D& newMesh,
735 Vector<Extent>& extents,
738 float underlinePosition,
739 float underlineThickness,
740 AtlasManager::AtlasSlot& slot,
741 uint32_t underlineChunkId)
745 float left = newMesh.mVertices[0].mPosition.x;
746 float right = newMesh.mVertices[1].mPosition.x;
748 // Check to see if there's a mesh data object that references the same atlas ?
750 for(std::vector<MeshRecord>::iterator mIt = meshContainer.begin(),
751 mEndIt = meshContainer.end();
755 if(slot.mAtlasId == mIt->mAtlasId)
757 // Append the mesh to the existing mesh and adjust any extents
758 Toolkit::Internal::AtlasMeshFactory::AppendMesh(mIt->mMesh, newMesh);
762 AdjustExtents(extents,
777 // No mesh data object currently exists that references this atlas, so create a new one
778 MeshRecord meshRecord;
779 meshRecord.mAtlasId = slot.mAtlasId;
780 meshRecord.mMesh = newMesh;
781 meshContainer.push_back(meshRecord);
785 // Adjust extents for this new meshrecord
786 AdjustExtents(extents,
788 meshContainer.size() - 1u,
799 void AdjustExtents(Vector<Extent>& extents,
800 std::vector<MeshRecord>& meshRecords,
805 float underlinePosition,
806 float underlineThickness,
807 uint32_t underlineChunkId)
809 bool foundExtent = false;
810 for(Vector<Extent>::Iterator eIt = extents.Begin(),
811 eEndIt = extents.End();
815 if(Equals(baseLine, eIt->mBaseLine) && underlineChunkId == eIt->mUnderlineChunkId)
818 if(left < eIt->mLeft)
822 if(right > eIt->mRight)
827 if(underlinePosition > eIt->mUnderlinePosition)
829 eIt->mUnderlinePosition = underlinePosition;
831 if(underlineThickness > eIt->mUnderlineThickness)
833 eIt->mUnderlineThickness = underlineThickness;
841 extent.mRight = right;
842 extent.mBaseLine = baseLine;
843 extent.mUnderlinePosition = underlinePosition;
844 extent.mUnderlineThickness = underlineThickness;
845 extent.mMeshRecordIndex = index;
846 extent.mUnderlineChunkId = underlineChunkId;
847 extents.PushBack(extent);
851 void CalculateBlocksSize(const Vector<GlyphInfo>& glyphs)
853 for(Vector<GlyphInfo>::ConstIterator glyphIt = glyphs.Begin(),
854 glyphEndIt = glyphs.End();
855 glyphIt != glyphEndIt;
858 const FontId fontId = (*glyphIt).fontId;
859 bool foundFont = false;
861 for(std::vector<MaxBlockSize>::const_iterator blockIt = mBlockSizes.begin(),
862 blockEndIt = mBlockSizes.end();
863 blockIt != blockEndIt;
866 if((*blockIt).mFontId == fontId) // Different size fonts will have a different fontId
868 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Text::AtlasRenderer::CalculateBlocksSize match found fontID(%u) glyphIndex(%u)\n", fontId, (*glyphIt).index);
876 FontMetrics fontMetrics;
877 mFontClient.GetFontMetrics(fontId, fontMetrics);
879 MaxBlockSize maxBlockSize;
880 maxBlockSize.mNeededBlockWidth = static_cast<uint32_t>(fontMetrics.height);
881 maxBlockSize.mNeededBlockHeight = maxBlockSize.mNeededBlockWidth;
882 maxBlockSize.mFontId = fontId;
883 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Text::AtlasRenderer::CalculateBlocksSize New font with no matched blocksize, setting blocksize[%u]\n", maxBlockSize.mNeededBlockWidth);
884 mBlockSizes.push_back(maxBlockSize);
889 void GenerateUnderlines(std::vector<MeshRecord>& meshRecords,
890 Vector<Extent>& extents,
891 const Vector4& underlineColor)
893 AtlasManager::Mesh2D newMesh;
894 unsigned short faceIndex = 0;
895 for(Vector<Extent>::ConstIterator eIt = extents.Begin(),
896 eEndIt = extents.End();
900 AtlasManager::Vertex2D vert;
901 uint32_t index = eIt->mMeshRecordIndex;
902 Vector2 uv = mGlyphManager.GetAtlasSize(meshRecords[index].mAtlasId);
904 // Make sure we don't hit texture edge for single pixel texture ( filled pixel is in top left of every atlas )
905 float u = HALF / uv.x;
906 float v = HALF / uv.y;
907 float thickness = eIt->mUnderlineThickness;
908 float baseLine = eIt->mBaseLine + eIt->mUnderlinePosition - (thickness * HALF);
909 float tlx = eIt->mLeft;
910 float brx = eIt->mRight;
912 vert.mPosition.x = tlx;
913 vert.mPosition.y = baseLine;
914 vert.mTexCoords.x = ZERO;
915 vert.mTexCoords.y = ZERO;
916 vert.mColor = underlineColor;
917 newMesh.mVertices.PushBack(vert);
919 vert.mPosition.x = brx;
920 vert.mPosition.y = baseLine;
921 vert.mTexCoords.x = u;
922 vert.mColor = underlineColor;
923 newMesh.mVertices.PushBack(vert);
925 vert.mPosition.x = tlx;
926 vert.mPosition.y = baseLine + thickness;
927 vert.mTexCoords.x = ZERO;
928 vert.mTexCoords.y = v;
929 vert.mColor = underlineColor;
930 newMesh.mVertices.PushBack(vert);
932 vert.mPosition.x = brx;
933 vert.mPosition.y = baseLine + thickness;
934 vert.mTexCoords.x = u;
935 vert.mColor = underlineColor;
936 newMesh.mVertices.PushBack(vert);
938 // Six indices in counter clockwise winding
939 newMesh.mIndices.PushBack(faceIndex + 1u);
940 newMesh.mIndices.PushBack(faceIndex);
941 newMesh.mIndices.PushBack(faceIndex + 2u);
942 newMesh.mIndices.PushBack(faceIndex + 2u);
943 newMesh.mIndices.PushBack(faceIndex + 3u);
944 newMesh.mIndices.PushBack(faceIndex + 1u);
947 Toolkit::Internal::AtlasMeshFactory::AppendMesh(meshRecords[index].mMesh, newMesh);
951 Actor mActor; ///< The actor parent which renders the text
952 AtlasGlyphManager mGlyphManager; ///< Glyph Manager to handle upload and caching
953 TextAbstraction::FontClient mFontClient; ///< The font client used to supply glyph information
954 Shader mShaderL8; ///< The shader for glyphs and emoji's shadows.
955 Shader mShaderRgba; ///< The shader for emojis.
956 std::vector<MaxBlockSize> mBlockSizes; ///< Maximum size needed to contain a glyph in a block within a new atlas
957 Vector<TextCacheEntry> mTextCache; ///< Caches data from previous render
958 Property::Map mQuadVertexFormat; ///< Describes the vertex format for text
959 int mDepth; ///< DepthIndex passed by control when connect to stage
962 Text::RendererPtr AtlasRenderer::New()
964 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Text::AtlasRenderer::New()\n");
966 return Text::RendererPtr(new AtlasRenderer());
969 Actor AtlasRenderer::Render(Text::ViewInterface& view,
971 Property::Index animatablePropertyIndex,
972 float& alignmentOffset,
975 DALI_LOG_INFO(gLogFilter, Debug::General, "Text::AtlasRenderer::Render()\n");
977 UnparentAndReset(mImpl->mActor);
979 Length numberOfGlyphs = view.GetNumberOfGlyphs();
981 if(numberOfGlyphs > 0u)
983 Vector<GlyphInfo> glyphs;
984 glyphs.Resize(numberOfGlyphs);
986 Vector<Vector2> positions;
987 positions.Resize(numberOfGlyphs);
989 numberOfGlyphs = view.GetGlyphs(glyphs.Begin(),
995 glyphs.Resize(numberOfGlyphs);
996 positions.Resize(numberOfGlyphs);
998 const Vector4* const colorsBuffer = view.GetColors();
999 const ColorIndex* const colorIndicesBuffer = view.GetColorIndices();
1000 const Vector4& defaultColor = view.GetTextColor();
1002 mImpl->AddGlyphs(view,
1004 animatablePropertyIndex,
1013 /* In the case where AddGlyphs does not create a renderable Actor for example when glyphs are all whitespace create a new Actor. */
1014 /* 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. */
1017 mImpl->mActor = Actor::New();
1021 return mImpl->mActor;
1024 AtlasRenderer::AtlasRenderer()
1029 AtlasRenderer::~AtlasRenderer()
1031 mImpl->RemoveText();