pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jahew...@apache.org
Subject svn commit: r1717510 - in /pdfbox/trunk: fontbox/src/main/java/org/apache/fontbox/cff/ fontbox/src/main/java/org/apache/fontbox/ttf/ pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/
Date Tue, 01 Dec 2015 20:08:47 GMT
Author: jahewson
Date: Tue Dec  1 20:08:46 2015
New Revision: 1717510

URL: http://svn.apache.org/viewvc?rev=1717510&view=rev
Log:
PDFBOX-3131: Expose CFF font data without storing it in memory

Modified:
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFFont.java
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFParser.java
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/CFFTable.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1CFont.java

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFFont.java?rev=1717510&r1=1717509&r2=1717510&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFFont.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFFont.java Tue Dec  1 20:08:46
2015
@@ -39,7 +39,7 @@ public abstract class CFFFont implements
     protected CFFCharset charset;
     protected final List<byte[]> charStrings = new ArrayList<byte[]>();
     protected List<byte[]> globalSubrIndex;
-    private byte[] data;
+    private CFFParser.ByteSource source;
 
     /**
      * The name of the font.
@@ -128,6 +128,22 @@ public abstract class CFFFont implements
     {
         return Collections.unmodifiableList(charStrings);
     }
+
+    /**
+     * Sets a byte source to re-read the CFF data in the future.
+     */
+    final void setData(CFFParser.ByteSource source)
+    {
+        this.source = source;
+    }
+    
+    /**
+     * Returns the CFF data.
+     */
+    public byte[] getData() throws IOException
+    {
+        return source.getBytes();
+    }
     
     /**
      * Returns the number of charstrings in the font.

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFParser.java?rev=1717510&r1=1717509&r2=1717510&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFParser.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CFFParser.java Tue Dec  1 20:08:46
2015
@@ -41,12 +41,39 @@ public class CFFParser
     private List<String> nameIndex = null;
     private List<byte[]> topDictIndex = null;
     private List<String> stringIndex = null;
+    private ByteSource source;
     
     // for debugging only
-    private String debugFontName; 
+    private String debugFontName;
 
     /**
-     * Parsing CFF Font using a byte array as input.
+     * Source from which bytes may be read in the future.
+     */
+    public interface ByteSource
+    {
+        /**
+         * Returns the source bytes. May be called more than once.
+         */
+        byte[] getBytes() throws IOException;
+    }
+
+    /**
+     * Parse CFF font using byte array, also passing in a byte source for future use.
+     * 
+     * @param bytes source bytes
+     * @param source source to re-read bytes from in the future
+     * @return the parsed CFF fonts
+     * @throws IOException If there is an error reading from the stream
+     */
+    public List<CFFFont> parse(byte[] bytes, ByteSource source) throws IOException
+    {
+        this.source = source;
+        return parse(bytes);
+    }
+    
+    /**
+     * Parse CFF font using a byte array as input.
+     * 
      * @param bytes the given byte array
      * @return the parsed CFF fonts
      * @throws IOException If there is an error reading from the stream
@@ -115,6 +142,7 @@ public class CFFParser
         {
             CFFFont font = parseFont(i);
             font.setGlobalSubrIndex(globalSubrIndex);
+            font.setData(source);
             fonts.add(font);
         }
         return fonts;

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/CFFTable.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/CFFTable.java?rev=1717510&r1=1717509&r2=1717510&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/CFFTable.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/CFFTable.java Tue Dec  1 20:08:46
2015
@@ -50,7 +50,7 @@ public class CFFTable extends TTFTable
         byte[] bytes = data.read((int)getLength());
 
         CFFParser parser = new CFFParser();
-        cffFont = parser.parse(bytes).get(0);
+        cffFont = parser.parse(bytes, new ByteSource(font)).get(0);
 
         initialized = true;
     }
@@ -62,4 +62,23 @@ public class CFFTable extends TTFTable
     {
         return cffFont;
     }
+    
+    /**
+     * Allows bytes to be re-read later by CFFParser.
+     */
+    private static class ByteSource implements CFFParser.ByteSource
+    {
+        private final TrueTypeFont ttf;
+        
+        ByteSource(TrueTypeFont ttf)
+        {
+           this.ttf = ttf; 
+        }
+        
+        @Override
+        public byte[] getBytes() throws IOException
+        {
+            return ttf.getTableBytes(ttf.getTableMap().get(CFFTable.TAG));
+        }
+    }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0.java?rev=1717510&r1=1717509&r2=1717510&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0.java Tue
Dec  1 20:08:46 2015
@@ -96,7 +96,7 @@ public class PDCIDFontType0 extends PDCI
             CFFParser cffParser = new CFFParser();
             try
             {
-                cffFont = cffParser.parse(bytes).get(0);
+                cffFont = cffParser.parse(bytes, new ByteSource()).get(0);
             }
             catch (IOException e)
             {
@@ -188,6 +188,16 @@ public class PDCIDFontType0 extends PDCI
         }
         return fontMatrix;
     }
+    
+    private class ByteSource implements CFFParser.ByteSource
+    {
+        @Override
+        public byte[] getBytes() throws IOException
+        {
+            PDStream ff3Stream = getFontDescriptor().getFontFile3();
+            return IOUtils.toByteArray(ff3Stream.createInputStream());
+        }
+    }
 
     @Override
     public BoundingBox getBoundingBox()

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1CFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1CFont.java?rev=1717510&r1=1717509&r2=1717510&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1CFont.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1CFont.java Tue
Dec  1 20:08:46 2015
@@ -99,7 +99,7 @@ public class PDType1CFont extends PDSimp
             {
                 // note: this could be an OpenType file, fortunately CFFParser can handle
that
                 CFFParser cffParser = new CFFParser();
-                cffEmbedded = (CFFType1Font)cffParser.parse(bytes).get(0);
+                cffEmbedded = (CFFType1Font)cffParser.parse(bytes, new ByteSource()).get(0);
             }
         }
         catch (IOException e)
@@ -131,6 +131,16 @@ public class PDType1CFont extends PDSimp
         fontMatrixTransform = getFontMatrix().createAffineTransform();
         fontMatrixTransform.scale(1000, 1000);
     }
+    
+    private class ByteSource implements CFFParser.ByteSource
+    {
+        @Override
+        public byte[] getBytes() throws IOException
+        {
+            PDStream ff3Stream = getFontDescriptor().getFontFile3();
+            return IOUtils.toByteArray(ff3Stream.createInputStream());
+        }
+    }
 
     @Override
     public FontBoxFont getFontBoxFont()



Mime
View raw message