mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 18:04:48 +00:00
craft-metafile: count refused bitmaps and lost pen textures, empty line clips
- A bitmap that the item or byte limit refuses is counted in dropped_images once, however many pieces a clip cuts it into. - An EMF+ pen whose texture brush was not read is counted in dropped_images, as a lost texture brush object is. - An EMF+ clip path or region path whose figures all lie on a line encloses no area, so it gives an empty area instead of its bounding rectangle.
This commit is contained in:
@@ -39,7 +39,7 @@ differently:
|
||||
| Field | Reports |
|
||||
|---|---|
|
||||
| `truncated` | The parse stopped at a limit that ends it (items, bytes kept, `SAVEDC` depth, clip paths), in the file or in an EMF+ metafile image drawn in it. |
|
||||
| `dropped_images` | Images that were not read: bitmaps past a size, byte or decode limit or not decodable, EMF+ metafile images that do not parse or are nested too deep, and pattern or texture brushes whose bitmap was not read (counted when the brush is created). An image with no pixels, or drawn from a source rectangle that selects none, is not counted. |
|
||||
| `dropped_images` | Images that were not read: bitmaps past a size, item, byte or decode limit or not decodable, EMF+ metafile images that do not parse or are nested too deep, and pattern or texture brushes whose bitmap was not read, a pen's brush included (counted when the brush or pen is created). An image with no pixels, or drawn from a source rectangle that selects none, is not counted. |
|
||||
| `ignored_clips` | Clip changes not applied as the file asks: `SELECTCLIPPATH` with `RGN_OR`, `RGN_XOR` or `RGN_DIFF`, EMF+ clips combined with XOR, exclude or complement, EMF+ unions drawn as the rectangle around both areas, and EMF+ clips whose area stands in for the one the file gives (a path that is not one convex figure, drawn as its rectangle, or a region built with XOR, exclude or complement). A change that cannot alter the clip is not counted: an area whose figures all lie on a line (except in an EMF+ complement of a clip that encloses some area, which would empty it), `RGN_OR` on a clip that is still the whole surface, and a union whose rectangle is no larger than the union. |
|
||||
| `unplayed_raster_ops` | Raster operations not played: bitmaps drawn as plain copies because their operation is not one of `SRCCOPY`, `SRCAND`, `SRCPAINT` and `SRCINVERT`, and operations without a source (such as `DSTINVERT` or `PATINVERT`) that are left out. |
|
||||
|
||||
|
||||
@@ -1335,7 +1335,12 @@ impl Canvas {
|
||||
let (x0, x1) = (xs.iter().copied().fold(f64::INFINITY, f64::min), xs.iter().copied().fold(f64::NEG_INFINITY, f64::max));
|
||||
let (y0, y1) = (ys.iter().copied().fold(f64::INFINITY, f64::min), ys.iter().copied().fold(f64::NEG_INFINITY, f64::max));
|
||||
let dev = if xf[1] == 0.0 && xf[2] == 0.0 { dev } else { [x0, y0, x1 - x0, y1 - y0] };
|
||||
let was_full = self.full;
|
||||
self.push(Raw::Bitmap { rect: dev, xf, img, region: RawClip::default() });
|
||||
// A bitmap the item or byte limit refuses is counted once, however many pieces a clip cuts it into.
|
||||
if self.full && !was_full {
|
||||
self.drop_image();
|
||||
}
|
||||
}
|
||||
|
||||
/// Draws a bitmap that a record asked to combine with raster operation `rop` (already applied by
|
||||
|
||||
@@ -1181,7 +1181,11 @@ fn pen(r: Bytes) -> Option<Obj> {
|
||||
off += 4 + (r.u32(off)? as usize).min(1 << 20);
|
||||
}
|
||||
}
|
||||
let color = brush(r, off)?.color();
|
||||
// A pen whose texture brush was not read is counted like a lost brush object.
|
||||
let Some(fill) = brush(r, off) else {
|
||||
return lost_texture(r.tail(off)?).then_some(Obj::LostBrush);
|
||||
};
|
||||
let color = fill.color();
|
||||
Some(Obj::Pen { color, width: if width.is_finite() { width.clamp(0.0, 1.0e5) } else { 0.0 } })
|
||||
}
|
||||
|
||||
@@ -1290,16 +1294,20 @@ fn region_node(r: Bytes, off: usize, depth: usize, work: &mut usize) -> Option<(
|
||||
}
|
||||
}
|
||||
|
||||
/// The area a path encloses: its outline when that is one convex figure, else the rectangle
|
||||
/// around it. The flag is set when the rectangle stands in for a path that encloses some area.
|
||||
/// The area a path encloses: its outline when that is one convex figure, none when its figures all
|
||||
/// lie on a line, else the rectangle around it. The flag is set when the rectangle stands in for
|
||||
/// the path.
|
||||
fn area_of_ops(ops: &[Op]) -> (Vec<P>, bool) {
|
||||
if !crate::geom::encloses_area(ops) {
|
||||
return (Vec::new(), false);
|
||||
}
|
||||
let subs = flatten(ops);
|
||||
if let [(pts, _)] = subs.as_slice()
|
||||
&& let Some(c) = convex(pts)
|
||||
{
|
||||
return (c, false);
|
||||
}
|
||||
(ops_bbox(ops).map(rect_poly).unwrap_or_default(), crate::geom::encloses_area(ops))
|
||||
(ops_bbox(ops).map(rect_poly).unwrap_or_default(), true)
|
||||
}
|
||||
|
||||
/// Two areas' intersection, and whether it ran out of work and kept the first area instead.
|
||||
|
||||
@@ -78,10 +78,10 @@ pub struct Picture {
|
||||
/// the picture holds what came before. The limits that cut one path, string, region or dash
|
||||
/// pattern short do not set it.
|
||||
pub truncated: bool,
|
||||
/// Images the file asks to draw that the reader did not read: bitmaps past a size, byte or decode
|
||||
/// limit or in a form the reader cannot decode, EMF+ metafile images that do not parse or are
|
||||
/// nested deeper than 3 levels, and pattern or texture brushes whose bitmap was not read (counted
|
||||
/// when the brush is created). An image whose source rectangle selects none of its pixels, or that
|
||||
/// Images the file asks to draw that the reader did not read: bitmaps past a size, item, byte or
|
||||
/// decode limit or in a form the reader cannot decode, EMF+ metafile images that do not parse or
|
||||
/// are nested deeper than 3 levels, and pattern or texture brushes whose bitmap was not read, a
|
||||
/// pen's brush included (counted when the brush or pen is created). An image whose source rectangle selects none of its pixels, or that
|
||||
/// has no pixels, draws nothing and is not counted. Bitmaps left out while clipping to an EMF+
|
||||
/// clip (see the README's limits) are not counted here.
|
||||
pub dropped_images: u32,
|
||||
|
||||
@@ -486,6 +486,34 @@ fn canvas_stops_at_the_byte_budget() {
|
||||
assert_eq!(cv.items.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_bitmap_the_byte_budget_refuses_is_counted_once() {
|
||||
let img = || Rgba { w: 4, h: 4, px: vec![0u8; 64] };
|
||||
let diamond = || Some(vec![(2.0, 0.0), (4.0, 2.0), (2.0, 4.0), (0.0, 2.0)]);
|
||||
let mut cv = Canvas::new();
|
||||
cv.set_clip(diamond());
|
||||
cv.bitmap([0.0, 0.0, 4.0, 4.0], img());
|
||||
assert!(!cv.full() && !cv.items.is_empty() && cv.dropped_images == 0, "a bitmap that is kept");
|
||||
// The clip cuts the bitmap into pieces, and none of them fits: one image is counted.
|
||||
let mut cv = Canvas::new();
|
||||
cv.hold(MAX_BYTES - 1);
|
||||
cv.set_clip(diamond());
|
||||
cv.bitmap([0.0, 0.0, 4.0, 4.0], img());
|
||||
assert!(cv.full() && cv.items.is_empty());
|
||||
assert_eq!(cv.dropped_images, 1);
|
||||
// A clip that leaves none of the bitmap refuses nothing.
|
||||
let mut cv = Canvas::new();
|
||||
cv.hold(MAX_BYTES - 1);
|
||||
cv.set_clip(Some(vec![(100.0, 100.0), (110.0, 100.0), (110.0, 110.0)]));
|
||||
cv.bitmap([0.0, 0.0, 4.0, 4.0], img());
|
||||
assert!(!cv.full() && cv.dropped_images == 0);
|
||||
// A bitmap drawn with a raster operation is counted the same way.
|
||||
let mut cv = Canvas::new();
|
||||
cv.hold(MAX_BYTES - 1);
|
||||
cv.bitmap_rop([0.0, 0.0, 4.0, 4.0], img(), 0x00CC_0020);
|
||||
assert!(cv.full() && cv.dropped_images == 1 && cv.unplayed_raster_ops == 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oversized_dib_header_size_is_rejected_without_overflow() {
|
||||
let mut info = info_header(2, 2, 24, 16);
|
||||
|
||||
@@ -485,6 +485,17 @@ fn pattern_and_texture_brushes_that_are_not_read_are_counted() {
|
||||
let none = cat(vec![h32(0xDBC0_1002), h32(1), h32(0), h32(4), h32(16), h32(0x0026_200A), h32(0)]);
|
||||
assert_eq!(parse(&texture(none)).unwrap().dropped_images, 0);
|
||||
assert_eq!(parse(&texture(bitmap_2x2())).unwrap().dropped_images, 0);
|
||||
// A pen whose brush is such a texture brush.
|
||||
let pen = |image: Vec<u8>| {
|
||||
let brush = cat(vec![h32(0xDBC0_1002), h32(2), h32(0), h32(0), image]);
|
||||
let pen = cat(vec![h32(0xDBC0_1002), h32(0), h32(0), h32(0), f32s(&[1.0]), brush]);
|
||||
emf(&[plus_comment(&[plus_header(false), prec(0x4008, 0x0200 | 5, &pen)])], [0; 4], [0; 4])
|
||||
};
|
||||
let short = cat(vec![h32(0xDBC0_1002), h32(1), h32(4), h32(4), h32(16), h32(0x0026_200A), h32(0), vec![1, 2, 3]]);
|
||||
assert_eq!(parse(&pen(short)).unwrap().dropped_images, 1);
|
||||
let none = cat(vec![h32(0xDBC0_1002), h32(1), h32(0), h32(4), h32(16), h32(0x0026_200A), h32(0)]);
|
||||
assert_eq!(parse(&pen(none)).unwrap().dropped_images, 0);
|
||||
assert_eq!(parse(&pen(bitmap_2x2())).unwrap().dropped_images, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -643,6 +654,25 @@ fn emfplus_clips_that_are_not_applied_are_counted() {
|
||||
assert_eq!(plus(vec![clip(1, [0.0, 0.0, 50.0, 50.0]), clip(0, [1.0, 1.0, 5.0, 5.0])]), 0, "intersect and replace are applied");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_emfplus_clip_on_a_diagonal_line_encloses_nothing() {
|
||||
let fill = prec(0x400A, 0x8000, &cat(vec![h32(0xFF00_0000), h32(1), f32s(&[0.0, 0.0, 100.0, 100.0])]));
|
||||
let drawn = |clip: Vec<Vec<u8>>| {
|
||||
let mut all = vec![plus_header(false)];
|
||||
all.extend(clip);
|
||||
all.push(fill.clone());
|
||||
let p = parse(&emf(&[plus_comment(&all)], [0; 4], [0; 4])).unwrap();
|
||||
(p.items.len(), p.ignored_clips)
|
||||
};
|
||||
let line = cat(vec![h32(0xDBC0_1002), h32(2), h32(0), f32s(&[0.0, 0.0, 10.0, 10.0]), vec![0, 1]]);
|
||||
// SetClipPath with the line replaces the clip with nothing.
|
||||
assert_eq!(drawn(vec![prec(0x4008, 0x0300 | 9, &line), prec(0x4033, 9, &[])]), (0, 0));
|
||||
// A region whose one node is the line.
|
||||
let node = cat(vec![h32(0x1000_0001), h32(line.len() as u32), line]);
|
||||
let region = cat(vec![h32(0xDBC0_1002), h32(1), node]);
|
||||
assert_eq!(drawn(vec![prec(0x4008, 0x0400 | 6, ®ion), prec(0x4034, 6, &[])]), (0, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_parse_stopped_at_a_limit_is_truncated() {
|
||||
assert!(!parse_emf(&[rect(0, 0, 10, 10)]).truncated);
|
||||
|
||||
Reference in New Issue
Block a user