Skip to main content

laser_pdf/test_utils/
fake_image.rs

1use crate::*;
2
3pub struct FakeImage {
4    pub width: f32,
5    pub height: f32,
6}
7
8impl FakeImage {
9    fn size(&self, width: WidthConstraint) -> (f32, ElementSize) {
10        let width = width.constrain(self.width);
11
12        let scale = width / self.width;
13        let size = (width, self.height * scale);
14
15        (
16            size.1,
17            ElementSize {
18                width: Some(size.0),
19                height: Some(size.1),
20            },
21        )
22    }
23}
24
25impl Element for FakeImage {
26    fn first_location_usage(&self, ctx: FirstLocationUsageCtx) -> FirstLocationUsage {
27        if ctx.break_appropriate_for_min_height(self.size(ctx.width).0) {
28            FirstLocationUsage::WillSkip
29        } else {
30            FirstLocationUsage::WillUse
31        }
32    }
33
34    fn measure(&self, mut ctx: MeasureCtx) -> ElementSize {
35        let (height, size) = self.size(ctx.width);
36        ctx.break_if_appropriate_for_min_height(height);
37        size
38    }
39
40    fn draw(&self, mut ctx: DrawCtx) -> ElementSize {
41        let (height, size) = self.size(ctx.width);
42        ctx.break_if_appropriate_for_min_height(height);
43        size
44    }
45}