svn commit: r1739083 [1/2] - in /xmlgraphics/fop-pdf-images/trunk: lib/ src/java/org/apache/fop/render/pdf/pdfbox/ test/java/org/apache/fop/render/pdf/

[email protected]
Newsgroups gmane.text.xml.fop.cvs
Message-ID <[email protected]>
Author: ssteiner
Date: Thu Apr 14 12:52:43 2016
New Revision: 1739083

URL: http://svn.apache.org/viewvc?rev=1739083&view=rev
Log:
FOP-2562: Update to PDFBox 2

Added:
    xmlgraphics/fop-pdf-images/trunk/lib/fontbox-2.0.0.jar
      - copied unchanged from r1739077, xmlgraphics/fop-pdf-images/branches/Temp_PDFBox2/lib/fontbox-2.0.0.jar
    xmlgraphics/fop-pdf-images/trunk/lib/pdfbox-2.0.0.jar
      - copied unchanged from r1739077, xmlgraphics/fop-pdf-images/branches/Temp_PDFBox2/lib/pdfbox-2.0.0.jar
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/FontContainer.java
      - copied unchanged from r1739077, xmlgraphics/fop-pdf-images/branches/Temp_PDFBox2/src/java/org/apache/fop/render/pdf/pdfbox/FontContainer.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MaximumProfileTable.java
      - copied unchanged from r1739077, xmlgraphics/fop-pdf-images/branches/Temp_PDFBox2/src/java/org/apache/fop/render/pdf/pdfbox/MaximumProfileTable.java
Removed:
    xmlgraphics/fop-pdf-images/trunk/lib/fontbox-2.0.0-SNAPSHOT.jar
    xmlgraphics/fop-pdf-images/trunk/lib/jempbox-2.0.0-SNAPSHOT.jar
    xmlgraphics/fop-pdf-images/trunk/lib/jempbox.LICENSE.txt
    xmlgraphics/fop-pdf-images/trunk/lib/jempbox.NOTICE.txt
    xmlgraphics/fop-pdf-images/trunk/lib/pdfbox-2.0.0-SNAPSHOT.jar
Modified:
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/AbstractPDFBoxHandler.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/FOPPDFMultiByteFont.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/FOPPDFSingleByteFont.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/ImageConverterPDF2G2D.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeCFFFonts.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeFonts.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeFontsPDFWriter.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeTTFonts.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeType1Fonts.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFUtil.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFWriter.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PSPDFGraphics2D.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PageParentTreeFinder.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PreloaderPDF.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/StructureTreeMerger.java
    xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/UniqueName.java
    xmlgraphics/fop-pdf-images/trunk/test/java/org/apache/fop/render/pdf/DocumentRootModifierTestCase.java
    xmlgraphics/fop-pdf-images/trunk/test/java/org/apache/fop/render/pdf/PDFBoxAdapterTestCase.java
    xmlgraphics/fop-pdf-images/trunk/test/java/org/apache/fop/render/pdf/PDFRotateTestCase.java
    xmlgraphics/fop-pdf-images/trunk/test/java/org/apache/fop/render/pdf/PageParentTreeFinderTestCase.java
    xmlgraphics/fop-pdf-images/trunk/test/java/org/apache/fop/render/pdf/StructureTreeMergerTestCase.java
    xmlgraphics/fop-pdf-images/trunk/test/java/org/apache/fop/render/pdf/TaggedPDFConductorTestCase.java

Modified: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/AbstractPDFBoxHandler.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/AbstractPDFBoxHandler.java?rev=1739083&r1=1739082&r2=1739083&view=diff
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/AbstractPDFBoxHandler.java (original)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/AbstractPDFBoxHandler.java Thu Apr 14 12:52:43 2016
@@ -93,7 +93,7 @@ public abstract class AbstractPDFBoxHand
 
         Map<Object, Object> objectCache = getObjectCache(originalImageUri, userAgent);
 
