Java: 使用 deriveFont 不会改变字体大小
Java: using deriveFont does not change font size
我正在将自定义字体(来自 ttf 文件)加载到我的项目中,并使用 deriveFont(float f) 来更改大小。但是,实际上并未设置大小(停留在 1)。这是我的代码:
public static void main(String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
Font mont =
Font.createFont(
Font.TRUETYPE_FONT,
new File(System.getProperty("user.dir") + "/data/Montserrat-MediumItalic.ttf"))
.deriveFont(20f);
ge.registerFont(mont);
Arrays.stream(ge.getAllFonts())
.filter(font -> font.getFontName().contains("Mont"))
.forEach(font -> System.out.println(font.getFontName() + ", Size: " + font.getSize()));
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
}
输出:
Montserrat Medium Italic, Size: 1
注意:用 font.getSize2D
替换 font.getSize()
打印 1.0
.
新:使用解码:
我现在正在使用这个
Font test = Font.decode("Montserrat Medium Italic-ITALIC-20");
(固定 class 未加载)
更新 2:
这一行:
Font mont = Font.createFont(Font.ITALIC, new File(System.getProperty("user.dir") + "/data/Montserrat-MediumItalic.ttf"));
抛出 IllegalArgumentException: font format not recognized
我想我知道问题出在哪里了。当您调用 ge.registerFont(mont)
时,它确实如此,它注册 底层字体 ,而不修改字体对象。 deriveFont()
函数只是改变当前字体对象的状态,不能修改实际的字体。当你注册一个字体时,它注册的是1的大小。如果你打印所有其他字体的大小,你会发现它们也有默认值1。我不认为你可以注册一个字体自定义默认大小,或覆盖 Font.getFont()
的默认大小。当您使用 Font.getFont()
获取字体时,它将始终具有默认大小 12(来自 specification)。
如果您需要特殊格式的字体,我建议创建一个静态 class 变量:
Font MontMediumItalic_20;
然后在资源加载器或构造函数中加载一次字体,并对其应用所有修改。
或者,您也可以使用 Font.decode()
如果您需要任何帮助,请告诉我。
However, the size is not actually being set (stuck at 1).
这似乎不太可能。我在问题的评论中要求直接确认("What mont.getSize()
return?" -- 哎呀,多么糟糕的语法),但到目前为止你还没有回答。我有理由相信,如果您检查一下,您会发现 mont.getSize()
的计算结果符合您要求的大小。
对于您观察到的行为的另一种解释很容易获得。您正在使用GraphicsEnvironment.getAllFonts()
报告已注册的字体,但是根据its documentation,此方法
Returns an array containing a one-point size instance of all fonts
available in this GraphicsEnvironment.
(强调已添加。)
另一个答案,尤其是对它的评论表明,GraphicsEnvironment.getAllFonts()
返回的 Font
对象在其他方面也可能与传递给 [=16= 的相应 Font
实例不同].尽管据我所知这些变化没有记录,但它们与从 GE 获得的 Font
对象的预期用途一致,如 getAllFonts()
文档所述:
Typical usage would be to allow a user to select a particular font. Then, the application can size the font and set various font attributes by calling the deriveFont method on the chosen instance.
他们接着说
If a font in this GraphicsEnvironment has multiple programmable variations, only one instance of that Font is returned in the array, and other variations must be derived by the application.
我不确定 "multiple programmable variations" 意味着当您从另一个 Font
对象派生出另一个对象时可以修改的属性(那么什么字体 不会 有可编程的变化?),但很明显 getAllFonts()
不是一种机制来回读之前呈现给 GraphicsEnvironment.registerFont()
的确切 Font
对象。这些对象甚至可能不会原样保留。
另一方面,您不必负责预先注册您可能需要的所有不同字体变体,这或许会让您松一口气。
我正在将自定义字体(来自 ttf 文件)加载到我的项目中,并使用 deriveFont(float f) 来更改大小。但是,实际上并未设置大小(停留在 1)。这是我的代码:
public static void main(String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
Font mont =
Font.createFont(
Font.TRUETYPE_FONT,
new File(System.getProperty("user.dir") + "/data/Montserrat-MediumItalic.ttf"))
.deriveFont(20f);
ge.registerFont(mont);
Arrays.stream(ge.getAllFonts())
.filter(font -> font.getFontName().contains("Mont"))
.forEach(font -> System.out.println(font.getFontName() + ", Size: " + font.getSize()));
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
}
输出:
Montserrat Medium Italic, Size: 1
注意:用 font.getSize2D
替换 font.getSize()
打印 1.0
.
新:使用解码:
我现在正在使用这个
Font test = Font.decode("Montserrat Medium Italic-ITALIC-20");
(固定 class 未加载)
更新 2:
这一行:
Font mont = Font.createFont(Font.ITALIC, new File(System.getProperty("user.dir") + "/data/Montserrat-MediumItalic.ttf"));
抛出 IllegalArgumentException: font format not recognized
我想我知道问题出在哪里了。当您调用 ge.registerFont(mont)
时,它确实如此,它注册 底层字体 ,而不修改字体对象。 deriveFont()
函数只是改变当前字体对象的状态,不能修改实际的字体。当你注册一个字体时,它注册的是1的大小。如果你打印所有其他字体的大小,你会发现它们也有默认值1。我不认为你可以注册一个字体自定义默认大小,或覆盖 Font.getFont()
的默认大小。当您使用 Font.getFont()
获取字体时,它将始终具有默认大小 12(来自 specification)。
如果您需要特殊格式的字体,我建议创建一个静态 class 变量:
Font MontMediumItalic_20;
然后在资源加载器或构造函数中加载一次字体,并对其应用所有修改。
或者,您也可以使用 Font.decode()
如果您需要任何帮助,请告诉我。
However, the size is not actually being set (stuck at 1).
这似乎不太可能。我在问题的评论中要求直接确认("What mont.getSize()
return?" -- 哎呀,多么糟糕的语法),但到目前为止你还没有回答。我有理由相信,如果您检查一下,您会发现 mont.getSize()
的计算结果符合您要求的大小。
对于您观察到的行为的另一种解释很容易获得。您正在使用GraphicsEnvironment.getAllFonts()
报告已注册的字体,但是根据its documentation,此方法
Returns an array containing a one-point size instance of all fonts available in this GraphicsEnvironment.
(强调已添加。)
另一个答案,尤其是对它的评论表明,GraphicsEnvironment.getAllFonts()
返回的 Font
对象在其他方面也可能与传递给 [=16= 的相应 Font
实例不同].尽管据我所知这些变化没有记录,但它们与从 GE 获得的 Font
对象的预期用途一致,如 getAllFonts()
文档所述:
Typical usage would be to allow a user to select a particular font. Then, the application can size the font and set various font attributes by calling the deriveFont method on the chosen instance.
他们接着说
If a font in this GraphicsEnvironment has multiple programmable variations, only one instance of that Font is returned in the array, and other variations must be derived by the application.
我不确定 "multiple programmable variations" 意味着当您从另一个 Font
对象派生出另一个对象时可以修改的属性(那么什么字体 不会 有可编程的变化?),但很明显 getAllFonts()
不是一种机制来回读之前呈现给 GraphicsEnvironment.registerFont()
的确切 Font
对象。这些对象甚至可能不会原样保留。
另一方面,您不必负责预先注册您可能需要的所有不同字体变体,这或许会让您松一口气。