mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-06 05:53:08 -05:00
Shapes: a star's stored settings read back with up to 2000 corners
A star with a radius on each corner stores its corner list in its shape settings entry. A 1000-point star's entry is longer than the 4,096-byte read limit, so it was dropped on read and the star lost its pie angles and arc mode on the next edit. The limit is now 128 KiB, which holds the longest entry a 1000-point star can write.
This commit is contained in:
1 parent
177d0c6700
commit
69eae94826
1 file changed
+48
-2
@@ -24,8 +24,9 @@ pub const ID: &str = "box3d";
|
||||
/// The `vc-` prefix marks it as internal so SVG export omits it from `data-*` attributes.
|
||||
pub const KEY: &str = "vc-shape-tools";
|
||||
|
||||
/// Maximum stored JSON length read back from a file.
|
||||
const MAX_STORED: usize = 4096;
|
||||
/// Maximum stored JSON length read back from a file: room for a 1000-point star's 2000 corner
|
||||
/// radii and kinds.
|
||||
const MAX_STORED: usize = 128 * 1024;
|
||||
|
||||
fn whole_mode() -> EllipseMode {
|
||||
EllipseMode::Whole
|
||||
@@ -863,4 +864,49 @@ mod tests {
|
||||
*pie = (0.0, 90.0);
|
||||
assert!(matches!(star.to_live_shape(), Some(LiveShape::Star { points: 5, .. })), "{star:?}");
|
||||
}
|
||||
|
||||
/// A 1000-point star with a radius on each of its 2000 corners, written and read back.
|
||||
fn round_trip(arc_mode: EllipseMode) -> CustomShape {
|
||||
let mut star = read_back(Some(0.5), 1000);
|
||||
let CustomShape::Polygon { radii, kinds, pie, arc_mode: mode, .. } = &mut star else { panic!("a polygon") };
|
||||
*radii = vec![12.0; 2000];
|
||||
*kinds = vec![CornerKind::Round; 2000];
|
||||
*pie = (0.0, 90.0);
|
||||
*mode = arc_mode;
|
||||
let mut node = Node::path(vectorcraft_doc::NodeId(1), PathData::default(), vectorcraft_doc::Appearance::default_art());
|
||||
star.write_to_node(&mut node);
|
||||
CustomShape::read(&node).unwrap_or_else(|| panic!("a shape"))
|
||||
}
|
||||
|
||||
/// A live star's stored entry holds a long corner list, and its pie angles still read back.
|
||||
#[test]
|
||||
fn a_live_star_with_many_corners_keeps_its_pie_angles() {
|
||||
let back = round_trip(EllipseMode::Whole);
|
||||
let CustomShape::Polygon { pie, radii, kinds, .. } = &back else { panic!("a polygon") };
|
||||
assert_eq!(*pie, (0.0, 90.0));
|
||||
assert_eq!(radii.len(), 2000);
|
||||
assert_eq!(kinds.len(), 2000);
|
||||
}
|
||||
|
||||
/// A sliced star has no `LiveShape`, so its stored entry holds every corner and still reads back.
|
||||
#[test]
|
||||
fn a_sliced_star_with_many_corners_reads_back() {
|
||||
let back = round_trip(EllipseMode::Slice);
|
||||
let CustomShape::Polygon { pie, radii, arc_mode, .. } = &back else { panic!("a polygon") };
|
||||
assert_eq!(*pie, (0.0, 90.0));
|
||||
assert_eq!(*arc_mode, EllipseMode::Slice);
|
||||
assert_eq!(radii.len(), 2000);
|
||||
}
|
||||
|
||||
/// The longest corner values a 1000-point star can hold still fit in the stored entry.
|
||||
#[test]
|
||||
fn the_longest_star_entry_fits_the_read_limit() {
|
||||
let mut star = read_back(Some(0.5), 1000);
|
||||
let CustomShape::Polygon { radius, radii, kinds, .. } = &mut star else { panic!("a polygon") };
|
||||
*radius = -f64::MAX;
|
||||
*radii = vec![-1.2345678901234567e-300; 2000];
|
||||
*kinds = vec![CornerKind::InvertedRound; 2000];
|
||||
let len = serde_json::to_string(&star).map(|s| s.len()).unwrap_or(usize::MAX);
|
||||
assert!(len <= MAX_STORED, "{len}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user