Formula for adjusting font height 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.
 A: General idea
Assuming that font size is proportional to the height of the rectangle, you have
$$\frac{\text{guessed font size}}{\text{computed height}}=
\frac{\text{optimal font size}}{\text{desired height}}$$
So you could compute the optimal font size from this as
$$\text{optimal font size}=\text{desired height}\times
\frac{\text{guessed font size}}{\text{computed height}}$$
But there are a lot of fine points to this. If you have pre-wrapped text, you'd likely want to do this for both dimensions independently, then choose the minimum of the font sizes computed in this way to make sure the text fits inside the box in both directions. If your algorithm (re)wraps the text, then the font size will not neccessarily be proportional to the height of the rectangle. Instead, the squared font size will be roughly proportional to the area of the rectangle, so if you compute the rectangle height for fixed width, the resulting height is proportional to the area and you should use this:
$$\frac{(\text{guessed font size})^2}
{\text{fixed width}\times\text{computed height}}=
\frac{(\text{optimal font size})^2}
{\text{fixed width}\times\text{desired height}}
\\
\text{optimal font size}=\text{guessed font size}\times\sqrt{\frac
{\text{desired height}}{\text{computed height}}}$$
Ugly details
The details of line breaking will make this a rough estimate, though.
Other details, like font hinting or integer-only sizes or metrics, could also make the computation imprecise. So you might want to iterate this a few times until you are satisfied with the fit.
You could also consider other iterative approaches, like e.g. bisection, which will take longer in well-behaved cases but will be pretty robust when it comes to corner cases. To do bisection, compute a target font size as above, then double that repeatedly until you get a font size which exceeds the box, and also halve it repeatedly until you get a size which fits well within the box. Then you can iteratively choose a font size between these two current extemes, and depending on whether the box computed for that size fits or not, adjust either boundary until the boundaries are close enough to one another that you can consider them as a solution.
One remark regarding your question: the details about orientation of the axis, or coordinates for all four corners of the rectangle, is pretty irrelevant from a mathematical point of view. The core of the problem is font size vs. rectangle dimensions, the rest is just implementation details.
When widths don't match
The reference you provided indicates that if the DT_WORDBREAK flag is given, the output will fit the desired width (i.e. be that width or smaller) unless there is a single word which exceeds that width. So if you notice that the computed width exceeds the desired width, you can conclude that as single word is too long, and adjust font size based on that word:
$$\text{optimal font size}=\text{desired width}\times
\frac{\text{previously chosen font size}}{\text{computed width}}$$
That should make the single word fit, unless rounding or kerning or hinting or whatever is biting you.
If the computed width is smaller, then the algorithm apparently chose to wrap text in more places than the above estimate would have assumed. Which is only reasonable, since the above basically assumed that you can wrap a line anywhere, and even obtain slices which have less height than a line. Anyway, if the width is too small but the height matches, you can simply use padding to center horizontally. If the height is too high now, you can use the formula at the top, for adjusting height. The one without the square root. But if you do this, you choose a smaller font, and suddenly the implementation might break the text in fewer places, so the chosen font size might be too small again.
So you either accept this fact that you might be choosing to small a font at that point, or you bisect as outlined above.
A: I feel almost like a bad person for suggesting this, and it's not really even a "mathy" answer, but you can always try to derive the font size each time, experimentally.
You can be smart about it too.  Pick some font size, and render your text.  Too big?  Cut it by half and try again.  Too small?  Double it and try again.  Each time, reduce the window of variation.  That is, do a binary search to find the optimal text size.
Since font sizes generally vary in integers (I've seen implementations support half integers, and maybe someone somewhere supports more), the algorithm will quickly converge to the right answer.  If you're dealing with incremental changes to text, then the solution can be adjusted quickly as well.
If you're feeling especially fancy, I guess you could use any root finder instead to find the optimal solution in a similar manner.
