slime wrote: ↑Tue Jan 07, 2025 12:03 pm
It's likely a bug in your code
Thanks to you, I've found it. I needed to simplify some stuff in order to make the code understandable, and by doing so I noticed I was converting the text to uppercase *after* calculating the font width. So horizontal centering now works as expected.
- y.png (18.27 KiB) Viewed 122 times
However, vertical centering does not. Even though the text is converted to uppercase and so should not have any parts of the glyphs hanging below the baseline, the text is still not centered properly in the Y axis (contrary to the rectangle, which is). Here's the code:
Code: Select all
-- 'global' is just a module with some global variables
love.graphics.setLineWidth(1)
love.graphics.setColor(global.theme.circle)
love.graphics.circle('fill', 0, 0, baseRadius * 0.2)
love.graphics.setColor(global.theme.accent)
love.graphics.circle('line', 0, 0, baseRadius * 0.2)
local text = string.upper('how') -- just an example
love.graphics.setColor(global.theme.color)
love.graphics.setFont(global.font.text)
local w = love.graphics.getFont():getWidth(text)
local h = love.graphics.getFont():getHeight()
love.graphics.print(text, 0 - w/2, 0 - h/2)
love.graphics.rectangle('line', 0 - w/2, 0 - h/2, w, h)