wcloud 0.1.0

Generate beautiful word clouds with support for masks, custom fonts, custom coloring functions, and more.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
use std::fs;
use std::path::{PathBuf};
use image::{GrayImage, Luma, RgbaImage, Rgba};
use ab_glyph::{PxScale, Point, point, FontVec, Font, Glyph};
use palette::{Pixel, Srgb, Hsl, IntoColor};
use std::process::exit;
use woff2::decode::{convert_woff2_to_ttf, is_woff2};

mod text;
use text::GlyphData;
pub mod sat;
mod tokenizer;
pub use tokenizer::{Tokenizer, DEFAULT_EXCLUDE_WORDS_TEXT};

use nanorand::{Rng, WyRand};
use crate::sat::{Rect, Region};

#[cfg(feature = "visualize")]
mod visualize;
#[cfg(feature = "visualize")]
use crate::visualize::{Init, Message};

pub struct Word<'a> {
    pub text: &'a str,
    pub font: &'a FontVec,
    pub font_size: PxScale,
    pub glyphs: GlyphData,
    pub rotated: bool,
    pub position: Point,
    pub frequency: f32,
    pub index: usize,
}

// TODO: Figure out a better way to structure this
pub enum WordCloudSize {
    FromDimensions { width: u32, height: u32 },
    FromMask(GrayImage),
}

pub struct WordCloud {
    tokenizer: Tokenizer,
    background_color: Rgba<u8>,
    pub font: FontVec,
    min_font_size: f32,
    max_font_size: Option<f32>,
    font_step: f32,
    word_margin: u32,
    word_rotate_chance: f64,
    relative_font_scaling: f32,
    rng_seed: Option<u64>,
}

impl Default for WordCloud {
    fn default() -> Self {
        let font = FontVec::try_from_vec(include_bytes!("../fonts/Ubuntu-B.ttf").to_vec()).unwrap();

        WordCloud {
            tokenizer: Tokenizer::default(),
            background_color: Rgba([0, 0, 0, 255]),
            font,
            min_font_size: 4.0,
            max_font_size: None,
            font_step: 1.0,
            word_margin: 2,
            word_rotate_chance: 0.10,
            relative_font_scaling: 0.5,
            rng_seed: None,
        }
    }
}

impl WordCloud {
    pub fn with_tokenizer(mut self, value: Tokenizer) -> Self {
        self.tokenizer = value;
        self
    }
    pub fn with_background_color(mut self, value: Rgba<u8>) -> Self {
        self.background_color = value;
        self
    }
    pub fn with_font(mut self, value: FontVec) -> Self {
        self.font = value;
        self
    }
    pub fn with_font_from_path(mut self, path: PathBuf) -> Self {
        let font_file = if path.extension() == Some("woff2".as_ref()) {
            let buffer = fs::read(path).unwrap();
            assert!(is_woff2(&buffer));
            convert_woff2_to_ttf(&mut std::io::Cursor::new(buffer)).expect("Invalid WOFF2 file")
        }
        else {
            fs::read(path).expect("Unable to read font file")
        };

        self.font = FontVec::try_from_vec(font_file)
            .expect("Font file may be invalid");

        self
    }
    pub fn with_min_font_size(mut self, value: f32) -> Self {
        assert!(value >= 0.0, "The minimum font size for a word cloud cannot be less than 0");
        self.min_font_size = value;
        self
    }
    pub fn with_max_font_size(mut self, value: Option<f32>) -> Self {
        self.max_font_size = value;
        self
    }
    pub fn with_font_step(mut self, value: f32) -> Self {
        self.font_step = value;
        self
    }
    pub fn with_word_margin(mut self, value: u32) -> Self {
        self.word_margin = value;
        self
    }
    pub fn with_word_rotate_chance(mut self, value: f64) -> Self {
        self.word_rotate_chance = value;
        self
    }
    pub fn with_relative_font_scaling(mut self, value: f32) -> Self {
        assert!((0.0..=1.0).contains(&value), "Relative scaling must be between 0 and 1");
        self.relative_font_scaling = value;
        self
    }
    pub fn with_rng_seed(mut self, value: u64) -> Self {
        self.rng_seed.replace(value);
        self
    }
}

