WordCloud

Struct WordCloud 

Source
pub struct WordCloud {
    pub width: u32,
    pub height: u32,
    pub background: String,
    pub words: Vec<PlacedWord>,
    /* private fields */
}

Fields§

§width: u32§height: u32§background: String§words: Vec<PlacedWord>

Implementations§

Source§

impl WordCloud

Source

pub fn to_svg(&self) -> String

Examples found in repository?
examples/simple.rs (line 30)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let start = Instant::now();
7
8    let words = vec![
9        ("Rust", 100.0),
10        ("Performance", 80.0),
11        ("Safety", 70.0),
12        ("Concurrency", 60.0),
13        ("Fast", 50.0),
14        ("Memory", 45.0),
15        ("Efficient", 40.0),
16        ("Reliable", 35.0),
17        ("Community", 30.0),
18        ("Cargo", 25.0),
19        ("Crates", 20.0),
20        ("Macro", 15.0),
21    ];
22
23    println!("Generating word cloud with {} words...", words.len());
24
25    let wordcloud = generate(&words)?;
26
27    let png_data = wordcloud.to_png(2.0)?;
28    fs::write("output_simple.png", png_data)?;
29
30    let svg_data = wordcloud.to_svg();
31    fs::write("output_simple.svg", svg_data)?;
32
33    println!("Done! Saved to output_simple.png and output_simple.svg");
34    println!("Time elapsed: {:?}", start.elapsed());
35
36    Ok(())
37}
Source

pub fn to_png(&self, scale: f32) -> Result<Vec<u8>, Error>

Examples found in repository?
examples/advanced.rs (line 22)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    let words = vec![
6        WordInput::new("Custom", 90.0),
7        WordInput::new("Colors", 80.0),
8        WordInput::new("Seed", 70.0),
9        WordInput::new("Fixed", 60.0),
10        WordInput::new("Layout", 50.0),
11    ];
12
13    let wordcloud = WordCloudBuilder::new()
14        .size(600, 400)
15        .background("#1a1a1a")
16        .colors(vec!["#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#00FFFF"])
17        .seed(42)
18        .angles(vec![0.0])
19        .font_size_range(20.0, 100.0)
20        .build(&words)?;
21
22    fs::write("output_advanced.png", wordcloud.to_png(1.0)?)?;
23    println!("Generated advanced word cloud: output_advanced.png");
24
25    Ok(())
26}
More examples
Hide additional examples
examples/simple.rs (line 27)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let start = Instant::now();
7
8    let words = vec![
9        ("Rust", 100.0),
10        ("Performance", 80.0),
11        ("Safety", 70.0),
12        ("Concurrency", 60.0),
13        ("Fast", 50.0),
14        ("Memory", 45.0),
15        ("Efficient", 40.0),
16        ("Reliable", 35.0),
17        ("Community", 30.0),
18        ("Cargo", 25.0),
19        ("Crates", 20.0),
20        ("Macro", 15.0),
21    ];
22
23    println!("Generating word cloud with {} words...", words.len());
24
25    let wordcloud = generate(&words)?;
26
27    let png_data = wordcloud.to_png(2.0)?;
28    fs::write("output_simple.png", png_data)?;
29
30    let svg_data = wordcloud.to_svg();
31    fs::write("output_simple.svg", svg_data)?;
32
33    println!("Done! Saved to output_simple.png and output_simple.svg");
34    println!("Time elapsed: {:?}", start.elapsed());
35
36    Ok(())
37}
examples/mask_shape.rs (line 33)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    let output_path = "output_mask_heart.png";
6
7    let width = 800;
8    let height = 800;
9
10    println!("Generating words...");
11    let mut words = Vec::new();
12    for i in 0..300 {
13        words.push(WordInput::new("Love", 100.0));
14        words.push(WordInput::new("Rust", 80.0));
15        words.push(WordInput::new("Heart", 60.0 + (i as f32 % 40.0)));
16        words.push(WordInput::new("Mask", 40.0));
17    }
18
19    println!("Building word cloud...");
20
21    let scheme = ColorScheme::Default;
22    let wordcloud = WordCloudBuilder::new()
23        .size(width, height)
24        .mask_preset(MaskShape::Heart)
25        .color_scheme(scheme)
26        .background(scheme.background_color())
27        .padding(2)
28        .word_spacing(2.0)
29        .font_size_range(12.0, 80.0)
30        .build(&words)?;
31
32    println!("Saving to {}...", output_path);
33    fs::write(output_path, wordcloud.to_png(2.0)?)?;
34
35    println!("Done! Check {}", output_path);
36    Ok(())
37}
examples/chinese_dense.rs (line 46)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let mut words = generate_dense_chinese_data();
8    println!("Generated {} Chinese words.", words.len());
9
10    let start = Instant::now();
11
12    let width = 1600;
13    let height = 1100;
14
15    println!("Building word cloud ({}x{})...", width, height);
16
17    let count_len = words.len();
18    let dynamic_max_font_size = if count_len < 20 {
19        150.0
20    } else if count_len < 50 {
21        120.0
22    } else {
23        100.0
24    };
25
26    let max_weight = words.first().map(|w| w.weight).unwrap_or(100.0);
27    for word in &mut words {
28        if max_weight > 100.0 {
29            word.weight = word.weight.sqrt() * 10.0;
30        }
31    }
32
33    let scheme = ColorScheme::Default;
34    let wordcloud = WordCloudBuilder::new()
35        .size(width, height)
36        .color_scheme(scheme)
37        .background(scheme.background_color())
38        .padding(2)
39        .angles(vec![0.0, 90.0])
40        .word_spacing(2.0)
41        .font_size_range(14.0, dynamic_max_font_size)
42        .seed(2025)
43        .build(&words)?;
44
45    let output_path = "output_chinese_dense.png";
46    fs::write(output_path, wordcloud.to_png(2.0)?)?;
47
48    println!("Success! Time elapsed: {:?}", start.elapsed());
49    println!("Saved to {}", output_path);
50
51    Ok(())
52}

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

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>,

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V