Skip to content

Commit

Permalink
implement rotate
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Jun 18, 2024
1 parent caa39df commit d56b497
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
6 changes: 0 additions & 6 deletions examples/font2svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 4 additions & 8 deletions src/tables/colr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -1569,7 +1565,7 @@ impl<'a> Table<'a> {
let paint_offset = s.read::<Offset24>()?;
let angle = s.read::<F2DOT14>()?.to_f32();

painter.push_rotate(angle);
painter.push_transform(Transform::new_rotate(angle));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand All @@ -1596,7 +1592,7 @@ impl<'a> Table<'a> {

let angle = s.read::<F2DOT14>()?.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,
Expand All @@ -1615,7 +1611,7 @@ impl<'a> Table<'a> {
let center_y = f32::from(s.read::<i16>()?);

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(),
Expand Down Expand Up @@ -1648,7 +1644,7 @@ impl<'a> Table<'a> {
let center_y = f32::from(s.read::<i16>()?) + 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(),
Expand Down
14 changes: 4 additions & 10 deletions tests/tables/colr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ enum Command {
Paint(CustomPaint),
PushLayer(CompositeMode),
PopLayer,
Rotate(f32),
Skew(f32, f32),
Transform(ttf_parser::Transform),
PopTransform,
PushClip,
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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]
Expand All @@ -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,
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down

0 comments on commit d56b497

Please sign in to comment.