xref: /aoo41x/main/vcl/source/glyphs/gcach_ftyp.hxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef _SV_GCACHFTYP_HXX
29*cdf0e10cSrcweir #define _SV_GCACHFTYP_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <glyphcache.hxx>
32*cdf0e10cSrcweir #include <rtl/textcvt.h>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <ft2build.h>
35*cdf0e10cSrcweir #include FT_FREETYPE_H
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir class FreetypeServerFont;
38*cdf0e10cSrcweir struct FT_GlyphRec_;
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir // -----------------------------------------------------------------------
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir // FtFontFile has the responsibility that a font file is only mapped once.
43*cdf0e10cSrcweir // (#86621#) the old directly ft-managed solution caused it to be mapped
44*cdf0e10cSrcweir // in up to nTTC*nSizes*nOrientation*nSynthetic times
45*cdf0e10cSrcweir class FtFontFile
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir public:
48*cdf0e10cSrcweir     static FtFontFile*      FindFontFile( const ::rtl::OString& rNativeFileName );
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir     bool                    Map();
51*cdf0e10cSrcweir     void                    Unmap();
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir     const unsigned char*    GetBuffer() const { return mpFileMap; }
54*cdf0e10cSrcweir     int                     GetFileSize() const { return mnFileSize; }
55*cdf0e10cSrcweir     const ::rtl::OString*   GetFileName() const { return &maNativeFileName; }
56*cdf0e10cSrcweir     int                     GetLangBoost() const { return mnLangBoost; }
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir private:
59*cdf0e10cSrcweir                             FtFontFile( const ::rtl::OString& rNativeFileName );
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir     const ::rtl::OString    maNativeFileName;
62*cdf0e10cSrcweir     const unsigned char*    mpFileMap;
63*cdf0e10cSrcweir     int                     mnFileSize;
64*cdf0e10cSrcweir     int                     mnRefCount;
65*cdf0e10cSrcweir     int                     mnLangBoost;
66*cdf0e10cSrcweir };
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir // -----------------------------------------------------------------------
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir // FtFontInfo corresponds to an unscaled font face
71*cdf0e10cSrcweir class FtFontInfo
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir public:
74*cdf0e10cSrcweir                            FtFontInfo( const ImplDevFontAttributes&,
75*cdf0e10cSrcweir                                const ::rtl::OString& rNativeFileName,
76*cdf0e10cSrcweir                                int nFaceNum, sal_IntPtr nFontId, int nSynthetic,
77*cdf0e10cSrcweir                                 const ExtraKernInfo* );
78*cdf0e10cSrcweir                           ~FtFontInfo();
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     const unsigned char*  GetTable( const char*, sal_uLong* pLength=0 ) const;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     FT_FaceRec_*          GetFaceFT();
83*cdf0e10cSrcweir     void                  ReleaseFaceFT( FT_FaceRec_* );
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     const ::rtl::OString* GetFontFileName() const   { return mpFontFile->GetFileName(); }
86*cdf0e10cSrcweir     int                   GetFaceNum() const        { return mnFaceNum; }
87*cdf0e10cSrcweir     int                   GetSynthetic() const      { return mnSynthetic; }
88*cdf0e10cSrcweir     sal_IntPtr            GetFontId() const         { return mnFontId; }
89*cdf0e10cSrcweir     bool                  IsSymbolFont() const      { return maDevFontAttributes.IsSymbolFont(); }
90*cdf0e10cSrcweir     const ImplFontAttributes& GetFontAttributes() const { return maDevFontAttributes; }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     void                  AnnounceFont( ImplDevFontList* );
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir     int                   GetGlyphIndex( sal_UCS4 cChar ) const;
95*cdf0e10cSrcweir     void                  CacheGlyphIndex( sal_UCS4 cChar, int nGI ) const;
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir     bool                  GetFontCodeRanges( CmapResult& ) const;
98*cdf0e10cSrcweir     const ImplFontCharMap* GetImplFontCharMap( void );
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir     bool                  HasExtraKerning() const;
101*cdf0e10cSrcweir     int                   GetExtraKernPairs( ImplKernPairData** ) const;
102*cdf0e10cSrcweir     int                   GetExtraGlyphKernValue( int nLeftGlyph, int nRightGlyph ) const;
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir private:
105*cdf0e10cSrcweir     FT_FaceRec_*    maFaceFT;
106*cdf0e10cSrcweir     FtFontFile*     mpFontFile;
107*cdf0e10cSrcweir     const int       mnFaceNum;
108*cdf0e10cSrcweir     int             mnRefCount;
109*cdf0e10cSrcweir     const int       mnSynthetic;
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     sal_IntPtr      mnFontId;
112*cdf0e10cSrcweir     ImplDevFontAttributes maDevFontAttributes;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     const ImplFontCharMap* mpFontCharMap;
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     // cache unicode->glyphid mapping because looking it up is expensive
117*cdf0e10cSrcweir     // TODO: change to hash_multimap when a use case requires a m:n mapping
118*cdf0e10cSrcweir     typedef ::std::hash_map<int,int> Int2IntMap;
119*cdf0e10cSrcweir     mutable Int2IntMap* mpChar2Glyph;
120*cdf0e10cSrcweir     mutable Int2IntMap* mpGlyph2Char;
121*cdf0e10cSrcweir     void InitHashes() const;
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir     const ExtraKernInfo* mpExtraKernInfo;
124*cdf0e10cSrcweir };
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir // these two inlines are very important for performance
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir inline int FtFontInfo::GetGlyphIndex( sal_UCS4 cChar ) const
129*cdf0e10cSrcweir {
130*cdf0e10cSrcweir     if( !mpChar2Glyph )
131*cdf0e10cSrcweir         return -1;
132*cdf0e10cSrcweir     Int2IntMap::const_iterator it = mpChar2Glyph->find( cChar );
133*cdf0e10cSrcweir     if( it == mpChar2Glyph->end() )
134*cdf0e10cSrcweir         return -1;
135*cdf0e10cSrcweir     return it->second;
136*cdf0e10cSrcweir }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir inline void FtFontInfo::CacheGlyphIndex( sal_UCS4 cChar, int nIndex ) const
139*cdf0e10cSrcweir {
140*cdf0e10cSrcweir     if( !mpChar2Glyph )
141*cdf0e10cSrcweir         InitHashes();
142*cdf0e10cSrcweir     (*mpChar2Glyph)[ cChar ] = nIndex;
143*cdf0e10cSrcweir     (*mpGlyph2Char)[ nIndex ] = cChar;
144*cdf0e10cSrcweir }
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir // -----------------------------------------------------------------------
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir class FreetypeManager
149*cdf0e10cSrcweir {
150*cdf0e10cSrcweir public:
151*cdf0e10cSrcweir                         FreetypeManager();
152*cdf0e10cSrcweir                         ~FreetypeManager();
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir     long                AddFontDir( const String& rUrlName );
155*cdf0e10cSrcweir     void                AddFontFile( const rtl::OString& rNormalizedName,
156*cdf0e10cSrcweir                             int nFaceNum, sal_IntPtr nFontId, const ImplDevFontAttributes&,
157*cdf0e10cSrcweir                             const ExtraKernInfo* );
158*cdf0e10cSrcweir     void                AnnounceFonts( ImplDevFontList* ) const;
159*cdf0e10cSrcweir     void                ClearFontList();
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir     FreetypeServerFont* CreateFont( const ImplFontSelectData& );
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir private:
164*cdf0e10cSrcweir     typedef ::std::hash_map<sal_IntPtr,FtFontInfo*> FontList;
165*cdf0e10cSrcweir     FontList            maFontList;
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir     sal_IntPtr          mnMaxFontId;
168*cdf0e10cSrcweir     sal_IntPtr          mnNextFontId;
169*cdf0e10cSrcweir };
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir // -----------------------------------------------------------------------
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir class FreetypeServerFont : public ServerFont
174*cdf0e10cSrcweir {
175*cdf0e10cSrcweir public:
176*cdf0e10cSrcweir                                 FreetypeServerFont( const ImplFontSelectData&, FtFontInfo* );
177*cdf0e10cSrcweir     virtual                     ~FreetypeServerFont();
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir     virtual const ::rtl::OString* GetFontFileName() const { return mpFontInfo->GetFontFileName(); }
180*cdf0e10cSrcweir     virtual int                 GetFontFaceNum() const { return mpFontInfo->GetFaceNum(); }
181*cdf0e10cSrcweir     virtual bool                TestFont() const;
182*cdf0e10cSrcweir     virtual void*               GetFtFace() const;
183*cdf0e10cSrcweir     virtual void                SetFontOptions( const ImplFontOptions&);
184*cdf0e10cSrcweir     virtual int                 GetLoadFlags() const { return (mnLoadFlags & ~FT_LOAD_IGNORE_TRANSFORM); }
185*cdf0e10cSrcweir     virtual bool                NeedsArtificialBold() const { return mbArtBold; }
186*cdf0e10cSrcweir     virtual bool                NeedsArtificialItalic() const { return mbArtItalic; }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir     virtual void                FetchFontMetric( ImplFontMetricData&, long& rFactor ) const;
189*cdf0e10cSrcweir     virtual const ImplFontCharMap* GetImplFontCharMap( void ) const;
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     virtual int                 GetGlyphIndex( sal_UCS4 ) const;
192*cdf0e10cSrcweir     int                         GetRawGlyphIndex( sal_UCS4 ) const;
193*cdf0e10cSrcweir     int                         FixupGlyphIndex( int nGlyphIndex, sal_UCS4 ) const;
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir     virtual bool                GetAntialiasAdvice( void ) const;
196*cdf0e10cSrcweir     virtual bool                GetGlyphBitmap1( int nGlyphIndex, RawBitmap& ) const;
197*cdf0e10cSrcweir     virtual bool                GetGlyphBitmap8( int nGlyphIndex, RawBitmap& ) const;
198*cdf0e10cSrcweir     virtual bool                GetGlyphOutline( int nGlyphIndex, ::basegfx::B2DPolyPolygon& ) const;
199*cdf0e10cSrcweir     virtual int                 GetGlyphKernValue( int nLeftGlyph, int nRightGlyph ) const;
200*cdf0e10cSrcweir     virtual sal_uLong               GetKernPairs( ImplKernPairData** ) const;
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir     const unsigned char*        GetTable( const char* pName, sal_uLong* pLength )
203*cdf0e10cSrcweir                                 { return mpFontInfo->GetTable( pName, pLength ); }
204*cdf0e10cSrcweir     int                         GetEmUnits() const;
205*cdf0e10cSrcweir     const FT_Size_Metrics&      GetMetricsFT() const { return maSizeFT->metrics; }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir protected:
208*cdf0e10cSrcweir     friend class GlyphCache;
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir     int                         ApplyGlyphTransform( int nGlyphFlags, FT_GlyphRec_*, bool ) const;
211*cdf0e10cSrcweir     virtual void                InitGlyphData( int nGlyphIndex, GlyphData& ) const;
212*cdf0e10cSrcweir     bool                        ApplyGSUB( const ImplFontSelectData& );
213*cdf0e10cSrcweir     virtual ServerFontLayoutEngine* GetLayoutEngine();
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir private:
216*cdf0e10cSrcweir     int                         mnWidth;
217*cdf0e10cSrcweir     int                         mnPrioEmbedded;
218*cdf0e10cSrcweir     int                         mnPrioAntiAlias;
219*cdf0e10cSrcweir     int                         mnPrioAutoHint;
220*cdf0e10cSrcweir     FtFontInfo*                 mpFontInfo;
221*cdf0e10cSrcweir     FT_Int                      mnLoadFlags;
222*cdf0e10cSrcweir     double                      mfStretch;
223*cdf0e10cSrcweir     FT_FaceRec_*                maFaceFT;
224*cdf0e10cSrcweir     FT_SizeRec_*                maSizeFT;
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir     bool                        mbFaceOk;
227*cdf0e10cSrcweir     bool			mbArtItalic;
228*cdf0e10cSrcweir     bool			mbArtBold;
229*cdf0e10cSrcweir     bool			mbUseGamma;
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir     typedef ::std::hash_map<int,int> GlyphSubstitution;
232*cdf0e10cSrcweir     GlyphSubstitution           maGlyphSubstitution;
233*cdf0e10cSrcweir     rtl_UnicodeToTextConverter  maRecodeConverter;
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir     ServerFontLayoutEngine*     mpLayoutEngine;
236*cdf0e10cSrcweir };
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir // -----------------------------------------------------------------------
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir class ImplFTSFontData : public ImplFontData
241*cdf0e10cSrcweir {
242*cdf0e10cSrcweir private:
243*cdf0e10cSrcweir     FtFontInfo*             mpFtFontInfo;
244*cdf0e10cSrcweir     enum { IFTSFONT_MAGIC = 0x1F150A1C };
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir public:
247*cdf0e10cSrcweir                             ImplFTSFontData( FtFontInfo*, const ImplDevFontAttributes& );
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir     FtFontInfo*             GetFtFontInfo() const { return mpFtFontInfo; }
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir     virtual ImplFontEntry*  CreateFontInstance( ImplFontSelectData& ) const;
252*cdf0e10cSrcweir     virtual ImplFontData*   Clone() const   { return new ImplFTSFontData( *this ); }
253*cdf0e10cSrcweir     virtual sal_IntPtr      GetFontId() const { return mpFtFontInfo->GetFontId(); }
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir     static bool             CheckFontData( const ImplFontData& r ) { return r.CheckMagic( IFTSFONT_MAGIC ); }
256*cdf0e10cSrcweir };
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir // -----------------------------------------------------------------------
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir #endif // _SV_GCACHFTYP_HXX
261