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.cds.CDSRectangle;
033: import de.intarsys.pdf.cos.COSBasedObject;
034: import de.intarsys.pdf.cos.COSDictionary;
035: import de.intarsys.pdf.cos.COSInteger;
036: import de.intarsys.pdf.cos.COSName;
037: import de.intarsys.pdf.cos.COSObject;
038: import de.intarsys.pdf.cos.COSRuntimeException;
039:
040: /**
041: * An object defining the shading to be used when filling a shape.
042: *
043: */
044: public abstract class PDShading extends PDObject {
045: /**
046: * The meta class implementation
047: */
048: static public class MetaClass extends PDObject.MetaClass {
049: protected MetaClass(Class paramInstanceClass) {
050: super (paramInstanceClass);
051: }
052:
053: public Class getRootClass() {
054: return PDShading.class;
055: }
056:
057: protected COSBasedObject.MetaClass doDetermineClass(
058: COSObject object) {
059: int type = ((COSInteger) ((COSDictionary) object)
060: .get(DK_ShadingType)).intValue();
061: switch (type) {
062: case SHADING_TYPE_FUNCTIONBASED:
063: return PDFunctionBasedShading.META;
064: case SHADING_TYPE_AXIAL:
065: return PDAxialShading.META;
066: case SHADING_TYPE_RADIAL:
067: return PDRadialShading.META;
068: case SHADING_TYPE_FREEFORM:
069: return PDFreeFormShading.META;
070: case SHADING_TYPE_LATTICEFORM:
071: return PDLatticeFormShading.META;
072: case SHADING_TYPE_COONS:
073: return PDCoonsShading.META;
074: case SHADING_TYPE_TENSORPRODUCT:
075: return PDTensorProductShading.META;
076: default:
077: object.handleException(new COSRuntimeException(
078: "unsupported shading type " + type));
079: return null;
080: }
081: }
082: }
083:
084: private static final COSName DK_AntiAlias = COSName
085: .constant("AntiAlias"); //$NON-NLS-1$
086:
087: private static final COSName DK_BBox = COSName.constant("BBox"); //$NON-NLS-1$
088:
089: private static final COSName DK_ColorSpace = COSName
090: .constant("ColorSpace"); //$NON-NLS-1$
091:
092: protected static final COSName DK_ShadingType = COSName
093: .constant("ShadingType"); //$NON-NLS-1$
094:
095: /** The meta class instance */
096: static public final MetaClass META = new MetaClass(MetaClass.class
097: .getDeclaringClass());
098:
099: public static final int SHADING_TYPE_AXIAL = 2;
100:
101: public static final int SHADING_TYPE_COONS = 6;
102:
103: public static final int SHADING_TYPE_FREEFORM = 4;
104:
105: public static final int SHADING_TYPE_FUNCTIONBASED = 1;
106:
107: public static final int SHADING_TYPE_LATTICEFORM = 5;
108:
109: public static final int SHADING_TYPE_RADIAL = 3;
110:
111: public static final int SHADING_TYPE_TENSORPRODUCT = 7;
112:
113: private boolean antiAlias;
114:
115: private CDSRectangle boundingBox;
116:
117: private PDColorSpace colorSpace;
118:
119: protected PDShading(COSObject object) {
120: super (object);
121:
122: COSObject cosAntiAlias;
123: COSObject cosBBox;
124:
125: cosAntiAlias = object.asDictionary().get(DK_AntiAlias);
126: if (cosAntiAlias.isNull()) {
127: antiAlias = false;
128: } else {
129: antiAlias = cosAntiAlias.asBoolean().booleanValue();
130: }
131:
132: cosBBox = object.asDictionary().get(DK_BBox);
133: if (cosBBox.isNull()) {
134: boundingBox = null;
135: } else {
136: boundingBox = CDSRectangle.createFromCOS(cosBBox.asArray());
137: }
138:
139: // color space will be resolved lazily
140: }
141:
142: public CDSRectangle getBoundingBox() {
143: return boundingBox;
144: }
145:
146: public PDColorSpace getColorSpace() {
147: if (colorSpace == null) {
148: colorSpace = (PDColorSpace) PDColorSpace.META
149: .createFromCos(cosGetObject().asDictionary().get(
150: DK_ColorSpace));
151: }
152: return colorSpace;
153: }
154:
155: public abstract int getShadingType();
156:
157: public boolean isAntiAlias() {
158: return antiAlias;
159: }
160: }
|