xref: /aoo41x/main/vcl/inc/glyphcache.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _SV_GLYPHCACHE_HXX
29 #define _SV_GLYPHCACHE_HXX
30 
31 #include <vcl/dllapi.h>
32 
33 class GlyphCache;
34 class GlyphMetric;
35 class GlyphData;
36 class ServerFont;
37 class GlyphCachePeer;
38 class ServerFontLayoutEngine;
39 class ServerFontLayout;
40 class ExtraKernInfo;
41 struct ImplKernPairData;
42 class ImplFontOptions;
43 
44 #include <tools/gen.hxx>
45 #include <hash_map>
46 #include <hash_set>
47 
48 namespace basegfx { class B2DPolyPolygon; }
49 
50 class RawBitmap;
51 
52 #include <outfont.hxx>
53 #include <impfont.hxx>
54 
55 class ServerFontLayout;
56 #include <sallayout.hxx>
57 
58 // =======================================================================
59 
60 class VCL_PLUGIN_PUBLIC GlyphCache
61 {
62 public:
63     explicit                    GlyphCache( GlyphCachePeer& );
64     /*virtual*/                 ~GlyphCache();
65 
66     static GlyphCache&		GetInstance();
67     void                        LoadFonts();
68 
69     void                        ClearFontPath();
70     void                        AddFontPath( const String& rFontPath );
71     void                        AddFontFile( const rtl::OString& rNormalizedName,
72                                     int nFaceNum, sal_IntPtr nFontId, const ImplDevFontAttributes&,
73                                     const ExtraKernInfo* = NULL );
74     void                        AnnounceFonts( ImplDevFontList* ) const;
75 
76     ServerFont*                 CacheFont( const ImplFontSelectData& );
77     void                        UncacheFont( ServerFont& );
78     void                        InvalidateAllGlyphs();
79 
80 protected:
81     GlyphCachePeer&             mrPeer;
82 
83 private:
84     friend class ServerFont;
85     // used by ServerFont class only
86     void                        AddedGlyph( ServerFont&, GlyphData& );
87     void                        RemovingGlyph( ServerFont&, GlyphData&, int nGlyphIndex );
88     void                        UsingGlyph( ServerFont&, GlyphData& );
89     void                        GrowNotify();
90 
91 private:
92     sal_uLong                       CalcByteCount() const;
93     void                        GarbageCollect();
94 
95     // the GlyphCache's FontList matches a font request to a serverfont instance
96     // the FontList key's mpFontData member is reinterpreted as integer font id
97     struct IFSD_Equal{  bool operator()( const ImplFontSelectData&, const ImplFontSelectData& ) const; };
98     struct IFSD_Hash{ size_t operator()( const ImplFontSelectData& ) const; };
99     typedef ::std::hash_map<ImplFontSelectData,ServerFont*,IFSD_Hash,IFSD_Equal > FontList;
100     FontList                    maFontList;
101     sal_uLong                       mnMaxSize;      // max overall cache size in bytes
102     mutable sal_uLong               mnBytesUsed;
103     mutable long                mnLruIndex;
104     mutable int                 mnGlyphCount;
105     ServerFont*                 mpCurrentGCFont;
106 
107     class FreetypeManager*      mpFtManager;
108 };
109 
110 // =======================================================================
111 
112 class GlyphMetric
113 {
114 public:
115     Point                   GetOffset() const   { return maOffset; }
116     Point                   GetDelta() const    { return maDelta; }
117     Size                    GetSize() const     { return maSize; }
118     long                    GetCharWidth() const { return mnAdvanceWidth; }
119 
120 protected:
121     friend class GlyphData;
122     void                    SetOffset( int nX, int nY ) { maOffset = Point( nX, nY); }
123     void                    SetDelta( int nX, int nY )  { maDelta = Point( nX, nY); }
124     void                    SetSize( const Size& s )    { maSize = s; }
125     void                    SetCharWidth( long nW )     { mnAdvanceWidth = nW; }
126 
127 private:
128     long                    mnAdvanceWidth;
129     Point                   maDelta;
130     Point                   maOffset;
131     Size                    maSize;
132 };
133 
134 // -----------------------------------------------------------------------
135 
136 // the glyph specific data needed by a GlyphCachePeer is usually trivial,
137 // not attaching it to the corresponding GlyphData would be overkill
138 struct ExtGlyphData
139 {
140     int     meInfo;
141     void*   mpData;
142 
143     ExtGlyphData() : meInfo(0), mpData(NULL) {}
144 };
145 
146 // -----------------------------------------------------------------------
147 
148 class GlyphData
149 {
150 public:
151     const GlyphMetric&      GetMetric() const           { return maMetric; }
152     Size                    GetSize() const             { return maMetric.GetSize(); }
153 
154     void                    SetSize( const Size& s)     { maMetric.SetSize( s ); }
155     void                    SetOffset( int nX, int nY ) { maMetric.SetOffset( nX, nY ); }
156     void                    SetDelta( int nX, int nY )  { maMetric.SetDelta( nX, nY ); }
157     void                    SetCharWidth( long nW )     { maMetric.SetCharWidth( nW ); }
158 
159     void                    SetLruValue( int n ) const  { mnLruValue = n; }
160     long                    GetLruValue() const         { return mnLruValue;}
161 
162     ExtGlyphData&           ExtDataRef()                { return maExtData; }
163     const ExtGlyphData&     ExtDataRef() const          { return maExtData; }
164 
165 private:
166     GlyphMetric             maMetric;
167     ExtGlyphData            maExtData;
168 
169     // used by GlyphCache for cache LRU algorithm
170     mutable long            mnLruValue;
171 };
172 
173 // =======================================================================
174 
175 class VCL_PLUGIN_PUBLIC ServerFont
176 {
177 public:
178     virtual const ::rtl::OString*   GetFontFileName() const { return NULL; }
179     virtual int                 GetFontFaceNumber() const   { return 0; }
180     virtual bool                TestFont() const            { return true; }
181     virtual void*               GetFtFace() const { return 0; }
182     virtual int                 GetLoadFlags() const { return 0; }
183     virtual void                SetFontOptions( const ImplFontOptions&) {}
184     virtual bool                NeedsArtificialBold() const { return false; }
185     virtual bool                NeedsArtificialItalic() const { return false; }
186 
187     const ImplFontSelectData&   GetFontSelData() const      { return maFontSelData; }
188 
189     virtual void                FetchFontMetric( ImplFontMetricData&, long& rFactor ) const = 0;
190     virtual sal_uLong               GetKernPairs( ImplKernPairData** ) const      { return 0; }
191     virtual int                 GetGlyphKernValue( int, int ) const           { return 0; }
192     virtual const ImplFontCharMap* GetImplFontCharMap() const = 0;
193     Point                       TransformPoint( const Point& ) const;
194 
195     GlyphData&                  GetGlyphData( int nGlyphIndex );
196     const GlyphMetric&          GetGlyphMetric( int nGlyphIndex )
197                                 { return GetGlyphData( nGlyphIndex ).GetMetric(); }
198 
199     virtual int                 GetGlyphIndex( sal_UCS4 ) const = 0;
200     virtual bool                GetGlyphOutline( int nGlyphIndex, ::basegfx::B2DPolyPolygon& ) const = 0;
201     virtual bool                GetAntialiasAdvice( void ) const = 0;
202     bool                        IsGlyphInvisible( int nGlyphIndex );
203     virtual bool                GetGlyphBitmap1( int nGlyphIndex, RawBitmap& ) const = 0;
204     virtual bool                GetGlyphBitmap8( int nGlyphIndex, RawBitmap& ) const = 0;
205 
206     void                        SetExtended( int nInfo, void* ppVoid );
207     int                         GetExtInfo() { return mnExtInfo; }
208     void*                       GetExtPointer() { return mpExtData; }
209 
210 protected:
211     friend class GlyphCache;
212     friend class ServerFontLayout;
213     explicit                    ServerFont( const ImplFontSelectData& );
214     virtual                     ~ServerFont();
215 
216     void                        AddRef() const      { ++mnRefCount; }
217     long                        GetRefCount() const { return mnRefCount; }
218     long                        Release() const;
219     sal_uLong                       GetByteCount() const { return mnBytesUsed; }
220 
221     virtual void                InitGlyphData( int nGlyphIndex, GlyphData& ) const = 0;
222     virtual void                GarbageCollect( long );
223     void                        ReleaseFromGarbageCollect();
224 
225     virtual ServerFontLayoutEngine* GetLayoutEngine() { return NULL; }
226 
227 private:
228     typedef ::std::hash_map<int,GlyphData> GlyphList;
229     mutable GlyphList           maGlyphList;
230 
231     const ImplFontSelectData    maFontSelData;
232 
233     // info for GlyphcachePeer
234     int                         mnExtInfo;
235     void*                       mpExtData;
236 
237     // used by GlyphCache for cache LRU algorithm
238     mutable long                mnRefCount;
239     mutable sal_uLong               mnBytesUsed;
240 
241     ServerFont*                 mpPrevGCFont;
242     ServerFont*                 mpNextGCFont;
243 
244 protected:
245     // 16.16 fixed point values used for a rotated font
246     long                        mnCos;
247     long                        mnSin;
248 private:
249     int                         mnZWJ;
250     int                         mnZWNJ;
251     bool                        mbCollectedZW;
252 };
253 
254 // =======================================================================
255 
256 // a class for cache entries for physical font instances that are based on serverfonts
257 class VCL_PLUGIN_PUBLIC ImplServerFontEntry : public ImplFontEntry
258 {
259 private:
260     ServerFont*    mpServerFont;
261     ImplFontOptions maFontOptions;
262     bool           mbGotFontOptions;
263     bool           mbValidFontOptions;
264 
265 public:
266                    ImplServerFontEntry( ImplFontSelectData& );
267     virtual        ~ImplServerFontEntry();
268     void           SetServerFont( ServerFont* p) { mpServerFont = p; }
269     void           HandleFontOptions();
270 };
271 
272 // =======================================================================
273 
274 class VCL_PLUGIN_PUBLIC ServerFontLayout : public GenericSalLayout
275 {
276 private:
277     ServerFont&     mrServerFont;
278 
279     // enforce proper copy semantic
280     SAL_DLLPRIVATE  ServerFontLayout( const ServerFontLayout& );
281     SAL_DLLPRIVATE  ServerFontLayout& operator=( const ServerFontLayout& );
282 
283 public:
284                     ServerFontLayout( ServerFont& );
285     virtual bool    LayoutText( ImplLayoutArgs& );
286     virtual void    AdjustLayout( ImplLayoutArgs& );
287     virtual void    DrawText( SalGraphics& ) const;
288     ServerFont&     GetServerFont() const   { return mrServerFont; }
289 };
290 
291 // =======================================================================
292 
293 class ServerFontLayoutEngine
294 {
295 public:
296     virtual         ~ServerFontLayoutEngine() {}
297     virtual bool    operator()( ServerFontLayout&, ImplLayoutArgs& );
298 };
299 
300 // =======================================================================
301 
302 class GlyphCachePeer
303 {
304 protected:
305                     GlyphCachePeer() : mnBytesUsed(0) {}
306     virtual         ~GlyphCachePeer() {}
307 
308 public:
309     sal_Int32       GetByteCount() const { return mnBytesUsed; }
310     virtual void    RemovingFont( ServerFont& ) {}
311     virtual void    RemovingGlyph( ServerFont&, GlyphData&, int ) {}
312 
313 protected:
314     sal_Int32       mnBytesUsed;
315 };
316 
317 // =======================================================================
318 
319 class VCL_PLUGIN_PUBLIC RawBitmap
320 {
321 public:
322                     RawBitmap();
323                     ~RawBitmap();
324     bool            Rotate( int nAngle );
325 
326 public:
327     unsigned char*  mpBits;
328     sal_uLong           mnAllocated;
329 
330     sal_uLong           mnWidth;
331     sal_uLong           mnHeight;
332 
333     sal_uLong           mnScanlineSize;
334     sal_uLong           mnBitCount;
335 
336     int             mnXOffset;
337     int             mnYOffset;
338 };
339 
340 // =======================================================================
341 
342 inline void ServerFont::SetExtended( int nInfo, void* pVoid )
343 {
344     mnExtInfo = nInfo;
345     mpExtData = pVoid;
346 }
347 
348 // =======================================================================
349 
350 // ExtraKernInfo allows an on-demand query of extra kerning info #i29881#
351 // The kerning values have to be scaled to match the font size before use
352 class VCL_PLUGIN_PUBLIC ExtraKernInfo
353 {
354 public:
355     ExtraKernInfo( sal_IntPtr nFontId );
356     virtual ~ExtraKernInfo() {}
357 
358     bool    HasKernPairs() const;
359     int     GetUnscaledKernPairs( ImplKernPairData** ) const;
360     int     GetUnscaledKernValue( sal_Unicode cLeft, sal_Unicode cRight ) const;
361 
362 protected:
363     mutable bool mbInitialized;
364     virtual void Initialize() const = 0;
365 
366 protected:
367     sal_IntPtr     mnFontId;
368 
369     // container to map a unicode pair to an unscaled kerning value
370     struct PairEqual{ int operator()(const ImplKernPairData& rA, const ImplKernPairData& rB) const
371                           { return (rA.mnChar1 == rB.mnChar1) && (rA.mnChar2 == rB.mnChar2); } };
372     struct PairHash{ int operator()(const ImplKernPairData& rA) const
373                          { return (rA.mnChar1) * 256 ^ rA.mnChar2; } };
374     typedef std::hash_set< ImplKernPairData, PairHash, PairEqual > UnicodeKernPairs;
375     mutable UnicodeKernPairs maUnicodeKernPairs;
376 };
377 
378 // =======================================================================
379 
380 #endif // _SV_GLYPHCACHE_HXX
381