mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 21:12:57 -05:00
Shapes: a file's polygon side or star point count is kept to 1000
A shape's stored add-on settings read their side or point count from the file with no upper bound, and a live star or polygon made from them took that count as it was, so a document could ask for billions of points. The count is now clamped to 2-1000 when it is read, as the Polygon and Star tools clamp it, the live polygon and star made from it are kept to 3-1000 sides and 2-1000 points, and shapes::star keeps to 1000 points.
This commit is contained in:
1 parent
5aadf6c616
commit
7e918324db
2 files changed
+61
-4
No files matched your search
@@ -455,9 +455,9 @@ pub fn polygon_configured(center: Point, radius: f64, sides: u32, rotation_deg:
|
||||
contour_with_rounding(&pts, rounded)
|
||||
}
|
||||
|
||||
/// Star with `points` tips (≥ 2), outer radius `r1`, inner radius `r2`, first tip up.
|
||||
/// Star with `points` tips (2–1000), outer radius `r1`, inner radius `r2`, first tip up.
|
||||
pub fn star(center: Point, r1: f64, r2: f64, points: u32, rotation_deg: f64) -> PathData {
|
||||
let n = points.max(2) * 2;
|
||||
let n = points.clamp(2, 1000) * 2;
|
||||
let rot = rotation_deg.to_radians();
|
||||
let pts: Vec<Point> = (0..n)
|
||||
.map(|i| {
|
||||
@@ -1485,6 +1485,12 @@ mod tests {
|
||||
assert!((d[0] - 10.0).abs() < 1e-9 && (d[1] - 5.0).abs() < 1e-9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn star_keeps_to_a_thousand_points() {
|
||||
assert_eq!(star(Point::ZERO, 10.0, 5.0, 5000, 0.0).anchor_count(), 2000);
|
||||
assert_eq!(star(Point::ZERO, 10.0, 5.0, 1, 0.0).anchor_count(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn straight_star_five_points() {
|
||||
let r2 = straight_star_inner_radius(100.0, 5);
|
||||
|
||||
@@ -35,6 +35,11 @@ fn default_pie() -> (f64, f64) {
|
||||
(0.0, 360.0)
|
||||
}
|
||||
|
||||
/// A polygon's sides or a star's points read from a file, clamped as the tools clamp them.
|
||||
fn de_sides<'de, D: serde::Deserializer<'de>>(d: D) -> Result<u32, D::Error> {
|
||||
u32::deserialize(d).map(|n| n.clamp(2, 1000))
|
||||
}
|
||||
|
||||
/// Parse an [`EllipseMode`] from a command or tool option string (`"slice"`/`"pie"`, `"arc"`/`"open"`,
|
||||
/// `"chord"`, `"whole"`/`"full"`/`"closed"`).
|
||||
pub fn parse_arc_mode(s: &str) -> Option<EllipseMode> {
|
||||
@@ -86,6 +91,9 @@ pub enum CustomShape {
|
||||
},
|
||||
Polygon {
|
||||
radius: f64,
|
||||
/// Sides, or a star's points: 2–1000, as the Polygon and Star tools give them; a file's
|
||||
/// count is clamped when read.
|
||||
#[serde(deserialize_with = "de_sides")]
|
||||
sides: u32,
|
||||
xf: Affine,
|
||||
#[serde(default)]
|
||||
@@ -184,7 +192,7 @@ impl CustomShape {
|
||||
{
|
||||
Some(LiveShape::Polygon {
|
||||
radius: *radius,
|
||||
sides: *sides,
|
||||
sides: (*sides).clamp(3, 1000),
|
||||
xf: *xf,
|
||||
radii: radii.clone(),
|
||||
kinds: kinds.clone(),
|
||||
@@ -211,7 +219,7 @@ impl CustomShape {
|
||||
Some(LiveShape::Star {
|
||||
r1: *radius,
|
||||
r2: (*radius * ratio.clamp(0.01, 1.0)).max(0.01),
|
||||
points: *sides,
|
||||
points: (*sides).clamp(2, 1000),
|
||||
xf: *xf,
|
||||
radii: radii.clone(),
|
||||
kinds: kinds.clone(),
|
||||
@@ -806,3 +814,46 @@ impl Tool for Box3dTool {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn read_back(spoke_ratio: Option<f64>, sides: u64) -> CustomShape {
|
||||
let shape = CustomShape::Polygon {
|
||||
radius: 10.0,
|
||||
sides: 5,
|
||||
xf: Affine::IDENTITY,
|
||||
radii: vec![],
|
||||
kinds: vec![],
|
||||
radii_relative: false,
|
||||
spoke_ratio,
|
||||
rounded: 0.0,
|
||||
randomized: 0.0,
|
||||
pie: (0.0, 360.0),
|
||||
arc_mode: EllipseMode::Whole,
|
||||
};
|
||||
let mut v = serde_json::to_value(&shape).unwrap_or_default();
|
||||
v["sides"] = json!(sides);
|
||||
serde_json::from_value(v).unwrap_or_else(|e| panic!("{e}"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_file_point_count_is_clamped_as_the_tools_clamp_it() {
|
||||
let star = read_back(Some(0.5), u64::from(u32::MAX));
|
||||
let CustomShape::Polygon { sides, .. } = &star else { panic!("a polygon") };
|
||||
assert_eq!(*sides, 1000);
|
||||
let CustomShape::Polygon { sides, .. } = read_back(None, 0) else { panic!("a polygon") };
|
||||
assert_eq!(sides, 2);
|
||||
let Some(LiveShape::Star { points, .. }) = star.to_live_shape() else { panic!("a live star") };
|
||||
assert_eq!(points, 1000);
|
||||
assert_eq!(star.to_path().anchor_count(), 2000);
|
||||
let polygon = read_back(None, u64::from(u32::MAX));
|
||||
let Some(LiveShape::Polygon { sides, .. }) = polygon.to_live_shape() else { panic!("a live polygon") };
|
||||
assert_eq!(sides, 1000);
|
||||
let Some(LiveShape::Polygon { sides, .. }) = read_back(None, 0).to_live_shape() else { panic!("a live polygon") };
|
||||
assert_eq!(sides, 3);
|
||||
let Some(LiveShape::Star { points, .. }) = read_back(Some(0.5), 0).to_live_shape() else { panic!("a live star") };
|
||||
assert_eq!(points, 2);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user