KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (c) 2003-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.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.COSNumber;
38
39 import org.pdfbox.pdmodel.common.COSObjectable;
40
41 import org.pdfbox.pdmodel.font.PDFont;
42 import org.pdfbox.pdmodel.font.PDFontFactory;
43
44 import java.io.IOException JavaDoc;
45
46 /**
47  * This class represents a font setting used for the graphics state. A font setting is a font and a
48  * font size. Maybe there is a better name for this?
49  *
50  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
51  * @version $Revision: 1.4 $
52  */

53 public class PDFontSetting implements COSObjectable
54 {
55     private COSArray fontSetting = null;
56
57     /**
58      * Creates a blank font setting, font will be null, size will be 1.
59      */

60     public PDFontSetting()
61     {
62         fontSetting = new COSArray();
63         fontSetting.add( null );
64         fontSetting.add( new COSFloat( 1 ) );
65     }
66
67     /**
68      * Constructs a font setting from an existing array.
69      *
70      * @param fs The new font setting value.
71      */

72     public PDFontSetting( COSArray fs )
73     {
74         fontSetting = fs;
75     }
76
77     /**
78      * {@inheritDoc}
79      */

80     public COSBase getCOSObject()
81     {
82         return fontSetting;
83     }
84
85     /**
86      * This will get the font for this font setting.
87      *
88      * @return The font for this setting of null if one was not found.
89      *
90      * @throws IOException If there is an error getting the font.
91      */

92     public PDFont getFont() throws IOException JavaDoc
93     {
94         PDFont retval = null;
95         COSBase font = fontSetting.get( 0 );
96         if( font instanceof COSDictionary )
97         {
98             retval = PDFontFactory.createFont( (COSDictionary)font );
99         }
100         return retval;
101     }
102
103     /**
104      * This will set the font for this font setting.
105      *
106      * @param font The new font.
107      */

108     public void setFont( PDFont font )
109     {
110         fontSetting.set( 0, font );
111     }
112
113     /**
114      * This will get the size of the font.
115      *
116      * @return The size of the font.
117      */

118     public float getFontSize()
119     {
120         COSNumber size = (COSNumber)fontSetting.get( 1 );
121         return size.floatValue();
122     }
123
124     /**
125      * This will set the size of the font.
126      *
127      * @param size The new size of the font.
128      */

129     public void setFontSize( float size )
130     {
131         fontSetting.set( 1, new COSFloat( size ) );
132     }
133 }
Popular Tags