Commit 8f2af75c authored by Paolo Borelli's avatar Paolo Borelli

dpi: remove interior mutability

Make Dpi a copy type and keep interior mutability in the Handle
parent 5f0ba352
Pipeline #50750 failed with stages
in 18 minutes and 48 seconds
use std::cell::Cell;
// This is configurable at runtime
const DEFAULT_DPI_X: f64 = 90.0;
const DEFAULT_DPI_Y: f64 = 90.0;
......@@ -7,36 +5,35 @@ 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, Clone, Default)]
#[derive(Debug, Copy, Clone, Default)]
pub struct Dpi {
x: Cell<f64>,
y: Cell<f64>,
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.get() <= 0.0 {
if self.x <= 0.0 {
unsafe { DPI_X }
} else {
self.x.get()
self.x
}
}
pub fn set_x(&self, dpi_x: f64) {
self.x.set(dpi_x)
}
pub fn y(&self) -> f64 {
if self.y.get() <= 0.0 {
if self.y <= 0.0 {
unsafe { DPI_Y }
} else {
self.y.get()
self.y
}
}
pub fn set_y(&self, dpi_y: f64) {
self.y.set(dpi_y)
}
}
#[no_mangle]
......
......@@ -140,7 +140,7 @@ impl Drop for SizeCallback {
}
pub struct Handle {
dpi: Dpi,
dpi: Cell<Dpi>,
base_url: RefCell<Option<Url>>,
base_url_cstring: RefCell<Option<CString>>, // needed because the C api returns *const char
svg: RefCell<Option<Rc<Svg>>>,
......@@ -155,7 +155,7 @@ pub struct Handle {
impl Handle {
fn new() -> Handle {
Handle {
dpi: Dpi::default(),
dpi: Cell::new(Dpi::default()),
base_url: RefCell::new(None),
base_url_cstring: RefCell::new(None),
svg: RefCell::new(None),
......@@ -309,7 +309,7 @@ impl Handle {
f64::from(dimensions.height),
dimensions.em,
dimensions.ex,
self.dpi.clone(),
self.dpi.get(),
self.is_testing.get(),
);
......@@ -439,7 +439,7 @@ impl Handle {
if is_root {
if let Some((root_width, root_height)) =
node.with_impl(|svg: &NodeSvg| svg.get_size(&self.dpi))
node.with_impl(|svg: &NodeSvg| svg.get_size(self.dpi.get()))
{
let ink_r = RsvgRectangle {
x: 0.0,
......@@ -821,28 +821,28 @@ pub unsafe extern "C" fn rsvg_handle_rust_get_base_url(
pub unsafe extern "C" fn rsvg_handle_rust_set_dpi_x(raw_handle: *const Handle, dpi_x: f64) {
let handle = &*(raw_handle as *const Handle);
handle.dpi.set_x(dpi_x);
handle.dpi.set(Dpi::new(dpi_x, handle.dpi.get().y()));
}
#[no_mangle]
pub unsafe extern "C" fn rsvg_handle_rust_get_dpi_x(raw_handle: *const Handle) -> f64 {
let handle = &*(raw_handle as *const Handle);
handle.dpi.x()
handle.dpi.get().x()
}
#[no_mangle]
pub unsafe extern "C" fn rsvg_handle_rust_set_dpi_y(raw_handle: *const Handle, dpi_y: f64) {
let handle = &*(raw_handle as *const Handle);
handle.dpi.set_y(dpi_y);
handle.dpi.set(Dpi::new(handle.dpi.get().x(), dpi_y));
}
#[no_mangle]
pub unsafe extern "C" fn rsvg_handle_rust_get_dpi_y(raw_handle: *const Handle) -> f64 {
let handle = &*(raw_handle as *const Handle);
handle.dpi.y()
handle.dpi.get().y()
}
#[no_mangle]
......
......@@ -125,19 +125,16 @@ impl NodeSvg {
}
}
pub fn get_size(&self, dpi: &Dpi) -> Option<(i32, i32)> {
let dpi_x = dpi.x();
let dpi_y = dpi.y();
pub fn get_size(&self, dpi: Dpi) -> Option<(i32, i32)> {
match (self.w.get(), self.h.get(), self.vbox.get()) {
(w, h, Some(vb)) => Some((
w.hand_normalize(dpi_x, vb.0.width, 12.0).round() as i32,
h.hand_normalize(dpi_y, vb.0.height, 12.0).round() as i32,
w.hand_normalize(dpi.x(), vb.0.width, 12.0).round() as i32,
h.hand_normalize(dpi.y(), vb.0.height, 12.0).round() as i32,
)),
(w, h, None) if w.unit != LengthUnit::Percent && h.unit != LengthUnit::Percent => {
Some((
w.hand_normalize(dpi_x, 0.0, 12.0).round() as i32,
h.hand_normalize(dpi_y, 0.0, 12.0).round() as i32,
w.hand_normalize(dpi.x(), 0.0, 12.0).round() as i32,
h.hand_normalize(dpi.y(), 0.0, 12.0).round() as i32,
))
}
(_, _, _) => None,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment