use crate::object::NameId;
use crate::page_ir::{BlendMode, LineCap, LineJoin, Matrix, PathData, PathVerb, Point};
use crate::content::semantic::{FontId, SemColor};
#[derive(Debug, Clone, PartialEq)]
pub enum ColorSpace {
DeviceGray,
DeviceRgb,
DeviceCmyk,
Pattern(Option<Box<ColorSpace>>),
Named(NameId),
}
impl ColorSpace {
pub fn components(&self) -> Option<usize> {
match self {
ColorSpace::DeviceGray => Some(1),
ColorSpace::DeviceRgb => Some(3),
ColorSpace::DeviceCmyk => Some(4),
ColorSpace::Pattern(_) | ColorSpace::Named(_) => None,
}
}
}
#[derive(Debug, Clone)]
pub struct GraphicsState {
pub ctm: Matrix,
pub fill_space: ColorSpace,
pub stroke_space: ColorSpace,
pub fill_color: SemColor,
pub stroke_color: SemColor,
pub fill_alpha: f32,
pub stroke_alpha: f32,
pub blend: BlendMode,
pub line_width: f64,
pub line_cap: LineCap,
pub line_join: LineJoin,
pub miter_limit: f64,
pub dash_pattern: Vec<f64>,
pub dash_phase: f64,
pub clip_depth: u32,
pub char_spacing: f64,
pub word_spacing: f64,
pub horizontal_scale: f64,
pub leading: f64,
pub font: Option<FontId>,
pub font_size: f64,
pub render_mode: u8,
pub rise: f64,
}
impl Default for GraphicsState {
fn default() -> Self {
Self {
ctm: Matrix::IDENTITY,
fill_space: ColorSpace::DeviceGray,
stroke_space: ColorSpace::DeviceGray,
fill_color: SemColor::DeviceGray(0.0),
stroke_color: SemColor::DeviceGray(0.0),
fill_alpha: 1.0,
stroke_alpha: 1.0,
blend: BlendMode::Normal,
line_width: 1.0,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
miter_limit: 10.0,
dash_pattern: Vec::new(),
dash_phase: 0.0,
clip_depth: 0,
char_spacing: 0.0,
word_spacing: 0.0,
horizontal_scale: 100.0,
leading: 0.0,
font: None,
font_size: 0.0,
render_mode: 0,
rise: 0.0,
}
}
}
#[derive(Debug, Default, Clone)]
pub struct PathBuilder {
verbs: Vec<PathVerb>,
points: Vec<Point>,
subpath: Option<Subpath>,
}
#[derive(Debug, Clone, Copy)]
struct Subpath {
start: Point,
current: Point,
}
impl PathBuilder {
pub fn is_empty(&self) -> bool {
self.verbs.is_empty()
}
pub fn move_to(&mut self, x: f64, y: f64) {
let p = Point { x, y };
self.verbs.push(PathVerb::MoveTo);
self.points.push(p);
self.subpath = Some(Subpath {
start: p,
current: p,
});
}
pub fn line_to(&mut self, x: f64, y: f64) {
if self.subpath.is_none() {
self.move_to(x, y);
return;
}
let p = Point { x, y };
self.verbs.push(PathVerb::LineTo);
self.points.push(p);
if let Some(subpath) = &mut self.subpath {
subpath.current = p;
}
}
pub fn curve_to(&mut self, x1: f64, y1: f64, x2: f64, y2: f64, x3: f64, y3: f64) {
if self.subpath.is_none() {
self.move_to(x1, y1);
}
self.verbs.push(PathVerb::CurveTo);
self.points.push(Point { x: x1, y: y1 });
self.points.push(Point { x: x2, y: y2 });
self.points.push(Point { x: x3, y: y3 });
if let Some(subpath) = &mut self.subpath {
subpath.current = Point { x: x3, y: y3 };
}
}
pub fn curve_v(&mut self, x2: f64, y2: f64, x3: f64, y3: f64) {
let c1 = self
.subpath
.map_or(Point::default(), |subpath| subpath.current);
self.curve_to(c1.x, c1.y, x2, y2, x3, y3);
}
pub fn curve_y(&mut self, x1: f64, y1: f64, x3: f64, y3: f64) {
self.curve_to(x1, y1, x3, y3, x3, y3);
}
pub fn rect(&mut self, x: f64, y: f64, w: f64, h: f64) {
self.move_to(x, y);
self.line_to(x + w, y);
self.line_to(x + w, y + h);
self.line_to(x, y + h);
self.close();
self.subpath = Some(Subpath {
start: Point { x, y },
current: Point { x, y },
});
}
pub fn close(&mut self) {
if let Some(subpath) = &mut self.subpath {
self.verbs.push(PathVerb::Close);
subpath.current = subpath.start;
}
}
pub fn take(&mut self) -> PathData {
let verbs = std::mem::take(&mut self.verbs);
let points = std::mem::take(&mut self.points);
self.subpath = None;
PathData {
verbs: verbs.into(),
points: points.into(),
}
}
pub fn clear(&mut self) {
self.verbs.clear();
self.points.clear();
self.subpath = None;
}
}