[librsvg: 18/35] Convert sizes to the units required by the target format




commit c4769b6b09be01b3082b1dc0ba70e4a6e10b466a
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri Jun 11 20:14:26 2021 -0500

    Convert sizes to the units required by the target format
    
    This adds a test for a PDF's resulting size from an SVG in inches, and
    removes the old test for the broken behavior.
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/547>

 src/bin/rsvg-convert.rs                  | 14 ++++++++++++--
 tests/fixtures/cmdline/dimensions-in.svg |  4 ++++
 tests/src/cmdline/rsvg_convert.rs        |  8 +++-----
 tests/src/predicates/pdf.rs              | 10 +++++++---
 4 files changed, 26 insertions(+), 10 deletions(-)
---
diff --git a/src/bin/rsvg-convert.rs b/src/bin/rsvg-convert.rs
index 0a8e3463..61fe8be9 100644
--- a/src/bin/rsvg-convert.rs
+++ b/src/bin/rsvg-convert.rs
@@ -495,14 +495,18 @@ impl Converter {
                 .with_language(&self.language);
 
             let geometry = natural_geometry(&renderer, input, self.export_id.as_deref())?;
+
+            // natural_size is in pixels
             let natural_size = Size::new(geometry.width, geometry.height);
 
             let params = NormalizeParams::from_dpi(Dpi::new(self.dpi.0, self.dpi.1));
 
-            let (requested_width, requested_height) = match self.format {
+            // Convert natural size and requested size to pixels or points, depending on the target format,
+            let (natural_size, requested_width, requested_height) = match self.format {
                 Format::Png => {
                     // PNG surface requires units in pixels
                     (
+                        natural_size,
                         self.width.map(|l| l.to_user(&params)),
                         self.height.map(|l| l.to_user(&params)),
                     )
@@ -510,8 +514,13 @@ impl Converter {
 
                 Format::Pdf | Format::Ps | Format::Eps => {
                     // These surfaces require units in points
-
                     (
+                        Size {
+                            w: ULength::<Horizontal>::new(natural_size.w, LengthUnit::Px)
+                                .to_points(&params),
+                            h: ULength::<Vertical>::new(natural_size.h, LengthUnit::Px)
+                                .to_points(&params),
+                        },
                         self.width.map(|l| l.to_points(&params)),
                         self.height.map(|l| l.to_points(&params)),
                     )
@@ -520,6 +529,7 @@ impl Converter {
                 Format::Svg => {
                     // TODO: SVG surface can be created with any unit type; let's use pixels for now
                     (
+                        natural_size,
                         self.width.map(|l| l.to_user(&params)),
                         self.height.map(|l| l.to_user(&params)),
                     )
diff --git a/tests/fixtures/cmdline/dimensions-in.svg b/tests/fixtures/cmdline/dimensions-in.svg
new file mode 100644
index 00000000..aa4f3219
--- /dev/null
+++ b/tests/fixtures/cmdline/dimensions-in.svg
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg"; width="1in" height="1in" viewBox="0 0 1 1">
+  <rect x="0" y="0" width="1" height="1" fill="blue"/>
+</svg>
diff --git a/tests/src/cmdline/rsvg_convert.rs b/tests/src/cmdline/rsvg_convert.rs
index 04f46265..b70af582 100644
--- a/tests/src/cmdline/rsvg_convert.rs
+++ b/tests/src/cmdline/rsvg_convert.rs
@@ -531,14 +531,12 @@ fn negative_resolution_is_invalid() {
 
 #[cfg(system_deps_have_cairo_pdf)]
 #[test]
-fn pdf_page_size() {
-    RsvgConvert::new_with_input("tests/fixtures/dimensions/521-with-viewbox.svg")
+fn unscaled_pdf_size() {
+    RsvgConvert::new_with_input("tests/fixtures/cmdline/dimensions-in.svg")
         .arg("--format=pdf")
         .assert()
         .success()
-        // TODO: the PDF size and resolution is actually a bug in rsvg-convert,
-        // see https://gitlab.gnome.org/GNOME/librsvg/issues/514
-        .stdout(file::is_pdf().with_page_size(200.0, 100.0));
+        .stdout(file::is_pdf().with_page_size(72.0, 72.0));
 }
 
 #[test]
diff --git a/tests/src/predicates/pdf.rs b/tests/src/predicates/pdf.rs
index 449992fb..d6fa86b1 100644
--- a/tests/src/predicates/pdf.rs
+++ b/tests/src/predicates/pdf.rs
@@ -19,12 +19,16 @@ impl PdfPredicate {
         }
     }
 
-    pub fn with_page_size(self: Self, width: f64, height: f64) -> DetailPredicate<Self> {
+    pub fn with_page_size(
+        self: Self,
+        width_in_points: f64,
+        height_in_points: f64,
+    ) -> DetailPredicate<Self> {
         DetailPredicate::<Self> {
             p: self,
             d: Detail::PageSize(Dimensions {
-                w: width,
-                h: height,
+                w: width_in_points,
+                h: height_in_points,
                 unit: 1.0,
             }),
         }


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