1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        pdffontparsertruetype.h
3 // Purpose:
4 // Author:      Ulrich Telle
5 // Created:     2009-03-04
6 // Copyright:   (c) Ulrich Telle
7 // Licence:     wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9 
10 /// \file pdffontparsertruetype.h Interface of the TrueType Font support classes
11 
12 #ifndef _PDF_FONT_PARSER_TRUETYPE_H_
13 #define _PDF_FONT_PARSER_TRUETYPE_H_
14 
15 // wxWidgets headers
16 #include <wx/hashmap.h>
17 #include <wx/mstream.h>
18 
19 // wxPdfDocument headers
20 #include "wx/pdfdocdef.h"
21 #include "wx/pdffontdata.h"
22 #include "wx/pdffontdescription.h"
23 #include "wx/pdffontparser.h"
24 
25 #include "wx/pdffontmacosx.h"
26 
27 class WXDLLIMPEXP_FWD_PDFDOC wxPdfFontData;
28 
29 /// Class representing a table directory entry for TrueType fonts (For internal use only)
30 class WXDLLIMPEXP_PDFDOC wxPdfTableDirectoryEntry
31 {
32 public:
33   /// Default constructor
wxPdfTableDirectoryEntry()34   wxPdfTableDirectoryEntry() : m_checksum(0), m_offset(0), m_length(0) {}
35   int m_checksum;  ///< Table checksum
36   int m_offset;    ///< Table offset
37   int m_length;    ///< Table length
38 };
39 
40 /// Hash map class for used/embedded fonts
41 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxPdfTableDirectoryEntry*, wxPdfTableDirectory, class WXDLLIMPEXP_PDFDOC);
42 
43 /// Class representing a CMap table entry
44 class WXDLLIMPEXP_PDFDOC wxPdfCMapEntry
45 {
46 public:
47   /// Default constructor
wxPdfCMapEntry()48   wxPdfCMapEntry() : m_glyph(0), m_width(0) {}
49   int m_glyph;  ///< glyph number
50   int m_width;  ///< glyph width
51 };
52 
53 /// Hash map class for used/embedded fonts
54 WX_DECLARE_HASH_MAP_WITH_DECL(long, wxPdfCMapEntry*, wxIntegerHash, wxIntegerEqual, wxPdfCMap, class WXDLLIMPEXP_PDFDOC);
55 
56 /// Class representing a TrueType/OpenType font parser
57 class WXDLLIMPEXP_PDFDOC wxPdfFontParserTrueType : public wxPdfFontParser
58 {
59 public:
60   /// Default constructor
61   wxPdfFontParserTrueType();
62 
63   /// Default destructor
64   virtual ~wxPdfFontParserTrueType();
65 
66   /// Identify font based on a font file
67   /**
68   * \param fontFileName fully qualified name of the font file
69   * \param fontIndex the index of the font within a font collection (if appropriate)
70   * \return a reference to a new font data instance
71   */
72   wxPdfFontData* IdentifyFont(const wxString& fontFileName, int fontIndex);
73 
74 #if defined(__WXMSW__) || defined(__WXMAC__)
75   /// Identify font based on a wxFont object
76   /**
77   * \param font the wxFont font object to be identified
78   * \return a reference to a new font data instance
79   */
80   wxPdfFontData* IdentifyFont(const wxFont& font);
81 #endif
82 
83   /// Load the font data
84   /**
85   * Before using a font it's data have be loaded into memory. This method tries
86   * to load the required font data for a previously identified font.
87   * \param fontData the font data instance to be loaded
88   * \return TRUE if the font data could be loaded successfully, FALSE otherwise
89   */
90   bool LoadFontData(wxPdfFontData* fontData);
91 
92   /// Get the number of fonts within a font collection
93   /**
94   * \param fontFileName the fully qualified name of the font collection file
95   * \return the number of fonts in the collection
96   */
97   int GetCollectionFontCount(const wxString& fontFileName);
98 
99 #ifdef __WXMSW__
100   /// Load a TrueType font stream based on a wxFont object
101   /**
102   * \param font the font for which the font stream is to loaded
103   * \return the font data as a memory stream
104   */
105   static wxMemoryInputStream* LoadTrueTypeFontStream(const wxFont& font);
106 #endif
107 
108 protected:
109   /// Clear the table directory
110   void ClearTableDirectory();
111 
112   /// Lock font table
113   void LockTable(const wxString& tableName);
114 
115   /// Release font table
116   void ReleaseTable();
117 
118   /// Calculate a check sum
119   int CalculateChecksum(const char* b, size_t length);
120 
121   /// Identify a font
122   wxPdfFontData* IdentifyFont();
123 
124   /// Prepare the font data
125   bool PrepareFontData(wxPdfFontData* fontData);
126 
127   /// Check whether the font contains all required tables
128   bool CheckTables();
129 
130   /// Check whether the font is in compact font format (CFF)
131   void CheckCff();
132 
133   /// Check the font's access restrictions
134   void CheckRestrictions();
135 
136   /// Get the base name of the font
137   /**
138   * Gets the Postscript font name.
139   * \return the Postscript font name
140   */
141   wxString GetBaseFont();
142 
143   /// Get a list of unique names
144   /**
145   * Extracts the names of the font in all the languages available.
146   * \param id the name id to retrieve
147   * \return a list of unique names
148   */
149   wxArrayString GetUniqueNames(int id);
150 
151   /// Get font names
152   /**
153   * Extracts the names of the font in all the languages available.
154   * Optionally the platform, encoding and language identifiers are extracted
155   * as well. In the latter case 4 consecutive entries in the resulting string
156   * array represent a single font name.
157   * \param id the name id to retrieve
158   * \param namesOnly flag whether to extract only names or names and identifiers
159   * \return a list of font names
160   */
161   wxArrayString GetNames(int id, bool namesOnly = true);
162 
163   /// Get the English language font name
164   /**
165   * \param id the name id to retrieve
166   * \return the English font name
167   */
168   wxString GetEnglishName(int id);
169 
170   /// Read font maps
171   /**
172   * Reads the tables 'head', 'hhea', 'OS/2' and 'post' filling several variables.
173   * \return TRUE if the font maps could be read successfully, FALSE otherwise
174   */
175   bool ReadMaps();
176 
177   /// Read the glyph widths
178   /**
179   * Reads the glyphs widths. The widths are extracted from the table 'hmtx'.
180   * The glyphs are normalized to 1000 units.
181   * \param numberOfHMetrics
182   * \param unitsPerEm
183   * \return TRUE if the glyph widths could be read successfully, FALSE otherwise
184   */
185   bool ReadGlyphWidths(int numberOfHMetrics, int unitsPerEm);
186 
187   /// Read kerning information
188   void ReadKerning(int unitsPerEm);
189 
190   /// Read table directory
191   bool ReadTableDirectory();
192 
193   /// Read a Format 0 CMap
194   wxPdfCMap* ReadFormat0();
195 
196   /// Read a Format 4 CMap
197   wxPdfCMap* ReadFormat4();
198 
199   /// Read a Format 6 CMap
200   wxPdfCMap* ReadFormat6();
201 
202   /// Read a Format 12 CMap
203   wxPdfCMap* ReadFormat12();
204 
205   /// Get the width of a specific glyph
206   int GetGlyphWidth(unsigned int glyph);
207 
208   size_t                m_directoryOffset;   ///< offset of the table directory
209   wxPdfTableDirectory*  m_tableDirectory;    ///< table directory of the font
210 
211   bool                  m_isMacCoreText;     ///< Flag whether a Mac Core Text font is to be parsed
212 #if defined(__WXMAC__)
213 #if wxPDFMACOSX_HAS_CORE_TEXT
214   CTFontRef             m_fontRef;           ///< Mac Core Text font reference
215   wxCFRef<CFDataRef>    m_tableRef;          ///< Font table reference
216 #endif
217 #endif
218 
219 private:
220   bool                  m_cff;               ///< Flag whether the font is in CFF format
221   size_t                m_cffOffset;         ///< offset of the CFF table
222   size_t                m_cffLength;         ///< length of the CFF table
223 
224   bool                  m_embedAllowed;      ///< Flag whether embedding is allowed
225   bool                  m_subsetAllowed;     ///< Flag whether subsetting is allowed
226   bool                  m_isFixedPitch;      ///< Flag whether the font has a fixed pitch
227 
228   wxPdfFontDescription  m_fd;                ///< font description
229 
230   wxPdfArrayUint16      m_glyphWidths;       ///< list of glyph widths
231   bool                  m_fontSpecific;      ///< Flag whether font has a specific encoding
232   wxPdfCMap*            m_cmap10;            ///< Reference of CMap (Type 1,0 - Postscript)
233   wxPdfCMap*            m_cmap31;            ///< Reference of CMap (Type 3,1 - Unicode)
234   wxPdfCMap*            m_cmapExt;           ///< Reference of CMap (Type Ext - Extended Unicode)
235   wxPdfKernPairMap*     m_kp;                ///< list of kerning pairs
236 
237   wxInputStream*        m_savedStream;       ///< Saved input stream
238 };
239 
240 #endif
241