Brought the branch in sync with rev. 1590916 of trunk


git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_WhitespaceManagement@1591437 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/findbugs-exclude.xml b/findbugs-exclude.xml
index 4dc4cb1..8e77622 100644
--- a/findbugs-exclude.xml
+++ b/findbugs-exclude.xml
@@ -444,6 +444,21 @@
           <Method name="renderSVGDocument"/>
         </Or>
       </And>
+      <And>
+        <Class name="org.apache.fop.render.ps.PSTextPainter"/>
+        <Or>
+          <Field name="gen"/>
+          <Field name="ps"/>
+          <Field name="psRun"/>
+          <Field name="textUtil"/>
+        </Or>
+      </And>
+      <And>
+        <Class name="org.apache.fop.svg.PDFTextPainter"/>
+        <Or>
+          <Field name="pdf"/>
+        </Or>
+      </And>
     </Or>
   </Match>
   <Match>
diff --git a/src/java/org/apache/fop/pdf/PDFProfile.java b/src/java/org/apache/fop/pdf/PDFProfile.java
index 4ae5ba8..4289d0e 100644
--- a/src/java/org/apache/fop/pdf/PDFProfile.java
+++ b/src/java/org/apache/fop/pdf/PDFProfile.java
@@ -171,8 +171,7 @@
     public void verifyTransparencyAllowed(String context) {
         Object profile = isTransparencyAllowed();
         if (profile != null) {
-            throw new PDFConformanceException(profile + " does not allow the use of transparency. ("
-                    + profile + ")");
+            throw new TransparencyDisallowedException(profile, context);
         }
     }
 
diff --git a/src/java/org/apache/fop/pdf/PDFResources.java b/src/java/org/apache/fop/pdf/PDFResources.java
index 6d09d57..dc6319f 100644
--- a/src/java/org/apache/fop/pdf/PDFResources.java
+++ b/src/java/org/apache/fop/pdf/PDFResources.java
@@ -45,7 +45,7 @@
     /**
      * /Font objects keyed by their internal name
      */
-    protected Map<String, PDFFont> fonts = new LinkedHashMap<String, PDFFont>();
+    protected Map<String, PDFDictionary> fonts = new LinkedHashMap<String, PDFDictionary>();
 
     /**
      * Set of XObjects
@@ -76,6 +76,8 @@
     /** Named properties */
     protected Map<String, PDFReference> properties = new LinkedHashMap<String, PDFReference>();
 
+    private PDFResources parent;
+
     /**
      * create a /Resources object.
      *
@@ -87,13 +89,29 @@
         setObjectNumber(objnum);
     }
 
+    public void setParentResources(PDFResources p) {
+        parent = p;
+    }
+
+    public PDFResources getParentResources() {
+        return parent;
+    }
+
     /**
      * add font object to resources list.
      *
      * @param font the PDFFont to add
      */
     public void addFont(PDFFont font) {
-        this.fonts.put(font.getName(), font);
+        fonts.put(font.getName(), font);
+    }
+
+    public void addFont(String name, PDFDictionary font) {
+        fonts.put(name, font);
+    }
+
+    public Map<String, PDFDictionary> getFonts() {
+        return fonts;
     }
 
     /**
@@ -220,12 +238,17 @@
     }
 
     private void populateDictionary() {
-        if (!this.fonts.isEmpty()) {
+        if (!this.fonts.isEmpty() || (parent != null && !parent.getFonts().isEmpty())) {
             PDFDictionary dict = new PDFDictionary(this);
             /* construct PDF dictionary of font object references */
-            for (Map.Entry<String, PDFFont> entry : fonts.entrySet()) {
+            for (Map.Entry<String, PDFDictionary> entry : fonts.entrySet()) {
                 dict.put(entry.getKey(), entry.getValue());
             }
+            if (parent != null) {
+                for (Map.Entry<String, PDFDictionary> entry : parent.getFonts().entrySet()) {
+                    dict.put(entry.getKey(), entry.getValue());
+                }
+            }
             put("Font", dict);
         }
 
