0

I'm trying to adequate the FontSize of my text into a width-height specific context. For instance, if I got a image (512x512 pixels) and I got for instance 140 characters. What would be the ideal FontSize?

In the above case, a 50 pixels Fontsize seems to be ok but what happened if there's a lot more text? the text will not fit into the picture so it needs reduction. I've been trying to calculate this on my own without success.

What I've tried is this: get total pixels, with a picture 512x512 = 262144 and divide into the length of the text. But that gives a big number. Even if I divided that number by 4 (thinking about a box-pixel-model for the font).

Do you have any solutions for this?

PS. I've been using truetype (if this is somehow useful) I've been using python for this purpose, and PIL for image manipulation.

Thank you in advance.

Javittoxs
  • 481
  • 2
  • 7
  • 18
  • You're likely to be needing a programming language for this. Which one(s) can you use? You should add that information as a tag to your question. – Pekka Oct 11 '14 at 21:23
  • Thank you. I'm using python. I'll edit the question. – Javittoxs Oct 11 '14 at 21:32
  • I don't know Python, but the only approach seems to be, as gnibbler suggests, trial and error. See e.g. http://stackoverflow.com/questions/10689225/imagettftext-calculate-font-size-to-ensure-text-fits-image-width for a PHP based answer with a bit more detail – Pekka Oct 12 '14 at 08:49

1 Answers1

3

It's pretty tricky to do analytically.

One way is trial and error. Choose a large font size and render the layout to see whether it fits

Use bisection algorithm to converge on the largest font that fits

John La Rooy
  • 295,403
  • 53
  • 369
  • 502