mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 19:14:26 +00:00
svg: share bounded compatibility transform parsing
Preserve EffectCraft source from 4035d181329e847d7a57bf3d059502ea2c91b560, based on Brandon Thomas original 94e1c86d89a866771f80ad1610afda9aa12e85d8. Algorithm unchanged; module imports only adapted. Co-authored-by: Brandon Thomas <echelon@gmail.com>
This commit is contained in:
committed by
Dylan M. Taylor
co-authored by
Brandon Thomas
parent
453b0da782
commit
399ecc74de
@@ -35,3 +35,6 @@ pub mod write;
|
||||
pub mod prefix_path;
|
||||
/// Compatibility CSS subset for simple SVG importers.
|
||||
pub mod simple_css;
|
||||
|
||||
/// Prefix-preserving SVG transform-list compatibility.
|
||||
pub mod transform_compat;
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
//! Prefix-preserving transform-list compatibility for existing SVG importers.
|
||||
//! At most six numbers are retained per operation; input scanning is not byte-capped.
|
||||
//! Unknown/invalid-arity operations are ignored, unterminated suffixes stop parsing.
|
||||
//! Matrix overflow behavior is unchanged; callers retain their geometry validation.
|
||||
|
||||
use crate::prefix_path as pathdata;
|
||||
use kurbo::Affine;
|
||||
|
||||
/// Parse transforms using the established EffectCraft recovery and composition order.
|
||||
pub fn parse_transform(s: &str) -> Affine {
|
||||
let mut m = Affine::IDENTITY;
|
||||
let mut rest = s;
|
||||
while let Some(open) = rest.find('(') {
|
||||
let name = rest[..open].trim().trim_start_matches(',').trim();
|
||||
let Some(close) = rest[open..].find(')') else { break };
|
||||
let (a, too_many) = pathdata::numbers_bounded(&rest[open + 1..open + close], 6);
|
||||
rest = &rest[open + close + 1..];
|
||||
let t = if too_many {
|
||||
Affine::IDENTITY
|
||||
} else {
|
||||
match (name, a.as_slice()) {
|
||||
("matrix", [a0, a1, a2, a3, a4, a5]) => Affine::new([*a0, *a1, *a2, *a3, *a4, *a5]),
|
||||
("translate", [x]) => Affine::translate((*x, 0.0)),
|
||||
("translate", [x, y]) => Affine::translate((*x, *y)),
|
||||
("scale", [scale]) => Affine::scale(*scale),
|
||||
("scale", [x, y]) => Affine::scale_non_uniform(*x, *y),
|
||||
("rotate", [angle]) => Affine::rotate(angle.to_radians()),
|
||||
("rotate", [angle, x, y]) => Affine::translate((*x, *y)) * Affine::rotate(angle.to_radians()) * Affine::translate((-*x, -*y)),
|
||||
("skewX", [angle]) => Affine::new([1.0, 0.0, angle.to_radians().tan(), 1.0, 0.0, 0.0]),
|
||||
("skewY", [angle]) => Affine::new([1.0, angle.to_radians().tan(), 0.0, 1.0, 0.0, 0.0]),
|
||||
_ => Affine::IDENTITY,
|
||||
}
|
||||
};
|
||||
m *= t;
|
||||
}
|
||||
m
|
||||
}
|
||||
Reference in New Issue
Block a user