Refactor path_precision

This commit is contained in:
Chris Tsang
2021-07-27 21:01:20 +08:00
parent 0b292ad35f
commit 8439cc995d
2 changed files with 16 additions and 10 deletions

View File

@@ -62,7 +62,7 @@ fn color_image_to_svg(config: ConverterConfig) -> Result<(), String> {
let view = clusters.view(); let view = clusters.view();
let mut svg = SvgFile::new(width, height); let mut svg = SvgFile::new(width, height, config.path_precision);
for &cluster_index in view.clusters_output.iter().rev() { for &cluster_index in view.clusters_output.iter().rev() {
let cluster = view.get_cluster(cluster_index); let cluster = view.get_cluster(cluster_index);
let paths = cluster.to_compound_path( let paths = cluster.to_compound_path(
@@ -74,7 +74,7 @@ fn color_image_to_svg(config: ConverterConfig) -> Result<(), String> {
config.max_iterations, config.max_iterations,
config.splice_threshold config.splice_threshold
); );
svg.add_path(paths, cluster.residue_color(), config.path_precision); svg.add_path(paths, cluster.residue_color());
} }
write_svg(svg, config.output_path) write_svg(svg, config.output_path)
@@ -95,7 +95,7 @@ fn binary_image_to_svg(config: ConverterConfig) -> Result<(), String> {
let clusters = img.to_clusters(false); let clusters = img.to_clusters(false);
let mut svg = SvgFile::new(width, height); let mut svg = SvgFile::new(width, height, config.path_precision);
for i in 0..clusters.len() { for i in 0..clusters.len() {
let cluster = clusters.get_cluster(i); let cluster = clusters.get_cluster(i);
if cluster.size() >= config.filter_speckle_area { if cluster.size() >= config.filter_speckle_area {
@@ -106,7 +106,7 @@ fn binary_image_to_svg(config: ConverterConfig) -> Result<(), String> {
config.max_iterations, config.max_iterations,
config.splice_threshold, config.splice_threshold,
); );
svg.add_path(paths, Color::color(&ColorName::Black), config.path_precision); svg.add_path(paths, Color::color(&ColorName::Black));
} }
} }

View File

@@ -5,28 +5,28 @@ pub struct SvgFile {
pub paths: Vec<SvgPath>, pub paths: Vec<SvgPath>,
pub width: usize, pub width: usize,
pub height: usize, pub height: usize,
pub path_precision: Option<u32>,
} }
pub struct SvgPath { pub struct SvgPath {
pub path: CompoundPath, pub path: CompoundPath,
pub color: Color, pub color: Color,
pub path_precision: Option<u32>,
} }
impl SvgFile { impl SvgFile {
pub fn new(width: usize, height: usize) -> Self { pub fn new(width: usize, height: usize, path_precision: Option<u32>) -> Self {
SvgFile { SvgFile {
paths: vec![], paths: vec![],
width, width,
height, height,
path_precision,
} }
} }
pub fn add_path(&mut self, path: CompoundPath, color: Color, path_precision: Option<u32>) { pub fn add_path(&mut self, path: CompoundPath, color: Color) {
self.paths.push(SvgPath { self.paths.push(SvgPath {
path, path,
color, color,
path_precision,
}) })
} }
} }
@@ -40,7 +40,7 @@ impl fmt::Display for SvgFile {
)?; )?;
for path in &self.paths { for path in &self.paths {
path.fmt(f)?; path.fmt_with_precision(f, self.path_precision)?;
}; };
writeln!(f, "</svg>") writeln!(f, "</svg>")
@@ -49,7 +49,13 @@ impl fmt::Display for SvgFile {
impl fmt::Display for SvgPath { impl fmt::Display for SvgPath {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let (string, offset) = self.path.to_svg_string(true, PointF64::default(), self.path_precision); self.fmt_with_precision(f, None)
}
}
impl SvgPath {
fn fmt_with_precision(&self, f: &mut fmt::Formatter, precision: Option<u32>) -> fmt::Result {
let (string, offset) = self.path.to_svg_string(true, PointF64::default(), precision);
writeln!( writeln!(
f, "<path d=\"{}\" fill=\"{}\" transform=\"translate({},{})\"/>", f, "<path d=\"{}\" fill=\"{}\" transform=\"translate({},{})\"/>",
string, self.color.to_hex_string(), string, self.color.to_hex_string(),