mgeisler / textwrap

An efficient and powerful Rust library for word wrapping text.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Make optimal-fit penalties adjustable

mgeisler opened this issue · comments

The current penalties work well for terminal text, but the fixed penalties don't work for text outside of the terminal. An an example, the

/// Penalty for lines ending with a hyphen.
const HYPHEN_PENALTY: i32 = 25;

penalty basically says "it costs the same to insert a hyphen as leaving a gap of 5 columns at the end of the line". This no longer makes sense if the font is variable width and the magnitude of the penalty doesn't match if we start measuring line lengths in pt or px.

It would also be nice to be able to adjust the overflow penalty. I think it is too low for variable-width settings because even small overflows can be problematic if there is a visual indicator of the line width, for example the border of a table.

I think it is too low for variable-width settings because even small overflows can be problematic if there is a visual indicator of the line width, for example the border of a table.

Yes, it's basically broken for anything other than monospaced text. I've finally turned the hard-coded constants into parameters in #389. The Wasm demo lets you play with all the parameters — it's not very pretty with all the sliders, but it'll do for now 😄