Page 1 of 1

Label.ComputeWidth fails with composed text

Posted: 07 Mar 2016, 15:51
by Dommy
Label.ComputeWidth returns incorrect values when trying to compute width of translated or composed text.

Example:

Code: Select all

Label.Value = _("Continue");
log(Label.ComputeWidth(Label.Value));
or

Code: Select all

Label.Value = TL::Compose("Current attacker: %1", Players[0].User.Name);
log(Label.ComputeWidth(Label.Value));
Looks like this function is taking into account raw text, containing PU1 and PU2 characters instead actual visible output. Tried also to call time function one tick after setting new value, but it hasn't changed anything.

Re: Label.ComputeWidth fails with composed text

Posted: 08 Mar 2016, 13:38
by zocka
Regarding only translations, it seems you could use TextLib::GetTranslatedText() to get the displayed Text.
I can't remember this function being available, when I last needed it, so I don't have experiences using it.

To fix the length of composed text, you might be able to hack your way with something like:
1. Remove all wildcards in your text (replace %1...%n by "")
2. Append the values to your text
3. Compute width of that
You might get into a bit of trouble, if you have line breaks in your text. ("something something.nickname" would break different from "something nickname something.", but as "a %1 b" might be "%1 a b" in another language, you can't just replace spaces along with the wildcards.)

To avoid that you could of course create your own sprintf function without too much effort.

Another thing regarding line breaks and ComputeWidth:
noyranea wrote: Label.ComputeWidth(Text) returns only the length of the last line (more precisely the length it should have without autonewline=True[...]). This will be fixed soon.
Maybe it is fixed by now, but as I learned about the not yet released ComputeHeight function in the same post in february 2015, I'm sceptical :D