-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharSize.java
55 lines (43 loc) · 1.35 KB
/
CharSize.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.sap.clap.test;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.widgets.Display;
import com.sap.clap.util.DisposeUtil;
@SuppressWarnings("nls")
public class CharSize {
private final Display display;
private final Font font;
private final int charWidth;
private final int charHeight;
public CharSize(final Display display) {
this(display, null);
}
public CharSize(final Display display, final Font font) {
this.display = display;
this.font = font;
final TextLayout textLayout = createTextLayout(display, font, "X");
charWidth = textLayout.getBounds().width;
charHeight = textLayout.getBounds().height;
DisposeUtil.dispose(textLayout);
}
private TextLayout createTextLayout(final Display display, final Font font, final String text) {
final TextLayout textLayout = new TextLayout(display);
if (font != null) {
textLayout.setFont(font);
}
textLayout.setText(text);
return textLayout;
}
public int getWidth() {
return charWidth;
}
public int getHeight() {
return charHeight;
}
public int getWidth(final String text) {
final TextLayout textLayout = createTextLayout(display, font, text);
final int textWidth = textLayout.getBounds().width;
DisposeUtil.dispose(textLayout);
return textWidth;
}
}