| 1 |
//======================================================================== |
| 2 |
// |
| 3 |
// FoFiTrueType.h |
| 4 |
// |
| 5 |
// Copyright 1999-2003 Glyph & Cog, LLC |
| 6 |
// |
| 7 |
//======================================================================== |
| 8 |
|
| 9 |
#ifndef FOFITRUETYPE_H |
| 10 |
#define FOFITRUETYPE_H |
| 11 |
|
| 12 |
#include <aconf.h> |
| 13 |
|
| 14 |
#include "gtypes.h" |
| 15 |
#include "FoFiBase.h" |
| 16 |
|
| 17 |
class GString; |
| 18 |
class GHash; |
| 19 |
struct TrueTypeTable; |
| 20 |
struct TrueTypeCmap; |
| 21 |
|
| 22 |
//------------------------------------------------------------------------ |
| 23 |
// FoFiTrueType |
| 24 |
//------------------------------------------------------------------------ |
| 25 |
|
| 26 |
class FoFiTrueType: public FoFiBase { |
| 27 |
public: |
| 28 |
|
| 29 |
// Create a FoFiTrueType object from a memory buffer. If |
| 30 |
// <allowHeadlessCFF> is true, OpenType CFF fonts without the 'head' |
| 31 |
// table are permitted -- this is useful when calling the convert* |
| 32 |
// functions. |
| 33 |
static FoFiTrueType *make(char *fileA, int lenA, int fontNum, |
| 34 |
GBool allowHeadlessCFF = gFalse); |
| 35 |
|
| 36 |
// Create a FoFiTrueType object from a file on disk. If |
| 37 |
// <allowHeadlessCFF> is true, OpenType CFF fonts without the 'head' |
| 38 |
// table are permitted -- this is useful when calling the convert* |
| 39 |
// functions. |
| 40 |
static FoFiTrueType *load(char *fileName, int fontNum, |
| 41 |
GBool allowHeadlessCFF = gFalse); |
| 42 |
|
| 43 |
virtual ~FoFiTrueType(); |
| 44 |
|
| 45 |
// Returns true if this an OpenType font containing CFF data, false |
| 46 |
// if it's a TrueType font (or OpenType font with TrueType data). |
| 47 |
GBool isOpenTypeCFF() { return openTypeCFF; } |
| 48 |
|
| 49 |
// Returns true if this is an OpenType CFF font that is missing the |
| 50 |
// 'head' table. This is a violation of the OpenType spec, but the |
| 51 |
// embedded CFF font can be usable for some purposes (e.g., the |
| 52 |
// convert* functions). |
| 53 |
GBool isHeadlessCFF() { return headlessCFF; } |
| 54 |
|
| 55 |
// Return the number of cmaps defined by this font. |
| 56 |
int getNumCmaps(); |
| 57 |
|
| 58 |
// Return the platform ID of the <i>th cmap. |
| 59 |
int getCmapPlatform(int i); |
| 60 |
|
| 61 |
// Return the encoding ID of the <i>th cmap. |
| 62 |
int getCmapEncoding(int i); |
| 63 |
|
| 64 |
// Return the index of the cmap for <platform>, <encoding>. Returns |
| 65 |
// -1 if there is no corresponding cmap. |
| 66 |
int findCmap(int platform, int encoding); |
| 67 |
|
| 68 |
// Return the GID corresponding to <c> according to the <i>th cmap. |
| 69 |
int mapCodeToGID(int i, int c); |
| 70 |
|
| 71 |
// Returns the GID corresponding to <name> according to the post |
| 72 |
// table. Returns 0 if there is no mapping for <name> or if the |
| 73 |
// font does not have a post table. |
| 74 |
int mapNameToGID(char *name); |
| 75 |
|
| 76 |
// Return the mapping from CIDs to GIDs, and return the number of |
| 77 |
// CIDs in *<nCIDs>. This is only useful for CID fonts. (Only |
| 78 |
// useful for OpenType CFF fonts.) |
| 79 |
int *getCIDToGIDMap(int *nCIDs); |
| 80 |
|
| 81 |
// Returns the least restrictive embedding licensing right (as |
| 82 |
// defined by the TrueType spec): |
| 83 |
// * 4: OS/2 table is missing or invalid |
| 84 |
// * 3: installable embedding |
| 85 |
// * 2: editable embedding |
| 86 |
// * 1: preview & print embedding |
| 87 |
// * 0: restricted license embedding |
| 88 |
int getEmbeddingRights(); |
| 89 |
|
| 90 |
// Return the font matrix as an array of six numbers. (Only useful |
| 91 |
// for OpenType CFF fonts.) |
| 92 |
void getFontMatrix(double *mat); |
| 93 |
|
| 94 |
// Return the number of glyphs in the font. |
| 95 |
int getNumGlyphs() { return nGlyphs; } |
| 96 |
|
| 97 |
// Returns true if this looks like a CJK font that uses bytecode |
| 98 |
// instructions to assemble glyphs. |
| 99 |
GBool checkForTrickyCJK(); |
| 100 |
|
| 101 |
// Convert to a Type 42 font, suitable for embedding in a PostScript |
| 102 |
// file. <psName> will be used as the PostScript font name (so we |
| 103 |
// don't need to depend on the 'name' table in the font). The |
| 104 |
// <encoding> array specifies the mapping from char codes to names. |
| 105 |
// If <encoding> is NULL, the encoding is unknown or undefined. The |
| 106 |
// <codeToGID> array specifies the mapping from char codes to GIDs. |
| 107 |
// (Not useful for OpenType CFF fonts.) |
| 108 |
void convertToType42(char *psName, char **encoding, |
| 109 |
int *codeToGID, |
| 110 |
FoFiOutputFunc outputFunc, void *outputStream); |
| 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(char *psName, const char **newEncoding, GBool ascii, |
| 120 |
FoFiOutputFunc outputFunc, void *outputStream); |
| 121 |
|
| 122 |
// Convert to a Type 2 CIDFont, suitable for embedding in a |
| 123 |
// PostScript file. <psName> will be used as the PostScript font |
| 124 |
// name (so we don't need to depend on the 'name' table in the |
| 125 |
// font). The <cidMap> array maps CIDs to GIDs; it has <nCIDs> |
| 126 |
// entries. (Not useful for OpenType CFF fonts.) |
| 127 |
void convertToCIDType2(char *psName, int *cidMap, int nCIDs, |
| 128 |
GBool needVerticalMetrics, |
| 129 |
FoFiOutputFunc outputFunc, void *outputStream); |
| 130 |
|
| 131 |
// Convert to a Type 0 CIDFont, suitable for embedding in a |
| 132 |
// PostScript file. <psName> will be used as the PostScript font |
| 133 |
// name. (Only useful for OpenType CFF fonts.) |
| 134 |
void convertToCIDType0(char *psName, int *cidMap, int nCIDs, |
| 135 |
FoFiOutputFunc outputFunc, void *outputStream); |
| 136 |
|
| 137 |
// Convert to a Type 0 (but non-CID) composite font, suitable for |
| 138 |
// embedding in a PostScript file. <psName> will be used as the |
| 139 |
// PostScript font name (so we don't need to depend on the 'name' |
| 140 |
// table in the font). The <cidMap> array maps CIDs to GIDs; it has |
| 141 |
// <nCIDs> entries. (Not useful for OpenType CFF fonts.) |
| 142 |
void convertToType0(char *psName, int *cidMap, int nCIDs, |
| 143 |
GBool needVerticalMetrics, |
| 144 |
FoFiOutputFunc outputFunc, void *outputStream); |
| 145 |
|
| 146 |
// Convert to a Type 0 (but non-CID) composite font, suitable for |
| 147 |
// embedding in a PostScript file. <psName> will be used as the |
| 148 |
// PostScript font name. (Only useful for OpenType CFF fonts.) |
| 149 |
void convertToType0(char *psName, int *cidMap, int nCIDs, |
| 150 |
FoFiOutputFunc outputFunc, void *outputStream); |
| 151 |
|
| 152 |
// Write a clean TTF file, filling in missing tables and correcting |
| 153 |
// various other errors. If <name> is non-NULL, the font is renamed |
| 154 |
// to <name>. If <codeToGID> is non-NULL, the font is re-encoded, |
| 155 |
// using a Windows Unicode cmap. If <name> is NULL and the font is |
| 156 |
// complete and correct, it will be written unmodified. If |
| 157 |
// <replacementCmapTable> is non-NULL it will be used as the cmap |
| 158 |
// table in the written font (overriding any existing cmap table |
| 159 |
// and/or the codeToGID arg). (Not useful for OpenType CFF fonts.) |
| 160 |
// Returns true if the font was modified. |
| 161 |
GBool writeTTF(FoFiOutputFunc outputFunc, void *outputStream, |
| 162 |
char *name = NULL, int *codeToGID = NULL, |
| 163 |
Guchar *replacementCmapTable = NULL, |
| 164 |
int replacementCmapTableLen = 0); |
| 165 |
|
| 166 |
// Returns a pointer to the CFF font embedded in this OpenType font. |
| 167 |
// If successful, sets *<start> and *<length>, and returns true. |
| 168 |
// Otherwise returns false. (Only useful for OpenType CFF fonts). |
| 169 |
GBool getCFFBlock(char **start, int *length); |
| 170 |
|
| 171 |
private: |
| 172 |
|
| 173 |
FoFiTrueType(char *fileA, int lenA, GBool freeFileDataA, |
| 174 |
int fontNum, GBool isDfont, GBool allowHeadlessCFF); |
| 175 |
void cvtEncoding(char **encoding, |
| 176 |
FoFiOutputFunc outputFunc, |
| 177 |
void *outputStream); |
| 178 |
void cvtCharStrings(char **encoding, |
| 179 |
int *codeToGID, |
| 180 |
FoFiOutputFunc outputFunc, |
| 181 |
void *outputStream); |
| 182 |
void cvtSfnts(FoFiOutputFunc outputFunc, |
| 183 |
void *outputStream, GString *name, |
| 184 |
GBool needVerticalMetrics, |
| 185 |
int *maxUsedGlyph); |
| 186 |
void dumpString(Guchar *s, int length, |
| 187 |
FoFiOutputFunc outputFunc, |
| 188 |
void *outputStream); |
| 189 |
Guint computeTableChecksum(Guchar *data, int length); |
| 190 |
void parse(int fontNum, GBool allowHeadlessCFF); |
| 191 |
void parseTTC(int fontNum, int *pos); |
| 192 |
void parseDfont(int fontNum, int *offset, int *pos); |
| 193 |
void readPostTable(); |
| 194 |
int seekTable(const char *tag); |
| 195 |
|
| 196 |
TrueTypeTable *tables; |
| 197 |
int nTables; |
| 198 |
TrueTypeCmap *cmaps; |
| 199 |
int nCmaps; |
| 200 |
int nGlyphs; |
| 201 |
int locaFmt; |
| 202 |
int bbox[4]; |
| 203 |
GHash *nameToGID; |
| 204 |
GBool openTypeCFF; |
| 205 |
GBool headlessCFF; |
| 206 |
GBool isDfont; |
| 207 |
GBool isTTC; |
| 208 |
|
| 209 |
GBool parsedOk; |
| 210 |
}; |
| 211 |
|
| 212 |
#endif |