From a63f8d028e15609b96764d4d46ef3d05519b0705 Mon Sep 17 00:00:00 2001 From: Steve Coffman Date: Thu, 27 Apr 2000 19:33:50 +0000 Subject: [PATCH] This adds the ColorSpace data type, and has the PDF files make use of it. This should make us all agree on the colorspace identifier codes at least. Adds the beginnings of support for DeviceGray colorspace. (not in patterns, but in colors) git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@193343 13f79535-47bb-0310-9956-ffa450edef68 --- src/org/apache/fop/datatypes/ColorSpace.java | 106 ++++++++++ src/org/apache/fop/pdf/PDFColor.java | 193 ++++++++++++++----- src/org/apache/fop/pdf/PDFPathPaint.java | 15 +- src/org/apache/fop/pdf/PDFPattern.java | 6 +- src/org/apache/fop/pdf/PDFShading.java | 21 +- 5 files changed, 273 insertions(+), 68 deletions(-) create mode 100644 src/org/apache/fop/datatypes/ColorSpace.java diff --git a/src/org/apache/fop/datatypes/ColorSpace.java b/src/org/apache/fop/datatypes/ColorSpace.java new file mode 100644 index 000000000..2cda6d827 --- /dev/null +++ b/src/org/apache/fop/datatypes/ColorSpace.java @@ -0,0 +1,106 @@ +/*-- $Id$ -- + + ============================================================================ + The Apache Software License, Version 1.1 + ============================================================================ + + Copyright (C) 1999 The Apache Software Foundation. All rights reserved. + + Redistribution and use in source and binary forms, with or without modifica- + tion, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + 3. The end-user documentation included with the redistribution, if any, must + include the following acknowledgment: "This product includes software + developed by the Apache Software Foundation (http://www.apache.org/)." + Alternately, this acknowledgment may appear in the software itself, if + and wherever such third-party acknowledgments normally appear. + + 4. The names "FOP" and "Apache Software Foundation" must not be used to + endorse or promote products derived from this software without prior + written permission. For written permission, please contact + apache@apache.org. + + 5. Products derived from this software may not be called "Apache", nor may + "Apache" appear in their name, without prior written permission of the + Apache Software Foundation. + + THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This software consists of voluntary contributions made by many individuals + on behalf of the Apache Software Foundation and was originally created by + James Tauber . For more information on the Apache + Software Foundation, please see . + + */ + +package org.apache.fop.datatypes; + +public class ColorSpace { + //Ok... so I had some grand purpose for this, but I can't recall. + //I'm just writing it + + public static int DEVICE_UNKNOWN = -1; + public static int DEVICE_GRAY = 1; + //what's the *official* spelling? + //public static int DEVICE_GREY = 1; + public static int DEVICE_RGB = 2; + public static int DEVICE_CMYK = 3; + + //Are there any others? + + protected int currentColorSpace = -1; + + public ColorSpace (int theColorSpace) + { + this.currentColorSpace = theColorSpace; + + } + public int getColorSpace() + { + return (this.currentColorSpace); + } + public void setColorSpace(int theColorSpace) + { + this.currentColorSpace = theColorSpace; + } + + public String getColorSpacePDFString() + {//this is for PDF Output. Does anyone else need a string representation? + + + //shouldn't this be a select-case? I can never remember + //the syntax for that. + if(this.currentColorSpace == this.DEVICE_RGB) + { + return("DeviceRGB"); + } + else if(this.currentColorSpace == this.DEVICE_CMYK) + { + return("DeviceCMYK"); + } + else if(this.currentColorSpace== this.DEVICE_GRAY) + { + return("DeviceGray"); + } + else + {//unknown... Error. Tell them it's RGB and hope they don't notice. + return("DeviceRGB"); + } + } +} \ No newline at end of file diff --git a/src/org/apache/fop/pdf/PDFColor.java b/src/org/apache/fop/pdf/PDFColor.java index 589959c51..3e47d4fa0 100644 --- a/src/org/apache/fop/pdf/PDFColor.java +++ b/src/org/apache/fop/pdf/PDFColor.java @@ -9,7 +9,7 @@ Redistribution and use in source and binary forms, with or without modifica- tion, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright notice, + 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, @@ -18,32 +18,32 @@ 3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software - developed by the Apache Software Foundation (http://www.apache.org/)." - Alternately, this acknowledgment may appear in the software itself, if + developed by the Apache Software Foundation (http://www.apache.org/)." + Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. 4. The names "Fop" and "Apache Software Foundation" must not be used to - endorse or promote products derived from this software without prior + endorse or promote products derived from this software without prior written permission. For written permission, please contact apache@apache.org. 5. Products derived from this software may not be called "Apache", nor may - "Apache" appear in their name, without prior written permission of the + "Apache" appear in their name, without prior written permission of the Apache Software Foundation. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- - DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - This software consists of voluntary contributions made by many individuals - on behalf of the Apache Software Foundation and was originally created by + This software consists of voluntary contributions made by many individuals + on behalf of the Apache Software Foundation and was originally created by James Tauber . For more information on the Apache Software Foundation, please see . @@ -51,26 +51,29 @@ package org.apache.fop.pdf; // Java +import java.util.Vector; import java.io.IOException; import java.io.PrintWriter; + +//FOP import org.apache.fop.datatypes.ColorType; -import java.util.Vector; +import org.apache.fop.datatypes.ColorSpace; public class PDFColor extends PDFPathPaint { - + protected static double blackFactor = 2.0;//could be 3.0 as well. protected double red = -1.0; protected double green= -1.0; protected double blue = -1.0; - protected double cyan = -1.0; + protected double cyan = -1.0; protected double magenta= -1.0; protected double yellow = -1.0; - protected double black = -1.0; + protected double black = -1.0; - public PDFColor(int theNumber, org.apache.fop.datatypes.ColorType theColor) + public PDFColor(org.apache.fop.datatypes.ColorType theColor) { - this.colorspace = 0; + this.colorSpace = new ColorSpace(ColorSpace.DEVICE_RGB); //super(theNumber) this.red = (double)theColor.red(); this.green = (double)theColor.green(); @@ -78,20 +81,19 @@ public class PDFColor extends PDFPathPaint { } - public PDFColor(int theNumber, double theRed, double theGreen, double theBlue) { + public PDFColor(double theRed, double theGreen, double theBlue) { //super(theNumber); - - this.colorspace=0; + this.colorSpace = new ColorSpace(ColorSpace.DEVICE_RGB); this.red = theRed; this.green = theGreen; this.blue = theBlue; } - public PDFColor(int theNumber, double theCyan, double theMagenta, double theYellow, double theBlack) { + public PDFColor(double theCyan, double theMagenta, double theYellow, double theBlack) { //super(theNumber);//? - this.colorspace = 1; + this.colorSpace = new ColorSpace(ColorSpace.DEVICE_CMYK); this.cyan = theCyan; this.magenta = theMagenta; @@ -99,28 +101,27 @@ public class PDFColor extends PDFPathPaint { this.black = theBlack; } - public int setColorspace() - { - return(this.colorspace); - } public Vector getVector() {//return a vector representation of the color //in the appropriate colorspace. Vector theColorVector= new Vector(); - if (this.colorspace == 0) + if (this.colorSpace.getColorSpace() == ColorSpace.DEVICE_RGB) {//RGB theColorVector.addElement(new Double(this.red)); theColorVector.addElement(new Double(this.green)); theColorVector.addElement(new Double(this.blue)); } - else + else if (this.colorSpace.getColorSpace() == ColorSpace.DEVICE_CMYK) {//CMYK theColorVector.addElement(new Double(this.cyan)); theColorVector.addElement(new Double(this.magenta)); theColorVector.addElement(new Double(this.yellow)); theColorVector.addElement(new Double(this.black)); - + } + else + {//GRAY + theColorVector.addElement(new Double(this.black)); } return(theColorVector); } @@ -154,23 +155,58 @@ public class PDFColor extends PDFPathPaint { return(this.black); } - public String getColorspaceOut(boolean fillNotStroke) + public void setColorSpace(int theColorSpace) + { + int theOldColorSpace = this.colorSpace.getColorSpace(); + if(theOldColorSpace!=theColorSpace) + { + if (theOldColorSpace==ColorSpace.DEVICE_RGB) + { + if(theColorSpace==ColorSpace.DEVICE_CMYK) + { + this.convertRGBtoCMYK(); + } + else //convert to Gray? + { + this.convertRGBtoGRAY(); + } + + } else if(theOldColorSpace==ColorSpace.DEVICE_CMYK) + { + if(theColorSpace == ColorSpace.DEVICE_RGB) + { + this.convertCMYKtoRGB(); + } + else //convert to Gray? + { + this.convertCMYKtoGRAY(); + } + } else //used to be Gray + { + if(theColorSpace == ColorSpace.DEVICE_RGB) + { + this.convertGRAYtoRGB(); + } + else //convert to CMYK? + { + this.convertGRAYtoCMYK(); + } + } + this.colorSpace.setColorSpace(theColorSpace); + } + } + + public String getColorSpaceOut(boolean fillNotStroke) { StringBuffer p = new StringBuffer(""); - + double tempDouble; //if the color was constructed in a different colorspace, //then we need to convert it. - if(this.colorspace==0) + if(this.colorSpace.getColorSpace()==ColorSpace.DEVICE_RGB) {//colorspace is RGB - if((this.red < 0) - || (this.green < 0) - || (this.blue < 0)) - { - this.convertCMYKtoRGB(); - } //end of convert from CMYK //output RGB if(fillNotStroke) @@ -188,15 +224,8 @@ public class PDFColor extends PDFPathPaint { +" RG \n"); } }//end of output RGB - else + else if (this.colorSpace.getColorSpace() == ColorSpace.DEVICE_CMYK) {//colorspace is CMYK - if((this.cyan < 0) - || (this.magenta < 0) - || (this.yellow < 0) - || (this.black < 0)) - { - this.convertRGBtoCMYK(); - }//end of if convert from RGB if(fillNotStroke) { //fill @@ -214,6 +243,19 @@ public class PDFColor extends PDFPathPaint { } }//end of if CMYK + else { //means we're in DeviceGray or Unknown. + //assume we're in DeviceGray, because otherwise we're screwed. + + if(fillNotStroke) + { + p.append(this.doubleOut(this.black) + " g \n"); + } + else + { + p.append(this.doubleOut(this.black) + " G \n"); + } + + } return(p.toString()); } @@ -221,6 +263,7 @@ public class PDFColor extends PDFPathPaint { public String doubleOut(double doubleDown) { + StringBuffer p = new StringBuffer(); double trouble = doubleDown % 1; @@ -257,16 +300,16 @@ public class PDFColor extends PDFPathPaint { this.green = 1.0 - this.green; this.blue= 1.0 - this.yellow; - this.red = (this.black / 2.0)+ this.red; - this.green = (this.black / 2.0)+ this.green; - this.blue = (this.blue / 2.0) + this.blue; + this.red = (this.black / this.blackFactor) + this.red; + this.green = (this.black / this.blackFactor) + this.green; + this.blue = (this.black / this.blackFactor) + this.blue; } protected void convertRGBtoCMYK() { //convert RGB to CMYK - this.cyan = 1.0 - this.red; + this.cyan = 1.0 - this.red; this.magenta= 1.0 - this.green; this.yellow = 1.0 - this.blue; @@ -281,12 +324,58 @@ public class PDFColor extends PDFPathPaint { if (this.blue < tempDouble) tempDouble = this.blue; - this.black = tempDouble / 2.0; // 3.0??? + this.black = tempDouble / this.blackFactor; */ } + protected void convertGRAYtoRGB() + { + this.red = 1.0 - this.black; + this.green= 1.0 - this.black; + this.blue = 1.0 - this.black; + } - + protected void convertGRAYtoCMYK() + { + this.cyan = this.black; + this.magenta= this.black; + this.yellow = this.black; + //this.black=0.0;//? + } + + protected void convertCMYKtoGRAY() + { + double tempDouble=0.0; + + //pick the lowest color + tempDouble = this.cyan; + + if (this.magenta < tempDouble) + tempDouble = this.magenta; + + if (this.yellow < tempDouble) + tempDouble = this.yellow; + + this.black = (tempDouble / this.blackFactor); + + } + + protected void convertRGBtoGRAY() + { + double tempDouble=0.0; + + //pick the lowest color + tempDouble = this.red; + + if (this.green < tempDouble) + tempDouble = this.green; + + if (this.blue < tempDouble) + tempDouble = this.blue; + + this.black = 1.0 - (tempDouble / this.blackFactor); + + } String toPDF() { diff --git a/src/org/apache/fop/pdf/PDFPathPaint.java b/src/org/apache/fop/pdf/PDFPathPaint.java index 894bd5457..702e20b17 100644 --- a/src/org/apache/fop/pdf/PDFPathPaint.java +++ b/src/org/apache/fop/pdf/PDFPathPaint.java @@ -50,10 +50,13 @@ */ package org.apache.fop.pdf; +import org.apache.fop.datatypes.ColorSpace; + public abstract class PDFPathPaint extends PDFObject { - protected int colorspace = 0; //default is 0:RGB, not 1:CMYK + //protected int colorspace = 0; //default is 0:RGB, not 1:CMYK + protected ColorSpace colorSpace; public PDFPathPaint(int theNumber) { super(theNumber); @@ -64,17 +67,17 @@ public abstract class PDFPathPaint extends PDFObject //do nothing } - abstract String getColorspaceOut(boolean fillNotStroke); + abstract String getColorSpaceOut(boolean fillNotStroke); - public void setColorspace(int theColorspace) + public void setColorSpace(int theColorSpace) { - this.colorspace = theColorspace; + this.colorSpace.setColorSpace(theColorSpace); } - public int getColorspace() + public int getColorSpace() { - return(this.colorspace); + return(this.colorSpace.getColorSpace()); } } diff --git a/src/org/apache/fop/pdf/PDFPattern.java b/src/org/apache/fop/pdf/PDFPattern.java index 40085a3f4..097c43d0a 100644 --- a/src/org/apache/fop/pdf/PDFPattern.java +++ b/src/org/apache/fop/pdf/PDFPattern.java @@ -54,6 +54,9 @@ package org.apache.fop.pdf; //Java... import java.util.Vector; +//FOP... +import org.apache.fop.datatypes.ColorSpace; + /** * class representing a PDF Function. * @@ -135,7 +138,6 @@ public class PDFPattern extends PDFPathPaint { protected StringBuffer patternDataStream = null; - /** * Create a tiling pattern (type 1). * @@ -215,7 +217,7 @@ public class PDFPattern extends PDFPathPaint { } - public String getColorspaceOut(boolean fillNotStroke) + public String getColorSpaceOut(boolean fillNotStroke) { if(fillNotStroke) { //fill but no stroke diff --git a/src/org/apache/fop/pdf/PDFShading.java b/src/org/apache/fop/pdf/PDFShading.java index 6ba18f9a2..da682fb73 100644 --- a/src/org/apache/fop/pdf/PDFShading.java +++ b/src/org/apache/fop/pdf/PDFShading.java @@ -54,6 +54,10 @@ package org.apache.fop.pdf; //Java... import java.util.Vector; + +//FOP +import org.apache.fop.datatypes.ColorSpace; + /** * class representing a PDF Smooth Shading object. * @@ -75,10 +79,10 @@ public class PDFShading extends PDFObject { protected int shadingType = 3; //Default /** - * A String representing the colorspace. "DeviceRGB" is an example. + * A ColorSpace representing the colorspace. "DeviceRGB" is an example. */ - protected StringBuffer colorSpace = null; - + //protected StringBuffer colorSpace = null; + protected ColorSpace colorSpace=null; /** * The background color. Since shading is opaque, * this is very rarely used. @@ -180,7 +184,7 @@ public class PDFShading extends PDFObject { * It's optional, the default is the identity matrix * @param theFunction The PDF Function that maps an (x,y) location to a color */ - public PDFShading(int theNumber, String theShadingName, int theShadingType, StringBuffer theColorSpace, + public PDFShading(int theNumber, String theShadingName, int theShadingType, ColorSpace theColorSpace, Vector theBackground, Vector theBBox, boolean theAntiAlias, Vector theDomain, Vector theMatrix, PDFFunction theFunction) { @@ -221,7 +225,7 @@ public class PDFShading extends PDFObject { * The default is [false, false] */ public PDFShading(int theNumber, String theShadingName, - int theShadingType, StringBuffer theColorSpace, + int theShadingType, ColorSpace theColorSpace, Vector theBackground, Vector theBBox, boolean theAntiAlias, Vector theCoords, Vector theDomain, PDFFunction theFunction, Vector theExtend) @@ -265,7 +269,7 @@ public class PDFShading extends PDFObject { * @param theDecode Vector of Doubles see PDF 1.3 spec pages 303 to 312. * @param theFunction the PDFFunction */ - public PDFShading(int theNumber, String theShadingName, int theShadingType, StringBuffer theColorSpace, + public PDFShading(int theNumber, String theShadingName, int theShadingType, ColorSpace theColorSpace, Vector theBackground, Vector theBBox, boolean theAntiAlias, int theBitsPerCoordinate, int theBitsPerComponent, int theBitsPerFlag, Vector theDecode, PDFFunction theFunction) @@ -307,7 +311,7 @@ public class PDFShading extends PDFObject { * @param theFunction The PDFFunction that's mapped on to this shape * @param theNumber the object number of this PDF object. */ - public PDFShading(int theNumber, String theShadingName, int theShadingType, StringBuffer theColorSpace, + public PDFShading(int theNumber, String theShadingName, int theShadingType, ColorSpace theColorSpace, Vector theBackground, Vector theBBox, boolean theAntiAlias, int theBitsPerCoordinate, int theBitsPerComponent, Vector theDecode, int theVerticesPerRow, PDFFunction theFunction) @@ -381,7 +385,8 @@ public class PDFShading extends PDFObject { + " obj\n<< \n/ShadingType "+this.shadingType+" \n"); if(this.colorSpace != null) { - p.append("/ColorSpace /"+this.colorSpace+" \n"); + p.append("/ColorSpace /" + +this.colorSpace.getColorSpacePDFString()+" \n"); } if(this.background != null) -- 2.39.5