Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / generic / glyphs / gcach_ftyp.hxx
blob590fb90d27271d6f886b297ab8fc4d7439fe4bbd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_VCL_GENERIC_GLYPHS_GCACH_FTYP_HXX
21 #define INCLUDED_VCL_GENERIC_GLYPHS_GCACH_FTYP_HXX
23 #include "generic/glyphcache.hxx"
24 #include "PhysicalFontFace.hxx"
26 #include <config_graphite.h>
27 #if ENABLE_GRAPHITE
28 class GraphiteFaceWrapper;
29 #endif
31 // FtFontFile has the responsibility that a font file is only mapped once.
32 // (#86621#) the old directly ft-managed solution caused it to be mapped
33 // in up to nTTC*nSizes*nOrientation*nSynthetic times
34 class FtFontFile
36 public:
37 static FtFontFile* FindFontFile( const OString& rNativeFileName );
39 bool Map();
40 void Unmap();
42 const unsigned char* GetBuffer() const { return mpFileMap; }
43 int GetFileSize() const { return mnFileSize; }
44 const OString& GetFileName() const { return maNativeFileName; }
45 int GetLangBoost() const { return mnLangBoost; }
47 private:
48 FtFontFile( const OString& rNativeFileName );
50 const OString maNativeFileName;
51 unsigned char* mpFileMap;
52 int mnFileSize;
53 int mnRefCount;
54 int mnLangBoost;
57 // FtFontInfo corresponds to an unscaled font face
58 class FtFontInfo
60 public:
61 FtFontInfo( const ImplDevFontAttributes&,
62 const OString& rNativeFileName,
63 int nFaceNum, sal_IntPtr nFontId, int nSynthetic);
64 ~FtFontInfo();
66 const unsigned char* GetTable( const char*, sal_uLong* pLength=0 ) const;
68 FT_FaceRec_* GetFaceFT();
69 #if ENABLE_GRAPHITE
70 GraphiteFaceWrapper* GetGraphiteFace();
71 #endif
72 void ReleaseFaceFT();
74 const OString& GetFontFileName() const { return mpFontFile->GetFileName(); }
75 int GetFaceNum() const { return mnFaceNum; }
76 int GetSynthetic() const { return mnSynthetic; }
77 sal_IntPtr GetFontId() const { return mnFontId; }
78 bool IsSymbolFont() const { return maDevFontAttributes.IsSymbolFont(); }
79 const ImplFontAttributes& GetFontAttributes() const { return maDevFontAttributes; }
81 void AnnounceFont( PhysicalFontCollection* );
83 int GetGlyphIndex( sal_UCS4 cChar ) const;
84 void CacheGlyphIndex( sal_UCS4 cChar, int nGI ) const;
86 bool GetFontCodeRanges( CmapResult& ) const;
87 const FontCharMapPtr GetFontCharMap();
89 private:
90 FT_FaceRec_* maFaceFT;
91 FtFontFile* mpFontFile;
92 const int mnFaceNum;
93 int mnRefCount;
94 const int mnSynthetic;
95 #if ENABLE_GRAPHITE
96 bool mbCheckedGraphite;
97 GraphiteFaceWrapper * mpGraphiteFace;
98 #endif
99 sal_IntPtr mnFontId;
100 ImplDevFontAttributes maDevFontAttributes;
102 FontCharMapPtr mpFontCharMap;
104 // cache unicode->glyphid mapping because looking it up is expensive
105 // TODO: change to std::unordered_multimap when a use case requires a m:n mapping
106 typedef std::unordered_map<int,int> Int2IntMap;
107 mutable Int2IntMap* mpChar2Glyph;
108 mutable Int2IntMap* mpGlyph2Char;
109 void InitHashes() const;
112 // these two inlines are very important for performance
114 inline int FtFontInfo::GetGlyphIndex( sal_UCS4 cChar ) const
116 if( !mpChar2Glyph )
117 return -1;
118 Int2IntMap::const_iterator it = mpChar2Glyph->find( cChar );
119 if( it == mpChar2Glyph->end() )
120 return -1;
121 return it->second;
124 inline void FtFontInfo::CacheGlyphIndex( sal_UCS4 cChar, int nIndex ) const
126 if( !mpChar2Glyph )
127 InitHashes();
128 (*mpChar2Glyph)[ cChar ] = nIndex;
129 (*mpGlyph2Char)[ nIndex ] = cChar;
132 class FreetypeManager
134 public:
135 FreetypeManager();
136 ~FreetypeManager();
138 void AddFontFile( const OString& rNormalizedName,
139 int nFaceNum, sal_IntPtr nFontId, const ImplDevFontAttributes&);
140 void AnnounceFonts( PhysicalFontCollection* ) const;
141 void ClearFontList();
143 ServerFont* CreateFont( const FontSelectPattern& );
145 private:
146 typedef std::unordered_map<sal_IntPtr,FtFontInfo*> FontList;
147 FontList maFontList;
149 sal_IntPtr mnMaxFontId;
152 class ImplFTSFontData : public PhysicalFontFace
154 private:
155 FtFontInfo* mpFtFontInfo;
156 enum { IFTSFONT_MAGIC = 0x1F150A1C };
158 public:
159 ImplFTSFontData( FtFontInfo*, const ImplDevFontAttributes& );
161 FtFontInfo* GetFtFontInfo() const { return mpFtFontInfo; }
163 virtual ImplFontEntry* CreateFontInstance( FontSelectPattern& ) const SAL_OVERRIDE;
164 virtual PhysicalFontFace* Clone() const SAL_OVERRIDE { return new ImplFTSFontData( *this ); }
165 virtual sal_IntPtr GetFontId() const SAL_OVERRIDE { return mpFtFontInfo->GetFontId(); }
167 static bool CheckFontData( const PhysicalFontFace& r ) { return r.CheckMagic( IFTSFONT_MAGIC ); }
170 #endif // INCLUDED_VCL_GENERIC_GLYPHS_GCACH_FTYP_HXX
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */