2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/text/font-factory.h>
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>
28 #include <boost/functional/hash.hpp>
30 using namespace Dali::Integration;
38 FontFactory::FontFactory(ResourceClient& resourceClient)
39 : mResourceClient(resourceClient),
41 mHorizontalDpi( 0.f ),
44 mAtlasManager = new GlyphAtlasManager( *this );
46 mResourceClient.SetGlyphLoadObserver( &mAtlasManager->GetLoadObserver() );
49 FontFactory::~FontFactory()
51 mResourceClient.SetGlyphLoadObserver( NULL );
56 FontMetricsIntrusivePtr FontFactory::GetFontMetrics( const std::string& fontFamily, const std::string& fontStyle )
60 std::size_t hashValue = hasher(fontFamily + fontStyle);
61 FontMetricsIntrusivePtr metrics;
63 FontMetricsIter iter = mMetricsCache.find( hashValue );
64 if ( iter == mMetricsCache.end() )
66 metrics = FontMetrics::New( Vector2(mHorizontalDpi, mVerticalDpi), hashValue, mFontCount, fontFamily, fontStyle );
69 // load the global metrics
70 metrics->LoadGlobalMetrics();
72 // insert into a lookup table.
73 mMetricsCache.insert( FontMetricsPair( hashValue, metrics ) );
78 metrics = iter->second;
80 // increase the number of fonts using this metrics
81 metrics->IncreaseFontCount();
86 void FontFactory::RemoveFontMetrics(const std::string& fontFamily,
87 const std::string& fontStyle)
90 std::size_t hashValue = hasher(fontFamily + fontStyle);
91 FontMetricsIntrusivePtr metrics;
93 FontMetricsIter iter = mMetricsCache.find( hashValue );
94 if ( iter != mMetricsCache.end() )
96 (*iter).second->DecreaseFontCount();
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
104 void FontFactory::GetFontInformation( FontId fontId,
107 float& maxGlyphWidth,
108 float& maxGlyphHeight) const
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();
115 for( FontMetricsMap::const_iterator iter = mMetricsCache.begin(); iter != endIter; ++iter)
117 const FontMetrics* metric( (*iter).second.Get() );
119 if( metric->GetFontId() == fontId)
121 family = metric->GetFontFamilyName();
122 style = metric->GetFontStyleName();
123 metric->GetMaximumGylphSize( maxGlyphWidth, maxGlyphHeight );
127 DALI_ASSERT_ALWAYS( 0 && "Font id not found");
130 void FontFactory::SendTextRequests()
132 mAtlasManager->SendTextRequests();
135 GlyphAtlasManagerInterface& FontFactory::GetAtlasManagerInterface()
137 return *mAtlasManager;
140 void FontFactory::SetDpi( float horizontalDpi, float verticalDpi )
142 mHorizontalDpi = horizontalDpi;
143 mVerticalDpi = verticalDpi;
146 } // namespace Internal