physical fonts java?
-
02-06-2021 - |
题
I'm bundling ttf files in a jar file and intend to use them as physical fonts for rendering text.
All the sample codes i've seen over the internet are as follow:
InputStream is=Essai.class.getResourceAsStream(resourcePath);
Font f=Font.createFont(fontFormat, is);
I have two questions about this code:
First, a single font, say DejaVu, holds 4 different ttf files, 1 per style (regular, bold, italic and bold-italic); is-it enough to create a font from one single ttf (any one of the 4)?
-
Second, none of the codes i've seen closed the stream after creating the font, which kept me wondering was it intentional ? maybe the the created font (which will be registered in the local GraphicsEnvironment later) needs the stream to stay opened?
The javadoc of the createFont() method says: "This method does not close the InputStream."
So my second question is should i close the stream after creating the font, or keep it opened?
解决方案
- I don't believe loading one variant will load them all. But then, it does not matter. Java can make a
Font
variant with any combination of bold+italic. I'd only put the standardFont
in the Jar in the first place. - Close the stream. Many examples cut corners for the sake of brevity (though that is generally a bad idea).
不隶属于 StackOverflow