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.COSDictionary; 22 import org.apache.pdfbox.cos.COSFloat; 23 import org.apache.pdfbox.cos.COSName; 24 import org.apache.pdfbox.pdmodel.common.PDRange; 25 26 /** 27 * This class represents a function in a PDF document. 28 * 29 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 30 * @version $Revision: 1.3 $ 31 */ 32 public abstract class PDDictionaryFunction extends PDFunction 33 { 34 private COSDictionary function = null; 35 36 /** 37 * Constructor to create a new blank function, should only be called by 38 * subclasses. 39 * 40 * @param functionType An integer describing the function type, only 0,2,3,4 41 * are defined by the PDF sepc. 42 */ 43 protected PDDictionaryFunction( int functionType ) 44 { 45 function = new COSDictionary(); 46 function.setInt( "FunctionType", functionType ); 47 } 48 49 /** 50 * Constructor. 51 * 52 * @param functionDictionary The prepopulated function dictionary. 53 */ 54 public PDDictionaryFunction( COSDictionary functionDictionary ) 55 { 56 function = functionDictionary; 57 } 58 59 /** 60 * {@inheritDoc} 61 */ 62 public COSBase getCOSObject() 63 { 64 return function; 65 } 66 67 /** 68 * Get the underlying cos dictionary. 69 * 70 * @return The underlying cos dictionary. 71 */ 72 public COSDictionary getCOSDictionary() 73 { 74 return function; 75 } 76 77 protected COSArray getRangeArray( String fieldName, int n ) 78 { 79 COSArray rangeArray = (COSArray)function.getDictionaryObject( COSName.getPDFName( "Range" ) ); 80 if( rangeArray == null ) 81 { 82 rangeArray = new COSArray(); 83 function.setItem( fieldName, rangeArray ); 84 while( rangeArray.size() < n*2 ) 85 { 86 rangeArray.add( new COSFloat( 0 ) ); 87 rangeArray.add( new COSFloat( 0 ) ); 88 } 89 } 90 return rangeArray; 91 } 92 93 /** 94 * {@inheritDoc} 95 */ 96 public int getNumberOfOutputParameters() 97 { 98 COSArray array = getRangeArray( "Range", 0 ); 99 return array.size() / 2; 100 } 101 102 /** 103 * {@inheritDoc} 104 */ 105 public PDRange getRangeForOutput( int n ) 106 { 107 COSArray rangeArray = getRangeArray( "Range", n ); 108 return new PDRange( rangeArray, n ); 109 } 110 111 /** 112 * {@inheritDoc} 113 */ 114 public void setRangeForOutput( PDRange range, int n ) 115 { 116 COSArray rangeArray = getRangeArray("Range", n ); 117 rangeArray.set( n*2, new COSFloat( range.getMin() ) ); 118 rangeArray.set( n*2+1, new COSFloat( range.getMax() ) ); 119 } 120 121 /** 122 * {@inheritDoc} 123 */ 124 public int getNumberOfInputParameters() 125 { 126 COSArray array = getRangeArray( "Domain", 0 ); 127 return array.size() / 2; 128 } 129 130 /** 131 * {@inheritDoc} 132 */ 133 public PDRange getDomainForInput( int n ) 134 { 135 COSArray rangeArray = getRangeArray( "Domain", n ); 136 return new PDRange( rangeArray, n ); 137 } 138 139 /** 140 * {@inheritDoc} 141 */ 142 public void setDomainForInput( PDRange range, int n ) 143 { 144 COSArray rangeArray = getRangeArray("Domain", n ); 145 rangeArray.set( n*2, new COSFloat( range.getMin() ) ); 146 rangeArray.set( n*2+1, new COSFloat( range.getMax() ) ); 147 } 148 }