Skip to content

Commit 795671f

Browse files
committed
[geometry] fix 0.0 scalar formatting
1 parent e060e17 commit 795671f

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rasterize"
3-
version = "0.6.0"
3+
version = "0.6.1"
44
authors = ["Pavel Aslanov <asl.pavel@gmail.com>"]
55
description = "Simple and small 2D rendering library"
66
edition = "2021"

src/geometry.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,13 @@ impl ScalarFormatter {
7171
}
7272

7373
pub fn round_significant(value: f64, precision: usize) -> f64 {
74-
let shift = precision as i32 - value.abs().log10().ceil() as i32;
75-
let shift_factor = 10_f64.powi(shift);
76-
(value * shift_factor).round() / shift_factor
74+
if value.abs() < EPSILON {
75+
return 0.0;
76+
} else {
77+
let shift = precision as i32 - value.abs().log10().ceil() as i32;
78+
let shift_factor = 10_f64.powi(shift);
79+
(value * shift_factor).round() / shift_factor
80+
}
7781
}
7882
}
7983

@@ -825,4 +829,29 @@ mod tests {
825829
assert_eq!(expected, expected.to_string().parse()?);
826830
Ok(())
827831
}
832+
833+
#[test]
834+
fn test_scalar_format() -> Result<(), Error> {
835+
let value: Scalar = 0.1234567;
836+
assert_eq!(format!("{}", ScalarFormat(value)), "0.1235".to_owned());
837+
assert_eq!(format!("{:#}", ScalarFormat(value)), "0.1235".to_owned());
838+
assert_eq!(format!("{:.3}", ScalarFormat(value)), "0.123".to_owned());
839+
840+
let value: Scalar = 12.3001;
841+
assert_eq!(format!("{}", ScalarFormat(value)), "12.30".to_owned());
842+
assert_eq!(format!("{:#}", ScalarFormat(value)), "12.3".to_owned());
843+
assert_eq!(format!("{:.3}", ScalarFormat(value)), "12.3".to_owned());
844+
845+
let value: Scalar = 12300.0;
846+
assert_eq!(format!("{}", ScalarFormat(value)), "12300".to_owned());
847+
assert_eq!(format!("{:#}", ScalarFormat(value)), "12300".to_owned());
848+
assert_eq!(format!("{:.3}", ScalarFormat(value)), "12300".to_owned());
849+
850+
let value: Scalar = 0.0;
851+
assert_eq!(format!("{}", ScalarFormat(value)), "0".to_owned());
852+
assert_eq!(format!("{:#}", ScalarFormat(value)), "0".to_owned());
853+
assert_eq!(format!("{:.3}", ScalarFormat(value)), "0".to_owned());
854+
855+
Ok(())
856+
}
828857
}

src/path.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -594,27 +594,16 @@ impl PathSvgDisplay<'_> {
594594
) -> Result<Point, fmt::Error> {
595595
let point_tr = self.tr.apply(point);
596596
let point = previous.map_or_else(|| point_tr, |point_prev| point_tr - point_prev);
597-
598597
let x = point.x();
599598
let y = point.y();
600-
let eps = 10_f64.powi(-(out.precision().unwrap_or(4) as i32));
601-
602599
if sep && x >= 0.0 {
603600
out.write_str(" ")?;
604601
}
605-
if x.abs() < eps {
606-
out.write_str("0")?;
607-
} else {
608-
out.write_str(formatter.format_str(x))?;
609-
}
602+
out.write_str(formatter.format_str(x))?;
610603
if y >= 0.0 {
611604
out.write_str(",")?;
612605
}
613-
if y.abs() < eps {
614-
out.write_str("0")?;
615-
} else {
616-
out.write_str(formatter.format_str(y))?;
617-
}
606+
out.write_str(formatter.format_str(y))?;
618607
Ok(point_tr)
619608
}
620609
}

0 commit comments

Comments
 (0)