use crate::emf::parser::{
objects::{PointL, RectL},
Cursor,
};
use crate::emf::Error;
pub fn parse_point(c: &mut Cursor<'_>) -> Result<PointL, Error> {
PointL::read(c)
}
pub fn parse_rect(c: &mut Cursor<'_>) -> Result<RectL, Error> {
RectL::read(c)
}
pub fn parse_round_rect(c: &mut Cursor<'_>) -> Result<(RectL, i32, i32), Error> {
let rect = RectL::read(c)?;
let cx = c.i32()?;
let cy = c.i32()?;
Ok((rect, cx, cy))
}
pub fn parse_arc_like(c: &mut Cursor<'_>) -> Result<(RectL, PointL, PointL), Error> {
let rect = RectL::read(c)?;
let start = PointL::read(c)?;
let end = PointL::read(c)?;
Ok((rect, start, end))
}
pub fn parse_points16(c: &mut Cursor<'_>) -> Result<(RectL, Vec<(i16, i16)>), Error> {
let bounds = RectL::read(c)?;
let count = (c.u32()? as usize).min(c.remaining() / 4);
let mut pts = Vec::with_capacity(count);
for _ in 0..count {
let x = c.u16()? as i16;
let y = c.u16()? as i16;
pts.push((x, y));
}
Ok((bounds, pts))
}
pub fn parse_poly_polygon16(c: &mut Cursor<'_>) -> Result<(RectL, Vec<Vec<(i16, i16)>>), Error> {
let bounds = RectL::read(c)?;
let polygons = c.u32()? as usize;
let points = c.u32()? as usize;
if polygons > c.remaining() / 4 {
return Err(Error::UnexpectedEof {
at: c.position(),
need: polygons.saturating_mul(4),
});
}
let mut counts = Vec::with_capacity(polygons);
let mut total = 0usize;
for _ in 0..polygons {
let count = c.u32()? as usize;
total = total.saturating_add(count);
counts.push(count);
}
if total != points {
return Err(Error::InvalidPolygonPointCount {
declared: points,
actual: total,
});
}
if points > c.remaining() / 4 {
return Err(Error::UnexpectedEof {
at: c.position(),
need: points.saturating_mul(4),
});
}
let mut result = Vec::with_capacity(polygons);
for count in counts {
let mut polygon = Vec::with_capacity(count);
for _ in 0..count {
polygon.push((c.u16()? as i16, c.u16()? as i16));
}
result.push(polygon);
}
Ok((bounds, result))
}