From 6c65be5cbca3bf27343bc89ef5737f32e2535a90 Mon Sep 17 00:00:00 2001 From: Simon Steiner Date: Thu, 6 Oct 2016 08:56:20 +0000 Subject: [PATCH] FOP-2525: Memory leak present when using Truetype Collection (.ttc) git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1763525 13f79535-47bb-0310-9956-ffa450edef68 --- .../fop/complexscripts/fonts/GlyphDefinitionTable.java | 7 ++++--- .../fop/complexscripts/fonts/GlyphPositioningTable.java | 7 ++++--- .../fop/complexscripts/fonts/GlyphSubstitutionTable.java | 7 ++++--- .../org/apache/fop/complexscripts/fonts/GlyphTable.java | 7 ++++++- .../fonts/OTFAdvancedTypographicTableReader.java | 9 ++++++--- .../fop/complexscripts/scripts/ScriptProcessor.java | 4 +--- .../org/apache/fop/complexscripts/fonts/ttx/TTXFile.java | 8 +++++--- .../scripts/arabic/ArabicJoinersTestCase.java | 3 ++- 8 files changed, 32 insertions(+), 20 deletions(-) diff --git a/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/GlyphDefinitionTable.java b/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/GlyphDefinitionTable.java index aaba89636..e3917561d 100644 --- a/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/GlyphDefinitionTable.java +++ b/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/GlyphDefinitionTable.java @@ -21,6 +21,7 @@ package org.apache.fop.complexscripts.fonts; import java.util.HashMap; import java.util.List; +import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -72,8 +73,8 @@ public class GlyphDefinitionTable extends GlyphTable { * Instantiate a GlyphDefinitionTable object using the specified subtables. * @param subtables a list of identified subtables */ - public GlyphDefinitionTable(List subtables) { - super(null, new HashMap(0)); + public GlyphDefinitionTable(List subtables, Map processors) { + super(null, new HashMap(0), processors); if ((subtables == null) || (subtables.size() == 0)) { throw new AdvancedTypographicTableFormatException("subtables must be non-empty"); } else { @@ -101,7 +102,7 @@ public class GlyphDefinitionTable extends GlyphTable { * @return the reordered (output) glyph sequence */ public GlyphSequence reorderCombiningMarks(GlyphSequence gs, int[] widths, int[][] gpa, String script, String language) { - ScriptProcessor sp = ScriptProcessor.getInstance(script); + ScriptProcessor sp = ScriptProcessor.getInstance(script, processors); return sp.reorderCombiningMarks(this, gs, widths, gpa, script, language); } diff --git a/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/GlyphPositioningTable.java b/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/GlyphPositioningTable.java index fb86dc211..67d09dd85 100644 --- a/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/GlyphPositioningTable.java +++ b/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/GlyphPositioningTable.java @@ -71,8 +71,9 @@ public class GlyphPositioningTable extends GlyphTable { * @param lookups a map of lookup specifications to subtable identifier strings * @param subtables a list of identified subtables */ - public GlyphPositioningTable(GlyphDefinitionTable gdef, Map lookups, List subtables) { - super(gdef, lookups); + public GlyphPositioningTable(GlyphDefinitionTable gdef, Map lookups, List subtables, + Map processors) { + super(gdef, lookups, processors); if ((subtables == null) || (subtables.size() == 0)) { throw new AdvancedTypographicTableFormatException("subtables must be non-empty"); } else { @@ -234,7 +235,7 @@ public class GlyphPositioningTable extends GlyphTable { public boolean position(GlyphSequence gs, String script, String language, int fontSize, int[] widths, int[][] adjustments) { Map> lookups = matchLookups(script, language, "*"); if ((lookups != null) && (lookups.size() > 0)) { - ScriptProcessor sp = ScriptProcessor.getInstance(script); + ScriptProcessor sp = ScriptProcessor.getInstance(script, processors); return sp.position(this, gs, script, language, fontSize, lookups, widths, adjustments); } else { return false; diff --git a/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/GlyphSubstitutionTable.java b/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/GlyphSubstitutionTable.java index f6d0b74f6..6ee333192 100644 --- a/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/GlyphSubstitutionTable.java +++ b/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/GlyphSubstitutionTable.java @@ -69,8 +69,9 @@ public class GlyphSubstitutionTable extends GlyphTable { * @param lookups a map of lookup specifications to subtable identifier strings * @param subtables a list of identified subtables */ - public GlyphSubstitutionTable(GlyphDefinitionTable gdef, Map lookups, List subtables) { - super(gdef, lookups); + public GlyphSubstitutionTable(GlyphDefinitionTable gdef, Map lookups, List subtables, + Map processors) { + super(gdef, lookups, processors); if ((subtables == null) || (subtables.size() == 0)) { throw new AdvancedTypographicTableFormatException("subtables must be non-empty"); } else { @@ -96,7 +97,7 @@ public class GlyphSubstitutionTable extends GlyphTable { GlyphSequence ogs; Map> lookups = matchLookups(script, language, "*"); if ((lookups != null) && (lookups.size() > 0)) { - ScriptProcessor sp = ScriptProcessor.getInstance(script); + ScriptProcessor sp = ScriptProcessor.getInstance(script, processors); ogs = sp.substitute(this, gs, script, language, lookups); } else { ogs = gs; diff --git a/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/GlyphTable.java b/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/GlyphTable.java index b3a0afe88..ce5c47ba0 100644 --- a/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/GlyphTable.java +++ b/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/GlyphTable.java @@ -33,6 +33,7 @@ import java.util.TreeSet; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.fop.complexscripts.scripts.ScriptProcessor; import org.apache.fop.complexscripts.util.GlyphSequence; import org.apache.fop.complexscripts.util.ScriptContextTester; @@ -74,12 +75,16 @@ public class GlyphTable { // if true, then prevent further subtable addition private boolean frozen; + protected Map processors; + /** * Instantiate glyph table with specified lookups. * @param gdef glyph definition table that applies * @param lookups map from lookup specs to lookup tables */ - public GlyphTable(GlyphTable gdef, Map> lookups) { + public GlyphTable(GlyphTable gdef, Map> lookups, + Map processors) { + this.processors = processors; if ((gdef != null) && !(gdef instanceof GlyphDefinitionTable)) { throw new AdvancedTypographicTableFormatException("bad glyph definition table"); } else if (lookups == null) { diff --git a/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/OTFAdvancedTypographicTableReader.java b/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/OTFAdvancedTypographicTableReader.java index 469f7f8f5..dd38ca952 100644 --- a/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/OTFAdvancedTypographicTableReader.java +++ b/fop-core/src/main/java/org/apache/fop/complexscripts/fonts/OTFAdvancedTypographicTableReader.java @@ -21,12 +21,14 @@ package org.apache.fop.complexscripts.fonts; import java.io.IOException; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.fop.complexscripts.scripts.ScriptProcessor; import org.apache.fop.fonts.truetype.FontFileReader; import org.apache.fop.fonts.truetype.OFDirTabEntry; import org.apache.fop.fonts.truetype.OFTableName; @@ -57,6 +59,7 @@ public final class OTFAdvancedTypographicTableReader { private transient GlyphMappingTable seMapping; // subtable entry mappings private transient List seEntries; // subtable entry entries private transient List seSubtables; // subtable entry subtables + private Map processors = new HashMap(); /** * Construct an OTFAdvancedTypographicTableReader instance. @@ -3515,7 +3518,7 @@ public final class OTFAdvancedTypographicTableReader { List subtables; if ((subtables = constructGDEFSubtables()) != null) { if (subtables.size() > 0) { - gdef = new GlyphDefinitionTable(subtables); + gdef = new GlyphDefinitionTable(subtables, processors); } } resetATState(); @@ -3534,7 +3537,7 @@ public final class OTFAdvancedTypographicTableReader { List subtables; if ((subtables = constructGSUBSubtables()) != null) { if ((lookups.size() > 0) && (subtables.size() > 0)) { - gsub = new GlyphSubstitutionTable(gdef, lookups, subtables); + gsub = new GlyphSubstitutionTable(gdef, lookups, subtables, processors); } } } @@ -3554,7 +3557,7 @@ public final class OTFAdvancedTypographicTableReader { List subtables; if ((subtables = constructGPOSSubtables()) != null) { if ((lookups.size() > 0) && (subtables.size() > 0)) { - gpos = new GlyphPositioningTable(gdef, lookups, subtables); + gpos = new GlyphPositioningTable(gdef, lookups, subtables, processors); } } } diff --git a/fop-core/src/main/java/org/apache/fop/complexscripts/scripts/ScriptProcessor.java b/fop-core/src/main/java/org/apache/fop/complexscripts/scripts/ScriptProcessor.java index 9d8c28890..af73a8f44 100644 --- a/fop-core/src/main/java/org/apache/fop/complexscripts/scripts/ScriptProcessor.java +++ b/fop-core/src/main/java/org/apache/fop/complexscripts/scripts/ScriptProcessor.java @@ -46,8 +46,6 @@ public abstract class ScriptProcessor { private final Map assembledLookups; - private static Map processors = new HashMap(); - /** * Instantiate a script processor. * @param script a script identifier @@ -229,7 +227,7 @@ public abstract class ScriptProcessor { * @param script a script identifier * @return a script processor instance or null if none found */ - public static synchronized ScriptProcessor getInstance(String script) { + public static synchronized ScriptProcessor getInstance(String script, Map processors) { ScriptProcessor sp = null; assert processors != null; if ((sp = processors.get(script)) == null) { diff --git a/fop-core/src/test/java/org/apache/fop/complexscripts/fonts/ttx/TTXFile.java b/fop-core/src/test/java/org/apache/fop/complexscripts/fonts/ttx/TTXFile.java index c94b0bcb0..9e4b54f4b 100644 --- a/fop-core/src/test/java/org/apache/fop/complexscripts/fonts/ttx/TTXFile.java +++ b/fop-core/src/test/java/org/apache/fop/complexscripts/fonts/ttx/TTXFile.java @@ -62,6 +62,7 @@ import org.apache.fop.complexscripts.fonts.GlyphSubstitutionTable.LigatureSet; import org.apache.fop.complexscripts.fonts.GlyphSubtable; import org.apache.fop.complexscripts.fonts.GlyphTable; import org.apache.fop.complexscripts.fonts.GlyphTable.RuleLookup; +import org.apache.fop.complexscripts.scripts.ScriptProcessor; import org.apache.fop.complexscripts.util.GlyphSequence; import org.apache.fop.complexscripts.util.UTF32; import org.apache.fop.util.CharUtilities; @@ -162,6 +163,7 @@ public class TTXFile { private GlyphDefinitionTable gdef; // constructed glyph definition table private GlyphSubstitutionTable gsub; // constructed glyph substitution table private GlyphPositioningTable gpos; // constructed glyph positioning table + private Map processors = new HashMap(); public TTXFile() { elements = new Stack(); @@ -2326,17 +2328,17 @@ public class TTXFile { } } else if (en[1].equals("GDEF")) { if (subtables.size() > 0) { - gdef = new GlyphDefinitionTable(subtables); + gdef = new GlyphDefinitionTable(subtables, processors); } clearTable(); } else if (en[1].equals("GPOS")) { if (subtables.size() > 0) { - gpos = new GlyphPositioningTable(gdef, extractLookups(), subtables); + gpos = new GlyphPositioningTable(gdef, extractLookups(), subtables, processors); } clearTable(); } else if (en[1].equals("GSUB")) { if (subtables.size() > 0) { - gsub = new GlyphSubstitutionTable(gdef, extractLookups(), subtables); + gsub = new GlyphSubstitutionTable(gdef, extractLookups(), subtables, processors); } clearTable(); } else if (en[1].equals("GlyphClassDef")) { diff --git a/fop-core/src/test/java/org/apache/fop/complexscripts/scripts/arabic/ArabicJoinersTestCase.java b/fop-core/src/test/java/org/apache/fop/complexscripts/scripts/arabic/ArabicJoinersTestCase.java index 092bebd41..6b34379aa 100644 --- a/fop-core/src/test/java/org/apache/fop/complexscripts/scripts/arabic/ArabicJoinersTestCase.java +++ b/fop-core/src/test/java/org/apache/fop/complexscripts/scripts/arabic/ArabicJoinersTestCase.java @@ -21,6 +21,7 @@ package org.apache.fop.complexscripts.scripts.arabic; import java.nio.IntBuffer; import java.util.BitSet; +import java.util.HashMap; import org.junit.Test; @@ -74,7 +75,7 @@ public class ArabicJoinersTestCase { @Test public void testArabicJoiners() { String script = CharScript.scriptTagFromCode(CharScript.SCRIPT_ARABIC); - ScriptProcessor sp = ScriptProcessor.getInstance(script); + ScriptProcessor sp = ScriptProcessor.getInstance(script, new HashMap()); assertTrue(sp != null); ScriptContextTester sct = sp.getSubstitutionContextTester(); assertTrue(sct != null); -- 2.39.5