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:18 UTC
svn commit: r1835077 - in
/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel:
PDDocument.java font/PDType0Font.java
Author: tilman
Date: Wed Jul 4 18:22:18 2018
New Revision: 1835077
URL: http://svn.apache.org/viewvc?rev=1835077&view=rev
Log:
PDFBOX-4242: register fonts and close them when closing the document to avoid memory leaks
Modified:
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=1835077&r1=1835076&r2=1835077&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Wed Jul 4 18:22:18 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;
@@ -145,7 +146,10 @@ public class PDDocument implements Close
// fonts to subset before saving
private final Set<PDFont> fontsToSubset = new HashSet<PDFont>();
-
+
+ // fonts to close when closing document
+ private final Set<TrueTypeFont> fontsToClose = new HashSet<TrueTypeFont>();
+
// Signature interface
private SignatureInterface signInterface;
@@ -894,6 +898,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()
@@ -1444,6 +1460,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/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java?rev=1835077&r1=1835076&r2=1835077&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java Wed Jul 4 18:22:18 2018
@@ -194,8 +194,20 @@ public class PDType0Font extends PDFont
fetchCMapUCS2();
}
+ /**
+ * 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(), 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)
{
@@ -205,11 +217,12 @@ public class PDType0Font extends PDFont
descendantFont = embedder.getCIDFont();
readEncoding();
fetchCMapUCS2();
- if (closeOnSubset)
+ if (closeTTF)
{
if (embedSubset)
{
this.ttf = ttf;
+ document.registerTrueTypeFont(ttf);
}
else
{