1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.fonts;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.graphics.Device;
18 import org.eclipse.swt.graphics.FontData;
21 * @author Antti Villberg
25 public static java.awt.Font awt(FontDescriptor descriptor) {
26 return new Font(descriptor.getFamily(), descriptor.getStyle(), descriptor.getSize());
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()));
33 private static boolean hasMask(int test, int mask) {
34 return (test & mask) == mask;
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";
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";
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 + "'");
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 + "'");
68 * Returns SWT Font instance. Be aware that this is a native resource and
69 * must be disposed of properly.
74 * @see #swt(org.simantics.datatypes.literal.Font)
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));
81 * Returns JFace FontDescriptor instance.
84 * @return FontDescriptor for specified font
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));
91 * Returns SWT FontData instance.
94 * @return FontData for specified font
96 public static FontData swtFontData(org.simantics.datatypes.literal.Font font) {
97 return new FontData(font.family, font.height, swtStyle(font.style));
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);