Text - Fix for emojis resampling.
[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/integration-api/debug.h>
22
23 namespace
24 {
25
26 #if defined(DEBUG_ENABLED)
27   Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_RENDERING");
28 #endif
29
30 } // unnamed namespace
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 AtlasGlyphManager::AtlasGlyphManager()
42 {
43   mAtlasManager = Dali::Toolkit::AtlasManager::New();
44 }
45
46 void AtlasGlyphManager::Add( const Text::GlyphInfo& glyph,
47                              const PixelData& bitmap,
48                              Dali::Toolkit::AtlasManager::AtlasSlot& slot )
49 {
50   DALI_LOG_INFO( gLogFilter, Debug::General, "Added glyph, font: %d index: %d\n", glyph.fontId, glyph.index );
51
52   if ( mAtlasManager.Add( bitmap, slot ) )
53   {
54     // A new atlas was created so set the texture set details for the atlas
55     Dali::Texture atlas = mAtlasManager.GetAtlasContainer( slot.mAtlasId );
56     TextureSet textureSet = TextureSet::New();
57     textureSet.SetTexture( 0u, atlas );
58     mAtlasManager.SetTextures( slot.mAtlasId, textureSet );
59   }
60
61   GlyphRecordEntry record;
62   record.mIndex = glyph.index;
63   record.mImageId = slot.mImageId;
64   record.mCount = 1;
65
66   // Have glyph records been created for this fontId ?
67   bool foundGlyph = false;
68   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
69         fontGlyphRecordIt != mFontGlyphRecords.end(); ++fontGlyphRecordIt )
70   {
71     if ( fontGlyphRecordIt->mFontId == glyph.fontId )
72     {
73       fontGlyphRecordIt->mGlyphRecords.PushBack( record );
74       foundGlyph = true;
75       break;
76     }
77   }
78
79   if ( !foundGlyph )
80   {
81     // We need to add a new font entry
82     FontGlyphRecord fontGlyphRecord;
83     fontGlyphRecord.mFontId = glyph.fontId;
84     fontGlyphRecord.mGlyphRecords.PushBack( record );
85     mFontGlyphRecords.push_back( fontGlyphRecord );
86   }
87 }
88
89 void AtlasGlyphManager::GenerateMeshData( uint32_t imageId,
90                                           const Vector2& position,
91                                           Toolkit::AtlasManager::Mesh2D& mesh )
92 {
93   // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it )
94   mAtlasManager.GenerateMeshData( imageId, position, mesh, false );
95 }
96
97 bool AtlasGlyphManager::IsCached( Text::FontId fontId,
98                                 Text::GlyphIndex index,
99                                 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
100 {
101   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
102         fontGlyphRecordIt != mFontGlyphRecords.end();
103         ++fontGlyphRecordIt )
104   {
105     if ( fontGlyphRecordIt->mFontId == fontId )
106     {
107       for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
108             glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
109             ++glyphRecordIt )
110       {
111         if ( glyphRecordIt->mIndex == index )
112         {
113           slot.mImageId = glyphRecordIt->mImageId;
114           slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
115           return true;
116         }
117       }
118     }
119   }
120   slot.mImageId = 0;
121   return false;
122 }
123
124 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
125 {
126   Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
127   return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
128 }
129
130 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
131 {
132   Toolkit::AtlasManager::AtlasSize size;
133   size.mWidth = width;
134   size.mHeight = height;
135   size.mBlockWidth = blockWidth;
136   size.mBlockHeight = blockHeight;
137   mAtlasManager.SetNewAtlasSize( size );
138 }
139
140 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
141 {
142   return mAtlasManager.GetPixelFormat( atlasId );
143 }
144
145 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
146 {
147   std::ostringstream verboseMetrics;
148
149   mMetrics.mGlyphCount = 0u;
150   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
151         fontGlyphRecordIt != mFontGlyphRecords.end();
152         ++fontGlyphRecordIt )
153   {
154     mMetrics.mGlyphCount += fontGlyphRecordIt->mGlyphRecords.Size();
155
156     verboseMetrics << "[FontId " << fontGlyphRecordIt->mFontId << " Glyph ";
157     for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin();
158           glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End();
159           ++glyphRecordEntryIt )
160     {
161       verboseMetrics << glyphRecordEntryIt->mIndex << "(" << glyphRecordEntryIt->mCount << ") ";
162     }
163     verboseMetrics << "] ";
164   }
165   mMetrics.mVerboseGlyphCounts = verboseMetrics.str();
166
167   mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
168
169   return mMetrics;
170 }
171
172 void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, Text::GlyphIndex index, int32_t delta )
173 {
174   if( 0 != delta )
175   {
176     DALI_LOG_INFO( gLogFilter, Debug::General, "AdjustReferenceCount %d, font: %d index: %d\n", delta, fontId, index );
177
178     for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
179           fontGlyphRecordIt != mFontGlyphRecords.end();
180           ++fontGlyphRecordIt )
181     {
182       if ( fontGlyphRecordIt->mFontId == fontId )
183       {
184         for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
185               glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
186               ++glyphRecordIt )
187         {
188           if ( glyphRecordIt->mIndex == index )
189           {
190             glyphRecordIt->mCount += delta;
191             DALI_ASSERT_DEBUG( glyphRecordIt->mCount >= 0 && "Glyph ref-count should not be negative" );
192
193             if ( !glyphRecordIt->mCount )
194             {
195               mAtlasManager.Remove( glyphRecordIt->mImageId );
196               fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt );
197             }
198             return;
199           }
200         }
201       }
202     }
203
204     // Should not arrive here
205     DALI_ASSERT_DEBUG( false && "Failed to adjust ref-count" );
206   }
207 }
208
209 TextureSet AtlasGlyphManager::GetTextures( uint32_t atlasId ) const
210 {
211   return mAtlasManager.GetTextures( atlasId );
212 }
213
214 AtlasGlyphManager::~AtlasGlyphManager()
215 {
216   // mAtlasManager handle is automatically released here
217 }
218
219 } // namespace Internal
220
221 } // namespace Toolkit
222
223 } // namespace Dali