I am developing an application to render a string that will use the largest font that fits into a rectangle.
So far, my algorithm is the following:
1 - Render the string at face size 8 to get the dimensions ( W and H )
2 - "Predict" the needed face size with a simple rule of three : X = 8 * rectW / W
3 - Render the string with the predicted face size
For some reason, at this point, the dimensions of the string is never near what we would expect. There seems to be a small error ( 1 pixel ) for each character which adds and gives totally unpredictable results.
Wondering why this happened, I wrote a little application that renders "The quick brown fox jumps over the lazy dog" for each face size from 10 to 500. Surprisingly, the overall widths of the string are not linear ( at all ).
I get results looking like this:
face size: string witch in pixels
For that reason, my function has to be iterative, meaning I have to test a lot of face sizes before I get the optimal one I'm looking for.
Maybe this is a "normal" font behavior, but I would like to know if there would be a better initial guess to my iterative solution.