craft-metafile: WMF scale and offset mapping records

META_SCALEVIEWPORTEXT, META_SCALEWINDOWEXT, META_OFFSETVIEWPORTORG and
META_OFFSETWINDOWORG were skipped, so a WMF that draws sub-pictures into
parts of its frame (each halving the viewport between SaveDC and RestoreDC)
stretched the first one over the whole frame and drew the next outside it
(storytold/deckcraft#2069).
This commit is contained in:
Hamza
2026-11-15 23:50:11 -05:00
committed by Dylan M. Taylor
parent 5c91598ccc
commit aaa7230822
4 changed files with 92 additions and 4 deletions
+2 -2
View File
@@ -53,14 +53,14 @@ decoder is where the size, pixel and work limits have to be enforced.
| Group | Records |
|---|---|
| Headers | Placeable header, memory and disk metafile headers. |
| Mapping | `SETMAPMODE`, `SETWINDOWORG`, `SETWINDOWEXT`, `SETVIEWPORTORG`, `SETVIEWPORTEXT`, all map modes (text, metric, isotropic, anisotropic). A placeable file is played into its box the way Office plays it. |
| Mapping | `SETMAPMODE`, `SETWINDOWORG`, `SETWINDOWEXT`, `SETVIEWPORTORG`, `SETVIEWPORTEXT`, `OFFSETWINDOWORG`, `OFFSETVIEWPORTORG`, `SCALEWINDOWEXT`, `SCALEVIEWPORTEXT`, all map modes (text, metric, isotropic, anisotropic). A placeable file is played into its box the way Office plays it. |
| State | `SAVEDC`, `RESTOREDC`, `SETPOLYFILLMODE`, `SETROP2`, `SETBKMODE`, `SETBKCOLOR`, `SETTEXTCOLOR`, `SETTEXTALIGN`. |
| Objects | `CREATEPENINDIRECT` (style, cap and join bits), `CREATEBRUSHINDIRECT` (solid, null, hatched), `DIBCREATEPATTERNBRUSH`, `CREATEFONTINDIRECT`, `SELECTOBJECT`, `DELETEOBJECT`, stock objects. `CREATEPALETTE`, `CREATEPATTERNBRUSH` and `CREATEREGION` take their handle slot. |
| Drawing | `MOVETO`, `LINETO`, `POLYLINE`, `POLYGON`, `POLYPOLYGON`, `RECTANGLE`, `ROUNDRECT`, `ELLIPSE`, `ARC`, `PIE`, `CHORD`, `PATBLT`. |
| Bitmaps | `STRETCHDIB`, `DIBSTRETCHBLT`, `DIBBITBLT` (with or without a source). |
| Text | `EXTTEXTOUT` (rectangle, opaque, clipped, dx), `TEXTOUT`, decoded in the selected font's character set: code pages 1250 to 1258 and 874, Shift_JIS, UHC, GBK and Big5 (a double-byte character takes the advances of both its bytes), and the symbol character set (also the Symbol font under `DEFAULT_CHARSET`), whose codes stay in the private-use area at U+F000. Face names are read in the font's code page. Johab pairs are blanks. |
| Clipping | `INTERSECTCLIPRECT`, `EXCLUDECLIPRECT`. |
| Not read | `OFFSETWINDOWORG`, `OFFSETVIEWPORTORG`, `SCALEWINDOWEXT`, `SCALEVIEWPORTEXT`, flood fills, `SETPIXEL`, region records (`FILLREGION`, `FRAMEREGION`, `PAINTREGION`, `INVERTREGION`, `SELECTCLIPREGION`, `OFFSETCLIPRGN`), the device-dependent `BITBLT`, `STRETCHBLT` and `SETDIBTODEV`. They are counted in `skipped_records`. |
| Not read | Flood fills, `SETPIXEL`, region records (`FILLREGION`, `FRAMEREGION`, `PAINTREGION`, `INVERTREGION`, `SELECTCLIPREGION`, `OFFSETCLIPRGN`), the device-dependent `BITBLT`, `STRETCHBLT` and `SETDIBTODEV`. They are counted in `skipped_records`. |
### EMF (MS-EMF)
+8 -2
View File
@@ -822,8 +822,14 @@ impl Canvas {
if self.arc_clockwise { (e, s) } else { (s, e) }
}
/// SCALEVIEWPORTEXTEX and SCALEWINDOWEXTEX: the extent times `num / den` on each axis. A zero
/// denominator leaves the extent alone.
/// OFFSETWINDOWORG and OFFSETVIEWPORTORG (WMF): the window or viewport origin moved by `d`.
pub fn offset_org(&mut self, window: bool, d: P) {
let o = if window { &mut self.map.wo } else { &mut self.map.vo };
*o = (o.0 + d.0, o.1 + d.1);
}
/// SCALEVIEWPORTEXTEX and SCALEWINDOWEXTEX (and WMF's SCALEVIEWPORTEXT and SCALEWINDOWEXT): the
/// extent times `num / den` on each axis. A zero denominator leaves the extent alone.
pub fn scale_ext(&mut self, window: bool, x: (f64, f64), y: (f64, f64)) {
if x.1 == 0.0 || y.1 == 0.0 {
return;
+73
View File
@@ -2654,3 +2654,76 @@ fn canvas_budget_counts_bytes_held_elsewhere() {
assert!(cv.full());
assert!(cv.items.is_empty());
}
/// The box through a path's segment ends (an ellipse's four cubics end on its box).
fn end_extent(item: &Item) -> (f32, f32, f32, f32) {
let (segs, ..) = path_segs(item);
let (xs, ys): (Vec<f32>, Vec<f32>) = segs
.iter()
.filter_map(|s| match s {
Seg::Move(x, y) | Seg::Line(x, y) | Seg::Cubic(.., x, y) => Some((*x, *y)),
Seg::Close => None,
})
.unzip();
let lo = |v: &[f32]| v.iter().copied().fold(f32::MAX, f32::min);
let hi = |v: &[f32]| v.iter().copied().fold(f32::MIN, f32::max);
(lo(&xs), lo(&ys), hi(&xs), hi(&ys))
}
/// Two sub-pictures drawn into halves of the frame, as charts pasted from older Office versions
/// are stored (deckcraft#2069): each saves the DC, halves the viewport with SCALEVIEWPORTEXT, sets
/// its own 400 x 400 window (the second shifted 400 to the left, so it lands in the right half),
/// draws a circle and restores the DC. The left circle stays in the left half and the right one
/// lands in the right half of the 800 x 400 frame.
#[test]
fn wmf_scale_viewport_ext_draws_sub_pictures_into_parts_of_the_frame() {
let sub = |org_x: i32| {
vec![
wrec(0x001E, &[]),
wrec(0x0103, &h16(8)),
// yDenom, yNum, xDenom, xNum: x * 1/2, y * 1/1.
wrec(0x0412, &cat(vec![h16(1), h16(1), h16(2), h16(1)])),
wrec(0x020B, &cat(vec![h16(0), h16(org_x)])),
wrec(0x020C, &cat(vec![h16(400), h16(400)])),
wrec(0x0418, &cat(vec![h16(380), h16(380), h16(20), h16(20)])),
wrec(0x0127, &h16(-1)),
]
};
let mut recs = vec![wrec(0x020B, &cat(vec![h16(0), h16(0)])), wrec(0x020C, &cat(vec![h16(400), h16(800)]))];
recs.extend(sub(0));
recs.extend(sub(-400));
let p = parse(&wmf(&recs, None)).unwrap();
assert_eq!((p.width, p.height), (800.0, 400.0));
assert_eq!((p.items.len(), p.skipped_records), (2, 0), "both circles, and every record played");
for (item, want) in p.items.iter().zip([(20.0, 20.0, 380.0, 380.0), (420.0, 20.0, 780.0, 380.0)]) {
let got = end_extent(item);
assert!(close(got.0, want.0) && close(got.1, want.1) && close(got.2, want.2) && close(got.3, want.3), "{got:?}, want {want:?}");
}
}
/// SCALEWINDOWEXT scales the window extent, and OFFSETWINDOWORG and OFFSETVIEWPORTORG move the
/// window and viewport origins by their offsets, on top of what the SET records gave them. The
/// 100 x 100 window of a plain WMF is the frame, and the records after it place a rectangle in it.
#[test]
fn wmf_scale_window_ext_and_offset_origins_change_the_mapping() {
let rect = || wrec(0x041B, &cat(vec![h16(20), h16(20), h16(10), h16(10)]));
let base = || vec![wrec(0x0103, &h16(8)), wrec(0x020B, &cat(vec![h16(0), h16(0)])), wrec(0x020C, &cat(vec![h16(100), h16(100)]))];
let extent = |extra: Vec<Vec<u8>>| {
let mut recs = base();
recs.extend(extra);
recs.push(rect());
rect_extent(&parse(&wmf(&recs, None)).unwrap().items[0])
};
// The window doubled (x * 2/1, y * 4/2): the rectangle shows at half size.
let got = extent(vec![wrec(0x0410, &cat(vec![h16(2), h16(4), h16(1), h16(2)]))]);
assert!(close(got.0, 5.0) && close(got.1, 5.0) && close(got.2, 10.0) && close(got.3, 10.0), "scaled window: {got:?}");
// The window origin moved by (5, 10): the rectangle moves the other way.
let got = extent(vec![wrec(0x020F, &cat(vec![h16(10), h16(5)]))]);
assert!(close(got.0, 5.0) && close(got.1, 0.0) && close(got.2, 15.0) && close(got.3, 10.0), "window origin: {got:?}");
// The viewport origin moved by (30, -5): the rectangle moves with it.
let got = extent(vec![wrec(0x0211, &cat(vec![h16(-5), h16(30)]))]);
assert!(close(got.0, 40.0) && close(got.1, 5.0) && close(got.2, 50.0) && close(got.3, 15.0), "viewport origin: {got:?}");
// A zero denominator leaves the extent alone.
let got = extent(vec![wrec(0x0412, &cat(vec![h16(0), h16(1), h16(0), h16(1)]))]);
assert!(close(got.0, 10.0) && close(got.1, 10.0) && close(got.2, 20.0) && close(got.3, 20.0), "zero denominator: {got:?}");
}
+9
View File
@@ -105,6 +105,15 @@ fn record(cv: &mut Canvas, func: u16, r: Bytes) {
0x020C => xy16(r, 6).map(|p| cv.set_window_ext(p)),
0x020D => xy16(r, 6).map(|p| cv.set_viewport_org(p)),
0x020E => xy16(r, 6).map(|p| cv.set_viewport_ext(p)),
// OFFSETWINDOWORG and OFFSETVIEWPORTORG: yOffset, xOffset, added to the origin.
0x020F | 0x0211 => xy16(r, 6).map(|d| cv.offset_org(func == 0x020F, d)),
// SCALEWINDOWEXT and SCALEVIEWPORTEXT: yDenom, yNum, xDenom, xNum ([MS-WMF] 2.3.5). A
// sub-picture drawn into part of the frame halves the viewport this way (deckcraft#2069).
0x0410 | 0x0412 => (|| {
let [y_den, y_num, x_den, x_num] = [r.i16(6)?, r.i16(8)?, r.i16(10)?, r.i16(12)?].map(f64::from);
cv.scale_ext(func == 0x0410, (x_num, x_den), (y_num, y_den));
Some(())
})(),
0x001E => {
cv.save_dc();
Some(())