mirror of
https://github.com/servo/servo.git
synced 2025-08-06 06:00:15 +01:00
Auto merge of #25512 - servo:background-image, r=nox
Render `background-image: url(…)` … and support most `background-*` properties. Still to do: * `background-attachment` * Gradients
This commit is contained in:
commit
1de92906e8
314 changed files with 1275 additions and 136 deletions
|
@ -18,12 +18,15 @@ atomic_refcell = "0.1"
|
|||
cssparser = "0.27"
|
||||
embedder_traits = {path = "../embedder_traits"}
|
||||
euclid = "0.20"
|
||||
fnv = "1.0"
|
||||
gfx = {path = "../gfx"}
|
||||
gfx_traits = {path = "../gfx_traits"}
|
||||
ipc-channel = "0.12"
|
||||
libc = "0.2"
|
||||
msg = {path = "../msg"}
|
||||
mitochondria = "1.1.2"
|
||||
net_traits = {path = "../net_traits"}
|
||||
parking_lot = "0.9"
|
||||
range = {path = "../range"}
|
||||
rayon = "1"
|
||||
rayon_croissant = "0.2.0"
|
||||
|
@ -32,6 +35,7 @@ script_traits = {path = "../script_traits"}
|
|||
serde = "1.0"
|
||||
servo_arc = { path = "../servo_arc" }
|
||||
servo_geometry = {path = "../geometry"}
|
||||
servo_url = {path = "../url"}
|
||||
style = {path = "../style", features = ["servo", "servo-layout-2020"]}
|
||||
style_traits = {path = "../style_traits"}
|
||||
unicode-script = {version = "0.3", features = ["harfbuzz"]}
|
||||
|
|
|
@ -2,18 +2,51 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use crate::display_list::WebRenderImageInfo;
|
||||
use fnv::FnvHashMap;
|
||||
use gfx::font_cache_thread::FontCacheThread;
|
||||
use gfx::font_context::FontContext;
|
||||
use msg::constellation_msg::PipelineId;
|
||||
use net_traits::image_cache::{CanRequestImages, ImageCache, ImageState};
|
||||
use net_traits::image_cache::{ImageOrMetadataAvailable, UsePlaceholder};
|
||||
use parking_lot::RwLock;
|
||||
use script_layout_interface::{PendingImage, PendingImageState};
|
||||
use servo_url::{ImmutableOrigin, ServoUrl};
|
||||
use std::cell::RefCell;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use style::context::SharedStyleContext;
|
||||
use style::dom::OpaqueNode;
|
||||
|
||||
pub struct LayoutContext<'a> {
|
||||
pub id: PipelineId,
|
||||
pub use_rayon: bool,
|
||||
pub origin: ImmutableOrigin,
|
||||
|
||||
/// Bits shared by the layout and style system.
|
||||
pub style_context: SharedStyleContext<'a>,
|
||||
|
||||
/// Interface to the font cache thread.
|
||||
pub font_cache_thread: Mutex<FontCacheThread>,
|
||||
|
||||
/// Reference to the script thread image cache.
|
||||
pub image_cache: Arc<dyn ImageCache>,
|
||||
|
||||
/// A list of in-progress image loads to be shared with the script thread.
|
||||
/// A None value means that this layout was not initiated by the script thread.
|
||||
pub pending_images: Option<Mutex<Vec<PendingImage>>>,
|
||||
|
||||
pub webrender_image_cache:
|
||||
Arc<RwLock<FnvHashMap<(ServoUrl, UsePlaceholder), WebRenderImageInfo>>>,
|
||||
}
|
||||
|
||||
impl<'a> Drop for LayoutContext<'a> {
|
||||
fn drop(&mut self) {
|
||||
if !std::thread::panicking() {
|
||||
if let Some(ref pending_images) = self.pending_images {
|
||||
assert!(pending_images.lock().unwrap().is_empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> LayoutContext<'a> {
|
||||
|
@ -21,6 +54,100 @@ impl<'a> LayoutContext<'a> {
|
|||
pub fn shared_context(&self) -> &SharedStyleContext {
|
||||
&self.style_context
|
||||
}
|
||||
|
||||
pub fn get_or_request_image_or_meta(
|
||||
&self,
|
||||
node: OpaqueNode,
|
||||
url: ServoUrl,
|
||||
use_placeholder: UsePlaceholder,
|
||||
) -> Option<ImageOrMetadataAvailable> {
|
||||
//XXXjdm For cases where we do not request an image, we still need to
|
||||
// ensure the node gets another script-initiated reflow or it
|
||||
// won't be requested at all.
|
||||
let can_request = if self.pending_images.is_some() {
|
||||
CanRequestImages::Yes
|
||||
} else {
|
||||
CanRequestImages::No
|
||||
};
|
||||
|
||||
// See if the image is already available
|
||||
let result = self.image_cache.find_image_or_metadata(
|
||||
url.clone(),
|
||||
self.origin.clone(),
|
||||
None,
|
||||
use_placeholder,
|
||||
can_request,
|
||||
);
|
||||
match result {
|
||||
Ok(image_or_metadata) => Some(image_or_metadata),
|
||||
// Image failed to load, so just return nothing
|
||||
Err(ImageState::LoadError) => None,
|
||||
// Not yet requested - request image or metadata from the cache
|
||||
Err(ImageState::NotRequested(id)) => {
|
||||
let image = PendingImage {
|
||||
state: PendingImageState::Unrequested(url),
|
||||
node: node.into(),
|
||||
id: id,
|
||||
};
|
||||
self.pending_images
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.lock()
|
||||
.unwrap()
|
||||
.push(image);
|
||||
None
|
||||
},
|
||||
// Image has been requested, is still pending. Return no image for this paint loop.
|
||||
// When the image loads it will trigger a reflow and/or repaint.
|
||||
Err(ImageState::Pending(id)) => {
|
||||
//XXXjdm if self.pending_images is not available, we should make sure that
|
||||
// this node gets marked dirty again so it gets a script-initiated
|
||||
// reflow that deals with this properly.
|
||||
if let Some(ref pending_images) = self.pending_images {
|
||||
let image = PendingImage {
|
||||
state: PendingImageState::PendingResponse,
|
||||
node: node.into(),
|
||||
id: id,
|
||||
};
|
||||
pending_images.lock().unwrap().push(image);
|
||||
}
|
||||
None
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_webrender_image_for_url(
|
||||
&self,
|
||||
node: OpaqueNode,
|
||||
url: ServoUrl,
|
||||
use_placeholder: UsePlaceholder,
|
||||
) -> Option<WebRenderImageInfo> {
|
||||
if let Some(existing_webrender_image) = self
|
||||
.webrender_image_cache
|
||||
.read()
|
||||
.get(&(url.clone(), use_placeholder))
|
||||
{
|
||||
return Some((*existing_webrender_image).clone());
|
||||
}
|
||||
|
||||
match self.get_or_request_image_or_meta(node, url.clone(), use_placeholder) {
|
||||
Some(ImageOrMetadataAvailable::ImageAvailable(image, _)) => {
|
||||
let image_info = WebRenderImageInfo {
|
||||
width: image.width,
|
||||
height: image.height,
|
||||
key: image.id,
|
||||
};
|
||||
if image_info.key.is_none() {
|
||||
Some(image_info)
|
||||
} else {
|
||||
let mut webrender_image_cache = self.webrender_image_cache.write();
|
||||
webrender_image_cache.insert((url, use_placeholder), image_info);
|
||||
Some(image_info)
|
||||
}
|
||||
},
|
||||
None | Some(ImageOrMetadataAvailable::MetadataAvailable(_)) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) type LayoutFontContext = FontContext<FontCacheThread>;
|
||||
|
|
|
@ -2,11 +2,15 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use crate::context::LayoutContext;
|
||||
use crate::fragments::{BoxFragment, Fragment};
|
||||
use crate::geom::physical::{Rect, Vec2};
|
||||
use crate::replaced::IntrinsicSizes;
|
||||
use embedder_traits::Cursor;
|
||||
use euclid::{Point2D, SideOffsets2D, Size2D};
|
||||
use euclid::{Point2D, SideOffsets2D, Size2D, Vector2D};
|
||||
use gfx::text::glyph::GlyphStore;
|
||||
use mitochondria::OnceCell;
|
||||
use net_traits::image_cache::UsePlaceholder;
|
||||
use std::sync::Arc;
|
||||
use style::dom::OpaqueNode;
|
||||
use style::properties::ComputedValues;
|
||||
|
@ -14,12 +18,20 @@ use style::values::computed::{BorderStyle, Length, LengthPercentage};
|
|||
use style::values::specified::ui::CursorKind;
|
||||
use webrender_api::{self as wr, units};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct WebRenderImageInfo {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub key: Option<wr::ImageKey>,
|
||||
}
|
||||
|
||||
// `webrender_api::display_item::ItemTag` is private
|
||||
type ItemTag = (u64, u16);
|
||||
type HitInfo = Option<ItemTag>;
|
||||
|
||||
pub struct DisplayListBuilder {
|
||||
pub struct DisplayListBuilder<'a> {
|
||||
current_space_and_clip: wr::SpaceAndClipInfo,
|
||||
pub context: &'a LayoutContext<'a>,
|
||||
pub wr: wr::DisplayListBuilder,
|
||||
|
||||
/// Contentful paint, for the purpose of
|
||||
|
@ -29,11 +41,16 @@ pub struct DisplayListBuilder {
|
|||
pub is_contentful: bool,
|
||||
}
|
||||
|
||||
impl DisplayListBuilder {
|
||||
pub fn new(pipeline_id: wr::PipelineId, viewport_size: wr::units::LayoutSize) -> Self {
|
||||
impl<'a> DisplayListBuilder<'a> {
|
||||
pub fn new(
|
||||
pipeline_id: wr::PipelineId,
|
||||
context: &'a LayoutContext,
|
||||
viewport_size: wr::units::LayoutSize,
|
||||
) -> Self {
|
||||
Self {
|
||||
current_space_and_clip: wr::SpaceAndClipInfo::root_scroll(pipeline_id),
|
||||
is_contentful: false,
|
||||
context,
|
||||
wr: wr::DisplayListBuilder::new(pipeline_id, viewport_size),
|
||||
}
|
||||
}
|
||||
|
@ -60,9 +77,7 @@ impl Fragment {
|
|||
containing_block: &Rect<Length>,
|
||||
) {
|
||||
match self {
|
||||
Fragment::Box(b) => {
|
||||
BuilderForBoxFragment::new(b, containing_block).build(builder, containing_block)
|
||||
},
|
||||
Fragment::Box(b) => BuilderForBoxFragment::new(b, containing_block).build(builder),
|
||||
Fragment::Anonymous(a) => {
|
||||
let rect = a
|
||||
.rect
|
||||
|
@ -92,7 +107,6 @@ impl Fragment {
|
|||
.push_text(&common, rect.into(), &glyphs, t.font_key, rgba(color), None);
|
||||
},
|
||||
Fragment::Image(i) => {
|
||||
use style::computed_values::image_rendering::T as ImageRendering;
|
||||
builder.is_contentful = true;
|
||||
let rect = i
|
||||
.rect
|
||||
|
@ -102,11 +116,7 @@ impl Fragment {
|
|||
builder.wr.push_image(
|
||||
&common,
|
||||
rect.into(),
|
||||
match i.style.get_inherited_box().image_rendering {
|
||||
ImageRendering::Auto => wr::ImageRendering::Auto,
|
||||
ImageRendering::CrispEdges => wr::ImageRendering::CrispEdges,
|
||||
ImageRendering::Pixelated => wr::ImageRendering::Pixelated,
|
||||
},
|
||||
image_rendering(i.style.get_inherited_box().image_rendering),
|
||||
wr::AlphaType::PremultipliedAlpha,
|
||||
i.image_key,
|
||||
wr::ColorF::WHITE,
|
||||
|
@ -118,16 +128,16 @@ impl Fragment {
|
|||
|
||||
struct BuilderForBoxFragment<'a> {
|
||||
fragment: &'a BoxFragment,
|
||||
containing_block: &'a Rect<Length>,
|
||||
border_rect: units::LayoutRect,
|
||||
padding_rect: OnceCell<units::LayoutRect>,
|
||||
content_rect: OnceCell<units::LayoutRect>,
|
||||
border_radius: wr::BorderRadius,
|
||||
|
||||
// Outer `Option` is `None`: not initialized yet
|
||||
// Inner `Option` is `None`: no border radius, no need to clip
|
||||
border_edge_clip_id: Option<Option<wr::ClipId>>,
|
||||
border_edge_clip_id: OnceCell<Option<wr::ClipId>>,
|
||||
}
|
||||
|
||||
impl<'a> BuilderForBoxFragment<'a> {
|
||||
fn new(fragment: &'a BoxFragment, containing_block: &Rect<Length>) -> Self {
|
||||
fn new(fragment: &'a BoxFragment, containing_block: &'a Rect<Length>) -> Self {
|
||||
let border_rect: units::LayoutRect = fragment
|
||||
.border_rect()
|
||||
.to_physical(fragment.style.writing_mode, containing_block)
|
||||
|
@ -155,29 +165,50 @@ impl<'a> BuilderForBoxFragment<'a> {
|
|||
|
||||
Self {
|
||||
fragment,
|
||||
containing_block,
|
||||
border_rect,
|
||||
border_radius,
|
||||
border_edge_clip_id: None,
|
||||
padding_rect: OnceCell::new(),
|
||||
content_rect: OnceCell::new(),
|
||||
border_edge_clip_id: OnceCell::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn content_rect(&self) -> &units::LayoutRect {
|
||||
self.content_rect.init_once(|| {
|
||||
self.fragment
|
||||
.content_rect
|
||||
.to_physical(self.fragment.style.writing_mode, self.containing_block)
|
||||
.translate(&self.containing_block.top_left)
|
||||
.into()
|
||||
})
|
||||
}
|
||||
|
||||
fn padding_rect(&self) -> &units::LayoutRect {
|
||||
self.padding_rect.init_once(|| {
|
||||
self.fragment
|
||||
.padding_rect()
|
||||
.to_physical(self.fragment.style.writing_mode, self.containing_block)
|
||||
.translate(&self.containing_block.top_left)
|
||||
.into()
|
||||
})
|
||||
}
|
||||
|
||||
fn with_border_edge_clip(
|
||||
&mut self,
|
||||
builder: &mut DisplayListBuilder,
|
||||
common: &mut wr::CommonItemProperties,
|
||||
) {
|
||||
let border_radius = &self.border_radius;
|
||||
let border_rect = &self.border_rect;
|
||||
let initialized = self.border_edge_clip_id.get_or_insert_with(|| {
|
||||
if border_radius.is_zero() {
|
||||
let initialized = self.border_edge_clip_id.init_once(|| {
|
||||
if self.border_radius.is_zero() {
|
||||
None
|
||||
} else {
|
||||
Some(builder.wr.define_clip(
|
||||
&builder.current_space_and_clip,
|
||||
*border_rect,
|
||||
self.border_rect,
|
||||
Some(wr::ComplexClipRegion {
|
||||
rect: *border_rect,
|
||||
radii: *border_radius,
|
||||
rect: self.border_rect,
|
||||
radii: self.border_radius,
|
||||
mode: wr::ClipMode::Clip,
|
||||
}),
|
||||
None,
|
||||
|
@ -189,7 +220,7 @@ impl<'a> BuilderForBoxFragment<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn build(&mut self, builder: &mut DisplayListBuilder, containing_block: &Rect<Length>) {
|
||||
fn build(&mut self, builder: &mut DisplayListBuilder) {
|
||||
let hit_info = hit_info(&self.fragment.style, self.fragment.tag, Cursor::Default);
|
||||
if hit_info.is_some() {
|
||||
let mut common = builder.common_properties(self.border_rect);
|
||||
|
@ -198,31 +229,308 @@ impl<'a> BuilderForBoxFragment<'a> {
|
|||
builder.wr.push_hit_test(&common)
|
||||
}
|
||||
|
||||
self.background_display_items(builder);
|
||||
self.border_display_items(builder);
|
||||
self.build_background(builder);
|
||||
self.build_border(builder);
|
||||
let content_rect = self
|
||||
.fragment
|
||||
.content_rect
|
||||
.to_physical(self.fragment.style.writing_mode, containing_block)
|
||||
.translate(&containing_block.top_left);
|
||||
.to_physical(self.fragment.style.writing_mode, self.containing_block)
|
||||
.translate(&self.containing_block.top_left);
|
||||
for child in &self.fragment.children {
|
||||
child.build_display_list(builder, &content_rect)
|
||||
}
|
||||
}
|
||||
|
||||
fn background_display_items(&mut self, builder: &mut DisplayListBuilder) {
|
||||
let background_color = self
|
||||
.fragment
|
||||
.style
|
||||
.resolve_color(self.fragment.style.clone_background_color());
|
||||
fn build_background(&mut self, builder: &mut DisplayListBuilder) {
|
||||
use style::values::computed::image::{Image, ImageLayer};
|
||||
let b = self.fragment.style.get_background();
|
||||
let background_color = self.fragment.style.resolve_color(b.background_color);
|
||||
if background_color.alpha > 0 {
|
||||
let mut common = builder.common_properties(self.border_rect);
|
||||
self.with_border_edge_clip(builder, &mut common);
|
||||
builder.wr.push_rect(&common, rgba(background_color))
|
||||
}
|
||||
// Reverse because the property is top layer first, we want to paint bottom layer first.
|
||||
for (index, layer) in b.background_image.0.iter().enumerate().rev() {
|
||||
match layer {
|
||||
ImageLayer::None => {},
|
||||
ImageLayer::Image(image) => match image {
|
||||
Image::Gradient(_gradient) => {
|
||||
// TODO
|
||||
},
|
||||
Image::Url(image_url) => {
|
||||
if let Some(url) = image_url.url() {
|
||||
let webrender_image = builder.context.get_webrender_image_for_url(
|
||||
self.fragment.tag,
|
||||
url.clone(),
|
||||
UsePlaceholder::No,
|
||||
);
|
||||
if let Some(WebRenderImageInfo {
|
||||
width,
|
||||
height,
|
||||
key: Some(key),
|
||||
}) = webrender_image
|
||||
{
|
||||
// FIXME: https://drafts.csswg.org/css-images-4/#the-image-resolution
|
||||
let dppx = 1.0;
|
||||
|
||||
let intrinsic = IntrinsicSizes {
|
||||
width: Some(Length::new(width as f32 / dppx)),
|
||||
height: Some(Length::new(height as f32 / dppx)),
|
||||
// FIXME https://github.com/w3c/csswg-drafts/issues/4572
|
||||
ratio: Some(width as f32 / height as f32),
|
||||
};
|
||||
|
||||
self.build_background_raster_image(builder, index, intrinsic, key)
|
||||
}
|
||||
}
|
||||
},
|
||||
// Gecko-only value, represented as a (boxed) empty enum on non-Gecko.
|
||||
Image::Rect(rect) => match **rect {},
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn border_display_items(&mut self, builder: &mut DisplayListBuilder) {
|
||||
fn build_background_raster_image(
|
||||
&mut self,
|
||||
builder: &mut DisplayListBuilder,
|
||||
index: usize,
|
||||
intrinsic: IntrinsicSizes,
|
||||
key: wr::ImageKey,
|
||||
) {
|
||||
use style::computed_values::background_clip::single_value::T as Clip;
|
||||
use style::computed_values::background_origin::single_value::T as Origin;
|
||||
use style::values::computed::background::BackgroundSize as Size;
|
||||
use style::values::specified::background::BackgroundRepeat as RepeatXY;
|
||||
use style::values::specified::background::BackgroundRepeatKeyword as Repeat;
|
||||
|
||||
fn get_cyclic<T>(values: &[T], index: usize) -> &T {
|
||||
&values[index % values.len()]
|
||||
}
|
||||
|
||||
let b = self.fragment.style.get_background();
|
||||
|
||||
let clipping_area = match get_cyclic(&b.background_clip.0, index) {
|
||||
Clip::ContentBox => self.content_rect(),
|
||||
Clip::PaddingBox => self.padding_rect(),
|
||||
Clip::BorderBox => &self.border_rect,
|
||||
};
|
||||
let positioning_area = match get_cyclic(&b.background_origin.0, index) {
|
||||
Origin::ContentBox => self.content_rect(),
|
||||
Origin::PaddingBox => self.padding_rect(),
|
||||
Origin::BorderBox => &self.border_rect,
|
||||
};
|
||||
|
||||
// https://drafts.csswg.org/css-backgrounds/#background-size
|
||||
enum ContainOrCover {
|
||||
Contain,
|
||||
Cover,
|
||||
}
|
||||
let size_contain_or_cover = |background_size| {
|
||||
let mut tile_size = positioning_area.size;
|
||||
if let Some(intrinsic_ratio) = intrinsic.ratio {
|
||||
let positioning_ratio = positioning_area.size.width / positioning_area.size.height;
|
||||
// Whether the tile width (as opposed to height)
|
||||
// is scaled to that of the positioning area
|
||||
let fit_width = match background_size {
|
||||
ContainOrCover::Contain => positioning_ratio <= intrinsic_ratio,
|
||||
ContainOrCover::Cover => positioning_ratio > intrinsic_ratio,
|
||||
};
|
||||
// The other dimension needs to be adjusted
|
||||
if fit_width {
|
||||
tile_size.height = tile_size.width / intrinsic_ratio
|
||||
} else {
|
||||
tile_size.width = tile_size.height * intrinsic_ratio
|
||||
}
|
||||
}
|
||||
tile_size
|
||||
};
|
||||
let mut tile_size = match get_cyclic(&b.background_size.0, index) {
|
||||
Size::Contain => size_contain_or_cover(ContainOrCover::Contain),
|
||||
Size::Cover => size_contain_or_cover(ContainOrCover::Cover),
|
||||
Size::ExplicitSize { width, height } => {
|
||||
let mut width = width.non_auto().map(|lp| {
|
||||
lp.0.percentage_relative_to(Length::new(positioning_area.size.width))
|
||||
});
|
||||
let mut height = height.non_auto().map(|lp| {
|
||||
lp.0.percentage_relative_to(Length::new(positioning_area.size.height))
|
||||
});
|
||||
|
||||
if width.is_none() && height.is_none() {
|
||||
// Both computed values are 'auto':
|
||||
// use intrinsic sizes, treating missing width or height as 'auto'
|
||||
width = intrinsic.width;
|
||||
height = intrinsic.height;
|
||||
}
|
||||
|
||||
match (width, height) {
|
||||
(Some(w), Some(h)) => units::LayoutSize::new(w.px(), h.px()),
|
||||
(Some(w), None) => {
|
||||
let h = if let Some(intrinsic_ratio) = intrinsic.ratio {
|
||||
w / intrinsic_ratio
|
||||
} else if let Some(intrinsic_height) = intrinsic.height {
|
||||
intrinsic_height
|
||||
} else {
|
||||
// Treated as 100%
|
||||
Length::new(positioning_area.size.height)
|
||||
};
|
||||
units::LayoutSize::new(w.px(), h.px())
|
||||
},
|
||||
(None, Some(h)) => {
|
||||
let w = if let Some(intrinsic_ratio) = intrinsic.ratio {
|
||||
h * intrinsic_ratio
|
||||
} else if let Some(intrinsic_width) = intrinsic.width {
|
||||
intrinsic_width
|
||||
} else {
|
||||
// Treated as 100%
|
||||
Length::new(positioning_area.size.width)
|
||||
};
|
||||
units::LayoutSize::new(w.px(), h.px())
|
||||
},
|
||||
// Both comptued values were 'auto', and neither intrinsic size is present
|
||||
(None, None) => size_contain_or_cover(ContainOrCover::Contain),
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
if tile_size.width == 0.0 || tile_size.height == 0.0 {
|
||||
return;
|
||||
}
|
||||
|
||||
struct Layout1DResult {
|
||||
repeat: bool,
|
||||
bounds_origin: f32,
|
||||
bounds_size: f32,
|
||||
}
|
||||
|
||||
/// Abstract over the horizontal or vertical dimension
|
||||
/// Coordinates (0, 0) for the purpose of this function are the positioning area’s origin.
|
||||
fn layout_1d(
|
||||
tile_size: &mut f32,
|
||||
tile_spacing: &mut f32,
|
||||
mut repeat: Repeat,
|
||||
position: &LengthPercentage,
|
||||
clipping_area_origin: f32,
|
||||
clipping_area_size: f32,
|
||||
positioning_area_size: f32,
|
||||
) -> Layout1DResult {
|
||||
// https://drafts.csswg.org/css-backgrounds/#background-repeat
|
||||
if let Repeat::Round = repeat {
|
||||
*tile_size = positioning_area_size / (positioning_area_size / *tile_size).round();
|
||||
}
|
||||
// https://drafts.csswg.org/css-backgrounds/#background-position
|
||||
let mut position = position
|
||||
.percentage_relative_to(Length::new(positioning_area_size - *tile_size))
|
||||
.px();
|
||||
// https://drafts.csswg.org/css-backgrounds/#background-repeat
|
||||
if let Repeat::Space = repeat {
|
||||
// The most entire tiles we can fit
|
||||
let tile_count = (positioning_area_size / *tile_size).floor();
|
||||
if tile_count >= 2.0 {
|
||||
position = 0.0;
|
||||
// Make the outsides of the first and last of that many tiles
|
||||
// touch the edges of the positioning area:
|
||||
let total_space = positioning_area_size - *tile_size * tile_count;
|
||||
let spaces_count = tile_count - 1.0;
|
||||
*tile_spacing = total_space / spaces_count;
|
||||
} else {
|
||||
repeat = Repeat::NoRepeat
|
||||
}
|
||||
}
|
||||
match repeat {
|
||||
Repeat::Repeat | Repeat::Round | Repeat::Space => {
|
||||
// WebRender’s `RepeatingImageDisplayItem` contains a `bounds` rectangle and:
|
||||
//
|
||||
// * The tiling is clipped to the intersection of `clip_rect` and `bounds`
|
||||
// * The origin (top-left corner) of `bounds` is the position
|
||||
// of the “first” (top-left-most) tile.
|
||||
//
|
||||
// In the general case that first tile is not the one that is positioned by
|
||||
// `background-position`.
|
||||
// We want it to be the top-left-most tile that intersects with `clip_rect`.
|
||||
// We find it by offsetting by a whole number of strides,
|
||||
// then compute `bounds` such that:
|
||||
//
|
||||
// * Its bottom-right is the bottom-right of `clip_rect`
|
||||
// * Its top-left is the top-left of first tile.
|
||||
let tile_stride = *tile_size + *tile_spacing;
|
||||
let offset = position - clipping_area_origin;
|
||||
let bounds_origin = position - tile_stride * (offset / tile_stride).ceil();
|
||||
let bounds_size = clipping_area_size - bounds_origin - clipping_area_origin;
|
||||
Layout1DResult {
|
||||
repeat: true,
|
||||
bounds_origin,
|
||||
bounds_size,
|
||||
}
|
||||
},
|
||||
Repeat::NoRepeat => {
|
||||
// `RepeatingImageDisplayItem` always repeats in both dimension.
|
||||
// When we want only one of the dimensions to repeat,
|
||||
// we use the `bounds` rectangle to clip the tiling to one tile
|
||||
// in that dimension.
|
||||
Layout1DResult {
|
||||
repeat: false,
|
||||
bounds_origin: position,
|
||||
bounds_size: *tile_size,
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
let mut tile_spacing = units::LayoutSize::zero();
|
||||
let RepeatXY(repeat_x, repeat_y) = *get_cyclic(&b.background_repeat.0, index);
|
||||
let result_x = layout_1d(
|
||||
&mut tile_size.width,
|
||||
&mut tile_spacing.width,
|
||||
repeat_x,
|
||||
get_cyclic(&b.background_position_x.0, index),
|
||||
clipping_area.origin.x - positioning_area.origin.x,
|
||||
clipping_area.size.width,
|
||||
positioning_area.size.width,
|
||||
);
|
||||
let result_y = layout_1d(
|
||||
&mut tile_size.height,
|
||||
&mut tile_spacing.height,
|
||||
repeat_y,
|
||||
get_cyclic(&b.background_position_y.0, index),
|
||||
clipping_area.origin.y - positioning_area.origin.y,
|
||||
clipping_area.size.height,
|
||||
positioning_area.size.height,
|
||||
);
|
||||
let bounds = units::LayoutRect::new(
|
||||
positioning_area.origin + Vector2D::new(result_x.bounds_origin, result_y.bounds_origin),
|
||||
Size2D::new(result_x.bounds_size, result_y.bounds_size),
|
||||
);
|
||||
|
||||
// The 'backgound-clip' property maps directly to `clip_rect` in `CommonItemProperties`:
|
||||
let mut common = builder.common_properties(*clipping_area);
|
||||
self.with_border_edge_clip(builder, &mut common);
|
||||
|
||||
if result_x.repeat || result_y.repeat {
|
||||
builder.wr.push_repeating_image(
|
||||
&common,
|
||||
bounds,
|
||||
tile_size,
|
||||
tile_spacing,
|
||||
image_rendering(self.fragment.style.clone_image_rendering()),
|
||||
wr::AlphaType::PremultipliedAlpha,
|
||||
key,
|
||||
wr::ColorF::WHITE,
|
||||
)
|
||||
} else {
|
||||
builder.wr.push_image(
|
||||
&common,
|
||||
bounds,
|
||||
image_rendering(self.fragment.style.clone_image_rendering()),
|
||||
wr::AlphaType::PremultipliedAlpha,
|
||||
key,
|
||||
wr::ColorF::WHITE,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn build_border(&mut self, builder: &mut DisplayListBuilder) {
|
||||
let b = self.fragment.style.get_border();
|
||||
let widths = SideOffsets2D::new(
|
||||
b.border_top_width.px(),
|
||||
|
@ -349,3 +657,12 @@ fn cursor(kind: CursorKind, auto_cursor: Cursor) -> Cursor {
|
|||
CursorKind::ZoomOut => Cursor::ZoomOut,
|
||||
}
|
||||
}
|
||||
|
||||
fn image_rendering(ir: style::computed_values::image_rendering::T) -> wr::ImageRendering {
|
||||
use style::computed_values::image_rendering::T as ImageRendering;
|
||||
match ir {
|
||||
ImageRendering::Auto => wr::ImageRendering::Auto,
|
||||
ImageRendering::CrispEdges => wr::ImageRendering::CrispEdges,
|
||||
ImageRendering::Pixelated => wr::ImageRendering::Pixelated,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,21 +20,25 @@ use style::Zero;
|
|||
#[derive(Debug)]
|
||||
pub(crate) struct ReplacedContent {
|
||||
pub kind: ReplacedContentKind,
|
||||
intrinsic: IntrinsicSizes,
|
||||
}
|
||||
|
||||
/// * Raster images always have an instrinsic width and height, with 1 image pixel = 1px.
|
||||
/// The intrinsic ratio should be based on dividing those.
|
||||
/// See https://github.com/w3c/csswg-drafts/issues/4572 for the case where either is zero.
|
||||
/// PNG specifically disallows this but I (SimonSapin) am not sure about other formats.
|
||||
///
|
||||
/// * Form controls have both intrinsic width and height **but no intrinsic ratio**.
|
||||
/// See https://github.com/w3c/csswg-drafts/issues/1044 and
|
||||
/// https://drafts.csswg.org/css-images/#intrinsic-dimensions “In general, […]”
|
||||
///
|
||||
/// * For SVG, see https://svgwg.org/svg2-draft/coords.html#SizingSVGInCSS
|
||||
/// and again https://github.com/w3c/csswg-drafts/issues/4572.
|
||||
intrinsic_width: Option<Length>,
|
||||
intrinsic_height: Option<Length>,
|
||||
intrinsic_ratio: Option<CSSFloat>,
|
||||
/// * Raster images always have an instrinsic width and height, with 1 image pixel = 1px.
|
||||
/// The intrinsic ratio should be based on dividing those.
|
||||
/// See https://github.com/w3c/csswg-drafts/issues/4572 for the case where either is zero.
|
||||
/// PNG specifically disallows this but I (SimonSapin) am not sure about other formats.
|
||||
///
|
||||
/// * Form controls have both intrinsic width and height **but no intrinsic ratio**.
|
||||
/// See https://github.com/w3c/csswg-drafts/issues/1044 and
|
||||
/// https://drafts.csswg.org/css-images/#intrinsic-dimensions “In general, […]”
|
||||
///
|
||||
/// * For SVG, see https://svgwg.org/svg2-draft/coords.html#SizingSVGInCSS
|
||||
/// and again https://github.com/w3c/csswg-drafts/issues/4572.
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct IntrinsicSizes {
|
||||
pub width: Option<Length>,
|
||||
pub height: Option<Length>,
|
||||
pub ratio: Option<CSSFloat>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -55,10 +59,12 @@ impl ReplacedContent {
|
|||
let height = (intrinsic_size_in_dots.y as CSSFloat) / dppx;
|
||||
return Some(Self {
|
||||
kind: ReplacedContentKind::Image(image),
|
||||
intrinsic_width: Some(Length::new(width)),
|
||||
intrinsic_height: Some(Length::new(height)),
|
||||
// FIXME https://github.com/w3c/csswg-drafts/issues/4572
|
||||
intrinsic_ratio: Some(width / height),
|
||||
intrinsic: IntrinsicSizes {
|
||||
width: Some(Length::new(width)),
|
||||
height: Some(Length::new(height)),
|
||||
// FIXME https://github.com/w3c/csswg-drafts/issues/4572
|
||||
ratio: Some(width / height),
|
||||
},
|
||||
});
|
||||
}
|
||||
None
|
||||
|
@ -66,8 +72,8 @@ impl ReplacedContent {
|
|||
|
||||
fn flow_relative_intrinsic_size(&self, style: &ComputedValues) -> Vec2<Option<Length>> {
|
||||
let intrinsic_size = physical::Vec2 {
|
||||
x: self.intrinsic_width,
|
||||
y: self.intrinsic_height,
|
||||
x: self.intrinsic.width,
|
||||
y: self.intrinsic.height,
|
||||
};
|
||||
intrinsic_size.size_to_flow_relative(style.writing_mode)
|
||||
}
|
||||
|
@ -76,7 +82,7 @@ impl ReplacedContent {
|
|||
&self,
|
||||
style: &ComputedValues,
|
||||
) -> Option<CSSFloat> {
|
||||
self.intrinsic_ratio.map(|width_over_height| {
|
||||
self.intrinsic.ratio.map(|width_over_height| {
|
||||
if style.writing_mode.is_vertical() {
|
||||
1. / width_over_height
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue