Struct text_layout::KnuthPlass
source · pub struct KnuthPlass<N> { /* private fields */ }
Expand description
Runs the Knuth-Plass line-breaking algorithm to calculate the optimal break points for a paragraph.
Implementations§
source§impl<N: Num> KnuthPlass<N>
impl<N: Num> KnuthPlass<N>
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new KnuthPlass layout with default parameter values.
Examples found in repository?
58 59 60 61 62 63 64 65 66 67 68 69 70
fn layout_text() -> Result<String, fmt::Error> {
let text = " Far out in the uncharted backwaters of the unfashionable end of the western spiral arm of the Galaxy lies a small unregarded yellow sun. Orbiting this at a distance of roughly ninety-two million miles is an utterly insignificant little blue-green planet whose ape-descended life forms are so amazingly primitive that they still think digital watches are a pretty neat idea.";
let knuth_plass = KnuthPlass::new().with_threshold(f32::INFINITY);
let lines = layout_paragraph(&text, &knuth_plass, 80);
let mut result = String::new();
writeln!(&mut result, "┏{}┓", "━".repeat(80))?;
for l in lines {
let pad = 80 - l.chars().count();
writeln!(&mut result, "┃{}{}┃", l, " ".repeat(pad))?;
}
writeln!(&mut result, "┗{}┛", "━".repeat(80))?;
Ok(result)
}
More examples
63 64 65 66 67 68 69 70 71 72 73 74 75
fn layout_text() -> Result<String, fmt::Error> {
let text = " Far out in the uncharted backwaters of the unfashionable end of the western spiral arm of the Galaxy lies a small unregarded yellow sun. Orbiting this at a distance of roughly ninety-two million miles is an utterly insignificant little blue-green planet whose ape-descended life forms are so amazingly primitive that they still think digital watches are a pretty neat idea.";
let knuth_plass = KnuthPlass::new().with_threshold(F::MAX);
let lines = layout_paragraph(&text, &knuth_plass, F::from_num(80));
let mut result = String::new();
writeln!(&mut result, "┏{}┓", "━".repeat(80))?;
for l in lines {
let pad = 80 - l.chars().count();
writeln!(&mut result, "┃{}{}┃", l, " ".repeat(pad))?;
}
writeln!(&mut result, "┗{}┛", "━".repeat(80))?;
Ok(result)
}
sourcepub fn with_flagged_demerit(self, flagged_demerit: N) -> Self
pub fn with_flagged_demerit(self, flagged_demerit: N) -> Self
Sets the demerit for flagged penalties. Defaults to 100. Referred to as 𝛂 in Knuth-Plass ’81.
sourcepub fn with_fitness_demerit(self, fitness_demerit: N) -> Self
pub fn with_fitness_demerit(self, fitness_demerit: N) -> Self
Sets the demerit for a line that belongs to a different fitness class than its predecessor. Defaults to 100. Referred to as 𝛄 in Knuth-Plass ’81.
sourcepub fn with_threshold(self, threshold: N) -> Self
pub fn with_threshold(self, threshold: N) -> Self
Sets the adjustment ratio threshold. Lines will not be allowed to break at a given point if doing so would cause the line’s adjustment ratio to exceed this value. Defaults to 1. Referred to as 𝛒 in Knuth-Plass ’81.
Examples found in repository?
58 59 60 61 62 63 64 65 66 67 68 69 70
fn layout_text() -> Result<String, fmt::Error> {
let text = " Far out in the uncharted backwaters of the unfashionable end of the western spiral arm of the Galaxy lies a small unregarded yellow sun. Orbiting this at a distance of roughly ninety-two million miles is an utterly insignificant little blue-green planet whose ape-descended life forms are so amazingly primitive that they still think digital watches are a pretty neat idea.";
let knuth_plass = KnuthPlass::new().with_threshold(f32::INFINITY);
let lines = layout_paragraph(&text, &knuth_plass, 80);
let mut result = String::new();
writeln!(&mut result, "┏{}┓", "━".repeat(80))?;
for l in lines {
let pad = 80 - l.chars().count();
writeln!(&mut result, "┃{}{}┃", l, " ".repeat(pad))?;
}
writeln!(&mut result, "┗{}┛", "━".repeat(80))?;
Ok(result)
}
More examples
63 64 65 66 67 68 69 70 71 72 73 74 75
fn layout_text() -> Result<String, fmt::Error> {
let text = " Far out in the uncharted backwaters of the unfashionable end of the western spiral arm of the Galaxy lies a small unregarded yellow sun. Orbiting this at a distance of roughly ninety-two million miles is an utterly insignificant little blue-green planet whose ape-descended life forms are so amazingly primitive that they still think digital watches are a pretty neat idea.";
let knuth_plass = KnuthPlass::new().with_threshold(F::MAX);
let lines = layout_paragraph(&text, &knuth_plass, F::from_num(80));
let mut result = String::new();
writeln!(&mut result, "┏{}┓", "━".repeat(80))?;
for l in lines {
let pad = 80 - l.chars().count();
writeln!(&mut result, "┃{}{}┃", l, " ".repeat(pad))?;
}
writeln!(&mut result, "┗{}┛", "━".repeat(80))?;
Ok(result)
}
sourcepub fn with_looseness(self, looseness: usize) -> Self
pub fn with_looseness(self, looseness: usize) -> Self
Sets the looseness parameter. The looseness is an integer 𝗾 such that the total number of lines produced for the paragraph is as close as possible to 𝗾 plus the optimum number, without violating the conditions of feasibility.