diff --git a/examples/font2svg.rs b/examples/font2svg.rs index 253b132..abd86ed 100644 --- a/examples/font2svg.rs +++ b/examples/font2svg.rs @@ -544,12 +544,6 @@ impl<'a> ttf::colr::Painter<'a> for GlyphPainter<'a> { self.svg.end_element(); // g } - fn push_rotate(&mut self, angle: f32) { - let cc = (angle * std::f32::consts::PI).cos(); - let ss = (angle * std::f32::consts::PI).sin(); - self.push_transform(ttf::Transform::new(cc, ss, -ss, cc, 0.0, 0.0)); - } - fn push_transform(&mut self, transform: ttf::Transform) { self.transforms_stack.push(self.transform); self.transform = ttf::Transform::combine(self.transform, transform); diff --git a/src/lib.rs b/src/lib.rs index 942cf33..587119b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -432,6 +432,22 @@ impl Transform { Transform::new(1.0, 0.0, 0.0, 1.0, tx, ty) } + /// Creates a new rotation transform. + #[inline] + pub fn new_rotate(angle: f32) -> Self { + #[cfg(feature = "no-std")] + let (cc, ss) = ( + libm::cos((angle * core::f32::consts::PI) as f64) as f32, + libm::sin((angle * core::f32::consts::PI) as f64) as f32, + ); + #[cfg(feature = "std")] + let (cc, ss) = ( + (angle * std::f32::consts::PI).cos(), + (angle * std::f32::consts::PI).sin(), + ); + Transform::new(cc, ss, -ss, cc, 0.0, 0.0) + } + /// Creates a new skew transform. #[inline] pub fn new_skew(skew_x: f32, skew_y: f32) -> Self { diff --git a/src/tables/colr.rs b/src/tables/colr.rs index 324585a..b07e68d 100644 --- a/src/tables/colr.rs +++ b/src/tables/colr.rs @@ -694,10 +694,6 @@ pub trait Painter<'a> { fn push_layer(&mut self, mode: CompositeMode); /// Pop the last layer. fn pop_layer(&mut self); - - // TODO: Unify transforms into one callback. - /// Push a rotation transform. - fn push_rotate(&mut self, angle: f32); /// Push a transform. fn push_transform(&mut self, transform: Transform); /// Pop the last transform. @@ -1569,7 +1565,7 @@ impl<'a> Table<'a> { let paint_offset = s.read::()?; let angle = s.read::()?.to_f32(); - painter.push_rotate(angle); + painter.push_transform(Transform::new_rotate(angle)); self.parse_paint( offset + paint_offset.to_usize(), palette, @@ -1596,7 +1592,7 @@ impl<'a> Table<'a> { let angle = s.read::()?.apply_float_delta(deltas[0]); - painter.push_rotate(angle); + painter.push_transform(Transform::new_rotate(angle)); self.parse_paint( offset + paint_offset.to_usize(), palette, @@ -1615,7 +1611,7 @@ impl<'a> Table<'a> { let center_y = f32::from(s.read::()?); painter.push_transform(Transform::new_translate(center_x, center_y)); - painter.push_rotate(angle); + painter.push_transform(Transform::new_rotate(angle)); painter.push_transform(Transform::new_translate(-center_x, -center_y)); self.parse_paint( offset + paint_offset.to_usize(), @@ -1648,7 +1644,7 @@ impl<'a> Table<'a> { let center_y = f32::from(s.read::()?) + deltas[2]; painter.push_transform(Transform::new_translate(center_x, center_y)); - painter.push_rotate(angle); + painter.push_transform(Transform::new_rotate(angle)); painter.push_transform(Transform::new_translate(-center_x, -center_y)); self.parse_paint( offset + paint_offset.to_usize(), diff --git a/tests/tables/colr.rs b/tests/tables/colr.rs index 68de8df..f0db049 100644 --- a/tests/tables/colr.rs +++ b/tests/tables/colr.rs @@ -102,8 +102,6 @@ enum Command { Paint(CustomPaint), PushLayer(CompositeMode), PopLayer, - Rotate(f32), - Skew(f32, f32), Transform(ttf_parser::Transform), PopTransform, PushClip, @@ -145,10 +143,6 @@ impl<'a> Painter<'a> for VecPainter { self.0.push(Command::PopLayer) } - fn push_rotate(&mut self, angle: f32) { - self.0.push(Command::Rotate(angle)) - } - fn push_transform(&mut self, transform: ttf_parser::Transform) { self.0.push(Command::Transform(transform)) } @@ -279,7 +273,7 @@ mod colr1_static { let face = Face::parse(COLR1_STATIC, 0).unwrap(); let mut vec_painter = VecPainter(vec![]); face.paint_color_glyph(GlyphId(99), 0, RgbaColor::new(0, 0, 0, 255), &mut vec_painter); - assert!(vec_painter.0.contains(&Rotate(0.055541992))) + assert!(vec_painter.0.contains(&Transform(ttf_parser::Transform::new_rotate(0.055541992)))) } #[test] @@ -295,7 +289,7 @@ mod colr1_static { PopClip, PushLayer(DestinationOver), Transform(ttf_parser::Transform::new_translate(500.0, 500.0)), - Rotate(0.13891602), + Transform(ttf_parser::Transform::new_rotate(0.13891602)), Transform(ttf_parser::Transform::new_translate(-500.0, -500.0)), OutlineGlyph(GlyphId(3)), PushClip, @@ -455,7 +449,7 @@ mod colr1_variable { face.set_variation(Tag::from_bytes(b"ROTA"), 150.0); let mut vec_painter = VecPainter(vec![]); face.paint_color_glyph(GlyphId(99), 0, RgbaColor::new(0, 0, 0, 255), &mut vec_painter); - assert!(vec_painter.0.contains(&Rotate(0.87341005))) + assert!(vec_painter.0.contains(&Transform(ttf_parser::Transform::new_rotate(0.87341005)))) } #[test] @@ -464,7 +458,7 @@ mod colr1_variable { face.set_variation(Tag::from_bytes(b"ROTA"), 150.0); let mut vec_painter = VecPainter(vec![]); face.paint_color_glyph(GlyphId(101), 0, RgbaColor::new(0, 0, 0, 255), &mut vec_painter); - assert!(vec_painter.0.contains(&Rotate(0.9336252))) + assert!(vec_painter.0.contains(&Transform(ttf_parser::Transform::new_rotate(0.9336252)))) } #[test]