swf: Number enum LineCapStyle

This commit is contained in:
relrelb 2021-06-05 23:47:02 +03:00 committed by Mike Welsh
parent f6d04061b0
commit ee14945974
3 changed files with 44 additions and 75 deletions

View File

@ -1356,24 +1356,16 @@ impl<'a> Reader<'a> {
// MorphLineStyle2 in DefineMorphShape2. // MorphLineStyle2 in DefineMorphShape2.
let flags0 = self.read_u8()?; let flags0 = self.read_u8()?;
let flags1 = self.read_u8()?; let flags1 = self.read_u8()?;
let start_cap = match flags0 >> 6 { let start_cap = LineCapStyle::from_u8(flags0 >> 6)
0 => LineCapStyle::Round, .ok_or_else(|| Error::invalid_data("Invalid line cap type."))?;
1 => LineCapStyle::None,
2 => LineCapStyle::Square,
_ => return Err(Error::invalid_data("Invalid line cap type.")),
};
let join_style_id = (flags0 >> 4) & 0b11; let join_style_id = (flags0 >> 4) & 0b11;
let has_fill = (flags0 & 0b1000) != 0; let has_fill = (flags0 & 0b1000) != 0;
let allow_scale_x = (flags0 & 0b100) == 0; let allow_scale_x = (flags0 & 0b100) == 0;
let allow_scale_y = (flags0 & 0b10) == 0; let allow_scale_y = (flags0 & 0b10) == 0;
let is_pixel_hinted = (flags0 & 0b1) != 0; let is_pixel_hinted = (flags0 & 0b1) != 0;
let allow_close = (flags1 & 0b100) == 0; let allow_close = (flags1 & 0b100) == 0;
let end_cap = match flags1 & 0b11 { let end_cap = LineCapStyle::from_u8(flags1 & 0b11)
0 => LineCapStyle::Round, .ok_or_else(|| Error::invalid_data("Invalid line cap type."))?;
1 => LineCapStyle::None,
2 => LineCapStyle::Square,
_ => return Err(Error::invalid_data("Invalid line cap type.")),
};
let join_style = match join_style_id { let join_style = match join_style_id {
0 => LineJoinStyle::Round, 0 => LineJoinStyle::Round,
1 => LineJoinStyle::Bevel, 1 => LineJoinStyle::Bevel,
@ -1691,24 +1683,16 @@ impl<'a> Reader<'a> {
let width = Twips::new(self.read_u16()?); let width = Twips::new(self.read_u16()?);
let flags0 = self.read_u8()?; let flags0 = self.read_u8()?;
let flags1 = self.read_u8()?; let flags1 = self.read_u8()?;
let start_cap = match flags0 >> 6 { let start_cap = LineCapStyle::from_u8(flags0 >> 6)
0 => LineCapStyle::Round, .ok_or_else(|| Error::invalid_data("Invalid line cap type."))?;
1 => LineCapStyle::None,
2 => LineCapStyle::Square,
_ => return Err(Error::invalid_data("Invalid line cap type.")),
};
let join_style_id = (flags0 >> 4) & 0b11; let join_style_id = (flags0 >> 4) & 0b11;
let has_fill = (flags0 & 0b1000) != 0; let has_fill = (flags0 & 0b1000) != 0;
let allow_scale_x = (flags0 & 0b100) == 0; let allow_scale_x = (flags0 & 0b100) == 0;
let allow_scale_y = (flags0 & 0b10) == 0; let allow_scale_y = (flags0 & 0b10) == 0;
let is_pixel_hinted = (flags0 & 0b1) != 0; let is_pixel_hinted = (flags0 & 0b1) != 0;
let allow_close = (flags1 & 0b100) == 0; let allow_close = (flags1 & 0b100) == 0;
let end_cap = match flags1 & 0b11 { let end_cap = LineCapStyle::from_u8(flags1 & 0b11)
0 => LineCapStyle::Round, .ok_or_else(|| Error::invalid_data("Invalid line cap type."))?;
1 => LineCapStyle::None,
2 => LineCapStyle::Square,
_ => return Err(Error::invalid_data("Invalid line cap type.")),
};
let join_style = match join_style_id { let join_style = match join_style_id {
0 => LineJoinStyle::Round, 0 => LineJoinStyle::Round,
1 => LineJoinStyle::Bevel, 1 => LineJoinStyle::Bevel,
@ -1771,11 +1755,8 @@ impl<'a> Reader<'a> {
let flags = self.read_u8()?; let flags = self.read_u8()?;
let spread = GradientSpread::from_u8((flags >> 6) & 0b11) let spread = GradientSpread::from_u8((flags >> 6) & 0b11)
.ok_or_else(|| Error::invalid_data("Invalid gradient spread mode"))?; .ok_or_else(|| Error::invalid_data("Invalid gradient spread mode"))?;
let interpolation = match flags & 0b11_0000 { let interpolation = GradientInterpolation::from_u8((flags >> 4) & 0b11)
0b00_0000 => GradientInterpolation::Rgb, .ok_or_else(|| Error::invalid_data("Invalid gradient interpolation mode"))?;
0b01_0000 => GradientInterpolation::LinearRgb,
_ => return Err(Error::invalid_data("Invalid gradient interpolation mode")),
};
let num_records = usize::from(flags & 0b1111); let num_records = usize::from(flags & 0b1111);
Ok((num_records, spread, interpolation)) Ok((num_records, spread, interpolation))
} }

View File

@ -1044,10 +1044,20 @@ impl GradientSpread {
} }
} }
#[derive(Debug, PartialEq, Clone, Copy)] #[derive(Debug, PartialEq, Clone, Copy, FromPrimitive, ToPrimitive)]
pub enum GradientInterpolation { pub enum GradientInterpolation {
Rgb, Rgb = 0,
LinearRgb, LinearRgb = 1,
}
impl GradientInterpolation {
pub fn from_u8(n: u8) -> Option<Self> {
num_traits::FromPrimitive::from_u8(n)
}
pub fn to_u8(self) -> u8 {
num_traits::ToPrimitive::to_u8(&self).unwrap()
}
} }
#[derive(Debug, PartialEq, Clone)] #[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 { pub enum LineCapStyle {
Round, Round = 0,
None, None = 1,
Square, Square = 2,
}
impl LineCapStyle {
pub fn from_u8(n: u8) -> Option<Self> {
num_traits::FromPrimitive::from_u8(n)
}
pub fn to_u8(self) -> u8 {
num_traits::ToPrimitive::to_u8(&self).unwrap()
}
} }
#[derive(Debug, PartialEq, Clone, Copy)] #[derive(Debug, PartialEq, Clone, Copy)]

