Return-Path: Delivered-To: apmail-xmlgraphics-commits-archive@www.apache.org Received: (qmail 60062 invoked from network); 21 Dec 2010 08:54:53 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 21 Dec 2010 08:54:53 -0000 Received: (qmail 98866 invoked by uid 500); 21 Dec 2010 08:54:52 -0000 Mailing-List: contact commits-help@xmlgraphics.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: general@xmlgraphics.apache.org Delivered-To: mailing list commits@xmlgraphics.apache.org Received: (qmail 98859 invoked by uid 99); 21 Dec 2010 08:54:51 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 21 Dec 2010 08:54:51 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 21 Dec 2010 08:54:47 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 64FCB2388903; Tue, 21 Dec 2010 08:54:27 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: svn commit: r1051421 - in /xmlgraphics/commons/trunk: ./ examples/java/java2d/ps/ src/java/org/apache/xmlgraphics/image/loader/impl/ src/java/org/apache/xmlgraphics/java2d/color/ src/java/org/apache/xmlgraphics/java2d/color/profile/ src/java/org/apache... Date: Tue, 21 Dec 2010 08:54:27 -0000 To: commits@xmlgraphics.apache.org From: jeremias@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20101221085427.64FCB2388903@eris.apache.org> Author: jeremias Date: Tue Dec 21 08:54:25 2010 New Revision: 1051421 URL: http://svn.apache.org/viewvc?rev=1051421&view=rev Log: Merge color branch (https://svn.apache.org/repos/asf/xmlgraphics/commons/branches/Temp_Color) with Trunk. Added: xmlgraphics/commons/trunk/examples/java/java2d/ps/EPSColorsExample.java - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/examples/java/java2d/ps/EPSColorsExample.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/AbstractDeviceSpecificColorSpace.java - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/AbstractDeviceSpecificColorSpace.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/CIELabColorSpace.java - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/CIELabColorSpace.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/ColorSpaceOrigin.java - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/ColorSpaceOrigin.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/ColorSpaces.java - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/ColorSpaces.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/ColorWithAlternatives.java - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/ColorWithAlternatives.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/GrayScaleColorConverter.java - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/GrayScaleColorConverter.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/ICCColorSpaceWithIntent.java - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/ICCColorSpaceWithIntent.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/NamedColorSpace.java - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/NamedColorSpace.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/RenderingIntent.java - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/RenderingIntent.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/package.html - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/package.html xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/profile/ - copied from r1051413, xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/profile/ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/profile/NamedColorProfile.java - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/profile/NamedColorProfile.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/profile/NamedColorProfileParser.java - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/profile/NamedColorProfileParser.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/profile/package.html - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/profile/package.html xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/java2d/ - copied from r1051413, xmlgraphics/commons/branches/Temp_Color/test/java/org/apache/xmlgraphics/java2d/ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/java2d/color/ - copied from r1051413, xmlgraphics/commons/branches/Temp_Color/test/java/org/apache/xmlgraphics/java2d/color/ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/java2d/color/ColorConverterTest.java - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/test/java/org/apache/xmlgraphics/java2d/color/ColorConverterTest.java xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/java2d/color/ColorWithAlternativesTest.java - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/test/java/org/apache/xmlgraphics/java2d/color/ColorWithAlternativesTest.java xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/java2d/color/NamedColorTest.java - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/test/java/org/apache/xmlgraphics/java2d/color/NamedColorTest.java xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/java2d/color/profile/ - copied from r1051413, xmlgraphics/commons/branches/Temp_Color/test/java/org/apache/xmlgraphics/java2d/color/profile/ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/java2d/color/profile/NamedColorProfileParserTest.java - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/test/java/org/apache/xmlgraphics/java2d/color/profile/NamedColorProfileParserTest.java xmlgraphics/commons/trunk/test/resources/org/apache/xmlgraphics/java2d/ - copied from r1051413, xmlgraphics/commons/branches/Temp_Color/test/resources/org/apache/xmlgraphics/java2d/ xmlgraphics/commons/trunk/test/resources/org/apache/xmlgraphics/java2d/color/ - copied from r1051413, xmlgraphics/commons/branches/Temp_Color/test/resources/org/apache/xmlgraphics/java2d/color/ xmlgraphics/commons/trunk/test/resources/org/apache/xmlgraphics/java2d/color/profile/ - copied from r1051413, xmlgraphics/commons/branches/Temp_Color/test/resources/org/apache/xmlgraphics/java2d/color/profile/ xmlgraphics/commons/trunk/test/resources/org/apache/xmlgraphics/java2d/color/profile/ncp-example.icc - copied unchanged from r1051413, xmlgraphics/commons/branches/Temp_Color/test/resources/org/apache/xmlgraphics/java2d/color/profile/ncp-example.icc Modified: xmlgraphics/commons/trunk/ (props changed) xmlgraphics/commons/trunk/build.xml xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawJPEG.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/ColorConverter.java (contents, props changed) xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/ColorUtil.java (contents, props changed) xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/DefaultColorConverter.java (contents, props changed) xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/DeviceCMYKColorSpace.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ps/PSGraphicsConfiguration.java (props changed) xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ps/PSGraphicsDevice.java (props changed) xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ps/PSTilingPattern.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSGenerator.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSState.java xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/XMPPropertyTest.java (contents, props changed) Propchange: xmlgraphics/commons/trunk/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Tue Dec 21 08:54:25 2010 @@ -1 +1,2 @@ +/xmlgraphics/commons/branches/Temp_Color:956483-1051413 /xmlgraphics/commons/branches/commons-1_4:950384-962801 Modified: xmlgraphics/commons/trunk/build.xml URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/build.xml?rev=1051421&r1=1051420&r2=1051421&view=diff ============================================================================== --- xmlgraphics/commons/trunk/build.xml (original) +++ xmlgraphics/commons/trunk/build.xml Tue Dec 21 08:54:25 2010 @@ -380,6 +380,7 @@ NOTE: source="${javac.source}">
+ Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawJPEG.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawJPEG.java?rev=1051421&r1=1051420&r2=1051421&view=diff ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawJPEG.java (original) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawJPEG.java Tue Dec 21 08:54:25 2010 @@ -39,7 +39,7 @@ import org.apache.xmlgraphics.image.load import org.apache.xmlgraphics.image.loader.ImageInfo; import org.apache.xmlgraphics.image.loader.ImageSessionContext; import org.apache.xmlgraphics.image.loader.util.ImageUtil; -import org.apache.xmlgraphics.java2d.color.DeviceCMYKColorSpace; +import org.apache.xmlgraphics.java2d.color.ColorSpaces; import org.apache.xmlgraphics.util.MimeConstants; /** @@ -119,7 +119,7 @@ public class ImageLoaderRawJPEG extends colorSpace = ColorSpace.getInstance( ColorSpace.CS_LINEAR_RGB); } else if (numComponents == 4) { - colorSpace = DeviceCMYKColorSpace.getInstance(); + colorSpace = ColorSpaces.getDeviceCMYKColorSpace(); } else { throw new ImageException("Unsupported ColorSpace for image " + info Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/ColorConverter.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/ColorConverter.java?rev=1051421&r1=1051420&r2=1051421&view=diff ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/ColorConverter.java (original) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/ColorConverter.java Tue Dec 21 08:54:25 2010 @@ -1,36 +1,36 @@ -/* - * 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.xmlgraphics.java2d.color; - -import java.awt.Color; - -/** - * Utility for implementing a color conversion scheme. - */ -public interface ColorConverter { - - /** - * @param color to convert - * @return converted color - */ - - Color convert(Color color); - -} +/* + * 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.xmlgraphics.java2d.color; + +import java.awt.Color; + +/** + * Utility for implementing a color conversion scheme. + */ +public interface ColorConverter { + + /** + * @param color to convert + * @return converted color + */ + + Color convert(Color color); + +} Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/ColorConverter.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/ColorConverter.java ------------------------------------------------------------------------------ svn:keywords = Id Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/ColorUtil.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/ColorUtil.java?rev=1051421&r1=1051420&r2=1051421&view=diff ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/ColorUtil.java (original) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/ColorUtil.java Tue Dec 21 08:54:25 2010 @@ -1,74 +1,165 @@ -/* - * 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: ColorUtil.java 815938 2009-09-16 19:38:13Z jeremias $ */ - -package org.apache.xmlgraphics.java2d.color; - -import java.awt.Color; - - -/** - * Generic Color helper class. - *

- * This class supports parsing string values into color values and creating - * color values for strings. It provides a list of standard color names. - */ -public final class ColorUtil { - - /** - * Private constructor since this is an utility class. - */ - private ColorUtil() { - } - - - /** - * Lightens up a color for groove, ridge, inset and outset border effects. - * @param col the color to lighten up - * @param factor factor by which to lighten up (negative values darken the color) - * @return the modified color - */ - public static Color lightenColor(Color col, float factor) { - // TODO: This function converts the color into the sRGB namespace. - // This should be avoided if possible. - float[] cols = new float[4]; - cols = col.getRGBComponents(cols); - if (factor > 0) { - cols[0] += (1.0 - cols[0]) * factor; - cols[1] += (1.0 - cols[1]) * factor; - cols[2] += (1.0 - cols[2]) * factor; - } else { - cols[0] -= cols[0] * -factor; - cols[1] -= cols[1] * -factor; - cols[2] -= cols[2] * -factor; - } - return new Color(cols[0], cols[1], cols[2], cols[3]); - } - - - - /** - * Indicates whether the color is a gray value. - * @param col the color - * @return true if it is a gray value - */ - public static boolean isGray(Color col) { - return (col.getRed() == col.getBlue() && col.getRed() == col.getGreen()); - } - -} +/* + * 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.xmlgraphics.java2d.color; + +import java.awt.Color; + + +/** + * Generic Color helper class. + *

+ * This class supports parsing string values into color values and creating + * color values for strings. It provides a list of standard color names. + */ +public final class ColorUtil { + + /** + * Private constructor since this is an utility class. + */ + private ColorUtil() { + } + + + /** + * Lightens up a color for groove, ridge, inset and outset border effects. + * @param col the color to lighten up + * @param factor factor by which to lighten up (negative values darken the color) + * @return the modified color + */ + public static Color lightenColor(Color col, float factor) { + // TODO: This function converts the color into the sRGB namespace. + // This should be avoided if possible. + float[] cols = new float[4]; + cols = col.getRGBComponents(cols); + if (factor > 0) { + cols[0] += (1.0 - cols[0]) * factor; + cols[1] += (1.0 - cols[1]) * factor; + cols[2] += (1.0 - cols[2]) * factor; + } else { + cols[0] -= cols[0] * -factor; + cols[1] -= cols[1] * -factor; + cols[2] -= cols[2] * -factor; + } + return new Color(cols[0], cols[1], cols[2], cols[3]); + } + + + + /** + * Indicates whether the color is a gray value. + * @param col the color + * @return true if it is a gray value + */ + public static boolean isGray(Color col) { + return (col.getRed() == col.getBlue() && col.getRed() == col.getGreen()); + } + + /** + * Creates an uncalibrated CMYK color with the given gray value. + * @param black the gray component (0 - 1) + * @return the CMYK color + */ + public static Color toCMYKGrayColor(float black) { + //Calculated color components + float[] cmyk = new float[] {0f, 0f, 0f, 1.0f - black}; + //Create native color + return DeviceCMYKColorSpace.createCMYKColor(cmyk); + } + + /** + * Converts an arbitrary {@link Color} to a plain sRGB color doing the conversion at the + * best possible conversion quality. + * @param col the original color + * @return the sRGB equivalent + */ + public static Color toSRGBColor(Color col) { + if (col.getColorSpace().isCS_sRGB()) { + return col; //Don't convert if already sRGB to avoid conversion differences + } + float[] comps = col.getColorComponents(null); + float[] srgb = col.getColorSpace().toRGB(comps); + comps = col.getComponents(null); + float alpha = comps[comps.length - 1]; + return new Color(srgb[0], srgb[1], srgb[2], alpha); + } + + /** + * Checks if two colors are the same color. This check is much more restrictive than + * {@link Color#equals(Object)} in that it doesn't only check if both colors result in the + * same sRGB value. For example, if two colors not of the same exact class are compared, + * they are treated as not the same. + *

+ * Note: At the moment, this method only supports {@link Color} and + * {@link ColorWithAlternatives} only. Other subclasses of {@link Color} are checked only using + * the {@link Color#equals(Object)} method. + * @param col1 the first color + * @param col2 the second color + * @return true if both colors are the same color + */ + public static boolean isSameColor(Color col1, Color col2) { + //Check fallback sRGB values first, then go into details + if (!col1.equals(col2)) { + return false; + } + + //Consider same-ness only between colors of the same class (not subclasses) + //but consider a ColorWithAlternatives without alternatives to be the same as a Color. + Class cl1 = col1.getClass(); + if (col1 instanceof ColorWithAlternatives + && !((ColorWithAlternatives) col1).hasAlternativeColors()) { + cl1 = Color.class; + } + Class cl2 = col2.getClass(); + if (col2 instanceof ColorWithAlternatives + && !((ColorWithAlternatives) col2).hasAlternativeColors()) { + cl2 = Color.class; + } + if (cl1 != cl2) { + return false; + } + + //Check color space + if (!col1.getColorSpace().equals(col2.getColorSpace())) { + return false; + } + + //Check native components + float[] comps1 = col1.getComponents(null); + float[] comps2 = col2.getComponents(null); + if (comps1.length != comps2.length) { + return false; + } + for (int i = 0, c = comps1.length; i < c; i++) { + if (comps1[i] != comps2[i]) { + return false; + } + } + + //Compare alternative colors, order is relevant + if (col1 instanceof ColorWithAlternatives && col2 instanceof ColorWithAlternatives) { + ColorWithAlternatives ca1 = (ColorWithAlternatives) col1; + ColorWithAlternatives ca2 = (ColorWithAlternatives) col2; + return ca1.hasSameAlternativeColors(ca2); + } + + return true; + } + +} Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/ColorUtil.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/ColorUtil.java ------------------------------------------------------------------------------ svn:keywords = Id Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/DefaultColorConverter.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/DefaultColorConverter.java?rev=1051421&r1=1051420&r2=1051421&view=diff ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/DefaultColorConverter.java (original) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/DefaultColorConverter.java Tue Dec 21 08:54:25 2010 @@ -1,52 +1,52 @@ -/* - * 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.xmlgraphics.java2d.color; - -import java.awt.Color; - -import org.apache.xmlgraphics.java2d.color.ColorConverter; - -/** - * A default implementation that does not apply any conversion - */ -public final class DefaultColorConverter implements ColorConverter { - - /** - * private constructor to support singleton pattern - */ - private static final DefaultColorConverter SINGLETON = new DefaultColorConverter(); - - private DefaultColorConverter() { - } - - /** - * static factory - * - * @return singleton instance of DefaultColorConverter - */ - public static DefaultColorConverter getInstance() { - return SINGLETON; - } - - /** {@inheritDoc} */ - public Color convert(Color color) { - return color; - } -} +/* + * 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.xmlgraphics.java2d.color; + +import java.awt.Color; + +import org.apache.xmlgraphics.java2d.color.ColorConverter; + +/** + * A default implementation that does not apply any conversion + */ +public final class DefaultColorConverter implements ColorConverter { + + /** + * private constructor to support singleton pattern + */ + private static final DefaultColorConverter SINGLETON = new DefaultColorConverter(); + + private DefaultColorConverter() { + } + + /** + * static factory + * + * @return singleton instance of DefaultColorConverter + */ + public static DefaultColorConverter getInstance() { + return SINGLETON; + } + + /** {@inheritDoc} */ + public Color convert(Color color) { + return color; + } +} Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/DefaultColorConverter.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/DefaultColorConverter.java ------------------------------------------------------------------------------ svn:keywords = Id Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/DeviceCMYKColorSpace.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/DeviceCMYKColorSpace.java?rev=1051421&r1=1051420&r2=1051421&view=diff ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/DeviceCMYKColorSpace.java (original) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/color/DeviceCMYKColorSpace.java Tue Dec 21 08:54:25 2010 @@ -19,38 +19,31 @@ package org.apache.xmlgraphics.java2d.color; +import java.awt.Color; import java.awt.color.ColorSpace; /** * This class represents an uncalibrated CMYK color space. */ -public class DeviceCMYKColorSpace extends ColorSpace { +public class DeviceCMYKColorSpace extends AbstractDeviceSpecificColorSpace + implements ColorSpaceOrigin { private static final long serialVersionUID = 2925508946083542974L; - private static DeviceCMYKColorSpace instance; + /** The name for the uncalibrated CMYK pseudo-profile */ + public static final String PSEUDO_PROFILE_NAME = "#CMYK"; /** * Constructs an uncalibrated CMYK ColorSpace object with {@link ColorSpace#TYPE_CMYK} and * 4 components. * @see java.awt.color.ColorSpace#ColorSpace(int, int) */ - protected DeviceCMYKColorSpace() { + public DeviceCMYKColorSpace() { super(TYPE_CMYK, 4); } - /** - * Returns an instance of an uncalibrated CMYK color space. - * @return CMYKColorSpace the requested color space object - */ - public static DeviceCMYKColorSpace getInstance() { - if (instance == null) { - instance = new DeviceCMYKColorSpace(); - } - return instance; - } - /** {@inheritDoc} */ + @Override public float[] toRGB(float[] colorvalue) { return new float [] { (1 - colorvalue[0]) * (1 - colorvalue[3]), @@ -59,18 +52,57 @@ public class DeviceCMYKColorSpace extend } /** {@inheritDoc} */ + @Override public float[] fromRGB(float[] rgbvalue) { - throw new UnsupportedOperationException("NYI"); + assert rgbvalue.length == 3; + //Note: this is an arbitrary conversion, not a color-managed one! + float r = rgbvalue[0]; + float g = rgbvalue[1]; + float b = rgbvalue[2]; + if (r == g && r == b) { + return new float[] {0, 0, 0, 1 - r}; + } else { + float c = 1 - r; + float m = 1 - g; + float y = 1 - b; + float k = Math.min(c, Math.min(m, y)); + return new float[] {c, m, y, k}; + } } /** {@inheritDoc} */ + @Override public float[] toCIEXYZ(float[] colorvalue) { throw new UnsupportedOperationException("NYI"); } /** {@inheritDoc} */ + @Override public float[] fromCIEXYZ(float[] colorvalue) { throw new UnsupportedOperationException("NYI"); } + /** + * Creates a color instance representing a device-specific CMYK color. An sRGB value + * is calculated from the CMYK colors but it may not correctly represent the given CMYK + * values. + * @param cmykComponents the CMYK components + * @return the device-specific color + */ + public static Color createCMYKColor(float[] cmykComponents) { + DeviceCMYKColorSpace cmykCs = ColorSpaces.getDeviceCMYKColorSpace(); + Color cmykColor = new ColorWithAlternatives(cmykCs, cmykComponents, 1.0f, null); + return cmykColor; + } + + /** {@inheritDoc} */ + public String getProfileName() { + return PSEUDO_PROFILE_NAME; + } + + /** {@inheritDoc} */ + public String getProfileURI() { + return null; //No URI + } + } Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ps/PSGraphicsConfiguration.java ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Tue Dec 21 08:54:25 2010 @@ -1,3 +1,4 @@ +/xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/ps/PSGraphicsConfiguration.java:956483-1051413 /xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/svg/PDFGraphicsConfiguration.java:603620-746655 /xmlgraphics/fop/branches/fop-0_95/src/java/org/apache/fop/svg/PDFGraphicsConfiguration.java:684572,688085,688696 /xmlgraphics/fop/branches/fop-1_0/src/java/org/apache/fop/svg/PDFGraphicsConfiguration.java:959975-964707 Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ps/PSGraphicsDevice.java ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Tue Dec 21 08:54:25 2010 @@ -1,3 +1,4 @@ +/xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/ps/PSGraphicsDevice.java:956483-1051413 /xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/svg/PDFGraphicsDevice.java:603620-746655 /xmlgraphics/fop/branches/fop-0_95/src/java/org/apache/fop/svg/PDFGraphicsDevice.java:684572,688085,688696 /xmlgraphics/fop/branches/fop-1_0/src/java/org/apache/fop/svg/PDFGraphicsDevice.java:959975-964707 Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ps/PSTilingPattern.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ps/PSTilingPattern.java?rev=1051421&r1=1051420&r2=1051421&view=diff ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ps/PSTilingPattern.java (original) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ps/PSTilingPattern.java Tue Dec 21 08:54:25 2010 @@ -51,7 +51,7 @@ public class PSTilingPattern { protected String patternName = null; /** - * The XUID is an extended unique ID — an array of integers that provides for + * The XUID is an extended unique ID -- an array of integers that provides for * distributed, hierarchical management of the space of unique ID numbers * (optional) */ @@ -65,7 +65,7 @@ public class PSTilingPattern { /** * An array of four numbers in the pattern coordinate system, giving * the coordinates of the left, bottom, right, and top edges, respectively, of the - * pattern cell’s bounding box + * pattern cell's bounding box */ protected Rectangle2D bBox = null; @@ -108,7 +108,7 @@ public class PSTilingPattern { * the name is null, the pattern should be stored in PSPatternStorage, where the pattern * gets a name (the pattern without name cannot be use in PS file) * @param paintProc a postscript procedure for painting the pattern cell - * @param bBox a pattern cell’s bounding box + * @param bBox a pattern cell's bounding box * @param xStep the desired horizontal spacing between pattern cells * @param yStep the desired vertical spacing between pattern cells * @param paintType 1 for colored pattern, 2 for uncolored pattern @@ -192,7 +192,7 @@ public class PSTilingPattern { /** * Returns the bounding box. * - * @return a pattern cell’s bounding box + * @return a pattern cell's bounding box */ public Rectangle2D getBoundingBox() { return (this.bBox); @@ -201,7 +201,7 @@ public class PSTilingPattern { /** * Sets the bounding box. * - * @param bBox a pattern cell’s bounding box + * @param bBox a pattern cell's bounding box */ public void setBoundingBox(Rectangle2D bBox) { if (bBox == null) { @@ -392,9 +392,9 @@ public class PSTilingPattern { } // define color image: width height bits/comp matrix - // datasrc0 … datasrcncomp-1 multi ncomp colorimage + // datasrc0 � datasrcncomp-1 multi ncomp colorimage sb.append(width + " " + height + " 8 " + "matrix\n"); // width height bits/comp matrix - int [] argb = new int[width * height]; // datasrc0 … datasrcncomp-1 + int [] argb = new int[width * height]; // datasrc0 � datasrcncomp-1 sb.append("{<"); texture.getImage().getRGB(0, 0, width, height, argb, 0, width); int count = 0; Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSGenerator.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSGenerator.java?rev=1051421&r1=1051420&r2=1051421&view=diff ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSGenerator.java (original) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSGenerator.java Tue Dec 21 08:54:25 2010 @@ -34,6 +34,11 @@ import java.util.Stack; import javax.xml.transform.Source; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.apache.xmlgraphics.java2d.color.ColorUtil; +import org.apache.xmlgraphics.java2d.color.ColorWithAlternatives; import org.apache.xmlgraphics.ps.dsc.ResourceTracker; /** @@ -54,18 +59,20 @@ public class PSGenerator implements PSCo * later in the document (mostly in the %%Trailer section). * @deprecated Please use DSCConstants.ATEND. This constant was in the wrong place. */ + @Deprecated public static final Object ATEND = DSCConstants.ATEND; /** Line feed used by PostScript */ public static final char LF = '\n'; + private Log log = LogFactory.getLog(getClass()); private OutputStream out; private int psLevel = DEFAULT_LANGUAGE_LEVEL; private boolean commentsEnabled = true; private boolean compactMode = true; private PSCommandMap commandMap = PSProcSets.STD_COMMAND_MAP; - private Stack graphicsStateStack = new Stack(); + private Stack graphicsStateStack = new Stack(); private PSState currentState; //private DecimalFormat df3 = new DecimalFormat("0.000", new DecimalFormatSymbols(Locale.US)); private DecimalFormat df3 = new DecimalFormat("0.###", new DecimalFormatSymbols(Locale.US)); @@ -449,7 +456,7 @@ public class PSGenerator implements PSCo public boolean restoreGraphicsState() throws IOException { if (this.graphicsStateStack.size() > 0) { writeln(mapCommand("grestore")); - this.currentState = (PSState)this.graphicsStateStack.pop(); + this.currentState = this.graphicsStateStack.pop(); return true; } else { return false; @@ -622,6 +629,7 @@ public class PSGenerator implements PSCo * @exception IOException In case of an I/O problem * @deprecated use useColor method instead */ + @Deprecated public void useRGBColor(Color col) throws IOException { useColor(col); } @@ -637,47 +645,81 @@ public class PSGenerator implements PSCo } } - private String convertColorToPS(Color col) { - StringBuffer p = new StringBuffer(); - float[] comps = col.getColorComponents(null); - - if (col.getColorSpace().getType() == ColorSpace.TYPE_RGB) { - // according to pdfspec 12.1 p.399 - // if the colors are the same then just use the g or G operator - boolean same = (comps[0] == comps[1] - && comps[0] == comps[2]); - // output RGB - if (same) { - p.append(formatDouble(comps[0])); - } else { - for (int i = 0; i < col.getColorSpace().getNumComponents(); i++) { - if (i > 0) { - p.append(" "); - } - p.append(formatDouble(comps[i])); + private String convertColorToPS(Color color) { + StringBuffer codeBuffer = new StringBuffer(); + + //Important: Right now, CMYK colors are treated as device colors (DeviceCMYK) irrespective + //of any associated color profile. All other colors are converted to sRGB (if necessary) + //and the resulting RGB components are treated as DeviceRGB colors. + //If all three RGB components are the same, DeviceGray is used. + + boolean established = false; + if (color instanceof ColorWithAlternatives) { + ColorWithAlternatives colExt = (ColorWithAlternatives)color; + //Alternative colors have priority + Color[] alt = colExt.getAlternativeColors(); + for (int i = 0, c = alt.length; i < c; i++) { + Color col = alt[i]; + established = establishColorFromColor(codeBuffer, col); + if (established) { + break; } } - if (same) { - p.append(" ").append(mapCommand("setgray")); - } else { - p.append(" ").append(mapCommand("setrgbcolor")); + if (log.isDebugEnabled() && alt.length > 0) { + log.debug("None of the alternative colors are supported. Using fallback: " + + color); } - } else if (col.getColorSpace().getType() == ColorSpace.TYPE_CMYK) { + } + + //Fallback + if (!established) { + established = establishColorFromColor(codeBuffer, color); + } + if (!established) { + establishFallbackRGB(codeBuffer, color); + } + + return codeBuffer.toString(); + } + + private boolean establishColorFromColor(StringBuffer codeBuffer, Color color) { + //Important: see above note about color handling! + float[] comps = color.getColorComponents(null); + if (color.getColorSpace().getType() == ColorSpace.TYPE_CMYK) { // colorspace is CMYK - for (int i = 0; i < col.getColorSpace().getNumComponents(); i++) { - if (i > 0) { - p.append(" "); - } - p.append(formatDouble(comps[i])); + writeSetColor(codeBuffer, comps, "setcmykcolor"); + return true; + } + return false; + } + + private void writeSetColor(StringBuffer codeBuffer, float[] comps, String command) { + for (int i = 0, c = comps.length; i < c; i++) { + if (i > 0) { + codeBuffer.append(" "); } - p.append(" ").append(mapCommand("setcmykcolor")); + codeBuffer.append(formatDouble(comps[i])); + } + codeBuffer.append(" ").append(mapCommand(command)); + } + + private void establishFallbackRGB(StringBuffer codeBuffer, Color color) { + float[] comps; + if (color.getColorSpace().isCS_sRGB()) { + comps = color.getColorComponents(null); } else { - // means we're in DeviceGray or Unknown. - // assume we're in DeviceGray, because otherwise we're screwed. - p.append(formatDouble(comps[0])); - p.append(" ").append(mapCommand("setgray")); + if (log.isDebugEnabled()) { + log.debug("Converting color to sRGB as a fallback: " + color); + } + ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB); + comps = color.getColorComponents(sRGB, null); + } + assert comps.length == 3; + boolean gray = ColorUtil.isGray(color); + if (gray) { + comps = new float[] {comps[0]}; } - return p.toString(); + writeSetColor(codeBuffer, comps, gray ? "setgray" : "setrgbcolor"); } /** @@ -715,6 +757,7 @@ public class PSGenerator implements PSCo * set can be cleared. * @deprecated Use the notifyStartNewPage() on ResourceTracker instead. */ + @Deprecated public void notifyStartNewPage() { getResourceTracker().notifyStartNewPage(); } @@ -725,6 +768,7 @@ public class PSGenerator implements PSCo * @param needed true if this is a needed resource, false for a supplied resource * @deprecated Use the notifyResourceUsageOnPage() on ResourceTracker instead */ + @Deprecated public void notifyResourceUsage(PSResource res, boolean needed) { getResourceTracker().notifyResourceUsageOnPage(res); } @@ -737,6 +781,7 @@ public class PSGenerator implements PSCo * @exception IOException In case of an I/O problem * @deprecated Use the writeResources() on ResourceTracker instead. */ + @Deprecated public void writeResources(boolean pageLevel) throws IOException { getResourceTracker().writeResources(pageLevel, this); } @@ -747,6 +792,7 @@ public class PSGenerator implements PSCo * @return true if the resource is registered as being supplied. * @deprecated Use the isResourceSupplied() on ResourceTracker instead. */ + @Deprecated public boolean isResourceSupplied(PSResource res) { return getResourceTracker().isResourceSupplied(res); } Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSState.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSState.java?rev=1051421&r1=1051420&r2=1051421&view=diff ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSState.java (original) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSState.java Tue Dec 21 08:54:25 2010 @@ -25,6 +25,8 @@ import java.io.IOException; import java.io.Serializable; import java.util.List; +import org.apache.xmlgraphics.java2d.color.ColorUtil; + /** * This class holds the current state of the PostScript interpreter. * @@ -184,7 +186,7 @@ public class PSState implements Serializ * @return true if the color changed compared to the previous setting */ public boolean useColor(Color value) { - if (!color.equals(value)) { + if (!ColorUtil.isSameColor(color, value)) { color = value; return true; } else { Modified: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/XMPPropertyTest.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/XMPPropertyTest.java?rev=1051421&r1=1051420&r2=1051421&view=diff ============================================================================== --- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/XMPPropertyTest.java (original) +++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/XMPPropertyTest.java Tue Dec 21 08:54:25 2010 @@ -1,186 +1,186 @@ -/* - * 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.xmlgraphics.xmp; - -import java.io.StringWriter; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Date; -import java.util.Locale; -import java.util.Set; -import java.util.TimeZone; - -import javax.xml.transform.stream.StreamResult; -import javax.xml.transform.stream.StreamSource; - -import junit.framework.TestCase; - -import org.apache.xmlgraphics.util.QName; -import org.apache.xmlgraphics.xmp.schemas.DublinCoreAdapter; -import org.apache.xmlgraphics.xmp.schemas.DublinCoreSchema; -import org.apache.xmlgraphics.xmp.schemas.XMPBasicAdapter; -import org.apache.xmlgraphics.xmp.schemas.XMPBasicSchema; - -/** - * Tests property access methods. - */ -public class XMPPropertyTest extends TestCase { - - public void testPropertyAccess() throws Exception { - Metadata xmp = new Metadata(); - DublinCoreAdapter dc = DublinCoreSchema.getAdapter(xmp); - assertNull(dc.getContributors()); - - dc.addContributor("Contributor1"); - assertEquals(1, dc.getContributors().length); - assertEquals("Contributor1", dc.getContributors()[0]); - dc.removeContributor("Contributor1"); - assertNull(dc.getContributors()); - - dc.addContributor("Contributor1"); - assertEquals(1, dc.getContributors().length); - dc.addContributor("Contributor2"); - assertEquals(2, dc.getContributors().length); - assertFalse(dc.removeContributor("DoesNotExist")); - assertTrue(dc.removeContributor("Contributor1")); - assertEquals(1, dc.getContributors().length); - assertTrue(dc.removeContributor("Contributor2")); - assertFalse(dc.removeContributor("Contributor2")); - assertNull(dc.getContributors()); - } - - public void testPropertyRemovalLangAlt() throws Exception { - Metadata xmp = new Metadata(); - DublinCoreAdapter dc = DublinCoreSchema.getAdapter(xmp); - - //dc:title is a "Lang Alt" - dc.setTitle("en", "The title"); - String title = dc.removeTitle("en"); - assertEquals("The title", title); - dc.setTitle("en", "The title"); - dc.setTitle("de", "Der Titel"); - title = dc.removeTitle("en"); - assertEquals("The title", title); - title = dc.removeTitle("en"); - assertNull(title); - - title = dc.removeTitle("de"); - assertEquals("Der Titel", title); - title = dc.removeTitle("de"); - assertNull(title); - } - - public void testReplaceLangAlt() throws Exception { - Metadata xmp = new Metadata(); - DublinCoreAdapter dc = DublinCoreSchema.getAdapter(xmp); - dc.setTitle("Default title"); - StringWriter writer = new StringWriter(); - XMPSerializer.writeXML(xmp, new StreamResult(writer)); - String xmpString = writer.toString(); - xmp = XMPParser.parseXMP(new StreamSource(new java.io.StringReader(xmpString))); - dc = DublinCoreSchema.getAdapter(xmp); - assertEquals("Default title", dc.getTitle()); - dc.setTitle("Updated title"); - XMPProperty prop = xmp.getProperty(new QName(DublinCoreSchema.NAMESPACE, "title")); - XMPArray array = prop.getArrayValue(); - assertNotNull(array); - //Check that only one title is present. There used to be a bug that didn't set the - //non-qualified value equal to the value qualified with "x-default". - assertEquals(1, array.getSize()); - assertEquals("Updated title", array.getValue(0)); - } - - public void testPropertyValues() throws Exception { - Metadata xmp = new Metadata(); - DublinCoreAdapter dc = DublinCoreSchema.getAdapter(xmp); - - String format = dc.getFormat(); - assertNull(format); - - dc.setFormat("application/pdf"); - format = dc.getFormat(); - assertEquals("application/pdf", format); - - dc.setFormat("image/jpeg"); - format = dc.getFormat(); - assertEquals("image/jpeg", format); - - dc.setFormat(null); - format = dc.getFormat(); - assertNull(format); - - dc.setFormat(""); //Empty string same as null value - format = dc.getFormat(); - assertNull(format); - - dc.setTitle("title"); - String title = dc.getTitle(); - assertEquals("title", title); - - dc.setTitle("Titel"); - title = dc.getTitle(); - assertEquals("Titel", title); - - dc.setTitle(null); - title = dc.getTitle(); - assertNull(title); - - dc.setTitle(""); - title = dc.getTitle(); - assertNull(title); - } - - public void testDates() throws Exception { - Metadata xmp = new Metadata(); - XMPBasicAdapter basic = XMPBasicSchema.getAdapter(xmp); - - Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ENGLISH); - cal.set(2008, Calendar.FEBRUARY, 07, 15, 11, 07); - cal.set(Calendar.MILLISECOND, 0); - Date dt = cal.getTime(); - - assertNull(basic.getCreateDate()); - basic.setCreateDate(dt); - Date dt2 = basic.getCreateDate(); - assertEquals(dt2, dt); - } - - public void testQualifiers() throws Exception { - Metadata xmp = new Metadata(); - XMPBasicAdapter basic = XMPBasicSchema.getAdapter(xmp); - - basic.addIdentifier("x123"); - basic.setIdentifier("id1", "system1"); - basic.setIdentifier("12345", "system2"); - - String[] ids = basic.getIdentifiers(); - assertEquals(3, ids.length); - Set set = new java.util.HashSet(Arrays.asList(ids)); - assertTrue(set.contains("x123")); - assertTrue(set.contains("id1")); - assertTrue(set.contains("12345")); - - assertEquals("id1", basic.getIdentifier("system1")); - basic.setIdentifier("id2", "system1"); - assertEquals("id2", basic.getIdentifier("system1")); - assertEquals(3, basic.getIdentifiers().length); - } - -} +/* + * 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.xmlgraphics.xmp; + +import java.io.StringWriter; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; +import java.util.Set; +import java.util.TimeZone; + +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; + +import junit.framework.TestCase; + +import org.apache.xmlgraphics.util.QName; +import org.apache.xmlgraphics.xmp.schemas.DublinCoreAdapter; +import org.apache.xmlgraphics.xmp.schemas.DublinCoreSchema; +import org.apache.xmlgraphics.xmp.schemas.XMPBasicAdapter; +import org.apache.xmlgraphics.xmp.schemas.XMPBasicSchema; + +/** + * Tests property access methods. + */ +public class XMPPropertyTest extends TestCase { + + public void testPropertyAccess() throws Exception { + Metadata xmp = new Metadata(); + DublinCoreAdapter dc = DublinCoreSchema.getAdapter(xmp); + assertNull(dc.getContributors()); + + dc.addContributor("Contributor1"); + assertEquals(1, dc.getContributors().length); + assertEquals("Contributor1", dc.getContributors()[0]); + dc.removeContributor("Contributor1"); + assertNull(dc.getContributors()); + + dc.addContributor("Contributor1"); + assertEquals(1, dc.getContributors().length); + dc.addContributor("Contributor2"); + assertEquals(2, dc.getContributors().length); + assertFalse(dc.removeContributor("DoesNotExist")); + assertTrue(dc.removeContributor("Contributor1")); + assertEquals(1, dc.getContributors().length); + assertTrue(dc.removeContributor("Contributor2")); + assertFalse(dc.removeContributor("Contributor2")); + assertNull(dc.getContributors()); + } + + public void testPropertyRemovalLangAlt() throws Exception { + Metadata xmp = new Metadata(); + DublinCoreAdapter dc = DublinCoreSchema.getAdapter(xmp); + + //dc:title is a "Lang Alt" + dc.setTitle("en", "The title"); + String title = dc.removeTitle("en"); + assertEquals("The title", title); + dc.setTitle("en", "The title"); + dc.setTitle("de", "Der Titel"); + title = dc.removeTitle("en"); + assertEquals("The title", title); + title = dc.removeTitle("en"); + assertNull(title); + + title = dc.removeTitle("de"); + assertEquals("Der Titel", title); + title = dc.removeTitle("de"); + assertNull(title); + } + + public void testReplaceLangAlt() throws Exception { + Metadata xmp = new Metadata(); + DublinCoreAdapter dc = DublinCoreSchema.getAdapter(xmp); + dc.setTitle("Default title"); + StringWriter writer = new StringWriter(); + XMPSerializer.writeXML(xmp, new StreamResult(writer)); + String xmpString = writer.toString(); + xmp = XMPParser.parseXMP(new StreamSource(new java.io.StringReader(xmpString))); + dc = DublinCoreSchema.getAdapter(xmp); + assertEquals("Default title", dc.getTitle()); + dc.setTitle("Updated title"); + XMPProperty prop = xmp.getProperty(new QName(DublinCoreSchema.NAMESPACE, "title")); + XMPArray array = prop.getArrayValue(); + assertNotNull(array); + //Check that only one title is present. There used to be a bug that didn't set the + //non-qualified value equal to the value qualified with "x-default". + assertEquals(1, array.getSize()); + assertEquals("Updated title", array.getValue(0)); + } + + public void testPropertyValues() throws Exception { + Metadata xmp = new Metadata(); + DublinCoreAdapter dc = DublinCoreSchema.getAdapter(xmp); + + String format = dc.getFormat(); + assertNull(format); + + dc.setFormat("application/pdf"); + format = dc.getFormat(); + assertEquals("application/pdf", format); + + dc.setFormat("image/jpeg"); + format = dc.getFormat(); + assertEquals("image/jpeg", format); + + dc.setFormat(null); + format = dc.getFormat(); + assertNull(format); + + dc.setFormat(""); //Empty string same as null value + format = dc.getFormat(); + assertNull(format); + + dc.setTitle("title"); + String title = dc.getTitle(); + assertEquals("title", title); + + dc.setTitle("Titel"); + title = dc.getTitle(); + assertEquals("Titel", title); + + dc.setTitle(null); + title = dc.getTitle(); + assertNull(title); + + dc.setTitle(""); + title = dc.getTitle(); + assertNull(title); + } + + public void testDates() throws Exception { + Metadata xmp = new Metadata(); + XMPBasicAdapter basic = XMPBasicSchema.getAdapter(xmp); + + Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ENGLISH); + cal.set(2008, Calendar.FEBRUARY, 07, 15, 11, 07); + cal.set(Calendar.MILLISECOND, 0); + Date dt = cal.getTime(); + + assertNull(basic.getCreateDate()); + basic.setCreateDate(dt); + Date dt2 = basic.getCreateDate(); + assertEquals(dt2, dt); + } + + public void testQualifiers() throws Exception { + Metadata xmp = new Metadata(); + XMPBasicAdapter basic = XMPBasicSchema.getAdapter(xmp); + + basic.addIdentifier("x123"); + basic.setIdentifier("id1", "system1"); + basic.setIdentifier("12345", "system2"); + + String[] ids = basic.getIdentifiers(); + assertEquals(3, ids.length); + Set set = new java.util.HashSet(Arrays.asList(ids)); + assertTrue(set.contains("x123")); + assertTrue(set.contains("id1")); + assertTrue(set.contains("12345")); + + assertEquals("id1", basic.getIdentifier("system1")); + basic.setIdentifier("id2", "system1"); + assertEquals("id2", basic.getIdentifier("system1")); + assertEquals(3, basic.getIdentifiers().length); + } + +} Propchange: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/XMPPropertyTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/XMPPropertyTest.java ------------------------------------------------------------------------------ svn:keywords = Id --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org For additional commands, e-mail: commits-help@xmlgraphics.apache.org