Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3dfb99b0297b28cc1f853d6e8655fdef6495bcaa (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
/*****************************************************************************
 * Copyright (c) 2018 EclipseSource 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:
 *   EclipseSource - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.ui.fonts.internal.addon;

import java.io.IOException;
import java.net.URL;

import javax.annotation.PostConstruct;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.papyrus.infra.ui.fonts.Activator;
import org.eclipse.swt.widgets.Display;

/**
 * <p>
 * An E4 Addon to load additional fonts in SWT. This is required to make sure we
 * have at least one common Font available on all platforms, that we can use as
 * the default for Papyrus.
 * </p>
 */
public class FontAddon {

	@PostConstruct
	public void loadFonts() {
		if (Display.getCurrent() == null) {
			Display.getDefault().asyncExec(this::loadFonts);
			return;
		}

		loadFonts(new String[] { //
				"roboto/Roboto-Regular.ttf", //$NON-NLS-1$
				"roboto/Roboto-Italic.ttf", //$NON-NLS-1$
				"roboto/Roboto-Bold.ttf", //$NON-NLS-1$
				"roboto/Roboto-BoldItalic.ttf", //$NON-NLS-1$
		});
	}

	private void loadFonts(String[] fonts) {
		try {
			URL fontFolder = new URL("platform:/plugin/" + Activator.PLUGIN_ID + "/fonts/"); //$NON-NLS-1$ //$NON-NLS-2$
			for (String font : fonts) {
				URL fontURL = new URL(fontFolder, font);
				URL fontFileURL = FileLocator.toFileURL(fontURL);

				if (fontFileURL == null) {
					Activator.getDefault().getLog()
							.log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, "Unable to find font " + fontURL)); //$NON-NLS-1$
					continue;
				}

				if (!Display.getCurrent().loadFont(fontFileURL.getFile())) {
					Activator.getDefault().getLog().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID,
							"Failed to load font " + fontURL + " (Resolved URL: " + fontFileURL + ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
					continue;
				}
			}
		} catch (IOException ex) {
			Activator.getDefault().getLog()
					.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to load default fonts", ex)); //$NON-NLS-1$
			return;
		}
	}

}

Back to the top