@@ -151,10 +151,12 @@ impl FontTransform {
151
151
FontTransform :: Rotate90 => ( -y, x) ,
152
152
FontTransform :: Rotate180 => ( -x, -y) ,
153
153
FontTransform :: Rotate270 => ( y, -x) ,
154
- FontTransform :: RotateAngle ( angle) => (
155
- ( ( x as f32 ) * angle. cos ( ) ) . round ( ) as i32 ,
156
- ( ( y as f32 ) * angle. sin ( ) ) . round ( ) as i32 ,
157
- ) ,
154
+ FontTransform :: RotateAngle ( angle) => {
155
+ let ( x, y) = ( x as f32 , y as f32 ) ;
156
+ let ( sin, cos) = angle. to_radians ( ) . sin_cos ( ) ;
157
+ let ( x, y) = ( x * cos - y * sin, x * sin + y * cos) ;
158
+ ( x. round ( ) as i32 , y. round ( ) as i32 )
159
+ }
158
160
}
159
161
}
160
162
}
@@ -249,3 +251,19 @@ pub trait BackendTextStyle {
249
251
draw : DrawFunc ,
250
252
) -> Result < Result < ( ) , E > , Self :: FontError > ;
251
253
}
254
+
255
+ #[ cfg( test) ]
256
+ mod tests {
257
+ use crate :: FontTransform ;
258
+
259
+ #[ test]
260
+ fn text_rotation_works ( ) {
261
+ assert_eq ! ( FontTransform :: None . transform( 1 , 0 ) , ( 1 , 0 ) ) ;
262
+ assert_eq ! ( FontTransform :: Rotate90 . transform( 1 , 0 ) , ( 0 , 1 ) ) ;
263
+ assert_eq ! ( FontTransform :: Rotate180 . transform( 1 , 0 ) , ( -1 , 0 ) ) ;
264
+ assert_eq ! ( FontTransform :: Rotate270 . transform( 1 , 0 ) , ( 0 , -1 ) ) ;
265
+
266
+ assert_eq ! ( FontTransform :: RotateAngle ( 45f32 ) . transform( 10 , 0 ) , ( 7 , 7 ) ) ;
267
+ assert_eq ! ( FontTransform :: RotateAngle ( 45f32 ) . transform( 0 , 10 ) , ( -7 , 7 ) ) ;
268
+ }
269
+ }
0 commit comments