fea111d77dafabb431ecb7dac0c4493006b0f3ee
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / rendering / atlas / atlas-glyph-manager-impl.cpp
1  /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 // CLASS HEADER
18 #include <dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager-impl.h>
19
20 // EXTERNAL INCLUDES
21 #include <dali/public-api/actors/image-actor.h>
22 #include <dali/public-api/common/stage.h>
23
24 #define MAKE_SHADER(A)#A
25
26 namespace
27 {
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;
34
35 void main()
36 {
37   mediump vec4 position = vec4( aPosition, 0.0, 1.0 );
38   position.xyz *= uSize;
39   gl_Position = uMvpMatrix * position;
40   vTexCoord = aTexCoord;
41 }
42 );
43
44 const char* FRAGMENT_SHADER = MAKE_SHADER(
45 uniform         sampler2D sTexture;
46 varying mediump vec2      vTexCoord;
47
48 void main()
49 {
50   gl_FragColor = texture2D( sTexture, vTexCoord );
51 }
52 );
53
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;
59
60 void main()
61 {
62   mediump vec4 position = vec4( aPosition, 0.0, 1.0 );
63   position.xyz *= uSize;
64   gl_Position = position;
65   vTexCoord = aTexCoord;
66 }
67 );
68
69 const char* FRAGMENT_SHADER_SHADOW = MAKE_SHADER(
70 uniform         sampler2D sTexture;
71 uniform lowp    vec4      uColor;
72 varying mediump vec2      vTexCoord;
73
74 void main()
75 {
76   mediump vec4 color = texture2D( sTexture, vTexCoord );
77   gl_FragColor = vec4(uColor.rgb, uColor.a*color.r);
78 }
79 );
80 }
81
82 namespace Dali
83 {
84
85 namespace Toolkit
86 {
87
88 namespace Internal
89 {
90
91 AtlasGlyphManager::AtlasGlyphManager()
92 {
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 );
96 }
97
98 AtlasGlyphManager::~AtlasGlyphManager()
99 {
100   // Clear up any remaining references
101   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
102         fontGlyphRecordIt != mFontGlyphRecords.end();
103         ++fontGlyphRecordIt )
104   {
105     for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin();
106           glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End();
107           ++glyphRecordEntryIt )
108     {
109       mAtlasManager.Remove( glyphRecordEntryIt->mImageId );
110     }
111   }
112 }
113
114 AtlasGlyphManagerPtr AtlasGlyphManager::New()
115 {
116   AtlasGlyphManagerPtr internal = new AtlasGlyphManager();
117   return internal;
118 }
119
120 void AtlasGlyphManager::Add( Text::FontId fontId,
121                              const Text::GlyphInfo& glyph,
122                              const BufferImage& bitmap,
123                              Dali::Toolkit::AtlasManager::AtlasSlot& slot )
124 {
125   mAtlasManager.Add( bitmap, slot );
126
127   GlyphRecordEntry record;
128   record.mIndex = glyph.index;
129   record.mImageId = slot.mImageId;
130   record.mCount = 1;
131
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 )
136   {
137     if ( fontGlyphRecordIt->mFontId == fontId )
138     {
139       fontGlyphRecordIt->mGlyphRecords.PushBack( record );
140       foundGlyph = true;
141       break;
142     }
143   }
144
145   if ( !foundGlyph )
146   {
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 );
152   }
153 }
154
155 void AtlasGlyphManager::GenerateMeshData( uint32_t imageId,
156                                           const Vector2& position,
157                                           Toolkit::AtlasManager::Mesh2D& mesh )
158 {
159   // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it )
160   mAtlasManager.GenerateMeshData( imageId, position, mesh, false );
161 }
162
163 void AtlasGlyphManager::StitchMesh( Toolkit::AtlasManager::Mesh2D& first,
164                                     const Toolkit::AtlasManager::Mesh2D& second )
165 {
166   mAtlasManager.StitchMesh( first, second );
167 }
168
169 bool AtlasGlyphManager::Cached( Text::FontId fontId,
170                                 uint32_t index,
171                                 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
172 {
173   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
174         fontGlyphRecordIt != mFontGlyphRecords.end();
175         ++fontGlyphRecordIt )
176   {
177     if ( fontGlyphRecordIt->mFontId == fontId )
178     {
179       for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
180             glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
181             ++glyphRecordIt )
182       {
183         if ( glyphRecordIt->mIndex == index )
184         {
185           slot.mImageId = glyphRecordIt->mImageId;
186           slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
187           return true;
188         }
189       }
190     }
191   }
192   slot.mImageId = 0;
193   return false;
194 }
195
196 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
197 {
198   Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
199   return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
200 }
201
202 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
203 {
204   Toolkit::AtlasManager::AtlasSize size;
205   size.mWidth = width;
206   size.mHeight = height;
207   size.mBlockWidth = blockWidth;
208   size.mBlockHeight = blockHeight;
209   mAtlasManager.SetNewAtlasSize( size );
210 }
211
212 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
213 {
214   return mAtlasManager.GetPixelFormat( atlasId );
215 }
216
217 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
218 {
219   mMetrics.mGlyphCount = mFontGlyphRecords.size();
220   mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
221   return mMetrics;
222 }
223
224 void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, uint32_t imageId, int32_t delta )
225 {
226   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
227         fontGlyphRecordIt != mFontGlyphRecords.end();
228         ++fontGlyphRecordIt )
229   {
230     if ( fontGlyphRecordIt->mFontId == fontId )
231     {
232       for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
233             glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
234             ++glyphRecordIt )
235       {
236         if ( glyphRecordIt->mImageId == imageId )
237         {
238           glyphRecordIt->mCount += delta;
239           if ( !glyphRecordIt->mCount )
240           {
241             mAtlasManager.Remove( glyphRecordIt->mImageId );
242             fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt );
243           }
244           return;
245         }
246       }
247     }
248   }
249 }
250
251 Material AtlasGlyphManager::GetMaterial( uint32_t atlasId ) const
252 {
253   return mAtlasManager.GetMaterial( atlasId );
254 }
255
256 Sampler AtlasGlyphManager::GetSampler( uint32_t atlasId ) const
257 {
258   return mAtlasManager.GetSampler( atlasId );
259 }
260
261 } // namespace Internal
262
263 } // namespace Toolkit
264
265 } // namespace Dali