swf: Extract `DefineMorphShapeFlag`

This commit is contained in:
relrelb 2022-09-02 14:21:52 +03:00 committed by Mike Welsh
parent ae452d29b8
commit efd449dc0d
4 changed files with 31 additions and 36 deletions

View File

@ -1244,24 +1244,22 @@ impl<'a> Reader<'a> {
})
}
pub fn read_define_morph_shape(&mut self, shape_version: u8) -> Result<DefineMorphShape> {
pub fn read_define_morph_shape(&mut self, version: u8) -> Result<DefineMorphShape> {
let id = self.read_character_id()?;
let start_shape_bounds = self.read_rectangle()?;
let end_shape_bounds = self.read_rectangle()?;
let (start_edge_bounds, end_edge_bounds, has_non_scaling_strokes, has_scaling_strokes) =
if shape_version >= 2 {
let start_edge_bounds = self.read_rectangle()?;
let end_edge_bounds = self.read_rectangle()?;
let flags = self.read_u8()?;
(
start_edge_bounds,
end_edge_bounds,
flags & 0b10 != 0,
flags & 0b1 != 0,
)
} else {
(start_shape_bounds, end_shape_bounds, true, false)
};
let start_edge_bounds;
let end_edge_bounds;
let flags;
if version >= 2 {
start_edge_bounds = self.read_rectangle()?;
end_edge_bounds = self.read_rectangle()?;
flags = DefineMorphShapeFlag::from_bits_truncate(self.read_u8()?);
} else {
start_edge_bounds = start_shape_bounds;
end_edge_bounds = end_shape_bounds;
flags = DefineMorphShapeFlag::HAS_NON_SCALING_STROKES;
}
self.read_u32()?; // Offset to EndEdges.
@ -1284,7 +1282,7 @@ impl<'a> Reader<'a> {
let mut start_line_styles = Vec::with_capacity(num_line_styles);
let mut end_line_styles = Vec::with_capacity(num_line_styles);
for _ in 0..num_line_styles {
let (start, end) = self.read_morph_line_style(shape_version)?;
let (start, end) = self.read_morph_line_style(version)?;
start_line_styles.push(start);
end_line_styles.push(end);
}
@ -1294,7 +1292,7 @@ impl<'a> Reader<'a> {
let mut bits = self.bits();
let mut shape_context = ShapeContext {
swf_version,
shape_version,
shape_version: version,
num_fill_bits: bits.read_ubits(4)? as u8,
num_line_bits: bits.read_ubits(4)? as u8,
};
@ -1307,7 +1305,7 @@ impl<'a> Reader<'a> {
self.read_u8()?; // NumFillBits and NumLineBits are written as 0 for the end shape.
let mut shape_context = ShapeContext {
swf_version: self.version,
shape_version,
shape_version: version,
num_fill_bits: 0,
num_line_bits: 0,
};
@ -1315,11 +1313,11 @@ impl<'a> Reader<'a> {
while let Some(record) = Self::read_shape_record(&mut bits, &mut shape_context)? {
end_shape.push(record);
}
Ok(DefineMorphShape {
id,
version: shape_version,
has_non_scaling_strokes,
has_scaling_strokes,
version,
flags,
start: MorphShape {
shape_bounds: start_shape_bounds,
edge_bounds: start_edge_bounds,

View File

@ -690,8 +690,7 @@ pub fn tag_tests() -> Vec<TagTestData> {
Tag::DefineMorphShape(Box::new(DefineMorphShape {
version: 1,
id: 1,
has_non_scaling_strokes: true,
has_scaling_strokes: false,
flags: DefineMorphShapeFlag::HAS_NON_SCALING_STROKES,
start: MorphShape {
shape_bounds: Rectangle {
x_min: Twips::from_pixels(15.0),
@ -898,8 +897,7 @@ pub fn tag_tests() -> Vec<TagTestData> {
Tag::DefineMorphShape(Box::new(DefineMorphShape {
version: 2,
id: 1,
has_non_scaling_strokes: false,
has_scaling_strokes: true,
flags: DefineMorphShapeFlag::HAS_SCALING_STROKES,
start: MorphShape {
shape_bounds: Rectangle {
x_min: Twips::from_pixels(15.0),
@ -1130,8 +1128,7 @@ pub fn tag_tests() -> Vec<TagTestData> {
Tag::DefineMorphShape(Box::new(DefineMorphShape {
version: 2,
id: 1,
has_non_scaling_strokes: false,
has_scaling_strokes: true,
flags: DefineMorphShapeFlag::HAS_SCALING_STROKES,
start: MorphShape {
shape_bounds: Rectangle {
x_min: Twips::from_pixels(0.0),

View File

@ -1195,12 +1195,18 @@ bitflags! {
pub struct DefineMorphShape {
pub version: u8,
pub id: CharacterId,
pub has_non_scaling_strokes: bool,
pub has_scaling_strokes: bool,
pub flags: DefineMorphShapeFlag,
pub start: MorphShape,
pub end: MorphShape,
}
bitflags! {
pub struct DefineMorphShapeFlag: u8 {
const HAS_SCALING_STROKES = 1 << 0;
const HAS_NON_SCALING_STROKES = 1 << 1;
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MorphShape {
pub shape_bounds: Rectangle,

View File

@ -1060,13 +1060,7 @@ impl<W: Write> Writer<W> {
if data.version >= 2 {
writer.write_rectangle(&data.start.edge_bounds)?;
writer.write_rectangle(&data.end.edge_bounds)?;
writer.write_u8(
if data.has_non_scaling_strokes {
0b10
} else {
0
} | if data.has_scaling_strokes { 0b1 } else { 0 },
)?;
writer.write_u8(data.flags.bits())?;
}
// Offset to EndEdges.