diff --git a/swf/src/read.rs b/swf/src/read.rs index 973ddbbcb..57860d00f 100644 --- a/swf/src/read.rs +++ b/swf/src/read.rs @@ -1356,24 +1356,16 @@ impl<'a> Reader<'a> { // MorphLineStyle2 in DefineMorphShape2. let flags0 = self.read_u8()?; let flags1 = self.read_u8()?; - let start_cap = match flags0 >> 6 { - 0 => LineCapStyle::Round, - 1 => LineCapStyle::None, - 2 => LineCapStyle::Square, - _ => return Err(Error::invalid_data("Invalid line cap type.")), - }; + let start_cap = LineCapStyle::from_u8(flags0 >> 6) + .ok_or_else(|| Error::invalid_data("Invalid line cap type."))?; let join_style_id = (flags0 >> 4) & 0b11; let has_fill = (flags0 & 0b1000) != 0; let allow_scale_x = (flags0 & 0b100) == 0; let allow_scale_y = (flags0 & 0b10) == 0; let is_pixel_hinted = (flags0 & 0b1) != 0; let allow_close = (flags1 & 0b100) == 0; - let end_cap = match flags1 & 0b11 { - 0 => LineCapStyle::Round, - 1 => LineCapStyle::None, - 2 => LineCapStyle::Square, - _ => return Err(Error::invalid_data("Invalid line cap type.")), - }; + let end_cap = LineCapStyle::from_u8(flags1 & 0b11) + .ok_or_else(|| Error::invalid_data("Invalid line cap type."))?; let join_style = match join_style_id { 0 => LineJoinStyle::Round, 1 => LineJoinStyle::Bevel, @@ -1691,24 +1683,16 @@ impl<'a> Reader<'a> { let width = Twips::new(self.read_u16()?); let flags0 = self.read_u8()?; let flags1 = self.read_u8()?; - let start_cap = match flags0 >> 6 { - 0 => LineCapStyle::Round, - 1 => LineCapStyle::None, - 2 => LineCapStyle::Square, - _ => return Err(Error::invalid_data("Invalid line cap type.")), - }; + let start_cap = LineCapStyle::from_u8(flags0 >> 6) + .ok_or_else(|| Error::invalid_data("Invalid line cap type."))?; let join_style_id = (flags0 >> 4) & 0b11; let has_fill = (flags0 & 0b1000) != 0; let allow_scale_x = (flags0 & 0b100) == 0; let allow_scale_y = (flags0 & 0b10) == 0; let is_pixel_hinted = (flags0 & 0b1) != 0; let allow_close = (flags1 & 0b100) == 0; - let end_cap = match flags1 & 0b11 { - 0 => LineCapStyle::Round, - 1 => LineCapStyle::None, - 2 => LineCapStyle::Square, - _ => return Err(Error::invalid_data("Invalid line cap type.")), - }; + let end_cap = LineCapStyle::from_u8(flags1 & 0b11) + .ok_or_else(|| Error::invalid_data("Invalid line cap type."))?; let join_style = match join_style_id { 0 => LineJoinStyle::Round, 1 => LineJoinStyle::Bevel, @@ -1771,11 +1755,8 @@ impl<'a> Reader<'a> { let flags = self.read_u8()?; let spread = GradientSpread::from_u8((flags >> 6) & 0b11) .ok_or_else(|| Error::invalid_data("Invalid gradient spread mode"))?; - let interpolation = match flags & 0b11_0000 { - 0b00_0000 => GradientInterpolation::Rgb, - 0b01_0000 => GradientInterpolation::LinearRgb, - _ => return Err(Error::invalid_data("Invalid gradient interpolation mode")), - }; + let interpolation = GradientInterpolation::from_u8((flags >> 4) & 0b11) + .ok_or_else(|| Error::invalid_data("Invalid gradient interpolation mode"))?; let num_records = usize::from(flags & 0b1111); Ok((num_records, spread, interpolation)) } diff --git a/swf/src/types.rs b/swf/src/types.rs index 87e809340..23dc05a02 100644 --- a/swf/src/types.rs +++ b/swf/src/types.rs @@ -1044,10 +1044,20 @@ impl GradientSpread { } } -#[derive(Debug, PartialEq, Clone, Copy)] +#[derive(Debug, PartialEq, Clone, Copy, FromPrimitive, ToPrimitive)] pub enum GradientInterpolation { - Rgb, - LinearRgb, + Rgb = 0, + LinearRgb = 1, +} + +impl GradientInterpolation { + pub fn from_u8(n: u8) -> Option { + num_traits::FromPrimitive::from_u8(n) + } + + pub fn to_u8(self) -> u8 { + num_traits::ToPrimitive::to_u8(&self).unwrap() + } } #[derive(Debug, PartialEq, Clone)] @@ -1087,11 +1097,21 @@ impl LineStyle { } } -#[derive(Debug, PartialEq, Clone, Copy)] +#[derive(Debug, PartialEq, Clone, Copy, FromPrimitive, ToPrimitive)] pub enum LineCapStyle { - Round, - None, - Square, + Round = 0, + None = 1, + Square = 2, +} + +impl LineCapStyle { + pub fn from_u8(n: u8) -> Option { + num_traits::FromPrimitive::from_u8(n) + } + + pub fn to_u8(self) -> u8 { + num_traits::ToPrimitive::to_u8(&self).unwrap() + } } #[derive(Debug, PartialEq, Clone, Copy)] diff --git a/swf/src/write.rs b/swf/src/write.rs index 8d6c2a2ca..70e3ab1c0 100644 --- a/swf/src/write.rs +++ b/swf/src/write.rs @@ -1299,14 +1299,7 @@ impl Writer { // MorphLineStyle2 let mut bits = self.bits(); - bits.write_ubits( - 2, - match start.start_cap { - LineCapStyle::Round => 0, - LineCapStyle::None => 1, - LineCapStyle::Square => 2, - }, - )?; + bits.write_ubits(2, start.start_cap.to_u8().into())?; bits.write_ubits( 2, match start.join_style { @@ -1321,14 +1314,7 @@ impl Writer { bits.write_bit(start.is_pixel_hinted)?; bits.write_ubits(5, 0)?; bits.write_bit(!start.allow_close)?; - bits.write_ubits( - 2, - match start.end_cap { - LineCapStyle::Round => 0, - LineCapStyle::None => 1, - LineCapStyle::Square => 2, - }, - )?; + bits.write_ubits(2, start.end_cap.to_u8().into())?; drop(bits); if let LineJoinStyle::Miter(miter_factor) = start.join_style { self.write_fixed8(miter_factor)?; @@ -1671,14 +1657,7 @@ impl Writer { if shape_version >= 4 { let mut bits = self.bits(); // LineStyle2 - bits.write_ubits( - 2, - match line_style.start_cap { - LineCapStyle::Round => 0, - LineCapStyle::None => 1, - LineCapStyle::Square => 2, - }, - )?; + bits.write_ubits(2, line_style.start_cap.to_u8().into())?; bits.write_ubits( 2, match line_style.join_style { @@ -1693,14 +1672,7 @@ impl Writer { bits.write_bit(line_style.is_pixel_hinted)?; bits.write_ubits(5, 0)?; bits.write_bit(!line_style.allow_close)?; - bits.write_ubits( - 2, - match line_style.end_cap { - LineCapStyle::Round => 0, - LineCapStyle::None => 1, - LineCapStyle::Square => 2, - }, - )?; + bits.write_ubits(2, line_style.end_cap.to_u8().into())?; drop(bits); if let LineJoinStyle::Miter(miter_factor) = line_style.join_style { self.write_fixed8(miter_factor)?; @@ -1734,13 +1706,9 @@ impl Writer { } fn write_gradient_flags(&mut self, gradient: &Gradient) -> Result<()> { - let mut flags = 0; - flags |= gradient.spread.to_u8() << 6; - flags |= match &gradient.interpolation { - GradientInterpolation::Rgb => 0b00_0000, - GradientInterpolation::LinearRgb => 0b_01_0000, - }; - flags |= (gradient.records.len() as u8) & 0b1111; + let flags = (gradient.spread.to_u8() << 6) + | (gradient.interpolation.to_u8() << 4) + | ((gradient.records.len() as u8) & 0b1111); self.write_u8(flags)?; Ok(()) }