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