#25 (Not a bug) Predict text dimensions

open-remind
nobody
None
4
2010-06-30
2010-06-14
No

Hi,

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
10 50
11 60
12 56
13 48
14 64
etc.

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.

Thank you,
Jean-Simon

Discussion

    • summary: (Not a bug) Predict text domensions --> (Not a bug) Predict text dimensions
     
  • Sean Morrison
    Sean Morrison
    2010-06-28

    • priority: 5 --> 4
    • status: open --> pending-remind
     
  • Sean Morrison
    Sean Morrison
    2010-06-28

    All very interesting and some of your results are unexpected to it's going to take some time to investigate.

    For what it's worth, the results are going to be very specific to the font that you are using as the fonts can contain hints and "custom" instructions for specific font sizes and multiples of sizes. I highly suspect that is what you're seeing where the size goes up, dips down, then back up at given sizes.

    Of course it's entirely possible that there's also a bug in FTGL (or in Freetype) where the kerning distances are getting miscomputed but this would be the first I've heard of it.

    You can probably get a lot more control on your font widths by being more specific about your font choice (e.g., a fixed-width font for starters), or changing the font kerning.

    Cheers!

     
  • Thank you for your answer. I will try to play with the kerning. Unfortunately, I cannot use only fixed-width fonts ( yeah it would be way too easy ;) ), I need to support all the fonts installed on the user computer.

    I am very surprised to be the first to notice this behavior. I will try to investigate on my side as well, maybe it occurs only with Microsoft Fonts.

    Jean-Simon

     
    • status: pending-remind --> open-remind