如何加载逻辑字体物理字体? (制作JComboBox字体选择器) [英] How does one load a logical fonts physical font? (Making a JComboBox font chooser)

查看:226
本文介绍了如何加载逻辑字体物理字体? (制作JComboBox字体选择器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



看起来有逻辑和物理字体。逻辑字体是:
Serif,SansSerif,Monospaced,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属性load(String propsName)throws异常{
属性props = new Properties();
URL url = ClassLoader.getSystemResource(propsName);
props.load(url.openStream());
回报道具;
}

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

然而,当我尝试使用第一个文件中的名字加载属性(他们没有找到,但我已经检查,并且在我的系统上实际找到它们)时,我只是得到了NullPointerExceptions。我不知道为什么我得到这个,但不管如何,我不禁想到必须有一个更简单的方法来做到这一点? (int i = 0; i< FontManager.getRegisteredFonts()。length; i ++){public static Font Font getPhysicalFont(Font logicalFont){

Font2D font = FontManager.getRegisteredFonts()[i];
if(font instanceof CompositeFont&& font.getFontName(Locale.getDefault())。equals(logicalFont.getFontName())){
PhysicalFont physicalFont =((CompositeFont)font).getSlotFont( 0);
返回新的字体(physicalFont.getFamilyName(Locale.getDefault()),physicalFont.getStyle(),logicalFont.getSize());
}
}
return logicalFont;
}


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?

解决方案

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;
}

这篇关于如何加载逻辑字体物理字体? (制作JComboBox字体选择器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