use std::rc::Rc;
use super::*;
use crate::image_store::ImageId;
#[derive(Debug, Clone, PartialEq)]
pub(in crate::layout) struct DecodedPngImage {
pub(in crate::layout) image_id: Option<ImageId>,
pub(in crate::layout) pixel_size: RasterPixelSize,
pub(in crate::layout) source_rect: Option<RenderedImageSourceRect>,
pub(in crate::layout) natural_size: crate::units::CssPixelSize,
pub(in crate::layout) sample_depth: crate::image_store::RasterSampleDepth,
pub(in crate::layout) rgb: EncodedRasterRgbSamples,
pub(in crate::layout) alpha: Option<Rc<[u8]>>,
pub(in crate::layout) color_space: crate::color::RasterColorSpace,
}
#[derive(Debug, Clone, PartialEq)]
pub(in crate::layout) struct EncodedRasterRgbSamples(Rc<[u8]>);
impl EncodedRasterRgbSamples {
pub(in crate::layout) fn new(samples: Vec<u8>) -> Self {
Self(Rc::from(samples.into_boxed_slice()))
}
pub(in crate::layout) fn from_shared(samples: Rc<[u8]>) -> Self {
Self(samples)
}
pub(in crate::layout) fn shared(&self) -> Rc<[u8]> {
Rc::clone(&self.0)
}
}
impl AsRef<[u8]> for EncodedRasterRgbSamples {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl std::ops::Deref for EncodedRasterRgbSamples {
type Target = [u8];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl PartialEq<Vec<u8>> for EncodedRasterRgbSamples {
fn eq(&self, other: &Vec<u8>) -> bool {
self.as_ref() == other.as_slice()
}
}
impl DecodedPngImage {
pub(in crate::layout) fn new(
pixel_width: u32,
pixel_height: u32,
rgb: Vec<u8>,
alpha: Option<Vec<u8>>,
) -> Self {
Self {
image_id: None,
pixel_size: RasterPixelSize::new(pixel_width, pixel_height),
source_rect: None,
natural_size: crate::units::CssPixelSize::new(pixel_width, pixel_height),
sample_depth: crate::image_store::RasterSampleDepth::Eight,
rgb: EncodedRasterRgbSamples::new(rgb),
alpha: alpha.map(|alpha| Rc::from(alpha.into_boxed_slice())),
color_space: crate::color::RasterColorSpace::SRGB,
}
}
pub(in crate::layout) fn in_color_space(
mut self,
color_space: crate::css::CssColorSpace,
) -> Self {
self.color_space = crate::color::RasterColorSpace::BuiltIn(color_space);
self
}
pub(in crate::layout) fn natural_layout_size(&self) -> crate::units::LayoutSize {
crate::units::css_pixel_natural_layout_size(self.natural_size)
}
pub(in crate::layout) fn with_source_rect(
mut self,
source_rect: RenderedImageSourceRect,
) -> Self {
self.natural_size = crate::units::CssPixelSize::new(source_rect.width, source_rect.height);
self.source_rect = Some(source_rect);
self
}
}
use crate::LayoutSize;
use crate::document::paint::patterns::RenderedImageSourceRect;
use crate::image_store::RasterOrientationPolicy;
use crate::layout::assets::rasterize_generated_css_image;
use crate::svg::SvgImageContext;
use crate::units::{IntoLayoutLength, LayoutLength, layout_px};
pub(in crate::layout) type PaintBackgroundOffset = euclid::Vector2D<f64, PaintSpace>;
#[derive(Debug, Clone)]
pub(super) enum ResolvedImageAsset {
Raster(DecodedPngImage),
Svg(SharedSvgAsset),
}
#[derive(Debug, Clone)]
pub(super) enum ResolvedCssImage {
External(ResolvedImageAsset),
SolidColor(CssColor),
Invalid,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct ImagePixelRect {
pub(super) x: u32,
pub(super) y: u32,
pub(super) width: u32,
pub(super) height: u32,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum ParsedImageFragment {
None,
Xywh(ImagePixelRect),
Other(String),
}
pub(super) struct ImageResolutionContext<'a> {
pub(super) base_url: Option<&'a url::Url>,
pub(super) root_url: Option<&'a url::Url>,
pub(super) current_color: CssColor,
pub(super) orientation: RasterOrientationPolicy,
pub(super) svg_context: SvgImageContext,
pub(super) resource_cache: &'a ResourceCache,
}
pub(super) fn resolve_css_image_source(
image: &css::BackgroundImage,
context: ImageResolutionContext<'_>,
) -> ResolvedCssImage {
let image = image.selected_image();
let resolve_url = |url: &css::ImageUrl| {
load_resolved_image_source_with_request(
&url.href,
url.base_url.as_ref().or(context.base_url),
url.root_url.as_ref().or(context.root_url),
context.resource_cache,
context.orientation,
context.svg_context,
&url.request_modifiers,
)
};
match image {
css::BackgroundImage::Url(url) => resolve_url(url)
.map(ResolvedCssImage::External)
.unwrap_or(ResolvedCssImage::Invalid),
css::BackgroundImage::ImageFunction(function) => match function.source.as_ref() {
Some(source) => resolve_image_function_source(source, &context)
.map(ResolvedCssImage::External)
.or_else(|| {
function.fallback_color.map(|color| {
ResolvedCssImage::SolidColor(color.resolve(context.current_color))
})
})
.unwrap_or(ResolvedCssImage::Invalid),
None => function
.fallback_color
.map(|color| ResolvedCssImage::SolidColor(color.resolve(context.current_color)))
.unwrap_or(ResolvedCssImage::Invalid),
},
_ => ResolvedCssImage::Invalid,
}
}
fn resolve_image_function_source(
source: &css::ImageUrl,
context: &ImageResolutionContext<'_>,
) -> Option<ResolvedImageAsset> {
let fragment = parse_image_fragment(&source.href)?;
match fragment {
ParsedImageFragment::None => load_resolved_image_source_with_request(
&source.href,
source.base_url.as_ref().or(context.base_url),
source.root_url.as_ref().or(context.root_url),
context.resource_cache,
context.orientation,
context.svg_context,
&source.request_modifiers,
),
ParsedImageFragment::Other(_) => {
match load_resolved_image_source_with_request(
&source.href,
source.base_url.as_ref().or(context.base_url),
source.root_url.as_ref().or(context.root_url),
context.resource_cache,
context.orientation,
context.svg_context,
&source.request_modifiers,
)? {
asset @ ResolvedImageAsset::Svg(_) => Some(asset),
ResolvedImageAsset::Raster(_) => None,
}
}
ParsedImageFragment::Xywh(rect) => {
let href = source
.href
.split_once('#')
.map_or(source.href.as_str(), |(href, _)| href);
let asset = load_resolved_image_source_with_request(
href,
source.base_url.as_ref().or(context.base_url),
source.root_url.as_ref().or(context.root_url),
context.resource_cache,
context.orientation,
context.svg_context,
&source.request_modifiers,
)?;
match asset {
ResolvedImageAsset::Raster(image) => {
clamp_raster_fragment(image, rect).map(ResolvedImageAsset::Raster)
}
ResolvedImageAsset::Svg(_) => None,
}
}
}
}
fn parse_image_fragment(href: &str) -> Option<ParsedImageFragment> {
let Some((_, fragment)) = href.split_once('#') else {
return Some(ParsedImageFragment::None);
};
let Some(value) = fragment.strip_prefix("xywh=") else {
return Some(ParsedImageFragment::Other(fragment.to_string()));
};
let mut values = value.split(',').map(str::trim);
let x = values.next()?.parse::<u32>().ok()?;
let y = values.next()?.parse::<u32>().ok()?;
let width = values.next()?.parse::<u32>().ok()?;
let height = values.next()?.parse::<u32>().ok()?;
if values.next().is_some() || width == 0 || height == 0 {
return None;
}
Some(ParsedImageFragment::Xywh(ImagePixelRect {
x,
y,
width,
height,
}))
}
fn clamp_raster_fragment(
image: DecodedPngImage,
fragment: ImagePixelRect,
) -> Option<DecodedPngImage> {
let width = image.pixel_size.width;
let height = image.pixel_size.height;
if fragment.x >= width || fragment.y >= height {
return None;
}
let right = fragment.x.saturating_add(fragment.width).min(width);
let bottom = fragment.y.saturating_add(fragment.height).min(height);
let source_rect = RenderedImageSourceRect {
x: fragment.x,
y: fragment.y,
width: right.checked_sub(fragment.x)?,
height: bottom.checked_sub(fragment.y)?,
};
(source_rect.width > 0 && source_rect.height > 0).then(|| image.with_source_rect(source_rect))
}
pub(super) const fn raster_orientation_policy(
orientation: css::ImageOrientation,
) -> RasterOrientationPolicy {
match orientation {
css::ImageOrientation::FromImage => RasterOrientationPolicy::FromImage,
css::ImageOrientation::None => RasterOrientationPolicy::Encoded,
}
}
impl ResolvedImageAsset {
pub(super) fn intrinsic_size(&self) -> LayoutSize {
match self {
Self::Raster(image) => image.natural_layout_size(),
Self::Svg(asset) => asset.intrinsic_size(),
}
}
}
#[cfg(test)]
pub(super) fn load_resolved_image_source(
src: &str,
base_url: Option<&url::Url>,
root_url: Option<&url::Url>,
resource_cache: &ResourceCache,
orientation_policy: RasterOrientationPolicy,
image_context: SvgImageContext,
) -> Option<ResolvedImageAsset> {
load_resolved_image_source_with_request(
src,
base_url,
root_url,
resource_cache,
orientation_policy,
image_context,
&css::RequestUrlModifiers::default(),
)
}
pub(super) fn load_resolved_image_source_with_request(
src: &str,
base_url: Option<&url::Url>,
root_url: Option<&url::Url>,
resource_cache: &ResourceCache,
orientation_policy: RasterOrientationPolicy,
image_context: SvgImageContext,
request_modifiers: &css::RequestUrlModifiers,
) -> Option<ResolvedImageAsset> {
let (asset, svg_fragment) = if src.starts_with("data:") {
(
resource_cache.data_image_asset_with_orientation(
src,
orientation_policy,
image_context,
)?,
None,
)
} else {
let url = crate::resource::resolve_url(src, base_url, root_url)?;
let taint =
resource_cache.css_image_fetch_taint(&url, root_url.or(base_url), request_modifiers)?;
let orientation_policy = orientation_policy_for_taint(orientation_policy, taint);
let svg_fragment = url.fragment().map(str::to_owned);
(
resource_cache.image_asset_url_with_orientation(
&url,
orientation_policy,
image_context,
)?,
svg_fragment,
)
};
Some(match asset {
crate::resource::ResourceImageAsset::Raster { image_id, metadata } => {
ResolvedImageAsset::Raster(DecodedPngImage {
image_id: Some(image_id),
pixel_size: metadata.pixel_size,
source_rect: None,
natural_size: metadata.natural_size,
sample_depth: crate::image_store::RasterSampleDepth::Eight,
rgb: EncodedRasterRgbSamples::from_shared(resource_cache.image_placeholder_rgb()),
alpha: None,
color_space: crate::color::RasterColorSpace::SRGB,
})
}
crate::resource::ResourceImageAsset::Svg(asset) => {
ResolvedImageAsset::Svg(Rc::new(asset.with_view_fragment(svg_fragment.as_deref())))
}
})
}
const fn orientation_policy_for_taint(
requested: RasterOrientationPolicy,
taint: crate::resource::ImageFetchTaint,
) -> RasterOrientationPolicy {
match (requested, taint) {
(RasterOrientationPolicy::Encoded, crate::resource::ImageFetchTaint::Opaque) => {
RasterOrientationPolicy::FromImage
}
(policy, _) => policy,
}
}
#[cfg(test)]
pub(super) fn load_image_source(
src: &str,
base_url: Option<&url::Url>,
root_url: Option<&url::Url>,
resource_cache: &ResourceCache,
apply_orientation: bool,
) -> Option<DecodedPngImage> {
load_image_source_with_request(
src,
base_url,
root_url,
resource_cache,
if apply_orientation {
RasterOrientationPolicy::FromImage
} else {
RasterOrientationPolicy::Encoded
},
&css::RequestUrlModifiers::default(),
)
}
pub(super) fn load_image_source_with_request(
src: &str,
base_url: Option<&url::Url>,
root_url: Option<&url::Url>,
resource_cache: &ResourceCache,
orientation_policy: RasterOrientationPolicy,
request_modifiers: &css::RequestUrlModifiers,
) -> Option<DecodedPngImage> {
match load_resolved_image_source_with_request(
src,
base_url,
root_url,
resource_cache,
orientation_policy,
SvgImageContext::default(),
request_modifiers,
)? {
ResolvedImageAsset::Raster(image) => Some(image),
ResolvedImageAsset::Svg(_) => None,
}
}
#[derive(Debug, Clone)]
pub(super) enum UsedReplacementImageSource {
Raster,
Svg,
Gradient { image: Box<BackgroundImage> },
}
pub(super) struct UsedImage {
pub(super) decoded: DecodedPngImage,
pub(super) svg: Option<SharedSvgAsset>,
pub(super) source: UsedReplacementImageSource,
pub(super) content_size: ContentBoxSize,
pub(super) border_box_size: BorderBoxSize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::layout) enum ObjectViewBoxSourceSpace {}
pub(in crate::layout) type NormalizedObjectSourcePoint =
euclid::Point2D<f32, ObjectViewBoxSourceSpace>;
pub(in crate::layout) type NormalizedObjectSourceSize =
euclid::Size2D<f32, ObjectViewBoxSourceSpace>;
pub(in crate::layout) type NormalizedObjectSourceRect = euclid::Rect<f32, ObjectViewBoxSourceSpace>;
#[derive(Debug, Clone, PartialEq)]
pub(in crate::layout) enum ResolvedObjectViewBox {
NoEffect,
Source {
rect: NormalizedObjectSourceRect,
radii: Option<css::BorderRadius>,
},
}
impl ResolvedObjectViewBox {
pub(in crate::layout) fn source_rect(&self) -> NormalizedObjectSourceRect {
match self {
Self::NoEffect => NormalizedObjectSourceRect::new(
NormalizedObjectSourcePoint::new(0.0, 0.0),
NormalizedObjectSourceSize::new(1.0, 1.0),
),
Self::Source { rect, .. } => *rect,
}
}
pub(in crate::layout) fn effective_natural_size(&self, natural_size: LayoutSize) -> LayoutSize {
let source = self.source_rect();
LayoutSize::new(
natural_size.width * source.size.width,
natural_size.height * source.size.height,
)
}
pub(in crate::layout) fn radii(&self) -> Option<&css::BorderRadius> {
match self {
Self::NoEffect => None,
Self::Source { radii, .. } => radii.as_ref(),
}
}
pub(in crate::layout) fn applies(&self) -> bool {
matches!(self, Self::Source { .. })
}
}
#[derive(Debug, Clone, Copy)]
pub(super) struct UsedReplacedBox {
pub(super) content_size: ContentBoxSize,
pub(super) border_box_size: BorderBoxSize,
}
impl UsedReplacedBox {
fn new(
content_size: ContentBoxSize,
horizontal_non_content: NonContentLength,
vertical_non_content: NonContentLength,
) -> Self {
Self {
border_box_size: content_box_to_border_box_size(
content_size,
horizontal_non_content,
vertical_non_content,
),
content_size,
}
}
}
#[derive(Debug, Clone, Copy)]
pub(super) struct IntrinsicReplacedSize {
pub(super) width: ContentBoxLength,
pub(super) height: ContentBoxLength,
pub(super) preferred_aspect_ratio: Option<f32>,
pub(super) has_intrinsic_size: bool,
pub(super) attr_width: Option<ContentBoxLength>,
pub(super) attr_height: Option<ContentBoxLength>,
}
impl IntrinsicReplacedSize {
pub(super) fn scaled_by_effective_zoom(mut self, factor: f32) -> Self {
self.width *= factor;
self.height *= factor;
self.attr_width = self.attr_width.map(|width| width * factor);
self.attr_height = self.attr_height.map(|height| height * factor);
self
}
pub(super) fn natural_aspect_ratio(self) -> Option<f32> {
self.preferred_aspect_ratio
}
pub(super) fn attribute_aspect_ratio(self) -> Option<f32> {
self.attr_width
.zip(self.attr_height)
.filter(|(width, height)| *width > content_box_pt(0.0) && *height > content_box_pt(0.0))
.map(|(width, height)| width.points() / height.points())
}
}
impl UsedImage {
fn new(
decoded: DecodedPngImage,
content_size: ContentBoxSize,
horizontal_non_content: NonContentLength,
vertical_non_content: NonContentLength,
) -> Self {
let border_box_size = content_box_to_border_box_size(
content_size,
horizontal_non_content,
vertical_non_content,
);
Self {
decoded,
svg: None,
source: UsedReplacementImageSource::Raster,
content_size,
border_box_size,
}
}
fn from_geometry(decoded: DecodedPngImage, geometry: UsedReplacedBox) -> Self {
Self {
decoded,
svg: None,
source: UsedReplacementImageSource::Raster,
content_size: geometry.content_size,
border_box_size: geometry.border_box_size,
}
}
fn with_svg(mut self, svg: Option<SharedSvgAsset>) -> Self {
if svg.is_some() {
self.source = UsedReplacementImageSource::Svg;
}
self.svg = svg;
self
}
fn with_generated_gradient(mut self, image: BackgroundImage) -> Self {
self.source = UsedReplacementImageSource::Gradient {
image: Box::new(image),
};
self
}
pub(super) fn into_inline_atom_content(self) -> InlineAtomContent {
match self.source {
UsedReplacementImageSource::Raster => InlineAtomContent::Image(self.decoded),
UsedReplacementImageSource::Svg => InlineAtomContent::Svg { asset: self.svg },
UsedReplacementImageSource::Gradient { image } => InlineAtomContent::Gradient {
image: *image,
fallback: self.decoded,
},
}
}
}
pub(super) struct IntrinsicImageSize {
pub(super) decoded: DecodedPngImage,
pub(super) svg: Option<SharedSvgAsset>,
pub(super) width: ContentBoxLength,
pub(super) height: ContentBoxLength,
}
impl IntrinsicImageSize {
pub(super) fn replaced_size(&self) -> IntrinsicReplacedSize {
IntrinsicReplacedSize {
width: self.width,
height: self.height,
preferred_aspect_ratio: self.svg.as_ref().map_or_else(
|| {
(self.width > content_box_pt(0.0) && self.height > content_box_pt(0.0))
.then_some(self.width.points() / self.height.points())
},
|asset| asset.intrinsic_dimensions().aspect_ratio,
),
has_intrinsic_size: self.svg.as_ref().is_none_or(|asset| {
let dimensions = asset.intrinsic_dimensions();
dimensions.width.is_some() || dimensions.height.is_some()
}),
attr_width: None,
attr_height: None,
}
}
}
pub(super) fn intrinsic_default_replaced_size(element: &Element) -> IntrinsicReplacedSize {
let attr_width = element
.attrs
.get("width")
.and_then(|value| parse_html_length(value))
.filter(|value| *value > 0.0);
let attr_height = element
.attrs
.get("height")
.and_then(|value| parse_html_length(value))
.filter(|value| *value > 0.0);
IntrinsicReplacedSize {
width: content_box_pt(attr_width.unwrap_or(300.0 * css::CSS_PX_TO_PT)),
height: content_box_pt(attr_height.unwrap_or(150.0 * css::CSS_PX_TO_PT)),
preferred_aspect_ratio: Some(
attr_width.unwrap_or(300.0 * css::CSS_PX_TO_PT)
/ attr_height.unwrap_or(150.0 * css::CSS_PX_TO_PT),
),
has_intrinsic_size: true,
attr_width: attr_width.map(content_box_pt),
attr_height: attr_height.map(content_box_pt),
}
}
pub(super) fn intrinsic_unavailable_image_size() -> IntrinsicReplacedSize {
IntrinsicReplacedSize {
width: content_box_pt(300.0 * css::CSS_PX_TO_PT),
height: content_box_pt(150.0 * css::CSS_PX_TO_PT),
preferred_aspect_ratio: None,
has_intrinsic_size: false,
attr_width: None,
attr_height: None,
}
}
pub(super) fn intrinsic_iframe_size(element: &Element) -> IntrinsicReplacedSize {
let _ = element;
intrinsic_unavailable_image_size()
}
pub(super) fn intrinsic_canvas_size(element: &Element) -> IntrinsicReplacedSize {
intrinsic_default_replaced_size(element)
}
pub(super) fn intrinsic_svg_size(element: &Element) -> Option<IntrinsicReplacedSize> {
let (size, dimensions) = crate::svg::svg_replaced_size(element)?;
let width = size.width;
let height = size.height;
Some(IntrinsicReplacedSize {
width: content_box_pt(width),
height: content_box_pt(height),
preferred_aspect_ratio: (width > 0.0 && height > 0.0).then_some(width / height),
has_intrinsic_size: dimensions.width.is_some() || dimensions.height.is_some(),
attr_width: element.attrs.get("width").and_then(|value| {
parse_html_length(value)
.filter(|width| *width > 0.0 && !value.contains('%'))
.map(content_box_pt)
}),
attr_height: element.attrs.get("height").and_then(|value| {
parse_html_length(value)
.filter(|height| *height > 0.0 && !value.contains('%'))
.map(content_box_pt)
}),
})
}
pub(super) fn intrinsic_image_size(
element: &Element,
style: &ComputedStyle,
base_url: Option<&url::Url>,
root_url: Option<&url::Url>,
resource_cache: &ResourceCache,
) -> Option<IntrinsicImageSize> {
let (src, intrinsic_resolution) = match element.tag.as_str() {
"object" => element
.attrs
.get("data")
.map(|src| (src.as_str(), crate::dom::ImageDensity::one()))?,
"video" => element
.attrs
.get("poster")
.map(|src| (src.as_str(), crate::dom::ImageDensity::one()))?,
"img" => crate::dom::selected_img_source(element)?,
_ => element
.attrs
.get("src")
.map(|src| (src.as_str(), crate::dom::ImageDensity::one()))?,
};
let request_modifiers = html_image_request_modifiers(element);
let asset = load_resolved_image_source_with_request(
src,
base_url,
root_url,
resource_cache,
raster_orientation_policy(style.image_orientation),
SvgImageContext::from_used_color_scheme(style.used_color_scheme),
&request_modifiers,
)?;
let intrinsic_size = match &asset {
ResolvedImageAsset::Raster(image) => image.natural_layout_size(),
ResolvedImageAsset::Svg(asset) => asset.replaced_intrinsic_size(),
};
let view_box = resolved_object_view_box_for_asset(style.object_view_box.clone(), &asset);
let intrinsic_size = view_box.effective_natural_size(intrinsic_size);
let (width, height) = match intrinsic_resolution {
crate::dom::ImageDensity::Finite(density) => (
intrinsic_size.width / density.value(),
intrinsic_size.height / density.value(),
),
crate::dom::ImageDensity::Infinite => (0.0, 0.0),
};
if width <= 0.0 || height <= 0.0 {
return None;
}
let (decoded, svg) = match asset {
ResolvedImageAsset::Raster(decoded) => (decoded, None),
ResolvedImageAsset::Svg(svg) => (
DecodedPngImage::new(1, 1, vec![0, 0, 0], Some(vec![0])),
Some(svg),
),
};
Some(IntrinsicImageSize {
decoded,
svg,
width: content_box_pt(width),
height: content_box_pt(height),
})
}
pub(crate) fn static_html_img_is_available(
element: &Element,
base_url: Option<&url::Url>,
root_url: Option<&url::Url>,
resource_cache: &ResourceCache,
) -> bool {
debug_assert_eq!(element.tag, "img");
let Some((src, _density)) = crate::dom::selected_img_source(element) else {
return false;
};
load_resolved_image_source_with_request(
src,
base_url,
root_url,
resource_cache,
RasterOrientationPolicy::Encoded,
SvgImageContext::default(),
&html_image_request_modifiers(element),
)
.is_some()
}
pub(super) fn html_image_request_modifiers(element: &Element) -> css::RequestUrlModifiers {
let cross_origin = match element.attrs.get("crossorigin") {
None => None,
Some(value) if value.eq_ignore_ascii_case("use-credentials") => {
Some(css::CrossOriginRequestMode::UseCredentials)
}
Some(_) => Some(css::CrossOriginRequestMode::Anonymous),
};
css::RequestUrlModifiers {
cross_origin,
integrity: None,
referrer_policy: None,
}
}
pub(super) fn used_image(
element: &Element,
style: &ComputedStyle,
available_width: f32,
height_basis: BlockSizePercentageBasis,
base_url: Option<&url::Url>,
root_url: Option<&url::Url>,
resource_cache: &ResourceCache,
) -> Option<UsedImage> {
let intrinsic = intrinsic_image_size(element, style, base_url, root_url, resource_cache);
let (decoded, svg, intrinsic) = match intrinsic {
Some(intrinsic) => {
let replaced_size = intrinsic.replaced_size();
(intrinsic.decoded, intrinsic.svg, replaced_size)
}
None if element.image_rendering == crate::dom::ImageRendering::Empty => (
transparent_replaced_fallback_pixel(),
None,
intrinsic_empty_image_size(),
),
None if unavailable_image_establishes_replaced_box(element, style) => (
transparent_replaced_fallback_pixel(),
None,
intrinsic_unavailable_image_size(),
),
None => return None,
};
let geometry = used_replaced_box(
intrinsic,
style,
content_box_pt(available_width),
height_basis,
);
Some(UsedImage::from_geometry(decoded, geometry).with_svg(svg))
}
fn intrinsic_empty_image_size() -> IntrinsicReplacedSize {
IntrinsicReplacedSize {
width: content_box_pt(0.0),
height: content_box_pt(0.0),
preferred_aspect_ratio: None,
has_intrinsic_size: false,
attr_width: None,
attr_height: None,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum FlexIntrinsicBlockBasisSource {
ExistingFlexItem,
DefiniteFlexBase,
PostFlexingMainSize,
DefinitePreferredSize,
BalancedLineSlot,
DefiniteSingleLineStretch,
AspectRatioTransfer,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum WinningBlockConstraintKind {
Minimum,
Maximum,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) enum IntrinsicBlockBasis {
Indefinite,
DefiniteFromContainingBlock(ContentBoxLength),
DefiniteFromFlexLayout {
value: ContentBoxLength,
source: FlexIntrinsicBlockBasisSource,
},
DefiniteWinningConstraint {
value: ContentBoxLength,
kind: WinningBlockConstraintKind,
},
}
impl IntrinsicBlockBasis {
pub(super) fn from_layout_percentage_basis(basis: BlockSizePercentageBasis) -> Self {
match basis {
PercentageBasis::Indefinite => Self::Indefinite,
PercentageBasis::Definite {
value,
source: BlockSizeBasisSource::FlexItem,
} => Self::from_flex_layout(value, FlexIntrinsicBlockBasisSource::ExistingFlexItem),
PercentageBasis::Definite { value, .. } => Self::DefiniteFromContainingBlock(value),
}
}
pub(super) fn from_flex_layout(
value: ContentBoxLength,
source: FlexIntrinsicBlockBasisSource,
) -> Self {
Self::DefiniteFromFlexLayout { value, source }
}
pub(super) fn from_winning_constraint(
value: ContentBoxLength,
kind: WinningBlockConstraintKind,
) -> Self {
Self::DefiniteWinningConstraint { value, kind }
}
pub(super) fn descendant_percentage_basis(self) -> BlockSizePercentageBasis {
let (value, source) = match self {
Self::Indefinite => return PercentageBasis::indefinite(),
Self::DefiniteFromContainingBlock(value) => {
(value, BlockSizeBasisSource::ContainingBlock)
}
Self::DefiniteFromFlexLayout { value, .. }
| Self::DefiniteWinningConstraint { value, .. } => {
(value, BlockSizeBasisSource::FlexItem)
}
};
PercentageBasis::definite_from(value, source)
}
}
#[derive(Debug, Clone, Copy)]
pub(super) struct ReplacedBoxSizingContext {
pub(super) available_width: ContentBoxLength,
pub(super) inline_percentage_basis: IntrinsicInlinePercentageBasis,
pub(super) block_basis: IntrinsicBlockBasis,
}
pub(super) enum ResolvedReplacedElement {
Canvas(UsedReplacedBox),
Image(UsedImage),
Svg(UsedReplacedBox),
}
impl ResolvedReplacedElement {
pub(super) fn geometry(&self) -> UsedReplacedBox {
match self {
Self::Canvas(geometry) | Self::Svg(geometry) => *geometry,
Self::Image(image) => UsedReplacedBox {
content_size: image.content_size,
border_box_size: image.border_box_size,
},
}
}
pub(super) fn into_image(self) -> Option<UsedImage> {
match self {
Self::Image(image) => Some(image),
Self::Canvas(_) | Self::Svg(_) => None,
}
}
}
pub(super) fn resolve_replaced_element(
element: &Element,
style: &ComputedStyle,
sizing: ReplacedBoxSizingContext,
base_url: Option<&url::Url>,
root_url: Option<&url::Url>,
resource_cache: &ResourceCache,
) -> Option<ResolvedReplacedElement> {
match replaced_element_kind(element)? {
ReplacedElementKind::Canvas => Some(ResolvedReplacedElement::Canvas(
used_canvas_with_intrinsic_sizing_context(element, style, sizing),
)),
ReplacedElementKind::Image => used_image_with_intrinsic_sizing_context(
element,
style,
sizing,
base_url,
root_url,
resource_cache,
)
.map(ResolvedReplacedElement::Image),
ReplacedElementKind::Svg => used_svg_with_intrinsic_sizing_context(element, style, sizing)
.map(ResolvedReplacedElement::Svg),
}
}
pub(super) fn used_image_with_intrinsic_sizing_context(
element: &Element,
style: &ComputedStyle,
sizing: ReplacedBoxSizingContext,
base_url: Option<&url::Url>,
root_url: Option<&url::Url>,
resource_cache: &ResourceCache,
) -> Option<UsedImage> {
let intrinsic = intrinsic_image_size(element, style, base_url, root_url, resource_cache);
let (decoded, svg, intrinsic) = match intrinsic {
Some(intrinsic) => {
let replaced_size = intrinsic.replaced_size();
(intrinsic.decoded, intrinsic.svg, replaced_size)
}
None if element.tag == "img" && crate::dom::selected_img_source(element).is_none() => (
transparent_replaced_fallback_pixel(),
None,
intrinsic_empty_image_size(),
),
None if unavailable_image_establishes_replaced_box(element, style) => (
transparent_replaced_fallback_pixel(),
None,
unavailable_image_intrinsic_size_for_inline_basis(
element,
style,
sizing.inline_percentage_basis,
),
),
None => return None,
};
let geometry = used_replaced_box_with_inline_percentage_basis(
intrinsic,
style,
sizing.available_width,
sizing.inline_percentage_basis,
sizing.block_basis.descendant_percentage_basis(),
);
Some(UsedImage::from_geometry(decoded, geometry).with_svg(svg))
}
fn unavailable_image_intrinsic_size_for_inline_basis(
element: &Element,
style: &ComputedStyle,
inline_percentage_basis: IntrinsicInlinePercentageBasis,
) -> IntrinsicReplacedSize {
let cyclic_inline_percentage = !inline_percentage_basis.is_definite()
&& element.tag == "img"
&& element
.attrs
.get("src")
.is_none_or(|source| source.trim().is_empty())
&& matches!(
style.box_values.width,
css::ComputedLengthPercentageOrAuto::LengthPercentage(ref value)
if value.needs_percentage_basis()
);
if !cyclic_inline_percentage {
return intrinsic_unavailable_image_size();
}
IntrinsicReplacedSize {
width: content_box_pt(0.0),
height: content_box_pt(0.0),
preferred_aspect_ratio: None,
has_intrinsic_size: false,
attr_width: None,
attr_height: None,
}
}
fn transparent_replaced_fallback_pixel() -> DecodedPngImage {
DecodedPngImage::new(1, 1, vec![0, 0, 0], Some(vec![0]))
}
fn unavailable_image_establishes_replaced_box(element: &Element, style: &ComputedStyle) -> bool {
if element.tag == "video" {
return true;
}
element.tag == "img"
&& (element
.attrs
.get("src")
.is_some_and(|source| !source.trim().is_empty())
|| element.attrs.contains_key("width")
|| element.attrs.contains_key("height")
|| !style.box_values.width.is_auto()
|| !style.box_values.height.is_auto())
}
pub(super) fn used_replaced_box(
intrinsic: IntrinsicReplacedSize,
style: &ComputedStyle,
available_width: ContentBoxLength,
height_basis: BlockSizePercentageBasis,
) -> UsedReplacedBox {
used_replaced_box_with_inline_percentage_basis(
intrinsic,
style,
available_width,
PercentageBasis::definite_from(
ContentBoxLength::new(available_width.points().max(0.0)),
IntrinsicInlinePercentageBasisSource::MeasurementAvailableWidth,
),
height_basis,
)
}
pub(super) fn used_replaced_box_with_inline_percentage_basis(
intrinsic: IntrinsicReplacedSize,
style: &ComputedStyle,
available_width: ContentBoxLength,
inline_percentage_basis: IntrinsicInlinePercentageBasis,
height_basis: BlockSizePercentageBasis,
) -> UsedReplacedBox {
let intrinsic = intrinsic.scaled_by_effective_zoom(style.effective_zoom.factor());
let attribute_aspect_ratio = intrinsic.attribute_aspect_ratio();
let contained_intrinsic_width = style
.contain
.size
.then(|| {
style.contain_intrinsic_size.width.clone().map(|width| {
used_length_percentage(
width,
PercentageBasis::definite(available_width.into_layout_length()),
)
.points()
})
})
.flatten()
.unwrap_or(0.0);
let contained_intrinsic_height = style
.contain
.size
.then(|| {
style.contain_intrinsic_size.height.clone().map(|height| {
used_length_percentage(
height,
PercentageBasis::definite(available_width.into_layout_length()),
)
.points()
})
})
.flatten()
.unwrap_or(0.0);
let natural_aspect_ratio = if style.contain.size {
attribute_aspect_ratio
} else {
intrinsic.natural_aspect_ratio()
};
let aspect_ratio = style
.aspect_ratio
.preferred_ratio(true, natural_aspect_ratio);
let borders = used_border_widths(style);
let horizontal_non_content =
borders.left + borders.right + style.padding.left + style.padding.right;
let vertical_non_content =
borders.top + borders.bottom + style.padding.top + style.padding.bottom;
let available_content_width = (available_width.points() - horizontal_non_content).max(0.0);
let css_width = used_content_box_width_or_auto_with_basis(
style,
inline_percentage_basis,
non_content_pt(horizontal_non_content),
)
.map(SemanticLengthExt::points);
let css_height = used_content_box_height_or_auto_with_basis(
style,
height_basis,
non_content_pt(vertical_non_content),
)
.map(SemanticLengthExt::points);
let (width, height) = if css_width.is_some() || css_height.is_some() {
(css_width, css_height)
} else {
(
intrinsic.attr_width.map(SemanticLengthExt::points),
intrinsic.attr_height.map(SemanticLengthExt::points),
)
};
let width_is_auto = css_width.is_none();
let height_is_auto = css_height.is_none();
let ratio_only_automatic_size =
width_is_auto && height_is_auto && !intrinsic.has_intrinsic_size && aspect_ratio.is_some();
let (mut content_width, mut content_height) = match (width, height, aspect_ratio) {
(Some(width_value), None, Some(ratio)) => (width_value, width_value / ratio),
(None, Some(height_value), Some(ratio)) => (height_value * ratio, height_value),
(Some(width_value), None, None) if style.contain.size => {
(width_value, contained_intrinsic_height)
}
(None, Some(height_value), None) if style.contain.size => {
(contained_intrinsic_width, height_value)
}
(Some(width_value), None, None) => (width_value, intrinsic.height.points()),
(None, Some(height_value), None) => (intrinsic.width.points(), height_value),
(None, None, _) if style.contain.size => {
(contained_intrinsic_width, contained_intrinsic_height)
}
(None, None, _) => (intrinsic.width.points(), intrinsic.height.points()),
(Some(width_value), Some(height_value), _) => (width_value, height_value),
};
if ratio_only_automatic_size {
content_width = content_width.min(available_content_width);
content_height = content_width / aspect_ratio.expect("ratio-only size has a ratio");
}
if let Some(aspect_ratio) = aspect_ratio {
let constrained_size = resolve_replaced_size_with_aspect_ratio(
content_box_size_pt(content_width, content_height),
aspect_ratio,
ReplacedPreferredSizeAxes {
width: ReplacedPreferredSize::from_is_automatic(width_is_auto),
height: ReplacedPreferredSize::from_is_automatic(height_is_auto),
},
ReplacedSizeConstraints {
min_width: used_min_width(
style,
PercentageBasis::definite(available_width.into_layout_length()),
)
.map(|width| width.max(content_box_pt(0.0))),
max_width: used_max_width(
style,
PercentageBasis::definite(available_width.into_layout_length()),
)
.map(|width| width.max(content_box_pt(0.0))),
min_height: used_length_percentage_or_auto_with_basis(
style.box_values.min_height.clone(),
height_basis,
)
.map(|height| content_box_pt(height.points().max(0.0))),
max_height: used_length_percentage_or_auto_with_basis(
style.box_values.max_height.clone(),
height_basis,
)
.map(|height| content_box_pt(height.points().max(0.0))),
},
);
content_width = constrained_size.width;
content_height = constrained_size.height;
} else {
content_width = constrain_content_width(
style,
content_box_pt(content_width),
PercentageBasis::definite(available_width.into_layout_length()),
)
.points();
content_height = constrain_content_height(
style,
content_box_pt(content_height),
PercentageBasis::definite(available_width.into_layout_length()),
)
.points();
}
UsedReplacedBox::new(
content_box_size_pt(content_width, content_height),
non_content_pt(horizontal_non_content),
non_content_pt(vertical_non_content),
)
}
fn used_generated_image_asset(
asset: ResolvedImageAsset,
style: &ComputedStyle,
available_width: f32,
intrinsic_resolution: f32,
) -> Option<UsedImage> {
let intrinsic_resolution = match &asset {
ResolvedImageAsset::Raster(_) => intrinsic_resolution,
ResolvedImageAsset::Svg(_) => 1.0,
}
.max(f32::MIN_POSITIVE);
let raw_intrinsic_size = match &asset {
ResolvedImageAsset::Raster(image) => image.natural_layout_size(),
ResolvedImageAsset::Svg(asset) => asset.replaced_intrinsic_size(),
};
let view_box = resolved_object_view_box_for_asset(style.object_view_box.clone(), &asset);
let raw_intrinsic_size = view_box.effective_natural_size(raw_intrinsic_size);
let intrinsic_width = raw_intrinsic_size.width / intrinsic_resolution;
let intrinsic_height = raw_intrinsic_size.height / intrinsic_resolution;
if intrinsic_width <= 0.0 || intrinsic_height <= 0.0 {
return None;
}
let geometry = used_replaced_box(
IntrinsicReplacedSize {
width: content_box_pt(intrinsic_width),
height: content_box_pt(intrinsic_height),
preferred_aspect_ratio: Some(intrinsic_width / intrinsic_height),
has_intrinsic_size: true,
attr_width: None,
attr_height: None,
},
style,
content_box_pt(available_width),
PercentageBasis::indefinite(),
);
let (decoded, svg) = match asset {
ResolvedImageAsset::Raster(decoded) => (decoded, None),
ResolvedImageAsset::Svg(svg) => (
DecodedPngImage::new(1, 1, vec![0, 0, 0], Some(vec![0])),
Some(svg),
),
};
Some(UsedImage::from_geometry(decoded, geometry).with_svg(svg))
}
pub(in crate::layout) fn resolved_object_view_box_for_asset(
view_box: css::ObjectViewBox,
asset: &ResolvedImageAsset,
) -> ResolvedObjectViewBox {
let natural_size = match asset {
ResolvedImageAsset::Raster(image) => Some(image.natural_layout_size()),
ResolvedImageAsset::Svg(asset) => asset
.intrinsic_dimensions()
.width
.zip(asset.intrinsic_dimensions().height)
.map(|(width, height)| LayoutSize::new(width.points(), height.points())),
};
resolved_object_view_box(view_box, natural_size)
}
pub(in crate::layout) fn resolved_object_view_box_for_svg(
view_box: css::ObjectViewBox,
asset: &SharedSvgAsset,
) -> ResolvedObjectViewBox {
let dimensions = asset.intrinsic_dimensions();
let natural_size = dimensions
.width
.zip(dimensions.height)
.map(|(width, height)| LayoutSize::new(width.points(), height.points()));
resolved_object_view_box(view_box, natural_size)
}
pub(in crate::layout) fn resolved_object_view_box(
view_box: css::ObjectViewBox,
natural_size: Option<LayoutSize>,
) -> ResolvedObjectViewBox {
let Some(natural_size) = natural_size.filter(|size| {
size.width.is_finite() && size.height.is_finite() && size.width > 0.0 && size.height > 0.0
}) else {
return ResolvedObjectViewBox::NoEffect;
};
let resolve_x = |value| {
used_length_percentage(
value,
PercentageBasis::definite(layout_pt(natural_size.width)),
)
.points()
};
let resolve_y = |value| {
used_length_percentage(
value,
PercentageBasis::definite(layout_pt(natural_size.height)),
)
.points()
};
let (rect, radii) = match view_box {
css::ObjectViewBox::None => return ResolvedObjectViewBox::NoEffect,
css::ObjectViewBox::Inset {
top,
right,
bottom,
left,
radii,
} => {
let left = resolve_x(left) / natural_size.width;
let right = resolve_x(right) / natural_size.width;
let top = resolve_y(top) / natural_size.height;
let bottom = resolve_y(bottom) / natural_size.height;
(
NormalizedObjectSourceRect::new(
NormalizedObjectSourcePoint::new(left, top),
NormalizedObjectSourceSize::new(1.0 - left - right, 1.0 - top - bottom),
),
radii,
)
}
css::ObjectViewBox::Xywh {
x,
y,
width,
height,
radii,
} => (
NormalizedObjectSourceRect::new(
NormalizedObjectSourcePoint::new(
resolve_x(x) / natural_size.width,
resolve_y(y) / natural_size.height,
),
NormalizedObjectSourceSize::new(
resolve_x(width) / natural_size.width,
resolve_y(height) / natural_size.height,
),
),
radii,
),
css::ObjectViewBox::Rect {
top,
right,
bottom,
left,
} => {
let left = resolve_x(left) / natural_size.width;
let right = resolve_x(right) / natural_size.width;
let top = resolve_y(top) / natural_size.height;
let bottom = resolve_y(bottom) / natural_size.height;
(
NormalizedObjectSourceRect::new(
NormalizedObjectSourcePoint::new(left, top),
NormalizedObjectSourceSize::new(right - left, bottom - top),
),
None,
)
}
};
if rect.origin.x.is_finite()
&& rect.origin.y.is_finite()
&& rect.size.width.is_finite()
&& rect.size.height.is_finite()
&& rect.size.width > 0.0
&& rect.size.height > 0.0
{
ResolvedObjectViewBox::Source { rect, radii }
} else {
ResolvedObjectViewBox::NoEffect
}
}
pub(super) fn used_generated_image_value(
image: &BackgroundImage,
style: &ComputedStyle,
available_width: f32,
fallback_base_url: Option<&url::Url>,
fallback_root_url: Option<&url::Url>,
resource_cache: &ResourceCache,
) -> Option<UsedImage> {
let intrinsic_resolution = image.intrinsic_resolution();
if matches!(
image.selected_image(),
BackgroundImage::Url(_) | BackgroundImage::ImageFunction(_)
) {
return match resolve_css_image_source(
image,
ImageResolutionContext {
base_url: fallback_base_url,
root_url: fallback_root_url,
current_color: style.color,
orientation: raster_orientation_policy(style.image_orientation),
svg_context: SvgImageContext::from_used_color_scheme(style.used_color_scheme),
resource_cache,
},
) {
ResolvedCssImage::External(asset) => {
used_generated_image_asset(asset, style, available_width, intrinsic_resolution)
}
ResolvedCssImage::SolidColor(_) => used_dimensionless_generated_image(
image,
style,
available_width,
fallback_base_url,
fallback_root_url,
resource_cache,
),
ResolvedCssImage::Invalid => None,
};
}
used_dimensionless_generated_image(
image,
style,
available_width,
fallback_base_url,
fallback_root_url,
resource_cache,
)
}
fn used_dimensionless_generated_image(
image: &BackgroundImage,
style: &ComputedStyle,
available_width: f32,
fallback_base_url: Option<&url::Url>,
fallback_root_url: Option<&url::Url>,
resource_cache: &ResourceCache,
) -> Option<UsedImage> {
let geometry = used_replaced_box(
IntrinsicReplacedSize {
width: content_box_pt(300.0 * css::CSS_PX_TO_PT),
height: content_box_pt(150.0 * css::CSS_PX_TO_PT),
preferred_aspect_ratio: None,
has_intrinsic_size: false,
attr_width: None,
attr_height: None,
},
style,
content_box_pt(available_width),
PercentageBasis::indefinite(),
);
let content_width = geometry.content_size.width;
let content_height = geometry.content_size.height;
let decoded = rasterize_generated_css_image(
image,
PaintSize::new(content_width, content_height),
style.color,
fallback_base_url,
fallback_root_url,
resource_cache,
)?;
Some(UsedImage::from_geometry(decoded, geometry).with_generated_gradient(image.clone()))
}
pub(super) fn used_invalid_replacement_image(
style: &ComputedStyle,
available_width: f32,
) -> UsedImage {
let borders = used_border_widths(style);
let horizontal_non_content =
borders.left + borders.right + style.padding.left + style.padding.right;
let vertical_non_content =
borders.top + borders.bottom + style.padding.top + style.padding.bottom;
let available_content_width = (available_width - horizontal_non_content).max(0.0);
let mut content_width = used_content_box_width_or_auto(
style,
layout_pt(available_width),
non_content_pt(horizontal_non_content),
)
.map(SemanticLengthExt::points)
.unwrap_or(0.0);
let content_height =
definite_image_content_height_without_percent(style, vertical_non_content).unwrap_or(0.0);
content_width = content_width.min(available_content_width);
UsedImage::new(
DecodedPngImage::new(1, 1, vec![0, 0, 0], Some(vec![0])),
content_box_size_pt(content_width, content_height),
non_content_pt(horizontal_non_content),
non_content_pt(vertical_non_content),
)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum ReplacedPreferredSize {
Automatic,
Definite,
}
impl ReplacedPreferredSize {
pub(super) const fn from_is_automatic(is_automatic: bool) -> Self {
if is_automatic {
Self::Automatic
} else {
Self::Definite
}
}
const fn is_automatic(self) -> bool {
matches!(self, Self::Automatic)
}
}
#[derive(Debug, Clone, Copy)]
pub(super) struct ReplacedPreferredSizeAxes {
pub(super) width: ReplacedPreferredSize,
pub(super) height: ReplacedPreferredSize,
}
#[derive(Debug, Clone, Copy)]
pub(super) struct ReplacedSizeConstraints {
pub(super) min_width: Option<ContentBoxLength>,
pub(super) max_width: Option<ContentBoxLength>,
pub(super) min_height: Option<ContentBoxLength>,
pub(super) max_height: Option<ContentBoxLength>,
}
pub(super) fn resolve_replaced_size_with_aspect_ratio(
size: ContentBoxSize,
aspect_ratio: f32,
preferred_sizes: ReplacedPreferredSizeAxes,
constraints: ReplacedSizeConstraints,
) -> ContentBoxSize {
if !aspect_ratio.is_finite() || aspect_ratio <= 0.0 {
return size;
}
let width_bounds = ReplacedAxisBounds::new(constraints.min_width, constraints.max_width);
let height_bounds = ReplacedAxisBounds::new(constraints.min_height, constraints.max_height);
if preferred_sizes.width.is_automatic() && preferred_sizes.height.is_automatic() {
return resolve_automatic_replaced_size_constraint_table(
size,
aspect_ratio,
width_bounds,
height_bounds,
);
}
let width_bounds = if preferred_sizes.width.is_automatic() {
width_bounds.with_transferred_from_height(height_bounds, aspect_ratio)
} else {
width_bounds
};
let height_bounds = if preferred_sizes.height.is_automatic() {
height_bounds.with_transferred_from_width(width_bounds, aspect_ratio)
} else {
height_bounds
};
content_box_size_pt(
width_bounds.clamp(size.width),
height_bounds.clamp(size.height),
)
}
#[derive(Debug, Clone, Copy)]
struct ReplacedAxisBounds {
min: ContentBoxLength,
max: ContentBoxLength,
}
impl ReplacedAxisBounds {
fn new(min: Option<ContentBoxLength>, max: Option<ContentBoxLength>) -> Self {
let min = min.unwrap_or_else(|| content_box_pt(0.0));
let max = max
.unwrap_or_else(|| content_box_pt(f32::INFINITY))
.max(min);
Self { min, max }
}
fn clamp(self, value: f32) -> f32 {
value.max(self.min.points()).min(self.max.points())
}
fn with_transferred_from_width(self, width: Self, aspect_ratio: f32) -> Self {
self.with_transferred_bounds(
content_box_pt(width.min.points() / aspect_ratio),
content_box_pt(width.max.points() / aspect_ratio),
)
}
fn with_transferred_from_height(self, height: Self, aspect_ratio: f32) -> Self {
self.with_transferred_bounds(
content_box_pt(height.min.points() * aspect_ratio),
content_box_pt(height.max.points() * aspect_ratio),
)
}
fn with_transferred_bounds(
self,
transferred_min: ContentBoxLength,
transferred_max: ContentBoxLength,
) -> Self {
let min = self.min.max(transferred_min.min(self.max));
let max = self.max.min(transferred_max.max(min));
Self { min, max }
}
}
fn resolve_automatic_replaced_size_constraint_table(
size: ContentBoxSize,
aspect_ratio: f32,
width_bounds: ReplacedAxisBounds,
height_bounds: ReplacedAxisBounds,
) -> ContentBoxSize {
let width = size.width.max(0.0);
let height = size.height.max(0.0);
let width_scale = |new_width: f32| {
if width > 0.0 {
new_width * height / width
} else {
new_width / aspect_ratio
}
};
let height_scale = |new_height: f32| {
if height > 0.0 {
new_height * width / height
} else {
new_height * aspect_ratio
}
};
let width_constraint_scale = |constraint: f32| {
if width > 0.0 {
constraint / width
} else if constraint > 0.0 {
f32::INFINITY
} else {
0.0
}
};
let height_constraint_scale = |constraint: f32| {
if height > 0.0 {
constraint / height
} else if constraint > 0.0 {
f32::INFINITY
} else {
0.0
}
};
let width_violation = ReplacedConstraintViolation::classify(width, width_bounds);
let height_violation = ReplacedConstraintViolation::classify(height, height_bounds);
let (width, height) = match (width_violation, height_violation) {
(ReplacedConstraintViolation::None, ReplacedConstraintViolation::None) => (width, height),
(ReplacedConstraintViolation::Maximum, ReplacedConstraintViolation::None) => {
let width = width_bounds.max.points();
(width, width_scale(width).max(height_bounds.min.points()))
}
(ReplacedConstraintViolation::Minimum, ReplacedConstraintViolation::None) => {
let width = width_bounds.min.points();
(width, width_scale(width).min(height_bounds.max.points()))
}
(ReplacedConstraintViolation::None, ReplacedConstraintViolation::Maximum) => {
let height = height_bounds.max.points();
(height_scale(height).max(width_bounds.min.points()), height)
}
(ReplacedConstraintViolation::None, ReplacedConstraintViolation::Minimum) => {
let height = height_bounds.min.points();
(height_scale(height).min(width_bounds.max.points()), height)
}
(ReplacedConstraintViolation::Maximum, ReplacedConstraintViolation::Maximum) => {
if width_constraint_scale(width_bounds.max.points())
<= height_constraint_scale(height_bounds.max.points())
{
let width = width_bounds.max.points();
(width, width_scale(width).max(height_bounds.min.points()))
} else {
let height = height_bounds.max.points();
(height_scale(height).max(width_bounds.min.points()), height)
}
}
(ReplacedConstraintViolation::Minimum, ReplacedConstraintViolation::Minimum) => {
if width_constraint_scale(width_bounds.min.points())
<= height_constraint_scale(height_bounds.min.points())
{
let height = height_bounds.min.points();
(height_scale(height).min(width_bounds.max.points()), height)
} else {
let width = width_bounds.min.points();
(width, width_scale(width).min(height_bounds.max.points()))
}
}
(ReplacedConstraintViolation::Minimum, ReplacedConstraintViolation::Maximum) => {
(width_bounds.min.points(), height_bounds.max.points())
}
(ReplacedConstraintViolation::Maximum, ReplacedConstraintViolation::Minimum) => {
(width_bounds.max.points(), height_bounds.min.points())
}
};
content_box_size_pt(width, height)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ReplacedConstraintViolation {
None,
Minimum,
Maximum,
}
impl ReplacedConstraintViolation {
fn classify(value: f32, bounds: ReplacedAxisBounds) -> Self {
if value < bounds.min.points() {
Self::Minimum
} else if value > bounds.max.points() {
Self::Maximum
} else {
Self::None
}
}
}
pub(super) fn definite_image_content_height_without_percent(
style: &ComputedStyle,
vertical_non_content: f32,
) -> Option<f32> {
let height = style.box_values.height.length_if_no_percent()?;
Some(match style.box_sizing {
BoxSizing::BorderBox => (height - vertical_non_content).max(0.0),
BoxSizing::ContentBox => height.max(0.0),
})
}
pub(super) fn used_canvas_size_with_height_basis(
element: &Element,
style: &ComputedStyle,
available_width: f32,
height_basis: BlockSizePercentageBasis,
) -> (f32, f32) {
let size = used_canvas(element, style, available_width, height_basis);
(size.content_size.width, size.content_size.height)
}
pub(super) fn used_canvas(
element: &Element,
style: &ComputedStyle,
available_width: f32,
height_basis: BlockSizePercentageBasis,
) -> UsedReplacedBox {
used_replaced_box(
intrinsic_canvas_size(element),
style,
content_box_pt(available_width),
height_basis,
)
}
pub(super) fn used_canvas_with_intrinsic_sizing_context(
element: &Element,
style: &ComputedStyle,
sizing: ReplacedBoxSizingContext,
) -> UsedReplacedBox {
used_replaced_box_with_inline_percentage_basis(
intrinsic_canvas_size(element),
style,
sizing.available_width,
sizing.inline_percentage_basis,
sizing.block_basis.descendant_percentage_basis(),
)
}
pub(super) fn used_svg_with_intrinsic_sizing_context(
element: &Element,
style: &ComputedStyle,
sizing: ReplacedBoxSizingContext,
) -> Option<UsedReplacedBox> {
intrinsic_svg_size(element).map(|intrinsic| {
used_replaced_box_with_inline_percentage_basis(
intrinsic,
style,
sizing.available_width,
sizing.inline_percentage_basis,
sizing.block_basis.descendant_percentage_basis(),
)
})
}
pub(super) fn used_svg(
element: &Element,
style: &ComputedStyle,
available_width: f32,
height_basis: BlockSizePercentageBasis,
) -> Option<UsedReplacedBox> {
intrinsic_svg_size(element).map(|intrinsic| {
used_replaced_box(
intrinsic,
style,
content_box_pt(available_width),
height_basis,
)
})
}
pub(super) fn used_background_size(
image: &DecodedPngImage,
positioning_area: PaintSize,
value: css::BackgroundSize,
intrinsic_resolution: f32,
) -> PaintSize {
let natural_size = image.natural_layout_size();
let intrinsic_resolution = intrinsic_resolution.max(f32::MIN_POSITIVE);
used_background_size_from_intrinsic_dimensions(
positioning_area,
value,
CssImageNaturalDimensions::from_raster_layout_size(natural_size, intrinsic_resolution),
)
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout) struct CssImageNaturalDimensions {
axes: ImageNaturalAxes,
pub(super) aspect_ratio: Option<f32>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
enum ImageNaturalAxes {
None,
Width(euclid::Length<f32, PaintSpace>),
Height(euclid::Length<f32, PaintSpace>),
Size(PaintSize),
}
impl CssImageNaturalDimensions {
#[cfg(test)]
pub(super) fn without_size(aspect_ratio: Option<f32>) -> Self {
Self {
axes: ImageNaturalAxes::None,
aspect_ratio,
}
}
pub(in crate::layout) fn from_layout_axes(
width: Option<LayoutLength>,
height: Option<LayoutLength>,
aspect_ratio: Option<f32>,
) -> Self {
let width = width.and_then(Self::paint_axis);
let height = height.and_then(Self::paint_axis);
let axes = match (width, height) {
(Some(width), Some(height)) => {
ImageNaturalAxes::Size(PaintSize::new(width.get(), height.get()))
}
(Some(width), None) => ImageNaturalAxes::Width(width),
(None, Some(height)) => ImageNaturalAxes::Height(height),
(None, None) => ImageNaturalAxes::None,
};
Self { axes, aspect_ratio }
}
pub(in crate::layout) fn from_layout_size(size: LayoutSize) -> Self {
let width = layout_pt(size.width);
let height = layout_pt(size.height);
Self::from_layout_axes(
Some(width),
Some(height),
(width > layout_pt(0.0) && height > layout_pt(0.0))
.then_some(width.points() / height.points()),
)
}
fn from_raster_layout_size(size: LayoutSize, intrinsic_resolution: f32) -> Self {
let width = layout_pt(size.width / intrinsic_resolution);
let height = layout_pt(size.height / intrinsic_resolution);
Self::from_layout_axes(
Some(width),
Some(height),
(width.get() > 0.0 && height.get() > 0.0).then_some(width.get() / height.get()),
)
}
fn paint_axis(value: LayoutLength) -> Option<euclid::Length<f32, PaintSpace>> {
let value = value.max(layout_pt(0.0));
value.get().is_finite().then_some(value.cast_unit())
}
fn width(self) -> Option<euclid::Length<f32, PaintSpace>> {
match self.axes {
ImageNaturalAxes::Width(width) => Some(width),
ImageNaturalAxes::Size(size) => Some(euclid::Length::new(size.width)),
ImageNaturalAxes::None | ImageNaturalAxes::Height(_) => None,
}
}
fn height(self) -> Option<euclid::Length<f32, PaintSpace>> {
match self.axes {
ImageNaturalAxes::Height(height) => Some(height),
ImageNaturalAxes::Size(size) => Some(euclid::Length::new(size.height)),
ImageNaturalAxes::None | ImageNaturalAxes::Width(_) => None,
}
}
pub(in crate::layout) fn scaled(self, width_scale: f32, height_scale: f32) -> Self {
debug_assert!(width_scale.is_finite() && width_scale > 0.0);
debug_assert!(height_scale.is_finite() && height_scale > 0.0);
let scale_axis =
|axis: euclid::Length<f32, PaintSpace>, scale| euclid::Length::new(axis.get() * scale);
let axes = match self.axes {
ImageNaturalAxes::None => ImageNaturalAxes::None,
ImageNaturalAxes::Width(width) => {
ImageNaturalAxes::Width(scale_axis(width, width_scale))
}
ImageNaturalAxes::Height(height) => {
ImageNaturalAxes::Height(scale_axis(height, height_scale))
}
ImageNaturalAxes::Size(size) => ImageNaturalAxes::Size(PaintSize::new(
size.width * width_scale,
size.height * height_scale,
)),
};
let aspect_ratio = self
.aspect_ratio
.filter(|ratio| ratio.is_finite() && *ratio > 0.0)
.and_then(|ratio| {
let scaled = ratio * width_scale / height_scale;
(scaled.is_finite() && scaled > 0.0).then_some(scaled)
});
Self { axes, aspect_ratio }
}
pub(in crate::layout) fn default_size(self, default_object_size: PaintSize) -> PaintSize {
let default_object_size = PaintSize::new(
default_object_size.width.max(0.0),
default_object_size.height.max(0.0),
);
let ratio = self
.aspect_ratio
.filter(|ratio| ratio.is_finite() && *ratio > 0.0);
match (self.width(), self.height(), ratio) {
(Some(width), Some(height), _) => PaintSize::new(width.get(), height.get()),
(Some(width), None, Some(ratio)) => PaintSize::new(width.get(), width.get() / ratio),
(None, Some(height), Some(ratio)) => PaintSize::new(height.get() * ratio, height.get()),
(Some(width), None, None) => PaintSize::new(width.get(), default_object_size.height),
(None, Some(height), None) => PaintSize::new(default_object_size.width, height.get()),
(None, None, Some(ratio)) => background_size_contain(default_object_size, ratio),
(None, None, None) => default_object_size,
}
}
pub(in crate::layout) fn contain_size(self, constraint: PaintSize) -> PaintSize {
self.aspect_ratio
.filter(|ratio| ratio.is_finite() && *ratio > 0.0)
.map(|ratio| background_size_contain(constraint, ratio))
.unwrap_or(constraint)
}
pub(in crate::layout) fn cover_size(self, constraint: PaintSize) -> PaintSize {
self.aspect_ratio
.filter(|ratio| ratio.is_finite() && *ratio > 0.0)
.map(|ratio| background_size_cover(constraint, ratio))
.unwrap_or(constraint)
}
}
pub(super) fn used_background_size_from_intrinsic_dimensions(
positioning_area: PaintSize,
value: css::BackgroundSize,
intrinsic: CssImageNaturalDimensions,
) -> PaintSize {
let positioning_area = PaintSize::new(
positioning_area.width.max(0.0),
positioning_area.height.max(0.0),
);
let ratio = intrinsic
.aspect_ratio
.filter(|ratio| ratio.is_finite() && *ratio > 0.0);
let auto_size = || intrinsic.default_size(positioning_area);
match value {
css::BackgroundSize::Auto => auto_size(),
css::BackgroundSize::Cover => intrinsic.cover_size(positioning_area),
css::BackgroundSize::Contain => intrinsic.contain_size(positioning_area),
css::BackgroundSize::Explicit { width, height } => match (
used_background_size_axis(width, positioning_area.width),
used_background_size_axis(height, positioning_area.height),
) {
(Some(width), Some(height)) => PaintSize::new(width, height),
(Some(width), None) => ratio
.map(|ratio| PaintSize::new(width, width / ratio))
.unwrap_or_else(|| {
PaintSize::new(
width,
intrinsic
.height()
.map_or(positioning_area.height, |height| height.get()),
)
}),
(None, Some(height)) => ratio
.map(|ratio| PaintSize::new(height * ratio, height))
.unwrap_or_else(|| {
PaintSize::new(
intrinsic
.width()
.map_or(positioning_area.width, |width| width.get()),
height,
)
}),
(None, None) => auto_size(),
},
}
}
fn background_size_cover(positioning_area: PaintSize, ratio: f32) -> PaintSize {
if positioning_area.width <= 0.0 && positioning_area.height <= 0.0 {
return PaintSize::new(0.0, 0.0);
}
let scale = (positioning_area.width / ratio).max(positioning_area.height);
PaintSize::new(scale * ratio, scale)
}
fn background_size_contain(positioning_area: PaintSize, ratio: f32) -> PaintSize {
if positioning_area.width <= 0.0 || positioning_area.height <= 0.0 {
return PaintSize::new(0.0, 0.0);
}
let scale = (positioning_area.width / ratio).min(positioning_area.height);
PaintSize::new(scale * ratio, scale)
}
pub(super) fn used_background_size_axis(
value: css::BackgroundSizeAxis,
reference: f32,
) -> Option<f32> {
match value {
css::BackgroundSizeAxis::Auto => None,
css::BackgroundSizeAxis::LengthPercentage(value) => Some(
used_length_percentage(
value,
PercentageBasis::definite(layout_pt(reference.max(0.0))),
)
.points(),
),
}
}
pub(super) fn background_position(
value: css::BackgroundPosition,
positioning_area: PaintSize,
image_size: PaintSize,
) -> PaintBackgroundOffset {
let free_x = f64::from(positioning_area.width) - f64::from(image_size.width);
let free_y = f64::from(positioning_area.height) - f64::from(image_size.height);
PaintBackgroundOffset::new(
used_background_position_axis_precise(value.x, free_x, false),
used_background_position_axis_precise(value.y, free_y, true),
)
}
fn used_background_position_axis_precise(
axis: css::BackgroundPositionAxis,
free_space: f64,
invert_start_end: bool,
) -> f64 {
let offset = axis
.offset
.percentage_coefficient()
.map(|percentage| {
f64::from(axis.offset.fixed_component().points()) + f64::from(percentage) * free_space
})
.unwrap_or_else(|| {
f64::from(
used_length_percentage(
axis.offset,
PercentageBasis::definite(layout_pt(free_space as f32)),
)
.points(),
)
});
match (axis.origin, invert_start_end) {
(css::BackgroundPositionOrigin::Start, false) => offset,
(css::BackgroundPositionOrigin::Start, true) => free_space - offset,
(css::BackgroundPositionOrigin::Center, _) => free_space / 2.0 + offset,
(css::BackgroundPositionOrigin::End, false) => free_space - offset,
(css::BackgroundPositionOrigin::End, true) => offset,
}
}
#[derive(Debug, Clone, Copy)]
pub(super) struct UsedBorderImageSlices {
pub top: f32,
pub right: f32,
pub bottom: f32,
pub left: f32,
}
pub(super) fn used_border_image_slices(
values: css::BorderImageSliceOffsets,
image_width: f32,
image_height: f32,
) -> UsedBorderImageSlices {
UsedBorderImageSlices {
top: used_border_image_slice_value(values.top, image_height),
right: used_border_image_slice_value(values.right, image_width),
bottom: used_border_image_slice_value(values.bottom, image_height),
left: used_border_image_slice_value(values.left, image_width),
}
}
fn used_border_image_slice_value(value: css::BorderImageSliceValue, reference: f32) -> f32 {
let resolved = match value {
css::BorderImageSliceValue::Number(value) => value,
css::BorderImageSliceValue::Percent(value) => value * reference,
};
resolved.clamp(0.0, reference.max(0.0))
}
pub(super) fn used_border_image_widths(
style: &ComputedStyle,
computed_border_widths: css::Edges,
border_image_area_width: BorderBoxLength,
border_image_area_height: BorderBoxLength,
slices: UsedBorderImageSlices,
) -> css::Edges {
css::Edges {
top: used_border_image_width_value(
style.border_image.width.top.clone(),
layout_pt(computed_border_widths.top),
border_image_area_height,
slices.top,
)
.points(),
right: used_border_image_width_value(
style.border_image.width.right.clone(),
layout_pt(computed_border_widths.right),
border_image_area_width,
slices.right,
)
.points(),
bottom: used_border_image_width_value(
style.border_image.width.bottom.clone(),
layout_pt(computed_border_widths.bottom),
border_image_area_height,
slices.bottom,
)
.points(),
left: used_border_image_width_value(
style.border_image.width.left.clone(),
layout_pt(computed_border_widths.left),
border_image_area_width,
slices.left,
)
.points(),
}
}
fn used_border_image_width_value(
value: css::BorderImageWidthValue,
border_width: LayoutLength,
reference: BorderBoxLength,
slice_width: f32,
) -> LayoutLength {
match value {
css::BorderImageWidthValue::Auto => {
if slice_width > 0.0 {
layout_px(slice_width)
} else {
border_width
}
}
css::BorderImageWidthValue::Number(value) => border_width * value,
css::BorderImageWidthValue::LengthPercentage(value) => used_length_percentage(
value,
PercentageBasis::definite(reference.into_layout_length()),
),
}
.max(layout_pt(0.0))
}
pub(super) fn fit_border_image_widths_to_area(
widths: css::Edges,
area_width: f32,
area_height: f32,
) -> css::Edges {
let horizontal_sum = widths.left + widths.right;
let vertical_sum = widths.top + widths.bottom;
let mut factor = 1.0_f32;
if horizontal_sum > area_width && horizontal_sum > 0.0 {
factor = factor.min(area_width / horizontal_sum);
}
if vertical_sum > area_height && vertical_sum > 0.0 {
factor = factor.min(area_height / vertical_sum);
}
if factor >= 1.0 {
widths
} else {
css::Edges {
top: widths.top * factor,
right: widths.right * factor,
bottom: widths.bottom * factor,
left: widths.left * factor,
}
}
}
pub(super) fn used_border_image_outsets(
style: &ComputedStyle,
border_widths: css::Edges,
) -> css::Edges {
css::Edges {
top: used_border_image_outset_value(
style.border_image.outset.top.clone(),
border_widths.top,
),
right: used_border_image_outset_value(
style.border_image.outset.right.clone(),
border_widths.right,
),
bottom: used_border_image_outset_value(
style.border_image.outset.bottom.clone(),
border_widths.bottom,
),
left: used_border_image_outset_value(
style.border_image.outset.left.clone(),
border_widths.left,
),
}
}
fn used_border_image_outset_value(value: css::BorderImageOutsetValue, border_width: f32) -> f32 {
match value {
css::BorderImageOutsetValue::Number(value) => value * border_width,
css::BorderImageOutsetValue::Length(value) => value.length_points(),
}
.max(0.0)
}
pub(super) fn used_background_position_axis(
axis: css::BackgroundPositionAxis,
free_space: f32,
invert_start_end: bool,
) -> f32 {
let offset = used_length_percentage(
axis.offset,
PercentageBasis::definite(layout_pt(free_space)),
)
.points();
match (axis.origin, invert_start_end) {
(css::BackgroundPositionOrigin::Start, false) => offset,
(css::BackgroundPositionOrigin::Start, true) => free_space - offset,
(css::BackgroundPositionOrigin::Center, _) => free_space / 2.0 + offset,
(css::BackgroundPositionOrigin::End, false) => free_space - offset,
(css::BackgroundPositionOrigin::End, true) => offset,
}
}
pub(super) fn svg_rect(element: &Element) -> Option<(f32, f32, CssColor)> {
crate::svg::svg_intrinsic_size(element)
.map(|size| (size.width, size.height, CssColor::TRANSPARENT))
}
pub(super) fn parse_html_length(value: &str) -> Option<f32> {
let value = value.trim();
if let Some(number) = value.strip_suffix("px") {
return number
.trim()
.parse::<f32>()
.ok()
.map(|value| value * css::CSS_PX_TO_PT);
}
if let Some(number) = value.strip_suffix("pt") {
return number.trim().parse().ok();
}
value
.parse::<f32>()
.ok()
.map(|value| value * css::CSS_PX_TO_PT)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn opaque_images_cannot_select_the_encoded_orientation_policy() {
assert_eq!(
orientation_policy_for_taint(
RasterOrientationPolicy::Encoded,
crate::resource::ImageFetchTaint::Opaque,
),
RasterOrientationPolicy::FromImage
);
assert_eq!(
orientation_policy_for_taint(
RasterOrientationPolicy::Encoded,
crate::resource::ImageFetchTaint::OriginClean,
),
RasterOrientationPolicy::Encoded
);
}
use std::rc::Rc;
use crate::units::{border_box_size_pt, border_box_to_content_box_size};
fn transparent_decoded_image() -> DecodedPngImage {
DecodedPngImage::new(1, 1, vec![0, 0, 0], Some(vec![0]))
}
fn canvas_element(attrs: &[(&str, &str)]) -> Element {
element_with_attrs("canvas", attrs)
}
fn element_with_attrs(tag: &str, attrs: &[(&str, &str)]) -> Element {
let NodeKind::Element(mut element) = Node::element(tag).kind else {
unreachable!("element constructor must produce an element")
};
element.attrs.extend(
attrs
.iter()
.map(|(name, value)| (name.to_string(), value.to_string())),
);
element
}
fn automatic_replaced_axes() -> ReplacedPreferredSizeAxes {
ReplacedPreferredSizeAxes {
width: ReplacedPreferredSize::Automatic,
height: ReplacedPreferredSize::Automatic,
}
}
#[test]
fn automatic_replaced_size_uses_the_css22_min_max_constraint_table() {
struct Case {
name: &'static str,
size: ContentBoxSize,
constraints: ReplacedSizeConstraints,
expected: ContentBoxSize,
}
let cases = [
Case {
name: "no violation",
size: content_box_size_pt(100.0, 50.0),
constraints: ReplacedSizeConstraints {
min_width: None,
max_width: None,
min_height: None,
max_height: None,
},
expected: content_box_size_pt(100.0, 50.0),
},
Case {
name: "maximum width",
size: content_box_size_pt(200.0, 100.0),
constraints: ReplacedSizeConstraints {
min_width: None,
max_width: Some(content_box_pt(150.0)),
min_height: None,
max_height: None,
},
expected: content_box_size_pt(150.0, 75.0),
},
Case {
name: "minimum width",
size: content_box_size_pt(100.0, 50.0),
constraints: ReplacedSizeConstraints {
min_width: Some(content_box_pt(150.0)),
max_width: None,
min_height: None,
max_height: None,
},
expected: content_box_size_pt(150.0, 75.0),
},
Case {
name: "maximum height",
size: content_box_size_pt(200.0, 100.0),
constraints: ReplacedSizeConstraints {
min_width: None,
max_width: None,
min_height: None,
max_height: Some(content_box_pt(75.0)),
},
expected: content_box_size_pt(150.0, 75.0),
},
Case {
name: "minimum height",
size: content_box_size_pt(100.0, 50.0),
constraints: ReplacedSizeConstraints {
min_width: None,
max_width: None,
min_height: Some(content_box_pt(75.0)),
max_height: None,
},
expected: content_box_size_pt(150.0, 75.0),
},
Case {
name: "both maxima",
size: content_box_size_pt(200.0, 100.0),
constraints: ReplacedSizeConstraints {
min_width: None,
max_width: Some(content_box_pt(150.0)),
min_height: None,
max_height: Some(content_box_pt(60.0)),
},
expected: content_box_size_pt(120.0, 60.0),
},
Case {
name: "both minima",
size: content_box_size_pt(100.0, 50.0),
constraints: ReplacedSizeConstraints {
min_width: Some(content_box_pt(150.0)),
max_width: None,
min_height: Some(content_box_pt(100.0)),
max_height: None,
},
expected: content_box_size_pt(200.0, 100.0),
},
Case {
name: "minimum width and maximum height",
size: content_box_size_pt(100.0, 50.0),
constraints: ReplacedSizeConstraints {
min_width: Some(content_box_pt(150.0)),
max_width: None,
min_height: None,
max_height: Some(content_box_pt(25.0)),
},
expected: content_box_size_pt(150.0, 25.0),
},
Case {
name: "maximum width and minimum height",
size: content_box_size_pt(200.0, 100.0),
constraints: ReplacedSizeConstraints {
min_width: None,
max_width: Some(content_box_pt(150.0)),
min_height: Some(content_box_pt(125.0)),
max_height: None,
},
expected: content_box_size_pt(150.0, 125.0),
},
Case {
name: "maximum is normalized by minimum",
size: content_box_size_pt(200.0, 100.0),
constraints: ReplacedSizeConstraints {
min_width: Some(content_box_pt(150.0)),
max_width: Some(content_box_pt(100.0)),
min_height: None,
max_height: None,
},
expected: content_box_size_pt(150.0, 75.0),
},
];
for case in cases {
assert_eq!(
resolve_replaced_size_with_aspect_ratio(
case.size,
2.0,
automatic_replaced_axes(),
case.constraints,
),
case.expected,
"{}",
case.name,
);
}
}
#[test]
fn ratio_only_replaced_size_resolves_before_min_max_constraints() {
let intrinsic = IntrinsicReplacedSize {
width: content_box_pt(225.0),
height: content_box_pt(112.5),
preferred_aspect_ratio: Some(2.0),
has_intrinsic_size: false,
attr_width: None,
attr_height: None,
};
let mut style = ComputedStyle::initial();
style.box_values.min_height = css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_points(75.0),
);
style.box_values.max_width = css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_points(75.0),
);
let geometry = used_replaced_box(
intrinsic,
&style,
content_box_pt(37.5),
PercentageBasis::indefinite(),
);
assert_eq!(geometry.content_size, content_box_size_pt(75.0, 75.0));
}
#[test]
fn square_ratio_only_replaced_size_transfers_min_height_after_stretch_fit() {
let intrinsic = IntrinsicReplacedSize {
width: content_box_pt(225.0),
height: content_box_pt(112.5),
preferred_aspect_ratio: Some(1.0),
has_intrinsic_size: false,
attr_width: None,
attr_height: None,
};
let mut style = ComputedStyle::initial();
style.box_values.min_height = css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_points(75.0),
);
let geometry = used_replaced_box(
intrinsic,
&style,
content_box_pt(37.5),
PercentageBasis::indefinite(),
);
assert_eq!(geometry.content_size, content_box_size_pt(75.0, 75.0));
}
#[test]
fn transferred_constraint_does_not_modify_a_definite_preferred_axis() {
let size = resolve_replaced_size_with_aspect_ratio(
content_box_size_pt(75.0, 37.5),
2.0,
ReplacedPreferredSizeAxes {
width: ReplacedPreferredSize::Definite,
height: ReplacedPreferredSize::Automatic,
},
ReplacedSizeConstraints {
min_width: None,
max_width: None,
min_height: Some(content_box_pt(75.0)),
max_height: None,
},
);
assert_eq!(size, content_box_size_pt(75.0, 75.0));
}
#[test]
fn percent_encoded_svg_data_url_resolves_as_a_vector_asset() {
let cache = ResourceCache::default();
let source = "data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2220%22%20height%3D%2210%22%3E%3Cpath%20d%3D%22M0%200H20V10H0Z%22%20fill%3D%22red%22%2F%3E%3C%2Fsvg%3E";
let asset = load_resolved_image_source(
source,
None,
None,
&cache,
RasterOrientationPolicy::FromImage,
SvgImageContext::default(),
)
.unwrap();
let ResolvedImageAsset::Svg(asset) = asset else {
panic!("expected SVG asset");
};
assert_eq!(asset.intrinsic_size(), LayoutSize::new(15.0, 7.5));
assert_eq!(
asset
.paint_paths(crate::document::paint::geometry::PaintRect::new(
crate::document::paint::geometry::PaintPoint::new(0.0, 0.0),
crate::document::paint::geometry::PaintSize::new(15.0, 7.5),
))
.len(),
1
);
}
#[test]
fn base64_svg_data_url_resolves_as_a_vector_asset() {
let cache = ResourceCache::default();
let source = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMCIgaGVpZ2h0PSIxMCI+PHBhdGggZD0iTTAgMEgyMFYxMEgwWiIgZmlsbD0icmVkIi8+PC9zdmc+";
let asset = load_resolved_image_source(
source,
None,
None,
&cache,
RasterOrientationPolicy::FromImage,
SvgImageContext::default(),
)
.unwrap();
assert!(matches!(asset, ResolvedImageAsset::Svg(_)));
}
#[tokio::test]
async fn preloaded_svg_file_resolves_as_a_vector_asset() {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/external-vector.svg");
let url = url::Url::from_file_path(path).unwrap();
let fetcher = crate::resource::ResourceFetcher::new(crate::ResourcePolicy::default())
.expect("default resource policy must create an HTTP client");
let cache = ResourceCache::preload(&fetcher, [url.clone()])
.await
.expect("local image fixture must preload");
assert!(matches!(
cache.image_asset_url_with_orientation(
&url,
RasterOrientationPolicy::FromImage,
SvgImageContext::default(),
),
Some(crate::resource::ResourceImageAsset::Svg(_))
));
}
#[test]
fn repeated_data_url_images_share_layout_placeholder_storage() {
let cache = ResourceCache::default();
let source = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCC";
let first = load_image_source(source, None, None, &cache, true).unwrap();
let second = load_image_source(source, None, None, &cache, true).unwrap();
assert!(Rc::ptr_eq(&first.rgb.shared(), &second.rgb.shared()));
assert!(match (&first.alpha, &second.alpha) {
(Some(first), Some(second)) => Rc::ptr_eq(first, second),
(None, None) => true,
_ => false,
});
}
#[test]
fn used_image_new_expands_content_size_to_border_box_size() {
let image = UsedImage::new(
transparent_decoded_image(),
content_box_size_pt(150.0, 100.0),
non_content_pt(20.0),
non_content_pt(30.0),
);
assert_eq!(image.content_size.width, 150.0);
assert_eq!(image.content_size.height, 100.0);
assert_eq!(image.border_box_size.width, 170.0);
assert_eq!(image.border_box_size.height, 130.0);
}
#[test]
fn used_replaced_box_expands_content_size_to_border_box_size() {
let size = UsedReplacedBox::new(
content_box_size_pt(150.0, 100.0),
non_content_pt(20.0),
non_content_pt(30.0),
);
assert_eq!(size.content_size.width, 150.0);
assert_eq!(size.content_size.height, 100.0);
assert_eq!(size.border_box_size.width, 170.0);
assert_eq!(size.border_box_size.height, 130.0);
}
#[test]
fn intrinsic_canvas_size_uses_html_defaults_and_attributes() {
let default_canvas = intrinsic_canvas_size(&canvas_element(&[]));
assert_eq!(default_canvas.width, content_box_pt(225.0));
assert_eq!(default_canvas.height, content_box_pt(112.5));
assert_eq!(default_canvas.attr_width, None);
assert_eq!(default_canvas.attr_height, None);
let attributed_canvas =
intrinsic_canvas_size(&canvas_element(&[("width", "96"), ("height", "48px")]));
assert_eq!(attributed_canvas.width, content_box_pt(72.0));
assert_eq!(attributed_canvas.height, content_box_pt(36.0));
assert_eq!(attributed_canvas.attribute_aspect_ratio(), Some(2.0));
}
#[test]
fn shared_replaced_geometry_applies_box_sizing_once_for_canvas_image_and_svg() {
let mut style = ComputedStyle::initial();
style.box_sizing = BoxSizing::BorderBox;
style.box_values.width = css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_points(120.0),
);
style.padding.left = 10.0;
style.padding.right = 10.0;
let canvas = used_canvas(
&canvas_element(&[("width", "200"), ("height", "100")]),
&style,
500.0,
BlockSizePercentageBasis::indefinite(),
);
let image = used_replaced_box(
IntrinsicReplacedSize {
width: content_box_pt(150.0),
height: content_box_pt(75.0),
preferred_aspect_ratio: Some(2.0),
has_intrinsic_size: true,
attr_width: Some(content_box_pt(150.0)),
attr_height: Some(content_box_pt(75.0)),
},
&style,
content_box_pt(500.0),
BlockSizePercentageBasis::indefinite(),
);
let svg = used_svg(
&element_with_attrs("svg", &[("width", "200"), ("height", "100")]),
&style,
500.0,
BlockSizePercentageBasis::indefinite(),
)
.unwrap();
for geometry in [canvas, image, svg] {
assert_eq!(geometry.content_size.width, 100.0);
assert_eq!(geometry.content_size.height, 50.0);
assert_eq!(geometry.border_box_size.width, 120.0);
assert_eq!(geometry.border_box_size.height, 50.0);
}
}
#[test]
fn shared_replaced_geometry_transfers_auto_axis_through_min_width() {
let mut style = ComputedStyle::initial();
style.box_values.min_width = css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_points(180.0),
);
let geometry = used_replaced_box(
IntrinsicReplacedSize {
width: content_box_pt(150.0),
height: content_box_pt(75.0),
preferred_aspect_ratio: Some(2.0),
has_intrinsic_size: true,
attr_width: None,
attr_height: None,
},
&style,
content_box_pt(500.0),
BlockSizePercentageBasis::indefinite(),
);
assert_eq!(geometry.content_size.width, 180.0);
assert_eq!(geometry.content_size.height, 90.0);
}
#[test]
fn no_ratio_replaced_element_keeps_the_independent_intrinsic_auto_axis() {
let intrinsic = IntrinsicReplacedSize {
width: content_box_pt(225.0),
height: content_box_pt(112.5),
preferred_aspect_ratio: None,
has_intrinsic_size: false,
attr_width: None,
attr_height: None,
};
let mut explicit_width = ComputedStyle::initial();
explicit_width.box_values.width = css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_points(30.0),
);
let width_geometry = used_replaced_box(
intrinsic,
&explicit_width,
content_box_pt(500.0),
BlockSizePercentageBasis::indefinite(),
);
let mut explicit_height = ComputedStyle::initial();
explicit_height.box_values.height.replace_with_used(
css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_points(15.0),
),
);
let height_geometry = used_replaced_box(
intrinsic,
&explicit_height,
content_box_pt(500.0),
BlockSizePercentageBasis::indefinite(),
);
assert_eq!(
width_geometry.content_size,
content_box_size_pt(30.0, 112.5)
);
assert_eq!(
height_geometry.content_size,
content_box_size_pt(225.0, 15.0)
);
}
#[test]
fn effective_zoom_scales_replaced_dimensions_but_not_aspect_ratio() {
let intrinsic = IntrinsicReplacedSize {
width: content_box_pt(100.0),
height: content_box_pt(50.0),
preferred_aspect_ratio: Some(2.0),
has_intrinsic_size: true,
attr_width: Some(content_box_pt(100.0)),
attr_height: Some(content_box_pt(50.0)),
}
.scaled_by_effective_zoom(2.0);
assert_eq!(intrinsic.width, content_box_pt(200.0));
assert_eq!(intrinsic.height, content_box_pt(100.0));
assert_eq!(intrinsic.attr_width, Some(content_box_pt(200.0)));
assert_eq!(intrinsic.attr_height, Some(content_box_pt(100.0)));
assert_eq!(intrinsic.natural_aspect_ratio(), Some(2.0));
}
#[test]
fn replaced_percentage_max_height_uses_the_block_size_basis() {
let mut style = ComputedStyle::initial();
style.box_values.max_height = css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_percent(1.0),
);
let intrinsic = IntrinsicReplacedSize {
width: content_box_pt(200.0),
height: content_box_pt(200.0),
preferred_aspect_ratio: Some(1.0),
has_intrinsic_size: true,
attr_width: None,
attr_height: None,
};
let definite = used_replaced_box(
intrinsic,
&style,
content_box_pt(500.0),
PercentageBasis::definite_from(
content_box_pt(100.0),
BlockSizeBasisSource::ContainingBlock,
),
);
let indefinite = used_replaced_box(
intrinsic,
&style,
content_box_pt(500.0),
PercentageBasis::indefinite(),
);
assert_eq!(definite.content_size.width, 100.0);
assert_eq!(definite.content_size.height, 100.0);
assert_eq!(indefinite.content_size.width, 200.0);
assert_eq!(indefinite.content_size.height, 200.0);
}
#[test]
fn only_closed_intrinsic_block_bases_create_replaced_percentage_bases() {
assert!(
!IntrinsicBlockBasis::Indefinite
.descendant_percentage_basis()
.is_definite()
);
for basis in [
IntrinsicBlockBasis::DefiniteFromContainingBlock(content_box_pt(100.0)),
IntrinsicBlockBasis::from_flex_layout(
content_box_pt(100.0),
FlexIntrinsicBlockBasisSource::ExistingFlexItem,
),
IntrinsicBlockBasis::from_winning_constraint(
content_box_pt(100.0),
WinningBlockConstraintKind::Minimum,
),
] {
assert_eq!(
basis.descendant_percentage_basis().value(),
Some(content_box_pt(100.0))
);
}
}
#[test]
fn shared_intrinsic_replaced_context_transfers_definite_height_for_canvas_geometry() {
let mut style = ComputedStyle::initial();
style.box_values.height.replace_with_used(
css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_percent(1.0),
),
);
let context = ReplacedBoxSizingContext {
available_width: content_box_pt(500.0),
inline_percentage_basis: PercentageBasis::indefinite(),
block_basis: IntrinsicBlockBasis::from_flex_layout(
content_box_pt(100.0),
FlexIntrinsicBlockBasisSource::ExistingFlexItem,
),
};
let geometry = used_canvas_with_intrinsic_sizing_context(
&canvas_element(&[("width", "10"), ("height", "10")]),
&style,
context,
);
assert_eq!(geometry.content_size, content_box_size_pt(100.0, 100.0));
}
#[test]
fn image_intrinsic_context_matches_normal_flow_replaced_sizing() {
let mut style = ComputedStyle::initial();
style.box_values.height.replace_with_used(
css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_percent(1.0),
),
);
let context = ReplacedBoxSizingContext {
available_width: content_box_pt(500.0),
inline_percentage_basis: PercentageBasis::indefinite(),
block_basis: IntrinsicBlockBasis::from_flex_layout(
content_box_pt(100.0),
FlexIntrinsicBlockBasisSource::ExistingFlexItem,
),
};
let image = element_with_attrs(
"img",
&[(
"src",
"data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2210%22%20height%3D%2210%22%2F%3E",
)],
);
let resource_cache = ResourceCache::default();
let intrinsic = used_image_with_intrinsic_sizing_context(
&image,
&style,
context,
None,
None,
&resource_cache,
)
.expect("data SVG image must establish a replaced box");
let normal_flow = used_image(
&image,
&style,
context.available_width.points(),
context.block_basis.descendant_percentage_basis(),
None,
None,
&resource_cache,
)
.expect("data SVG image must establish a replaced box");
assert_eq!(intrinsic.content_size, content_box_size_pt(100.0, 100.0));
assert_eq!(intrinsic.content_size, normal_flow.content_size);
assert_eq!(intrinsic.border_box_size, normal_flow.border_box_size);
}
#[test]
fn used_image_shared_border_to_content_conversion_clamps_at_zero() {
let content = border_box_to_content_box_size(
border_box_size_pt(100.0, 100.0),
non_content_pt(150.0),
non_content_pt(150.0),
);
assert_eq!(content.width, 0.0);
assert_eq!(content.height, 0.0);
}
#[test]
fn used_image_invalid_replacement_keeps_transparent_pixel_and_zero_content_size() {
let style = ComputedStyle::initial();
let image = used_invalid_replacement_image(&style, 100.0);
assert_eq!(image.decoded.pixel_size.width, 1);
assert_eq!(image.decoded.pixel_size.height, 1);
assert_eq!(image.decoded.rgb.as_ref(), &[0, 0, 0]);
assert_eq!(image.decoded.alpha.as_deref(), Some([0].as_slice()));
assert_eq!(image.content_size.width, 0.0);
assert_eq!(image.content_size.height, 0.0);
assert_eq!(image.border_box_size.width, 0.0);
assert_eq!(image.border_box_size.height, 0.0);
}
#[test]
fn background_size_uses_contain_for_ratio_only_images() {
let size = used_background_size_from_intrinsic_dimensions(
PaintSize::new(300.0, 150.0),
css::BackgroundSize::Auto,
CssImageNaturalDimensions::without_size(Some(1.0 / 4.0)),
);
assert_eq!(size, PaintSize::new(37.5, 150.0));
}
#[test]
fn background_size_fills_area_for_images_without_intrinsic_geometry() {
let intrinsic = CssImageNaturalDimensions::without_size(None);
assert_eq!(
used_background_size_from_intrinsic_dimensions(
PaintSize::new(300.0, 150.0),
css::BackgroundSize::Auto,
intrinsic,
),
PaintSize::new(300.0, 150.0)
);
assert_eq!(
used_background_size_from_intrinsic_dimensions(
PaintSize::new(300.0, 150.0),
css::BackgroundSize::Contain,
intrinsic,
),
PaintSize::new(300.0, 150.0)
);
}
#[test]
fn background_size_uses_positioning_area_for_auto_axis_without_ratio() {
let size = used_background_size_from_intrinsic_dimensions(
PaintSize::new(300.0, 150.0),
css::BackgroundSize::Explicit {
width: css::BackgroundSizeAxis::LengthPercentage(
css::ComputedLengthPercentage::from_points(50.0),
),
height: css::BackgroundSizeAxis::Auto,
},
CssImageNaturalDimensions::without_size(None),
);
assert_eq!(size, PaintSize::new(50.0, 150.0));
}
#[test]
fn background_size_cover_uses_the_nonzero_positioning_axis() {
assert_eq!(
background_size_cover(PaintSize::new(100.0, 0.0), 2.0),
PaintSize::new(100.0, 50.0)
);
assert_eq!(
background_size_cover(PaintSize::new(0.0, 100.0), 2.0),
PaintSize::new(200.0, 100.0)
);
assert_eq!(
background_size_cover(PaintSize::new(0.0, 0.0), 2.0),
PaintSize::new(0.0, 0.0)
);
}
#[test]
fn background_position_returns_a_typed_high_precision_offset() {
let offset: PaintBackgroundOffset = background_position(
css::BackgroundPosition::INITIAL,
PaintSize::new(100.0, 80.0),
PaintSize::new(30.0, 20.0),
);
assert_eq!(offset, PaintBackgroundOffset::new(0.0, 60.0));
}
#[test]
fn object_view_box_resolution_preserves_valid_source_geometry() {
let view_box = css::ObjectViewBox::Xywh {
x: css::ComputedLengthPercentage::from_points(25.0),
y: css::ComputedLengthPercentage::from_points(50.0),
width: css::ComputedLengthPercentage::from_points(25.0),
height: css::ComputedLengthPercentage::from_points(50.0),
radii: None,
};
let resolved = resolved_object_view_box(view_box, Some(LayoutSize::new(50.0, 100.0)));
assert!(resolved.applies());
assert_eq!(
resolved.source_rect(),
NormalizedObjectSourceRect::new(
NormalizedObjectSourcePoint::new(0.5, 0.5),
NormalizedObjectSourceSize::new(0.5, 0.5),
)
);
assert_eq!(
resolved.effective_natural_size(LayoutSize::new(50.0, 100.0)),
LayoutSize::new(25.0, 50.0)
);
}
#[test]
fn empty_or_inapplicable_object_view_box_has_no_effect() {
let empty = css::ObjectViewBox::Inset {
top: css::ComputedLengthPercentage::from_points(50.0),
right: css::ComputedLengthPercentage::ZERO,
bottom: css::ComputedLengthPercentage::from_points(50.0),
left: css::ComputedLengthPercentage::ZERO,
radii: None,
};
let natural = LayoutSize::new(100.0, 100.0);
for resolved in [
resolved_object_view_box(empty.clone(), Some(natural)),
resolved_object_view_box(empty, None),
] {
assert!(!resolved.applies());
assert_eq!(
resolved.source_rect().size,
NormalizedObjectSourceSize::new(1.0, 1.0)
);
assert_eq!(resolved.effective_natural_size(natural), natural);
}
}
#[test]
fn svg_without_both_natural_axes_ignores_object_view_box() {
let asset = Rc::new(
crate::svg::parse_svg_bytes(
br#"<svg xmlns="http://www.w3.org/2000/svg"><rect width="100%" height="100%"/></svg>"#,
)
.expect("dimensionless SVG parses"),
);
let view_box = css::ObjectViewBox::Inset {
top: css::ComputedLengthPercentage::from_points(50.0),
right: css::ComputedLengthPercentage::ZERO,
bottom: css::ComputedLengthPercentage::ZERO,
left: css::ComputedLengthPercentage::ZERO,
radii: None,
};
let resolved = resolved_object_view_box_for_svg(view_box, &asset);
assert!(!resolved.applies());
assert_eq!(
resolved.source_rect().size,
NormalizedObjectSourceSize::new(1.0, 1.0)
);
}
#[test]
fn border_image_percentage_slices_use_the_concrete_svg_viewport() {
let slices = used_border_image_slices(
css::BorderImageSliceOffsets {
top: css::BorderImageSliceValue::Percent(0.40),
right: css::BorderImageSliceValue::Percent(0.30),
bottom: css::BorderImageSliceValue::Percent(0.20),
left: css::BorderImageSliceValue::Percent(0.10),
},
200.0,
200.0,
);
assert!((slices.top - 80.0).abs() < 0.001);
assert!((slices.right - 60.0).abs() < 0.001);
assert!((slices.bottom - 40.0).abs() < 0.001);
assert!((slices.left - 20.0).abs() < 0.001);
}
#[test]
fn border_image_numeric_slices_keep_concrete_svg_coordinates() {
let slices = used_border_image_slices(
css::BorderImageSliceOffsets {
top: css::BorderImageSliceValue::Number(40.0),
right: css::BorderImageSliceValue::Number(30.0),
bottom: css::BorderImageSliceValue::Number(20.0),
left: css::BorderImageSliceValue::Number(10.0),
},
200.0,
200.0,
);
assert_eq!(slices.top, 40.0);
assert_eq!(slices.right, 30.0);
assert_eq!(slices.bottom, 20.0);
assert_eq!(slices.left, 10.0);
}
#[test]
fn border_image_opposing_slices_overlap_without_rescaling() {
let slices = used_border_image_slices(
css::BorderImageSliceOffsets {
top: css::BorderImageSliceValue::Number(80.0),
right: css::BorderImageSliceValue::Number(90.0),
bottom: css::BorderImageSliceValue::Number(70.0),
left: css::BorderImageSliceValue::Number(60.0),
},
100.0,
100.0,
);
assert_eq!(slices.top, 80.0);
assert_eq!(slices.bottom, 70.0);
assert_eq!(slices.left, 60.0);
assert_eq!(slices.right, 90.0);
}
#[test]
fn border_image_width_percentages_use_the_outset_area() {
let mut style = ComputedStyle::initial();
style.border_image.width = css::BorderImageWidth {
top: css::BorderImageWidthValue::LengthPercentage(
css::ComputedLengthPercentage::from_percent(0.5),
),
right: css::BorderImageWidthValue::LengthPercentage(
css::ComputedLengthPercentage::from_percent(0.5),
),
bottom: css::BorderImageWidthValue::LengthPercentage(
css::ComputedLengthPercentage::from_percent(0.5),
),
left: css::BorderImageWidthValue::LengthPercentage(
css::ComputedLengthPercentage::from_percent(0.5),
),
};
let widths = used_border_image_widths(
&style,
css::Edges {
top: 0.0,
right: 0.0,
bottom: 0.0,
left: 0.0,
},
border_box_pt(75.0),
border_box_pt(75.0),
UsedBorderImageSlices {
top: 1.0,
right: 1.0,
bottom: 1.0,
left: 1.0,
},
);
assert_eq!(
widths,
css::Edges {
top: 37.5,
right: 37.5,
bottom: 37.5,
left: 37.5,
}
);
}
#[test]
fn numeric_border_image_outset_uses_computed_width_input() {
let mut style = ComputedStyle::initial();
style.border_image.outset = css::BorderImageOutset {
top: css::BorderImageOutsetValue::Number(2.0),
right: css::BorderImageOutsetValue::Number(2.0),
bottom: css::BorderImageOutsetValue::Number(2.0),
left: css::BorderImageOutsetValue::Number(2.0),
};
let outsets = used_border_image_outsets(
&style,
css::Edges {
top: 5.0,
right: 5.0,
bottom: 5.0,
left: 5.0,
},
);
assert_eq!(
outsets,
css::Edges {
top: 10.0,
right: 10.0,
bottom: 10.0,
left: 10.0,
}
);
}
#[test]
fn image_function_xywh_fragment_requires_nonzero_integer_geometry() {
assert_eq!(
parse_image_fragment("image.png#xywh=2,3,4,5"),
Some(ParsedImageFragment::Xywh(ImagePixelRect {
x: 2,
y: 3,
width: 4,
height: 5,
}))
);
for href in [
"image.png#xywh=2,3,0,5",
"image.png#xywh=2,3,4.0,5",
"image.png#xywh=pixel:2,3,4,5",
"image.png#xywh=2,3,4,5&foo=bar",
] {
assert_eq!(parse_image_fragment(href), None, "{href}");
}
}
#[test]
fn image_function_xywh_crop_clamps_to_the_source_grid() {
let image = DecodedPngImage::new(10, 8, vec![0; 10 * 8 * 3], None);
let image = clamp_raster_fragment(
image,
ImagePixelRect {
x: 8,
y: 6,
width: 8,
height: 8,
},
)
.unwrap();
assert_eq!(
image.source_rect,
Some(RenderedImageSourceRect {
x: 8,
y: 6,
width: 2,
height: 2,
})
);
assert_eq!(image.natural_size, crate::units::CssPixelSize::new(2, 2));
}
}