KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > graphics > PDExtendedGraphicsState


1 /**
2  * Copyright (c) 2003, 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.graphics;
32
33 import org.pdfbox.cos.COSArray;
34 import org.pdfbox.cos.COSBase;
35 import org.pdfbox.cos.COSDictionary;
36 import org.pdfbox.cos.COSFloat;
37 import org.pdfbox.cos.COSName;
38 import org.pdfbox.cos.COSNumber;
39
40 import org.pdfbox.pdmodel.common.COSObjectable;
41
42 import java.io.IOException JavaDoc;
43
44 import java.util.Iterator JavaDoc;
45
46 /**
47  * This class represents the graphics state dictionary that is stored in the PDF document.
48  * The PDGraphicsStateValue holds the current runtime values as a stream is being executed.
49  *
50  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
51  * @version $Revision: 1.5 $
52  */

53 public class PDExtendedGraphicsState implements COSObjectable
54 {
55     private static final COSName LW = COSName.getPDFName( "LW" );
56     private static final COSName LC = COSName.getPDFName( "LC" );
57     private static final COSName LJ = COSName.getPDFName( "LJ" );
58     private static final COSName ML = COSName.getPDFName( "ML" );
59     private static final COSName D = COSName.getPDFName( "D" );
60     private static final COSName RI = COSName.getPDFName( "RI" );
61     private static final COSName OP = COSName.getPDFName( "OP" );
62     private static final COSName OP_NS = COSName.getPDFName( "op" );
63     private static final COSName OPM = COSName.getPDFName( "OPM" );
64     private static final COSName FONT = COSName.getPDFName( "Font" );
65     private static final COSName FL = COSName.getPDFName( "FL" );
66     private static final COSName SM = COSName.getPDFName( "SM" );
67     private static final COSName SA = COSName.getPDFName( "SA" );
68     private static final COSName CA = COSName.getPDFName( "CA" );
69     private static final COSName CA_NS = COSName.getPDFName( "ca" );
70     private static final COSName AIS = COSName.getPDFName( "AIS" );
71     private static final COSName TK = COSName.getPDFName( "TK" );
72
73     /**
74      * Rendering intent constants, see PDF Reference 1.5 Section 4.5.4 Rendering Intents.
75      */

76     public static final String JavaDoc RENDERING_INTENT_ABSOLUTE_COLORIMETRIC = "AbsoluteColorimetric";
77     /**
78      * Rendering intent constants, see PDF Reference 1.5 Section 4.5.4 Rendering Intents.
79      */

80     public static final String JavaDoc RENDERING_INTENT_RELATIVE_COLORIMETRIC = "RelativeColorimetric";
81     /**
82      * Rendering intent constants, see PDF Reference 1.5 Section 4.5.4 Rendering Intents.
83      */

84     public static final String JavaDoc RENDERING_INTENT_SATURATION = "Saturation";
85     /**
86      * Rendering intent constants, see PDF Reference 1.5 Section 4.5.4 Rendering Intents.
87      */

88     public static final String JavaDoc RENDERING_INTENT_PERCEPTUAL = "Perceptual";
89
90
91     private COSDictionary graphicsState;
92
93     /**
94      * Default constructor, creates blank graphics state.
95      */

96     public PDExtendedGraphicsState()
97     {
98         graphicsState = new COSDictionary();
99         graphicsState.setItem( COSName.TYPE, COSName.getPDFName( "ExtGState" ) );
100     }
101
102     /**
103      * Create a graphics state from an existing dictionary.
104      *
105      * @param dictionary The existing graphics state.
106      */

107     public PDExtendedGraphicsState( COSDictionary dictionary )
108     {
109         graphicsState = dictionary;
110     }
111
112     /**
113      * This will implement the gs operator.
114      *
115      * @param gs The state to copy this dictionaries values into.
116      *
117      * @throws IOException If there is an error copying font information.
118      */

119     public void copyIntoGraphicsState( PDGraphicsState gs ) throws IOException JavaDoc
120     {
121         Iterator JavaDoc keys = graphicsState.keyList().iterator();
122         while( keys.hasNext() )
123         {
124             COSName key = (COSName)keys.next();
125             if( key.equals( LW ) )
126             {
127                 gs.setLineWidth( getLineWidth().doubleValue() );
128             }
129             else if( key.equals( LC ) )
130             {
131                 gs.setLineCap( getLineCapStyle() );
132             }
133             else if( key.equals( LJ ) )
134             {
135                 gs.setLineJoin( getLineJoinStyle() );
136             }
137             else if( key.equals( ML ) )
138             {
139                 gs.setMiterLimit( getMiterLimit().doubleValue() );
140             }
141             else if( key.equals( D ) )
142             {
143                 gs.setLineDashPattern( getLineDashPattern() );
144             }
145             else if( key.equals( RI ) )
146             {
147                 gs.setRenderingIntent( getRenderingIntent() );
148             }
149             else if( key.equals( OPM ) )
150             {
151                 gs.setOverprintMode( getOverprintMode().doubleValue() );
152             }
153             else if( key.equals( FONT ) )
154             {
155                 PDFontSetting setting = getFontSetting();
156                 gs.getTextState().setFont( setting.getFont() );
157                 gs.getTextState().setFontSize( setting.getFontSize() );
158             }
159             else if( key.equals( FL ) )
160             {
161                 gs.setFlatness( getFlatnessTolerance().floatValue() );
162             }
163             else if( key.equals( SM ) )
164             {
165                 gs.setSmoothness( getSmoothnessTolerance().floatValue() );
166             }
167             else if( key.equals( SA ) )
168             {
169                 gs.setStrokeAdjustment( getAutomaticStrokeAdjustment() );
170             }
171             else if( key.equals( CA ) )
172             {
173                 gs.setAlphaConstants( getStrokingAlpaConstant().floatValue() );
174             }/**
175             else if( key.equals( CA_NS ) )
176             {
177             }**/

178             else if( key.equals( AIS ) )
179             {
180                 gs.setAlphaSource( getAlphaSourceFlag() );
181             }
182             else if( key.equals( TK ) )
183             {
184                 gs.getTextState().setKnockoutFlag( getTextKnockoutFlag() );
185             }
186         }
187     }
188
189     /**
190      * This will get the underlying dictionary that this class acts on.
191      *
192      * @return The underlying dictionary for this class.
193      */

194     public COSDictionary getCOSDictionary()
195     {
196         return graphicsState;
197     }
198
199     /**
200      * Convert this standard java object to a COS object.
201      *
202      * @return The cos object that matches this Java object.
203      */

204     public COSBase getCOSObject()
205     {
206         return graphicsState;
207     }
208
209     /**
210      * This will get the line width. This will return null if there is no line width
211      *
212      * @return null or the LW value of the dictionary.
213      */

214     public Float JavaDoc getLineWidth()
215     {
216         return getFloatItem( LW );
217     }
218
219     /**
220      * This will set the line width.
221      *
222      * @param width The line width for the object.
223      */

224     public void setLineWidth( Float JavaDoc width )
225     {
226         setFloatItem( LW, width );
227     }
228
229     /**
230      * This will get the line cap style.
231      *
232      * @return null or the LC value of the dictionary.
233      */

234     public int getLineCapStyle()
235     {
236         return graphicsState.getInt( LC );
237     }
238
239     /**
240      * This will set the line cap style for the graphics state.
241      *
242      * @param style The new line cap style to set.
243      */

244     public void setLineCapStyle( int style )
245     {
246         graphicsState.setInt( LC, style );
247     }
248
249     /**
250      * This will get the line join style.
251      *
252      * @return null or the LJ value in the dictionary.
253      */

254     public int getLineJoinStyle()
255     {
256         return graphicsState.getInt( LJ );
257     }
258
259     /**
260      * This will set the line join style.
261      *
262      * @param style The new line join style.
263      */

264     public void setLineJoinStyle( int style )
265     {
266         graphicsState.setInt( LJ, style );
267     }
268
269
270     /**
271      * This will get the miter limit.
272      *
273      * @return null or the ML value in the dictionary.
274      */

275     public Float JavaDoc getMiterLimit()
276     {
277         return getFloatItem( ML );
278     }
279
280     /**
281      * This will set the miter limit for the graphics state.
282      *
283      * @param miterLimit The new miter limit value
284      */

285     public void setMiterLimit( Float JavaDoc miterLimit )
286     {
287         setFloatItem( ML, miterLimit );
288     }
289
290     /**
291      * This will get the dash pattern.
292      *
293      * @return null or the D value in the dictionary.
294      */

295     public PDLineDashPattern getLineDashPattern()
296     {
297         PDLineDashPattern retval = null;
298         COSArray dp = (COSArray)graphicsState.getDictionaryObject( D );
299         if( dp != null )
300         {
301             retval = new PDLineDashPattern( dp );
302         }
303         return retval;
304     }
305
306     /**
307      * This will set the dash pattern for the graphics state.
308      *
309      * @param dashPattern The dash pattern
310      */

311     public void setLineDashPattern( PDLineDashPattern dashPattern )
312     {
313         graphicsState.setItem( D, dashPattern.getCOSObject() );
314     }
315
316     /**
317      * This will get the rendering intent.
318      *
319      * @return null or the RI value in the dictionary.
320      */

321     public String JavaDoc getRenderingIntent()
322     {
323         return graphicsState.getNameAsString( "RI" );
324     }
325
326     /**
327      * This will set the rendering intent for the graphics state.
328      *
329      * @param ri The new rendering intent
330      */

331     public void setRenderingIntent( String JavaDoc ri )
332     {
333         graphicsState.setName( "RI", ri );
334     }
335
336     /**
337      * This will get the overprint control.
338      *
339      * @return The overprint control or null if one has not been set.
340      */

341     public boolean getStrokingOverprintControl()
342     {
343         return graphicsState.getBoolean( OP, false );
344     }
345
346     /**
347      * This will get the overprint control(OP).
348      *
349      * @param op The overprint control.
350      */

351     public void setStrokingOverprintControl( boolean op )
352     {
353         graphicsState.setBoolean( OP, op );
354     }
355
356     /**
357      * This will get the overprint control for non stroking operations. If this
358      * value is null then the regular overprint control value will be returned.
359      *
360      * @return The overprint control or null if one has not been set.
361      */

362     public boolean getNonStrokingOverprintControl()
363     {
364         return graphicsState.getBoolean( OP_NS, getStrokingOverprintControl() );
365     }
366
367     /**
368      * This will get the overprint control(OP).
369      *
370      * @param op The overprint control.
371      */

372     public void setNonStrokingOverprintControl( boolean op )
373     {
374         graphicsState.setBoolean( OP_NS, op );
375     }
376
377     /**
378      * This will get the overprint control mode.
379      *
380      * @return The overprint control mode or null if one has not been set.
381      */

382     public Float JavaDoc getOverprintMode()
383     {
384         return getFloatItem( OPM );
385     }
386
387     /**
388      * This will get the overprint mode(OPM).
389      *
390      * @param overprintMode The overprint mode
391      */

392     public void setOverprintMode( Float JavaDoc overprintMode )
393     {
394         setFloatItem( OPM, overprintMode );
395     }
396
397     /**
398      * This will get the font setting of the graphics state.
399      *
400      * @return The font setting.
401      */

402     public PDFontSetting getFontSetting()
403     {
404         PDFontSetting setting = null;
405         COSArray font = (COSArray)graphicsState.getDictionaryObject( FONT );
406         if( font != null )
407         {
408             setting = new PDFontSetting( font );
409         }
410         return setting;
411     }
412
413     /**
414      * This will set the font setting for this graphics state.
415      *
416      * @param fs The new font setting.
417      */

418     public void setFontSetting( PDFontSetting fs )
419     {
420         graphicsState.setItem( FONT, fs );
421     }
422
423     /**
424      * This will get the flatness tolerance.
425      *
426      * @return The flatness tolerance or null if one has not been set.
427      */

428     public Float JavaDoc getFlatnessTolerance()
429     {
430         return getFloatItem( FL );
431     }
432
433     /**
434      * This will get the flatness tolerance.
435      *
436      * @param flatness The new flatness tolerance
437      */

438     public void setFlatnessTolerance( Float JavaDoc flatness )
439     {
440         setFloatItem( FL, flatness );
441     }
442
443     /**
444      * This will get the smothness tolerance.
445      *
446      * @return The smothness tolerance or null if one has not been set.
447      */

448     public Float JavaDoc getSmoothnessTolerance()
449     {
450         return getFloatItem( SM );
451     }
452
453     /**
454      * This will get the smoothness tolerance.
455      *
456      * @param smoothness The new smoothness tolerance
457      */

458     public void setSmoothnessTolerance( Float JavaDoc smoothness )
459     {
460         setFloatItem( SM, smoothness );
461     }
462
463     /**
464      * This will get the automatic stroke adjustment flag.
465      *
466      * @return The automatic stroke adjustment flag or null if one has not been set.
467      */

468     public boolean getAutomaticStrokeAdjustment()
469     {
470         return graphicsState.getBoolean( SA,false );
471     }
472
473     /**
474      * This will get the automatic stroke adjustment flag.
475      *
476      * @param sa The new automatic stroke adjustment flag.
477      */

478     public void setAutomaticStrokeAdjustment( boolean sa )
479     {
480         graphicsState.setBoolean( SA, sa );
481     }
482
483     /**
484      * This will get the stroking alpha constant.
485      *
486      * @return The stroking alpha constant or null if one has not been set.
487      */

488     public Float JavaDoc getStrokingAlpaConstant()
489     {
490         return getFloatItem( CA );
491     }
492
493     /**
494      * This will get the stroking alpha constant.
495      *
496      * @param alpha The new stroking alpha constant.
497      */

498     public void setStrokingAlphaConstant( Float JavaDoc alpha )
499     {
500         setFloatItem( CA, alpha );
501     }
502
503     /**
504      * This will get the non stroking alpha constant.
505      *
506      * @return The non stroking alpha constant or null if one has not been set.
507      */

508     public Float JavaDoc getNonStrokingAlpaConstant()
509     {
510         return getFloatItem( CA_NS );
511     }
512
513     /**
514      * This will get the non stroking alpha constant.
515      *
516      * @param alpha The new non stroking alpha constant.
517      */

518     public void setNonStrokingAlphaConstant( Float JavaDoc alpha )
519     {
520         setFloatItem( CA_NS, alpha );
521     }
522
523     /**
524      * This will get the alpha source flag.
525      *
526      * @return The alpha source flag.
527      */

528     public boolean getAlphaSourceFlag()
529     {
530         return graphicsState.getBoolean( AIS, false );
531     }
532
533     /**
534      * This will get the alpha source flag.
535      *
536      * @param alpha The alpha source flag.
537      */

538     public void setAlphaSourceFlag( boolean alpha )
539     {
540         graphicsState.setBoolean( AIS, alpha );
541     }
542
543     /**
544      * This will get the text knockout flag.
545      *
546      * @return The text knockout flag.
547      */

548     public boolean getTextKnockoutFlag()
549     {
550         return graphicsState.getBoolean( TK,true );
551     }
552
553     /**
554      * This will get the text knockout flag.
555      *
556      * @param tk The text knockout flag.
557      */

558     public void setTextKnockoutFlag( boolean tk )
559     {
560         graphicsState.setBoolean( TK, tk );
561     }
562
563     /**
564      * This will get a float item from the dictionary.
565      *
566      * @param key The key to the item.
567      *
568      * @return The value for that item.
569      */

570     private Float JavaDoc getFloatItem( COSName key )
571     {
572         Float JavaDoc retval = null;
573         COSNumber value = (COSNumber)graphicsState.getDictionaryObject( key );
574         if( value != null )
575         {
576             retval = new Float JavaDoc( value.floatValue() );
577         }
578         return retval;
579     }
580
581     /**
582      * This will set a float object.
583      *
584      * @param key The key to the data that we are setting.
585      * @param value The value that we are setting.
586      */

587     private void setFloatItem( COSName key, Float JavaDoc value )
588     {
589         if( value == null )
590         {
591             graphicsState.removeItem( key );
592         }
593         else
594         {
595             graphicsState.setItem( key, new COSFloat( value.floatValue() ) );
596         }
597     }
598 }
Popular Tags