1use super::*;
4
5pub(crate) fn margin_is_h_auto(node: &StyledNode) -> bool {
7 if node
8 .value_ref("margin-left")
9 .map(|v| v.trim() == "auto")
10 .unwrap_or(false)
11 {
12 return true;
13 }
14 if let Some(m) = node.value_ref("margin") {
15 let w = crate::os_lib::css::CUR_WORLD.load(AtomicOrdering::Relaxed);
16 if w != 0 {
17 return shorthand_side_token(m, crate::os_lib::css::world_phys_side(w, 3)) == Some("auto");
19 }
20 let mut it = m.split_whitespace();
21 let first = it.next();
22 let second = it.next();
23 let lr = second.or(first);
24 return lr.map(|s| s == "auto").unwrap_or(false);
25 }
26 false
27}
28
29pub(crate) fn shorthand_side_token(m: &str, idx: usize) -> Option<&str> {
31 let mut toks = [""; 4];
32 let mut count = 0;
33 for t in m.split_whitespace().take(4) {
34 toks[count] = t;
35 count += 1;
36 }
37 match count {
38 1 => Some(toks[0]),
39 2 => Some(if idx == 0 || idx == 2 { toks[0] } else { toks[1] }),
40 3 => Some(match idx {
41 0 => toks[0],
42 2 => toks[2],
43 _ => toks[1],
44 }),
45 4 => Some(toks[idx]),
46 _ => None,
47 }
48}
49
50pub(crate) fn box_main_auto_margins(b: &LayoutBox, is_row: bool) -> (bool, bool) {
53 fn side_auto(b: &LayoutBox, side: &str, short_idx_4: usize) -> bool {
54 if let Some(v) = box_style_value_ref(b, side) {
55 return v.trim() == "auto";
56 }
57 if let Some(m) = box_style_value_ref(b, "margin") {
58 let w = crate::os_lib::css::CUR_WORLD.load(AtomicOrdering::Relaxed);
59 let short_idx_4 = if w != 0 { crate::os_lib::css::world_phys_side(w, short_idx_4) } else { short_idx_4 };
60 let mut toks = [""; 4];
61 let mut count = 0;
62 for tok in m.split_whitespace().take(4) {
63 toks[count] = tok;
64 count += 1;
65 }
66 let val = match count {
68 1 => Some(toks[0]),
69 2 => {
70 if short_idx_4 == 0 || short_idx_4 == 2 {
72 Some(toks[0])
73 } else {
74 Some(toks[1])
75 }
76 }
77 3 => match short_idx_4 {
78 0 => Some(toks[0]),
79 2 => Some(toks[2]),
80 _ => Some(toks[1]), },
82 4 => Some(toks[short_idx_4]),
83 _ => None,
84 };
85 return val.map(|s| s == "auto").unwrap_or(false);
86 }
87 false
88 }
89 if is_row {
90 (
92 side_auto(b, "margin-left", 3),
93 side_auto(b, "margin-right", 1),
94 )
95 } else {
96 (
97 side_auto(b, "margin-top", 0),
98 side_auto(b, "margin-bottom", 2),
99 )
100 }
101}
102
103pub(crate) fn box_position(b: &LayoutBox) -> &'static str {
107 let Some(v) = box_style_value_ref(b, "position") else { return "static" };
108 let v = v.trim();
109 if v.eq_ignore_ascii_case("relative") {
110 "relative"
111 } else if v.eq_ignore_ascii_case("absolute") {
112 "absolute"
113 } else if v.eq_ignore_ascii_case("fixed") {
114 "fixed"
115 } else if v.eq_ignore_ascii_case("sticky") {
116 "sticky"
117 } else {
118 "static"
119 }
120}
121
122pub fn styled_establishes_fixed_cb(styled: &StyledNode) -> bool {
126 let non_none = |prop: &str| {
127 styled.value_ref(prop).is_some_and(|v| {
128 let t = v.trim();
129 !t.is_empty() && !t.eq_ignore_ascii_case("none")
130 })
131 };
132 if non_none("transform")
133 || non_none("perspective")
134 || non_none("filter")
135 || non_none("backdrop-filter")
136 {
137 return true;
138 }
139 if styled.value_ref("contain").is_some_and(|v| {
140 v.split_whitespace().any(|tok| {
141 tok.eq_ignore_ascii_case("paint")
142 || tok.eq_ignore_ascii_case("layout")
143 || tok.eq_ignore_ascii_case("strict")
144 || tok.eq_ignore_ascii_case("content")
145 })
146 }) {
147 return true;
148 }
149 styled.value_ref("will-change").is_some_and(|v| {
150 v.split(|c: char| c == ',' || c.is_ascii_whitespace()).any(|tok| {
151 tok.eq_ignore_ascii_case("transform")
152 || tok.eq_ignore_ascii_case("perspective")
153 || tok.eq_ignore_ascii_case("filter")
154 || tok.eq_ignore_ascii_case("backdrop-filter")
155 || tok.eq_ignore_ascii_case("contain")
156 || tok.eq_ignore_ascii_case("scale")
157 || tok.eq_ignore_ascii_case("rotate")
158 || tok.eq_ignore_ascii_case("translate")
159 })
160 })
161}
162
163pub(crate) fn box_establishes_fixed_cb(b: &LayoutBox) -> bool {
165 let styled = match &b.box_type {
166 BoxType::BlockNode(s)
167 | BoxType::InlineNode(s)
168 | BoxType::InlineBlockNode(s)
169 | BoxType::FlexNode(s)
170 | BoxType::GridNode(s)
171 | BoxType::TableNode(s)
172 | BoxType::TableRowGroupNode(s)
173 | BoxType::TableRowNode(s)
174 | BoxType::TableCellNode(s) => *s,
175 BoxType::AnonymousBlock => return false,
176 };
177 styled_establishes_fixed_cb(styled)
178}
179
180pub(crate) fn box_establishes_abs_cb(b: &LayoutBox) -> bool {
183 matches!(box_position(b), "relative" | "absolute" | "fixed" | "sticky")
184 || box_establishes_fixed_cb(b)
185}
186
187pub(crate) fn box_is_rtl(b: &LayoutBox) -> bool {
191 box_style_value_ref(b, "direction")
192 .map(|v| v.trim().eq_ignore_ascii_case("rtl"))
193 .unwrap_or(false)
194}
195
196pub(crate) fn box_float(b: &LayoutBox) -> &'static str {
200 let Some(raw) = box_style_value_ref(b, "float") else {
201 return "none";
202 };
203 let v = raw.trim();
204 if v.eq_ignore_ascii_case("none") || v.is_empty() {
205 "none"
206 } else if v.eq_ignore_ascii_case("left") {
207 "left"
208 } else if v.eq_ignore_ascii_case("right") {
209 "right"
210 } else if v.eq_ignore_ascii_case("inline-start") {
211 if box_is_rtl(b) { "right" } else { "left" }
212 } else if v.eq_ignore_ascii_case("inline-end") {
213 if box_is_rtl(b) { "left" } else { "right" }
214 } else {
215 "none"
216 }
217}
218
219pub(crate) fn box_clear(b: &LayoutBox) -> &'static str {
222 let Some(raw) = box_style_value_ref(b, "clear") else {
223 return "none";
224 };
225 let v = raw.trim();
226 if v.eq_ignore_ascii_case("none") || v.is_empty() {
227 "none"
228 } else if v.eq_ignore_ascii_case("left") {
229 "left"
230 } else if v.eq_ignore_ascii_case("right") {
231 "right"
232 } else if v.eq_ignore_ascii_case("both") {
233 "both"
234 } else if v.eq_ignore_ascii_case("inline-start") {
235 if box_is_rtl(b) { "right" } else { "left" }
236 } else if v.eq_ignore_ascii_case("inline-end") {
237 if box_is_rtl(b) { "left" } else { "right" }
238 } else {
239 "none"
240 }
241}
242
243pub(crate) fn box_len(b: &LayoutBox, prop: &str, base: i32) -> Option<i32> {
245 let raw = box_style_value_ref(b, prop);
246 if raw.is_some_and(is_stretch_value) {
247 return stretch_len(b, prop, base);
248 }
249 parse_length_value(raw).and_then(|lv| resolve_length_value(lv, base))
250}
251
252pub(crate) fn is_stretch_value(v: &str) -> bool {
254 let t = v.trim();
255 t.eq_ignore_ascii_case("stretch")
256 || t.eq_ignore_ascii_case("-webkit-fill-available")
257 || t.eq_ignore_ascii_case("-moz-available")
258 || t.eq_ignore_ascii_case("fill-available")
259}
260
261fn stretch_len(b: &LayoutBox, prop: &str, base: i32) -> Option<i32> {
266 let horizontal = prop.ends_with("width");
267 if !horizontal && base <= 0 {
268 return None;
269 }
270 let d = &b.dimensions;
271 let (margin, edges) = if horizontal {
272 (d.margin.left + d.margin.right, d.padding.left + d.padding.right + d.border.left + d.border.right)
273 } else {
274 (d.margin.top + d.margin.bottom, d.padding.top + d.padding.bottom + d.border.top + d.border.bottom)
275 };
276 let border_box = box_style_value_ref(b, "box-sizing").is_some_and(|v| v.trim().eq_ignore_ascii_case("border-box"));
277 let total = base - margin;
278 Some(if border_box { total } else { total - edges }.max(0))
279}
280
281pub(crate) fn box_inset_len(b: &LayoutBox, side: &str, base: i32) -> Option<i32> {
285 if let Some(v) = box_len(b, side, base) {
286 return Some(v);
287 }
288 let side_idx = match side {
289 "top" => 0,
290 "right" => 1,
291 "bottom" => 2,
292 "left" => 3,
293 _ => return None,
294 };
295 let inset = box_style_value_ref(b, "inset")?;
296 let w = crate::os_lib::css::CUR_WORLD.load(AtomicOrdering::Relaxed);
297 let side_idx = if w != 0 { crate::os_lib::css::world_phys_side(w, side_idx) } else { side_idx };
298 let mut toks = [""; 4];
299 let mut count = 0;
300 for t in inset.split_whitespace().take(4) {
301 toks[count] = t;
302 count += 1;
303 }
304 let tok = match count {
305 1 => Some(toks[0]),
306 2 => match side_idx {
307 0 | 2 => Some(toks[0]),
308 _ => Some(toks[1]),
309 },
310 3 => match side_idx {
311 0 => Some(toks[0]),
312 2 => Some(toks[2]),
313 _ => Some(toks[1]),
314 },
315 4 => Some(toks[side_idx]),
316 _ => None,
317 }?;
318 parse_length_value(Some(tok)).and_then(|lv| resolve_length_value(lv, base))
319}
320
321pub(crate) fn extract_fn_args_ref<'a>(s: &'a str, name: &str) -> Option<&'a str> {
323 let mut rest = s;
324 while let Some(pos) = rest.find(name) {
325 let after_name = rest.get(pos + name.len()..)?;
326 if let Some(after_paren) = after_name.strip_prefix('(') {
327 let end = after_paren.find(')')?;
328 return after_paren.get(..end);
329 }
330 rest = after_name;
331 }
332 None
333}
334
335pub(crate) fn extract_fn_args(s: &str, name: &str) -> Option<String> {
337 extract_fn_args_ref(s, name).map(String::from)
338}
339
340pub(crate) fn resolve_translate_len(tok: &str, base: i32) -> i32 {
352 let t = tok.trim();
353 if let Some(p) = t.strip_suffix('%') {
354 return p
355 .trim()
356 .parse::<f32>()
357 .map(|v| (v / 100.0 * base as f32) as i32)
358 .unwrap_or(0);
359 }
360 if let Some(p) = t.strip_suffix("rem") {
361 return p.trim().parse::<f32>().map(|v| (v * crate::os_lib::css::units::root_font_px()) as i32).unwrap_or(0);
362 }
363 if let Some(p) = t.strip_suffix("vh") {
364 let h = super::values::VIEWPORT_HINT_H.load(core::sync::atomic::Ordering::Relaxed);
365 return p
366 .trim()
367 .parse::<f32>()
368 .map(|v| (v / 100.0 * h as f32) as i32)
369 .unwrap_or(0);
370 }
371 if let Some(p) = t.strip_suffix("vw") {
372 let w = super::values::VIEWPORT_HINT_W.load(core::sync::atomic::Ordering::Relaxed);
373 return p
374 .trim()
375 .parse::<f32>()
376 .map(|v| (v / 100.0 * w as f32) as i32)
377 .unwrap_or(0);
378 }
379 let n = t.strip_suffix("px").unwrap_or(t).trim();
380 n.parse::<f32>().map(|v| v as i32).unwrap_or(0)
381}
382
383pub(crate) fn box_translate(b: &LayoutBox) -> (i32, i32) {
386 let Some(t) = box_style_value_ref(b, "transform") else {
387 return (0, 0);
388 };
389 let w = b.dimensions.content.width;
390 let h = b.dimensions.content.height;
391 if let Some(args) = extract_fn_args_ref(t, "translate") {
392 let mut parts = args.split(',');
393 let dx = parts
394 .next()
395 .map(|a| resolve_translate_len(a, w))
396 .unwrap_or(0);
397 let dy = parts
398 .next()
399 .map(|a| resolve_translate_len(a, h))
400 .unwrap_or(0);
401 return (dx, dy);
402 }
403 let dx = extract_fn_args_ref(t, "translateX")
404 .map(|a| resolve_translate_len(a, w))
405 .unwrap_or(0);
406 let dy = extract_fn_args_ref(t, "translateY")
407 .map(|a| resolve_translate_len(a, h))
408 .unwrap_or(0);
409 (dx, dy)
410}
411
412pub(crate) fn occ_is(occ: &[Vec<bool>], r: usize, c: usize) -> bool {
414 occ.get(r)
415 .and_then(|row| row.get(c))
416 .copied()
417 .unwrap_or(false)
418}
419pub(crate) fn occ_mark(occ: &mut Vec<Vec<bool>>, r: usize, c: usize) {
420 while occ.len() <= r {
421 occ.push(Vec::new());
422 }
423 let row = &mut occ[r];
424 while row.len() <= c {
425 row.push(false);
426 }
427 row[c] = true;
428}
429
430