From 2a53c91b02db34586072a074ee208fa4b73a2ba5 Mon Sep 17 00:00:00 2001 From: Moulins Date: Sat, 7 May 2022 23:05:46 +0200 Subject: [PATCH 1/3] Match flag order for `DefineFont` and `DefineFontInfo` --- CHANGELOG.md | 4 ++++ rs/src/tags.rs | 2 +- tests/tags | 2 +- ts/src/lib/tags/define-font-info.mts | 4 ++-- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26f8d47..7c63229 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# Next + +- **[Change]** Match flag order for `DefineFont` and `DefineFontInfo`. + # 0.14.0 (2022-05-07) ## Rust diff --git a/rs/src/tags.rs b/rs/src/tags.rs index 7ac0896..2e8bf0e 100644 --- a/rs/src/tags.rs +++ b/rs/src/tags.rs @@ -186,8 +186,8 @@ pub struct DefineFontInfo { pub is_bold: bool, pub is_italic: bool, pub is_ansi: bool, - pub is_shift_jis: bool, pub is_small: bool, + pub is_shift_jis: bool, pub language: LanguageCode, pub code_units: Vec, } diff --git a/tests/tags b/tests/tags index cdec40f..2586809 160000 --- a/tests/tags +++ b/tests/tags @@ -1 +1 @@ -Subproject commit cdec40fca2674008b47f53c00a6cc5d484892fca +Subproject commit 258680930fc3712496f8f634c6070bebb2595ed8 diff --git a/ts/src/lib/tags/define-font-info.mts b/ts/src/lib/tags/define-font-info.mts index 8041b29..573c0fd 100644 --- a/ts/src/lib/tags/define-font-info.mts +++ b/ts/src/lib/tags/define-font-info.mts @@ -18,8 +18,8 @@ export interface DefineFontInfo extends _Tag { readonly isBold: boolean; readonly isItalic: boolean; readonly isAnsi: boolean; - readonly isShiftJis: boolean; readonly isSmall: boolean; + readonly isShiftJis: boolean; readonly language: LanguageCode; readonly codeUnits: ReadonlyArray; } @@ -32,8 +32,8 @@ export const $DefineFontInfo: RecordIoType = new RecordType Date: Sun, 8 May 2022 00:50:49 +0200 Subject: [PATCH 2/3] [rs] Use bitflags instead of groups of boolean fields --- CHANGELOG.md | 6 +++ rs/Cargo.lock | 7 ++++ rs/Cargo.toml | 3 +- rs/src/bitflags.rs | 61 ++++++++++++++++++++++++++++++ rs/src/button.rs | 53 +++++++++++++++++++------- rs/src/filters.rs | 83 ++++++++++++++++++++++++++++++++--------- rs/src/lib.rs | 3 ++ rs/src/shape.rs | 90 ++++++++++++++++++++++++++++---------------- rs/src/tags.rs | 93 +++++++++++++++++++++++++++++++--------------- 9 files changed, 306 insertions(+), 93 deletions(-) create mode 100644 rs/src/bitflags.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c63229..5d736a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ - **[Change]** Match flag order for `DefineFont` and `DefineFontInfo`. +## Rust + +- **[Breaking change]** Use bitflags instead of separate `bool` fields in `Bevel`, `ButtonCond`, `ButtonRecord`, + `ClipEventFlags`, `DefineDynamicText`, `DefineFont`, `DefineFontInfo`, `FileAttributes`, `Glow`, `GradientBevel`, + `GradientGlow`, `LineStyle` and `MorphLineStyle`. + # 0.14.0 (2022-05-07) ## Rust diff --git a/rs/Cargo.lock b/rs/Cargo.lock index 4312c06..1b55faa 100644 --- a/rs/Cargo.lock +++ b/rs/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "glob" version = "0.3.0" @@ -124,6 +130,7 @@ dependencies = [ name = "swf-types" version = "0.14.0" dependencies = [ + "bitflags", "hex", "serde", "serde_json_v8", diff --git a/rs/Cargo.toml b/rs/Cargo.toml index c7d4746..c46dc8b 100644 --- a/rs/Cargo.toml +++ b/rs/Cargo.toml @@ -20,6 +20,7 @@ name = "swf_types" path = "src/lib.rs" [dependencies] +bitflags = "1.3.2" hex = { version = "0.4.3", optional = true } serde = { version = "1.0.137", optional = true, features = ["derive"] } swf-fixed = "0.1.5" @@ -29,7 +30,7 @@ swf-fixed = "0.1.5" # Use `swf-types = { version = "...", default-features = false }` to disable it. default = ["serde"] -# Adds implementation for `serde`'s `Serialiaze` and `Deserialize` traits. +# Adds implementation for `serde`'s `Serialize` and `Deserialize` traits. serde = ["dep:serde", "dep:hex"] [dev-dependencies] diff --git a/rs/src/bitflags.rs b/rs/src/bitflags.rs new file mode 100644 index 0000000..6f683c2 --- /dev/null +++ b/rs/src/bitflags.rs @@ -0,0 +1,61 @@ + +macro_rules! serde_bitflags { + ( + $(#[$outer:meta])* + $vis:vis struct $BitFlags:ident: $T:ty { + $( + #[serde(name = $serde:literal)] + $(#[$inner:ident $($args:tt)*])* + const $Flag:ident = $value:expr; + )* + } + ) => { + ::bitflags::bitflags! { + $(#[$outer])* + $vis struct $BitFlags: $T { + $( + $(#[$inner $($args)*])* + const $Flag = $value; + )* + } + } + + #[cfg(feature = "serde")] + impl ::serde::Serialize for $BitFlags { + fn serialize(&self, ser: S) -> Result { + use ::serde::ser::SerializeStruct; + + const NB_FLAGS: usize = [$($serde),*].len(); + let mut map = ser.serialize_struct(stringify!($BitFlags), NB_FLAGS)?; + $( + map.serialize_field($serde, &self.contains($BitFlags::$Flag))?; + )* + map.end() + } + } + + #[cfg(feature = "serde")] + impl<'de> ::serde::Deserialize<'de> for $BitFlags { + fn deserialize>(de: D) -> Result { + #[derive(::serde::Deserialize)] + #[allow(non_snake_case)] + struct $BitFlags { + $( + #[serde(rename = $serde)] + $Flag: bool, + )* + } + + let flags = <$BitFlags as ::serde::Deserialize>::deserialize(de)?; + + let mut out = Self::empty(); + $( + if flags.$Flag { + out.insert(Self::$Flag); + } + )* + Ok(out) + } + } + } +} diff --git a/rs/src/button.rs b/rs/src/button.rs index 32ce600..46d2dfe 100644 --- a/rs/src/button.rs +++ b/rs/src/button.rs @@ -11,10 +11,8 @@ use crate::{BlendMode, SoundInfo}; #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Clone, Debug, PartialEq, Eq)] pub struct ButtonRecord { - pub state_up: bool, - pub state_over: bool, - pub state_down: bool, - pub state_hit_test: bool, + #[cfg_attr(feature = "serde", serde(flatten))] + pub state: ButtonStates, pub character_id: u16, pub depth: u16, pub matrix: Matrix, @@ -23,6 +21,19 @@ pub struct ButtonRecord { pub blend_mode: BlendMode, } +serde_bitflags! { + pub struct ButtonStates: u8 { + #[serde(name = "state_up")] + const UP = 1 << 0; + #[serde(name = "state_over")] + const OVER = 1 << 1; + #[serde(name = "state_down")] + const DOWN = 1 << 2; + #[serde(name = "state_hit_test")] + const HIT_TEST = 1 << 3; + } +} + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ButtonCondAction { @@ -35,18 +46,34 @@ pub struct ButtonCondAction { pub actions: Vec, } +serde_bitflags! { + pub struct ButtonStateTransitions: u16 { + #[serde(name = "idle_to_over_up")] + const IDLE_TO_OVER_UP = 1 << 0; + #[serde(name = "over_up_to_idle")] + const OVER_UP_TO_IDLE = 1 << 1; + #[serde(name = "over_up_to_over_down")] + const OVER_UP_TO_OVER_DOWN = 1 << 2; + #[serde(name = "over_down_to_over_up")] + const OVER_DOWN_TO_OVER_UP = 1 << 3; + #[serde(name = "over_down_to_out_down")] + const OVER_DOWN_TO_OUT_DOWN = 1 << 4; + #[serde(name = "out_down_to_over_down")] + const OUT_DOWN_TO_OVER_DOWN = 1 << 5; + #[serde(name = "out_down_to_idle")] + const OUT_DOWN_TO_IDLE = 1 << 6; + #[serde(name = "idle_to_over_down")] + const IDLE_TO_OVER_DOWN = 1 << 7; + #[serde(name = "over_down_to_idle")] + const OVER_DOWN_TO_IDLE = 1 << 8; + } +} + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ButtonCond { - pub idle_to_over_up: bool, - pub over_up_to_idle: bool, - pub over_up_to_over_down: bool, - pub over_down_to_over_up: bool, - pub over_down_to_out_down: bool, - pub out_down_to_over_down: bool, - pub out_down_to_idle: bool, - pub idle_to_over_down: bool, - pub over_down_to_idle: bool, + #[cfg_attr(feature = "serde", serde(flatten))] + pub transitions: ButtonStateTransitions, #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))] pub key_press: Option, } diff --git a/rs/src/filters.rs b/rs/src/filters.rs index 352bcd6..8bcc7ae 100644 --- a/rs/src/filters.rs +++ b/rs/src/filters.rs @@ -6,6 +6,40 @@ use crate::fixed::{Sfixed16P16, Sfixed8P8}; use crate::float_is::Is; use crate::gradient::ColorStop; +macro_rules! decl_filter_flags { + ($(#[$meta:meta])* $vis:vis struct $name:ident;) => { + serde_bitflags! { + $(#[$meta])* + $vis struct $name: u8 { + #[serde(name = "composite_source")] + const COMPOSITE_SOURCE = 1 << 5; + #[serde(name = "knockout")] + const KNOCKOUT = 1 << 6; + #[serde(name = "inner")] + const INNER = 1 << 7; + } + } + } +} + +macro_rules! decl_filter_flags_full { + ($(#[$meta:meta])* $vis:vis struct $name:ident;) => { + serde_bitflags! { + $(#[$meta])* + $vis struct $name: u8 { + #[serde(name = "on_top")] + const ON_TOP = 1 << 4; + #[serde(name = "composite_source")] + const COMPOSITE_SOURCE = 1 << 5; + #[serde(name = "knockout")] + const KNOCKOUT = 1 << 6; + #[serde(name = "inner")] + const INNER = 1 << 7; + } + } + } +} + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Blur { @@ -24,13 +58,15 @@ pub struct Bevel { pub angle: Sfixed16P16, pub distance: Sfixed16P16, pub strength: Sfixed8P8, - pub inner: bool, - pub knockout: bool, - pub composite_source: bool, - pub on_top: bool, + #[cfg_attr(feature = "serde", serde(flatten))] + pub flags: BevelFlags, pub passes: u8, } +decl_filter_flags_full! { + pub struct BevelFlags; +} + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Clone, Debug)] pub struct ColorMatrix { @@ -82,12 +118,15 @@ pub struct DropShadow { pub angle: Sfixed16P16, pub distance: Sfixed16P16, pub strength: Sfixed8P8, - pub inner: bool, - pub knockout: bool, - pub composite_source: bool, + #[cfg_attr(feature = "serde", serde(flatten))] + pub flags: DropShadowFlags, pub passes: u8, } +decl_filter_flags! { + pub struct DropShadowFlags; +} + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Glow { @@ -95,12 +134,15 @@ pub struct Glow { pub blur_x: Sfixed16P16, pub blur_y: Sfixed16P16, pub strength: Sfixed8P8, - pub inner: bool, - pub knockout: bool, - pub composite_source: bool, + #[cfg_attr(feature = "serde", serde(flatten))] + pub flags: GlowFlags, pub passes: u8, } +decl_filter_flags! { + pub struct GlowFlags; +} + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct GradientBevel { @@ -110,13 +152,15 @@ pub struct GradientBevel { pub angle: Sfixed16P16, pub distance: Sfixed16P16, pub strength: Sfixed8P8, - pub inner: bool, - pub knockout: bool, - pub composite_source: bool, - pub on_top: bool, + #[cfg_attr(feature = "serde", serde(flatten))] + pub flags: GradientBevelFlags, pub passes: u8, } +decl_filter_flags_full! { + pub struct GradientBevelFlags; +} + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct GradientGlow { @@ -126,9 +170,12 @@ pub struct GradientGlow { pub angle: Sfixed16P16, pub distance: Sfixed16P16, pub strength: Sfixed8P8, - pub inner: bool, - pub knockout: bool, - pub composite_source: bool, - pub on_top: bool, + #[cfg_attr(feature = "serde", serde(flatten))] + pub flags: GradientGlowFlags, pub passes: u8, } + +decl_filter_flags_full! { + pub struct GradientGlowFlags; +} + diff --git a/rs/src/lib.rs b/rs/src/lib.rs index 4c4a6c2..583de8c 100644 --- a/rs/src/lib.rs +++ b/rs/src/lib.rs @@ -54,6 +54,9 @@ pub use crate::sound::SoundType; pub use crate::video::VideoCodec; pub use crate::video::VideoDeblocking; +#[macro_use] +mod bitflags; + pub mod float_is; #[cfg(feature = "serde")] diff --git a/rs/src/shape.rs b/rs/src/shape.rs index 2f116d3..8143813 100644 --- a/rs/src/shape.rs +++ b/rs/src/shape.rs @@ -27,28 +27,47 @@ pub struct ClipAction { pub actions: Vec, } -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct ClipEventFlags { - pub key_up: bool, - pub key_down: bool, - pub mouse_up: bool, - pub mouse_down: bool, - pub mouse_move: bool, - pub unload: bool, - pub enter_frame: bool, - pub load: bool, - pub drag_over: bool, - pub roll_out: bool, - pub roll_over: bool, - pub release_outside: bool, - pub release: bool, - pub press: bool, - pub initialize: bool, - pub data: bool, - pub construct: bool, - pub key_press: bool, - pub drag_out: bool, +serde_bitflags! { + pub struct ClipEventFlags: u32 { + #[serde(name = "key_up")] + const KEY_UP = 1 << 7; + #[serde(name = "key_down")] + const KEY_DOWN = 1 << 6; + #[serde(name = "mouse_up")] + const MOUSE_UP = 1 << 5; + #[serde(name = "mouse_down")] + const MOUSE_DOWN = 1 << 4; + #[serde(name = "mouse_move")] + const MOUSE_MOVE = 1 << 3; + #[serde(name = "unload")] + const UNLOAD = 1 << 2; + #[serde(name = "enter_frame")] + const ENTER_FRAME = 1 << 1; + #[serde(name = "load")] + const LOAD = 1 << 0; + #[serde(name = "drag_over")] + const DRAG_OVER = 1 << 15; + #[serde(name = "roll_out")] + const ROLL_OUT = 1 << 14; + #[serde(name = "roll_over")] + const ROLL_OVER = 1 << 13; + #[serde(name = "release_outside")] + const RELEASE_OUTSIDE = 1 << 12; + #[serde(name = "release")] + const RELEASE = 1 << 11; + #[serde(name = "press")] + const PRESS = 1 << 10; + #[serde(name = "initialize")] + const INITIALIZE = 1 << 9; + #[serde(name = "data")] + const DATA = 1 << 8; + #[serde(name = "construct")] + const CONSTRUCT = 1 << 18; + #[serde(name = "key_press")] + const KEY_PRESS = 1 << 17; + #[serde(name = "drag_out")] + const DRAG_OUT = 1 << 16; + } } #[cfg_attr( @@ -98,13 +117,24 @@ pub struct LineStyle { pub start_cap: CapStyle, pub end_cap: CapStyle, pub join: JoinStyle, - pub no_h_scale: bool, - pub no_v_scale: bool, - pub no_close: bool, - pub pixel_hinting: bool, + #[serde(flatten)] + pub flags: StyleFlags, pub fill: FillStyle, } +serdcfg_attr(feature = "serde", serde(flatten)) + pub struct StyleFlags: u16 { + #[serde(name = "no_h_scale")] + const NO_H_SCALE = 1 << 2; + #[serde(name = "no_v_scale")] + const NO_V_SCALE = 1 << 1; + #[serde(name = "no_close")] + const NO_CLOSE = 1 << 10; + #[serde(name = "pixel_hinting")] + const PIXEL_HINTING = 1 << 0; + } +} + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct MorphLineStyle { @@ -113,14 +143,12 @@ pub struct MorphLineStyle { pub start_cap: CapStyle, pub end_cap: CapStyle, pub join: JoinStyle, - pub no_h_scale: bool, - pub no_v_scale: bool, - pub no_close: bool, - pub pixel_hinting: bool, + #[serde(flatten)] + pub flags: StyleFlags, pub fill: MorphFillStyle, } -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfcfg_attr(feature = "serde", serde(flatten)) = "serde", derive(Serialize, Deserialize))] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Glyph { pub records: Vec, diff --git a/rs/src/tags.rs b/rs/src/tags.rs index 2e8bf0e..d4b8208 100644 --- a/rs/src/tags.rs +++ b/rs/src/tags.rs @@ -119,16 +119,8 @@ pub struct DefineCffFont { pub struct DefineDynamicText { pub id: u16, pub bounds: Rect, - pub word_wrap: bool, - pub multiline: bool, - pub password: bool, - pub readonly: bool, - pub auto_size: bool, - pub no_select: bool, - pub border: bool, - pub was_static: bool, - pub html: bool, - pub use_glyph_font: bool, + #[cfg_attr(feature = "serde", serde(flatten))] + pub flags: DynamicTextFlags, #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))] pub font_id: Option, #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))] @@ -150,16 +142,38 @@ pub struct DefineDynamicText { pub text: Option, } +serde_bitflags! { + pub struct DynamicTextFlags: u16 { + #[serde(name = "word_wrap")] + const WORD_WRAP = 1 << 6; + #[serde(name = "multiline")] + const MULTILINE = 1 << 5; + #[serde(name = "password")] + const PASSWORD = 1 << 4; + #[serde(name = "readonly")] + const READONLY = 1 << 3; + #[serde(name = "auto_size")] + const AUTO_SIZE = 1 << 14; + #[serde(name = "no_select")] + const NO_SELECT = 1 << 12; + #[serde(name = "border")] + const BORDER = 1 << 11; + #[serde(name = "was_static")] + const WAS_STATIC = 1 << 10; + #[serde(name = "html")] + const HTML = 1 << 9; + #[serde(name = "use_glyph_font")] + const USE_GLYPH_FONT = 1 << 8; + } +} + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct DefineFont { pub id: u16, pub font_name: String, - pub is_bold: bool, - pub is_italic: bool, - pub is_ansi: bool, - pub is_small: bool, - pub is_shift_jis: bool, + #[cfg_attr(feature = "serde", serde(flatten))] + pub flags: FontFlags, pub em_square_size: EmSquareSize, pub language: LanguageCode, #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))] @@ -170,6 +184,21 @@ pub struct DefineFont { pub layout: Option, } +serde_bitflags! { + pub struct FontFlags: u8 { + #[serde(name = "is_bold")] + const BOLD = 1 << 1; + #[serde(name = "is_italic")] + const ITALIC = 1 << 2; + #[serde(name = "is_ansi")] + const ANSI = 1 << 3; + #[serde(name = "is_small")] + const SMALL = 1 << 4; + #[serde(name = "is_shift_jis")] + const SHIFT_JIS = 1 << 5; + } +} + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Clone, Debug, PartialEq, Eq)] pub struct DefineFontAlignZones { @@ -183,11 +212,8 @@ pub struct DefineFontAlignZones { pub struct DefineFontInfo { pub font_id: u16, pub font_name: String, - pub is_bold: bool, - pub is_italic: bool, - pub is_ansi: bool, - pub is_small: bool, - pub is_shift_jis: bool, + #[cfg_attr(feature = "serde", serde(flatten))] + pub flags: FontFlags, pub language: LanguageCode, pub code_units: Vec, } @@ -350,16 +376,23 @@ pub struct ExportAssets { pub assets: Vec, } -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct FileAttributes { - pub use_network: bool, - pub use_relative_urls: bool, - pub no_cross_domain_caching: bool, - pub use_as3: bool, - pub has_metadata: bool, - pub use_gpu: bool, - pub use_direct_blit: bool, +serde_bitflags! { + pub struct FileAttributes: u8 { + #[serde(name = "use_network")] + const USE_NETWORK = 1 << 0; + #[serde(name = "use_relative_urls")] + const USE_RELATIVE_URLS = 1 << 1; + #[serde(name = "no_cross_domain_caching")] + const NO_CROSS_DOMAIN_CACHING = 1 << 2; + #[serde(name = "use_as3")] + const USE_AS3 = 1 << 3; + #[serde(name = "has_metadata")] + const HAS_METADATA = 1 << 4; + #[serde(name = "use_gpu")] + const USE_GPU = 1 << 5; + #[serde(name = "use_direct_blit")] + const USE_DIRECT_BLIT = 1 << 6; + } } #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] From bbbe982cd3e687f5d5570c1e1577eea12fed7158 Mon Sep 17 00:00:00 2001 From: Moulins Date: Sun, 8 May 2022 01:51:00 +0200 Subject: [PATCH 3/3] [rs] Box big variants in enums to reduce their size --- CHANGELOG.md | 2 ++ rs/Cargo.lock | 7 +++++++ rs/Cargo.toml | 1 + rs/src/lib.rs | 19 +++++++++++-------- rs/src/shape.rs | 29 +++++++++++++++++++---------- 5 files changed, 40 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d736a1..6bdb3c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ - **[Breaking change]** Use bitflags instead of separate `bool` fields in `Bevel`, `ButtonCond`, `ButtonRecord`, `ClipEventFlags`, `DefineDynamicText`, `DefineFont`, `DefineFontInfo`, `FileAttributes`, `Glow`, `GradientBevel`, `GradientGlow`, `LineStyle` and `MorphLineStyle`. +- **[Breaking change]** Box big variants to reduce size of enums `FillStyle`, `MorphFillStyle`, `MorphShapeRecord`, + `ShapeRecord` and `Tag`. # 0.14.0 (2022-05-07) diff --git a/rs/Cargo.lock b/rs/Cargo.lock index 1b55faa..5789697 100644 --- a/rs/Cargo.lock +++ b/rs/Cargo.lock @@ -117,6 +117,12 @@ dependencies = [ "serde_json", ] +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "swf-fixed" version = "0.1.5" @@ -134,6 +140,7 @@ dependencies = [ "hex", "serde", "serde_json_v8", + "static_assertions", "swf-fixed", "test-generator", ] diff --git a/rs/Cargo.toml b/rs/Cargo.toml index c46dc8b..d275fc6 100644 --- a/rs/Cargo.toml +++ b/rs/Cargo.toml @@ -23,6 +23,7 @@ path = "src/lib.rs" bitflags = "1.3.2" hex = { version = "0.4.3", optional = true } serde = { version = "1.0.137", optional = true, features = ["derive"] } +static_assertions = "1.1.0" swf-fixed = "0.1.5" [features] diff --git a/rs/src/lib.rs b/rs/src/lib.rs index 583de8c..81cad43 100644 --- a/rs/src/lib.rs +++ b/rs/src/lib.rs @@ -1,5 +1,6 @@ #[cfg(feature = "serde")] use ::serde::{Deserialize, Serialize}; +use static_assertions::const_assert; pub use ::swf_fixed as fixed; pub use crate::basic_types::ColorTransform; @@ -141,22 +142,22 @@ pub enum Tag { DefineBitmap(tags::DefineBitmap), DefineButton(tags::DefineButton), DefineButtonColorTransform(tags::DefineButtonColorTransform), - DefineButtonSound(tags::DefineButtonSound), + DefineButtonSound(Box), DefineCffFont(tags::DefineCffFont), - DefineDynamicText(tags::DefineDynamicText), - DefineFont(tags::DefineFont), + DefineDynamicText(Box), + DefineFont(Box), DefineFontAlignZones(tags::DefineFontAlignZones), DefineFontInfo(tags::DefineFontInfo), DefineFontName(tags::DefineFontName), DefineGlyphFont(tags::DefineGlyphFont), DefineJpegTables(tags::DefineJpegTables), - DefineMorphShape(tags::DefineMorphShape), + DefineMorphShape(Box), DefineScalingGrid(tags::DefineScalingGrid), DefineSceneAndFrameLabelData(tags::DefineSceneAndFrameLabelData), - DefineShape(tags::DefineShape), + DefineShape(Box), DefineSound(tags::DefineSound), DefineSprite(tags::DefineSprite), - DefineText(tags::DefineText), + DefineText(Box), DefineVideoStream(tags::DefineVideoStream), EnablePostscript, DoAbc(tags::DoAbc), @@ -168,7 +169,7 @@ pub enum Tag { FrameLabel(tags::FrameLabel), ImportAssets(tags::ImportAssets), Metadata(tags::Metadata), - PlaceObject(tags::PlaceObject), + PlaceObject(Box), Protect(tags::Protect), Raw(tags::Raw), RawBody(tags::RawBody), @@ -180,12 +181,14 @@ pub enum Tag { SoundStreamBlock(tags::SoundStreamBlock), SoundStreamHead(tags::SoundStreamHead), StartSound(tags::StartSound), - StartSound2(tags::StartSound2), + StartSound2(Box), SymbolClass(tags::SymbolClass), Telemetry(tags::Telemetry), VideoFrame(tags::VideoFrame), } +const_assert!(std::mem::size_of::() <= 64); + #[cfg(all(test, feature = "serde"))] mod tests { use std::path::Path; diff --git a/rs/src/shape.rs b/rs/src/shape.rs index 8143813..7a98f53 100644 --- a/rs/src/shape.rs +++ b/rs/src/shape.rs @@ -1,5 +1,6 @@ #[cfg(feature = "serde")] use ::serde::{Deserialize, Serialize}; +use static_assertions::const_assert; use crate::fill_styles; use crate::join_styles; @@ -77,13 +78,15 @@ serde_bitflags! { )] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum FillStyle { - Bitmap(fill_styles::Bitmap), - FocalGradient(fill_styles::FocalGradient), - LinearGradient(fill_styles::LinearGradient), - RadialGradient(fill_styles::RadialGradient), + Bitmap(Box), + FocalGradient(Box), + LinearGradient(Box), + RadialGradient(Box), Solid(fill_styles::Solid), } +const_assert!(std::mem::size_of::() <= 16); + #[cfg_attr( feature = "serde", derive(Serialize, Deserialize), @@ -91,13 +94,15 @@ pub enum FillStyle { )] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum MorphFillStyle { - Bitmap(fill_styles::MorphBitmap), - FocalGradient(fill_styles::MorphFocalGradient), - LinearGradient(fill_styles::MorphLinearGradient), - RadialGradient(fill_styles::MorphRadialGradient), + Bitmap(Box), + FocalGradient(Box), + LinearGradient(Box), + RadialGradient(Box), Solid(fill_styles::MorphSolid), } +const_assert!(std::mem::size_of::() <= 16); + #[cfg_attr( feature = "serde", derive(Serialize, Deserialize), @@ -176,9 +181,11 @@ pub struct MorphShape { #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum ShapeRecord { Edge(shape_records::Edge), - StyleChange(shape_records::StyleChange), + StyleChange(Box), } +const_assert!(std::mem::size_of::() <= 24); + #[cfg_attr( feature = "serde", derive(Serialize, Deserialize), @@ -187,9 +194,11 @@ pub enum ShapeRecord { #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum MorphShapeRecord { Edge(shape_records::MorphEdge), - StyleChange(shape_records::MorphStyleChange), + StyleChange(Box), } +const_assert!(std::mem::size_of::() <= 48); + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ShapeStyles {