SWT Load Font in a RCP App

2019-07-15 09:00发布

Here's what I want to do:

Display.getCurrent().loadFont("fonts/helveticaNeueBold_iOS7.ttf")
  • Works in a tester (i.e. class with entry point).
  • Doesn't work in an RCP app.

How do the loading mechanisms differ? Should I get the ttf file, then extract the path to it?

2条回答
Lonely孤独者°
2楼-- · 2019-07-15 09:39

Alexander's answer could also be helpful to some.

For me, the following snippet did the trick:

final String path = "fonts/helveticaNeueBold_iOS7.ttf";
final URL pathUrl = BundleUtility.find(PLUGIN_ID, path);
final boolean isFontLoaded = Display.getCurrent().loadFont(pathUrl.toExternalForm());

Although beware that BundleUtility has restricted access.

查看更多
干净又极端
3楼-- · 2019-07-15 09:41

Eclipse bundles have different paths (something like "bundleentry://bundle_number/path_to_your_file"). You might want to use FileLocator to load files properly. For example:

Bundle bundle = Activator.getDefault().getBundle();
Path path = new Path("fonts/helveticaNeueBold_iOS7.ttf");
URL url = FileLocator.find(bundle, path, Collections.EMPTY_MAP);
URL fileUrl = null;
try {
fileUrl = FileLocator.toFileURL(url);
}
catch (IOException e) {
// Will happen if the file cannot be read for some reason
e.printStackTrace();
}
File file = new File(fileUrl.getPath());
boolean loadFont = Display.getCurrent().loadFont(file.toString());

Also, please check other methods, available within FileLocator.

查看更多
登录 后发表回答