KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > afmtypes > FontMetric


1 /**
2  * Copyright (c) 2003-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.afmtypes;
32
33 import java.io.IOException JavaDoc;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40
41 import org.pdfbox.util.BoundingBox;
42
43 /**
44  * This is the outermost AFM type. This can be created by the afmparser with a valid
45  * AFM document.
46  *
47  * @author Ben Litchfield (ben@csh.rit.edu)
48  * @version $Revision: 1.7 $
49  */

50 public class FontMetric
51 {
52     /**
53      * This is the version of the FontMetrics.
54      */

55     private float afmVersion;
56     private int metricSets = 0;
57     private String JavaDoc fontName;
58     private String JavaDoc fullName;
59     private String JavaDoc familyName;
60     private String JavaDoc weight;
61     private BoundingBox fontBBox;
62     private String JavaDoc fontVersion;
63     private String JavaDoc notice;
64     private String JavaDoc encodingScheme;
65     private int mappingScheme;
66     private int escChar;
67     private String JavaDoc characterSet;
68     private int characters;
69     private boolean isBaseFont;
70     private float[] vVector;
71     private boolean isFixedV;
72     private float capHeight;
73     private float xHeight;
74     private float ascender;
75     private float descender;
76     private List JavaDoc comments = new ArrayList JavaDoc();
77
78     private float underlinePosition;
79     private float underlineThickness;
80     private float italicAngle;
81     private float[] charWidth;
82     private boolean isFixedPitch;
83     private float standardHorizontalWidth;
84     private float standardVerticalWidth;
85
86     private List JavaDoc charMetrics = new ArrayList JavaDoc();
87     private Map JavaDoc charMetricsMap = new HashMap JavaDoc();
88     private List JavaDoc trackKern = new ArrayList JavaDoc();
89     private List JavaDoc composites = new ArrayList JavaDoc();
90     private List JavaDoc kernPairs = new ArrayList JavaDoc();
91     private List JavaDoc kernPairs0 = new ArrayList JavaDoc();
92     private List JavaDoc kernPairs1 = new ArrayList JavaDoc();
93
94     /**
95      * Constructor.
96      */

97     public FontMetric()
98     {
99     }
100
101     /**
102      * This will get the width of a character.
103      *
104      * @param name The character to get the width for.
105      *
106      * @return The width of the character.
107      *
108      * @throws IOException If this AFM file does not handle the character.
109      */

110     public float getCharacterWidth( String JavaDoc name ) throws IOException JavaDoc
111     {
112         float result = 0;
113         CharMetric metric = (CharMetric)charMetricsMap.get( name );
114         if( metric == null )
115         {
116             result=0;
117             //don't throw an exception right away.
118
//throw new IOException( "Unknown AFM(" + getFullName() + ") characer '" + name + "'" );
119
}
120         else
121         {
122             result = metric.getWx();
123         }
124         return result;
125     }
126
127     /**
128      * This will get the average width of a character.
129      *
130      * @return The width of the character.
131      *
132      * @throws IOException If this AFM file does not handle the character.
133      */

134     public float getAverageCharacterWidth() throws IOException JavaDoc
135     {
136         float average = 0;
137         float totalWidths = 0;
138         float characterCount = 0;
139         Iterator JavaDoc iter = charMetricsMap.values().iterator();
140         while( iter.hasNext() )
141         {
142             CharMetric metric = (CharMetric)iter.next();
143             if( metric.getWx() > 0 )
144             {
145                 totalWidths += metric.getWx();
146                 characterCount += 1;
147             }
148         }
149         if( totalWidths > 0 )
150         {
151             average = totalWidths / characterCount;
152         }
153
154         return average;
155     }
156
157     /**
158      * This will add a new comment.
159      *
160      * @param comment The comment to add to this metric.
161      */

162     public void addComment( String JavaDoc comment )
163     {
164         comments.add( comment );
165     }
166
167     /**
168      * This will get all comments.
169      *
170      * @return The list of all comments.
171      */

172     public List JavaDoc getComments()
173     {
174         return comments;
175     }
176
177     /**
178      * This will get the version of the AFM document.
179      *
180      * @return The version of the document.
181      */

182     public float getAFMVersion()
183     {
184         return afmVersion;
185     }
186
187     /**
188      * This will get the metricSets attribute.
189      *
190      * @return The value of the metric sets.
191      */

192     public int getMetricSets()
193     {
194         return metricSets;
195     }
196
197     /**
198      * This will set the version of the AFM document.
199      *
200      * @param afmVersionValue The version of the document.
201      */

202     public void setAFMVersion( float afmVersionValue )
203     {
204         afmVersion = afmVersionValue;
205     }
206
207     /**
208      * This will set the metricSets attribute. This value must be 0,1, or 2.
209      *
210      * @param metricSetsValue The new metric sets attribute.
211      */

212     public void setMetricSets( int metricSetsValue )
213     {
214         if( metricSetsValue < 0 || metricSetsValue > 2 )
215         {
216             throw new RuntimeException JavaDoc( "The metricSets attribute must be in the " +
217                                         "set {0,1,2} and not '" + metricSetsValue + "'" );
218         }
219         metricSets = metricSetsValue;
220     }
221
222     /**
223      * Getter for property fontName.
224      *
225      * @return Value of property fontName.
226      */

227     public String JavaDoc getFontName()
228     {
229         return fontName;
230     }
231
232     /**
233      * Setter for property fontName.
234      *
235      * @param name New value of property fontName.
236      */

237     public void setFontName(String JavaDoc name)
238     {
239         fontName = name;
240     }
241
242     /**
243      * Getter for property fullName.
244      *
245      * @return Value of property fullName.
246      */

247     public String JavaDoc getFullName()
248     {
249         return fullName;
250     }
251
252     /**
253      * Setter for property fullName.
254      *
255      * @param fullNameValue New value of property fullName.
256      */

257     public void setFullName(String JavaDoc fullNameValue)
258     {
259         fullName = fullNameValue;
260     }
261
262     /**
263      * Getter for property familyName.
264      *
265      * @return Value of property familyName.
266      */

267     public String JavaDoc getFamilyName()
268     {
269         return familyName;
270     }
271
272     /**
273      * Setter for property familyName.
274      *
275      * @param familyNameValue New value of property familyName.
276      */

277     public void setFamilyName(String JavaDoc familyNameValue)
278     {
279         familyName = familyNameValue;
280     }
281
282     /**
283      * Getter for property weight.
284      *
285      * @return Value of property weight.
286      */

287     public String JavaDoc getWeight()
288     {
289         return weight;
290     }
291
292     /**
293      * Setter for property weight.
294      *
295      * @param weightValue New value of property weight.
296      */

297     public void setWeight(String JavaDoc weightValue)
298     {
299         weight = weightValue;
300     }
301
302     /**
303      * Getter for property fontBBox.
304      *
305      * @return Value of property fontBBox.
306      */

307     public BoundingBox getFontBBox()
308     {
309         return fontBBox;
310     }
311
312     /**
313      * Setter for property fontBBox.
314      *
315      * @param bBox New value of property fontBBox.
316      */

317     public void setFontBBox(BoundingBox bBox)
318     {
319         this.fontBBox = bBox;
320     }
321
322     /**
323      * Getter for property notice.
324      *
325      * @return Value of property notice.
326      */

327     public String JavaDoc getNotice()
328     {
329         return notice;
330     }
331
332     /**
333      * Setter for property notice.
334      *
335      * @param noticeValue New value of property notice.
336      */

337     public void setNotice(String JavaDoc noticeValue)
338     {
339         notice = noticeValue;
340     }
341
342     /**
343      * Getter for property encodingScheme.
344      *
345      * @return Value of property encodingScheme.
346      */

347     public String JavaDoc getEncodingScheme()
348     {
349         return encodingScheme;
350     }
351
352     /**
353      * Setter for property encodingScheme.
354      *
355      * @param encodingSchemeValue New value of property encodingScheme.
356      */

357     public void setEncodingScheme(String JavaDoc encodingSchemeValue)
358     {
359         encodingScheme = encodingSchemeValue;
360     }
361
362     /**
363      * Getter for property mappingScheme.
364      *
365      * @return Value of property mappingScheme.
366      */

367     public int getMappingScheme()
368     {
369         return mappingScheme;
370     }
371
372     /**
373      * Setter for property mappingScheme.
374      *
375      * @param mappingSchemeValue New value of property mappingScheme.
376      */

377     public void setMappingScheme(int mappingSchemeValue)
378     {
379         mappingScheme = mappingSchemeValue;
380     }
381
382     /**
383      * Getter for property escChar.
384      *
385      * @return Value of property escChar.
386      */

387     public int getEscChar()
388     {
389         return escChar;
390     }
391
392     /**
393      * Setter for property escChar.
394      *
395      * @param escCharValue New value of property escChar.
396      */

397     public void setEscChar(int escCharValue)
398     {
399         escChar = escCharValue;
400     }
401
402     /**
403      * Getter for property characterSet.
404      *
405      * @return Value of property characterSet.
406      */

407     public String JavaDoc getCharacterSet()
408     {
409         return characterSet;
410     }
411
412     /**
413      * Setter for property characterSet.
414      *
415      * @param characterSetValue New value of property characterSet.
416      */

417     public void setCharacterSet(String JavaDoc characterSetValue)
418     {
419         characterSet = characterSetValue;
420     }
421
422     /**
423      * Getter for property characters.
424      *
425      * @return Value of property characters.
426      */

427     public int getCharacters()
428     {
429         return characters;
430     }
431
432     /**
433      * Setter for property characters.
434      *
435      * @param charactersValue New value of property characters.
436      */

437     public void setCharacters(int charactersValue)
438     {
439         characters = charactersValue;
440     }
441
442     /**
443      * Getter for property isBaseFont.
444      *
445      * @return Value of property isBaseFont.
446      */

447     public boolean isBaseFont()
448     {
449         return isBaseFont;
450     }
451
452     /**
453      * Setter for property isBaseFont.
454      *
455      * @param isBaseFontValue New value of property isBaseFont.
456      */

457     public void setIsBaseFont(boolean isBaseFontValue)
458     {
459         isBaseFont = isBaseFontValue;
460     }
461
462     /**
463      * Getter for property vVector.
464      *
465      * @return Value of property vVector.
466      */

467     public float[] getVVector()
468     {
469         return this.vVector;
470     }
471
472     /**
473      * Setter for property vVector.
474      *
475      * @param vVectorValue New value of property vVector.
476      */

477     public void setVVector(float[] vVectorValue)
478     {
479         vVector = vVectorValue;
480     }
481
482     /**
483      * Getter for property isFixedV.
484      *
485      * @return Value of property isFixedV.
486      */

487     public boolean isFixedV()
488     {
489         return isFixedV;
490     }
491
492     /**
493      * Setter for property isFixedV.
494      *
495      * @param isFixedVValue New value of property isFixedV.
496      */

497     public void setIsFixedV(boolean isFixedVValue)
498     {
499         isFixedV = isFixedVValue;
500     }
501
502     /**
503      * Getter for property capHeight.
504      *
505      * @return Value of property capHeight.
506      */

507     public float getCapHeight()
508     {
509         return capHeight;
510     }
511
512     /**
513      * Setter for property capHeight.
514      *
515      * @param capHeightValue New value of property capHeight.
516      */

517     public void setCapHeight(float capHeightValue)
518     {
519         capHeight = capHeightValue;
520     }
521
522     /**
523      * Getter for property xHeight.
524      *
525      * @return Value of property xHeight.
526      */

527     public float getXHeight()
528     {
529         return xHeight;
530     }
531
532     /**
533      * Setter for property xHeight.
534      *
535      * @param xHeightValue New value of property xHeight.
536      */

537     public void setXHeight( float xHeightValue )
538     {
539         xHeight = xHeightValue;
540     }
541
542     /**
543      * Getter for property ascender.
544      *
545      * @return Value of property ascender.
546      */

547     public float getAscender()
548     {
549         return ascender;
550     }
551
552     /**
553      * Setter for property ascender.
554      *
555      * @param ascenderValue New value of property ascender.
556      */

557     public void setAscender( float ascenderValue )
558     {
559         ascender = ascenderValue;
560     }
561
562     /**
563      * Getter for property descender.
564      *
565      * @return Value of property descender.
566      */

567     public float getDescender()
568     {
569         return descender;
570     }
571
572     /**
573      * Setter for property descender.
574      *
575      * @param descenderValue New value of property descender.
576      */

577     public void setDescender( float descenderValue )
578     {
579         descender = descenderValue;
580     }
581
582     /**
583      * Getter for property fontVersion.
584      *
585      * @return Value of property fontVersion.
586      */

587     public String JavaDoc getFontVersion()
588     {
589         return fontVersion;
590     }
591
592     /**
593      * Setter for property fontVersion.
594      *
595      * @param fontVersionValue New value of property fontVersion.
596      */

597     public void setFontVersion(String JavaDoc fontVersionValue)
598     {
599         fontVersion = fontVersionValue;
600     }
601
602     /**
603      * Getter for property underlinePosition.
604      *
605      * @return Value of property underlinePosition.
606      */

607     public float getUnderlinePosition()
608     {
609         return underlinePosition;
610     }
611
612     /**
613      * Setter for property underlinePosition.
614      *
615      * @param underlinePositionValue New value of property underlinePosition.
616      */

617     public void setUnderlinePosition(float underlinePositionValue)
618     {
619         underlinePosition = underlinePositionValue;
620     }
621
622     /**
623      * Getter for property underlineThickness.
624      *
625      * @return Value of property underlineThickness.
626      */

627     public float getUnderlineThickness()
628     {
629         return underlineThickness;
630     }
631
632     /**
633      * Setter for property underlineThickness.
634      *
635      * @param underlineThicknessValue New value of property underlineThickness.
636      */

637     public void setUnderlineThickness(float underlineThicknessValue)
638     {
639         underlineThickness = underlineThicknessValue;
640     }
641
642     /**
643      * Getter for property italicAngle.
644      *
645      * @return Value of property italicAngle.
646      */

647     public float getItalicAngle()
648     {
649         return italicAngle;
650     }
651
652     /**
653      * Setter for property italicAngle.
654      *
655      * @param italicAngleValue New value of property italicAngle.
656      */

657     public void setItalicAngle(float italicAngleValue)
658     {
659         italicAngle = italicAngleValue;
660     }
661
662     /**
663      * Getter for property charWidth.
664      *
665      * @return Value of property charWidth.
666      */

667     public float[] getCharWidth()
668     {
669         return this.charWidth;
670     }
671
672     /**
673      * Setter for property charWidth.
674      *
675      * @param charWidthValue New value of property charWidth.
676      */

677     public void setCharWidth(float[] charWidthValue)
678     {
679         charWidth = charWidthValue;
680     }
681
682     /**
683      * Getter for property isFixedPitch.
684      *
685      * @return Value of property isFixedPitch.
686      */

687     public boolean isFixedPitch()
688     {
689         return isFixedPitch;
690     }
691
692     /**
693      * Setter for property isFixedPitch.
694      *
695      * @param isFixedPitchValue New value of property isFixedPitch.
696      */

697     public void setFixedPitch(boolean isFixedPitchValue)
698     {
699         isFixedPitch = isFixedPitchValue;
700     }
701
702     /** Getter for property charMetrics.
703      * @return Value of property charMetrics.
704      */

705     public List JavaDoc getCharMetrics()
706     {
707         return charMetrics;
708     }
709
710     /** Setter for property charMetrics.
711      * @param charMetricsValue New value of property charMetrics.
712      */

713     public void setCharMetrics(List JavaDoc charMetricsValue)
714     {
715         charMetrics = charMetricsValue;
716     }
717
718     /**
719      * This will add another character metric.
720      *
721      * @param metric The character metric to add.
722      */

723     public void addCharMetric( CharMetric metric )
724     {
725         charMetrics.add( metric );
726         charMetricsMap.put( metric.getName(), metric );
727     }
728
729     /** Getter for property trackKern.
730      * @return Value of property trackKern.
731      */

732     public List JavaDoc getTrackKern()
733     {
734         return trackKern;
735     }
736
737     /** Setter for property trackKern.
738      * @param trackKernValue New value of property trackKern.
739      */

740     public void setTrackKern(List JavaDoc trackKernValue)
741     {
742         trackKern = trackKernValue;
743     }
744
745     /**
746      * This will add another track kern.
747      *
748      * @param kern The track kerning data.
749      */

750     public void addTrackKern( TrackKern kern )
751     {
752         trackKern.add( kern );
753     }
754
755     /** Getter for property composites.
756      * @return Value of property composites.
757      */

758     public List JavaDoc getComposites()
759     {
760         return composites;
761     }
762
763     /** Setter for property composites.
764      * @param compositesList New value of property composites.
765      */

766     public void setComposites(List JavaDoc compositesList)
767     {
768         composites = compositesList;
769     }
770
771     /**
772      * This will add a single composite part to the picture.
773      *
774      * @param composite The composite info to add.
775      */

776     public void addComposite( Composite composite )
777     {
778         composites.add( composite );
779     }
780
781     /** Getter for property kernPairs.
782      * @return Value of property kernPairs.
783      */

784     public java.util.List JavaDoc getKernPairs()
785     {
786         return kernPairs;
787     }
788
789     /**
790      * This will add a kern pair.
791      *
792      * @param kernPair The kern pair to add.
793      */

794     public void addKernPair( KernPair kernPair )
795     {
796         kernPairs.add( kernPair );
797     }
798
799     /** Setter for property kernPairs.
800      * @param kernPairsList New value of property kernPairs.
801      */

802     public void setKernPairs(java.util.List JavaDoc kernPairsList)
803     {
804         kernPairs = kernPairsList;
805     }
806
807     /** Getter for property kernPairs0.
808      * @return Value of property kernPairs0.
809      */

810     public java.util.List JavaDoc getKernPairs0()
811     {
812         return kernPairs0;
813     }
814
815     /**
816      * This will add a kern pair.
817      *
818      * @param kernPair The kern pair to add.
819      */

820     public void addKernPair0( KernPair kernPair )
821     {
822         kernPairs0.add( kernPair );
823     }
824
825     /** Setter for property kernPairs0.
826      * @param kernPairs0List New value of property kernPairs0.
827      */

828     public void setKernPairs0(java.util.List JavaDoc kernPairs0List)
829     {
830         kernPairs0 = kernPairs0List;
831     }
832
833     /** Getter for property kernPairs1.
834      * @return Value of property kernPairs1.
835      */

836     public java.util.List JavaDoc getKernPairs1()
837     {
838         return kernPairs1;
839     }
840
841     /**
842      * This will add a kern pair.
843      *
844      * @param kernPair The kern pair to add.
845      */

846     public void addKernPair1( KernPair kernPair )
847     {
848         kernPairs1.add( kernPair );
849     }
850
851     /** Setter for property kernPairs1.
852      * @param kernPairs1List New value of property kernPairs1.
853      */

854     public void setKernPairs1(java.util.List JavaDoc kernPairs1List)
855     {
856         kernPairs1 = kernPairs1List;
857     }
858
859     /** Getter for property standardHorizontalWidth.
860      * @return Value of property standardHorizontalWidth.
861      */

862     public float getStandardHorizontalWidth()
863     {
864         return standardHorizontalWidth;
865     }
866
867     /** Setter for property standardHorizontalWidth.
868      * @param standardHorizontalWidthValue New value of property standardHorizontalWidth.
869      */

870     public void setStandardHorizontalWidth(float standardHorizontalWidthValue)
871     {
872         standardHorizontalWidth = standardHorizontalWidthValue;
873     }
874
875     /** Getter for property standardVerticalWidth.
876      * @return Value of property standardVerticalWidth.
877      */

878     public float getStandardVerticalWidth()
879     {
880         return standardVerticalWidth;
881     }
882
883     /** Setter for property standardVerticalWidth.
884      * @param standardVerticalWidthValue New value of property standardVerticalWidth.
885      */

886     public void setStandardVerticalWidth(float standardVerticalWidthValue)
887     {
888         standardVerticalWidth = standardVerticalWidthValue;
889     }
890
891 }
Popular Tags