linebender / piet

An abstraction for 2D graphics.
Apache License 2.0
1.24k stars 93 forks source link

Panic when trying to render ideographic space character #427

Closed Majora320 closed 3 years ago

Majora320 commented 3 years ago

Note: This was a bug I observed when building something with druid, but since the traceback led to this library I'm filing it here as that is likely where the issue lies.

When rendering the character U+3000 IDEOGRAPHIC SPACE ( ) at the end of a line or by itself, the program panics and this a message similar to this one is printed (this specific example is for a string containing the character alone):

thread 'main' panicked at 'byte index 2 is not a char boundary; it is inside '\u{3000}' (bytes 0..3) of ` `', /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/piet-cairo-0.3.0/src/text.rs:313:45

I'm not sure if this is a bug in piet, cairo, or what have you, but I would appreciate it being looked into.

raphlinus commented 3 years ago

Quick look through the code reveals the likely culprit: count_trailing_whitespace in text/lines.rs is returning count which is a number of code points, but it's being interpreted as utf-8 code units. The count should be replaced by something like a sum of len_utf8.