[poppler] 2 commits - poppler/CairoFontEngine.cc poppler/CairoFontEngine.h

Carlos Garcia Campos carlosgc at kemper.freedesktop.org
Sat Feb 27 09:01:58 PST 2010


 poppler/CairoFontEngine.cc |  152 ++++++++++++++++++++++++---------------------
 poppler/CairoFontEngine.h  |    3 
 2 files changed, 83 insertions(+), 72 deletions(-)

New commits:
commit 32aa9ae7d0087298661829265de00e93398272b3
Author: Jan Kümmel <jan+freedesktop at snorc.org>
Date:   Sat Feb 27 17:58:46 2010 +0100

    [cairo] Omit writing of embedded fonts into tempary files
    
    Fixes bug #26694.

diff --git a/poppler/CairoFontEngine.cc b/poppler/CairoFontEngine.cc
index 1208f7f..4abf263 100644
--- a/poppler/CairoFontEngine.cc
+++ b/poppler/CairoFontEngine.cc
@@ -165,14 +165,21 @@ _ft_done_face_uncached (void *closure)
 static GBool
 _ft_new_face_uncached (FT_Library lib,
 		       const char *filename,
+		       char *font_data,
+		       int font_data_len,
 		       FT_Face *face_out,
 		       cairo_font_face_t **font_face_out)
 {
   FT_Face face;
   cairo_font_face_t *font_face;
 
-  if (FT_New_Face (lib, filename, 0, &face))
-    return gFalse;
+  if (font_data == NULL) {
+    if (FT_New_Face (lib, filename, 0, &face))
+      return gFalse;
+  } else {
+    if (FT_New_Memory_Face (lib, (unsigned char *)font_data, font_data_len, 0, &face))
+      return gFalse;
+  }
 
   font_face = cairo_ft_font_face_create_for_ft_face (face,
 							  FT_LOAD_NO_HINTING |
@@ -257,6 +264,8 @@ _ft_done_face (void *closure)
 static GBool
 _ft_new_face (FT_Library lib,
 	      const char *filename,
+	      char *font_data,
+	      int font_data_len,
 	      FT_Face *face_out,
 	      cairo_font_face_t **font_face_out)
 {
@@ -264,37 +273,46 @@ _ft_new_face (FT_Library lib,
   struct stat st;
   struct _ft_face_data tmpl;
 
-  /* if we fail to mmap the file, just pass it to FreeType instead */
-  tmpl.fd = open (filename, O_RDONLY);
-  if (tmpl.fd == -1)
-    return _ft_new_face_uncached (lib, filename, face_out, font_face_out);
+  tmpl.fd = -1;
 
-  if (fstat (tmpl.fd, &st) == -1) {
-    close (tmpl.fd);
-    return _ft_new_face_uncached (lib, filename, face_out, font_face_out);
-  }
+  if (font_data == NULL) {
+    /* if we fail to mmap the file, just pass it to FreeType instead */
+    tmpl.fd = open (filename, O_RDONLY);
+    if (tmpl.fd == -1)
+      return _ft_new_face_uncached (lib, filename, font_data, font_data_len, face_out, font_face_out);
 
-  tmpl.bytes = (unsigned char *) mmap (NULL, st.st_size,
-				       PROT_READ, MAP_PRIVATE,
-				       tmpl.fd, 0);
-  if (tmpl.bytes == MAP_FAILED) {
-    close (tmpl.fd);
-    return _ft_new_face_uncached (lib, filename, face_out, font_face_out);
+    if (fstat (tmpl.fd, &st) == -1) {
+      close (tmpl.fd);
+      return _ft_new_face_uncached (lib, filename, font_data, font_data_len, face_out, font_face_out);
+    }
+
+    tmpl.bytes = (unsigned char *) mmap (NULL, st.st_size,
+					 PROT_READ, MAP_PRIVATE,
+					 tmpl.fd, 0);
+    if (tmpl.bytes == MAP_FAILED) {
+      close (tmpl.fd);
+      return _ft_new_face_uncached (lib, filename, font_data, font_data_len, face_out, font_face_out);
+    }
+    tmpl.size = st.st_size;
+  } else {
+    tmpl.bytes = (unsigned char*) font_data;
+    tmpl.size = font_data_len;
   }
 
   /* check to see if this is a duplicate of any of the currently open fonts */
   tmpl.lib = lib;
-  tmpl.size = st.st_size;
   tmpl.hash = _djb_hash (tmpl.bytes, tmpl.size);
 
   for (l = _ft_open_faces; l; l = l->next) {
     if (_ft_face_data_equal (l, &tmpl)) {
+      if (tmpl.fd != -1) {
 #if defined(__SUNPRO_CC) && defined(__sun) && defined(__SVR4)
-      munmap ((char*)tmpl.bytes, tmpl.size);
+        munmap ((char*)tmpl.bytes, tmpl.size);
 #else
-      munmap (tmpl.bytes, tmpl.size);
+        munmap (tmpl.bytes, tmpl.size);
 #endif
-      close (tmpl.fd);
+        close (tmpl.fd);
+      }
       *face_out = l->face;
       *font_face_out = cairo_font_face_reference (l->font_face);
       return gTrue;
@@ -306,13 +324,15 @@ _ft_new_face (FT_Library lib,
 			  (FT_Byte *) tmpl.bytes, tmpl.size,
 			  0, &tmpl.face))
   {
+    if (tmpl.fd != -1) {
 #if defined(__SUNPRO_CC) && defined(__sun) && defined(__SVR4)
-    munmap ((char*)tmpl.bytes, tmpl.size);
+      munmap ((char*)tmpl.bytes, tmpl.size);
 #else
-    munmap (tmpl.bytes, tmpl.size);
+      munmap (tmpl.bytes, tmpl.size);
 #endif
 
-    close (tmpl.fd);
+      close (tmpl.fd);
+    }
     return gFalse;
   }
 
@@ -362,10 +382,12 @@ CairoFreeTypeFont *CairoFreeTypeFont::create(GfxFont *gfxFont, XRef *xref,
 					     FT_Library lib, GBool useCIDs) {
   Ref embRef;
   Object refObj, strObj;
-  GooString *tmpFileName, *fileName;
+  GooString *fileName;
+  char *fileNameC;
+  char *font_data;
+  int font_data_len;
   DisplayFontParam *dfp;
-  FILE *tmpFile;
-  int c, i, n;
+  int i, n;
   GfxFontType fontType;
   char **enc;
   char *name;
@@ -381,38 +403,20 @@ CairoFreeTypeFont *CairoFreeTypeFont::create(GfxFont *gfxFont, XRef *xref,
   dfp = NULL;
   codeToGID = NULL;
   codeToGIDLen = 0;
+  font_data = NULL;
+  font_data_len = 0;
+  fileName = NULL;
+  fileNameC = NULL;
 
   GBool substitute = gFalse;
   
   ref = *gfxFont->getID();
   fontType = gfxFont->getType();
 
-  tmpFileName = NULL;
-
   if (gfxFont->getEmbeddedFontID(&embRef)) {
-    if (!openTempFile(&tmpFileName, &tmpFile, "wb")) {
-      error(-1, "Couldn't create temporary font file");
-      goto err2;
-    }
-    
-    refObj.initRef(embRef.num, embRef.gen);
-    refObj.fetch(xref, &strObj);
-    refObj.free();
-    if (!strObj.isStream()) {
-      error(-1, "Embedded font object is wrong type");
-      strObj.free();
-      fclose(tmpFile);
+    font_data = gfxFont->readEmbFontFile(xref, &font_data_len);
+    if (NULL == font_data)
       goto err2;
-    }
-    strObj.streamReset();
-    while ((c = strObj.streamGetChar()) != EOF) {
-      fputc(c, tmpFile);
-    }
-    strObj.streamClose();
-    strObj.free();
-    fclose(tmpFile);
-    fileName = tmpFileName;
-    
   } else if (!(fileName = gfxFont->getExtFontFile())) {
     // look for a display font mapping or a substitute font
     dfp = NULL;
@@ -438,11 +442,15 @@ CairoFreeTypeFont *CairoFreeTypeFont::create(GfxFont *gfxFont, XRef *xref,
     substitute = gTrue;
   }
 
+  if (fileName != NULL) {
+    fileNameC = fileName->getCString();
+  }
+
   switch (fontType) {
   case fontType1:
   case fontType1C:
   case fontType1COT:
-    if (! _ft_new_face (lib, fileName->getCString(), &face, &font_face)) {
+    if (! _ft_new_face (lib, fileNameC, font_data, font_data_len, &face, &font_face)) {
       error(-1, "could not create type1 face");
       goto err2;
     }
@@ -470,7 +478,11 @@ CairoFreeTypeFont *CairoFreeTypeFont::create(GfxFont *gfxFont, XRef *xref,
 		n * sizeof(Gushort));
       }
     } else {
-      ff = FoFiTrueType::load(fileName->getCString());
+      if (font_data != NULL) {
+        ff = FoFiTrueType::make(font_data, font_data_len);
+      } else {
+        ff = FoFiTrueType::load(fileNameC);
+      }
       if (! ff)
 	goto err2;
       codeToGID = ((GfxCIDFont *)gfxFont)->getCodeToGIDMap(ff, &n);
@@ -479,7 +491,12 @@ CairoFreeTypeFont *CairoFreeTypeFont::create(GfxFont *gfxFont, XRef *xref,
     codeToGIDLen = n;
     /* Fall through */
   case fontTrueType:
-    if (!(ff = FoFiTrueType::load(fileName->getCString()))) {
+    if (font_data != NULL) {
+      ff = FoFiTrueType::make(font_data, font_data_len);
+    } else {
+      ff = FoFiTrueType::load(fileNameC);
+    }
+    if (! ff) {
       error(-1, "failed to load truetype font\n");
       goto err2;
     }
@@ -489,7 +506,7 @@ CairoFreeTypeFont *CairoFreeTypeFont::create(GfxFont *gfxFont, XRef *xref,
       codeToGIDLen = 256;
     }
     delete ff;
-    if (! _ft_new_face (lib, fileName->getCString(), &face, &font_face)) {
+    if (! _ft_new_face (lib, fileNameC, font_data, font_data_len, &face, &font_face)) {
       error(-1, "could not create truetype face\n");
       goto err2;
     }
@@ -503,13 +520,18 @@ CairoFreeTypeFont *CairoFreeTypeFont::create(GfxFont *gfxFont, XRef *xref,
 
     if (!useCIDs)
     {
-      if ((ff1c = FoFiType1C::load(fileName->getCString()))) {
+      if (font_data != NULL) {
+        ff1c = FoFiType1C::make(font_data, font_data_len);
+      } else {
+        ff1c = FoFiType1C::load(fileNameC);
+      }
+      if (ff1c) {
         codeToGID = ff1c->getCIDToGIDMap(&codeToGIDLen);
         delete ff1c;
       }
     }
 
-    if (! _ft_new_face (lib, fileName->getCString(), &face, &font_face)) {
+    if (! _ft_new_face (lib, fileNameC, font_data, font_data_len, &face, &font_face)) {
       gfree(codeToGID);
       codeToGID = NULL;
       error(-1, "could not create cid face\n");
@@ -518,19 +540,11 @@ CairoFreeTypeFont *CairoFreeTypeFont::create(GfxFont *gfxFont, XRef *xref,
     break;
     
   default:
-    printf ("font type %d not handled\n", (int)fontType);
+    fprintf (stderr, "font type %d not handled\n", (int)fontType);
     goto err2;
     break;
   }
 
-  // delete the (temporary) font file -- with Unix hard link
-  // semantics, this will remove the last link; otherwise it will
-  // return an error, leaving the file to be deleted later
-  if (fileName == tmpFileName) {
-    unlink (fileName->getCString());
-    delete tmpFileName;
-  }
-
   return new CairoFreeTypeFont(ref,
 		       font_face,
 		       codeToGID, codeToGIDLen,
@@ -538,7 +552,7 @@ CairoFreeTypeFont *CairoFreeTypeFont::create(GfxFont *gfxFont, XRef *xref,
 
  err2:
   /* hmm? */
-  printf ("some font thing failed\n");
+  fprintf (stderr, "some font thing failed\n");
   return NULL;
 }
 
commit 7ba52a32343ca73730a80b64c136e3f03348e7d9
Author: Carlos Garcia Campos <carlosgc at gnome.org>
Date:   Sat Feb 27 17:42:46 2010 +0100

    [cairo] Remove unused 'face' from CairoFreeTypeFont class

diff --git a/poppler/CairoFontEngine.cc b/poppler/CairoFontEngine.cc
index 6ef5e11..1208f7f 100644
--- a/poppler/CairoFontEngine.cc
+++ b/poppler/CairoFontEngine.cc
@@ -347,7 +347,6 @@ _ft_new_face (FT_Library lib,
 
 CairoFreeTypeFont::CairoFreeTypeFont(Ref ref,
 				     cairo_font_face_t *cairo_font_face,
-				     FT_Face face,
 				     Gushort *codeToGID,
 				     int codeToGIDLen,
 				     GBool substitute) : CairoFont(ref,
@@ -355,8 +354,7 @@ CairoFreeTypeFont::CairoFreeTypeFont(Ref ref,
 								   codeToGID,
 								   codeToGIDLen,
 								   substitute,
-								   gTrue),
-							 face(face) { }
+								   gTrue) { }
 
 CairoFreeTypeFont::~CairoFreeTypeFont() { }
 
@@ -534,7 +532,7 @@ CairoFreeTypeFont *CairoFreeTypeFont::create(GfxFont *gfxFont, XRef *xref,
   }
 
   return new CairoFreeTypeFont(ref,
-		       font_face, face,
+		       font_face,
 		       codeToGID, codeToGIDLen,
 		       substitute);
 
diff --git a/poppler/CairoFontEngine.h b/poppler/CairoFontEngine.h
index f156cab..9e9c8ab 100644
--- a/poppler/CairoFontEngine.h
+++ b/poppler/CairoFontEngine.h
@@ -75,9 +75,8 @@ public:
   virtual ~CairoFreeTypeFont();
 
 private:
-  CairoFreeTypeFont(Ref ref, cairo_font_face_t *cairo_font_face, FT_Face face,
+  CairoFreeTypeFont(Ref ref, cairo_font_face_t *cairo_font_face,
 	    Gushort *codeToGID, int codeToGIDLen, GBool substitute);
-  FT_Face face;
 };
 
 //------------------------------------------------------------------------


More information about the poppler mailing list