I'm working on a small component for a game that essentially wraps the provided String so that it fits within the width of the provided rectangle. The rectangle is provided by the Slick2D library as well. I've assumed thus far that the width of that rectangle is in OpenGL units, but the distinction has been irrelevant in any way I've used Slick2D geometry so far.

Now, I'm using the UnicodeFont class and I'm not sure what units the return value of the getWidth method of that class is in. UnicodeFont.getWidth() returns an int value, so my initial inclination was to think it returned the value in pixels.

The Rectangle class supplied by Slick2D however, returns a float value. This leads me to believe that it is measuring the width using OpenGL coordinates, but I'm not sure.

So perhaps the community knows. Do the return values of both the Rectangle.getWidth() and the UnicodeFont.getWidth() methods use the same system of measurement?


We can look at the source to answer this question. The getWidth method goes through each character and gets the getGlyphBounds which goes and gets vector.getGlyphPixelBounds, eventually leading down to Glyph.getWidth(). The getWidth method there says:

Returns: The width in pixels of the glyphs image

So, pixels is the unit of measurement used for getWidth of a UnicodeFont.

  • \$\begingroup\$ Thank you very much! This is exactly what I was looking for. \$\endgroup\$
    – jctjct3
    Sep 10 '13 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.