[librsvg: 26/26] CairoRenderer: implement get_intrinsic_dimensions()
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 26/26] CairoRenderer: implement get_intrinsic_dimensions()
- Date: Tue, 19 Feb 2019 02:50:13 +0000 (UTC)
commit c5579012b84a73fb02455bd51f3d79e9be438b04
Author: Federico Mena Quintero <federico gnome org>
Date: Mon Feb 18 20:44:24 2019 -0600
CairoRenderer: implement get_intrinsic_dimensions()
This requires making Length public. It has a bunch of methods that
the public crate shouldn't have; we'll see about that later.
Also, this has the first integration tests in Rust!!!
librsvg_crate/src/lib.rs | 26 ++++++++++++-
librsvg_crate/tests/intrinsic-dimensions.rs | 59 +++++++++++++++++++++++++++++
rsvg_internals/src/handle.rs | 2 +-
rsvg_internals/src/length.rs | 2 +-
rsvg_internals/src/lib.rs | 2 +
5 files changed, 88 insertions(+), 3 deletions(-)
---
diff --git a/librsvg_crate/src/lib.rs b/librsvg_crate/src/lib.rs
index 7d462729..5f2b6b59 100644
--- a/librsvg_crate/src/lib.rs
+++ b/librsvg_crate/src/lib.rs
@@ -102,7 +102,7 @@ use glib::object::Cast;
use rsvg_internals::{Dpi, Handle, LoadFlags};
use url::Url;
-pub use rsvg_internals::{LoadingError, RenderingError};
+pub use rsvg_internals::{Length, LengthUnit, LoadingError, RenderingError};
/// Full configuration for loading an [`SvgHandle`][SvgHandle].
///
@@ -327,6 +327,13 @@ impl SvgHandle {
}
}
+#[derive(Debug, Copy, Clone, PartialEq)]
+pub struct IntrinsicDimensions {
+ pub width: Option<Length>,
+ pub height: Option<Length>,
+ pub vbox: Option<cairo::Rectangle>,
+}
+
impl<'a> CairoRenderer<'a> {
/// Configures the dots-per-inch for resolving physical lengths.
///
@@ -347,6 +354,23 @@ impl<'a> CairoRenderer<'a> {
.map(|dimensions| (dimensions.width, dimensions.height))
}
+ pub fn get_intrinsic_dimensions(&self) -> IntrinsicDimensions {
+ let d = self.handle
+ .0
+ .get_intrinsic_dimensions();
+
+ IntrinsicDimensions {
+ width: d.width.map(|l| l.to_length()),
+ height: d.height.map(|l| l.to_length()),
+ vbox: d.vbox.map(|v| cairo::Rectangle {
+ x: v.x,
+ y: v.y,
+ width: v.width,
+ height: v.height,
+ }),
+ }
+ }
+
/// Returns (ink_rect, logical_rect) of an SVG element.
///
/// Element IDs should look like an URL fragment identifier; for
diff --git a/librsvg_crate/tests/intrinsic-dimensions.rs b/librsvg_crate/tests/intrinsic-dimensions.rs
new file mode 100644
index 00000000..e109db4f
--- /dev/null
+++ b/librsvg_crate/tests/intrinsic-dimensions.rs
@@ -0,0 +1,59 @@
+extern crate cairo;
+extern crate gio;
+extern crate glib;
+extern crate librsvg;
+
+use gio::MemoryInputStreamExt;
+use glib::Cast;
+
+use librsvg::{IntrinsicDimensions, Length, LengthUnit, LoadOptions, SvgHandle};
+
+fn load_svg(input: &'static [u8]) -> SvgHandle {
+ let stream = gio::MemoryInputStream::new();
+ stream.add_bytes(&glib::Bytes::from_static(input));
+
+ LoadOptions::new()
+ .read_stream(&stream.upcast(), None, None)
+ .unwrap()
+}
+
+#[test]
+fn no_intrinsic_dimensions() {
+ let svg = load_svg(
+ br#"<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg"/>
+"#,
+ );
+
+ assert_eq!(
+ svg.get_cairo_renderer().get_intrinsic_dimensions(),
+ IntrinsicDimensions {
+ width: None,
+ height: None,
+ vbox: None,
+ }
+ );
+}
+
+#[test]
+fn has_intrinsic_dimensions() {
+ let svg = load_svg(
+ br#"<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg" width="10cm" height="20" viewBox="0 0 100 200"/>
+"#,
+ );
+
+ assert_eq!(
+ svg.get_cairo_renderer().get_intrinsic_dimensions(),
+ IntrinsicDimensions {
+ width: Some(Length::new(10.0, LengthUnit::Cm)),
+ height: Some(Length::new(20.0, LengthUnit::Px)),
+ vbox: Some(cairo::Rectangle {
+ x: 0.0,
+ y: 0.0,
+ width: 100.0,
+ height: 200.0,
+ }),
+ }
+ );
+}
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index cf2ebea1..61ea7b29 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -537,7 +537,7 @@ impl Handle {
self.read_stream_sync(stream, cancellable)
}
- fn get_intrinsic_dimensions(&self) -> IntrinsicDimensions {
+ pub fn get_intrinsic_dimensions(&self) -> IntrinsicDimensions {
let svg_ref = self.svg.borrow();
let svg = svg_ref.as_ref().unwrap();
diff --git a/rsvg_internals/src/length.rs b/rsvg_internals/src/length.rs
index 0bf128ad..496ef9ef 100644
--- a/rsvg_internals/src/length.rs
+++ b/rsvg_internals/src/length.rs
@@ -227,7 +227,7 @@ impl Parse for Length {
}
impl Length {
- fn new(l: f64, unit: LengthUnit) -> Length {
+ pub fn new(l: f64, unit: LengthUnit) -> Length {
Length { length: l, unit }
}
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index 2516c6f3..8421a3d7 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -81,6 +81,8 @@ pub use handle::{
LoadFlags,
};
+pub use length::{Length, LengthUnit};
+
pub use pixbuf_utils::{
rsvg_rust_pixbuf_from_file_at_max_size,
rsvg_rust_pixbuf_from_file_at_size,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]