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
25 #include FT_FREETYPE_H
28 #include <com/sun/star/i18n/XBreakIterator.hpp>
29 #include <tools/gen.hxx>
30 #include <vcl/dllapi.h>
31 #include <vcl/metric.hxx>
33 #include <fontinstance.hxx>
34 #include <sallayout.hxx>
35 #include "fontattributes.hxx"
36 #include "impfontmetricdata.hxx"
39 #include <unordered_map>
41 class FreetypeManager
;
42 class FreetypeFontInfo
;
44 class FontConfigFontOptions
;
45 class PhysicalFontCollection
;
49 namespace basegfx
{ class B2DPolyPolygon
; }
50 namespace vcl
{ struct FontCapabilities
; }
52 class VCL_DLLPUBLIC GlyphCache
55 explicit GlyphCache();
56 virtual ~GlyphCache();
58 static GlyphCache
& GetInstance();
61 const OString
& rNormalizedName
,
62 int nFaceNum
, sal_IntPtr nFontId
,
63 const FontAttributes
&);
65 void AnnounceFonts( PhysicalFontCollection
* ) const;
67 FreetypeFont
* CacheFont( const FontSelectPattern
& );
68 void UncacheFont( FreetypeFont
& );
69 void ClearFontCache();
70 void InvalidateAllGlyphs();
71 void ClearFontOptions();
74 friend class FreetypeFont
;
75 // used by FreetypeFont class only
76 void AddedGlyph( GlyphData
& );
78 void UsingGlyph( GlyphData
& );
81 void GarbageCollect();
83 // the GlyphCache's FontList matches a font request to a serverfont instance
84 // the FontList key's mpFontData member is reinterpreted as integer font id
85 struct IFSD_Equal
{ bool operator()( const FontSelectPattern
&, const FontSelectPattern
& ) const; };
86 struct IFSD_Hash
{ size_t operator()( const FontSelectPattern
& ) const; };
87 typedef std::unordered_map
<FontSelectPattern
,FreetypeFont
*,IFSD_Hash
,IFSD_Equal
> FontList
;
90 sal_uLong mnMaxSize
; // max overall cache size in bytes
91 mutable sal_uLong mnBytesUsed
;
92 mutable long mnLruIndex
;
93 mutable int mnGlyphCount
;
94 FreetypeFont
* mpCurrentGCFont
;
96 FreetypeManager
* mpFtManager
;
102 GlyphData() : mnLruValue(0) {}
104 const tools::Rectangle
& GetBoundRect() const { return maBoundRect
; }
105 void SetBoundRect(tools::Rectangle r
) { maBoundRect
= r
; }
107 void SetLruValue( int n
) const { mnLruValue
= n
; }
108 long GetLruValue() const { return mnLruValue
;}
111 tools::Rectangle maBoundRect
;
113 // used by GlyphCache for cache LRU algorithm
114 mutable long mnLruValue
;
117 class VCL_DLLPUBLIC FreetypeFont final
120 FreetypeFont( const FontSelectPattern
&, FreetypeFontInfo
* );
123 const OString
& GetFontFileName() const;
124 int GetFontFaceIndex() const;
125 bool TestFont() const { return mbFaceOk
;}
126 FT_Face
GetFtFace() const;
127 int GetLoadFlags() const { return (mnLoadFlags
& ~FT_LOAD_IGNORE_TRANSFORM
); }
128 const FontConfigFontOptions
* GetFontOptions() const;
129 void ClearFontOptions();
130 bool NeedsArtificialBold() const { return mbArtBold
; }
131 bool NeedsArtificialItalic() const { return mbArtItalic
; }
133 const FontSelectPattern
& GetFontSelData() const { return maFontSelData
; }
135 void GetFontMetric(ImplFontMetricDataRef
&) const;
136 const unsigned char* GetTable( const char* pName
, sal_uLong
* pLength
);
137 const FontCharMapRef
GetFontCharMap() const;
138 bool GetFontCapabilities(vcl::FontCapabilities
&) const;
140 const tools::Rectangle
& GetGlyphBoundRect(const GlyphItem
& rGlyph
);
141 bool GetGlyphOutline(const GlyphItem
& rGlyph
, basegfx::B2DPolyPolygon
&) const;
142 bool GetAntialiasAdvice() const;
143 hb_font_t
* GetHbFont() { return mpHbFont
; }
144 void SetHbFont( hb_font_t
* pHbFont
) { mpHbFont
= pHbFont
; }
147 friend class GlyphCache
;
148 friend class FreetypeFontInstance
;
149 friend class X11SalGraphics
;
150 friend class CairoTextRender
;
152 void AddRef() const { ++mnRefCount
; }
153 long GetRefCount() const { return mnRefCount
; }
154 long Release() const;
155 sal_uLong
GetByteCount() const { return mnBytesUsed
; }
157 void InitGlyphData(const GlyphItem
&, GlyphData
&) const;
158 void GarbageCollect( long );
159 void ReleaseFromGarbageCollect();
161 void ApplyGlyphTransform(bool bVertical
, FT_Glyph
) const;
163 typedef std::unordered_map
<int,GlyphData
> GlyphList
;
164 mutable GlyphList maGlyphList
;
166 const FontSelectPattern maFontSelData
;
168 // used by GlyphCache for cache LRU algorithm
169 mutable long mnRefCount
;
170 mutable sal_uLong mnBytesUsed
;
172 FreetypeFont
* mpPrevGCFont
;
173 FreetypeFont
* mpNextGCFont
;
175 // 16.16 fixed point values used for a rotated font
182 FreetypeFontInfo
* mpFontInfo
;
185 FT_FaceRec_
* maFaceFT
;
186 FT_SizeRec_
* maSizeFT
;
188 mutable std::unique_ptr
<FontConfigFontOptions
> mxFontOptions
;
198 // a class for cache entries for physical font instances that are based on serverfonts
199 class VCL_DLLPUBLIC FreetypeFontInstance
: public LogicalFontInstance
202 FreetypeFontInstance( FontSelectPattern
& );
203 virtual ~FreetypeFontInstance() override
;
205 void SetFreetypeFont(FreetypeFont
* p
);
208 FreetypeFont
* mpFreetypeFont
;
211 #endif // INCLUDED_VCL_INC_GENERIC_GLYPHCACHE_HXX
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */