LibreOffice Module vcl (master)  1
glyphcache.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #ifndef INCLUDED_VCL_INC_GENERIC_GLYPHCACHE_HXX
21 #define INCLUDED_VCL_INC_GENERIC_GLYPHCACHE_HXX
22 
23 #include <memory>
24 #include <freetype/config/ftheader.h>
25 #include FT_FREETYPE_H
26 #include FT_GLYPH_H
27 
28 #include <tools/gen.hxx>
29 #include <tools/solar.h>
30 #include <unx/gendata.hxx>
31 #include <vcl/dllapi.h>
32 #include <vcl/outdev.hxx>
33 
34 #include <fontattributes.hxx>
35 #include <fontinstance.hxx>
36 #include <impfontmetricdata.hxx>
37 
38 #include <unordered_map>
39 
40 class FreetypeFont;
41 class FreetypeFontFile;
43 class FreetypeFontInfo;
46 class FreetypeFont;
47 class SvpGcpHelper;
48 
49 namespace basegfx { class B2DPolyPolygon; }
50 namespace vcl { struct FontCapabilities; }
51 
79 {
80 public:
81  ~FreetypeManager();
82 
83  static FreetypeManager& get();
84 
85  void AddFontFile(const OString& rNormalizedName,
86  int nFaceNum, int nVariantNum,
87  sal_IntPtr nFontId,
88  const FontAttributes&);
89 
90  void AnnounceFonts( PhysicalFontCollection* ) const;
91 
92  FreetypeFont* CacheFont(LogicalFontInstance* pFontInstance);
93  void UncacheFont( FreetypeFont& );
94 
105  void TryGarbageCollectFont(LogicalFontInstance*);
106 
107  void ClearFontCache();
108  void ClearFontOptions();
109 
110 private:
111  // to access the constructor (can't use InitFreetypeManager function, because it's private?!)
112  friend class GenericUnixSalData;
113  explicit FreetypeManager();
114 
115  static void InitFreetype();
116  void GarbageCollect();
118  FreetypeFontFile* FindFontFile(const OString& rNativeFileName);
119 
120  // the FreetypeManager's FontList matches a font request to a serverfont instance
121  // the FontList key's mpFontData member is reinterpreted as integer font id
122  struct IFSD_Equal{ bool operator()( const rtl::Reference<LogicalFontInstance>&, const rtl::Reference<LogicalFontInstance>& ) const; };
123  struct IFSD_Hash{ size_t operator()( const rtl::Reference<LogicalFontInstance>& ) const; };
124  typedef std::unordered_map<rtl::Reference<LogicalFontInstance>,std::unique_ptr<FreetypeFont>,IFSD_Hash,IFSD_Equal > FontList;
125  typedef std::unordered_map<sal_IntPtr, std::unique_ptr<FreetypeFontInfo>> FontInfoList;
126  typedef std::unordered_map<const char*, std::unique_ptr<FreetypeFontFile>, rtl::CStringHash, rtl::CStringEqual> FontFileList;
127 
128  FontList maFontList;
129  static constexpr sal_uLong gnMaxSize = 1500000; // max overall cache size in bytes
132 
133  FontInfoList m_aFontInfoList;
134  sal_IntPtr m_nMaxFontId;
135 
136  FontFileList m_aFontFileList;
137 };
138 
140 {
141 public:
142  ~FreetypeFont();
143 
144  const OString& GetFontFileName() const;
145  int GetFontFaceIndex() const;
146  int GetFontFaceVariation() const;
147  bool TestFont() const { return mbFaceOk;}
148  FT_Face GetFtFace() const;
149  int GetLoadFlags() const { return (mnLoadFlags & ~FT_LOAD_IGNORE_TRANSFORM); }
150  const FontConfigFontOptions* GetFontOptions() const;
151  void ClearFontOptions();
152  bool NeedsArtificialBold() const { return mbArtBold; }
153  bool NeedsArtificialItalic() const { return mbArtItalic; }
154 
155  void GetFontMetric(ImplFontMetricDataRef const &) const;
156  const unsigned char* GetTable( const char* pName, sal_uLong* pLength ) const;
157  FontCharMapRef GetFontCharMap() const;
158  bool GetFontCapabilities(vcl::FontCapabilities &) const;
159 
160  bool GetGlyphBoundRect(sal_GlyphId, tools::Rectangle&, bool) const;
161  bool GetGlyphOutline(sal_GlyphId, basegfx::B2DPolyPolygon&, bool) const;
162  bool GetAntialiasAdvice() const;
163 
164  FreetypeFontInstance* GetFontInstance() const { return mpFontInstance.get(); }
165 
166  void SetFontVariationsOnHBFont(hb_font_t* pHbFace) const;
167 
168  // tdf#127189 FreeType <= 2.8 will fail to render stretched horizontal brace glyphs
169  // in starmath at a fairly low stretch ratio. This appears fixed in 2.9 with
170  // https://git.savannah.gnu.org/cgit/freetype/freetype2.git/commit/?id=91015cb41d8f56777f93394f5a60914bc0c0f330
171  // "Improve complex rendering at high ppem"
172  static bool AlmostHorizontalDrainsRenderingPool(int nRatio, const FontSelectPattern& rFSD);
173 
174 private:
175  friend class FreetypeManager;
177 
178  void AddRef() const { ++mnRefCount; }
179  long GetRefCount() const { return mnRefCount; }
180  long Release() const;
181  sal_uLong GetByteCount() const { return mnBytesUsed; }
182 
183  void ReleaseFromGarbageCollect();
184 
185  void ApplyGlyphTransform(bool bVertical, FT_Glyph) const;
186 
188 
189  // used by FreetypeManager for cache LRU algorithm
190  mutable long mnRefCount;
192 
195 
196  // 16.16 fixed point values used for a rotated font
197  long mnCos;
198  long mnSin;
199 
200  int mnWidth;
203  FT_Int mnLoadFlags;
204  double mfStretch;
205  FT_FaceRec_* maFaceFT;
206  FT_SizeRec_* maSizeFT;
207 
208  mutable std::unique_ptr<FontConfigFontOptions> mxFontOptions;
209 
210  bool mbFaceOk;
212  bool mbArtBold;
213 };
214 
215 #endif // INCLUDED_VCL_INC_GENERIC_GLYPHCACHE_HXX
216 
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
FT_SizeRec_ * maSizeFT
Definition: glyphcache.hxx:206
long GetRefCount() const
Definition: glyphcache.hxx:179
std::unordered_map< sal_IntPtr, std::unique_ptr< FreetypeFontInfo > > FontInfoList
Definition: glyphcache.hxx:125
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
sal_uIntPtr sal_uLong
sal_IntPtr m_nMaxFontId
Definition: glyphcache.hxx:134
FontFileList m_aFontFileList
Definition: glyphcache.hxx:136
FreetypeFont * mpNextGCFont
Definition: glyphcache.hxx:194
std::unordered_map< rtl::Reference< LogicalFontInstance >, std::unique_ptr< FreetypeFont >, IFSD_Hash, IFSD_Equal > FontList
Definition: glyphcache.hxx:124
sal_uInt16 sal_GlyphId
Definition: glyphitem.hxx:26
FreetypeFontInfo * mpFontInfo
Definition: glyphcache.hxx:202
FreetypeFontInstance * GetFontInstance() const
Definition: glyphcache.hxx:164
std::unordered_map< const char *, std::unique_ptr< FreetypeFontFile >, rtl::CStringHash, rtl::CStringEqual > FontFileList
Definition: glyphcache.hxx:126
bool NeedsArtificialItalic() const
Definition: glyphcache.hxx:153
FreetypeFont * mpCurrentGCFont
Definition: glyphcache.hxx:131
bool NeedsArtificialBold() const
Definition: glyphcache.hxx:152
sal_uInt16 char * pName
double mfStretch
Definition: glyphcache.hxx:204
The FreetypeManager caches various aspects of Freetype fonts.
Definition: glyphcache.hxx:78
FontList maFontList
Definition: glyphcache.hxx:128
void CreateFont(SvxFont &rFont, const SfxItemSet &rSet, bool bSearchInParent, SvtScriptType nScriptType)
sal_uLong mnBytesUsed
Definition: glyphcache.hxx:130
FT_FaceRec_ * maFaceFT
Definition: glyphcache.hxx:205
sal_uLong GetByteCount() const
Definition: glyphcache.hxx:181
FreetypeFont * mpPrevGCFont
Definition: glyphcache.hxx:193
bool TestFont() const
Definition: glyphcache.hxx:147
sal_uLong mnBytesUsed
Definition: glyphcache.hxx:191
FontInfoList m_aFontInfoList
Definition: glyphcache.hxx:133
void AddRef() const
Definition: glyphcache.hxx:178
std::unique_ptr< FontConfigFontOptions > mxFontOptions
Definition: glyphcache.hxx:208
rtl::Reference< FreetypeFontInstance > mpFontInstance
Definition: glyphcache.hxx:187
int GetLoadFlags() const
Definition: glyphcache.hxx:149
FT_Int mnLoadFlags
Definition: glyphcache.hxx:203