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.
Width of text
Width of text
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
Re: Width of text
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!
Re: Width of text
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.
- eradicator
- Smart Inserter
- Posts: 5206
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Width of text
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.
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.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Re: Width of text
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!
Re: Width of text
Makes sense. But about the font: how much can you customize it anyway?eradicator wrote: ↑Fri Sep 20, 2019 7:20 amEven 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).
From the description of draw_text{}:
For flying-text, I only find:font :: string (optional): Name of font to use. Defaults to the same font as flying-text
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?flying-text
text :: LocalisedString: The string to show.
color :: Color (optional): Color of the displayed text.
render_player_index :: uint (optional)
Been there, done that already!Edit: Oh...but you said you only wanted to center, so just do as the master said above.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
- eradicator
- Smart Inserter
- Posts: 5206
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Width of text
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
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.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Re: Width of text
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".eradicator wrote: ↑Fri Sep 20, 2019 5:00 pmHm. 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
That would make sense.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?
Commercial font? Same thing that happens when people include commercial graphics, music, sound effects or code in violation of the license............ #gottagonow
+1@Bilka: Also thanks again for remaking the wiki in api style. Sooo nice to use now.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!