1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.pdfbox.pdmodel.graphics; 18 19 import org.apache.pdfbox.pdmodel.common.COSObjectable; 20 import org.apache.pdfbox.cos.COSBase; 21 import org.apache.pdfbox.cos.COSBoolean; 22 import org.apache.pdfbox.cos.COSArray; 23 import org.apache.pdfbox.cos.COSName; 24 import org.apache.pdfbox.cos.COSDictionary; 25 import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpaceFactory; 26 import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace; 27 import org.apache.pdfbox.pdmodel.common.function.PDFunction; 28 29 30 31 import java.io.IOException; 32 33 /** 34 * This class represents a Shading Pattern color space. 35 * See section 4.6.3 of the PDF 1.7 specification. 36 * 37 * @author <a href="mailto:Daniel.Wilson@BlackLocustSoftware.com">Daniel wilson</a> 38 * @version $Revision: 1.0 $ 39 */ 40 public class PDShading implements COSObjectable 41 { 42 private COSDictionary DictShading; 43 private COSName shadingname; 44 45 /** 46 * The name of this object. 47 */ 48 public static final String NAME = "Shading"; 49 50 /** 51 * Default constructor. 52 */ 53 public PDShading() 54 { 55 DictShading = new COSDictionary(); 56 //DictShading.add( COSName.getPDFName( NAME ) ); 57 } 58 59 /** 60 * Constructor. 61 * 62 * @param shading The shading dictionary. 63 */ 64 public PDShading(COSName name, COSDictionary shading) 65 { 66 DictShading = shading; 67 shadingname = name; 68 } 69 70 /** 71 * This will return the name of the object. 72 * 73 * @return The name of the object. 74 */ 75 public String getName() 76 { 77 return NAME; 78 } 79 80 /** 81 * Convert this standard java object to a COS object. 82 * 83 * @return The cos object that matches this Java object. 84 */ 85 public COSBase getCOSObject() 86 { 87 return COSName.getPDFName( getName() ); 88 } 89 90 /** 91 * This will return the name of this particular shading dictionary 92 * 93 * @return The name of the shading dictionary 94 */ 95 public COSName getShadingName() 96 { 97 return shadingname; 98 } 99 100 /** 101 * This will return the ShadingType -- an integer between 1 and 7 that specifies the gradient type. 102 * Required in all Shading Dictionaries. 103 * 104 * @return The Shading Type 105 */ 106 public int getShadingType() 107 { 108 return DictShading.getInt("ShadingType"); 109 } 110 111 /** 112 * This will return the Color Space. 113 * Required in all Shading Dictionaries. 114 * 115 * @return The Color Space of the shading dictionary 116 */ 117 public PDColorSpace getColorSpace() throws IOException 118 { 119 return PDColorSpaceFactory.createColorSpace(DictShading.getDictionaryObject("ColorSpace")); 120 } 121 122 /** 123 * This will return a boolean flag indicating whether to antialias the shading pattern. 124 * 125 * @return The antialias flag, defaulting to False 126 */ 127 public boolean getAntiAlias() 128 { 129 return DictShading.getBoolean("AntiAlias",false); 130 } 131 132 /** 133 * Returns the coordinate array used by several of the gradient types. Interpretation depends on the ShadingType. 134 * 135 * @return The coordinate array. 136 */ 137 public COSArray getCoords() 138 { 139 return (COSArray)(DictShading.getDictionaryObject("Coords")); 140 } 141 142 /** 143 * Returns the function used by several of the gradient types. Interpretation depends on the ShadingType. 144 * 145 * @return The gradient function. 146 */ 147 public PDFunction getFunction() throws IOException 148 { 149 return PDFunction.create(DictShading.getDictionaryObject("Function")); 150 } 151 152 /** 153 * Returns the Domain array used by several of the gradient types. Interpretation depends on the ShadingType. 154 * 155 * @return The Domain array. 156 */ 157 public COSArray getDomain() 158 { 159 return (COSArray)(DictShading.getDictionaryObject("Domain")); 160 } 161 162 /** 163 * Returns the Extend array used by several of the gradient types. Interpretation depends on the ShadingType. 164 * Default is {false, false}. 165 * 166 * @return The Extend array. 167 */ 168 public COSArray getExtend() 169 { 170 COSArray arExtend=(COSArray)(DictShading.getDictionaryObject("Extend")); 171 if (arExtend == null) 172 { 173 arExtend = new COSArray(); 174 arExtend.add(COSBoolean.FALSE); 175 arExtend.add(COSBoolean.FALSE); 176 } 177 178 return arExtend; 179 } 180 181 /** 182 * {@inheritDoc} 183 */ 184 public String toString() 185 { 186 String sColorSpace; 187 String sFunction; 188 try 189 { 190 sColorSpace = getColorSpace().toString(); 191 }catch (IOException e) 192 { 193 sColorSpace = "Failure retrieving ColorSpace: " + e.toString(); 194 } 195 try 196 { 197 sFunction = getFunction().toString(); 198 }catch(IOException e) 199 { 200 sFunction = "n/a"; 201 } 202 203 204 String s = "Shading " + shadingname + "\n" 205 + "\tShadingType: " + getShadingType() + "\n" 206 + "\tColorSpace: " + sColorSpace + "\n" 207 + "\tAntiAlias: " + getAntiAlias() + "\n" 208 + "\tCoords: " + getCoords().toString() + "\n" 209 + "\tDomain: " + getDomain().toString() + "\n" 210 + "\tFunction: " + sFunction + "\n" 211 + "\tExtend: " + getExtend().toString() + "\n" 212 + "\tRaw Value:\n" + 213 DictShading.toString(); 214 215 return s; 216 } 217 218 }