Skip to main content

laser_pdf/test_utils/
frantic_jumper.rs

1use crate::*;
2
3pub struct FranticJumper {
4    pub jumps: Vec<(u32, Option<f32>)>,
5    pub size: ElementSize,
6}
7
8impl Element for FranticJumper {
9    fn measure(&self, ctx: MeasureCtx) -> ElementSize {
10        if let Some(b) = ctx.breakable {
11            *b.break_count = self
12                .jumps
13                .iter()
14                .map(|j| j.0)
15                .max()
16                .map(|m| m + 1)
17                .unwrap_or(0);
18        }
19
20        self.size
21    }
22
23    fn draw(&self, ctx: DrawCtx) -> ElementSize {
24        if let Some(b) = ctx.breakable {
25            let mut previous: Vec<Option<Location>> = vec![
26                None;
27                self.jumps
28                    .iter()
29                    .map(|j| j.0)
30                    .max()
31                    .map(|m| m as usize + 1)
32                    .unwrap_or(0)
33            ];
34
35            for &jump in &self.jumps {
36                let location = (b.do_break)(ctx.pdf, jump.0, jump.1);
37
38                if let Some(prev) = &previous[jump.0 as usize] {
39                    assert_eq!(prev.pos, location.pos);
40                    assert_eq!(prev.page_idx, location.page_idx);
41                    assert_eq!(prev.layer_idx, location.layer_idx);
42                } else {
43                    previous[jump.0 as usize] = Some(location);
44                }
45            }
46        }
47
48        self.size
49    }
50}