use std::fmt;
use std::rc::Rc;
use waft_embedder::valo::{self, FillRule, Op, PaintStyle};
use waft_embedder::{Color, Offset, RRect, Rect, StrokeCap};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CanvasMethod {
Save,
Restore,
Translate,
Scale,
Rotate,
Clip,
DrawRect,
DrawRRect,
DrawDRRect,
DrawCircle,
DrawLine,
DrawPath,
DrawShadow,
DrawParagraph,
DrawPaint,
Other,
}
impl CanvasMethod {
pub fn of(op: &Op) -> CanvasMethod {
match op {
Op::Save | Op::SaveLayer { .. } => CanvasMethod::Save,
Op::Restore => CanvasMethod::Restore,
Op::Transform(matrix) => match Transform::of(matrix) {
Transform::Translate(..) => CanvasMethod::Translate,
Transform::Scale(..) => CanvasMethod::Scale,
Transform::Other(_) => CanvasMethod::Rotate,
},
Op::ClipPath { .. } => CanvasMethod::Clip,
Op::DrawPath {
path, fill_rule, ..
} => {
if path.as_oval().is_some() {
CanvasMethod::DrawCircle
} else if path.as_round_rect().is_some() {
CanvasMethod::DrawRRect
} else if is_drrect(path, *fill_rule) {
CanvasMethod::DrawDRRect
} else if line_of(path).is_some() {
CanvasMethod::DrawLine
} else {
CanvasMethod::DrawPath
}
}
Op::DrawRect { .. } => CanvasMethod::DrawRect,
Op::RRectBlur { radii, .. } if radii.iter().all(|radius| *radius == 0.0) => {
CanvasMethod::DrawRect
}
Op::RRectBlur { .. } => CanvasMethod::DrawRRect,
Op::DrawShadow { .. } => CanvasMethod::DrawShadow,
Op::GlyphRun { .. } => CanvasMethod::DrawParagraph,
Op::DrawPaint { .. } => CanvasMethod::DrawPaint,
_ => CanvasMethod::Other,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
enum Transform {
Translate(f64, f64),
Scale(f64, f64),
Other([f64; 6]),
}
impl Transform {
fn of(matrix: &valo::Matrix) -> Transform {
let [a, b, c, d, tx, ty] = matrix.to_affine().map(f64::from);
if b == 0.0 && c == 0.0 && a == 1.0 && d == 1.0 {
Transform::Translate(tx, ty)
} else if b == 0.0 && c == 0.0 && tx == 0.0 && ty == 0.0 {
Transform::Scale(a, d)
} else {
Transform::Other([a, b, c, d, tx, ty])
}
}
fn scale(self) -> Option<(f64, f64)> {
match self {
Transform::Scale(x, y) => Some((x, y)),
Transform::Translate(0.0, 0.0) => Some((1.0, 1.0)),
_ => None,
}
}
}
fn is_drrect(path: &valo::Path, fill_rule: FillRule) -> bool {
fill_rule == FillRule::EvenOdd && path.flatten(1.0).len() == 2
}
fn line_of(path: &valo::Path) -> Option<(Offset, Offset)> {
let contours = path.flatten(1.0);
match contours.as_slice() {
[contour] if !contour.closed && contour.points.len() == 2 => {
let point = |p: valo::Point| Offset::new(f64::from(p.x), f64::from(p.y));
Some((point(contour.points[0]), point(contour.points[1])))
}
_ => None,
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum RecordedCall {
Save,
Restore,
Translate {
x: f64,
y: f64,
},
Scale {
x: f64,
y: f64,
},
Transform {
affine: [f64; 6],
},
Clip,
Rect {
rect: Rect,
color: Color,
},
Circle {
center: Offset,
radius: f64,
color: Color,
stroke_width: Option<f64>,
},
RRect {
bounds: Rect,
color: Color,
stroke_width: Option<f64>,
},
DRRect {
color: Color,
},
Line {
p1: Offset,
p2: Offset,
color: Color,
stroke_width: Option<f64>,
},
Path {
bounds: Rect,
color: Color,
stroke_width: Option<f64>,
},
Shadow {
elevation: f64,
color: Color,
},
Paragraph,
Paint {
color: Color,
},
Other,
}
impl RecordedCall {
pub fn of(op: &Op) -> RecordedCall {
match op {
Op::Save | Op::SaveLayer { .. } => RecordedCall::Save,
Op::Restore => RecordedCall::Restore,
Op::Transform(matrix) => match Transform::of(matrix) {
Transform::Translate(x, y) => RecordedCall::Translate { x, y },
Transform::Scale(x, y) => RecordedCall::Scale { x, y },
Transform::Other(affine) => RecordedCall::Transform { affine },
},
Op::ClipPath { .. } => RecordedCall::Clip,
Op::DrawRect { rect, paint, .. } => RecordedCall::Rect {
rect: rect_of(*rect),
color: paint.color.into(),
},
Op::RRectBlur {
rect, radii, paint, ..
} if radii.iter().all(|radius| *radius == 0.0) => RecordedCall::Rect {
rect: rect_of(*rect),
color: paint.color.into(),
},
Op::RRectBlur { rect, paint, .. } => RecordedCall::RRect {
bounds: rect_of(*rect),
color: paint.color.into(),
stroke_width: None,
},
Op::DrawPath {
path,
fill_rule,
paint,
..
} => {
let color = paint.color.into();
let stroke_width = stroke_width_of(&paint.style);
if let Some(oval) = path.as_oval() {
RecordedCall::Circle {
center: Offset::new(
f64::from(oval.x + oval.width / 2.0),
f64::from(oval.y + oval.height / 2.0),
),
radius: f64::from(oval.width / 2.0),
color,
stroke_width,
}
} else if path.as_round_rect().is_some() {
RecordedCall::RRect {
bounds: rect_of(path.bounds()),
color,
stroke_width,
}
} else if is_drrect(path, *fill_rule) {
RecordedCall::DRRect { color }
} else if let Some((p1, p2)) = line_of(path) {
RecordedCall::Line {
p1,
p2,
color,
stroke_width,
}
} else {
RecordedCall::Path {
bounds: rect_of(path.bounds()),
color,
stroke_width,
}
}
}
Op::DrawShadow {
elevation, color, ..
} => RecordedCall::Shadow {
elevation: f64::from(*elevation),
color: (*color).into(),
},
Op::GlyphRun { .. } => RecordedCall::Paragraph,
Op::DrawPaint { paint, .. } => RecordedCall::Paint {
color: paint.color.into(),
},
_ => RecordedCall::Other,
}
}
}
fn stroke_width_of(style: &PaintStyle) -> Option<f64> {
match style {
PaintStyle::Stroke(stroke) => Some(f64::from(stroke.width)),
PaintStyle::Fill => None,
}
}
fn rect_of(rect: valo::Rect) -> Rect {
Rect::from_ltwh(
f64::from(rect.x),
f64::from(rect.y),
f64::from(rect.width),
f64::from(rect.height),
)
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PaintedStyle {
Fill,
Stroke(Option<f64>),
}
impl PaintedStyle {
fn matches(self, style: &PaintStyle) -> bool {
match (self, style) {
(PaintedStyle::Fill, PaintStyle::Fill) => true,
(PaintedStyle::Stroke(width), PaintStyle::Stroke(stroke)) => {
width.is_none_or(|width| f32_is(f64::from(stroke.width), width))
}
_ => false,
}
}
}
pub type SomethingPredicate = Rc<dyn Fn(&RecordedCall) -> Result<bool, String>>;
#[derive(Clone)]
pub enum PaintCall {
Save,
Restore,
Translate { x: Option<f64>, y: Option<f64> },
Scale { x: Option<f64>, y: Option<f64> },
Rotate { angle: Option<f64> },
Clip,
Path {
color: Option<Color>,
style: Option<PaintedStyle>,
includes: Vec<Offset>,
excludes: Vec<Offset>,
},
Line {
p1: Option<Offset>,
p2: Option<Offset>,
color: Option<Color>,
stroke_width: Option<f64>,
},
Arc {
rect: Option<Rect>,
color: Option<Color>,
stroke_width: Option<f64>,
stroke_cap: Option<StrokeCap>,
},
Rect {
rect: Option<Rect>,
color: Option<Color>,
},
RRect {
rrect: Option<RRect>,
color: Option<Color>,
style: Option<PaintedStyle>,
has_mask_filter: Option<bool>,
},
DRRect {
outer: Option<RRect>,
inner: Option<RRect>,
color: Option<Color>,
style: Option<PaintedStyle>,
},
Circle {
x: Option<f64>,
y: Option<f64>,
radius: Option<f64>,
color: Option<Color>,
style: Option<PaintedStyle>,
},
Shadow {
elevation: Option<f64>,
color: Option<Color>,
},
Paragraph,
SaveRestore,
Something(SomethingPredicate),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Reading {
Is,
Could,
Not,
}
impl PaintCall {
pub fn translate(x: Option<f64>, y: Option<f64>) -> PaintCall {
PaintCall::Translate { x, y }
}
pub fn scale(x: Option<f64>, y: Option<f64>) -> PaintCall {
PaintCall::Scale { x, y }
}
pub fn rotate(angle: Option<f64>) -> PaintCall {
PaintCall::Rotate { angle }
}
pub fn path(color: Option<Color>) -> PaintCall {
PaintCall::Path {
color,
style: None,
includes: Vec::new(),
excludes: Vec::new(),
}
}
pub fn path_covering(includes: Vec<Offset>, excludes: Vec<Offset>) -> PaintCall {
PaintCall::Path {
color: None,
style: None,
includes,
excludes,
}
}
pub fn line(p1: Offset, p2: Offset, stroke_width: Option<f64>) -> PaintCall {
PaintCall::Line {
p1: Some(p1),
p2: Some(p2),
color: None,
stroke_width,
}
}
pub fn arc(rect: Option<Rect>) -> PaintCall {
PaintCall::Arc {
rect,
color: None,
stroke_width: None,
stroke_cap: None,
}
}
pub fn rect(color: Option<Color>) -> PaintCall {
PaintCall::Rect { rect: None, color }
}
pub fn rect_at(rect: Rect, color: Option<Color>) -> PaintCall {
PaintCall::Rect {
rect: Some(rect),
color,
}
}
pub fn rrect(color: Option<Color>) -> PaintCall {
PaintCall::RRect {
rrect: None,
color,
style: None,
has_mask_filter: None,
}
}
pub fn rrect_at(rrect: RRect, color: Option<Color>) -> PaintCall {
PaintCall::RRect {
rrect: Some(rrect),
color,
style: None,
has_mask_filter: None,
}
}
pub fn drrect(color: Option<Color>) -> PaintCall {
PaintCall::DRRect {
outer: None,
inner: None,
color,
style: None,
}
}
pub fn drrect_between(outer: RRect, inner: RRect, color: Option<Color>) -> PaintCall {
PaintCall::DRRect {
outer: Some(outer),
inner: Some(inner),
color,
style: None,
}
}
pub fn circle(color: Option<Color>) -> PaintCall {
PaintCall::Circle {
x: None,
y: None,
radius: None,
color,
style: None,
}
}
pub fn stroked_circle(color: Color, stroke_width: f64) -> PaintCall {
PaintCall::Circle {
x: None,
y: None,
radius: None,
color: Some(color),
style: Some(PaintedStyle::Stroke(Some(stroke_width))),
}
}
pub fn shadow(color: Option<Color>) -> PaintCall {
PaintCall::Shadow {
elevation: None,
color,
}
}
pub fn shadow_with_elevation(elevation: f64, color: Option<Color>) -> PaintCall {
PaintCall::Shadow {
elevation: Some(elevation),
color,
}
}
pub fn something(
predicate: impl Fn(&RecordedCall) -> Result<bool, String> + 'static,
) -> PaintCall {
PaintCall::Something(Rc::new(predicate))
}
fn reading(&self, op: &Op) -> Reading {
let method = CanvasMethod::of(op);
let is = |yes: bool| if yes { Reading::Is } else { Reading::Not };
match self {
PaintCall::Save | PaintCall::SaveRestore => is(method == CanvasMethod::Save),
PaintCall::Restore => is(method == CanvasMethod::Restore),
PaintCall::Translate { .. } => is(method == CanvasMethod::Translate),
PaintCall::Scale { .. } => is(scale_of(op).is_some()),
PaintCall::Rotate { .. } => is(method == CanvasMethod::Rotate),
PaintCall::Clip => is(method == CanvasMethod::Clip),
PaintCall::Path { .. } if method == CanvasMethod::DrawLine => Reading::Could,
PaintCall::Path { .. } | PaintCall::Arc { .. } => is(method == CanvasMethod::DrawPath),
PaintCall::Line { .. } => is(method == CanvasMethod::DrawLine),
PaintCall::Rect { .. } if method == CanvasMethod::DrawRect => Reading::Could,
PaintCall::Rect { .. } => Reading::Not,
PaintCall::RRect { rrect: None, .. } => match method {
CanvasMethod::DrawRRect => Reading::Is,
CanvasMethod::DrawRect | CanvasMethod::DrawCircle | CanvasMethod::DrawPath => {
Reading::Could
}
_ => Reading::Not,
},
PaintCall::RRect {
rrect: Some(rrect), ..
} => match drawn_rrect_method(*rrect) {
drawn if drawn != method => Reading::Not,
CanvasMethod::DrawRRect => Reading::Is,
_ => Reading::Could,
},
PaintCall::DRRect { .. } => is(method == CanvasMethod::DrawDRRect),
PaintCall::Circle { .. } => is(method == CanvasMethod::DrawCircle),
PaintCall::Shadow { .. } => is(method == CanvasMethod::DrawShadow),
PaintCall::Paragraph => is(method == CanvasMethod::DrawParagraph),
PaintCall::Something(_) => Reading::Is,
}
}
fn takes(&self, op: &Op) -> bool {
match self.reading(op) {
Reading::Is => true,
Reading::Could => self.matches(op),
Reading::Not => false,
}
}
fn matches(&self, op: &Op) -> bool {
match (self, op) {
(PaintCall::Translate { x, y }, Op::Transform(matrix)) => {
let Transform::Translate(tx, ty) = Transform::of(matrix) else {
return false;
};
x.is_none_or(|x| near(tx, x)) && y.is_none_or(|y| near(ty, y))
}
(PaintCall::Scale { x, y }, op) => scale_of(op).is_some_and(|(sx, sy)| {
x.is_none_or(|x| near(sx, x)) && y.is_none_or(|y| near(sy, y))
}),
(PaintCall::Rotate { angle }, Op::Transform(matrix)) => {
let [a, b, ..] = matrix.to_affine().map(f64::from);
angle.is_none_or(|angle| near(b.atan2(a), angle))
}
(
PaintCall::Path {
color,
style,
includes,
excludes,
},
Op::DrawPath { path, paint, .. },
) => {
let covers = |point: &Offset| path.contains((*point).into(), FillRule::NonZero);
color_matches(paint.color, *color)
&& style_matches(*style, &paint.style)
&& includes.iter().all(covers)
&& !excludes.iter().any(covers)
}
(
PaintCall::Line {
p1,
p2,
color,
stroke_width,
},
Op::DrawPath { path, paint, .. },
) => {
let Some((start, end)) = line_of(path) else {
return false;
};
color_matches(paint.color, *color)
&& p1.is_none_or(|p1| same_point(start, p1))
&& p2.is_none_or(|p2| same_point(end, p2))
&& stroke_width.is_none_or(|width| {
stroke_width_of(&paint.style).is_some_and(|w| f32_is(w, width))
})
}
(
PaintCall::Arc {
rect,
color,
stroke_width,
stroke_cap,
},
Op::DrawPath { path, paint, .. },
) => {
let PaintStyle::Stroke(stroke) = &paint.style else {
return false;
};
color_matches(paint.color, *color)
&& stroke_width.is_none_or(|width| f32_is(f64::from(stroke.width), width))
&& stroke_cap.is_none_or(|cap| stroke.cap == cap)
&& rect.is_none_or(|rect| runs_along_oval(path, rect))
}
(
PaintCall::Rect { rect, color },
Op::DrawRect {
rect: painted,
paint,
..
},
) => {
color_matches(paint.color, *color)
&& rect.is_none_or(|rect| valo::Rect::from(rect) == *painted)
}
(
PaintCall::DRRect {
outer,
inner,
color,
style,
},
Op::DrawPath { path, paint, .. },
) => {
color_matches(paint.color, *color)
&& style_matches(*style, &paint.style)
&& outer
.zip(*inner)
.is_none_or(|(outer, inner)| same_ring(path, outer, inner))
}
(
PaintCall::RRect {
rrect,
color,
style,
has_mask_filter,
},
Op::DrawPath { path, paint, .. },
) => {
color_matches(paint.color, *color)
&& style_matches(*style, &paint.style)
&& mask_matches(*has_mask_filter, paint)
&& rrect.is_none_or(|rrect| same_round_rect(path, rrect))
}
(
PaintCall::RRect {
rrect,
color,
style,
has_mask_filter,
},
Op::DrawRect { rect, paint, .. },
) => {
color_matches(paint.color, *color)
&& style_matches(*style, &paint.style)
&& mask_matches(*has_mask_filter, paint)
&& rrect
.is_none_or(|rrect| same_rect(valo::Rect::from(rrect.outer_rect()), *rect))
}
(
PaintCall::RRect {
rrect,
color,
style,
has_mask_filter,
},
Op::RRectBlur {
rect, radii, paint, ..
},
) => {
color_matches(paint.color, *color)
&& style_matches(*style, &paint.style)
&& mask_matches(*has_mask_filter, paint)
&& rrect.is_none_or(|rrect| same_blurred_round_rect(*rect, *radii, rrect))
}
(
PaintCall::Rect { rect, color },
Op::RRectBlur {
rect: painted,
paint,
..
},
) => {
color_matches(paint.color, *color)
&& rect.is_none_or(|rect| valo::Rect::from(rect) == *painted)
}
(
PaintCall::Circle {
x,
y,
radius,
color,
style,
},
Op::DrawPath { path, paint, .. },
) => {
let Some(oval) = path.as_oval() else {
return false;
};
color_matches(paint.color, *color)
&& style_matches(*style, &paint.style)
&& x.is_none_or(|x| f32_is(f64::from(oval.x + oval.width / 2.0), x))
&& y.is_none_or(|y| f32_is(f64::from(oval.y + oval.height / 2.0), y))
&& radius.is_none_or(|radius| f32_is(f64::from(oval.width / 2.0), radius))
}
(
PaintCall::Shadow { elevation, color },
Op::DrawShadow {
color: shadow,
elevation: shadow_elevation,
..
},
) => {
color_matches(*shadow, *color)
&& elevation
.is_none_or(|elevation| f32_is(f64::from(*shadow_elevation), elevation))
}
(PaintCall::Save, _)
| (PaintCall::Restore, _)
| (PaintCall::Clip, _)
| (PaintCall::Paragraph, _) => true,
_ => false,
}
}
}
fn mask_matches(expected: Option<bool>, paint: &valo::Paint) -> bool {
expected.is_none_or(|expected| paint.mask_blur.is_some() == expected)
}
fn style_matches(expected: Option<PaintedStyle>, style: &PaintStyle) -> bool {
expected.is_none_or(|expected| expected.matches(style))
}
fn same_point(a: Offset, b: Offset) -> bool {
f32_is(a.dx(), b.dx()) && f32_is(a.dy(), b.dy())
}
fn runs_along_oval(path: &valo::Path, rect: Rect) -> bool {
let center = rect.center();
let (radius_x, radius_y) = (rect.width() / 2.0, rect.height() / 2.0);
let contours = path.measure(0.01);
!contours.is_empty()
&& contours.iter().all(|contour| {
(0..=10).all(|step| {
let point = contour
.sample(contour.length() * step as f32 / 10.0)
.position;
let x = (f64::from(point.x) - center.dx()) / radius_x;
let y = (f64::from(point.y) - center.dy()) / radius_y;
(x * x + y * y - 1.0).abs() < 1e-3
})
})
}
fn scale_of(op: &Op) -> Option<(f64, f64)> {
match op {
Op::Transform(matrix) => Transform::of(matrix).scale(),
_ => None,
}
}
fn same_round_rect(path: &valo::Path, rrect: RRect) -> bool {
let mut canvas = waft_embedder::Canvas::new();
waft_painting::draw_rrect(&mut canvas, rrect, &waft_embedder::Paint::default());
let expected = canvas.build();
let Some(Op::DrawPath { path: expected, .. }) = expected.ops().first() else {
return false;
};
match (round_rect_of(path), round_rect_of(expected)) {
(Some(recorded), Some(expected)) => same_corners(recorded, expected),
_ => *path == **expected,
}
}
fn same_blurred_round_rect(rect: valo::Rect, radii: [f32; 4], rrect: RRect) -> bool {
let mut canvas = waft_embedder::Canvas::new();
waft_painting::draw_rrect(&mut canvas, rrect, &waft_embedder::Paint::default());
let expected = match canvas.build().ops().first() {
Some(Op::DrawPath { path, .. }) => round_rect_of(path),
Some(Op::DrawRect { rect, .. }) => Some((*rect, [[0.0; 2]; 4])),
_ => None,
};
expected.is_some_and(|expected| {
same_corners((rect, radii.map(|radius| [radius, radius])), expected)
})
}
fn same_corners(
(rect, radii): (valo::Rect, [[f32; 2]; 4]),
(expected_rect, expected_radii): (valo::Rect, [[f32; 2]; 4]),
) -> bool {
same_rect(rect, expected_rect)
&& radii
.iter()
.flatten()
.zip(expected_radii.iter().flatten())
.all(|(a, b)| within_rrect_eps(*a, *b))
}
fn round_rect_of(path: &valo::Path) -> Option<(valo::Rect, [[f32; 2]; 4])> {
path.as_round_rect().or_else(|| {
path.as_oval()
.map(|oval| (oval, [[oval.width / 2.0, oval.height / 2.0]; 4]))
})
}
fn same_rect(a: valo::Rect, b: valo::Rect) -> bool {
[
(a.x, b.x),
(a.y, b.y),
(a.right(), b.right()),
(a.bottom(), b.bottom()),
]
.iter()
.all(|(a, b)| within_rrect_eps(*a, *b))
}
fn within_rrect_eps(a: f32, b: f32) -> bool {
const EPS: f64 = 0.0001;
(f64::from(a) - f64::from(b)).abs() <= EPS
}
fn same_ring(path: &valo::Path, outer: RRect, inner: RRect) -> bool {
let mut canvas = waft_embedder::Canvas::new();
waft_painting::draw_drrect(&mut canvas, outer, inner, &waft_embedder::Paint::default());
let ring = canvas.build();
matches!(ring.ops().first(), Some(Op::DrawPath { path: expected, .. }) if **expected == *path)
}
fn drawn_rrect_method(rrect: RRect) -> CanvasMethod {
let mut canvas = waft_embedder::Canvas::new();
waft_painting::draw_rrect(&mut canvas, rrect, &waft_embedder::Paint::default());
canvas
.build()
.ops()
.first()
.map_or(CanvasMethod::Other, CanvasMethod::of)
}
fn f32_is(recorded: f64, expected: f64) -> bool {
(recorded - expected).abs() <= f64::from(f32::EPSILON) * expected.abs().max(1.0)
}
fn near(actual: f64, expected: f64) -> bool {
(actual - expected).abs() < 1e-4
}
fn color_matches(painted: valo::Color, expected: Option<Color>) -> bool {
expected.is_none_or(|expected| colors_match(painted, expected))
}
pub fn colors_match(painted: valo::Color, expected: Color) -> bool {
const LIMIT: f64 = 1.0 / 255.0;
(f64::from(painted.a) - expected.a).abs() < LIMIT
&& (f64::from(painted.r) - expected.r).abs() < LIMIT
&& (f64::from(painted.g) - expected.g).abs() < LIMIT
&& (f64::from(painted.b) - expected.b).abs() < LIMIT
}
#[derive(Clone, Debug)]
pub struct PaintMismatch {
pub step: usize,
pub expected: String,
pub found: Option<(usize, RecordedCall)>,
pub reason: String,
pub log: Vec<RecordedCall>,
}
impl fmt::Display for PaintMismatch {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(
f,
"the paints pattern failed at step {} ({}): {}",
self.step, self.expected, self.reason
)?;
match &self.found {
Some((index, call)) => writeln!(f, "found: #{index} {call:?}")?,
None => writeln!(f, "found: no call left")?,
}
writeln!(f, "log:")?;
for (index, call) in self.log.iter().enumerate() {
writeln!(f, " #{index} {call:?}")?;
}
Ok(())
}
}
impl fmt::Debug for PaintCall {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PaintCall::Save => f.write_str("save"),
PaintCall::Restore => f.write_str("restore"),
PaintCall::Translate { x, y } => write!(f, "translate(x: {x:?}, y: {y:?})"),
PaintCall::Scale { x, y } => write!(f, "scale(x: {x:?}, y: {y:?})"),
PaintCall::Rotate { angle } => write!(f, "rotate(angle: {angle:?})"),
PaintCall::Clip => f.write_str("clip"),
PaintCall::Path {
color,
style,
includes,
excludes,
} => write!(
f,
"path(color: {color:?}, style: {style:?}, includes: {includes:?}, \
excludes: {excludes:?})"
),
PaintCall::Line {
p1,
p2,
color,
stroke_width,
} => write!(
f,
"line(p1: {p1:?}, p2: {p2:?}, color: {color:?}, stroke_width: {stroke_width:?})"
),
PaintCall::Arc {
rect,
color,
stroke_width,
stroke_cap,
} => write!(
f,
"arc(rect: {rect:?}, color: {color:?}, stroke_width: {stroke_width:?}, \
stroke_cap: {stroke_cap:?})"
),
PaintCall::Rect { rect, color } => write!(f, "rect(rect: {rect:?}, color: {color:?})"),
PaintCall::RRect {
rrect,
color,
style,
has_mask_filter,
} => write!(
f,
"rrect(rrect: {rrect:?}, color: {color:?}, style: {style:?}, \
has_mask_filter: {has_mask_filter:?})"
),
PaintCall::DRRect {
outer,
inner,
color,
style,
} => write!(
f,
"drrect(outer: {outer:?}, inner: {inner:?}, color: {color:?}, style: {style:?})"
),
PaintCall::Circle {
x,
y,
radius,
color,
style,
} => write!(
f,
"circle(x: {x:?}, y: {y:?}, radius: {radius:?}, color: {color:?}, \
style: {style:?})"
),
PaintCall::Shadow { elevation, color } => {
write!(f, "shadow(elevation: {elevation:?}, color: {color:?})")
}
PaintCall::Paragraph => f.write_str("paragraph"),
PaintCall::SaveRestore => f.write_str("saveRestore"),
PaintCall::Something(_) => f.write_str("something"),
}
}
}
pub fn paints(ops: &[Op], pattern: &[PaintCall]) -> Result<(), Box<PaintMismatch>> {
let log: Vec<RecordedCall> = ops.iter().map(RecordedCall::of).collect();
let mismatch = |step: usize, found: Option<usize>, reason: &str| {
Box::new(PaintMismatch {
step,
expected: format!("{:?}", pattern[step]),
found: found.map(|index| (index, log[index].clone())),
reason: reason.to_string(),
log: log.clone(),
})
};
if ops.is_empty() || pattern.is_empty() {
return Err(Box::new(PaintMismatch {
step: 0,
expected: format!("{pattern:?}"),
found: None,
reason: if ops.is_empty() {
"it painted nothing"
} else {
"it painted something, but the pattern has no steps"
}
.to_string(),
log,
}));
}
let mut index = 0;
for (step, expected) in pattern.iter().enumerate() {
match expected {
PaintCall::SaveRestore => {
index = next_call(ops, index, expected)
.ok_or_else(|| mismatch(step, None, "no save was called"))?;
let mut depth = 1;
while depth > 0 {
index += 1;
match ops.get(index).map(CanvasMethod::of) {
None => return Err(mismatch(step, None, "no restore balances the save")),
Some(CanvasMethod::Save) => depth += 1,
Some(CanvasMethod::Restore) => depth -= 1,
Some(_) => {}
}
}
index += 1;
}
PaintCall::Something(predicate) => loop {
let Some(call) = log.get(index) else {
return Err(mismatch(step, None, "nothing matched the predicate"));
};
let answer =
predicate(call).map_err(|reason| mismatch(step, Some(index), &reason))?;
index += 1;
if answer {
break;
}
},
_ => {
let found = next_call(ops, index, expected)
.ok_or_else(|| mismatch(step, None, "the method was not called"))?;
if !expected.matches(&ops[found]) {
return Err(mismatch(step, Some(found), "the call has other arguments"));
}
index = found + 1;
}
}
}
Ok(())
}
fn next_call(ops: &[Op], from: usize, step: &PaintCall) -> Option<usize> {
(from..ops.len()).find(|&index| step.takes(&ops[index]))
}
#[track_caller]
pub fn assert_paints(ops: &[Op], pattern: &[PaintCall]) {
if let Err(mismatch) = paints(ops, pattern) {
panic!("{mismatch}");
}
}
pub fn paints_exactly(ops: &[Op], pattern: &[PaintCall]) -> Result<(), Box<PaintMismatch>> {
let log: Vec<RecordedCall> = ops.iter().map(RecordedCall::of).collect();
let mismatch = |step: usize, found: Option<usize>, expected: String, reason: &str| {
Box::new(PaintMismatch {
step,
expected,
found: found.map(|index| (index, log[index].clone())),
reason: reason.to_string(),
log: log.clone(),
})
};
for (step, expected) in pattern.iter().enumerate() {
let written = format!("{expected:?}");
let Some(op) = ops.get(step) else {
return Err(mismatch(step, None, written, "it painted fewer calls"));
};
let fits = match expected {
PaintCall::Something(predicate) => predicate(&log[step])
.map_err(|reason| mismatch(step, Some(step), written.clone(), &reason))?,
PaintCall::SaveRestore => {
return Err(mismatch(
step,
Some(step),
written,
"saveRestore spans calls",
));
}
_ => expected.takes(op) && expected.matches(op),
};
if !fits {
return Err(mismatch(step, Some(step), written, "the call differs"));
}
}
if ops.len() > pattern.len() {
return Err(Box::new(PaintMismatch {
step: pattern.len(),
expected: "nothing more".to_string(),
found: Some((pattern.len(), log[pattern.len()].clone())),
reason: "it painted more calls".to_string(),
log,
}));
}
Ok(())
}
pub fn draws(ops: &[Op]) -> Vec<Op> {
ops.iter()
.filter(|op| {
!matches!(
CanvasMethod::of(op),
CanvasMethod::Save
| CanvasMethod::Restore
| CanvasMethod::Translate
| CanvasMethod::Scale
| CanvasMethod::Rotate
| CanvasMethod::Clip
)
})
.cloned()
.collect()
}
pub fn count_calls(ops: &[Op], method: CanvasMethod) -> usize {
ops.iter()
.filter(|op| CanvasMethod::of(op) == method)
.count()
}