[1.1.34] rendering.draw_text centering is weird

Post Reply
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

[1.1.34] rendering.draw_text centering is weird

Post 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 2465 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 2465 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.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [1.1.34] rendering.draw_text centering is weird

Post 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.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

posila
Factorio Staff
Factorio Staff
Posts: 5202
Joined: Thu Jun 11, 2015 1:35 pm
Contact:

Re: [1.1.34] rendering.draw_text centering is weird

Post by posila »

The alignment property is horizontal alignment, and vertical alignment is hardcoded to be "top" deep down in text rendering

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [1.1.34] rendering.draw_text centering is weird

Post 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."
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2248
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: [1.1.34] rendering.draw_text centering is weird

Post 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

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [1.1.34] rendering.draw_text centering is weird

Post 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 2308 times
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2248
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: [1.1.34] rendering.draw_text centering is weird

Post by boskid »

Fine. Modding interface request implemented for 1.1.35.

Below is vertical_alignment="middle".
98850.png
98850.png (71.84 KiB) Viewed 2288 times

posila
Factorio Staff
Factorio Staff
Posts: 5202
Joined: Thu Jun 11, 2015 1:35 pm
Contact:

Re: [1.1.34] rendering.draw_text centering is weird

Post 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 2282 times

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [1.1.34] rendering.draw_text centering is weird

Post 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 2264 times
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [1.1.34] rendering.draw_text centering is weird

Post by eradicator »

TL;DR: Sorry for the fuss. And thanks to everyone involved for their patience.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Post Reply

Return to “Implemented mod requests”