]> www.fi.muni.cz Git - evince.git/blob - pdf/splash/SplashFontEngine.cc
Initial revision
[evince.git] / pdf / splash / SplashFontEngine.cc
1 //========================================================================
2 //
3 // SplashFontEngine.cc
4 //
5 //========================================================================
6
7 #include <aconf.h>
8
9 #ifdef USE_GCC_PRAGMAS
10 #pragma implementation
11 #endif
12
13 #if HAVE_T1LIB_H
14 #include <t1lib.h>
15 #endif
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #ifndef WIN32
20 #  include <unistd.h>
21 #endif
22 #include "gmem.h"
23 #include "GString.h"
24 #include "SplashT1FontEngine.h"
25 #include "SplashFTFontEngine.h"
26 #include "SplashFontFile.h"
27 #include "SplashFontFileID.h"
28 #include "SplashFont.h"
29 #include "SplashFontEngine.h"
30
31 #ifdef VMS
32 #if (__VMS_VER < 70000000)
33 extern "C" int unlink(char *filename);
34 #endif
35 #endif
36
37 //------------------------------------------------------------------------
38 // SplashFontEngine
39 //------------------------------------------------------------------------
40
41 SplashFontEngine::SplashFontEngine(
42 #if HAVE_T1LIB_H
43                                    GBool enableT1lib,
44 #endif
45 #if HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H
46                                    GBool enableFreeType,
47 #endif
48                                    GBool aa) {
49   int i;
50
51   for (i = 0; i < splashFontCacheSize; ++i) {
52     fontCache[i] = NULL;
53   }
54
55 #if HAVE_T1LIB_H
56   if (enableT1lib) {
57     t1Engine = SplashT1FontEngine::init(aa);
58   } else {
59     t1Engine = NULL;
60   }
61 #endif
62 #if HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H
63   if (enableFreeType) {
64     ftEngine = SplashFTFontEngine::init(aa);
65   } else {
66     ftEngine = NULL;
67   }
68 #endif
69 }
70
71 SplashFontEngine::~SplashFontEngine() {
72   int i;
73
74   for (i = 0; i < splashFontCacheSize; ++i) {
75     if (fontCache[i]) {
76       delete fontCache[i];
77     }
78   }
79
80 #if HAVE_T1LIB_H
81   if (t1Engine) {
82     delete t1Engine;
83   }
84 #endif
85 #if HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H
86   if (ftEngine) {
87     delete ftEngine;
88   }
89 #endif
90 }
91
92 SplashFontFile *SplashFontEngine::getFontFile(SplashFontFileID *id) {
93   SplashFontFile *fontFile;
94   int i;
95
96   for (i = 0; i < splashFontCacheSize; ++i) {
97     if (fontCache[i]) {
98       fontFile = fontCache[i]->getFontFile();
99       if (fontFile && fontFile->getID()->matches(id)) {
100         return fontFile;
101       }
102     }
103   }
104   return NULL;
105 }
106
107 SplashFontFile *SplashFontEngine::loadType1Font(SplashFontFileID *idA,
108                                                 char *fileName,
109                                                 GBool deleteFile, char **enc) {
110   SplashFontFile *fontFile;
111
112   fontFile = NULL;
113 #if HAVE_T1LIB_H
114   if (!fontFile && t1Engine) {
115     fontFile = t1Engine->loadType1Font(idA, fileName, deleteFile, enc);
116   }
117 #endif
118 #if HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H
119   if (!fontFile && ftEngine) {
120     fontFile = ftEngine->loadType1Font(idA, fileName, deleteFile, enc);
121   }
122 #endif
123
124   // delete the (temporary) font file -- with Unix hard link
125   // semantics, this will remove the last link; otherwise it will
126   // return an error, leaving the file to be deleted later (if
127   // loadXYZFont failed, the file will always be deleted)
128   if (deleteFile) {
129     unlink(fontFile ? fontFile->fileName->getCString() : fileName);
130   }
131
132   return fontFile;
133 }
134
135 SplashFontFile *SplashFontEngine::loadType1CFont(SplashFontFileID *idA,
136                                                  char *fileName,
137                                                  GBool deleteFile,
138                                                  char **enc) {
139   SplashFontFile *fontFile;
140
141   fontFile = NULL;
142 #if HAVE_T1LIB_H
143   if (!fontFile && t1Engine) {
144     fontFile = t1Engine->loadType1CFont(idA, fileName, deleteFile, enc);
145   }
146 #endif
147 #if HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H
148   if (!fontFile && ftEngine) {
149     fontFile = ftEngine->loadType1CFont(idA, fileName, deleteFile, enc);
150   }
151 #endif
152
153   // delete the (temporary) font file -- with Unix hard link
154   // semantics, this will remove the last link; otherwise it will
155   // return an error, leaving the file to be deleted later (if
156   // loadXYZFont failed, the file will always be deleted)
157   if (deleteFile) {
158     unlink(fontFile ? fontFile->fileName->getCString() : fileName);
159   }
160
161   return fontFile;
162 }
163
164 SplashFontFile *SplashFontEngine::loadCIDFont(SplashFontFileID *idA,
165                                               char *fileName,
166                                               GBool deleteFile) {
167   SplashFontFile *fontFile;
168
169   fontFile = NULL;
170 #if HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H
171   if (!fontFile && ftEngine) {
172     fontFile = ftEngine->loadCIDFont(idA, fileName, deleteFile);
173   }
174 #endif
175
176   // delete the (temporary) font file -- with Unix hard link
177   // semantics, this will remove the last link; otherwise it will
178   // return an error, leaving the file to be deleted later (if
179   // loadXYZFont failed, the file will always be deleted)
180   if (deleteFile) {
181     unlink(fontFile ? fontFile->fileName->getCString() : fileName);
182   }
183
184   return fontFile;
185 }
186
187 SplashFontFile *SplashFontEngine::loadTrueTypeFont(SplashFontFileID *idA,
188                                                    char *fileName,
189                                                    GBool deleteFile,
190                                                    Gushort *codeToGID,
191                                                    int codeToGIDLen) {
192   SplashFontFile *fontFile;
193
194   fontFile = NULL;
195 #if HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H
196   if (!fontFile && ftEngine) {
197     fontFile = ftEngine->loadTrueTypeFont(idA, fileName, deleteFile,
198                                           codeToGID, codeToGIDLen);
199   }
200 #endif
201
202   if (!fontFile) {
203     gfree(codeToGID);
204   }
205
206   // delete the (temporary) font file -- with Unix hard link
207   // semantics, this will remove the last link; otherwise it will
208   // return an error, leaving the file to be deleted later (if
209   // loadXYZFont failed, the file will always be deleted)
210   if (deleteFile) {
211     unlink(fontFile ? fontFile->fileName->getCString() : fileName);
212   }
213
214   return fontFile;
215 }
216
217 SplashFont *SplashFontEngine::getFont(SplashFontFile *fontFile,
218                                       SplashCoord *mat) {
219   SplashFont *font;
220   int i, j;
221
222   font = fontCache[0];
223   if (font && font->matches(fontFile, mat)) {
224     return font;
225   }
226   for (i = 1; i < splashFontCacheSize; ++i) {
227     font = fontCache[i];
228     if (font && font->matches(fontFile, mat)) {
229       for (j = i; j > 0; --j) {
230         fontCache[j] = fontCache[j-1];
231       }
232       fontCache[0] = font;
233       return font;
234     }
235   }
236   font = fontFile->makeFont(mat);
237   if (fontCache[splashFontCacheSize - 1]) {
238     delete fontCache[splashFontCacheSize - 1];
239   }
240   for (j = splashFontCacheSize - 1; j > 0; --j) {
241     fontCache[j] = fontCache[j-1];
242   }
243   fontCache[0] = font;
244   return font;
245 }