1 //========================================================================
2 //
3 // SplashFTFontEngine.cc
4 //
5 //========================================================================
6 
7 //========================================================================
8 //
9 // Modified under the Poppler project - http://poppler.freedesktop.org
10 //
11 // All changes made under the Poppler project to this file are licensed
12 // under GPL version 2 or later
13 //
14 // Copyright (C) 2006 Takashi Iwai <tiwai@suse.de>
15 // Copyright (C) 2009, 2011, 2012 Albert Astals Cid <aacid@kde.org>
16 // Copyright (C) 2009 Petr Gajdos <pgajdos@novell.com>
17 // Copyright (C) 2011 Andreas Hartmetz <ahartmetz@gmail.com>
18 //
19 // To see a description of the changes please see the Changelog file that
20 // came with your tarball or type make ChangeLog if you are building from git
21 //
22 //========================================================================
23 
24 #include <config.h>
25 
26 #if HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H
27 
28 #ifdef USE_GCC_PRAGMAS
29 #pragma implementation
30 #endif
31 
32 #include <stdio.h>
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #include "goo/gmem.h"
37 #include "goo/GooString.h"
38 #include "goo/gfile.h"
39 #include "fofi/FoFiTrueType.h"
40 #include "fofi/FoFiType1C.h"
41 #include "SplashFTFontFile.h"
42 #include "SplashFTFontEngine.h"
43 
44 #ifdef VMS
45 #if (__VMS_VER < 70000000)
46 extern "C" int unlink(char *filename);
47 #endif
48 #endif
49 
50 //------------------------------------------------------------------------
51 // SplashFTFontEngine
52 //------------------------------------------------------------------------
53 
SplashFTFontEngine(GBool aaA,GBool enableFreeTypeHintingA,GBool enableSlightHintingA,FT_Library libA)54 SplashFTFontEngine::SplashFTFontEngine(GBool aaA, GBool enableFreeTypeHintingA,
55 				       GBool enableSlightHintingA, FT_Library libA) {
56   FT_Int major, minor, patch;
57 
58   aa = aaA;
59   enableFreeTypeHinting = enableFreeTypeHintingA;
60   enableSlightHinting = enableSlightHintingA;
61   lib = libA;
62 
63   // as of FT 2.1.8, CID fonts are indexed by CID instead of GID
64   FT_Library_Version(lib, &major, &minor, &patch);
65   useCIDs = major > 2 ||
66             (major == 2 && (minor > 1 || (minor == 1 && patch > 7)));
67 }
68 
init(GBool aaA,GBool enableFreeTypeHintingA,GBool enableSlightHintingA)69 SplashFTFontEngine *SplashFTFontEngine::init(GBool aaA, GBool enableFreeTypeHintingA,
70 					     GBool enableSlightHintingA) {
71   FT_Library libA;
72 
73   if (FT_Init_FreeType(&libA)) {
74     return NULL;
75   }
76   return new SplashFTFontEngine(aaA, enableFreeTypeHintingA, enableSlightHintingA, libA);
77 }
78 
~SplashFTFontEngine()79 SplashFTFontEngine::~SplashFTFontEngine() {
80   FT_Done_FreeType(lib);
81 }
82 
loadType1Font(SplashFontFileID * idA,SplashFontSrc * src,const char ** enc)83 SplashFontFile *SplashFTFontEngine::loadType1Font(SplashFontFileID *idA,
84 						  SplashFontSrc *src,
85 						  const char **enc) {
86   return SplashFTFontFile::loadType1Font(this, idA, src, enc);
87 }
88 
loadType1CFont(SplashFontFileID * idA,SplashFontSrc * src,const char ** enc)89 SplashFontFile *SplashFTFontEngine::loadType1CFont(SplashFontFileID *idA,
90 						   SplashFontSrc *src,
91 						   const char **enc) {
92   return SplashFTFontFile::loadType1Font(this, idA, src, enc);
93 }
94 
loadOpenTypeT1CFont(SplashFontFileID * idA,SplashFontSrc * src,const char ** enc)95 SplashFontFile *SplashFTFontEngine::loadOpenTypeT1CFont(SplashFontFileID *idA,
96 							SplashFontSrc *src,
97 							const char **enc) {
98   return SplashFTFontFile::loadType1Font(this, idA, src, enc);
99 }
100 
loadCIDFont(SplashFontFileID * idA,SplashFontSrc * src)101 SplashFontFile *SplashFTFontEngine::loadCIDFont(SplashFontFileID *idA,
102 						SplashFontSrc *src) {
103   FoFiType1C *ff;
104   int *cidToGIDMap;
105   int nCIDs;
106   SplashFontFile *ret;
107 
108   // check for a CFF font
109   if (useCIDs) {
110     cidToGIDMap = NULL;
111     nCIDs = 0;
112   } else {
113     if (src->isFile) {
114       ff = FoFiType1C::load(src->fileName->getCString());
115     } else {
116       ff = FoFiType1C::make(src->buf, src->bufLen);
117     }
118     if (ff) {
119       cidToGIDMap = ff->getCIDToGIDMap(&nCIDs);
120       delete ff;
121     } else {
122       cidToGIDMap = NULL;
123       nCIDs = 0;
124     }
125   }
126   ret = SplashFTFontFile::loadCIDFont(this, idA, src, cidToGIDMap, nCIDs);
127   if (!ret) {
128     gfree(cidToGIDMap);
129   }
130   return ret;
131 }
132 
loadOpenTypeCFFFont(SplashFontFileID * idA,SplashFontSrc * src,int * codeToGID,int codeToGIDLen)133 SplashFontFile *SplashFTFontEngine::loadOpenTypeCFFFont(SplashFontFileID *idA,
134 							SplashFontSrc *src,
135                                                         int *codeToGID,
136                                                         int codeToGIDLen) {
137   FoFiTrueType *ff;
138   int *cidToGIDMap;
139   int nCIDs;
140   SplashFontFile *ret;
141 
142   cidToGIDMap = NULL;
143   nCIDs = 0;
144   if (!codeToGID) {
145     if (!useCIDs) {
146       if (src->isFile) {
147         ff = FoFiTrueType::load(src->fileName->getCString());
148       } else {
149         ff = FoFiTrueType::make(src->buf, src->bufLen);
150       }
151       if (ff) {
152         if (ff->isOpenTypeCFF()) {
153           cidToGIDMap = ff->getCIDToGIDMap(&nCIDs);
154         }
155         delete ff;
156       }
157     }
158   }
159   ret = SplashFTFontFile::loadCIDFont(this, idA, src,
160                                       codeToGID ? codeToGID : cidToGIDMap,
161                                       codeToGID ? codeToGIDLen : nCIDs);
162   if (!ret) {
163     gfree(cidToGIDMap);
164   }
165   return ret;
166 }
167 
loadTrueTypeFont(SplashFontFileID * idA,SplashFontSrc * src,int * codeToGID,int codeToGIDLen,int faceIndex)168 SplashFontFile *SplashFTFontEngine::loadTrueTypeFont(SplashFontFileID *idA,
169 						     SplashFontSrc *src,
170 						     int *codeToGID,
171 						     int codeToGIDLen,
172 						     int faceIndex) {
173   SplashFontFile *ret;
174   ret = SplashFTFontFile::loadTrueTypeFont(this, idA, src,
175 					   codeToGID, codeToGIDLen,
176 					   faceIndex);
177   return ret;
178 }
179 
180 #endif // HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H
181