Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3638453726135e571597e25ca493dfdc52240b14 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*******************************************************************************
 * Copyright (c) 2004, 2008 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.provisional.commons.ui;

import java.lang.reflect.Field;

import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;

/**
 * @author Mik Kersten
 * @since 3.0
 */
public class CommonFonts {

	public static Font BOLD;

	public static Font ITALIC;

	public static Font BOLD_ITALIC;

	public static Font STRIKETHROUGH = null;

	public static boolean HAS_STRIKETHROUGH;

	static {
		if (Display.getCurrent() != null) {
			init();
		} else {
			Display.getDefault().asyncExec(new Runnable() {
				public void run() {
					init();
				}
			});
		}
	}

	private static void init() {
		BOLD = JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT);
		ITALIC = JFaceResources.getFontRegistry().getItalic(JFaceResources.DEFAULT_FONT);
		BOLD_ITALIC = new Font(Display.getCurrent(), getModifiedFontData(ITALIC.getFontData(), SWT.BOLD | SWT.ITALIC));

		Font defaultFont = JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT);
		FontData[] defaultData = defaultFont.getFontData();
		if (defaultData != null && defaultData.length == 1) {
			FontData data = new FontData(defaultData[0].getName(), defaultData[0].getHeight(),
					defaultData[0].getStyle());

			if ("win32".equals(SWT.getPlatform())) { //$NON-NLS-1$
				// NOTE: Windows only, for: data.data.lfStrikeOut = 1;
				try {
					Field dataField = data.getClass().getDeclaredField("data"); //$NON-NLS-1$
					Object dataObject = dataField.get(data);
					Class<?> clazz = dataObject.getClass().getSuperclass();
					Field strikeOutFiled = clazz.getDeclaredField("lfStrikeOut"); //$NON-NLS-1$
					strikeOutFiled.set(dataObject, (byte) 1);
					CommonFonts.STRIKETHROUGH = new Font(Display.getCurrent(), data);
				} catch (Throwable t) {
					// ignore
				}
			}
		}
		if (CommonFonts.STRIKETHROUGH == null) {
			CommonFonts.HAS_STRIKETHROUGH = false;
			CommonFonts.STRIKETHROUGH = defaultFont;
		} else {
			CommonFonts.HAS_STRIKETHROUGH = true;
		}
	}

	/**
	 * NOTE: disposal of JFaceResources fonts handled by registry.
	 */
	public static void dispose() {
		if (CommonFonts.STRIKETHROUGH != null && !CommonFonts.STRIKETHROUGH.isDisposed()) {
			CommonFonts.STRIKETHROUGH.dispose();
			CommonFonts.BOLD_ITALIC.dispose();
		}
	}

	/**
	 * Copied from {@link FontRegistry}
	 */
	private static FontData[] getModifiedFontData(FontData[] baseData, int style) {
		FontData[] styleData = new FontData[baseData.length];
		for (int i = 0; i < styleData.length; i++) {
			FontData base = baseData[i];
			styleData[i] = new FontData(base.getName(), base.getHeight(), base.getStyle() | style);
		}

		return styleData;
	}
}

Back to the top