Skip to content

Commit b20cda8

Browse files
committed
Convert gentests to use custom Measurable
1 parent 0821d80 commit b20cda8

File tree

926 files changed

+1880
-2770
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

926 files changed

+1880
-2770
lines changed

scripts/gentest/src/main.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ fn generate_test(name: impl AsRef<str>, description: &Value) -> TokenStream {
216216
fn #name() {
217217
#[allow(unused_imports)]
218218
use taffy::{tree::{Layout, MeasureFunc}, prelude::*, Taffy};
219-
let mut taffy : Taffy<MeasureFunc<()>> = Taffy::new();
219+
let mut taffy : Taffy<crate::TextMeasure> = Taffy::new();
220220
#set_rounding_mode
221221
#node_description
222222
taffy.compute_layout(node, #available_space).unwrap();
@@ -879,9 +879,10 @@ fn generate_measure_function(text_content: &str, writing_mode: Option<&str>, asp
879879
};
880880

881881
quote!(
882-
taffy::tree::MeasureFunc::Raw(|known_dimensions, available_space, _context| {
883-
const TEXT : &str = #text_content;
884-
crate::measure_standard_text(known_dimensions, available_space, TEXT, #writing_mode_token, #aspect_ratio_token)
885-
})
882+
crate::TextMeasure {
883+
text_content: #text_content,
884+
writing_mode: #writing_mode_token,
885+
_aspect_ratio: #aspect_ratio_token,
886+
}
886887
)
887888
}

tests/fixtures.rs

Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,74 @@ enum WritingMode {
1111
}
1212

1313
#[allow(dead_code)]
14-
fn measure_standard_text(
15-
known_dimensions: taffy::geometry::Size<Option<f32>>,
16-
available_space: taffy::geometry::Size<taffy::style::AvailableSpace>,
17-
text_content: &str,
14+
struct TextMeasure {
15+
text_content: &'static str,
1816
writing_mode: WritingMode,
1917
_aspect_ratio: Option<f32>,
20-
) -> taffy::geometry::Size<f32> {
21-
use taffy::geometry::AbsoluteAxis;
22-
use taffy::prelude::*;
23-
const ZWS: char = '\u{200B}';
24-
const H_WIDTH: f32 = 10.0;
25-
const H_HEIGHT: f32 = 10.0;
18+
}
2619

27-
if let Size { width: Some(width), height: Some(height) } = known_dimensions {
28-
return Size { width, height };
29-
}
20+
#[allow(dead_code)]
21+
impl taffy::tree::Measurable for TextMeasure {
22+
type Context = ();
3023

31-
let inline_axis = match writing_mode {
32-
WritingMode::Horizontal => AbsoluteAxis::Horizontal,
33-
WritingMode::Vertical => AbsoluteAxis::Vertical,
34-
};
35-
let block_axis = inline_axis.other_axis();
24+
fn measure(
25+
&mut self,
26+
known_dimensions: taffy::geometry::Size<Option<f32>>,
27+
available_space: taffy::geometry::Size<taffy::style::AvailableSpace>,
28+
_context: &mut (),
29+
) -> taffy::geometry::Size<f32> {
30+
use taffy::geometry::AbsoluteAxis;
31+
use taffy::prelude::*;
3632

37-
let lines: Vec<&str> = text_content.split(ZWS).collect();
38-
if lines.is_empty() {
39-
return Size::ZERO;
40-
}
33+
const ZWS: char = '\u{200B}';
34+
const H_WIDTH: f32 = 10.0;
35+
const H_HEIGHT: f32 = 10.0;
36+
37+
if let Size { width: Some(width), height: Some(height) } = known_dimensions {
38+
return Size { width, height };
39+
}
40+
41+
let inline_axis = match self.writing_mode {
42+
WritingMode::Horizontal => AbsoluteAxis::Horizontal,
43+
WritingMode::Vertical => AbsoluteAxis::Vertical,
44+
};
45+
let block_axis = inline_axis.other_axis();
46+
let lines: Vec<&str> = self.text_content.split(ZWS).collect();
4147

42-
let min_line_length: usize = lines.iter().map(|line| line.len()).max().unwrap_or(0);
43-
let max_line_length: usize = lines.iter().map(|line| line.len()).sum();
44-
let inline_size =
45-
known_dimensions.get_abs(inline_axis).unwrap_or_else(|| match available_space.get_abs(inline_axis) {
46-
AvailableSpace::MinContent => min_line_length as f32 * H_WIDTH,
47-
AvailableSpace::MaxContent => max_line_length as f32 * H_WIDTH,
48-
AvailableSpace::Definite(inline_size) => {
49-
inline_size.min(max_line_length as f32 * H_WIDTH).max(min_line_length as f32 * H_WIDTH)
48+
if lines.is_empty() {
49+
return Size::ZERO;
50+
}
51+
52+
let min_line_length: usize = lines.iter().map(|line| line.len()).max().unwrap_or(0);
53+
let max_line_length: usize = lines.iter().map(|line| line.len()).sum();
54+
let inline_size =
55+
known_dimensions.get_abs(inline_axis).unwrap_or_else(|| match available_space.get_abs(inline_axis) {
56+
AvailableSpace::MinContent => min_line_length as f32 * H_WIDTH,
57+
AvailableSpace::MaxContent => max_line_length as f32 * H_WIDTH,
58+
AvailableSpace::Definite(inline_size) => {
59+
inline_size.min(max_line_length as f32 * H_WIDTH).max(min_line_length as f32 * H_WIDTH)
60+
}
61+
});
62+
let block_size = known_dimensions.get_abs(block_axis).unwrap_or_else(|| {
63+
let inline_line_length = (inline_size / H_WIDTH).floor() as usize;
64+
let mut line_count = 1;
65+
let mut current_line_length = 0;
66+
for line in &lines {
67+
if current_line_length + line.len() > inline_line_length {
68+
if current_line_length > 0 {
69+
line_count += 1
70+
};
71+
current_line_length = line.len();
72+
} else {
73+
current_line_length += line.len();
74+
};
5075
}
76+
(line_count as f32) * H_HEIGHT
5177
});
52-
let block_size = known_dimensions.get_abs(block_axis).unwrap_or_else(|| {
53-
let inline_line_length = (inline_size / H_WIDTH).floor() as usize;
54-
let mut line_count = 1;
55-
let mut current_line_length = 0;
56-
for line in &lines {
57-
if current_line_length + line.len() > inline_line_length {
58-
if current_line_length > 0 {
59-
line_count += 1
60-
};
61-
current_line_length = line.len();
62-
} else {
63-
current_line_length += line.len();
64-
};
65-
}
66-
(line_count as f32) * H_HEIGHT
67-
});
6878

69-
match writing_mode {
70-
WritingMode::Horizontal => Size { width: inline_size, height: block_size },
71-
WritingMode::Vertical => Size { width: block_size, height: inline_size },
79+
match self.writing_mode {
80+
WritingMode::Horizontal => Size { width: inline_size, height: block_size },
81+
WritingMode::Vertical => Size { width: block_size, height: inline_size },
82+
}
7283
}
7384
}

tests/generated/block/block_absolute_aspect_ratio_aspect_ratio_overrides_height_of_full_inset.rs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/generated/block/block_absolute_aspect_ratio_fill_height.rs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/generated/block/block_absolute_aspect_ratio_fill_height_from_inset.rs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/generated/block/block_absolute_aspect_ratio_fill_max_height.rs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/generated/block/block_absolute_aspect_ratio_fill_max_width.rs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/generated/block/block_absolute_aspect_ratio_fill_min_height.rs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/generated/block/block_absolute_aspect_ratio_fill_min_width.rs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/generated/block/block_absolute_aspect_ratio_fill_width.rs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)