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?
58fn layout_text() -> Result<String, fmt::Error> {
59 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.";
60 let knuth_plass = KnuthPlass::new().with_threshold(f32::INFINITY);
61 let lines = layout_paragraph(&text, &knuth_plass, 80);
62 let mut result = String::new();
63 writeln!(&mut result, "┏{}┓", "━".repeat(80))?;
64 for l in lines {
65 let pad = 80 - l.chars().count();
66 writeln!(&mut result, "┃{}{}┃", l, " ".repeat(pad))?;
67 }
68 writeln!(&mut result, "┗{}┛", "━".repeat(80))?;
69 Ok(result)
70}More examples
63fn layout_text() -> Result<String, fmt::Error> {
64 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.";
65 let knuth_plass = KnuthPlass::new().with_threshold(F::MAX);
66 let lines = layout_paragraph(&text, &knuth_plass, F::from_num(80));
67 let mut result = String::new();
68 writeln!(&mut result, "┏{}┓", "━".repeat(80))?;
69 for l in lines {
70 let pad = 80 - l.chars().count();
71 writeln!(&mut result, "┃{}{}┃", l, " ".repeat(pad))?;
72 }
73 writeln!(&mut result, "┗{}┛", "━".repeat(80))?;
74 Ok(result)
75}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?
58fn layout_text() -> Result<String, fmt::Error> {
59 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.";
60 let knuth_plass = KnuthPlass::new().with_threshold(f32::INFINITY);
61 let lines = layout_paragraph(&text, &knuth_plass, 80);
62 let mut result = String::new();
63 writeln!(&mut result, "┏{}┓", "━".repeat(80))?;
64 for l in lines {
65 let pad = 80 - l.chars().count();
66 writeln!(&mut result, "┃{}{}┃", l, " ".repeat(pad))?;
67 }
68 writeln!(&mut result, "┗{}┛", "━".repeat(80))?;
69 Ok(result)
70}More examples
63fn layout_text() -> Result<String, fmt::Error> {
64 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.";
65 let knuth_plass = KnuthPlass::new().with_threshold(F::MAX);
66 let lines = layout_paragraph(&text, &knuth_plass, F::from_num(80));
67 let mut result = String::new();
68 writeln!(&mut result, "┏{}┓", "━".repeat(80))?;
69 for l in lines {
70 let pad = 80 - l.chars().count();
71 writeln!(&mut result, "┃{}{}┃", l, " ".repeat(pad))?;
72 }
73 writeln!(&mut result, "┗{}┛", "━".repeat(80))?;
74 Ok(result)
75}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.