001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.pdf.pd;
031:
032: import de.intarsys.pdf.cos.COSArray;
033: import de.intarsys.pdf.cos.COSBasedObject;
034: import de.intarsys.pdf.cos.COSName;
035: import de.intarsys.pdf.cos.COSObject;
036:
037: /**
038: * An abstract superclass for all color spaces.
039: *
040: */
041: public abstract class PDColorSpace extends PDObject {
042: /**
043: * The meta class implementation
044: */
045: public static class MetaClass extends PDObject.MetaClass {
046: protected MetaClass(Class paramInstanceClass) {
047: super (paramInstanceClass);
048: }
049:
050: public Class getRootClass() {
051: return PDColorSpace.class;
052: }
053:
054: /*
055: * (non-Javadoc)
056: *
057: * @see de.intarsys.pdf.cos.COSBasedObject.MetaClass#doCreateCOSBasedObject(de.intarsys.pdf.cos.COSObject)
058: */
059: protected COSBasedObject doCreateCOSBasedObject(COSObject object) {
060: COSArray array;
061: COSName type;
062:
063: if (object instanceof COSName) {
064: return getNamed((COSName) object);
065: }
066:
067: if (object instanceof COSArray) {
068: array = (COSArray) object;
069: type = array.get(0).asName();
070: if (type == null) {
071: throw new IllegalArgumentException(
072: "ColorSpace array has no type");
073: }
074: if (type.equals(CN_CS_DeviceCMYK)
075: || type.equals(CN_CS_CMYK)) {
076: return PDCSDeviceCMYK.SINGLETON;
077: }
078: if (type.equals(CN_CS_DeviceRGB)
079: || type.equals(CN_CS_RGB)) {
080: return PDCSDeviceRGB.SINGLETON;
081: }
082: if (type.equals(CN_CS_DeviceGray)
083: || type.equals(CN_CS_G)) {
084: return PDCSDeviceGray.SINGLETON;
085: }
086: }
087: return super .doCreateCOSBasedObject(object);
088: }
089:
090: protected COSBasedObject.MetaClass doDetermineClass(
091: COSObject object) {
092: if (object instanceof COSArray) {
093: COSName type = ((COSArray) object).get(0).asName();
094: if (type == null) {
095: throw new IllegalArgumentException(
096: "ColorSpace array has no type");
097: }
098: if (type.equals(CN_CS_CalGray)) {
099: return PDCSCalGray.META;
100: }
101: if (type.equals(CN_CS_CalRGB)) {
102: return PDCSCalRGB.META;
103: }
104: if (type.equals(CN_CS_Lab)) {
105: return PDCSLab.META;
106: }
107: if (type.equals(CN_CS_ICCBased)) {
108: return PDCSICCBased.META;
109: }
110: if (type.equals(CN_CS_Indexed) || type.equals(CN_CS_I)) {
111: return PDCSIndexed.META;
112: }
113: if (type.equals(CN_CS_Pattern)) {
114: return PDCSPattern.META;
115: }
116: if (type.equals(CN_CS_Separation)) {
117: return PDCSSeparation.META;
118: }
119: if (type.equals(CN_CS_DeviceN)) {
120: return PDCSDeviceN.META;
121: }
122: }
123: return super .doDetermineClass(object);
124: }
125: }
126:
127: public static final COSName CN_CS_CalGray = COSName
128: .constant("CalGray");
129:
130: public static final COSName CN_CS_CalRGB = COSName
131: .constant("CalRGB");
132:
133: public static final COSName CN_CS_CMYK = COSName.constant("CMYK");
134:
135: public static final COSName CN_CS_DeviceCMYK = COSName
136: .constant("DeviceCMYK");
137:
138: public static final COSName CN_CS_DeviceGray = COSName
139: .constant("DeviceGray");
140:
141: public static final COSName CN_CS_DeviceN = COSName
142: .constant("DeviceN");
143:
144: public static final COSName CN_CS_DeviceRGB = COSName
145: .constant("DeviceRGB");
146:
147: public static final COSName CN_CS_G = COSName.constant("G");
148:
149: public static final COSName CN_CS_ICCBased = COSName
150: .constant("ICCBased");
151:
152: public static final COSName CN_CS_Indexed = COSName
153: .constant("Indexed");
154:
155: public static final COSName CN_CS_I = COSName.constant("I");
156:
157: public static final COSName CN_CS_Lab = COSName.constant("Lab");
158:
159: public static final COSName CN_CS_Pattern = COSName
160: .constant("Pattern");
161:
162: public static final COSName CN_CS_RGB = COSName.constant("RGB");
163:
164: public static final COSName CN_CS_Separation = COSName
165: .constant("Separation");
166:
167: /** The meta class instance */
168: public static final MetaClass META = new MetaClass(MetaClass.class
169: .getDeclaringClass());
170:
171: public static PDColorSpace getNamed(COSName name) {
172: if (name.equals(CN_CS_Pattern)) {
173: return new PDCSPattern(name);
174: }
175: return getSingleton(name);
176: }
177:
178: /**
179: * return the singleton color space instance corresponding to the given name
180: *
181: * @param name
182: * must be one of the predefined color space names
183: *
184: * @return return the singleton color space instance corresponding to the
185: * given name
186: */
187: public static PDColorSpace getSingleton(COSName name) {
188: if (CN_CS_DeviceGray.equals(name)) {
189: return PDCSDeviceGray.SINGLETON;
190: } else if (CN_CS_G.equals(name)) {
191: return PDCSDeviceGray.SINGLETON;
192: } else if (CN_CS_DeviceRGB.equals(name)) {
193: return PDCSDeviceRGB.SINGLETON;
194: } else if (CN_CS_RGB.equals(name)) {
195: return PDCSDeviceRGB.SINGLETON;
196: } else if (CN_CS_DeviceCMYK.equals(name)) {
197: return PDCSDeviceCMYK.SINGLETON;
198: } else if (CN_CS_CMYK.equals(name)) {
199: return PDCSDeviceCMYK.SINGLETON;
200: } else {
201: return null;
202: }
203: }
204:
205: /**
206: * This attribute is stored here to ease invalidating, should be in
207: * PlatformColorSpace
208: */
209: private Object cachedColorSpace;
210:
211: protected PDColorSpace(COSObject object) {
212: super (object);
213: }
214:
215: public Object getCachedColorSpace() {
216: // todo 1 review, maybe move to platform.
217: return cachedColorSpace;
218: }
219:
220: public void invalidateCaches() {
221: super .invalidateCaches();
222: cachedColorSpace = null;
223: }
224:
225: public void setCachedColorSpace(Object paramColorSpace) {
226: cachedColorSpace = paramColorSpace;
227: }
228: }
|