1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <svdata.hxx>
#include <fontinstance.hxx>
#include <impfontcache.hxx>
#include <PhysicalFontCollection.hxx>
#include <PhysicalFontFace.hxx>
#include <PhysicalFontFamily.hxx>
#include <sal/log.hxx>
size_t ImplFontCache::IFSD_Hash::operator()( const FontSelectPattern& rFSD ) const
{
return rFSD.hashCode();
}
bool ImplFontCache::IFSD_Equal::operator()(const FontSelectPattern& rA, const FontSelectPattern& rB) const
{
// check normalized font family name
if( rA.maSearchName != rB.maSearchName )
return false;
// check font transformation
if( (rA.mnHeight != rB.mnHeight)
|| (rA.mnWidth != rB.mnWidth)
|| (rA.mnOrientation != rB.mnOrientation) )
return false;
// check mapping relevant attributes
if( (rA.mbVertical != rB.mbVertical)
|| (rA.meLanguage != rB.meLanguage) )
return false;
// check font face attributes
if( (rA.GetWeight() != rB.GetWeight())
|| (rA.GetItalic() != rB.GetItalic())
// || (rA.meFamily != rB.meFamily) // TODO: remove this mostly obsolete member
|| (rA.GetPitch() != rB.GetPitch()) )
return false;
// check style name
if( rA.GetStyleName() != rB.GetStyleName() )
return false;
// Symbol fonts may recode from one type to another So they are only
// safely equivalent for equal targets
if (rA.IsSymbolFont() || rB.IsSymbolFont())
{
if (rA.maTargetName != rB.maTargetName)
return false;
}
// check for features
if ((rA.maTargetName.indexOf(FontSelectPattern::FEAT_PREFIX)
!= -1 ||
rB.maTargetName.indexOf(FontSelectPattern::FEAT_PREFIX)
!= -1) && rA.maTargetName != rB.maTargetName)
return false;
if (rA.mbEmbolden != rB.mbEmbolden)
return false;
if (rA.maItalicMatrix != rB.maItalicMatrix)
return false;
return true;
}
ImplFontCache::ImplFontCache()
: mpLastHitCacheEntry( nullptr )
, maFontInstanceList(0)
// The cache limit is set by the rough number of characters needed to read your average Asian newspaper.
, m_aBoundRectCache(3000)
{}
ImplFontCache::~ImplFontCache()
{
for (const auto & rLFI : maFontInstanceList)
rLFI.second->mpFontCache = nullptr;
}
rtl::Reference<LogicalFontInstance> ImplFontCache::GetFontInstance( PhysicalFontCollection const * pFontList,
const vcl::Font& rFont, const Size& rSize, float fExactHeight )
{
// initialize internal font request object
FontSelectPattern aFontSelData(rFont, rFont.GetFamilyName(), rSize, fExactHeight);
return GetFontInstance( pFontList, aFontSelData );
}
rtl::Reference<LogicalFontInstance> ImplFontCache::GetFontInstance( PhysicalFontCollection const * pFontList,
FontSelectPattern& aFontSelData )
{
rtl::Reference<LogicalFontInstance> pFontInstance;
PhysicalFontFamily* pFontFamily = nullptr;
// check if a directly matching logical font instance is already cached,
// the most recently used font usually has a hit rate of >50%
if (mpLastHitCacheEntry && IFSD_Equal()(aFontSelData, mpLastHitCacheEntry->GetFontSelectPattern()))
pFontInstance = mpLastHitCacheEntry;
else
{
FontInstanceList::const_iterator it = maFontInstanceList.find( aFontSelData );
if( it != maFontInstanceList.end() )
pFontInstance = (*it).second;
}
if( !pFontInstance ) // no direct cache hit
{
// find the best matching logical font family and update font selector accordingly
pFontFamily = pFontList->FindFontFamily( aFontSelData );
SAL_WARN_IF( (pFontFamily == nullptr), "vcl", "ImplFontCache::Get() No logical font found!" );
if( pFontFamily )
{
aFontSelData.maSearchName = pFontFamily->GetSearchName();
// check if an indirectly matching logical font instance is already cached
FontInstanceList::const_iterator it = maFontInstanceList.find( aFontSelData );
if( it != maFontInstanceList.end() )
pFontInstance = (*it).second;
}
}
if( !pFontInstance && pFontFamily) // still no cache hit => create a new font instance
{
PhysicalFontFace* pFontData = pFontFamily->FindBestFontFace(aFontSelData);
// create a new logical font instance from this physical font face
pFontInstance = pFontData->CreateFontInstance( aFontSelData );
pFontInstance->mpFontCache = this;
// if we're substituting from or to a symbol font we may need a symbol
// conversion table
if( pFontData->IsSymbolFont() || aFontSelData.IsSymbolFont() )
{
if( aFontSelData.maTargetName != aFontSelData.maSearchName )
pFontInstance->mpConversion = ConvertChar::GetRecodeData( aFontSelData.maTargetName, aFontSelData.maSearchName );
}
#ifdef MACOSX
//It might be better to dig out the font version of the target font
//to see if it's a modern re-coded apple symbol font in case that
//font shows up on a different platform
if (!pFontInstance->mpConversion &&
aFontSelData.maTargetName.equalsIgnoreAsciiCase("symbol") &&
aFontSelData.maSearchName.equalsIgnoreAsciiCase("symbol"))
{
pFontInstance->mpConversion = ConvertChar::GetRecodeData( "Symbol", "AppleSymbol" );
}
#endif
static const size_t FONTCACHE_MAX = getenv("LO_TESTNAME") ? 1 : 50;
if (maFontInstanceList.size() >= FONTCACHE_MAX)
{
struct limit_exception : public std::exception {};
try
{
maFontInstanceList.remove_if([this] (FontInstanceListPair const& rFontPair)
{
if (maFontInstanceList.size() < FONTCACHE_MAX)
throw limit_exception();
LogicalFontInstance* pFontEntry = rFontPair.second.get();
if (pFontEntry->m_nCount > 1)
return false;
m_aBoundRectCache.remove_if([&pFontEntry] (GlpyhBoundRectCachePair const& rGlyphPair)
{ return rGlyphPair.first.m_pFont == pFontEntry; });
if (mpLastHitCacheEntry == pFontEntry)
mpLastHitCacheEntry = nullptr;
return true;
});
}
catch (limit_exception&) {}
}
assert(pFontInstance);
// add the new entry to the cache
maFontInstanceList.insert({aFontSelData, pFontInstance.get()});
}
mpLastHitCacheEntry = pFontInstance.get();
return pFontInstance;
}
rtl::Reference<LogicalFontInstance> ImplFontCache::GetGlyphFallbackFont( PhysicalFontCollection const * pFontCollection,
FontSelectPattern& rFontSelData, LogicalFontInstance* pFontInstance, int nFallbackLevel, OUString& rMissingCodes )
{
// get a candidate font for glyph fallback
// unless the previously selected font got a device specific substitution
// e.g. PsPrint Arial->Helvetica for udiaeresis when Helvetica doesn't support it
if( nFallbackLevel >= 1)
{
PhysicalFontFamily* pFallbackData = nullptr;
//fdo#33898 If someone has EUDC installed then they really want that to
//be used as the first-choice glyph fallback seeing as it's filled with
//private area codes with don't make any sense in any other font so
//prioritize it here if it's available. Ideally we would remove from
//rMissingCodes all the glyphs which it is able to resolve as an
//optimization, but that's tricky to achieve cross-platform without
//sufficient heavy-weight code that's likely to undo the value of the
//optimization
if (nFallbackLevel == 1)
pFallbackData = pFontCollection->FindFontFamily("EUDC");
if (!pFallbackData)
pFallbackData = pFontCollection->GetGlyphFallbackFont(rFontSelData, pFontInstance, rMissingCodes, nFallbackLevel-1);
// escape when there are no font candidates
if( !pFallbackData )
return nullptr;
// override the font name
rFontSelData.SetFamilyName( pFallbackData->GetFamilyName() );
// clear the cached normalized name
rFontSelData.maSearchName.clear();
}
rtl::Reference<LogicalFontInstance> pFallbackFont = GetFontInstance( pFontCollection, rFontSelData );
return pFallbackFont;
}
void ImplFontCache::Invalidate()
{
// #112304# make sure the font cache is really clean
mpLastHitCacheEntry = nullptr;
for (auto const & pair : maFontInstanceList)
pair.second->mpFontCache = nullptr;
maFontInstanceList.clear();
m_aBoundRectCache.clear();
}
bool ImplFontCache::GetCachedGlyphBoundRect(LogicalFontInstance *pFont, sal_GlyphId nID, tools::Rectangle &rRect)
{
if (!pFont->GetFontCache())
return false;
assert(pFont->GetFontCache() == this);
if (pFont->GetFontCache() != this)
return false;
auto it = m_aBoundRectCache.find({pFont, nID});
if (it != m_aBoundRectCache.end())
{
rRect = it->second;
return true;
}
return false;
}
void ImplFontCache::CacheGlyphBoundRect(LogicalFontInstance *pFont, sal_GlyphId nID, tools::Rectangle &rRect)
{
if (!pFont->GetFontCache())
return;
assert(pFont->GetFontCache() == this);
if (pFont->GetFontCache() != this)
return;
m_aBoundRectCache.insert({{pFont, nID}, rRect});
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|