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 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() {
let cluster = view.get_cluster(cluster_index);
let paths = cluster.to_compound_path(
@@ -74,7 +74,7 @@ fn color_image_to_svg(config: ConverterConfig) -> Result<(), String> {
config.max_iterations,
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)
@@ -95,7 +95,7 @@ fn binary_image_to_svg(config: ConverterConfig) -> Result<(), String> {
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() {
let cluster = clusters.get_cluster(i);
if cluster.size() >= config.filter_speckle_area {
@@ -106,7 +106,7 @@ fn binary_image_to_svg(config: ConverterConfig) -> Result<(), String> {
config.max_iterations,
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 width: usize,
pub height: usize,
pub path_precision: Option<u32>,
}
pub struct SvgPath {
pub path: CompoundPath,
pub color: Color,
pub path_precision: Option<u32>,
}
impl SvgFile {
pub fn new(width: usize, height: usize) -> Self {
pub fn new(width: usize, height: usize, path_precision: Option<u32>) -> Self {
SvgFile {
paths: vec![],
width,
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 {
path,
color,
path_precision,
})
}
}
@@ -40,7 +40,7 @@ impl fmt::Display for SvgFile {
)?;
for path in &self.paths {
path.fmt(f)?;
path.fmt_with_precision(f, self.path_precision)?;
};
writeln!(f, "</svg>")
@@ -49,7 +49,13 @@ impl fmt::Display for SvgFile {
impl fmt::Display for SvgPath {
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!(
f, "<path d=\"{}\" fill=\"{}\" transform=\"translate({},{})\"/>",
string, self.color.to_hex_string(),