]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/fonts/Fonts.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / fonts / Fonts.java
diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/fonts/Fonts.java b/bundles/org.simantics.ui/src/org/simantics/ui/fonts/Fonts.java
new file mode 100644 (file)
index 0000000..c0276f5
--- /dev/null
@@ -0,0 +1,104 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.ui.fonts;\r
+\r
+import java.awt.Font;\r
+\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.graphics.Device;\r
+import org.eclipse.swt.graphics.FontData;\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ */\r
+public class Fonts {\r
+\r
+    public static java.awt.Font awt(FontDescriptor descriptor) {\r
+        return new Font(descriptor.getFamily(), descriptor.getStyle(), descriptor.getSize());\r
+    }\r
+    \r
+    public static org.simantics.datatypes.literal.Font fromAWT(Font f) {\r
+       return new org.simantics.datatypes.literal.Font(f.getFamily(), f.getSize(), fromAwtStyle(f.getStyle()));\r
+    }\r
+\r
+    private static boolean hasMask(int test, int mask) {\r
+        return (test & mask) == mask;\r
+    }\r
+\r
+    public static String fromSwtStyle(int swtStyle) {\r
+        if(hasMask(swtStyle, SWT.BOLD|SWT.ITALIC)) return "BoldItalic";\r
+        else if (hasMask(swtStyle, SWT.BOLD)) return "Bold";\r
+        else if (hasMask(swtStyle, SWT.ITALIC)) return "Italic";\r
+        return "Normal";\r
+    }\r
+\r
+    public static String fromAwtStyle(int awtStyle) {\r
+        if(hasMask(awtStyle, Font.BOLD|Font.ITALIC)) return "BoldItalic";\r
+        else if (hasMask(awtStyle, Font.BOLD)) return "Bold";\r
+        else if (hasMask(awtStyle, Font.ITALIC)) return "Italic";\r
+        return "Normal";\r
+    }\r
+\r
+    public static int swtStyle(String text) {\r
+       if("Normal".equals(text)) return SWT.NORMAL;\r
+       else if ("Bold".equals(text)) return SWT.BOLD;\r
+       else if ("Italic".equals(text)) return SWT.ITALIC;\r
+       else if ("BoldItalic".equals(text)) return SWT.BOLD|SWT.ITALIC;\r
+       else throw new RuntimeException("Illegal style '" + text + "'");\r
+    }\r
+    \r
+    public static int awtStyle(String text) {\r
+       if("Normal".equals(text)) return 0;\r
+       else if ("Bold".equals(text)) return Font.BOLD;\r
+       else if ("Italic".equals(text)) return Font.ITALIC;\r
+       else if ("BoldItalic".equals(text)) return Font.BOLD|Font.ITALIC;\r
+       else throw new RuntimeException("Illegal style '" + text + "'");\r
+    }\r
+\r
+    /**\r
+     * Returns SWT Font instance. Be aware that this is a native resource and\r
+     * must be disposed of properly.\r
+     * \r
+     * @param device\r
+     * @param font\r
+     * @return\r
+     * @see #swt(org.simantics.datatypes.literal.Font)\r
+     */\r
+    public static org.eclipse.swt.graphics.Font swt(Device device, org.simantics.datatypes.literal.Font font) {\r
+        return new org.eclipse.swt.graphics.Font(device, font.family, font.height, swtStyle(font.style));\r
+    }\r
+\r
+    /**\r
+     * Returns JFace FontDescriptor instance.\r
+     * \r
+     * @param font\r
+     * @return FontDescriptor for specified font\r
+     */\r
+    public static org.eclipse.jface.resource.FontDescriptor swt(org.simantics.datatypes.literal.Font font) {\r
+        return org.eclipse.jface.resource.FontDescriptor.createFrom(font.family, font.height, swtStyle(font.style));\r
+    }\r
+\r
+    /**\r
+     * Returns SWT FontData instance.\r
+     * \r
+     * @param font\r
+     * @return FontData for specified font\r
+     */\r
+    public static FontData swtFontData(org.simantics.datatypes.literal.Font font) {\r
+        return new FontData(font.family, font.height, swtStyle(font.style));\r
+    }\r
+\r
+    public static java.awt.Font awt(org.simantics.datatypes.literal.Font font) {\r
+        return new java.awt.Font(font.family, awtStyle(font.style), font.height);\r
+    }\r
+\r
+}\r