Text metrics excludes trailing spaces in width value (DEF-1761)

This seems to be true:
gui.get_text_metrics(…, "a ", …, …).width == gui.get_text_metrics(…, “a”, …, …).width

4 Likes

I think this is by design, get_text_metrics returns the actual width of what is “seen” I believe. But I agree that it would be nice to know the width including trailing spaces to show cursor etc. Hm, maybe toggle-able as an argument to get_text_metrics? Adding an issue for this: DEF-1761

4 Likes

Yes, that seems correct, although one could argue the user should be in control of what should be seen, it might be convenient to be able to choose between both alternatives.

2 Likes

I just ran into this too, kind of annoying and unintuitive, but I guess I see the reason. It was fairly easy to work around, I just added an extra non-space character at the end before using get_text_metrics and then subtracted the width of that character from the result.

local endChar = "."
local endCharWidth = gui.get_text_metrics("my font", endChar).width

local text = "string with trailing spaces    "
local textWidth = gui.get_text_metrics("my font", text .. endChar).width - endCharWidth
1 Like

If it’s by design why do you count leading space then and exclude just trailing?

2 Likes

Bump.

Despite my earlier post, if you consider that this doesn’t just affect gui.get_text_metrics, but all text rendering in Defold, it seems quite serious, and a major hassle to work around.

Considering that: 1) it’s mangling user input without permission, 2) it only strips trailing spaces, not leading ones, and 3) it would be trivially easy for users to strip trailing spaces themselves if they wanted to, but a major pain to counteract it being forcibly done…I’d call it a bug.

This doesn’t look like anyone’s intended/expected behavior to me…
defold_trailing spaces metrics bug

I understand that trailing spaces with line wrapping can get complicated, but I’m talking about the case where line wrapping is disabled.


[Edit] OK, now that most of my righteous fury is out of my system…

The problem is that all text layout in Defold doesn’t properly handle the case where line wrapping is disabled.

It makes sense to strip trailing whitespace when you are wrapping lines of text. It does not make sense when you are not wrapping lines, when you’re just displaying a string of text as-is.

I don’t think any sane person ever deliberately disables line wrapping, adds spaces at the end of their string, and then wants those spaces to not be displayed, so if this bug is fixed, no one will ever miss it (and any workarounds should still work).

I don’t know anything about C++, but I think I at least tracked get_text_metrics back to here: defold/font_renderer.cpp at 1ae302ec33d4514408c04ad3ae5d3c1efe2057bd · defold/defold · GitHub
If line breaks are disabled, it looks like it just sets the width limit to some very large number…

if (!line_break) {
    width = FLT_MAX;
}

…but it still calls the Layout function which appears to be intended to wrap text into lines and trim the whitespace.

Instead, anytime text is measured or rendered (labels, gui text nodes), and line breaks are disabled, it should skip the layout step entirely and just measure/draw it as-is.

Again, I don’t really know how to read C++, so this is just my best guess.

2 Likes

I agree with you @ross.grams and I’ve created a ticket to address this issue: Trailing whitespace is trimmed from text when line-breaks are disabled · Issue #5911 · defold/defold · GitHub

1 Like