]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/utils/FontHelper.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / utils / FontHelper.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 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.g2d.utils;\r
13 \r
14 import java.awt.Font;\r
15 import java.awt.Toolkit;\r
16 \r
17 import org.eclipse.jface.resource.FontRegistry;\r
18 import org.eclipse.swt.graphics.FontData;\r
19 import org.eclipse.ui.PlatformUI;\r
20 \r
21 public final class FontHelper {\r
22 \r
23     /**\r
24      * @return\r
25      * @throws IllegalStateException if there's no eclipse workbench context\r
26      *         available\r
27      */\r
28     public static FontRegistry getCurrentThemeFontRegistry() {\r
29         return PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getFontRegistry();\r
30     }\r
31 \r
32     public static java.awt.Font toAwt(FontRegistry registry, String themeFontId) {\r
33         FontData fd = registry.getFontData(themeFontId)[0];\r
34         return toAwt(fd);\r
35     }\r
36 \r
37     public static java.awt.Font toAwt(FontData fd) {\r
38         int resolution = Toolkit.getDefaultToolkit().getScreenResolution();\r
39         int awtFontSize = (int) Math.round((double) fd.getHeight() * resolution / 72.0);\r
40         // The style constants for SWT and AWT map exactly, and since they are int constants, they should\r
41         // never change. So, the SWT style is passed through as the AWT style.\r
42         Font font = new java.awt.Font(fd.getName(), fd.getStyle(), awtFontSize);\r
43 //        System.out.println("awt font: " + font);\r
44         return font;\r
45     }\r
46 \r
47 }\r