use crate::css::{self, ComputedStyle, ContainerType, ElementSignature, Stylesheets};
use crate::dom::Element;
use crate::layout::inline_collect::TextDecorationPropagationContext;
use crate::layout::{
ContainerUnitContext, LayoutBuilder, PhysicalContentHeight, PhysicalContentWidth,
layout_element_signature, style_for_layout_signature_with_parent_ch_advance,
synchronize_resolved_fixed_box_edge_cache,
};
use crate::units::LayoutSize;
impl<'a> LayoutBuilder<'a> {
pub(in crate::layout) fn resolve_style_viewport_lengths(
style: &mut ComputedStyle,
viewport: LayoutSize,
container_physical: LayoutSize,
) {
style.resolve_viewport_lengths_for_viewport_and_container(viewport, container_physical);
if let Some(style) = &mut style.marker_style {
Self::resolve_style_viewport_lengths(style, viewport, container_physical);
}
if let Some(style) = &mut style.before_style {
Self::resolve_style_viewport_lengths(style, viewport, container_physical);
}
if let Some(style) = &mut style.after_style {
Self::resolve_style_viewport_lengths(style, viewport, container_physical);
}
}
pub(in crate::layout) fn style_with_current_viewport_lengths(
&self,
style: &impl css::CascadedStyleSource,
) -> css::ZoomedLayoutStyle {
let mut style = css::LayoutStyle::from_computed(style);
self.resolve_style_current_viewport_lengths(&mut style);
style.into_zoomed()
}
pub(in crate::layout) fn style_with_current_used_lengths(
&mut self,
style: &impl css::CascadedStyleSource,
) -> css::ZoomedLayoutStyle {
let mut style = css::LayoutStyle::from_computed(style);
self.resolve_style_current_viewport_lengths(&mut style);
style.finalize_computed_font_relative_lengths();
self.resolve_style_font_metric_lengths(&mut style);
let mut style = style.into_zoomed();
synchronize_resolved_fixed_box_edge_cache(&mut style);
style
}
pub(in crate::layout) fn resolve_style_current_viewport_lengths(
&self,
style: &mut ComputedStyle,
) {
let viewport = self
.iframe_viewport
.map(|context| context.viewport.layout_size())
.unwrap_or_else(|| {
LayoutSize::new(
self.initial_viewport_context.area_width(),
self.initial_viewport_context.area_height(),
)
});
Self::resolve_style_viewport_lengths(
style,
viewport,
self.current_container_unit_physical(viewport),
);
}
fn current_container_unit_physical(&self, fallback: LayoutSize) -> LayoutSize {
let mut width = None;
let mut height = None;
for context in self.container_unit_contexts.iter().rev().copied() {
if width.is_none() && context.supplies_physical_width() {
width = Some(context.physical_width.points());
}
if height.is_none() && context.supplies_physical_height() {
height = Some(context.physical_height.points());
}
if width.is_some() && height.is_some() {
break;
}
}
LayoutSize::new(
width.unwrap_or(fallback.width),
height.unwrap_or(fallback.height),
)
}
pub(in crate::layout) fn push_container_unit_context(
&mut self,
style: &ComputedStyle,
physical_width: PhysicalContentWidth,
physical_height: PhysicalContentHeight,
) -> bool {
if matches!(style.container_type, ContainerType::Normal) {
return false;
}
self.container_unit_contexts.push(ContainerUnitContext {
physical_width,
physical_height,
writing_mode: style.writing_mode,
container_type: style.container_type,
});
true
}
pub(in crate::layout) fn pop_container_unit_context(&mut self, active: bool) {
if active {
self.container_unit_contexts
.pop()
.expect("container unit scopes must be lexically balanced");
}
}
pub(in crate::layout) fn style_for_layout_element_with_parent_font_metrics(
&mut self,
element: &Element,
signature: ElementSignature,
stylesheets: &Stylesheets<'_>,
parent: Option<&ComputedStyle>,
) -> ComputedStyle {
let ancestors = self.ancestors.clone();
self.style_for_layout_element_with_parent_font_metrics_and_ancestors(
element,
signature,
stylesheets,
parent,
&ancestors,
)
}
pub(in crate::layout) fn style_for_layout_element_with_parent_font_metrics_and_ancestors(
&mut self,
element: &Element,
signature: ElementSignature,
stylesheets: &Stylesheets<'_>,
parent: Option<&ComputedStyle>,
ancestors: &[ElementSignature],
) -> ComputedStyle {
let inheritance_source = parent.cloned().unwrap_or_else(ComputedStyle::initial);
let mut parent_ch_advance = css::fallback_ch_advance_for_style(&inheritance_source);
let signature = layout_element_signature(element, signature, parent);
let inline_style = element.attrs.get("style").map(String::as_str);
let mut style = style_for_layout_signature_with_parent_ch_advance(
signature.clone(),
inline_style,
stylesheets,
parent,
ancestors,
Some(parent_ch_advance),
);
if style
.deferred_font_size
.requires_parent_ch_advance(inheritance_source.font_size)
{
parent_ch_advance = self.font_system.ch_advance(&inheritance_source);
style = style_for_layout_signature_with_parent_ch_advance(
signature.clone(),
inline_style,
stylesheets,
parent,
ancestors,
Some(parent_ch_advance),
);
}
let pseudo_parent_ch_advance = css::fallback_ch_advance_for_style(&style);
css::apply_pseudo_rules_with_parent_ch_advance(
&mut style,
&signature,
stylesheets,
ancestors,
pseudo_parent_ch_advance,
);
if style.pseudo_styles_require_parent_ch_advance() {
let pseudo_parent_ch_advance = self.font_system.ch_advance(&style);
css::apply_pseudo_rules_with_parent_ch_advance(
&mut style,
&signature,
stylesheets,
ancestors,
pseudo_parent_ch_advance,
);
}
self.resolve_deferred_parent_font_metric_font_size(&mut style, &inheritance_source);
self.resolve_deferred_root_font_metric_font_size(&mut style);
self.resolve_style_font_metric_lengths(&mut style);
if let Some(parent_style) = parent {
style =
TextDecorationPropagationContext::from_style(parent_style).used_child_style(&style);
}
style
}
pub(in crate::layout) fn style_for_signature_with_parent_font_metrics(
&mut self,
signature: ElementSignature,
inline_style: Option<&str>,
stylesheets: &Stylesheets<'_>,
parent: Option<&ComputedStyle>,
ancestors: &[ElementSignature],
) -> ComputedStyle {
let inheritance_source = parent.cloned().unwrap_or_else(ComputedStyle::initial);
let mut parent_ch_advance = css::fallback_ch_advance_for_style(&inheritance_source);
let mut style = css::style_for_element_with_signature_and_parent_ch_advance(
signature.clone(),
inline_style,
stylesheets,
parent,
ancestors,
parent_ch_advance,
);
if style
.deferred_font_size
.requires_parent_ch_advance(inheritance_source.font_size)
{
parent_ch_advance = self.font_system.ch_advance(&inheritance_source);
style = css::style_for_element_with_signature_and_parent_ch_advance(
signature.clone(),
inline_style,
stylesheets,
parent,
ancestors,
parent_ch_advance,
);
}
let pseudo_parent_ch_advance = css::fallback_ch_advance_for_style(&style);
css::apply_pseudo_rules_with_parent_ch_advance(
&mut style,
&signature,
stylesheets,
ancestors,
pseudo_parent_ch_advance,
);
if style.pseudo_styles_require_parent_ch_advance() {
let pseudo_parent_ch_advance = self.font_system.ch_advance(&style);
css::apply_pseudo_rules_with_parent_ch_advance(
&mut style,
&signature,
stylesheets,
ancestors,
pseudo_parent_ch_advance,
);
}
self.resolve_deferred_parent_font_metric_font_size(&mut style, &inheritance_source);
self.resolve_deferred_root_font_metric_font_size(&mut style);
self.resolve_style_font_metric_lengths(&mut style);
style
}
}