KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.pdfbox.cos.COSArray;
34 import org.pdfbox.cos.COSBase;
35 import org.pdfbox.cos.COSFloat;
36 import org.pdfbox.cos.COSName;
37 import org.pdfbox.cos.COSStream;
38 import org.pdfbox.pdmodel.PDDocument;
39 import org.pdfbox.pdmodel.common.PDRange;
40 import org.pdfbox.pdmodel.common.PDStream;
41
42 /**
43  * This class represents a function in a PDF document.
44  *
45  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
46  * @version $Revision: 1.3 $
47  */

48 public abstract class PDStreamFunction extends PDFunction
49 {
50     private PDStream function = null;
51
52     /**
53      * Constructor to create a new blank function, should only be called by
54      * subclasses.
55      *
56      * @param doc The document that this function is part of.
57      * @param functionType An integer describing the function type, only 0,2,3,4
58      * are defined by the PDF sepc.
59      */

60     protected PDStreamFunction( PDDocument doc, int functionType )
61     {
62         function = new PDStream( doc );
63         function.getStream().setInt( "FunctionType", functionType );
64     }
65
66     /**
67      * Constructor.
68      *
69      * @param functionDictionary The prepopulated function dictionary.
70      */

71     public PDStreamFunction( PDStream functionDictionary )
72     {
73         function = functionDictionary;
74     }
75
76     /**
77      * Convert this standard java object to a COS object.
78      *
79      * @return The cos object that matches this Java object.
80      */

81     public COSBase getCOSObject()
82     {
83         return function.getCOSObject();
84     }
85
86     /**
87      * This will get the underlying array value.
88      *
89      * @return The cos object that this object wraps.
90      */

91     public COSStream getCOSStream()
92     {
93         return function.getStream();
94     }
95     
96     private COSArray getRangeArray( String JavaDoc fieldName, int n )
97     {
98         COSArray rangeArray = (COSArray)function.getStream().getDictionaryObject( COSName.getPDFName( "Range" ) );
99         if( rangeArray == null )
100         {
101             rangeArray = new COSArray();
102             function.getStream().setItem( fieldName, rangeArray );
103             while( rangeArray.size() < n*2 )
104             {
105                 rangeArray.add( new COSFloat( 0 ) );
106                 rangeArray.add( new COSFloat( 0 ) );
107             }
108         }
109         return rangeArray;
110     }
111     
112     /**
113      * This will get the number of output parameters that
114      * have a range specified. A range for output parameters
115      * is optional so this may return zero for a function
116      * that does have output parameters, this will simply return the
117      * number that have the rnage specified.
118      *
119      * @return The number of input parameters that have a range
120      * specified.
121      */

122     public int getNumberOfOutputParameters()
123     {
124         COSArray array = getRangeArray( "Range", 0 );
125         return array.size() / 2;
126     }
127
128     /**
129      * This will get the range for a certain output parameters. This is will never
130      * return null. If it is not present then the range 0 to 0 will
131      * be returned.
132      *
133      * @param n The output parameter number to get the range for.
134      *
135      * @return The range for this component.
136      */

137     public PDRange getRangeForOutput( int n )
138     {
139         COSArray rangeArray = getRangeArray( "Range", n );
140         return new PDRange( rangeArray, n );
141     }
142
143     /**
144      * This will set the a range for output parameter.
145      *
146      * @param range The new range for the output parameter.
147      * @param n The ouput parameter number to set the range for.
148      */

149     public void setRangeForOutput( PDRange range, int n )
150     {
151         COSArray rangeArray = getRangeArray("Range", n );
152         rangeArray.set( n*2, new COSFloat( range.getMin() ) );
153         rangeArray.set( n*2+1, new COSFloat( range.getMax() ) );
154     }
155     
156     /**
157      * This will get the number of input parameters that
158      * have a domain specified.
159      *
160      * @return The number of input parameters that have a domain
161      * specified.
162      */

163     public int getNumberOfInputParameters()
164     {
165         COSArray array = getRangeArray( "Domain", 0 );
166         return array.size() / 2;
167     }
168     
169     /**
170      * This will get the range for a certain input parameter. This is will never
171      * return null. If it is not present then the range 0 to 0 will
172      * be returned.
173      *
174      * @param n The parameter number to get the domain for.
175      *
176      * @return The domain range for this component.
177      */

178     public PDRange getDomainForInput( int n )
179     {
180         COSArray rangeArray = getRangeArray( "Domain", n );
181         return new PDRange( rangeArray, n );
182     }
183
184     /**
185      * This will set the domain for the input values.
186      *
187      * @param range The new range for the input.
188      * @param n The number of the input parameter to set the domain for.
189      */

190     public void setDomainForInput( PDRange range, int n )
191     {
192         COSArray rangeArray = getRangeArray("Domain", n );
193         rangeArray.set( n*2, new COSFloat( range.getMin() ) );
194         rangeArray.set( n*2+1, new COSFloat( range.getMax() ) );
195     }
196 }
Popular Tags