jeremias    2003/01/09 05:51:06

  Modified:    src/org/apache/fop/fonts/apps PFMReader.java TTFReader.java
               src/org/apache/fop/fonts/truetype TTFFile.java
                        TTFMtxEntry.java
  Log:
  Fixed bug #15877: ArrayIndexOutOfBoundException with certain TrueType fonts.
  Reserved name indexes were not ignored.
  
  Fixed ugly exception handling in TTFReader and PFMReader.
  
  Revision  Changes    Path
  1.11      +9 -12     xml-fop/src/org/apache/fop/fonts/apps/PFMReader.java
  
  Index: PFMReader.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/fonts/apps/PFMReader.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- PFMReader.java    2 Dec 2002 14:27:58 -0000       1.10
  +++ PFMReader.java    9 Jan 2003 13:51:05 -0000       1.11
  @@ -15,13 +15,13 @@
   import java.util.Iterator;
   import javax.xml.parsers.DocumentBuilderFactory;
   import javax.xml.transform.Transformer;
  +import javax.xml.transform.TransformerException;
   import javax.xml.transform.TransformerFactory;
   
   import org.w3c.dom.Document;
   import org.w3c.dom.Element;
   
   //Avalon
  -import org.apache.avalon.framework.CascadingRuntimeException;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
   import org.apache.avalon.framework.logger.Logger;
   import org.apache.avalon.framework.logger.ConsoleLogger;
  @@ -199,19 +199,16 @@
        * @param   doc The DOM Document to save.
        * @param   target The target filename for the XML file.
        */
  -    public void writeFontXML(org.w3c.dom.Document doc, String target) {
  +    public void writeFontXML(org.w3c.dom.Document doc, String target) 
  +                throws TransformerException {
           getLogger().info("Writing xml font file " + target + "...");
           getLogger().info("");
   
  -        try {
  -            TransformerFactory factory = TransformerFactory.newInstance();
  -            Transformer transformer = factory.newTransformer();
  -            transformer.transform(
  -                    new javax.xml.transform.dom.DOMSource(doc),
  -                    new javax.xml.transform.stream.StreamResult(new File(target)));
  -        } catch (Exception e) {
  -            throw new CascadingRuntimeException("Error while serializing XML font 
metric file", e);
  -        }
  +        TransformerFactory factory = TransformerFactory.newInstance();
  +        Transformer transformer = factory.newTransformer();
  +        transformer.transform(
  +                new javax.xml.transform.dom.DOMSource(doc),
  +                new javax.xml.transform.stream.StreamResult(new File(target)));
       }
   
       /**
  
  
  
  1.12      +11 -15    xml-fop/src/org/apache/fop/fonts/apps/TTFReader.java
  
  Index: TTFReader.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/fonts/apps/TTFReader.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- TTFReader.java    8 Jan 2003 13:54:04 -0000       1.11
  +++ TTFReader.java    9 Jan 2003 13:51:05 -0000       1.12
  @@ -13,13 +13,13 @@
   import java.util.Iterator;
   import javax.xml.parsers.DocumentBuilderFactory;
   import javax.xml.transform.Transformer;
  +import javax.xml.transform.TransformerException;
   import javax.xml.transform.TransformerFactory;
   
   import org.w3c.dom.Document;
   import org.w3c.dom.Element;
   
   //Avalon
  -import org.apache.avalon.framework.CascadingRuntimeException;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
   import org.apache.avalon.framework.logger.ConsoleLogger;
   import org.apache.avalon.framework.logger.Logger;
  @@ -130,7 +130,7 @@
           TTFReader app = new TTFReader();
           app.enableLogging(log);
   
  -        log.info("TTF Reader v1.1.3");
  +        log.info("TTF Reader v1.1.4");
   
           if (options.get("-enc") != null) {
               String enc = (String)options.get("-enc");
  @@ -222,19 +222,15 @@
        * @param   doc The DOM Document to save.
        * @param   target The target filename for the XML file.
        */
  -    public void writeFontXML(org.w3c.dom.Document doc, String target) {
  +    public void writeFontXML(org.w3c.dom.Document doc, String target) 
  +                throws TransformerException {
           getLogger().info("Writing xml font file " + target + "...");
   
  -        try {
  -            TransformerFactory factory = TransformerFactory.newInstance();
  -            Transformer transformer = factory.newTransformer();
  -            transformer.transform(
  -                    new javax.xml.transform.dom.DOMSource(doc),
  -                    new javax.xml.transform.stream.StreamResult(new File(target)));
  -        } catch (Exception e) {
  -            throw new CascadingRuntimeException(
  -                    "Error while serializing XML font metrics file", e);
  -        }
  +        TransformerFactory factory = TransformerFactory.newInstance();
  +        Transformer transformer = factory.newTransformer();
  +        transformer.transform(
  +                new javax.xml.transform.dom.DOMSource(doc),
  +                new javax.xml.transform.stream.StreamResult(new File(target)));
       }
   
       /**
  @@ -277,7 +273,7 @@
           // "Perpetua-Bold", but the TrueType spec says that in the ttf file
           // it should be "Perpetua,Bold".
   
  -        String s = stripWhiteSpace(ttf.getPostscriptName());
  +        String s = stripWhiteSpace(ttf.getPostScriptName());
   
           if (fontName != null) {
               el.appendChild(doc.createTextNode(stripWhiteSpace(fontName)));
  
  
  
  1.2       +71 -48    xml-fop/src/org/apache/fop/fonts/truetype/TTFFile.java
  
  Index: TTFFile.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/fonts/truetype/TTFFile.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TTFFile.java      8 Jan 2003 13:54:04 -0000       1.1
  +++ TTFFile.java      9 Jan 2003 13:51:06 -0000       1.2
  @@ -172,10 +172,12 @@
               int cmapEntrySelector = in.readTTFUShort();
               int cmapRangeShift = in.readTTFUShort();
   
  -            getLogger().debug("segCountX2   : " + cmapSegCountX2);
  -            getLogger().debug("searchRange  : " + cmapSearchRange);
  -            getLogger().debug("entrySelector: " + cmapEntrySelector);
  -            getLogger().debug("rangeShift   : " + cmapRangeShift);
  +            if (getLogger().isDebugEnabled()) {
  +                getLogger().debug("segCountX2   : " + cmapSegCountX2);
  +                getLogger().debug("searchRange  : " + cmapSearchRange);
  +                getLogger().debug("entrySelector: " + cmapEntrySelector);
  +                getLogger().debug("rangeShift   : " + cmapRangeShift);
  +            }
   
   
               int cmapEndCounts[] = new int[cmapSegCountX2 / 2];
  @@ -247,36 +249,44 @@
                                       ansiWidth[aIdx.intValue()] =
                                           mtxTab[glyphIdx].getWx();
   
  -                                    getLogger().debug("Added width "
  -                                        + mtxTab[glyphIdx].getWx()
  -                                        + " uni: " + j
  -                                        + " ansi: " + aIdx.intValue());
  +                                    if (getLogger().isDebugEnabled()) {
  +                                        getLogger().debug("Added width "
  +                                            + mtxTab[glyphIdx].getWx()
  +                                            + " uni: " + j
  +                                            + " ansi: " + aIdx.intValue());
  +                                    }
                                   }
                               }
   
  -                            getLogger().debug("Idx: "
  -                                + glyphIdx
  -                                + " Delta: " + cmapDeltas[i]
  -                                + " Unicode: " + j
  -                                + " name: " + mtxTab[glyphIdx].getName());
  +                            if (getLogger().isDebugEnabled()) {
  +                                getLogger().debug("Idx: "
  +                                    + glyphIdx
  +                                    + " Delta: " + cmapDeltas[i]
  +                                    + " Unicode: " + j
  +                                    + " name: " + mtxTab[glyphIdx].getName());
  +                            }
                           } else {
                               glyphIdx = (j + cmapDeltas[i]) & 0xffff;
   
                               if (glyphIdx < mtxTab.length) {
                                   mtxTab[glyphIdx].getUnicodeIndex().add(new 
Integer(j));
                               } else {
  -                                getLogger().debug("Glyph " + glyphIdx
  +                                if (getLogger().isDebugEnabled()) {
  +                                    getLogger().debug("Glyph " + glyphIdx
                                                      + " out of range: "
                                                      + mtxTab.length);
  +                                }
                               }
   
                               unicodeMapping.add(new UnicodeMapping(glyphIdx, j));
                               if (glyphIdx < mtxTab.length) {
                                   mtxTab[glyphIdx].getUnicodeIndex().add(new 
Integer(j));
                               } else {
  -                                getLogger().debug("Glyph " + glyphIdx
  +                                if (getLogger().isDebugEnabled()) {
  +                                    getLogger().debug("Glyph " + glyphIdx
                                                      + " out of range: "
                                                      + mtxTab.length);
  +                                }
                               }
   
                               // Also add winAnsiWidth
  @@ -393,7 +403,7 @@
           readHorizontalHeader(in);
           readHorizontalMetrics(in);
           initAnsiWidths();
  -        readPostscript(in);
  +        readPostScript(in);
           readOS2(in);
           readIndexToLocation(in);
           readGlyf(in);
  @@ -453,7 +463,7 @@
        * Returns the PostScript name of the font.
        * @return String The PostScript name
        */
  -    public String getPostscriptName() {
  +    public String getPostScriptName() {
           if ("Regular".equals(subFamilyName) || "Roman".equals(subFamilyName)) {
               return familyName;
           } else {
  @@ -715,7 +725,7 @@
               throws IOException {
           seekTab(in, "hmtx", 0);
   
  -        int mtxSize = (numberOfGlyphs > nhmtx) ? numberOfGlyphs : nhmtx;
  +        int mtxSize = Math.max(numberOfGlyphs, nhmtx);
           mtxTab = new TTFMtxEntry[mtxSize];
   
           getLogger().debug("*** Widths array: \n");
  @@ -726,8 +736,10 @@
               mtxTab[i].setWx(in.readTTFUShort());
               mtxTab[i].setLsb(in.readTTFUShort());
   
  -            getLogger().debug("   width[" + i + "] = "
  -                + convertTTFUnit2PDFUnit(mtxTab[i].getWx()) + ";");
  +            if (getLogger().isDebugEnabled()) {
  +                getLogger().debug("   width[" + i + "] = "
  +                    + convertTTFUnit2PDFUnit(mtxTab[i].getWx()) + ";");
  +            }
           }
   
           if (nhmtx < mtxSize) {
  @@ -743,12 +755,9 @@
   
       /**
        * Read the "post" table
  -     * containing the postscript names of the glyphs.
  +     * containing the PostScript names of the glyphs.
        */
  -    private final void readPostscript(FontFileReader in) throws IOException {
  -        String[] psGlyphsBuffer;
  -        int i, k, l;
  -
  +    private final void readPostScript(FontFileReader in) throws IOException {
           seekTab(in, "post", 0);
           postFormat = in.readTTFLong();
           italicAngle = in.readTTFULong();
  @@ -756,60 +765,74 @@
           underlineThickness = in.readTTFShort();
           isFixedPitch = in.readTTFULong();
   
  +        //Skip memory usage values
           in.skip(4 * 4);
   
  -        getLogger().debug("Post format: " + postFormat);
  +        getLogger().debug("PostScript format: " + postFormat);
           switch (postFormat) {
           case 0x00010000:
  -            getLogger().debug("Postscript format 1");
  -            for (i = 0; i < Glyphs.MAC_GLYPH_NAMES.length; i++) {
  +            getLogger().debug("PostScript format 1");
  +            for (int i = 0; i < Glyphs.MAC_GLYPH_NAMES.length; i++) {
                   mtxTab[i].setName(Glyphs.MAC_GLYPH_NAMES[i]);
               }
               break;
           case 0x00020000:
  -            getLogger().debug("Postscript format 2");
  +            getLogger().debug("PostScript format 2");
               int numGlyphStrings = 0;
  -            l = in.readTTFUShort();      // Num Glyphs
  -            // short minIndex=256;
  -            for (i = 0; i < l; i++) {    // Read indexes
  +            
  +            // Read Number of Glyphs
  +            int l = in.readTTFUShort();      
  +            
  +            // Read indexes
  +            for (int i = 0; i < l; i++) {    
                   mtxTab[i].setIndex(in.readTTFUShort());
  -                // if (minIndex > mtxTab[i].index)
  -                // minIndex=(short)mtxTab[i].index;
   
                   if (mtxTab[i].getIndex() > 257) {
  +                    //Index is not in the Macintosh standard set
                       numGlyphStrings++;
                   }
   
  -                getLogger().debug("Post index: " + mtxTab[i].getIndex());
  +                if (getLogger().isDebugEnabled()) {
  +                    getLogger().debug("PostScript index: " + 
mtxTab[i].getIndexAsString());
  +                }
               }
  +            
               // firstChar=minIndex;
  -            psGlyphsBuffer = new String[numGlyphStrings];
  -            getLogger().debug("Reading " + numGlyphStrings
  -                + " glyphnames" + ", was n num glyphs=" + l);
  -            for (i = 0; i < psGlyphsBuffer.length; i++) {
  +            String[] psGlyphsBuffer = new String[numGlyphStrings];
  +            if (getLogger().isDebugEnabled()) {
  +                getLogger().debug("Reading " + numGlyphStrings
  +                        + " glyphnames, that are not in the standard Macintosh"
  +                        + " set. Total number of glyphs=" + l);
  +            }
  +            for (int i = 0; i < psGlyphsBuffer.length; i++) {
                   psGlyphsBuffer[i] = in.readTTFString(in.readTTFUByte());
               }
   
  -            for (i = 0; i < l; i++) {
  +            //Set glyph names
  +            for (int i = 0; i < l; i++) {
                   if (mtxTab[i].getIndex() < NMACGLYPHS) {
                       mtxTab[i].setName(Glyphs.MAC_GLYPH_NAMES[mtxTab[i].getIndex()]);
                   } else {
  -                    k = mtxTab[i].getIndex() - NMACGLYPHS;
  +                    if (!mtxTab[i].isIndexReserved()) {
  +                        int k = mtxTab[i].getIndex() - NMACGLYPHS;
   
  -                    getLogger().debug(k + " i=" + i + " mtx=" + mtxTab.length
  -                        + " ps=" + psGlyphsBuffer.length);
  +                        if (getLogger().isDebugEnabled()) {
  +                            getLogger().debug(k + " i=" + i + " mtx=" + 
mtxTab.length
  +                                + " ps=" + psGlyphsBuffer.length);
  +                        }
   
  -                    mtxTab[i].setName(psGlyphsBuffer[k]);
  +                        mtxTab[i].setName(psGlyphsBuffer[k]);
  +                    }
                   }
               }
   
               break;
           case 0x00030000:
  -            // Postscript format 3 contains no glyph names
  -            getLogger().debug("Postscript format 3");
  +            // PostScript format 3 contains no glyph names
  +            getLogger().debug("PostScript format 3");
               break;
           default:
  -            getLogger().error("Unknown Postscript format: " + postFormat);
  +            getLogger().error("Unknown PostScript format: " + postFormat);
           }
       }
   
  
  
  
  1.2       +23 -2     xml-fop/src/org/apache/fop/fonts/truetype/TTFMtxEntry.java
  
  Index: TTFMtxEntry.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/fonts/truetype/TTFMtxEntry.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TTFMtxEntry.java  8 Jan 2003 13:54:04 -0000       1.1
  +++ TTFMtxEntry.java  9 Jan 2003 13:51:06 -0000       1.2
  @@ -30,7 +30,7 @@
        * @return String String representation
        */
       public String toString(TTFFile t) {
  -        return "Glyph " + name + " index: " + index + " bbox [ "
  +        return "Glyph " + name + " index: " + getIndexAsString() + " bbox ["
                + t.convertTTFUnit2PDFUnit(boundingBox[0]) + " "
                + t.convertTTFUnit2PDFUnit(boundingBox[1]) + " "
                + t.convertTTFUnit2PDFUnit(boundingBox[2]) + " "
  @@ -68,6 +68,27 @@
        */
       public int getIndex() {
           return index;
  +    }
  +        
  +    /**
  +     * Determines whether this index represents a reserved character.
  +     * @return True if it is reserved
  +     */
  +    public boolean isIndexReserved() {
  +        return (getIndex() >= 32768) && (getIndex() <= 65535);
  +    }
  +    
  +    /**
  +     * Returns a String representation of the index taking into account if
  +     * the index is in the reserved range.
  +     * @return index as String
  +     */
  +    public String getIndexAsString() {
  +        if (isIndexReserved()) {
  +            return Integer.toString(getIndex()) + " (reserved)";
  +        } else {
  +            return Integer.toString(getIndex());
  +        }
       }
   
       /**
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to