Update dependencies, remove unnecessary RwLock on database
This commit is contained in:
parent
7526a82bb7
commit
7b5626c279
20 changed files with 1114 additions and 983 deletions
|
@ -4,11 +4,11 @@
|
|||
use std::{
|
||||
ffi::OsStr,
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
// use clap::builder::OsStr;
|
||||
use color_eyre::{eyre::bail, Result};
|
||||
use usvg_text_layout::TreeTextToPath;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Args {
|
||||
|
@ -37,7 +37,7 @@ enum ColorType {
|
|||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Renderer {
|
||||
fontdb: usvg_text_layout::fontdb::Database,
|
||||
fontdb: Arc<usvg::fontdb::Database>,
|
||||
colors: ColorType,
|
||||
size: (u32, u32),
|
||||
pub count: u64,
|
||||
|
@ -45,40 +45,35 @@ pub struct Renderer {
|
|||
|
||||
impl Renderer {
|
||||
pub fn new(args: &Args) -> Result<Self> {
|
||||
let mut db = usvg_text_layout::fontdb::Database::new();
|
||||
let mut db = usvg::fontdb::Database::default();
|
||||
db.load_system_fonts();
|
||||
|
||||
let mut this = Self {
|
||||
fontdb: db,
|
||||
fontdb: Arc::new(db),
|
||||
colors: ColorType::None,
|
||||
size: (args.width, args.height),
|
||||
count: 0,
|
||||
};
|
||||
|
||||
let colors = if args.colors.contains(':') {
|
||||
this.colors = if args.colors.contains(':') {
|
||||
//? object
|
||||
let obj = args
|
||||
.colors
|
||||
.split(',')
|
||||
.map(|s| {
|
||||
let s = s.split(':').collect::<Vec<&str>>();
|
||||
let obj = args.colors.split(',').map(|s| {
|
||||
let mut iter = s.split(':');
|
||||
let [Some(a), Some(b), None] = std::array::from_fn(|_| iter.next()) else {
|
||||
dbg!("Invalid color object, try checking help");
|
||||
return None;
|
||||
};
|
||||
|
||||
if s.len() < 2 {
|
||||
dbg!("Invalid color object, try checking help");
|
||||
return None;
|
||||
}
|
||||
Some((a.to_string(), b.to_string()))
|
||||
});
|
||||
|
||||
Some((s[0].to_string(), s[1].to_string()))
|
||||
let colors = obj
|
||||
.flatten()
|
||||
.map(|c| {
|
||||
std::fs::create_dir_all(args.output.join(&c.0))?;
|
||||
Ok(c)
|
||||
})
|
||||
.collect::<Vec<Option<(String, String)>>>();
|
||||
|
||||
let mut colors = Vec::new();
|
||||
|
||||
for c in obj.into_iter().flatten() {
|
||||
std::fs::create_dir_all(args.output.join(&c.0))?;
|
||||
|
||||
colors.push(c);
|
||||
}
|
||||
.collect::<std::io::Result<_>>()?;
|
||||
|
||||
ColorType::Object(colors)
|
||||
} else {
|
||||
|
@ -88,17 +83,17 @@ impl Renderer {
|
|||
// })
|
||||
// .collect::<Vec<String>>();
|
||||
|
||||
let mut colors = Vec::new();
|
||||
|
||||
for color in args.colors.split(',') {
|
||||
std::fs::create_dir_all(args.output.join(color))?;
|
||||
colors.push(color.to_string())
|
||||
}
|
||||
let colors = args
|
||||
.colors
|
||||
.split(',')
|
||||
.map(|color| -> std::io::Result<String> {
|
||||
std::fs::create_dir_all(args.output.join(color))?;
|
||||
Ok(color.to_string())
|
||||
})
|
||||
.collect::<std::io::Result<_>>()?;
|
||||
|
||||
ColorType::Array(colors)
|
||||
};
|
||||
|
||||
this.colors = colors;
|
||||
Ok(this)
|
||||
}
|
||||
|
||||
|
@ -144,10 +139,11 @@ impl Renderer {
|
|||
let opt = usvg::Options {
|
||||
// Get file's absolute directory.
|
||||
resources_dir: std::fs::canonicalize(fi).ok().and_then(|p| p.parent().map(|p| p.to_path_buf())),
|
||||
fontdb: self.fontdb.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let mut tree = match usvg::Tree::from_data(svg.as_bytes(), &opt) {
|
||||
let tree = match usvg::Tree::from_data(svg.as_bytes(), &opt) {
|
||||
Ok(v) => v,
|
||||
Err(_) => {
|
||||
dbg!("Failed to parse {fi:?}");
|
||||
|
@ -155,14 +151,17 @@ impl Renderer {
|
|||
}
|
||||
};
|
||||
|
||||
tree.convert_text(&self.fontdb);
|
||||
|
||||
let mut pixmap = tiny_skia::Pixmap::new(self.size.0, self.size.1).unwrap();
|
||||
let scale = {
|
||||
let x = tree.size().width() / self.size.0 as f32;
|
||||
let y = tree.size().height() / self.size.0 as f32;
|
||||
x.min(y)
|
||||
};
|
||||
|
||||
// log::info!("Rendering {fo:?}");
|
||||
|
||||
//? maybe handle this and possibly throw error if its none
|
||||
let _ = resvg::render(&tree, usvg::FitTo::Size(self.size.0, self.size.1), tiny_skia::Transform::default(), pixmap.as_mut());
|
||||
resvg::render(&tree, usvg::Transform::default().post_scale(scale, scale), &mut pixmap.as_mut());
|
||||
|
||||
pixmap.save_png(fo)?;
|
||||
self.count += 1;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue