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