Skip to content

Commit 29ba47c

Browse files
committed
feat: refactor the text testing framework
1 parent 58729a3 commit 29ba47c

File tree

3 files changed

+162
-49
lines changed

3 files changed

+162
-49
lines changed

float-pigment-forest/tests/custom/css_inline/inline.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ fn inline() {
55
assert_xml!(
66
r#"
77
<div>
8-
<div expect_height="16" expect_width="375">hello</div>
8+
<div expect_height="16" expect_width="375">XX</div>
99
<div style="display: inline;" expect_width="32" expect_height="16">
10-
<div style="display: inline" expect_height="16">world</div>
10+
<div style="display: inline" expect_height="16">XX</div>
1111
</div>
1212
<div style="display: inline;" expect_width="64">
13-
hello <div style="display: inline" expect_left="32">world</div>
13+
XX <div style="display: inline" expect_left="32">XX</div>
1414
</div>
1515
</div>
1616
"#
@@ -76,10 +76,10 @@ fn inline_in_flexbox() {
7676
r#"
7777
<div>
7878
<div style="display: flex; flex-direction: column; height: 100px" expect_height="100" expect_width="375">
79-
<div style="display: inline" expect_height="16" expect_width="375">hello</div>
79+
<div style="display: inline" expect_height="16" expect_width="375">XX</div>
8080
</div>
8181
<div style="display: flex;" expect_height="16" expect_width="375">
82-
<div style="display: inline" expect_height="16" expect_width="32">hello</div>
82+
<div style="display: inline" expect_height="16" expect_width="32">XX</div>
8383
</div>
8484
</div>
8585
"#
@@ -92,7 +92,7 @@ fn inline_in_flexbox() {
9292
// r#"
9393
// <div>
9494
// <div style="display: inline;" expect_width="32" expect_height="16">
95-
// <div style="display: inline" expect_height="16">world</div>
95+
// <div style="display: inline" expect_height="16">XX</div>
9696
// </div>
9797
// </div>
9898
// "#
@@ -104,8 +104,8 @@ fn inline_block() {
104104
assert_xml!(
105105
r#"
106106
<div expect_height="16" expect_width="375">
107-
<div style="display: inline-block" expect_height="16" expect_width="32">hello</div>
108-
<div style="display: inline-block" expect_height="16" expect_width="32" expect_left="32">hello</div>
107+
<div style="display: inline-block" expect_height="16" expect_width="32">XX</div>
108+
<div style="display: inline-block" expect_height="16" expect_width="32" expect_left="32">XX</div>
109109
</div>
110110
"#
111111
)
@@ -128,7 +128,7 @@ fn inline_block_vertical_align_2() {
128128
assert_xml!(
129129
r#"
130130
<div expect_height="50" expect_width="375">
131-
<div style="display: inline-block; height: 40px; width: 20px;" expect_height="40" expect_top="34">hello</div>
131+
<div style="display: inline-block; height: 40px; width: 20px;" expect_height="40" expect_top="34">XX</div>
132132
<div style="display: inline-block; height: 50px; width: 20px" expect_height="50" expect_top="0" expect_left="20"></div>
133133
</div>
134134
"#
@@ -242,19 +242,19 @@ fn inline_block_in_flexbox() {
242242
r#"
243243
<div>
244244
<div style="display: flex; flex-direction: column; height: 100px" expect_height="100" expect_width="375">
245-
<div style="display: inline-block" expect_height="16" expect_width="375">hello</div>
245+
<div style="display: inline-block" expect_height="16" expect_width="375">XX</div>
246246
</div>
247247
<div style="display: flex;" expect_height="16" expect_width="375">
248-
<div style="display: inline-block" expect_height="16" expect_width="32">hello</div>
248+
<div style="display: inline-block" expect_height="16" expect_width="32">XX</div>
249249
</div>
250250
<div style="display: flex; flex-direction: column; align-items: center; width: 96px;" expect_height="16" expect_width="96">
251251
<div style="display: inline-block" expect_left="32" expect_height="16" expect_width="32">
252-
hello
252+
XX
253253
</div>
254254
</div>
255255
<div style="display: flex; flex-direction: row; align-items: center; justify-content: center; width: 96px;" expect_height="16" expect_width="96">
256256
<div style="display: inline-block" expect_left="32" expect_height="16" expect_width="32">
257-
hello
257+
XX
258258
</div>
259259
</div>
260260
</div>
@@ -269,7 +269,7 @@ fn block_in_inline_block() {
269269
<div>
270270
<div expect_width="375">
271271
<div style="display: inline-block" expect_height="16" expect_width="32">
272-
<div expect_height="16" expect_width="32">123</div>
272+
<div expect_height="16" expect_width="32">XX</div>
273273
</div>
274274
</div>
275275
</div>

float-pigment-forest/tests/custom/css_position.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ fn fixed_with_margin() {
108108
assert_xml!(
109109
r#"
110110
<div>
111-
<div style="position: fixed; margin-left: 100px; margin-top: 100px" expect_left="100" expect_top="100" expect_width="32" expect_height="16">hello</div>
111+
<div style="position: fixed; margin-left: 100px; margin-top: 100px" expect_left="100" expect_top="100" expect_width="32" expect_height="16">XX</div>
112112
</div>
113113
"#
114114
)

float-pigment-forest/tests/mod.rs

Lines changed: 147 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ pub use float_pigment_forest::Len;
1313
use float_pigment_forest::{layout::LayoutPosition, node::Length, *};
1414
use float_pigment_layout::DefLength;
1515
use float_pigment_mlp::{
16-
context::Context,
17-
context::Parse,
16+
context::{Context, Parse},
1817
node::{attribute::Attribute, NodeType},
1918
};
2019

@@ -98,6 +97,125 @@ impl Default for TestCtx {
9897
}
9998
}
10099

100+
#[inline(always)]
101+
fn is_block_tag(tag: &str) -> bool {
102+
tag == "div" || tag == "view"
103+
}
104+
105+
#[inline(always)]
106+
fn is_measure_text_slot(tag: &str) -> bool {
107+
tag == "text-slot"
108+
}
109+
#[derive(Debug, Default, Clone)]
110+
struct TextInfo {
111+
font_size: f32,
112+
// raw_text: String
113+
text_len: usize,
114+
}
115+
116+
impl TextInfo {
117+
fn measure(
118+
&self,
119+
min_width: Len,
120+
min_height: Len,
121+
max_width: Len,
122+
max_height: Len,
123+
max_content_width: Len,
124+
max_content_height: Len,
125+
) -> Size<Len> {
126+
let text_len = self.text_len;
127+
let text_width = self.font_size * text_len as f32;
128+
let max_w = max_width.min(max_content_width);
129+
let max_h = max_height.min(max_content_height);
130+
let measured_width;
131+
let measured_height;
132+
if text_width <= max_w {
133+
// single line
134+
measured_width = Len::from_f32(text_width);
135+
measured_height = Len::from_f32(self.font_size);
136+
} else {
137+
// multi line
138+
let mut row_count = (max_w.to_f32() / self.font_size).floor();
139+
if row_count < 1. {
140+
row_count = 1.;
141+
}
142+
let col_count = (text_len as f32 / row_count).ceil();
143+
measured_width = Len::from_f32((row_count * self.font_size) as i32 as f32);
144+
measured_height = Len::from_f32((col_count * self.font_size) as i32 as f32);
145+
}
146+
println!(
147+
"text_info: {:?}, width: {:?} ~ {:?}, height: {:?} ~ {:?}, max_content_width: {:?}, max_content_height: {:?}, measured_width: {:?}, measured_height: {:?}",
148+
self,
149+
min_width,
150+
max_width,
151+
min_height,
152+
max_height,
153+
max_content_width,
154+
max_content_height,
155+
measured_width,
156+
measured_height,
157+
);
158+
Size::new(measured_width, measured_height.min(max_h))
159+
}
160+
}
161+
162+
struct TextInfoBuilder(TextInfo);
163+
164+
impl TextInfoBuilder {
165+
fn new() -> Self {
166+
Self(TextInfo {
167+
font_size: 16.,
168+
// raw_text: String::new(),
169+
text_len: 0,
170+
})
171+
}
172+
// fn with_text(mut self, text: String) -> Self {
173+
// self.0.raw_text = text;
174+
// self
175+
// }
176+
fn with_text_len(mut self, text_len: usize) -> Self {
177+
self.0.text_len = text_len;
178+
self
179+
}
180+
fn with_font_size(mut self, font_size: f32) -> Self {
181+
self.0.font_size = font_size;
182+
self
183+
}
184+
fn build(self) -> TextInfo {
185+
self.0
186+
}
187+
}
188+
189+
#[inline(always)]
190+
fn prepare_measure_node(node: *mut Node, text_info: TextInfo) {
191+
let node = unsafe { &mut *node };
192+
node.set_measure_func(Some(Box::new(
193+
move |_,
194+
max_width,
195+
_,
196+
max_height,
197+
_,
198+
min_width,
199+
min_height,
200+
max_content_width,
201+
max_content_height| {
202+
text_info.measure(
203+
min_width,
204+
min_height,
205+
max_width,
206+
max_height,
207+
max_content_width,
208+
max_content_height,
209+
)
210+
},
211+
)));
212+
unsafe {
213+
node.set_display(Display::Inline);
214+
node.set_baseline_func(Some(Box::new(|_, _, _| Len::from_f32(16.))));
215+
node.set_node_type(float_pigment_forest::NodeType::Text);
216+
}
217+
}
218+
101219
impl TestCtx {
102220
pub fn new() -> Self {
103221
Self {
@@ -241,50 +359,45 @@ impl TestCtx {
241359
parent_node_props: Option<&NodeProperties>,
242360
) -> NodePtr {
243361
match cur {
244-
NodeType::Text(_t) => {
362+
NodeType::Text(t) => {
245363
let node = Node::new_ptr();
246-
(*node).set_display(Display::Inline);
247-
// TODO
248-
(*node).set_measure_func(Some(Box::new(
249-
|_,
250-
max_width,
251-
_,
252-
max_height,
253-
_,
254-
min_width,
255-
min_height,
256-
max_content_width,
257-
max_content_height| {
258-
println!(
259-
"width: {:?} ~ {:?}, height: {:?} ~ {:?}, max_content_width: {:?}, max_content_height: {:?}",
260-
min_width,
261-
max_width,
262-
min_height,
263-
max_height,
264-
max_content_width,
265-
max_content_height
266-
);
267-
Size::new(Len::from_f32(32.), Len::from_f32(16.))
268-
},
269-
)));
270-
(*node).set_baseline_func(Some(Box::new(|_, _, _| Len::from_f32(16.))));
271-
(*node).set_node_type(float_pigment_forest::NodeType::Text);
272-
364+
let text_info = TextInfoBuilder::new().with_text_len(t.text().len()).build();
365+
prepare_measure_node(node, text_info);
273366
node
274367
}
275368
NodeType::Element(e) => {
276369
let node = Node::new_ptr();
277-
match e.tag() {
278-
"div" => {}
279-
_ => (*node).set_display(float_pigment_css::typing::Display::Inline),
280-
};
370+
if !is_block_tag(e.tag()) {
371+
(*node).set_display(float_pigment_css::typing::Display::Inline);
372+
}
373+
281374
let mut node_props = NodeProperties::new(parent_node_props);
282375
if let Some(style) = e.attributes().get("style") {
283376
unsafe {
284377
TestCtx::set_style(&*node, &style, &mut node_props, parent_node_props);
285378
}
286379
}
287380
self.set_expect_layout_pos(node, e.attributes());
381+
382+
if is_measure_text_slot(e.tag()) {
383+
let node = Node::new_ptr();
384+
let text_len = e
385+
.attributes()
386+
.get("len")
387+
.map(|v| v.parse::<usize>().unwrap())
388+
.unwrap_or(0);
389+
let font_size = e
390+
.attributes()
391+
.get("fontSize")
392+
.map(|v| v.parse::<f32>().unwrap())
393+
.unwrap_or(16.);
394+
let text_info = TextInfoBuilder::new()
395+
.with_text_len(text_len)
396+
.with_font_size(font_size)
397+
.build();
398+
prepare_measure_node(node, text_info);
399+
}
400+
288401
e.children_mut().iter().for_each(|item| {
289402
let child = self.create_node_recursive(item, Some(&node_props));
290403
(*node).append_child(child);

0 commit comments

Comments
 (0)