xref: /aoo42x/main/vcl/source/glyphs/gcach_ftyp.hxx (revision 248a599f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _SV_GCACHFTYP_HXX
25 #define _SV_GCACHFTYP_HXX
26 
27 #include <glyphcache.hxx>
28 #include <rtl/textcvt.h>
29 
30 #include <ft2build.h>
31 #include FT_FREETYPE_H
32 
33 class FreetypeServerFont;
34 struct FT_GlyphRec_;
35 
36 // -----------------------------------------------------------------------
37 
38 // FtFontFile has the responsibility that a font file is only mapped once.
39 // (#86621#) the old directly ft-managed solution caused it to be mapped
40 // in up to nTTC*nSizes*nOrientation*nSynthetic times
41 class FtFontFile
42 {
43 public:
44     static FtFontFile*      FindFontFile( const ::rtl::OString& rNativeFileName );
45 
46     bool                    Map();
47     void                    Unmap();
48 
GetBuffer() const49     const unsigned char*    GetBuffer() const { return mpFileMap; }
GetFileSize() const50     int                     GetFileSize() const { return mnFileSize; }
GetFileName() const51     const ::rtl::OString*   GetFileName() const { return &maNativeFileName; }
GetLangBoost() const52     int                     GetLangBoost() const { return mnLangBoost; }
53 
54 private:
55                             FtFontFile( const ::rtl::OString& rNativeFileName );
56 
57     const ::rtl::OString    maNativeFileName;
58     const unsigned char*    mpFileMap;
59     int                     mnFileSize;
60     int                     mnRefCount;
61     int                     mnLangBoost;
62 };
63 
64 // -----------------------------------------------------------------------
65 
66 // FtFontInfo corresponds to an unscaled font face
67 class FtFontInfo
68 {
69 public:
70                            FtFontInfo( const ImplDevFontAttributes&,
71                                const ::rtl::OString& rNativeFileName,
72                                int nFaceNum, sal_IntPtr nFontId, int nSynthetic,
73                                 const ExtraKernInfo* );
74                           ~FtFontInfo();
75 
76     const unsigned char*  GetTable( const char*, sal_uLong* pLength=0 ) const;
77 
78     FT_FaceRec_*          GetFaceFT();
79     void                  ReleaseFaceFT( FT_FaceRec_* );
80 
GetFontFileName() const81     const ::rtl::OString* GetFontFileName() const   { return mpFontFile->GetFileName(); }
GetFaceNum() const82     int                   GetFaceNum() const        { return mnFaceNum; }
GetSynthetic() const83     int                   GetSynthetic() const      { return mnSynthetic; }
GetFontId() const84     sal_IntPtr            GetFontId() const         { return mnFontId; }
IsSymbolFont() const85     bool                  IsSymbolFont() const      { return maDevFontAttributes.IsSymbolFont(); }
GetFontAttributes() const86     const ImplFontAttributes& GetFontAttributes() const { return maDevFontAttributes; }
87 
88     void                  AnnounceFont( ImplDevFontList* );
89 
90     int                   GetGlyphIndex( sal_UCS4 cChar ) const;
91     void                  CacheGlyphIndex( sal_UCS4 cChar, int nGI ) const;
92 
93     bool                  GetFontCodeRanges( CmapResult& ) const;
94     const ImplFontCharMap* GetImplFontCharMap( void );
95 
96     bool                  HasExtraKerning() const;
97     int                   GetExtraKernPairs( ImplKernPairData** ) const;
98     int                   GetExtraGlyphKernValue( int nLeftGlyph, int nRightGlyph ) const;
99 
100 private:
101     FT_FaceRec_*    maFaceFT;
102     FtFontFile*     mpFontFile;
103     const int       mnFaceNum;
104     int             mnRefCount;
105     const int       mnSynthetic;
106 
107     sal_IntPtr      mnFontId;
108     ImplDevFontAttributes maDevFontAttributes;
109 
110     const ImplFontCharMap* mpFontCharMap;
111 
112     // cache unicode->glyphid mapping because looking it up is expensive
113     // TODO: change to hash_multimap when a use case requires a m:n mapping
114     typedef ::std::hash_map<int,int> Int2IntMap;
115     mutable Int2IntMap* mpChar2Glyph;
116     mutable Int2IntMap* mpGlyph2Char;
117     void InitHashes() const;
118 
119     const ExtraKernInfo* mpExtraKernInfo;
120 };
121 
122 // these two inlines are very important for performance
123 
GetGlyphIndex(sal_UCS4 cChar) const124 inline int FtFontInfo::GetGlyphIndex( sal_UCS4 cChar ) const
125 {
126     if( !mpChar2Glyph )
127         return -1;
128     Int2IntMap::const_iterator it = mpChar2Glyph->find( cChar );
129     if( it == mpChar2Glyph->end() )
130         return -1;
131     return it->second;
132 }
133 
CacheGlyphIndex(sal_UCS4 cChar,int nIndex) const134 inline void FtFontInfo::CacheGlyphIndex( sal_UCS4 cChar, int nIndex ) const
135 {
136     if( !mpChar2Glyph )
137         InitHashes();
138     (*mpChar2Glyph)[ cChar ] = nIndex;
139     (*mpGlyph2Char)[ nIndex ] = cChar;
140 }
141 
142 // -----------------------------------------------------------------------
143 
144 class FreetypeManager
145 {
146 public:
147                         FreetypeManager();
148                         ~FreetypeManager();
149 
150     long                AddFontDir( const String& rUrlName );
151     void                AddFontFile( const rtl::OString& rNormalizedName,
152                             int nFaceNum, sal_IntPtr nFontId, const ImplDevFontAttributes&,
153                             const ExtraKernInfo* );
154     void                AnnounceFonts( ImplDevFontList* ) const;
155     void                ClearFontList();
156 
157     FreetypeServerFont* CreateFont( const ImplFontSelectData& );
158 
159 private:
160     typedef ::std::hash_map<sal_IntPtr,FtFontInfo*> FontList;
161     FontList            maFontList;
162 
163     sal_IntPtr          mnMaxFontId;
164     sal_IntPtr          mnNextFontId;
165 };
166 
167 // -----------------------------------------------------------------------
168 
169 class FreetypeServerFont : public ServerFont
170 {
171 public:
172                                 FreetypeServerFont( const ImplFontSelectData&, FtFontInfo* );
173     virtual                     ~FreetypeServerFont();
174 
GetFontFileName() const175     virtual const ::rtl::OString* GetFontFileName() const { return mpFontInfo->GetFontFileName(); }
GetFontFaceNum() const176     virtual int                 GetFontFaceNum() const { return mpFontInfo->GetFaceNum(); }
177     virtual bool                TestFont() const;
178     virtual void*               GetFtFace() const;
179     virtual void                SetFontOptions( const ImplFontOptions&);
GetLoadFlags() const180     virtual int                 GetLoadFlags() const { return (mnLoadFlags & ~FT_LOAD_IGNORE_TRANSFORM); }
NeedsArtificialBold() const181     virtual bool                NeedsArtificialBold() const { return mbArtBold; }
NeedsArtificialItalic() const182     virtual bool                NeedsArtificialItalic() const { return mbArtItalic; }
183 
184     virtual void                FetchFontMetric( ImplFontMetricData&, long& rFactor ) const;
185     virtual const ImplFontCharMap* GetImplFontCharMap( void ) const;
186 
187     virtual sal_GlyphId         GetGlyphIndex( sal_UCS4 ) const;
188     sal_GlyphId                 GetRawGlyphIndex( sal_UCS4 ) const;
189     sal_GlyphId                 FixupGlyphIndex( sal_GlyphId, sal_UCS4 ) const;
190 
191     virtual bool                GetAntialiasAdvice( void ) const;
192     virtual bool                GetGlyphBitmap1( sal_GlyphId, RawBitmap& ) const;
193     virtual bool                GetGlyphBitmap8( sal_GlyphId, RawBitmap& ) const;
194     virtual bool                GetGlyphOutline( sal_GlyphId, ::basegfx::B2DPolyPolygon& ) const;
195     virtual int                 GetGlyphKernValue( int nLeftGlyph, int nRightGlyph ) const;
196     virtual sal_uLong               GetKernPairs( ImplKernPairData** ) const;
197 
GetTable(const char * pName,sal_uLong * pLength)198     const unsigned char*        GetTable( const char* pName, sal_uLong* pLength )
199                                 { return mpFontInfo->GetTable( pName, pLength ); }
200     int                         GetEmUnits() const;
GetMetricsFT() const201     const FT_Size_Metrics&      GetMetricsFT() const { return maSizeFT->metrics; }
202 
203 protected:
204     friend class GlyphCache;
205 
206     int                         ApplyGlyphTransform( int nGlyphFlags, FT_GlyphRec_*, bool ) const;
207     virtual void                InitGlyphData( sal_GlyphId, GlyphData& ) const;
208     bool                        ApplyGSUB( const ImplFontSelectData& );
209     virtual ServerFontLayoutEngine* GetLayoutEngine();
210 
211 private:
212     int                         mnWidth;
213     int                         mnPrioEmbedded;
214     int                         mnPrioAntiAlias;
215     int                         mnPrioAutoHint;
216     FtFontInfo*                 mpFontInfo;
217     FT_Int                      mnLoadFlags;
218     double                      mfStretch;
219     FT_FaceRec_*                maFaceFT;
220     FT_SizeRec_*                maSizeFT;
221 
222     bool                        mbFaceOk;
223     bool			mbArtItalic;
224     bool			mbArtBold;
225     bool			mbUseGamma;
226 
227     typedef ::std::hash_map<int,int> GlyphSubstitution;
228     GlyphSubstitution           maGlyphSubstitution;
229     rtl_UnicodeToTextConverter  maRecodeConverter;
230 
231     ServerFontLayoutEngine*     mpLayoutEngine;
232 };
233 
234 // -----------------------------------------------------------------------
235 
236 class ImplFTSFontData : public ImplFontData
237 {
238 private:
239     FtFontInfo*             mpFtFontInfo;
240     enum { IFTSFONT_MAGIC = 0x1F150A1C };
241 
242 public:
243                             ImplFTSFontData( FtFontInfo*, const ImplDevFontAttributes& );
244 
GetFtFontInfo() const245     FtFontInfo*             GetFtFontInfo() const { return mpFtFontInfo; }
246 
247     virtual ImplFontEntry*  CreateFontInstance( ImplFontSelectData& ) const;
Clone() const248     virtual ImplFontData*   Clone() const   { return new ImplFTSFontData( *this ); }
GetFontId() const249     virtual sal_IntPtr      GetFontId() const { return mpFtFontInfo->GetFontId(); }
250 
CheckFontData(const ImplFontData & r)251     static bool             CheckFontData( const ImplFontData& r ) { return r.CheckMagic( IFTSFONT_MAGIC ); }
252 };
253 
254 // -----------------------------------------------------------------------
255 
256 #endif // _SV_GCACHFTYP_HXX
257