[librsvg: 29/35] rsvg-convert: add --top and --left options for the image offset within the page




commit 03a388a6d993e6feb8be1c475e7573c383b06085
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Jun 17 12:46:33 2021 -0500

    rsvg-convert: add --top and --left options for the image offset within the page
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/547>

 src/bin/rsvg-convert.rs               |  39 +++++++++++++++++++++++++++++++++-
 src/lib.rs                            |   4 +++-
 tests/fixtures/cmdline/offset-png.png | Bin 0 -> 1426 bytes
 tests/src/cmdline/rsvg_convert.rs     |  14 ++++++++++++
 4 files changed, 55 insertions(+), 2 deletions(-)
---
diff --git a/src/bin/rsvg-convert.rs b/src/bin/rsvg-convert.rs
index 797bbdbb..970452d7 100644
--- a/src/bin/rsvg-convert.rs
+++ b/src/bin/rsvg-convert.rs
@@ -21,7 +21,8 @@ mod windows_imports {
 use self::windows_imports::*;
 
 use librsvg::rsvg_convert_only::{
-    CssLength, Dpi, Horizontal, LegacySize, Normalize, NormalizeParams, PathOrUrl, ULength, Validate, 
Vertical,
+    CssLength, Dpi, Horizontal, LegacySize, Length, Normalize, NormalizeParams, PathOrUrl, ULength,
+    Validate, Vertical,
 };
 use librsvg::{
     AcceptLanguage, CairoRenderer, Color, Language, LengthUnit, Loader, Parse, RenderingError,
@@ -254,6 +255,8 @@ impl Surface {
     pub fn render(
         &self,
         renderer: &CairoRenderer,
+        left: f64,
+        top: f64,
         final_size: Size,
         geometry: cairo::Rectangle,
         background_color: Option<Color>,
@@ -272,6 +275,8 @@ impl Surface {
             cr.paint();
         }
 
+        cr.translate(left, top);
+
         // Note that we don't scale the viewport; we change the cr's transform instead.  This
         // is because SVGs are rendered proportionally to fit within the viewport, regardless
         // of the viewport's proportions.  Rsvg-convert allows non-proportional scaling, so
@@ -443,6 +448,8 @@ struct Converter {
     pub zoom: Scale,
     pub width: Option<ULength<Horizontal>>,
     pub height: Option<ULength<Vertical>>,
+    pub left: Option<Length<Horizontal>>,
+    pub top: Option<Length<Vertical>>,
     pub page_size: Option<(ULength<Horizontal>, ULength<Vertical>)>,
     pub format: Format,
     pub export_id: Option<String>,
@@ -571,8 +578,13 @@ impl Converter {
             let page_size = page_size.unwrap_or(final_size);
             let s = surface.get_or_try_init(|| self.create_surface(page_size))?;
 
+            let left = self.left.map(|l| l.to_user(&params)).unwrap_or(0.0);
+            let top = self.top.map(|l| l.to_user(&params)).unwrap_or(0.0);
+
             s.render(
                 &renderer,
+                left,
+                top,
                 final_size,
                 geometry,
                 self.background_color,
@@ -722,6 +734,20 @@ fn parse_args() -> Result<Converter, Error> {
                 .value_name("length")
                 .help("Height [defaults to the height of the SVG]"),
         )
+        .arg(
+            clap::Arg::with_name("top")
+                .long("top")
+                .takes_value(true)
+                .value_name("length")
+                .help("Distance between top edge of page and the image [defaults to 0]"),
+        )
+        .arg(
+            clap::Arg::with_name("left")
+                .long("left")
+                .takes_value(true)
+                .value_name("length")
+                .help("Distance between left edge of page and the image [defaults to 0]"),
+        )
         .arg(
             clap::Arg::with_name("page_width")
                 .long("page-width")
@@ -856,6 +882,15 @@ fn parse_args() -> Result<Converter, Error> {
         .map(parse_length)
         .transpose()?;
 
+    let left = value_t!(matches, "left", String)
+        .or_none()?
+        .map(parse_length)
+        .transpose()?;
+    let top = value_t!(matches, "top", String)
+        .or_none()?
+        .map(parse_length)
+        .transpose()?;
+
     let page_width = value_t!(matches, "page_width", String)
         .or_none()?
         .map(parse_length)
@@ -905,6 +940,8 @@ fn parse_args() -> Result<Converter, Error> {
         },
         width,
         height,
+        left,
+        top,
         page_size,
         format,
         export_id: value_t!(matches, "export_id", String)
diff --git a/src/lib.rs b/src/lib.rs
index 681d67e6..165e56d0 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -252,5 +252,7 @@ pub mod rsvg_convert_only {
     pub use crate::c_api::sizing::LegacySize;
     pub use crate::dpi::Dpi;
     pub use crate::error::ParseError;
-    pub use crate::length::{CssLength, Horizontal, Length, Normalize, NormalizeParams, ULength, Validate, 
Vertical};
+    pub use crate::length::{
+        CssLength, Horizontal, Length, Normalize, NormalizeParams, ULength, Validate, Vertical,
+    };
 }
diff --git a/tests/fixtures/cmdline/offset-png.png b/tests/fixtures/cmdline/offset-png.png
new file mode 100644
index 00000000..a4bc7e2a
Binary files /dev/null and b/tests/fixtures/cmdline/offset-png.png differ
diff --git a/tests/src/cmdline/rsvg_convert.rs b/tests/src/cmdline/rsvg_convert.rs
index e00d6397..49e72ed6 100644
--- a/tests/src/cmdline/rsvg_convert.rs
+++ b/tests/src/cmdline/rsvg_convert.rs
@@ -541,6 +541,20 @@ fn zero_offset_png() {
         .stdout(file::is_png().with_contents("tests/fixtures/cmdline/zero-offset-png.png"));
 }
 
+#[test]
+fn offset_png() {
+    RsvgConvert::new_with_input("tests/fixtures/cmdline/dimensions-in.svg")
+        .arg("--page-width=640")
+        .arg("--page-height=480")
+        .arg("--width=200")
+        .arg("--height=100")
+        .arg("--left=100")
+        .arg("--top=50")
+        .assert()
+        .success()
+        .stdout(file::is_png().with_contents("tests/fixtures/cmdline/offset-png.png"));
+}
+
 #[cfg(system_deps_have_cairo_pdf)]
 #[test]
 fn unscaled_pdf_size() {


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