pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jahew...@apache.org
Subject svn commit: r1690799 - in /pdfbox/trunk: examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/
Date Mon, 13 Jul 2015 19:22:38 GMT
Author: jahewson
Date: Mon Jul 13 19:22:38 2015
New Revision: 1690799

URL: http://svn.apache.org/r1690799
Log:
private_annotations_flags

Modified:
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1Font.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1FontEmbedder.java

Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1.java?rev=1690799&r1=1690798&r2=1690799&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1.java
(original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1.java
Mon Jul 13 19:22:38 2015
@@ -26,7 +26,7 @@ import org.apache.pdfbox.pdmodel.font.PD
 import org.apache.pdfbox.pdmodel.font.PDType1Font;
 
 /**
- * Creates a simple document with a Type 1 font (.afm + .pfb).
+ * Creates a simple document with a Type 1 font (.pfb).
  */
 public class HelloWorldType1
 {
@@ -35,13 +35,13 @@ public class HelloWorldType1
         if (args.length != 3)
         {
             System.err.println("usage: " + HelloWorldType1.class.getName() +
-                    " <output-file> <Message> <afm-file>");
+                    " <output-file> <Message> <pfb-file>");
             System.exit(1);
         }
 
         String file = args[0];
         String message = args[1];
-        String afmPath = args[2];
+        String pfbPath = args[2];
         
         PDDocument doc = new PDDocument();
         try
@@ -49,8 +49,7 @@ public class HelloWorldType1
             PDPage page = new PDPage();
             doc.addPage(page);
 
-            PDFont font = new PDType1Font(doc, new FileInputStream(afmPath),
-                    new FileInputStream(afmPath.replace(".afm", ".pfb")));
+            PDFont font = new PDType1Font(doc, new FileInputStream(pfbPath));
 
             PDPageContentStream contents = new PDPageContentStream(doc, page);
             contents.beginText();

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1Font.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1Font.java?rev=1690799&r1=1690798&r2=1690799&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1Font.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1Font.java Mon
Jul 13 19:22:38 2015
@@ -133,13 +133,12 @@ public class PDType1Font extends PDSimpl
      * Creates a new Type 1 font for embedding.
      *
      * @param doc PDF document to write to
-     * @param afmIn AFM file stream
      * @param pfbIn PFB file stream
      * @throws IOException
      */
-    public PDType1Font(PDDocument doc, InputStream afmIn, InputStream pfbIn) throws IOException
+    public PDType1Font(PDDocument doc, InputStream pfbIn) throws IOException
     {
-        PDType1FontEmbedder embedder = new PDType1FontEmbedder(doc, dict, afmIn, pfbIn, null);
+        PDType1FontEmbedder embedder = new PDType1FontEmbedder(doc, dict, pfbIn, null);
         encoding = embedder.getFontEncoding();
         glyphList = embedder.getGlyphList();
         type1font = embedder.getType1Font();
@@ -153,14 +152,12 @@ public class PDType1Font extends PDSimpl
      * Creates a new Type 1 font for embedding.
      *
      * @param doc PDF document to write to
-     * @param afmIn AFM file stream
      * @param pfbIn PFB file stream
      * @throws IOException
      */
-    public PDType1Font(PDDocument doc, InputStream afmIn, InputStream pfbIn, Encoding encoding)
-            throws IOException
+    public PDType1Font(PDDocument doc, InputStream pfbIn, Encoding encoding) throws IOException
     {
-        PDType1FontEmbedder embedder = new PDType1FontEmbedder(doc, dict, afmIn, pfbIn, encoding);
+        PDType1FontEmbedder embedder = new PDType1FontEmbedder(doc, dict, pfbIn, encoding);
         this.encoding = encoding;
         glyphList = embedder.getGlyphList();
         type1font = embedder.getType1Font();

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1FontEmbedder.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1FontEmbedder.java?rev=1690799&r1=1690798&r2=1690799&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1FontEmbedder.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1FontEmbedder.java
Mon Jul 13 19:22:38 2015
@@ -21,20 +21,16 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
-import org.apache.fontbox.afm.AFMParser;
 import org.apache.fontbox.afm.FontMetrics;
 import org.apache.fontbox.pfb.PfbParser;
 import org.apache.fontbox.type1.Type1Font;
-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.io.IOUtils;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.common.COSArrayList;
 import org.apache.pdfbox.pdmodel.common.PDRectangle;
 import org.apache.pdfbox.pdmodel.common.PDStream;
-import org.apache.pdfbox.pdmodel.font.encoding.DictionaryEncoding;
 import org.apache.pdfbox.pdmodel.font.encoding.Encoding;
 import org.apache.pdfbox.pdmodel.font.encoding.GlyphList;
 import org.apache.pdfbox.pdmodel.font.encoding.Type1Encoding;
@@ -47,42 +43,37 @@ import org.apache.pdfbox.pdmodel.font.en
 class PDType1FontEmbedder
 {
     private final Encoding fontEncoding;
-    private final FontMetrics metrics;
     private final Type1Font type1;
     
     /**
-     * This will load a afm and pfb to be embedding into a document.
+     * This will load a PFB to be embedded into a document.
      *
      * @param doc The PDF document that will hold the embedded font.
      * @param dict The Font dictionary to write to.
-     * @param afmStream The afm input.
      * @param pfbStream The pfb input.
      * @throws IOException If there is an error loading the data.
      */
-    PDType1FontEmbedder(PDDocument doc, COSDictionary dict, InputStream afmStream,
-                               InputStream pfbStream, Encoding encoding) throws IOException
+    PDType1FontEmbedder(PDDocument doc, COSDictionary dict, InputStream pfbStream,
+                        Encoding encoding) throws IOException
     {
         dict.setItem(COSName.SUBTYPE, COSName.TYPE1);
 
-        // read the afm
-        AFMParser afmParser = new AFMParser(afmStream);
-        metrics = afmParser.parse();
+        // read the pfb
+        byte[] pfbBytes = IOUtils.toByteArray(pfbStream);
+        PfbParser pfbParser = new PfbParser(new ByteArrayInputStream(pfbBytes));
+        type1 = Type1Font.createWithPFB(new ByteArrayInputStream(pfbBytes));
+        
         if (encoding == null)
         {
-            this.fontEncoding = encodingFromAFM(metrics);
+            fontEncoding = Type1Encoding.fromFontBox(type1.getEncoding());
         }
         else
         {
-            this.fontEncoding = encoding;
+            fontEncoding = encoding;
         }
 
         // build font descriptor
-        PDFontDescriptor fd = buildFontDescriptor(metrics);
-
-        // read the pfb
-        byte[] pfbBytes = IOUtils.toByteArray(pfbStream);
-        PfbParser pfbParser = new PfbParser(new ByteArrayInputStream(pfbBytes));
-        type1 = Type1Font.createWithPFB(new ByteArrayInputStream(pfbBytes));
+        PDFontDescriptor fd = buildFontDescriptor(type1);
 
         PDStream fontStream = new PDStream(doc, pfbParser.getInputStream(), false);
         fontStream.getStream().setInt("Length", pfbParser.size());
@@ -95,14 +86,14 @@ class PDType1FontEmbedder
 
         // set the values
         dict.setItem(COSName.FONT_DESC, fd);
-        dict.setName(COSName.BASE_FONT, metrics.getFontName());
+        dict.setName(COSName.BASE_FONT, type1.getName());
 
         // widths
         List<Integer> widths = new ArrayList<Integer>(256);
         for (int code = 0; code <= 255; code++)
         {
             String name = fontEncoding.getName(code);
-            int width = Math.round(metrics.getCharacterWidth(name));
+            int width = Math.round(type1.getWidth(name));
             widths.add(width);
         }
         
@@ -112,7 +103,30 @@ class PDType1FontEmbedder
     }
 
     /**
-     * Returns a PDFontDescriptor for the given AFM.
+     * Returns a PDFontDescriptor for the given PFB.
+     */
+    static PDFontDescriptor buildFontDescriptor(Type1Font type1)
+    {
+        boolean isSymbolic = type1.getEncoding()
+                instanceof org.apache.fontbox.encoding.BuiltInEncoding;
+
+        PDFontDescriptor fd = new PDFontDescriptor();
+        fd.setFontName(type1.getName());
+        fd.setFontFamily(type1.getFamilyName());
+        fd.setNonSymbolic(!isSymbolic);
+        fd.setSymbolic(isSymbolic);
+        fd.setFontBoundingBox(new PDRectangle(type1.getFontBBox()));
+        fd.setItalicAngle(type1.getItalicAngle());
+        fd.setAscent(type1.getFontBBox().getUpperRightY());
+        fd.setDescent(type1.getFontBBox().getLowerLeftY());
+        fd.setCapHeight(type1.getBlueValues().get(2).floatValue());
+        fd.setStemV(0); // for PDF/A
+        return fd;
+    }
+
+
+    /**
+     * Returns a PDFontDescriptor for the given AFM. Used only for Standard 14 fonts.
      *
      * @param metrics AFM
      */
@@ -136,34 +150,7 @@ class PDType1FontEmbedder
         fd.setStemV(0); // for PDF/A
         return fd;
     }
-
-    // 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 encodingFromAFM(FontMetrics metrics) throws IOException
-    {
-        Type1Encoding encoding = new Type1Encoding(metrics);
-
-        COSArray differences = new COSArray();
-        differences.add(COSInteger.ZERO);
-        for (int i = 0; i < 256; i++)
-        {
-            differences.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
-        differences.set(0337 + 1, COSName.getPDFName("germandbls"));
-        differences.set(0344 + 1, COSName.getPDFName("adieresis"));
-        differences.set(0366 + 1, COSName.getPDFName("odieresis"));
-        differences.set(0374 + 1, COSName.getPDFName("udieresis"));
-        differences.set(0304 + 1, COSName.getPDFName("Adieresis"));
-        differences.set(0326 + 1, COSName.getPDFName("Odieresis"));
-        differences.set(0334 + 1, COSName.getPDFName("Udieresis"));
-
-        return new DictionaryEncoding(COSName.STANDARD_ENCODING, differences);
-    }
-
+    
     /**
      * Returns the font's encoding.
      */
@@ -181,14 +168,6 @@ class PDType1FontEmbedder
     }
 
     /**
-     * Returns the font's metrics.
-     */
-    public FontMetrics getFontMetrics()
-    {
-        return metrics;
-    }
-
-    /**
      * Returns the Type 1 font.
      */
     public Type1Font getType1Font()



Mime
View raw message