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 <vcl/dllapi.h>
31 #include <vcl/outdev.hxx>
32 
33 #include <fontattributes.hxx>
34 #include <fontinstance.hxx>
35 #include <impfontmetricdata.hxx>
36 
37 #include <unordered_map>
38 
39 class FreetypeFont;
41 class FreetypeFontInfo;
44 class FreetypeFont;
45 class SvpGcpHelper;
46 
47 namespace basegfx { class B2DPolyPolygon; }
48 namespace vcl { struct FontCapabilities; }
49 
51 {
52 public:
53  explicit GlyphCache();
54  virtual ~GlyphCache();
55 
56  static GlyphCache& GetInstance();
57 
58  void AddFontFile(const OString& rNormalizedName,
59  int nFaceNum, int nVariantNum,
60  sal_IntPtr nFontId,
61  const FontAttributes&);
62 
63  void AnnounceFonts( PhysicalFontCollection* ) const;
64 
65  FreetypeFont* CacheFont(LogicalFontInstance* pFontInstance);
66  void UncacheFont( FreetypeFont& );
67  void ClearFontCache();
68  void ClearFontOptions();
69 
70 private:
71  static void InitFreetype();
72  void GarbageCollect();
74 
75  // the GlyphCache's FontList matches a font request to a serverfont instance
76  // the FontList key's mpFontData member is reinterpreted as integer font id
77  struct IFSD_Equal{ bool operator()( const rtl::Reference<LogicalFontInstance>&, const rtl::Reference<LogicalFontInstance>& ) const; };
78  struct IFSD_Hash{ size_t operator()( const rtl::Reference<LogicalFontInstance>& ) const; };
79  typedef std::unordered_map<rtl::Reference<LogicalFontInstance>,std::unique_ptr<FreetypeFont>,IFSD_Hash,IFSD_Equal > FontList;
80  typedef std::unordered_map<sal_IntPtr, std::unique_ptr<FreetypeFontInfo>> FontInfoList;
81 
82  FontList maFontList;
83  static constexpr sal_uLong gnMaxSize = 1500000; // max overall cache size in bytes
86 
87  FontInfoList m_aFontInfoList;
88  sal_IntPtr m_nMaxFontId;
89 };
90 
91 class FreetypeFont final
92 {
93 public:
95  ~FreetypeFont();
96 
97  const OString& GetFontFileName() const;
98  int GetFontFaceIndex() const;
99  int GetFontFaceVariation() const;
100  bool TestFont() const { return mbFaceOk;}
101  FT_Face GetFtFace() const;
102  int GetLoadFlags() const { return (mnLoadFlags & ~FT_LOAD_IGNORE_TRANSFORM); }
103  const FontConfigFontOptions* GetFontOptions() const;
104  void ClearFontOptions();
105  bool NeedsArtificialBold() const { return mbArtBold; }
106  bool NeedsArtificialItalic() const { return mbArtItalic; }
107 
108  void GetFontMetric(ImplFontMetricDataRef const &) const;
109  const unsigned char* GetTable( const char* pName, sal_uLong* pLength ) const;
112 
113  bool GetGlyphBoundRect(sal_GlyphId, tools::Rectangle&, bool) const;
115  bool GetAntialiasAdvice() const;
116 
118 
119  void SetFontVariationsOnHBFont(hb_font_t* pHbFace) const;
120 
121  // tdf#127189 FreeType <= 2.8 will fail to render stretched horizontal brace glyphs
122  // in starmath at a fairly low stretch ratio. This appears fixed in 2.9 with
123  // https://git.savannah.gnu.org/cgit/freetype/freetype2.git/commit/?id=91015cb41d8f56777f93394f5a60914bc0c0f330
124  // "Improve complex rendering at high ppem"
126 
127 private:
128  friend class GlyphCache;
129  friend class FreetypeFontInstance;
130  friend class X11SalGraphics;
131  friend class CairoTextRender;
132 
133  void AddRef() const { ++mnRefCount; }
134  long GetRefCount() const { return mnRefCount; }
135  long Release() const;
136  sal_uLong GetByteCount() const { return mnBytesUsed; }
137 
139 
140  void ApplyGlyphTransform(bool bVertical, FT_Glyph) const;
141 
143 
144  // used by GlyphCache for cache LRU algorithm
145  mutable long mnRefCount;
147 
150 
151  // 16.16 fixed point values used for a rotated font
152  long mnCos;
153  long mnSin;
154 
155  int mnWidth;
156  int const mnPrioAntiAlias;
158  FT_Int mnLoadFlags;
159  double mfStretch;
160  FT_FaceRec_* maFaceFT;
161  FT_SizeRec_* maSizeFT;
162 
163  mutable std::unique_ptr<FontConfigFontOptions> mxFontOptions;
164 
165  bool mbFaceOk;
167  bool mbArtBold;
168 };
169 
170 #endif // INCLUDED_VCL_INC_GENERIC_GLYPHCACHE_HXX
171 
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const unsigned char * GetTable(const char *pName, sal_uLong *pLength) const
FT_SizeRec_ * maSizeFT
Definition: glyphcache.hxx:161
long GetRefCount() const
Definition: glyphcache.hxx:134
sal_IntPtr m_nMaxFontId
Definition: glyphcache.hxx:88
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
sal_uIntPtr sal_uLong
sal_uLong mnBytesUsed
Definition: glyphcache.hxx:84
FT_Face GetFtFace() const
void GetFontMetric(ImplFontMetricDataRef const &) const
FreetypeFont * mpNextGCFont
Definition: glyphcache.hxx:149
FreetypeFont(LogicalFontInstance *pFontInstance, FreetypeFontInfo *)
sal_uInt16 sal_GlyphId
Definition: glyphitem.hxx:26
FreetypeFontInfo * mpFontInfo
Definition: glyphcache.hxx:157
FreetypeFontInstance * GetFontInstance() const
Definition: glyphcache.hxx:117
bool NeedsArtificialItalic() const
Definition: glyphcache.hxx:106
FontInfoList m_aFontInfoList
Definition: glyphcache.hxx:87
static bool AlmostHorizontalDrainsRenderingPool()
long Release() const
bool NeedsArtificialBold() const
Definition: glyphcache.hxx:105
bool GetGlyphBoundRect(sal_GlyphId, tools::Rectangle &, bool) const
void ClearFontOptions()
void ReleaseFromGarbageCollect()
double mfStretch
Definition: glyphcache.hxx:159
FontCharMapRef GetFontCharMap() const
void CreateFont(SvxFont &rFont, const SfxItemSet &rSet, bool bSearchInParent, SvtScriptType nScriptType)
void SetFontVariationsOnHBFont(hb_font_t *pHbFace) const
void ApplyGlyphTransform(bool bVertical, FT_Glyph) const
FreetypeFont * mpCurrentGCFont
Definition: glyphcache.hxx:85
bool GetFontCapabilities(vcl::FontCapabilities &) const
FT_FaceRec_ * maFaceFT
Definition: glyphcache.hxx:160
sal_uInt16 sal_Char * pName
std::unordered_map< rtl::Reference< LogicalFontInstance >, std::unique_ptr< FreetypeFont >, IFSD_Hash, IFSD_Equal > FontList
Definition: glyphcache.hxx:79
const OString & GetFontFileName() const
sal_uLong GetByteCount() const
Definition: glyphcache.hxx:136
FreetypeFont * mpPrevGCFont
Definition: glyphcache.hxx:148
int GetFontFaceVariation() const
bool TestFont() const
Definition: glyphcache.hxx:100
sal_uLong mnBytesUsed
Definition: glyphcache.hxx:146
int GetFontFaceIndex() const
void AddRef() const
Definition: glyphcache.hxx:133
std::unique_ptr< FontConfigFontOptions > mxFontOptions
Definition: glyphcache.hxx:163
bool GetGlyphOutline(sal_GlyphId, basegfx::B2DPolyPolygon &, bool) const
FontList maFontList
Definition: glyphcache.hxx:82
const FontConfigFontOptions * GetFontOptions() const
bool GetAntialiasAdvice() const
std::unordered_map< sal_IntPtr, std::unique_ptr< FreetypeFontInfo > > FontInfoList
Definition: glyphcache.hxx:80
rtl::Reference< FreetypeFontInstance > mpFontInstance
Definition: glyphcache.hxx:142
int GetLoadFlags() const
Definition: glyphcache.hxx:102
int const mnPrioAntiAlias
Definition: glyphcache.hxx:156
FT_Int mnLoadFlags
Definition: glyphcache.hxx:158