Author: leleueri
Date: Tue Apr 17 19:34:37 2012
New Revision: 1327241

URL: http://svn.apache.org/viewvc?rev=1327241&view=rev
Log:
PDFBOX-490 & PDFBOX-1265 : read glyphIndex as UnsignedShort to avoid negative 
index & fix a null pointer

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

Modified: 
pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyfCompositeComp.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyfCompositeComp.java?rev=1327241&r1=1327240&r2=1327241&view=diff
==============================================================================
--- 
pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyfCompositeComp.java
 (original)
+++ 
pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/GlyfCompositeComp.java
 Tue Apr 17 19:34:37 2012
@@ -91,7 +91,7 @@ public class GlyfCompositeComp 
     protected GlyfCompositeComp(TTFDataStream bais) throws IOException 
     {
         flags = bais.readSignedShort();
-        glyphIndex = bais.readSignedShort();
+        glyphIndex = bais.readUnsignedShort();// number of glyph in a font is 
uint16
 
         // Get the arguments as just their raw values
         if ((flags & ARG_1_AND_2_ARE_WORDS) != 0) 

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=1327241&r1=1327240&r2=1327241&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
 Tue Apr 17 19:34:37 2012
@@ -34,223 +34,225 @@ import java.util.List;
 public class GlyfCompositeDescript extends GlyfDescript 
 {
 
-    private List<GlyfCompositeComp> components = new 
ArrayList<GlyfCompositeComp>();
-    private GlyphData[] glyphs = null;
-    private boolean beingResolved = false;
-    private boolean resolved = false;
-
-    /**
-     * Constructor.
-     * 
-     * @param bais the stream to be read
-     * @param glyphTable the Glyphtable containing all glyphs
-     * @throws IOException is thrown if something went wrong
-     */
-    public GlyfCompositeDescript(TTFDataStream bais, GlyphTable glyphTable) 
throws IOException 
-    {
-        super((short) -1, bais);
-        
-        glyphs = glyphTable.getGlyphs();
-        
-        // Get all of the composite components
-        GlyfCompositeComp comp;
-        do 
-        {
-            comp = new GlyfCompositeComp(bais);
-            components.add(comp);
-        } 
-        while ((comp.getFlags() & GlyfCompositeComp.MORE_COMPONENTS) != 0);
-
-        // Are there hinting instructions to read?
-        if ((comp.getFlags() & GlyfCompositeComp.WE_HAVE_INSTRUCTIONS) != 0) 
-        {
-            readInstructions(bais, (bais.read()<<8 | bais.read()));
-        }
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public void resolve() 
-    {
-        if (resolved) 
-        {
-            return;
-        }
-        if (beingResolved) 
-        {
-            System.err.println("Circular reference in GlyfCompositeDesc");
-            return;
-        }
-        beingResolved = true;
-
-        int firstIndex = 0;
-        int firstContour = 0;
-
-        Iterator<GlyfCompositeComp> i = components.iterator();
-        while (i.hasNext()) 
-        {
-            GlyfCompositeComp comp = (GlyfCompositeComp)i.next();
-            comp.setFirstIndex(firstIndex);
-            comp.setFirstContour(firstContour);
-
-            GlyphDescription desc;
-            desc = getGlypDescription(comp.getGlyphIndex());
-            if (desc != null) 
-            {
-                desc.resolve();
-                firstIndex   += desc.getPointCount();
-                firstContour += desc.getContourCount();
-            }
-        }
-        resolved = true;
-        beingResolved = false;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public int getEndPtOfContours(int i) 
-    {
-        GlyfCompositeComp c = getCompositeCompEndPt(i);
-        if (c != null) 
-        {
-            GlyphDescription gd = getGlypDescription(c.getGlyphIndex());
-            return gd.getEndPtOfContours(i - c.getFirstContour()) + 
c.getFirstIndex();
-        }
-        return 0;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public byte getFlags(int i) 
-    {
-        GlyfCompositeComp c = getCompositeComp(i);
-        if (c != null) 
-        {
-            GlyphDescription gd = getGlypDescription(c.getGlyphIndex());
-            return gd.getFlags(i - c.getFirstIndex());
-        }
-        return 0;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public short getXCoordinate(int i) 
-    {
-        GlyfCompositeComp c = getCompositeComp(i);
-        if (c != null) 
-        {
-            GlyphDescription gd = getGlypDescription(c.getGlyphIndex());
-            int n = i - c.getFirstIndex();
-            int x = gd.getXCoordinate(n);
-            int y = gd.getYCoordinate(n);
-            short x1 = (short) c.scaleX(x, y);
-            x1 += c.getXTranslate();
-            return x1;
-        }
-        return 0;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public short getYCoordinate(int i) 
-    {
-        GlyfCompositeComp c = getCompositeComp(i);
-        if (c != null) 
-        {
-            GlyphDescription gd = getGlypDescription(c.getGlyphIndex());
-            int n = i - c.getFirstIndex();
-            int x = gd.getXCoordinate(n);
-            int y = gd.getYCoordinate(n);
-            short y1 = (short) c.scaleY(x, y);
-            y1 += c.getYTranslate();
-            return y1;
-        }
-        return 0;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public boolean isComposite() 
-    {
-        return true;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public int getPointCount() 
-    {
-        if (!resolved)
-        {
-            System.err.println("getPointCount called on unresolved 
GlyfCompositeDescript");
-        }
-        GlyfCompositeComp c = (GlyfCompositeComp) 
components.get(components.size()-1);
-        return c.getFirstIndex() + 
getGlypDescription(c.getGlyphIndex()).getPointCount();
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public int getContourCount() 
-    {
-        if (!resolved)
-        {
-            System.err.println("getContourCount called on unresolved 
GlyfCompositeDescript");
-        }
-        GlyfCompositeComp c = (GlyfCompositeComp) 
components.get(components.size()-1);
-        return c.getFirstContour() + 
getGlypDescription(c.getGlyphIndex()).getContourCount();
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public int getComponentCount() 
-    {
-        return components.size();
-    }
-
-    private GlyfCompositeComp getCompositeComp(int i) 
-    {
-        GlyfCompositeComp c;
-        for (int n = 0; n < components.size(); n++) 
-        {
-            c = (GlyfCompositeComp) components.get(n);
-            GlyphDescription gd = getGlypDescription(c.getGlyphIndex());
-            if (c.getFirstIndex() <= i && i < (c.getFirstIndex() + 
gd.getPointCount())) 
-            {
-                return c;
-            }
-        }
-        return null;
-    }
-
-    private GlyfCompositeComp getCompositeCompEndPt(int i) 
-    {
-        GlyfCompositeComp c;
-        for (int j = 0; j < components.size(); j++) 
-        {
-            c = (GlyfCompositeComp) components.get(j);
-            GlyphDescription gd = getGlypDescription(c.getGlyphIndex());
-            if (c.getFirstContour() <= i && i < (c.getFirstContour() + 
gd.getContourCount())) 
-            {
-                return c;
-            }
-        }
-        return null;
-    }
-    
-    private GlyphDescription getGlypDescription(int index)
-    {
-        if (glyphs != null && index < glyphs.length) 
-        {
-            return glyphs[index].getDescription();
-        }
-        return null;
-    }
+       private List<GlyfCompositeComp> components = new 
ArrayList<GlyfCompositeComp>();
+       private GlyphData[] glyphs = null;
+       private boolean beingResolved = false;
+       private boolean resolved = false;
+
+       /**
+        * Constructor.
+        * 
+        * @param bais the stream to be read
+        * @param glyphTable the Glyphtable containing all glyphs
+        * @throws IOException is thrown if something went wrong
+        */
+       public GlyfCompositeDescript(TTFDataStream bais, GlyphTable glyphTable) 
throws IOException 
+       {
+               super((short) -1, bais);
+
+               glyphs = glyphTable.getGlyphs();
+
+               // Get all of the composite components
+               GlyfCompositeComp comp;
+               do 
+               {
+                       comp = new GlyfCompositeComp(bais);
+                       components.add(comp);
+               } 
+               while ((comp.getFlags() & GlyfCompositeComp.MORE_COMPONENTS) != 
0);
+
+               // Are there hinting instructions to read?
+               if ((comp.getFlags() & GlyfCompositeComp.WE_HAVE_INSTRUCTIONS) 
!= 0) 
+               {
+                       readInstructions(bais, (bais.read()<<8 | bais.read()));
+               }
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void resolve() 
+       {
+               if (resolved) 
+               {
+                       return;
+               }
+               if (beingResolved) 
+               {
+                       System.err.println("Circular reference in 
GlyfCompositeDesc");
+                       return;
+               }
+               beingResolved = true;
+
+               int firstIndex = 0;
+               int firstContour = 0;
+
+               Iterator<GlyfCompositeComp> i = components.iterator();
+               while (i.hasNext()) 
+               {
+                       GlyfCompositeComp comp = (GlyfCompositeComp)i.next();
+                       comp.setFirstIndex(firstIndex);
+                       comp.setFirstContour(firstContour);
+
+                       GlyphDescription desc;
+                       desc = getGlypDescription(comp.getGlyphIndex());
+                       if (desc != null) 
+                       {
+                               desc.resolve();
+                               firstIndex   += desc.getPointCount();
+                               firstContour += desc.getContourCount();
+                       }
+               }
+               resolved = true;
+               beingResolved = false;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public int getEndPtOfContours(int i) 
+       {
+               GlyfCompositeComp c = getCompositeCompEndPt(i);
+               if (c != null) 
+               {
+                       GlyphDescription gd = 
getGlypDescription(c.getGlyphIndex());
+                       return gd.getEndPtOfContours(i - c.getFirstContour()) + 
c.getFirstIndex();
+               }
+               return 0;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public byte getFlags(int i) 
+       {
+               GlyfCompositeComp c = getCompositeComp(i);
+               if (c != null) 
+               {
+                       GlyphDescription gd = 
getGlypDescription(c.getGlyphIndex());
+                       return gd.getFlags(i - c.getFirstIndex());
+               }
+               return 0;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public short getXCoordinate(int i) 
+       {
+               GlyfCompositeComp c = getCompositeComp(i);
+               if (c != null) 
+               {
+                       GlyphDescription gd = 
getGlypDescription(c.getGlyphIndex());
+                       int n = i - c.getFirstIndex();
+                       int x = gd.getXCoordinate(n);
+                       int y = gd.getYCoordinate(n);
+                       short x1 = (short) c.scaleX(x, y);
+                       x1 += c.getXTranslate();
+                       return x1;
+               }
+               return 0;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public short getYCoordinate(int i) 
+       {
+               GlyfCompositeComp c = getCompositeComp(i);
+               if (c != null) 
+               {
+                       GlyphDescription gd = 
getGlypDescription(c.getGlyphIndex());
+                       int n = i - c.getFirstIndex();
+                       int x = gd.getXCoordinate(n);
+                       int y = gd.getYCoordinate(n);
+                       short y1 = (short) c.scaleY(x, y);
+                       y1 += c.getYTranslate();
+                       return y1;
+               }
+               return 0;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public boolean isComposite() 
+       {
+               return true;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public int getPointCount() 
+       {
+               if (!resolved)
+               {
+                       System.err.println("getPointCount called on unresolved 
GlyfCompositeDescript");
+               }
+               GlyfCompositeComp c = (GlyfCompositeComp) 
components.get(components.size()-1);
+               return c.getFirstIndex() + 
getGlypDescription(c.getGlyphIndex()).getPointCount();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public int getContourCount() 
+       {
+               if (!resolved)
+               {
+                       System.err.println("getContourCount called on 
unresolved GlyfCompositeDescript");
+               }
+               GlyfCompositeComp c = (GlyfCompositeComp) 
components.get(components.size()-1);
+               return c.getFirstContour() + 
getGlypDescription(c.getGlyphIndex()).getContourCount();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public int getComponentCount() 
+       {
+               return components.size();
+       }
+
+       private GlyfCompositeComp getCompositeComp(int i) 
+       {
+               GlyfCompositeComp c;
+               for (int n = 0; n < components.size(); n++) 
+               {
+                       c = (GlyfCompositeComp) components.get(n);
+                       GlyphDescription gd = 
getGlypDescription(c.getGlyphIndex());
+                       if (c.getFirstIndex() <= i && i < (c.getFirstIndex() + 
gd.getPointCount())) 
+                       {
+                               return c;
+                       }
+               }
+               return null;
+       }
+
+       private GlyfCompositeComp getCompositeCompEndPt(int i) 
+       {
+               GlyfCompositeComp c;
+               for (int j = 0; j < components.size(); j++) 
+               {
+                       c = (GlyfCompositeComp) components.get(j);
+                       GlyphDescription gd = 
getGlypDescription(c.getGlyphIndex());
+                       if (c.getFirstContour() <= i && i < 
(c.getFirstContour() + gd.getContourCount())) 
+                       {
+                               return c;
+                       }
+               }
+               return null;
+       }
+
+       private GlyphDescription getGlypDescription(int index)
+       {
+               if (glyphs != null && index < glyphs.length) 
+               {
+                       GlyphData glyph = glyphs[index];
+                       if (glyph != null)
+                               return glyph.getDescription();
+               }
+               return null;
+       }
 }


Reply via email to