@@ -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,15 @@ 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+ let pitch = self . pixel_pitch ( ) ;
20+ Point :: new ( output_point. x / pitch. x , output_point. y / pitch. y )
2121 }
2222
23- pub ( crate ) const fn pixel_pitch ( & self ) -> u32 {
24- self . scale + self . pixel_spacing
23+ pub ( crate ) const fn pixel_pitch ( & self ) -> Point {
24+ Point :: new (
25+ ( self . scale . width + self . pixel_spacing ) as i32 ,
26+ ( self . scale . height + self . pixel_spacing ) as i32 ,
27+ )
2528 }
2629}
2730
@@ -34,7 +37,7 @@ impl Default for OutputSettings {
3437/// Output settings builder.
3538#[ derive( Default ) ]
3639pub struct OutputSettingsBuilder {
37- scale : Option < u32 > ,
40+ scale : Option < Size > ,
3841 pixel_spacing : Option < u32 > ,
3942 theme : BinaryColorTheme ,
4043}
@@ -55,7 +58,7 @@ impl OutputSettingsBuilder {
5558 pub fn scale ( mut self , scale : u32 ) -> Self {
5659 assert ! ( scale > 0 , "scale must be > 0" ) ;
5760
58- self . scale = Some ( scale) ;
61+ self . scale = Some ( Size :: new ( scale, scale ) ) ;
5962
6063 self
6164 }
@@ -77,7 +80,7 @@ impl OutputSettingsBuilder {
7780 pub fn theme ( mut self , theme : BinaryColorTheme ) -> Self {
7881 self . theme = theme;
7982
80- self . scale . get_or_insert ( 3 ) ;
83+ self . scale . get_or_insert ( Size :: new ( 3 , 3 ) ) ;
8184 self . pixel_spacing . get_or_insert ( 1 ) ;
8285
8386 self
@@ -94,10 +97,25 @@ impl OutputSettingsBuilder {
9497 self
9598 }
9699
100+ /// Sets the pixel scale to a non-square value. This is useful for displaying
101+ /// non-square pixels.
102+ ///
103+ /// # Panics
104+ ///
105+ /// Panics if `width` or `height` is `0`.
106+ pub fn scale_non_square ( mut self , width : u32 , height : u32 ) -> Self {
107+ assert ! ( width > 0 , "width must be > 0" ) ;
108+ assert ! ( height > 0 , "height must be > 0" ) ;
109+
110+ self . scale = Some ( Size :: new ( width, height) ) ;
111+
112+ self
113+ }
114+
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 ( 1 , 1 ) ) ,
101119 pixel_spacing : self . pixel_spacing . unwrap_or ( 0 ) ,
102120 theme : self . theme ,
103121 }
0 commit comments