pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r1512661 - in /pdfbox/trunk: pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/ preflight/src/main/java/org/apache/pdfbox/preflight/font/
Date Sat, 10 Aug 2013 12:06:09 GMT
Author: lehmi
Date: Sat Aug 10 12:06:09 2013
New Revision: 1512661

URL: http://svn.apache.org/r1512661
Log:
PDFBOX-1663: avoid ArrayOutOfBounds-Exception when dealing with monospaced fonts, changed
width values from float to int

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1AfmPfbFont.java
    pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/font/Type3FontValidator.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java?rev=1512661&r1=1512660&r2=1512661&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java Sat Aug 10
12:06:09 2013
@@ -88,7 +88,7 @@ public abstract class PDFont implements 
     /**
      * A list a floats representing the widths.
      */
-    private List<Float> widths = null;
+    private List<Integer> widths = null;
 
     /**
      * The static map of the default Adobe font metrics.
@@ -746,14 +746,14 @@ public abstract class PDFont implements 
      * 
      * @return The widths of the characters.
      */
-    public List<Float> getWidths()
+    public List<Integer> getWidths()
     {
         if (widths == null)
         {
             COSArray array = (COSArray) font.getDictionaryObject(COSName.WIDTHS);
             if (array != null)
             {
-                widths = COSArrayList.convertFloatCOSArrayToList(array);
+                widths = COSArrayList.convertIntegerCOSArrayToList(array);
             }
         }
         return widths;
@@ -764,7 +764,7 @@ public abstract class PDFont implements 
      * 
      * @param widthsList The widths of the character codes.
      */
-    public void setWidths(List<Float> widthsList)
+    public void setWidths(List<Integer> widthsList)
     {
         widths = widthsList;
         font.setItem(COSName.WIDTHS, COSArrayList.converterToCOSArray(widths));

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java?rev=1512661&r1=1512660&r2=1512661&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java Sat
Aug 10 12:06:09 2013
@@ -279,7 +279,6 @@ public class PDTrueTypeFont extends PDSi
             fd.setSymbolic(isSymbolic);
             fd.setNonSymbolic(!isSymbolic);
 
-            // todo retval.setFixedPitch
             // todo retval.setItalic
             // todo retval.setAllCap
             // todo retval.setSmallCap
@@ -354,14 +353,16 @@ public class PDTrueTypeFont extends PDSi
             int[] widthValues = hMet.getAdvanceWidth();
             // some monospaced fonts provide only one value for the width
             // instead of an array containing the same value for every glyphid
-            boolean isMonospaced = widthValues.length == 1;
+            boolean isMonospaced = fd.isFixedPitch();
             int nWidths = lastChar - firstChar + 1;
-            List<Float> widths = new ArrayList<Float>(nWidths);
-            // width of the .notdef character.
-            Float zero = Float.valueOf(widthValues[0] * scaling);
+            List<Integer> widths = new ArrayList<Integer>(nWidths);
+            // use the first width as default
+            // proportional fonts -> width of the .notdef character
+            // monospaced-fonts -> the first width
+            int defaultWidth = Math.round(widthValues[0] * scaling);
             for (int i = 0; i < nWidths; i++)
             {
-                widths.add(zero);
+                widths.add(defaultWidth);
             }
             // Encoding singleton to have acces to the chglyph name to
             // unicode cpoint point mapping of Adobe's glyphlist.txt
@@ -383,11 +384,11 @@ public class PDTrueTypeFont extends PDSi
                 {
                     if (isMonospaced)
                     {
-                        widths.set(e.getKey().intValue() - firstChar, widthValues[0] * scaling);
+                        widths.set(e.getKey().intValue() - firstChar, defaultWidth);
                     }
                     else
                     {
-                        widths.set(e.getKey().intValue() - firstChar, widthValues[gid] *
scaling);
+                        widths.set(e.getKey().intValue() - firstChar, Math.round(widthValues[gid]
* scaling));
                     }
                 }
             }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1AfmPfbFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1AfmPfbFont.java?rev=1512661&r1=1512660&r2=1512661&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1AfmPfbFont.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1AfmPfbFont.java
Sat Aug 10 12:06:09 2013
@@ -27,24 +27,21 @@ import java.util.List;
 import org.apache.fontbox.afm.AFMParser;
 import org.apache.fontbox.afm.CharMetric;
 import org.apache.fontbox.afm.FontMetric;
-
 import org.apache.fontbox.pfb.PfbParser;
-
+import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSInteger;
+import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.encoding.AFMEncoding;
 import org.apache.pdfbox.encoding.DictionaryEncoding;
 import org.apache.pdfbox.encoding.Encoding;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.common.PDRectangle;
 import org.apache.pdfbox.pdmodel.common.PDStream;
-import org.apache.pdfbox.cos.COSArray;
-import org.apache.pdfbox.cos.COSDictionary;
-import org.apache.pdfbox.cos.COSInteger;
-import org.apache.pdfbox.cos.COSName;
 
 /**
- * This is implementation of the Type1 Font
- * with a afm and a pfb file.
- *
+ * This is implementation of the Type1 Font with a afm and a pfb file.
+ * 
  * @author <a href="mailto:m.g.n@gmx.de">Michael Niedermair</a>
  * @version $Revision: 1.5 $
  */
@@ -62,12 +59,12 @@ public class PDType1AfmPfbFont extends P
 
     /**
      * Create a new object.
-     * @param doc       The PDF document that will hold the embedded font.
-     * @param afmname   The font filename.
+     * 
+     * @param doc The PDF document that will hold the embedded font.
+     * @param afmname The font filename.
      * @throws IOException If there is an error loading the data.
      */
-    public PDType1AfmPfbFont(final PDDocument doc, final String afmname)
-        throws IOException
+    public PDType1AfmPfbFont(final PDDocument doc, final String afmname) throws IOException
     {
 
         super();
@@ -79,13 +76,13 @@ public class PDType1AfmPfbFont extends P
 
     /**
      * Create a new object.
-     * @param doc       The PDF document that will hold the embedded font.
-     * @param afm       The afm input.
-     * @param pfb       The pfb input.
+     * 
+     * @param doc The PDF document that will hold the embedded font.
+     * @param afm The afm input.
+     * @param pfb The pfb input.
      * @throws IOException If there is an error loading the data.
      */
-    public PDType1AfmPfbFont(final PDDocument doc, final InputStream afm, final InputStream
pfb)
-        throws IOException
+    public PDType1AfmPfbFont(final PDDocument doc, final InputStream afm, final InputStream
pfb) throws IOException
     {
         super();
         load(doc, afm, pfb);
@@ -93,14 +90,13 @@ public class PDType1AfmPfbFont extends P
 
     /**
      * This will load a afm and pfb to be embedding into a document.
-     *
-     * @param doc   The PDF document that will hold the embedded font.
-     * @param afm   The afm input.
-     * @param pfb   The pfb input.
+     * 
+     * @param doc The PDF document that will hold the embedded font.
+     * @param afm The afm input.
+     * @param pfb The pfb input.
      * @throws IOException If there is an error loading the data.
      */
-    private void load(final PDDocument doc, final InputStream afm,
-            final InputStream pfb) throws IOException
+    private void load(final PDDocument doc, final InputStream afm, final InputStream pfb)
throws IOException
     {
 
         PDFontDescriptorDictionary fd = new PDFontDescriptorDictionary();
@@ -110,13 +106,11 @@ public class PDType1AfmPfbFont extends P
         PfbParser pfbparser = new PfbParser(pfb);
         pfb.close();
 
-        PDStream fontStream = new PDStream(doc, pfbparser.getInputStream(),
-                false);
+        PDStream fontStream = new PDStream(doc, pfbparser.getInputStream(), false);
         fontStream.getStream().setInt("Length", pfbparser.size());
         for (int i = 0; i < pfbparser.getLengths().length; i++)
         {
-            fontStream.getStream().setInt("Length" + (i + 1),
-                    pfbparser.getLengths()[i]);
+            fontStream.getStream().setInt("Length" + (i + 1), pfbparser.getLengths()[i]);
         }
         fontStream.addCompression();
         fd.setFontFile(fontStream);
@@ -149,10 +143,10 @@ public class PDType1AfmPfbFont extends P
         List<CharMetric> listmetric = metric.getCharMetrics();
         Encoding encoding = getFontEncoding();
         int maxWidths = 256;
-        List<Float> widths = new ArrayList<Float>(maxWidths);
-        float zero = 250;
+        List<Integer> widths = new ArrayList<Integer>(maxWidths);
+        int zero = 250;
         Iterator<CharMetric> iter = listmetric.iterator();
-        for( int i=0; i<maxWidths; i++ )
+        for (int i = 0; i < maxWidths; i++)
         {
             widths.add(zero);
         }
@@ -166,44 +160,44 @@ public class PDType1AfmPfbFont extends P
                 lastchar = Math.max(lastchar, n);
                 if (m.getWx() > 0)
                 {
-                    float width = m.getWx();
-                    widths.set(n,new Float(width));
+                    int width = Math.round(m.getWx());
+                    widths.set(n, width);
                     // germandbls has 2 character codes !! Don't ask me why .....
                     // StandardEncoding = 0373 = 251
                     // WinANSIEncoding = 0337 = 223
                     if (m.getName().equals("germandbls") && n != 223)
                     {
-                        widths.set(0337,new Float(width));
+                        widths.set(0337, width);
                     }
                 }
             }
-            else 
+            else
             {
                 // my AFMPFB-Fonts has no character-codes for german umlauts
                 // so that I've to add them here by hand
                 if (m.getName().equals("adieresis"))
                 {
-                    widths.set(0344,(Float)widths.get(encoding.getCode("a")));
+                    widths.set(0344, widths.get(encoding.getCode("a")));
                 }
                 else if (m.getName().equals("odieresis"))
                 {
-                    widths.set(0366,(Float)widths.get(encoding.getCode("o")));
+                    widths.set(0366, widths.get(encoding.getCode("o")));
                 }
                 else if (m.getName().equals("udieresis"))
                 {
-                    widths.set(0374,(Float)widths.get(encoding.getCode("u")));
+                    widths.set(0374, widths.get(encoding.getCode("u")));
                 }
                 else if (m.getName().equals("Adieresis"))
                 {
-                    widths.set(0304,(Float)widths.get(encoding.getCode("A")));
+                    widths.set(0304, widths.get(encoding.getCode("A")));
                 }
                 else if (m.getName().equals("Odieresis"))
                 {
-                    widths.set(0326,(Float)widths.get(encoding.getCode("O")));
+                    widths.set(0326, widths.get(encoding.getCode("O")));
                 }
                 else if (m.getName().equals("Udieresis"))
                 {
-                    widths.set(0334,(Float)widths.get(encoding.getCode("U")));
+                    widths.set(0334, widths.get(encoding.getCode("U")));
                 }
             }
         }
@@ -213,28 +207,28 @@ public class PDType1AfmPfbFont extends P
     }
 
     /*
-     *  This will generate a Encoding from the AFM-Encoding, because the AFM-Enconding isn't
exported to the pdf
-     *  and consequently the StandardEncoding is used so that any special character is missing
-     *  I've copied the code from the pdfbox-forum posted by V0JT4 and made some additions
concerning german umlauts
-     *  see also https://sourceforge.net/forum/message.php?msg_id=4705274
+     * This will generate a Encoding from the AFM-Encoding, because the AFM-Enconding isn't
exported to the pdf and
+     * consequently the StandardEncoding is used so that any special character is missing
I've copied the code from the
+     * pdfbox-forum posted by V0JT4 and made some additions concerning german umlauts see
also
+     * https://sourceforge.net/forum/message.php?msg_id=4705274
      */
-    private DictionaryEncoding afmToDictionary(AFMEncoding encoding) throws java.io.IOException

+    private DictionaryEncoding afmToDictionary(AFMEncoding encoding) throws java.io.IOException
     {
         COSArray array = new COSArray();
         array.add(COSInteger.ZERO);
-        for (int i = 0; i < 256; i++) 
+        for (int i = 0; i < 256; i++)
         {
             array.add(COSName.getPDFName(encoding.getName(i)));
         }
         // my AFMPFB-Fonts has no character-codes for german umlauts
         // so that I've to add them here by hand
-        array.set( 0337+1, COSName.getPDFName("germandbls"));
-        array.set( 0344+1, COSName.getPDFName("adieresis"));
-        array.set( 0366+1, COSName.getPDFName("odieresis"));
-        array.set( 0374+1, COSName.getPDFName("udieresis"));
-        array.set( 0304+1, COSName.getPDFName("Adieresis"));
-        array.set( 0326+1, COSName.getPDFName("Odieresis"));
-        array.set( 0334+1, COSName.getPDFName("Udieresis"));
+        array.set(0337 + 1, COSName.getPDFName("germandbls"));
+        array.set(0344 + 1, COSName.getPDFName("adieresis"));
+        array.set(0366 + 1, COSName.getPDFName("odieresis"));
+        array.set(0374 + 1, COSName.getPDFName("udieresis"));
+        array.set(0304 + 1, COSName.getPDFName("Adieresis"));
+        array.set(0326 + 1, COSName.getPDFName("Odieresis"));
+        array.set(0334 + 1, COSName.getPDFName("Udieresis"));
 
         COSDictionary dictionary = new COSDictionary();
         dictionary.setItem(COSName.NAME, COSName.ENCODING);

Modified: pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/font/Type3FontValidator.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/font/Type3FontValidator.java?rev=1512661&r1=1512660&r2=1512661&view=diff
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/font/Type3FontValidator.java
(original)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/font/Type3FontValidator.java
Sat Aug 10 12:06:09 2013
@@ -276,7 +276,7 @@ public class Type3FontValidator extends 
      */
     private void checkCharProcsAndMetrics() throws ValidationException
     {
-        List<Float> widths = font.getWidths();
+        List<Integer> widths = font.getWidths();
         if (widths == null || widths.isEmpty())
         {
             this.fontContainer.push(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
@@ -460,7 +460,8 @@ public class Type3FontValidator extends 
                     }
                     catch (IOException e)
                     {
-                        context.addValidationError(new ValidationError(PreflightConstants.ERROR_FONTS_DAMAGED,
"Unable to valid the Type3 : " + e.getMessage()));
+                        context.addValidationError(new ValidationError(PreflightConstants.ERROR_FONTS_DAMAGED,
+                                "Unable to valid the Type3 : " + e.getMessage()));
                     }
                 }
             }



Mime
View raw message