calculate FontSize

Hello Java Fans,
my question is, if there is an method or something equals, to calculate a FontSize to draw a text which must fit to a given Width or Height?

For example, if we have a area which is 200px width and we have a text like “This is a text” which we will draw with the font (yx) and these text should fit in the area with 200px width.

I haven’t found something in FontMetrics but maybe there a other solutions?
The first witch i think is the bisection method to set a Size and get the Width witch FontMetrics’ getStringWidth…, but i think this is very inefficient.

thx and sorry for my bad English

The problem here is that scaling a font in points doesn’t necessarily equal scaling it by the same factor in pixels. Maybe when printing, but definitely not on a screen. So there is no real solution other than to converge to a solution using FontMetrics.stringWidth

thx for fast reply.
i programm the method now with the bisektion method. I know this is not the best solution but thats why i asks first ^^.
here is the code (not efficient if using a loop!!!).
if other ideas…tell me ^^


private void getFontSize(Graphics g,int xres,float size){
        boolean ende=false;
        float calcSize=32;
        FontMetrics metrics;
        do{
            font=font.deriveFont(calcSize);
            metrics = g.getFontMetrics(font);
            if((metrics.stringWidth(Text)>xres*(size-0.01f))&&(metrics.stringWidth(Text)<xres*(size+0.01f))){
                ende=true;
            }else{
                if(metrics.stringWidth(Text)>xres*size){
                    calcSize=calcSize-calcSize/2;
                }else{
                    calcSize=calcSize+calcSize/2;
                }
            }
        }while(ende==false);
    }

Font font and
String Text are global
size is the “percentually” size from xres to fits the Text

You are calling stringWidth three times, on the same font-metrics object, with the same parameters. Simply store the result in a variable.

Further, don’t use the ‘ende’ variable. The line ‘ende=true;’ should be ‘break;’

Also, you might get stuck in an infinite loop, as the string width might never be between xres*(size-0.01) and +0.01. So if stringWidth returns exactly the same value as the previous call, you can break from the loop.

I do something like:


FontMetrics fontMetrics = g.getFontMetrics(); // of the currently set graphics font.
int width = fontMetrics.stringWidth("hello world");
int height = fontMetrics.getHeight();

Cool, and how does that answer the question?

;D Don’t you just love it when people don’t read the question, and just assume something. Alright, here’s an idea:

You know the targetWidth and targetHeight. You also know the textWidth and textHeight. From it you could calculate some scaling factor for a new font instance.

getStringBounds and dervieFont are good methods for doing this stuff.

The thing is, as explained in my first reply (… :)) that scaling in font-size is not the same as scaling in pixel-size. Hence you can make some rough guesses, but the last few steps must be calculated through a ‘binary search’.

I wonder if this will scale in pixel-size.

Depends on whether you enabled anti-aliasing for text. And even if you do, I think it’s highly unlikely that the characters themselves that at floating point offsets in the ‘regular range’ (8-12pt).