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 2015/11/05 22:58:07 UTC

svn commit: r1712873 - /pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyphTable.java

Author: tilman
Date: Thu Nov  5 21:58:06 2015
New Revision: 1712873

URL: http://svn.apache.org/viewvc?rev=1712873&view=rev
Log:
PDFBOX-3088: set some limits to caching to avoid using too much memory

Modified:
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyphTable.java

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyphTable.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyphTable.java?rev=1712873&r1=1712872&r2=1712873&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyphTable.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyphTable.java Thu Nov  5 21:58:06 2015
@@ -36,6 +36,18 @@ public class GlyphTable extends TTFTable
     private TTFDataStream data;
     private IndexToLocationTable loca;
     private int numGlyphs;
+    
+    private int cached = 0;
+    
+    /**
+     * Don't even bother to cache huge fonts.
+     */
+    private static final int MAX_CACHE_SIZE = 5000;
+    
+    /**
+     * Don't cache more glyphs than this.
+     */
+    private static final int MAX_CACHED_GLYPHS = 100;
 
     GlyphTable(TrueTypeFont font)
     {
@@ -53,9 +65,15 @@ public class GlyphTable extends TTFTable
     {
         loca = ttf.getIndexToLocation();
         numGlyphs = ttf.getNumberOfGlyphs();
-        glyphs = new GlyphData[numGlyphs];
 
-        // we don't actually read the table yet because it can contain tens of thousands of glyphs
+        if (numGlyphs < MAX_CACHE_SIZE)
+        {
+            // don't cache the huge fonts to save memory
+            glyphs = new GlyphData[numGlyphs];
+            cached = 0;
+        }
+
+        // we don't actually read the complete table here because it can contain tens of thousands of glyphs
         this.data = data;
         initialized = true;
     }
@@ -145,7 +163,7 @@ public class GlyphTable extends TTFTable
             return null;
         }
         
-        if (glyphs[gid] != null)
+        if (glyphs != null && glyphs[gid] != null)
         {
             return glyphs[gid];
         }
@@ -183,7 +201,12 @@ public class GlyphTable extends TTFTable
 
             // restore
             data.seek(currentPosition);
-            glyphs[gid] = glyph;
+
+            if (glyphs != null && cached < MAX_CACHED_GLYPHS)
+            {
+                glyphs[gid] = glyph;
+                ++cached;
+            }            
             return glyph;
         }
     }