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( Text::FontId fontId,
121 const Text::GlyphInfo& glyph,
122 const BufferImage& bitmap,
123 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
125 mAtlasManager.Add( bitmap, slot );
127 GlyphRecordEntry record;
128 record.mIndex = glyph.index;
129 record.mImageId = slot.mImageId;
132 // Have glyph records been created for this fontId ?
133 bool foundGlyph = false;
134 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
135 fontGlyphRecordIt != mFontGlyphRecords.end(); ++fontGlyphRecordIt )
137 if ( fontGlyphRecordIt->mFontId == fontId )
139 fontGlyphRecordIt->mGlyphRecords.PushBack( record );
147 // We need to add a new font entry
148 FontGlyphRecord fontGlyphRecord;
149 fontGlyphRecord.mFontId = fontId;
150 fontGlyphRecord.mGlyphRecords.PushBack( record );
151 mFontGlyphRecords.push_back( fontGlyphRecord );
155 void AtlasGlyphManager::GenerateMeshData( uint32_t imageId,
156 const Vector2& position,
157 Toolkit::AtlasManager::Mesh2D& mesh )
159 // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it )
160 mAtlasManager.GenerateMeshData( imageId, position, mesh, false );
163 void AtlasGlyphManager::StitchMesh( Toolkit::AtlasManager::Mesh2D& first,
164 const Toolkit::AtlasManager::Mesh2D& second )
166 mAtlasManager.StitchMesh( first, second );
169 bool AtlasGlyphManager::Cached( Text::FontId fontId,
171 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
173 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
174 fontGlyphRecordIt != mFontGlyphRecords.end();
175 ++fontGlyphRecordIt )
177 if ( fontGlyphRecordIt->mFontId == fontId )
179 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
180 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
183 if ( glyphRecordIt->mIndex == index )
185 slot.mImageId = glyphRecordIt->mImageId;
186 slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
196 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
198 Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
199 return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
202 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
204 Toolkit::AtlasManager::AtlasSize size;
206 size.mHeight = height;
207 size.mBlockWidth = blockWidth;
208 size.mBlockHeight = blockHeight;
209 mAtlasManager.SetNewAtlasSize( size );
212 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
214 return mAtlasManager.GetPixelFormat( atlasId );
217 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
219 mMetrics.mGlyphCount = mFontGlyphRecords.size();
220 mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
224 void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, uint32_t imageId, int32_t delta )
226 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
227 fontGlyphRecordIt != mFontGlyphRecords.end();
228 ++fontGlyphRecordIt )
230 if ( fontGlyphRecordIt->mFontId == fontId )
232 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
233 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
236 if ( glyphRecordIt->mImageId == imageId )
238 glyphRecordIt->mCount += delta;
239 if ( !glyphRecordIt->mCount )
241 mAtlasManager.Remove( glyphRecordIt->mImageId );
242 fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt );
251 Material AtlasGlyphManager::GetMaterial( uint32_t atlasId ) const
253 return mAtlasManager.GetMaterial( atlasId );
256 Sampler AtlasGlyphManager::GetSampler( uint32_t atlasId ) const
258 return mAtlasManager.GetSampler( atlasId );
261 } // namespace Internal
263 } // namespace Toolkit