1use crate::geometry::{LayoutGeometry, Rect};
4use crate::tree::LayoutNode;
5use strata_style::types::{BoxSizing, Dimension, Position};
6
7pub fn collapse_margins(m1: f32, m2: f32) -> f32 {
15 let m1 = if m1.is_finite() {
16 m1.clamp(-1e7, 1e7)
17 } else {
18 0.0
19 };
20 let m2 = if m2.is_finite() {
21 m2.clamp(-1e7, 1e7)
22 } else {
23 0.0
24 };
25 if m1 >= 0.0 && m2 >= 0.0 {
26 m1.max(m2)
27 } else if m1 < 0.0 && m2 < 0.0 {
28 -(-m1).max(-m2)
29 } else {
30 let max_pos = m1.max(m2).max(0.0);
31 let min_neg = m1.min(m2).min(0.0);
32 max_pos + min_neg
33 }
34}
35
36pub fn resolve_dimension(dim: Dimension, available: f32) -> Option<f32> {
38 const MAX_DIMENSION: f32 = 1e7;
39
40 let avail = if available.is_finite() {
41 available.clamp(-MAX_DIMENSION, MAX_DIMENSION)
42 } else {
43 0.0
44 };
45
46 match dim {
47 Dimension::Auto => None,
48 Dimension::Px(px) => {
49 if px.is_finite() {
50 Some(px.clamp(-MAX_DIMENSION, MAX_DIMENSION))
51 } else {
52 None
53 }
54 }
55 Dimension::Percent(pct) => {
56 if pct.is_finite() {
57 let px = avail * (pct / 100.0);
58 if px.is_finite() {
59 Some(px.clamp(-MAX_DIMENSION, MAX_DIMENSION))
60 } else {
61 None
62 }
63 } else {
64 None
65 }
66 }
67 Dimension::Vw(vw) => {
68 if vw.is_finite() {
69 let px = avail * (vw / 100.0);
70 if px.is_finite() {
71 Some(px.clamp(-MAX_DIMENSION, MAX_DIMENSION))
72 } else {
73 None
74 }
75 } else {
76 None
77 }
78 }
79 Dimension::Vh(vh) => {
80 if vh.is_finite() {
81 let px = avail * (vh / 100.0);
82 if px.is_finite() {
83 Some(px.clamp(-MAX_DIMENSION, MAX_DIMENSION))
84 } else {
85 None
86 }
87 } else {
88 None
89 }
90 }
91 Dimension::Rem(rem) => {
92 if rem.is_finite() {
93 Some((rem * 16.0).clamp(-MAX_DIMENSION, MAX_DIMENSION))
94 } else {
95 None
96 }
97 }
98 Dimension::Em(em) => {
99 if em.is_finite() {
100 Some((em * 16.0).clamp(-MAX_DIMENSION, MAX_DIMENSION))
101 } else {
102 None
103 }
104 }
105 Dimension::Calc(id) => strata_style::calc::get_calc(id).and_then(|expr| {
106 let ctx = strata_style::calc::CalcContext {
107 parent_size: avail,
108 root_font_size: 16.0,
109 current_font_size: 16.0,
110 viewport_width: 1920.0,
111 viewport_height: 1080.0,
112 };
113 let val = expr.eval(&ctx);
114 if val.is_finite() {
115 Some(val.clamp(-MAX_DIMENSION, MAX_DIMENSION))
116 } else {
117 None
118 }
119 }),
120 Dimension::MinContent | Dimension::MaxContent | Dimension::FitContent(_) => None,
121 }
122}
123
124pub fn clamp_dimension(size: f32, min: Dimension, max: Dimension, available: f32) -> f32 {
126 let mut clamped = if size.is_finite() {
127 size.clamp(0.0, 1e7)
128 } else {
129 0.0
130 };
131 if let Some(min_px) = resolve_dimension(min, available) {
132 clamped = clamped.max(min_px.max(0.0));
133 }
134 if let Some(max_px) = resolve_dimension(max, available) {
135 clamped = clamped.min(max_px.max(0.0));
136 }
137 clamped.clamp(0.0, 1e7)
138}
139
140pub fn resolve_dimension_with_node(
143 dim: Dimension,
144 available: f32,
145 node: &LayoutNode,
146) -> Option<f32> {
147 match dim {
148 Dimension::MinContent => {
149 let sizes = crate::intrinsic::compute_min_max_sizes(node, Some(available));
150 let inline_insets = node.style.padding.left
151 + node.style.padding.right
152 + node.style.border_width.left
153 + node.style.border_width.right;
154 let val = match node.style.box_sizing {
155 BoxSizing::BorderBox => sizes.min_content,
156 BoxSizing::ContentBox => (sizes.min_content - inline_insets).max(0.0),
157 };
158 Some(val)
159 }
160 Dimension::MaxContent => {
161 let sizes = crate::intrinsic::compute_min_max_sizes(node, Some(available));
162 let inline_insets = node.style.padding.left
163 + node.style.padding.right
164 + node.style.border_width.left
165 + node.style.border_width.right;
166 let val = match node.style.box_sizing {
167 BoxSizing::BorderBox => sizes.max_content,
168 BoxSizing::ContentBox => (sizes.max_content - inline_insets).max(0.0),
169 };
170 Some(val)
171 }
172 Dimension::FitContent(arg) => {
173 let sizes = crate::intrinsic::compute_min_max_sizes(node, Some(available));
174 let inline_insets = node.style.padding.left
175 + node.style.padding.right
176 + node.style.border_width.left
177 + node.style.border_width.right;
178 let limit = if arg > 0.0 { arg } else { available };
179 let fitted = sizes.shrink_to_fit(limit);
180 let val = match node.style.box_sizing {
181 BoxSizing::BorderBox => fitted,
182 BoxSizing::ContentBox => (fitted - inline_insets).max(0.0),
183 };
184 Some(val)
185 }
186 other => resolve_dimension(other, available),
187 }
188}
189
190pub fn clamp_dimension_with_node(
192 size: f32,
193 min: Dimension,
194 max: Dimension,
195 available: f32,
196 node: &LayoutNode,
197) -> f32 {
198 let mut clamped = if size.is_finite() {
199 size.clamp(0.0, 1e7)
200 } else {
201 0.0
202 };
203 if let Some(min_px) = resolve_dimension_with_node(min, available, node) {
204 clamped = clamped.max(min_px.max(0.0));
205 }
206 if let Some(max_px) = resolve_dimension_with_node(max, available, node) {
207 clamped = clamped.min(max_px.max(0.0));
208 }
209 clamped.clamp(0.0, 1e7)
210}
211
212pub fn inline_style_from_computed_style(
214 style: &strata_style::types::ComputedStyle,
215) -> crate::typography::InlineTextStyle {
216 let is_monospace = crate::text::is_monospace_family(style.font_family);
217
218 crate::typography::InlineTextStyle {
219 font_family: style.font_family,
220 font_size: style.font_size,
221 font_weight: style.font_weight,
222 font_style: style.font_style,
223 color: style.color,
224 background_color: style.background_color,
225 text_decoration: style.text_decoration,
226 vertical_align: match style.vertical_align {
227 strata_style::types::VerticalAlign::Baseline => {
228 crate::typography::VerticalAlignment::Baseline
229 }
230 strata_style::types::VerticalAlign::Top => crate::typography::VerticalAlignment::Top,
231 strata_style::types::VerticalAlign::Bottom => {
232 crate::typography::VerticalAlignment::Bottom
233 }
234 strata_style::types::VerticalAlign::Middle => {
235 crate::typography::VerticalAlignment::Middle
236 }
237 strata_style::types::VerticalAlign::Sub => crate::typography::VerticalAlignment::Sub,
238 strata_style::types::VerticalAlign::Super => {
239 crate::typography::VerticalAlignment::Super
240 }
241 strata_style::types::VerticalAlign::TextTop => {
242 crate::typography::VerticalAlignment::TextTop
243 }
244 strata_style::types::VerticalAlign::TextBottom => {
245 crate::typography::VerticalAlignment::TextBottom
246 }
247 },
248 letter_spacing: style.letter_spacing,
249 line_height: Some(style.font_size * style.line_height),
250 is_monospace,
251 href: style
252 .get_custom_property(strata_primitives::AtomId::HREF)
253 .and_then(strata_primitives::atom::resolve),
254 }
255}
256
257pub fn is_inline_phrasing_node(child: &LayoutNode) -> bool {
259 matches!(
260 child.style.display,
261 strata_style::types::Display::Inline
262 | strata_style::types::Display::InlineBlock
263 | strata_style::types::Display::None
264 ) || child.text.is_some()
265}
266
267pub fn refresh_inline_fragment_styles(node: &mut LayoutNode) {
275 let mut stack = vec![node];
276 while let Some(curr) = stack.pop() {
277 if !curr.line_boxes.is_empty() {
278 if !curr.children.is_empty() {
279 let mut items = Vec::new();
280 collect_inline_items_from_children(
281 &curr.children,
282 None,
283 None,
284 &mut items,
285 curr.geometry.content_box.width,
286 curr.geometry.content_box.height,
287 );
288 curr.inline_items = items;
289 }
290 for line in &mut curr.line_boxes {
291 for frag in &mut line.text_fragments {
292 if let Some(crate::typography::InlineItem::Text { style, .. }) =
293 curr.inline_items.get(frag.item_index)
294 {
295 frag.style.color = style.color;
296 frag.style.background_color = style.background_color;
297 frag.style.text_decoration = style.text_decoration;
298 frag.style.font_family = style.font_family;
299 frag.font_family = style.font_family;
300 } else if curr.inline_items.is_empty() && curr.text.is_some() {
301 let style = inline_style_from_computed_style(&curr.style);
302 frag.style.color = style.color;
303 frag.style.background_color = style.background_color;
304 frag.style.text_decoration = style.text_decoration;
305 frag.style.font_family = style.font_family;
306 frag.font_family = style.font_family;
307 }
308 }
309 }
310 }
311 for child in &mut curr.children {
312 stack.push(child);
313 }
314 }
315}
316
317pub fn clear_inline_descendant_line_boxes(node: &mut LayoutNode) {
319 if node.style.display != strata_style::types::Display::Inline {
320 return;
321 }
322 let mut stack = vec![node];
323 while let Some(curr) = stack.pop() {
324 if curr.style.display == strata_style::types::Display::Inline {
325 curr.line_boxes.clear();
326 for child in &mut curr.children {
327 stack.push(child);
328 }
329 }
330 }
331}
332
333fn collect_inline_items_from_children(
335 children: &[LayoutNode],
336 parent_style: Option<&strata_style::types::ComputedStyle>,
337 top_level_idx: Option<usize>,
338 items: &mut Vec<crate::typography::InlineItem>,
339 content_width: f32,
340 available_height: f32,
341) {
342 collect_inline_items_from_children_guarded(
343 children,
344 parent_style,
345 top_level_idx,
346 items,
347 content_width,
348 available_height,
349 0,
350 );
351}
352
353fn collect_inline_items_from_children_guarded(
354 children: &[LayoutNode],
355 parent_style: Option<&strata_style::types::ComputedStyle>,
356 top_level_idx: Option<usize>,
357 items: &mut Vec<crate::typography::InlineItem>,
358 content_width: f32,
359 available_height: f32,
360 depth: usize,
361) {
362 if depth > crate::tree::MAX_LAYOUT_DEPTH {
363 return;
364 }
365 let _prof = crate::prof::Scope::new(crate::prof::Slot::StyleClone);
366 for (idx, child) in children.iter().enumerate() {
367 if child.style.display == strata_style::types::Display::None {
368 continue;
369 }
370 let effective_top_idx = top_level_idx.unwrap_or(idx);
371 if let Some(ref text) = child.text {
372 let mut computed_style = child.style.clone();
373 if let Some(p) = parent_style {
374 if computed_style.font_family == strata_primitives::AtomId::FONT_SANS
375 && p.font_family != strata_primitives::AtomId::FONT_SANS
376 {
377 computed_style.font_family = p.font_family;
378 }
379 }
380 let mut s = inline_style_from_computed_style(&computed_style);
381 if s.background_color.is_none() {
382 if let Some(p) = parent_style {
383 if p.display == strata_style::types::Display::Inline {
384 s.background_color = p.background_color;
385 }
386 }
387 }
388 if s.text_decoration == strata_style::types::TextDecoration::None {
389 if let Some(p) = parent_style {
390 if p.text_decoration != strata_style::types::TextDecoration::None {
391 s.text_decoration = p.text_decoration;
392 }
393 }
394 }
395 if s.href.is_none() {
396 if let Some(p) = parent_style {
397 s.href = p
398 .get_custom_property(strata_primitives::AtomId::HREF)
399 .and_then(strata_primitives::atom::resolve);
400 }
401 }
402 items.push(crate::typography::InlineItem::text(text.clone(), s));
403 } else if child.style.display == strata_style::types::Display::Inline
404 && !child.children.is_empty()
405 {
406 let mut merged_style = child.style.clone();
407 if let Some(p) = parent_style {
408 if merged_style.background_color.is_none()
409 && p.display == strata_style::types::Display::Inline
410 {
411 merged_style.background_color = p.background_color;
412 }
413 if merged_style.text_decoration == strata_style::types::TextDecoration::None {
414 merged_style.text_decoration = p.text_decoration;
415 }
416 if merged_style
417 .get_custom_property(strata_primitives::AtomId::HREF)
418 .is_none()
419 {
420 if let Some(h) = p.get_custom_property(strata_primitives::AtomId::HREF) {
421 merged_style.set_custom_property(strata_primitives::AtomId::HREF, h);
422 }
423 }
424 }
425 collect_inline_items_from_children_guarded(
426 &child.children,
427 Some(&merged_style),
428 Some(effective_top_idx),
429 items,
430 content_width,
431 available_height,
432 depth + 1,
433 );
434 } else {
435 let mut w =
436 resolve_dimension_with_node(child.style.width, content_width, child).unwrap_or(0.0);
437 let mut h = resolve_dimension(child.style.height, available_height).unwrap_or(0.0);
438 if w == 0.0 && child.geometry.border_box.width > 0.0 {
439 w = child.geometry.border_box.width;
440 }
441 if h == 0.0 && child.geometry.border_box.height > 0.0 {
442 h = child.geometry.border_box.height;
443 }
444 if h == 0.0 && w > 0.0 && child.children.is_empty() {
445 h = w;
447 }
448 items.push(crate::typography::InlineItem::atomic(
449 effective_top_idx,
450 w,
451 h,
452 child.style.margin.left,
453 child.style.margin.right,
454 crate::typography::VerticalAlignment::Baseline,
455 ));
456 }
457 }
458}
459
460fn propagate_atomic_bounds_to_inline_descendants(node: &mut LayoutNode, bounds: Rect) {
462 if node.style.display != strata_style::types::Display::Inline {
463 return;
464 }
465 let mut stack = vec![node];
466 while let Some(curr) = stack.pop() {
467 for child in &mut curr.children {
468 child.geometry.border_box.x = bounds.x;
469 child.geometry.border_box.y = bounds.y;
470 if child.geometry.border_box.width == 0.0 {
471 child.geometry.border_box.width = bounds.width;
472 }
473 if child.geometry.border_box.height == 0.0 {
474 child.geometry.border_box.height = bounds.height;
475 }
476 if child.style.display == strata_style::types::Display::Inline {
477 stack.push(child);
478 }
479 }
480 }
481}
482
483pub fn translate_node_and_descendants(node: &mut LayoutNode, dx: f32, dy: f32) {
485 if dx == 0.0 && dy == 0.0 {
486 return;
487 }
488 node.translate(dx, dy);
489}
490
491fn truncate_inline_overflow(
500 node: &LayoutNode,
501 res: &mut crate::typography::InlineLayoutResult,
502 content_width: f32,
503) {
504 use strata_style::types::{Overflow, TextOverflow};
505
506 if node.style.text_overflow != TextOverflow::Ellipsis
507 || node.style.overflow_x == Overflow::Visible
508 || content_width <= 0.0
509 {
510 return;
511 }
512
513 for line in &mut res.lines {
514 let mut new_fragments = Vec::with_capacity(line.text_fragments.len());
515 let mut cut = false;
516 for fragment in line.text_fragments.drain(..) {
517 if cut {
518 continue;
519 }
520 let frag_start = fragment.bounds.x;
521 let frag_end = frag_start + fragment.bounds.width;
522 if frag_end <= content_width + 1.0 {
523 new_fragments.push(fragment);
524 continue;
525 }
526
527 let available = content_width - frag_start;
528 if available <= 0.0 {
529 cut = true;
530 continue;
531 }
532
533 let style = &fragment.style;
534 let multiplier = match style.line_height {
535 Some(line_h) if style.font_size > 0.0 => (line_h / style.font_size).max(0.01),
536 _ => 1.2,
537 };
538 let measure = |text: &str| {
539 crate::text::measure_text_bounds_styled_with_family(
540 text,
541 style.font_size,
542 multiplier,
543 None,
544 style.is_monospace,
545 style.font_family,
546 style.font_weight,
547 style.font_style,
548 )
549 .0
550 };
551
552 let ellipsis_width = measure("\u{2026}");
553 if ellipsis_width > available {
554 cut = true;
555 continue;
556 }
557
558 let boundaries: Vec<usize> = fragment
561 .text
562 .char_indices()
563 .map(|(idx, _)| idx)
564 .chain(std::iter::once(fragment.text.len()))
565 .collect();
566 let mut low = 0usize;
567 let mut high = boundaries.len().saturating_sub(1);
568 let mut best = 0usize;
569 while low <= high {
570 let mid = (low + high) / 2;
571 let end = boundaries[mid];
572 let prefix = &fragment.text[..end];
573 if measure(prefix) + ellipsis_width <= available + 0.01 {
574 best = mid;
575 low = mid + 1;
576 } else if mid == 0 {
577 break;
578 } else {
579 high = mid - 1;
580 }
581 }
582
583 let end = boundaries[best];
584 let mut text = fragment.text[..end].to_string();
585 text.push('\u{2026}');
586 let width = measure(&text);
587 let mut truncated_fragment = fragment;
588 truncated_fragment.text = text;
589 truncated_fragment.bounds.width = width;
590 new_fragments.push(truncated_fragment);
591 cut = true;
592 }
593
594 if let Some(last) = new_fragments.last() {
595 line.bounds.width = last.bounds.x + last.bounds.width;
596 } else {
597 line.bounds.width = 0.0;
598 }
599 line.text_fragments = new_fragments;
600 }
601
602 res.total_width = res
603 .lines
604 .iter()
605 .map(|line| line.bounds.width)
606 .fold(0.0f32, f32::max);
607}
608
609pub fn solve_block_children(
611 node: &mut LayoutNode,
612 content_width: f32,
613 available_height: f32,
614) -> f32 {
615 solve_block_children_with_exclusions(node, content_width, available_height, None, 0.0)
616}
617
618pub fn solve_block_children_with_exclusions(
620 node: &mut LayoutNode,
621 content_width: f32,
622 available_height: f32,
623 parent_exclusions: Option<&mut crate::exclusions::ExclusionSpace>,
624 bfc_offset_y: f32,
625) -> f32 {
626 solve_block_children_with_exclusions_guarded(
627 node,
628 content_width,
629 available_height,
630 parent_exclusions,
631 bfc_offset_y,
632 crate::tree::LayoutDepthGuard::root(),
633 )
634}
635
636#[inline]
639pub fn is_empty_collapsible_block(node: &LayoutNode) -> bool {
640 node.children.is_empty()
641 && node.text.is_none()
642 && node.inline_items.is_empty()
643 && node.style.border_width.top == 0.0
644 && node.style.border_width.bottom == 0.0
645 && node.style.padding.top == 0.0
646 && node.style.padding.bottom == 0.0
647 && matches!(node.style.height, Dimension::Auto | Dimension::Px(0.0))
648 && matches!(node.style.min_height, Dimension::Auto | Dimension::Px(0.0))
649 && node.style.clear == strata_style::types::Clear::None
650 && node.style.float == strata_style::types::Float::None
651 && node.style.overflow_x == strata_style::types::Overflow::Visible
652 && node.style.overflow_y == strata_style::types::Overflow::Visible
653}
654
655#[inline]
658pub fn node_establishes_bfc(node: &LayoutNode) -> bool {
659 node.style.float != strata_style::types::Float::None
660 || matches!(node.style.position, Position::Absolute | Position::Fixed)
661 || matches!(
662 node.style.display,
663 strata_style::types::Display::InlineBlock
664 | strata_style::types::Display::TableCell
665 | strata_style::types::Display::TableCaption
666 | strata_style::types::Display::Flex
667 | strata_style::types::Display::Grid
668 | strata_style::types::Display::InlineFlex
669 | strata_style::types::Display::InlineGrid
670 | strata_style::types::Display::Table
671 | strata_style::types::Display::InlineTable
672 )
673 || node.style.overflow_x != strata_style::types::Overflow::Visible
674 || node.style.overflow_y != strata_style::types::Overflow::Visible
675 || node.style.contain.layout
676 || child_is_layout_boundary(node)
677}
678
679#[inline]
680fn child_is_layout_boundary(node: &LayoutNode) -> bool {
681 node.is_layout_boundary()
682}
683
684pub fn solve_block_children_with_exclusions_guarded(
686 node: &mut LayoutNode,
687 content_width: f32,
688 available_height: f32,
689 parent_exclusions: Option<&mut crate::exclusions::ExclusionSpace>,
690 bfc_offset_y: f32,
691 guard: crate::tree::LayoutDepthGuard,
692) -> f32 {
693 let _prof = crate::prof::Scope::new(crate::prof::Slot::Block);
694 let padding = node.style.padding;
695 let border = node.style.border_width;
696
697 if guard.is_exhausted() {
698 crate::tree::record_layout_overflow(guard.current_depth(), guard.max_depth());
699 return padding.top + border.top + padding.bottom + border.bottom;
700 }
701
702 if node.children.is_empty()
707 && node.inline_items.is_empty()
708 && node.style.text_overflow == strata_style::types::TextOverflow::Ellipsis
709 {
710 if let Some(text) = node.text.clone() {
711 let style = inline_style_from_computed_style(&node.style);
712 let items = vec![crate::typography::InlineItem::text(text, style)];
713 let opt = crate::typography::InlineLayoutOptions {
714 font_size: node.style.font_size,
715 line_height: Some(node.style.font_size * node.style.line_height),
716 max_width: Some(content_width),
717 white_space: match node.style.white_space {
718 strata_style::types::WhiteSpace::Normal => {
719 crate::typography::WhiteSpaceMode::Normal
720 }
721 strata_style::types::WhiteSpace::NoWrap => {
722 crate::typography::WhiteSpaceMode::NoWrap
723 }
724 strata_style::types::WhiteSpace::Pre => crate::typography::WhiteSpaceMode::Pre,
725 strata_style::types::WhiteSpace::PreWrap => {
726 crate::typography::WhiteSpaceMode::PreWrap
727 }
728 strata_style::types::WhiteSpace::PreLine => {
729 crate::typography::WhiteSpaceMode::PreLine
730 }
731 },
732 vertical_align: crate::typography::VerticalAlignment::Baseline,
733 is_monospace: crate::text::is_monospace_family(node.style.font_family),
734 exclusion_space: None,
735 bfc_offset_y: 0.0,
736 };
737 let ifc = crate::typography::InlineFormattingContext::new();
738 let mut res = ifc.layout_inline_stream(&items, &opt);
739 truncate_inline_overflow(node, &mut res, content_width);
740
741 let origin_x = node.geometry.border_box.x + border.left + padding.left;
742 let origin_y = node.geometry.border_box.y + border.top + padding.top;
743 let mut lines = res.lines;
744 for line in &mut lines {
745 line.bounds.x += origin_x;
746 line.bounds.y += origin_y;
747 for frag in &mut line.text_fragments {
748 frag.bounds.x += origin_x;
749 frag.bounds.y += origin_y;
750 frag.baseline_y += origin_y;
751 }
752 for atomic in &mut line.atomic_fragments {
753 atomic.bounds.x += origin_x;
754 atomic.bounds.y += origin_y;
755 }
756 }
757 let total_h = res.total_height;
758 node.inline_items = items;
759 node.line_boxes = lines;
760 return total_h + padding.top + padding.bottom + border.top + border.bottom;
761 }
762 }
763
764 let has_implicit_inlines =
766 !node.children.is_empty() && node.children.iter().all(is_inline_phrasing_node);
767 let has_explicit_inlines = node.children.is_empty() && !node.inline_items.is_empty();
768
769 if has_explicit_inlines || has_implicit_inlines {
770 if has_implicit_inlines {
771 let child_guard = guard.enter();
772 for child in &mut node.children {
773 if let Some(cg) = child_guard {
778 child.compute_layout_guarded(content_width, available_height, None, 0.0, cg);
779 } else {
780 child.compute_layout_guarded(content_width, available_height, None, 0.0, guard);
781 }
782 clear_inline_descendant_line_boxes(child);
783 }
784 let mut items = Vec::new();
785 collect_inline_items_from_children(
786 &node.children,
787 None,
788 None,
789 &mut items,
790 content_width,
791 available_height,
792 );
793 node.inline_items = items;
794 }
795
796 let ifc = crate::typography::InlineFormattingContext::new();
797 let opt = crate::typography::InlineLayoutOptions {
798 font_size: node.style.font_size,
799 line_height: Some(node.style.font_size * node.style.line_height),
800 max_width: Some(content_width),
801 white_space: match node.style.white_space {
802 strata_style::types::WhiteSpace::Normal => {
803 crate::typography::WhiteSpaceMode::Normal
804 }
805 strata_style::types::WhiteSpace::NoWrap => {
806 crate::typography::WhiteSpaceMode::NoWrap
807 }
808 strata_style::types::WhiteSpace::Pre => crate::typography::WhiteSpaceMode::Pre,
809 strata_style::types::WhiteSpace::PreWrap => {
810 crate::typography::WhiteSpaceMode::PreWrap
811 }
812 strata_style::types::WhiteSpace::PreLine => {
813 crate::typography::WhiteSpaceMode::PreLine
814 }
815 },
816 vertical_align: match node.style.vertical_align {
817 strata_style::types::VerticalAlign::Baseline => {
818 crate::typography::VerticalAlignment::Baseline
819 }
820 strata_style::types::VerticalAlign::Top => {
821 crate::typography::VerticalAlignment::Top
822 }
823 strata_style::types::VerticalAlign::Bottom => {
824 crate::typography::VerticalAlignment::Bottom
825 }
826 strata_style::types::VerticalAlign::Middle => {
827 crate::typography::VerticalAlignment::Middle
828 }
829 strata_style::types::VerticalAlign::Sub => {
830 crate::typography::VerticalAlignment::Sub
831 }
832 strata_style::types::VerticalAlign::Super => {
833 crate::typography::VerticalAlignment::Super
834 }
835 strata_style::types::VerticalAlign::TextTop => {
836 crate::typography::VerticalAlignment::TextTop
837 }
838 strata_style::types::VerticalAlign::TextBottom => {
839 crate::typography::VerticalAlignment::TextBottom
840 }
841 },
842 is_monospace: crate::text::is_monospace_family(node.style.font_family),
843 exclusion_space: parent_exclusions.as_deref(),
844 bfc_offset_y: bfc_offset_y + padding.top + border.top,
845 };
846
847 let mut res = ifc.layout_inline_stream(&node.inline_items, &opt);
848 truncate_inline_overflow(node, &mut res, content_width);
849 let content_origin_x = node.geometry.border_box.x + border.left + padding.left;
850 let content_origin_y = node.geometry.border_box.y + border.top + padding.top;
851
852 let mut positioned_lines = res.lines;
853 for line in &mut positioned_lines {
854 let align_offset_x = match node.style.text_align {
855 strata_style::types::TextAlign::Center => {
856 ((content_width - line.bounds.width) / 2.0).max(0.0)
857 }
858 strata_style::types::TextAlign::Right => {
859 (content_width - line.bounds.width).max(0.0)
860 }
861 _ => 0.0,
862 };
863
864 let total_shift_x = content_origin_x + align_offset_x;
865 line.bounds.x += total_shift_x;
866 line.bounds.y += content_origin_y;
867 for frag in &mut line.text_fragments {
868 frag.bounds.x += total_shift_x;
869 frag.bounds.y += content_origin_y;
870 frag.baseline_y += content_origin_y;
871 }
872 for at in &mut line.atomic_fragments {
873 at.bounds.x += total_shift_x;
874 at.bounds.y += content_origin_y;
875 if at.node_index < node.children.len() {
876 let child = &mut node.children[at.node_index];
877 let dx = at.bounds.x - child.geometry.border_box.x;
878 let dy = at.bounds.y - child.geometry.border_box.y;
879 child.geometry.border_box.width = at.bounds.width;
880 child.geometry.border_box.height = at.bounds.height;
881 child.translate(dx, dy);
882
883 let target_bounds = child.geometry.border_box;
885 propagate_atomic_bounds_to_inline_descendants(child, target_bounds);
886 }
887 }
888 }
889
890 let total_h = res.total_height;
891 for child in &mut node.children {
892 if let Some(ref text) = child.text {
893 let mut found = false;
894 for line in &positioned_lines {
895 for frag in &line.text_fragments {
896 if frag.text.contains(text.as_str()) || text.contains(frag.text.as_str()) {
897 child.geometry.border_box = frag.bounds;
898 found = true;
899 break;
900 }
901 }
902 if found {
903 break;
904 }
905 }
906 if !found {
907 child.geometry.border_box.x = content_origin_x;
908 child.geometry.border_box.y = content_origin_y;
909 }
910 }
911 }
912 node.line_boxes = positioned_lines;
913 return total_h + padding.top + padding.bottom + border.top + border.bottom;
914 }
915
916 let establishes_bfc = node_establishes_bfc(node);
920
921 let is_root_exclusion_context = parent_exclusions.is_none();
922 let mut local_exclusions = crate::exclusions::ExclusionSpace::new();
923 let (exclusion_space, base_bfc_y): (&mut crate::exclusions::ExclusionSpace, f32) =
924 if establishes_bfc || is_root_exclusion_context {
925 (&mut local_exclusions, 0.0)
926 } else if let Some(pe) = parent_exclusions {
927 (pe, bfc_offset_y)
928 } else {
929 (&mut local_exclusions, 0.0)
930 };
931 let initial_exclusions_count = exclusion_space.exclusions().len();
932 let parent_top_adjoining = !establishes_bfc && border.top == 0.0 && padding.top == 0.0;
933 let parent_bottom_adjoining = !establishes_bfc
934 && border.bottom == 0.0
935 && padding.bottom == 0.0
936 && node.style.height == Dimension::Auto;
937
938 let mut current_y = border.top + padding.top;
939 let mut pending_margin: Option<f32> = None;
940 let mut is_first_in_flow = true;
941 let mut last_child_establishes_bfc = false;
942
943 for child in &mut node.children {
944 if child.style.position == Position::Absolute || child.style.position == Position::Fixed {
945 continue;
946 }
947
948 if child.style.float == strata_style::types::Float::None
950 && child.style.clear != strata_style::types::Clear::None
951 {
952 let cleared_bfc_y =
953 exclusion_space.clearance_offset(child.style.clear, base_bfc_y + current_y);
954 if cleared_bfc_y > base_bfc_y + current_y {
955 current_y = cleared_bfc_y - base_bfc_y;
956 pending_margin = None;
957 }
958 }
959
960 if child.style.float != strata_style::types::Float::None {
962 let child_guard = guard.enter();
964 if let Some(cg) = child_guard {
965 child.compute_layout_guarded(content_width, available_height, None, 0.0, cg);
966 } else {
967 child.compute_layout_guarded(content_width, available_height, None, 0.0, guard);
968 }
969 let float_margin = child.style.margin;
970 let float_w = child.geometry.border_box.width + float_margin.left + float_margin.right;
971 let float_h = child.geometry.border_box.height + float_margin.top + float_margin.bottom;
972
973 let mut float_bfc_y = base_bfc_y + current_y;
975 if child.style.clear != strata_style::types::Clear::None {
976 let cleared_bfc_y =
977 exclusion_space.clearance_offset(child.style.clear, float_bfc_y);
978 if cleared_bfc_y > float_bfc_y {
979 float_bfc_y = cleared_bfc_y;
980 }
981 }
982 let mut opp = exclusion_space.line_opportunity(float_bfc_y, float_h, content_width);
983 if opp.available_width < float_w && content_width >= float_w {
984 while let Some(next_bfc_y) = exclusion_space.next_float_bottom_after(float_bfc_y) {
986 float_bfc_y = next_bfc_y;
987 opp = exclusion_space.line_opportunity(float_bfc_y, float_h, content_width);
988 if opp.available_width >= float_w {
989 break;
990 }
991 }
992 }
993 let local_float_y = float_bfc_y - base_bfc_y;
994
995 let (target_x, target_y) = match child.style.float {
996 strata_style::types::Float::Left | strata_style::types::Float::InlineStart => {
997 let float_x =
998 border.left + padding.left + opp.left_float_edge + float_margin.left;
999 let float_y = local_float_y + float_margin.top;
1000 let excl_rect = crate::geometry::Rect::from_xywh(
1001 opp.left_float_edge,
1002 float_bfc_y,
1003 float_w,
1004 float_h,
1005 );
1006 exclusion_space
1007 .add_exclusion(excl_rect, crate::exclusions::ExclusionKind::FloatLeft);
1008 (
1009 node.geometry.border_box.x + float_x,
1010 node.geometry.border_box.y + float_y,
1011 )
1012 }
1013 strata_style::types::Float::Right | strata_style::types::Float::InlineEnd => {
1014 let start_r = opp.right_float_edge;
1015 let float_x =
1016 border.left + padding.left + start_r - float_w + float_margin.left;
1017 let float_y = local_float_y + float_margin.top;
1018 let excl_rect = crate::geometry::Rect::from_xywh(
1019 start_r - float_w,
1020 float_bfc_y,
1021 float_w,
1022 float_h,
1023 );
1024 exclusion_space
1025 .add_exclusion(excl_rect, crate::exclusions::ExclusionKind::FloatRight);
1026 (
1027 node.geometry.border_box.x + float_x,
1028 node.geometry.border_box.y + float_y,
1029 )
1030 }
1031 strata_style::types::Float::None => unreachable!(),
1032 };
1033
1034 let old_x = child.geometry.border_box.x;
1035 let old_y = child.geometry.border_box.y;
1036 let dx = target_x - old_x;
1037 let dy = target_y - old_y;
1038 translate_node_and_descendants(child, dx, dy);
1039
1040 continue;
1042 }
1043
1044 let child_margin = child.style.margin;
1045
1046 if is_empty_collapsible_block(child) {
1048 let through_margin = collapse_margins(child_margin.top, child_margin.bottom);
1049 let combined = match pending_margin {
1050 Some(pm) => collapse_margins(pm, through_margin),
1051 None => through_margin,
1052 };
1053
1054 if is_first_in_flow && parent_top_adjoining {
1055 node.style.margin.top = collapse_margins(node.style.margin.top, combined);
1056 pending_margin = None;
1057 } else {
1058 pending_margin = Some(combined);
1059 }
1060
1061 child.geometry.border_box.x =
1062 node.geometry.border_box.x + border.left + padding.left + child_margin.left;
1063 child.geometry.border_box.y =
1064 node.geometry.border_box.y + current_y + pending_margin.unwrap_or(0.0);
1065 child.geometry.border_box.width = content_width;
1066 child.geometry.border_box.height = 0.0;
1067 continue;
1068 }
1069
1070 let child_establishes_bfc = node_establishes_bfc(child);
1071
1072 let mut collapsed_v_margin = match pending_margin.take() {
1073 Some(prev_m) => collapse_margins(prev_m, child_margin.top),
1074 None => child_margin.top,
1075 };
1076
1077 if is_first_in_flow {
1078 is_first_in_flow = false;
1079 if parent_top_adjoining && !child_establishes_bfc {
1080 node.style.margin.top = collapse_margins(node.style.margin.top, collapsed_v_margin);
1081 collapsed_v_margin = 0.0;
1082 }
1083 }
1084
1085 current_y += collapsed_v_margin;
1086
1087 let mut child_available_width = content_width;
1088 let mut child_x = border.left + padding.left + child_margin.left;
1089
1090 if child_establishes_bfc && !exclusion_space.exclusions().is_empty() {
1092 let mut child_bfc_y = base_bfc_y + current_y;
1093 let mut opp = exclusion_space.line_opportunity(child_bfc_y, 0.0, content_width);
1094
1095 if opp.available_width < content_width || opp.left_float_edge > 0.0 {
1096 let child_desired_w =
1097 resolve_dimension_with_node(child.style.width, content_width, child);
1098 if let Some(w) = child_desired_w {
1099 if w + child_margin.left + child_margin.right > opp.available_width {
1100 while let Some(next_y) =
1102 exclusion_space.next_float_bottom_after(child_bfc_y)
1103 {
1104 child_bfc_y = next_y;
1105 opp = exclusion_space.line_opportunity(child_bfc_y, 0.0, content_width);
1106 if opp.available_width >= w + child_margin.left + child_margin.right {
1107 break;
1108 }
1109 }
1110 current_y = child_bfc_y - base_bfc_y;
1111 }
1112 }
1113
1114 child_available_width =
1115 (opp.available_width - child_margin.left - child_margin.right).max(0.0);
1116 child_x = border.left + padding.left + opp.left_float_edge + child_margin.left;
1117 }
1118 }
1119
1120 child.geometry.border_box.x = node.geometry.border_box.x + child_x;
1121 child.geometry.border_box.y = node.geometry.border_box.y + current_y;
1122
1123 let passed_exclusions = if child_establishes_bfc || exclusion_space.exclusions().is_empty()
1126 {
1127 None
1128 } else {
1129 Some(&mut *exclusion_space)
1130 };
1131
1132 let child_guard = guard.enter();
1133 if let Some(cg) = child_guard {
1134 child.compute_layout_guarded(
1135 child_available_width,
1136 available_height,
1137 passed_exclusions,
1138 base_bfc_y + current_y,
1139 cg,
1140 );
1141 } else {
1142 child.compute_layout_guarded(
1143 child_available_width,
1144 available_height,
1145 passed_exclusions,
1146 base_bfc_y + current_y,
1147 guard,
1148 );
1149 }
1150
1151 if child.style.margin_auto_left && child.style.margin_auto_right {
1153 let free_space = child_available_width - child.geometry.border_box.width;
1154 let auto_m = (free_space / 2.0).max(0.0);
1155 let target_x = node.geometry.border_box.x + child_x - child_margin.left + auto_m;
1156 let dx = target_x - child.geometry.border_box.x;
1157 if dx != 0.0 {
1158 translate_node_and_descendants(child, dx, 0.0);
1159 }
1160 } else if child.style.margin_auto_left {
1161 let free_space =
1162 child_available_width - child.geometry.border_box.width - child_margin.right;
1163 let target_x =
1164 node.geometry.border_box.x + child_x - child_margin.left + free_space.max(0.0);
1165 let dx = target_x - child.geometry.border_box.x;
1166 if dx != 0.0 {
1167 translate_node_and_descendants(child, dx, 0.0);
1168 }
1169 } else if node.style.text_align == strata_style::types::TextAlign::Center {
1170 let free_space = content_width - child.geometry.border_box.width;
1171 if free_space > 0.0 {
1172 let offset_x = (free_space / 2.0).max(0.0);
1173 let target_x = node.geometry.border_box.x + border.left + padding.left + offset_x;
1174 let dx = target_x - child.geometry.border_box.x;
1175 if dx != 0.0 {
1176 translate_node_and_descendants(child, dx, 0.0);
1177 }
1178 }
1179 } else if node.style.text_align == strata_style::types::TextAlign::Right {
1180 let free_space = content_width - child.geometry.border_box.width;
1181 if free_space > 0.0 {
1182 let target_x = node.geometry.border_box.x + border.left + padding.left + free_space;
1183 let dx = target_x - child.geometry.border_box.x;
1184 if dx != 0.0 {
1185 translate_node_and_descendants(child, dx, 0.0);
1186 }
1187 }
1188 }
1189
1190 current_y += child.geometry.border_box.height;
1191 pending_margin = Some(child_margin.bottom);
1192 last_child_establishes_bfc = child_establishes_bfc;
1193 }
1194
1195 if let Some(trailing_margin) = pending_margin {
1196 if parent_bottom_adjoining && !last_child_establishes_bfc {
1197 node.style.margin.bottom = collapse_margins(node.style.margin.bottom, trailing_margin);
1198 } else {
1199 current_y += trailing_margin;
1200 }
1201 }
1202
1203 if (establishes_bfc || is_root_exclusion_context)
1205 && exclusion_space.exclusions().len() > initial_exclusions_count
1206 {
1207 let max_float_bottom = exclusion_space.max_float_bottom();
1208 if max_float_bottom > current_y {
1209 current_y = max_float_bottom;
1210 }
1211 }
1212
1213 current_y += padding.bottom + border.bottom;
1214
1215 current_y
1216}
1217
1218pub fn solve_block(node: &mut LayoutNode, available_width: f32, available_height: f32) {
1220 solve_block_with_exclusions_guarded(
1221 node,
1222 available_width,
1223 available_height,
1224 None,
1225 0.0,
1226 crate::tree::LayoutDepthGuard::root(),
1227 );
1228}
1229
1230pub fn solve_block_with_exclusions(
1232 node: &mut LayoutNode,
1233 available_width: f32,
1234 available_height: f32,
1235 parent_exclusions: Option<&mut crate::exclusions::ExclusionSpace>,
1236 bfc_offset_y: f32,
1237) {
1238 solve_block_with_exclusions_guarded(
1239 node,
1240 available_width,
1241 available_height,
1242 parent_exclusions,
1243 bfc_offset_y,
1244 crate::tree::LayoutDepthGuard::root(),
1245 );
1246}
1247
1248pub fn solve_block_with_exclusions_guarded(
1250 node: &mut LayoutNode,
1251 available_width: f32,
1252 available_height: f32,
1253 parent_exclusions: Option<&mut crate::exclusions::ExclusionSpace>,
1254 bfc_offset_y: f32,
1255 guard: crate::tree::LayoutDepthGuard,
1256) {
1257 let padding = node.style.padding;
1258 let border = node.style.border_width;
1259 let margin = node.style.margin;
1260
1261 if guard.is_exhausted() {
1262 crate::tree::record_layout_overflow(guard.current_depth(), guard.max_depth());
1263 let border_box_width =
1264 match resolve_dimension_with_node(node.style.width, available_width, node) {
1265 Some(w) => clamp_dimension_with_node(
1266 w,
1267 node.style.min_width,
1268 node.style.max_width,
1269 available_width,
1270 node,
1271 ),
1272 None => (available_width - margin.left - margin.right).max(0.0),
1273 };
1274 let border_box_height = match resolve_dimension(node.style.height, available_height) {
1275 Some(h) => clamp_dimension(
1276 h,
1277 node.style.min_height,
1278 node.style.max_height,
1279 available_height,
1280 ),
1281 None => 0.0,
1282 };
1283 let border_box = Rect::new(
1284 node.geometry.border_box.x,
1285 node.geometry.border_box.y,
1286 border_box_width,
1287 border_box_height,
1288 );
1289 node.geometry = LayoutGeometry::from_border_box(border_box, padding, border, margin);
1290 node.geometry.snap_to_pixel();
1291 return;
1292 }
1293
1294 if node.style.content_visibility == strata_style::types::ContentVisibility::Hidden {
1296 let intrinsic_w = node
1297 .style
1298 .contain_intrinsic_width
1299 .and_then(|dim| resolve_dimension_with_node(dim, available_width, node));
1300 let intrinsic_h = node
1301 .style
1302 .contain_intrinsic_height
1303 .and_then(|dim| resolve_dimension(dim, available_height))
1304 .unwrap_or(0.0);
1305 let border_box_width =
1306 match resolve_dimension_with_node(node.style.width, available_width, node)
1307 .or(intrinsic_w)
1308 {
1309 Some(w) => clamp_dimension_with_node(
1310 w,
1311 node.style.min_width,
1312 node.style.max_width,
1313 available_width,
1314 node,
1315 ),
1316 None => (available_width - margin.left - margin.right).max(0.0),
1317 };
1318 let border_box_height = match resolve_dimension(node.style.height, available_height) {
1319 Some(h) => clamp_dimension(
1320 h,
1321 node.style.min_height,
1322 node.style.max_height,
1323 available_height,
1324 ),
1325 None => clamp_dimension(
1326 intrinsic_h,
1327 node.style.min_height,
1328 node.style.max_height,
1329 available_height,
1330 ),
1331 };
1332 let border_box = Rect::new(
1333 node.geometry.border_box.x,
1334 node.geometry.border_box.y,
1335 border_box_width,
1336 border_box_height,
1337 );
1338 node.geometry = LayoutGeometry::from_border_box(border_box, padding, border, margin);
1339 node.geometry.snap_to_pixel();
1340 return;
1341 }
1342
1343 let inset_h = padding.left + padding.right + border.left + border.right;
1344 let avail_border_w = (available_width - margin.left - margin.right).max(0.0);
1345 let max_border_limit = if let Some(explicit_w) =
1346 resolve_dimension_with_node(node.style.width, available_width, node)
1347 {
1348 let clamped = clamp_dimension_with_node(
1349 explicit_w,
1350 node.style.min_width,
1351 node.style.max_width,
1352 available_width,
1353 node,
1354 );
1355 match node.style.box_sizing {
1356 BoxSizing::BorderBox => clamped,
1357 BoxSizing::ContentBox => clamped + inset_h,
1358 }
1359 } else {
1360 let mut limit = avail_border_w;
1361 if let Some(max_w) =
1362 resolve_dimension_with_node(node.style.max_width, available_width, node)
1363 {
1364 let max_border = match node.style.box_sizing {
1365 BoxSizing::BorderBox => max_w,
1366 BoxSizing::ContentBox => max_w + inset_h,
1367 };
1368 limit = limit.min(max_border.max(0.0));
1369 }
1370 if let Some(min_w) =
1371 resolve_dimension_with_node(node.style.min_width, available_width, node)
1372 {
1373 let min_border = match node.style.box_sizing {
1374 BoxSizing::BorderBox => min_w,
1375 BoxSizing::ContentBox => min_w + inset_h,
1376 };
1377 limit = limit.max(min_border.max(0.0));
1378 }
1379 limit
1380 };
1381 let base_text_avail_w = (max_border_limit - inset_h).max(0.0);
1382 let text_avail_w = node
1383 .transition_content_width()
1384 .map_or(base_text_avail_w, |tw| base_text_avail_w.max(tw));
1385
1386 let (text_w, text_h) = if node.children.is_empty() && !node.inline_items.is_empty() {
1387 let ifc = crate::typography::InlineFormattingContext::new();
1388 let opt = crate::typography::InlineLayoutOptions {
1389 font_size: node.style.font_size,
1390 line_height: Some(node.style.font_size * node.style.line_height),
1391 max_width: Some(text_avail_w),
1392 white_space: match node.style.white_space {
1393 strata_style::types::WhiteSpace::Normal => {
1394 crate::typography::WhiteSpaceMode::Normal
1395 }
1396 strata_style::types::WhiteSpace::NoWrap => {
1397 crate::typography::WhiteSpaceMode::NoWrap
1398 }
1399 strata_style::types::WhiteSpace::Pre => crate::typography::WhiteSpaceMode::Pre,
1400 strata_style::types::WhiteSpace::PreWrap => {
1401 crate::typography::WhiteSpaceMode::PreWrap
1402 }
1403 strata_style::types::WhiteSpace::PreLine => {
1404 crate::typography::WhiteSpaceMode::PreLine
1405 }
1406 },
1407 vertical_align: crate::typography::VerticalAlignment::Baseline,
1408 is_monospace: crate::text::is_monospace_family(node.style.font_family),
1409 exclusion_space: parent_exclusions.as_deref(),
1410 bfc_offset_y,
1411 };
1412 let res = ifc.layout_inline_stream(&node.inline_items, &opt);
1413 (res.total_width, res.total_height)
1414 } else if let Some(ref text) = node.text {
1415 let is_mono = crate::text::is_monospace_family(node.style.font_family);
1416 crate::text::measure_text_bounds_with_white_space(
1417 text,
1418 node.style.font_size,
1419 node.style.line_height,
1420 Some(text_avail_w),
1421 is_mono,
1422 node.style.white_space,
1423 )
1424 } else {
1425 (0.0, 0.0)
1426 };
1427
1428 let resolved_width = match resolve_dimension_with_node(node.style.width, available_width, node)
1430 {
1431 Some(w) => clamp_dimension_with_node(
1432 w,
1433 node.style.min_width,
1434 node.style.max_width,
1435 available_width,
1436 node,
1437 ),
1438 None => {
1439 if node.text.is_some()
1440 || matches!(
1441 node.style.display,
1442 strata_style::types::Display::Inline
1443 | strata_style::types::Display::InlineBlock
1444 )
1445 {
1446 let base_w = if !node.children.is_empty() {
1449 let min_max =
1450 crate::intrinsic::compute_min_max_sizes(node, Some(available_width));
1451 let avail_content_w = (available_width
1452 - padding.left
1453 - padding.right
1454 - border.left
1455 - border.right
1456 - margin.left
1457 - margin.right)
1458 .max(0.0);
1459 min_max
1460 .min_content
1461 .max(avail_content_w)
1462 .min(min_max.max_content)
1463 .max(text_w)
1464 } else {
1465 text_w
1466 };
1467 let mut fit_w = base_w + padding.left + padding.right + border.left + border.right;
1468 if node.style.overflow_x != strata_style::types::Overflow::Visible {
1469 fit_w = fit_w.min(max_border_limit);
1470 }
1471 clamp_dimension_with_node(
1472 fit_w,
1473 node.style.min_width,
1474 node.style.max_width,
1475 available_width,
1476 node,
1477 )
1478 } else {
1479 let auto_w = (available_width - margin.left - margin.right).max(0.0);
1481 clamp_dimension_with_node(
1482 auto_w,
1483 node.style.min_width,
1484 node.style.max_width,
1485 available_width,
1486 node,
1487 )
1488 }
1489 }
1490 };
1491
1492 let border_box_width = match node.style.box_sizing {
1493 BoxSizing::BorderBox => resolved_width,
1494 BoxSizing::ContentBox => {
1495 resolved_width + padding.left + padding.right + border.left + border.right
1496 }
1497 };
1498
1499 let content_width =
1500 (border_box_width - padding.left - padding.right - border.left - border.right).max(0.0);
1501 let child_layout_width = node
1502 .transition_content_width()
1503 .map_or(content_width, |tw| content_width.max(tw));
1504
1505 let children_height = if node.children.is_empty() && node.text.is_some() {
1507 text_h + padding.top + padding.bottom + border.top + border.bottom
1508 } else {
1509 solve_block_children_with_exclusions_guarded(
1510 node,
1511 child_layout_width,
1512 available_height,
1513 parent_exclusions,
1514 bfc_offset_y,
1515 guard,
1516 )
1517 };
1518
1519 let resolved_height = match resolve_dimension(node.style.height, available_height) {
1521 Some(h) => {
1522 let clamped_h = clamp_dimension(
1523 h,
1524 node.style.min_height,
1525 node.style.max_height,
1526 available_height,
1527 );
1528 match node.style.box_sizing {
1529 BoxSizing::BorderBox => clamped_h,
1530 BoxSizing::ContentBox => {
1531 clamped_h + padding.top + padding.bottom + border.top + border.bottom
1532 }
1533 }
1534 }
1535 None => {
1536 clamp_dimension(
1538 children_height,
1539 node.style.min_height,
1540 node.style.max_height,
1541 available_height,
1542 )
1543 }
1544 };
1545
1546 let border_box_height = resolved_height;
1547
1548 let border_box = Rect::new(
1550 node.geometry.border_box.x,
1551 node.geometry.border_box.y,
1552 border_box_width,
1553 border_box_height,
1554 );
1555
1556 node.geometry = LayoutGeometry::from_border_box(border_box, padding, border, node.style.margin);
1557 node.geometry.snap_to_pixel();
1558
1559 if node.children.is_empty()
1562 && node.style.text_overflow == strata_style::types::TextOverflow::Ellipsis
1563 && node.style.overflow_x != strata_style::types::Overflow::Visible
1564 && content_width > 0.0
1565 {
1566 if let Some(ref text) = node.text {
1567 let inline_style = inline_style_from_computed_style(&node.style);
1568 let item = crate::typography::InlineItem::text(text.clone(), inline_style);
1569 let ifc = crate::typography::InlineFormattingContext::new();
1570 let opt = crate::typography::InlineLayoutOptions {
1571 font_size: node.style.font_size,
1572 line_height: Some(node.style.font_size * node.style.line_height),
1573 max_width: Some(content_width),
1574 white_space: match node.style.white_space {
1575 strata_style::types::WhiteSpace::Normal => {
1576 crate::typography::WhiteSpaceMode::Normal
1577 }
1578 strata_style::types::WhiteSpace::NoWrap => {
1579 crate::typography::WhiteSpaceMode::NoWrap
1580 }
1581 strata_style::types::WhiteSpace::Pre => crate::typography::WhiteSpaceMode::Pre,
1582 strata_style::types::WhiteSpace::PreWrap => {
1583 crate::typography::WhiteSpaceMode::PreWrap
1584 }
1585 strata_style::types::WhiteSpace::PreLine => {
1586 crate::typography::WhiteSpaceMode::PreLine
1587 }
1588 },
1589 vertical_align: crate::typography::VerticalAlignment::Baseline,
1590 is_monospace: crate::text::is_monospace_family(node.style.font_family),
1591 exclusion_space: None,
1592 bfc_offset_y: 0.0,
1593 };
1594 let mut res = ifc.layout_inline_stream(&[item], &opt);
1595 truncate_inline_overflow(node, &mut res, content_width);
1596 let origin_x = node.geometry.content_box.x;
1597 let origin_y = node.geometry.content_box.y;
1598 for line in &mut res.lines {
1599 let align_offset_x = match node.style.text_align {
1600 strata_style::types::TextAlign::Center => {
1601 ((content_width - line.bounds.width) / 2.0).max(0.0)
1602 }
1603 strata_style::types::TextAlign::Right => {
1604 (content_width - line.bounds.width).max(0.0)
1605 }
1606 _ => 0.0,
1607 };
1608 line.bounds.x += origin_x + align_offset_x;
1609 line.bounds.y += origin_y;
1610 for frag in &mut line.text_fragments {
1611 frag.bounds.x += origin_x + align_offset_x;
1612 frag.bounds.y += origin_y;
1613 frag.baseline_y += origin_y;
1614 }
1615 }
1616 node.line_boxes = res.lines;
1617 }
1618 }
1619}