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 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 )
58 std::size_t hashValue = CalculateHash(fontFamily,fontStyle);
59 FontMetricsIntrusivePtr metrics;
61 FontMetricsIter iter = mMetricsCache.find( hashValue );
62 if ( iter == mMetricsCache.end() )
64 metrics = FontMetrics::New( Vector2(mHorizontalDpi, mVerticalDpi), hashValue, mFontCount, fontFamily, fontStyle );
67 // load the global metrics
68 metrics->LoadGlobalMetrics();
70 // insert into a lookup table.
71 mMetricsCache.insert( FontMetricsPair( hashValue, metrics ) );
76 metrics = iter->second;
78 // increase the number of fonts using this metrics
79 metrics->IncreaseFontCount();
84 void FontFactory::RemoveFontMetrics(const std::string& fontFamily,
85 const std::string& fontStyle)
87 std::size_t hashValue = CalculateHash(fontFamily, fontStyle);
88 FontMetricsIntrusivePtr metrics;
90 FontMetricsIter iter = mMetricsCache.find( hashValue );
91 if ( iter != mMetricsCache.end() )
93 (*iter).second->DecreaseFontCount();
95 // for now we keep metrics in memory even if ref count = 0
96 // @todo implement a scheme to delete metrics that haven't been used for
97 // a certain amount of time
101 void FontFactory::GetFontInformation( FontId fontId,
104 float& maxGlyphWidth,
105 float& maxGlyphHeight) const
107 // typically we only have around 4 fonts in the cache,
108 // and GetFontInformation is only called a couple of times on startup
109 // so just iterate over the map, manually searching for the font id
110 FontMetricsMap::const_iterator endIter = mMetricsCache.end();
112 for( FontMetricsMap::const_iterator iter = mMetricsCache.begin(); iter != endIter; ++iter)
114 const FontMetrics* metric( (*iter).second.Get() );
116 if( metric->GetFontId() == fontId)
118 family = metric->GetFontFamilyName();
119 style = metric->GetFontStyleName();
120 metric->GetMaximumGylphSize( maxGlyphWidth, maxGlyphHeight );
124 DALI_ASSERT_ALWAYS( 0 && "Font id not found");
127 void FontFactory::SendTextRequests()
129 mAtlasManager->SendTextRequests();
132 GlyphAtlasManagerInterface& FontFactory::GetAtlasManagerInterface()
134 return *mAtlasManager;
137 void FontFactory::SetDpi( float horizontalDpi, float verticalDpi )
139 mHorizontalDpi = horizontalDpi;
140 mVerticalDpi = verticalDpi;
143 void FontFactory::RecoverFromContextLoss()
145 mAtlasManager->ReloadAtlases();
148 } // namespace Internal