INTRODUCTION AND RELEVANT INFORMATION:
I am a software developer that needs to implement printing in my application. In my application user can choose different paper sizes ( A3, A4, A5 ...) which requires from my application to scale drawing accordingly.
I have managed to solve most of the tasks except adjusting the font height. This is the place where I got stuck.
I have asked for help in StackOveflow but got no satisfying answer. Since all I need is formula for scaling font height, I have decided to ask for help here.
Since this is mathematical site, I will phrase my question in a way that does not require any programming knowledge ( all I ask for is to carefully read the question, since this will be hard for me ). If I need to clarify something or add more info please leave a comment and I will update my post.
PROBLEM:
I have coordinates of the rectangle in which text should be drawn.
Unfortunately, I must pick random value for font before drawing the text inside ( there is no way around it ), instead of calculating proper font size. All I can do now is to calculate the rectangle this text ( with this font height ) will fit in.
Below image shows what I mean:
- I have font height of the text in proposed rectangle;
- I have
(x,y)
coordinates of both rectangles ( since my English is unable to precisely describe what I mean please see image below ):
IMPORTANT NOTE:
In my programming framework, y-axis is reversed -> positive values are below x-axis and negative ones above. Please see below picture:
QUESTION:
Can you give me the formula for properly changing current font height so the text can fit into target rectangle?
EDIT:
I have tried to apply formula recommended by member Nikos M. and got very decent results. The earlier problem why his formula malfunctioned was related to the way my programming language performed conversion between integer and real numbers. After correcting this, the output is nearly perfect ( the last letter barely exceeds the limit ). I will keep trying with this approach since it looks very promising, but would welcome other solutions if there are any.
END OF EDIT
EDIT #2:
I have altered the formula from member MvG's answer to this:
$$\text{optimal font size}=\text{guessed font size}\times\sqrt{\frac {\text{desired width}}{\text{computed width}}}$$
There was only one case where text exceeded the limit, in all other cases the behavior was perfect.
END OF EDIT #2
Again, this is my fisrt post so if edit is required/adding of proper tags/anything leave me a comment and I will react accordingly as soon as possible. Thank you for your patience and understanding.
FINAL REMARKS:
I have tried everything but the accepted solution always failed in one or two cases. No matter how much answerer tried, each time failure would happen. I believe that the problem doesn't lie in mathematical part, but is rather related to a faulty API I use. I have consulted experienced engineers and they agreed. Therefore I have officially accepted the answer, since from the mathematical standpoint it does solve the problem. This section is written to warn programers to become misguided that the formula actually solves programming part of the problem too. Thank you everyone for trying to help and for your support. Best regards 'till next time.
fontSizeNew = fontSizeOld x RectangleNewHeight / rectangleTarget
. If this is what you have meant, than you are correct but I have already tried this approach and it has failed :( $\endgroup$ – AlwaysLearningNewStuff Jul 5 '14 at 13:48