Width of text

Place to get help with not working mods / modding interface.
Post Reply
Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Width of text

Post by Pi-C »

The rendering API provides get_width(id) to get the width of lines, circles, or rectangles. Is there something like that for text?

Please note: The width of a text may differ from the length of the string the text is made of if it contains line breaks! I need the width of localized text rendered with draw_text() to calculate the offset and center it around a certain position.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Width of text

Post by Pi-C »

Just noticed that my question is pointless. Apparently, draw_text() ignores line breaks, so I should indeed use the length of the string.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Bilka
Factorio Staff
Factorio Staff
Posts: 3138
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Width of text

Post by Bilka »

If you want a centered text on a position, just use the center alignment on the text and set the target as that position.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

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

Re: Width of text

Post by eradicator »

Even with the localized text now available via API (Player.request_translation) the length of a string never says anything about it's visible length, because that depends both on the font used and on the *language* used (i.e. ASCII vs double-width characters, etc).

Edit: Oh...but you said you only wanted to center, so just do as the master said above.
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.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Width of text

Post by Pi-C »

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.
Ooops, didn't notice the alignment parameter. Thank you for making me see!

By the way: Is there a reason that line breaks are replaced with spaces? I think it could be useful to have line breaks. Especially with localized strings, you can't know how long a string could get, so in the worst case you could get a line of text that is so long that it doesn't fit onto the screen. One could try to manually break a line into multiple strings (with \n as delimiter) and manually draw the text lines below each other. But looking at the description, getting the correct positions could be harder than it seems because scale factor and font size would have to be accounted for. Having something like the saveboxes from LaTeX (pre-rendering output with line breaks allows you to read height/width of a box and to place it accordingly) would be ideal! :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Width of text

Post by Pi-C »

eradicator wrote:
Fri Sep 20, 2019 7:20 am
Even with the localized text now available via API (Player.request_translation) the length of a string never says anything about it's visible length, because that depends both on the font used and on the *language* used (i.e. ASCII vs double-width characters, etc).
Makes sense. But about the font: how much can you customize it anyway?

From the description of draw_text{}:
font :: string (optional): Name of font to use. Defaults to the same font as flying-text
For flying-text, I only find:
flying-text

text :: LocalisedString: The string to show.
color :: Color (optional): Color of the displayed text.
render_player_index :: uint (optional)
So it seems you can't select an arbitrary font or set a font size, but just the text color. Did I miss something or is that really all we can do? Even if we could choose an arbitrary font name -- wouldn't it be necessary to provide these fonts somehow so it is guaranteed that every player will have the same fonts in the game? Can you embed fonts in the mod? What about legal implications -- e.g. if somebody just includes a commercial font?
Edit: Oh...but you said you only wanted to center, so just do as the master said above.
Been there, done that already! :-D
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

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

Re: Width of text

Post by eradicator »

Hm. Not sure if you can change the font of flying-text prototypes, at least according to the wiki you can't:
https://wiki.factorio.com/Prototype/FlyingText

But i assume that draw_text will take the name of a custom gui font prototype:
https://wiki.factorio.com/Prototype/Font

The games font files (.ttf) are defined in an info.json file, so you probably can't add new ones? But different languages will use different fonts because the english default font doesn't have all the glyphs.

Commercial font? Same thing that happens when people include commercial graphics, music, sound effects or code in violation of the license............ #gottagonow

@Bilka: Also thanks again for remaking the wiki in api style. Sooo nice to use now. :D
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.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Width of text

Post by Pi-C »

eradicator wrote:
Fri Sep 20, 2019 5:00 pm
Hm. Not sure if you can change the font of flying-text prototypes, at least according to the wiki you can't:
https://wiki.factorio.com/Prototype/FlyingText
Yep, that's basically what I found. It makes the "Defaults to the same font as flying-text" part of the draw_text{} description a bit ambiguous, though, because one could think (I did) that defining fonts would be explained under "flying-text".
But i assume that draw_text will take the name of a custom gui font prototype:
https://wiki.factorio.com/Prototype/Font

The games font files (.ttf) are defined in an info.json file, so you probably can't add new ones?
That would make sense.
Commercial font? Same thing that happens when people include commercial graphics, music, sound effects or code in violation of the license............ #gottagonow
:mrgreen:
@Bilka: Also thanks again for remaking the wiki in api style. Sooo nice to use now. :D
+1
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Post Reply

Return to “Modding help”