In the Haskell Gloss library, one draws text with the Text constructor of the Picture type. But how, then does one find the width (and height) of such a picture?
Asked
Active
Viewed 946 times
1 Answers
1
Here's how text is rendered in Gloss:
Text str
-> do
GL.blend $= GL.Disabled
GL.preservingMatrix $ GLUT.renderString GLUT.Roman str
GL.blend $= GL.Enabled
The important point here is that it calls renderString
. Looking at the documentation for renderString
, we immediately see two other useful functions: stringWidth
and fontHeight
. As such, you can get your width and height like this:
import Graphics.UI.GLUT.Fonts
do
width <- stringWidth Roman str
height <- fontHeight Roman

Joseph Sible-Reinstate Monica
- 45,431
- 5
- 48
- 98
-
Thanks for this. But I think stringWidth gives the width in pixels - how to I convert the result to Float that Gloss uses? – user1023733 Dec 17 '19 at 17:25
-
GLInt returned by stringWidth is just type synonym for [Int32](https://hackage.haskell.org/package/OpenGLRaw-3.3.3.0/docs/Graphics-GL-Types.html#t:GLint). Int32 can be converted to Float using [fromIntegral](https://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html#v:fromIntegral) – Jan Hrcek Dec 31 '21 at 12:02