【问题标题】:How to load font not installed on system如何加载系统上未安装的字体
【发布时间】:2018-06-21 16:48:15
【问题描述】:

我想从一个 Eclipse 插件修改默认编辑器文本字体,我通过以下方式管理 tp: WorkbenchPlugin.getDefault().getPreferenceStore().put(JFaceResources.TEXT_FONT, "dummy font values");

问题是我要设置的字体(Source Code Pro)没有安装在系统中,也不应该安装。

我从here 下载了字体,但我没有找到从下载的文件中加载字体并将其设置为JFaceResources.TEXT_FONT 首选项条目的值的方法。

org.eclipse.swt.graphics.Device 有一个方法 loadFont(String path) 看起来很有希望,但 Display.getCurrent() 在我的插件中返回 null

如何从文件中加载字体?
加载后如何在插件中设置字体?

【问题讨论】:

  • Display.getCurrent 只能在 UI 线程中工作 - 它在任何其他线程中返回 null,
  • @greg-449 后来调试的时候发现,忘了把代码放在runSync( new Runnable() {});里。我将添加解决方案,也许它会对某人有用

标签: eclipse eclipse-plugin swt


【解决方案1】:

试试下面的 sn-p。它适用于大多数情况。我通过参考 SO question 链接得到了这个 sn-p ---> Load a font in RCP

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

【讨论】:

  • 很遗憾,它不起作用。 BundleUtility.find() 返回格式为 bundleentry://24.fwk24119573/fonts/SourceCodePro-BoldIt.ttf 的值。当loadForm 被调用时,它会调用OS.AddFontResourceEx 返回false
  • 我添加了一些更正,为我解决了问题
猜你喜欢
  • 2013-04-08
  • 2017-08-02
  • 1970-01-01
  • 2019-01-14
  • 2017-04-19
  • 1970-01-01
  • 2017-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多