use crate::emf::parser::objects::{PointL, RectL};
use crate::emf::Error;
#[derive(Debug, Clone)]
pub struct ExtTextOut {
pub bounds: RectL,
pub graphics_mode: u32,
pub ex_scale: f32,
pub ey_scale: f32,
pub reference: PointL,
pub options: u32,
pub rectangle: RectL,
pub text: String, }
const HEADER_BYTES: usize = 8;
pub fn parse(payload: &[u8]) -> Result<ExtTextOut, Error> {
if payload.len() < 68 {
return Err(Error::UnexpectedEof { at: 0, need: 68 });
}
let bounds = read_rectl(&payload[0..16])?;
let graphics_mode = read_u32(&payload[16..20]);
let ex_scale = f32::from_le_bytes(payload[20..24].try_into().unwrap());
let ey_scale = f32::from_le_bytes(payload[24..28].try_into().unwrap());
let reference = PointL {
x: read_i32(&payload[28..32]),
y: read_i32(&payload[32..36]),
};
let n_chars = read_u32(&payload[36..40]) as usize;
let off_string = read_u32(&payload[40..44]) as usize;
let options = read_u32(&payload[44..48]);
let rectangle = read_rectl(&payload[48..64])?;
let _off_dx = read_u32(&payload[64..68]);
let text = if n_chars == 0 {
String::new()
} else {
let start = off_string
.checked_sub(HEADER_BYTES)
.ok_or(Error::UnexpectedEof {
at: 0,
need: HEADER_BYTES,
})?;
let byte_len = n_chars
.checked_mul(2)
.ok_or(Error::UnexpectedEof { at: start, need: 0 })?;
if start + byte_len > payload.len() {
return Err(Error::UnexpectedEof {
at: start,
need: byte_len,
});
}
let slice = &payload[start..start + byte_len];
let mut utf16 = Vec::with_capacity(n_chars);
for i in 0..n_chars {
utf16.push(u16::from_le_bytes([slice[i * 2], slice[i * 2 + 1]]));
}
String::from_utf16_lossy(&utf16)
};
Ok(ExtTextOut {
bounds,
graphics_mode,
ex_scale,
ey_scale,
reference,
options,
rectangle,
text,
})
}
fn read_u32(b: &[u8]) -> u32 {
u32::from_le_bytes([b[0], b[1], b[2], b[3]])
}
fn read_i32(b: &[u8]) -> i32 {
read_u32(b) as i32
}
fn read_rectl(b: &[u8]) -> Result<RectL, Error> {
Ok(RectL {
left: read_i32(&b[0..4]),
top: read_i32(&b[4..8]),
right: read_i32(&b[8..12]),
bottom: read_i32(&b[12..16]),
})
}