9b8ff206cc703e0e7c39dd71b7e5b13af16bcc7c
[platform/core/uifw/dali-core.git] / dali / internal / event / text / font-factory.cpp
1 /*
2  * Copyright (c) 2014 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
18 // CLASS HEADER
19 #include <dali/internal/event/text/font-factory.h>
20
21 // INTERNAL HEADERS
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/internal/event/resources/resource-client.h>
25 #include <dali/internal/event/text/atlas/glyph-atlas-manager.h>
26 #include <dali/internal/common/dali-hash.h>
27
28 // EXTERNAL INCLUDES
29 #include <boost/functional/hash.hpp>
30
31 using namespace Dali::Integration;
32
33 namespace Dali
34 {
35
36 namespace Internal
37 {
38
39 FontFactory::FontFactory(ResourceClient& resourceClient)
40 : mResourceClient(resourceClient),
41   mFontCount( 0 ),
42   mHorizontalDpi( 0.f ),
43   mVerticalDpi( 0.f )
44 {
45   mAtlasManager = new GlyphAtlasManager( *this );
46
47   mResourceClient.SetGlyphLoadObserver( &mAtlasManager->GetLoadObserver() );
48 }
49
50 FontFactory::~FontFactory()
51 {
52   mResourceClient.SetGlyphLoadObserver( NULL );
53
54   delete mAtlasManager;
55 }
56
57 FontMetricsIntrusivePtr FontFactory::GetFontMetrics( const std::string& fontFamily, const std::string& fontStyle )
58 {
59
60   StringHash hasher;
61   std::size_t hashValue = hasher(fontFamily + fontStyle);
62   FontMetricsIntrusivePtr metrics;
63
64   FontMetricsIter iter = mMetricsCache.find( hashValue );
65   if ( iter == mMetricsCache.end() )
66   {
67     metrics = FontMetrics::New( Vector2(mHorizontalDpi, mVerticalDpi), hashValue, mFontCount, fontFamily, fontStyle );
68     mFontCount++;
69
70     // load the global metrics
71     metrics->LoadGlobalMetrics();
72
73     // insert into a lookup table.
74     mMetricsCache.insert( FontMetricsPair( hashValue, metrics ) );
75
76   }
77   else
78   {
79     metrics = iter->second;
80   }
81   // increase the number of fonts using this metrics
82   metrics->IncreaseFontCount();
83
84   return metrics;
85 }
86
87 void FontFactory::RemoveFontMetrics(const std::string& fontFamily,
88                                     const std::string& fontStyle)
89 {
90   StringHash hasher;
91   std::size_t hashValue = hasher(fontFamily + fontStyle);
92   FontMetricsIntrusivePtr metrics;
93
94   FontMetricsIter iter = mMetricsCache.find( hashValue );
95   if ( iter != mMetricsCache.end() )
96   {
97     (*iter).second->DecreaseFontCount();
98   }
99   // for now we keep metrics in memory even if ref count = 0
100   // @todo implement a scheme to delete metrics that haven't been used for
101   // a certain amount of time
102 }
103
104
105 void FontFactory::GetFontInformation( FontId fontId,
106                                       std::string& family,
107                                       std::string& style,
108                                       float& maxGlyphWidth,
109                                       float& maxGlyphHeight) const
110 {
111   // typically we only have around 4 fonts in the cache,
112   // and GetFontInformation is only called a couple of times on startup
113   // so just iterate over the map, manually searching for the font id
114   FontMetricsMap::const_iterator endIter = mMetricsCache.end();
115
116   for( FontMetricsMap::const_iterator iter =  mMetricsCache.begin(); iter != endIter; ++iter)
117   {
118     const FontMetrics* metric( (*iter).second.Get() );
119
120     if( metric->GetFontId() == fontId)
121     {
122       family = metric->GetFontFamilyName();
123       style = metric->GetFontStyleName();
124       metric->GetMaximumGylphSize( maxGlyphWidth, maxGlyphHeight );
125       return;
126     }
127   }
128   DALI_ASSERT_ALWAYS( 0 && "Font id not found");
129 }
130
131 void FontFactory::SendTextRequests()
132 {
133   mAtlasManager->SendTextRequests();
134 }
135
136 GlyphAtlasManagerInterface& FontFactory::GetAtlasManagerInterface()
137 {
138   return *mAtlasManager;
139 }
140
141 void FontFactory::SetDpi( float horizontalDpi, float verticalDpi )
142 {
143   mHorizontalDpi = horizontalDpi;
144   mVerticalDpi = verticalDpi;
145 }
146
147 } // namespace Internal
148
149 } // namespace Dali