[librsvg: 1/6] Move the default DPI machinery to c_api
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 1/6] Move the default DPI machinery to c_api
- Date: Tue, 5 May 2020 20:07:02 +0000 (UTC)
commit e410bef4fdf9bcc246b1dffe2bd69bb4daec7341
Author: Federico Mena Quintero <federico gnome org>
Date: Tue May 5 12:51:59 2020 -0500
Move the default DPI machinery to c_api
This makes rsvg_internals::handle take only well-defined DPI values,
instead of pulling global defaults if needed.
Makefile.am | 1 +
librsvg/c_api.rs | 17 ++++++------
librsvg/dpi.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++++
librsvg/lib.rs | 3 ++
rsvg_internals/src/dpi.rs | 36 ++----------------------
rsvg_internals/src/lib.rs | 2 +-
6 files changed, 88 insertions(+), 42 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 6b5977a6..7a5d43fc 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -123,6 +123,7 @@ LIBRSVG_C_API_SRC = \
librsvg/Cargo.toml \
librsvg/c_api.rs \
librsvg/color_utils.rs \
+ librsvg/dpi.rs \
librsvg/lib.rs \
librsvg/messages.rs \
librsvg/pixbuf_utils.rs \
diff --git a/librsvg/c_api.rs b/librsvg/c_api.rs
index e72cca48..1cd691ac 100644
--- a/librsvg/c_api.rs
+++ b/librsvg/c_api.rs
@@ -33,10 +33,11 @@ use glib::types::instance_of;
use gobject_sys::{GEnumValue, GFlagsValue};
use rsvg_internals::{
- rsvg_log, DefsLookupErrorKind, Dpi, Handle, IntrinsicDimensions, LoadOptions, LoadingError,
+ rsvg_log, DefsLookupErrorKind, Handle, IntrinsicDimensions, LoadOptions, LoadingError,
RenderingError, RsvgLength, SharedImageSurface, SurfaceType, ViewBox,
};
+use crate::dpi::Dpi;
use crate::messages::{rsvg_g_critical, rsvg_g_warning};
use crate::pixbuf_utils::{empty_pixbuf, pixbuf_from_surface};
@@ -811,7 +812,7 @@ impl CHandle {
inner.size_callback.start_loop();
let res = handle
- .get_geometry_sub(id, inner.dpi, inner.is_testing)
+ .get_geometry_sub(id, inner.dpi.into(), inner.is_testing)
.and_then(|(ink_r, _)| {
let width = checked_i32(ink_r.width().round())?;
let height = checked_i32(ink_r.height().round())?;
@@ -843,7 +844,7 @@ impl CHandle {
}
handle
- .get_geometry_sub(id, inner.dpi, inner.is_testing)
+ .get_geometry_sub(id, inner.dpi.into(), inner.is_testing)
.and_then(|(ink_r, _)| {
let width = checked_i32(ink_r.width().round())?;
let height = checked_i32(ink_r.height().round())?;
@@ -930,7 +931,7 @@ impl CHandle {
let handle = self.get_handle_ref()?;
let inner = self.inner.borrow();
- handle.render_document(cr, viewport, inner.dpi, inner.is_testing)
+ handle.render_document(cr, viewport, inner.dpi.into(), inner.is_testing)
}
fn get_geometry_for_layer(
@@ -941,7 +942,7 @@ impl CHandle {
let handle = self.get_handle_ref()?;
let inner = self.inner.borrow();
handle
- .get_geometry_for_layer(id, viewport, inner.dpi, inner.is_testing)
+ .get_geometry_for_layer(id, viewport, inner.dpi.into(), inner.is_testing)
.map(|(i, l)| (RsvgRectangle::from(i), RsvgRectangle::from(l)))
.map_err(warn_on_invalid_id)
}
@@ -957,7 +958,7 @@ impl CHandle {
let handle = self.get_handle_ref()?;
let inner = self.inner.borrow();
handle
- .render_layer(cr, id, viewport, inner.dpi, inner.is_testing)
+ .render_layer(cr, id, viewport, inner.dpi.into(), inner.is_testing)
.map_err(warn_on_invalid_id)
}
@@ -968,7 +969,7 @@ impl CHandle {
let handle = self.get_handle_ref()?;
let inner = self.inner.borrow();
handle
- .get_geometry_for_element(id, inner.dpi, inner.is_testing)
+ .get_geometry_for_element(id, inner.dpi.into(), inner.is_testing)
.map(|(i, l)| (RsvgRectangle::from(i), RsvgRectangle::from(l)))
.map_err(warn_on_invalid_id)
}
@@ -984,7 +985,7 @@ impl CHandle {
let handle = self.get_handle_ref()?;
let inner = self.inner.borrow();
handle
- .render_element(cr, id, element_viewport, inner.dpi, inner.is_testing)
+ .render_element(cr, id, element_viewport, inner.dpi.into(), inner.is_testing)
.map_err(warn_on_invalid_id)
}
diff --git a/librsvg/dpi.rs b/librsvg/dpi.rs
new file mode 100644
index 00000000..a50961b1
--- /dev/null
+++ b/librsvg/dpi.rs
@@ -0,0 +1,71 @@
+//! Legacy C API for setting a default DPI (dots per inch = DPI).
+//!
+//! There are two deprecated functions, `rsvg_set_default_dpi` and
+//! `rsvg_set_default_dpi_x_y`, which set global values for the default DPI to be used
+//! with `RsvgHandle`. In turn, `RsvgHandle` assumes that when its own DPI value is set
+//! to `0.0` (which is in fact its default), it will fall back to the global DPI.
+//!
+//! This is clearly not thread-safe, but it is the legacy behavior.
+//!
+//! This module encapsulates that behavior so that the `rsvg_internals` crate
+//! can always have immutable DPI values as intended.
+
+
+use rsvg_internals;
+
+// This is configurable at runtime
+const DEFAULT_DPI_X: f64 = 90.0;
+const DEFAULT_DPI_Y: f64 = 90.0;
+
+static mut DPI_X: f64 = DEFAULT_DPI_X;
+static mut DPI_Y: f64 = DEFAULT_DPI_Y;
+
+#[derive(Debug, Copy, Clone, Default)]
+pub struct Dpi {
+ x: f64,
+ y: f64,
+}
+
+impl Dpi {
+ pub fn new(x: f64, y: f64) -> Dpi {
+ Dpi { x, y }
+ }
+
+ pub fn x(&self) -> f64 {
+ if self.x <= 0.0 {
+ unsafe { DPI_X }
+ } else {
+ self.x
+ }
+ }
+
+ pub fn y(&self) -> f64 {
+ if self.y <= 0.0 {
+ unsafe { DPI_Y }
+ } else {
+ self.y
+ }
+ }
+}
+
+impl From<Dpi> for rsvg_internals::Dpi {
+ fn from(dpi: Dpi) -> rsvg_internals::Dpi {
+ rsvg_internals::Dpi::new(dpi.x(), dpi.y())
+ }
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn rsvg_rust_set_default_dpi_x_y(dpi_x: f64, dpi_y: f64) {
+ if dpi_x <= 0.0 {
+ DPI_X = DEFAULT_DPI_X;
+ } else {
+ DPI_X = dpi_x;
+ }
+
+ if dpi_y <= 0.0 {
+ DPI_Y = DEFAULT_DPI_Y;
+ } else {
+ DPI_Y = dpi_y;
+ }
+}
+
diff --git a/librsvg/lib.rs b/librsvg/lib.rs
index bec12ede..a8029640 100644
--- a/librsvg/lib.rs
+++ b/librsvg/lib.rs
@@ -37,6 +37,8 @@ pub use crate::c_api::{
pub use crate::color_utils::rsvg_css_parse_color;
+pub use crate::dpi::rsvg_rust_set_default_dpi_x_y;
+
#[rustfmt::skip]
pub use crate::pixbuf_utils::{
rsvg_rust_pixbuf_from_file_at_max_size,
@@ -50,4 +52,5 @@ mod messages;
mod c_api;
mod color_utils;
+mod dpi;
mod pixbuf_utils;
diff --git a/rsvg_internals/src/dpi.rs b/rsvg_internals/src/dpi.rs
index 903da987..cf2efae8 100644
--- a/rsvg_internals/src/dpi.rs
+++ b/rsvg_internals/src/dpi.rs
@@ -1,13 +1,6 @@
//! Resolution for rendering (dots per inch = DPI).
-// This is configurable at runtime
-const DEFAULT_DPI_X: f64 = 90.0;
-const DEFAULT_DPI_Y: f64 = 90.0;
-
-static mut DPI_X: f64 = DEFAULT_DPI_X;
-static mut DPI_Y: f64 = DEFAULT_DPI_Y;
-
-#[derive(Debug, Copy, Clone, Default)]
+#[derive(Debug, Copy, Clone)]
pub struct Dpi {
x: f64,
y: f64,
@@ -19,33 +12,10 @@ impl Dpi {
}
pub fn x(&self) -> f64 {
- if self.x <= 0.0 {
- unsafe { DPI_X }
- } else {
- self.x
- }
+ self.x
}
pub fn y(&self) -> f64 {
- if self.y <= 0.0 {
- unsafe { DPI_Y }
- } else {
- self.y
- }
- }
-}
-
-#[no_mangle]
-pub unsafe extern "C" fn rsvg_rust_set_default_dpi_x_y(dpi_x: f64, dpi_y: f64) {
- if dpi_x <= 0.0 {
- DPI_X = DEFAULT_DPI_X;
- } else {
- DPI_X = dpi_x;
- }
-
- if dpi_y <= 0.0 {
- DPI_Y = DEFAULT_DPI_Y;
- } else {
- DPI_Y = dpi_y;
+ self.y
}
}
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index 0b005705..fe86e648 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -49,7 +49,7 @@
pub use crate::color::Color;
-pub use crate::dpi::{rsvg_rust_set_default_dpi_x_y, Dpi};
+pub use crate::dpi::Dpi;
pub use crate::error::{DefsLookupErrorKind, HrefError, LoadingError, RenderingError};
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]