KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > common > function > PDFunction


1 /**
2  * Copyright (c) 2006, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel.common.function;
32
33 import java.io.IOException JavaDoc;
34
35 import org.pdfbox.cos.COSBase;
36 import org.pdfbox.cos.COSDictionary;
37 import org.pdfbox.cos.COSObject;
38 import org.pdfbox.cos.COSStream;
39 import org.pdfbox.pdmodel.common.COSObjectable;
40 import org.pdfbox.pdmodel.common.PDRange;
41 import org.pdfbox.pdmodel.common.PDStream;
42
43 /**
44  * This class represents a function in a PDF document.
45  *
46  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
47  * @version $Revision: 1.3 $
48  */

49 public abstract class PDFunction implements COSObjectable
50 {
51     /**
52      * Create the correct PD Model function based on the COS base function.
53      *
54      * @param function The COS function dictionary.
55      *
56      * @return The PDModel Function object.
57      *
58      * @throws IOException If we are unable to create the PDFunction object.
59      */

60     public static PDFunction create( COSBase function ) throws IOException JavaDoc
61     {
62         PDFunction retval = null;
63         if( function instanceof COSObject )
64         {
65             function = ((COSObject)function).getCOSObject();
66         }
67         if( function instanceof COSDictionary )
68         {
69             COSDictionary funcDic = (COSDictionary)function;
70             int functionType = funcDic.getInt( "FunctionType" );
71             if( function instanceof COSStream )
72             {
73                 if( functionType == 0 )
74                 {
75                     retval = new PDFunctionType0(new PDStream((COSStream)function));
76                 }
77                 else if( functionType == 4 )
78                 {
79                     retval = new PDFunctionType4(new PDStream((COSStream)function));
80                 }
81                 else
82                 {
83                     throw new IOException JavaDoc( "Error: Unknown stream function type " + functionType );
84                 }
85             }
86             else
87             {
88                 if( functionType == 2 )
89                 {
90                     retval = new PDFunctionType2(funcDic);
91                 }
92                 else if( functionType == 3 )
93                 {
94                     retval = new PDFunctionType3(funcDic);
95                 }
96                 else
97                 {
98                     throw new IOException JavaDoc( "Error: Unknown dictionary function type " + functionType );
99                 }
100             }
101             
102         }
103         else
104         {
105             throw new IOException JavaDoc( "Error: Unknown COS type for function " + function );
106         }
107         return retval;
108     }
109     
110     /**
111      * This will get the number of output parameters that
112      * have a range specified. A range for output parameters
113      * is optional so this may return zero for a function
114      * that does have output parameters, this will simply return the
115      * number that have the rnage specified.
116      *
117      * @return The number of input parameters that have a range
118      * specified.
119      */

120     public abstract int getNumberOfOutputParameters();
121
122     /**
123      * This will get the range for a certain output parameters. This is will never
124      * return null. If it is not present then the range 0 to 0 will
125      * be returned.
126      *
127      * @param n The output parameter number to get the range for.
128      *
129      * @return The range for this component.
130      */

131     public abstract PDRange getRangeForOutput(int n);
132
133     /**
134      * This will set the a range for output parameter.
135      *
136      * @param range The new range for the output parameter.
137      * @param n The ouput parameter number to set the range for.
138      */

139     public abstract void setRangeForOutput(PDRange range, int n);
140
141     /**
142      * This will get the number of input parameters that
143      * have a domain specified.
144      *
145      * @return The number of input parameters that have a domain
146      * specified.
147      */

148     public abstract int getNumberOfInputParameters();
149
150     /**
151      * This will get the range for a certain input parameter. This is will never
152      * return null. If it is not present then the range 0 to 0 will
153      * be returned.
154      *
155      * @param n The parameter number to get the domain for.
156      *
157      * @return The domain range for this component.
158      */

159     public abstract PDRange getDomainForInput(int n);
160
161     /**
162      * This will set the domain for the input values.
163      *
164      * @param range The new range for the input.
165      * @param n The number of the input parameter to set the domain for.
166      */

167     public abstract void setDomainForInput(PDRange range, int n);
168
169 }
Popular Tags