use std::collections::HashMap;
use crate::emf::parser::objects::{LogBrush, LogFontW, LogPen, XForm};
#[derive(Debug, Clone, Hash)]
pub struct BitmapPattern {
pub width: u32,
pub height: u32,
pub data_url: String,
}
#[derive(Debug, Clone)]
pub enum GraphicsObject {
Pen(LogPen),
Brush(LogBrush),
PatternBrush(BitmapPattern),
Font(LogFontW),
}
#[derive(Debug, Clone)]
pub struct DeviceContext {
pub pen: Option<LogPen>,
pub brush: Option<LogBrush>,
pub pattern_brush: Option<BitmapPattern>,
pub brush_origin: (i32, i32),
pub poly_fill_mode: u32,
pub font: Option<LogFontW>,
pub text_color: u32,
pub bk_color: u32,
pub bk_mode: u32, pub text_align: u32, pub map_mode: u32,
pub world_xform: XForm,
pub window_org: (i32, i32),
pub window_ext: (i32, i32),
pub viewport_org: (i32, i32),
pub viewport_ext: (i32, i32),
pub current_pos: (i32, i32),
pub clip_rect: Option<(i32, i32, i32, i32)>,
}
impl Default for DeviceContext {
fn default() -> Self {
Self {
clip_rect: None,
pen: None,
brush: None,
pattern_brush: None,
brush_origin: (0, 0),
poly_fill_mode: 1,
font: None,
text_color: 0x00_00_00_00,
bk_color: 0x00_FF_FF_FF,
bk_mode: 2, text_align: 0,
map_mode: 1, world_xform: XForm::identity(),
window_org: (0, 0),
window_ext: (1, 1),
viewport_org: (0, 0),
viewport_ext: (1, 1),
current_pos: (0, 0),
}
}
}
#[derive(Debug, Default)]
pub struct DcStack {
current: DeviceContext,
stack: Vec<DeviceContext>,
}
impl DcStack {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn save(&mut self) {
self.stack.push(self.current.clone());
}
pub fn restore(&mut self, relative: i32) -> bool {
if relative == 0 {
return false;
}
let n = if relative < 0 {
(-relative) as usize
} else {
return false;
};
if self.stack.len() < n {
return false;
}
let target_idx = self.stack.len() - n;
self.stack.truncate(target_idx + 1);
if let Some(dc) = self.stack.pop() {
self.current = dc;
true
} else {
false
}
}
#[must_use]
pub fn current(&self) -> &DeviceContext {
&self.current
}
pub fn current_mut(&mut self) -> &mut DeviceContext {
&mut self.current
}
#[must_use]
pub fn depth(&self) -> usize {
self.stack.len()
}
}
#[derive(Debug, Default)]
pub struct ObjectTable {
handles: HashMap<u32, GraphicsObject>,
}
impl ObjectTable {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn insert(&mut self, handle: u32, obj: GraphicsObject) {
self.handles.insert(handle, obj);
}
#[must_use]
pub fn get(&self, handle: u32) -> Option<&GraphicsObject> {
self.handles.get(&handle)
}
pub fn remove(&mut self, handle: u32) -> Option<GraphicsObject> {
self.handles.remove(&handle)
}
#[must_use]
pub fn len(&self) -> usize {
self.handles.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.handles.is_empty()
}
}