Files
vectorcraft/crates/metafile/src/bytes.rs
T
dotCooCoo 9efde43f1d Metafile import: EMF and WMF are read through craft-metafile, so EMF+ pictures open
EMF and WMF import now plays the records with the shared craft-metafile
reader (storytold/craft-libs#778, at the craft-libs rev the SVG, EPS and
DXF crates already use) and maps its picture onto the document here:
paths with their fills, strokes, caps, joins, miter limits and dashes,
clipping groups, images and point type. The EMF and WMF record players in
crates/metafile/src/import and the DIB decoder they used are removed. The
writers are unchanged, and the PICT reader keeps its own player.

What changes for a file being opened, placed or pasted, and for an EPS
file's Windows metafile preview (read when its PostScript can't be):

- An EMF that draws with EMF+ records is read from them. It opened empty,
  with a warning that EMF+ is not read. A file with both kinds of record
  is read from its EMF+ records too.
- Hatched and pattern brushes fill with the average color of the brush's
  tile, translucent where the tile has gaps. Hatches filled with the hatch
  color and pattern brushes with gray.
- Translucent fills, strokes and EMF+ text keep their alpha as opacity.
- Text keeps a stretch across from the world transform; it kept only the
  turn. Text the file clips to a rectangle is clipped to it, and an
  underlined or struck-through font gives underlined or struck-through
  type.
- A clip path whose bounding box holds the whole picture is kept unless
  it is that rectangle; a diamond or a bow tie around the picture was
  dropped.
- Images drawn with SRCAND, SRCPAINT or SRCINVERT are drawn as they look
  over white, and images drawn with other raster operations are drawn as
  a plain copy. They were left out with a warning.
- Excluded clip rectangles and the other clip region modes are applied.
  A clip path combined with RGN_OR, RGN_XOR or RGN_DIFF is ignored
  without the warning it gave.
- An image past the reader's size or pixel limits, or one that can't be
  decoded, is left out without a warning of its own. A file past the
  reader's limits (more than 32 nested clip paths, 200,000 drawn items,
  256 MiB held) is read up to that point without a warning.
- An EMF record whose size is not a multiple of 4 ends the file, with the
  "ends early" warning.

Tests: an EMF+ only picture imports its fill (it failed before), a dual
EMF+ file draws its translucent EMF+ fill and not its fallback, hatch and
pattern brush colors, clipped and underlined text, excluded clip
rectangles, a shared clipping group, an odd record size, WMF at 1440
units an inch without a placeable header and at its own units with an
inverted box, an EMF without a frame, diamond and bow-tie clips, text with a
stretch, clips nested past the stack and 200,000 clip records; the fuzz
test gains random EMF+ records.
2026-11-14 06:04:19 -08:00

47 lines
1.3 KiB
Rust

//! Little-endian values: the buffer both writers build records in.
/// Bytes being written.
#[derive(Default)]
pub(crate) struct Out(pub Vec<u8>);
impl Out {
pub fn len(&self) -> usize {
self.0.len()
}
pub fn u16(&mut self, v: u16) {
self.0.extend_from_slice(&v.to_le_bytes());
}
pub fn i16(&mut self, v: i16) {
self.0.extend_from_slice(&v.to_le_bytes());
}
pub fn u32(&mut self, v: u32) {
self.0.extend_from_slice(&v.to_le_bytes());
}
pub fn i32(&mut self, v: i32) {
self.0.extend_from_slice(&v.to_le_bytes());
}
pub fn f32(&mut self, v: f32) {
self.0.extend_from_slice(&v.to_le_bytes());
}
pub fn bytes(&mut self, b: &[u8]) {
self.0.extend_from_slice(b);
}
/// Zeros up to the next multiple of `n` bytes.
pub fn pad(&mut self, n: usize) {
while !self.0.len().is_multiple_of(n) {
self.0.push(0);
}
}
/// Overwrite the `u32` at `at` (a size or count known only later).
pub fn set_u32(&mut self, at: usize, v: u32) {
if let Some(b) = self.0.get_mut(at..at + 4) {
b.copy_from_slice(&v.to_le_bytes());
}
}
pub fn set_i32(&mut self, at: usize, v: i32) {
if let Some(b) = self.0.get_mut(at..at + 4) {
b.copy_from_slice(&v.to_le_bytes());
}
}
}