1
2 package org.freehep.graphics2d.font;
3
4 import java.awt.Font;
5 import java.awt.GraphicsEnvironment;
6 import java.awt.font.TextAttribute;
7 import java.io.IOException;
8 import java.util.Arrays;
9 import java.util.List;
10 import java.util.Properties;
11 import java.util.Hashtable;
12
13
14
15
16
17
18 public class FontUtilities {
19
20 private FontUtilities() {
21 }
22
23 public static List getAllAvailableFonts() {
24 return Arrays.asList(GraphicsEnvironment.getLocalGraphicsEnvironment()
25 .getAvailableFontFamilyNames());
26 }
27
28 private static final Properties windowsFonts = new Properties();
29 static {
30
31
32
33
34 String arial = "Arial";
35
36
37 windowsFonts.setProperty("Dialog", arial);
38 windowsFonts.setProperty("DialogInput", "Courier New");
39 windowsFonts.setProperty("Serif", "Times New Roman");
40 windowsFonts.setProperty("SansSerif", arial);
41 windowsFonts.setProperty("Monospaced", "Courier New");
42
43
44 windowsFonts.setProperty("Courier", "Courier New");
45 windowsFonts.setProperty("Helvetica", arial);
46 windowsFonts.setProperty("Times-Roman", "Times New Roman");
47 windowsFonts.setProperty("TimesRoman", "Times New Roman");
48 windowsFonts.setProperty("Times", "Times New Roman");
49 windowsFonts.setProperty("Symbol", "Arial Unicode MS");
50 windowsFonts.setProperty("ZapfDingbats", "Arial Unicode MS");
51 }
52
53 public static String getWindowsFontName(String fontName) {
54 return windowsFonts.getProperty(fontName, fontName);
55 }
56
57
58
59
60
61 public static String getEncodedString(String string, String tableName) {
62 return FontEncoder.getEncodedString(string, tableName);
63 }
64
65
66
67
68
69
70
71
72 public static String getEncodedString(String string, CharTable charTable) {
73 return FontEncoder.getEncodedString(string, charTable);
74 }
75
76 public interface ShowString {
77 public void showString(Font font, String string) throws IOException;
78 }
79
80 private static final CharTable STANDARD_CHAR_TABLES[] = { null,
81 Lookup.getInstance().getTable("Symbol"),
82 Lookup.getInstance().getTable("Zapfdingbats") };
83
84 private static final Font STANDARD_FONT[] = { null,
85 new Font("Symbol", Font.PLAIN, 10),
86 new Font("ZapfDingbats", Font.PLAIN, 10), };
87
88
89
90
91
92
93
94
95 public static void showString(Font font, String string,
96 CharTable latinTable, ShowString device) throws IOException {
97
98 if (latinTable == null) throw new RuntimeException("FontUtilities.showString(...): latinTable cannot be 'null'");
99
100 STANDARD_FONT[0] = font;
101 STANDARD_FONT[1] = new Font("Symbol", Font.PLAIN, font.getSize());
102 STANDARD_FONT[2] = new Font("ZapfDingbats", Font.PLAIN, font.getSize());
103 STANDARD_CHAR_TABLES[0] = latinTable;
104
105 char[] chars = string.toCharArray();
106 String out = "";
107 int lastTable = 0;
108
109 for (int i = 0; i < chars.length; i++) {
110
111
112
113 int table = lastTable;
114 char encoding = (char) STANDARD_CHAR_TABLES[table]
115 .toEncoding(chars[i]);
116
117 if (encoding == 0) {
118 table = -1;
119 do {
120 table++;
121 if (table != lastTable) {
122 encoding = (char) STANDARD_CHAR_TABLES[table]
123 .toEncoding(chars[i]);
124 }
125 } while ((encoding == 0)
126 && (table < STANDARD_CHAR_TABLES.length - 1));
127 }
128 if (encoding == 0)
129 table = lastTable;
130
131 if ((table != lastTable) && (!out.equals(""))) {
132
133 device.showString(STANDARD_FONT[lastTable], out);
134 out = "";
135 }
136
137 out += encoding;
138 lastTable = table;
139 }
140
141 device.showString(STANDARD_FONT[lastTable], out);
142 }
143
144
145
146
147
148
149
150
151
152
153 public static Hashtable getAttributes(Font font) {
154 Hashtable result = new Hashtable(7, (float)0.9);
155 result.put(
156 TextAttribute.TRANSFORM,
157 font.getTransform());
158 result.put(
159 TextAttribute.FAMILY,
160 font.getName());
161 result.put(
162 TextAttribute.SIZE,
163 new Float(font.getSize2D()));
164 result.put(
165 TextAttribute.WEIGHT,
166 (font.getStyle() & Font.BOLD) != 0 ?
167 TextAttribute.WEIGHT_BOLD :
168 TextAttribute.WEIGHT_REGULAR);
169 result.put(
170 TextAttribute.POSTURE,
171 (font.getStyle() & Font.ITALIC) != 0 ?
172 TextAttribute.POSTURE_OBLIQUE :
173 TextAttribute.POSTURE_REGULAR);
174 result.put(
175 TextAttribute.SUPERSCRIPT,
176 new Integer(0
177 result.put(
178 TextAttribute.WIDTH,
179 new Float(1
180 return result;
181 }
182 }