Fonts

Is it possible to use the methods that are in the rather important FontMetrics class without having a Graphics available? Is the nearest possible way of achieving this to create a headless-device and get a dummy Graphics from it?

Mostly, I’m interested in fixing the “feature” of various swing classes (like JTree) that practically ignore things like “setRowHeight”. However, there are so very very very many occasions when I need to fit text to an area that I’d like to make a generic class for it (I wish it were in the java.awt.* - if I’m missing something, please let me know! I’ve looked and looked and never found :frowning: ).

Dynamic font-resizing can be extremely slow (especially on linux - big one-time penalty to get some font-cache somewhere filled) but you can get decent performance with standard tricks, like exponential “homing-in” on an approximately correct answer, erring on the side of being too small.

…and I have the age-old stupid stupid stupid problem with Swing that it’s impossible to setSize() etc my components until they have a Graphics, but by the time I’m allowed access to a Graphics its too late to setSize() my components (unless I put in apparently nonsensical code (think of the poor guy who has to try and maintain my code!) just to relayout the Swing hierarchy on the first time a call is made to the paintComponent method with a non-null Graphics which ALSO has a width and height greater than 0 because Sun’s JDK likes to pass in a fake Graphics every now and then with 0 dimensions but otherwise apparently valid).

Phew! Just like the “good ole days” of AWT, where almost everything is broken and you have to override every method you can just to get the darned windowing system to work how it should have done in the first place if they’d thought about what they were doing more!

(apologies for ranting; anyone else trying to work with Fonts in a similar way is bound to have these problems too and I’d like to spare them the weeks of frustration that it takes to discover all this through trial and error :frowning: ).

I just created myself a BufferedImage of 1x1 pixel and got its Graphics2D and then keep it hanging around as long as I like. I create the FontRenderContext on it too and set antialiasing on. Didn’t seem to hurt. (Of course, all I do with it is blit the whole font out and steal all the glyph metrics ahead of time)

Cas :slight_smile:

…which AFAICS is just about the same as using a headless’s Graphics, but less hassle but less elegant :slight_smile: ?

FWIW, FontRenderContext doesn’t work on Linux without a Graphics to come from (although this may be my fault). The docs imply that it SHOULD work, but that it will be “inaccurate” because e.g. fractional font metrics calcs will be slightly inaccurate.

In practice, it seems to just arbitrarily returns “1” for the width of all text, and other brain-dead stuff.

I’ve not tried very hard to get it to work, since it’s giving me deja-vu of trying to create Image’s without any GUI code in 1.1.x - when I finally achieved success, I just crashed the VM every time, because without a GUI there’s no native callback for preparing valid mem etc, IIRC. Had to wait rather a lot of years for the headless displays instead :).

[quote]…which AFAICS is just about the same as using a headless’s Graphics, but less hassle but less elegant :slight_smile: ?
[/quote]
…but works, so thanks anyway :).