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.
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))
}

View File

@ -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<Self> {
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<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)]

View File

@ -1299,14 +1299,7 @@ impl<W: Write> Writer<W> {
// 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<W: Write> Writer<W> {
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<W: Write> Writer<W> {
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<W: Write> Writer<W> {
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<W: Write> Writer<W> {
}
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(())
}