aboutsummaryrefslogtreecommitdiffstats
path: root/fop-core/src
diff options
context:
space:
mode:
authorSimon Steiner <ssteiner@apache.org>2022-09-01 09:36:44 +0000
committerSimon Steiner <ssteiner@apache.org>2022-09-01 09:36:44 +0000
commitc974d14e5c8c1fc8fb2b869939613ded4d46745d (patch)
tree0f4e92fac5a318dbf56c40a01575fd1fe825240f /fop-core/src
parent96ed0cd419f9720dd6e1364a169df9cdd608b871 (diff)
downloadxmlgraphics-fop-c974d14e5c8c1fc8fb2b869939613ded4d46745d.tar.gz
xmlgraphics-fop-c974d14e5c8c1fc8fb2b869939613ded4d46745d.zip
FOP-3091: Add transparency color support for PDF
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1903803 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'fop-core/src')
-rw-r--r--fop-core/src/main/java/org/apache/fop/fo/expr/PropertyTokenizer.java2
-rw-r--r--fop-core/src/main/java/org/apache/fop/pdf/PDFColorHandler.java27
-rw-r--r--fop-core/src/main/java/org/apache/fop/pdf/PDFDocument.java2
-rw-r--r--fop-core/src/main/java/org/apache/fop/render/pdf/PDFContentGenerator.java4
-rw-r--r--fop-core/src/main/java/org/apache/fop/render/ps/PSImageHandlerRenderedImage.java2
-rw-r--r--fop-core/src/main/java/org/apache/fop/svg/PDFDocumentGraphics2D.java4
-rw-r--r--fop-core/src/main/java/org/apache/fop/svg/PDFGraphics2D.java4
-rw-r--r--fop-core/src/test/java/org/apache/fop/render/pdf/PDFPainterTestCase.java32
8 files changed, 64 insertions, 13 deletions
diff --git a/fop-core/src/main/java/org/apache/fop/fo/expr/PropertyTokenizer.java b/fop-core/src/main/java/org/apache/fop/fo/expr/PropertyTokenizer.java
index f9b4cd87d..8e278156a 100644
--- a/fop-core/src/main/java/org/apache/fop/fo/expr/PropertyTokenizer.java
+++ b/fop-core/src/main/java/org/apache/fop/fo/expr/PropertyTokenizer.java
@@ -229,7 +229,7 @@ class PropertyTokenizer {
++exprIndex;
scanHexDigits();
int len = exprIndex - currentTokenStartIndex - 1;
- if (len % 3 == 0) {
+ if (len % 3 == 0 || len == 8) {
currentToken = TOK_COLORSPEC;
} else {
//Actually not a color at all, but an NCNAME starting with "#"
diff --git a/fop-core/src/main/java/org/apache/fop/pdf/PDFColorHandler.java b/fop-core/src/main/java/org/apache/fop/pdf/PDFColorHandler.java
index 027fedce9..193bd6dfd 100644
--- a/fop-core/src/main/java/org/apache/fop/pdf/PDFColorHandler.java
+++ b/fop-core/src/main/java/org/apache/fop/pdf/PDFColorHandler.java
@@ -23,6 +23,7 @@ import java.awt.Color;
import java.awt.color.ColorSpace;
import java.awt.color.ICC_ColorSpace;
import java.awt.color.ICC_Profile;
+import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
@@ -45,6 +46,7 @@ public class PDFColorHandler {
private Log log = LogFactory.getLog(PDFColorHandler.class);
private PDFResources resources;
+ private PDFResourceContext resourceContext;
private Map<String, PDFCIELabColorSpace> cieLabColorSpaces;
@@ -52,8 +54,9 @@ public class PDFColorHandler {
* Create a new instance for the given {@link PDFResources}
* @param resources the PDF resources
*/
- public PDFColorHandler(PDFResources resources) {
+ public PDFColorHandler(PDFResources resources, PDFResourceContext resourceContext) {
this.resources = resources;
+ this.resourceContext = resourceContext;
}
private PDFDocument getDocument() {
@@ -67,7 +70,7 @@ public class PDFColorHandler {
* @param color the color
* @param fill true for fill color, false for stroke color
*/
- public void establishColor(StringBuffer codeBuffer, Color color, boolean fill) {
+ public void establishColor(StringBuffer codeBuffer, Color color, boolean fill, boolean alpha) {
if (color instanceof ColorWithAlternatives) {
ColorWithAlternatives colExt = (ColorWithAlternatives)color;
//Alternate colors have priority
@@ -87,7 +90,7 @@ public class PDFColorHandler {
//Fallback
boolean established = establishColorFromColor(codeBuffer, color, fill);
if (!established) {
- establishDeviceRGB(codeBuffer, color, fill);
+ establishDeviceRGB(codeBuffer, color, fill, alpha);
}
}
@@ -188,7 +191,7 @@ public class PDFColorHandler {
}
}
- private void establishDeviceRGB(StringBuffer codeBuffer, Color color, boolean fill) {
+ private void establishDeviceRGB(StringBuffer codeBuffer, Color color, boolean fill, boolean alpha) {
float[] comps;
if (color.getColorSpace().isCS_sRGB()) {
comps = color.getColorComponents(null);
@@ -199,6 +202,9 @@ public class PDFColorHandler {
ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB);
comps = color.getColorComponents(sRGB, null);
}
+ if (alpha) {
+ writeAlpha(color, codeBuffer);
+ }
if (ColorUtil.isGray(color)) {
comps = new float[] {comps[0]}; //assuming that all components are the same
writeColor(codeBuffer, comps, 1, (fill ? "g" : "G"));
@@ -207,7 +213,20 @@ public class PDFColorHandler {
}
}
+ private void writeAlpha(Color color, StringBuffer codeBuffer) {
+ int alpha = color.getAlpha();
+ if (alpha != 255 && alpha != 0 && resourceContext != null) {
+ Map<String, Float> vals = new HashMap<>();
+ vals.put(PDFGState.GSTATE_ALPHA_NONSTROKE, alpha / 255f);
+ vals.put(PDFGState.GSTATE_ALPHA_STROKE, 1.0f);
+ PDFGState gState = getDocument().getFactory().makeGState(vals, PDFGState.DEFAULT);
+ resourceContext.addGState(gState);
+ codeBuffer.append("/").append(gState.getName()).append(" gs\n");
+ }
+ }
+
private void establishDeviceCMYK(StringBuffer codeBuffer, Color color, boolean fill) {
+ writeAlpha(color, codeBuffer);
writeColor(codeBuffer, color, 4, (fill ? "k" : "K"));
}
diff --git a/fop-core/src/main/java/org/apache/fop/pdf/PDFDocument.java b/fop-core/src/main/java/org/apache/fop/pdf/PDFDocument.java
index 220875349..2e13e7015 100644
--- a/fop-core/src/main/java/org/apache/fop/pdf/PDFDocument.java
+++ b/fop-core/src/main/java/org/apache/fop/pdf/PDFDocument.java
@@ -751,7 +751,7 @@ public class PDFDocument {
poss = new PDFGState();
poss.addValues(current);
poss.addValues(avail);
- if (poss.equals(wanted)) {
+ if (poss.contentEquals(wanted)) {
return avail;
}
}
diff --git a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFContentGenerator.java b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFContentGenerator.java
index c11df3c94..668734bde 100644
--- a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFContentGenerator.java
+++ b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFContentGenerator.java
@@ -96,7 +96,7 @@ public class PDFContentGenerator {
};
this.currentState = new PDFPaintingState();
- this.colorHandler = new PDFColorHandler(document.getResources());
+ this.colorHandler = new PDFColorHandler(document.getResources(), resourceContext);
this.context = context;
}
@@ -455,7 +455,7 @@ public class PDFContentGenerator {
*/
protected void setColor(Color col, boolean fill, StringBuffer pdf) {
if (pdf != null) {
- colorHandler.establishColor(pdf, col, fill);
+ colorHandler.establishColor(pdf, col, fill, true);
} else {
setColor(col, fill, getStream());
}
diff --git a/fop-core/src/main/java/org/apache/fop/render/ps/PSImageHandlerRenderedImage.java b/fop-core/src/main/java/org/apache/fop/render/ps/PSImageHandlerRenderedImage.java
index 6b5745e86..eec267c3a 100644
--- a/fop-core/src/main/java/org/apache/fop/render/ps/PSImageHandlerRenderedImage.java
+++ b/fop-core/src/main/java/org/apache/fop/render/ps/PSImageHandlerRenderedImage.java
@@ -83,7 +83,7 @@ public class PSImageHandlerRenderedImage implements PSImageHandler {
ImageEncodingHelper helper = new ImageEncodingHelper(ri);
ColorModel cm = helper.getEncodedColorModel();
- PSImageUtils.writeImage(encoder, imgDim, imgDescription, targetRect, cm, gen, ri);
+ PSImageUtils.writeImage(encoder, imgDim, imgDescription, targetRect, cm, gen, ri, null);
}
/** {@inheritDoc} */
diff --git a/fop-core/src/main/java/org/apache/fop/svg/PDFDocumentGraphics2D.java b/fop-core/src/main/java/org/apache/fop/svg/PDFDocumentGraphics2D.java
index 0e49cc40c..ab03355b7 100644
--- a/fop-core/src/main/java/org/apache/fop/svg/PDFDocumentGraphics2D.java
+++ b/fop-core/src/main/java/org/apache/fop/svg/PDFDocumentGraphics2D.java
@@ -102,7 +102,7 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D {
this.pdfDoc = new PDFDocument("Apache FOP Version " + Version.getVersion()
+ ": PDFDocumentGraphics2D");
this.pdfContext = new PDFContext();
- this.colorHandler = new PDFColorHandler(this.pdfDoc.getResources());
+ this.colorHandler = new PDFColorHandler(this.pdfDoc.getResources(), resourceContext);
}
/**
@@ -237,7 +237,7 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D {
public void setBackgroundColor(Color col) {
StringBuffer sb = new StringBuffer();
sb.append("q\n");
- this.colorHandler.establishColor(sb, col, true);
+ this.colorHandler.establishColor(sb, col, true, true);
sb.append("0 0 ").append(width).append(" ").append(height).append(" re\n");
diff --git a/fop-core/src/main/java/org/apache/fop/svg/PDFGraphics2D.java b/fop-core/src/main/java/org/apache/fop/svg/PDFGraphics2D.java
index dbb0a2e65..cf8701522 100644
--- a/fop-core/src/main/java/org/apache/fop/svg/PDFGraphics2D.java
+++ b/fop-core/src/main/java/org/apache/fop/svg/PDFGraphics2D.java
@@ -217,7 +217,7 @@ public class PDFGraphics2D extends AbstractGraphics2D implements NativeImageHand
TransparencyIgnoredEventListener listener) {
this(textAsShapes);
pdfDoc = doc;
- this.colorHandler = new PDFColorHandler(doc.getResources());
+ this.colorHandler = new PDFColorHandler(doc.getResources(), page);
resourceContext = page;
currentFontName = font;
currentFontSize = size;
@@ -790,7 +790,7 @@ public class PDFGraphics2D extends AbstractGraphics2D implements NativeImageHand
}
if (doWrite) {
StringBuffer sb = new StringBuffer();
- colorHandler.establishColor(sb, col, fill);
+ colorHandler.establishColor(sb, col, fill, false);
currentStream.write(sb.toString());
}
}
diff --git a/fop-core/src/test/java/org/apache/fop/render/pdf/PDFPainterTestCase.java b/fop-core/src/test/java/org/apache/fop/render/pdf/PDFPainterTestCase.java
index 41747c72d..9a1872f5f 100644
--- a/fop-core/src/test/java/org/apache/fop/render/pdf/PDFPainterTestCase.java
+++ b/fop-core/src/test/java/org/apache/fop/render/pdf/PDFPainterTestCase.java
@@ -471,4 +471,36 @@ public class PDFPainterTestCase {
pdfPainter.drawImageUsingImageHandler(info, new Rectangle());
Assert.assertEquals(event[0].getEventKey(), "imageWritingError");
}
+
+ @Test
+ public void testAlphaColor() throws Exception {
+ FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
+ foUserAgent = fopFactory.newFOUserAgent();
+ PDFDocumentHandler pdfDocumentHandler = new PDFDocumentHandler(new IFContext(foUserAgent));
+ pdfDocumentHandler.setResult(new StreamResult(new ByteArrayOutputStream()));
+ pdfDocumentHandler.startDocument();
+ pdfDocumentHandler.startPage(0, "", "", new Dimension());
+ PDFPainter pdfPainter = new PDFPainter(pdfDocumentHandler, null);
+ pdfPainter.fillRect(new Rectangle(10, 10), new Color(0, 0, 0, 128));
+ PDFFilterList filters = pdfPainter.generator.getStream().getFilterList();
+ filters.setDisableAllFilters(true);
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ pdfPainter.generator.getStream().output(bos);
+ Assert.assertEquals("<< /Length 1 0 R >>\n"
+ + "stream\n"
+ + "q\n"
+ + "1 0 0 -1 0 0 cm\n"
+ + "/GS1 gs\n"
+ + "0 g\n"
+ + "0 0 0.01 0.01 re f\n"
+ + "\n"
+ + "endstream", bos.toString());
+ bos.reset();
+ pdfDocumentHandler.getCurrentPage().getGStates().iterator().next().output(bos);
+ Assert.assertEquals(bos.toString(), "<<\n"
+ + "/Type /ExtGState\n"
+ + "/ca 0.5019608\n"
+ + "/CA 1.0\n"
+ + ">>");
+ }
}