Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVG cloud word. #14

Open
eufelipemateus opened this issue Feb 16, 2024 · 7 comments
Open

SVG cloud word. #14

eufelipemateus opened this issue Feb 16, 2024 · 7 comments

Comments

@eufelipemateus
Copy link

This lib generate SVG cloud word?

@eufelipemateus eufelipemateus changed the title SBG cloud word. SVG cloud word. Feb 16, 2024
@isaackd
Copy link
Owner

isaackd commented Apr 14, 2024

At the moment it only outputs to a PNG file, but in theory it wouldn't be hard to modify it to support SVG.

wcloud/src/lib.rs

Lines 133 to 168 in 21aee44

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
}

This function could be changed to place the words in an SVG file since all the necessary data is there: word, font size, x, y.

@eufelipemateus
Copy link
Author

Cool! i am starting change here will try make this feature. Still not working yet

eufelipemateus@baf0f7a

@isaackd
Copy link
Owner

isaackd commented Apr 15, 2024

Looks good! Excited to try it out.

@isaackd
Copy link
Owner

isaackd commented May 1, 2024

image

I checked out your branch and was able to modify it to get an SVG word cloud. Everything is slightly off because of the rendering differences between theab_glyph library used for calculating word positions and SVG though. Let me know if you're interested in trying to take this further and I can make a PR on your repo with the code.

@extrawurst
Copy link

This feature would be dope! have not found any other wordcloud implementation supporting svg

@eufelipemateus
Copy link
Author

image

I checked out your branch and was able to modify it to get an SVG word cloud. Everything is slightly off because of the rendering differences between theab_glyph library used for calculating word positions and SVG though. Let me know if you're interested in trying to take this further and I can make a PR on your repo with the code.

Hello @isaackd Yes, I am interested in help. If you can do the PR to fix it I'll be happy. I can generate the svg but I still can't create the cloud itself.

@eufelipemateus
Copy link
Author

At the moment it only outputs to a PNG file, but in theory it wouldn't be hard to modify it to support SVG.

wcloud/src/lib.rs

Lines 133 to 168 in 21aee44

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
}

This function could be changed to place the words in an SVG file since all the necessary data is there: word, font size, x, y.

Yes, I couldn't generate the svg, now I can't create the cloud.
I want to create another function to allow me to generate only svg and keep the png generation function as it is today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants