Line data Source code
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 <config_graphite.h>
24 :
25 : #include <ft2build.h>
26 : #include FT_FREETYPE_H
27 : #include FT_GLYPH_H
28 :
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>
34 :
35 : #include <outfont.hxx>
36 : #include <sallayout.hxx>
37 :
38 : #include <unordered_map>
39 :
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;
50 : class SvpGcpHelper;
51 :
52 : namespace basegfx { class B2DPolyPolygon; }
53 : namespace vcl { struct FontCapabilities; }
54 :
55 : class VCL_DLLPUBLIC GlyphCache
56 : {
57 : public:
58 : explicit GlyphCache( GlyphCachePeer& );
59 : ~GlyphCache();
60 :
61 : static GlyphCache& GetInstance();
62 :
63 : void AddFontFile(
64 : const OString& rNormalizedName,
65 : int nFaceNum, sal_IntPtr nFontId,
66 : const ImplDevFontAttributes&);
67 :
68 : void AnnounceFonts( PhysicalFontCollection* ) const;
69 :
70 : ServerFont* CacheFont( const FontSelectPattern& );
71 : void UncacheFont( ServerFont& );
72 : void ClearFontCache();
73 : void InvalidateAllGlyphs();
74 :
75 : protected:
76 : GlyphCachePeer& mrPeer;
77 :
78 : private:
79 : friend class ServerFont;
80 : // used by ServerFont class only
81 : void AddedGlyph( ServerFont&, GlyphData& );
82 : void RemovingGlyph( GlyphData& );
83 : void UsingGlyph( ServerFont&, GlyphData& );
84 : void GrowNotify();
85 :
86 : private:
87 : void GarbageCollect();
88 :
89 : // the GlyphCache's FontList matches a font request to a serverfont instance
90 : // the FontList key's mpFontData member is reinterpreted as integer font id
91 : struct IFSD_Equal{ bool operator()( const FontSelectPattern&, const FontSelectPattern& ) const; };
92 : struct IFSD_Hash{ size_t operator()( const FontSelectPattern& ) const; };
93 : typedef std::unordered_map<FontSelectPattern,ServerFont*,IFSD_Hash,IFSD_Equal > FontList;
94 :
95 : FontList maFontList;
96 : sal_uLong mnMaxSize; // max overall cache size in bytes
97 : mutable sal_uLong mnBytesUsed;
98 : mutable long mnLruIndex;
99 : mutable int mnGlyphCount;
100 : ServerFont* mpCurrentGCFont;
101 :
102 : class FreetypeManager* mpFtManager;
103 : };
104 :
105 : class GlyphMetric
106 : {
107 : public:
108 70498 : GlyphMetric() : mnAdvanceWidth(0) {}
109 :
110 1280321 : Point GetOffset() const { return maOffset; }
111 : Point GetDelta() const { return maDelta; }
112 1280321 : Size GetSize() const { return maSize; }
113 24818561 : long GetCharWidth() const { return mnAdvanceWidth; }
114 :
115 : protected:
116 : friend class GlyphData;
117 70498 : void SetOffset( int nX, int nY ) { maOffset = Point( nX, nY); }
118 70498 : void SetDelta( int nX, int nY ) { maDelta = Point( nX, nY); }
119 70498 : void SetSize( const Size& s ) { maSize = s; }
120 70498 : void SetCharWidth( long nW ) { mnAdvanceWidth = nW; }
121 :
122 : private:
123 : long mnAdvanceWidth;
124 : Point maDelta;
125 : Point maOffset;
126 : Size maSize;
127 : };
128 :
129 : // the glyph specific data needed by a GlyphCachePeer is usually trivial,
130 : // not attaching it to the corresponding GlyphData would be overkill;
131 : // this is currently only used by the headless (aka svp) plugin, where meInfo is
132 : // basebmp::Format and mpData is SvpGcpHelper*
133 : struct ExtGlyphData
134 : {
135 : basebmp::Format meInfo;
136 : SvpGcpHelper* mpData;
137 :
138 70498 : ExtGlyphData() : meInfo(basebmp::Format::NONE), mpData(NULL) {}
139 : };
140 :
141 : class GlyphData
142 : {
143 : public:
144 70498 : GlyphData() : mnLruValue(0) {}
145 :
146 26098882 : const GlyphMetric& GetMetric() const { return maMetric; }
147 : Size GetSize() const { return maMetric.GetSize(); }
148 :
149 70498 : void SetSize( const Size& s) { maMetric.SetSize( s ); }
150 70498 : void SetOffset( int nX, int nY ) { maMetric.SetOffset( nX, nY ); }
151 70498 : void SetDelta( int nX, int nY ) { maMetric.SetDelta( nX, nY ); }
152 70498 : void SetCharWidth( long nW ) { maMetric.SetCharWidth( nW ); }
153 :
154 27709168 : void SetLruValue( int n ) const { mnLruValue = n; }
155 0 : long GetLruValue() const { return mnLruValue;}
156 :
157 3264444 : ExtGlyphData& ExtDataRef() { return maExtData; }
158 : const ExtGlyphData& ExtDataRef() const { return maExtData; }
159 :
160 : private:
161 : GlyphMetric maMetric;
162 : ExtGlyphData maExtData;
163 :
164 : // used by GlyphCache for cache LRU algorithm
165 : mutable long mnLruValue;
166 : };
167 :
168 : class VCL_DLLPUBLIC ServerFont
169 : {
170 : public:
171 : ServerFont( const FontSelectPattern&, FtFontInfo* );
172 : virtual ~ServerFont();
173 :
174 : const OString& GetFontFileName() const;
175 285773 : bool TestFont() const { return mbFaceOk;}
176 : FT_Face GetFtFace() const;
177 0 : int GetLoadFlags() const { return (mnLoadFlags & ~FT_LOAD_IGNORE_TRANSFORM); }
178 : void SetFontOptions(std::shared_ptr<ImplFontOptions>);
179 : std::shared_ptr<ImplFontOptions> GetFontOptions() const;
180 3 : bool NeedsArtificialBold() const { return mbArtBold; }
181 3 : bool NeedsArtificialItalic() const { return mbArtItalic; }
182 :
183 24929848 : const FontSelectPattern& GetFontSelData() const { return maFontSelData; }
184 :
185 : void FetchFontMetric( ImplFontMetricData&, long& rFactor ) const;
186 : const unsigned char* GetTable( const char* pName, sal_uLong* pLength );
187 4725 : int GetEmUnits() const { return maFaceFT->units_per_EM;}
188 : const FT_Size_Metrics& GetMetricsFT() const { return maSizeFT->metrics; }
189 : const FontCharMapPtr GetFontCharMap() const;
190 : bool GetFontCapabilities(vcl::FontCapabilities &) const;
191 :
192 : GlyphData& GetGlyphData( sal_GlyphId );
193 26097139 : const GlyphMetric& GetGlyphMetric( sal_GlyphId aGlyphId )
194 26097139 : { return GetGlyphData( aGlyphId ).GetMetric(); }
195 :
196 : #if ENABLE_GRAPHITE
197 : GraphiteFaceWrapper* GetGraphiteFace() const;
198 : #endif
199 :
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;
207 :
208 : private:
209 : friend class GlyphCache;
210 : friend class ServerFontLayout;
211 : friend class ImplServerFontEntry;
212 : friend class X11SalGraphics;
213 : friend class CairoTextRender;
214 :
215 285773 : void AddRef() const { ++mnRefCount; }
216 0 : long GetRefCount() const { return mnRefCount; }
217 : long Release() const;
218 5931 : sal_uLong GetByteCount() const { return mnBytesUsed; }
219 :
220 : void InitGlyphData( sal_GlyphId, GlyphData& ) const;
221 : void GarbageCollect( long );
222 : void ReleaseFromGarbageCollect();
223 :
224 : int ApplyGlyphTransform( int nGlyphFlags, FT_GlyphRec_*, bool ) const;
225 : bool ApplyGSUB( const FontSelectPattern& );
226 :
227 : ServerFontLayoutEngine* GetLayoutEngine();
228 :
229 : typedef std::unordered_map<int,GlyphData> GlyphList;
230 : mutable GlyphList maGlyphList;
231 :
232 : const FontSelectPattern maFontSelData;
233 :
234 : // used by GlyphCache for cache LRU algorithm
235 : mutable long mnRefCount;
236 : mutable sal_uLong mnBytesUsed;
237 :
238 : ServerFont* mpPrevGCFont;
239 : ServerFont* mpNextGCFont;
240 :
241 : // 16.16 fixed point values used for a rotated font
242 : long mnCos;
243 : long mnSin;
244 :
245 : bool mbCollectedZW;
246 :
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;
256 :
257 : std::shared_ptr<ImplFontOptions> mxFontOptions;
258 :
259 : bool mbFaceOk;
260 : bool mbArtItalic;
261 : bool mbArtBold;
262 : bool mbUseGamma;
263 :
264 : typedef std::unordered_map<int,int> GlyphSubstitution;
265 : GlyphSubstitution maGlyphSubstitution;
266 :
267 : ServerFontLayoutEngine* mpLayoutEngine;
268 : };
269 :
270 : // a class for cache entries for physical font instances that are based on serverfonts
271 : class VCL_DLLPUBLIC ImplServerFontEntry : public ImplFontEntry
272 : {
273 : public:
274 : ImplServerFontEntry( FontSelectPattern& );
275 : virtual ~ImplServerFontEntry();
276 :
277 : void SetServerFont(ServerFont* p);
278 : void HandleFontOptions();
279 :
280 : private:
281 : ServerFont* mpServerFont;
282 : std::shared_ptr<ImplFontOptions> mxFontOptions;
283 : bool mbGotFontOptions;
284 :
285 : };
286 :
287 3169363 : class VCL_DLLPUBLIC ServerFontLayout : public GenericSalLayout
288 : {
289 : public:
290 : ServerFontLayout( ServerFont& );
291 :
292 : virtual bool LayoutText( ImplLayoutArgs& ) SAL_OVERRIDE;
293 : virtual void AdjustLayout( ImplLayoutArgs& ) SAL_OVERRIDE;
294 : virtual void DrawText( SalGraphics& ) const SAL_OVERRIDE;
295 :
296 : void SetNeedFallback(
297 : ImplLayoutArgs& rArgs,
298 : sal_Int32 nIndex,
299 : bool bRightToLeft);
300 :
301 1531568 : ServerFont& GetServerFont() const { return mrServerFont; }
302 :
303 : virtual std::shared_ptr<vcl::TextLayoutCache>
304 : CreateTextLayoutCache(OUString const&) const SAL_OVERRIDE;
305 :
306 : private:
307 : ServerFont& mrServerFont;
308 : com::sun::star::uno::Reference<com::sun::star::i18n::XBreakIterator> mxBreak;
309 :
310 : ServerFontLayout( const ServerFontLayout& ) SAL_DELETED_FUNCTION;
311 : ServerFontLayout& operator=( const ServerFontLayout& ) SAL_DELETED_FUNCTION;
312 :
313 : };
314 :
315 4725 : class ServerFontLayoutEngine
316 : {
317 : public:
318 4725 : virtual ~ServerFontLayoutEngine() {}
319 :
320 : virtual bool Layout(ServerFontLayout&, ImplLayoutArgs&) = 0;
321 : };
322 :
323 : class GlyphCachePeer
324 : {
325 : protected:
326 204 : GlyphCachePeer() {}
327 204 : virtual ~GlyphCachePeer() {}
328 :
329 : public:
330 37 : virtual void RemovingFont( ServerFont& ) {}
331 0 : virtual void RemovingGlyph( GlyphData& ) {}
332 : };
333 :
334 : class VCL_DLLPUBLIC RawBitmap
335 : {
336 : public:
337 : RawBitmap();
338 : ~RawBitmap();
339 :
340 : bool Rotate( int nAngle );
341 :
342 : public:
343 : basebmp::RawMemorySharedArray mpBits;
344 : sal_uLong mnAllocated;
345 :
346 : sal_uLong mnWidth;
347 : sal_uLong mnHeight;
348 :
349 : sal_uLong mnScanlineSize;
350 : sal_uLong mnBitCount;
351 :
352 : int mnXOffset;
353 : int mnYOffset;
354 : };
355 :
356 : #endif // INCLUDED_VCL_INC_GENERIC_GLYPHCACHE_HXX
357 :
358 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|