]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
AFPFontReader a lot more robust now with less copy/pasted code but still far from...
authorAdrian Cumiskey <acumiskey@apache.org>
Fri, 24 Oct 2008 13:20:51 +0000 (13:20 +0000)
committerAdrian Cumiskey <acumiskey@apache.org>
Fri, 24 Oct 2008 13:20:51 +0000 (13:20 +0000)
CharacterSetOrientation width() renamed to getWidth() and now protects itself against a possible ArrayIndexOutOfBoundsException.

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@707627 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/fonts/Font.java
src/java/org/apache/fop/fonts/FontSetup.java
src/java/org/apache/fop/render/afp/fonts/AFPFontReader.java
src/java/org/apache/fop/render/afp/fonts/CharacterSet.java
src/java/org/apache/fop/render/afp/fonts/CharacterSetOrientation.java
src/java/org/apache/fop/render/afp/fonts/FopCharacterSet.java
src/java/org/apache/fop/render/afp/fonts/OutlineFont.java
src/java/org/apache/fop/render/afp/fonts/RasterFont.java
src/java/org/apache/fop/render/afp/tools/StructuredFieldReader.java

index de6904d9b05d639b9eb6c4fbb1f5fef6e052c6cb..ebd45457d2cce242cdcd2c44508d041bf1f47057 100644 (file)
@@ -64,16 +64,16 @@ public class Font {
     /** logger */
     private  static Log log = LogFactory.getLog(Font.class);
 
-    private String fontName;
-    private FontTriplet triplet;
-    private int fontSize;
+    private final String fontName;
+    private final FontTriplet triplet;
+    private final int fontSize;
 
     /**
      * normal or small-caps font
      */
     //private int fontVariant;
 
-    private FontMetrics metric;
+    private final FontMetrics metric;
 
     /**
      * Main constructor
@@ -268,7 +268,8 @@ public class Font {
             width = getCharWidth(' ');
         } else {
             if (hasChar(c)) {
-                width = getWidth(mapChar(c));
+                int mappedChar = mapChar(c);
+                width = getWidth(mappedChar);
             } else {
                 width = -1;
             }
index eea04a58006a4d2e5cc8fb21a20bc254bc65a24d..f7ad6fc653ee5c2a1420b95f719da569eba7d705 100644 (file)
@@ -27,7 +27,6 @@ import javax.xml.transform.stream.StreamSource;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-
 import org.apache.fop.fonts.base14.Courier;
 import org.apache.fop.fonts.base14.CourierBold;
 import org.apache.fop.fonts.base14.CourierBoldOblique;
@@ -202,7 +201,7 @@ public class FontSetup {
 
         if (resolver == null) {
             //Ensure that we have minimal font resolution capabilities
-            resolver = createMinimalFontResolver1();
+            resolver = createMinimalFontResolver();
         }
 
         String internalName = null;
@@ -225,7 +224,7 @@ public class FontSetup {
     }
 
     /** @return a new FontResolver to be used by the font subsystem */
-    public static FontResolver createMinimalFontResolver1() {
+    public static FontResolver createMinimalFontResolver() {
         return new FontResolver() {
 
             /** {@inheritDoc} */
index 7951be26cbb953201927c5937588f3ee0d6e2321..0c190738c9d91c0c89822b48c8156dafca5bea10 100644 (file)
@@ -26,12 +26,11 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.util.ArrayList;
-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.render.afp.exceptions.FontRuntimeException;
 import org.apache.fop.render.afp.modca.AFPConstants;
 import org.apache.fop.render.afp.tools.StructuredFieldReader;
 
@@ -111,143 +110,19 @@ public final class AFPFontReader {
     /**
      * The collection of code pages
      */
-    private HashMap codePages = new HashMap();
+    private final Map/*<String, Map<String, String>>*/ codePages
+        = new java.util.HashMap/*<String, Map<String, String>>*/();
 
     /**
-     * Load the font details and metrics into the CharacterSetMetric object,
-     * this will use the actual afp code page and character set files to load
-     * the object with the necessary metrics.
+     * Returns an InputStream to a given file path and filename
      *
-     * @param characterSet the CharacterSetMetric object to populate
-     */
-    public void loadCharacterSetMetric(CharacterSet characterSet) {
-
-        InputStream inputStream = null;
-
-        try {
-
-            /**
-             * Get the code page which contains the character mapping
-             * information to map the unicode character id to the graphic
-             * chracter global identifier.
-             */
-            String cp = new String(characterSet.getCodePage());
-            String path = characterSet.getPath();
-
-            HashMap codepage = (HashMap) codePages.get(cp);
-
-            if (codepage == null) {
-                codepage = loadCodePage(cp, characterSet.getEncoding(), path);
-                codePages.put(cp, codepage);
-            }
-
-            /**
-             * Load the character set metric information, no need to cache this
-             * information as it should be cached by the objects that wish to
-             * load character set metric information.
-             */
-            final String characterset = characterSet.getName();
-
-            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
-            if (classLoader == null) {
-                classLoader = AFPFontReader.class.getClassLoader();
-            }
-
-            URL url = classLoader.getResource(path);
-            if (url == null) {
-                try {
-                    File file = new File(path);
-                    url = file.toURI().toURL();
-                    if (url == null) {
-                        String msg = "CharacterSet file not found for "
-                            + characterset + " in classpath: " + path;
-                        log.error(msg);
-                        throw new FileNotFoundException(msg);
-                    }
-                } catch (MalformedURLException ex) {
-                    String msg = "CharacterSet file not found for "
-                        + characterset + " in classpath: " + path;
-                    log.error(msg);
-                    throw new FileNotFoundException(msg);
-                }
-
-            }
-
-            File directory = new File(url.getPath());
-
-            final String filterpattern = characterset.trim();
-            FilenameFilter filter = new FilenameFilter() {
-                public boolean accept(File dir, String name) {
-                    return name.startsWith(filterpattern);
-                }
-            };
-
-            File[] csfont = directory.listFiles(filter);
-            if (csfont.length < 1) {
-                String msg = "CharacterSet file search for " + characterset
-                    + " located " + csfont.length + " files";
-                log.error(msg);
-                throw new FileNotFoundException(msg);
-            } else if (csfont.length > 1) {
-                String msg = "CharacterSet file search for " + characterset
-                    + " located " + csfont.length + " files";
-                log.warn(msg);
-            }
-
-            inputStream = csfont[0].toURI().toURL().openStream();
-            if (inputStream == null) {
-                String msg = "Failed to open character set resource "
-                    + characterset;
-                log.error(msg);
-                throw new FileNotFoundException(msg);
-            }
-
-            StructuredFieldReader sfr = new StructuredFieldReader(inputStream);
-
-            // Process D3A789 Font Control
-            FontControl fnc = processFontControl(sfr);
-
-            //process D3AE89 Font Orientation
-            CharacterSetOrientation[] csoArray = processFontOrientation(sfr);
-
-            //process D3AC89 Font Position
-            processFontPosition(sfr, csoArray, fnc.getDpi());
-
-            //process D38C89 Font Index (per orientation)
-            for (int i = 0; i < csoArray.length; i++) {
-                processFontIndex(sfr, csoArray[i], codepage, fnc.getDpi());
-                characterSet.addCharacterSetOrientation(csoArray[i]);
-            }
-
-        } catch (Exception ex) {
-            throw new FontRuntimeException(
-                "Failed to load the character set metrics for code page "
-                + characterSet.getCodePage(), ex);
-        } finally {
-            try {
-                inputStream.close();
-            } catch (Exception ex) {
-                // Ignore
-            }
-        }
-
-    }
-
-    /**
-     * Load the code page information from the appropriate file. The file name
-     * to load is determined by the code page name and the file extension 'CDP'.
+     * @param path the file path
+     * @param filename the file name
+     * @return an inputStream
      *
-     * @param codePage
-     *            the code page identifier
-     * @param encoding
-     *            the encoding to use for the character decoding
+     * @throws IOException in the event that an I/O exception of some sort has occurred
      */
-    private static HashMap loadCodePage(String codePage, String encoding,
-        String path) throws IOException {
-
-        // Create the HashMap to store code page information
-        HashMap codepages = new HashMap();
-
+    private InputStream openInputStream(String path, String filename) throws IOException {
         ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
         if (classLoader == null) {
             classLoader = AFPFontReader.class.getClassLoader();
@@ -260,14 +135,12 @@ public final class AFPFontReader {
                 File file = new File(path);
                 url = file.toURI().toURL();
                 if (url == null) {
-                    String msg = "CodePage file not found for " + codePage
-                        + " in classpath: " + path;
+                    String msg = "file not found " + filename + " in classpath: " + path;
                     log.error(msg);
                     throw new FileNotFoundException(msg);
                 }
             } catch (MalformedURLException ex) {
-                String msg = "CodePage file not found for " + codePage
-                    + " in classpath: " + path;
+                String msg = "file not found " + filename + " in classpath: " + path;
                 log.error(msg);
                 throw new FileNotFoundException(msg);
             }
@@ -280,115 +153,220 @@ public final class AFPFontReader {
             throw new FileNotFoundException(msg);
         }
 
-        final String filterpattern = codePage.trim();
+        final String filterpattern = filename.trim();
         FilenameFilter filter = new FilenameFilter() {
             public boolean accept(File dir, String name) {
                 return name.startsWith(filterpattern);
             }
         };
 
-        File[] codepage = directory.listFiles(filter);
+        File[] files = directory.listFiles(filter);
 
-        if (codepage.length < 1) {
-            String msg = "CodePage file search for " + codePage + " located "
-                + codepage.length + " files";
+        if (files.length < 1) {
+            String msg = "file search for " + filename + " located "
+                + files.length + " files";
             log.error(msg);
             throw new FileNotFoundException(msg);
-        } else if (codepage.length > 1) {
-            String msg = "CodePage file search for " + codePage + " located "
-                + codepage.length + " files";
+        } else if (files.length > 1) {
+            String msg = "file search for " + filename + " located "
+                + files.length + " files";
             log.warn(msg);
         }
 
-        InputStream is = codepage[0].toURI().toURL().openStream();
+        InputStream inputStream = files[0].toURI().toURL().openStream();
 
-        if (is == null) {
-            String msg = "AFPFontReader:: loadCodePage(String):: code page file not found for "
-                + codePage;
+        if (inputStream == null) {
+            String msg = "AFPFontReader:: getInputStream():: file not found for " + filename;
             log.error(msg);
             throw new FileNotFoundException(msg);
         }
 
-        StructuredFieldReader sfr = new StructuredFieldReader(is);
-        byte[] data = sfr.getNext(CHARACTER_TABLE_SF);
+        return inputStream;
+    }
 
-        int position = 0;
-        byte[] gcgiBytes = new byte[8];
-        byte[] charBytes = new byte[1];
+    /**
+     * Closes the inputstream
+     *
+     * @param inputStream the inputstream to close
+     */
+    private void closeInputStream(InputStream inputStream) {
+        try {
+            if (inputStream != null) {
+                inputStream.close();
+            }
+        } catch (Exception ex) {
+            // Lets log at least!
+            log.error(ex.getMessage());
+        }
+    }
 
-        // Read data, ignoring bytes 0 - 2
-        for (int index = 3; index < data.length; index++) {
-            if (position < 8) {
-                // Build the graphic character global identifier key
-                gcgiBytes[position] = data[index];
-                position++;
-            } else if (position == 9) {
-                position = 0;
-                // Set the character
-                charBytes[0] = data[index];
-                String gcgiString = new String(gcgiBytes,
-                    AFPConstants.EBCIDIC_ENCODING);
-                String charString = new String(charBytes, encoding);
-//                int value = charString.charAt(0);
-                codepages.put(gcgiString, charString);
+    /**
+     * Load the font details and metrics into the CharacterSetMetric object,
+     * this will use the actual afp code page and character set files to load
+     * the object with the necessary metrics.
+     *
+     * @param characterSet the CharacterSetMetric object to populate
+     * @throws IOException if an I/O exception of some sort has occurred.
+     */
+    public void loadCharacterSetMetric(CharacterSet characterSet) throws IOException {
+
+        InputStream inputStream = null;
+
+        try {
+
+            /**
+             * Get the code page which contains the character mapping
+             * information to map the unicode character id to the graphic
+             * chracter global identifier.
+             */
+            String codePageId = new String(characterSet.getCodePage());
+            String path = characterSet.getPath();
+
+            Map/*<String,String>*/ codePage = (Map/*<String,String>*/)codePages.get(codePageId);
+
+            if (codePage == null) {
+                codePage = loadCodePage(codePageId, characterSet.getEncoding(), path);
+                codePages.put(codePageId, codePage);
+            }
+
+            /**
+             * Load the character set metric information, no need to cache this
+             * information as it should be cached by the objects that wish to
+             * load character set metric information.
+             */
+            final String characterSetName = characterSet.getName();
+
+            inputStream = openInputStream(path, characterSetName);
+
+            StructuredFieldReader structuredFieldReader = new StructuredFieldReader(inputStream);
+
+            // Process D3A789 Font Control
+            FontControl fontControl = processFontControl(structuredFieldReader);
+
+            if (fontControl != null) {
+                //process D3AE89 Font Orientation
+                CharacterSetOrientation[] characterSetOrientations
+                    = processFontOrientation(structuredFieldReader);
+
+                int dpi = fontControl.getDpi();
+
+                //process D3AC89 Font Position
+                processFontPosition(structuredFieldReader, characterSetOrientations, dpi);
+
+                //process D38C89 Font Index (per orientation)
+                for (int i = 0; i < characterSetOrientations.length; i++) {
+                    processFontIndex(structuredFieldReader,
+                            characterSetOrientations[i], codePage, dpi);
+                    characterSet.addCharacterSetOrientation(characterSetOrientations[i]);
+                }
             } else {
-                position++;
+                throw new IOException(
+                        "Failed to read font control structured field in character set "
+                        + characterSetName);
             }
+
+        } finally {
+            closeInputStream(inputStream);
         }
 
+    }
+
+    /**
+     * Load the code page information from the appropriate file. The file name
+     * to load is determined by the code page name and the file extension 'CDP'.
+     *
+     * @param codePage
+     *            the code page identifier
+     * @param encoding
+     *            the encoding to use for the character decoding
+     * @returns a code page mapping
+     */
+    private Map/*<String,String>*/ loadCodePage(String codePage, String encoding,
+        String path) throws IOException {
+
+        // Create the HashMap to store code page information
+        Map/*<String,String>*/ codePages = new java.util.HashMap/*<String,String>*/();
+
+        InputStream inputStream = null;
         try {
-            is.close();
-        } catch (Exception ex) {
-            // Ignore
+            inputStream = openInputStream(path, codePage.trim());
+
+            StructuredFieldReader structuredFieldReader = new StructuredFieldReader(inputStream);
+            byte[] data = structuredFieldReader.getNext(CHARACTER_TABLE_SF);
+
+            int position = 0;
+            byte[] gcgiBytes = new byte[8];
+            byte[] charBytes = new byte[1];
+
+            // Read data, ignoring bytes 0 - 2
+            for (int index = 3; index < data.length; index++) {
+                if (position < 8) {
+                    // Build the graphic character global identifier key
+                    gcgiBytes[position] = data[index];
+                    position++;
+                } else if (position == 9) {
+                    position = 0;
+                    // Set the character
+                    charBytes[0] = data[index];
+                    String gcgiString = new String(gcgiBytes,
+                            AFPConstants.EBCIDIC_ENCODING);
+                    String charString = new String(charBytes, encoding);
+//                int value = charString.charAt(0);
+                    codePages.put(gcgiString, charString);
+                } else {
+                    position++;
+                }
+            }
+        } finally {
+            closeInputStream(inputStream);
         }
 
-        return codepages;
-
+        return codePages;
     }
 
     /**
      * Process the font control details using the structured field reader.
      *
-     * @param sfr
+     * @param structuredFieldReader
      *            the structured field reader
      */
-    private static FontControl processFontControl(StructuredFieldReader sfr)
+    private FontControl processFontControl(StructuredFieldReader structuredFieldReader)
     throws IOException {
 
-        byte[] fncData = sfr.getNext(FONT_CONTROL_SF);
+        byte[] fncData = structuredFieldReader.getNext(FONT_CONTROL_SF);
 
 //        int position = 0;
+        FontControl fontControl = null;
+        if (fncData != null) {
+            fontControl = new FontControl();
 
-        FontControl fontControl = new AFPFontReader().new FontControl();
-
-        if (fncData[7] == (byte) 0x02) {
-            fontControl.setRelative(true);
-        }
-
-        int dpi = (((fncData[9] & 0xFF) << 8) + (fncData[10] & 0xFF)) / 10;
+            if (fncData[7] == (byte) 0x02) {
+                fontControl.setRelative(true);
+            }
 
-        fontControl.setDpi(dpi);
+            int dpi = (((fncData[9] & 0xFF) << 8) + (fncData[10] & 0xFF)) / 10;
 
+            fontControl.setDpi(dpi);
+        }
         return fontControl;
-
     }
 
     /**
      * Process the font orientation details from using the structured field
      * reader.
      *
-     * @param sfr
+     * @param structuredFieldReader
      *            the structured field reader
      */
-    private static CharacterSetOrientation[] processFontOrientation(
-        StructuredFieldReader sfr) throws IOException {
+    private CharacterSetOrientation[] processFontOrientation(
+        StructuredFieldReader structuredFieldReader) throws IOException {
 
-        byte[] data = sfr.getNext(FONT_ORIENTATION_SF);
+        byte[] data = structuredFieldReader.getNext(FONT_ORIENTATION_SF);
 
         int position = 0;
         byte[] fnoData = new byte[26];
 
-        ArrayList orientations = new ArrayList();
+        List orientations = new java.util.ArrayList();
 
         // Read data, ignoring bytes 0 - 2
         for (int index = 3; index < data.length; index++) {
@@ -434,20 +412,20 @@ public final class AFPFontReader {
      * Populate the CharacterSetOrientation object in the suplied array with the
      * font position details using the supplied structured field reader.
      *
-     * @param sfr
+     * @param structuredFieldReader
      *            the structured field reader
-     * @param csoArray
+     * @param characterSetOrientations
      *            the array of CharacterSetOrientation objects
      */
-    private static void processFontPosition(StructuredFieldReader sfr,
-        CharacterSetOrientation[] csoArray, int dpi) throws IOException {
+    private void processFontPosition(StructuredFieldReader structuredFieldReader,
+        CharacterSetOrientation[] characterSetOrientations, int dpi) throws IOException {
 
-        byte[] data = sfr.getNext(FONT_POSITION_SF);
+        byte[] data = structuredFieldReader.getNext(FONT_POSITION_SF);
 
         int position = 0;
         byte[] fpData = new byte[26];
 
-        int csoIndex = 0;
+        int characterSetOrientationIndex = 0;
         int fopFactor = 0;
 
         switch (dpi) {
@@ -475,7 +453,8 @@ public final class AFPFontReader {
 
                 position = 0;
 
-                CharacterSetOrientation cso = csoArray[csoIndex];
+                CharacterSetOrientation characterSetOrientation
+                    = characterSetOrientations[characterSetOrientationIndex];
 
                 int xHeight = ((fpData[2] & 0xFF) << 8) + (fpData[3] & 0xFF);
                 int capHeight = ((fpData[4] & 0xFF) << 8) + (fpData[5] & 0xFF);
@@ -484,12 +463,12 @@ public final class AFPFontReader {
 
                 dscHeight = dscHeight * -1;
 
-                cso.setXHeight(xHeight * fopFactor);
-                cso.setCapHeight(capHeight * fopFactor);
-                cso.setAscender(ascHeight * fopFactor);
-                cso.setDescender(dscHeight * fopFactor);
+                characterSetOrientation.setXHeight(xHeight * fopFactor);
+                characterSetOrientation.setCapHeight(capHeight * fopFactor);
+                characterSetOrientation.setAscender(ascHeight * fopFactor);
+                characterSetOrientation.setDescender(dscHeight * fopFactor);
 
-                csoIndex++;
+                characterSetOrientationIndex++;
 
                 fpData[position] = data[index];
 
@@ -503,18 +482,18 @@ public final class AFPFontReader {
     /**
      * Process the font index details for the character set orientation.
      *
-     * @param sfr
+     * @param structuredFieldReader
      *            the structured field reader
      * @param cso
      *            the CharacterSetOrientation object to populate
      * @param codepage
      *            the map of code pages
      */
-    private static void processFontIndex(StructuredFieldReader sfr,
-        CharacterSetOrientation cso, HashMap codepage, int dpi)
+    private void processFontIndex(StructuredFieldReader structuredFieldReader,
+        CharacterSetOrientation cso, Map/*<String,String>*/ codepage, int dpi)
         throws IOException {
 
-        byte[] data = sfr.getNext(FONT_INDEX_SF);
+        byte[] data = structuredFieldReader.getNext(FONT_INDEX_SF);
 
         int fopFactor = 0;
 
@@ -545,14 +524,14 @@ public final class AFPFontReader {
         // Read data, ignoring bytes 0 - 2
         for (int index = 3; index < data.length; index++) {
             if (position < 8) {
-                gcgid[position] = (byte) data[index];
+                gcgid[position] = data[index];
                 position++;
             } else if (position < 27) {
-                fiData[position - 8] = (byte) data[index];
+                fiData[position - 8] = data[index];
                 position++;
             } else if (position == 27) {
 
-                fiData[position - 8] = (byte) data[index];
+                fiData[position - 8] = data[index];
 
                 position = 0;
 
index 434f541a46346c94251b28cf7dde79584d1fd00d..f7680b15cf2ac18685a8d23a043a2eeadcb296e9 100644 (file)
@@ -19,6 +19,7 @@
 
 package org.apache.fop.render.afp.fonts;
 
+import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.util.Map;
 
@@ -49,6 +50,15 @@ public class CharacterSet {
     /** Static logging instance */
     protected static final Log log = LogFactory.getLog(CharacterSet.class.getName());
 
+    /** default codepage */
+    protected static final String DEFAULT_CODEPAGE = "T1V10500";
+
+    /** default encoding */
+    protected static final String DEFAULT_ENCODING = "Cp500";
+
+    private static final int MAX_NAME_LEN = 8;
+
+
     /** The code page to which the character set relates */
     protected String codePage;
 
@@ -64,8 +74,8 @@ public class CharacterSet {
     /** Indicator as to whether to metrics have been loaded */
     private boolean isMetricsLoaded = false;
 
-    /** The current orientation (currently only 0 is suppoted by FOP) */
-    private String currentOrientation = "0";
+    /** The current orientation (currently only 0 is supported by FOP) */
+    private final String currentOrientation = "0";
 
     /** The collection of objects for each orientation */
     private Map characterSetOrientations = null;
@@ -79,27 +89,23 @@ public class CharacterSet {
      * @param name the character set name
      * @param path the path to the installed afp fonts
      */
-    public CharacterSet(
-        String codePage,
-        String encoding,
-        String name,
-        String path) {
-
-        if (name.length() > 8) {
-            String msg = "Character set name must be a maximum of 8 characters " + name;
+    public CharacterSet(String codePage, String encoding, String name, String path) {
+        if (name.length() > MAX_NAME_LEN) {
+            String msg = "Character set name '" + name + "' must be a maximum of "
+                + MAX_NAME_LEN + " characters";
             log.error("Constructor:: " + msg);
             throw new IllegalArgumentException(msg);
         }
 
-        if (name.length() < 8) {
-            this.name = StringUtils.rpad(name, ' ', 8);
+        if (name.length() < MAX_NAME_LEN) {
+            this.name = StringUtils.rpad(name, ' ', MAX_NAME_LEN);
         } else {
             this.name = name;
         }
-
         this.codePage = codePage;
         this.encoding = encoding;
         this.path = path;
+
         this.characterSetOrientations = new java.util.HashMap(4);
     }
 
@@ -121,7 +127,7 @@ public class CharacterSet {
      * a character rotation other than 0, ascender height loses its
      * meaning when the character is lying on its side or is upside down
      * with respect to normal viewing orientation. For the general case,
-     * Ascender Height is the characters most positive y-axis value.
+     * Ascender Height is the characters most positive y-axis value.
      * For bounded character boxes, for a given character having an
      * ascender, ascender height and baseline offset are equal.
      *
@@ -157,9 +163,9 @@ public class CharacterSet {
     }
 
     /**
-     * The first character in the character set
+     * Returns the first character in the character set
      *
-     * @return the first character
+     * @return the first character in the character set
      */
     public int getFirstChar() {
         load();
@@ -169,7 +175,7 @@ public class CharacterSet {
     /**
      * Returns the last character in the character set
      *
-     * @return the last character
+     * @return the last character in the character set
      */
     public int getLastChar() {
         load();
@@ -197,6 +203,7 @@ public class CharacterSet {
 
     /**
      * XHeight refers to the height of the lower case letters above the baseline.
+     *
      * @return the typical height of characters
      */
     public int getXHeight() {
@@ -211,9 +218,9 @@ public class CharacterSet {
      * @param character the character from which the width will be calculated
      * @return the width of the character
      */
-    public int width(int character) {
+    public int getWidth(int character) {
         load();
-        return getCharacterSetOrientation().width(character);
+        return getCharacterSetOrientation().getWidth(character);
     }
 
     /**
@@ -223,24 +230,30 @@ public class CharacterSet {
     private void load() {
         if (!isMetricsLoaded) {
             AFPFontReader afpFontReader = new AFPFontReader();
-            afpFontReader.loadCharacterSetMetric(this);
-            isMetricsLoaded = true;
+            try {
+                afpFontReader.loadCharacterSetMetric(this);
+                isMetricsLoaded = true;
+            } catch (IOException e) {
+                String msg = "Failed to load the character set metrics for code page " + codePage;
+                log.error(msg);
+                throw new RuntimeException(e.getMessage());
+            }
         }
     }
 
     /**
      * Returns the AFP character set identifier
      *
-     * @return String
+     * @return the AFP character set identifier
      */
     public String getName() {
         return name;
     }
 
     /**
-     * Returns the AFP character set identifier
+     * Returns the AFP character set identifier as a byte array
      *
-     * @return the AFP character set identifier
+     * @return the AFP character set identifier as a byte array
      */
     public byte[] getNameBytes() {
         byte[] nameBytes = null;
@@ -249,8 +262,7 @@ public class CharacterSet {
         } catch (UnsupportedEncodingException usee) {
             nameBytes = name.getBytes();
             log.warn(
-                "UnsupportedEncodingException translating the name "
-                + name);
+                "UnsupportedEncodingException translating the name " + name);
         }
         return nameBytes;
     }
@@ -281,16 +293,14 @@ public class CharacterSet {
      * implementation (whenever FOP implement the mechanism). This is also
      * the case for landscape prints which use an orientation of 270 degrees,
      * in 99.9% of cases the font metrics will be the same as the 0 degrees
-     * therefore the implementation currely will always use 0 degrees.
+     * therefore the implementation currently will always use 0 degrees.
      *
      * @return characterSetOrentation The current orientation metrics.
      */
     private CharacterSetOrientation getCharacterSetOrientation() {
-
         CharacterSetOrientation c
             = (CharacterSetOrientation) characterSetOrientations.get(currentOrientation);
         return c;
-
     }
 
     /**
index be6dbf6ea46ef3f0fc54c356c1fb51efd7690c3b..e130297177efaad2efe3254fa6dbd2f40e394512 100644 (file)
@@ -183,11 +183,15 @@ public class CharacterSetOrientation {
     /**
      * Get the width (in 1/1000ths of a point size) of the character
      * identified by the parameter passed.
-     * @param character the character to evaluate
+     * @param characterIndex the character to evaluate
      * @return the widths of the character
      */
-    public int width(int character) {
-        return chars[character];
+    public int getWidth(int characterIndex) {
+        if (characterIndex >= chars.length) {
+            throw new IllegalArgumentException("Invalid character index: "
+                    + characterIndex + ", maximum is " + (chars.length - 1));
+        }
+        return chars[characterIndex];
     }
 
     /**
index f6864d73fc9875ed89ee76bb9ee5b9523e20cd25..d5beb5a3341d37d0ad09efb409f9d5327816f979 100644 (file)
@@ -125,7 +125,7 @@ public class FopCharacterSet extends CharacterSet {
      * @param character the character from which the width will be calculated
      * @return the width of the character
      */
-    public int width(int character) {
+    public int getWidth(int character) {
         return charSet.getWidth(character, size);
     }
 
index f8dd9a1ed4c29c741b29cda6eda379c666f9307d..71c5dfb6f3eb8dbbc7b4b9ea1db3fbe3d5eeaad2 100644 (file)
@@ -136,7 +136,7 @@ public class OutlineFont extends AFPFont {
      * @return the width of the character for the specified point size
      */
     public int getWidth(int character, int size) {
-        return charSet.width(character) / 1000 * size;
+        return charSet.getWidth(character) / 1000 * size;
     }
 
     /**
index 8312dca72af57d0e83f96651366dec9cda908c94..5c1696aa4aa62cc52d158d72de16d613f3d61538 100644 (file)
@@ -196,7 +196,7 @@ public class RasterFont extends AFPFont {
      * @return the width for the given point size
      */
     public int getWidth(int character, int size) {
-        return getCharacterSet(size).width(character);
+        return getCharacterSet(size).getWidth(character);
     }
 
     /**
index 1939d2ddaec4a482bb87d7b02788e45d06ea0510..48beff023e4c1ff23a33d0503d8f3b33ba23625b 100644 (file)
@@ -58,14 +58,14 @@ public class StructuredFieldReader {
      * parameter (this must be a valid MO:DCA structured field.
      * @param identifier the three byte identifier
      * @throws IOException if an I/O exception occurred
-     * @return the next structured field
+     * @return the next structured field or null when there are no more
      */
     public byte[] getNext(byte[] identifier) throws IOException {
 
         int bufferPointer = 0;
         byte[] bufferData = new byte[identifier.length + 2];
         for (int x = 0; x < identifier.length; x++) {
-            bufferData[x] = (byte) 0;
+            bufferData[x] = 0x00;
         }
 
         int c;
@@ -128,7 +128,6 @@ public class StructuredFieldReader {
 
         }
 
-        return new byte[] {
-        };
+        return null;
     }
 }