1use super::*;
10use crate::os_lib::css::{world_phys_side, CUR_WORLD};
11
12pub(crate) fn world_of(node: &StyledNode) -> u8 {
15 let wm = match node.value_raw("writing-mode") {
16 Some(v) => v.trim(),
17 None => return 0,
18 };
19 let (fb, fi) = match wm {
20 "vertical-rl" | "tb-rl" | "sideways-rl" => (true, false),
21 "vertical-lr" | "tb-lr" => (false, false),
22 "sideways-lr" => (false, true),
23 _ => return 0,
24 };
25 let rtl = node
26 .value_raw("direction")
27 .map(|v| v.trim().eq_ignore_ascii_case("rtl"))
28 .unwrap_or(false);
29 1 | ((fb as u8) << 1) | (((fi ^ rtl) as u8) << 2)
30}
31
32fn edges_arr(e: &EdgeSizes) -> [i32; 4] {
33 [e.top, e.right, e.bottom, e.left]
34}
35
36fn arr_edges(a: [i32; 4]) -> EdgeSizes {
37 EdgeSizes { top: a[0], right: a[1], bottom: a[2], left: a[3] }
38}
39
40fn edges_to_phys(e: &EdgeSizes, w: u8) -> EdgeSizes {
42 let l = edges_arr(e);
43 let mut p = [0i32; 4];
44 for (s, v) in l.iter().enumerate() {
45 p[world_phys_side(w, s)] = *v;
46 }
47 arr_edges(p)
48}
49
50fn edges_to_log(e: &EdgeSizes, w: u8) -> EdgeSizes {
51 let p = edges_arr(e);
52 let mut l = [0i32; 4];
53 for (s, v) in l.iter_mut().enumerate() {
54 *v = p[world_phys_side(w, s)];
55 }
56 arr_edges(l)
57}
58
59pub(crate) fn style_box_edges_world(
61 node: &StyledNode,
62 containing_width: i32,
63 init: &Dimensions,
64 w: u8,
65) -> (EdgeSizes, EdgeSizes, EdgeSizes) {
66 let init_p = Dimensions {
67 margin: edges_to_phys(&init.margin, w),
68 padding: edges_to_phys(&init.padding, w),
69 border: edges_to_phys(&init.border, w),
70 ..Default::default()
71 };
72 CUR_WORLD.store(0, AtomicOrdering::Relaxed);
73 let r = style_box_edges_uncached(node, containing_width, &init_p);
74 CUR_WORLD.store(w, AtomicOrdering::Relaxed);
75 (edges_to_log(&r.0, w), edges_to_log(&r.1, w), edges_to_log(&r.2, w))
76}
77
78fn to_phys(w: u8, r: &Rect, ext: (i32, i32)) -> (Rect, (i32, i32)) {
80 if w == 0 {
81 return (r.clone(), ext);
82 }
83 let (ew, eh) = ext;
84 let x = if w & 2 != 0 { eh - (r.y + r.height) } else { r.y };
85 let y = if w & 4 != 0 { ew - (r.x + r.width) } else { r.x };
86 (Rect { x, y, width: r.height, height: r.width }, (eh, ew))
87}
88
89fn from_phys(w: u8, r: &Rect, ext: (i32, i32)) -> (Rect, (i32, i32)) {
91 if w == 0 {
92 return (r.clone(), ext);
93 }
94 let (pw, ph) = ext;
95 let y = if w & 2 != 0 { pw - (r.x + r.width) } else { r.x };
96 let x = if w & 4 != 0 { ph - (r.y + r.height) } else { r.y };
97 (Rect { x, y, width: r.height, height: r.width }, (ph, pw))
98}
99
100fn rect_c_to_p(r: &Rect, c: u8, p: u8, ext: (i32, i32)) -> Rect {
102 let (ph, pext) = to_phys(c, r, ext);
103 from_phys(p, &ph, pext).0
104}
105
106fn edges_c_to_p(e: &EdgeSizes, c: u8, p: u8) -> EdgeSizes {
107 let ph = edges_to_phys(e, c);
108 edges_to_log(&ph, p)
109}
110
111static PROBING: AtomicBool = AtomicBool::new(false);
113pub(crate) static PARENT_FLEXGRID: AtomicBool = AtomicBool::new(false);
115
116impl<'a> LayoutBox<'a> {
117 pub(crate) fn orthogonal_intrinsic_widths(&self, p: u8, c: u8) -> (i32, i32) {
120 let Some(node) = self.world_node() else { return (0, 0) };
121 let mut probe = build_layout_tree(node);
123 probe.trim_leading_space = self.trim_leading_space;
124 let base_w = if p == 0 { VIEWPORT_HINT_W.load(AtomicOrdering::Relaxed) } else { VIEWPORT_HINT_H.load(AtomicOrdering::Relaxed) };
125 let cb = Dimensions {
126 content: Rect { x: 0, y: 0, width: base_w.max(1), height: 0 },
127 ..Default::default()
128 };
129 let was = PROBING.swap(true, AtomicOrdering::Relaxed);
130 let saved_forced = FORCED_BLOCK_H.swap(-1, AtomicOrdering::Relaxed);
131 probe.layout_other_world(cb, p, c);
132 FORCED_BLOCK_H.store(saved_forced, AtomicOrdering::Relaxed);
133 PROBING.store(was, AtomicOrdering::Relaxed);
134 let w = probe.dimensions.content.width.max(0);
135 (w, w)
136 }
137
138 pub(crate) fn world_node(&self) -> Option<&'a StyledNode<'a>> {
140 match &self.box_type {
141 BoxType::BlockNode(n) | BoxType::FlexNode(n) | BoxType::GridNode(n) => Some(*n),
142 BoxType::InlineBlockNode(n) if self.is_inline_flex() || self.is_inline_grid() => Some(*n),
143 _ => None,
144 }
145 }
146
147 fn convert_world(&mut self, c: u8, p: u8, ext: (i32, i32)) {
149 let d = &mut self.dimensions;
150 d.content = rect_c_to_p(&d.content, c, p, ext);
151 d.padding = edges_c_to_p(&d.padding, c, p);
152 d.border = edges_c_to_p(&d.border, c, p);
153 d.margin = edges_c_to_p(&d.margin, c, p);
154 for ch in &mut self.children {
155 ch.convert_world(c, p, ext);
156 }
157 }
158
159 pub(crate) fn layout_other_world(&mut self, cb: Dimensions, p: u8, c: u8) {
161 let node = match self.world_node() {
162 Some(n) => n,
163 None => return,
164 };
165 self.dimensions = Dimensions::default();
166 let swap = (p != 0) != (c != 0);
167 let forced_p = FORCED_BLOCK_H.swap(-1, AtomicOrdering::Relaxed);
168 let parent_fg = PARENT_FLEXGRID.load(AtomicOrdering::Relaxed);
169 let pct_p = PCT_BASE_H.load(AtomicOrdering::Relaxed);
170 let avail_x_p = cb.content.width.max(1);
171 let avail_y_p = if pct_p > 0 {
172 pct_p
173 } else if p == 0 {
174 VIEWPORT_HINT_H.load(AtomicOrdering::Relaxed)
175 } else {
176 VIEWPORT_HINT_W.load(AtomicOrdering::Relaxed)
177 };
178 CUR_WORLD.store(c, AtomicOrdering::Relaxed);
179 let base = if swap { avail_y_p.max(1) } else { avail_x_p };
180 let (m, pd, bd) = style_box_edges(node, base, &Dimensions::default());
181 let nc_x = m.left + m.right + pd.left + pd.right + bd.left + bd.right;
182 let nc_y = m.top + m.bottom + pd.top + pd.bottom + bd.top + bd.bottom;
183 let is_auto = |name: &str| {
184 let v = len_value(node, name);
185 matches!(parse_length_value(v.as_deref()), Some(LengthValue::Auto) | None)
186 };
187 let width_auto = is_auto("width");
188 let height_auto = is_auto("height");
189 let is_root = matches!(&node.node.node_type, NodeType::Element { tag_name, .. } if tag_name == "html");
191 let cb_w;
192 if !swap && (!width_auto || !Self::child_is_out_of_flow(self)) {
193 cb_w = avail_x_p;
194 } else if forced_p >= 0 && width_auto {
195 cb_w = forced_p + nc_x;
196 } else if !width_auto || is_root {
197 cb_w = avail_y_p.max(1);
198 } else {
199 let (minc, maxc) = self.block_intrinsic_widths();
201 let a = (avail_y_p - nc_x).max(0);
202 cb_w = a.min(maxc).max(minc) + nc_x;
203 }
204 let cbc = Dimensions {
205 content: Rect { x: 0, y: 0, width: cb_w.max(1), height: 0 },
206 ..Default::default()
207 };
208 let guard = PctBaseHGuard::set(if swap { avail_x_p } else { pct_p });
209 let inline_level = matches!(self.box_type, BoxType::InlineBlockNode(_));
210 let mut fill_block: Option<i32> = None;
211 if swap
212 && height_auto
213 && !inline_level
214 && parent_fg
215 && !Self::child_is_out_of_flow(self)
216 && !PROBING.load(AtomicOrdering::Relaxed)
217 {
218 let fill = (avail_x_p - nc_y).max(0);
220 if matches!(self.box_type, BoxType::GridNode(_)) {
221 FORCED_BLOCK_H.store(fill, AtomicOrdering::Relaxed);
222 } else {
223 fill_block = Some(fill);
224 }
225 } else if !swap && forced_p >= 0 {
226 FORCED_BLOCK_H.store(forced_p, AtomicOrdering::Relaxed);
227 }
228 self.layout(cbc);
229 if let Some(f) = fill_block {
230 self.dimensions.content.height = f;
231 }
232 drop(guard);
233 CUR_WORLD.store(p, AtomicOrdering::Relaxed);
234 let mb = self.dimensions.margin_box();
235 self.convert_world(c, p, (mb.width, mb.height));
236 self.translate(cb.content.x, cb.content.y + cb.content.height);
237 }
238}