Skip to main content

ppt_rs/core/
placement.rs

1//! Shared placement state for positioned slide elements (tables, charts, images).
2
3/// Position and optional size for a slide element, in EMU.
4#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5pub struct ElementPlacement {
6    pub x: u32,
7    pub y: u32,
8    pub width: u32,
9    pub height: u32,
10}
11
12impl ElementPlacement {
13    /// Create placement at the origin with zero size.
14    pub const fn new() -> Self {
15        Self {
16            x: 0,
17            y: 0,
18            width: 0,
19            height: 0,
20        }
21    }
22
23    /// Create placement with default chart dimensions (5" × 3.75").
24    pub const fn chart_defaults() -> Self {
25        Self {
26            x: 0,
27            y: 0,
28            width: 5_000_000,
29            height: 3_750_000,
30        }
31    }
32
33    /// Create placement with default image dimensions (2" square).
34    pub const fn image_defaults() -> Self {
35        Self {
36            x: 0,
37            y: 0,
38            width: 1_828_800,
39            height: 1_828_800,
40        }
41    }
42
43    /// Set position (fluent, consuming).
44    pub fn with_position(mut self, x: u32, y: u32) -> Self {
45        self.x = x;
46        self.y = y;
47        self
48    }
49
50    /// Set size (fluent, consuming).
51    pub fn with_size(mut self, width: u32, height: u32) -> Self {
52        self.width = width;
53        self.height = height;
54        self
55    }
56
57    /// Set position (mutable builder style).
58    pub fn set_position(&mut self, x: u32, y: u32) {
59        self.x = x;
60        self.y = y;
61    }
62
63    /// Set size (mutable builder style).
64    pub fn set_size(&mut self, width: u32, height: u32) {
65        self.width = width;
66        self.height = height;
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn test_placement_defaults() {
76        let chart = ElementPlacement::chart_defaults();
77        assert_eq!(chart.width, 5_000_000);
78        assert_eq!(chart.height, 3_750_000);
79
80        let image = ElementPlacement::image_defaults();
81        assert_eq!(image.width, 1_828_800);
82    }
83
84    #[test]
85    fn test_placement_fluent() {
86        let p = ElementPlacement::new()
87            .with_position(100, 200)
88            .with_size(300, 400);
89        assert_eq!(p.x, 100);
90        assert_eq!(p.y, 200);
91        assert_eq!(p.width, 300);
92        assert_eq!(p.height, 400);
93    }
94}