mooman219 / fontdue

The fastest font renderer in the world, written in pure rust.
Apache License 2.0
1.38k stars 70 forks source link

Error when running "layout" example #104

Closed Blatko1 closed 2 years ago

Blatko1 commented 2 years ago

Running layout example on the newest version 0.7.0 prints this error message;

thread 'main' panicked at 'attempt to subtract with overflow',
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\/library\std\src\panicking.rs:498
   1: core::panicking::panic_fmt
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\/library\core\src\panicking.rs:107
   2: core::panicking::panic
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\/library\core\src\panicking.rs:48
   3: fontdue::layout::Layout<tuple$<> >::append<tuple$<>,fontdue::font::Font>
             at .\src\layout.rs:448
   4: layout::main
             at .\examples\layout.rs:24
   5: core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\core\src\ops\function.rs:227
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
error: process didn't exit successfully: `target\debug\examples\layout.exe` (exit code: 101)

It seems to fail in this line of code:

layout.append(fonts, &TextStyle::new("Hello ", 35.0, 0));

where the subtraction happens:

if linebreak >= self.linebreak_prev {
                self.linebreak_prev = linebreak;
                self.linebreak_pos = self.current_pos;
                self.linebreak_idx = self.glyphs.len() - 1; // <--- HERE
            }
Blatko1 commented 2 years ago

Changing line 448:

if linebreak >= self.linebreak_prev {
                self.linebreak_prev = linebreak;
                self.linebreak_pos = self.current_pos;
                self.linebreak_idx = (self.glyphs.len() - 1).min(0);  // <--- CHANGED
            }

and changing line 498:

if let Some(line) = self.line_metrics.last_mut() {
            line.padding = self.max_width - (self.current_pos - self.start_pos);
            line.line_end = (self.glyphs.len() - 1).min(0); // <--- CHANGED
        }

in src/layout seems to fix the problem.

mooman219 commented 2 years ago

It doesn't actually cause a logic issue, there's a check later on for this. That's unfortunate this triggers an error for you, I'll resolve it

mooman219 commented 2 years ago

0.7.1 published