mirror of
https://github.com/storytold/AniCraft.git
synced 2026-11-25 03:53:23 +01:00
fix(svg): finish parser routing and reject missing moveto
Route the newer Continue Path entry point through the shared adapter. Require an initial moveto and assert precise preflight refusal variants. Focused geom/SVG tests: 137 + 31 passed. Quadratic gradient parity remains a separate merge blocker.
This commit is contained in:
1 parent
8ab42268d7
commit
42da2b7bba
3 files changed
+21
-4
No files matched your search
@@ -305,7 +305,7 @@ fn extend(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if d.len() > 4_000_000 {
|
||||
return Err(EngineError::Param("param 'd' is too long".into()));
|
||||
}
|
||||
let piece = anicraft_geom::svg::parse_path_data(d).map_err(|e| EngineError::Param(format!("param 'd': {e}")))?;
|
||||
let piece = anicraft_svg::parse_path_data(d).map_err(|e| EngineError::Param(format!("param 'd': {e}")))?;
|
||||
let at_start = match opt_str(p, "end")?.unwrap_or("end") {
|
||||
"end" => false,
|
||||
"start" => true,
|
||||
|
||||
@@ -16,6 +16,13 @@ fn v(p: Point) -> Vec2 {
|
||||
|
||||
/// Parses SVG path data into a [`Path`]: icon outlines, `path.d` commands and SVG import.
|
||||
pub fn parse_path_data(d: &str) -> Result<Path, String> {
|
||||
// The shared geometry parser permits an implicit origin; SVG and command input must
|
||||
// begin with moveto rather than manufacturing an anchor for malformed drawing commands.
|
||||
if let Some(first) = d.bytes().find(|byte| !matches!(byte, b' ' | b'\t' | b'\r' | b'\n'))
|
||||
&& !matches!(first, b'M' | b'm')
|
||||
{
|
||||
return Err("path data must start with moveto".into());
|
||||
}
|
||||
let subpaths = parse_path(d).map_err(|e| e.to_string())?;
|
||||
let mut path = Path::new();
|
||||
for sub in subpaths {
|
||||
@@ -68,6 +75,16 @@ mod tests {
|
||||
p.anchors().into_iter().map(|(_, a)| a).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nonempty_paths_require_an_initial_moveto() {
|
||||
for data in ["L10 10", "H10", "V10", "C0 0 1 1 2 2", "Q1 1 2 2", "Z", " l10 10"] {
|
||||
assert!(parse_path_data(data).is_err(), "accepted drawing command without moveto: {data}");
|
||||
}
|
||||
for data in ["", " \t\r\n", "m1 2 l3 4", " M0 0 L1 1"] {
|
||||
assert!(parse_path_data(data).is_ok(), "rejected valid path: {data:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_tokens_do_not_import_a_truncated_path() {
|
||||
for data in ["@", "M0 0 L1 1 @", "M0 0 L1 1 1e", "M0 0 L1 1 1e9999"] {
|
||||
|
||||
@@ -258,13 +258,13 @@ fn hostile_markup_is_refused_before_parsing() {
|
||||
prev = name;
|
||||
}
|
||||
bomb.push_str("]><svg xmlns=\"http://www.w3.org/2000/svg\"><text>&h;</text></svg>");
|
||||
assert!(matches!(import_with_fonts(bomb.as_bytes(), &fonts), Err(SvgError::Preflight(_))));
|
||||
assert!(matches!(import_with_fonts(bomb.as_bytes(), &fonts), Err(SvgError::Preflight(craft_svg::preflight::Error::Entities))));
|
||||
// Groups nested far past any drawing.
|
||||
let deep = format!("<svg xmlns=\"http://www.w3.org/2000/svg\">{}{}</svg>", "<g>".repeat(5000), "</g>".repeat(5000));
|
||||
assert!(matches!(import_with_fonts(deep.as_bytes(), &fonts), Err(SvgError::Preflight(_))));
|
||||
assert!(matches!(import_with_fonts(deep.as_bytes(), &fonts), Err(SvgError::Preflight(craft_svg::preflight::Error::TooDeep(_)))));
|
||||
// An unpacked SVGZ past the size limit.
|
||||
let huge = craft_svg::svgz::compress(&vec![b' '; (64 << 20) + 1]);
|
||||
assert!(matches!(import_with_fonts(&huge, &fonts), Err(SvgError::Preflight(_))));
|
||||
assert!(matches!(import_with_fonts(&huge, &fonts), Err(SvgError::Preflight(craft_svg::preflight::Error::Text(craft_svg::svgz::Error::TooLarge(_))))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user