diff --git a/Cargo.lock b/Cargo.lock index e495bad15e..90cef51faf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1251,7 +1251,7 @@ dependencies = [ [[package]] name = "craft-metafile" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=d7c9632bfb3040cf45db394e7500cc93c81c49e0#d7c9632bfb3040cf45db394e7500cc93c81c49e0" +source = "git+https://github.com/storytold/craft-libs?rev=c56fbd8271a42ffcc82b07c8f31d714e345fdcd9#c56fbd8271a42ffcc82b07c8f31d714e345fdcd9" dependencies = [ "encoding_rs", "image", diff --git a/Cargo.toml b/Cargo.toml index 4bbd7d964d..01cddf5b45 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,7 @@ craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "068e079c7b craft-plotter = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } craft-dxf = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } # WMF, EMF and EMF+ reader (craft-libs#778). -craft-metafile = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } +craft-metafile = { git = "https://github.com/storytold/craft-libs", rev = "c56fbd8271a42ffcc82b07c8f31d714e345fdcd9" } craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "ba25ccd233934d1b2f74e3c0f235168bf5f13ef3" } craft-gif = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } diff --git a/crates/metafile/src/import/picture.rs b/crates/metafile/src/import/picture.rs index dab2111119..0b72246b54 100644 --- a/crates/metafile/src/import/picture.rs +++ b/crates/metafile/src/import/picture.rs @@ -1164,46 +1164,6 @@ fn retracked(t: &TextObject, laid: &vectorcraft_text::TextLayout, dx: &[f64]) -> Some(out) } -/// Does the file stop before its end-of-file record? -fn ends_early(bytes: &[u8], kind: Kind) -> bool { - let u32_at = |at: usize| bytes.get(at..at.checked_add(4)?).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]])); - let u16_at = |at: usize| bytes.get(at..at.checked_add(2)?).map(|b| u16::from_le_bytes([b[0], b[1]])); - let mut at = match kind { - Kind::Emf => 0, - Kind::Wmf => { - let start = if u32_at(0) == Some(PLACEABLE_KEY) { 22 } else { 0 }; - let Some(words) = u16_at(start + 2) else { return true }; - start + usize::from(words) * 2 - } - }; - loop { - let size = match kind { - Kind::Emf => { - let (Some(kind), Some(size)) = (u32_at(at), u32_at(at.saturating_add(4))) else { return true }; - if kind == 14 { - return false; - } - // Records are a multiple of 4 bytes long; reading stops at one that isn't. - if !size.is_multiple_of(4) { - return true; - } - size as usize - } - Kind::Wmf => { - let (Some(words), Some(function)) = (u32_at(at), u16_at(at.saturating_add(4))) else { return true }; - if function == 0 { - return false; - } - (words as usize).saturating_mul(2) - } - }; - if size < 6 { - return true; - } - at = at.saturating_add(size); - } -} - /// Read an EMF or WMF file. pub(crate) fn import(bytes: &[u8]) -> Result { let kind = crate::sniff(bytes).ok_or("not an EMF or WMF file")?; @@ -1214,7 +1174,11 @@ pub(crate) fn import(bytes: &[u8]) -> Result { })?; let k = unit(bytes, kind); let mut m = Mapper::new(f64::from(pic.width), f64::from(pic.height), k); - if ends_early(bytes, kind) { + let wire_kind = match kind { + Kind::Emf => craft_metafile::Kind::Emf, + Kind::Wmf => craft_metafile::Kind::Wmf, + }; + if craft_metafile::ends_early(bytes, wire_kind) { m.warn(ENDS_EARLY); } if pic.truncated {