metafile: address CodeRabbit feedback on truncation warnings, patterns, and region shapes

This commit is contained in:
ruchirguitar
2026-11-23 00:00:42 +05:30
parent e3c874b13f
commit 946f33c878
4 changed files with 14 additions and 7 deletions
Generated
+1 -1
View File
@@ -1251,7 +1251,7 @@ dependencies = [
[[package]]
name = "craft-metafile"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=0bf4cb99dabb6b6d9335771ed398e999387b0d62#0bf4cb99dabb6b6d9335771ed398e999387b0d62"
source = "git+https://github.com/storytold/craft-libs?rev=ccd093d2316c1445ab3bdeecbecfacaaae4093d4#ccd093d2316c1445ab3bdeecbecfacaaae4093d4"
dependencies = [
"encoding_rs",
"image",
+1 -1
View File
@@ -39,7 +39,7 @@ craft-plotter = { git = "https://github.com/storytold/craft-libs", rev = "d7c963
craft-dxf = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" }
# WMF, EMF and EMF+ reader (craft-libs#778).
# WMF, EMF and EMF+ reader (craft-libs#778).
craft-metafile = { git = "https://github.com/storytold/craft-libs", rev = "0bf4cb99dabb6b6d9335771ed398e999387b0d62" }
craft-metafile = { git = "https://github.com/storytold/craft-libs", rev = "ccd093d2316c1445ab3bdeecbecfacaaae4093d4" }
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" }
+8 -2
View File
@@ -51,6 +51,7 @@ const PATTERNS: &str = "hatched and pattern brushes are filled with their averag
const ENDS_EARLY: &str = "the file ends early: what it held was imported";
const TOO_MANY: &str = "objects past the most one file may hold were left out";
const AT_LIMIT: &str = "the file or a picture inside it reached a limit on what one file may hold: what came after the limit was not read";
const PICT_TRUNCATED: &str = "the picture is cut short or reached a limit on what one file may hold: what came after was not read";
/// Points a unit of a WMF without a placeable header (1440 units an inch).
const WMF_UNIT: f64 = 72.0 / 1440.0;
@@ -272,7 +273,12 @@ fn average(rgba: &[u8]) -> Option<[u8; 4]> {
}
let a = sum[3];
let alpha = (a / pixels.len().max(1) as u64) as u8;
(a > 0).then(|| [(sum[0] / a) as u8, (sum[1] / a) as u8, (sum[2] / a) as u8, alpha.max(1)])
(a > 0).then(|| [
((sum[0] + a / 2) / a) as u8,
((sum[1] + a / 2) / a) as u8,
((sum[2] + a / 2) / a) as u8,
alpha.max(1),
])
}
impl Mapper {
@@ -1229,7 +1235,7 @@ pub(crate) fn import(bytes: &[u8]) -> Result<Imported, String> {
pub(crate) fn convert(pic: craft_metafile::Picture) -> crate::Picture {
let mut m = Mapper::new(f64::from(pic.width), f64::from(pic.height), (1.0, 1.0));
if pic.truncated {
m.warn(AT_LIMIT);
m.warn(PICT_TRUNCATED);
}
let lost = Lost { skipped: pic.skipped_records, images: pic.dropped_images, clips: pic.ignored_clips, rops: pic.unplayed_raster_ops };
for item in pic.items {
+4 -3
View File
@@ -231,6 +231,7 @@ fn patterns_fill_with_their_average_color() {
assert_eq!(fill_rgb(&v[0]), Some([128, 128, 128]));
// eraseRect with an RGB background pattern.
assert_eq!(fill_rgb(&v[1]), Some([0, 255, 0]));
assert!(p.warnings.iter().any(|w| w.contains("their average color")), "{:?}", p.warnings);
}
#[test]
@@ -418,11 +419,11 @@ fn pictures_cut_short_keep_what_came_before() {
let cut = &full[..full.len() - 6];
let p = import_pict(cut).unwrap();
assert_eq!(paths(&p.document).len(), 1);
assert!(p.warnings.iter().any(|w| w.contains("limit on what one file may hold")), "{:?}", p.warnings);
assert!(p.warnings.iter().any(|w| w.contains("cut short")), "{:?}", p.warnings);
// A long comment longer than the file.
let p = import_pict(&v2([0, 0, 50, 50], &[(0x31, rect(0, 0, 10, 10)), (0xA1, be(&[100, 5000]))])).unwrap();
assert_eq!(paths(&p.document).len(), 1);
assert!(p.warnings.iter().any(|w| w.contains("limit on what one file may hold")), "{:?}", p.warnings);
assert!(p.warnings.iter().any(|w| w.contains("cut short")), "{:?}", p.warnings);
// Nothing drawn.
let p = import_pict(&v2([0, 0, 50, 50], &[(0xA0, be(&[100]))])).unwrap();
assert!(p.warnings.iter().any(|w| w.contains("draws nothing")), "{:?}", p.warnings);
@@ -463,7 +464,7 @@ fn regions_past_the_picture_budget_are_left_out() {
let body = [rect(0, 0, 1, 16_000), be(&words)].concat();
let region = [be(&[(body.len() + 2) as i16]), body].concat();
let p = import_pict(&v2([0, 0, 300, 300], &[(0x81, region.clone()), (0x81, region.clone()), (0x81, region)])).unwrap();
assert!(p.warnings.iter().any(|w| w.contains("clip couldn't be applied")), "{:?}", p.warnings);
assert!(p.warnings.iter().any(|w| w.contains("skipped")), "{:?}", p.warnings);
let v = paths(&p.document);
assert_eq!(v.len(), 2);
let NodeKind::Path { path, .. } = &v[0].kind else { panic!("a path") };