use crate::{GlyphRun, GradientItem, GroupClip, ImageItem, Item, Page, PathItem, Rect};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Painted<'a> {
Glyphs(&'a GlyphRun),
Rect(&'a Rect),
Path(&'a PathItem),
Gradient(&'a GradientItem),
Image(&'a ImageItem),
ClipStart(&'a GroupClip),
ClipEnd,
}
pub trait PageSink {
type Error;
fn glyphs(&mut self, run: &GlyphRun) -> Result<(), Self::Error>;
fn rect(&mut self, rect: &Rect) -> Result<(), Self::Error>;
fn path(&mut self, path: &PathItem) -> Result<(), Self::Error>;
fn gradient(&mut self, gradient: &GradientItem) -> Result<(), Self::Error>;
fn image(&mut self, image: &ImageItem) -> Result<(), Self::Error>;
fn clip_start(&mut self, clip: &GroupClip) -> Result<(), Self::Error>;
fn clip_end(&mut self) -> Result<(), Self::Error>;
}
impl Page {
#[must_use]
pub fn painted(&self) -> Vec<Painted<'_>> {
let mut painted = Vec::new();
for item in &self.items {
item.paint_into(&mut painted);
}
painted
}
pub fn replay<S: PageSink>(&self, sink: &mut S) -> Result<(), S::Error> {
self.painted().into_iter().try_for_each(|item| match item {
Painted::Glyphs(run) => sink.glyphs(run),
Painted::Rect(rect) => sink.rect(rect),
Painted::Path(path) => sink.path(path),
Painted::Gradient(gradient) => sink.gradient(gradient),
Painted::Image(image) => sink.image(image),
Painted::ClipStart(clip) => sink.clip_start(clip),
Painted::ClipEnd => sink.clip_end(),
})
}
}
impl Item {
fn paint_into<'a>(&'a self, painted: &mut Vec<Painted<'a>>) {
match self {
Self::Glyphs(run) => painted.push(Painted::Glyphs(run)),
Self::Rect(rect) => painted.push(Painted::Rect(rect)),
Self::Path(path) => painted.push(Painted::Path(path)),
Self::Gradient(gradient) => painted.push(Painted::Gradient(gradient)),
Self::Image(image) => painted.push(Painted::Image(image)),
Self::Group(group) => {
if group.shows_nothing() {
return;
}
let clip = group.clip.as_ref();
if let Some(clip) = clip {
painted.push(Painted::ClipStart(clip));
}
for item in &group.items {
item.paint_into(painted);
}
if clip.is_some() {
painted.push(Painted::ClipEnd);
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::PageSink;
use crate::{
FillRule, GlyphRun, GradientItem, Group, GroupClip, GroupPlaces, ImageItem, Item, Page,
PathItem, Rect, Rgb,
};
use f_office_units::Length;
struct Calls {
made: Vec<i64>,
stop: i64,
}
impl PageSink for Calls {
type Error = i64;
fn glyphs(&mut self, _: &GlyphRun) -> Result<(), i64> {
Err(-1)
}
fn rect(&mut self, rect: &Rect) -> Result<(), i64> {
let x = rect.x.units();
if x == self.stop {
return Err(x);
}
self.made.push(x);
Ok(())
}
fn path(&mut self, _: &PathItem) -> Result<(), i64> {
self.made.push(0);
Ok(())
}
fn gradient(&mut self, _: &GradientItem) -> Result<(), i64> {
self.made.push(9);
Ok(())
}
fn image(&mut self, _: &ImageItem) -> Result<(), i64> {
Err(-2)
}
fn clip_start(&mut self, clip: &GroupClip) -> Result<(), i64> {
let left = clip
.commands
.iter()
.flat_map(crate::PathCommand::points)
.map(|p| p.x.units());
self.made.push(-left.min().unwrap_or_default());
Ok(())
}
fn clip_end(&mut self) -> Result<(), i64> {
self.made.push(-100);
Ok(())
}
}
fn clip(x: i64, width: i64) -> GroupClip {
let u = Length::from_units;
GroupClip::rect(u(x), Length::ZERO, u(width), u(1))
}
fn rect(x: i64) -> Item {
Item::Rect(Rect {
x: Length::from_units(x),
y: Length::ZERO,
width: Length::from_units(1),
height: Length::from_units(1),
color: Rgb::BLACK,
})
}
fn page() -> Page {
let path = Item::Path(PathItem {
commands: Vec::new(),
color: Rgb::BLACK,
fill_rule: FillRule::NonZero,
});
let origin = crate::Point {
x: Length::ZERO,
y: Length::ZERO,
};
let gradient = Item::Gradient(GradientItem::new(
Vec::new(),
FillRule::NonZero,
(origin, origin),
Vec::new(),
));
Page {
width: Length::from_units(10),
height: Length::from_units(10),
orientation: crate::Orientation::Portrait,
items: vec![
rect(1),
Item::Group(Group::new(vec![
Item::Group(Group {
items: vec![rect(2), path, gradient],
clip: Some(clip(7, 1)),
places: GroupPlaces::Own,
}),
rect(3),
])),
rect(4),
Item::Group(Group {
items: vec![rect(5)],
clip: Some(clip(8, 0)),
places: GroupPlaces::Own,
}),
],
}
}
#[test]
fn replays_a_groups_items_in_its_place_inside_its_clip() {
let mut calls = Calls {
made: Vec::new(),
stop: 0,
};
assert_eq!(page().replay(&mut calls), Ok(()));
assert_eq!(calls.made, [1, -7, 2, 0, 9, -100, 3, 4]);
}
#[test]
fn stops_at_the_first_error_of_the_sink() {
let mut calls = Calls {
made: Vec::new(),
stop: 3,
};
assert_eq!(page().replay(&mut calls), Err(3));
assert_eq!(calls.made, [1, -7, 2, 0, 9, -100]);
}
}