Author: lehmi Date: Fri May 27 16:44:04 2016 New Revision: 1745772 URL: http://svn.apache.org/viewvc?rev=1745772&view=rev Log: PDFBOX-3089: cache GlyphDescription
Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyfCompositeDescript.java Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyfCompositeDescript.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyfCompositeDescript.java?rev=1745772&r1=1745771&r2=1745772&view=diff ============================================================================== --- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyfCompositeDescript.java (original) +++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyfCompositeDescript.java Fri May 27 16:44:04 2016 @@ -20,8 +20,11 @@ package org.apache.fontbox.ttf; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -41,10 +44,13 @@ public class GlyfCompositeDescript exten private static final Log LOG = LogFactory.getLog(GlyfCompositeDescript.class); private final List<GlyfCompositeComp> components = new ArrayList<GlyfCompositeComp>(); + private final Map<Integer,GlyphDescription> descriptions = new HashMap<Integer,GlyphDescription>(); private GlyphTable glyphTable = null; private boolean beingResolved = false; private boolean resolved = false; private int pointCount = -1; + private int contourCount = -1; + /** * Constructor. * @@ -72,6 +78,7 @@ public class GlyfCompositeDescript exten { readInstructions(bais, (bais.readUnsignedShort())); } + initDescriptions(); } /** @@ -101,8 +108,7 @@ public class GlyfCompositeDescript exten comp.setFirstIndex(firstIndex); comp.setFirstContour(firstContour); - GlyphDescription desc; - desc = getGlypDescription(comp.getGlyphIndex()); + GlyphDescription desc = descriptions.get(comp.getGlyphIndex()); if (desc != null) { desc.resolve(); @@ -123,7 +129,7 @@ public class GlyfCompositeDescript exten GlyfCompositeComp c = getCompositeCompEndPt(i); if (c != null) { - GlyphDescription gd = getGlypDescription(c.getGlyphIndex()); + GlyphDescription gd = descriptions.get(c.getGlyphIndex()); return gd.getEndPtOfContours(i - c.getFirstContour()) + c.getFirstIndex(); } return 0; @@ -138,7 +144,7 @@ public class GlyfCompositeDescript exten GlyfCompositeComp c = getCompositeComp(i); if (c != null) { - GlyphDescription gd = getGlypDescription(c.getGlyphIndex()); + GlyphDescription gd = descriptions.get(c.getGlyphIndex()); return gd.getFlags(i - c.getFirstIndex()); } return 0; @@ -153,7 +159,7 @@ public class GlyfCompositeDescript exten GlyfCompositeComp c = getCompositeComp(i); if (c != null) { - GlyphDescription gd = getGlypDescription(c.getGlyphIndex()); + GlyphDescription gd = descriptions.get(c.getGlyphIndex()); int n = i - c.getFirstIndex(); int x = gd.getXCoordinate(n); int y = gd.getYCoordinate(n); @@ -173,7 +179,7 @@ public class GlyfCompositeDescript exten GlyfCompositeComp c = getCompositeComp(i); if (c != null) { - GlyphDescription gd = getGlypDescription(c.getGlyphIndex()); + GlyphDescription gd = descriptions.get(c.getGlyphIndex()); int n = i - c.getFirstIndex(); int x = gd.getXCoordinate(n); int y = gd.getYCoordinate(n); @@ -206,7 +212,7 @@ public class GlyfCompositeDescript exten if (pointCount < 0) { GlyfCompositeComp c = components.get(components.size() - 1); - GlyphDescription gd = getGlypDescription(c.getGlyphIndex()); + GlyphDescription gd = descriptions.get(c.getGlyphIndex()); if (gd == null) { LOG.error("getGlypDescription(" + c.getGlyphIndex() + ") is null, returning 0"); @@ -230,8 +236,12 @@ public class GlyfCompositeDescript exten { LOG.error("getContourCount called on unresolved GlyfCompositeDescript"); } - GlyfCompositeComp c = components.get(components.size() - 1); - return c.getFirstContour() + getGlypDescription(c.getGlyphIndex()).getContourCount(); + if (contourCount < 0) + { + GlyfCompositeComp c = components.get(components.size() - 1); + contourCount = c.getFirstContour() + descriptions.get(c.getGlyphIndex()).getContourCount(); + } + return contourCount; } /** @@ -248,7 +258,7 @@ public class GlyfCompositeDescript exten { for (GlyfCompositeComp c : components) { - GlyphDescription gd = getGlypDescription(c.getGlyphIndex()); + GlyphDescription gd = descriptions.get(c.getGlyphIndex()); if (c.getFirstIndex() <= i && i < (c.getFirstIndex() + gd.getPointCount())) { return c; @@ -261,7 +271,7 @@ public class GlyfCompositeDescript exten { for (GlyfCompositeComp c : components) { - GlyphDescription gd = getGlypDescription(c.getGlyphIndex()); + GlyphDescription gd = descriptions.get(c.getGlyphIndex()); if (c.getFirstContour() <= i && i < (c.getFirstContour() + gd.getContourCount())) { return c; @@ -270,21 +280,23 @@ public class GlyfCompositeDescript exten return null; } - private GlyphDescription getGlypDescription(int index) + private void initDescriptions() { - try + for (GlyfCompositeComp component : components) { - GlyphData glyph = glyphTable.getGlyph(index); - if (glyph != null) + try { - return glyph.getDescription(); + int index = component.getGlyphIndex(); + GlyphData glyph = glyphTable.getGlyph(index); + if (glyph != null) + { + descriptions.put(index, glyph.getDescription()); + } } - return null; - } - catch (IOException e) - { - LOG.error(e); - return null; + catch (IOException e) + { + LOG.error(e); + } } } }