diff --git a/src/java/org/apache/fop/pdf/TransparencyDisallowedException.java b/src/java/org/apache/fop/pdf/TransparencyDisallowedException.java
new file mode 100644
index 0000000..11d8baf
--- /dev/null
+++ b/src/java/org/apache/fop/pdf/TransparencyDisallowedException.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.pdf;
+
+/**
+ * The PDF profile being used does not allow transparency.
+ */
+public class TransparencyDisallowedException extends PDFConformanceException {
+
+    private static final long serialVersionUID = -1653621832449817596L;
+
+    private final Object profile;
+
+    private final String context;
+
+    public TransparencyDisallowedException(Object profile, String context) {
+        super(profile + " does not allow the use of transparency."
+                + (context == null ? "" : " (" + context + ")"));
+        this.profile = profile;
+        this.context = context;
+    }
+
+    /**
+     * Returns the profile that is being used and disallows transparency.
+     *
+     * @see PDFAMode
+     * @see PDFXMode
+     */
+    public Object getProfile() {
+        return profile;
+    }
+
+    /**
+     * Returns context information to help spotting the problem.
+     */
+    public String getContext() {
+        return context;
+    }
+
+}
diff --git a/src/java/org/apache/fop/render/bitmap/AbstractBitmapDocumentHandler.java b/src/java/org/apache/fop/render/bitmap/AbstractBitmapDocumentHandler.java
index 8d0ce14..374fa58 100644
--- a/src/java/org/apache/fop/render/bitmap/AbstractBitmapDocumentHandler.java
+++ b/src/java/org/apache/fop/render/bitmap/AbstractBitmapDocumentHandler.java
@@ -229,7 +229,10 @@
         }
 
         //Set up bitmap to paint on
-        this.currentImage = createBufferedImage(bitmapWidth, bitmapHeight);
+        if (currentImage == null || currentImage.getWidth() != bitmapWidth
+                || currentImage.getHeight() != bitmapHeight) {
+            currentImage = createBufferedImage(bitmapWidth, bitmapHeight);
+        }
         Graphics2D graphics2D = this.currentImage.createGraphics();
 
         // draw page background
@@ -316,7 +319,6 @@
                 this.multiImageWriter.writeImage(this.currentImage,
                         getSettings().getWriterParams());
             }
-            this.currentImage = null;
         } catch (IOException ioe) {
             throw new IFException("I/O error while encoding BufferedImage", ioe);
         }
diff --git a/src/java/org/apache/fop/render/pdf/PDFContentGenerator.java b/src/java/org/apache/fop/render/pdf/PDFContentGenerator.java
index ac7b1d9..3f75554 100644
--- a/src/java/org/apache/fop/render/pdf/PDFContentGenerator.java
+++ b/src/java/org/apache/fop/render/pdf/PDFContentGenerator.java
@@ -486,6 +486,13 @@
         restoreGraphicsState();
     }
 
