Struct text_layout::KnuthPlass

source ·
pub struct KnuthPlass { /* private fields */ }
Expand description

Runs the Kunth-Plass line-breaking algorithm to calculate the optimal break points for a paragraph.

Implementations§

source§

impl KnuthPlass

source

pub fn new() -> Self

Creates a new with default parameter values.

Examples found in repository?
examples/readme.rs (line 37)
35
36
37
38
39
40
41
42
43
44
45
46
47
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)
}
source

pub fn with_flagged_demerit(self, flagged_demerit: f32) -> Self

Sets the demerit for flagged penalties. Defaults to 100. Referred to as 𝛂 in Kunth-Plass ’81.

source

pub fn with_fitness_demerit(self, fitness_demerit: f32) -> 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.

source

pub fn with_threshold(self, threshold: f32) -> 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. Referred to as 𝛒 in Knuth-Plass ’81.

Examples found in repository?
examples/readme.rs (line 37)
35
36
37
38
39
40
41
42
43
44
45
46
47
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)
}
source

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.

Trait Implementations§

source§

impl Default for KnuthPlass

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl ParagraphLayout for KnuthPlass

source§

fn layout_paragraph(&self, items: &[Item], line_width: f32) -> Vec<Line>

Lays out a paragraph with the given line width that consists of as list of items and returns the laid-out lines.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.