1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.foTools;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.apache.fop.fonts.FontTriplet;
18 import org.eclipse.swt.graphics.Device;
19 import org.eclipse.swt.graphics.Font;
20 import org.eclipse.swt.graphics.FontData;
21
22 /***
23 * Central storage for SWT fonts.
24 *
25 * @author Tomas Studva 27.jul 2006
26 */
27 public class SWTFontRepository {
28
29 /***
30 * Holds data about default SWT font.
31 */
32 private Font defaulFont;
33
34 /***
35 * Associated device. Can be printer or display device.
36 */
37 private Device device;
38
39 /***
40 * Holds font datas of all available fonts in system.
41 */
42 private FontData[] fontDatas;
43
44 private List<Font> fontCache = new ArrayList<Font>();
45
46 public SWTFontRepository(Device device) {
47 if (device == null)
48 throw new IllegalArgumentException();
49 this.device = device;
50
51
52 this.defaulFont = device.getSystemFont();
53
54
55 FontData[] f1 = this.device.getFontList(null, true);
56 FontData[] f2 = this.device.getFontList(null, false);
57 this.fontDatas = new FontData[f1.length + f2.length];
58 System.arraycopy(f1, 0, this.fontDatas, 0, f1.length);
59 System.arraycopy(f2, 0, this.fontDatas, f1.length, f2.length);
60 }
61
62 public SWTFontMetrics getMetrics(FontData fontData) {
63 return new SWTFontMetrics(fontData, this);
64 }
65
66 /***
67 * All OS significant resources (fonts, in this case) are returned to
68 * the underlying OS for destruction, caches are cleared. Repository can
69 * be further used, however fonts are going to be recreated when needed.
70 */
71 public void dispose() {
72
73 for (int i = 0; i < fontCache.size(); i++) {
74 Font f = fontCache.get(i);
75 f.dispose();
76 }
77 fontCache.clear();
78 }
79
80 /***
81 * Main accessor for font objects.
82 *
83 * @param fontName
84 * Full (and exact) name of the font.
85 * @param fontHeight
86 * Font's height(size) (in points = 1/72th 6f an inch)
87 * @return - Font object if desired font is creatable, default in other
88 * cases.
89 */
90
91 public Font getFont(String fontName, int fontHeight, int fontStyle) {
92
93 if (fontName == null || fontName.equals("")
94 || (fontHeight <= 0))
95 throw new IllegalArgumentException();
96
97 Font result = lookupInCache(fontName, fontHeight, fontStyle);
98
99 if (result == null) {
100 for (int i = 0; i < fontDatas.length; i++) {
101 if (fontDatas[i].getName().equals(fontName)) {
102 String name = fontDatas[i].getName();
103 result = new Font(this.device, name,
104 fontHeight, fontStyle);
105 fontCache.add(result);
106 return result;
107 }
108 }
109 }
110 if (result != null)
111 return result;
112
113 return getDefaultFont();
114
115
116 }
117
118 /***
119 * Returns font datas of all available fonts.
120 *
121 * @return all available font datas
122 */
123 public FontData[] getAvailableFontDatas() {
124 return this.fontDatas;
125 }
126
127 protected Font lookupInCache(String fontName, int fontHeight, int fontStyle) {
128 for (int i = 0; i < fontCache.size(); i++) {
129 Font f = fontCache.get(i);
130 FontData fd = f.getFontData()[0];
131 if (fd.getName().equals(fontName)
132 && (fd.getHeight() == fontHeight)
133 && (fd.getStyle() == fontStyle)) {
134 return f;
135 }
136 }
137 return null;
138 }
139
140 /***
141 * Default font getter.
142 *
143 * @return default font
144 */
145 public Font getDefaultFont() {
146 return this.defaulFont;
147 }
148
149 /***
150 * Default font data getter.
151 *
152 * @return default font data
153 */
154 public FontData getDefaultFontData() {
155 return this.defaulFont.getFontData()[0];
156 }
157
158 /***
159 * Computes font triplet decsriptor of font data.
160 *
161 * @param fontData
162 * @return
163 */
164 public static FontTriplet getFontTriplet(FontData fontData) {
165 return new FontTriplet(fontData.getName(), SWTFontMetrics
166 .convertSWTFontStyleToFopFontStyle(fontData
167 .getStyle()),
168 getSWTFontWeight(fontData));
169
170 }
171
172 /***
173 * Returns weight of SWT font data.
174 *
175 * @param fontData
176 * to compute weight from
177 * @return
178 */
179 public static int getSWTFontWeight(FontData fontData) {
180
181
182 return 400;
183 }
184
185 }