简体   繁体   中英

How to find out what chars can be rendered in PDF in the given font?

I have got a problem like in this question : Some characters can't be shown in the font I'm using ( Lato-Regular.ttf ). The font is gets loaded via

document = new PDDocument();
baseFont = PDType0Font.load(document, stream);

In a comment, I was told that type 0 fonts are long obsolete, however, the Javadoc of PDTrueTypeFont#load says

Simple fonts only support 256 characters. For Unicode support, use PDType0Font#load(PDDocument, File) instead.

which is what I did.


The problem happened with , which is actually a control character, so there's obviously no point in rendering it. The primary cause was a wrong charset used in an import.

The font seems to contain all characters I needed so far. However, there may be some proper characters missing in the font as the input is an arbitrary Unicode string.

The replacement of invalid chars is trivial, but I need to know which chars are valid. Initially, I thought, I could use

PDType0Font.hasGlyph(code);

but the code is some PDF-internal code, ie, completely useless when you have a String and no idea how to convert. There's a protected method encode , which "Encodes the given string for use in a PDF content stream" (whatever it means) and nothing else sounding like a conversion.

Is there a better way than this ugliness?

private boolean canRender(PDType0Font font, int codepoint) {
    try {
        font.getStringWidth(new String(Character.toChars(codepoint)));
        return true;
    } catch (final Exception e) {
        return false;
    }
}

The replacement of invalid chars is trivial, but I need to know which chars are valid.

Use Font.canDisplayUpto(text)

String content = "ĈĉĜĝĤĥĴĵŜŝŬŭ";
java.awt.Font font = java.awt.Font.createFont​(java.awt.TRUETYPE_FONT,
                                              inputStream);
int index = font.canDisplayUpTo​(content);
if (index != -1) {
    throw new IllegalStateException(
        String.format("Font does not contain U+%06X: %s",
             context.codePointAt(index),
             font.getFamily()));
}

For loading TRUETYPE_FONT and TYPE1_FONT are possible. Type0 might possibly not covered, but you mentioned True-Type compatibility.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM