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