Skip to content

Commit

Permalink
linting and housekeeping
Browse files Browse the repository at this point in the history
  • Loading branch information
tooxo committed Jan 25, 2024
1 parent e4d33e6 commit a97ad6a
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 44 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ doctest = false

[dependencies]
rayon = "1.6.1"
svg = "0.13.0"
svg = "0.14.0"
image = { version = "0.24.5", optional = true }
edge-detection = { version = "0.2.6", optional = true }
rand = { version = "0.8.5", features = ["small_rng"] }
parking_lot = "0.12.1"
quadtree_rs = "0.1.3"
num-traits = "0.2.15"
swash = "0.1.6"
itertools = "0.10.5"
itertools = "0.12.0"
base64 = "0.21.2"
unicode-script = "0.5.5"
woff2 = { version = "0.3.0", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion example/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::env::args;
use rayon::prelude::ParallelString;
use wordcloud::font::{Font, FontSetBuilder};
use wordcloud::{StopWordsIterator, StopWordsIteratorPar};
use wordcloud::StopWordsIteratorPar;
use wordcloud::{clean, Dimensions, WordCloudBuilder};
use wordcloud::{RankedWords, StopWords};
use rayon::iter::ParallelIterator;
Expand Down
13 changes: 6 additions & 7 deletions src/cloud/word.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use std::cell::RefCell;
use std::ops::Range;
use svg::Node;
use svg::node::element::{Rectangle, Text};
use swash::scale::outline::Outline;
use swash::scale::ScaleContext;
use swash::shape::Direction::LeftToRight;
use swash::shape::ShapeContext;
Expand Down Expand Up @@ -87,7 +84,7 @@ impl<'a> Word<'a> {
.iter()
.map(|glyph| {
let outline =
scaler.scale_outline(glyph.id).unwrap_or(Outline::new());
scaler.scale_outline(glyph.id).unwrap_or_default();

let bounds = outline.bounds();
let bbox = Rect {
Expand Down Expand Up @@ -328,7 +325,7 @@ impl<'a> Word<'a> {
false
}

pub(crate) fn get_font_size_range(&self, dimensions: Dimensions) -> Range<f32> {
pub(crate) fn _get_font_size_range(&self, dimensions: Dimensions) -> Range<f32> {
let max = if self.rotation == Rotation::Ninety || self.rotation == Rotation::TwoSeventy {
// compare mainly with height
dimensions.width() as f32 * 0.8
Expand Down Expand Up @@ -400,6 +397,8 @@ impl<'a> WordBuilder<'a> {

#[test]
fn test() {
use svg::Node;
use svg::node::element::Text;
let mut font = Vec::new();
font.extend_from_slice(include_bytes!("../../example/assets/OpenSans-Regular.ttf"));
let f = Font::from_data(&mut font).unwrap();
Expand All @@ -417,7 +416,7 @@ fn test() {
.set("stroke-width", 1)
.set("d", word.d());

let p2 = Rectangle::new()
let p2 = svg::node::element::Rectangle::new()
.set("stroke", "green")
.set("stroke-width", 1)
.set("fill", "none")
Expand All @@ -440,5 +439,5 @@ fn test() {

dbg!(word.bounding_box);

svg::save("test.svg", &document);
svg::save("test.svg", &document).expect("TODO: panic message");
}
12 changes: 2 additions & 10 deletions src/cloud/word_cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ impl<'a> WordCloud<'a> {

let search_area = AreaBuilder::default()
.anchor((pos_x as u64, pos_y as u64).into())
.dimensions(((4.) as u64, (4.) as u64))
.dimensions((4. as u64, 4. as u64))
.build()
.expect("Error while calculating dimensions");

let insert_area = AreaBuilder::default()
.anchor(((x as f32) as u64, (y as f32) as u64).into())
.dimensions(((1.) as u64, (1.) as u64))
.dimensions((1. as u64, 1. as u64))
.build()
.expect("Error while calculating dimensions");

Expand Down Expand Up @@ -317,14 +317,6 @@ impl<'a> WordCloud<'a> {
Add new words to the [`WordCloud`]. For the best results, call this function only once.
*/
pub fn write_content(&self, content: RankedWords, max_word_count: usize) {
let max = content
.0
.iter()
.take(max_word_count)
.map(|x| (x.count() as f32))
.sum::<f32>()
/ max_word_count as f32;

let max = content.0.iter().max_by_key(|x| x.count()).unwrap().count() as f32;

let inp: Vec<WordBuilder> = content
Expand Down
13 changes: 5 additions & 8 deletions src/common/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::hash::{Hash, Hasher};
use std::io::Cursor;

use std::sync::Arc;
use swash::text::{Codepoint, Script};
use swash::text::{Codepoint};

use swash::scale::ScaleContext;
use swash::{FontRef, StringId, Tag};
Expand Down Expand Up @@ -42,7 +42,7 @@ pub type FontLoadingError = String;
pub type FontLoadingResult<T> = Result<T, FontLoadingError>;

/**
Represents a Font stored in memory. By default it supports `OTF` and `TTF` fonts, with
Represents a Font stored in memory. By default, it supports `OTF` and `TTF` fonts, with
the create features `woff` and `woff2` it also supports loading `WOFF` fonts.
*/
pub struct Font<'a> {
Expand All @@ -51,7 +51,7 @@ pub struct Font<'a> {
font_type: FontType,
supported_scripts: HashSet<CScript>,
packed_font_data: Option<Vec<u8>>,
approximate_pixel_width: f32,
_approximate_pixel_width: f32,
}

impl<'a> Font<'a> {
Expand All @@ -77,7 +77,7 @@ impl<'a> Font<'a> {
}

#[allow(clippy::unwrap_used)]
scripts_in_specs.insert(CScript::try_from(Script::Common).unwrap());
scripts_in_specs.insert(CScript::try_from(swash::text::Script::Common).unwrap());

scripts_in_specs
}
Expand Down Expand Up @@ -148,7 +148,7 @@ impl<'a> Font<'a> {
font_type,
supported_scripts: Font::identify_scripts_in_font(&re),
packed_font_data: packed_data,
approximate_pixel_width: outline.bounds().width() / 20.,
_approximate_pixel_width: outline.bounds().width() / 20.,
})
}

Expand All @@ -168,9 +168,6 @@ impl<'a> Font<'a> {
&self.packed_font_data
}

pub(crate) fn approximate_pixel_width(&self) -> f32 {
self.approximate_pixel_width
}
#[allow(dead_code)]
pub(crate) fn supported_features(&self) -> impl IntoIterator<Item = (Tag, u16)> + '_ {
self.reference().features().map(|f| (f.tag(), 1))
Expand Down
2 changes: 1 addition & 1 deletion src/filtering/stop_words.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl StopWords {
let entry = self
.stop_word_map
.entry(script)
.or_insert_with(HashSet::new);
.or_default();
entry.insert(word.clone());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/point.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::{Add, Mul, Range, Sub};
use std::ops::{Add, Mul, Sub};

#[derive(Copy, Clone, Debug)]
pub(crate) struct Point<T> {
Expand Down
12 changes: 0 additions & 12 deletions src/types/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,18 +205,6 @@ where
}
}

impl<T> Rect<T>
where
T: PartialOrd + Copy + Sized,
{
pub(crate) fn get_intersection(&self, rhs: &Rect<T>) -> Rect<T> {
Rect {
min: self.min.max(&rhs.min),
max: self.max.min(&rhs.max),
}
}
}

impl<T> Rect<T>
where
T: Copy + Sub<Output = T>,
Expand Down
4 changes: 2 additions & 2 deletions src/types/rotation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::types::point::Point;
use crate::types::rect::Rect;
use rand::{thread_rng, Rng};
use rand::random;

#[derive(Copy, Clone, Debug, PartialEq)]
#[allow(dead_code)]
Expand Down Expand Up @@ -72,7 +72,7 @@ impl Rotation {
}

pub(crate) fn random() -> Self {
let (a, b): (bool, bool) = (thread_rng().gen(), thread_rng().gen());
let (a, b): (bool, bool) = (random(), random());
match (a, b) {
(true, true) => Rotation::Zero,
(true, false) => Rotation::Ninety,
Expand Down

0 comments on commit a97ad6a

Please sign in to comment.