Java:OTF字体的使用,检查特定字体是否存在
我正在开发一个 Java 程序,该程序检查 XML 文件中是否有某种字体中没有字形表示的字符(使用 Font.canDisplay()
)。
默认情况下,XML 文件应采用 utf-8 编码。 XML 代码中的实体在被 canDisplay
检查之前会被解析并转换为 char/int 值。
为了提供灵活性,该程序接受两个参数:
- XML 文件的文件名
- 字体名称
我想检查系统上是否存在指定的字体,因为否则使用 canDisplay< 没有多大意义/代码>。
据我了解,Java 6支持OTF字体。但是,当我使用 java.awt.* 中的 getAvailableFontFamilyNames() 时,它显然只列出了 TTF 字体。
我想知道是否有办法检查 OTF 字体(由其名称给出)是否存在。一般来说,如果在Windows上使用Java中的OTF字体是实用的。
这样做的目的:我们使用renderX的XEP将xsl-fo转换为PDF。我希望能够检测 xsl-fo 文件是否包含无法使用某种字体显示的字符。 XEP 不会发出警告,只是打印空格而不是字形。更糟糕的是,它似乎混淆了他对合理文本间距的计算,因此具有这些空白空间的行比当前合理的块大小更长或更短。
我知道有一些警告,例如我必须检查一个字符是否要显示为常规、斜体或粗体,然后检查该特定字体中的字形。但目前,一种更通用的方法来检查常规字体中是否存在字符的字形就足够了。
I am working on a Java program that checks XML files for characters that have no glyph representation in a certain font (with Font.canDisplay()
)
The XML files should be utf-8 encoded by default. Entities in the XML code are parsed and transformed to a char/int value before they get checked by canDisplay
.
In order to provide flexibility, the program accepts two parameters:
- The file name of the XML file
- A font name
I would like to check whether the specified font exists on the system, because otherwise there is not much sense in using canDisplay
.
As far as I understand, Java 6 supports OTF fonts. However, when I use getAvailableFontFamilyNames()
from java.awt.*, it apparently only lists TTF fonts.
I'd like to know whether there is a way to check whether a OTF font (given by it's name) exists. And in general, if using OTF fonts in Java on windows is practical.
The purpose of this: We use renderX's XEP to convert xsl-fo into PDFs. I would like to be able to detect whether the xsl-fo file contains characters that won't be displayable with a certain font. XEP doesn't give a warning and just prints empty spaces instead of the glyphs. What makes it worse is that it seems to confuse his computation of the spacing for justified text, so lines with those empty spaces are longer or shorter than the current justified block size.
I know that there are certain caveats, e.g. I would have to check whether a character is to be displayed regular, italic or bold, and then check for a glyph in that specific font face. But for now a more general approach that checks whether a glyph for a character exists at all in the regular font face would be enough.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也得到了 .OTF 字体。
因此,要么特定字体文件有问题,要么字体名称与文件名不同,要么必须向 getLocalGraphicsEnvironment 添加 Locale 参数。
对于带有变音符号的拉丁字符,用于检索基本字母代码 + 变音符号组合代码的 unicode 规范化(java 文本)可能会有所帮助。像这样的东西:
I get the .OTF fonts too.
So either the specific font file has an issue, the font name is different from the file name, or one has to add a Locale parameter to getLocalGraphicsEnvironment.
For latin characters with diacritics, a unicode normalization (java text) to retrieve basic letter code + diacritic combining code might help. Something like: