1

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?
1
  • thanks for taking the time to answer. I don't agree with your edit, i need the second question to stay tagged under the second element of the list, because ... it's the second question :-) Apr 22, 2012 at 16:30

1 Answer 1

3
  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).
1
  • ok no problem, i'll keep that in mind :) "I don't believe loading one variant will load them all.", that's true, i've just tested it. I also tested if variants created by java look the same as the ones defined in ttf files, and figured out that they don't. The variants loaded from the corresponding ttfs look much "sexier"! :P. Now the problem is that they are getting registered with the same name in the GraphicsEnvironment, which means that i can only use one of them properly with the Font constructor. But any way i'll find a way to manage it. Thanks again for the answer :) i'll mark it solved Apr 22, 2012 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.