2 * Copyright (c) 2015 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.
18 #include <dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager-impl.h>
21 #include <dali/public-api/actors/image-actor.h>
22 #include <dali/public-api/common/stage.h>
33 //#define DISPLAY_ATLAS
35 AtlasGlyphManager::AtlasGlyphManager()
38 mAtlasManager = Dali::Toolkit::AtlasManager::New();
41 AtlasGlyphManager::~AtlasGlyphManager()
43 // Clear up any remaining references
44 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
45 fontGlyphRecordIt != mFontGlyphRecords.end();
48 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin();
49 glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End();
50 ++glyphRecordEntryIt )
52 mAtlasManager.Remove( glyphRecordEntryIt->mImageId );
57 AtlasGlyphManagerPtr AtlasGlyphManager::New()
59 AtlasGlyphManagerPtr internal = new AtlasGlyphManager();
63 void AtlasGlyphManager::Add( Text::FontId fontId,
64 const Text::GlyphInfo& glyph,
65 const BufferImage& bitmap,
66 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
68 mAtlasManager.Add( bitmap, slot );
70 GlyphRecordEntry record;
71 record.mIndex = glyph.index;
72 record.mImageId = slot.mImageId;
75 // Have glyph records been created for this fontId ?
76 bool foundGlyph = false;
77 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
78 fontGlyphRecordIt != mFontGlyphRecords.end(); ++fontGlyphRecordIt )
80 if ( fontGlyphRecordIt->mFontId == fontId )
82 fontGlyphRecordIt->mGlyphRecords.PushBack( record );
90 // We need to add a new font entry
91 FontGlyphRecord fontGlyphRecord;
92 fontGlyphRecord.mFontId = fontId;
93 fontGlyphRecord.mGlyphRecords.PushBack( record );
94 mFontGlyphRecords.push_back( fontGlyphRecord );
99 uint32_t atlasCount = mAtlasManager.GetAtlasCount();
100 if ( atlasCount > mCount )
102 for ( uint32_t i = 0; i < atlasCount; ++i )
104 ImageActor actor = ImageActor::New( mAtlasManager.GetAtlasContainer( i + 1u ) );
105 actor.SetParentOrigin( Vector3( 0.5f, 0.25f + ( static_cast< float >( i ) * 0.25f ), 0.5f ) );
106 actor.SetAnchorPoint( AnchorPoint::CENTER );
107 actor.SetSize( 256.0f, 256.0f );
108 Stage::GetCurrent().Add( actor );
116 void AtlasGlyphManager::GenerateMeshData( uint32_t imageId,
117 const Vector2& position,
120 // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it )
121 mAtlasManager.GenerateMeshData( imageId, position, meshData, false );
124 void AtlasGlyphManager::StitchMesh( MeshData& first,
125 const MeshData& second )
127 mAtlasManager.StitchMesh( first, second );
130 bool AtlasGlyphManager::Cached( Text::FontId fontId,
132 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
134 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
135 fontGlyphRecordIt != mFontGlyphRecords.end();
136 ++fontGlyphRecordIt )
138 if ( fontGlyphRecordIt->mFontId == fontId )
140 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
141 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
144 if ( glyphRecordIt->mIndex == index )
146 slot.mImageId = glyphRecordIt->mImageId;
147 slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
157 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
159 Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
160 return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
163 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
165 Toolkit::AtlasManager::AtlasSize size;
167 size.mHeight = height;
168 size.mBlockWidth = blockWidth;
169 size.mBlockHeight = blockHeight;
170 mAtlasManager.SetNewAtlasSize( size );
173 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
175 return mAtlasManager.GetPixelFormat( atlasId );
178 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
180 mMetrics.mGlyphCount = mFontGlyphRecords.size();
181 mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
185 void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, uint32_t imageId, int32_t delta )
187 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
188 fontGlyphRecordIt != mFontGlyphRecords.end();
189 ++fontGlyphRecordIt )
191 if ( fontGlyphRecordIt->mFontId == fontId )
193 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
194 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
197 if ( glyphRecordIt->mImageId == imageId )
199 glyphRecordIt->mCount += delta;
200 if ( !glyphRecordIt->mCount )
202 mAtlasManager.Remove( glyphRecordIt->mImageId );
203 fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt );
212 } // namespace Internal
214 } // namespace Toolkit