Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Font mono = new Font(parent.getDisplay(), "Mono", 10, SWT.NONE);
- private static Font loadMonospacedFont(Display display) {
- String jreHome = System.getProperty("java.home");
- File file = new File(jreHome, "/lib/fonts/LucidaTypewriterRegular.ttf");
- if (!file.exists()) {
- throw new IllegalStateException(file.toString());
- }
- if (!display.loadFont(file.toString())) {
- throw new IllegalStateException(file.toString());
- }
- final Font font = new Font(display, "Lucida Sans Typewriter", 10,
- SWT.NORMAL);
- display.addListener(SWT.Dispose, new Listener() {
- public void handleEvent(Event event) {
- font.dispose();
- }
- });
- return font;
- }
- public class Monotest {
- private static boolean isMonospace(GC gc) {
- final String wide = "wgh8";
- final String narrow = "1l;.";
- assert wide.length() == narrow.length();
- return gc.textExtent(wide).x == gc.textExtent(narrow).x;
- }
- private static void testFont(Display display, Font font) {
- Image image = new Image(display, 100, 100);
- try {
- GC gc = new GC(image);
- try {
- gc.setFont(font);
- System.out.println(isMonospace(gc) + "t"
- + font.getFontData()[0].getName());
- } finally {
- gc.dispose();
- }
- } finally {
- image.dispose();
- }
- }
- private static void walkFonts(Display display) {
- final boolean scalable = true;
- for (FontData fontData : display.getFontList(null, scalable)) {
- Font font = new Font(display, fontData);
- try {
- testFont(display, font);
- } finally {
- font.dispose();
- }
- }
- }
- public static void main(String[] args) {
- Display display = new Display();
- try {
- walkFonts(display);
- } finally {
- display.dispose();
- }
- }
- }
Add Comment
Please, Sign In to add comment