1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
//========================================================================
//
// SplashFTFontEngine.cc
//
// Copyright 2003-2013 Glyph & Cog, LLC
//
//========================================================================
#include <aconf.h>
#if HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H
#ifdef USE_GCC_PRAGMAS
#pragma implementation
#endif
#include <stdio.h>
#ifndef _WIN32
# include <unistd.h>
#endif
#include "gmem.h"
#include "GString.h"
#include "gfile.h"
#include "FoFiTrueType.h"
#include "FoFiType1C.h"
#include "SplashFTFontFile.h"
#include "SplashFTFontEngine.h"
#include FT_MODULE_H
#ifdef FT_CFF_DRIVER_H
# include FT_CFF_DRIVER_H
#endif
#ifdef VMS
#if (__VMS_VER < 70000000)
extern "C" int unlink(char *filename);
#endif
#endif
//------------------------------------------------------------------------
static void fileWrite(void *stream, const char *data, int len) {
fwrite(data, 1, len, (FILE *)stream);
}
#if LOAD_FONTS_FROM_MEM
static void gstringWrite(void *stream, const char *data, int len) {
((GString *)stream)->append(data, len);
}
#endif
//------------------------------------------------------------------------
// SplashFTFontEngine
//------------------------------------------------------------------------
SplashFTFontEngine::SplashFTFontEngine(GBool aaA, Guint flagsA,
FT_Library libA) {
FT_Int major, minor, patch;
aa = aaA;
flags = flagsA;
lib = libA;
// as of FT 2.1.8, CID fonts are indexed by CID instead of GID
FT_Library_Version(lib, &major, &minor, &patch);
useCIDs = major > 2 ||
(major == 2 && (minor > 1 || (minor == 1 && patch > 7)));
}
SplashFTFontEngine *SplashFTFontEngine::init(GBool aaA, Guint flagsA) {
FT_Library libA;
if (FT_Init_FreeType(&libA)) {
return NULL;
}
return new SplashFTFontEngine(aaA, flagsA, libA);
}
SplashFTFontEngine::~SplashFTFontEngine() {
FT_Done_FreeType(lib);
}
SplashFontFile *SplashFTFontEngine::loadType1Font(SplashFontFileID *idA,
#if LOAD_FONTS_FROM_MEM
GString *fontBuf,
#else
char *fileName,
GBool deleteFile,
#endif
const char **enc) {
return SplashFTFontFile::loadType1Font(this, idA,
#if LOAD_FONTS_FROM_MEM
fontBuf,
#else
fileName, deleteFile,
#endif
enc, gTrue);
}
SplashFontFile *SplashFTFontEngine::loadType1CFont(SplashFontFileID *idA,
#if LOAD_FONTS_FROM_MEM
GString *fontBuf,
#else
char *fileName,
GBool deleteFile,
#endif
const char **enc) {
return SplashFTFontFile::loadType1Font(this, idA,
#if LOAD_FONTS_FROM_MEM
fontBuf,
#else
fileName, deleteFile,
#endif
enc, gFalse);
}
SplashFontFile *SplashFTFontEngine::loadOpenTypeT1CFont(SplashFontFileID *idA,
#if LOAD_FONTS_FROM_MEM
GString *fontBuf,
#else
char *fileName,
GBool deleteFile,
#endif
const char **enc) {
FoFiTrueType *ff;
#if LOAD_FONTS_FROM_MEM
GString *fontBuf2;
#else
GString *tmpFileName;
FILE *tmpFile;
#endif
SplashFontFile *ret;
#if LOAD_FONTS_FROM_MEM
if (!(ff = FoFiTrueType::make(fontBuf->getCString(), fontBuf->getLength(),
0, gTrue))) {
#else
if (!(ff = FoFiTrueType::load(fileName, 0, gTrue))) {
#endif
return NULL;
}
if (ff->isHeadlessCFF()) {
#if LOAD_FONTS_FROM_MEM
fontBuf2 = new GString();
ff->convertToType1(NULL, enc, gFalse, &gstringWrite, fontBuf2);
delete ff;
ret = SplashFTFontFile::loadType1Font(this, idA, fontBuf2, enc,
gFalse);
if (ret) {
delete fontBuf;
} else {
delete fontBuf2;
}
#else
tmpFileName = NULL;
if (!openTempFile(&tmpFileName, &tmpFile, "wb", NULL)) {
delete ff;
return NULL;
}
ff->convertToType1(NULL, enc, gFalse, &fileWrite, tmpFile);
delete ff;
fclose(tmpFile);
ret = SplashFTFontFile::loadType1Font(this, idA, tmpFileName->getCString(),
gTrue, enc, gFalse);
if (ret) {
if (deleteFile) {
unlink(fileName);
}
} else {
unlink(tmpFileName->getCString());
}
delete tmpFileName;
#endif
} else {
delete ff;
ret = SplashFTFontFile::loadType1Font(this, idA,
#if LOAD_FONTS_FROM_MEM
fontBuf,
#else
fileName, deleteFile,
#endif
enc, gFalse);
}
return ret;
}
SplashFontFile *SplashFTFontEngine::loadCIDFont(SplashFontFileID *idA,
#if LOAD_FONTS_FROM_MEM
GString *fontBuf
#else
char *fileName,
GBool deleteFile
#endif
) {
FoFiType1C *ff;
int *cidToGIDMap;
int nCIDs;
SplashFontFile *ret;
// check for a CFF font
if (useCIDs) {
cidToGIDMap = NULL;
nCIDs = 0;
#if LOAD_FONTS_FROM_MEM
} else if ((ff = FoFiType1C::make(fontBuf->getCString(),
fontBuf->getLength()))) {
#else
} else if ((ff = FoFiType1C::load(fileName))) {
#endif
cidToGIDMap = ff->getCIDToGIDMap(&nCIDs);
delete ff;
} else {
cidToGIDMap = NULL;
nCIDs = 0;
}
ret = SplashFTFontFile::loadCIDFont(this, idA,
#if LOAD_FONTS_FROM_MEM
fontBuf,
#else
fileName, deleteFile,
#endif
cidToGIDMap, nCIDs);
if (!ret) {
gfree(cidToGIDMap);
}
return ret;
}
SplashFontFile *SplashFTFontEngine::loadOpenTypeCFFFont(SplashFontFileID *idA,
#if LOAD_FONTS_FROM_MEM
GString *fontBuf,
#else
char *fileName,
GBool deleteFile,
#endif
int *codeToGID,
int codeToGIDLen) {
FoFiTrueType *ff;
#if LOAD_FONTS_FROM_MEM
GString *fontBuf2;
#else
GString *tmpFileName;
FILE *tmpFile;
#endif
char *cffStart;
int cffLength;
int *cidToGIDMap;
int nCIDs;
SplashFontFile *ret;
#if LOAD_FONTS_FROM_MEM
if (!(ff = FoFiTrueType::make(fontBuf->getCString(), fontBuf->getLength(),
0, gTrue))) {
#else
if (!(ff = FoFiTrueType::load(fileName, 0, gTrue))) {
#endif
return NULL;
}
cidToGIDMap = NULL;
nCIDs = 0;
if (ff->isHeadlessCFF()) {
if (!ff->getCFFBlock(&cffStart, &cffLength)) {
return NULL;
}
#if LOAD_FONTS_FROM_MEM
fontBuf2 = new GString(cffStart, cffLength);
if (!useCIDs) {
cidToGIDMap = ff->getCIDToGIDMap(&nCIDs);
}
ret = SplashFTFontFile::loadCIDFont(this, idA, fontBuf2,
cidToGIDMap, nCIDs);
if (ret) {
delete fontBuf;
} else {
delete fontBuf2;
}
#else
tmpFileName = NULL;
if (!openTempFile(&tmpFileName, &tmpFile, "wb", NULL)) {
delete ff;
return NULL;
}
fwrite(cffStart, 1, cffLength, tmpFile);
fclose(tmpFile);
if (!useCIDs) {
cidToGIDMap = ff->getCIDToGIDMap(&nCIDs);
}
ret = SplashFTFontFile::loadCIDFont(this, idA,
tmpFileName->getCString(), gTrue,
cidToGIDMap, nCIDs);
if (ret) {
if (deleteFile) {
unlink(fileName);
}
} else {
unlink(tmpFileName->getCString());
}
delete tmpFileName;
#endif
} else {
if (!codeToGID && !useCIDs && ff->isOpenTypeCFF()) {
cidToGIDMap = ff->getCIDToGIDMap(&nCIDs);
}
ret = SplashFTFontFile::loadCIDFont(this, idA,
#if LOAD_FONTS_FROM_MEM
fontBuf,
#else
fileName, deleteFile,
#endif
codeToGID ? codeToGID : cidToGIDMap,
codeToGID ? codeToGIDLen : nCIDs);
}
delete ff;
if (!ret) {
gfree(cidToGIDMap);
}
return ret;
}
SplashFontFile *SplashFTFontEngine::loadTrueTypeFont(SplashFontFileID *idA,
#if LOAD_FONTS_FROM_MEM
GString *fontBuf,
#else
char *fileName,
GBool deleteFile,
#endif
int fontNum,
int *codeToGID,
int codeToGIDLen) {
FoFiTrueType *ff;
#if LOAD_FONTS_FROM_MEM
GString *fontBuf2;
#else
GString *tmpFileName;
FILE *tmpFile;
#endif
SplashFontFile *ret;
#if LOAD_FONTS_FROM_MEM
if (!(ff = FoFiTrueType::make(fontBuf->getCString(), fontBuf->getLength(),
fontNum))) {
#else
if (!(ff = FoFiTrueType::load(fileName, fontNum))) {
#endif
return NULL;
}
#if LOAD_FONTS_FROM_MEM
fontBuf2 = new GString;
ff->writeTTF(&gstringWrite, fontBuf2);
#else
tmpFileName = NULL;
if (!openTempFile(&tmpFileName, &tmpFile, "wb", NULL)) {
delete ff;
return NULL;
}
ff->writeTTF(&fileWrite, tmpFile);
fclose(tmpFile);
#endif
delete ff;
ret = SplashFTFontFile::loadTrueTypeFont(this, idA,
#if LOAD_FONTS_FROM_MEM
fontBuf2,
#else
tmpFileName->getCString(), gTrue,
#endif
0, codeToGID, codeToGIDLen);
#if LOAD_FONTS_FROM_MEM
if (ret) {
delete fontBuf;
} else {
delete fontBuf2;
}
#else
if (ret) {
if (deleteFile) {
unlink(fileName);
}
} else {
unlink(tmpFileName->getCString());
}
delete tmpFileName;
#endif
return ret;
}
#endif // HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H
|