KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > documentinterchange > prepress > PDBoxStyle


1 /**
2  * Copyright (c) 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.documentinterchange.prepress;
32
33 import org.pdfbox.cos.COSArray;
34 import org.pdfbox.cos.COSBase;
35 import org.pdfbox.cos.COSDictionary;
36 import org.pdfbox.cos.COSInteger;
37 import org.pdfbox.pdmodel.common.COSObjectable;
38 import org.pdfbox.pdmodel.graphics.PDLineDashPattern;
39 import org.pdfbox.pdmodel.graphics.color.PDColorSpaceInstance;
40 import org.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
41
42 /**
43  * The Box Style specifies visual characteristics for displaying box areas.
44  *
45  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
46  * @version $Revision: 1.3 $
47  */

48 public class PDBoxStyle implements COSObjectable
49 {
50     /**
51      * Style for guideline.
52      */

53     public static final String JavaDoc GUIDELINE_STYLE_SOLID = "S";
54     /**
55      * Style for guideline.
56      */

57     public static final String JavaDoc GUIDELINE_STYLE_DASHED = "D";
58     
59     private COSDictionary dictionary;
60     
61     /**
62      * Default Constructor.
63      *
64      */

65     public PDBoxStyle()
66     {
67         dictionary = new COSDictionary();
68     }
69     
70     /**
71      * Constructor for an existing BoxStyle element.
72      *
73      * @param dic The existing dictionary.
74      */

75     public PDBoxStyle( COSDictionary dic )
76     {
77         dictionary = dic;
78     }
79     
80     /**
81      * Convert this standard java object to a COS object.
82      *
83      * @return The cos object that matches this Java object.
84      */

85     public COSBase getCOSObject()
86     {
87         return dictionary;
88     }
89     
90     /**
91      * Convert this standard java object to a COS object.
92      *
93      * @return The cos object that matches this Java object.
94      */

95     public COSDictionary getDictionary()
96     {
97         return dictionary;
98     }
99     
100     /**
101      * Get the color to be used for the guidelines. This is guaranteed to
102      * not return null. The color space will always be DeviceRGB and the
103      * default color is [0,0,0].
104      *
105      *@return The guideline color.
106      */

107     public PDColorSpaceInstance getGuidelineColor()
108     {
109         COSArray colorValues = (COSArray)dictionary.getDictionaryObject( "C" );
110         if( colorValues == null )
111         {
112             colorValues = new COSArray();
113             colorValues.add( COSInteger.ZERO );
114             colorValues.add( COSInteger.ZERO );
115             colorValues.add( COSInteger.ZERO );
116             dictionary.setItem( "C", colorValues );
117         }
118         PDColorSpaceInstance instance = new PDColorSpaceInstance( colorValues );
119         instance.setColorSpace( PDDeviceRGB.INSTANCE );
120         return instance;
121     }
122     
123     /**
124      * Set the color space instance for this box style. This must be a
125      * PDDeviceRGB!
126      *
127      * @param color The new colorspace value.
128      */

129     public void setGuideLineColor( PDColorSpaceInstance color )
130     {
131         COSArray values = null;
132         if( color != null )
133         {
134             values = color.getCOSColorSpaceValue();
135         }
136         dictionary.setItem( "C", values );
137     }
138     
139     /**
140      * Get the width of the of the guideline in default user space units.
141      * The default is 1.
142      *
143      * @return The width of the guideline.
144      */

145     public float getGuidelineWidth()
146     {
147         return dictionary.getFloat( "W", 1 );
148     }
149     
150     /**
151      * Set the guideline width.
152      *
153      * @param width The width in default user space units.
154      */

155     public void setGuidelineWidth( float width )
156     {
157         dictionary.setFloat( "W", width );
158     }
159     
160     /**
161      * Get the style for the guideline. The default is "S" for solid.
162      *
163      * @return The guideline style.
164      * @see PDBoxStyle#GUIDELINE_STYLE_DASHED
165      * @see PDBoxStyle#GUIDELINE_STYLE_SOLID
166      */

167     public String JavaDoc getGuidelineStyle()
168     {
169         return dictionary.getNameAsString( "S", GUIDELINE_STYLE_SOLID );
170     }
171     
172     /**
173      * Set the style for the box.
174      *
175      * @param style The style for the box line.
176      * @see PDBoxStyle#GUIDELINE_STYLE_DASHED
177      * @see PDBoxStyle#GUIDELINE_STYLE_SOLID
178      */

179     public void setGuidelineStyle( String JavaDoc style )
180     {
181         dictionary.setName( "S", style );
182     }
183     
184     /**
185      * Get the line dash pattern for this box style. This is guaranteed to not
186      * return null. The default is [3],0.
187      *
188      * @return The line dash pattern.
189      */

190     public PDLineDashPattern getLineDashPattern()
191     {
192         PDLineDashPattern pattern = null;
193         COSArray d = (COSArray)dictionary.getDictionaryObject( "D" );
194         if( d == null )
195         {
196             d = new COSArray();
197             d.add( new COSInteger(3) );
198             dictionary.setItem( "D", d );
199         }
200         COSArray lineArray = new COSArray();
201         lineArray.add( d );
202         //dash phase is not specified and assumed to be zero.
203
lineArray.add( new COSInteger( 0 ) );
204         pattern = new PDLineDashPattern( lineArray );
205         return pattern;
206     }
207     
208     /**
209      * Set the line dash pattern associated with this box style.
210      *
211      * @param pattern The patter for this box style.
212      */

213     public void setLineDashPattern( PDLineDashPattern pattern )
214     {
215         COSArray array = null;
216         if( pattern != null )
217         {
218             array = pattern.getCOSDashPattern();
219         }
220         dictionary.setItem( "D", array );
221     }
222 }
Popular Tags