1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.foTools;
13
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import org.apache.fop.fonts.FontType;
18 import org.eclipse.draw2d.FigureUtilities;
19 import org.eclipse.draw2d.geometry.Dimension;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.graphics.Font;
22 import org.eclipse.swt.graphics.FontData;
23 import org.eclipse.swt.graphics.FontMetrics;
24
25 /***
26 * Description of SWT font of given name(family name), style and weight for
27 * different sizes.
28 */
29 public class SWTFontMetrics implements org.apache.fop.fonts.FontMetrics {
30
31 private static final float FF = 750.0f * 1000.0f;
32
33 private String fontName;
34
35 private int fontStyle;
36
37 private SWTFontRepository repository;
38
39 /***
40 * Cach for created font metrics for concrete sizes.
41 */
42 private Map<Integer, FontMetrics> sizeMetricsMap = new HashMap<Integer, FontMetrics>();
43
44 /***
45 * Constructor.
46 *
47 * @param fontData
48 * @param repository
49 */
50 public SWTFontMetrics(FontData fontData, SWTFontRepository repository) {
51 this.repository = repository;
52 this.fontName = fontData.getName();
53 this.fontStyle = fontData.getStyle();
54 }
55
56 public SWTFontMetrics(String fontName, int fontStyle,
57 SWTFontRepository repository) {
58 this.repository = repository;
59 this.fontName = fontName;
60 this.fontStyle = fontStyle;
61 }
62
63 protected int convert(float f) {
64 return Math.round(f + 0.5f);
65 }
66
67 protected int convertInput(int size) {
68 float dpi = 96.0f;
69 float result = size;
70
71 result /= 1000.0f;
72
73
74
75 return Math.round(result);
76 }
77
78 public int getAscender(int size) {
79 size = convertInput(size);
80
81 return convert((getSWTFontMetrics(size).getHeight()
82 - getSWTFontMetrics(size).getDescent()) * FF);
83 }
84
85 public int getCapHeight(int size) {
86 return getXHeight(size);
87 }
88
89 /***
90 * Returns SWT metrics of font of concrete size. Results are cached.
91 *
92 * @param size
93 * @return
94 */
95 private FontMetrics getSWTFontMetrics(int size) {
96
97 FontMetrics fontMetrics = this.sizeMetricsMap.get(new Integer(
98 size));
99 if (fontMetrics != null)
100 return fontMetrics;
101
102
103 fontMetrics = FigureUtilities.getFontMetrics(repository
104 .getFont(fontName, size, fontStyle));
105 this.sizeMetricsMap.put(new Integer(size), fontMetrics);
106 return fontMetrics;
107
108 }
109
110 public int getDescender(int size) {
111 size = convertInput(size);
112
113 return Math.round(-1 * getSWTFontMetrics(size).getDescent()
114 * FF);
115 }
116
117 public String getFontName() {
118 return fontName;
119 }
120
121 public FontType getFontType() {
122 return FontType.TRUETYPE;
123 }
124
125 public Map getKerningInfo() {
126 return null;
127 }
128
129 public int getWidth(int i, int size) {
130 size = convertInput(size);
131
132 StringBuffer buffer = new StringBuffer(100);
133 for (int count = 0; count < 100; count++) {
134 buffer.append((char) i);
135 }
136
137 float w = FigureUtilities.getStringExtents(buffer.toString(),
138 getFont(size)).width;
139
140
141
142 return convert((w / 100.0f) * FF);
143 }
144
145 public int[] getWidths() {
146 return null;
147 }
148
149 public int getXHeight(int size) {
150 size = convertInput(size);
151
152 Dimension d = FigureUtilities.getStringExtents("X",
153 getFont(size));
154 return convert(d.height * FF);
155 }
156
157
158
159
160
161
162 public boolean hasKerningInfo() {
163 return false;
164 }
165
166 /***
167 * Returns SWT font of this metric and given size.
168 *
169 * @param size
170 * size(height) of font
171 */
172 public Font getFont(int size) {
173 return this.repository.getFont(this.fontName, size,
174 this.fontStyle);
175 }
176
177 /***
178 * Converts SWT style to FOP font style.
179 *
180 * @param SWTStyle
181 * to convert
182 * @return
183 */
184 public static String convertSWTFontStyleToFopFontStyle(int SWTStyle) {
185 switch (SWTStyle) {
186 case SWT.NORMAL:
187 return "normal";
188 case SWT.BOLD:
189 return "bold";
190 case SWT.ITALIC:
191 return "italic";
192 }
193
194 return "normal";
195 }
196
197 }