emfsdk 0.1.0

EMF, EMF+, and WMF SDK for Rust
Documentation
//! Typed, byte-preserving EMF, EMF+, and WMF parsing and writing.

#![forbid(unsafe_code)]

extern crate self as emfsdk;

pub mod bitmap;
pub mod common;
pub mod emf;
pub mod emfplus;
#[cfg(feature = "render")]
pub mod render;
pub mod string;
pub mod types;
pub mod wmf;

pub use emfsdk_derive::{SdkEnum, SdkObject};

pub use crate::bitmap::{
  BITMAP_CORE_HEADER_SIZE, BITMAP_INFO_HEADER_SIZE, BITMAP_V4_HEADER_SIZE, BITMAP_V5_HEADER_SIZE,
  BitmapBitCount, BitmapCompression, BitmapCoreHeader, BitmapInfoHeader, DeviceIndependentBitmap,
  DibBitmapInfo, DibColorTable, DibColorUsage, DibHeader, EmbeddedBitmapFormat, RgbQuad,
};
pub use crate::common::{
  Error, Format, Reader, Result, SdkEnumValue, SdkRead, SdkSize, SdkWrite, UnknownRecord, Writer,
};
pub use crate::emf::{
  BitmapSourceBounds, EMR_EOF, EMR_HEADER, EmfHeader, EmfMetafile, EmfRecord, EmfRecordData,
  EmfRecordType, EmrBitmapBuffer, EmrComment, EmrCreateBrushIndirect, EmrCreateDibPatternBrushPt,
  EmrCreateMonoBrush, EmrCreatePen, EmrDeleteObject, EmrEllipse, EmrExcludeClipRect,
  EmrExtCreateFontIndirectW, EmrExtCreatePen, EmrExtTextOut, EmrIntersectClipRect, EmrLineTo,
  EmrModifyWorldTransform, EmrMoveToEx, EmrPolyPointsL, EmrPolyPointsS, EmrPolyPolygonL,
  EmrPolyPolygonS, EmrRectangle, EmrSelectObject, EmrSetBkColor, EmrSetBrushOrgEx,
  EmrSetDiBitsToDevice, EmrSetTextColor, EmrSetViewportExtEx, EmrSetViewportOrgEx,
  EmrSetWindowExtEx, EmrSetWindowOrgEx, EmrSetWorldTransform, EmrStretchDiBits, EmrText,
  ExtTextOutOptions, LogFontW,
};
pub use crate::emfplus::{
  EmfPlusBrushRef, EmfPlusDrawRectsData, EmfPlusFillRectsData, EmfPlusGraphicsVersion,
  EmfPlusGraphicsVersionValue, EmfPlusHeaderData, EmfPlusRecord, EmfPlusRecordData,
  EmfPlusRecordFlags, EmfPlusRecordType, EmfPlusRect, EmfPlusRectS, EmfPlusScaleWorldTransformData,
  EmfPlusTranslateWorldTransformData,
};
pub use crate::string::{SdkEncoding, SdkString};
pub use crate::types::{
  ColorRef, EmfPlusArgb, PointF, PointL, PointS, RectF, RectL, RectS, SizeF, SizeL, SizeS,
  TriVertex, XForm,
};
pub use crate::wmf::{
  META_EOF, WmfHeader, WmfMetafile, WmfPlaceableHeader, WmfRecord, WmfRecordData, WmfRecordFunction,
};

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Metafile {
  Emf(EmfMetafile),
  Wmf(WmfMetafile),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CompatibilityDiagnostic {
  pub format: Format,
  pub record_index: Option<usize>,
  pub nested_record_index: Option<usize>,
  pub message: String,
}

impl Metafile {
  pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
    match detect_format(bytes) {
      Some(Format::Emf) => Ok(Self::Emf(EmfMetafile::from_bytes(bytes)?)),
      Some(Format::Wmf) => Ok(Self::Wmf(WmfMetafile::from_bytes(bytes)?)),
      None => Err(Error::UnsupportedFormat),
    }
  }

  pub fn from_bytes_strict(bytes: &[u8]) -> Result<Self> {
    let metafile = Self::from_bytes(bytes)?;
    metafile.validate_strict()?;
    Ok(metafile)
  }

  pub fn to_bytes(&self) -> Result<Vec<u8>> {
    match self {
      Self::Emf(value) => value.to_bytes(),
      Self::Wmf(value) => value.to_bytes(),
    }
  }

  pub fn format(&self) -> Format {
    match self {
      Self::Emf(_) => Format::Emf,
      Self::Wmf(_) => Format::Wmf,
    }
  }

  pub fn validate_strict(&self) -> Result<()> {
    if let Some(diagnostic) = self.compatibility_diagnostics().into_iter().next() {
      return Err(Error::invalid(0, diagnostic.message));
    }
    Ok(())
  }

  pub fn compatibility_diagnostics(&self) -> Vec<CompatibilityDiagnostic> {
    match self {
      Self::Emf(value) => emf_compatibility_diagnostics(value),
      Self::Wmf(value) => wmf_compatibility_diagnostics(value),
    }
  }
}

