1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "generic/geninst.h"
21 #include "generic/genpspgraphics.h"
22 #include "generic/glyphcache.hxx"
24 #include "vcl/sysdata.hxx"
25 #include "outfont.hxx"
27 #include "generic/printergfx.hxx"
29 #include "impfont.hxx"
30 #include "outfont.hxx"
32 #include "fontsubset.hxx"
36 #include <unotools/fontdefs.hxx>
39 // ===========================================================================
40 // platform specific font substitution hooks
41 // ===========================================================================
43 struct FontSelectPatternAttributesHash
45 size_t operator()(const FontSelectPatternAttributes
& rAttributes
) const
46 { return rAttributes
.hashCode(); }
49 class FcPreMatchSubstititution
50 : public ImplPreMatchFontSubstitution
53 bool FindFontSubstitute( FontSelectPattern
& ) const;
54 typedef ::std::pair
<FontSelectPatternAttributes
, FontSelectPatternAttributes
> value_type
;
56 typedef ::std::list
<value_type
> CachedFontMapType
;
57 mutable CachedFontMapType maCachedFontMap
;
60 class FcGlyphFallbackSubstititution
61 : public ImplGlyphFallbackFontSubstitution
65 bool FindFontSubstitute( FontSelectPattern
&, rtl::OUString
& rMissingCodes
) const;
68 int SalGenericInstance::FetchFontSubstitutionFlags()
70 // init font substitution defaults
73 nDisableBits
= 1; // disable "font fallback" here on default
75 // apply the environment variable if any
76 const char* pEnvStr
= ::getenv( "SAL_DISABLE_FC_SUBST" );
79 if( (*pEnvStr
>= '0') && (*pEnvStr
<= '9') )
80 nDisableBits
= (*pEnvStr
- '0');
82 nDisableBits
= ~0U; // no specific bits set: disable all
87 void SalGenericInstance::RegisterFontSubstitutors( ImplDevFontList
* pList
)
89 // init font substitution defaults
92 nDisableBits
= 1; // disable "font fallback" here on default
94 // apply the environment variable if any
95 const char* pEnvStr
= ::getenv( "SAL_DISABLE_FC_SUBST" );
98 if( (*pEnvStr
>= '0') && (*pEnvStr
<= '9') )
99 nDisableBits
= (*pEnvStr
- '0');
101 nDisableBits
= ~0U; // no specific bits set: disable all
104 // register font fallback substitutions (unless disabled by bit0)
105 if( (nDisableBits
& 1) == 0 )
107 static FcPreMatchSubstititution aSubstPreMatch
;
108 pList
->SetPreMatchHook( &aSubstPreMatch
);
111 // register glyph fallback substitutions (unless disabled by bit1)
112 if( (nDisableBits
& 2) == 0 )
114 static FcGlyphFallbackSubstititution aSubstFallback
;
115 pList
->SetFallbackHook( &aSubstFallback
);
119 // -----------------------------------------------------------------------
121 static FontSelectPattern
GetFcSubstitute(const FontSelectPattern
&rFontSelData
, rtl::OUString
& rMissingCodes
)
123 FontSelectPattern
aSubstituted(rFontSelData
);
124 psp::PrintFontManager
& rMgr
= psp::PrintFontManager::get();
125 rMgr
.Substitute(aSubstituted
, rMissingCodes
);
131 bool uselessmatch(const FontSelectPattern
&rOrig
, const FontSelectPattern
&rNew
)
135 rOrig
.maTargetName
== rNew
.maSearchName
&&
136 rOrig
.meWeight
== rNew
.meWeight
&&
137 rOrig
.meItalic
== rNew
.meItalic
&&
138 rOrig
.mePitch
== rNew
.mePitch
&&
139 rOrig
.meWidthType
== rNew
.meWidthType
146 const FontSelectPatternAttributes
& mrAttributes
;
148 equal(const FontSelectPatternAttributes
& rAttributes
)
149 : mrAttributes(rAttributes
)
152 bool operator()(const FcPreMatchSubstititution::value_type
& rOther
) const
153 { return rOther
.first
== mrAttributes
; }
157 //--------------------------------------------------------------------------
159 bool FcPreMatchSubstititution::FindFontSubstitute( FontSelectPattern
&rFontSelData
) const
161 // We dont' actually want to talk to Fontconfig at all for symbol fonts
162 if( rFontSelData
.IsSymbolFont() )
164 // StarSymbol is a unicode font, but it still deserves the symbol flag
165 if ( IsStarSymbol(rFontSelData
.maSearchName
) )
168 //see fdo#41556 and fdo#47636
169 //fontconfig can return e.g. an italic font for a non-italic input and/or
170 //different fonts depending on fontsize, bold, etc settings so don't cache
171 //just on the name, cache map all the input and all the output not just map
172 //from original selection to output fontname
173 FontSelectPatternAttributes
& rPatternAttributes
= rFontSelData
;
174 CachedFontMapType
&rCachedFontMap
= const_cast<CachedFontMapType
&>(maCachedFontMap
);
175 CachedFontMapType::iterator itr
= std::find_if(rCachedFontMap
.begin(), rCachedFontMap
.end(), equal(rPatternAttributes
));
176 if (itr
!= rCachedFontMap
.end())
178 // Cached substitution
179 rFontSelData
.copyAttributes(itr
->second
);
180 if (itr
!= rCachedFontMap
.begin())
182 // MRU, move it to the front
183 rCachedFontMap
.splice(rCachedFontMap
.begin(), rCachedFontMap
, itr
);
188 rtl::OUString aDummy
;
189 const FontSelectPattern aOut
= GetFcSubstitute( rFontSelData
, aDummy
);
191 if( !aOut
.maSearchName
.Len() )
194 const bool bHaveSubstitute
= !uselessmatch( rFontSelData
, aOut
);
197 const rtl::OString
aOrigName(rtl::OUStringToOString(rFontSelData
.maTargetName
,
198 RTL_TEXTENCODING_UTF8
));
199 const rtl::OString
aSubstName(rtl::OUStringToOString(aOut
.maSearchName
,
200 RTL_TEXTENCODING_UTF8
));
201 printf( "FcPreMatchSubstititution \"%s\" bipw=%d%d%d%d -> ",
202 aOrigName
.getStr(), rFontSelData
.meWeight
, rFontSelData
.meItalic
,
203 rFontSelData
.mePitch
, rFontSelData
.meWidthType
);
204 if( !bHaveSubstitute
)
205 printf( "no substitute available\n" );
207 printf( "\"%s\" bipw=%d%d%d%d\n", aSubstName
.getStr(),
208 aOut
.meWeight
, aOut
.meItalic
, aOut
.mePitch
, aOut
.meWidthType
);
211 if( bHaveSubstitute
)
213 rCachedFontMap
.push_front(value_type(rFontSelData
, aOut
));
214 //fairly arbitrary limit in this case, but I recall measuring max 8
215 //fonts as the typical max amount of fonts in medium sized documents
216 if (rCachedFontMap
.size() > 8)
217 rCachedFontMap
.pop_back();
221 return bHaveSubstitute
;
224 // -----------------------------------------------------------------------
226 bool FcGlyphFallbackSubstititution::FindFontSubstitute( FontSelectPattern
& rFontSelData
,
227 rtl::OUString
& rMissingCodes
) const
229 // We dont' actually want to talk to Fontconfig at all for symbol fonts
230 if( rFontSelData
.IsSymbolFont() )
232 // StarSymbol is a unicode font, but it still deserves the symbol flag
233 if ( IsStarSymbol(rFontSelData
.maSearchName
) )
236 const FontSelectPattern aOut
= GetFcSubstitute( rFontSelData
, rMissingCodes
);
237 // TODO: cache the unicode + srcfont specific result
238 // FC doing it would be preferable because it knows the invariables
239 // e.g. FC knows the FC rule that all Arial gets replaced by LiberationSans
240 // whereas we would have to check for every size or attribute
241 if( !aOut
.maSearchName
.Len() )
244 const bool bHaveSubstitute
= !uselessmatch( rFontSelData
, aOut
);
247 const rtl::OString
aOrigName(rtl::OUStringToOString(rFontSelData
.maTargetName
,
248 RTL_TEXTENCODING_UTF8
));
249 const rtl::OString
aSubstName(rtl::OUStringToOString(aOut
.maSearchName
,
250 RTL_TEXTENCODING_UTF8
));
251 printf( "FcGFSubstititution \"%s\" bipw=%d%d%d%d ->",
252 aOrigName
.getStr(), rFontSelData
.meWeight
, rFontSelData
.meItalic
,
253 rFontSelData
.mePitch
, rFontSelData
.meWidthType
);
254 if( !bHaveSubstitute
)
255 printf( "no substitute available\n" );
257 printf( "\"%s\" bipw=%d%d%d%d\n", aSubstName
.getStr(),
258 aOut
.meWeight
, aOut
.meItalic
, aOut
.mePitch
, aOut
.meWidthType
);
261 if( bHaveSubstitute
)
264 return bHaveSubstitute
;
267 // ===========================================================================
270 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */