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