File xpdf-3.02-ttf-cid-fix.dif of Package xpdf

--- xpdf-3.01/fofi/FoFiTrueType.cc
+++ xpdf-3.01/fofi/FoFiTrueType.cc
@@ -241,10 +241,10 @@
 // FoFiTrueType
 //------------------------------------------------------------------------
 
-FoFiTrueType *FoFiTrueType::make(char *fileA, int lenA) {
+FoFiTrueType *FoFiTrueType::make(char *fileA, int lenA, int faceIndexA) {
   FoFiTrueType *ff;
 
-  ff = new FoFiTrueType(fileA, lenA, gFalse);
+  ff = new FoFiTrueType(fileA, lenA, gFalse, faceIndexA);
   if (!ff->parsedOk) {
     delete ff;
     return NULL;
@@ -252,7 +252,7 @@
   return ff;
 }
 
-FoFiTrueType *FoFiTrueType::load(char *fileName) {
+FoFiTrueType *FoFiTrueType::load(char *fileName, int faceIndexA) {
   FoFiTrueType *ff;
   char *fileA;
   int lenA;
@@ -260,7 +260,7 @@
   if (!(fileA = FoFiBase::readFile(fileName, &lenA))) {
     return NULL;
   }
-  ff = new FoFiTrueType(fileA, lenA, gTrue);
+  ff = new FoFiTrueType(fileA, lenA, gTrue, faceIndexA);
   if (!ff->parsedOk) {
     delete ff;
     return NULL;
@@ -268,7 +268,7 @@
   return ff;
 }
 
-FoFiTrueType::FoFiTrueType(char *fileA, int lenA, GBool freeFileDataA):
+FoFiTrueType::FoFiTrueType(char *fileA, int lenA, GBool freeFileDataA, int faceIndexA):
   FoFiBase(fileA, lenA, freeFileDataA)
 {
   tables = NULL;
@@ -277,6 +277,7 @@
   nCmaps = 0;
   nameToGID = NULL;
   parsedOk = gFalse;
+  faceIndex = faceIndexA;
 
   parse();
 }
--- xpdf-3.01/fofi/FoFiTrueType.h
+++ xpdf-3.01/fofi/FoFiTrueType.h
@@ -31,10 +31,10 @@
 public:
 
   // Create a FoFiTrueType object from a memory buffer.
-  static FoFiTrueType *make(char *fileA, int lenA);
+  static FoFiTrueType *make(char *fileA, int lenA, int faceIndexA=0);
 
   // Create a FoFiTrueType object from a file on disk.
-  static FoFiTrueType *load(char *fileName);
+  static FoFiTrueType *load(char *fileName, int faceIndexA=0);
 
   virtual ~FoFiTrueType();
 
@@ -106,7 +106,7 @@
 
 private:
 
-  FoFiTrueType(char *fileA, int lenA, GBool freeFileDataA);
+  FoFiTrueType(char *fileA, int lenA, GBool freeFileDataA, int faceIndexA=0);
   void cvtEncoding(char **encoding,
 		   FoFiOutputFunc outputFunc,
 		   void *outputStream);
@@ -135,6 +135,7 @@
   GHash *nameToGID;
 
   GBool parsedOk;
+  int faceIndex;
 };
 
 #endif
--- xpdf-3.01/splash/SplashFTFontEngine.cc
+++ xpdf-3.01/splash/SplashFTFontEngine.cc
@@ -110,13 +110,14 @@
 						     char *fileName,
 						     GBool deleteFile,
 						     Gushort *codeToGID,
-						     int codeToGIDLen) {
+						     int codeToGIDLen,
+						     int faceIndex) {
   FoFiTrueType *ff;
   GString *tmpFileName;
   FILE *tmpFile;
   SplashFontFile *ret;
 
-  if (!(ff = FoFiTrueType::load(fileName))) {
+  if (!(ff = FoFiTrueType::load(fileName, faceIndex))) {
     return NULL;
   }
   tmpFileName = NULL;
--- xpdf-3.01/splash/SplashFTFontEngine.h
+++ xpdf-3.01/splash/SplashFTFontEngine.h
@@ -42,7 +42,8 @@
 			      GBool deleteFile);
   SplashFontFile *loadTrueTypeFont(SplashFontFileID *idA, char *fileName,
 				   GBool deleteFile,
-				   Gushort *codeToGID, int codeToGIDLen);
+				   Gushort *codeToGID, int codeToGIDLen,
+				   int faceIndex=0);
 
 private:
 
--- xpdf-3.01/splash/SplashFontEngine.cc
+++ xpdf-3.01/splash/SplashFontEngine.cc
@@ -194,14 +194,15 @@
 						   char *fileName,
 						   GBool deleteFile,
 						   Gushort *codeToGID,
-						   int codeToGIDLen) {
+						   int codeToGIDLen,
+						   int faceIndex) {
   SplashFontFile *fontFile;
 
   fontFile = NULL;
 #if HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H
   if (!fontFile && ftEngine) {
     fontFile = ftEngine->loadTrueTypeFont(idA, fileName, deleteFile,
-					  codeToGID, codeToGIDLen);
+					  codeToGID, codeToGIDLen, faceIndex);
   }
 #endif
 
--- xpdf-3.01/splash/SplashFontEngine.h
+++ xpdf-3.01/splash/SplashFontEngine.h
@@ -59,7 +59,8 @@
 			      GBool deleteFile);
   SplashFontFile *loadTrueTypeFont(SplashFontFileID *idA, char *fileName,
 				   GBool deleteFile,
-				   Gushort *codeToGID, int codeToGIDLen);
+				   Gushort *codeToGID, int codeToGIDLen,
+				   int faceIndex=0);
 
   // Get a font - this does a cache lookup first, and if not found,
   // creates a new SplashFont object and adds it to the cache.  The
