From a45fdba4d1f8888a87ae8178b85a498ba5e44de0 Mon Sep 17 00:00:00 2001 From: relrelb Date: Sun, 6 Jun 2021 00:00:20 +0300 Subject: [PATCH] swf: Number enum FontThickness --- swf/src/read.rs | 8 ++------ swf/src/types.rs | 18 ++++++++++++++---- swf/src/write.rs | 6 +----- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/swf/src/read.rs b/swf/src/read.rs index 3702cb898..586424928 100644 --- a/swf/src/read.rs +++ b/swf/src/read.rs @@ -1164,12 +1164,8 @@ impl<'a> Reader<'a> { fn read_define_font_align_zones(&mut self) -> Result> { let id = self.read_character_id()?; - let thickness = match self.read_u8()? { - 0b00_000000 => FontThickness::Thin, - 0b01_000000 => FontThickness::Medium, - 0b10_000000 => FontThickness::Thick, - _ => return Err(Error::invalid_data("Invalid font thickness type.")), - }; + let thickness = FontThickness::from_u8(self.read_u8()? >> 6) + .ok_or_else(|| Error::invalid_data("Invalid font thickness type."))?; let mut zones = vec![]; while let Ok(zone) = self.read_font_align_zone() { zones.push(zone); diff --git a/swf/src/types.rs b/swf/src/types.rs index 256383eb5..8ea1127c4 100644 --- a/swf/src/types.rs +++ b/swf/src/types.rs @@ -1398,11 +1398,21 @@ pub struct FontAlignZone { pub height: i16, } -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, FromPrimitive, ToPrimitive)] pub enum FontThickness { - Thin, - Medium, - Thick, + Thin = 0, + Medium = 1, + Thick = 2, +} + +impl FontThickness { + 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(Clone, Debug, PartialEq)] diff --git a/swf/src/write.rs b/swf/src/write.rs index 2a76a5061..39055af97 100644 --- a/swf/src/write.rs +++ b/swf/src/write.rs @@ -685,11 +685,7 @@ impl Writer { } => { self.write_tag_header(TagCode::DefineFontAlignZones, 3 + 10 * zones.len() as u32)?; self.write_character_id(id)?; - self.write_u8(match thickness { - FontThickness::Thin => 0b00_000000, - FontThickness::Medium => 0b01_000000, - FontThickness::Thick => 0b10_000000, - })?; + self.write_u8(thickness.to_u8() << 6)?; for zone in zones { self.write_u8(2)?; // Always 2 dimensions. self.write_i16(zone.left)?;