SWT Load Font in a RCP App

有些话、适合烂在心里 提交于 2019-12-11 02:04:38

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/19981868/swt-load-font-in-a-rcp-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!