use crate::FormatError;
pub const MAX_WIDTH: u32 = 4096;
pub const MAX_HEIGHT: u32 = 4096;
pub const MAX_GLYPHS: usize = 65_536;
pub const MAX_TOTAL_ALLOCATION: usize = 256 * 1024 * 1024;
pub const MAX_ANIMATION_FRAMES: usize = 1024;
pub const MAX_SERIALIZED_BYTES: usize = 256 * 1024 * 1024;
pub fn validate_dimensions(width: u32, height: u32) -> Result<usize, FormatError> {
if width > MAX_WIDTH {
return Err(FormatError::WidthExceeded {
width,
max: MAX_WIDTH,
});
}
if height > MAX_HEIGHT {
return Err(FormatError::HeightExceeded {
height,
max: MAX_HEIGHT,
});
}
let w = width as usize;
let h = height as usize;
w.checked_mul(h)
.ok_or(FormatError::DimensionOverflow { width, height })
}
#[derive(Debug, Default)]
pub struct AllocationTracker {
total: usize,
limit: usize,
}
impl AllocationTracker {
pub fn new() -> Self {
Self {
total: 0,
limit: MAX_TOTAL_ALLOCATION,
}
}
pub fn reserve(&mut self, bytes: usize) -> Result<(), FormatError> {
let new_total = self
.total
.checked_add(bytes)
.ok_or(FormatError::AllocationOverflow)?;
if new_total > self.limit {
return Err(FormatError::AllocationExceeded {
requested: new_total,
limit: self.limit,
});
}
self.total = new_total;
Ok(())
}
}