[librsvg: 5/19] Compose multiple transforms into a transform list



commit 3da7e5fcc4ba1b5db8a87274d1775afa2e61a545
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Nov 22 11:04:36 2017 -0600

    Compose multiple transforms into a transform list

 rust/src/transform.rs | 36 ++++++++++++++++++++++++++++++------
 1 file changed, 30 insertions(+), 6 deletions(-)
---
diff --git a/rust/src/transform.rs b/rust/src/transform.rs
index 92184ca..da31814 100644
--- a/rust/src/transform.rs
+++ b/rust/src/transform.rs
@@ -22,19 +22,43 @@ impl Parse for cairo::Matrix {
 }
 
 pub fn parse_transform(s: &str) -> Result<cairo::Matrix, AttributeError> {
+    let matrix = parse_transform_list(s)?;
+
+    matrix.try_invert ().map (|_| matrix)
+        .map_err (|_| AttributeError::Value ("invalid transformation matrix".to_string ()))
+}
+
+fn parse_transform_list(s: &str) -> Result<cairo::Matrix, AttributeError> {
     let mut input = ParserInput::new(s);
     let mut parser = Parser::new(&mut input);
 
+    let mut matrix = cairo::Matrix::identity();
+
+    loop {
+        let m = parse_transform_command(&mut parser)?;
+        matrix = cairo::Matrix::multiply(&m, &matrix);
+
+        if parser.is_exhausted() {
+            break;
+        }
+
+        optional_comma(&mut parser);
+    }
+
+    Ok(matrix)
+}
+
+fn parse_transform_command(parser: &mut Parser) -> Result<cairo::Matrix, AttributeError> {
     let xform = parser.expect_ident_cloned()?;
     let _ = parser.expect_parenthesis_block()?;
 
     match xform.as_ref() {
-        "matrix"    => parse_matrix_args(&mut parser),
-        "translate" => parse_translate_args(&mut parser),
-        "scale"     => parse_scale_args(&mut parser),
-        "rotate"    => parse_rotate_args(&mut parser),
-        "skewX"     => parse_skewX_args(&mut parser),
-        "skewY"     => parse_skewY_args(&mut parser),
+        "matrix"    => parse_matrix_args(parser),
+        "translate" => parse_translate_args(parser),
+        "scale"     => parse_scale_args(parser),
+        "rotate"    => parse_rotate_args(parser),
+        "skewX"     => parse_skewX_args(parser),
+        "skewY"     => parse_skewY_args(parser),
         _           => Err(AttributeError::from(ParseError::new("expected 
matrix|translate|scale|rotate|skewX|skewY"))),
     }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]