Skip to main content

laser_pdf/test_utils/
element_proxy.rs

1use crate::*;
2
3#[non_exhaustive]
4pub struct ElementProxy<'a, E: Element> {
5    pub element: E,
6    pub before_draw: &'a dyn Fn(&mut DrawCtx),
7    pub after_break: &'a dyn Fn(u32, &Location, WidthConstraint, f32),
8}
9
10impl<'a, E: Element> ElementProxy<'a, E> {
11    pub fn new(element: E) -> Self {
12        ElementProxy {
13            element,
14            before_draw: &|_| {},
15            after_break: &|_, _, _, _| {},
16        }
17    }
18}
19
20impl<'a, E: Element> Element for ElementProxy<'a, E> {
21    fn first_location_usage(&self, ctx: FirstLocationUsageCtx) -> FirstLocationUsage {
22        self.element.first_location_usage(ctx)
23    }
24
25    fn measure(&self, ctx: MeasureCtx) -> ElementSize {
26        self.element.measure(ctx)
27    }
28
29    fn draw(&self, mut ctx: DrawCtx) -> ElementSize {
30        (self.before_draw)(&mut ctx);
31
32        if let Some(breakable) = ctx.breakable {
33            self.element.draw(DrawCtx {
34                breakable: Some(BreakableDraw {
35                    do_break: &mut |pdf, location_idx, height| {
36                        let location = (breakable.do_break)(pdf, location_idx, height);
37
38                        (self.after_break)(location_idx, &location, ctx.width, ctx.first_height);
39
40                        location
41                    },
42                    ..breakable
43                }),
44                ..ctx
45            })
46        } else {
47            self.element.draw(ctx)
48        }
49    }
50}