Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / inc / generic / glyphcache.hxx
blob7fe9e36bf063f1feca6a31f5145ac22f0019ce35
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 <basebmp/bitmapdevice.hxx>
30 #include <com/sun/star/i18n/XBreakIterator.hpp>
31 #include <tools/gen.hxx>
32 #include <vcl/dllapi.h>
33 #include <vcl/metric.hxx>
35 #include <outfont.hxx>
36 #include <sallayout.hxx>
38 #include <unordered_map>
40 class FtFontInfo;
41 class GlyphCachePeer;
42 class GlyphData;
43 class GraphiteFaceWrapper;
44 class ImplFontOptions;
45 class PhysicalFontCollection;
46 class RawBitmap;
47 class ServerFont;
48 class ServerFontLayout;
49 class ServerFontLayoutEngine;
51 namespace basegfx { class B2DPolyPolygon; }
52 namespace vcl { struct FontCapabilities; }
54 class VCL_DLLPUBLIC GlyphCache
56 public:
57 explicit GlyphCache( GlyphCachePeer& );
58 ~GlyphCache();
60 static GlyphCache& GetInstance();
62 void AddFontFile(
63 const OString& rNormalizedName,
64 int nFaceNum, sal_IntPtr nFontId,
65 const ImplDevFontAttributes&);
67 void AnnounceFonts( PhysicalFontCollection* ) const;
69 ServerFont* CacheFont( const FontSelectPattern& );
70 void UncacheFont( ServerFont& );
71 void ClearFontCache();
72 void InvalidateAllGlyphs();
74 protected:
75 GlyphCachePeer& mrPeer;
77 private:
78 friend class ServerFont;
79 // used by ServerFont class only
80 void AddedGlyph( ServerFont&, GlyphData& );
81 void RemovingGlyph( GlyphData& );
82 void UsingGlyph( ServerFont&, GlyphData& );
83 void GrowNotify();
85 private:
86 void GarbageCollect();
88 // the GlyphCache's FontList matches a font request to a serverfont instance
89 // the FontList key's mpFontData member is reinterpreted as integer font id
90 struct IFSD_Equal{ bool operator()( const FontSelectPattern&, const FontSelectPattern& ) const; };
91 struct IFSD_Hash{ size_t operator()( const FontSelectPattern& ) const; };
92 typedef std::unordered_map<FontSelectPattern,ServerFont*,IFSD_Hash,IFSD_Equal > FontList;
94 FontList maFontList;
95 sal_uLong mnMaxSize; // max overall cache size in bytes
96 mutable sal_uLong mnBytesUsed;
97 mutable long mnLruIndex;
98 mutable int mnGlyphCount;
99 ServerFont* mpCurrentGCFont;
101 class FreetypeManager* mpFtManager;
104 class GlyphMetric
106 public:
107 GlyphMetric() : mnAdvanceWidth(0) {}
109 Point GetOffset() const { return maOffset; }
110 Point GetDelta() const { return maDelta; }
111 Size GetSize() const { return maSize; }
112 long GetCharWidth() const { return mnAdvanceWidth; }
114 protected:
115 friend class GlyphData;
116 void SetOffset( int nX, int nY ) { maOffset = Point( nX, nY); }
117 void SetDelta( int nX, int nY ) { maDelta = Point( nX, nY); }
118 void SetSize( const Size& s ) { maSize = s; }
119 void SetCharWidth( long nW ) { mnAdvanceWidth = nW; }
121 private:
122 long mnAdvanceWidth;
123 Point maDelta;
124 Point maOffset;
125 Size maSize;
128 // the glyph specific data needed by a GlyphCachePeer is usually trivial,
129 // not attaching it to the corresponding GlyphData would be overkill;
130 // this is currently only used by the headless (aka svp) plugin, where meInfo is
131 // basebmp::Format and mpData is SvpGcpHelper*
132 struct ExtGlyphData
134 int meInfo;
135 void* mpData;
137 ExtGlyphData() : meInfo(0), mpData(NULL) {}
140 class GlyphData
142 public:
143 GlyphData() : mnLruValue(0) {}
145 const GlyphMetric& GetMetric() const { return maMetric; }
146 Size GetSize() const { return maMetric.GetSize(); }
148 void SetSize( const Size& s) { maMetric.SetSize( s ); }
149 void SetOffset( int nX, int nY ) { maMetric.SetOffset( nX, nY ); }
150 void SetDelta( int nX, int nY ) { maMetric.SetDelta( nX, nY ); }
151 void SetCharWidth( long nW ) { maMetric.SetCharWidth( nW ); }
153 void SetLruValue( int n ) const { mnLruValue = n; }
154 long GetLruValue() const { return mnLruValue;}
156 ExtGlyphData& ExtDataRef() { return maExtData; }
157 const ExtGlyphData& ExtDataRef() const { return maExtData; }
159 private:
160 GlyphMetric maMetric;
161 ExtGlyphData maExtData;
163 // used by GlyphCache for cache LRU algorithm
164 mutable long mnLruValue;
167 class VCL_DLLPUBLIC ServerFont
169 public:
170 ServerFont( const FontSelectPattern&, FtFontInfo* );
171 virtual ~ServerFont();
173 const OString& GetFontFileName() const;
174 bool TestFont() const { return mbFaceOk;}
175 FT_Face GetFtFace() const;
176 int GetLoadFlags() const { return (mnLoadFlags & ~FT_LOAD_IGNORE_TRANSFORM); }
177 void SetFontOptions(std::shared_ptr<ImplFontOptions>);
178 std::shared_ptr<ImplFontOptions> GetFontOptions() const;
179 bool NeedsArtificialBold() const { return mbArtBold; }
180 bool NeedsArtificialItalic() const { return mbArtItalic; }
182 const FontSelectPattern& GetFontSelData() const { return maFontSelData; }
184 void FetchFontMetric( ImplFontMetricData&, long& rFactor ) const;
185 const unsigned char* GetTable( const char* pName, sal_uLong* pLength );
186 int GetEmUnits() const { return maFaceFT->units_per_EM;}
187 const FT_Size_Metrics& GetMetricsFT() const { return maSizeFT->metrics; }
188 double GetStretch() { return mfStretch; }
189 const FontCharMapPtr GetFontCharMap() const;
190 bool GetFontCapabilities(vcl::FontCapabilities &) const;
192 GlyphData& GetGlyphData( sal_GlyphId );
193 const GlyphMetric& GetGlyphMetric( sal_GlyphId aGlyphId )
194 { return GetGlyphData( aGlyphId ).GetMetric(); }
196 #if ENABLE_GRAPHITE
197 GraphiteFaceWrapper* GetGraphiteFace() const;
198 #endif
200 sal_GlyphId GetGlyphIndex( sal_UCS4 ) const;
201 sal_GlyphId GetRawGlyphIndex( sal_UCS4, sal_UCS4 = 0 ) const;
202 sal_GlyphId FixupGlyphIndex( sal_GlyphId aGlyphId, sal_UCS4 ) const;
203 bool GetGlyphOutline( sal_GlyphId aGlyphId, ::basegfx::B2DPolyPolygon& ) const;
204 bool GetAntialiasAdvice() const;
205 bool GetGlyphBitmap1( sal_GlyphId aGlyphId, RawBitmap& ) const;
206 bool GetGlyphBitmap8( sal_GlyphId aGlyphId, RawBitmap& ) const;
208 private:
209 friend class GlyphCache;
210 friend class ServerFontLayout;
211 friend class ImplServerFontEntry;
212 friend class X11SalGraphics;
213 friend class CairoTextRender;
215 void AddRef() const { ++mnRefCount; }
216 long GetRefCount() const { return mnRefCount; }
217 long Release() const;
218 sal_uLong GetByteCount() const { return mnBytesUsed; }
220 void InitGlyphData( sal_GlyphId, GlyphData& ) const;
221 void GarbageCollect( long );
222 void ReleaseFromGarbageCollect();
224 int ApplyGlyphTransform( int nGlyphFlags, FT_GlyphRec_*, bool ) const;
225 bool ApplyGSUB( const FontSelectPattern& );
227 ServerFontLayoutEngine* GetLayoutEngine();
229 typedef std::unordered_map<int,GlyphData> GlyphList;
230 mutable GlyphList maGlyphList;
232 const FontSelectPattern maFontSelData;
234 // used by GlyphCache for cache LRU algorithm
235 mutable long mnRefCount;
236 mutable sal_uLong mnBytesUsed;
238 ServerFont* mpPrevGCFont;
239 ServerFont* mpNextGCFont;
241 // 16.16 fixed point values used for a rotated font
242 long mnCos;
243 long mnSin;
245 bool mbCollectedZW;
247 int mnWidth;
248 int mnPrioEmbedded;
249 int mnPrioAntiAlias;
250 int mnPrioAutoHint;
251 FtFontInfo* mpFontInfo;
252 FT_Int mnLoadFlags;
253 double mfStretch;
254 FT_FaceRec_* maFaceFT;
255 FT_SizeRec_* maSizeFT;
257 std::shared_ptr<ImplFontOptions> mxFontOptions;
259 bool mbFaceOk;
260 bool mbArtItalic;
261 bool mbArtBold;
262 bool mbUseGamma;
264 typedef std::unordered_map<int,int> GlyphSubstitution;
265 GlyphSubstitution maGlyphSubstitution;
267 ServerFontLayoutEngine* mpLayoutEngine;
270 // a class for cache entries for physical font instances that are based on serverfonts
271 class VCL_DLLPUBLIC ImplServerFontEntry : public ImplFontEntry
273 public:
274 ImplServerFontEntry( FontSelectPattern& );
275 virtual ~ImplServerFontEntry();
277 void SetServerFont(ServerFont* p);
278 void HandleFontOptions();
280 private:
281 ServerFont* mpServerFont;
282 std::shared_ptr<ImplFontOptions> mxFontOptions;
283 bool mbGotFontOptions;
287 class VCL_DLLPUBLIC ServerFontLayout : public GenericSalLayout
289 public:
290 ServerFontLayout( ServerFont& );
292 virtual bool LayoutText( ImplLayoutArgs& ) SAL_OVERRIDE;
293 virtual void AdjustLayout( ImplLayoutArgs& ) SAL_OVERRIDE;
294 virtual void DrawText( SalGraphics& ) const SAL_OVERRIDE;
296 void SetNeedFallback(
297 ImplLayoutArgs& rArgs,
298 sal_Int32 nIndex,
299 bool bRightToLeft);
301 ServerFont& GetServerFont() const { return mrServerFont; }
303 virtual std::shared_ptr<vcl::TextLayoutCache>
304 CreateTextLayoutCache(OUString const&) const SAL_OVERRIDE;
306 private:
307 ServerFont& mrServerFont;
308 com::sun::star::uno::Reference<com::sun::star::i18n::XBreakIterator> mxBreak;
310 ServerFontLayout( const ServerFontLayout& ) SAL_DELETED_FUNCTION;
311 ServerFontLayout& operator=( const ServerFontLayout& ) SAL_DELETED_FUNCTION;
315 class ServerFontLayoutEngine
317 public:
318 virtual ~ServerFontLayoutEngine() {}
320 virtual bool Layout(ServerFontLayout&, ImplLayoutArgs&) = 0;
323 class GlyphCachePeer
325 protected:
326 GlyphCachePeer() {}
327 virtual ~GlyphCachePeer() {}
329 public:
330 virtual void RemovingFont( ServerFont& ) {}
331 virtual void RemovingGlyph( GlyphData& ) {}
334 class VCL_DLLPUBLIC RawBitmap
336 public:
337 RawBitmap();
338 ~RawBitmap();
340 bool Rotate( int nAngle );
342 public:
343 basebmp::RawMemorySharedArray mpBits;
344 sal_uLong mnAllocated;
346 sal_uLong mnWidth;
347 sal_uLong mnHeight;
349 sal_uLong mnScanlineSize;
350 sal_uLong mnBitCount;
352 int mnXOffset;
353 int mnYOffset;
356 #endif // INCLUDED_VCL_INC_GENERIC_GLYPHCACHE_HXX
358 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */