Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / vcl / inc / unx / glyphcache.hxx
blobf605e16641f384ecd2ec6bfc094e5c526bc943bb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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
23 #include <config_graphite.h>
25 #include <ft2build.h>
26 #include FT_FREETYPE_H
27 #include FT_GLYPH_H
29 #include <com/sun/star/i18n/XBreakIterator.hpp>
30 #include <tools/gen.hxx>
31 #include <vcl/dllapi.h>
32 #include <vcl/metric.hxx>
34 #include <fontinstance.hxx>
35 #include <sallayout.hxx>
36 #include "fontattributes.hxx"
37 #include "impfontmetricdata.hxx"
39 #include <unordered_map>
41 class FreetypeManager;
42 class FreetypeFontInfo;
43 class GlyphData;
44 class GraphiteFaceWrapper;
45 class FontConfigFontOptions;
46 class PhysicalFontCollection;
47 class ServerFont;
48 class ServerFontLayout;
49 class ServerFontLayoutEngine;
50 class SvpGcpHelper;
52 namespace basegfx { class B2DPolyPolygon; }
53 namespace vcl { struct FontCapabilities; }
55 class VCL_DLLPUBLIC GlyphCache
57 public:
58 explicit GlyphCache();
59 virtual ~GlyphCache();
61 static GlyphCache& GetInstance();
63 void AddFontFile(
64 const OString& rNormalizedName,
65 int nFaceNum, sal_IntPtr nFontId,
66 const FontAttributes&);
68 void AnnounceFonts( PhysicalFontCollection* ) const;
70 ServerFont* CacheFont( const FontSelectPattern& );
71 void UncacheFont( ServerFont& );
72 void ClearFontCache();
73 void InvalidateAllGlyphs();
75 private:
76 friend class ServerFont;
77 // used by ServerFont class only
78 void AddedGlyph( ServerFont&, GlyphData& );
79 void RemovingGlyph();
80 void UsingGlyph( ServerFont&, GlyphData& );
81 void GrowNotify();
83 private:
84 void GarbageCollect();
86 // the GlyphCache's FontList matches a font request to a serverfont instance
87 // the FontList key's mpFontData member is reinterpreted as integer font id
88 struct IFSD_Equal{ bool operator()( const FontSelectPattern&, const FontSelectPattern& ) const; };
89 struct IFSD_Hash{ size_t operator()( const FontSelectPattern& ) const; };
90 typedef std::unordered_map<FontSelectPattern,ServerFont*,IFSD_Hash,IFSD_Equal > FontList;
92 FontList maFontList;
93 sal_uLong mnMaxSize; // max overall cache size in bytes
94 mutable sal_uLong mnBytesUsed;
95 mutable long mnLruIndex;
96 mutable int mnGlyphCount;
97 ServerFont* mpCurrentGCFont;
99 FreetypeManager* mpFtManager;
102 class GlyphMetric
104 public:
105 GlyphMetric() : mnAdvanceWidth(0) {}
107 const Point& GetOffset() const { return maOffset; }
108 const Size& GetSize() const { return maSize; }
109 long GetCharWidth() const { return mnAdvanceWidth; }
111 protected:
112 friend class GlyphData;
113 void SetOffset( int nX, int nY ) { maOffset = Point( nX, nY); }
114 void SetDelta( int nX, int nY ) { maDelta = Point( nX, nY); }
115 void SetSize( const Size& s ) { maSize = s; }
116 void SetCharWidth( long nW ) { mnAdvanceWidth = nW; }
118 private:
119 long mnAdvanceWidth;
120 Point maDelta;
121 Point maOffset;
122 Size maSize;
125 class GlyphData
127 public:
128 GlyphData() : mnLruValue(0) {}
130 const GlyphMetric& GetMetric() const { return maGlyphMetric; }
132 void SetSize( const Size& s) { maGlyphMetric.SetSize( s ); }
133 void SetOffset( int nX, int nY ) { maGlyphMetric.SetOffset( nX, nY ); }
134 void SetDelta( int nX, int nY ) { maGlyphMetric.SetDelta( nX, nY ); }
135 void SetCharWidth( long nW ) { maGlyphMetric.SetCharWidth( nW ); }
137 void SetLruValue( int n ) const { mnLruValue = n; }
138 long GetLruValue() const { return mnLruValue;}
140 private:
141 GlyphMetric maGlyphMetric;
143 // used by GlyphCache for cache LRU algorithm
144 mutable long mnLruValue;
147 class VCL_DLLPUBLIC ServerFont
149 public:
150 ServerFont( const FontSelectPattern&, FreetypeFontInfo* );
151 virtual ~ServerFont();
153 const OString& GetFontFileName() const;
154 int GetFontFaceIndex() const;
155 bool TestFont() const { return mbFaceOk;}
156 FT_Face GetFtFace() const;
157 int GetLoadFlags() const { return (mnLoadFlags & ~FT_LOAD_IGNORE_TRANSFORM); }
158 void SetFontOptions(const std::shared_ptr<FontConfigFontOptions>&);
159 const std::shared_ptr<FontConfigFontOptions>& GetFontOptions() const;
160 bool NeedsArtificialBold() const { return mbArtBold; }
161 bool NeedsArtificialItalic() const { return mbArtItalic; }
163 const FontSelectPattern& GetFontSelData() const { return maFontSelData; }
165 void GetFontMetric( ImplFontMetricDataPtr&, long& rFactor ) const;
166 const unsigned char* GetTable( const char* pName, sal_uLong* pLength );
167 int GetEmUnits() const { return maFaceFT->units_per_EM;}
168 double GetStretch() { return mfStretch; }
169 const FontCharMapPtr GetFontCharMap() const;
170 bool GetFontCapabilities(vcl::FontCapabilities &) const;
172 GlyphData& GetGlyphData( sal_GlyphId );
173 const GlyphMetric& GetGlyphMetric( sal_GlyphId aGlyphId )
174 { return GetGlyphData( aGlyphId ).GetMetric(); }
176 #if ENABLE_GRAPHITE
177 GraphiteFaceWrapper* GetGraphiteFace() const;
178 #endif
180 sal_GlyphId GetGlyphIndex( sal_UCS4 ) const;
181 sal_GlyphId GetRawGlyphIndex( sal_UCS4, sal_UCS4 = 0 ) const;
182 sal_GlyphId FixupGlyphIndex( sal_GlyphId aGlyphId, sal_UCS4 ) const;
183 bool GetGlyphOutline( sal_GlyphId aGlyphId, basegfx::B2DPolyPolygon& ) const;
184 bool GetAntialiasAdvice() const;
186 private:
187 friend class GlyphCache;
188 friend class ServerFontLayout;
189 friend class ServerFontInstance;
190 friend class X11SalGraphics;
191 friend class CairoTextRender;
193 void AddRef() const { ++mnRefCount; }
194 long GetRefCount() const { return mnRefCount; }
195 long Release() const;
196 sal_uLong GetByteCount() const { return mnBytesUsed; }
198 void InitGlyphData( sal_GlyphId, GlyphData& ) const;
199 void GarbageCollect( long );
200 void ReleaseFromGarbageCollect();
202 void ApplyGlyphTransform( int nGlyphFlags, FT_GlyphRec_*, bool ) const;
203 void ApplyGSUB( const FontSelectPattern& );
205 ServerFontLayoutEngine* GetLayoutEngine();
207 typedef std::unordered_map<int,GlyphData> GlyphList;
208 mutable GlyphList maGlyphList;
210 const FontSelectPattern maFontSelData;
212 // used by GlyphCache for cache LRU algorithm
213 mutable long mnRefCount;
214 mutable sal_uLong mnBytesUsed;
216 ServerFont* mpPrevGCFont;
217 ServerFont* mpNextGCFont;
219 // 16.16 fixed point values used for a rotated font
220 long mnCos;
221 long mnSin;
223 int mnWidth;
224 int mnPrioEmbedded;
225 int mnPrioAntiAlias;
226 int mnPrioAutoHint;
227 FreetypeFontInfo* mpFontInfo;
228 FT_Int mnLoadFlags;
229 double mfStretch;
230 FT_FaceRec_* maFaceFT;
231 FT_SizeRec_* maSizeFT;
233 mutable std::shared_ptr<FontConfigFontOptions> mxFontOptions;
235 bool mbFaceOk;
236 bool mbArtItalic;
237 bool mbArtBold;
238 bool mbUseGamma;
240 typedef std::unordered_map<int,int> GlyphSubstitution;
241 GlyphSubstitution maGlyphSubstitution;
243 ServerFontLayoutEngine* mpLayoutEngine;
246 // a class for cache entries for physical font instances that are based on serverfonts
247 class VCL_DLLPUBLIC ServerFontInstance : public LogicalFontInstance
249 public:
250 ServerFontInstance( FontSelectPattern& );
251 virtual ~ServerFontInstance();
253 void SetServerFont(ServerFont* p);
254 void HandleFontOptions();
256 private:
257 ServerFont* mpServerFont;
258 std::shared_ptr<FontConfigFontOptions> mxFontOptions;
259 bool mbGotFontOptions;
263 class VCL_DLLPUBLIC ServerFontLayout : public GenericSalLayout
265 public:
266 ServerFontLayout( ServerFont& );
268 virtual bool LayoutText( ImplLayoutArgs& ) override;
269 virtual void AdjustLayout( ImplLayoutArgs& ) override;
270 virtual void DrawText( SalGraphics& ) const override;
272 void SetNeedFallback(
273 ImplLayoutArgs& rArgs,
274 sal_Int32 nIndex,
275 bool bRightToLeft);
277 ServerFont& GetServerFont() const { return mrServerFont; }
279 virtual std::shared_ptr<vcl::TextLayoutCache>
280 CreateTextLayoutCache(OUString const&) const override;
282 private:
283 ServerFont& mrServerFont;
284 css::uno::Reference<css::i18n::XBreakIterator> mxBreak;
286 ServerFontLayout( const ServerFontLayout& ) = delete;
287 ServerFontLayout& operator=( const ServerFontLayout& ) = delete;
291 class ServerFontLayoutEngine
293 public:
294 virtual ~ServerFontLayoutEngine() {}
296 virtual bool Layout(ServerFontLayout&, ImplLayoutArgs&) = 0;
299 #endif // INCLUDED_VCL_INC_GENERIC_GLYPHCACHE_HXX
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */