Question

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?
Was it helpful?

Solution

  1. 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 standard Font in the Jar in the first place.
  2. Close the stream. Many examples cut corners for the sake of brevity (though that is generally a bad idea).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top