Functions that return text size do not return the same sizes

When we need to know the size of text, there are three functions:

  • pd.gfx.getTextSizeForMaxWidth(text, maxWidth)
  • gfx.font:getTextWidth(text) and gfx.font:getHeight()
  • gfx.font:drawText(text, x, y) returns width, height

The last one drawText returns a different size from the other two methods. That is, the width, height returned from getTextSizeForMaxWidth returns the same values as getTextWidth and getHeight; while drawText always returns a shorter/smaller height value.

Same result using default font or a custom font: functions return mismatched height values.

Example code:

-- default font
local font = gfx.getFont()

-- using (g)raphics
local gWidth, gHeight = gfx.getTextSizeForMaxWidth("EDIT", pd.display.getWidth())
-- gWidth  = 32, gHeight = 20

-- using (f)ont
local fWidth, fHeight = font:getTextWidth("EDIT"), font:getHeight()
-- fWitdh = 32, fHeight = 20

-- (d)rawn
local dWidth, dHeight = font:drawText("EDIT", 0,0)
-- dWidth = 32, fHeight = 14

Changing the string to `"graphics", these sizes are returned:

  • 63, 20 - graphics
  • 63, 20 - font
  • 63, 17 - drawn

It looks like font:drawText() is returning an incorrect height, while the others are correct. I'm not sure where that wrong number is coming from, but I'll look into it and try to get a fix in as soon as possible.

Thanks for bringing this to our attention!