Page 1 of 1

[1.1.34] rendering.draw_text centering is weird

Posted: Mon Jun 14, 2021 10:31 pm
by eradicator
What

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.

Code: Select all

/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
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
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:
Bilka wrote:
Fri Sep 20, 2019 6:26 am
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

Posted: Tue Jun 15, 2021 11:04 pm
by eradicator
posila wrote:
Tue Jun 15, 2021 6:42 pm
The alignment property is horizontal alignment,
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?
posila wrote:
Tue Jun 15, 2021 6:42 pm
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

Posted: Tue Jun 15, 2021 11:37 pm
by boskid
eradicator wrote:
Tue Jun 15, 2021 11:04 pm
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."
https://github.com/memononen/fontstash/ ... sh.h#L1282

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
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
98850.png (71.84 KiB) Viewed 2359 times

Re: [1.1.34] rendering.draw_text centering is weird

Posted: Wed Jun 16, 2021 11:51 am
by posila
eradicator wrote:
Wed Jun 16, 2021 10:59 am
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
empty-space-scaled.png (37.47 KiB) Viewed 2353 times

Re: [1.1.34] rendering.draw_text centering is weird

Posted: Wed Jun 16, 2021 12:31 pm
by eradicator
boskid wrote:
Wed Jun 16, 2021 11:49 am
Fine. Modding interface request implemented for 1.1.35.
Below is vertical_alignment="middle".
Thank you very much! So much really! :o
posila wrote:
Wed Jun 16, 2021 11:51 am
eradicator wrote:
Wed Jun 16, 2021 10:59 am
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). :oops:

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?.)
TheCode
Here's a picture:
ascenders.png
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.