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 DEFAULT_ATLAS_WIDTH = 512u;
50 const uint32_t DEFAULT_ATLAS_HEIGHT = 512u;
51 const uint32_t MAX_ATLAS_WIDTH = 1024u;
52 const uint32_t MAX_ATLAS_HEIGHT = 1024u;
53 const uint32_t DOUBLE_PIXEL_PADDING = 4u;//Padding will be added twice to Atlas
54 const uint16_t NO_OUTLINE = 0u;
57 struct AtlasRenderer::Impl
73 AtlasManager::Mesh2D mMesh;
77 * brief Struct used to generate the underline mesh.
78 * There is one Extent per line of text.
86 mUnderlinePosition(0.0f),
87 mUnderlineThickness(0.0f),
96 float mUnderlinePosition;
97 float mUnderlineThickness;
98 uint32_t mMeshRecordIndex;
99 uint32_t mUnderlineChunkId;
106 mNeededBlockWidth(0),
107 mNeededBlockHeight(0)
112 uint32_t mNeededBlockWidth;
113 uint32_t mNeededBlockHeight;
125 Text::GlyphIndex mIndex;
128 struct TextCacheEntry
141 Text::GlyphIndex mIndex;
143 uint16_t mOutlineWidth;
151 mGlyphManager = AtlasGlyphManager::Get();
152 mFontClient = TextAbstraction::FontClient::Get();
154 mQuadVertexFormat["aPosition"] = Property::VECTOR2;
155 mQuadVertexFormat["aTexCoord"] = Property::VECTOR2;
156 mQuadVertexFormat["aColor"] = Property::VECTOR4;
159 bool IsGlyphUnderlined(GlyphIndex index,
160 const Vector<GlyphRun>& underlineRuns)
162 for(Vector<GlyphRun>::ConstIterator it = underlineRuns.Begin(),
163 endIt = underlineRuns.End();
167 const GlyphRun& run = *it;
169 if((run.glyphIndex <= index) && (index < run.glyphIndex + run.numberOfGlyphs))
178 void CacheGlyph(const GlyphInfo& glyph, FontId lastFontId, const AtlasGlyphManager::GlyphStyle& style, AtlasManager::AtlasSlot& slot)
180 const bool glyphNotCached = !mGlyphManager.IsCached(glyph.fontId, glyph.index, style, slot); // Check FontGlyphRecord vector for entry with glyph index and fontId
182 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "AddGlyphs fontID[%u] glyphIndex[%u] [%s]\n", glyph.fontId, glyph.index, (glyphNotCached) ? "not cached" : "cached");
186 MaxBlockSize& blockSize = mBlockSizes[0u];
188 if(lastFontId != glyph.fontId)
191 // 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.
192 // CalculateBlocksSize() above ensures a block size entry exists.
193 for(std::vector<MaxBlockSize>::const_iterator it = mBlockSizes.begin(),
194 endIt = mBlockSizes.end();
198 const MaxBlockSize& blockSizeEntry = *it;
199 if(blockSizeEntry.mFontId == glyph.fontId)
201 blockSize = mBlockSizes[index];
206 // Create a new image for the glyph
209 // Whether the glyph is an outline.
210 const bool isOutline = 0u != style.outline;
212 // Whether the current glyph is a color one.
213 const bool isColorGlyph = mFontClient.IsColorGlyph(glyph.fontId, glyph.index);
215 if(!isOutline || (isOutline && !isColorGlyph))
217 // Retrieve the emoji's bitmap.
218 TextAbstraction::FontClient::GlyphBufferData glyphBufferData;
219 glyphBufferData.width = isColorGlyph ? glyph.width : 0; // Desired width and height.
220 glyphBufferData.height = isColorGlyph ? glyph.height : 0;
222 mFontClient.CreateBitmap(glyph.fontId,
224 glyph.isItalicRequired,
225 glyph.isBoldRequired,
229 // Create the pixel data.
230 bitmap = PixelData::New(glyphBufferData.buffer,
231 glyphBufferData.width * glyphBufferData.height * GetBytesPerPixel(glyphBufferData.format),
232 glyphBufferData.width,
233 glyphBufferData.height,
234 glyphBufferData.format,
235 PixelData::DELETE_ARRAY);
239 // Ensure that the next image will fit into the current block size
240 if(bitmap.GetWidth() > blockSize.mNeededBlockWidth)
242 blockSize.mNeededBlockWidth = bitmap.GetWidth();
245 if(bitmap.GetHeight() > blockSize.mNeededBlockHeight)
247 blockSize.mNeededBlockHeight = bitmap.GetHeight();
250 // If CheckAtlas in AtlasManager::Add can't fit the bitmap in the current atlas it will create a new atlas
252 // Setting the block size and size of new atlas does not mean a new one will be created. An existing atlas may still surffice.
253 uint32_t default_width = DEFAULT_ATLAS_WIDTH;
254 uint32_t default_height = DEFAULT_ATLAS_HEIGHT;
257 (blockSize.mNeededBlockWidth >= (default_width - (DOUBLE_PIXEL_PADDING + 1u)) ||
258 blockSize.mNeededBlockHeight >= (default_height - (DOUBLE_PIXEL_PADDING + 1u)))
260 (default_width < MAX_ATLAS_WIDTH &&
261 default_height < MAX_ATLAS_HEIGHT))
263 default_width <<= 1u;
264 default_height <<= 1u;
266 mGlyphManager.SetNewAtlasSize(default_width,
268 blockSize.mNeededBlockWidth,
269 blockSize.mNeededBlockHeight);
271 // Locate a new slot for our glyph
272 mGlyphManager.Add(glyph, style, bitmap, slot); // slot will be 0 is glyph not added
278 // We have 2+ copies of the same glyph
279 mGlyphManager.AdjustReferenceCount(glyph.fontId, glyph.index, style, 1); //increment
283 void GenerateMesh(const GlyphInfo& glyph,
284 const Vector2& position,
285 const Vector4& color,
287 AtlasManager::AtlasSlot& slot,
289 float currentUnderlinePosition,
290 float currentUnderlineThickness,
291 std::vector<MeshRecord>& meshContainer,
292 Vector<TextCacheEntry>& newTextCache,
293 Vector<Extent>& extents,
294 uint32_t underlineChunkId)
296 // Generate mesh data for this quad, plugging in our supplied position
297 AtlasManager::Mesh2D newMesh;
298 mGlyphManager.GenerateMeshData(slot.mImageId, position, newMesh);
300 TextCacheEntry textCacheEntry;
301 textCacheEntry.mFontId = glyph.fontId;
302 textCacheEntry.mImageId = slot.mImageId;
303 textCacheEntry.mIndex = glyph.index;
304 textCacheEntry.mOutlineWidth = outline;
305 textCacheEntry.isItalic = glyph.isItalicRequired;
306 textCacheEntry.isBold = glyph.isBoldRequired;
308 newTextCache.PushBack(textCacheEntry);
310 AtlasManager::Vertex2D* verticesBuffer = newMesh.mVertices.Begin();
312 for(unsigned int index = 0u, size = newMesh.mVertices.Count();
316 AtlasManager::Vertex2D& vertex = *(verticesBuffer + index);
318 // Set the color of the vertex.
319 vertex.mColor = color;
322 // Find an existing mesh data object to attach to ( or create a new one, if we can't find one using the same atlas)
323 StitchTextMesh(meshContainer,
326 position.y + glyph.yBearing,
328 currentUnderlinePosition,
329 currentUnderlineThickness,
334 void CreateActors(const std::vector<MeshRecord>& meshContainer,
335 const Size& textSize,
336 const Vector4& color,
337 const Vector4& shadowColor,
338 const Vector2& shadowOffset,
340 Property::Index animatablePropertyIndex,
345 // Create a container actor to act as a common parent for text and shadow, to avoid color inheritence issues.
346 mActor = Actor::New();
347 mActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
348 mActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
349 mActor.SetProperty(Actor::Property::SIZE, textSize);
350 mActor.SetProperty(Actor::Property::COLOR_MODE, USE_OWN_MULTIPLY_PARENT_COLOR);
353 for(std::vector<MeshRecord>::const_iterator it = meshContainer.begin(),
354 endIt = meshContainer.end();
358 const MeshRecord& meshRecord = *it;
360 Actor actor = CreateMeshActor(textControl, animatablePropertyIndex, color, meshRecord, textSize, STYLE_NORMAL);
362 // Whether the actor has renderers.
363 const bool hasRenderer = actor.GetRendererCount() > 0u;
365 // Create an effect if necessary
369 // Change the color of the vertices.
370 for(Vector<AtlasManager::Vertex2D>::Iterator vIt = meshRecord.mMesh.mVertices.Begin(),
371 vEndIt = meshRecord.mMesh.mVertices.End();
375 AtlasManager::Vertex2D& vertex = *vIt;
377 vertex.mColor = shadowColor;
380 Actor shadowActor = CreateMeshActor(textControl, animatablePropertyIndex, color, meshRecord, textSize, STYLE_DROP_SHADOW);
381 #if defined(DEBUG_ENABLED)
382 shadowActor.SetProperty(Dali::Actor::Property::NAME, "Text Shadow renderable actor");
384 // Offset shadow in x and y
385 shadowActor.RegisterProperty("uOffset", shadowOffset);
386 Dali::Renderer renderer(shadowActor.GetRendererAt(0));
387 int depthIndex = renderer.GetProperty<int>(Dali::Renderer::Property::DEPTH_INDEX);
388 renderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, depthIndex - 1);
389 mActor.Add(shadowActor);
399 void AddGlyphs(Text::ViewInterface& view,
401 Property::Index animatablePropertyIndex,
402 const Vector<Vector2>& positions,
403 const Vector<GlyphInfo>& glyphs,
404 const Vector4& defaultColor,
405 const Vector4* const colorsBuffer,
406 const ColorIndex* const colorIndicesBuffer,
410 AtlasManager::AtlasSlot slot;
414 AtlasManager::AtlasSlot slotOutline;
415 slotOutline.mImageId = 0u;
416 slotOutline.mAtlasId = 0u;
418 std::vector<MeshRecord> meshContainer;
419 std::vector<MeshRecord> meshContainerOutline;
420 Vector<Extent> extents;
423 const Vector2& textSize(view.GetLayoutSize());
424 const Vector2 halfTextSize(textSize * 0.5f);
425 const Vector2& shadowOffset(view.GetShadowOffset());
426 const Vector4& shadowColor(view.GetShadowColor());
427 const bool underlineEnabled = view.IsUnderlineEnabled();
428 const Vector4& underlineColor(view.GetUnderlineColor());
429 const float underlineHeight = view.GetUnderlineHeight();
430 const uint16_t outlineWidth = view.GetOutlineWidth();
431 const Vector4& outlineColor(view.GetOutlineColor());
432 const bool isOutline = 0u != outlineWidth;
434 const bool useDefaultColor = (NULL == colorsBuffer);
436 // Get the underline runs.
437 const Length numberOfUnderlineRuns = view.GetNumberOfUnderlineRuns();
438 Vector<GlyphRun> underlineRuns;
439 underlineRuns.Resize(numberOfUnderlineRuns);
440 view.GetUnderlineRuns(underlineRuns.Begin(),
442 numberOfUnderlineRuns);
444 bool thereAreUnderlinedGlyphs = false;
446 float currentUnderlinePosition = ZERO;
447 float currentUnderlineThickness = underlineHeight;
448 FontId lastFontId = 0;
449 FontId lastUnderlinedFontId = 0;
450 Style style = STYLE_NORMAL;
452 if(fabsf(shadowOffset.x) > Math::MACHINE_EPSILON_1 || fabsf(shadowOffset.y) > Math::MACHINE_EPSILON_1)
454 style = STYLE_DROP_SHADOW;
457 CalculateBlocksSize(glyphs);
459 // Avoid emptying mTextCache (& removing references) until after incremented references for the new text
460 Vector<TextCacheEntry> newTextCache;
461 const GlyphInfo* const glyphsBuffer = glyphs.Begin();
462 const Vector2* const positionsBuffer = positions.Begin();
463 const Vector2 lineOffsetPosition(minLineOffset, 0.f);
465 //For septated underlined chunks. (this is for Markup case)
466 uint32_t underlineChunkId = 0u; // give id for each chunk.
467 bool isPreUnderlined = false; // status of underlined for previous glyph.
469 for(uint32_t i = 0, glyphSize = glyphs.Size(); i < glyphSize; ++i)
471 const GlyphInfo& glyph = *(glyphsBuffer + i);
472 const bool isGlyphUnderlined = underlineEnabled || IsGlyphUnderlined(i, underlineRuns);
473 thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || isGlyphUnderlined;
475 // No operation for white space
476 if(glyph.width && glyph.height)
478 // Are we still using the same fontId as previous
479 if(isGlyphUnderlined && (glyph.fontId != lastUnderlinedFontId))
481 // We need to fetch fresh font underline metrics
482 FontMetrics fontMetrics;
483 mFontClient.GetFontMetrics(glyph.fontId, fontMetrics);
484 currentUnderlinePosition = ceil(fabsf(fontMetrics.underlinePosition));
485 const float descender = ceil(fabsf(fontMetrics.descender));
487 if(fabsf(underlineHeight) < Math::MACHINE_EPSILON_1000)
489 currentUnderlineThickness = fontMetrics.underlineThickness;
491 // Ensure underline will be at least a pixel high
492 if(currentUnderlineThickness < ONE)
494 currentUnderlineThickness = ONE;
498 currentUnderlineThickness = ceil(currentUnderlineThickness);
502 // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
503 if(currentUnderlinePosition > descender)
505 currentUnderlinePosition = descender;
508 if(fabsf(currentUnderlinePosition) < Math::MACHINE_EPSILON_1000)
510 // Move offset down by one ( EFL behavior )
511 currentUnderlinePosition = ONE;
514 lastUnderlinedFontId = glyph.fontId;
518 AtlasGlyphManager::GlyphStyle style;
519 style.isItalic = glyph.isItalicRequired;
520 style.isBold = glyph.isBoldRequired;
522 // Retrieves and caches the glyph's bitmap.
523 CacheGlyph(glyph, lastFontId, style, slot);
525 // Retrieves and caches the outline glyph's bitmap.
528 style.outline = outlineWidth;
529 CacheGlyph(glyph, lastFontId, style, slotOutline);
532 // Move the origin (0,0) of the mesh to the center of the actor
533 const Vector2& temp = *(positionsBuffer + i);
534 const Vector2 position = Vector2(roundf(temp.x), temp.y) - halfTextSize - lineOffsetPosition; // roundf() avoids pixel alignment issues.
536 if(0u != slot.mImageId) // invalid slot id, glyph has failed to be added to atlas
538 Vector2 positionPlusOutlineOffset = position;
541 // Add an offset to the text.
542 const float outlineWidthOffset = static_cast<float>(outlineWidth);
543 positionPlusOutlineOffset += Vector2(outlineWidthOffset, outlineWidthOffset);
546 // Get the color of the character.
547 const ColorIndex colorIndex = useDefaultColor ? 0u : *(colorIndicesBuffer + i);
548 const Vector4& color = (useDefaultColor || (0u == colorIndex)) ? defaultColor : *(colorsBuffer + colorIndex - 1u);
551 positionPlusOutlineOffset,
556 currentUnderlinePosition,
557 currentUnderlineThickness,
563 lastFontId = glyph.fontId; // Prevents searching for existing blocksizes when string of the same fontId.
566 if(isOutline && (0u != slotOutline.mImageId)) // invalid slot id, glyph has failed to be added to atlas
574 currentUnderlinePosition,
575 currentUnderlineThickness,
576 meshContainerOutline,
583 //The new underlined chunk. Add new id if they are not consecutive indices (this is for Markup case)
584 // Examples: "Hello <u>World</u> Hello <u>World</u>", "<u>World</u> Hello <u>World</u>", "<u> World</u> Hello <u>World</u>"
585 if( isPreUnderlined && (isPreUnderlined != isGlyphUnderlined))
589 //Keep status of underlined for previous glyph to check consecutive indices
590 isPreUnderlined = isGlyphUnderlined;
594 // Now remove references for the old text
596 mTextCache.Swap(newTextCache);
598 if(thereAreUnderlinedGlyphs)
600 // Check to see if any of the text needs an underline
601 GenerateUnderlines(meshContainer, extents, underlineColor);
604 // For each MeshData object, create a mesh actor and add to the renderable actor
605 bool isShadowDrawn = false;
606 if(!meshContainerOutline.empty())
608 const bool drawShadow = STYLE_DROP_SHADOW == style;
609 CreateActors(meshContainerOutline,
615 animatablePropertyIndex,
618 isShadowDrawn = drawShadow;
621 // For each MeshData object, create a mesh actor and add to the renderable actor
622 if(!meshContainer.empty())
624 const bool drawShadow = !isShadowDrawn && (STYLE_DROP_SHADOW == style);
625 CreateActors(meshContainer,
631 animatablePropertyIndex,
635 #if defined(DEBUG_ENABLED)
636 Toolkit::AtlasGlyphManager::Metrics metrics = mGlyphManager.GetMetrics();
637 DALI_LOG_INFO(gLogFilter, Debug::General, "TextAtlasRenderer::GlyphManager::GlyphCount: %i, AtlasCount: %i, TextureMemoryUse: %iK\n", metrics.mGlyphCount, metrics.mAtlasMetrics.mAtlasCount, metrics.mAtlasMetrics.mTextureMemoryUsed / 1024);
639 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "%s\n", metrics.mVerboseGlyphCounts.c_str());
641 for(uint32_t i = 0; i < metrics.mAtlasMetrics.mAtlasCount; ++i)
643 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);
650 for(Vector<TextCacheEntry>::Iterator oldTextIter = mTextCache.Begin(); oldTextIter != mTextCache.End(); ++oldTextIter)
652 AtlasGlyphManager::GlyphStyle style;
653 style.outline = oldTextIter->mOutlineWidth;
654 style.isItalic = oldTextIter->isItalic;
655 style.isBold = oldTextIter->isBold;
656 mGlyphManager.AdjustReferenceCount(oldTextIter->mFontId, oldTextIter->mIndex, style, -1 /*decrement*/);
658 mTextCache.Resize(0);
661 Actor CreateMeshActor(Actor textControl, Property::Index animatablePropertyIndex, const Vector4& defaultColor, const MeshRecord& meshRecord, const Vector2& actorSize, Style style)
663 VertexBuffer quadVertices = VertexBuffer::New(mQuadVertexFormat);
664 quadVertices.SetData(const_cast<AtlasManager::Vertex2D*>(&meshRecord.mMesh.mVertices[0]), meshRecord.mMesh.mVertices.Size());
666 Geometry quadGeometry = Geometry::New();
667 quadGeometry.AddVertexBuffer(quadVertices);
668 quadGeometry.SetIndexBuffer(&meshRecord.mMesh.mIndices[0], meshRecord.mMesh.mIndices.Size());
670 TextureSet textureSet(mGlyphManager.GetTextures(meshRecord.mAtlasId));
672 // Choose the shader to use.
673 const bool isColorShader = (STYLE_DROP_SHADOW != style) && (Pixel::BGRA8888 == mGlyphManager.GetPixelFormat(meshRecord.mAtlasId));
677 // The glyph is an emoji and is not a shadow.
680 mShaderRgba = Shader::New(SHADER_TEXT_ATLAS_SHADER_VERT, SHADER_TEXT_ATLAS_RGBA_SHADER_FRAG);
682 shader = mShaderRgba;
686 // The glyph is text or a shadow.
689 mShaderL8 = Shader::New(SHADER_TEXT_ATLAS_SHADER_VERT, SHADER_TEXT_ATLAS_L8_SHADER_FRAG);
694 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "defaultColor[%f, %f, %f, %f ]\n", defaultColor.r, defaultColor.g, defaultColor.b, defaultColor.a);
696 Dali::Property::Index shaderTextColorIndex = shader.RegisterProperty("textColorAnimatable", defaultColor);
698 if(animatablePropertyIndex != Property::INVALID_INDEX)
700 // create constraint for the animatable text's color Property with textColorAnimatable in the shader.
701 if(shaderTextColorIndex)
703 Constraint constraint = Constraint::New<Vector4>(shader, shaderTextColorIndex, EqualToConstraint());
704 constraint.AddSource(Source(textControl, animatablePropertyIndex));
710 // If not animating the text colour then set to 1's so shader uses the current vertex color
711 shader.RegisterProperty("textColorAnimatable", Vector4(1.0, 1.0, 1.0, 1.0));
714 Dali::Renderer renderer = Dali::Renderer::New(quadGeometry, shader);
715 renderer.SetTextures(textureSet);
716 renderer.SetProperty(Dali::Renderer::Property::BLEND_MODE, BlendMode::ON);
717 renderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, DepthIndex::CONTENT + mDepth);
719 Actor actor = Actor::New();
720 #if defined(DEBUG_ENABLED)
721 actor.SetProperty(Dali::Actor::Property::NAME, "Text renderable actor");
723 actor.AddRenderer(renderer);
724 // Keep all of the origins aligned
725 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
726 actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
727 actor.SetProperty(Actor::Property::SIZE, actorSize);
728 actor.RegisterProperty("uOffset", Vector2::ZERO);
729 actor.SetProperty(Actor::Property::COLOR_MODE, USE_OWN_MULTIPLY_PARENT_COLOR);
734 void StitchTextMesh(std::vector<MeshRecord>& meshContainer,
735 AtlasManager::Mesh2D& newMesh,
736 Vector<Extent>& extents,
739 float underlinePosition,
740 float underlineThickness,
741 AtlasManager::AtlasSlot& slot,
742 uint32_t underlineChunkId)
746 float left = newMesh.mVertices[0].mPosition.x;
747 float right = newMesh.mVertices[1].mPosition.x;
749 // Check to see if there's a mesh data object that references the same atlas ?
751 for(std::vector<MeshRecord>::iterator mIt = meshContainer.begin(),
752 mEndIt = meshContainer.end();
756 if(slot.mAtlasId == mIt->mAtlasId)
758 // Append the mesh to the existing mesh and adjust any extents
759 Toolkit::Internal::AtlasMeshFactory::AppendMesh(mIt->mMesh, newMesh);
763 AdjustExtents(extents,
778 // No mesh data object currently exists that references this atlas, so create a new one
779 MeshRecord meshRecord;
780 meshRecord.mAtlasId = slot.mAtlasId;
781 meshRecord.mMesh = newMesh;
782 meshContainer.push_back(meshRecord);
786 // Adjust extents for this new meshrecord
787 AdjustExtents(extents,
789 meshContainer.size() - 1u,
800 void AdjustExtents(Vector<Extent>& extents,
801 std::vector<MeshRecord>& meshRecords,
806 float underlinePosition,
807 float underlineThickness,
808 uint32_t underlineChunkId)
810 bool foundExtent = false;
811 for(Vector<Extent>::Iterator eIt = extents.Begin(),
812 eEndIt = extents.End();
816 if(Equals(baseLine, eIt->mBaseLine) && underlineChunkId == eIt->mUnderlineChunkId)
819 if(left < eIt->mLeft)
823 if(right > eIt->mRight)
828 if(underlinePosition > eIt->mUnderlinePosition)
830 eIt->mUnderlinePosition = underlinePosition;
832 if(underlineThickness > eIt->mUnderlineThickness)
834 eIt->mUnderlineThickness = underlineThickness;
842 extent.mRight = right;
843 extent.mBaseLine = baseLine;
844 extent.mUnderlinePosition = underlinePosition;
845 extent.mUnderlineThickness = underlineThickness;
846 extent.mMeshRecordIndex = index;
847 extent.mUnderlineChunkId = underlineChunkId;
848 extents.PushBack(extent);
852 void CalculateBlocksSize(const Vector<GlyphInfo>& glyphs)
854 for(Vector<GlyphInfo>::ConstIterator glyphIt = glyphs.Begin(),
855 glyphEndIt = glyphs.End();
856 glyphIt != glyphEndIt;
859 const FontId fontId = (*glyphIt).fontId;
860 bool foundFont = false;
862 for(std::vector<MaxBlockSize>::const_iterator blockIt = mBlockSizes.begin(),
863 blockEndIt = mBlockSizes.end();
864 blockIt != blockEndIt;
867 if((*blockIt).mFontId == fontId) // Different size fonts will have a different fontId
869 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Text::AtlasRenderer::CalculateBlocksSize match found fontID(%u) glyphIndex(%u)\n", fontId, (*glyphIt).index);
877 FontMetrics fontMetrics;
878 mFontClient.GetFontMetrics(fontId, fontMetrics);
880 MaxBlockSize maxBlockSize;
881 maxBlockSize.mNeededBlockWidth = static_cast<uint32_t>(fontMetrics.height);
882 maxBlockSize.mNeededBlockHeight = maxBlockSize.mNeededBlockWidth;
883 maxBlockSize.mFontId = fontId;
884 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Text::AtlasRenderer::CalculateBlocksSize New font with no matched blocksize, setting blocksize[%u]\n", maxBlockSize.mNeededBlockWidth);
885 mBlockSizes.push_back(maxBlockSize);
890 void GenerateUnderlines(std::vector<MeshRecord>& meshRecords,
891 Vector<Extent>& extents,
892 const Vector4& underlineColor)
894 AtlasManager::Mesh2D newMesh;
895 unsigned short faceIndex = 0;
896 for(Vector<Extent>::ConstIterator eIt = extents.Begin(),
897 eEndIt = extents.End();
901 AtlasManager::Vertex2D vert;
902 uint32_t index = eIt->mMeshRecordIndex;
903 Vector2 uv = mGlyphManager.GetAtlasSize(meshRecords[index].mAtlasId);
905 // Make sure we don't hit texture edge for single pixel texture ( filled pixel is in top left of every atlas )
906 float u = HALF / uv.x;
907 float v = HALF / uv.y;
908 float thickness = eIt->mUnderlineThickness;
909 float baseLine = eIt->mBaseLine + eIt->mUnderlinePosition - (thickness * HALF);
910 float tlx = eIt->mLeft;
911 float brx = eIt->mRight;
913 vert.mPosition.x = tlx;
914 vert.mPosition.y = baseLine;
915 vert.mTexCoords.x = ZERO;
916 vert.mTexCoords.y = ZERO;
917 vert.mColor = underlineColor;
918 newMesh.mVertices.PushBack(vert);
920 vert.mPosition.x = brx;
921 vert.mPosition.y = baseLine;
922 vert.mTexCoords.x = u;
923 vert.mColor = underlineColor;
924 newMesh.mVertices.PushBack(vert);
926 vert.mPosition.x = tlx;
927 vert.mPosition.y = baseLine + thickness;
928 vert.mTexCoords.x = ZERO;
929 vert.mTexCoords.y = v;
930 vert.mColor = underlineColor;
931 newMesh.mVertices.PushBack(vert);
933 vert.mPosition.x = brx;
934 vert.mPosition.y = baseLine + thickness;
935 vert.mTexCoords.x = u;
936 vert.mColor = underlineColor;
937 newMesh.mVertices.PushBack(vert);
939 // Six indices in counter clockwise winding
940 newMesh.mIndices.PushBack(faceIndex + 1u);
941 newMesh.mIndices.PushBack(faceIndex);
942 newMesh.mIndices.PushBack(faceIndex + 2u);
943 newMesh.mIndices.PushBack(faceIndex + 2u);
944 newMesh.mIndices.PushBack(faceIndex + 3u);
945 newMesh.mIndices.PushBack(faceIndex + 1u);
948 Toolkit::Internal::AtlasMeshFactory::AppendMesh(meshRecords[index].mMesh, newMesh);
952 Actor mActor; ///< The actor parent which renders the text
953 AtlasGlyphManager mGlyphManager; ///< Glyph Manager to handle upload and caching
954 TextAbstraction::FontClient mFontClient; ///< The font client used to supply glyph information
955 Shader mShaderL8; ///< The shader for glyphs and emoji's shadows.
956 Shader mShaderRgba; ///< The shader for emojis.
957 std::vector<MaxBlockSize> mBlockSizes; ///< Maximum size needed to contain a glyph in a block within a new atlas
958 Vector<TextCacheEntry> mTextCache; ///< Caches data from previous render
959 Property::Map mQuadVertexFormat; ///< Describes the vertex format for text
960 int mDepth; ///< DepthIndex passed by control when connect to stage
963 Text::RendererPtr AtlasRenderer::New()
965 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Text::AtlasRenderer::New()\n");
967 return Text::RendererPtr(new AtlasRenderer());
970 Actor AtlasRenderer::Render(Text::ViewInterface& view,
972 Property::Index animatablePropertyIndex,
973 float& alignmentOffset,
976 DALI_LOG_INFO(gLogFilter, Debug::General, "Text::AtlasRenderer::Render()\n");
978 UnparentAndReset(mImpl->mActor);
980 Length numberOfGlyphs = view.GetNumberOfGlyphs();
982 if(numberOfGlyphs > 0u)
984 Vector<GlyphInfo> glyphs;
985 glyphs.Resize(numberOfGlyphs);
987 Vector<Vector2> positions;
988 positions.Resize(numberOfGlyphs);
990 numberOfGlyphs = view.GetGlyphs(glyphs.Begin(),
996 glyphs.Resize(numberOfGlyphs);
997 positions.Resize(numberOfGlyphs);
999 const Vector4* const colorsBuffer = view.GetColors();
1000 const ColorIndex* const colorIndicesBuffer = view.GetColorIndices();
1001 const Vector4& defaultColor = view.GetTextColor();
1003 mImpl->AddGlyphs(view,
1005 animatablePropertyIndex,
1014 /* In the case where AddGlyphs does not create a renderable Actor for example when glyphs are all whitespace create a new Actor. */
1015 /* 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. */
1018 mImpl->mActor = Actor::New();
1022 return mImpl->mActor;
1025 AtlasRenderer::AtlasRenderer()
1030 AtlasRenderer::~AtlasRenderer()
1032 mImpl->RemoveText();