fn emf_compatibility_diagnostics(value: &EmfMetafile) -> Vec<CompatibilityDiagnostic> {
  let mut diagnostics = Vec::new();
  for (record_index, record) in value.records.iter().enumerate() {
    match record.parse_data() {
      Ok(data) => {
        if let EmfRecordData::Comment(EmrComment::EmfPlus { records, .. }) = &data {
          for (nested_record_index, record) in records.iter().enumerate() {
            match record.parse_data() {
                            Ok(data) => {
                                let flags = EmfPlusRecordFlags::from_bits_retain(record.flags);
                                match EmfPlusRecord::from_data(&data, flags) {
                                    Ok(mut rebuilt) => {
                                        rebuilt.padding = record.padding.clone();
                                        if rebuilt != *record {
                                            diagnostics.push(CompatibilityDiagnostic {
                                                format: Format::Emf,
                                                record_index: Some(record_index),
                                                nested_record_index: Some(nested_record_index),
                                                message: format!(
                                                    "EMF+ record {nested_record_index} in EMF record {record_index} differs after typed parse/write"
                                                ),
                                            });
                                        }
                                    }
                                    Err(error) => diagnostics.push(CompatibilityDiagnostic {
                                        format: Format::Emf,
                                        record_index: Some(record_index),
                                        nested_record_index: Some(nested_record_index),
                                        message: format!(
                                            "EMF+ record {nested_record_index} in EMF record {record_index} cannot be rebuilt: {error}"
                                        ),
                                    }),
                                }
                            }
                            Err(error) => diagnostics.push(CompatibilityDiagnostic {
                                format: Format::Emf,
                                record_index: Some(record_index),
                                nested_record_index: Some(nested_record_index),
                                message: format!(
                                    "EMF+ record {nested_record_index} in EMF record {record_index} is nonconforming: {error}"
                                ),
                            }),
                        }
          }
        }
        match data.to_record() {
          Ok(rebuilt) if rebuilt == *record => {}
          Ok(_) => diagnostics.push(CompatibilityDiagnostic {
            format: Format::Emf,
            record_index: Some(record_index),
            nested_record_index: None,
            message: format!(
              "EMF record {record_index} differs after typed parse/write (type {})",
              record.record_type
            ),
          }),
          Err(error) => diagnostics.push(CompatibilityDiagnostic {
            format: Format::Emf,
            record_index: Some(record_index),
            nested_record_index: None,
            message: format!(
              "EMF record {record_index} cannot be rebuilt (type {}): {error}",
              record.record_type
            ),
          }),
        }
      }
      Err(error) => diagnostics.push(CompatibilityDiagnostic {
        format: Format::Emf,
        record_index: Some(record_index),
        nested_record_index: None,
        message: format!(
          "EMF record {record_index} is nonconforming (type {}): {error}",
          record.record_type
        ),
      }),
    }
  }
  push_unique_metafile_diagnostic(
    &mut diagnostics,
    Format::Emf,
    value.validate_header_metrics(),
  );
  if !value.trailing_data.is_empty() {
    diagnostics.push(CompatibilityDiagnostic {
      format: Format::Emf,
      record_index: None,
      nested_record_index: None,
      message: format!(
        "EMF contains {} bytes after EMR_EOF",
        value.trailing_data.len()
      ),
    });
  }
  diagnostics
}