impl WordCloud {
    fn generate_from_word_positions(
        rng: &mut WyRand,
        width: u32,
        height: u32,
        word_positions: Vec<Word>,
        scale: f32,
        background_color: Rgba<u8>,
        color_func: fn(&Word, &mut WyRand) -> Rgba<u8>
    ) -> RgbaImage {
        // TODO: Refactor this so that we can fail earlier
        if !(0.0..=100.0).contains(&scale) {
            // TODO: Idk if this is good practice
            // println!("The scale must be between 0 and 100 (both exclusive)");
            exit(1);
        }

        let mut final_image_buffer = RgbaImage::from_pixel((width as f32 * scale) as u32, (height as f32 * scale) as u32, background_color);

        for mut word in word_positions.into_iter() {
            let col = color_func(&word, rng);

            if scale != 1.0 {
                word.font_size.x *= scale;
                word.font_size.y *= scale;

                word.position.x *= scale;
                word.position.y *= scale;

                word.glyphs = text::text_to_glyphs(word.text, word.font, word.font_size);
            }

            text::draw_glyphs_to_rgba_buffer(&mut final_image_buffer, word.glyphs, word.font, word.position, word.rotated, col);
        }

        final_image_buffer
    }

    fn check_font_size(font_size: &mut f32, font_step: f32, min_font_size: f32) -> bool {
        let next_font_size = *font_size - font_step;

        if next_font_size >= min_font_size && next_font_size > 0.0 {
            *font_size = next_font_size;
            true
        }
        else {
            false
        }
    }

    fn glyphs_height(&self, glyphs: &[Glyph]) -> u32 {
        glyphs.iter().map(|g| {
            let outlined = self.font.outline_glyph(g.clone())
                .expect("Unable to outline glyph");

            let bounds = outlined.px_bounds();
            bounds.height() as u32
        }).max().expect("No glyphs!")
    }

    fn text_dimensions_at_font_size(&self, text: &str, font_size: PxScale) -> Rect {
        let glyphs = text::text_to_glyphs(text, &self.font, font_size);
        Rect { width: glyphs.width + self.word_margin, height: glyphs.height + self.word_margin }
    }

    pub fn generate_from_text(&self, text: &str, size: WordCloudSize, scale: f32) -> RgbaImage {
        self.generate_from_text_with_color_func(text, size, scale, random_color_rgba)
    }

    pub fn generate_from_text_with_color_func(
        &self,
        text: &str,
        size: WordCloudSize,
        scale: f32,
        color_func: fn(&Word, &mut WyRand) -> Rgba<u8>
    ) -> RgbaImage {
        let words = self.tokenizer.get_normalized_word_frequencies(text);

        let (mut summed_area_table, mut gray_buffer) = match size {
            WordCloudSize::FromDimensions { width, height } => {
                let buf = GrayImage::from_pixel(width, height, Luma([0]));
                let mut summed_area_table = vec![0; buf.len()];

                u8_to_u32_vec(&buf, &mut summed_area_table);
                (summed_area_table, buf)
            },
            WordCloudSize::FromMask(image) => {
                let mut table = vec![0; image.len()];

                u8_to_u32_vec(&image, &mut table);
                sat::to_summed_area_table(
                    &mut table, image.width() as usize, 0
                );
                (table, image)
            }
        };

        #[cfg(feature = "visualize")]
        {
            let mask = if matches!(WordCloudSize::FromMask, _size) {
                Some(gray_buffer.to_vec())
            }
            else {
                None
            };

            let serialized = serde_json::to_string(&Message::InitMessage(Init {
                width: gray_buffer.width(),
                height: gray_buffer.height(),
                mask,
                font: self.font.as_slice().to_vec(),
                background_color: self.background_color.0,
            })).unwrap();
            println!("{}", serialized);
        };

        let mut final_words = Vec::with_capacity(words.len());

        let mut last_freq = 1.0;

        let mut rng = match self.rng_seed {
            Some(seed) => WyRand::new_seed(seed),
            None => WyRand::new(),
        };

        let first_word = words.first()
            .expect("There are no words!");

        let skip_list = create_mask_skip_list(&gray_buffer);

        let mut font_size = {
            let rect_at_image_height = self.text_dimensions_at_font_size(
                first_word.0,
                PxScale::from(gray_buffer.height() as f32 * 0.95)
            );

            let height_ratio = rect_at_image_height.height as f32 / rect_at_image_height.width as f32;

            let mut start_height = gray_buffer.width() as f32 * height_ratio;

            if matches!(WordCloudSize::FromMask, _size) {
                let black_pixels = gray_buffer.as_raw().iter().filter(|p| **p == 0).count();
                let available_space: f32 = black_pixels as f32 / gray_buffer.len() as f32;
                start_height *= available_space;
            }

            if let Some(max) = self.max_font_size {
                start_height.min(max)
            }
            else {
                start_height
            }
        };

        'outer: for (word, freq) in &words {
            if !self.tokenizer.repeat && self.relative_font_scaling != 0.0 {
                font_size *= self.relative_font_scaling * (freq / last_freq) + (1.0 - self.relative_font_scaling);
            }

            if font_size < self.min_font_size {
                break;
            }

            let initial_font_size = font_size;

            let mut should_rotate = rng.generate::<u8>() <= (255.0 * self.word_rotate_chance) as u8;
            let mut tried_rotate = false;
            let mut glyphs;

            let has_mask = matches!(WordCloudSize::FromMask, _size);

            let pos = loop {
                glyphs = text::text_to_glyphs(word, &self.font, PxScale::from(font_size));
                let glyphs_height = self.glyphs_height(&glyphs.glyphs);

                let rect = if !should_rotate {
                    Rect { width: glyphs.width + self.word_margin, height: glyphs.height + self.word_margin }
                }
                else {
                    Rect { width: glyphs.height + self.word_margin, height: glyphs.width + self.word_margin }
                };

                #[cfg(feature = "visualize")]
                {
                    let serialized = serde_json::to_string(&Message::ChangeWordMessage(visualize::Word {
                        text: word.to_string(),
                        font_size: font_size as u32,
                        rect_width: rect.width,
                        rect_height: rect.height,
                        rotation: if should_rotate { 270 } else { 0 },
                    })).unwrap();
                    println!("{}", serialized);
                };

                if rect.width > gray_buffer.width() || rect.height > gray_buffer.height() {
                    if Self::check_font_size(&mut font_size, self.font_step, self.min_font_size) {
                        continue;
                    }
                    else {
                        break 'outer;
                    };
                }

                if has_mask {
                    match sat::find_space_for_rect_masked(&summed_area_table, gray_buffer.width(), gray_buffer.height(), &skip_list, &rect, &mut rng) {
                        Some(pos) => {
                            let half_margin = self.word_margin as f32 / 2.0;
                            let x = pos.x as f32 + half_margin;
                            let y = pos.y as f32 + half_margin;

                            break point(x, y)
                        },
                        None => {
                            if !Self::check_font_size(&mut font_size, self.font_step, self.min_font_size) {
                                if !tried_rotate {
                                    should_rotate = true;
                                    tried_rotate = true;
                                    font_size = initial_font_size;
                                }
                                else {
                                    break 'outer;
                                }
                            }
                        }
                    };
                }
                else {
                    match sat::find_space_for_rect(&summed_area_table, gray_buffer.width(), gray_buffer.height(), &rect, &mut rng) {
                        Some(pos) => {
                            let half_margin = self.word_margin as f32 / 2.0;
                            let x = pos.x as f32 + half_margin;
                            let y = pos.y as f32 + half_margin;

                            break point(x, y)
                        },
                        None => {
                            if !Self::check_font_size(&mut font_size, self.font_step, self.min_font_size) {
                                if !tried_rotate {
                                    should_rotate = true;
                                    tried_rotate = true;
                                    font_size = initial_font_size;
                                }
                                else {
                                    break 'outer;
                                }
                            }
                        }
                    };
                }

            };
            text::draw_glyphs_to_gray_buffer(&mut gray_buffer, glyphs.clone(), &self.font, pos, should_rotate);

            #[cfg(feature = "visualize")]
            {
                let serialized = serde_json::to_string(&Message::PlacedWordMessage(visualize::PlaceWord {
                    text: word.to_string(),
                    font_size: font_size as u32,
                    x: pos.x as u32,
                    y: pos.y as u32,
                    rotation: if should_rotate { 270 } else { 0 },
                })).unwrap();
                println!("{}", serialized);
            };

            final_words.push(Word {
                text: word,
                font: &self.font,
                font_size: PxScale::from(font_size),
                glyphs: glyphs.clone(),
                rotated: should_rotate,
                position: pos,
                frequency: *freq,
                index: final_words.len()
            });

            // TODO: Do a partial sat like the Python implementation
            u8_to_u32_vec(&gray_buffer, &mut summed_area_table);
            let start_row = (pos.y - 1.0).min(0.0) as usize;
            sat::to_summed_area_table(&mut summed_area_table, gray_buffer.width() as usize, start_row);

            last_freq = *freq;
        }

