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 #ifndef INCLUDED_VCL_INC_GENERIC_GLYPHCACHE_HXX
21 #define INCLUDED_VCL_INC_GENERIC_GLYPHCACHE_HXX
24 #include <freetype/config/ftheader.h>
25 #include FT_FREETYPE_H
28 #include <tools/gen.hxx>
29 #include <tools/solar.h>
30 #include <vcl/dllapi.h>
31 #include <vcl/outdev.hxx>
33 #include <fontattributes.hxx>
34 #include <fontinstance.hxx>
35 #include <impfontmetricdata.hxx>
37 #include <unordered_map>
40 class FreetypeFontFile
;
41 class FreetypeFontInstance
;
42 class FreetypeFontInfo
;
43 class FontConfigFontOptions
;
44 class PhysicalFontCollection
;
48 namespace basegfx
{ class B2DPolyPolygon
; }
49 namespace vcl
{ struct FontCapabilities
; }
52 * The GlyphCache caches various aspects of Freetype fonts
54 * It mainly consists of three std::unordered_map lists, which hold the items of the cache.
56 * They form kind of a tree, with FreetypeFontFile as the roots, referenced by multiple FreetypeFontInfo
57 * entries, which are referenced by the FreetypeFont items.
59 * All of these items have reference counters, but these don't control the items life-cycle, but that of
60 * the managed resources.
62 * The respective resources are:
63 * FreetypeFontFile = holds the mmapped font file, as long as it's used by any FreetypeFontInfo.
64 * FreetypeFontInfo = holds the FT_FaceRec_ object, as long as it's used by any FreetypeFont.
65 * FreetypeFont = holds the FT_SizeRec_.
67 * FreetypeFontInfo therefore is embedded in the Freetype subclass of PhysicalFontFace.
68 * FreetypeFont is embedded in the Freetype subclass of LogicalFontInstance.
70 * Nowadays there is not really a reason to have seperate files for the classes, as the GlyphCache is
71 * just about handling of Freetype based fonts, not some abstract glyphs.
73 * One additional note: the byte-size based garbage collection of unused fonts can currently be assumed
74 * to be broken. Since the move of the glyph rect cache into the ImplFontCache, so it can be used by all
75 * platforms, it just takes too long to kick-in, as there is no real accounting left.
77 class VCL_DLLPUBLIC GlyphCache final
82 static GlyphCache
& GetInstance();
84 void AddFontFile(const OString
& rNormalizedName
,
85 int nFaceNum
, int nVariantNum
,
87 const FontAttributes
&);
89 void AnnounceFonts( PhysicalFontCollection
* ) const;
91 FreetypeFont
* CacheFont(LogicalFontInstance
* pFontInstance
);
92 void UncacheFont( FreetypeFont
& );
94 /** Try to GarbageCollect an explicit logical font
96 * This should just be called from the ~ImplFontCache destructor, which holds the mapping of the
97 * FontSelectPattern to the LogicalFontInstance per OutputDevice. All other users should just
98 * call CacheFont and UncacheFont correctly. When the ImplFontCache is destroyed with its
99 * OutputDevice, we can safely garbage collection its unused entries, as these can't be reused.
101 * It's always safe to call this, as it just ignores the used bytes when considering a font for
102 * garbage collection, which normally keeps unreferenced fonts alive.
104 void TryGarbageCollectFont(LogicalFontInstance
*);
106 void ClearFontCache();
107 void ClearFontOptions();
110 // to access the constructor (can't use InitFreetypeManager function, because it's private?!)
111 friend class GenericUnixSalData
;
112 explicit GlyphCache();
114 static void InitFreetype();
115 void GarbageCollect();
116 FreetypeFont
* CreateFont(LogicalFontInstance
* pLogicalFont
);
117 FreetypeFontFile
* FindFontFile(const OString
& rNativeFileName
);
119 // the GlyphCache's FontList matches a font request to a serverfont instance
120 // the FontList key's mpFontData member is reinterpreted as integer font id
121 struct IFSD_Equal
{ bool operator()( const rtl::Reference
<LogicalFontInstance
>&, const rtl::Reference
<LogicalFontInstance
>& ) const; };
122 struct IFSD_Hash
{ size_t operator()( const rtl::Reference
<LogicalFontInstance
>& ) const; };
123 typedef std::unordered_map
<rtl::Reference
<LogicalFontInstance
>,std::unique_ptr
<FreetypeFont
>,IFSD_Hash
,IFSD_Equal
> FontList
;
124 typedef std::unordered_map
<sal_IntPtr
, std::unique_ptr
<FreetypeFontInfo
>> FontInfoList
;
125 typedef std::unordered_map
<const char*, std::unique_ptr
<FreetypeFontFile
>, rtl::CStringHash
, rtl::CStringEqual
> FontFileList
;
128 static constexpr sal_uLong gnMaxSize
= 1500000; // max overall cache size in bytes
129 mutable sal_uLong mnBytesUsed
;
130 FreetypeFont
* mpCurrentGCFont
;
132 FontInfoList m_aFontInfoList
;
133 sal_IntPtr m_nMaxFontId
;
135 FontFileList m_aFontFileList
;
138 class FreetypeFont final
143 const OString
& GetFontFileName() const;
144 int GetFontFaceIndex() const;
145 int GetFontFaceVariation() const;
146 bool TestFont() const { return mbFaceOk
;}
147 FT_Face
GetFtFace() const;
148 int GetLoadFlags() const { return (mnLoadFlags
& ~FT_LOAD_IGNORE_TRANSFORM
); }
149 const FontConfigFontOptions
* GetFontOptions() const;
150 void ClearFontOptions();
151 bool NeedsArtificialBold() const { return mbArtBold
; }
152 bool NeedsArtificialItalic() const { return mbArtItalic
; }
154 void GetFontMetric(ImplFontMetricDataRef
const &) const;
155 const unsigned char* GetTable( const char* pName
, sal_uLong
* pLength
) const;
156 FontCharMapRef
GetFontCharMap() const;
157 bool GetFontCapabilities(vcl::FontCapabilities
&) const;
159 bool GetGlyphBoundRect(sal_GlyphId
, tools::Rectangle
&, bool) const;
160 bool GetGlyphOutline(sal_GlyphId
, basegfx::B2DPolyPolygon
&, bool) const;
161 bool GetAntialiasAdvice() const;
163 FreetypeFontInstance
* GetFontInstance() const { return mpFontInstance
.get(); }
165 void SetFontVariationsOnHBFont(hb_font_t
* pHbFace
) const;
167 // tdf#127189 FreeType <= 2.8 will fail to render stretched horizontal brace glyphs
168 // in starmath at a fairly low stretch ratio. This appears fixed in 2.9 with
169 // https://git.savannah.gnu.org/cgit/freetype/freetype2.git/commit/?id=91015cb41d8f56777f93394f5a60914bc0c0f330
170 // "Improve complex rendering at high ppem"
171 static bool AlmostHorizontalDrainsRenderingPool();
174 friend class GlyphCache
;
175 explicit FreetypeFont(LogicalFontInstance
*, FreetypeFontInfo
*);
177 void AddRef() const { ++mnRefCount
; }
178 long GetRefCount() const { return mnRefCount
; }
179 long Release() const;
180 sal_uLong
GetByteCount() const { return mnBytesUsed
; }
182 void ReleaseFromGarbageCollect();
184 void ApplyGlyphTransform(bool bVertical
, FT_Glyph
) const;
186 rtl::Reference
<FreetypeFontInstance
> mpFontInstance
;
188 // used by GlyphCache for cache LRU algorithm
189 mutable long mnRefCount
;
190 mutable sal_uLong mnBytesUsed
;
192 FreetypeFont
* mpPrevGCFont
;
193 FreetypeFont
* mpNextGCFont
;
195 // 16.16 fixed point values used for a rotated font
200 int const mnPrioAntiAlias
;
201 FreetypeFontInfo
* mpFontInfo
;
204 FT_FaceRec_
* maFaceFT
;
205 FT_SizeRec_
* maSizeFT
;
207 mutable std::unique_ptr
<FontConfigFontOptions
> mxFontOptions
;
214 #endif // INCLUDED_VCL_INC_GENERIC_GLYPHCACHE_HXX
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */