# use AROS_LIB/INCLUDES
[AROS-Contrib.git] / arospdf / splash / SplashFTFontEngine.cc
blobb05f53d75cb1635fadbe63d6da0fafbdc0bb7e1f
1 //========================================================================
2 //
3 // SplashFTFontEngine.cc
4 //
5 //========================================================================
7 #include <aconf.h>
9 #if HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
13 #endif
15 #include <stdio.h>
16 #ifndef WIN32
17 # include <unistd.h>
18 #endif
19 #include "gmem.h"
20 #include "GString.h"
21 #include "gfile.h"
22 #include "FoFiTrueType.h"
23 #include "FoFiType1C.h"
24 #include "SplashFTFontFile.h"
25 #include "SplashFTFontEngine.h"
26 #include <aros/debug.h>
27 #ifdef VMS
28 #if (__VMS_VER < 70000000)
29 extern "C" int unlink(char *filename);
30 #endif
31 #endif
33 //------------------------------------------------------------------------
35 static void fileWrite(void *stream, char *data, int len) {
36 fwrite(data, 1, len, (FILE *)stream);
39 //------------------------------------------------------------------------
40 // SplashFTFontEngine
41 //------------------------------------------------------------------------
43 SplashFTFontEngine::SplashFTFontEngine(GBool aaA, FT_Library libA) {
44 FT_Int major, minor, patch;
46 aa = aaA;
47 lib = libA;
49 // as of FT 2.1.8, CID fonts are indexed by CID instead of GID
50 FT_Library_Version(lib, &major, &minor, &patch);
51 useCIDs = major > 2 ||
52 (major == 2 && (minor > 1 || (minor == 1 && patch > 7)));
55 SplashFTFontEngine *SplashFTFontEngine::init(GBool aaA) {
56 FT_Library libA;
58 if (FT_Init_FreeType(&libA)) {
59 return NULL;
61 return new SplashFTFontEngine(aaA, libA);
64 SplashFTFontEngine::~SplashFTFontEngine() {
65 FT_Done_FreeType(lib);
68 SplashFontFile *SplashFTFontEngine::loadType1Font(SplashFontFileID *idA,
69 char *fileName,
70 GBool deleteFile,
71 char **enc) {
72 return SplashFTFontFile::loadType1Font(this, idA, fileName, deleteFile, enc);
75 SplashFontFile *SplashFTFontEngine::loadType1CFont(SplashFontFileID *idA,
76 char *fileName,
77 GBool deleteFile,
78 char **enc) {
79 return SplashFTFontFile::loadType1Font(this, idA, fileName, deleteFile, enc);
82 SplashFontFile *SplashFTFontEngine::loadOpenTypeT1CFont(SplashFontFileID *idA,
83 char *fileName,
84 GBool deleteFile,
85 char **enc) {
86 return SplashFTFontFile::loadType1Font(this, idA, fileName, deleteFile, enc);
89 SplashFontFile *SplashFTFontEngine::loadCIDFont(SplashFontFileID *idA,
90 char *fileName,
91 GBool deleteFile) {
92 FoFiType1C *ff;
93 Gushort *cidToGIDMap;
94 int nCIDs;
95 SplashFontFile *ret;
97 // check for a CFF font
98 if (useCIDs) {
99 cidToGIDMap = NULL;
100 nCIDs = 0;
101 } else if ((ff = FoFiType1C::load(fileName))) {
102 cidToGIDMap = ff->getCIDToGIDMap(&nCIDs);
103 delete ff;
104 } else {
105 cidToGIDMap = NULL;
106 nCIDs = 0;
108 ret = SplashFTFontFile::loadCIDFont(this, idA, fileName, deleteFile,
109 cidToGIDMap, nCIDs);
110 if (!ret) {
111 gfree(cidToGIDMap);
113 return ret;
116 SplashFontFile *SplashFTFontEngine::loadOpenTypeCFFFont(SplashFontFileID *idA,
117 char *fileName,
118 GBool deleteFile) {
119 FoFiTrueType *ff;
120 GBool isCID;
121 Gushort *cidToGIDMap;
122 int nCIDs;
123 SplashFontFile *ret;
125 cidToGIDMap = NULL;
126 nCIDs = 0;
127 isCID = gFalse;
128 if (!useCIDs) {
129 if ((ff = FoFiTrueType::load(fileName))) {
130 if (ff->isOpenTypeCFF()) {
131 cidToGIDMap = ff->getCIDToGIDMap(&nCIDs);
133 delete ff;
136 ret = SplashFTFontFile::loadCIDFont(this, idA, fileName, deleteFile,
137 cidToGIDMap, nCIDs);
138 if (!ret) {
139 gfree(cidToGIDMap);
141 return ret;
144 SplashFontFile *SplashFTFontEngine::loadTrueTypeFont(SplashFontFileID *idA,
145 char *fileName,
146 GBool deleteFile,
147 Gushort *codeToGID,
148 int codeToGIDLen) {
149 FoFiTrueType *ff;
150 GString *tmpFileName;
151 FILE *tmpFile;
152 SplashFontFile *ret;
154 if (!(ff = FoFiTrueType::load(fileName))) {
155 return NULL;
157 tmpFileName = NULL;
158 if (!openTempFile(&tmpFileName, &tmpFile, "wb", NULL)) {
159 delete ff;
160 return NULL;
162 ff->writeTTF(&fileWrite, tmpFile);
163 delete ff;
164 fclose(tmpFile);
165 ret = SplashFTFontFile::loadTrueTypeFont(this, idA,
166 tmpFileName->getCString(),
167 gTrue, codeToGID, codeToGIDLen);
168 if (ret) {
169 if (deleteFile) {
170 unlink(fileName);
172 } else {
173 unlink(tmpFileName->getCString());
175 delete tmpFileName;
176 return ret;
179 #endif // HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H