Page 1 of 1

Width of text

Posted: Thu Sep 19, 2019 9:43 pm
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.

Re: Width of text

Posted: Thu Sep 19, 2019 9:59 pm
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.

Re: Width of text

Posted: Fri Sep 20, 2019 6:26 am
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.

Re: Width of text

Posted: Fri Sep 20, 2019 7:20 am
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.

Re: Width of text

Posted: Fri Sep 20, 2019 7:40 am
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! :-)

Re: Width of text

Posted: Fri Sep 20, 2019 8:02 am
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

Re: Width of text

Posted: Fri Sep 20, 2019 5:00 pm
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

Re: Width of text

Posted: Fri Sep 20, 2019 6:14 pm
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