Merge remote-tracking branch 'origin/tizen' into new_text
[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 namespace Dali
25 {
26
27 namespace Toolkit
28 {
29
30 namespace Internal
31 {
32
33 //#define DISPLAY_ATLAS
34
35 AtlasGlyphManager::AtlasGlyphManager()
36 : mCount( 0 )
37 {
38   mAtlasManager = Dali::Toolkit::AtlasManager::New();
39 }
40
41 AtlasGlyphManager::~AtlasGlyphManager()
42 {
43 }
44
45 AtlasGlyphManagerPtr AtlasGlyphManager::New()
46 {
47   AtlasGlyphManagerPtr internal = new AtlasGlyphManager();
48   return internal;
49 }
50
51 void AtlasGlyphManager::Add( const Text::GlyphInfo& glyph,
52                              const BufferImage& bitmap,
53                              Dali::Toolkit::AtlasManager::AtlasSlot& slot )
54 {
55   GlyphRecord record;
56   record.mFontId = glyph.fontId;
57   record.mIndex = glyph.index;
58
59   mAtlasManager.Add( bitmap, slot );
60   record.mImageId = slot.mImageId;
61   mGlyphRecords.PushBack( record );
62
63 #ifdef DISPLAY_ATLAS
64   {
65     uint32_t atlasCount = mAtlasManager.GetAtlasCount();
66     if ( atlasCount > mCount )
67     {
68       for ( uint32_t i = 0; i < atlasCount; ++i )
69       {
70         ImageActor actor = ImageActor::New( mAtlasManager.GetAtlasContainer( i + 1u ) );
71         actor.SetParentOrigin( Vector3( 0.5f, 0.25f + ( static_cast< float >( i ) * 0.25f ), 0.5f ) );
72         actor.SetAnchorPoint( AnchorPoint::CENTER );
73         actor.SetSize( 256.0f, 256.0f );
74         Stage::GetCurrent().Add( actor );
75       }
76     }
77     mCount = atlasCount;
78   }
79 #endif
80 }
81
82 void AtlasGlyphManager::GenerateMeshData( uint32_t imageId,
83                                           const Vector2& position,
84                                           MeshData& meshData )
85 {
86   mAtlasManager.GenerateMeshData( imageId, position, meshData );
87 }
88
89 void AtlasGlyphManager::StitchMesh( MeshData& first,
90                                     const MeshData& second )
91 {
92   mAtlasManager.StitchMesh( first, second );
93 }
94
95 void AtlasGlyphManager::Cached( Text::FontId fontId,
96                                 uint32_t index,
97                                 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
98 {
99   for ( uint32_t i = 0; i < mGlyphRecords.Size(); ++i )
100   {
101     if ( fontId == mGlyphRecords[ i ].mFontId && index == mGlyphRecords[ i ].mIndex )
102     {
103       slot.mImageId = mGlyphRecords[ i ].mImageId;
104       slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
105       return;
106     }
107   }
108   slot.mImageId = 0;
109 }
110
111 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
112 {
113   return mAtlasManager.GetAtlasSize( atlasId );
114 }
115
116 void AtlasGlyphManager::SetNewAtlasSize( const Vector2& size,
117                                          const Vector2& blockSize )
118 {
119     mAtlasManager.SetNewAtlasSize( size, blockSize );
120 }
121
122 void AtlasGlyphManager::Remove( uint32_t imageId )
123 {
124   if ( mAtlasManager.Remove( imageId ) )
125   {
126     for ( uint32_t i = 0; i < mGlyphRecords.Size(); ++i )
127     {
128       if ( mGlyphRecords[ i ].mImageId == imageId )
129       {
130         mGlyphRecords.Remove( mGlyphRecords.Begin() + i );
131         return;
132       }
133     }
134   }
135 }
136
137 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
138 {
139   return mAtlasManager.GetPixelFormat( atlasId );
140 }
141
142 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
143 {
144   mMetrics.mGlyphCount = mGlyphRecords.Size();
145   mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
146   return mMetrics;
147 }
148
149 } // namespace Internal
150
151 } // namespace Toolkit
152
153 } // namespace Dali