diff --git a/crates/metafile/src/import/picture.rs b/crates/metafile/src/import/picture.rs index 19db6158c5..a32c37ff84 100644 --- a/crates/metafile/src/import/picture.rs +++ b/crates/metafile/src/import/picture.rs @@ -4,7 +4,8 @@ //! - A filled or stroked path becomes a path with a fill and a stroke (caps, joins, miter limit //! and dashes from the pen). Opaque solid strokes with round caps and round joins drawn one after //! another, each starting where the last one ended, with the same pen and clip, join into one -//! path. Other strokes stay apart, each with its own caps and its own dashes. +//! path. Other strokes stay apart, each with its own caps and its own dashes. A path that is +//! only filled comes in with every figure closed. //! - A path filled with a pattern or hatched brush is filled with the average color of the //! brush's tile. //! - A bitmap becomes an image (its pixels as PNG) placed where the picture draws it, turned or @@ -309,11 +310,14 @@ impl Mapper { st } - /// A path node painted with `fill` and `stroke`; None when neither shows. + /// A path node painted with `fill` and `stroke`; None when neither shows. A path that is only + /// filled has every figure closed, as a fill closes it: a figure a file fills without closing + /// it (FILLPATH without CLOSEFIGURE) comes in as a closed path. fn path_node(&self, bp: BezPath, fill: Option<[u8; 4]>, stroke: Option<([u8; 4], f32, &LineStyle)>, even_odd: bool) -> Option { if bp.elements().is_empty() { return None; } + let bp = if stroke.is_none() { closed_figures(&bp) } else { bp }; let mut items = vec![]; if let Some(c) = fill.filter(|c| c[3] > 0) { let mut f = FillLayer::new(color(c)); @@ -793,6 +797,29 @@ impl Mapper { } } +/// `bp` with every figure that has a segment and no close closed. +fn closed_figures(bp: &BezPath) -> BezPath { + let mut out = BezPath::new(); + let mut open = false; + for el in bp.elements() { + match el { + PathEl::MoveTo(_) => { + if open { + out.close_path(); + } + open = false; + } + PathEl::ClosePath => open = false, + PathEl::LineTo(_) | PathEl::QuadTo(..) | PathEl::CurveTo(..) => open = true, + } + out.push(*el); + } + if open { + out.close_path(); + } + out +} + /// Where a line `lead + span` long (along the baseline) starts from the reference point, as /// `align` puts it, and `lead` on from there: where its first character starts. fn aligned(lead: f64, span: f64, align: &HAlign) -> f64 { diff --git a/crates/metafile/src/tests/import.rs b/crates/metafile/src/tests/import.rs index 42a66bc8d5..91121395cf 100644 --- a/crates/metafile/src/tests/import.rs +++ b/crates/metafile/src/tests/import.rs @@ -1560,3 +1560,51 @@ fn emf_without_a_frame_is_read_at_its_device_resolution() { let p = paths(&back.document); assert!(near(p[0].geometric_bounds().unwrap(), Rect::new(10.0 * mm, 5.0 * mm, 20.0 * mm, 7.5 * mm), 0.01), "{:?}", p[0].geometric_bounds()); } + +/// Whether each subpath of each path in `bytes` is closed, in order. +fn closed_subpaths(bytes: &[u8]) -> Vec> { + paths(&import(bytes).unwrap().document) + .iter() + .map(|n| match &n.kind { + NodeKind::Path { path, .. } => path.subpaths.iter().map(|s| s.closed).collect(), + _ => panic!("a path"), + }) + .collect() +} + +#[test] +fn filled_ellipses_and_filled_path_figures_come_in_closed() { + // A green brush in slot 1, selected; a null pen. + let brush = vec![(39, le(&[1, 0, 0x00_ff_00, 0])), (37, le(&[1])), (37, le(&[0x8000_0008u32 as i32]))]; + // EMR_ELLIPSE. + let ellipse = [brush.clone(), vec![(42, le(&[1000, 1000, 2000, 1500]))]].concat(); + assert_eq!(closed_subpaths(&hand_emf(5000, 3000, &ellipse)), vec![vec![true]]); + // EmfPlusFillEllipse with an inline green color, in a dual file. + let header = le(&[0xDBC0_1002u32 as i32, 0, 96, 96]); + let fill = [le(&[0xFF00_FF00u32 as i32]), le_f32(&[1000.0, 1000.0, 1000.0, 500.0])].concat(); + let comment = emf_plus(&[(0x4001, 1, header), (0x400E, 0x8000, fill), (0x4002, 0, vec![])]); + assert_eq!(closed_subpaths(&hand_emf(5000, 3000, &[(70, comment)])), vec![vec![true]]); + // BEGINPATH, two figures of MOVETOEX and POLYBEZIERTO16 (half an ellipse each) without + // CLOSEFIGURE, ENDPATH, then `paint`. + let bracket = |paint: u32, pen: bool| { + let mut r = brush.clone(); + if pen { + r.push((37, le(&[0x8000_0007u32 as i32]))); + } + r.push((59, vec![])); + for x in [1000, 3000] { + let half: [i16; 12] = [x + 1000, 1500, x + 200, 1500, x, 1400, x, 1250, x, 1100, x + 200, 1000]; + r.push((27, le(&[i32::from(x) + 1000, 1250]))); + let mut body = le(&[0, 0, 0, 0, 6]); + body.extend(half.iter().flat_map(|v| v.to_le_bytes())); + r.push((88, body)); + } + r.push((60, vec![])); + r.push((paint, le(&[0, 0, 0, 0]))); + closed_subpaths(&hand_emf(5000, 3000, &r)) + }; + // FILLPATH: a fill closes every open figure. + assert_eq!(bracket(62, false), vec![vec![true, true]]); + // STROKEPATH: a stroke leaves them open. + assert_eq!(bracket(64, true), vec![vec![false, false]]); +}