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>
24 #define MAKE_SHADER(A)#A
28 const char* VERTEX_SHADER = MAKE_SHADER(
29 attribute mediump vec2 aPosition;
30 attribute mediump vec2 aTexCoord;
31 uniform mediump mat4 uMvpMatrix;
32 uniform mediump vec3 uSize;
33 varying mediump vec2 vTexCoord;
37 mediump vec4 position = vec4( aPosition, 0.0, 1.0 );
38 position.xyz *= uSize;
39 gl_Position = uMvpMatrix * position;
40 vTexCoord = aTexCoord;
44 const char* FRAGMENT_SHADER = MAKE_SHADER(
45 uniform sampler2D sTexture;
46 varying mediump vec2 vTexCoord;
50 gl_FragColor = texture2D( sTexture, vTexCoord );
54 const char* VERTEX_SHADER_SHADOW = MAKE_SHADER(
55 attribute mediump vec2 aPosition;
56 attribute mediump vec2 aTexCoord;
57 uniform mediump vec3 uSize;
58 varying mediump vec2 vTexCoord;
62 mediump vec4 position = vec4( aPosition, 0.0, 1.0 );
63 position.xyz *= uSize;
64 gl_Position = position;
65 vTexCoord = aTexCoord;
69 const char* FRAGMENT_SHADER_SHADOW = MAKE_SHADER(
70 uniform sampler2D sTexture;
71 uniform lowp vec4 uColor;
72 varying mediump vec2 vTexCoord;
76 mediump vec4 color = texture2D( sTexture, vTexCoord );
77 gl_FragColor = vec4(uColor.rgb, uColor.a*color.r);
91 AtlasGlyphManager::AtlasGlyphManager()
93 mAtlasManager = Dali::Toolkit::AtlasManager::New();
94 mEffectBufferShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
95 mShadowShader = Shader::New( VERTEX_SHADER_SHADOW, FRAGMENT_SHADER_SHADOW, Dali::Shader::HINT_MODIFIES_GEOMETRY );
98 AtlasGlyphManager::~AtlasGlyphManager()
100 // Clear up any remaining references
101 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
102 fontGlyphRecordIt != mFontGlyphRecords.end();
103 ++fontGlyphRecordIt )
105 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin();
106 glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End();
107 ++glyphRecordEntryIt )
109 mAtlasManager.Remove( glyphRecordEntryIt->mImageId );
114 AtlasGlyphManagerPtr AtlasGlyphManager::New()
116 AtlasGlyphManagerPtr internal = new AtlasGlyphManager();
120 void AtlasGlyphManager::Add( const Text::GlyphInfo& glyph,
121 const BufferImage& bitmap,
122 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
124 mAtlasManager.Add( bitmap, slot );
126 GlyphRecordEntry record;
127 record.mIndex = glyph.index;
128 record.mImageId = slot.mImageId;
131 // Have glyph records been created for this fontId ?
132 bool foundGlyph = false;
133 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
134 fontGlyphRecordIt != mFontGlyphRecords.end(); ++fontGlyphRecordIt )
136 if ( fontGlyphRecordIt->mFontId == glyph.fontId )
138 fontGlyphRecordIt->mGlyphRecords.PushBack( record );
146 // We need to add a new font entry
147 FontGlyphRecord fontGlyphRecord;
148 fontGlyphRecord.mFontId = glyph.fontId;
149 fontGlyphRecord.mGlyphRecords.PushBack( record );
150 mFontGlyphRecords.push_back( fontGlyphRecord );
154 void AtlasGlyphManager::GenerateMeshData( uint32_t imageId,
155 const Vector2& position,
156 Toolkit::AtlasManager::Mesh2D& mesh )
158 // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it )
159 mAtlasManager.GenerateMeshData( imageId, position, mesh, false );
162 void AtlasGlyphManager::StitchMesh( Toolkit::AtlasManager::Mesh2D& first,
163 const Toolkit::AtlasManager::Mesh2D& second )
165 mAtlasManager.StitchMesh( first, second );
168 bool AtlasGlyphManager::Cached( Text::FontId fontId,
170 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
172 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
173 fontGlyphRecordIt != mFontGlyphRecords.end();
174 ++fontGlyphRecordIt )
176 if ( fontGlyphRecordIt->mFontId == fontId )
178 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
179 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
182 if ( glyphRecordIt->mIndex == index )
184 slot.mImageId = glyphRecordIt->mImageId;
185 slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
195 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
197 Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
198 return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
201 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
203 Toolkit::AtlasManager::AtlasSize size;
205 size.mHeight = height;
206 size.mBlockWidth = blockWidth;
207 size.mBlockHeight = blockHeight;
208 mAtlasManager.SetNewAtlasSize( size );
211 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
213 return mAtlasManager.GetPixelFormat( atlasId );
216 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
218 mMetrics.mGlyphCount = 0u;
219 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
220 fontGlyphRecordIt != mFontGlyphRecords.end();
221 ++fontGlyphRecordIt )
223 mMetrics.mGlyphCount += fontGlyphRecordIt->mGlyphRecords.Size();
225 mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
229 void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, uint32_t imageId, int32_t delta )
231 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
232 fontGlyphRecordIt != mFontGlyphRecords.end();
233 ++fontGlyphRecordIt )
235 if ( fontGlyphRecordIt->mFontId == fontId )
237 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
238 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
241 if ( glyphRecordIt->mImageId == imageId )
243 glyphRecordIt->mCount += delta;
244 if ( !glyphRecordIt->mCount )
246 mAtlasManager.Remove( glyphRecordIt->mImageId );
247 fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt );
256 Material AtlasGlyphManager::GetMaterial( uint32_t atlasId ) const
258 return mAtlasManager.GetMaterial( atlasId );
261 Sampler AtlasGlyphManager::GetSampler( uint32_t atlasId ) const
263 return mAtlasManager.GetSampler( atlasId );
266 } // namespace Internal
268 } // namespace Toolkit