]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.ui.fonts;\r
13 \r
14 import java.awt.Font;\r
15 \r
16 import org.eclipse.swt.SWT;\r
17 import org.eclipse.swt.graphics.Device;\r
18 import org.eclipse.swt.graphics.FontData;\r
19 \r
20 /**\r
21  * @author Antti Villberg\r
22  */\r
23 public class Fonts {\r
24 \r
25     public static java.awt.Font awt(FontDescriptor descriptor) {\r
26         return new Font(descriptor.getFamily(), descriptor.getStyle(), descriptor.getSize());\r
27     }\r
28     \r
29     public static org.simantics.datatypes.literal.Font fromAWT(Font f) {\r
30         return new org.simantics.datatypes.literal.Font(f.getFamily(), f.getSize(), fromAwtStyle(f.getStyle()));\r
31     }\r
32 \r
33     private static boolean hasMask(int test, int mask) {\r
34         return (test & mask) == mask;\r
35     }\r
36 \r
37     public static String fromSwtStyle(int swtStyle) {\r
38         if(hasMask(swtStyle, SWT.BOLD|SWT.ITALIC)) return "BoldItalic";\r
39         else if (hasMask(swtStyle, SWT.BOLD)) return "Bold";\r
40         else if (hasMask(swtStyle, SWT.ITALIC)) return "Italic";\r
41         return "Normal";\r
42     }\r
43 \r
44     public static String fromAwtStyle(int awtStyle) {\r
45         if(hasMask(awtStyle, Font.BOLD|Font.ITALIC)) return "BoldItalic";\r
46         else if (hasMask(awtStyle, Font.BOLD)) return "Bold";\r
47         else if (hasMask(awtStyle, Font.ITALIC)) return "Italic";\r
48         return "Normal";\r
49     }\r
50 \r
51     public static int swtStyle(String text) {\r
52         if("Normal".equals(text)) return SWT.NORMAL;\r
53         else if ("Bold".equals(text)) return SWT.BOLD;\r
54         else if ("Italic".equals(text)) return SWT.ITALIC;\r
55         else if ("BoldItalic".equals(text)) return SWT.BOLD|SWT.ITALIC;\r
56         else throw new RuntimeException("Illegal style '" + text + "'");\r
57     }\r
58     \r
59     public static int awtStyle(String text) {\r
60         if("Normal".equals(text)) return 0;\r
61         else if ("Bold".equals(text)) return Font.BOLD;\r
62         else if ("Italic".equals(text)) return Font.ITALIC;\r
63         else if ("BoldItalic".equals(text)) return Font.BOLD|Font.ITALIC;\r
64         else throw new RuntimeException("Illegal style '" + text + "'");\r
65     }\r
66 \r
67     /**\r
68      * Returns SWT Font instance. Be aware that this is a native resource and\r
69      * must be disposed of properly.\r
70      * \r
71      * @param device\r
72      * @param font\r
73      * @return\r
74      * @see #swt(org.simantics.datatypes.literal.Font)\r
75      */\r
76     public static org.eclipse.swt.graphics.Font swt(Device device, org.simantics.datatypes.literal.Font font) {\r
77         return new org.eclipse.swt.graphics.Font(device, font.family, font.height, swtStyle(font.style));\r
78     }\r
79 \r
80     /**\r
81      * Returns JFace FontDescriptor instance.\r
82      * \r
83      * @param font\r
84      * @return FontDescriptor for specified font\r
85      */\r
86     public static org.eclipse.jface.resource.FontDescriptor swt(org.simantics.datatypes.literal.Font font) {\r
87         return org.eclipse.jface.resource.FontDescriptor.createFrom(font.family, font.height, swtStyle(font.style));\r
88     }\r
89 \r
90     /**\r
91      * Returns SWT FontData instance.\r
92      * \r
93      * @param font\r
94      * @return FontData for specified font\r
95      */\r
96     public static FontData swtFontData(org.simantics.datatypes.literal.Font font) {\r
97         return new FontData(font.family, font.height, swtStyle(font.style));\r
98     }\r
99 \r
100     public static java.awt.Font awt(org.simantics.datatypes.literal.Font font) {\r
101         return new java.awt.Font(font.family, awtStyle(font.style), font.height);\r
102     }\r
103 \r
104 }\r