Speedy2D icon indicating copy to clipboard operation
Speedy2D copied to clipboard

Leading whitespaces in text are not drawn to the screen

Open d0rianb opened this issue 3 years ago • 7 comments

When there is leading whitespace in a text, they are not calculated by the layout_text method. By removing this block of code, it works as intended :

    // Skip whitespace
    while let Some(word) = words.peek() {
        if word.is_whitespace {
            words.next().unwrap();
        } else {
            break;
        }
    }

d0rianb avatar Dec 23 '21 14:12 d0rianb

Thanks for the report. This is intentional due to the word wrapping logic, and it's the behaviour of most text boxes/word processors/etc. However, if you use non-breaking spaces then I think you may get the behaviour you want.

https://unicode-table.com/en/00A0/

It's unicode 160 (0x00A0) -- if you replace all the normal spaces with this then I think they shouldn't be removed.

QuantumBadger avatar Dec 23 '21 19:12 QuantumBadger

Thanks it works. Is it a solution for tab characters \t which are also cut during the wrapping ?

d0rianb avatar Dec 23 '21 21:12 d0rianb

Glad to hear it works :) The same solution should be usable for tabs, but you would need to replace the \t with multiple non-breaking spaces.

QuantumBadger avatar Dec 24 '21 02:12 QuantumBadger

Maybe it would be better to add a parameter to layout_text, say keep_spaces, to ease the life? :-/

Revertron avatar Dec 24 '21 09:12 Revertron

Maybe it would be better to add a parameter to layout_text, say keep_spaces, to ease the life? :-/

Yeah I think that's a good idea.

QuantumBadger avatar Dec 24 '21 10:12 QuantumBadger

Also hitting this! In my case, I am rendering in monospaced font, so I obviously care about whitespace for alignment.

Couple of other options here:

  • trim only leading whitespace which would have been introduced by word-wrap
  • trim only if options.wrap_words_after_width is set

matklad avatar Mar 19 '23 21:03 matklad

Now fixed thanks to @InfiniteCoder01! TextOptions::with_trim_each_line() will be in version 2.0.0.

QuantumBadger avatar Nov 05 '23 19:11 QuantumBadger