View File

@ -1299,14 +1299,7 @@ impl<W: Write> Writer<W> {
// MorphLineStyle2 // MorphLineStyle2
let mut bits = self.bits(); let mut bits = self.bits();
bits.write_ubits( bits.write_ubits(2, start.start_cap.to_u8().into())?;
2,
match start.start_cap {
LineCapStyle::Round => 0,
LineCapStyle::None => 1,
LineCapStyle::Square => 2,
},
)?;
bits.write_ubits( bits.write_ubits(
2, 2,
match start.join_style { match start.join_style {
@ -1321,14 +1314,7 @@ impl<W: Write> Writer<W> {
bits.write_bit(start.is_pixel_hinted)?; bits.write_bit(start.is_pixel_hinted)?;
bits.write_ubits(5, 0)?; bits.write_ubits(5, 0)?;
bits.write_bit(!start.allow_close)?; bits.write_bit(!start.allow_close)?;
bits.write_ubits( bits.write_ubits(2, start.end_cap.to_u8().into())?;
2,
match start.end_cap {
LineCapStyle::Round => 0,
LineCapStyle::None => 1,
LineCapStyle::Square => 2,
},
)?;
drop(bits); drop(bits);
if let LineJoinStyle::Miter(miter_factor) = start.join_style { if let LineJoinStyle::Miter(miter_factor) = start.join_style {
self.write_fixed8(miter_factor)?; self.write_fixed8(miter_factor)?;
@ -1671,14 +1657,7 @@ impl<W: Write> Writer<W> {
if shape_version >= 4 { if shape_version >= 4 {
let mut bits = self.bits(); let mut bits = self.bits();
// LineStyle2 // LineStyle2
bits.write_ubits( bits.write_ubits(2, line_style.start_cap.to_u8().into())?;
2,
match line_style.start_cap {
LineCapStyle::Round => 0,
LineCapStyle::None => 1,
LineCapStyle::Square => 2,
},
)?;
bits.write_ubits( bits.write_ubits(
2, 2,
match line_style.join_style { match line_style.join_style {
@ -1693,14 +1672,7 @@ impl<W: Write> Writer<W> {
bits.write_bit(line_style.is_pixel_hinted)?; bits.write_bit(line_style.is_pixel_hinted)?;
bits.write_ubits(5, 0)?; bits.write_ubits(5, 0)?;
bits.write_bit(!line_style.allow_close)?; bits.write_bit(!line_style.allow_close)?;
bits.write_ubits( bits.write_ubits(2, line_style.end_cap.to_u8().into())?;
2,
match line_style.end_cap {
LineCapStyle::Round => 0,
LineCapStyle::None => 1,
LineCapStyle::Square => 2,
},
)?;
drop(bits); drop(bits);
if let LineJoinStyle::Miter(miter_factor) = line_style.join_style { if let LineJoinStyle::Miter(miter_factor) = line_style.join_style {
self.write_fixed8(miter_factor)?; self.write_fixed8(miter_factor)?;
@ -1734,13 +1706,9 @@ impl<W: Write> Writer<W> {
} }
fn write_gradient_flags(&mut self, gradient: &Gradient) -> Result<()> { fn write_gradient_flags(&mut self, gradient: &Gradient) -> Result<()> {
let mut flags = 0; let flags = (gradient.spread.to_u8() << 6)
flags |= gradient.spread.to_u8() << 6; | (gradient.interpolation.to_u8() << 4)
flags |= match &gradient.interpolation { | ((gradient.records.len() as u8) & 0b1111);
GradientInterpolation::Rgb => 0b00_0000,
GradientInterpolation::LinearRgb => 0b_01_0000,
};
flags |= (gradient.records.len() as u8) & 0b1111;
self.write_u8(flags)?; self.write_u8(flags)?;
Ok(()) Ok(())
} }