From f02bbf2ae988d8fec3324281043924dff532b5ed Mon Sep 17 00:00:00 2001 From: lights0123 Date: Thu, 17 Sep 2020 21:16:31 -0400 Subject: [PATCH] Use built-in functions --- render/svg/src/lib.rs | 58 +++++++++++-------------------------------- swf/src/types.rs | 24 ++++++++++++++++++ 2 files changed, 38 insertions(+), 44 deletions(-) diff --git a/render/svg/src/lib.rs b/render/svg/src/lib.rs index 16d6ec184469..bd46f48f6f32 100644 --- a/render/svg/src/lib.rs +++ b/render/svg/src/lib.rs @@ -247,36 +247,7 @@ impl RenderBackend for SvgRenderBackend { } fn register_glyph_shape(&mut self, glyph: &swf::Glyph) -> ShapeHandle { - // Per SWF19 p.164, the FontBoundsTable can contain empty bounds for every glyph (reserved). - // SWF19 says this is true through SWFv7, but it seems like it might be generally true? - // In any case, we have to be sure to calculate the shape bounds ourselves to make a proper - // SVG. - let bounds = glyph - .clone() - .bounds - .filter(|b| b.x_min != b.x_max || b.y_min != b.y_max) - .unwrap_or_else(|| { - ruffle_core::shape_utils::calculate_shape_bounds(&glyph.shape_records[..]) - }); - let shape = swf::Shape { - version: 2, - id: 0, - shape_bounds: bounds.clone(), - edge_bounds: bounds, - has_fill_winding_rule: false, - has_non_scaling_strokes: false, - has_scaling_strokes: true, - styles: swf::ShapeStyles { - fill_styles: vec![swf::FillStyle::Color(Color { - r: 255, - g: 255, - b: 255, - a: 255, - })], - line_styles: vec![], - }, - shape: glyph.shape_records.clone(), - }; + let shape = ruffle_core::shape_utils::swf_glyph_to_shape(glyph); self.register_shape((&shape).into()) } @@ -375,6 +346,17 @@ impl RenderBackend for SvgRenderBackend { } } + fn draw_rect(&mut self, Color { r, g, b, a }: Color, matrix: &Matrix) { + self.document.append( + Rectangle::new() + .set("width", 1) + .set("height", 1) + .set("transform", format!("matrix({})", matrix.get_transform())) + .set("fill", format!("rgb({},{},{})", r, g, b)) + .set("fill-opacity", f32::from(a) / 255.0), + ); + } + fn end_frame(&mut self) {} fn draw_letterbox(&mut self, letterbox: Letterbox) { @@ -854,18 +836,6 @@ fn swf_shape_to_svg( } /// Converts an SWF color from sRGB space to linear color space. -pub fn srgb_to_linear(mut color: swf::Color) -> swf::Color { - fn to_linear_channel(n: u8) -> u8 { - let mut n = f32::from(n) / 255.0; - n = if n <= 0.04045 { - n / 12.92 - } else { - f32::powf((n + 0.055) / 1.055, 2.4) - }; - (n.max(0.0).min(1.0) * 255.0).round() as u8 - } - color.r = to_linear_channel(color.r); - color.g = to_linear_channel(color.g); - color.b = to_linear_channel(color.b); - color +pub fn srgb_to_linear(color: swf::Color) -> swf::Color { + ruffle_core::backend::render::srgb_to_linear(color.into()).into() } diff --git a/swf/src/types.rs b/swf/src/types.rs index e2e60544ef06..4da4620ece46 100644 --- a/swf/src/types.rs +++ b/swf/src/types.rs @@ -182,6 +182,30 @@ impl Color { } } +impl From for [f32; 4] { + fn from(color: Color) -> Self { + let to_f32 = |n: u8| f32::from(n) / 255.0; + [ + to_f32(color.r), + to_f32(color.g), + to_f32(color.b), + to_f32(color.a), + ] + } +} + +impl From<[f32; 4]> for Color { + fn from(color: [f32; 4]) -> Self { + let from_f32 = |n: f32| (n.max(0.0).min(1.0) * 255.0).round() as u8; + Color { + r: from_f32(color[0]), + g: from_f32(color[1]), + b: from_f32(color[2]), + a: from_f32(color[3]), + } + } +} + #[derive(Debug, PartialEq, Clone)] pub struct ColorTransform { pub r_multiply: f32,