1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.microemu.device.swt;
21
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.graphics.Font;
24 import org.microemu.app.ui.swt.SwtDeviceComponent;
25
26 public class SwtSystemFont implements SwtFont {
27
28 private String name;
29
30 private String style;
31
32 private int size;
33
34 private boolean antialiasing;
35
36 private boolean initialized;
37
38 private Font font;
39
40 public SwtSystemFont(String name, String style, int size, boolean antialiasing) {
41 this.name = name;
42 this.style = style.toLowerCase();
43 this.size = size;
44 this.antialiasing = antialiasing;
45
46 this.initialized = false;
47 }
48
49 public void setAntialiasing(boolean antialiasing) {
50 if (this.antialiasing != antialiasing) {
51 this.antialiasing = antialiasing;
52 initialized = false;
53 }
54 }
55
56 public Font getFont() {
57 checkInitialized();
58
59 return font;
60 }
61
62 private synchronized void checkInitialized() {
63 if (!initialized) {
64 int swtStyle = 0;
65 if (style.indexOf("plain") != -1) {
66 swtStyle |= SWT.NORMAL;
67 }
68 if (style.indexOf("bold") != -1) {
69 swtStyle |= SWT.BOLD;
70 }
71 if (style.indexOf("italic") != -1) {
72 swtStyle |= SWT.ITALIC;
73 }
74 if (style.indexOf("underlined") != -1) {
75
76 }
77 font = SwtDeviceComponent.getFont(name, size, swtStyle, antialiasing);
78 initialized = true;
79 }
80 }
81
82 public int charWidth(char ch) {
83 return charsWidth(new char[] {ch}, 0, 1);
84 }
85
86 public int charsWidth(char[] ch, int offset, int length) {
87 checkInitialized();
88
89 return SwtDeviceComponent.stringWidth(font, new String(ch, offset, length));
90 }
91
92 public int getBaselinePosition() {
93 checkInitialized();
94
95 return SwtDeviceComponent.getFontMetrics(font).getAscent();
96 }
97
98 public int getHeight() {
99 checkInitialized();
100
101 return SwtDeviceComponent.getFontMetrics(font).getHeight();
102 }
103
104 public int stringWidth(String str) {
105 checkInitialized();
106
107 return SwtDeviceComponent.stringWidth(font, str);
108 }
109
110 }