Hello,

I've implemented a small patch which allows to automatically set the overprint state for text in pdf output. This is useful when text and background colors are printed on different printing plates because it ensures that no white spaces are visible if the printing plates are not positioned perfectly.

See also: http://www.nabble.com/Overprint-Black-Text-td9852903.html

Feel free to review or integrate it into fop trunk if it's for any use to you.

Usage:
foUserAgent.getRendererOptions().put(PDFConfigurationConstants.KEY_ENABLE_AUTO_OVERPRINT, Boolean.TRUE);

Regards,
Matthias Reischenbacher



__________ Information from ESET NOD32 Antivirus, version of virus signature 
database 4470 (20090930) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

Index: src/java/org/apache/fop/pdf/PDFGState.java
===================================================================
--- src/java/org/apache/fop/pdf/PDFGState.java  (revision 820021)
+++ src/java/org/apache/fop/pdf/PDFGState.java  (working copy)
@@ -153,6 +153,9 @@
         sb.append("<<\n/Type /ExtGState\n");
         appendVal(sb, GSTATE_ALPHA_NONSTROKE);
         appendVal(sb, GSTATE_ALPHA_STROKE);
+        appendVal(sb, GSTATE_OVERPRINT_FILL);
+        appendVal(sb, GSTATE_OVERPRINT_MODE);
+        appendVal(sb, GSTATE_OVERPRINT_STROKE);
 
         sb.append(">>\nendobj\n");
         return sb.toString();
Index: src/java/org/apache/fop/render/pdf/PDFConfigurationConstants.java
===================================================================
--- src/java/org/apache/fop/render/pdf/PDFConfigurationConstants.java   
(revision 820021)
+++ src/java/org/apache/fop/render/pdf/PDFConfigurationConstants.java   
(working copy)
@@ -49,4 +49,7 @@
      * PDF/X profile is active).
      */
     String KEY_DISABLE_SRGB_COLORSPACE = "disable-srgb-colorspace";
+    
+    /** Rendering Options key for enabling automatic text overprinting. */
+    String KEY_ENABLE_AUTO_OVERPRINT = "auto-overprint-enabled";
 }
Index: src/java/org/apache/fop/render/pdf/PDFPainter.java
===================================================================
--- src/java/org/apache/fop/render/pdf/PDFPainter.java  (revision 820021)
+++ src/java/org/apache/fop/render/pdf/PDFPainter.java  (working copy)
@@ -26,6 +26,8 @@
 import java.awt.Rectangle;
 import java.awt.geom.AffineTransform;
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
 
 import org.w3c.dom.Document;
 
@@ -39,6 +41,7 @@
 import org.apache.fop.fonts.SingleByteFont;
 import org.apache.fop.fonts.Typeface;
 import org.apache.fop.pdf.PDFDocument;
+import org.apache.fop.pdf.PDFGState;
 import org.apache.fop.pdf.PDFNumber;
 import org.apache.fop.pdf.PDFTextUtil;
 import org.apache.fop.pdf.PDFXObject;
@@ -59,6 +62,14 @@
     /** logging instance */
     private static Log log = LogFactory.getLog(PDFPainter.class);
 
+    /** Graphic state settings with overprinting enabled */
+    private static Map overprintGStateSettings = new HashMap();
+    static {
+        overprintGStateSettings.put(PDFGState.GSTATE_OVERPRINT_STROKE, 
Boolean.TRUE);
+        overprintGStateSettings.put(PDFGState.GSTATE_OVERPRINT_FILL, 
Boolean.TRUE);
+        overprintGStateSettings.put(PDFGState.GSTATE_OVERPRINT_MODE, new 
Integer(1));
+    }
+
     private PDFDocumentHandler documentHandler;
 
     /** The current content generator */
@@ -255,6 +266,25 @@
     /** {...@inheritdoc} */
     public void drawText(int x, int y, int letterSpacing, int wordSpacing, 
int[] dx, String text)
             throws IFException {
+        boolean useOverprint = false;
+        if (this.getPDFUtil().isAutoOverpintEnabled()) {
+            Color backColor = generator.currentState.getBackColor();
+            Color textColor = state.getTextColor();
+            useOverprint = !backColor.equals(Color.white) && 
!textColor.equals(Color.white)
+                    && !textColor.equals(backColor);
+        }
+
+        if (useOverprint) {
+            generator.saveGraphicsState();
+
+            PDFGState gstate = 
this.getPDFDoc().getFactory().makeGState(overprintGStateSettings,
+                    generator.getState().getGState());
+
+            documentHandler.pdfResources.addGState(gstate);
+
+            generator.add("/" + gstate.getName() + " gs\n");
+        }
+
         generator.updateColor(state.getTextColor(), true, null);
         generator.beginTextObject();
         FontTriplet triplet = new FontTriplet(
@@ -330,6 +360,10 @@
 
         }
         textutil.writeTJ();
+        
+        if (useOverprint) {
+            generator.restoreGraphicsState();
+        }
     }
 
 }
Index: src/java/org/apache/fop/render/pdf/PDFRenderingUtil.java
===================================================================
--- src/java/org/apache/fop/render/pdf/PDFRenderingUtil.java    (revision 
820021)
+++ src/java/org/apache/fop/render/pdf/PDFRenderingUtil.java    (working copy)
@@ -91,6 +91,7 @@
     /** Optional URI to an output profile to be used. */
     protected String outputProfileURI;
 
+    protected boolean autoOverprintEnabled = false;
 
     PDFRenderingUtil(FOUserAgent userAgent) {
         this.userAgent = userAgent;
@@ -173,6 +174,10 @@
         if (setting != null) {
             this.disableSRGBColorSpace = booleanValueOf(setting);
         }
+        setting = 
userAgent.getRendererOptions().get(KEY_ENABLE_AUTO_OVERPRINT);
+        if (setting != null) {
+            this.autoOverprintEnabled = booleanValueOf(setting);
+        }
     }
 
     public FOUserAgent getUserAgent() {
@@ -408,4 +413,11 @@
         nums.put(pageIndex, dict);
     }
 
+    /**
+     * Checks if automatic overprinting is enabled (defaults to false).
+     * @return true if overprinting should be applied to text 
+     */
+    public boolean isAutoOverpintEnabled() {
+        return autoOverprintEnabled;
+    }
 }
---------------------------------------------------------------------
To unsubscribe, e-mail: fop-users-unsubscr...@xmlgraphics.apache.org
For additional commands, e-mail: fop-users-h...@xmlgraphics.apache.org

Reply via email to