Skip to content

Commit

Permalink
Convert gentests to use custom Measurable
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Aug 14, 2023
1 parent 3d913f3 commit 6e408a6
Show file tree
Hide file tree
Showing 926 changed files with 1,937 additions and 2,821 deletions.
123 changes: 67 additions & 56 deletions scripts/gentest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ fn generate_test(name: impl AsRef<str>, description: &Value) -> TokenStream {
fn #name() {
#[allow(unused_imports)]
use taffy::{tree::{Layout, MeasureFunc}, prelude::*, Taffy};
let mut taffy : Taffy<MeasureFunc<()>> = Taffy::new();
let mut taffy : Taffy<super::TextMeasure> = Taffy::new();
#set_rounding_mode
#node_description
taffy.compute_layout(node, #available_space).unwrap();
Expand Down Expand Up @@ -870,66 +870,76 @@ fn generate_generic_measure_function() -> TokenStream {
Vertical,
}

// WARNING: This function is generated by the gentest script. Do not edit directly
#[allow(dead_code)]
fn measure_standard_text(
known_dimensions: taffy::geometry::Size<Option<f32>>,
available_space: taffy::geometry::Size<taffy::style::AvailableSpace>,
text_content: &str,
struct TextMeasure {
text_content: &'static str,
writing_mode: WritingMode,
_aspect_ratio: Option<f32>,
) -> taffy::geometry::Size<f32> {
use taffy::geometry::AbsoluteAxis;
use taffy::prelude::*;
const ZWS: char = '\u{200B}';
const H_WIDTH: f32 = 10.0;
const H_HEIGHT: f32 = 10.0;

if let Size { width: Some(width), height: Some(height) } = known_dimensions {
return Size { width, height };
}
}

let inline_axis = match writing_mode {
WritingMode::Horizontal => AbsoluteAxis::Horizontal,
WritingMode::Vertical => AbsoluteAxis::Vertical,
};
let block_axis = inline_axis.other_axis();
// WARNING: This function is generated by the gentest script. Do not edit directly
#[allow(dead_code)]
impl taffy::tree::Measurable for TextMeasure {
type Context = ();

fn measure(
&mut self,
known_dimensions: taffy::geometry::Size<Option<f32>>,
available_space: taffy::geometry::Size<taffy::style::AvailableSpace>,
_context: &mut (),
) -> taffy::geometry::Size<f32> {
use taffy::geometry::AbsoluteAxis;
use taffy::prelude::*;
const ZWS: char = '\u{200B}';
const H_WIDTH: f32 = 10.0;
const H_HEIGHT: f32 = 10.0;

if let Size { width: Some(width), height: Some(height) } = known_dimensions {
return Size { width, height };
}

let lines: Vec<&str> = text_content.split(ZWS).collect();
if lines.is_empty() {
return Size::ZERO;
}
let inline_axis = match self.writing_mode {
WritingMode::Horizontal => AbsoluteAxis::Horizontal,
WritingMode::Vertical => AbsoluteAxis::Vertical,
};
let block_axis = inline_axis.other_axis();

let min_line_length: usize = lines.iter().map(|line| line.len()).max().unwrap_or(0);
let max_line_length: usize = lines.iter().map(|line| line.len()).sum();
let inline_size =
known_dimensions.get_abs(inline_axis).unwrap_or_else(|| match available_space.get_abs(inline_axis) {
AvailableSpace::MinContent => min_line_length as f32 * H_WIDTH,
AvailableSpace::MaxContent => max_line_length as f32 * H_WIDTH,
AvailableSpace::Definite(inline_size) => {
inline_size.min(max_line_length as f32 * H_WIDTH).max(min_line_length as f32 * H_WIDTH)
let lines: Vec<&str> = self.text_content.split(ZWS).collect();
if lines.is_empty() {
return Size::ZERO;
}

let min_line_length: usize = lines.iter().map(|line| line.len()).max().unwrap_or(0);
let max_line_length: usize = lines.iter().map(|line| line.len()).sum();
let inline_size = known_dimensions.get_abs(inline_axis).unwrap_or_else(|| {
match available_space.get_abs(inline_axis) {
AvailableSpace::MinContent => min_line_length as f32 * H_WIDTH,
AvailableSpace::MaxContent => max_line_length as f32 * H_WIDTH,
AvailableSpace::Definite(inline_size) => {
inline_size.min(max_line_length as f32 * H_WIDTH).max(min_line_length as f32 * H_WIDTH)
}
}
});
let block_size = known_dimensions.get_abs(block_axis).unwrap_or_else(|| {
let inline_line_length = (inline_size / H_WIDTH).floor() as usize;
let mut line_count = 1;
let mut current_line_length = 0;
for line in &lines {
if current_line_length + line.len() > inline_line_length {
if current_line_length > 0 {
line_count += 1
let block_size = known_dimensions.get_abs(block_axis).unwrap_or_else(|| {
let inline_line_length = (inline_size / H_WIDTH).floor() as usize;
let mut line_count = 1;
let mut current_line_length = 0;
for line in &lines {
if current_line_length + line.len() > inline_line_length {
if current_line_length > 0 {
line_count += 1
};
current_line_length = line.len();
} else {
current_line_length += line.len();
};
current_line_length = line.len();
} else {
current_line_length += line.len();
};
}
(line_count as f32) * H_HEIGHT
});
}
(line_count as f32) * H_HEIGHT
});

match writing_mode {
WritingMode::Horizontal => Size { width: inline_size, height: block_size },
WritingMode::Vertical => Size { width: block_size, height: inline_size },
match self.writing_mode {
WritingMode::Horizontal => Size { width: inline_size, height: block_size },
WritingMode::Vertical => Size { width: block_size, height: inline_size },
}
}
}
)
Expand All @@ -947,9 +957,10 @@ fn generate_measure_function(text_content: &str, writing_mode: Option<&str>, asp
};

quote!(
taffy::tree::MeasureFunc::Raw(|known_dimensions, available_space, _context| {
const TEXT : &str = #text_content;
super::measure_standard_text(known_dimensions, available_space, TEXT, #writing_mode_token, #aspect_ratio_token)
})
super::TextMeasure {
text_content: #text_content,
writing_mode: #writing_mode_token,
_aspect_ratio: #aspect_ratio_token,
}
)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/generated/absolute_aspect_ratio_fill_height.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/generated/absolute_aspect_ratio_fill_max_height.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/generated/absolute_aspect_ratio_fill_max_width.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/generated/absolute_aspect_ratio_fill_min_height.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/generated/absolute_aspect_ratio_fill_min_width.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/generated/absolute_aspect_ratio_fill_width.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 6 additions & 11 deletions tests/generated/absolute_child_with_cross_margin.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/generated/absolute_child_with_main_margin.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/generated/absolute_child_with_max_height.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/generated/absolute_layout_align_items_center.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/generated/absolute_layout_child_order.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6e408a6

Please sign in to comment.