        WordCloud::generate_from_word_positions(
            &mut rng, gray_buffer.width(), gray_buffer.height(), final_words, scale, self.background_color, color_func
        )
    }
}

fn random_color_rgba(_word: &Word, rng: &mut WyRand) -> Rgba<u8> {
    let hue: u8 = rng.generate_range(0..255);
    // TODO: Python uses 0.8 for the saturation but it looks too washed out when used here
    //   Maybe something to do with the linear stuff?
    //   It's not really a problem just curious
    //   https://github.com/python-pillow/Pillow/blob/66209168847ad1b55190a629b49cc6ba829efe92/src/PIL/ImageColor.py#L83
    let col = Hsl::new(hue as f32, 1.0, 0.5);
    let rgb: Srgb = col.into_color();

    let raw: [u8; 3] = rgb.into_format()
        .into_raw();

    Rgba([raw[0], raw[1], raw[2], 1])
}

// TODO: This doesn't seem particularly efficient
fn u8_to_u32_vec(buffer: &GrayImage, dst: &mut [u32]) {
    for (i, el) in buffer.as_raw().iter().enumerate() {
        dst[i] = *el as u32;
    }
}

/// Crops the image to its boundaries
///
/// Useful for making the search space smaller when looking for a space to place a word
fn _find_image_bounds(img: &GrayImage) -> Region {
    let mut min_x = img.width();
    let mut min_y = 0;
    let mut max_x = 0;
    let mut max_y = 0;

    let mut found_min_y = false;
    for (y, mut row) in img.enumerate_rows() {
        if let Some(pos) = row.position(|p| p.2 == &Luma::from([0])) {
            if !found_min_y {
                min_y = y;
                found_min_y = true;
            }

            max_y = y;

            if pos < min_x as usize {
                min_x = pos as u32;
            }

            if let Some(last_pos) = row.filter(|p| p.2 == &Luma::from([0])).last() {
                if last_pos.0 > max_x {
                    max_x = last_pos.0;
                }
            }
            else if pos > max_x as usize {
                max_x = pos as u32;
            }
        }
    }

    let width = max_x - min_x;
    let height = max_y - min_y;

    Region { x: min_x, y: min_y, width, height }
}

/// Finds the outline of a mask on the x axis
///
/// Useful for skipping white pixels that can't be used when looking for a space to place a word
fn create_mask_skip_list(img: &GrayImage) -> Vec<(usize, usize)> {
    img.rows().map(|mut row| {
        let furthest_left = row.rposition(|p| p == &Luma::from([0])).unwrap_or(img.width() as usize);
        let furthest_right = row.position(|p| p == &Luma::from([0])).unwrap_or(0);

        (furthest_right, furthest_left)
    }).collect()
}