1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::{utils::max_optional_size, *};
pub struct ExpandToPreferredHeight<E: Element>(pub E);
impl<E: Element> Element for ExpandToPreferredHeight<E> {
fn first_location_usage(&self, ctx: FirstLocationUsageCtx) -> FirstLocationUsage {
self.0.first_location_usage(ctx)
}
fn measure(&self, ctx: MeasureCtx) -> ElementSize {
self.0.measure(ctx)
}
fn draw(&self, ctx: DrawCtx) -> ElementSize {
let preferred_height = ctx.preferred_height;
let preferred_breaks = ctx
.breakable
.as_ref()
.map(|b| b.preferred_height_break_count)
.unwrap_or(0);
let size;
let height;
if let Some(breakable) = ctx.breakable {
let mut break_count = 0;
size = self.0.draw(DrawCtx {
pdf: ctx.pdf,
breakable: Some(BreakableDraw {
do_break: &mut |pdf, location_idx, _height| {
break_count = break_count.max(location_idx + 1);
(breakable.do_break)(
pdf,
location_idx,
// we also expand all of the heights
if location_idx == 0 {
Some(ctx.first_height)
} else {
Some(breakable.full_height)
},
)
},
..breakable
}),
..ctx
});
match break_count.cmp(&preferred_breaks) {
std::cmp::Ordering::Less => {
// We need to go through all of the locations to use the full_height on all of
// them.
for i in break_count..preferred_breaks {
(breakable.do_break)(
ctx.pdf,
i,
if i == 0 {
Some(ctx.first_height)
} else {
Some(breakable.full_height)
},
);
}
height = preferred_height;
}
std::cmp::Ordering::Equal => {
height = max_optional_size(size.height, preferred_height);
}
std::cmp::Ordering::Greater => {
height = size.height;
}
}
} else {
size = self.0.draw(ctx);
height = max_optional_size(size.height, preferred_height);
}
ElementSize { height, ..size }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
elements::text::Text, fonts::builtin::BuiltinFont, test_utils::binary_snapshots::*,
};
use insta::*;
#[test]
fn test_basic() {
let bytes = test_element_bytes(
TestElementParams {
preferred_height: Some(12.),
breakable: Some(TestElementParamsBreakable {
preferred_height_break_count: 7,
full_height: TestElementParams::DEFAULT_FULL_HEIGHT,
}),
..TestElementParams::breakable()
},
|mut callback| {
let font = BuiltinFont::courier(callback.pdf());
let content = Text::new(LOREM_IPSUM, &font, 32.);
let content = content.debug(1);
callback.call(&ExpandToPreferredHeight(content).debug(0));
},
);
assert_binary_snapshot!(".pdf", bytes);
}
#[test]
fn test_single_location_content() {
let bytes = test_element_bytes(
TestElementParams {
preferred_height: Some(32.),
breakable: Some(TestElementParamsBreakable {
preferred_height_break_count: 3,
full_height: TestElementParams::DEFAULT_FULL_HEIGHT,
}),
..TestElementParams::breakable()
},
|mut callback| {
let font = BuiltinFont::courier(callback.pdf());
let content = Text::new(LOREM_IPSUM, &font, 12.);
let content = content.debug(1);
callback.call(&ExpandToPreferredHeight(content).debug(0));
},
);
assert_binary_snapshot!(".pdf", bytes);
}
// TODO: Figure out what makes sense here.
// #[test]
// fn test_collapse() {
// let output = test_element(
// TestElementParams {
// width: WidthConstraint {
// max: 20.,
// expand: true,
// },
// first_height: 21.,
// preferred_height: Some(12.),
// breakable: Some(TestElementParamsBreakable {
// preferred_height_break_count: 7,
// full_height: 500.,
// }),
// pos: (11., 600.0),
// ..Default::default()
// },
// |assert, callback| {
// let content = RecordPasses::new(NoneElement);
// let element = ExpandToPreferredHeight(&content);
// let ret = callback.call(element);
// if assert {
// assert_debug_snapshot!(content.into_passes());
// }
// ret
// },
// );
// assert_debug_snapshot!(output);
// }
}