如何加载逻辑字体物理字体? (制作 JComboBox 字体选择器)

发布于 2024-11-07 09:22:04 字数 1153 浏览 0 评论 0原文

此问题的后续问题: Swing 字体名称不匹配? (制作字体选择器,并尝试在 JComboBox 中显示默认系统字体)

看来有逻辑字体和物理字体。逻辑字体是: Serif、SansSerif、等宽字体、Dialog 和 DialogInput。

这些字体是动态的,它们各自的物理字体(它们在程序执行期间表示的字体)是在程序加载时决定的。

我需要访问这些逻辑字体的物理字体。

我的第一个想法是尝试加载这些文件: http://download.oracle.com/ javase/6/docs/technotes/guides/intl/fontconfig.html#loading

使用如下内容: http://www.rgagnon.com/javadetails/java-0434.html

public static Properties load(String propsName) throws Exception {
    Properties props = new Properties();
    URL url = ClassLoader.getSystemResource(propsName);
    props.load(url.openStream());
    return props;
}

然后从这些属性文件中获取物理字体。

但是,当我尝试使用第一个文件中的名称加载属性时,我刚刚收到 NullPointerExceptions(未找到它们,但我已检查并实际上在我的系统上找到了它们)。我不知道为什么我会得到这个,但无论如何,我不禁想一定有一种更简单的方法来做到这一点?

Followup question from this one:
Swing font names do not match? (Making a font chooser, and am trying to display the default system font in a JComboBox)

It appears there are logical and physical fonts. The logical fonts are:
Serif, SansSerif, Monospaced, Dialog, and DialogInput.

These fonts are dynamic, and their respective physical font (the font that they will represent during program execution) are decided when the program loads.

I need to access the physical font of these logical fonts.

My first idea was to try and load these files:
http://download.oracle.com/javase/6/docs/technotes/guides/intl/fontconfig.html#loading

by using something like this:
http://www.rgagnon.com/javadetails/java-0434.html

public static Properties load(String propsName) throws Exception {
    Properties props = new Properties();
    URL url = ClassLoader.getSystemResource(propsName);
    props.load(url.openStream());
    return props;
}

and then getting the physical fonts from these properties files.

However, I am just getting NullPointerExceptions when trying to load the properties using the names in the first file (they are not found, but I have checked and actually found them on my system). I don't know why I am getting this, but regardless of that, I can't help to think there must be an easier way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔戏命师 2024-11-14 09:22:04
public static Font getPhysicalFont(Font logicalFont) {
    for (int i=0; i<FontManager.getRegisteredFonts().length; i++) {
        Font2D font = FontManager.getRegisteredFonts()[i];
        if (font instanceof CompositeFont && font.getFontName(Locale.getDefault()).equals(logicalFont.getFontName())) {
            PhysicalFont physicalFont = ((CompositeFont) font).getSlotFont(0);
            return new Font(physicalFont.getFamilyName(Locale.getDefault()), physicalFont.getStyle(), logicalFont.getSize());
        }
    }
    return logicalFont;
}
public static Font getPhysicalFont(Font logicalFont) {
    for (int i=0; i<FontManager.getRegisteredFonts().length; i++) {
        Font2D font = FontManager.getRegisteredFonts()[i];
        if (font instanceof CompositeFont && font.getFontName(Locale.getDefault()).equals(logicalFont.getFontName())) {
            PhysicalFont physicalFont = ((CompositeFont) font).getSlotFont(0);
            return new Font(physicalFont.getFamilyName(Locale.getDefault()), physicalFont.getStyle(), logicalFont.getSize());
        }
    }
    return logicalFont;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文