Skip to main content

winprint_ext/ticket/
page_resolution.rs

1use super::{
2    define_feature_option_pack,
3    document::{ParameterInit, PrintFeatureOption, WithScoredProperties, NS_PSK},
4};
5use xml::name::OwnedName;
6
7define_feature_option_pack!(
8    OwnedName::qualified("PageResolution", NS_PSK, Some("psk")),
9    PageResolution
10);
11
12impl PageResolution {
13    /// Get the resolution of the page in DPI.
14    pub fn dpi(&self) -> (u32, u32) {
15        let x = self
16            .option
17            .get_scored_property("ResolutionX", Some(NS_PSK))
18            .and_then(|x| x.value_with(&self.parameters))
19            .and_then(|x| x.integer())
20            .unwrap_or_default();
21        let y = self
22            .option
23            .get_scored_property("ResolutionY", Some(NS_PSK))
24            .and_then(|x| x.value_with(&self.parameters))
25            .and_then(|x| x.integer())
26            .unwrap_or_default();
27        (x as u32, y as u32)
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use crate::{
34        test_utils::null_device,
35        ticket::{PrintCapabilities, PrintTicketBuilder},
36    };
37
38    #[test]
39    fn use_page_resolution() {
40        let device = null_device::thread_local();
41        let capabilities = PrintCapabilities::fetch(&device).unwrap();
42        for resolution in capabilities.page_resolutions() {
43            let mut builder = PrintTicketBuilder::new(&device).unwrap();
44            builder.merge(resolution).unwrap();
45        }
46    }
47
48    #[test]
49    fn get_dpi() {
50        let device = null_device::thread_local();
51        let capabilities = PrintCapabilities::fetch(&device).unwrap();
52        for resolution in capabilities.page_resolutions() {
53            let (x, y) = resolution.dpi();
54            assert!(x > 0);
55            assert!(y > 0);
56        }
57    }
58}