[dali_1.0.43] Merge branch 'tizen'
[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   Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
114   return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
115 }
116
117 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
118 {
119   Toolkit::AtlasManager::AtlasSize size;
120   size.mWidth = width;
121   size.mHeight = height;
122   size.mBlockWidth = blockWidth;
123   size.mBlockHeight = blockHeight;
124   mAtlasManager.SetNewAtlasSize( size );
125 }
126
127 void AtlasGlyphManager::Remove( uint32_t imageId )
128 {
129   if ( mAtlasManager.Remove( imageId ) )
130   {
131     for ( uint32_t i = 0; i < mGlyphRecords.Size(); ++i )
132     {
133       if ( mGlyphRecords[ i ].mImageId == imageId )
134       {
135         mGlyphRecords.Remove( mGlyphRecords.Begin() + i );
136         return;
137       }
138     }
139   }
140 }
141
142 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
143 {
144   return mAtlasManager.GetPixelFormat( atlasId );
145 }
146
147 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
148 {
149   mMetrics.mGlyphCount = mGlyphRecords.Size();
150   mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
151   return mMetrics;
152 }
153
154 } // namespace Internal
155
156 } // namespace Toolkit
157
158 } // namespace Dali