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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.ui.fonts;
\r
14 import java.awt.Font;
\r
16 import org.eclipse.swt.SWT;
\r
17 import org.eclipse.swt.graphics.Device;
\r
18 import org.eclipse.swt.graphics.FontData;
\r
21 * @author Antti Villberg
\r
23 public class Fonts {
\r
25 public static java.awt.Font awt(FontDescriptor descriptor) {
\r
26 return new Font(descriptor.getFamily(), descriptor.getStyle(), descriptor.getSize());
\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
33 private static boolean hasMask(int test, int mask) {
\r
34 return (test & mask) == mask;
\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
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
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
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
68 * Returns SWT Font instance. Be aware that this is a native resource and
\r
69 * must be disposed of properly.
\r
74 * @see #swt(org.simantics.datatypes.literal.Font)
\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
81 * Returns JFace FontDescriptor instance.
\r
84 * @return FontDescriptor for specified font
\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
91 * Returns SWT FontData instance.
\r
94 * @return FontData for specified font
\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
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