1 //========================================================================
2 //
3 // FoFiTrueType.h
4 //
5 // Copyright 1999-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8 
9 //========================================================================
10 //
11 // Modified under the Poppler project - http://poppler.freedesktop.org
12 //
13 // All changes made under the Poppler project to this file are licensed
14 // under GPL version 2 or later
15 //
16 // Copyright (C) 2006 Takashi Iwai <tiwai@suse.de>
17 // Copyright (C) 2007 Koji Otani <sho@bbr.jp>
18 // Copyright (C) 2011, 2012, 2018-2020 Albert Astals Cid <aacid@kde.org>
19 // Copyright (C) 2012 Suzuki Toshiya <mpsuzuki@hiroshima-u.ac.jp>
20 // Copyright (C) 2016 William Bader <williambader@hotmail.com>
21 // Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
22 //
23 // To see a description of the changes please see the Changelog file that
24 // came with your tarball or type make ChangeLog if you are building from git
25 //
26 //========================================================================
27 
28 #ifndef FOFITRUETYPE_H
29 #define FOFITRUETYPE_H
30 
31 #include <cstddef>
32 #include <unordered_map>
33 #include <string>
34 #include "FoFiBase.h"
35 #include "poppler_private_export.h"
36 
37 class GooString;
38 struct TrueTypeTable;
39 struct TrueTypeCmap;
40 
41 //------------------------------------------------------------------------
42 // FoFiTrueType
43 //------------------------------------------------------------------------
44 
45 class POPPLER_PRIVATE_EXPORT FoFiTrueType : public FoFiBase
46 {
47 public:
48     // Create a FoFiTrueType object from a memory buffer.
49     static FoFiTrueType *make(const char *fileA, int lenA, int faceIndexA = 0);
50 
51     // Create a FoFiTrueType object from a file on disk.
52     static FoFiTrueType *load(const char *fileName, int faceIndexA = 0);
53 
54     ~FoFiTrueType() override;
55 
56     // Returns true if this an OpenType font containing CFF data, false
57     // if it's a TrueType font (or OpenType font with TrueType data).
isOpenTypeCFF()58     bool isOpenTypeCFF() const { return openTypeCFF; }
59 
60     // Return the number of cmaps defined by this font.
61     int getNumCmaps() const;
62 
63     // Return the platform ID of the <i>th cmap.
64     int getCmapPlatform(int i) const;
65 
66     // Return the encoding ID of the <i>th cmap.
67     int getCmapEncoding(int i) const;
68 
69     // Return the index of the cmap for <platform>, <encoding>.  Returns
70     // -1 if there is no corresponding cmap.
71     int findCmap(int platform, int encoding) const;
72 
73     // Return the GID corresponding to <c> according to the <i>th cmap.
74     int mapCodeToGID(int i, unsigned int c) const;
75 
76     // map gid to vertical glyph gid if exist.
77     //   if not exist return original gid
78     unsigned int mapToVertGID(unsigned int orgGID);
79 
80     // Returns the GID corresponding to <name> according to the post
81     // table.  Returns 0 if there is no mapping for <name> or if the
82     // font does not have a post table.
83     int mapNameToGID(const char *name) const;
84 
85     // Return the mapping from CIDs to GIDs, and return the number of
86     // CIDs in *<nCIDs>.  This is only useful for CID fonts.  (Only
87     // useful for OpenType CFF fonts.)
88     int *getCIDToGIDMap(int *nCIDs) const;
89 
90     // Returns the least restrictive embedding licensing right (as
91     // defined by the TrueType spec):
92     // * 4: OS/2 table is missing or invalid
93     // * 3: installable embedding
94     // * 2: editable embedding
95     // * 1: preview & print embedding
96     // * 0: restricted license embedding
97     int getEmbeddingRights() const;
98 
99     // Return the font matrix as an array of six numbers.  (Only useful
100     // for OpenType CFF fonts.)
101     void getFontMatrix(double *mat) const;
102 
103     // Convert to a Type 42 font, suitable for embedding in a PostScript
104     // file.  <psName> will be used as the PostScript font name (so we
105     // don't need to depend on the 'name' table in the font).  The
106     // <encoding> array specifies the mapping from char codes to names.
107     // If <encoding> is NULL, the encoding is unknown or undefined.  The
108     // <codeToGID> array specifies the mapping from char codes to GIDs.
109     // (Not useful for OpenType CFF fonts.)
110     void convertToType42(const char *psName, char **encoding, int *codeToGID, FoFiOutputFunc outputFunc, void *outputStream) const;
111 
112     // Convert to a Type 1 font, suitable for embedding in a PostScript
113     // file.  This is only useful with 8-bit fonts.  If <newEncoding> is
114     // not NULL, it will be used in place of the encoding in the Type 1C
115     // font.  If <ascii> is true the eexec section will be hex-encoded,
116     // otherwise it will be left as binary data.  If <psName> is
117     // non-NULL, it will be used as the PostScript font name.  (Only
118     // useful for OpenType CFF fonts.)
119     void convertToType1(const char *psName, const char **newEncoding, bool ascii, FoFiOutputFunc outputFunc, void *outputStream) const;
120 
121     // Convert to a Type 2 CIDFont, suitable for embedding in a
122     // PostScript file.  <psName> will be used as the PostScript font
123     // name (so we don't need to depend on the 'name' table in the
124     // font).  The <cidMap> array maps CIDs to GIDs; it has <nCIDs>
125     // entries.  (Not useful for OpenType CFF fonts.)
126     void convertToCIDType2(const char *psName, const int *cidMap, int nCIDs, bool needVerticalMetrics, FoFiOutputFunc outputFunc, void *outputStream) const;
127 
128     // Convert to a Type 0 CIDFont, suitable for embedding in a
129     // PostScript file.  <psName> will be used as the PostScript font
130     // name.  (Only useful for OpenType CFF fonts.)
131     void convertToCIDType0(const char *psName, int *cidMap, int nCIDs, FoFiOutputFunc outputFunc, void *outputStream) const;
132 
133     // Convert to a Type 0 (but non-CID) composite font, suitable for
134     // embedding in a PostScript file.  <psName> will be used as the
135     // PostScript font name (so we don't need to depend on the 'name'
136     // table in the font).  The <cidMap> array maps CIDs to GIDs; it has
137     // <nCIDs> entries.  (Not useful for OpenType CFF fonts.)
138     void convertToType0(const char *psName, int *cidMap, int nCIDs, bool needVerticalMetrics, int *maxValidGlyph, FoFiOutputFunc outputFunc, void *outputStream) const;
139 
140     // Convert to a Type 0 (but non-CID) composite font, suitable for
141     // embedding in a PostScript file.  <psName> will be used as the
142     // PostScript font name.  (Only useful for OpenType CFF fonts.)
143     void convertToType0(const char *psName, int *cidMap, int nCIDs, FoFiOutputFunc outputFunc, void *outputStream) const;
144 
145     // Returns a pointer to the CFF font embedded in this OpenType font.
146     // If successful, sets *<start> and *<length>, and returns true.
147     // Otherwise returns false.  (Only useful for OpenType CFF fonts).
148     bool getCFFBlock(char **start, int *length) const;
149 
150     // setup vert/vrt2 GSUB for default lang
151     int setupGSUB(const char *scriptName);
152 
153     // setup vert/vrt2 GSUB for specified lang
154     int setupGSUB(const char *scriptName, const char *languageName);
155 
156 private:
157     FoFiTrueType(const char *fileA, int lenA, bool freeFileDataA, int faceIndexA);
158     void cvtEncoding(char **encoding, FoFiOutputFunc outputFunc, void *outputStream) const;
159     void cvtCharStrings(char **encoding, const int *codeToGID, FoFiOutputFunc outputFunc, void *outputStream) const;
160     void cvtSfnts(FoFiOutputFunc outputFunc, void *outputStream, const GooString *name, bool needVerticalMetrics, int *maxUsedGlyph) const;
161     void dumpString(const unsigned char *s, int length, FoFiOutputFunc outputFunc, void *outputStream) const;
162     unsigned int computeTableChecksum(const unsigned char *data, int length) const;
163     void parse();
164     void readPostTable();
165     int seekTable(const char *tag) const;
166     unsigned int charToTag(const char *tagName);
167     unsigned int doMapToVertGID(unsigned int orgGID);
168     unsigned int scanLookupList(unsigned int listIndex, unsigned int orgGID);
169     unsigned int scanLookupSubTable(unsigned int subTable, unsigned int orgGID);
170     int checkGIDInCoverage(unsigned int coverage, unsigned int orgGID);
171 
172     TrueTypeTable *tables;
173     int nTables;
174     TrueTypeCmap *cmaps;
175     int nCmaps;
176     int nGlyphs;
177     int locaFmt;
178     int bbox[4];
179     std::unordered_map<std::string, int> nameToGID;
180     bool openTypeCFF;
181 
182     bool parsedOk;
183     int faceIndex;
184     unsigned int gsubFeatureTable;
185     unsigned int gsubLookupList;
186 };
187 
188 #endif
189