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 23, 2023
1 parent 0821d80 commit b20cda8
Show file tree
Hide file tree
Showing 926 changed files with 1,880 additions and 2,770 deletions.
11 changes: 6 additions & 5 deletions scripts/gentest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,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<crate::TextMeasure> = Taffy::new();
#set_rounding_mode
#node_description
taffy.compute_layout(node, #available_space).unwrap();
Expand Down Expand Up @@ -879,9 +879,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;
crate::measure_standard_text(known_dimensions, available_space, TEXT, #writing_mode_token, #aspect_ratio_token)
})
crate::TextMeasure {
text_content: #text_content,
writing_mode: #writing_mode_token,
_aspect_ratio: #aspect_ratio_token,
}
)
}
109 changes: 60 additions & 49 deletions tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,74 @@ enum WritingMode {
}

#[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 };
}
#[allow(dead_code)]
impl taffy::tree::Measurable for TextMeasure {
type Context = ();

let inline_axis = match writing_mode {
WritingMode::Horizontal => AbsoluteAxis::Horizontal,
WritingMode::Vertical => AbsoluteAxis::Vertical,
};
let block_axis = inline_axis.other_axis();
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::*;

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

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)
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
};
current_line_length = line.len();
} else {
current_line_length += line.len();
};
}
(line_count as f32) * H_HEIGHT
});
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();
};
}
(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 },
}
}
}

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.

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/block/block_absolute_layout_child_order.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/block/block_absolute_layout_no_size.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.

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.

Loading

0 comments on commit b20cda8

Please sign in to comment.