[librsvg: 19/21] Support only translate/translateX/translateY in pixel units for now




commit 1770bd2b9f9c722b810183b53c74ee65336eb6c4
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Aug 31 13:05:57 2021 -0500

    Support only translate/translateX/translateY in pixel units for now
    
    We don't yet have machinery to convert transforms to user space
    coordinates.  So, for now librsvg will only support translation
    transform functions given in pixels.
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/576>

 src/transform.rs | 37 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)
---
diff --git a/src/transform.rs b/src/transform.rs
index fdfd212a..37c62051 100644
--- a/src/transform.rs
+++ b/src/transform.rs
@@ -169,10 +169,22 @@ fn parse_prop_matrix_args<'i>(
     })
 }
 
+fn length_is_in_pixels<N: Normalize>(l: &Length<N>) -> bool {
+    l.unit == LengthUnit::Px
+}
+
+fn only_pixels_error<'i>(loc: cssparser::SourceLocation) -> ParseError<'i> {
+    loc.new_custom_error(ValueErrorKind::parse_error(
+        "only translations in pixels are supported for now",
+    ))
+}
+
 fn parse_prop_translate_args<'i>(
     parser: &mut Parser<'i, '_>,
 ) -> Result<TransformFunction, ParseError<'i>> {
     parser.parse_nested_block(|p| {
+        let loc = p.current_source_location();
+
         let tx: Length<Horizontal> = Length::parse(p)?;
 
         let ty: Length<Vertical> = if p.try_parse(|p| p.expect_comma()).is_ok() {
@@ -181,6 +193,10 @@ fn parse_prop_translate_args<'i>(
             Length::new(0.0, LengthUnit::Px)
         };
 
+        if !(length_is_in_pixels(&tx) && length_is_in_pixels(&ty)) {
+            return Err(only_pixels_error(loc));
+        }
+
         Ok(TransformFunction::Translate(tx, ty))
     })
 }
@@ -189,8 +205,14 @@ fn parse_prop_translate_x_args<'i>(
     parser: &mut Parser<'i, '_>,
 ) -> Result<TransformFunction, ParseError<'i>> {
     parser.parse_nested_block(|p| {
+        let loc = p.current_source_location();
+
         let tx: Length<Horizontal> = Length::parse(p)?;
 
+        if !length_is_in_pixels(&tx) {
+            return Err(only_pixels_error(loc));
+        }
+
         Ok(TransformFunction::TranslateX(tx))
     })
 }
@@ -199,8 +221,14 @@ fn parse_prop_translate_y_args<'i>(
     parser: &mut Parser<'i, '_>,
 ) -> Result<TransformFunction, ParseError<'i>> {
     parser.parse_nested_block(|p| {
+        let loc = p.current_source_location();
+
         let ty: Length<Vertical> = Length::parse(p)?;
 
+        if !length_is_in_pixels(&ty) {
+            return Err(only_pixels_error(loc));
+        }
+
         Ok(TransformFunction::TranslateY(ty))
     })
 }
@@ -906,7 +934,14 @@ mod tests {
         assert!(parse_transform_prop("translateY(1)").is_ok());
         assert!(parse_transform_prop("translateY(100 100)").is_err());
         assert!(parse_transform_prop("translatey(1px)").is_err());
-        assert!(parse_transform_prop("translateY(1%)").is_ok());
+    }
+
+    #[test]
+    fn test_translate_only_supports_pixel_units() {
+        assert!(parse_transform_prop("translate(1in, 2)").is_err());
+        assert!(parse_transform_prop("translate(1, 2in)").is_err());
+        assert!(parse_transform_prop("translateX(1cm)").is_err());
+        assert!(parse_transform_prop("translateY(1cm)").is_err());
     }
 
     #[test]


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