2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
18 #include <dali/internal/event/text/font-factory.h>
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>
27 #include <boost/functional/hash.hpp>
29 using namespace Dali::Integration;
37 FontFactory::FontFactory(ResourceClient& resourceClient)
38 : mResourceClient(resourceClient),
40 mHorizontalDpi( 0.f ),
43 mAtlasManager = new GlyphAtlasManager( *this );
45 mResourceClient.SetGlyphLoadObserver( &mAtlasManager->GetLoadObserver() );
48 FontFactory::~FontFactory()
50 mResourceClient.SetGlyphLoadObserver( NULL );
55 FontMetricsIntrusivePtr FontFactory::GetFontMetrics( const std::string& fontFamily, const std::string& fontStyle )
59 std::size_t hashValue = hasher(fontFamily + fontStyle);
60 FontMetricsIntrusivePtr metrics;
62 FontMetricsIter iter = mMetricsCache.find( hashValue );
63 if ( iter == mMetricsCache.end() )
65 metrics = FontMetrics::New( Vector2(mHorizontalDpi, mVerticalDpi), hashValue, mFontCount, fontFamily, fontStyle );
68 // load the global metrics
69 metrics->LoadGlobalMetrics();
71 // insert into a lookup table.
72 mMetricsCache.insert( FontMetricsPair( hashValue, metrics ) );
77 metrics = iter->second;
79 // increase the number of fonts using this metrics
80 metrics->IncreaseFontCount();
85 void FontFactory::RemoveFontMetrics(const std::string& fontFamily,
86 const std::string& fontStyle)
89 std::size_t hashValue = hasher(fontFamily + fontStyle);
90 FontMetricsIntrusivePtr metrics;
92 FontMetricsIter iter = mMetricsCache.find( hashValue );
93 if ( iter != mMetricsCache.end() )
95 (*iter).second->DecreaseFontCount();
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
103 void FontFactory::GetFontInformation( FontId fontId,
106 float& maxGlyphWidth,
107 float& maxGlyphHeight) const
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();
114 for( FontMetricsMap::const_iterator iter = mMetricsCache.begin(); iter != endIter; ++iter)
116 const FontMetrics* metric( (*iter).second.Get() );
118 if( metric->GetFontId() == fontId)
120 family = metric->GetFontFamilyName();
121 style = metric->GetFontStyleName();
122 metric->GetMaximumGylphSize( maxGlyphWidth, maxGlyphHeight );
126 DALI_ASSERT_ALWAYS( 0 && "Font id not found");
129 void FontFactory::SendTextRequests()
131 mAtlasManager->SendTextRequests();
134 GlyphAtlasManagerInterface& FontFactory::GetAtlasManagerInterface()
136 return *mAtlasManager;
139 void FontFactory::SetDpi( float horizontalDpi, float verticalDpi )
141 mHorizontalDpi = horizontalDpi;
142 mVerticalDpi = verticalDpi;
145 } // namespace Internal