KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > contrib > metrics > FontMetricsDumper


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18 package org.apache.poi.contrib.metrics;
19
20 import java.awt.*;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 public class FontMetricsDumper
26 {
27     public static void main( String JavaDoc[] args ) throws IOException JavaDoc
28     {
29
30         Properties JavaDoc props = new Properties JavaDoc();
31
32         Font[] allFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
33         for ( int i = 0; i < allFonts.length; i++ )
34         {
35             String JavaDoc fontName = allFonts[i].getFontName();
36
37             Font font = new Font(fontName, Font.BOLD, 10);
38             FontMetrics fontMetrics = Toolkit.getDefaultToolkit().getFontMetrics(font);
39             int fontHeight = fontMetrics.getHeight();
40
41             props.setProperty("font." + fontName + ".height", fontHeight+"");
42             StringBuffer JavaDoc characters = new StringBuffer JavaDoc();
43             for (char c = 'a'; c <= 'z'; c++)
44             {
45                 characters.append( c + ", " );
46             }
47             for (char c = 'A'; c <= 'Z'; c++)
48             {
49                 characters.append( c + ", " );
50             }
51             for (char c = '0'; c <= '9'; c++)
52             {
53                 characters.append( c + ", " );
54             }
55             StringBuffer JavaDoc widths = new StringBuffer JavaDoc();
56             for (char c = 'a'; c <= 'z'; c++)
57             {
58                 widths.append( fontMetrics.getWidths()[c] + ", " );
59             }
60             for (char c = 'A'; c <= 'Z'; c++)
61             {
62                 widths.append( fontMetrics.getWidths()[c] + ", " );
63             }
64             for (char c = '0'; c <= '9'; c++)
65             {
66                 widths.append( fontMetrics.getWidths()[c] + ", " );
67             }
68             props.setProperty("font." + fontName + ".characters", characters.toString());
69             props.setProperty("font." + fontName + ".widths", widths.toString());
70         }
71
72         FileOutputStream JavaDoc fileOut = new FileOutputStream JavaDoc("font_metrics.properties");
73         try
74         {
75             props.store(fileOut, "Font Metrics");
76         }
77         finally
78         {
79             fileOut.close();
80         }
81     }
82 }
83
Popular Tags