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