You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2018/07/04 18:22:12 UTC

svn commit: r1835076 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel: PDDocument.java font/PDType0Font.java

Author: tilman
Date: Wed Jul  4 18:22:12 2018
New Revision: 1835076

URL: http://svn.apache.org/viewvc?rev=1835076&view=rev
Log:
PDFBOX-4242: register fonts and close them when closing the document to avoid memory leaks

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=1835076&r1=1835075&r2=1835076&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Wed Jul  4 18:22:12 2018
@@ -35,6 +35,7 @@ import java.util.List;
 import java.util.Set;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.fontbox.ttf.TrueTypeFont;
 import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
@@ -134,7 +135,10 @@ public class PDDocument implements Close
     
     // fonts to subset before saving
     private final Set<PDFont> fontsToSubset = new HashSet<>();
-    
+
+    // fonts to close when closing document
+    private final Set<TrueTypeFont> fontsToClose = new HashSet<>();
+
     // Signature interface
     private SignatureInterface signInterface;
 
@@ -874,6 +878,18 @@ public class PDDocument implements Close
     }
 
     /**
+     * For internal PDFBox use when creating PDF documents: register a TrueTypeFont to make sure it
+     * is closed when the PDDocument is closed to avoid memory leaks. Users don't have to call this
+     * method, it is done by the appropriate PDFont classes.
+     *
+     * @param ttf
+     */
+    public void registerTrueTypeFont(TrueTypeFont ttf)
+    {
+        fontsToClose.add(ttf);
+    }
+
+    /**
      * Returns the list of fonts which will be subset before the document is saved.
      */
     Set<PDFont> getFontsToSubset()
@@ -1413,6 +1429,12 @@ public class PDDocument implements Close
                 firstException = IOUtils.closeAndLogException(pdfSource, LOG, "RandomAccessRead pdfSource", firstException);
             }
 
+            // close fonts
+            for (TrueTypeFont ttf : fontsToClose)
+            {
+                firstException = IOUtils.closeAndLogException(ttf, LOG, "TrueTypeFont", firstException);
+            }
+
             // rethrow first exception to keep method contract
             if (firstException != null)
             {

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java?rev=1835076&r1=1835075&r2=1835076&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java Wed Jul  4 18:22:12 2018
@@ -92,10 +92,19 @@ public class PDType0Font extends PDFont
     }
 
     /**
-    * Private. Creates a new TrueType font for embedding.
-    */
+     * Private. Creates a new PDType0Font font for embedding.
+     *
+     * @param document
+     * @param ttf
+     * @param embedSubset
+     * @param closeTTF whether to close the ttf parameter after embedding. Must be true when the ttf
+     * parameter was created in the load() method, false when the ttf parameter was passed to the
+     * load() method.
+     * @param vertical
+     * @throws IOException
+     */
     private PDType0Font(PDDocument document, TrueTypeFont ttf, boolean embedSubset,
-            boolean closeOnSubset, boolean vertical) throws IOException
+            boolean closeTTF, boolean vertical) throws IOException
     {
         if (vertical)
         {
@@ -109,11 +118,12 @@ public class PDType0Font extends PDFont
         descendantFont = embedder.getCIDFont();
         readEncoding();
         fetchCMapUCS2();
-        if (closeOnSubset)
+        if (closeTTF)
         {
             if (embedSubset)
             {
                 this.ttf = ttf;
+                document.registerTrueTypeFont(ttf);
             }
             else
             {