use super::*;
pub(in crate::layout) type TableHeightEstimateCacheKey =
(ElementId, u32, Option<u32>, Option<u32>, Option<u32>);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(in crate::layout) struct TableHeightPlanCacheKey {
pub(in crate::layout) table_element: ElementId,
pub(in crate::layout) column_width_bits: u32,
pub(in crate::layout) wrapper_border_box_block_size_bits: Option<u32>,
pub(in crate::layout) positioned_table_block_content_size_bits: Option<u32>,
pub(in crate::layout) wrapper_non_grid_block_size_bits: u32,
pub(in crate::layout) target: table::TableHeightDistributionTargetKey,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::layout) enum PendingPaintFragmentKind {
FragmentedFloat,
InFlowOverflow,
PositionedOrScoped,
}
#[derive(Debug, Clone, PartialEq)]
pub(in crate::layout) struct PendingPaintFragment {
pub(in crate::layout) page_index: usize,
pub(in crate::layout) fragment: PaintFragment,
pub(in crate::layout) kind: PendingPaintFragmentKind,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub(in crate::layout) struct PendingPageSideEffects {
pub(in crate::layout) page_index: usize,
pub(in crate::layout) named_strings: HashMap<String, Vec<NamedStringAssignment>>,
pub(in crate::layout) running_elements: HashMap<String, Vec<NamedStringAssignment>>,
pub(in crate::layout) links: Vec<RenderedLink>,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub(in crate::layout) struct DeferredLayoutSideEffects {
pub(in crate::layout) bookmarks: Vec<Bookmark>,
pub(in crate::layout) anchors: Vec<(String, usize)>,
pub(in crate::layout) anchor_source_positions: Vec<(String, PaintPoint)>,
pub(in crate::layout) anchor_text: Vec<(String, AnchorText)>,
pub(in crate::layout) anchor_counters: Vec<(String, HashMap<String, Vec<i32>>)>,
pub(in crate::layout) page_effects: Vec<PendingPageSideEffects>,
}
#[cfg(test)]
pub(in crate::layout) fn block_align_content_y_offset(
align_content: AlignContent,
free_space: f32,
) -> f32 {
content_alignment_y_offset(align_content, free_space, true)
}
pub(in crate::layout) fn block_align_content_y_offset_for_style(
style: &ComputedStyle,
free_space: f32,
) -> f32 {
content_alignment_y_offset(
style.align_content,
free_space,
block_align_content_defaults_to_safe_overflow(style),
)
}
pub(in crate::layout) fn block_align_content_defaults_to_safe_overflow(
style: &ComputedStyle,
) -> bool {
let (overflow_x, overflow_y) = resolved_overflow_axes(style);
match block_start_side(style.writing_mode).axis() {
PhysicalAxis::Horizontal => !overflow_x.is_scrollable(),
PhysicalAxis::Vertical => !overflow_y.is_scrollable(),
}
}
pub(in crate::layout) fn multicol_align_content_y_offset(
align_content: AlignContent,
free_space: f32,
) -> f32 {
content_alignment_y_offset(align_content, free_space, false)
}
pub(in crate::layout) fn content_alignment_y_offset(
align_content: AlignContent,
free_space: f32,
default_safe_overflow: bool,
) -> f32 {
-content_alignment_offset_toward_end(align_content, free_space, default_safe_overflow)
}
pub(in crate::layout) fn content_alignment_offset_toward_end(
align_content: AlignContent,
free_space: f32,
default_safe_overflow: bool,
) -> f32 {
let has_overflow = free_space <= 0.0;
let implicit_safe = matches!(
align_content.keyword,
ContentAlignmentKeyword::Stretch
| ContentAlignmentKeyword::SpaceBetween
| ContentAlignmentKeyword::SpaceAround
| ContentAlignmentKeyword::SpaceEvenly
| ContentAlignmentKeyword::Baseline
| ContentAlignmentKeyword::LastBaseline
);
let safety = match align_content.safety {
AlignmentSafety::Default if default_safe_overflow => AlignmentSafety::Safe,
AlignmentSafety::Default => AlignmentSafety::Unsafe,
explicit => explicit,
};
if has_overflow && (safety == AlignmentSafety::Safe || implicit_safe) {
return 0.0;
}
let factor = match align_content.keyword {
ContentAlignmentKeyword::Normal
| ContentAlignmentKeyword::Start
| ContentAlignmentKeyword::FlexStart
| ContentAlignmentKeyword::Stretch
| ContentAlignmentKeyword::SpaceBetween
| ContentAlignmentKeyword::Baseline => 0.0,
ContentAlignmentKeyword::End
| ContentAlignmentKeyword::FlexEnd
| ContentAlignmentKeyword::LastBaseline => 1.0,
ContentAlignmentKeyword::Center
| ContentAlignmentKeyword::SpaceAround
| ContentAlignmentKeyword::SpaceEvenly => 0.5,
ContentAlignmentKeyword::Left | ContentAlignmentKeyword::Right => 0.0,
};
free_space * factor
}
pub(in crate::layout) fn block_align_content_establishes_independent_formatting_context(
align_content: AlignContent,
) -> bool {
align_content.keyword != ContentAlignmentKeyword::Normal
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct PageSize {
pub(crate) width: LayoutLength,
pub(crate) height: LayoutLength,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct IframeEmbeddingContext {
pub(crate) viewport: PageSize,
pub(crate) effective_zoom: css::EffectiveZoom,
}
pub(in crate::layout) const LIST_ITEM_COUNTER_NAME: &str = "list-item";
impl PageSize {
pub(crate) const A4_POINTS: Self = Self {
width: layout_pt(595.2756),
height: layout_pt(841.8898),
};
pub(crate) const fn from_points(width: f32, height: f32) -> Self {
Self {
width: layout_pt(width),
height: layout_pt(height),
}
}
pub(crate) fn width(&self) -> f32 {
layout_points(self.width)
}
pub(crate) fn height(&self) -> f32 {
layout_points(self.height)
}
pub(crate) fn layout_size(self) -> crate::units::LayoutSize {
crate::units::LayoutSize::new(layout_points(self.width), layout_points(self.height))
}
pub(crate) fn has_positive_area(self) -> bool {
self.width().is_finite()
&& self.height().is_finite()
&& self.width() > 0.0
&& self.height() > 0.0
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct PageMargins {
pub(crate) top: LayoutLength,
pub(crate) right: LayoutLength,
pub(crate) bottom: LayoutLength,
pub(crate) left: LayoutLength,
}
impl PageMargins {
pub(crate) const WEASYPRINT_DEFAULT_POINTS: f32 = 56.25;
pub(crate) const DEFAULT: Self = Self {
top: layout_pt(Self::WEASYPRINT_DEFAULT_POINTS),
right: layout_pt(Self::WEASYPRINT_DEFAULT_POINTS),
bottom: layout_pt(Self::WEASYPRINT_DEFAULT_POINTS),
left: layout_pt(Self::WEASYPRINT_DEFAULT_POINTS),
};
pub(crate) const fn all(length: LayoutLength) -> Self {
Self {
top: length,
right: length,
bottom: length,
left: length,
}
}
pub(crate) const fn all_points(value: f32) -> Self {
Self::all(layout_pt(value))
}
pub(crate) const fn from_points(top: f32, right: f32, bottom: f32, left: f32) -> Self {
Self {
top: layout_pt(top),
right: layout_pt(right),
bottom: layout_pt(bottom),
left: layout_pt(left),
}
}
pub(crate) fn top(&self) -> f32 {
layout_points(self.top)
}
pub(crate) fn right(&self) -> f32 {
layout_points(self.right)
}
pub(crate) fn bottom(&self) -> f32 {
layout_points(self.bottom)
}
pub(crate) fn left(&self) -> f32 {
layout_points(self.left)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct RenderOptions {
pub media_type: crate::css::MediaType,
pub color_scheme_preference: crate::css::ColorSchemePreference,
pub forced_colors: crate::css::ForcedColorsMode,
pub(crate) device_resolution_dppx: f32,
pub(crate) page_size: PageSize,
pub(crate) iframe_page_margins: Option<PageMargins>,
pub(crate) font_size: LayoutLength,
pub(crate) line_height: LayoutLength,
pub target_fragment: Option<String>,
}
impl RenderOptions {
pub fn initial_viewport_size(&self) -> crate::css::CssViewportSize {
crate::css::CssViewportSize::new(
self.page_size.width() / crate::css::CSS_PX_TO_PT,
self.page_size.height() / crate::css::CSS_PX_TO_PT,
)
}
pub fn set_initial_viewport_size(
&mut self,
viewport: crate::css::CssViewportSize,
) -> crate::Result<()> {
if !viewport.width.is_finite()
|| !viewport.height.is_finite()
|| viewport.width <= 0.0
|| viewport.height <= 0.0
{
return Err(crate::Error::InvalidInput(
"initial viewport dimensions must be finite and greater than zero".to_string(),
));
}
self.page_size = PageSize::from_points(
viewport.width * crate::css::CSS_PX_TO_PT,
viewport.height * crate::css::CSS_PX_TO_PT,
);
Ok(())
}
pub fn device_resolution_dppx(&self) -> f32 {
self.device_resolution_dppx
}
pub fn set_device_resolution_dppx(&mut self, resolution_dppx: f32) -> crate::Result<()> {
if !resolution_dppx.is_finite() || resolution_dppx <= 0.0 {
return Err(crate::Error::InvalidInput(
"device resolution must be finite and greater than zero".to_string(),
));
}
self.device_resolution_dppx = resolution_dppx;
Ok(())
}
pub fn font_size(&self) -> f32 {
layout_points(self.font_size)
}
pub fn line_height(&self) -> f32 {
layout_points(self.line_height)
}
pub(crate) fn media_environment(&self) -> crate::css::MediaEnvironment {
crate::css::MediaEnvironment::new(
self.media_type,
crate::css::CssViewportSize::new(
self.page_size.width() / crate::css::CSS_PX_TO_PT,
self.page_size.height() / crate::css::CSS_PX_TO_PT,
),
)
.with_resolution_dppx(self.device_resolution_dppx)
.with_forced_colors(self.forced_colors)
.with_color_scheme_preference(self.color_scheme_preference)
}
}
impl Default for RenderOptions {
fn default() -> Self {
let font_size = 12.0;
Self {
media_type: crate::css::MediaType::Print,
color_scheme_preference: crate::css::ColorSchemePreference::None,
forced_colors: crate::css::ForcedColorsMode::Inactive,
device_resolution_dppx: 1.0,
page_size: PageSize::A4_POINTS,
iframe_page_margins: None,
font_size: layout_pt(font_size),
line_height: layout_pt(font_size * 1.2),
target_fragment: None,
}
}
}
pub(crate) fn start_font_system_load() -> FontSystemLoad {
FontSystem::start_loading()
}
pub(in crate::layout) enum LayoutResult {
Document(LayoutPass),
}
pub(in crate::layout) struct LayoutPass {
pub(in crate::layout) document: Document,
pub(in crate::layout) target_references: TargetReferenceSnapshot,
pub(in crate::layout) has_normal_flow_target_references: bool,
}
pub(crate) struct PreparedDomLayout<'a> {
pub(crate) root: &'a Node,
pub(crate) stylesheets: Stylesheets<'a>,
pub(crate) options: &'a RenderOptions,
pub(crate) document_url: Option<&'a url::Url>,
pub(crate) base_url: Option<&'a url::Url>,
pub(crate) root_url: Option<&'a url::Url>,
pub(crate) resource_cache: &'a ResourceCache,
pub(crate) iframe_documents: &'a HashMap<crate::dom::ElementId, Document>,
pub(crate) iframe_viewport: Option<IframeEmbeddingContext>,
pub(crate) font_system: FontSystem,
}
pub(crate) fn layout_prepared_dom(config: PreparedDomLayout<'_>) -> Document {
let PreparedDomLayout {
root,
stylesheets,
options,
document_url,
base_url,
root_url,
resource_cache,
iframe_documents,
iframe_viewport,
mut font_system,
} = config;
let _timer = DebugTimer::start("layout pipeline");
#[cfg(feature = "layout-profile")]
let _layout_profile_document = super::layout_profile::begin_document();
prime_selector_snapshots(root, document_url, base_url);
let default_line_height_multiplier = if options.font_size() > 0.0 {
options.line_height() / options.font_size()
} else {
1.2
};
let mut parent_style = ComputedStyle::initial();
parent_style.font_size = options.font_size();
parent_style.deferred_font_size = css::DeferredFontSize::Absolute(options.font_size());
parent_style.line_height_value =
css::ComputedLineHeight::Number(default_line_height_multiplier);
parent_style.line_height = options.line_height();
parent_style.color = CssColor::BLACK;
if let Some(context) = iframe_viewport {
parent_style.effective_zoom = context.effective_zoom;
}
let parent_style = Box::new(parent_style);
resource_cache.set_inline_svg_presentation_overrides(inline_svg_presentation_overrides(
root,
&stylesheets,
parent_style.as_ref(),
));
let mut page_margin_inherited_style = {
let _timer = DebugTimer::start("building deferred page-margin inheritance");
document_root_style(root, &stylesheets, parent_style.as_ref())
};
let page_box = {
let _timer = DebugTimer::start("building deferred formatting box tree");
Box::new(box_tree::build_page_box(
root,
&stylesheets,
parent_style.as_ref(),
))
};
let document_canvas_resolution = DocumentCanvasResolution::from_page_box(page_box.as_ref());
let principal_flow = document_canvas_resolution.principal_flow();
let page_progression_direction = principal_flow.page_progression_direction();
let parent_ch_advance = if page_margin_inherited_style
.deferred_font_size
.requires_parent_ch_advance(parent_style.font_size)
{
font_system.ch_advance(parent_style.as_ref())
} else {
css::fallback_ch_advance_for_style(parent_style.as_ref())
};
page_margin_inherited_style.resolve_deferred_font_size(css::FontRelativeLengthBasis::new(
layout_pt(parent_style.font_size),
parent_ch_advance,
));
let root_ch_advance = if page_margin_inherited_style.requires_ch_advance() {
font_system.ch_advance(&page_margin_inherited_style)
} else {
css::fallback_ch_advance_for_style(&page_margin_inherited_style)
};
page_margin_inherited_style.resolve_font_metric_lengths(root_ch_advance);
const MAX_TARGET_REFERENCE_PASSES: usize = 8;
let mut target_references = TargetReferenceSnapshot::default();
let mut seen_target_snapshots = Vec::new();
let mut last_pass = None;
for pass_index in 0..MAX_TARGET_REFERENCE_PASSES {
let page_box = Box::new(box_tree::build_page_box(
root,
&stylesheets,
parent_style.as_ref(),
));
let LayoutResult::Document(pass) = layout_dom_with_font_system(
root,
&stylesheets,
options,
base_url,
root_url,
resource_cache,
iframe_documents,
iframe_viewport,
parent_style.clone(),
font_system.clone(),
page_progression_direction,
principal_flow,
document_canvas_resolution,
page_margin_inherited_style.clone(),
page_box,
target_references.clone(),
);
if !pass.has_normal_flow_target_references || pass.target_references == target_references {
return pass.document;
}
if seen_target_snapshots
.iter()
.any(|previous| previous == &pass.target_references)
{
log::warn!(
"normal-flow generated target references did not converge after {} layout passes; retaining the last complete pass",
pass_index + 1
);
return pass.document;
}
seen_target_snapshots.push(target_references);
target_references = pass.target_references.clone();
last_pass = Some(pass);
}
log::warn!(
"normal-flow generated target references exceeded {MAX_TARGET_REFERENCE_PASSES} layout passes; retaining the last complete pass"
);
last_pass
.expect("a target-reference layout loop always produces a first pass")
.document
}
fn inline_svg_presentation_overrides(
root: &Node,
stylesheets: &Stylesheets<'_>,
parent_style: &ComputedStyle,
) -> crate::svg::SvgPresentationOverrides {
let NodeKind::Element(root_element) = &root.kind else {
return crate::svg::SvgPresentationOverrides::new();
};
let mut overrides = crate::svg::SvgPresentationOverrides::new();
let forced_color_palette = stylesheets
.iter()
.find_map(|stylesheet| stylesheet.forced_colors.palette());
let svg_source_has_system_color = inline_svg_has_system_color(root);
let sibling_tags = element_sibling_signature_list(root_element);
let mut element_index = 0;
for child in &root_element.children {
let NodeKind::Element(element) = &child.kind else {
continue;
};
let signature =
ElementSignature::from_sibling_snapshot(element_index, sibling_tags.clone())
.expect("source child must have a cached sibling signature");
element_index += 1;
collect_inline_svg_presentation_overrides(
element,
signature,
stylesheets,
parent_style,
&[],
false,
forced_color_palette,
svg_source_has_system_color,
&mut overrides,
);
}
overrides
}
#[allow(clippy::too_many_arguments)]
fn collect_inline_svg_presentation_overrides(
element: &Element,
signature: ElementSignature,
stylesheets: &Stylesheets<'_>,
parent_style: &ComputedStyle,
ancestors: &[ElementSignature],
inside_inline_svg: bool,
forced_color_palette: Option<css::ForcedColorPalette>,
svg_source_has_system_color: bool,
overrides: &mut crate::svg::SvgPresentationOverrides,
) {
let svg_presentation = svg_transform_presentation_declarations(element);
let style = css::style_for_element_with_signature_and_svg_presentation(
layout_element_signature(element, signature.clone(), Some(parent_style)),
element.attrs.get("style").map(String::as_str),
svg_presentation
.has_declarations()
.then_some(&svg_presentation),
stylesheets,
Some(parent_style),
ancestors,
);
let signature = layout_element_signature(element, signature, Some(parent_style));
let enters_inline_svg = !inside_inline_svg
&& element.namespace_url == "http://www.w3.org/2000/svg"
&& element.tag == "svg";
let applies_to_svg_scene = inside_inline_svg || enters_inline_svg;
if applies_to_svg_scene {
let transform = if !enters_inline_svg
&& !style.has_transform()
&& svg_presentation.has_valid_transform()
{
Some(crate::svg::SvgTransformOverride::Scene(
crate::svg::SvgUsedTransform::None,
))
} else {
(!enters_inline_svg && style.has_transform())
.then(|| svg_css_transform_is_resolvable(&style))
.filter(|resolvable| *resolvable)
.and_then(|_| svg_css_transform_for_element(&style, element))
.map(|transform| {
crate::svg::SvgTransformOverride::Scene(crate::svg::SvgUsedTransform::Affine(
transform,
))
})
};
let force_colors = forced_color_palette.filter(|_| {
style.forced_color_adjust == css::ForcedColorAdjust::Auto
&& !svg_source_has_system_color
});
let presentation = crate::svg::SvgPresentationOverride {
display: svg_display_override(element, &style, enters_inline_svg),
transform,
fill: force_colors
.map(|palette| svg_presentation_paint(Some(palette.canvas_text)))
.or_else(|| {
style
.svg_fill
.is_overridden()
.then(|| svg_presentation_paint(style.svg_fill.paint.resolve(style.color)))
}),
stroke: force_colors
.filter(|_| !matches!(style.svg_stroke.paint, css::SvgPaint::None))
.map(|palette| svg_presentation_paint(Some(palette.canvas_text)))
.or_else(|| {
style.svg_stroke.is_overridden().then(|| {
svg_presentation_paint(style.svg_stroke.paint.resolve(style.color))
})
})
.or_else(|| {
enters_inline_svg.then(|| {
svg_presentation_paint(style.svg_stroke.paint.resolve(style.color))
})
}),
stroke_width: style.svg_stroke_width.is_overridden().then(|| {
format!(
"{}px",
style.svg_stroke_width.value().length_points() / css::CSS_PX_TO_PT
)
}),
font_family: Some(crate::svg::svg_font_family_presentation_attribute(
&style.font_family,
)),
font_size: Some(format!("{}px", style.font_size / css::CSS_PX_TO_PT)),
font_weight: Some(style.font_weight.0.to_string()),
font_style: Some({
match style.font_style {
css::FontStyle::Normal => "normal".to_owned(),
css::FontStyle::Italic => "italic".to_owned(),
css::FontStyle::Oblique(angle) => {
format!("oblique {}deg", f32::from_bits(angle))
}
}
}),
font_stretch: Some(format!("{}%", style.font_width.0 as f32 / 10.0)),
font_variation_settings: Some(
crate::svg::svg_font_variation_settings_presentation_attribute(
&style.font_variation_settings,
),
),
font_kerning: Some({
match style.font_kerning {
css::FontKerning::Auto => "auto",
css::FontKerning::Normal => "normal",
css::FontKerning::None => "none",
}
.to_owned()
}),
letter_spacing: Some(format!(
"{}px",
style.used_letter_spacing().points() / css::CSS_PX_TO_PT
)),
word_spacing: Some(format!(
"{}px",
style.used_word_spacing().points() / css::CSS_PX_TO_PT
)),
writing_mode: Some({
match style.writing_mode {
css::WritingMode::HorizontalTb => "horizontal-tb",
css::WritingMode::VerticalRl => "vertical-rl",
css::WritingMode::VerticalLr => "vertical-lr",
css::WritingMode::SidewaysRl => "sideways-rl",
css::WritingMode::SidewaysLr => "sideways-lr",
}
.to_owned()
}),
text_orientation: Some(
match style.text_orientation {
css::TextOrientation::Mixed => "mixed",
css::TextOrientation::Upright => "upright",
css::TextOrientation::Sideways => "sideways",
}
.to_owned(),
),
direction: Some(match style.direction {
css::Direction::Ltr => "ltr".to_owned(),
css::Direction::Rtl => "rtl".to_owned(),
}),
unicode_bidi: Some({
match style.unicode_bidi {
css::UnicodeBidi::Normal => "normal",
css::UnicodeBidi::Embed => "embed",
css::UnicodeBidi::Isolate => "isolate",
css::UnicodeBidi::BidiOverride => "bidi-override",
css::UnicodeBidi::IsolateOverride => "isolate-override",
css::UnicodeBidi::Plaintext => "plaintext",
}
.to_owned()
}),
flood_color: svg_filter_color_element(element)
.then(|| crate::svg::SvgFilterColorOverride::from(style.svg_flood_color)),
lighting_color: svg_lighting_color_element(element)
.then(|| crate::svg::SvgFilterColorOverride::from(style.svg_lighting_color)),
remove_filter: force_colors.is_some()
&& (element.attrs.contains_key("filter")
|| element
.attrs
.get("style")
.is_some_and(|style| style.to_ascii_lowercase().contains("filter"))),
};
if presentation.display.is_some()
|| presentation.transform.is_some()
|| presentation.fill.is_some()
|| presentation.stroke.is_some()
|| presentation.stroke_width.is_some()
|| presentation.font_family.is_some()
|| presentation.font_size.is_some()
|| presentation.font_weight.is_some()
|| presentation.font_style.is_some()
|| presentation.font_stretch.is_some()
|| presentation.font_variation_settings.is_some()
|| presentation.font_kerning.is_some()
|| presentation.letter_spacing.is_some()
|| presentation.word_spacing.is_some()
|| presentation.writing_mode.is_some()
|| presentation.text_orientation.is_some()
|| presentation.direction.is_some()
|| presentation.unicode_bidi.is_some()
|| presentation.flood_color.is_some()
|| presentation.lighting_color.is_some()
{
overrides.insert(element.id, presentation);
}
}
let mut child_ancestors = ancestors.to_vec();
child_ancestors.push(signature);
let sibling_tags = element_sibling_signature_list(element);
let mut element_index = 0;
for child in &element.children {
let NodeKind::Element(child) = &child.kind else {
continue;
};
let signature =
ElementSignature::from_sibling_snapshot(element_index, sibling_tags.clone())
.expect("source child must have a cached sibling signature");
element_index += 1;
collect_inline_svg_presentation_overrides(
child,
signature,
stylesheets,
&style,
&child_ancestors,
inside_inline_svg || enters_inline_svg,
forced_color_palette,
svg_source_has_system_color,
overrides,
);
}
}
fn svg_display_override(
element: &Element,
style: &ComputedStyle,
enters_inline_svg: bool,
) -> Option<crate::svg::SvgDisplayOverride> {
if style.display.is_none() {
return Some(crate::svg::SvgDisplayOverride::None);
}
if !style.display.is_contents() {
return None;
}
if enters_inline_svg {
return Some(crate::svg::SvgDisplayOverride::None);
}
match element.tag.as_str() {
"svg" | "g" | "a" | "switch" | "tspan" | "textPath" => {
Some(crate::svg::SvgDisplayOverride::Contents)
}
"use" => Some(crate::svg::SvgDisplayOverride::UseContents),
_ => Some(crate::svg::SvgDisplayOverride::None),
}
}
fn inline_svg_has_system_color(node: &Node) -> bool {
const SYSTEM_COLORS: &[&str] = &[
"accentcolor",
"accentcolortext",
"activetext",
"buttonborder",
"buttonface",
"buttontext",
"canvas",
"canvastext",
"field",
"fieldtext",
"graytext",
"highlight",
"highlighttext",
"linktext",
"mark",
"marktext",
"selecteditem",
"selecteditemtext",
"visitedtext",
"window",
"windowtext",
];
let NodeKind::Element(element) = &node.kind else {
return false;
};
element.attrs.values().any(|value| {
SYSTEM_COLORS
.iter()
.any(|system_color| value.eq_ignore_ascii_case(system_color))
}) || element.children.iter().any(inline_svg_has_system_color)
}
fn svg_presentation_paint(color: Option<CssColor>) -> String {
let Some(color) = color else {
return "none".to_owned();
};
format!(
"rgba({}, {}, {}, {})",
(color.components()[0] * 255.0).round().clamp(0.0, 255.0),
(color.components()[1] * 255.0).round().clamp(0.0, 255.0),
(color.components()[2] * 255.0).round().clamp(0.0, 255.0),
color.alpha()
)
}
fn svg_filter_color_element(element: &Element) -> bool {
element.namespace_url == "http://www.w3.org/2000/svg"
&& matches!(element.tag.as_str(), "feFlood" | "feDropShadow")
}
fn svg_lighting_color_element(element: &Element) -> bool {
element.namespace_url == "http://www.w3.org/2000/svg"
&& matches!(
element.tag.as_str(),
"feDiffuseLighting" | "feSpecularLighting"
)
}
fn svg_transform_presentation_declarations(
element: &Element,
) -> css::SvgPresentationAttributeDeclarations {
const TEXT_PRESENTATION_ATTRIBUTES: &[&str] = &[
"font-family",
"font-size",
"font-size-adjust",
"font-weight",
"font-style",
"font-stretch",
"font-language-override",
"font-synthesis",
"font-synthesis-weight",
"font-synthesis-style",
"font-synthesis-small-caps",
"font-synthesis-position",
"font-feature-settings",
"font-variation-settings",
"font-kerning",
"font-variant",
"font-variant-ligatures",
"font-variant-position",
"font-variant-caps",
"font-variant-numeric",
"font-variant-alternates",
"font-variant-east-asian",
"font-variant-emoji",
"font-palette",
"letter-spacing",
"word-spacing",
"direction",
"unicode-bidi",
"writing-mode",
"text-orientation",
];
let text_presentation_attributes = TEXT_PRESENTATION_ATTRIBUTES
.iter()
.filter_map(|name| {
element
.attrs
.get(*name)
.map(|value| (*name, value.as_str()))
})
.collect::<Vec<_>>();
css::SvgPresentationAttributeDeclarations::svg_properties(
element.attrs.get("transform").map(String::as_str),
element.attrs.get("transform-origin").map(String::as_str),
element.attrs.get("transform-box").map(String::as_str),
element.attrs.get("flood-color").map(String::as_str),
element.attrs.get("lighting-color").map(String::as_str),
&text_presentation_attributes,
)
}
fn svg_css_transform_for_element(
style: &ComputedStyle,
element: &Element,
) -> Option<crate::svg::SvgElementTransform> {
let Some(reference_boxes) = svg_rect_transform_reference_boxes(element, style) else {
return svg_css_transform_without_reference_box(style);
};
let reference_box = reference_boxes
.select(style.transform_box)
.or_else(|| reference_boxes.select(css::TransformBox::FillBox))?;
let rect = reference_box.rect();
let origin = if style.transform_origin.is_initial {
reference_box.origin(0.0, 0.0)
} else {
reference_box.origin(
svg_css_used_length_in_user_units(style.transform_origin.x.clone(), rect.width()),
svg_css_used_length_in_user_units(style.transform_origin.y.clone(), rect.height()),
)
};
let transform = crate::layout::assets::compose_css_transform_matrix(
origin.point(),
style.individual_transforms.clone(),
&style.transform,
|function| svg_css_transform_function_matrix_for_box(function, rect.width(), rect.height()),
);
Some(transform)
}
fn svg_css_transform_without_reference_box(
style: &ComputedStyle,
) -> Option<crate::svg::SvgElementTransform> {
let origin_is_resolvable = style.transform_origin.is_initial
|| (svg_css_length_is_absolute(&style.transform_origin.x)
&& svg_css_length_is_absolute(&style.transform_origin.y));
let transforms_are_resolvable =
style
.individual_transforms
.translate
.as_ref()
.is_none_or(|translation| {
svg_css_length_is_absolute(&translation.x)
&& svg_css_length_is_absolute(&translation.y)
})
&& style.transform.iter().all(|function| match function {
css::TransformFunction::Translate(translation) => {
svg_css_length_is_absolute(&translation.x)
&& svg_css_length_is_absolute(&translation.y)
}
css::TransformFunction::Matrix(_)
| css::TransformFunction::Scale(_)
| css::TransformFunction::Rotate(..)
| css::TransformFunction::Skew(_) => true,
css::TransformFunction::Matrix3D(_)
| css::TransformFunction::Translate3D(_)
| css::TransformFunction::Scale3D(_)
| css::TransformFunction::Rotate3D(_)
| css::TransformFunction::Perspective(_) => false,
});
if !(origin_is_resolvable && transforms_are_resolvable) {
return None;
}
let origin = if style.transform_origin.is_initial {
crate::svg::SvgElementPoint::new(0.0, 0.0)
} else {
crate::svg::SvgElementPoint::new(
svg_css_length_in_user_units(style.transform_origin.x.clone()),
svg_css_length_in_user_units(style.transform_origin.y.clone()),
)
};
let transform = crate::layout::assets::compose_css_transform_matrix(
origin,
style.individual_transforms.clone(),
&style.transform,
svg_css_transform_function_matrix,
);
Some(transform)
}
fn svg_rect_transform_reference_boxes(
element: &Element,
style: &ComputedStyle,
) -> Option<crate::svg::SvgTransformReferenceBoxes> {
match style.transform_box {
css::TransformBox::FillBox
| css::TransformBox::ContentBox
| css::TransformBox::BorderBox => {
let x = element
.attrs
.get("x")
.map_or(Some(0.0), |value| value.parse().ok())?;
let y = element
.attrs
.get("y")
.map_or(Some(0.0), |value| value.parse().ok())?;
let width = element.attrs.get("width")?.parse().ok()?;
let height = element.attrs.get("height")?.parse().ok()?;
if element.tag != "rect" || width < 0.0 || height < 0.0 {
return None;
}
let fill = crate::svg::SvgElementRect::new(
crate::svg::SvgElementPoint::new(x, y),
crate::svg::SvgElementSize::new(width, height),
);
let view = crate::svg::SvgElementRect::new(
crate::svg::SvgElementPoint::new(0.0, 0.0),
crate::svg::SvgElementSize::new(width, height),
);
Some(crate::svg::SvgTransformReferenceBoxes::new(
fill,
fill,
Some(view),
))
}
css::TransformBox::StrokeBox | css::TransformBox::ViewBox => None,
}
}
fn svg_css_used_length_in_user_units(value: css::ComputedLengthPercentage, basis: f32) -> f32 {
used_length_percentage(
value,
PercentageBasis::definite(layout_pt(basis * css::CSS_PX_TO_PT)),
)
.points()
/ css::CSS_PX_TO_PT
}
fn svg_css_transform_function_matrix_for_box(
function: css::TransformFunction,
width: f32,
height: f32,
) -> SvgCssTransform {
match function {
css::TransformFunction::Translate(translation) => SvgCssTransform::translation(
svg_css_used_length_in_user_units(translation.x, width),
svg_css_used_length_in_user_units(translation.y, height),
),
function => svg_css_transform_function_matrix(function),
}
}
fn svg_css_transform_is_resolvable(style: &ComputedStyle) -> bool {
style.transform.iter().all(|function| match function {
css::TransformFunction::Matrix(_)
| css::TransformFunction::Translate(_)
| css::TransformFunction::Scale(_)
| css::TransformFunction::Rotate(..)
| css::TransformFunction::Skew(_) => true,
css::TransformFunction::Matrix3D(_)
| css::TransformFunction::Translate3D(_)
| css::TransformFunction::Scale3D(_)
| css::TransformFunction::Rotate3D(_)
| css::TransformFunction::Perspective(_) => false,
})
}
fn svg_css_length_is_absolute(value: &css::ComputedLengthPercentage) -> bool {
value.is_definitely_absolute()
}
type SvgCssTransform = crate::svg::SvgElementTransform;
fn svg_css_transform_function_matrix(function: css::TransformFunction) -> SvgCssTransform {
match function {
css::TransformFunction::Matrix(matrix) => matrix.into_space(euclid::Scale::new(1.0)),
css::TransformFunction::Translate(translation) => SvgCssTransform::translation(
svg_css_length_in_user_units(translation.x),
svg_css_length_in_user_units(translation.y),
),
css::TransformFunction::Scale(scale) => SvgCssTransform::scale(scale.x, scale.y),
css::TransformFunction::Rotate(angle) => SvgCssTransform::rotation(angle),
css::TransformFunction::Skew(angles) => SvgCssTransform::new(
1.0,
angles.y.radians.tan(),
angles.x.radians.tan(),
1.0,
0.0,
0.0,
),
css::TransformFunction::Matrix3D(_)
| css::TransformFunction::Translate3D(_)
| css::TransformFunction::Scale3D(_)
| css::TransformFunction::Rotate3D(_)
| css::TransformFunction::Perspective(_) => SvgCssTransform::identity(),
}
}
fn svg_css_length_in_user_units(value: css::ComputedLengthPercentage) -> f32 {
used_length_percentage(value, PercentageBasis::definite(layout_pt(0.0))).points()
/ css::CSS_PX_TO_PT
}
#[cfg(test)]
mod svg_css_transform_tests {
use super::*;
#[test]
fn css_matrix_projects_to_svg_source_units_without_paint_conversion() {
let transform = svg_css_transform_function_matrix(css::TransformFunction::Matrix(
css::CssAffineMatrix::new(1.0, 0.0, 0.0, 1.0, 10.0, 20.0),
));
assert_eq!(transform.m31, 10.0);
assert_eq!(transform.m32, 20.0);
}
#[test]
fn svg_transform_eligibility_rejects_relative_and_deferred_lengths() {
assert!(svg_css_length_is_absolute(
&css::ComputedLengthPercentage::from_points(10.0)
));
assert!(!svg_css_length_is_absolute(
&css::ComputedLengthPercentage::from_percent(0.0)
));
assert!(!svg_css_length_is_absolute(
&css::ComputedLengthPercentage::from_em(1.0)
));
}
}
#[expect(
clippy::too_many_arguments,
reason = "This helper carries layout pipeline state out of the async frame."
)]
fn layout_dom_with_font_system(
_root: &Node,
stylesheets: &Stylesheets<'_>,
options: &RenderOptions,
base_url: Option<&url::Url>,
root_url: Option<&url::Url>,
resource_cache: &ResourceCache,
iframe_documents: &HashMap<crate::dom::ElementId, Document>,
iframe_viewport: Option<IframeEmbeddingContext>,
parent_style: Box<ComputedStyle>,
font_system: FontSystem,
page_progression_direction: Direction,
principal_flow: DocumentPrincipalFlow,
document_canvas_resolution: DocumentCanvasResolution,
page_margin_inherited_style: ComputedStyle,
mut page_box: Box<box_tree::MutablePageBox<'_>>,
target_references: TargetReferenceSnapshot,
) -> LayoutResult {
let _timer = DebugTimer::start("building and flowing page box content");
let mut builder = Box::new(LayoutBuilder::new(LayoutBuilderConfig {
options,
stylesheets: *stylesheets,
base_url,
root_url,
resource_cache,
iframe_documents,
iframe_viewport,
page_progression_direction,
page_counter_initial_values: HashMap::new(),
target_references,
font_system,
}));
builder.page_margin_inherited_style = page_margin_inherited_style;
builder.principal_flow = principal_flow;
builder.document_canvas_overflow = document_canvas_resolution;
builder.initial_containing_block_writing_mode = principal_flow.writing_mode;
builder.containing_block_writing_mode = principal_flow.writing_mode;
builder.containing_block_direction = principal_flow.used_direction();
builder.document_root_generates_box = !builder.page_margin_inherited_style.display.is_none();
builder.rebuild_empty_current_page_context();
if !builder.document_root_generates_box {
return LayoutResult::Document(builder.finish_boxed());
}
{
let _timer = DebugTimer::start("resolving font-metric lengths");
builder.resolve_font_metric_lengths_in_page_box(page_box.as_mut(), parent_style.as_ref());
for child in &mut page_box.children {
builder.resolve_font_metric_lengths_in_box(child);
}
}
builder.rebuild_empty_current_page_context();
let page_box = {
let _timer = DebugTimer::start("freezing formatting box tree");
Box::new(box_tree::freeze_page_box(*page_box))
};
{
let _timer = DebugTimer::start("planning and flowing page footnotes");
if page_box.footnotes.is_empty() {
builder.layout_page_box(page_box.as_ref(), stylesheets);
} else {
let initial_snapshot = builder.snapshot();
builder.footnote_layout_mode = FootnoteLayoutMode::Measure;
builder.footnote_reservations.clear();
builder.footnote_measurements.clear();
builder.layout_page_box(page_box.as_ref(), stylesheets);
let mut measurements = std::mem::take(&mut builder.footnote_measurements);
let mut reservations =
LayoutBuilder::footnote_reservations_from_measurements(&measurements);
builder.restore(initial_snapshot.clone());
for attempt in 0..8 {
builder.footnote_layout_mode = FootnoteLayoutMode::Render;
builder.footnote_reservations = reservations.clone();
builder.footnote_measurements = measurements.clone();
builder.rendered_footnote_measurements.clear();
builder.layout_page_box(page_box.as_ref(), stylesheets);
let next_measurements = std::mem::take(&mut builder.rendered_footnote_measurements);
let next_reservations =
LayoutBuilder::footnote_reservations_from_measurements(&next_measurements);
if next_reservations == reservations {
break;
}
builder.restore(initial_snapshot.clone());
builder.constrain_footnote_calls_to_observed_pages(&next_measurements);
measurements = next_measurements;
reservations = next_reservations;
if attempt == 7 {
builder.footnote_layout_mode = FootnoteLayoutMode::Render;
builder.footnote_reservations = reservations;
builder.footnote_measurements = measurements;
builder.rendered_footnote_measurements.clear();
builder.layout_page_box(page_box.as_ref(), stylesheets);
break;
}
}
}
}
if !builder.has_renderable_content() {
LayoutResult::Document(builder.finish_boxed())
} else {
let _timer = DebugTimer::start("finalizing laid out document");
LayoutResult::Document(builder.finish_boxed())
}
}
pub(in crate::layout) fn document_root_style(
root: &Node,
stylesheets: &Stylesheets<'_>,
parent_style: &ComputedStyle,
) -> ComputedStyle {
let NodeKind::Element(root_element) = &root.kind else {
return parent_style.clone();
};
let sibling_tags = element_sibling_signature_list(root_element);
let element_index = 0usize;
for child in &root_element.children {
let NodeKind::Element(element) = &child.kind else {
continue;
};
let signature = ElementSignature::from_sibling_snapshot(element_index, sibling_tags)
.expect("source child must have a cached sibling signature");
let style =
style_for_layout_element(element, signature, stylesheets, Some(parent_style), &[]);
return style;
}
parent_style.clone()
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(in crate::layout) enum FootnoteLayoutMode {
#[default]
Measure,
Render,
}
#[derive(Debug, Clone, PartialEq)]
pub(in crate::layout) struct FootnoteMeasurement {
pub(in crate::layout) element: ElementId,
pub(in crate::layout) page_index: usize,
pub(in crate::layout) area_vertical_non_content: f32,
pub(in crate::layout) height: f32,
}
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub(in crate::layout) struct TextBoxLineTrim {
pub(in crate::layout) trims_block_start: bool,
pub(in crate::layout) trims_block_end: bool,
pub(in crate::layout) block_start: f32,
pub(in crate::layout) block_end: f32,
}
impl TextBoxLineTrim {
pub(in crate::layout) fn is_empty(self) -> bool {
!self.trims_block_start && !self.trims_block_end
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(in crate::layout) struct FirstFormattedLineState {
pending_first_formatted_line: bool,
pending_typographic_pseudos: bool,
}
impl FirstFormattedLineState {
pub(in crate::layout) fn for_style(style: &ComputedStyle) -> Self {
Self {
pending_first_formatted_line: true,
pending_typographic_pseudos: style.first_line_style.is_some()
|| style.first_letter_style.is_some(),
}
}
pub(in crate::layout) fn applies_to_next_inline_run(self) -> bool {
self.pending_typographic_pseudos
}
pub(in crate::layout) fn is_pending(self) -> bool {
self.pending_first_formatted_line
}
pub(in crate::layout) fn consume_next_formatted_line(&mut self) {
self.pending_first_formatted_line = false;
self.pending_typographic_pseudos = false;
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout) struct ContainerUnitContext {
pub(in crate::layout) physical_width: PhysicalContentWidth,
pub(in crate::layout) physical_height: PhysicalContentHeight,
pub(in crate::layout) writing_mode: WritingMode,
pub(in crate::layout) container_type: ContainerType,
}
impl ContainerUnitContext {
pub(in crate::layout) fn supplies_physical_width(self) -> bool {
matches!(self.container_type, ContainerType::Size)
|| (matches!(self.container_type, ContainerType::InlineSize)
&& !self.writing_mode.has_vertical_lines())
}
pub(in crate::layout) fn supplies_physical_height(self) -> bool {
matches!(self.container_type, ContainerType::Size)
|| (matches!(self.container_type, ContainerType::InlineSize)
&& self.writing_mode.has_vertical_lines())
}
}
pub(in crate::layout) fn style_without_typographic_first_line_pseudos(
style: &ComputedStyle,
) -> Option<ComputedStyle> {
if style.first_line_style.is_none() && style.first_letter_style.is_none() {
return None;
}
let mut style = style.clone();
style.first_line_style = None;
style.first_line_overrides = css::ModeledLonghandSet::empty();
style.first_letter_style = None;
Some(style)
}
pub(in crate::layout) fn style_with_originating_typographic_pseudos(
anonymous_style: &ComputedStyle,
originating_style: &ComputedStyle,
) -> Option<ComputedStyle> {
if originating_style.first_line_style.is_none()
&& originating_style.first_letter_style.is_none()
{
return None;
}
let mut style = anonymous_style.clone();
style.first_line_style = originating_style.first_line_style.clone();
style.first_line_overrides = originating_style.first_line_overrides.clone();
style.first_letter_style = originating_style.first_letter_style.clone();
Some(style)
}
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub(in crate::layout) struct BlockLayoutOutcome {
pub(in crate::layout) consumed_bottom_margin: LayoutLength,
pub(in crate::layout) margin_collapse_boundary: BlockMarginCollapseBoundary,
pub(in crate::layout) physical_border_box_inline_span: BorderBoxLength,
pub(in crate::layout) static_border_box: Option<PaintRect>,
pub(in crate::layout) clamp_line_slots: usize,
pub(in crate::layout) has_local_continuation_cutoff: bool,
pub(in crate::layout) in_flow_child_fragment_end: Option<InFlowFragmentEnd>,
}
#[derive(Debug, Clone, Copy, Default)]
pub(in crate::layout) struct ClampLineSlotCapture {
pub(in crate::layout) line_slots: usize,
pub(in crate::layout) block_advance: crate::units::ContentBoxLength,
pub(in crate::layout) has_local_continuation_cutoff: bool,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout) struct InFlowFragmentEnd {
pub(in crate::layout) page_index: usize,
pub(in crate::layout) cursor: PageTopBlockPosition,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout) struct BlockEndMarginCollapse {
pub(in crate::layout) child_consumed_margin: LayoutLength,
pub(in crate::layout) collapsed_margin: LayoutLength,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout) struct FragmentOffsets {
pub(in crate::layout) left: f32,
pub(in crate::layout) right: f32,
pub(in crate::layout) top: f32,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout) struct FragmentTopOffset {
first_fragment_start: f32,
continuation_start: f32,
continuation_end: f32,
}
impl FragmentTopOffset {
pub(in crate::layout) const fn unreserved(first_fragment_start: f32) -> Self {
Self {
first_fragment_start,
continuation_start: 0.0,
continuation_end: 0.0,
}
}
pub(in crate::layout) const fn cloned_block_decoration(
first_fragment_start: f32,
continuation_start: f32,
continuation_end: f32,
) -> Self {
Self {
first_fragment_start,
continuation_start,
continuation_end,
}
}
pub(in crate::layout) const fn first_fragment_start(self) -> f32 {
self.first_fragment_start
}
pub(in crate::layout) const fn continuation_start(self) -> f32 {
self.continuation_start
}
pub(in crate::layout) const fn continuation_end(self) -> f32 {
self.continuation_end
}
}
impl FragmentOffsets {
pub(in crate::layout) const ZERO: Self = Self {
left: 0.0,
right: 0.0,
top: 0.0,
};
pub(in crate::layout) fn clear_fragmentainer_block_start(&mut self, axes: FlowAxes) {
match axes.block_start_side() {
PhysicalSide::Top => self.top = 0.0,
PhysicalSide::Right => self.right = 0.0,
PhysicalSide::Left => self.left = 0.0,
PhysicalSide::Bottom => {
unreachable!("page continuation has no bottom-edge cursor");
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::layout::assets::DocumentPageIndex;
#[test]
fn fragment_offsets_only_clear_the_destination_block_start_inset() {
let offsets = FragmentOffsets {
left: 11.0,
right: 13.0,
top: 17.0,
};
let mut horizontal = offsets;
horizontal.clear_fragmentainer_block_start(FlowAxes::new(
WritingMode::HorizontalTb,
Direction::Ltr,
));
assert_eq!(
horizontal,
FragmentOffsets {
top: 0.0,
..offsets
}
);
let mut vertical_rl = offsets;
vertical_rl.clear_fragmentainer_block_start(FlowAxes::new(
WritingMode::VerticalRl,
Direction::Ltr,
));
assert_eq!(
vertical_rl,
FragmentOffsets {
right: 0.0,
..offsets
}
);
let mut vertical_lr = offsets;
vertical_lr.clear_fragmentainer_block_start(FlowAxes::new(
WritingMode::VerticalLr,
Direction::Rtl,
));
assert_eq!(
vertical_lr,
FragmentOffsets {
left: 0.0,
..offsets
}
);
}
#[test]
fn document_canvas_background_sources_remain_distinct() {
assert_ne!(
DocumentCanvasBackgroundSource::Root,
DocumentCanvasBackgroundSource::EligibleBodyFallback
);
}
#[test]
fn render_options_do_not_expose_document_page_geometry() {
let options = RenderOptions::default();
assert_eq!(
PageContext::from_options(&options).margins,
PageMargins::DEFAULT
);
}
#[test]
fn render_options_validate_and_expose_device_resolution() {
let mut options = RenderOptions::default();
assert_eq!(options.device_resolution_dppx(), 1.0);
options.set_device_resolution_dppx(2.0).unwrap();
assert_eq!(options.device_resolution_dppx(), 2.0);
assert_eq!(options.media_environment().resolution_dppx, 2.0);
assert!(options.set_device_resolution_dppx(0.0).is_err());
assert!(options.set_device_resolution_dppx(f32::NAN).is_err());
}
#[test]
fn render_options_expose_and_validate_initial_viewport() {
let mut options = RenderOptions::default();
assert_eq!(
options.initial_viewport_size(),
options.media_environment().viewport
);
options
.set_initial_viewport_size(crate::css::CssViewportSize::new(800.0, 600.0))
.unwrap();
assert_eq!(
options.initial_viewport_size(),
crate::css::CssViewportSize::new(800.0, 600.0)
);
assert_eq!(
options.media_environment().viewport,
crate::css::CssViewportSize::new(800.0, 600.0)
);
assert!(
options
.set_initial_viewport_size(crate::css::CssViewportSize::new(0.0, 600.0))
.is_err()
);
assert!(
options
.set_initial_viewport_size(crate::css::CssViewportSize::new(f32::NAN, 600.0))
.is_err()
);
}
#[test]
fn render_options_carry_color_scheme_preference_into_media_environment() {
let options = RenderOptions {
color_scheme_preference: crate::css::ColorSchemePreference::Dark,
..RenderOptions::default()
};
assert_eq!(
options.media_environment().color_scheme_preference,
crate::css::ColorSchemePreference::Dark,
);
}
#[test]
fn page_size_converts_to_a_physical_layout_size() {
assert_eq!(
PageSize::from_points(300.0, 200.0).layout_size(),
crate::units::LayoutSize::new(300.0, 200.0)
);
}
#[test]
fn page_context_maps_logical_axes_for_vertical_fragmentation() {
let context = PageContext {
size: PageSize::from_points(600.0, 400.0),
margins: PageMargins::all_points(0.0),
edges: PageBoxEdges::ZERO,
rotation: 0,
};
assert_eq!(
context.logical_inline_size(WritingMode::HorizontalTb),
600.0
);
assert_eq!(context.logical_block_size(WritingMode::HorizontalTb), 400.0);
assert_eq!(context.logical_inline_size(WritingMode::VerticalRl), 400.0);
assert_eq!(context.logical_block_size(WritingMode::VerticalRl), 600.0);
assert_eq!(context.logical_inline_size(WritingMode::SidewaysLr), 400.0);
assert_eq!(context.logical_block_size(WritingMode::SidewaysLr), 600.0);
}
#[test]
fn completed_document_canvas_projects_root_track_for_vertical_and_sideways_flows() {
let completion = CompletedDocumentCanvas {
body: None,
source_page: DocumentPageIndex::new(0),
source_block_track: PageInlineSpan::from_edges(0.0, 100.0),
inline_origin: PageTopBlockPosition::new(12.0),
inline_end_inset: layout_pt(0.0),
block_track_occupancy: layout_pt(100.0),
block_end_inset: layout_pt(7.0),
trailing_child_block_margin: layout_pt(11.0),
};
let vertical_rl = WritingModeAxes::new(WritingMode::VerticalRl, Direction::Ltr);
let vertical_lr = WritingModeAxes::new(WritingMode::VerticalLr, Direction::Ltr);
let sideways_rl = WritingModeAxes::new(WritingMode::SidewaysRl, Direction::Ltr);
let sideways_lr = WritingModeAxes::new(WritingMode::SidewaysLr, Direction::Ltr);
assert_eq!(
completion.root_inline_block_track_advance(vertical_rl),
layout_pt(111.0)
);
assert_eq!(
completion.root_inline_block_track_advance(vertical_lr),
layout_pt(107.0)
);
assert_eq!(
completion.root_inline_block_track_advance(sideways_rl),
layout_pt(118.0)
);
assert_eq!(
completion.root_inline_block_track_advance(sideways_lr),
layout_pt(118.0)
);
}
#[test]
fn principal_flow_page_progression_follows_writing_mode_not_inline_direction() {
let principal = |writing_mode, direction| DocumentPrincipalFlow {
writing_mode,
direction,
text_orientation: TextOrientation::Mixed,
source: PrincipalFlowSource::Root,
};
assert_eq!(
principal(WritingMode::HorizontalTb, Direction::Ltr).page_progression_direction(),
Direction::Ltr
);
assert_eq!(
principal(WritingMode::HorizontalTb, Direction::Rtl).page_progression_direction(),
Direction::Rtl
);
assert_eq!(
principal(WritingMode::VerticalRl, Direction::Ltr).page_progression_direction(),
Direction::Rtl
);
assert_eq!(
principal(WritingMode::VerticalRl, Direction::Rtl).page_progression_direction(),
Direction::Rtl
);
assert_eq!(
principal(WritingMode::VerticalLr, Direction::Rtl).page_progression_direction(),
Direction::Ltr
);
assert_eq!(
principal(WritingMode::SidewaysRl, Direction::Ltr).page_progression_direction(),
Direction::Rtl
);
assert_eq!(
principal(WritingMode::SidewaysLr, Direction::Rtl).page_progression_direction(),
Direction::Ltr
);
}
#[test]
fn completed_document_canvas_selects_remaining_or_next_root_track() {
let completion = CompletedDocumentCanvas {
body: None,
source_page: DocumentPageIndex::new(0),
source_block_track: PageInlineSpan::from_edges(0.0, 100.0),
inline_origin: PageTopBlockPosition::new(12.0),
inline_end_inset: layout_pt(0.0),
block_track_occupancy: layout_pt(100.0),
block_end_inset: layout_pt(0.0),
trailing_child_block_margin: layout_pt(0.0),
};
let axes = WritingModeAxes::new(WritingMode::VerticalRl, Direction::Ltr);
assert!(!completion.exhausts_root_block_track(axes, 101.0));
assert!(completion.exhausts_root_block_track(axes, 100.0));
}
#[test]
fn completed_document_canvas_resolves_a_page_local_root_placement() {
let completion = CompletedDocumentCanvas {
body: None,
source_page: DocumentPageIndex::new(2),
source_block_track: PageInlineSpan::from_edges(0.0, 100.0),
inline_origin: PageTopBlockPosition::new(17.0),
inline_end_inset: layout_pt(0.0),
block_track_occupancy: layout_pt(40.0),
block_end_inset: layout_pt(0.0),
trailing_child_block_margin: layout_pt(0.0),
};
let remaining = completion.resolve_root_inline_placement(
WritingModeAxes::new(WritingMode::VerticalLr, Direction::Ltr),
PageInlineSpan::from_edges(0.0, 100.0),
);
assert_eq!(
remaining,
RootInlineCanvasPlacement::RemainingTrack {
block_track: PageInlineSpan::from_edges(40.0, 100.0),
inline_origin: PageTopBlockPosition::new(17.0),
}
);
let next_page = completion.resolve_root_inline_placement(
WritingModeAxes::new(WritingMode::VerticalLr, Direction::Ltr),
PageInlineSpan::from_edges(0.0, 40.0),
);
assert_eq!(
next_page,
RootInlineCanvasPlacement::NextPage {
inline_origin: PageTopBlockPosition::new(17.0),
}
);
}
}