-        PDPage page = (PDPage) pddoc.getDocumentCatalog().getAllPages().get(selectedPage);
+        PDPage page = (PDPage) pddoc.getDocumentCatalog().getPages().get(selectedPage);
 
         if (targetPage.getPDFResources().getParentResources() == null) {
             PDFResources res = pdfDoc.getFactory().makeResources();

Modified: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/FOPPDFMultiByteFont.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/FOPPDFMultiByteFont.java?rev=1739083&r1=1739082&r2=1739083&view=diff
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/FOPPDFMultiByteFont.java (original)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/FOPPDFMultiByteFont.java Thu Apr 14 12:52:43 2016
@@ -19,18 +19,19 @@ package org.apache.fop.render.pdf.pdfbox
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.Collection;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.TreeMap;
 
 import org.apache.commons.io.IOUtils;
+import org.apache.fontbox.cff.CFFCharset;
 import org.apache.fontbox.cff.CFFFont;
+import org.apache.fontbox.cff.CFFStandardString;
 import org.apache.fontbox.cmap.CMap;
-import org.apache.fontbox.ttf.CMAPEncodingEntry;
+
+import org.apache.fontbox.ttf.CmapSubtable;
 import org.apache.fontbox.ttf.GlyphData;
-import org.apache.fontbox.ttf.MaximumProfileTable;
 import org.apache.fontbox.ttf.TrueTypeFont;
 
 import org.apache.pdfbox.cos.COSArray;
@@ -38,16 +39,15 @@ import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSObject;
-import org.apache.pdfbox.encoding.Encoding;
 import org.apache.pdfbox.pdmodel.common.PDStream;
 import org.apache.pdfbox.pdmodel.font.PDCIDFont;
-import org.apache.pdfbox.pdmodel.font.PDCIDFontType0Font;
-import org.apache.pdfbox.pdmodel.font.PDCIDFontType2Font;
+import org.apache.pdfbox.pdmodel.font.PDCIDFontType0;
+import org.apache.pdfbox.pdmodel.font.PDCIDFontType2;
 import org.apache.pdfbox.pdmodel.font.PDFont;
-import org.apache.pdfbox.pdmodel.font.PDFontDescriptorDictionary;
-import org.apache.pdfbox.pdmodel.font.PDFontFactory;
+import org.apache.pdfbox.pdmodel.font.PDFontDescriptor;
 import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
 import org.apache.pdfbox.pdmodel.font.PDType0Font;
+import org.apache.pdfbox.pdmodel.font.encoding.GlyphList;
 
 import org.apache.fop.fonts.CIDFontType;
 import org.apache.fop.fonts.CustomFont;
@@ -63,7 +63,7 @@ public class FOPPDFMultiByteFont extends
     private Map<String, Integer> charMapGlobal = new LinkedHashMap<String, Integer>();
     private MergeFonts mergeFonts;
     //private Map<String, GlyphData> glyphs = new HashMap<String, GlyphData>();
-    private final Map<COSDictionary, PDFont> fontMap = new HashMap<COSDictionary, PDFont>();
+    private final Map<COSDictionary, FontContainer> fontMap = new HashMap<COSDictionary, FontContainer>();
 
     public FOPPDFMultiByteFont(COSDictionary fontData, String name) throws IOException {
         super(null, EmbeddingMode.SUBSET);
@@ -75,22 +75,23 @@ public class FOPPDFMultiByteFont extends
     }
 
     public String addFont(COSDictionary fontData) throws IOException {
-        PDFont font = getFont(fontData);
-        setProperties(this, font);
-        PDFont mainFont = font;
+        FontContainer font = getFont(fontData);
+        setProperties(this, font.font);
+        PDCIDFont mainFont = null;
         TrueTypeFont ttf = null;
-        if (font instanceof PDType0Font) {
-            PDCIDFont cidFont = (PDCIDFont) ((PDType0Font) font).getDescendantFont();
-            setDefaultWidth((int) cidFont.getDefaultWidth());
+        if (font.font instanceof PDType0Font) {
+            PDCIDFont cidFont = ((PDType0Font) font.font).getDescendantFont();
+            int dw = cidFont.getCOSObject().getInt(COSName.DW);
+            setDefaultWidth(dw);
             mainFont = cidFont;
-            if (cidFont instanceof PDCIDFontType0Font) {
+            if (cidFont instanceof PDCIDFontType0) {
                 setCIDType(CIDFontType.CIDTYPE0);
                 setFontType(FontType.CIDTYPE0);
             } else {
-                ttf = ((PDCIDFontType2Font) cidFont).getTTFFont();
+                ttf = ((PDCIDFontType2) cidFont).getTrueTypeFont();
             }
         } else {
-            ttf = ((PDTrueTypeFont) font).getTTFFont();
+            ttf = ((PDTrueTypeFont) font.font).getTrueTypeFont();
             setDefaultWidth(1000);
         }
         GlyphData[] glyphData = new GlyphData[0];
@@ -101,26 +102,22 @@ public class FOPPDFMultiByteFont extends
         if (charMapGlobal.isEmpty()) {
             oldToNewGIMap.put(0, 0); // .notdef glyph
         }
-        CMap c = mainFont.getToUnicodeCMap();
-        if (c == null) {
-            c = font.getToUnicodeCMap();
-        }
-        Map<Integer, String> mapping = getMapping(mainFont, c, glyphData.length);
+        CMap c = font.getToUnicodeCMap();
+        Map<Integer, String> mapping = getMapping(font, c, glyphData.length);
         //if (glyphData.length > 0 && differentGlyphData(glyphData, mapping)) {
         //    return null;
         //}
         Map<Integer, String> gidToGlyph = new TreeMap<Integer, String>(mapping);
-        if (mainFont instanceof PDTrueTypeFont) {
-            CMAPEncodingEntry cmap = ttf.getCMAP().getCmaps()[0];
+        if (font.font instanceof PDTrueTypeFont) {
+            CmapSubtable cmap = ttf.getCmap().getCmaps()[0];
             gidToGlyph.clear();
-            int[] gidToCode = cmap.getGlyphIdToCharacterCode();
-            for (int i = 1; i < glyphData.length && i < gidToCode.length; i++) {
-                String mappedChar = mapping.get(gidToCode[i]);
+            for (int i = 1; i < glyphData.length; i++) {
+                String mappedChar = mapping.get(cmap.getCharacterCode(i));
                 gidToGlyph.put(i, mappedChar);
             }
         }
         readCharMap(font, gidToGlyph, glyphData, mainFont, oldToNewGIMap);
-        InputStream ffr = readFontFile(mainFont);
+        InputStream ffr = readFontFile(font.font);
         if (mergeFonts == null) {
             if (ttf != null) {
                 mergeFonts = new MergeTTFonts(null);
@@ -141,8 +138,8 @@ public class FOPPDFMultiByteFont extends
         return getFontName();
     }
 
-    private void readCharMap(PDFont font, Map<Integer, String> gidToGlyph, GlyphData[] glyphData,
-                             PDFont mainFont, Map<Integer, Integer> oldToNewGIMap) {
+    private void readCharMap(FontContainer font, Map<Integer, String> gidToGlyph, GlyphData[] glyphData,
+                             PDCIDFont mainFont, Map<Integer, Integer> oldToNewGIMap) throws IOException {
         int widthPos = font.getFirstChar() + 1;
         for (Map.Entry<Integer, String> i : gidToGlyph.entrySet()) {
             String mappedChar = i.getValue();
@@ -173,12 +170,12 @@ public class FOPPDFMultiByteFont extends
                 }
                 oldToNewGIMap.put(key, glyph);
                 if (!skipWidth) {
-                    if (!(mainFont instanceof PDTrueTypeFont)) {
+                    if (!(font.font instanceof PDTrueTypeFont)) {
                         widthPos = key;
                     }
-                    float w = font.getFontWidth(widthPos);
+                    float w = font.font.getWidth(widthPos);
                     if (w >= 0) {
-                        if (mainFont instanceof PDCIDFontType0Font) {
+                        if (mainFont instanceof PDCIDFontType0) {
                             newWidth.put(key, (int)w);
                         } else {
                             newWidth.put(glyph, (int)w);
@@ -192,14 +189,12 @@ public class FOPPDFMultiByteFont extends
         }
     }
 
-    private Map<Integer, String> getMapping(PDFont font, CMap c, int len) throws IOException {
+    private Map<Integer, String> getMapping(FontContainer font, CMap c, int len) throws IOException {
         Map<Integer, String> mapping = new HashMap<Integer, String>();
-        if (font instanceof PDCIDFontType0Font) {
-            Collection<CFFFont.Mapping> mappings =
-                    ((PDCIDFontType0Font) font).getType1CFont().getCFFFont().getMappings();
-            for (CFFFont.Mapping m : mappings) {
-                String character = Encoding.getCharacterForName(m.getName());
-                mapping.put(m.getSID(), character);
+        if (font.font instanceof PDType0Font) {
+            PDCIDFont cidFont = ((PDType0Font) font.font).getDescendantFont();
+            if (cidFont instanceof PDCIDFontType0) {
+                mapping = getStrings(((PDCIDFontType0) cidFont).getCFFFont());
             }
         }
         if (c != null) {
@@ -207,12 +202,8 @@ public class FOPPDFMultiByteFont extends
             if (last == -1) {
                 last = len;
             }
-            int size = 1;
-            if (c.hasTwoByteMappings()) {
-                size = 2;
-            }
             for (int i = font.getFirstChar(); i <= last; i++) {
-                String l = c.lookup(i, size);
+                String l = c.toUnicode(i);
                 if (l != null) {
                     mapping.put(i, l);
                 }
@@ -221,6 +212,26 @@ public class FOPPDFMultiByteFont extends
         return mapping;
     }
 
+    private Map<Integer, String> getStrings(CFFFont ff) throws IOException {
+        CFFCharset cs = ff.getCharset();
+        Map<Integer, String> strings = new LinkedHashMap<Integer, String>();
+        for (int gid = 0; gid < 256; gid++) {
+            int sid = cs.getCIDForGID(gid);
+            if (sid != 0) {
+                strings.put(sid, GlyphList.getAdobeGlyphList().toUnicode(readString(sid)));
+            }
+        }
+        return strings;
+    }
+
+    private String readString(int index) throws IOException {
+        if (index >= 0 && index <= 390) {
+            return CFFStandardString.getName(index);
+        }
+        // technically this maps to .notdef, but we need a unique glyph name
+        return "SID" + index;
+    }
+
 //        private boolean differentGlyphData(GlyphData[] data, Map<Integer, String> mapping) throws IOException {
 //            Map<String, Integer> tmpMap = new HashMap<String, Integer>();
 //            for (Map.Entry<Integer, String> entry : mapping.entrySet()) {
@@ -245,7 +256,11 @@ public class FOPPDFMultiByteFont extends
 //        }
 
     private InputStream readFontFile(PDFont font) throws IOException {
-        PDFontDescriptorDictionary fd = (PDFontDescriptorDictionary) font.getFontDescriptor();
+        PDFontDescriptor fd = font.getFontDescriptor();
+        if (font instanceof PDType0Font) {
+            PDCIDFont cidFont = ((PDType0Font) font).getDescendantFont();
+            fd = cidFont.getFontDescriptor();
+        }
         PDStream ff = fd.getFontFile3();
         if (ff == null) {
             ff = fd.getFontFile2();
@@ -254,7 +269,7 @@ public class FOPPDFMultiByteFont extends
             }
         }
         if (ff == null) {
-            throw new IOException(font.getBaseFont() + " no fontfile");
+            throw new IOException(font.getName() + " no fontfile");
         }
         InputStream is = ff.createInputStream();
         return new ByteArrayInputStream(IOUtils.toByteArray(is));
@@ -313,12 +328,12 @@ public class FOPPDFMultiByteFont extends
         return new ByteArrayInputStream(mergeFonts.getMergedFontSubset());
     }
 
-    protected PDFont getFont(COSDictionary fontData) throws IOException {
+    protected FontContainer getFont(COSDictionary fontData) throws IOException {
         if (!fontMap.containsKey(fontData)) {
             if (fontMap.size() > 10) {
                 fontMap.clear();
             }
-            fontMap.put(fontData, PDFontFactory.createFont(fontData));
+            fontMap.put(fontData, new FontContainer(fontData));
         }
         return fontMap.get(fontData);
     }
@@ -333,8 +348,8 @@ public class FOPPDFMultiByteFont extends
         }
     }
 
-    protected static void mergeMaxp(TrueTypeFont ttf, MaximumProfileTable outMaxp) {
-        MaximumProfileTable mp = ttf.getMaximumProfile();
+    protected static void mergeMaxp(TrueTypeFont ttf, MaximumProfileTable outMaxp) throws IOException {
+        org.apache.fontbox.ttf.MaximumProfileTable mp = ttf.getMaximumProfile();
         outMaxp.setVersion(mp.getVersion());
         outMaxp.setNumGlyphs(outMaxp.getNumGlyphs() + mp.getNumGlyphs());
         outMaxp.setMaxPoints(outMaxp.getMaxPoints() + mp.getMaxPoints());

Modified: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/FOPPDFSingleByteFont.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/FOPPDFSingleByteFont.java?rev=1739083&r1=1739082&r2=1739083&view=diff
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/FOPPDFSingleByteFont.java (original)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/FOPPDFSingleByteFont.java Thu Apr 14 12:52:43 2016
@@ -28,8 +28,10 @@ import java.util.Map;
 import java.util.Set;
 import java.util.TreeMap;
 
+import org.apache.fontbox.cff.CFFType1Font;
 import org.apache.fontbox.cmap.CMap;
-import org.apache.fontbox.ttf.CMAPEncodingEntry;
+
+import org.apache.fontbox.ttf.CmapSubtable;
 import org.apache.fontbox.ttf.TrueTypeFont;
 
 import org.apache.pdfbox.cos.COSArray;
@@ -37,13 +39,15 @@ import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSNumber;
-import org.apache.pdfbox.encoding.Encoding;
+
 import org.apache.pdfbox.pdmodel.common.PDStream;
 import org.apache.pdfbox.pdmodel.font.PDFont;
-import org.apache.pdfbox.pdmodel.font.PDFontDescriptorDictionary;
-import org.apache.pdfbox.pdmodel.font.PDFontFactory;
+import org.apache.pdfbox.pdmodel.font.PDFontDescriptor;
 import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
+import org.apache.pdfbox.pdmodel.font.PDType1CFont;
 import org.apache.pdfbox.pdmodel.font.PDType1Font;
+import org.apache.pdfbox.pdmodel.font.encoding.BuiltInEncoding;
+import org.apache.pdfbox.pdmodel.font.encoding.Encoding;
 
 import org.apache.fop.fonts.EmbeddingMode;
 import org.apache.fop.fonts.FontType;
@@ -53,7 +57,7 @@ import org.apache.fop.pdf.PDFDictionary;
 
 public class FOPPDFSingleByteFont extends SingleByteFont implements FOPPDFFont {
     private int fontCount;
-    private PDFont font;
+    private FontContainer font;
     protected PDFDictionary ref;
     protected Map<String, Integer> charMapGlobal = new LinkedHashMap<String, Integer>();
     private Map<Integer, Integer> newWidth = new HashMap<Integer, Integer>();
@@ -63,7 +67,7 @@ public class FOPPDFSingleByteFont extend
     private int encodingSkip;
     private MergeFonts mergeFonts;
     private String shortFontName;
-    private final Map<COSDictionary, PDFont> fontMap = new HashMap<COSDictionary, PDFont>();
+    private final Map<COSDictionary, FontContainer> fontMap = new HashMap<COSDictionary, FontContainer>();
 
     public FOPPDFSingleByteFont(COSDictionary fontData, String name) throws IOException {
         super(null, EmbeddingMode.FULL);
@@ -74,9 +78,9 @@ public class FOPPDFSingleByteFont extend
         font = getFont(fontData);
         setFirstChar(font.getFirstChar());
         setLastChar(font.getLastChar());
-        shortFontName = MergeFontsPDFWriter.getName(font.getBaseFont());
+        shortFontName = MergeFontsPDFWriter.getName(font.font.getName());
         loadFontFile(font);
-        float[] bBoxF = font.getFontBoundingBox().getCOSArray().toFloatArray();
+        float[] bBoxF = font.getBoundingBox();
         int[] bBox = new int[bBoxF.length];
         for (int i = 0; i < bBox.length; i++) {
             bBox[i] = (int)bBoxF[i];
@@ -93,11 +97,11 @@ public class FOPPDFSingleByteFont extend
         }
         //mark font as used
         notifyMapOperation();
-        FOPPDFMultiByteFont.setProperties(this, font);
+        FOPPDFMultiByteFont.setProperties(this, font.font);
         if (font.getWidths() != null) {
             //if width contains 0 we cant rely on codeToNameMap
             boolean usesZero = font.getWidths().contains(0);
-            Set<Integer> codeToName = getCodeToName(font.getFontEncoding()).keySet();
+            Set<Integer> codeToName = getCodeToName(font.getEncoding()).keySet();
             for (int i = getFirstChar();
                  i <= Math.min(getLastChar(), getFirstChar() + font.getWidths().size()); i++) {
                 if (usesZero || codeToName.contains(i)) {
@@ -113,10 +117,13 @@ public class FOPPDFSingleByteFont extend
         addEncoding(font);
     }
 
-    private Map<Integer, String> getCodeToName(Encoding encoding) throws IOException {
+    private Map<Integer, String> getCodeToName(Encoding encoding) {
         Map<Integer, String> codeToName = new HashMap<Integer, String>();
         if (encoding != null) {
-            COSBase cos = encoding.getCOSObject();
+            COSBase cos = null;
+            if (!(encoding instanceof BuiltInEncoding)) {
+                cos = encoding.getCOSObject();
+            }
             if (cos instanceof COSDictionary) {
                 COSDictionary enc = (COSDictionary) cos;
                 COSName baseEncodingName = (COSName) enc.getDictionaryObject(COSName.BASE_ENCODING);
@@ -142,18 +149,18 @@ public class FOPPDFSingleByteFont extend
         return codeToName;
     }
 
-    private Object getCmap(PDFont font) throws IOException {
-        if (font.getFontEncoding() != null) {
-            return font.getFontEncoding();
+    private Object getCmap(FontContainer font) throws IOException {
+        if (font.getEncoding() != null) {
+            return font.getEncoding();
         }
         if (font.getToUnicodeCMap() == null) {
-            throw new IOException("No cmap found in " + font.getBaseFont());
+            throw new IOException("No cmap found in " + font.font.getName());
         }
         return font.getToUnicodeCMap();
     }
 
     private PDStream readFontFile(PDFont font) throws IOException {
-        PDFontDescriptorDictionary fd = (PDFontDescriptorDictionary) font.getFontDescriptor();
+        PDFontDescriptor fd = font.getFontDescriptor();
         setFlags(fd.getFlags());
         PDStream ff = fd.getFontFile3();
         if (ff == null) {
@@ -165,18 +172,18 @@ public class FOPPDFSingleByteFont extend
             setFontType(FontType.TYPE1C);
         }
         if (ff == null) {
-            throw new IOException(font.getBaseFont() + " no font file");
+            throw new IOException(font.getName() + " no font file");
         }
         return ff;
     }
 
-    private void loadFontFile(PDFont font) throws IOException {
-        PDStream ff = readFontFile(font);
+    private void loadFontFile(FontContainer font) throws IOException {
+        PDStream ff = readFontFile(font.font);
         mergeFontFile(ff.createInputStream(), font);
-        if (font instanceof PDTrueTypeFont) {
-            TrueTypeFont ttfont = ((PDTrueTypeFont) font).getTTFFont();
-            CMAPEncodingEntry[] cmapList = ttfont.getCMAP().getCmaps();
-            for (CMAPEncodingEntry c : cmapList) {
+        if (font.font instanceof PDTrueTypeFont) {
+            TrueTypeFont ttfont = ((PDTrueTypeFont) font.font).getTrueTypeFont();
+            CmapSubtable[] cmapList = ttfont.getCmap().getCmaps();
+            for (CmapSubtable c : cmapList) {
                 MergeTTFonts.Cmap tempCmap = getNewCmap(c.getPlatformId(), c.getPlatformEncodingId());
                 for (int i = 0; i < 256 * 256; i++) {
                     int gid = c.getGlyphId(i);
@@ -227,8 +234,8 @@ public class FOPPDFSingleByteFont extend
     }
 
     public String addFont(COSDictionary fontData) throws IOException {
-        PDFont font = getFont(fontData);
-        if (font instanceof PDType1Font && differentGlyphData((PDType1Font) font)) {
+        FontContainer font = getFont(fontData);
+        if ((font.font instanceof PDType1Font || font.font instanceof PDType1CFont) && differentGlyphData(font.font)) {
             return null;
         }
         mergeWidths(font);
@@ -249,18 +256,25 @@ public class FOPPDFSingleByteFont extend
         return fontCount;
     }
 
-    private Map<String, byte[]> getCharStringsDict(PDType1Font font) throws IOException {
-        if (getFontType() == FontType.TYPE1) {
-            return font.getType1Font().getCharStringsDict();
+    private Map<String, byte[]> getCharStringsDict(PDFont font) throws IOException {
+        if (font instanceof PDType1Font) {
+            return ((PDType1Font)font).getType1Font().getCharStringsDict();
         }
-        return font.getType1CFont().getCFFFont().getCharStringsDict();
+        CFFType1Font cffFont = ((PDType1CFont) font).getCFFType1Font();
+        List<byte[]> bytes = cffFont.getCharStringBytes();
+        Map<String, byte[]> map = new HashMap<String, byte[]>();
+        for (int i = 0; i < bytes.size(); i++) {
+            map.put(cffFont.getCharset().getNameForGID(i), bytes.get(i));
+        }
+        return map;
     }
 
-    private boolean differentGlyphData(PDType1Font otherFont) throws IOException {
+    private boolean differentGlyphData(PDFont otherFont) throws IOException {
         if (charStringsDict == null) {
-            charStringsDict = getCharStringsDict((PDType1Font) font);
+            charStringsDict = getCharStringsDict(font.font);
         }
-        for (Map.Entry<String, byte[]> s : getCharStringsDict(otherFont).entrySet()) {
+        Map<String, byte[]> otherFontMap = getCharStringsDict(otherFont);
+        for (Map.Entry<String, byte[]> s : otherFontMap.entrySet()) {
             if (charStringsDict.containsKey(s.getKey())) {
                 int numberDiff = 0;
                 byte[] b1 = charStringsDict.get(s.getKey());
@@ -286,15 +300,15 @@ public class FOPPDFSingleByteFont extend
         return false;
     }
 
-    private void mergeWidths(PDFont font) throws IOException {
+    private void mergeWidths(FontContainer font) throws IOException {
         int w = 0;
         int skipGlyphIndex = getLastChar() + 1;
         Object cmap = getCmap(font);
-        Set<Integer> codeToName = getCodeToName(font.getFontEncoding()).keySet();
+        Set<Integer> codeToName = getCodeToName(font.getEncoding()).keySet();
         for (int i = font.getFirstChar(); i <= font.getLastChar(); i++) {
             boolean addedWidth = false;
             int glyphIndexPos = skipGlyphIndex;
-            if (font instanceof PDTrueTypeFont) {
+            if (font.font instanceof PDTrueTypeFont) {
                 glyphIndexPos = i;
             }
             int neww = 0;
@@ -302,7 +316,7 @@ public class FOPPDFSingleByteFont extend
                 neww = font.getWidths().get(i - font.getFirstChar());
                 if (!newWidth.containsKey(i) || newWidth.get(i) == 0) {
                     if (getFontType() == FontType.TYPE1
-                            || font instanceof PDTrueTypeFont
+                            || font.font instanceof PDTrueTypeFont
                             || codeToName.contains(i)) {
                         newWidth.put(i, neww);
                         glyphIndexPos = i;
@@ -327,38 +341,19 @@ public class FOPPDFSingleByteFont extend
     private String getChar(Object cmap, int i) throws IOException {
         if (cmap instanceof CMap) {
             CMap c = (CMap)cmap;
-            int size = 1;
-            if (c.hasTwoByteMappings()) {
-                size = 2;
-            }
-            return c.lookup(i, size);
+            return c.toUnicode(i);
         }
         Encoding enc = (Encoding)cmap;
         return enc.getName(i);
     }
 
     public String getEncodingName() {
-        if (font.getFontEncoding() != null) {
-            COSBase cosObject = font.getFontEncoding().getCOSObject();
-            if (cosObject != null) {
-                if (cosObject instanceof COSDictionary) {
-                    COSBase item = ((COSDictionary) cosObject).getItem(COSName.BASE_ENCODING);
-                    if (item != null) {
-                        return ((COSName)item).getName();
-                    }
-                } else if (cosObject instanceof COSName) {
-                    return ((COSName) cosObject).getName();
-                } else {
-                    throw new RuntimeException(cosObject.toString() + " not supported");
-                }
-            }
-        }
-        return null;
+        return font.getBaseEncodingName();
     }
 
-    private void addEncoding(PDFont fontForEnc) throws IOException {
+    private void addEncoding(FontContainer fontForEnc) {
         List<String> added = new ArrayList<String>(encodingMap.values());
-        Map<Integer, String> codeToName = getCodeToName(fontForEnc.getFontEncoding());
+        Map<Integer, String> codeToName = getCodeToName(fontForEnc.getEncoding());
         for (int i = fontForEnc.getFirstChar(); i <= fontForEnc.getLastChar(); i++) {
             if (codeToName.keySet().contains(i)) {
                 String s = codeToName.get(i);
@@ -395,7 +390,7 @@ public class FOPPDFSingleByteFont extend
 
         public char[] getUnicodeCharMap() {
             if (cmap) {
-                if (MergeFontsPDFWriter.getToUnicode(font) == null) {
+                if (font.getToUnicode() == null) {
                     return new char[0];
                 }
                 List<String> cmapStrings = new ArrayList<String>();
@@ -453,7 +448,7 @@ public class FOPPDFSingleByteFont extend
         return false;
     }
 
-    private void mergeFontFile(InputStream ff, PDFont pdFont) throws IOException {
+    private void mergeFontFile(InputStream ff, FontContainer pdFont) throws IOException {
         if (mergeFonts == null) {
             if (getFontType() == FontType.TRUETYPE) {
                 mergeFonts = new MergeTTFonts(newCmap);
@@ -473,12 +468,12 @@ public class FOPPDFSingleByteFont extend
         return new ByteArrayInputStream(mergeFonts.getMergedFontSubset());
     }
 
-    protected PDFont getFont(COSDictionary fontData) throws IOException {
+    protected FontContainer getFont(COSDictionary fontData) throws IOException {
         if (!fontMap.containsKey(fontData)) {
             if (fontMap.size() > 10) {
                 fontMap.clear();
             }
-            fontMap.put(fontData, PDFontFactory.createFont(fontData));
+            fontMap.put(fontData, new FontContainer(fontData));
         }
         return fontMap.get(fontData);
     }

Modified: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/ImageConverterPDF2G2D.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/ImageConverterPDF2G2D.java?rev=1739083&r1=1739082&r2=1739083&view=diff
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/ImageConverterPDF2G2D.java (original)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/ImageConverterPDF2G2D.java Thu Apr 14 12:52:43 2016
@@ -29,7 +29,7 @@ import java.util.Map;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.PDPage;
 import org.apache.pdfbox.pdmodel.common.PDRectangle;
-import org.apache.pdfbox.rendering.PageDrawer;
+import org.apache.pdfbox.rendering.PDFRenderer;
 
 import org.apache.xmlgraphics.image.loader.Image;
 import org.apache.xmlgraphics.image.loader.ImageException;
@@ -57,10 +57,8 @@ public class ImageConverterPDF2G2D exten
                 src.getInfo().getOriginalURI());
 
         PDDocument pddoc = imgPDF.getPDDocument();
-        PDPage page = (PDPage)pddoc.getDocumentCatalog().getAllPages().get(selectedPage);
 
-        Graphics2DImagePainter painter = new Graphics2DImagePainterPDF(
-                page);
+        Graphics2DImagePainter painter = new Graphics2DImagePainterPDF(pddoc, selectedPage);
 
         ImageGraphics2D g2dImage = new ImageGraphics2D(src.getInfo(), painter);
         return g2dImage;
@@ -85,14 +83,18 @@ public class ImageConverterPDF2G2D exten
     private static class Graphics2DImagePainterPDF implements GeneralGraphics2DImagePainter {
 
         private final PDPage page;
+        private final PDDocument pdDocument;
+        private int selectedPage;
 
-        public Graphics2DImagePainterPDF(PDPage page) {
-            this.page = page;
+        public Graphics2DImagePainterPDF(PDDocument pddoc, int selectedPage) {
+            pdDocument = pddoc;
+            this.selectedPage = selectedPage;
+            page = pdDocument.getPage(selectedPage);
         }
 
         /** {@inheritDoc} */
         public Dimension getImageSize() {
-            PDRectangle mediaBox = page.findMediaBox();
+            PDRectangle mediaBox = page.getMediaBox();
             int wmpt = (int)Math.ceil(mediaBox.getWidth() * 1000);
             int hmpt = (int)Math.ceil(mediaBox.getHeight() * 1000);
             return new Dimension(wmpt, hmpt);
@@ -101,41 +103,17 @@ public class ImageConverterPDF2G2D exten
         /** {@inheritDoc} */
         public void paint(Graphics2D g2d, Rectangle2D area) {
             try {
-                PDRectangle mediaBox = page.findCropBox();
-                Dimension pageDimension = mediaBox.createDimension();
-
+                PDRectangle mediaBox = page.getCropBox();
                 AffineTransform at = new AffineTransform();
-
-                Integer rotation = page.getRotation();
-                if (rotation != null) {
-                    switch (rotation) {
-                    case 270:
-                        at.scale(area.getWidth() / area.getHeight(), area.getHeight() / area.getWidth());
-                        at.translate(0, area.getWidth());
-                        at.rotate(-Math.PI / 2.0);
-                        break;
-                    case 180:
-                        at.translate(area.getWidth(), area.getHeight());
-                        at.rotate(-Math.PI);
-                        break;
-                    case 90:
-                        at.scale(area.getWidth() / area.getHeight(), area.getHeight() / area.getWidth());
-                        at.translate(area.getHeight(), 0);
-                        at.rotate(-Math.PI * 1.5);
-                            break;
-                    default:
-                        //no additional transformations necessary
-                            break;
-                    }
+                int rotation = page.getRotation();
+                if (rotation == 90 || rotation == 270) {
+                    at.scale(area.getWidth() / area.getHeight(), area.getHeight() / area.getWidth());
                 }
-
                 at.translate(area.getX(), area.getY());
-                at.scale(area.getWidth() / pageDimension.width,
-                        area.getHeight() / pageDimension.height);
+                at.scale(area.getWidth() / mediaBox.getWidth(),
+                        area.getHeight() / mediaBox.getHeight());
                 g2d.transform(at);
-
-                PageDrawer drawer = new PageDrawer(null, page);
-                drawer.drawPage(g2d, mediaBox);
+                new PDFRenderer(pdDocument).renderPageToGraphics(selectedPage, g2d);
             } catch (IOException ioe) {
                 //TODO Better exception handling
                 throw new RuntimeException("I/O error while painting PDF page", ioe);

Modified: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeCFFFonts.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeCFFFonts.java?rev=1739083&r1=1739082&r2=1739083&view=diff
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeCFFFonts.java (original)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeCFFFonts.java Thu Apr 14 12:52:43 2016
@@ -18,6 +18,9 @@ package org.apache.fop.render.pdf.pdfbox
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.Method;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashSet;
@@ -27,14 +30,15 @@ import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
 
+import org.apache.fontbox.cff.CFFCIDFont;
+import org.apache.fontbox.cff.CFFCharset;
+import org.apache.fontbox.cff.CFFEncoding;
 import org.apache.fontbox.cff.CFFFont;
-import org.apache.fontbox.cff.CFFFontROS;
+import org.apache.fontbox.cff.CFFISOAdobeCharset;
 import org.apache.fontbox.cff.CFFParser;
-import org.apache.fontbox.cff.charset.CFFCharset;
-import org.apache.fontbox.cff.charset.CFFISOAdobeCharset;
-import org.apache.fontbox.cff.encoding.CFFEncoding;
-import org.apache.fontbox.cff.encoding.CFFStandardEncoding;
-import org.apache.pdfbox.pdmodel.font.PDFont;
+import org.apache.fontbox.cff.CFFStandardEncoding;
+import org.apache.fontbox.cff.CFFStandardString;
+import org.apache.fontbox.cff.CFFType1Font;
 
 import org.apache.fop.fonts.cff.CFFDataReader;
 import org.apache.fop.fonts.truetype.FontFileReader;
@@ -50,14 +54,15 @@ public class MergeCFFFonts extends OTFSu
     private List<String> added = new ArrayList<String>();
     private Map<Integer, Integer> range = new LinkedHashMap<Integer, Integer>();
     private int noOfFonts;
+    private CFFEncoding encoding = null;
 
     public MergeCFFFonts() throws IOException {
         gidToSID = new LinkedHashMap<Integer, Integer>();
         subsetCharStringsIndex = new ArrayList<byte[]>();
     }
 
-    public void readFont(InputStream is, String name, PDFont pdFont, Map<Integer, Integer> subsetGlyphs, boolean cid)
-        throws IOException {
+    public void readFont(InputStream is, String name, FontContainer fontContainer,
+                         Map<Integer, Integer> subsetGlyphs, boolean cid) throws IOException {
         this.embeddedName = name;
         FontFileReader fontFile = new FontFileReader(is);
         CFFParser p = new CFFParser();
@@ -73,7 +78,7 @@ public class MergeCFFFonts extends OTFSu
             fileFont = ff;
         }
         LinkedHashMap<Integer, Integer> sg = new LinkedHashMap<Integer, Integer>();
-        for (int i = 0; i < ff.getCharStringsDict().size() + 1; i++) {
+        for (int i = 0; i < ff.getNumCharStrings() + 1; i++) {
             sg.put(i, i);
         }
         subsetGlyphsList.add(sg);
@@ -91,12 +96,13 @@ public class MergeCFFFonts extends OTFSu
             }
         }
 
-        CFFEncoding encoding = ff.getEncoding();
-        if (!(encoding instanceof CFFStandardEncoding)) {
-            for (CFFEncoding.Entry e : encoding.getEntries()) {
-                int c = e.getCode();
-                if (!chars.contains(c) && c != 0) {
-                    chars.add(c);
+        if (ff instanceof CFFType1Font) {
+            encoding = ((CFFType1Font)ff).getEncoding();
+            if (!(encoding instanceof CFFStandardEncoding)) {
+                for (int c : encoding.getCodeToNameMap().keySet()) {
+                    if (!chars.contains(c) && c != 0) {
+                        chars.add(c);
+                    }
                 }
             }
         }
@@ -144,16 +150,66 @@ public class MergeCFFFonts extends OTFSu
 
     public static List<Integer> getSids(CFFCharset cSet) {
         List<Integer> sids = new ArrayList<Integer>();
-        for (CFFCharset.Entry x : cSet.getEntries()) {
-            if (x.getSID() != 0) {
-                sids.add(x.getSID());
+        try {
+            for (int gid = 0; gid < 1024; gid++) {
+                int sid = cSet.getCIDForGID(gid);
+                if (sid != 0) {
+                    sids.add(sid);
+                }
+            }
+        } catch (IllegalStateException e) {
+            try {
+                final Method getSIDForGID = CFFCharset.class.getDeclaredMethod("getSIDForGID", int.class);
+                AccessController.doPrivileged(new PrivilegedAction() {
+                    public Object run() {
+                        getSIDForGID.setAccessible(true);
+                        return null;
+                    }
+                });
+                for (int gid = 0; gid < 1024; gid++) {
+                    int sid = (Integer)getSIDForGID.invoke(cSet, gid);
+                    if (sid != 0) {
+                        sids.add(sid);
+                    }
+                }
+            } catch (ReflectiveOperationException e1) {
+                throw new RuntimeException(e1);
             }
         }
         return sids;
     }
 
     public static Map<String, byte[]> getStrings(CFFFont ff) throws IOException {
-        return ff.getCharStringsDict();
+        CFFCharset cs = ff.getCharset();
+        List<byte[]> csbytes = ff.getCharStringBytes();
+        Map<String, byte[]> strings = new LinkedHashMap<String, byte[]>();
+        int i = 0;
+        try {
+            for (int gid = 0; gid < 256; gid++) {
+                String name = cs.getNameForGID(gid);
+                if (name != null && i < csbytes.size()) {
+                    strings.put(name, csbytes.get(i));
+                    i++;
+                }
+            }
+        } catch (IllegalStateException e) {
+            strings.put(".notdef", csbytes.get(0));
+            for (int sid : getSids(ff.getCharset())) {
+                if (i < csbytes.size()) {
+                    i++;
+                    strings.put(readString(sid), csbytes.get(i));
+                }
+            }
+        }
+        return strings;
+    }
+
+    private static String readString(int index) throws IOException {
+        if (index >= 0 && index <= 390) {
+            return CFFStandardString.getName(index);
+        }
+        // technically this maps to .notdef, but we need a unique glyph name
+        return "SID" + index;
     }
 
     public byte[] getMergedFontSubset() throws IOException {
@@ -296,15 +352,15 @@ public class MergeCFFFonts extends OTFSu
                     stringIndexData.add(stringIndexData.remove(0));
                 }
             } else {
-                String notice = (String)fileFont.getProperty("Notice");
-                if (notice != null && !(fileFont instanceof CFFFontROS)) {
+                String notice = (String)fileFont.getTopDict().get("Notice");
+                if (notice != null && !(fileFont instanceof CFFCIDFont)) {
                     stringIndexData.add(notice.getBytes("ISO-8859-1"));
                 }
             }
             stringIndexData.add(embeddedName.getBytes("UTF-8"));
             writeIndex(stringIndexData);
         } else {
-            String notice = (String)fileFont.getProperty("Notice");
+            String notice = (String)fileFont.getTopDict().get("Notice");
             if (notice != null) {
                 writeIndex(Arrays.<byte[]>asList(notice.getBytes("ISO-8859-1"), embeddedName.getBytes("UTF-8")));
             } else {

Modified: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeFonts.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeFonts.java?rev=1739083&r1=1739082&r2=1739083&view=diff
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeFonts.java (original)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeFonts.java Thu Apr 14 12:52:43 2016
@@ -20,10 +20,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.Map;
 
-import org.apache.pdfbox.pdmodel.font.PDFont;
-
 public interface MergeFonts {
-    void readFont(InputStream is, String name, PDFont pdFont, Map<Integer, Integer> subsetGlyphs, boolean cid)
-            throws IOException;
+    void readFont(InputStream is, String name, FontContainer fontContainer,
+                         Map<Integer, Integer> subsetGlyphs, boolean cid) throws IOException;
     byte[] getMergedFontSubset() throws IOException;
 }

Modified: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeFontsPDFWriter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeFontsPDFWriter.java?rev=1739083&r1=1739082&r2=1739083&view=diff
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeFontsPDFWriter.java (original)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeFontsPDFWriter.java Thu Apr 14 12:52:43 2016
@@ -16,7 +16,9 @@
  */
 package org.apache.fop.render.pdf.pdfbox;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -28,42 +30,44 @@ import java.util.regex.Pattern;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+
+import org.apache.fontbox.cff.CFFCIDFont;
+import org.apache.fontbox.cff.CFFCharset;
+import org.apache.fontbox.cff.CFFEncoding;
 import org.apache.fontbox.cff.CFFFont;
-import org.apache.fontbox.cff.CFFFontROS;
-import org.apache.fontbox.cff.charset.CFFCharset;
-import org.apache.fontbox.cff.encoding.CFFEncoding;
-import org.apache.fontbox.ttf.CMAPEncodingEntry;
+import org.apache.fontbox.ttf.CmapSubtable;
 import org.apache.fontbox.ttf.TrueTypeFont;
-
+import org.apache.pdfbox.contentstream.operator.Operator;
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSString;
+
 import org.apache.pdfbox.pdmodel.common.PDStream;
-import org.apache.pdfbox.pdmodel.font.PDCIDFontType0Font;
-import org.apache.pdfbox.pdmodel.font.PDCIDFontType2Font;
+import org.apache.pdfbox.pdmodel.font.PDCIDFontType0;
+import org.apache.pdfbox.pdmodel.font.PDCIDFontType2;
 import org.apache.pdfbox.pdmodel.font.PDFont;
-import org.apache.pdfbox.pdmodel.font.PDFontDescriptorDictionary;
-import org.apache.pdfbox.pdmodel.font.PDFontFactory;
 import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
 import org.apache.pdfbox.pdmodel.font.PDType0Font;
+import org.apache.pdfbox.pdmodel.font.PDType1CFont;
 import org.apache.pdfbox.pdmodel.font.PDType1Font;
-import org.apache.pdfbox.util.operator.Operator;
 
 import org.apache.fop.fonts.FontInfo;
 import org.apache.fop.fonts.SingleByteFont;
 import org.apache.fop.fonts.Typeface;
 import org.apache.fop.fonts.truetype.OTFSubSetFile;
+
 import org.apache.fop.pdf.PDFText;
 
+
 public class MergeFontsPDFWriter extends PDFWriter {
     protected static final Log log = LogFactory.getLog(MergeFontsPDFWriter.class);
     private COSDictionary fonts;
     private FontInfo fontInfo;
     private Typeface font;
-    private PDFont oldFont = null;
+    private FontContainer oldFont = null;
     protected Map<COSName, String> fontsToRemove = new HashMap<COSName, String>();
-    private final Map<COSDictionary, PDFont> fontMap = new HashMap<COSDictionary, PDFont>();
+    private final Map<COSDictionary, FontContainer> fontMap = new HashMap<COSDictionary, FontContainer>();
     private static final Pattern SUBSET_PATTERN = Pattern.compile("[A-Z][A-Z][A-Z][A-Z][A-Z][A-Z]\\+.+");
     private Collection<String> parentFonts;
 
@@ -83,7 +87,8 @@ public class MergeFontsPDFWriter extends
         for (COSName cn : fontsToRemove.keySet()) {
             fonts.removeItem(cn);
         }
-        parentFonts = fontsToRemove.values();
+        parentFonts.clear();
+        parentFonts.addAll(fontsToRemove.values());
         return txt;
     }
 
@@ -98,7 +103,7 @@ public class MergeFontsPDFWriter extends
                 }
                 if (fontData == null || internalName == null) {
                     s.append("/" + key.getName(cn));
-                    if (op.getOperation().equals("Tf")) {
+                    if (op.getName().equals("Tf")) {
                         font = null;
                         oldFont = null;
                     }
@@ -154,66 +159,63 @@ public class MergeFontsPDFWriter extends
     }
 
     private String getUniqueFontName(COSDictionary fontData) throws IOException {
-        PDFont font = getFont(fontData);
-        String extra = "";
-        String name = getName(font.getBaseFont()) + "_" + ((COSName)fontData.getItem(COSName.SUBTYPE)).getName();
-        if (font instanceof PDType0Font
-                && ((PDType0Font) font).getDescendantFont() instanceof PDCIDFontType0Font
-                && ((PDCIDFontType0Font) ((PDType0Font) font).getDescendantFont()).getType1CFont() != null) {
-            CFFFont cffFont =
-                    ((PDCIDFontType0Font) ((PDType0Font) font).getDescendantFont()).getType1CFont().getCFFFont();
-            if (cffFont instanceof CFFFontROS
-                    && ((CFFFontROS)cffFont).getFdSelect().getClass().getName()
-                    .equals("org.apache.fontbox.cff.CFFParser$Format0FDSelect")) {
-                extra += "format0";
-            }
-            return name + extra;
-        } else if (font instanceof PDType0Font
-                && getToUnicode(font) != null
-                && ((PDType0Font) font).getDescendantFont() instanceof PDCIDFontType2Font) {
-            if (!isSubsetFont(font.getBaseFont())) {
-                extra = "f3";
-            }
-            return name + extra;
-        } else if (font instanceof PDTrueTypeFont && isSubsetFont(font.getBaseFont())) {
-            TrueTypeFont tt = ((PDTrueTypeFont) font).getTTFFont();
-            for (CMAPEncodingEntry c : tt.getCMAP().getCmaps()) {
-                if (c.getGlyphId(1) > 0) {
-                    extra = "cid";
+        FontContainer fontContainer = getFont(fontData);
+        PDFont font = fontContainer.font;
+        if (font.getName() != null) {
+            String extra = "";
+            String name = getName(font.getName()) + "_" + ((COSName)fontData.getItem(COSName.SUBTYPE)).getName();
+            if (font instanceof PDType0Font
+                    && ((PDType0Font) font).getDescendantFont() instanceof PDCIDFontType0) {
+                CFFFont cffFont = ((PDCIDFontType0) ((PDType0Font) font).getDescendantFont()).getCFFFont();
+                if (cffFont instanceof CFFCIDFont
+                        && ((CFFCIDFont)cffFont).getFdSelect().getClass().getName()
+                        .equals("org.apache.fontbox.cff.CFFParser$Format0FDSelect")) {
+                    extra += "format0";
                 }
+                return name + extra;
+            } else if (font instanceof PDType0Font
+                    && fontContainer.getToUnicode() != null
+                    && ((PDType0Font) font).getDescendantFont() instanceof PDCIDFontType2) {
+                if (!isSubsetFont(font.getName())) {
+                    extra = "f3";
+                }
+                return name + extra;
+            } else if (font instanceof PDTrueTypeFont && isSubsetFont(font.getName())) {
+                TrueTypeFont tt = ((PDTrueTypeFont) font).getTrueTypeFont();
+                for (CmapSubtable c : tt.getCmap().getCmaps()) {
+                    if (c.getGlyphId(1) > 0) {
+                        extra = "cid";
+                    }
+                }
+                return name + extra;
+            } else if (font instanceof PDType1CFont) {
+                return getNamePDType1Font(name, (PDType1CFont) font);
+            } else if (font instanceof PDType1Font) {
+                return name;
             }
-            return name + extra;
-        } else if (font instanceof PDType1Font) {
-            return getNamePDType1Font(name, (PDType1Font) font);
         }
         return null;
     }
 
-    private String getNamePDType1Font(String name, PDType1Font font) throws IOException {
+    private String getNamePDType1Font(String name, PDType1CFont font) throws IOException {
         String extra = "";
-        if (font.getType1CFont() == null
-                || font.getType1CFont().getCFFFont() == null) {
-            if (font.getFontDescriptor() instanceof PDFontDescriptorDictionary) {
-                return name;
-            }
-            return null;
-        }
-        CFFEncoding encoding = font.getType1CFont().getCFFFont().getEncoding();
+        CFFEncoding encoding = font.getCFFType1Font().getEncoding();
         String eClass = encoding.getClass().getName();
         if (eClass.equals("org.apache.fontbox.cff.CFFParser$Format1Encoding")) {
             extra = "f1enc";
         } else if (eClass.equals("org.apache.fontbox.cff.CFFParser$Format0Encoding")) {
             extra = "f0enc";
         }
-        CFFCharset cs = font.getType1CFont().getCFFFont().getCharset();
-        if (cs.getEntries().get(0).getSID() < OTFSubSetFile.NUM_STANDARD_STRINGS) {
+        CFFCharset cs = font.getCFFType1Font().getCharset();
+        List<Integer> sids = MergeCFFFonts.getSids(cs);
+        if (!sids.isEmpty() && sids.get(0) < OTFSubSetFile.NUM_STANDARD_STRINGS) {
             extra += "stdcs";
         }
         if (cs.getClass().getName().equals("org.apache.fontbox.cff.CFFParser$Format1Charset")) {
             extra += "f1cs";
         }
-        if (font.getFontEncoding() != null) {
-            String enc = font.getFontEncoding().getClass().getSimpleName();
+        if (font.getEncoding() != null) {
+            String enc = font.getEncoding().getClass().getSimpleName();
             if (!"DictionaryEncoding".equals(enc)) {
                 extra += enc;
             }
@@ -265,44 +267,36 @@ public class MergeFontsPDFWriter extends
     }
 
     private Integer getMapping(byte i) throws IOException {
-        if (oldFont.getFontEncoding() != null && font instanceof FOPPDFSingleByteFont) {
-            String name = oldFont.getFontEncoding().getName(i);
-            if (name != null && ((FOPPDFSingleByteFont)font).charMapGlobal.containsKey(name)) {
+        if (oldFont.getEncoding() != null && font instanceof FOPPDFSingleByteFont) {
+            String name = oldFont.getEncoding().getName(i);
+            if (!name.equals(".notdef") && ((FOPPDFSingleByteFont)font).charMapGlobal.containsKey(name)) {
                 return ((FOPPDFSingleByteFont)font).charMapGlobal.get(name);
             }
         }
         return null;
     }
 
-    private List<String> readCOSString(COSString s, PDFont oldFont) throws IOException {
+    private List<String> readCOSString(COSString s, FontContainer oldFont) throws IOException {
         List<String> word = new ArrayList<String>();
         byte[] string = s.getBytes();
-        int codeLength;
-//            String t1Str = new String(string, "UTF-8");
-        for (int i = 0; i < string.length; i += codeLength) {
-            codeLength = 1;
-            String c = oldFont.encode(string, i, codeLength);
-//                if (oldFont instanceof PDType1Font && i < t1Str.length()) {
-//                    c = ((PDType1Font)oldFont).encodetype1(string, i, codeLength);
-//                }
-            if (c == null && i + 1 < string.length) {
-                codeLength++;
-                c = oldFont.encode(string, i, codeLength);
-            }
-            if (c == null) {
+        InputStream in = new ByteArrayInputStream(string);
+        while (in.available() > 0) {
+            int code = oldFont.font.readCode(in);
+            String unicode = oldFont.font.toUnicode(code);
+            if (unicode == null) {
                 return null;
             }
-            word.add(c);
+            word.add(unicode);
         }
         return word;
     }
 
-    protected PDFont getFont(COSDictionary fontData) throws IOException {
+    protected FontContainer getFont(COSDictionary fontData) throws IOException {
         if (!fontMap.containsKey(fontData)) {
             if (fontMap.size() > 10) {
                 fontMap.clear();
             }
-            fontMap.put(fontData, PDFontFactory.createFont(fontData));
+            fontMap.put(fontData, new FontContainer(fontData));
         }
         return fontMap.get(fontData);
     }
@@ -317,9 +311,4 @@ public class MergeFontsPDFWriter extends
         }
         return name.replace(" ", "");
     }
-
-    protected static COSBase getToUnicode(PDFont font) {
-        COSDictionary dict = (COSDictionary) font.getCOSObject();
-        return dict.getDictionaryObject(COSName.TO_UNICODE);
-    }
 }

Modified: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeTTFonts.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeTTFonts.java?rev=1739083&r1=1739082&r2=1739083&view=diff
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeTTFonts.java (original)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeTTFonts.java Thu Apr 14 12:52:43 2016
@@ -22,9 +22,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
 
-import org.apache.fontbox.ttf.MaximumProfileTable;
-import org.apache.pdfbox.pdmodel.font.PDFont;
-
 import org.apache.fop.fonts.truetype.FontFileReader;
 import org.apache.fop.fonts.truetype.GlyfTable;
 import org.apache.fop.fonts.truetype.OFDirTabEntry;
@@ -209,14 +206,14 @@ public class MergeTTFonts extends TTFSub
      *
      * @param is font file
      * @param name name
-     * @param pdFont pdFont
+     * @param fontContainer fontContainer
      * @param subsetGlyphs Map of glyphs (glyphs has old index as (Integer) key and
      * new index as (Integer) value)
      * @param cid is cid
      * @throws IOException in case of an I/O problem
      */
-    public void readFont(InputStream is, String name, PDFont pdFont, Map<Integer, Integer> subsetGlyphs, boolean cid)
-        throws IOException {
+    public void readFont(InputStream is, String name, FontContainer fontContainer,
+                         Map<Integer, Integer> subsetGlyphs, boolean cid) throws IOException {
         this.cid = cid;
         if (subsetGlyphs.isEmpty()) {
             return;

Modified: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeType1Fonts.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeType1Fonts.java?rev=1739083&r1=1739082&r2=1739083&view=diff
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeType1Fonts.java (original)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/MergeType1Fonts.java Thu Apr 14 12:52:43 2016
@@ -28,7 +28,7 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.fontbox.encoding.Encoding;
-import org.apache.pdfbox.pdmodel.font.PDFont;
+import org.apache.fontbox.type1.Type1Font;
 import org.apache.pdfbox.pdmodel.font.PDType1Font;
 
 import org.apache.fop.fonts.type1.PFBData;
@@ -55,16 +55,15 @@ public class MergeType1Fonts extends Typ
         subsetEncodingEntries.add("dup 0 /.notdef put");
     }
 
-    public void readFont(InputStream fontFile, String name, PDFont pdFont,
+    public void readFont(InputStream fontFile, String name, FontContainer font,
                          Map<Integer, Integer> subsetGlyphs, boolean cid) throws IOException {
         PFBParser pfbParser = new PFBParser();
         pfbData = pfbParser.parsePFB(fontFile);
 
         PostscriptParser psParser = new PostscriptParser();
         List<Integer> glyphs = new ArrayList<Integer>();
-        assert pdFont instanceof PDType1Font;
-        PDType1Font font = (PDType1Font) pdFont;
-        Encoding enc = font.getType1Font().getEncoding();
+        Type1Font t1f = ((PDType1Font)font.font).getType1Font();
+        Encoding enc = t1f.getEncoding();
         for (int i = font.getFirstChar(); i <= font.getLastChar(); i++) {
             if (!enc.getName(i).equals(".notdef")) {
                 nameMap.put(i, enc.getName(i));
@@ -97,14 +96,14 @@ public class MergeType1Fonts extends Typ
             subrsEndStream.write(decoded, subroutines.getEndPoint(),
                     charStrings.getStartPoint() - subroutines.getEndPoint());
         }
-        List<byte[]> subArray = font.getType1Font().getSubrsArray();
+        List<byte[]> subArray = t1f.getSubrsArray();
         for (int i = 0; i < subArray.size(); i++) {
             if (subByteMap.containsKey(i) && !Arrays.equals(subByteMap.get(i), subArray.get(i))) {
-                throw new IOException("Can't merge font subroutines " + font.getBaseFont());
+                throw new IOException("Can't merge font subroutines " + font.font.getName());
             }
             subByteMap.put(i, subArray.get(i));
         }
-        Map<String, byte[]> cs = font.getType1Font().getCharStringsDict();
+        Map<String, byte[]> cs = t1f.getCharStringsDict();
         int lenIV = 4;
         PostscriptParser.PSElement element = getElement("/lenIV", mainSection);
         if (element != null && element instanceof PostscriptParser.PSVariable) {

Modified: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java?rev=1739083&r1=1739082&r2=1739083&view=diff
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java (original)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java Thu Apr 14 12:52:43 2016
@@ -33,7 +33,6 @@ import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -58,10 +57,8 @@ import org.apache.pdfbox.cos.COSString;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
 import org.apache.pdfbox.pdmodel.PDPage;
-import org.apache.pdfbox.pdmodel.PDPageNode;
 import org.apache.pdfbox.pdmodel.PDResources;
 import org.apache.pdfbox.pdmodel.common.COSObjectable;
-import org.apache.pdfbox.pdmodel.common.COSStreamArray;
 import org.apache.pdfbox.pdmodel.common.PDRectangle;
 import org.apache.pdfbox.pdmodel.common.PDStream;
 
@@ -166,14 +163,14 @@ public class PDFBoxAdapter {
                 newArray.add(cloneForNewDocument(array.get(i), array.get(i), exclude));
             }
             return newArray;
-        } else if (base instanceof COSStreamArray) {
-            COSStreamArray array = (COSStreamArray)base;
-            PDFArray newArray = new PDFArray();
-            cacheClonedObject(keyBase, newArray);
-            for (int i = 0, c = array.getStreamCount(); i < c; i++) {
-                newArray.add(cloneForNewDocument(array.get(i)));
-            }
-            return newArray;
+//        } else if (base instanceof COSStreamArray) {
+//            COSStreamArray array = (COSStreamArray)base;
+//            PDFArray newArray = new PDFArray();
+//            cacheClonedObject(keyBase, newArray);
+//            for (int i = 0, c = array.getStreamCount(); i < c; i++) {
+//                newArray.add(cloneForNewDocument(array.get(i)));
+//            }
+//            return newArray;
         } else if (base instanceof COSStream) {
             return readCOSStream((COSStream) base, keyBase);
         } else if (base instanceof COSDictionary) {
@@ -220,8 +217,8 @@ public class PDFBoxAdapter {
     private Object readCOSObject(COSObject object, Collection exclude) throws IOException {
         if (log.isTraceEnabled()) {
             log.trace("Cloning indirect object: "
-                    + object.getObjectNumber().longValue()
-                    + " " + object.getGenerationNumber().longValue());
+                    + object.getObjectNumber()
+                    + " " + object.getGenerationNumber());
         }
         Object obj = cloneForNewDocument(object.getObject(), object, exclude);
         if (obj instanceof PDFObject) {
@@ -235,8 +232,8 @@ public class PDFBoxAdapter {
                         + pdfobj.getObjectNumber()
                         + " " + pdfobj.getGeneration()
                         + " for COSObject: "
-                        + object.getObjectNumber().longValue()
-                        + " " + object.getGenerationNumber().longValue());
+                        + object.getObjectNumber()
+                        + " " + object.getGenerationNumber());
             }
         }
         return obj;
@@ -262,7 +259,8 @@ public class PDFBoxAdapter {
     private Object readCOSStream(COSStream originalStream, Object keyBase) throws IOException {
         InputStream in;
         Set filter;
-        if (pdfDoc.isEncryptionActive()) {
+        if (pdfDoc.isEncryptionActive()
+                || (originalStream.containsKey(COSName.DECODE_PARMS) && !originalStream.containsKey(COSName.FILTER))) {
             in = originalStream.getUnfilteredStream();
             filter = FILTER_FILTER;
         } else {
@@ -301,7 +299,7 @@ public class PDFBoxAdapter {
     private Object getBaseKey(Object base) {
         if (base instanceof COSObject) {
             COSObject obj = (COSObject)base;
-            return obj.getObjectNumber().intValue() + " " + obj.getGenerationNumber().intValue();
+            return obj.getObjectNumber() + " " + obj.getGenerationNumber();
         } else if (base instanceof COSDictionary) {
             return base;
         } else {
@@ -345,15 +343,12 @@ public class PDFBoxAdapter {
         if (pageNumbers.containsKey(targetPage.getPageIndex())) {
             pageNumbers.get(targetPage.getPageIndex()).set(0, targetPage.makeReference());
         }
-        PDResources sourcePageResources = page.findResources();
-        PDFDictionary pageResources;
-        PDStream pdStream = page.getContents();
-        if (pdStream == null) {
-            return "";
-        }
-        COSDictionary fonts = (COSDictionary)sourcePageResources.getCOSDictionary().getDictionaryObject(COSName.FONT);
+        PDResources sourcePageResources = page.getResources();
+        PDStream pdStream = getContents(page);
+
+        COSDictionary fonts = (COSDictionary)sourcePageResources.getCOSObject().getDictionaryObject(COSName.FONT);
         COSDictionary fontsBackup = null;
-        UniqueName uniqueName = new UniqueName(key, sourceDoc, sourcePageResources);
+        UniqueName uniqueName = new UniqueName(key, sourcePageResources);
         String newStream = null;
         if (fonts != null && pdfDoc.isMergeFontsEnabled()) {
             fontsBackup = new COSDictionary(fonts);
@@ -375,8 +370,8 @@ public class PDFBoxAdapter {
 
         }
         pdStream = new PDStream(sourceDoc, new ByteArrayInputStream(newStream.getBytes("ISO-8859-1")));
-        mergeXObj(sourcePageResources.getCOSDictionary(), fontinfo, uniqueName);
-        pageResources = (PDFDictionary)cloneForNewDocument(sourcePageResources.getCOSDictionary());
+        mergeXObj(sourcePageResources.getCOSObject(), fontinfo, uniqueName);
+        PDFDictionary pageResources = (PDFDictionary)cloneForNewDocument(sourcePageResources.getCOSObject());
 
         PDFDictionary fontDict = (PDFDictionary)pageResources.get("Font");
         if (fontDict != null && pdfDoc.isMergeFontsEnabled()) {
@@ -392,9 +387,9 @@ public class PDFBoxAdapter {
                 }
             }
         }
-        updateXObj(sourcePageResources.getCOSDictionary(), pageResources);
+        updateXObj(sourcePageResources.getCOSObject(), pageResources);
         if (fontsBackup != null) {
-            sourcePageResources.getCOSDictionary().setItem(COSName.FONT, fontsBackup);
+            sourcePageResources.getCOSObject().setItem(COSName.FONT, fontsBackup);
         }
 
         COSStream originalPageContents = (COSStream)pdStream.getCOSObject();
@@ -403,17 +398,17 @@ public class PDFBoxAdapter {
 
         PDFStream pageStream;
         Set filter;
-        if (originalPageContents instanceof COSStreamArray) {
-            COSStreamArray array = (COSStreamArray)originalPageContents;
-            pageStream = new PDFStream();
-            InputStream in = array.getUnfilteredStream();
-            OutputStream out = pageStream.getBufferOutputStream();
-            IOUtils.copyLarge(in, out);
-            filter = FILTER_FILTER;
-        } else {
+//        if (originalPageContents instanceof COSStreamArray) {
+//            COSStreamArray array = (COSStreamArray)originalPageContents;
+//            pageStream = new PDFStream();
+//            InputStream in = array.getUnfilteredStream();
+//            OutputStream out = pageStream.getBufferOutputStream();
+//            IOUtils.copyLarge(in, out);
+//            filter = FILTER_FILTER;
+//        } else {
             pageStream = (PDFStream)cloneForNewDocument(originalPageContents);
             filter = Collections.EMPTY_SET;
-        }
+//        }
         if (pageStream == null) {
             pageStream = new PDFStream();
         }
@@ -423,8 +418,8 @@ public class PDFBoxAdapter {
 
         transferPageDict(fonts, uniqueName, sourcePageResources);
 
-        PDRectangle mediaBox = page.findMediaBox();
-        PDRectangle cropBox = page.findCropBox();
+        PDRectangle mediaBox = page.getMediaBox();
+        PDRectangle cropBox = page.getCropBox();
         PDRectangle viewBox = cropBox != null ? cropBox : mediaBox;
 
         //Handle the /Rotation entry on the page dict
@@ -444,6 +439,19 @@ public class PDFBoxAdapter {
         atdoc.scale(-1, 1);
         atdoc.translate(-viewBox.getLowerLeftX(), -viewBox.getLowerLeftY());
 
+        rotate(rotation, viewBox, atdoc);
+
+        StringBuilder boxStr = new StringBuilder();
+        boxStr.append(PDFNumber.doubleOut(mediaBox.getLowerLeftX())).append(' ')
+                .append(PDFNumber.doubleOut(mediaBox.getLowerLeftY())).append(' ')
+                .append(PDFNumber.doubleOut(mediaBox.getWidth())).append(' ')
+                .append(PDFNumber.doubleOut(mediaBox.getHeight())).append(" re W n\n");
+        return boxStr.toString() + IOUtils.toString(pdStream.createInputStream(null), "ISO-8859-1");
+    }
+
+    private void rotate(int rotation, PDRectangle viewBox, AffineTransform atdoc) {
+        float x = viewBox.getWidth() + viewBox.getLowerLeftX();
+        float y = viewBox.getHeight() + viewBox.getLowerLeftY();
         switch (rotation) {
             case 90:
                 atdoc.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
@@ -452,23 +460,27 @@ public class PDFBoxAdapter {
                 atdoc.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
                 break;
             case 180:
-                atdoc.translate(viewBox.getWidth(), viewBox.getHeight());
+                atdoc.translate(x, y);
                 atdoc.rotate(-Math.PI);
+                atdoc.translate(-viewBox.getLowerLeftX(), -viewBox.getLowerLeftY());
                 break;
             case 270:
-                atdoc.translate(0, viewBox.getHeight());
+                atdoc.translate(viewBox.getLowerLeftX(), y);
                 atdoc.rotate(Math.toRadians(270 + 180));
-                atdoc.translate(-viewBox.getWidth(), -viewBox.getHeight());
+                atdoc.translate(-x, -y);
                 break;
             default:
                 //no additional transformations necessary
                 break;
         }
-        StringBuilder boxStr = new StringBuilder();
-        boxStr.append(0).append(' ').append(0).append(' ');
-        boxStr.append(PDFNumber.doubleOut(mediaBox.getWidth())).append(' ');
-        boxStr.append(PDFNumber.doubleOut(mediaBox.getHeight())).append(" re W n\n");
-        return boxStr.toString() + pdStream.getInputStreamAsString();
+    }
+
+    private PDStream getContents(PDPage page) throws IOException {
+        PDStream pdStream = new PDStream(new COSStream());
+        OutputStream os = pdStream.createOutputStream();
+        IOUtils.copy(page.getContents(), os);
+        os.close();
+        return pdStream;
     }
 
     private void mergeXObj(COSDictionary sourcePageResources, FontInfo fontinfo, UniqueName uniqueName)
@@ -493,7 +505,7 @@ public class PDFBoxAdapter {
                             }
                         }
                         PDFWriter writer = new MergeFontsPDFWriter(src, fontinfo, uniqueName, parentFonts, 0);
-                        String c = writer.writeText(PDStream.createFromCOS(stream));
+                        String c = writer.writeText(new PDStream(stream));
                         if (c != null) {
                             stream.removeItem(COSName.FILTER);
                             newXObj.put(i.getKey(), c);
@@ -532,7 +544,7 @@ public class PDFBoxAdapter {
                 targetPage.getPDFResources().addFont(name, (PDFDictionary)cloneForNewDocument(f.getValue()));
             }
         }
-        for (Map.Entry<COSName, COSBase> e : sourcePageResources.getCOSDictionary().entrySet()) {
+        for (Map.Entry<COSName, COSBase> e : sourcePageResources.getCOSObject().entrySet()) {
             transferDict(e, uniqueName);
         }
     }
@@ -580,31 +592,26 @@ public class PDFBoxAdapter {
         //Pseudo-cache the target page in place of the original source page.
         //This essentially replaces the original page reference with the target page.
         COSObject cosPage = null;
-        if (page.getCOSObject() instanceof COSObject) {
-            cosPage = (COSObject) page.getCOSObject();
-            cacheClonedObject(cosPage, this.targetPage);
-        } else {
-            PDPageNode pageNode = page.getParent();
-            COSArray kids = (COSArray) pageNode.getDictionary().getDictionaryObject(COSName.KIDS);
-            for (int i = 0; i < kids.size(); i++) {
-                //Hopefully safe to cast, as kids need to be indirect objects
-                COSObject kid = (COSObject) kids.get(i);
-                if (!pageNumbers.containsKey(i)) {
-                    PDFArray a = new PDFArray();
-                    a.add(null);
-                    pdfDoc.assignObjectNumber(a);
-                    pdfDoc.addTrailerObject(a);
-                    pageNumbers.put(i, a);
-                }
-                cacheClonedObject(kid, pageNumbers.get(i));
-                if (kid.getObject() == page.getCOSObject()) {
-                    cosPage = kid;
-                }
-            }
-            if (cosPage == null) {
-                throw new IOException("Illegal PDF. Page not part of parent page node.");
+        COSDictionary parentDic = (COSDictionary) page.getCOSObject().getDictionaryObject(COSName.PARENT, COSName.P);
+        COSArray kids = (COSArray) parentDic.getDictionaryObject(COSName.KIDS);
+        for (int i = 0; i < kids.size(); i++) {
+            //Hopefully safe to cast, as kids need to be indirect objects
+            COSObject kid = (COSObject) kids.get(i);
+            if (!pageNumbers.containsKey(i)) {
+                PDFArray a = new PDFArray();
+                a.add(null);
+                pdfDoc.assignObjectNumber(a);
+                pdfDoc.addTrailerObject(a);
+                pageNumbers.put(i, a);
+            }
+            cacheClonedObject(kid, pageNumbers.get(i));
+            if (kid.getObject() == page.getCOSObject()) {
+                cosPage = kid;
             }
         }
+        if (cosPage == null) {
+            throw new IOException("Illegal PDF. Page not part of parent page node.");
+        }
 
         Set<COSObject> fields = copyAnnotations(page);
 
@@ -661,7 +668,7 @@ public class PDFBoxAdapter {
     }
 
     private Set<COSObject> copyAnnotations(PDPage page) throws IOException {
-        COSArray annots = (COSArray) page.getCOSDictionary().getDictionaryObject(COSName.ANNOTS);
+        COSArray annots = (COSArray) page.getCOSObject().getDictionaryObject(COSName.ANNOTS);
         Set<COSObject> fields = Collections.emptySet();
         if (annots != null) {
             fields = new TreeSet<COSObject>(new CompareFields());
@@ -696,8 +703,8 @@ public class PDFBoxAdapter {
     }
 
     private void moveAnnotations(PDPage page, List pageAnnotations, AffineTransform at) {
-        PDRectangle mediaBox = page.findMediaBox();
-        PDRectangle cropBox = page.findCropBox();
+        PDRectangle mediaBox = page.getMediaBox();
+        PDRectangle cropBox = page.getCropBox();
         PDRectangle viewBox = cropBox != null ? cropBox : mediaBox;
         for (Object obj : pageAnnotations) {
             PDAnnotation annot = (PDAnnotation)obj;
@@ -705,19 +712,22 @@ public class PDFBoxAdapter {
             float translateX = (float) (at.getTranslateX() - viewBox.getLowerLeftX());
             float translateY = (float) (at.getTranslateY() - viewBox.getLowerLeftY());
             if (rect != null) {
-                rect.move(translateX, translateY);
+                rect.setUpperRightX(rect.getUpperRightX() + translateX);
+                rect.setLowerLeftX(rect.getLowerLeftX() + translateX);
+                rect.setUpperRightY(rect.getUpperRightY() + translateY);
+                rect.setLowerLeftY(rect.getLowerLeftY() + translateY);
                 annot.setRectangle(rect);
             }
-            COSArray vertices = (COSArray) annot.getDictionary().getDictionaryObject("Vertices");
-            if (vertices != null) {
-                Iterator iter = vertices.iterator();
-                while (iter.hasNext()) {
-                    COSFloat x = (COSFloat) iter.next();
-                    COSFloat y = (COSFloat) iter.next();
-                    x.setValue(x.floatValue() + translateX);
-                    y.setValue(y.floatValue() + translateY);
-                }
-            }
+//            COSArray vertices = (COSArray) annot.getCOSObject().getDictionaryObject("Vertices");
+//            if (vertices != null) {
+//                Iterator iter = vertices.iterator();
+//                while (iter.hasNext()) {
+//                    COSFloat x = (COSFloat) iter.next();
+//                    COSFloat y = (COSFloat) iter.next();
+//                    x.setValue(x.floatValue() + translateX);
+//                    y.setValue(y.floatValue() + translateY);
+//                }
+//            }
         }
     }
 
@@ -725,7 +735,7 @@ public class PDFBoxAdapter {
         private static final long serialVersionUID = -6081505461660440801L;
 
         public int compare(COSObject o1, COSObject o2) {
-            return o1.getObjectNumber().intValue() - o2.getObjectNumber().intValue();
+            return (int) (o1.getObjectNumber() - o2.getObjectNumber());
         }
     }
 }

Modified: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFUtil.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFUtil.java?rev=1739083&r1=1739082&r2=1739083&view=diff
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFUtil.java (original)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFUtil.java Thu Apr 14 12:52:43 2016
@@ -36,7 +36,7 @@ public final class PDFUtil {
      */
     public static int getNormalizedRotation(PDPage page) {
         //Handle the /Rotation entry on the page dict
-        int rotation = page.findRotation();
+        int rotation = page.getRotation();
         rotation %= 360;
         if (rotation < 0) {
             rotation += 360;

Modified: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFWriter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFWriter.java?rev=1739083&r1=1739082&r2=1739083&view=diff
==============================================================================
--- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFWriter.java (original)
+++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFWriter.java Thu Apr 14 12:52:43 2016
@@ -21,11 +21,11 @@ import java.text.DecimalFormat;
 import java.text.DecimalFormatSymbols;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 
+import org.apache.pdfbox.contentstream.operator.Operator;
 import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSBoolean;
@@ -36,7 +36,7 @@ import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSString;
 import org.apache.pdfbox.pdfparser.PDFStreamParser;
 import org.apache.pdfbox.pdmodel.common.PDStream;
-import org.apache.pdfbox.util.operator.Operator;
+
 
 public class PDFWriter {
     protected StringBuilder s = new StringBuilder();
@@ -49,14 +49,15 @@ public class PDFWriter {
     }
 
     public String writeText(PDStream pdStream) throws IOException {
-        Iterator<Object> it = new PDFStreamParser(pdStream).getTokenIterator();
+        PDFStreamParser pdfStreamParser = new PDFStreamParser(pdStream);
+        pdfStreamParser.parse();
+        List<Object> it = pdfStreamParser.getTokens();
         List<COSBase> arguments = new ArrayList<COSBase>();
-        while (it.hasNext()) {
-            Object o = it.next();
+        for (Object o : it) {
             if (o instanceof Operator) {
                 Operator op = (Operator)o;
                 readPDFArguments(op, arguments);
-                s.append(op.getOperation() + "\n");
+                s.append(op.getName() + "\n");
                 arguments.clear();
                 if (op.getImageParameters() != null) {
                     for (Map.Entry<COSName, COSBase> cn : op.getImageParameters().entrySet()) {
@@ -94,14 +95,14 @@ public class PDFWriter {
             s.append("/" + key.getName(cn));
             s.append(" ");
         } else if (c instanceof COSString) {
-            s.append("<" + ((COSString) c).getHexString() + ">");
+            s.append("<" + ((COSString) c).toHexString() + ">");
         } else if (c instanceof COSArray) {
             s.append("[");
             readPDFArguments(op, (Collection<COSBase>) ((COSArray) c).toList());
             s.append("] ");
         } else if (c instanceof COSDictionary) {
             Collection<COSBase> dictArgs = new ArrayList<COSBase>();
-            if (currentMCID != 0 && op.getOperation().equals("BDC")) {
+            if (currentMCID != 0 && op.getName().equals("BDC")) {
                 for (Map.Entry<COSName, COSBase> cn : ((COSDictionary)c).entrySet()) {
                     if (cn.getKey().getName().equals("MCID")) {
                         updateMCID(cn, dictArgs);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.