The solution is to use the new API (sim.*, gfx.*, elem.*) whenever you can and only use the legacy API (tpt.*) when you must. The legacy API is ... well, legacy, meaning that you shouldn't trust it. tpt.textwidth is legacy, its supported counterpart is gfx.textSize, which returns the width of the widest line in the source string (lines are delimited by \n) and the height of all lines combined. Yes, it returns two integers.