Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline text layout panics when multi-byte UTF-8 is used #571

Merged
merged 8 commits into from May 20, 2024

Conversation

oswald2
Copy link
Contributor

@oswald2 oswald2 commented Apr 14, 2024

The layout function for multi-line text crashes when multi-byte UTF-8 characters are used. This pull request fixes this issue.

The old algorithm was simply operating on bytes and directly indexing the string slices on byte boundaries, which panics for UTF-8 multi-byte characters (one of my use-cases).

The function has been rewritten to use an iterator over the string slice via str::char_indices, which is on base of a char and not of a byte and therefore avoids the panic. A small test case was added in the code. The test depends on the ttf feature though, as it must use a font to determine the width of a text in pixels. So, the test is only compiled and run when also the ttf feature is enabled.

NOTE: the CONTRIBUTING file mentions that plotters should be compilable with Rust 1.36. Unfortunately, I didn't manage to do that. After setting the toolchain, it first complained about the ab_glyph feature having the same name as a dependency. Fixing this, the compilation ran into a problem as the newer version of chrono (which are automatically downloaded on the build with 1.36) are for edition 2021, which is then of course unrecognized as 1.36 is for edition 2018. After trying around a bit, I could not get this to work with 1.36, so I stopped. I assume, that plotters itself is normally compiled with newer versions than 1.36 as the ab_glyph feature and chrono would not allow that. I hope this is ok.

.gitignore Outdated Show resolved Hide resolved

// iterate over indices
while let Some(idx) = indices.next() {

Check warning

Code scanning / clippy

this loop could be written as a for loop Warning

this loop could be written as a for loop
Copy link
Member

@AaronErhardt AaronErhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@AaronErhardt AaronErhardt merged commit f29a6e3 into plotters-rs:master May 20, 2024
16 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants