1
//! Legacy C API for setting a default DPI (dots per inch = DPI).
2
//!
3
//! There are two deprecated functions, `rsvg_set_default_dpi` and
4
//! `rsvg_set_default_dpi_x_y`, which set global values for the default DPI to be used
5
//! with `RsvgHandle`.  In turn, `RsvgHandle` assumes that when its own DPI value is set
6
//! to `0.0` (which is in fact its default), it will fall back to the global DPI.
7
//!
8
//! This is clearly not thread-safe, but it is the legacy behavior.
9
//!
10
//! This module encapsulates that behavior so that the `rsvg_internals` crate
11
//! can always have immutable DPI values as intended.
12

            
13
// This is configurable at runtime
14
const DEFAULT_DPI_X: f64 = 90.0;
15
const DEFAULT_DPI_Y: f64 = 90.0;
16

            
17
static mut DPI_X: f64 = DEFAULT_DPI_X;
18
static mut DPI_Y: f64 = DEFAULT_DPI_Y;
19

            
20
122
#[derive(Debug, Copy, Clone, Default)]
21
pub(crate) struct Dpi {
22
61
    x: f64,
23
61
    y: f64,
24
}
25

            
26
impl Dpi {
27
118
    pub(crate) fn new(x: f64, y: f64) -> Dpi {
28
        Dpi { x, y }
29
118
    }
30

            
31
124
    pub(crate) fn x(&self) -> f64 {
32
124
        if self.x <= 0.0 {
33
60
            unsafe { DPI_X }
34
        } else {
35
64
            self.x
36
        }
37
124
    }
38

            
39
124
    pub(crate) fn y(&self) -> f64 {
40
124
        if self.y <= 0.0 {
41
118
            unsafe { DPI_Y }
42
        } else {
43
6
            self.y
44
        }
45
124
    }
46
}
47

            
48
#[no_mangle]
49
1
pub unsafe extern "C" fn rsvg_set_default_dpi_x_y(dpi_x: libc::c_double, dpi_y: libc::c_double) {
50
1
    if dpi_x <= 0.0 {
51
        DPI_X = DEFAULT_DPI_X;
52
    } else {
53
1
        DPI_X = dpi_x;
54
    }
55

            
56
1
    if dpi_y <= 0.0 {
57
        DPI_Y = DEFAULT_DPI_Y;
58
    } else {
59
1
        DPI_Y = dpi_y;
60
    }
61
1
}
62

            
63
#[no_mangle]
64
1
pub unsafe extern "C" fn rsvg_set_default_dpi(dpi: libc::c_double) {
65
1
    rsvg_set_default_dpi_x_y(dpi, dpi);
66
1
}