@@ -4,8 +4,8 @@ use embedded_graphics::prelude::*;
44/// Output settings.
55#[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
66pub struct OutputSettings {
7- /// Pixel scale.
8- pub scale : u32 ,
7+ /// Pixel scale, allowing for non-square pixels .
8+ pub scale : Size ,
99 /// Spacing between pixels.
1010 pub pixel_spacing : u32 ,
1111 /// Binary color theme.
@@ -16,12 +16,14 @@ pub struct OutputSettings {
1616impl OutputSettings {
1717 /// Translates a output coordinate to the corresponding display coordinate.
1818 pub ( crate ) const fn output_to_display ( & self , output_point : Point ) -> Point {
19- let pitch = self . pixel_pitch ( ) as i32 ;
20- Point :: new ( output_point. x / pitch, output_point. y / pitch)
19+ output_point. component_div ( self . pixel_pitch ( ) )
2120 }
2221
23- pub ( crate ) const fn pixel_pitch ( & self ) -> u32 {
24- self . scale + self . pixel_spacing
22+ pub ( crate ) const fn pixel_pitch ( & self ) -> Point {
23+ Point :: new (
24+ ( self . scale . width + self . pixel_spacing ) as i32 ,
25+ ( self . scale . height + self . pixel_spacing ) as i32 ,
26+ )
2527 }
2628}
2729
@@ -34,7 +36,7 @@ impl Default for OutputSettings {
3436/// Output settings builder.
3537#[ derive( Default ) ]
3638pub struct OutputSettingsBuilder {
37- scale : Option < u32 > ,
39+ scale : Option < Size > ,
3840 pixel_spacing : Option < u32 > ,
3941 theme : BinaryColorTheme ,
4042}
@@ -55,6 +57,22 @@ impl OutputSettingsBuilder {
5557 pub fn scale ( mut self , scale : u32 ) -> Self {
5658 assert ! ( scale > 0 , "scale must be > 0" ) ;
5759
60+ self . scale = Some ( Size :: new ( scale, scale) ) ;
61+
62+ self
63+ }
64+
65+ /// Sets a non-square pixel scale.
66+ ///
67+ /// This is useful for simulating a display with a non-square pixel aspect ratio.
68+ ///
69+ /// # Panics
70+ ///
71+ /// Panics if `width` or `height` is `0`.
72+ pub fn scale_non_square ( mut self , scale : Size ) -> Self {
73+ assert ! ( scale. width > 0 , "width must be > 0" ) ;
74+ assert ! ( scale. height > 0 , "height must be > 0" ) ;
75+
5876 self . scale = Some ( scale) ;
5977
6078 self
@@ -77,7 +95,7 @@ impl OutputSettingsBuilder {
7795 pub fn theme ( mut self , theme : BinaryColorTheme ) -> Self {
7896 self . theme = theme;
7997
80- self . scale . get_or_insert ( 3 ) ;
98+ self . scale . get_or_insert ( Size :: new_equal ( 3 ) ) ;
8199 self . pixel_spacing . get_or_insert ( 1 ) ;
82100
83101 self
@@ -97,7 +115,7 @@ impl OutputSettingsBuilder {
97115 /// Builds the output settings.
98116 pub fn build ( self ) -> OutputSettings {
99117 OutputSettings {
100- scale : self . scale . unwrap_or ( 1 ) ,
118+ scale : self . scale . unwrap_or ( Size :: new_equal ( 1 ) ) ,
101119 pixel_spacing : self . pixel_spacing . unwrap_or ( 0 ) ,
102120 theme : self . theme ,
103121 }
0 commit comments