Enable correct image measure functions (#661)
1// this declaration is necessary to "mount" the generated code where cargo can see it
2// this allows us to both keep code generation scoped to a singe directory for fs events
3// and to keep each test in a separate file
4mod generated;
5
6#[allow(dead_code)]
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8enum WritingMode {
9 Horizontal,
10 Vertical,
11}
12
13#[allow(dead_code)]
14struct TextMeasure {
15 text_content: &'static str,
16 writing_mode: WritingMode,
17 _aspect_ratio: Option<f32>,
18}
19
20#[allow(dead_code)]
21fn test_measure_function(
22 known_dimensions: taffy::Size<Option<f32>>,
23 available_space: taffy::Size<taffy::AvailableSpace>,
24 _node_id: taffy::NodeId,
25 node_context: Option<&mut TextMeasure>,
26 _style: &taffy::Style,
27) -> taffy::Size<f32> {
28 use taffy::prelude::*;
29 use taffy::AbsoluteAxis;
30
31 const ZWS: char = '\u{200B}';
32 const H_WIDTH: f32 = 10.0;
33 const H_HEIGHT: f32 = 10.0;
34
35 if let Size { width: Some(width), height: Some(height) } = known_dimensions {
36 return Size { width, height };
37 }
38
39 let Some(node_context) = node_context else { return Size::ZERO };
40
41 let inline_axis = match node_context.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> = node_context.text_content.split(ZWS).collect();
47
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 = known_dimensions
55 .get_abs(inline_axis)
56 .unwrap_or_else(|| match available_space.get_abs(inline_axis) {
57 AvailableSpace::MinContent => min_line_length as f32 * H_WIDTH,
58 AvailableSpace::MaxContent => max_line_length as f32 * H_WIDTH,
59 AvailableSpace::Definite(inline_size) => inline_size.min(max_line_length as f32 * H_WIDTH),
60 })
61 .max(min_line_length as f32 * H_WIDTH);
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 };
75 }
76 (line_count as f32) * H_HEIGHT
77 });
78
79 match node_context.writing_mode {
80 WritingMode::Horizontal => Size { width: inline_size, height: block_size },
81 WritingMode::Vertical => Size { width: block_size, height: inline_size },
82 }
83}