[librsvg: 10/15] Export intrinsic_size_in_pixels from the Rust API
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 10/15] Export intrinsic_size_in_pixels from the Rust API
- Date: Tue, 27 Oct 2020 23:36:50 +0000 (UTC)
commit 6e9f846e5491fa63474ed7c46763158a125926fd
Author: Federico Mena Quintero <federico gnome org>
Date: Tue Oct 27 14:16:42 2020 -0600
Export intrinsic_size_in_pixels from the Rust API
librsvg_crate/src/lib.rs | 18 ++++++++
.../tests/standalone/intrinsic_dimensions.rs | 51 ++++++++++++++++++++++
2 files changed, 69 insertions(+)
---
diff --git a/librsvg_crate/src/lib.rs b/librsvg_crate/src/lib.rs
index b9294e65..e140a594 100644
--- a/librsvg_crate/src/lib.rs
+++ b/librsvg_crate/src/lib.rs
@@ -459,7 +459,11 @@ impl<'a> CairoRenderer<'a> {
/// consider simply using [`render_document`] instead; it will do the scaling
/// computations automatically.
///
+ /// See also [`intrinsic_size_in_pixels`], which does the conversion to pixels if
+ /// possible.
+ ///
/// [`render_document`]: #method.render_document
+ /// [`intrinsic_size_in_pixels`]: #method.intrinsic_size_in_pixels
pub fn intrinsic_dimensions(&self) -> IntrinsicDimensions {
let d = self.handle.0.get_intrinsic_dimensions();
@@ -470,6 +474,20 @@ impl<'a> CairoRenderer<'a> {
}
}
+ /// Converts the SVG document's intrinsic dimensions to pixels, if possible.
+ ///
+ /// Returns `Some(width, height)` in pixel units if the SVG document has `width` and
+ /// `height` attributes with physical dimensions (CSS pixels, cm, in, etc.) or
+ /// font-based dimensions (em, ex).
+ ///
+ /// If the SVG document has percentage-based `width` and `height` attributes, or if
+ /// either of those attributes are not present, returns `None`. Dimensions of that
+ /// kind require more information to be resolved to pixels; for example, the calling
+ /// application can use a viewport size to scale percentage-based dimensions.
+ pub fn intrinsic_size_in_pixels(&self) -> Option<(f64, f64)> {
+ self.handle.0.get_intrinsic_size_in_pixels(self.dpi)
+ }
+
/// Renders the whole SVG document fitted to a viewport
///
/// The `viewport` gives the position and size at which the whole SVG
diff --git a/librsvg_crate/tests/standalone/intrinsic_dimensions.rs
b/librsvg_crate/tests/standalone/intrinsic_dimensions.rs
index aa8a50b2..9851302e 100644
--- a/librsvg_crate/tests/standalone/intrinsic_dimensions.rs
+++ b/librsvg_crate/tests/standalone/intrinsic_dimensions.rs
@@ -53,6 +53,57 @@ fn has_intrinsic_dimensions() {
);
}
+#[test]
+fn intrinsic_size_in_pixels() {
+ let svg = load_svg(
+ br#"<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg" width="10" height="20" viewBox="0 0 100 200"/>
+"#,
+ )
+ .unwrap();
+
+ assert_eq!(
+ CairoRenderer::new(&svg).intrinsic_size_in_pixels(),
+ Some((10.0, 20.0)),
+ );
+}
+
+#[test]
+fn no_intrinsic_size_in_pixels_with_percent_dimensions() {
+ let svg = load_svg(
+ br#"<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 100 200"/>
+"#,
+ )
+ .unwrap();
+
+ assert_eq!(CairoRenderer::new(&svg).intrinsic_size_in_pixels(), None);
+}
+
+#[test]
+fn no_intrinsic_size_in_pixels_with_no_dimensions() {
+ let svg = load_svg(
+ br#"<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 200"/>
+"#,
+ )
+ .unwrap();
+
+ assert_eq!(CairoRenderer::new(&svg).intrinsic_size_in_pixels(), None);
+}
+
+#[test]
+fn no_intrinsic_size_in_pixels_with_one_missing_dimension() {
+ let svg = load_svg(
+ br#"<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg" width="100" viewBox="0 0 100 200"/>
+"#,
+ )
+ .unwrap();
+
+ assert_eq!(CairoRenderer::new(&svg).intrinsic_size_in_pixels(), None);
+}
+
#[test]
fn root_geometry_with_percent_viewport() {
let svg = load_svg(
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]