[librsvg: 2/5] crate: add CairoRenderer::new



commit dfaa80f3bf909b192c8ff53330e1a72d670b3987
Author: Paolo Borelli <pborelli gnome org>
Date:   Sat Feb 23 21:17:50 2019 +0100

    crate: add CairoRenderer::new
    
    Instead of getting the renderer from the handle, add a constructor
    that takes the handle as a parameter. This is more flexible because
    we can add more constructors and even more renderers without
    affecting the SvgHandle struct.

 librsvg_crate/examples/proportional.rs      |  2 +-
 librsvg_crate/examples/render.rs            |  2 +-
 librsvg_crate/src/lib.rs                    | 39 +++++++++++++----------------
 librsvg_crate/tests/intrinsic-dimensions.rs | 17 +++++++------
 4 files changed, 29 insertions(+), 31 deletions(-)
---
diff --git a/librsvg_crate/examples/proportional.rs b/librsvg_crate/examples/proportional.rs
index ea19cf0f..74387ee8 100644
--- a/librsvg_crate/examples/proportional.rs
+++ b/librsvg_crate/examples/proportional.rs
@@ -29,7 +29,7 @@ fn main() {
     assert!(width > 0 && height > 0);
 
     let handle = librsvg::Loader::new().read_path(input).unwrap();
-    let renderer = handle.get_cairo_renderer();
+    let renderer = librsvg::CairoRenderer::new(&handle);
 
     let surface = cairo::ImageSurface::create(cairo::Format::ARgb32, width, height).unwrap();
     let cr = cairo::Context::new(&surface);
diff --git a/librsvg_crate/examples/render.rs b/librsvg_crate/examples/render.rs
index e0dde8b8..ee88cb0d 100644
--- a/librsvg_crate/examples/render.rs
+++ b/librsvg_crate/examples/render.rs
@@ -19,7 +19,7 @@ fn main() {
     let output = args.next().unwrap();
 
     let handle = librsvg::Loader::new().read_path(input).unwrap();
-    let renderer = handle.get_cairo_renderer();
+    let renderer = librsvg::CairoRenderer::new(&handle);
 
     let (w, h) = renderer.get_dimensions().unwrap();
 
diff --git a/librsvg_crate/src/lib.rs b/librsvg_crate/src/lib.rs
index af7b6c6b..e4f6e605 100644
--- a/librsvg_crate/src/lib.rs
+++ b/librsvg_crate/src/lib.rs
@@ -14,7 +14,7 @@
 //!
 //! * Create a [`Loader`] struct.
 //! * Get an [`SvgHandle`] from the [`Loader`].
-//! * Get a [`CairoRenderer`] from the [`SvgHandle`] and render to a Cairo context.
+//! * Create a [`CairoRenderer`] for the [`SvgHandle`] and render to a Cairo context.
 //!
 //! [`Loader`]: struct.Loader.html
 //! [`SvgHandle`]: struct.SvgHandle.html
@@ -207,7 +207,7 @@ impl Loader {
     /// let surface = cairo::pdf::File::new(..., "hello.pdf");
     /// let cr = cairo::Context::new(&surface);
     ///
-    /// let renderer = svg_handle.get_cairo_renderer();
+    /// let renderer = CairoRenderer::new(&svg_handle);
     /// renderer.render(&cr).unwrap();
     /// ```
     pub fn keep_image_data(mut self, keep: bool) -> Self {
@@ -315,10 +315,6 @@ impl Loader {
 pub struct SvgHandle(Handle);
 
 /// Can render an `SvgHandle` to a Cairo context.
-///
-/// Use the
-/// [`get_cairo_renderer`](struct.SvgHandle.html#method.get_cairo_renderer)
-/// method to create this structure.
 pub struct CairoRenderer<'a> {
     handle: &'a SvgHandle,
     dpi: Dpi,
@@ -328,16 +324,6 @@ pub struct CairoRenderer<'a> {
 const DEFAULT_DPI_X: f64 = 96.0;
 const DEFAULT_DPI_Y: f64 = 96.0;
 
-impl SvgHandle {
-    /// Creates a Cairo rendering context for the SVG handle.
-    pub fn get_cairo_renderer(&self) -> CairoRenderer {
-        CairoRenderer {
-            handle: self,
-            dpi: Dpi::new(DEFAULT_DPI_X, DEFAULT_DPI_Y),
-        }
-    }
-}
-
 #[derive(Debug, Copy, Clone, PartialEq)]
 pub struct IntrinsicDimensions {
     pub width: Option<Length>,
@@ -346,16 +332,27 @@ pub struct IntrinsicDimensions {
 }
 
 impl<'a> CairoRenderer<'a> {
-    /// Configures the dots-per-inch for resolving physical lengths.
+    /// Creates a `CairoRenderer` for the specified `SvgHandle`.
     ///
     /// If an SVG file has physical units like `5cm`, they must be resolved
-    /// to pixel-based values.  Use this function to configure the pixel density
-    /// of your output; the defaults are `96.0` DPI in both dimensions.
-    pub fn set_dpi(&mut self, dpi_x: f64, dpi_y: f64) {
+    /// to pixel-based values.  The default pixel density is `96.0` DPI in
+    /// both dimensions.
+    pub fn new(handle: &'a SvgHandle) -> Self {
+        CairoRenderer {
+            handle,
+            dpi: Dpi::new(DEFAULT_DPI_X, DEFAULT_DPI_Y),
+        }
+    }
+
+    /// Configures the dots-per-inch for resolving physical lengths.
+    pub fn with_dpi(self, dpi_x: f64, dpi_y: f64) -> Self {
         assert!(dpi_x > 0.0);
         assert!(dpi_y > 0.0);
 
-        self.dpi = Dpi::new(dpi_x, dpi_y);
+        CairoRenderer {
+            handle: self.handle,
+            dpi: Dpi::new(dpi_x, dpi_y),
+        }
     }
 
     pub fn get_dimensions(&self) -> Result<(i32, i32), RenderingError> {
diff --git a/librsvg_crate/tests/intrinsic-dimensions.rs b/librsvg_crate/tests/intrinsic-dimensions.rs
index 188f0b40..f112de1f 100644
--- a/librsvg_crate/tests/intrinsic-dimensions.rs
+++ b/librsvg_crate/tests/intrinsic-dimensions.rs
@@ -7,12 +7,13 @@ use gio::MemoryInputStreamExt;
 use glib::Cast;
 
 use librsvg::{
+    CairoRenderer,
     DefsLookupErrorKind,
     HrefError,
     IntrinsicDimensions,
     Length,
     LengthUnit,
-    LoadOptions,
+    Loader,
     RenderingError,
     SvgHandle,
 };
@@ -21,7 +22,7 @@ fn load_svg(input: &'static [u8]) -> SvgHandle {
     let stream = gio::MemoryInputStream::new();
     stream.add_bytes(&glib::Bytes::from_static(input));
 
-    LoadOptions::new()
+    Loader::new()
         .read_stream(&stream.upcast(), None, None)
         .unwrap()
 }
@@ -35,7 +36,7 @@ fn no_intrinsic_dimensions() {
     );
 
     assert_eq!(
-        svg.get_cairo_renderer().get_intrinsic_dimensions(),
+        CairoRenderer::new(&svg).get_intrinsic_dimensions(),
         IntrinsicDimensions {
             width: None,
             height: None,
@@ -53,7 +54,7 @@ fn has_intrinsic_dimensions() {
     );
 
     assert_eq!(
-        svg.get_cairo_renderer().get_intrinsic_dimensions(),
+        CairoRenderer::new(&svg).get_intrinsic_dimensions(),
         IntrinsicDimensions {
             width: Some(Length::new(10.0, LengthUnit::Cm)),
             height: Some(Length::new(20.0, LengthUnit::Px)),
@@ -77,7 +78,7 @@ fn root_geometry_with_percent_viewport() {
 "#,
     );
 
-    let renderer = svg.get_cairo_renderer();
+    let renderer = CairoRenderer::new(&svg);
     let (ink_r, logical_r) = renderer.get_geometry_for_element(None).unwrap();
 
     let rect = cairo::Rectangle {
@@ -100,7 +101,7 @@ fn element_geometry_with_percent_viewport() {
 "#,
     );
 
-    let renderer = svg.get_cairo_renderer();
+    let renderer = CairoRenderer::new(&svg);
     let (ink_r, logical_r) = renderer.get_geometry_for_element(Some("#foo")).unwrap();
 
     let rect = cairo::Rectangle {
@@ -121,7 +122,7 @@ fn element_geometry_for_nonexistent_element() {
 "#,
     );
 
-    let renderer = svg.get_cairo_renderer();
+    let renderer = CairoRenderer::new(&svg);
     match renderer.get_geometry_for_element(Some("#foo")) {
         Err(RenderingError::InvalidId(DefsLookupErrorKind::NotFound)) => (),
         _ => panic!(),
@@ -136,7 +137,7 @@ fn element_geometry_for_invalid_id() {
 "#,
     );
 
-    let renderer = svg.get_cairo_renderer();
+    let renderer = CairoRenderer::new(&svg);
     match renderer.get_geometry_for_element(Some("foo")) {
         Err(RenderingError::InvalidId(DefsLookupErrorKind::CannotLookupExternalReferences)) => (),
         _ => panic!(),


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