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>
26 #include <dali/internal/common/dali-hash.h>
29 #include <boost/functional/hash.hpp>
31 using namespace Dali::Integration;
39 FontFactory::FontFactory(ResourceClient& resourceClient)
40 : mResourceClient(resourceClient),
42 mHorizontalDpi( 0.f ),
45 mAtlasManager = new GlyphAtlasManager( *this );
47 mResourceClient.SetGlyphLoadObserver( &mAtlasManager->GetLoadObserver() );
50 FontFactory::~FontFactory()
52 mResourceClient.SetGlyphLoadObserver( NULL );
57 FontMetricsIntrusivePtr FontFactory::GetFontMetrics( const std::string& fontFamily, const std::string& fontStyle )
61 std::size_t hashValue = hasher(fontFamily + fontStyle);
62 FontMetricsIntrusivePtr metrics;
64 FontMetricsIter iter = mMetricsCache.find( hashValue );
65 if ( iter == mMetricsCache.end() )
67 metrics = FontMetrics::New( Vector2(mHorizontalDpi, mVerticalDpi), hashValue, mFontCount, fontFamily, fontStyle );
70 // load the global metrics
71 metrics->LoadGlobalMetrics();
73 // insert into a lookup table.
74 mMetricsCache.insert( FontMetricsPair( hashValue, metrics ) );
79 metrics = iter->second;
81 // increase the number of fonts using this metrics
82 metrics->IncreaseFontCount();
87 void FontFactory::RemoveFontMetrics(const std::string& fontFamily,
88 const std::string& fontStyle)
91 std::size_t hashValue = hasher(fontFamily + fontStyle);
92 FontMetricsIntrusivePtr metrics;
94 FontMetricsIter iter = mMetricsCache.find( hashValue );
95 if ( iter != mMetricsCache.end() )
97 (*iter).second->DecreaseFontCount();
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
105 void FontFactory::GetFontInformation( FontId fontId,
108 float& maxGlyphWidth,
109 float& maxGlyphHeight) const
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();
116 for( FontMetricsMap::const_iterator iter = mMetricsCache.begin(); iter != endIter; ++iter)
118 const FontMetrics* metric( (*iter).second.Get() );
120 if( metric->GetFontId() == fontId)
122 family = metric->GetFontFamilyName();
123 style = metric->GetFontStyleName();
124 metric->GetMaximumGylphSize( maxGlyphWidth, maxGlyphHeight );
128 DALI_ASSERT_ALWAYS( 0 && "Font id not found");
131 void FontFactory::SendTextRequests()
133 mAtlasManager->SendTextRequests();
136 GlyphAtlasManagerInterface& FontFactory::GetAtlasManagerInterface()
138 return *mAtlasManager;
141 void FontFactory::SetDpi( float horizontalDpi, float verticalDpi )
143 mHorizontalDpi = horizontalDpi;
144 mVerticalDpi = verticalDpi;
147 } // namespace Internal