Width and Height calculation with fonts

Hi all,

I have the following problem:

My applet displays a page of information using a fixed pitch font set at 12pt. My applet can dynamically alter the font size. However the cost of working out the max height/width of the page is costly and I would like to avoid having to recompute it. I know from the start (one of calculation) its width and height. From this information im wondering could I calculate its new height and width at say a font size of 13pt.

So initally the w=560 h=300 @ 12pt when I increase it by 1 i get w=640 h=325.

I would like to calculate this new h and w information.

I hope i have explained this clearly enough.

I would love your help pls!

Thanks

[731 byte] By [RcpAnimal] at [2007-9-26 16:32:40]
# 1
I also have the current fonts width and height and the new font size width and height if that helpsPls help me out here.
RcpAnimal at 2007-7-2 20:14:12 > top of Java-index,Archived Forums,Java Programming...
# 2

I'm confused about what you're trying to do.

Please explain more.

What do you mean by the max width/height of the page?

How are you currently calculating these values?

Also, what font are you using and is this fixed and widely available and platform independant? 2 different font's displayed at the same point size can be very different in pixel size. Also, the same font in the same point size could be a different size on different systems.

I worked on an application once that had to display different fonts at giveb pixel heights and it was a real pain.

lnoelstorr at 2007-7-2 20:14:12 > top of Java-index,Archived Forums,Java Programming...
# 3

bascially I have a system which draws information onto a canvas.

I actually calculate the width and height of the page based on object i recieve which contain width and height.

From these objects its possible to determine at the given font the exact size of the page being displayed. You can imagine the object describes a rectangle.

Now this calculation is costly since i have to check every object coming in (dont worry about this).

Now i know the exact height and width of the information needed to be displayed.

Monospaced/Courier font is fixed pitch and every JRE has to implement a subset of fonts.

So based on how big the page current is at a given font size (12) it must be possible to work out how big the page will be at 13pt. There must be some math I can use with the given info.

Heres an example of the data i have.

Current w:560 h:300

After increase :640 h:325 ****

FONT INFO unitWidth:8 before unitWidth:7

FONT INFO unitHeight:13 before unitHeight:12

So from this data it must be possible to work out ***

The function to work out the h and w is as follows. (although you dont actually need this but this gives me the 560 and 300 values)

private void calcDimensions(Rectangle rect)

{

boolean bDimensionChange = false;

if ((rect.x + rect.width) >= m_MaxWidth)

{

bDimensionChange = true;

m_MaxWidth = (rect.x + rect.width);

}

if ((rect.y + rect.height) >= m_MaxHeight)

{

bDimensionChange = true;

m_MaxHeight = (rect.y + rect.height);

}

if (bDimensionChange)

canvas.setupScroll();

}

RcpAnimal at 2007-7-2 20:14:12 > top of Java-index,Archived Forums,Java Programming...
# 4

I think I'm even more confused now.

The code snippet you have given that calculates the widths and heights you've given contains no reference to a font.

Where does the font size come into your current calculations of the height and width.

Also, I'm not too sure where font comes into it at all, you seem to want to display rectangles.

lnoelstorr at 2007-7-2 20:14:12 > top of Java-index,Archived Forums,Java Programming...
# 5

Yeah the font information is not important. How i get it is not important.

Im sent the rectangles from a server which actually contain text also. Just that they are bounded in a rectangle.

I imagine this is a problem I will have to sort out myself.

Im sure the calculation is in there somewhere.

Thanks

RcpAnimal at 2007-7-2 20:14:12 > top of Java-index,Archived Forums,Java Programming...
# 6

Are you looking for something like this?

Font f = new Font("Dialog", Font.BOLD, 12);

Button b = new Button("Testing");

b.setFont(f);

FontMetrics fm = b.getFontMetrics(f);

System.out.println(fm.stringWidth("Testing"));

Mark

mvantuyl at 2007-7-2 20:14:12 > top of Java-index,Archived Forums,Java Programming...
# 7
no.Thanks all though.
RcpAnimal at 2007-7-2 20:14:12 > top of Java-index,Archived Forums,Java Programming...
# 8

This will compute the ratio between the old pitch and the

new pitch and apply it to the width and height:int w = 560;

int h = 300;

int pitch = 12;

int newPitch = 13;

double ratio = (double)newPitch / (double)pitch;

w = (int)(w * ratio);

h = (int)(h * ratio);

System.out.println(w + " " + h);

Mark

mvantuyl at 2007-7-2 20:14:12 > top of Java-index,Archived Forums,Java Programming...
# 9
Thats fantastic.However I dont know why but the height is spot on everytime but the width is somewhat short.Im getting 606 on width based on 560 * ratio stuff.Is there something this isnt considering on the width?Thanks
RcpAnimal at 2007-7-2 20:14:12 > top of Java-index,Archived Forums,Java Programming...
# 10

The ratio between 12 and 13 is 1.0833333333333333.

When I multiply this by the width of 560 I get 606. If you

multiply the ratio by 1.05 you will get the same

(approximate) width you're looking for. Try this:int w = 560;

int h = 300;

int pitch = 12;

int newPitch = 13;

double ratio = (double)newPitch / (double)pitch;

w = (int)(w * (ratio * 1.05));

h = (int)(h * ratio);

System.out.println(ratio);

System.out.println(w + " " + h);

Mark

mvantuyl at 2007-7-2 20:14:12 > top of Java-index,Archived Forums,Java Programming...
# 11
Right however where are you getting 1.05 from?It is kinda working.
RcpAnimal at 2007-7-2 20:14:12 > top of Java-index,Archived Forums,Java Programming...
# 12

int w = 560;

int h = 300;

int pitch = 12;

int newPitch = 13;

double ratio = (double)newPitch / (double)pitch;

double fudgedRatio = (double)640 / (double)560;

System.out.println(fudgedRatio);

System.out.println(fudgedRatio - ratio);

w = (int)(w * (ratio * 1.05));

h = (int)(h * ratio);

System.out.println(w + " " + h);

fudgedRatio represents the ratio between the original

width and the width you are expecting. The difference

between the actual ratio between pitches and the

fudgedRatio is .059523809523809534. I just chose to

use the constant .05 instead of using the entire number.

Mark

mvantuyl at 2007-7-2 20:14:12 > top of Java-index,Archived Forums,Java Programming...