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