QuantumBadger / Speedy2D

Rust library for hardware accelerated drawing of 2D shapes, images, and text, with an easy to use API.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Leading whitespaces in text are not drawn to the screen

d0rianb opened this issue · 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;
        }
    }

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.

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

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.

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

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.

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

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