KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > font > PDFontDescriptorDictionary


1 /**
2  * Copyright (c) 2004, 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.font;
32
33 import org.pdfbox.cos.COSArray;
34 import org.pdfbox.cos.COSBase;
35 import org.pdfbox.cos.COSDictionary;
36 import org.pdfbox.cos.COSName;
37 import org.pdfbox.cos.COSString;
38 import org.pdfbox.cos.COSStream;
39
40 import org.pdfbox.pdmodel.common.COSObjectable;
41 import org.pdfbox.pdmodel.common.PDRectangle;
42 import org.pdfbox.pdmodel.common.PDStream;
43
44 /**
45  * This class represents an implementation to the font descriptor that gets its
46  * information from a COS Dictionary.
47  *
48  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
49  * @version $Revision: 1.4 $
50  */

51 public class PDFontDescriptorDictionary extends PDFontDescriptor implements COSObjectable
52 {
53     private COSDictionary dic;
54
55     /**
56      * Constructor.
57      */

58     public PDFontDescriptorDictionary()
59     {
60         dic = new COSDictionary();
61         dic.setName( "Type", "FontDescriptor" );
62     }
63     
64     /**
65      * Constructor.
66      *
67      * @param desc The wrapped COS Dictionary.
68      */

69     public PDFontDescriptorDictionary( COSDictionary desc )
70     {
71         dic = desc;
72     }
73
74     /**
75      * This will get the dictionary for this object.
76      *
77      * @return The COS dictionary.
78      */

79     public COSDictionary getCOSDictionary()
80     {
81         return dic;
82     }
83     
84     /**
85      * Convert this standard java object to a COS object.
86      *
87      * @return The cos object that matches this Java object.
88      */

89     public COSBase getCOSObject()
90     {
91         return dic;
92     }
93
94     /**
95      * Get the font name.
96      *
97      * @return The name of the font.
98      */

99     public String JavaDoc getFontName()
100     {
101         String JavaDoc retval = null;
102         COSName name = (COSName)dic.getDictionaryObject( COSName.getPDFName( "FontName" ) );
103         if( name != null )
104         {
105             retval = name.getName();
106         }
107         return retval;
108     }
109
110     /**
111      * This will set the font name.
112      *
113      * @param fontName The new name for the font.
114      */

115     public void setFontName( String JavaDoc fontName )
116     {
117         COSName name = null;
118         if( fontName != null )
119         {
120             name = COSName.getPDFName( fontName );
121         }
122         dic.setItem( COSName.getPDFName( "FontName" ), name );
123     }
124
125     /**
126      * A string representing the preferred font family.
127      *
128      * @return The font family.
129      */

130     public String JavaDoc getFontFamily()
131     {
132         String JavaDoc retval = null;
133         COSString name = (COSString)dic.getDictionaryObject( COSName.getPDFName( "FontFamily" ) );
134         if( name != null )
135         {
136             retval = name.getString();
137         }
138         return retval;
139     }
140
141     /**
142      * This will set the font family.
143      *
144      * @param fontFamily The font family.
145      */

146     public void setFontFamily( String JavaDoc fontFamily )
147     {
148         COSString name = null;
149         if( fontFamily != null )
150         {
151             name = new COSString( fontFamily );
152         }
153         dic.setItem( COSName.getPDFName( "FontFamily" ), name );
154     }
155
156     /**
157      * The weight of the font. According to the PDF spec "possible values are
158      * 100, 200, 300, 400, 500, 600, 700, 800 or 900" Where a higher number is
159      * more weight and appears to be more bold.
160      *
161      * @return The font weight.
162      */

163     public float getFontWeight()
164     {
165         return dic.getFloat( "FontWeight",0 );
166     }
167
168     /**
169      * Set the weight of the font.
170      *
171      * @param fontWeight The new weight of the font.
172      */

173     public void setFontWeight( float fontWeight )
174     {
175         dic.setFloat( "FontWeight", fontWeight );
176     }
177
178     /**
179      * A string representing the preferred font stretch.
180      * According to the PDF Spec:
181      * The font stretch value; it must be one of the following (ordered from
182      * narrowest to widest): UltraCondensed, ExtraCondensed, Condensed, SemiCondensed,
183      * Normal, SemiExpanded, Expanded, ExtraExpanded or UltraExpanded.
184      *
185      * @return The stretch of the font.
186      */

187     public String JavaDoc getFontStretch()
188     {
189         String JavaDoc retval = null;
190         COSName name = (COSName)dic.getDictionaryObject( COSName.getPDFName( "FontStretch" ) );
191         if( name != null )
192         {
193             retval = name.getName();
194         }
195         return retval;
196     }
197
198     /**
199      * This will set the font stretch.
200      *
201      * @param fontStretch The new stretch for the font.
202      */

203     public void setFontStretch( String JavaDoc fontStretch )
204     {
205         COSName name = null;
206         if( fontStretch != null )
207         {
208             name = COSName.getPDFName( fontStretch );
209         }
210         dic.setItem( COSName.getPDFName( "FontStretch" ), name );
211     }
212
213     /**
214      * This will get the font flags.
215      *
216      * @return The font flags.
217      */

218     public int getFlags()
219     {
220         return dic.getInt( "Flags", 0 );
221     }
222
223     /**
224      * This will set the font flags.
225      *
226      * @param flags The new font flags.
227      */

228     public void setFlags( int flags )
229     {
230         dic.setInt( "Flags", flags );
231     }
232
233     /**
234      * This will get the fonts bouding box.
235      *
236      * @return The fonts bouding box.
237      */

238     public PDRectangle getFontBoundingBox()
239     {
240         COSArray rect = (COSArray)dic.getDictionaryObject( COSName.getPDFName( "FontBBox" ) );
241         PDRectangle retval = null;
242         if( rect != null )
243         {
244             retval = new PDRectangle( rect );
245         }
246         return retval;
247     }
248
249     /**
250      * Set the fonts bounding box.
251      *
252      * @param rect The new bouding box.
253      */

254     public void setFontBoundingBox( PDRectangle rect )
255     {
256         COSArray array = null;
257         if( rect != null )
258         {
259             array = rect.getCOSArray();
260         }
261         dic.setItem( COSName.getPDFName( "FontBBox" ), array );
262     }
263
264     /**
265      * This will get the italic angle for the font.
266      *
267      * @return The italic angle.
268      */

269     public float getItalicAngle()
270     {
271         return dic.getFloat( "ItalicAngle", 0 );
272     }
273
274     /**
275      * This will set the italic angle for the font.
276      *
277      * @param angle The new italic angle for the font.
278      */

279     public void setItalicAngle( float angle )
280     {
281         dic.setFloat( "ItalicAngle", angle );
282     }
283
284     /**
285      * This will get the ascent for the font.
286      *
287      * @return The ascent.
288      */

289     public float getAscent()
290     {
291         return dic.getFloat( "Ascent", 0 );
292     }
293
294     /**
295      * This will set the ascent for the font.
296      *
297      * @param ascent The new ascent for the font.
298      */

299     public void setAscent( float ascent )
300     {
301         dic.setFloat( "Ascent", ascent );
302     }
303
304     /**
305      * This will get the descent for the font.
306      *
307      * @return The descent.
308      */

309     public float getDescent()
310     {
311         return dic.getFloat( "Descent", 0 );
312     }
313
314     /**
315      * This will set the descent for the font.
316      *
317      * @param descent The new descent for the font.
318      */

319     public void setDescent( float descent )
320     {
321         dic.setFloat( "Descent", descent );
322     }
323
324     /**
325      * This will get the leading for the font.
326      *
327      * @return The leading.
328      */

329     public float getLeading()
330     {
331         return dic.getFloat( "Leading", 0 );
332     }
333
334     /**
335      * This will set the leading for the font.
336      *
337      * @param leading The new leading for the font.
338      */

339     public void setLeading( float leading )
340     {
341         dic.setFloat( "Leading", leading );
342     }
343
344     /**
345      * This will get the CapHeight for the font.
346      *
347      * @return The cap height.
348      */

349     public float getCapHeight()
350     {
351         return dic.getFloat( "CapHeight", 0 );
352     }
353
354     /**
355      * This will set the cap height for the font.
356      *
357      * @param capHeight The new cap height for the font.
358      */

359     public void setCapHeight( float capHeight )
360     {
361         dic.setFloat( "CapHeight", capHeight );
362     }
363
364     /**
365      * This will get the x height for the font.
366      *
367      * @return The x height.
368      */

369     public float getXHeight()
370     {
371         return dic.getFloat( "XHeight", 0 );
372     }
373
374     /**
375      * This will set the x height for the font.
376      *
377      * @param xHeight The new x height for the font.
378      */

379     public void setXHeight( float xHeight )
380     {
381         dic.setFloat( "XHeight", xHeight );
382     }
383
384     /**
385      * This will get the stemV for the font.
386      *
387      * @return The stem v value.
388      */

389     public float getStemV()
390     {
391         return dic.getFloat( "StemV", 0 );
392     }
393
394     /**
395      * This will set the stem V for the font.
396      *
397      * @param stemV The new stem v for the font.
398      */

399     public void setStemV( float stemV )
400     {
401         dic.setFloat( "StemV", stemV );
402     }
403
404     /**
405      * This will get the stemH for the font.
406      *
407      * @return The stem h value.
408      */

409     public float getStemH()
410     {
411         return dic.getFloat( "StemH", 0 );
412     }
413
414     /**
415      * This will set the stem H for the font.
416      *
417      * @param stemH The new stem h for the font.
418      */

419     public void setStemH( float stemH )
420     {
421         dic.setFloat( "StemH", stemH );
422     }
423
424     /**
425      * This will get the average width for the font.
426      *
427      * @return The average width value.
428      */

429     public float getAverageWidth()
430     {
431         return dic.getFloat( "AvgWidth", 0 );
432     }
433
434     /**
435      * This will set the average width for the font.
436      *
437      * @param averageWidth The new average width for the font.
438      */

439     public void setAverageWidth( float averageWidth )
440     {
441         dic.setFloat( "AvgWidth", averageWidth );
442     }
443
444     /**
445      * This will get the max width for the font.
446      *
447      * @return The max width value.
448      */

449     public float getMaxWidth()
450     {
451         return dic.getFloat( "MaxWidth", 0 );
452     }
453
454     /**
455      * This will set the max width for the font.
456      *
457      * @param maxWidth The new max width for the font.
458      */

459     public void setMaxWidth( float maxWidth )
460     {
461         dic.setFloat( "MaxWidth", maxWidth );
462     }
463
464     /**
465      * This will get the missing width for the font.
466      *
467      * @return The missing width value.
468      */

469     public float getMissingWidth()
470     {
471         return dic.getFloat( "MissingWidth", 0 );
472     }
473
474     /**
475      * This will set the missing width for the font.
476      *
477      * @param missingWidth The new missing width for the font.
478      */

479     public void setMissingWidth( float missingWidth )
480     {
481         dic.setFloat( "MissingWidth", missingWidth );
482     }
483
484     /**
485      * This will get the character set for the font.
486      *
487      * @return The character set value.
488      */

489     public String JavaDoc getCharSet()
490     {
491         String JavaDoc retval = null;
492         COSString name = (COSString)dic.getDictionaryObject( COSName.getPDFName( "CharSet" ) );
493         if( name != null )
494         {
495             retval = name.getString();
496         }
497         return retval;
498     }
499
500     /**
501      * This will set the character set for the font.
502      *
503      * @param charSet The new character set for the font.
504      */

505     public void setCharacterSet( String JavaDoc charSet )
506     {
507         COSString name = null;
508         if( charSet != null )
509         {
510             name = new COSString( charSet );
511         }
512         dic.setItem( COSName.getPDFName( "CharSet" ), name );
513     }
514     
515     /**
516      * A stream containing a Type 1 font program.
517      *
518      * @return A stream containing a Type 1 font program.
519      */

520     public PDStream getFontFile()
521     {
522         PDStream retval = null;
523         COSStream stream = (COSStream)dic.getDictionaryObject( "FontFile" );
524         if( stream != null )
525         {
526             retval = new PDStream( stream );
527         }
528         return retval;
529     }
530     
531     /**
532      * Set the type 1 font program.
533      *
534      * @param type1Stream The type 1 stream.
535      */

536     public void setFontFile( PDStream type1Stream )
537     {
538         dic.setItem( "FontFile", type1Stream );
539     }
540     
541     /**
542      * A stream containing a true type font program.
543      *
544      * @return A stream containing a true type font program.
545      */

546     public PDStream getFontFile2()
547     {
548         PDStream retval = null;
549         COSStream stream = (COSStream)dic.getDictionaryObject( "FontFile2" );
550         if( stream != null )
551         {
552             retval = new PDStream( stream );
553         }
554         return retval;
555     }
556     
557     /**
558      * Set the true type font program.
559      *
560      * @param ttfStream The true type stream.
561      */

562     public void setFontFile2( PDStream ttfStream )
563     {
564         dic.setItem( "FontFile2", ttfStream );
565     }
566     
567     /**
568      * A stream containing a font program that is not true type or type 1.
569      *
570      * @return A stream containing a font program.
571      */

572     public PDStream getFontFile3()
573     {
574         PDStream retval = null;
575         COSStream stream = (COSStream)dic.getDictionaryObject( "FontFile3" );
576         if( stream != null )
577         {
578             retval = new PDStream( stream );
579         }
580         return retval;
581     }
582     
583     /**
584      * Set a stream containing a font program that is not true type or type 1.
585      *
586      * @param stream The font program stream.
587      */

588     public void setFontFile3( PDStream stream )
589     {
590         dic.setItem( "FontFile3", stream );
591     }
592 }
Popular Tags