--- xpdf-3.01/xpdf/CharCodeToUnicode.h
+++ xpdf-3.01/xpdf/CharCodeToUnicode.h
@@ -71,6 +71,8 @@
   // code supported by the mapping.
   CharCode getLength() { return mapLen; }
 
+  CharCode getMapLen() { return mapLen; }
+
 private:
 
   void parseCMap1(int (*getCharFunc)(void *), void *data, int nBits);
--- xpdf-3.01/xpdf/GfxFont.cc
+++ xpdf-3.01/xpdf/GfxFont.cc
@@ -319,7 +319,7 @@
 
 void GfxFont::findExtFontFile() {
   static char *type1Exts[] = { ".pfa", ".pfb", ".ps", "", NULL };
-  static char *ttExts[] = { ".ttf", NULL };
+  static char *ttExts[] = { ".ttf", ".ttc", NULL };
 
   if (name) {
     if (type == fontType1) {
@@ -1480,6 +1480,53 @@
   return cMap ? cMap->getCollection() : (GString *)NULL;
 }
 
+Gushort *GfxCIDFont::getCodeToGIDMap(FoFiTrueType *ff, int *mapsizep) {
+  Gushort *map;
+  int cmapPlatform, cmapEncoding;
+  int unicodeCmap, macRomanCmap, msSymbolCmap, cmap;
+  GBool useMacRoman, useUnicode;
+  char *charName;
+  Unicode u;
+  int code, i;
+  int mapsize;
+  int cidlen;
+
+  *mapsizep = 0;
+
+  /* we use only unicode cmap */
+  cmap = -1;
+  for (i = 0; i < ff->getNumCmaps(); ++i) {
+    cmapPlatform = ff->getCmapPlatform(i);
+    cmapEncoding = ff->getCmapEncoding(i);
+    if ((cmapPlatform == 3 && cmapEncoding == 1) || cmapPlatform == 0)
+      cmap = i;
+  }
+  if (cmap < 0)
+    return NULL;
+
+  cidlen = 0;
+  mapsize = 64;
+  map = (Gushort *)gmalloc(mapsize * sizeof(Gushort));
+
+  while (cidlen < ctu->getMapLen()) {
+    int n;
+    if ((n = ctu->mapToUnicode((CharCode)cidlen, &u, 1)) == 0) {
+      cidlen++;
+      continue;
+    }
+    if (cidlen >= mapsize) {
+      while (cidlen >= mapsize)
+	mapsize *= 2;
+      map = (Gushort *)grealloc(map, mapsize * sizeof(Gushort));
+    }
+    map[cidlen] = ff->mapCodeToGID(cmap, u);
+    cidlen++;
+  }
+
+  *mapsizep = cidlen;
+  return map;
+}
+
 //------------------------------------------------------------------------
 // GfxFontDict
 //------------------------------------------------------------------------
--- xpdf-3.01/xpdf/GfxFont.h
+++ xpdf-3.01/xpdf/GfxFont.h
@@ -276,6 +276,8 @@
   Gushort *getCIDToGID() { return cidToGID; }
   int getCIDToGIDLen() { return cidToGIDLen; }
 
+  Gushort *getCodeToGIDMap(FoFiTrueType *ff, int *length);
+
 private:
 
   CMap *cMap;			// char code --> CID
--- xpdf-3.01/xpdf/GlobalParams.cc
+++ xpdf-3.01/xpdf/GlobalParams.cc
@@ -15,6 +15,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <ctype.h>
+#include <sys/stat.h>
 #ifdef ENABLE_PLUGINS
 #  ifndef WIN32
 #    include <dlfcn.h>
@@ -777,6 +778,7 @@
 				    DisplayFontParamKind kind,
 				    GString *fileName, int line) {
   DisplayFontParam *param, *old;
+  struct stat statbuf;
 
   if (tokens->getLength() < 2) {
     goto err1;
@@ -789,12 +791,24 @@
       goto err2;
     }
     param->t1.fileName = ((GString *)tokens->get(2))->copy();
+    if (stat((param->t1.fileName->getCString)(), &statbuf)) {
+      delete param; // silently ignore non-existing files
+      return;
+    }    
     break;
   case displayFontTT:
-    if (tokens->getLength() != 3) {
+    if (tokens->getLength() < 3) {
       goto err2;
     }
     param->tt.fileName = ((GString *)tokens->get(2))->copy();
+    if (stat((param->tt.fileName->getCString)(), &statbuf)) {
+      delete param; // silently ignore non-existing files
+      return;
+    }
+    if (tokens->getLength() > 3)
+      param->tt.faceIndex = atoi(((GString *)tokens->get(3))->getCString());
+    else
+      param->tt.faceIndex = 0;
     break;
   }
 
--- xpdf-3.01/xpdf/GlobalParams.h
+++ xpdf-3.01/xpdf/GlobalParams.h
@@ -54,6 +54,7 @@
 
 struct DisplayFontParamTT {
   GString *fileName;
+  int faceIndex;
 };
 
 class DisplayFontParam {
--- xpdf-3.01/xpdf/SplashOutputDev.cc
+++ xpdf-3.01/xpdf/SplashOutputDev.cc
@@ -987,6 +987,7 @@
   char *name;
   Unicode uBuf[8];
   int c, substIdx, n, code, cmap;
+  int faceIndex = 0;
 
   needFontUpdate = gFalse;
   font = NULL;
@@ -1076,6 +1077,7 @@
       case displayFontTT:
 	fileName = dfp->tt.fileName;
 	fontType = gfxFont->isCIDFont() ? fontCIDType2 : fontTrueType;
+	faceIndex = dfp->tt.faceIndex;
 	break;
       }
     }
@@ -1179,13 +1181,20 @@
 	  codeToGID = (Gushort *)gmallocn(n, sizeof(Gushort));
 	  memcpy(codeToGID, ((GfxCIDFont *)gfxFont)->getCIDToGID(),
 		 n * sizeof(Gushort));
+        } else {
+	  if (!(ff = FoFiTrueType::load(fileName->getCString()))) {
+	    fprintf(stderr, "failture loading cid2 %s\n", fileName->getCString());
+		      goto err2;
+	  }
+	  codeToGID = ((GfxCIDFont *)gfxFont)->getCodeToGIDMap(ff, &n);
+	  delete ff;
 	}
       }
       if (!(fontFile = fontEngine->loadTrueTypeFont(
 			   id,
 			   fileName->getCString(),
 			   fileName == tmpFileName,
-			   codeToGID, n))) {
+			   codeToGID, n, faceIndex))) {
 	error(-1, "Couldn't create a font for '%s'",
 	      gfxFont->getName() ? gfxFont->getName()->getCString()
 	                         : "(unnamed)");
openSUSE Build Service is sponsored by