开发者

Java: Usage of OTF fonts, checking if particular font exists

开发者 https://www.devze.com 2023-02-03 00:31 出处:网络
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())

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 i开发者_StackOverflow社区n 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.


I get the .OTF fonts too.

    GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
    for (String ffname : genv.getAvailableFontFamilyNames()) {
        System.out.println(ffname);
    }

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:

java.text.Normalizer.normalize(letter, java.text.Normalizer.Form.NFKD).replaceAll("\\p{M}", "");
0

精彩评论

暂无评论...
验证码 换一张
取 消