[librsvg: 1/10] Strategy to select user specified units




commit aee0b414006660e4e732d4da55bdedab1a0cbb3a
Author: Daniel Petri Rocha <daniel petri tum de>
Date:   Mon Nov 1 22:01:46 2021 +0100

    Strategy to select user specified units

 src/bin/rsvg-convert.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 47 insertions(+), 2 deletions(-)
---
diff --git a/src/bin/rsvg-convert.rs b/src/bin/rsvg-convert.rs
index d3667b9f..0e67e7b3 100644
--- a/src/bin/rsvg-convert.rs
+++ b/src/bin/rsvg-convert.rs
@@ -482,11 +482,9 @@ impl Converter {
 
             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));
-
             // Convert natural size and requested size to pixels or points, depending on the target format,
             let (natural_size, requested_width, requested_height, page_size) = match self.format {
                 Format::Png => {
@@ -522,6 +520,39 @@ impl Converter {
 
                 Format::Svg => {
                     // TODO: SVG surface can be created with any unit type; let's use pixels for now
+
+                    // Determine original unit type
+                    let w_unit = self.width.map(|l| l.unit);
+                    let h_unit = self.height.map(|l| l.unit);
+                    let page_size_w_unit = self.page_size.map(|(w, _)| w.unit);
+                    let page_size_h_unit = self.page_size.map(|(_, h)| h.unit);
+
+                    println!("Width unit is {:?} ({:?})", w_unit, print_type_of(&w_unit));
+                    println!("Height unit is {:?}", h_unit);
+                    println!("Page width unit is {:?}", page_size_w_unit);
+                    println!("Page height unit is {:?}", page_size_h_unit);
+                    
+                    let mut specified_units = vec![w_unit, h_unit, page_size_w_unit, page_size_h_unit];
+                    
+                    println!("Specified units before {:?}", specified_units);
+                    specified_units.retain(|u| u.is_some());
+                    println!("Specified units after {:?}", specified_units);
+                    println!("Length of specified units {:?}", specified_units.len());
+
+                    println!("Units are {:?}", &specified_units);
+                    println!("All equal: {:?}", all_equal_units(&specified_units));
+                    
+                    let svg_unit = if !specified_units.is_empty() && all_equal_units(&specified_units) {
+                        match specified_units.pop().unwrap() {
+                            Some(u) => u,
+                            _ => LengthUnit::Px,
+                        }
+                        
+                    } else {
+                            LengthUnit::Px
+                    };
+
+                    println!("SVG unit is {:?}", svg_unit);
                     (
                         natural_size,
                         self.width.map(|l| l.to_user(&params)),
@@ -635,6 +666,20 @@ impl Converter {
     }
 }
 
+fn print_type_of<T>(_: &T) {
+    println!("{}", std::any::type_name::<T>())
+}
+
+// https://weirder.earth/@Eden/102226720432099086
+fn all_equal_units(vec: &[Option<LengthUnit>]) -> bool {
+    match vec {
+        [] => true,
+        [_] => true,
+        [x, y, ..] if x != y => false,
+        [_, zs @ ..] => all_equal_units(zs),
+    }
+}
+
 fn natural_geometry(
     renderer: &CairoRenderer,
     input: &Input,


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