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.common.function; 18 19 import org.apache.pdfbox.cos.COSArray; 20 import org.apache.pdfbox.cos.COSBase; 21 import org.apache.pdfbox.cos.COSFloat; 22 import org.apache.pdfbox.cos.COSName; 23 import org.apache.pdfbox.cos.COSStream; 24 import org.apache.pdfbox.pdmodel.PDDocument; 25 import org.apache.pdfbox.pdmodel.common.PDRange; 26 import org.apache.pdfbox.pdmodel.common.PDStream; 27 28 /** 29 * This class represents a function in a PDF document. 30 * 31 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 32 * @version $Revision: 1.3 $ 33 */ 34 public abstract class PDStreamFunction extends PDFunction 35 { 36 private PDStream function = null; 37 38 /** 39 * Constructor to create a new blank function, should only be called by 40 * subclasses. 41 * 42 * @param doc The document that this function is part of. 43 * @param functionType An integer describing the function type, only 0,2,3,4 44 * are defined by the PDF sepc. 45 */ 46 protected PDStreamFunction( PDDocument doc, int functionType ) 47 { 48 function = new PDStream( doc ); 49 function.getStream().setInt( "FunctionType", functionType ); 50 } 51 52 /** 53 * Constructor. 54 * 55 * @param functionDictionary The prepopulated function dictionary. 56 */ 57 public PDStreamFunction( PDStream functionDictionary ) 58 { 59 function = functionDictionary; 60 } 61 62 /** 63 * Convert this standard java object to a COS object. 64 * 65 * @return The cos object that matches this Java object. 66 */ 67 public COSBase getCOSObject() 68 { 69 return function.getCOSObject(); 70 } 71 72 /** 73 * This will get the underlying array value. 74 * 75 * @return The cos object that this object wraps. 76 */ 77 public COSStream getCOSStream() 78 { 79 return function.getStream(); 80 } 81 82 private COSArray getRangeArray( String fieldName, int n ) 83 { 84 COSArray rangeArray = (COSArray)function.getStream().getDictionaryObject( COSName.getPDFName( "Range" ) ); 85 if( rangeArray == null ) 86 { 87 rangeArray = new COSArray(); 88 function.getStream().setItem( fieldName, rangeArray ); 89 while( rangeArray.size() < n*2 ) 90 { 91 rangeArray.add( new COSFloat( 0 ) ); 92 rangeArray.add( new COSFloat( 0 ) ); 93 } 94 } 95 return rangeArray; 96 } 97 98 /** 99 * This will get the number of output parameters that 100 * have a range specified. A range for output parameters 101 * is optional so this may return zero for a function 102 * that does have output parameters, this will simply return the 103 * number that have the rnage specified. 104 * 105 * @return The number of input parameters that have a range 106 * specified. 107 */ 108 public int getNumberOfOutputParameters() 109 { 110 COSArray array = getRangeArray( "Range", 0 ); 111 return array.size() / 2; 112 } 113 114 /** 115 * This will get the range for a certain output parameters. This is will never 116 * return null. If it is not present then the range 0 to 0 will 117 * be returned. 118 * 119 * @param n The output parameter number to get the range for. 120 * 121 * @return The range for this component. 122 */ 123 public PDRange getRangeForOutput( int n ) 124 { 125 COSArray rangeArray = getRangeArray( "Range", n ); 126 return new PDRange( rangeArray, n ); 127 } 128 129 /** 130 * This will set the a range for output parameter. 131 * 132 * @param range The new range for the output parameter. 133 * @param n The ouput parameter number to set the range for. 134 */ 135 public void setRangeForOutput( PDRange range, int n ) 136 { 137 COSArray rangeArray = getRangeArray("Range", n ); 138 rangeArray.set( n*2, new COSFloat( range.getMin() ) ); 139 rangeArray.set( n*2+1, new COSFloat( range.getMax() ) ); 140 } 141 142 /** 143 * This will get the number of input parameters that 144 * have a domain specified. 145 * 146 * @return The number of input parameters that have a domain 147 * specified. 148 */ 149 public int getNumberOfInputParameters() 150 { 151 COSArray array = getRangeArray( "Domain", 0 ); 152 return array.size() / 2; 153 } 154 155 /** 156 * This will get the range for a certain input parameter. This is will never 157 * return null. If it is not present then the range 0 to 0 will 158 * be returned. 159 * 160 * @param n The parameter number to get the domain for. 161 * 162 * @return The domain range for this component. 163 */ 164 public PDRange getDomainForInput( int n ) 165 { 166 COSArray rangeArray = getRangeArray( "Domain", n ); 167 return new PDRange( rangeArray, n ); 168 } 169 170 /** 171 * This will set the domain for the input values. 172 * 173 * @param range The new range for the input. 174 * @param n The number of the input parameter to set the domain for. 175 */ 176 public void setDomainForInput( PDRange range, int n ) 177 { 178 COSArray rangeArray = getRangeArray("Domain", n ); 179 rangeArray.set( n*2, new COSFloat( range.getMin() ) ); 180 rangeArray.set( n*2+1, new COSFloat( range.getMax() ) ); 181 } 182 }