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
120
#[derive(Debug, Copy, Clone, Default)]
21
pub(crate) struct Dpi {
22
60
    x: f64,
23
60
    y: f64,
24
}
25

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

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

            
39
122
    pub(crate) fn y(&self) -> f64 {
40
122
        if self.y <= 0.0 {
41
116
            unsafe { DPI_Y }
42
        } else {
43
6
            self.y
44
        }
45
122
    }
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
}