+    public void placeImage(AffineTransform at, String stream) {
+        saveGraphicsState();
+        concatenate(at);
+        add(stream);
+        restoreGraphicsState();
+    }
+
     /**
      * Places a previously registered image at a certain place on the page,
      * bracketing it as a marked-content sequence.
diff --git a/src/java/org/apache/fop/render/pdf/PDFImageHandlerSVG.java b/src/java/org/apache/fop/render/pdf/PDFImageHandlerSVG.java
index ae282f1..a71fe94 100644
--- a/src/java/org/apache/fop/render/pdf/PDFImageHandlerSVG.java
+++ b/src/java/org/apache/fop/render/pdf/PDFImageHandlerSVG.java
@@ -44,6 +44,7 @@
 import org.apache.fop.events.EventBroadcaster;
 import org.apache.fop.image.loader.batik.BatikImageFlavors;
 import org.apache.fop.image.loader.batik.BatikUtil;
+import org.apache.fop.pdf.TransparencyDisallowedException;
 import org.apache.fop.render.ImageHandler;
 import org.apache.fop.render.ImageHandlerUtil;
 import org.apache.fop.render.RenderingContext;
@@ -207,6 +208,10 @@
             root.paint(graphics);
             ctx.dispose();
             generator.add(graphics.getString());
+        } catch (TransparencyDisallowedException e) {
+            SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
+                    context.getUserAgent().getEventBroadcaster());
+            eventProducer.bitmapWithTransparency(this, e.getProfile(), image.getInfo().getOriginalURI());
         } catch (Exception e) {
             SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
                     context.getUserAgent().getEventBroadcaster());
diff --git a/src/java/org/apache/fop/render/ps/PSImageHandlerRenderedImage.java b/src/java/org/apache/fop/render/ps/PSImageHandlerRenderedImage.java
index d943f1c..6b5745e 100644
--- a/src/java/org/apache/fop/render/ps/PSImageHandlerRenderedImage.java
+++ b/src/java/org/apache/fop/render/ps/PSImageHandlerRenderedImage.java
@@ -19,9 +19,12 @@
 
 package org.apache.fop.render.ps;
 
+import java.awt.Color;
 import java.awt.Dimension;
+import java.awt.Graphics2D;
 import java.awt.Rectangle;
 import java.awt.geom.Rectangle2D;
+import java.awt.image.BufferedImage;
 import java.awt.image.ColorModel;
 import java.awt.image.RenderedImage;
 import java.io.IOException;
@@ -63,6 +66,17 @@
         Rectangle2D targetRect = new Rectangle2D.Double(x, y, w, h);
 
         RenderedImage ri = imageRend.getRenderedImage();
+
+        if (ri instanceof BufferedImage && ri.getColorModel().hasAlpha()) {
+            BufferedImage convertedImg = new BufferedImage(ri.getWidth(), ri.getHeight(), BufferedImage.TYPE_INT_RGB);
+            Graphics2D g = (Graphics2D) convertedImg.getGraphics();
+            g.setBackground(Color.WHITE);
+            g.clearRect(0, 0, ri.getWidth(), ri.getHeight());
+            g.drawImage((BufferedImage)ri, 0, 0, null);
+            g.dispose();
+            ri = convertedImg;
+        }
+
         ImageEncoder encoder = ImageEncodingHelper.createRenderedImageEncoder(ri);
         Dimension imgDim = new Dimension(ri.getWidth(), ri.getHeight());
         String imgDescription = ri.getClass().getName();
diff --git a/src/java/org/apache/fop/svg/AbstractFOPTextPainter.java b/src/java/org/apache/fop/svg/AbstractFOPTextPainter.java
index 7df127d..9cb8684 100644
--- a/src/java/org/apache/fop/svg/AbstractFOPTextPainter.java
+++ b/src/java/org/apache/fop/svg/AbstractFOPTextPainter.java
@@ -152,8 +152,21 @@
             double y = outputLocation.getY();
             try {
                 try {
+                    AFPGraphics2D afpg2d = (AFPGraphics2D)g2d;
+                    int fontSize = 0;
+                    if (font != null) {
+                        fontSize = (int) Math.round(afpg2d.convertToAbsoluteLength(font.getFontSize()));
+                    }
+                    if (fontSize < 6000) {
+                        nativeTextHandler.drawString(g2d, txt, (float) (x + tx), (float) y);
+                    } else {
+                        double scaleX = g2d.getTransform().getScaleX();
+                        for (int i = 0; i < txt.length(); i++) {
+                            double ad = run.getLayout().getGlyphAdvances()[i] * scaleX;
+                            nativeTextHandler.drawString(g2d, txt.charAt(i) + "", (float) (x + tx + ad), (float) y);
+                        }
+                    }
                     //TODO draw underline and overline if set
-                    nativeTextHandler.drawString(g2d, txt, (float) (x + tx), (float) y);
                     //TODO draw strikethrough if set
                 } catch (IOException ioe) {
                     if (g2d instanceof AFPGraphics2D) {
diff --git a/src/java/org/apache/fop/svg/SVGEventProducer.java b/src/java/org/apache/fop/svg/SVGEventProducer.java
index 6bf7ed1..fd529ef 100644
--- a/src/java/org/apache/fop/svg/SVGEventProducer.java
+++ b/src/java/org/apache/fop/svg/SVGEventProducer.java
@@ -98,4 +98,13 @@
      */
     void transparencyIgnored(Object source, Object pdfProfile, String uri);
 
+
+    /**
+     * SVG references a bitmap image that contains transparency while it is not allowed.
+     * @param source the event source
+     * @param pdfProfile the PDF profile that disallows transparency
+     * @param uri the image URI, if available
+     * @event.severity ERROR
+     */
+    void bitmapWithTransparency(Object source, Object pdfProfile, String uri);
 }
diff --git a/src/java/org/apache/fop/svg/SVGEventProducer.xml b/src/java/org/apache/fop/svg/SVGEventProducer.xml
index f5e1d30..e7d9b55 100644
--- a/src/java/org/apache/fop/svg/SVGEventProducer.xml
+++ b/src/java/org/apache/fop/svg/SVGEventProducer.xml
@@ -23,4 +23,5 @@
   <message key="svgNotBuilt">SVG graphic could not be built. Reason: {e}</message>
   <message key="svgRenderingError">SVG graphic could not be rendered. Reason: {e}</message>
   <message key="transparencyIgnored">Transparency in an SVG image will be ignored because {pdfProfile} does not allow it[ (see {uri})].</message>
+  <message key="bitmapWithTransparency">An SVG image is referencing a bitmap with transparency while {pdfProfile} does not allow it[ (see {uri})].</message>
 </catalogue>
diff --git a/test/java/org/apache/fop/events/EventChecker.java b/test/java/org/apache/fop/events/EventChecker.java
index 5066584..c2d5fcf 100644
--- a/test/java/org/apache/fop/events/EventChecker.java
+++ b/test/java/org/apache/fop/events/EventChecker.java
@@ -46,7 +46,7 @@
         if (id.equals(expectedEventID)) {
             eventReceived = true;
             for (Map.Entry<String, Object> param : expectedParams.entrySet()) {
-                assertEquals(event.getParam(param.getKey()), param.getValue());
+                assertEquals(param.getValue(), event.getParam(param.getKey()));
             }
         }
     }
diff --git a/test/java/org/apache/fop/render/pdf/PDFAConformanceTestCase.java b/test/java/org/apache/fop/render/pdf/PDFAConformanceTestCase.java
index b2b77e8..752d666 100644
--- a/test/java/org/apache/fop/render/pdf/PDFAConformanceTestCase.java
+++ b/test/java/org/apache/fop/render/pdf/PDFAConformanceTestCase.java
@@ -115,15 +115,29 @@
 
     @Test
     public void svgTransparency() throws Exception {
-        Map<String, Object> params = new HashMap<String, Object>();
-        params.put("pdfProfile", PDFAMode.PDFA_1B);
-        EventChecker eventChecker = new EventChecker(SVGEventProducer.class.getName()
-                + ".transparencyIgnored", params);
         FOUserAgent ua = getUserAgent();
-        ua.getEventBroadcaster().addEventListener(eventChecker);
+        EventChecker eventChecker = setupEventChecker(ua, "transparencyIgnored");
         File foFile = new File(foBaseDir, "svg-transparency.fo");
         convertFO(foFile, ua, dumpPDF);
         eventChecker.end();
     }
 
+    @Test
+    public void svgContainingBitmapWithTransparency() throws Exception {
+        FOUserAgent ua = getUserAgent();
+        EventChecker eventChecker = setupEventChecker(ua, "bitmapWithTransparency");
+        File foFile = new File(foBaseDir, "svg-with-transparent-image.fo");
+        convertFO(foFile, ua, dumpPDF);
+        eventChecker.end();
+    }
+
+    private EventChecker setupEventChecker(FOUserAgent ua, String expectedEvent) {
+        Map<String, Object> params = new HashMap<String, Object>();
+        params.put("pdfProfile", PDFAMode.PDFA_1B);
+        EventChecker eventChecker = new EventChecker(SVGEventProducer.class.getName()
+                + "." + expectedEvent, params);
+        ua.getEventBroadcaster().addEventListener(eventChecker);
+        return eventChecker;
+    }
+
 }
diff --git a/test/xml/pdf-a/svg-with-transparent-image.fo b/test/xml/pdf-a/svg-with-transparent-image.fo
new file mode 100644
index 0000000..5038722
--- /dev/null
+++ b/test/xml/pdf-a/svg-with-transparent-image.fo
@@ -0,0 +1,24 @@
+<?xml version="1.0"?>
+<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" font-family="Gladiator">
+  <fo:layout-master-set>
+    <fo:simple-page-master master-name="page"
+      page-height="320pt" page-width="420pt" margin="10pt">
+      <fo:region-body display-align="center"/>
+    </fo:simple-page-master>
+  </fo:layout-master-set>
+  <fo:page-sequence master-reference="page">
+    <fo:flow flow-name="xsl-region-body" text-align="center">
+      <fo:block start-indent="50pt" end-indent="50pt" font-size="15pt" space-after="10pt">SVG 
+        referencing a bitmap image that has transparency</fo:block>
+      <fo:block><fo:instream-foreign-object>
+          <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" 
+            width="100" height="100">
+            <rect x="0" y="0" width="100%" height="100%" fill="none" stroke="black" 
+              stroke-width="2"/>
+            <image x="0" y="0" width="100%" height="100%" 
+              xlink:href="resources/images/list-item.png"/>
+          </svg>
+      </fo:instream-foreign-object></fo:block>
+    </fo:flow>
+  </fo:page-sequence>
+</fo:root>