KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > fdf > FDFIconFit


1 /**
2  * Copyright (c) 2004-2005, 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.fdf;
32
33 import org.pdfbox.cos.COSArray;
34 import org.pdfbox.cos.COSBase;
35 import org.pdfbox.cos.COSDictionary;
36
37 import org.pdfbox.pdmodel.common.COSObjectable;
38 import org.pdfbox.pdmodel.common.PDRange;
39
40 /**
41  * This represents an Icon fit dictionary for an FDF field.
42  *
43  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
44  * @version $Revision: 1.3 $
45  */

46 public class FDFIconFit implements COSObjectable
47 {
48     private COSDictionary fit;
49
50     /**
51      * A scale option.
52      */

53     public static final String JavaDoc SCALE_OPTION_ALWAYS = "A";
54     /**
55      * A scale option.
56      */

57     public static final String JavaDoc SCALE_OPTION_ONLY_WHEN_ICON_IS_BIGGER = "B";
58     /**
59      * A scale option.
60      */

61     public static final String JavaDoc SCALE_OPTION_ONLY_WHEN_ICON_IS_SMALLER = "S";
62     /**
63      * A scale option.
64      */

65     public static final String JavaDoc SCALE_OPTION_NEVER = "N";
66
67     /**
68      * Scale to fill with of annotation, disregarding aspect ratio.
69      */

70     public static final String JavaDoc SCALE_TYPE_ANAMORPHIC = "A";
71     /**
72      * Scale to fit width or height, smaller of two, while retaining aspect ration.
73      */

74     public static final String JavaDoc SCALE_TYPE_PROPORTIONAL = "P";
75
76
77
78     /**
79      * Default constructor.
80      */

81     public FDFIconFit()
82     {
83         fit = new COSDictionary();
84     }
85
86     /**
87      * Constructor.
88      *
89      * @param f The icon fit dictionary.
90      */

91     public FDFIconFit( COSDictionary f )
92     {
93         fit = f;
94     }
95
96     /**
97      * Convert this standard java object to a COS object.
98      *
99      * @return The cos object that matches this Java object.
100      */

101     public COSBase getCOSObject()
102     {
103         return fit;
104     }
105
106     /**
107      * Convert this standard java object to a COS object.
108      *
109      * @return The cos object that matches this Java object.
110      */

111     public COSDictionary getCOSDictionary()
112     {
113         return fit;
114     }
115
116     /**
117      * This will get the scale option. See the SCALE_OPTION_XXX constants. This
118      * is guaranteed to never return null. Default: Always
119      *
120      * @return The scale option.
121      */

122     public String JavaDoc getScaleOption()
123     {
124         String JavaDoc retval = fit.getNameAsString( "SW" );
125         if( retval == null )
126         {
127             retval = SCALE_OPTION_ALWAYS;
128         }
129         return retval;
130     }
131
132     /**
133      * This will set the scale option for the icon. Set the SCALE_OPTION_XXX constants.
134      *
135      * @param option The scale option.
136      */

137     public void setScaleOption( String JavaDoc option )
138     {
139         fit.setName( "SW", option );
140     }
141
142     /**
143      * This will get the scale type. See the SCALE_TYPE_XXX constants. This is
144      * guaranteed to never return null. Default: Proportional
145      *
146      * @return The scale type.
147      */

148     public String JavaDoc getScaleType()
149     {
150         String JavaDoc retval = fit.getNameAsString( "S" );
151         if( retval == null )
152         {
153             retval = SCALE_TYPE_PROPORTIONAL;
154         }
155         return retval;
156     }
157
158     /**
159      * This will set the scale type. See the SCALE_TYPE_XXX constants.
160      *
161      * @param scale The scale type.
162      */

163     public void setScaleType( String JavaDoc scale )
164     {
165         fit.setName( "S", scale );
166     }
167
168     /**
169      * This is guaranteed to never return null.<br />
170      *
171      * To quote the PDF Spec
172      * "An array of two numbers between 0.0 and 1.0 indicating the fraction of leftover
173      * space to allocate at the left and bottom of the icon. A value of [0.0 0.0] positions the
174      * icon at the bottom-left corner of the annotation rectangle; a value of [0.5 0.5] centers it
175      * within the rectangle. This entry is used only if the icon is scaled proportionally. Default
176      * value: [0.5 0.5]."
177      *
178      * @return The fractional space to allocate.
179      */

180     public PDRange getFractionalSpaceToAllocate()
181     {
182         PDRange retval = null;
183         COSArray array = (COSArray)fit.getDictionaryObject( "A" );
184         if( array == null )
185         {
186             retval = new PDRange();
187             retval.setMin( .5f );
188             retval.setMax( .5f );
189             setFractionalSpaceToAllocate( retval );
190         }
191         else
192         {
193             retval = new PDRange( array );
194         }
195         return retval;
196     }
197
198     /**
199      * This will set frational space to allocate.
200      *
201      * @param space The space to allocate.
202      */

203     public void setFractionalSpaceToAllocate( PDRange space )
204     {
205         fit.setItem( "A", space );
206     }
207
208     /**
209      * This will tell if the icon should scale to fit the annotation bounds. Default: false
210      *
211      * @return A flag telling if the icon should scale.
212      */

213     public boolean shouldScaleToFitAnnotation()
214     {
215         return fit.getBoolean( "FB", false );
216     }
217
218     /**
219      * This will tell the icon to scale.
220      *
221      * @param value The flag value.
222      */

223     public void setScaleToFitAnnotation( boolean value )
224     {
225         fit.setBoolean( "FB", value );
226     }
227 }
Popular Tags