I am a programmer who saw this site "from" StackOverflow in hope to ask here for help with a specific problem. The reason I post this question here is because I fear that my question might not fit allowed topics for this site, so I want to check if it is valid before I post it.

I have a rectangle that should contain text in it. The problem is that I need to calculate font size, so the text can fit into rectangle. I need formula for this ( colleagues at StackOverflow could not help me with this ). In a nutshell, this is the most important information about the question I would ask.

Is this question suitable for this site ( and if it is, can you suggest me proper tags )?

Thank you.

  • $\begingroup$ It could be tagged geometry. But one has to flesh out a math question from a practical problem. Variable width font or constant? Hyphenation allowed? (I hope not). What do you know about the font metrics?... $\endgroup$
    – user147263
    Jul 4 '14 at 14:05
  • $\begingroup$ Also, this was brought up several times on SO, for example stackoverflow.com/q/687998. Text layout is an algorithm design problem. So is automatic font sizing. My point is, SO is still a better choice of a site. $\endgroup$
    – user147263
    Jul 4 '14 at 14:10
  • $\begingroup$ @Thisismuchhealthier.: Unfortunately, I do not use JQuery/JS so they were unable to help. Since I need formula and not the code, I thought that well posed question on this site could solve the problem. Thanks anyway... $\endgroup$ Jul 4 '14 at 14:18
  • $\begingroup$ You see, mathematical formulas need precise input. What would be the input in your case? Box dimensions, number of characters, length of every word, width of every character, hyphenation rules, kerning rules, line spacing. How all of this will be expressed mathematicaly? This looks more like a library function than a formula. $\endgroup$
    – user147263
    Jul 4 '14 at 14:24
  • $\begingroup$ @Thisismuchhealthier.: Maybe you are right, this would be too much, when we look from that perspective... Best regards. $\endgroup$ Jul 4 '14 at 14:40
  • 4
    $\begingroup$ Please don't take my comments as dismissal; I was trying to elucidate the nature of the question. My point is, to make this work in every case, you need a lot of conditional logic. But if you just need a rule of thumb that kind of works in most cases, then try $$\text{size} = C \sqrt{\frac{ab}{L}}$$ where $L$ is the number of characters, $a,b$ are dimensions of the box, and $C$ is a constant that hides all the layout complexity, and is found experimentally. That is, you pick a font and run tests using 100 sample texts of various kinds. Then reduce $C$ to have a safety margin. $\endgroup$
    – user147263
    Jul 4 '14 at 14:48
  • $\begingroup$ I think that question could be answered if I put enough info, which your suggested "solution" proves. I didn't misunderstand you, I just couldn't decide if I should post the question or not. Seeing your comment, I believe I should give a try... Thank you for your time and help. $\endgroup$ Jul 4 '14 at 16:20
  • 4
    $\begingroup$ If you can give us enough information that this becomes a purely mathematical question - that is, a question answerable by someone who knows nothing about programming - then I don't see why it would be off-topic. However, it does sound (to me) like that could be a bit difficult. $\endgroup$
    – Alexander Gruber Mod
    Jul 4 '14 at 16:27
  • $\begingroup$ @AlexanderGruber: I think I can do this, but I need to formulate the question very precisely, and must find a way to present data in a manner that doesn't require programming knowledge in order to interpret them. Thank you for answering. $\endgroup$ Jul 4 '14 at 16:44
  • 2
    $\begingroup$ IMHO, this is off topic for math.SE. To properly answer this question, one need to to know what components a glyph consists of, what sort of data is available in a font set more than the math involved. For this sort of typography related questions, I think tex.SE has more experts than here. However, I'm not sure whether this question is on topic for them or not. $\endgroup$ Jul 6 '14 at 9:15
  • $\begingroup$ I think this is off-topic because you would need to know all about the specific font, and how exactly the software converts the font-size (in points or ems or whatever) into the actual pixel-size. $\endgroup$
    – Jack M
    Jul 6 '14 at 14:55
  • $\begingroup$ For font point size vs pixel size, see graphic design $\endgroup$
    – Justin
    Jul 12 '14 at 5:40

If you can phrase the question in such a way that both the input to your computation and a characterization of the desired output are available in a purely mathematical form, without reference to specific fonts, specific font formats, specific libraries, and so on, then it would be on topic. Answers would likely be in the same spirit, so you'd have to turn the result back into an expression (in the best case) or an algorithm (of things are more complicated). You should also be clear on whether you want an optimal solution, or rather an easy-to-calculate approximation, and in the latter case whether an approximation is allowed to err in both directions, or only one.

Since you have posted your question by now, and that didn't get mass-downvoted so far. So it seems you did a reasonably good job there.

  • $\begingroup$ Thank you. I assumed the same, and thank you for answering. $\endgroup$ Jul 8 '14 at 15:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .