No matter what I try I can't get a text centered on an entity as expected.
Example
Here's an example that creates a text rendering and a circle for comparison. The circle is centered as expected. But the text is nowhere near the center of the entity.
/c
local p = game.player
local target = p.selected.position
p.teleport(target)
rendering.clear()
for scale=1, 4 do
rendering.draw_text {
text = '42',
surface = p.surface,
color = {r=1},
scale = scale,
scale_with_zoom = true,
alignment = 'center',
target = target,
}
rendering.draw_circle{
color = {g=1},
radius = scale,
width = 2,
filled = false,
target = target,
surface = p.surface,
}
end
The larger I set the scale the further the text moves away from the center. It's approximately one text-height away from the center.
screenshot_000h_48m_54s.png (62.13 KiB) Viewed 2536 times
It gets even weirder: When scale_with_zoom = true is set the text seems to be locked to an absolute *screen* position instead of an absolute world position.
factorio_20210615-001607[000]ut.avi_gifsicle.gif (3.31 MiB) Viewed 2536 times
I even found an old post by Bilka according to which this *should* work:
If you want a centered text on a position, just use the center alignment on the text and set the target as that position.
Re: [1.1.34] rendering.draw_text centering is weird
Posted: Tue Jun 15, 2021 1:00 pm
by eradicator
Just a comment on the "minor issue" classification (and for future readers): For scale_with_zoom = false this can be somewhat hacked-around by adjusting the initial position manually. For scale_with_zoom = true there are no workarounds, so I'm not sure what that can even be used for in the current state.
Re: [1.1.34] rendering.draw_text centering is weird
Posted: Tue Jun 15, 2021 6:42 pm
by posila
The alignment property is horizontal alignment, and vertical alignment is hardcoded to be "top" deep down in text rendering
Re: [1.1.34] rendering.draw_text centering is weird
I understand that (even if I wish that was different). If I could just set vertical_alignment with draw_text I'd use that, but requesting it seems unlikely to be implemented?
and vertical alignment is hardcoded to be "top" deep down in text rendering
Looking at the screenshots (especially the one with the four sizes) the "top" is moving downwards with increased scale. And even at scale = 1 the top (assuming that to be the highest pixel of text) is nowhere near the center of the assembling machine.
Without the knowledge of "top alignment" I'd describe it as: "The text looks like it has an invisible second line above the visible line, and that second line is center aligned on the target position."
Re: [1.1.34] rendering.draw_text centering is weird
Without the knowledge of "top alignment" I'd describe it as: "The text looks like it has an invisible second line above the visible line, and that second line is center aligned on the target position."
Re: [1.1.34] rendering.draw_text centering is weird
Posted: Wed Jun 16, 2021 10:59 am
by eradicator
Well, let me show you two more pictures. Both with scale_with_zoom = false, one with alignment = 'right', the other with alignment = 'left'. I've included the position's center axis in blue (+collision box debug overlay). You can clearly see how "scale" has no effect on the left/right alignment (as expected), but does unexpectedly affect top alignment.
notaligned.png (67.97 KiB) Viewed 2379 times
Re: [1.1.34] rendering.draw_text centering is weird
Posted: Wed Jun 16, 2021 11:49 am
by boskid
Fine. Modding interface request implemented for 1.1.35.
Below is vertical_alignment="middle".
98850.png (71.84 KiB) Viewed 2359 times
Re: [1.1.34] rendering.draw_text centering is weird
You can clearly see how "scale" has no effect on the left/right alignment (as expected)
You can clearly see that is not true
empty-space-scaled.png
Darn. True. And with my limited typography knowledge I can't explain that .
However after digging through wiki and learning new stuff about typography thanks to the link from @boskid I found out that the "weird" top alignment is caused by some unexpectedly high ascenders. I'm sure there's more than one but the first good example I found is "࠱" (\u831, presumably Arabic, looks like a very high "=" equals sign).
This would be the code, but you can't paste it into the game because the console complains about invalid escape sequences for the unicode characters. (Works fine in my lua terminal, so not sure if Lua 5.2 limitation?.)
/c
local s = table.concat{'\u{830}','\u{831}','\u{832}','\u{833}','\u{834}','\u{835}','\u{836}','\u{837}','\u{838}','\u{839}','\u{840}','42'}]]
rendering.clear()
local y = 0
for _, font in pairs(game.font_prototypes) do
y = y + 4
local p = game.player
local target = {p.position.x, p.position.y+y}
for scale=4,1,-1 do
rendering.draw_text {
text = s .. '42',
surface = p.surface,
color = {r=1},
scale = scale,
scale_with_zoom = false,
alignment = 'right',
target = target,
font = font.name,
}
rendering.draw_circle{
color = {g=1},
radius = scale,
width = 2,
filled = false,
target = target,
surface = p.surface,
}
end
end
Here's a picture:
ascenders.png (67.87 KiB) Viewed 2335 times
Re: [1.1.34] rendering.draw_text centering is weird
Posted: Wed Jun 16, 2021 12:41 pm
by eradicator
TL;DR: Sorry for the fuss. And thanks to everyone involved for their patience.