fn wmf_compatibility_diagnostics(value: &WmfMetafile) -> Vec<CompatibilityDiagnostic> {
  let mut diagnostics = Vec::new();
  if let Some(header) = &value.placeable_header {
    push_unique_metafile_diagnostic(&mut diagnostics, Format::Wmf, header.validate());
  }
  for (record_index, record) in value.records.iter().enumerate() {
    match record.parse_data() {
      Ok(data) => match data.to_record_with_function(record.function) {
        Ok(rebuilt) if rebuilt == *record => {}
        Ok(_) => diagnostics.push(CompatibilityDiagnostic {
          format: Format::Wmf,
          record_index: Some(record_index),
          nested_record_index: None,
          message: format!(
            "WMF record {record_index} differs after typed parse/write (function {})",
            record.function
          ),
        }),
        Err(error) => diagnostics.push(CompatibilityDiagnostic {
          format: Format::Wmf,
          record_index: Some(record_index),
          nested_record_index: None,
          message: format!(
            "WMF record {record_index} cannot be rebuilt (function {}): {error}",
            record.function
          ),
        }),
      },
      Err(error) => diagnostics.push(CompatibilityDiagnostic {
        format: Format::Wmf,
        record_index: Some(record_index),
        nested_record_index: None,
        message: format!(
          "WMF record {record_index} is nonconforming (function {}): {error}",
          record.function
        ),
      }),
    }
  }
  push_unique_metafile_diagnostic(
    &mut diagnostics,
    Format::Wmf,
    value.validate_header_metrics(),
  );
  if !value.trailing_data.is_empty() {
    diagnostics.push(CompatibilityDiagnostic {
      format: Format::Wmf,
      record_index: None,
      nested_record_index: None,
      message: format!(
        "WMF contains {} bytes after META_EOF",
        value.trailing_data.len()
      ),
    });
  }
  diagnostics
}

fn push_unique_metafile_diagnostic(
  diagnostics: &mut Vec<CompatibilityDiagnostic>,
  format: Format,
  result: Result<()>,
) {
  if let Err(error) = result {
    let message = error.to_string();
    if !diagnostics
      .iter()
      .any(|diagnostic| diagnostic.message.ends_with(&message))
    {
      diagnostics.push(CompatibilityDiagnostic {
        format,
        record_index: None,
        nested_record_index: None,
        message,
      });
    }
  }
}

pub fn detect_format(bytes: &[u8]) -> Option<Format> {
  if emf::looks_like_emf(bytes) {
    Some(Format::Emf)
  } else if wmf::looks_like_wmf(bytes) {
    Some(Format::Wmf)
  } else {
    None
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  fn minimal_wmf() -> Vec<u8> {
    [
      1u16.to_le_bytes().as_slice(),
      9u16.to_le_bytes().as_slice(),
      0x0300u16.to_le_bytes().as_slice(),
      12u32.to_le_bytes().as_slice(),
      0u16.to_le_bytes().as_slice(),
      3u32.to_le_bytes().as_slice(),
      0u16.to_le_bytes().as_slice(),
      3u32.to_le_bytes().as_slice(),
      META_EOF.to_le_bytes().as_slice(),
    ]
    .concat()
  }

  #[test]
  fn default_parse_preserves_compatibility_and_strict_parse_rejects_it() {
    let mut bytes = minimal_wmf();
    bytes.extend_from_slice(&[0xAA, 0xBB]);

    let metafile = Metafile::from_bytes(&bytes).unwrap();
    assert_eq!(metafile.to_bytes().unwrap(), bytes);
    assert_eq!(metafile.compatibility_diagnostics().len(), 1);
    assert!(metafile.validate_strict().is_err());
    assert!(Metafile::from_bytes_strict(&bytes).is_err());
    assert!(Metafile::from_bytes_strict(&minimal_wmf()).is_ok());
  }
}