layout: Combine layout_2020 and layout_thread_2020 into a crate called layout (#36613)

Now that legacy layout has been removed, the name `layout_2020` doesn't
make much sense any longer, also it's 2025 now for better or worse. The
split between the "layout thread" and "layout" also doesn't make as much
sense since layout doesn't run on it's own thread. There's a possibility
that it will in the future, but that should be something that the user
of the crate controls rather than layout iself.

This is part of the larger layout interface cleanup and optimization
that
@Looriool and I are doing.

Testing: Covered by existing tests as this is just code movement.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-04-19 12:17:03 +02:00 committed by GitHub
parent 3ab5b8c447
commit 7787cab521
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
62 changed files with 58 additions and 122 deletions

View file

@ -0,0 +1,69 @@
[package]
name = "layout"
version.workspace = true
authors.workspace = true
license.workspace = true
edition.workspace = true
publish.workspace = true
rust-version.workspace = true
[lib]
name = "layout"
path = "lib.rs"
test = true
doctest = false
[features]
tracing = ["dep:tracing"]
[dependencies]
app_units = { workspace = true }
atomic_refcell = { workspace = true }
base = { workspace = true }
bitflags = { workspace = true }
canvas_traits = { workspace = true }
compositing_traits = { workspace = true }
constellation_traits = { workspace = true }
data-url = { workspace = true }
embedder_traits = { workspace = true }
euclid = { workspace = true }
fnv = { workspace = true }
fonts = { path = "../fonts" }
fonts_traits = { workspace = true }
fxhash = { workspace = true }
html5ever = { workspace = true }
icu_locid = { workspace = true }
icu_segmenter = { workspace = true }
ipc-channel = { workspace = true }
itertools = { workspace = true }
log = { workspace = true }
malloc_size_of = { workspace = true }
malloc_size_of_derive = { workspace = true }
net_traits = { workspace = true }
parking_lot = { workspace = true }
pixels = { path = "../pixels" }
profile_traits = { workspace = true }
range = { path = "../range" }
rayon = { workspace = true }
script = { path = "../script" }
script_layout_interface = { workspace = true }
script_traits = { workspace = true }
selectors = { workspace = true }
servo_arc = { workspace = true }
servo_config = { path = "../config" }
servo_geometry = { path = "../geometry" }
servo_url = { path = "../url" }
stylo = { workspace = true }
stylo_atoms = { workspace = true }
stylo_traits = { workspace = true }
taffy = { workspace = true }
tracing = { workspace = true, optional = true }
unicode-bidi = { workspace = true }
unicode-script = { workspace = true }
url = { workspace = true }
webrender_api = { workspace = true }
xi-unicode = { workspace = true }
[dev-dependencies]
num-traits = { workspace = true }
quickcheck = "1"

60
components/layout/cell.rs Normal file
View file

@ -0,0 +1,60 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 std::fmt;
use std::ops::Deref;
use atomic_refcell::AtomicRefCell;
use malloc_size_of_derive::MallocSizeOf;
use servo_arc::Arc;
#[derive(MallocSizeOf)]
pub struct ArcRefCell<T> {
#[conditional_malloc_size_of]
value: Arc<AtomicRefCell<T>>,
}
impl<T> ArcRefCell<T> {
pub fn new(value: T) -> Self {
Self {
value: Arc::new(AtomicRefCell::new(value)),
}
}
}
impl<T> Clone for ArcRefCell<T> {
fn clone(&self) -> Self {
Self {
value: self.value.clone(),
}
}
}
impl<T> Default for ArcRefCell<T>
where
T: Default,
{
fn default() -> Self {
Self {
value: Arc::new(AtomicRefCell::new(Default::default())),
}
}
}
impl<T> Deref for ArcRefCell<T> {
type Target = AtomicRefCell<T>;
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl<T> fmt::Debug for ArcRefCell<T>
where
T: fmt::Debug,
{
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
self.value.fmt(formatter)
}
}

View file

@ -0,0 +1,231 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
//! Layout construction code that is shared between modern layout modes (Flexbox and CSS Grid)
use std::borrow::Cow;
use std::sync::LazyLock;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use style::selector_parser::PseudoElement;
use crate::PropagatedBoxTreeData;
use crate::context::LayoutContext;
use crate::dom::{BoxSlot, NodeExt};
use crate::dom_traversal::{Contents, NodeAndStyleInfo, TraversalHandler};
use crate::flow::inline::construct::InlineFormattingContextBuilder;
use crate::flow::{BlockContainer, BlockFormattingContext};
use crate::formatting_contexts::{
IndependentFormattingContext, IndependentFormattingContextContents,
IndependentNonReplacedContents,
};
use crate::layout_box_base::LayoutBoxBase;
use crate::style_ext::DisplayGeneratingBox;
/// A builder used for both flex and grid containers.
pub(crate) struct ModernContainerBuilder<'a, 'dom, Node> {
context: &'a LayoutContext<'a>,
info: &'a NodeAndStyleInfo<Node>,
propagated_data: PropagatedBoxTreeData,
contiguous_text_runs: Vec<ModernContainerTextRun<'dom, Node>>,
/// To be run in parallel with rayon in `finish`
jobs: Vec<ModernContainerJob<'dom, Node>>,
has_text_runs: bool,
}
enum ModernContainerJob<'dom, Node> {
ElementOrPseudoElement {
info: NodeAndStyleInfo<Node>,
display: DisplayGeneratingBox,
contents: Contents,
box_slot: BoxSlot<'dom>,
},
TextRuns(Vec<ModernContainerTextRun<'dom, Node>>),
}
struct ModernContainerTextRun<'dom, Node> {
info: NodeAndStyleInfo<Node>,
text: Cow<'dom, str>,
}
impl<Node> ModernContainerTextRun<'_, Node> {
/// <https://drafts.csswg.org/css-text/#white-space>
fn is_only_document_white_space(&self) -> bool {
// FIXME: is this the right definition? See
// https://github.com/w3c/csswg-drafts/issues/5146
// https://github.com/w3c/csswg-drafts/issues/5147
self.text
.bytes()
.all(|byte| matches!(byte, b' ' | b'\n' | b'\t'))
}
}
pub(crate) enum ModernItemKind {
InFlow,
OutOfFlow,
}
pub(crate) struct ModernItem<'dom> {
pub kind: ModernItemKind,
pub order: i32,
pub box_slot: Option<BoxSlot<'dom>>,
pub formatting_context: IndependentFormattingContext,
}
impl<'dom, Node: 'dom> TraversalHandler<'dom, Node> for ModernContainerBuilder<'_, 'dom, Node>
where
Node: NodeExt<'dom>,
{
fn handle_text(&mut self, info: &NodeAndStyleInfo<Node>, text: Cow<'dom, str>) {
self.contiguous_text_runs.push(ModernContainerTextRun {
info: info.clone(),
text,
})
}
/// Or pseudo-element
fn handle_element(
&mut self,
info: &NodeAndStyleInfo<Node>,
display: DisplayGeneratingBox,
contents: Contents,
box_slot: BoxSlot<'dom>,
) {
self.wrap_any_text_in_anonymous_block_container();
self.jobs.push(ModernContainerJob::ElementOrPseudoElement {
info: info.clone(),
display,
contents,
box_slot,
})
}
}
impl<'a, 'dom, Node: 'dom> ModernContainerBuilder<'a, 'dom, Node>
where
Node: NodeExt<'dom>,
{
pub fn new(
context: &'a LayoutContext<'a>,
info: &'a NodeAndStyleInfo<Node>,
propagated_data: PropagatedBoxTreeData,
) -> Self {
ModernContainerBuilder {
context,
info,
propagated_data: propagated_data.disallowing_percentage_table_columns(),
contiguous_text_runs: Vec::new(),
jobs: Vec::new(),
has_text_runs: false,
}
}
fn wrap_any_text_in_anonymous_block_container(&mut self) {
let runs = std::mem::take(&mut self.contiguous_text_runs);
if runs
.iter()
.all(ModernContainerTextRun::is_only_document_white_space)
{
// There is no text run, or they all only contain document white space characters
} else {
self.jobs.push(ModernContainerJob::TextRuns(runs));
self.has_text_runs = true;
}
}
pub(crate) fn finish(mut self) -> Vec<ModernItem<'dom>> {
self.wrap_any_text_in_anonymous_block_container();
let anonymous_info = LazyLock::new(|| {
self.info
.pseudo(self.context, PseudoElement::ServoAnonymousBox)
.expect("Should always be able to construct info for anonymous boxes.")
});
let mut children: Vec<ModernItem> = std::mem::take(&mut self.jobs)
.into_par_iter()
.filter_map(|job| match job {
ModernContainerJob::TextRuns(runs) => {
let mut inline_formatting_context_builder =
InlineFormattingContextBuilder::new();
for flex_text_run in runs.into_iter() {
inline_formatting_context_builder
.push_text(flex_text_run.text, &flex_text_run.info);
}
let inline_formatting_context = inline_formatting_context_builder.finish(
self.context,
self.propagated_data,
true, /* has_first_formatted_line */
false, /* is_single_line_text_box */
self.info.style.writing_mode.to_bidi_level(),
)?;
let block_formatting_context = BlockFormattingContext::from_block_container(
BlockContainer::InlineFormattingContext(inline_formatting_context),
);
let info: &NodeAndStyleInfo<_> = &*anonymous_info;
let formatting_context = IndependentFormattingContext {
base: LayoutBoxBase::new(info.into(), info.style.clone()),
contents: IndependentFormattingContextContents::NonReplaced(
IndependentNonReplacedContents::Flow(block_formatting_context),
),
};
Some(ModernItem {
kind: ModernItemKind::InFlow,
order: 0,
box_slot: None,
formatting_context,
})
},
ModernContainerJob::ElementOrPseudoElement {
info,
display,
contents,
box_slot,
} => {
let is_abspos = info.style.get_box().position.is_absolutely_positioned();
// Text decorations are not propagated to any out-of-flow descendants. In addition,
// absolutes don't affect the size of ancestors so it is fine to allow descendent
// tables to resolve percentage columns.
let propagated_data = match is_abspos {
false => self.propagated_data,
true => PropagatedBoxTreeData::default(),
};
let formatting_context = IndependentFormattingContext::construct(
self.context,
&info,
display.display_inside(),
contents,
propagated_data,
);
if is_abspos {
Some(ModernItem {
kind: ModernItemKind::OutOfFlow,
order: 0,
box_slot: Some(box_slot),
formatting_context,
})
} else {
Some(ModernItem {
kind: ModernItemKind::InFlow,
order: info.style.clone_order(),
box_slot: Some(box_slot),
formatting_context,
})
}
},
})
.collect::<Vec<_>>();
// https://drafts.csswg.org/css-flexbox/#order-modified-document-order
children.sort_by_key(|child| child.order);
children
}
}

View file

@ -0,0 +1,216 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 std::sync::Arc;
use base::id::PipelineId;
use euclid::Size2D;
use fnv::FnvHashMap;
use fonts::FontContext;
use fxhash::FxHashMap;
use net_traits::image_cache::{
ImageCache, ImageCacheResult, ImageOrMetadataAvailable, UsePlaceholder,
};
use parking_lot::{Mutex, RwLock};
use pixels::Image as PixelImage;
use script_layout_interface::{IFrameSizes, ImageAnimationState, PendingImage, PendingImageState};
use servo_url::{ImmutableOrigin, ServoUrl};
use style::context::SharedStyleContext;
use style::dom::OpaqueNode;
use style::values::computed::image::{Gradient, Image};
use crate::display_list::WebRenderImageInfo;
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>,
/// A FontContext to be used during layout.
pub font_context: Arc<FontContext>,
/// 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.
pub pending_images: Mutex<Vec<PendingImage>>,
/// A collection of `<iframe>` sizes to send back to script.
pub iframe_sizes: Mutex<IFrameSizes>,
pub webrender_image_cache:
Arc<RwLock<FnvHashMap<(ServoUrl, UsePlaceholder), WebRenderImageInfo>>>,
pub node_image_animation_map: Arc<RwLock<FxHashMap<OpaqueNode, ImageAnimationState>>>,
}
pub enum ResolvedImage<'a> {
Gradient(&'a Gradient),
Image(WebRenderImageInfo),
}
impl Drop for LayoutContext<'_> {
fn drop(&mut self) {
if !std::thread::panicking() {
assert!(self.pending_images.lock().is_empty());
}
}
}
impl LayoutContext<'_> {
#[inline(always)]
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> {
// Check for available image or start tracking.
let cache_result = self.image_cache.get_cached_image_status(
url.clone(),
self.origin.clone(),
None,
use_placeholder,
);
match cache_result {
ImageCacheResult::Available(img_or_meta) => Some(img_or_meta),
// 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.
ImageCacheResult::Pending(id) => {
let image = PendingImage {
state: PendingImageState::PendingResponse,
node: node.into(),
id,
origin: self.origin.clone(),
};
self.pending_images.lock().push(image);
None
},
// Not yet requested - request image or metadata from the cache
ImageCacheResult::ReadyForRequest(id) => {
let image = PendingImage {
state: PendingImageState::Unrequested(url),
node: node.into(),
id,
origin: self.origin.clone(),
};
self.pending_images.lock().push(image);
None
},
// Image failed to load, so just return nothing
ImageCacheResult::LoadError => None,
}
}
pub fn handle_animated_image(&self, node: OpaqueNode, image: Arc<PixelImage>) {
let mut store = self.node_image_animation_map.write();
// 1. first check whether node previously being track for animated image.
if let Some(image_state) = store.get(&node) {
// a. if the node is not containing the same image as before.
if image_state.image_key() != image.id {
if image.should_animate() {
// i. Register/Replace tracking item in image_animation_manager.
store.insert(node, ImageAnimationState::new(image));
} else {
// ii. Cancel Action if the node's image is no longer animated.
store.remove(&node);
}
}
} else if image.should_animate() {
store.insert(node, ImageAnimationState::new(image));
}
}
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);
}
match self.get_or_request_image_or_meta(node, url.clone(), use_placeholder) {
Some(ImageOrMetadataAvailable::ImageAvailable { image, .. }) => {
self.handle_animated_image(node, image.clone());
let image_info = WebRenderImageInfo {
size: Size2D::new(image.width, 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 fn resolve_image<'a>(
&self,
node: Option<OpaqueNode>,
image: &'a Image,
) -> Option<ResolvedImage<'a>> {
match image {
// TODO: Add support for PaintWorklet and CrossFade rendering.
Image::None | Image::CrossFade(_) | Image::PaintWorklet(_) => None,
Image::Gradient(gradient) => Some(ResolvedImage::Gradient(gradient)),
Image::Url(image_url) => {
// FIXME: images wont always have in intrinsic width or
// height when support for SVG is added, or a WebRender
// `ImageKey`, for that matter.
//
// FIXME: It feels like this should take into account the pseudo
// element and not just the node.
let image_url = image_url.url()?;
let webrender_info = self.get_webrender_image_for_url(
node?,
image_url.clone().into(),
UsePlaceholder::No,
)?;
Some(ResolvedImage::Image(webrender_info))
},
Image::ImageSet(image_set) => {
image_set
.items
.get(image_set.selected_index)
.and_then(|image| {
self.resolve_image(node, &image.image)
.map(|info| match info {
ResolvedImage::Image(mut image_info) => {
// From <https://drafts.csswg.org/css-images-4/#image-set-notation>:
// > A <resolution> (optional). This is used to help the UA decide
// > which <image-set-option> to choose. If the image reference is
// > for a raster image, it also specifies the images natural
// > resolution, overriding any other source of data that might
// > supply a natural resolution.
image_info.size = (image_info.size.to_f32() /
image.resolution.dppx())
.to_u32();
ResolvedImage::Image(image_info)
},
_ => info,
})
})
},
}
}
}

View file

@ -0,0 +1,361 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 app_units::Au;
use euclid::{Point2D, Size2D, Vector2D};
use style::computed_values::background_attachment::SingleComputedValue as BackgroundAttachment;
use style::computed_values::background_clip::single_value::T as Clip;
use style::computed_values::background_origin::single_value::T as Origin;
use style::properties::ComputedValues;
use style::values::computed::LengthPercentage;
use style::values::computed::background::BackgroundSize as Size;
use style::values::specified::background::{
BackgroundRepeat as RepeatXY, BackgroundRepeatKeyword as Repeat,
};
use webrender_api::{self as wr, units};
use wr::ClipChainId;
use wr::units::LayoutSize;
use crate::replaced::NaturalSizes;
pub(super) struct BackgroundLayer {
pub common: wr::CommonItemProperties,
pub bounds: units::LayoutRect,
pub tile_size: units::LayoutSize,
pub tile_spacing: units::LayoutSize,
pub repeat: bool,
}
#[derive(Debug)]
struct Layout1DResult {
repeat: bool,
bounds_origin: f32,
bounds_size: f32,
tile_spacing: f32,
}
fn get_cyclic<T>(values: &[T], layer_index: usize) -> &T {
&values[layer_index % values.len()]
}
pub(super) struct BackgroundPainter<'a> {
pub style: &'a ComputedValues,
pub positioning_area_override: Option<units::LayoutRect>,
pub painting_area_override: Option<units::LayoutRect>,
}
impl<'a> BackgroundPainter<'a> {
/// Get the painting area for this background, which is the actual rectangle in the
/// current coordinate system that the background will be painted.
pub(super) fn painting_area(
&self,
fragment_builder: &'a super::BuilderForBoxFragment,
builder: &mut super::DisplayListBuilder,
layer_index: usize,
) -> units::LayoutRect {
let fb = fragment_builder;
if let Some(painting_area_override) = self.painting_area_override.as_ref() {
return *painting_area_override;
}
if self.positioning_area_override.is_some() {
return fb.border_rect;
}
let background = self.style.get_background();
if &BackgroundAttachment::Fixed ==
get_cyclic(&background.background_attachment.0, layer_index)
{
let viewport_size = builder.display_list.compositor_info.viewport_size;
return units::LayoutRect::from_origin_and_size(Point2D::origin(), viewport_size);
}
match get_cyclic(&background.background_clip.0, layer_index) {
Clip::ContentBox => *fragment_builder.content_rect(),
Clip::PaddingBox => *fragment_builder.padding_rect(),
Clip::BorderBox => fragment_builder.border_rect,
}
}
fn clip(
&self,
fragment_builder: &'a super::BuilderForBoxFragment,
builder: &mut super::DisplayListBuilder,
layer_index: usize,
) -> Option<ClipChainId> {
if self.painting_area_override.is_some() {
return None;
}
if self.positioning_area_override.is_some() {
return fragment_builder.border_edge_clip(builder, false);
}
// The 'backgound-clip' property maps directly to `clip_rect` in `CommonItemProperties`:
let background = self.style.get_background();
let force_clip_creation = get_cyclic(&background.background_attachment.0, layer_index) ==
&BackgroundAttachment::Fixed;
match get_cyclic(&background.background_clip.0, layer_index) {
Clip::ContentBox => fragment_builder.content_edge_clip(builder, force_clip_creation),
Clip::PaddingBox => fragment_builder.padding_edge_clip(builder, force_clip_creation),
Clip::BorderBox => fragment_builder.border_edge_clip(builder, force_clip_creation),
}
}
/// Get the [`wr::CommonItemProperties`] for this background. This includes any clipping
/// established by border radii as well as special clipping and spatial node assignment
/// necessary for `background-attachment`.
pub(super) fn common_properties(
&self,
fragment_builder: &'a super::BuilderForBoxFragment,
builder: &mut super::DisplayListBuilder,
layer_index: usize,
painting_area: units::LayoutRect,
) -> wr::CommonItemProperties {
let clip = self.clip(fragment_builder, builder, layer_index);
let style = &fragment_builder.fragment.style;
let mut common = builder.common_properties(painting_area, style);
if let Some(clip_chain_id) = clip {
common.clip_chain_id = clip_chain_id;
}
if &BackgroundAttachment::Fixed ==
get_cyclic(&style.get_background().background_attachment.0, layer_index)
{
common.spatial_id = builder.current_reference_frame_scroll_node_id.spatial_id;
}
common
}
/// Get the positioning area of the background which is the rectangle that defines where
/// the origin of the background content is, regardless of where the background is actual
/// painted.
pub(super) fn positioning_area(
&self,
fragment_builder: &'a super::BuilderForBoxFragment,
layer_index: usize,
) -> units::LayoutRect {
if let Some(positioning_area_override) = self.positioning_area_override {
return positioning_area_override;
}
match get_cyclic(
&self.style.get_background().background_attachment.0,
layer_index,
) {
BackgroundAttachment::Scroll => match get_cyclic(
&self.style.get_background().background_origin.0,
layer_index,
) {
Origin::ContentBox => *fragment_builder.content_rect(),
Origin::PaddingBox => *fragment_builder.padding_rect(),
Origin::BorderBox => fragment_builder.border_rect,
},
BackgroundAttachment::Fixed => {
// This isn't the viewport size because that rects larger than the viewport might be
// transformed down into areas smaller than the viewport.
units::LayoutRect::from_origin_and_size(
Point2D::origin(),
LayoutSize::new(f32::MAX, f32::MAX),
)
},
}
}
}
pub(super) fn layout_layer(
fragment_builder: &mut super::BuilderForBoxFragment,
painter: &BackgroundPainter,
builder: &mut super::DisplayListBuilder,
layer_index: usize,
natural_sizes: NaturalSizes,
) -> Option<BackgroundLayer> {
let painting_area = painter.painting_area(fragment_builder, builder, layer_index);
let positioning_area = painter.positioning_area(fragment_builder, layer_index);
let common = painter.common_properties(fragment_builder, builder, layer_index, painting_area);
// 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(natural_ratio) = natural_sizes.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 <= natural_ratio,
ContainOrCover::Cover => positioning_ratio > natural_ratio,
};
// The other dimension needs to be adjusted
if fit_width {
tile_size.height = tile_size.width / natural_ratio
} else {
tile_size.width = tile_size.height * natural_ratio
}
}
tile_size
};
let b = painter.style.get_background();
let mut tile_size = match get_cyclic(&b.background_size.0, layer_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.to_used_value(Au::from_f32_px(positioning_area.size().width))
});
let mut height = height.non_auto().map(|lp| {
lp.0.to_used_value(Au::from_f32_px(positioning_area.size().height))
});
if width.is_none() && height.is_none() {
// Both computed values are 'auto':
// use natural sizes, treating missing width or height as 'auto'
width = natural_sizes.width;
height = natural_sizes.height;
}
match (width, height) {
(Some(w), Some(h)) => units::LayoutSize::new(w.to_f32_px(), h.to_f32_px()),
(Some(w), None) => {
let h = if let Some(natural_ratio) = natural_sizes.ratio {
w.scale_by(1.0 / natural_ratio)
} else if let Some(natural_height) = natural_sizes.height {
natural_height
} else {
// Treated as 100%
Au::from_f32_px(positioning_area.size().height)
};
units::LayoutSize::new(w.to_f32_px(), h.to_f32_px())
},
(None, Some(h)) => {
let w = if let Some(natural_ratio) = natural_sizes.ratio {
h.scale_by(natural_ratio)
} else if let Some(natural_width) = natural_sizes.width {
natural_width
} else {
// Treated as 100%
Au::from_f32_px(positioning_area.size().width)
};
units::LayoutSize::new(w.to_f32_px(), h.to_f32_px())
},
// Both comptued values were 'auto', and neither natural size is present
(None, None) => size_contain_or_cover(ContainOrCover::Contain),
}
},
};
if tile_size.width == 0.0 || tile_size.height == 0.0 {
return None;
}
let RepeatXY(repeat_x, repeat_y) = *get_cyclic(&b.background_repeat.0, layer_index);
let result_x = layout_1d(
&mut tile_size.width,
repeat_x,
get_cyclic(&b.background_position_x.0, layer_index),
painting_area.min.x - positioning_area.min.x,
painting_area.size().width,
positioning_area.size().width,
);
let result_y = layout_1d(
&mut tile_size.height,
repeat_y,
get_cyclic(&b.background_position_y.0, layer_index),
painting_area.min.y - positioning_area.min.y,
painting_area.size().height,
positioning_area.size().height,
);
let bounds = units::LayoutRect::from_origin_and_size(
positioning_area.min + Vector2D::new(result_x.bounds_origin, result_y.bounds_origin),
Size2D::new(result_x.bounds_size, result_y.bounds_size),
);
let tile_spacing = units::LayoutSize::new(result_x.tile_spacing, result_y.tile_spacing);
Some(BackgroundLayer {
common,
bounds,
tile_size,
tile_spacing,
repeat: result_x.repeat || result_y.repeat,
})
}
/// Abstract over the horizontal or vertical dimension
/// Coordinates (0, 0) for the purpose of this function are the positioning areas origin.
fn layout_1d(
tile_size: &mut f32,
mut repeat: Repeat,
position: &LengthPercentage,
painting_area_origin: f32,
painting_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
.to_used_value(Au::from_f32_px(positioning_area_size - *tile_size))
.to_f32_px();
let mut tile_spacing = 0.0;
// 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 => {
// WebRenders `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 - painting_area_origin;
let bounds_origin = position - tile_stride * (offset / tile_stride).ceil();
let bounds_end = painting_area_origin + painting_area_size;
let bounds_size = bounds_end - bounds_origin;
Layout1DResult {
repeat: true,
bounds_origin,
bounds_size,
tile_spacing,
}
},
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,
tile_spacing: 0.0,
}
},
}
}

View file

@ -0,0 +1,259 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 app_units::Au;
use base::id::ScrollTreeNodeId;
use style::values::computed::basic_shape::{BasicShape, ClipPath};
use style::values::computed::length_percentage::NonNegativeLengthPercentage;
use style::values::computed::position::Position;
use style::values::generics::basic_shape::{GenericShapeRadius, ShapeBox, ShapeGeometryBox};
use style::values::generics::position::GenericPositionOrAuto;
use webrender_api::ClipChainId;
use webrender_api::units::{LayoutRect, LayoutSideOffsets, LayoutSize};
use super::{BuilderForBoxFragment, DisplayList, compute_margin_box_radius, normalize_radii};
pub(super) fn build_clip_path_clip_chain_if_necessary(
clip_path: ClipPath,
display_list: &mut DisplayList,
parent_scroll_node_id: &ScrollTreeNodeId,
parent_clip_chain_id: &ClipChainId,
fragment_builder: BuilderForBoxFragment,
) -> Option<ClipChainId> {
let geometry_box = match clip_path {
ClipPath::Shape(_, ShapeGeometryBox::ShapeBox(shape_box)) => shape_box,
ClipPath::Shape(_, ShapeGeometryBox::ElementDependent) => ShapeBox::BorderBox,
ClipPath::Box(ShapeGeometryBox::ShapeBox(shape_box)) => shape_box,
ClipPath::Box(ShapeGeometryBox::ElementDependent) => ShapeBox::BorderBox,
_ => return None,
};
let layout_rect = match geometry_box {
ShapeBox::BorderBox => fragment_builder.border_rect,
ShapeBox::ContentBox => *fragment_builder.content_rect(),
ShapeBox::PaddingBox => *fragment_builder.padding_rect(),
ShapeBox::MarginBox => *fragment_builder.margin_rect(),
};
if let ClipPath::Shape(shape, _) = clip_path {
match *shape {
BasicShape::Circle(_) | BasicShape::Ellipse(_) | BasicShape::Rect(_) => {
build_simple_shape(
*shape,
layout_rect,
parent_scroll_node_id,
parent_clip_chain_id,
display_list,
)
},
BasicShape::Polygon(_) | BasicShape::PathOrShape(_) => None,
}
} else {
Some(create_rect_clip_chain(
match geometry_box {
ShapeBox::MarginBox => compute_margin_box_radius(
fragment_builder.border_radius,
layout_rect.size(),
fragment_builder.fragment,
),
_ => fragment_builder.border_radius,
},
layout_rect,
parent_scroll_node_id,
parent_clip_chain_id,
display_list,
))
}
}
#[cfg_attr(
feature = "tracing",
tracing::instrument(
name = "display_list::build_simple_shape",
skip_all,
fields(servo_profiling = true),
level = "trace",
)
)]
fn build_simple_shape(
shape: BasicShape,
layout_box: LayoutRect,
parent_scroll_node_id: &ScrollTreeNodeId,
parent_clip_chain_id: &ClipChainId,
display_list: &mut DisplayList,
) -> Option<ClipChainId> {
match shape {
BasicShape::Rect(rect) => {
let box_height = Au::from_f32_px(layout_box.height());
let box_width = Au::from_f32_px(layout_box.width());
let insets = LayoutSideOffsets::new(
rect.rect.0.to_used_value(box_height).to_f32_px(),
rect.rect.1.to_used_value(box_width).to_f32_px(),
rect.rect.2.to_used_value(box_height).to_f32_px(),
rect.rect.3.to_used_value(box_width).to_f32_px(),
);
// `inner_rect()` will cause an assertion failure if the insets are larger than the
// rectangle dimension.
let shape_rect = if insets.left + insets.right >= layout_box.width() ||
insets.top + insets.bottom > layout_box.height()
{
LayoutRect::from_origin_and_size(layout_box.min, LayoutSize::zero())
} else {
layout_box.to_rect().inner_rect(insets).to_box2d()
};
let corner = |corner: &style::values::computed::BorderCornerRadius| {
LayoutSize::new(
corner.0.width.0.to_used_value(box_width).to_f32_px(),
corner.0.height.0.to_used_value(box_height).to_f32_px(),
)
};
let mut radii = webrender_api::BorderRadius {
top_left: corner(&rect.round.top_left),
top_right: corner(&rect.round.top_right),
bottom_left: corner(&rect.round.bottom_left),
bottom_right: corner(&rect.round.bottom_right),
};
normalize_radii(&layout_box, &mut radii);
Some(create_rect_clip_chain(
radii,
shape_rect,
parent_scroll_node_id,
parent_clip_chain_id,
display_list,
))
},
BasicShape::Circle(circle) => {
let center = match circle.position {
GenericPositionOrAuto::Position(position) => position,
GenericPositionOrAuto::Auto => Position::center(),
};
let anchor_x = center
.horizontal
.to_used_value(Au::from_f32_px(layout_box.width()));
let anchor_y = center
.vertical
.to_used_value(Au::from_f32_px(layout_box.height()));
let center = layout_box
.min
.add_size(&LayoutSize::new(anchor_x.to_f32_px(), anchor_y.to_f32_px()));
let horizontal =
compute_shape_radius(center.x, &circle.radius, layout_box.min.x, layout_box.max.x);
let vertical =
compute_shape_radius(center.y, &circle.radius, layout_box.min.y, layout_box.max.y);
// If the value is `Length` then both values should be the same at this point.
let radius = match circle.radius {
GenericShapeRadius::FarthestSide => horizontal.max(vertical),
GenericShapeRadius::ClosestSide => horizontal.min(vertical),
GenericShapeRadius::Length(_) => horizontal,
};
let radius = LayoutSize::new(radius, radius);
let mut radii = webrender_api::BorderRadius {
top_left: radius,
top_right: radius,
bottom_left: radius,
bottom_right: radius,
};
let start = center.add_size(&-radius);
let rect = LayoutRect::from_origin_and_size(start, radius * 2.);
normalize_radii(&layout_box, &mut radii);
Some(create_rect_clip_chain(
radii,
rect,
parent_scroll_node_id,
parent_clip_chain_id,
display_list,
))
},
BasicShape::Ellipse(ellipse) => {
let center = match ellipse.position {
GenericPositionOrAuto::Position(position) => position,
GenericPositionOrAuto::Auto => Position::center(),
};
let anchor_x = center
.horizontal
.to_used_value(Au::from_f32_px(layout_box.width()));
let anchor_y = center
.vertical
.to_used_value(Au::from_f32_px(layout_box.height()));
let center = layout_box
.min
.add_size(&LayoutSize::new(anchor_x.to_f32_px(), anchor_y.to_f32_px()));
let width = compute_shape_radius(
center.x,
&ellipse.semiaxis_x,
layout_box.min.x,
layout_box.max.x,
);
let height = compute_shape_radius(
center.y,
&ellipse.semiaxis_y,
layout_box.min.y,
layout_box.max.y,
);
let mut radii = webrender_api::BorderRadius {
top_left: LayoutSize::new(width, height),
top_right: LayoutSize::new(width, height),
bottom_left: LayoutSize::new(width, height),
bottom_right: LayoutSize::new(width, height),
};
let size = LayoutSize::new(width, height);
let start = center.add_size(&-size);
let rect = LayoutRect::from_origin_and_size(start, size * 2.);
normalize_radii(&rect, &mut radii);
Some(create_rect_clip_chain(
radii,
rect,
parent_scroll_node_id,
parent_clip_chain_id,
display_list,
))
},
_ => None,
}
}
fn compute_shape_radius(
center: f32,
radius: &GenericShapeRadius<NonNegativeLengthPercentage>,
min_edge: f32,
max_edge: f32,
) -> f32 {
let distance_from_min_edge = (min_edge - center).abs();
let distance_from_max_edge = (max_edge - center).abs();
match radius {
GenericShapeRadius::FarthestSide => distance_from_min_edge.max(distance_from_max_edge),
GenericShapeRadius::ClosestSide => distance_from_min_edge.min(distance_from_max_edge),
GenericShapeRadius::Length(length) => length
.0
.to_used_value(Au::from_f32_px(max_edge - min_edge))
.to_f32_px(),
}
}
fn create_rect_clip_chain(
radii: webrender_api::BorderRadius,
rect: LayoutRect,
parent_scroll_node_id: &ScrollTreeNodeId,
parent_clip_chain_id: &ClipChainId,
display_list: &mut DisplayList,
) -> ClipChainId {
let new_clip_id = if radii.is_zero() {
display_list
.wr
.define_clip_rect(parent_scroll_node_id.spatial_id, rect)
} else {
display_list.wr.define_clip_rounded_rect(
parent_scroll_node_id.spatial_id,
webrender_api::ComplexClipRegion {
rect,
radii,
mode: webrender_api::ClipMode::Clip,
},
)
};
display_list.define_clip_chain(*parent_clip_chain_id, [new_clip_id])
}

View file

@ -0,0 +1,160 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 app_units::Au;
use style::color::AbsoluteColor;
use style::computed_values::image_rendering::T as ComputedImageRendering;
use style::computed_values::mix_blend_mode::T as ComputedMixBlendMode;
use style::computed_values::text_decoration_style::T as ComputedTextDecorationStyle;
use style::computed_values::transform_style::T as ComputedTransformStyle;
use style::values::computed::Filter as ComputedFilter;
use style::values::specified::border::BorderImageRepeatKeyword;
use webrender_api::{
FilterOp, ImageRendering, LineStyle, MixBlendMode, RepeatMode, Shadow, TransformStyle, units,
};
use crate::geom::{PhysicalPoint, PhysicalRect, PhysicalSides, PhysicalSize};
pub trait ToWebRender {
type Type;
fn to_webrender(&self) -> Self::Type;
}
pub trait FilterToWebRender {
type Type;
fn to_webrender(&self, current_color: &AbsoluteColor) -> Self::Type;
}
impl FilterToWebRender for ComputedFilter {
type Type = FilterOp;
fn to_webrender(&self, current_color: &AbsoluteColor) -> Self::Type {
match *self {
ComputedFilter::Blur(radius) => FilterOp::Blur(radius.px(), radius.px()),
ComputedFilter::Brightness(amount) => FilterOp::Brightness(amount.0),
ComputedFilter::Contrast(amount) => FilterOp::Contrast(amount.0),
ComputedFilter::Grayscale(amount) => FilterOp::Grayscale(amount.0),
ComputedFilter::HueRotate(angle) => FilterOp::HueRotate(angle.radians()),
ComputedFilter::Invert(amount) => FilterOp::Invert(amount.0),
ComputedFilter::Opacity(amount) => FilterOp::Opacity(amount.0.into(), amount.0),
ComputedFilter::Saturate(amount) => FilterOp::Saturate(amount.0),
ComputedFilter::Sepia(amount) => FilterOp::Sepia(amount.0),
ComputedFilter::DropShadow(ref shadow) => FilterOp::DropShadow(Shadow {
blur_radius: shadow.blur.px(),
offset: units::LayoutVector2D::new(shadow.horizontal.px(), shadow.vertical.px()),
color: super::rgba(shadow.color.clone().resolve_to_absolute(current_color)),
}),
// Statically check that Url is impossible.
ComputedFilter::Url(ref url) => match *url {},
}
}
}
impl ToWebRender for ComputedMixBlendMode {
type Type = MixBlendMode;
fn to_webrender(&self) -> Self::Type {
match *self {
ComputedMixBlendMode::Normal => MixBlendMode::Normal,
ComputedMixBlendMode::Multiply => MixBlendMode::Multiply,
ComputedMixBlendMode::Screen => MixBlendMode::Screen,
ComputedMixBlendMode::Overlay => MixBlendMode::Overlay,
ComputedMixBlendMode::Darken => MixBlendMode::Darken,
ComputedMixBlendMode::Lighten => MixBlendMode::Lighten,
ComputedMixBlendMode::ColorDodge => MixBlendMode::ColorDodge,
ComputedMixBlendMode::ColorBurn => MixBlendMode::ColorBurn,
ComputedMixBlendMode::HardLight => MixBlendMode::HardLight,
ComputedMixBlendMode::SoftLight => MixBlendMode::SoftLight,
ComputedMixBlendMode::Difference => MixBlendMode::Difference,
ComputedMixBlendMode::Exclusion => MixBlendMode::Exclusion,
ComputedMixBlendMode::Hue => MixBlendMode::Hue,
ComputedMixBlendMode::Saturation => MixBlendMode::Saturation,
ComputedMixBlendMode::Color => MixBlendMode::Color,
ComputedMixBlendMode::Luminosity => MixBlendMode::Luminosity,
ComputedMixBlendMode::PlusLighter => MixBlendMode::PlusLighter,
}
}
}
impl ToWebRender for ComputedTransformStyle {
type Type = TransformStyle;
fn to_webrender(&self) -> Self::Type {
match *self {
ComputedTransformStyle::Flat => TransformStyle::Flat,
ComputedTransformStyle::Preserve3d => TransformStyle::Preserve3D,
}
}
}
impl ToWebRender for PhysicalPoint<Au> {
type Type = units::LayoutPoint;
fn to_webrender(&self) -> Self::Type {
units::LayoutPoint::new(self.x.to_f32_px(), self.y.to_f32_px())
}
}
impl ToWebRender for PhysicalSize<Au> {
type Type = units::LayoutSize;
fn to_webrender(&self) -> Self::Type {
units::LayoutSize::new(self.width.to_f32_px(), self.height.to_f32_px())
}
}
impl ToWebRender for PhysicalRect<Au> {
type Type = units::LayoutRect;
fn to_webrender(&self) -> Self::Type {
units::LayoutRect::from_origin_and_size(
self.origin.to_webrender(),
self.size.to_webrender(),
)
}
}
impl ToWebRender for PhysicalSides<Au> {
type Type = units::LayoutSideOffsets;
fn to_webrender(&self) -> Self::Type {
units::LayoutSideOffsets::new(
self.top.to_f32_px(),
self.right.to_f32_px(),
self.bottom.to_f32_px(),
self.left.to_f32_px(),
)
}
}
impl ToWebRender for ComputedTextDecorationStyle {
type Type = LineStyle;
fn to_webrender(&self) -> Self::Type {
match *self {
ComputedTextDecorationStyle::Solid => LineStyle::Solid,
ComputedTextDecorationStyle::Dotted => LineStyle::Dotted,
ComputedTextDecorationStyle::Dashed => LineStyle::Dashed,
ComputedTextDecorationStyle::Wavy => LineStyle::Wavy,
_ => LineStyle::Solid,
}
}
}
impl ToWebRender for BorderImageRepeatKeyword {
type Type = RepeatMode;
fn to_webrender(&self) -> Self::Type {
match *self {
BorderImageRepeatKeyword::Stretch => RepeatMode::Stretch,
BorderImageRepeatKeyword::Repeat => RepeatMode::Repeat,
BorderImageRepeatKeyword::Round => RepeatMode::Round,
BorderImageRepeatKeyword::Space => RepeatMode::Space,
}
}
}
impl ToWebRender for ComputedImageRendering {
type Type = ImageRendering;
fn to_webrender(&self) -> Self::Type {
match self {
ComputedImageRendering::Auto => ImageRendering::Auto,
ComputedImageRendering::CrispEdges => ImageRendering::CrispEdges,
ComputedImageRendering::Pixelated => ImageRendering::Pixelated,
}
}
}

View file

@ -0,0 +1,468 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 app_units::Au;
use euclid::Size2D;
use style::Zero;
use style::color::mix::ColorInterpolationMethod;
use style::properties::ComputedValues;
use style::values::computed::image::{EndingShape, Gradient, LineDirection};
use style::values::computed::{Angle, AngleOrPercentage, Color, LengthPercentage, Position};
use style::values::generics::image::{
Circle, ColorStop, Ellipse, GradientFlags, GradientItem, ShapeExtent,
};
use webrender_api::units::LayoutPixel;
use webrender_api::{
self as wr, ConicGradient as WebRenderConicGradient, Gradient as WebRenderLinearGradient,
RadialGradient as WebRenderRadialGradient, units,
};
use wr::ColorF;
pub(super) enum WebRenderGradient {
Linear(WebRenderLinearGradient),
Radial(WebRenderRadialGradient),
Conic(WebRenderConicGradient),
}
pub(super) fn build(
style: &ComputedValues,
gradient: &Gradient,
size: Size2D<f32, LayoutPixel>,
builder: &mut super::DisplayListBuilder,
) -> WebRenderGradient {
match gradient {
Gradient::Linear {
items,
direction,
color_interpolation_method,
flags,
compat_mode: _,
} => build_linear(
style,
items,
direction,
color_interpolation_method,
*flags,
size,
builder,
),
Gradient::Radial {
shape,
position,
color_interpolation_method,
items,
flags,
compat_mode: _,
} => build_radial(
style,
items,
shape,
position,
color_interpolation_method,
*flags,
size,
builder,
),
Gradient::Conic {
angle,
position,
color_interpolation_method,
items,
flags,
} => build_conic(
style,
*angle,
position,
*color_interpolation_method,
items,
*flags,
size,
builder,
),
}
}
/// <https://drafts.csswg.org/css-images-3/#linear-gradients>
pub(super) fn build_linear(
style: &ComputedValues,
items: &[GradientItem<Color, LengthPercentage>],
line_direction: &LineDirection,
_color_interpolation_method: &ColorInterpolationMethod,
flags: GradientFlags,
gradient_box: Size2D<f32, LayoutPixel>,
builder: &mut super::DisplayListBuilder,
) -> WebRenderGradient {
use style::values::specified::position::HorizontalPositionKeyword::*;
use style::values::specified::position::VerticalPositionKeyword::*;
use units::LayoutVector2D as Vec2;
// A vector of length 1.0 in the direction of the gradient line
let direction = match line_direction {
LineDirection::Horizontal(Right) => Vec2::new(1., 0.),
LineDirection::Vertical(Top) => Vec2::new(0., -1.),
LineDirection::Horizontal(Left) => Vec2::new(-1., 0.),
LineDirection::Vertical(Bottom) => Vec2::new(0., 1.),
LineDirection::Angle(angle) => {
let radians = angle.radians();
// “`0deg` points upward,
// and positive angles represent clockwise rotation,
// so `90deg` point toward the right.”
Vec2::new(radians.sin(), -radians.cos())
},
LineDirection::Corner(horizontal, vertical) => {
// “If the argument instead specifies a corner of the box such as `to top left`,
// the gradient line must be angled such that it points
// into the same quadrant as the specified corner,
// and is perpendicular to a line intersecting
// the two neighboring corners of the gradient box.”
// Note that that last line is a diagonal of the gradient box rectangle,
// since two neighboring corners of a third corner
// are necessarily opposite to each other.
// `{ x: gradient_box.width, y: gradient_box.height }` is such a diagonal vector,
// from the bottom left corner to the top right corner of the gradient box.
// (Both coordinates are positive.)
// Changing either or both signs produces the other three (oriented) diagonals.
// Swapping the coordinates `{ x: gradient_box.height, y: gradient_box.height }`
// produces a vector perpendicular to some diagonal of the rectangle.
// Finally, we choose the sign of each cartesian coordinate
// such that our vector points to the desired quadrant.
let x = match horizontal {
Right => gradient_box.height,
Left => -gradient_box.height,
};
let y = match vertical {
Top => gradient_box.width,
Bottom => -gradient_box.width,
};
// `{ x, y }` is now a vector of arbitrary length
// with the same direction as the gradient line.
// This normalizes the length to 1.0:
Vec2::new(x, y).normalize()
},
};
// This formula is given as `abs(W * sin(A)) + abs(H * cos(A))` in a note in the spec, under
// https://drafts.csswg.org/css-images-3/#linear-gradient-syntax
//
// Sketch of a proof:
//
// * Take the top side of the gradient box rectangle. It is a segment of length `W`
// * Project onto the gradient line. You get a segment of length `abs(W * sin(A))`
// * Similarly, the left side of the rectangle (length `H`)
// projects to a segment of length `abs(H * cos(A))`
// * These two segments add up to exactly the gradient line.
//
// See the illustration in the example under
// https://drafts.csswg.org/css-images-3/#linear-gradient-syntax
let gradient_line_length =
(gradient_box.width * direction.x).abs() + (gradient_box.height * direction.y).abs();
let half_gradient_line = direction * (gradient_line_length / 2.);
let center = (gradient_box / 2.).to_vector().to_point();
let start_point = center - half_gradient_line;
let end_point = center + half_gradient_line;
let mut color_stops =
gradient_items_to_color_stops(style, items, Au::from_f32_px(gradient_line_length));
let stops = fixup_stops(&mut color_stops);
let extend_mode = if flags.contains(GradientFlags::REPEATING) {
wr::ExtendMode::Repeat
} else {
wr::ExtendMode::Clamp
};
WebRenderGradient::Linear(builder.wr().create_gradient(
start_point,
end_point,
stops,
extend_mode,
))
}
/// <https://drafts.csswg.org/css-images-3/#radial-gradients>
#[allow(clippy::too_many_arguments)]
pub(super) fn build_radial(
style: &ComputedValues,
items: &[GradientItem<Color, LengthPercentage>],
shape: &EndingShape,
center: &Position,
_color_interpolation_method: &ColorInterpolationMethod,
flags: GradientFlags,
gradient_box: Size2D<f32, LayoutPixel>,
builder: &mut super::DisplayListBuilder,
) -> WebRenderGradient {
let center = units::LayoutPoint::new(
center
.horizontal
.to_used_value(Au::from_f32_px(gradient_box.width))
.to_f32_px(),
center
.vertical
.to_used_value(Au::from_f32_px(gradient_box.height))
.to_f32_px(),
);
let radii = match shape {
EndingShape::Circle(circle) => {
let radius = match circle {
Circle::Radius(r) => r.0.px(),
Circle::Extent(extent) => match extent {
ShapeExtent::ClosestSide | ShapeExtent::Contain => {
let vec = abs_vector_to_corner(gradient_box, center, f32::min);
vec.x.min(vec.y)
},
ShapeExtent::FarthestSide => {
let vec = abs_vector_to_corner(gradient_box, center, f32::max);
vec.x.max(vec.y)
},
ShapeExtent::ClosestCorner => {
abs_vector_to_corner(gradient_box, center, f32::min).length()
},
ShapeExtent::FarthestCorner | ShapeExtent::Cover => {
abs_vector_to_corner(gradient_box, center, f32::max).length()
},
},
};
units::LayoutSize::new(radius, radius)
},
EndingShape::Ellipse(Ellipse::Radii(rx, ry)) => units::LayoutSize::new(
rx.0.to_used_value(Au::from_f32_px(gradient_box.width))
.to_f32_px(),
ry.0.to_used_value(Au::from_f32_px(gradient_box.height))
.to_f32_px(),
),
EndingShape::Ellipse(Ellipse::Extent(extent)) => match extent {
ShapeExtent::ClosestSide | ShapeExtent::Contain => {
abs_vector_to_corner(gradient_box, center, f32::min).to_size()
},
ShapeExtent::FarthestSide => {
abs_vector_to_corner(gradient_box, center, f32::max).to_size()
},
ShapeExtent::ClosestCorner => {
abs_vector_to_corner(gradient_box, center, f32::min).to_size() *
(std::f32::consts::FRAC_1_SQRT_2 * 2.0)
},
ShapeExtent::FarthestCorner | ShapeExtent::Cover => {
abs_vector_to_corner(gradient_box, center, f32::max).to_size() *
(std::f32::consts::FRAC_1_SQRT_2 * 2.0)
},
},
};
/// Returns the distance to the nearest or farthest sides in the respective dimension,
/// depending on `select`.
fn abs_vector_to_corner(
gradient_box: units::LayoutSize,
center: units::LayoutPoint,
select: impl Fn(f32, f32) -> f32,
) -> units::LayoutVector2D {
let left = center.x.abs();
let top = center.y.abs();
let right = (gradient_box.width - center.x).abs();
let bottom = (gradient_box.height - center.y).abs();
units::LayoutVector2D::new(select(left, right), select(top, bottom))
}
// “The gradient lines starting point is at the center of the gradient,
// and it extends toward the right, with the ending point on the point
// where the gradient line intersects the ending shape.”
let gradient_line_length = radii.width;
let mut color_stops =
gradient_items_to_color_stops(style, items, Au::from_f32_px(gradient_line_length));
let stops = fixup_stops(&mut color_stops);
let extend_mode = if flags.contains(GradientFlags::REPEATING) {
wr::ExtendMode::Repeat
} else {
wr::ExtendMode::Clamp
};
WebRenderGradient::Radial(builder.wr().create_radial_gradient(
center,
radii,
stops,
extend_mode,
))
}
/// <https://drafts.csswg.org/css-images-4/#conic-gradients>
#[allow(clippy::too_many_arguments)]
fn build_conic(
style: &ComputedValues,
angle: Angle,
center: &Position,
_color_interpolation_method: ColorInterpolationMethod,
items: &[GradientItem<Color, AngleOrPercentage>],
flags: GradientFlags,
gradient_box: Size2D<f32, LayoutPixel>,
builder: &mut super::DisplayListBuilder<'_>,
) -> WebRenderGradient {
let center = units::LayoutPoint::new(
center
.horizontal
.to_used_value(Au::from_f32_px(gradient_box.width))
.to_f32_px(),
center
.vertical
.to_used_value(Au::from_f32_px(gradient_box.height))
.to_f32_px(),
);
let mut color_stops = conic_gradient_items_to_color_stops(style, items);
let stops = fixup_stops(&mut color_stops);
let extend_mode = if flags.contains(GradientFlags::REPEATING) {
wr::ExtendMode::Repeat
} else {
wr::ExtendMode::Clamp
};
WebRenderGradient::Conic(builder.wr().create_conic_gradient(
center,
angle.radians(),
stops,
extend_mode,
))
}
fn conic_gradient_items_to_color_stops(
style: &ComputedValues,
items: &[GradientItem<Color, AngleOrPercentage>],
) -> Vec<ColorStop<ColorF, f32>> {
// Remove color transititon hints, which are not supported yet.
// https://drafts.csswg.org/css-images-4/#color-transition-hint
//
// This gives an approximation of the gradient that might be visibly wrong,
// but maybe better than not parsing that value at all?
// Its debatble whether thats better or worse
// than not parsing and allowing authors to set a fallback.
// Either way, the best outcome is to add support.
// Gecko does so by approximating the non-linear interpolation
// by up to 10 piece-wise linear segments (9 intermediate color stops)
items
.iter()
.filter_map(|item| {
match item {
GradientItem::SimpleColorStop(color) => Some(ColorStop {
color: super::rgba(style.resolve_color(color)),
position: None,
}),
GradientItem::ComplexColorStop { color, position } => Some(ColorStop {
color: super::rgba(style.resolve_color(color)),
position: match position {
AngleOrPercentage::Percentage(percentage) => Some(percentage.0),
AngleOrPercentage::Angle(angle) => Some(angle.degrees() / 360.),
},
}),
// FIXME: approximate like in:
// https://searchfox.org/mozilla-central/rev/f98dad153b59a985efd4505912588d4651033395/layout/painting/nsCSSRenderingGradients.cpp#315-391
GradientItem::InterpolationHint(_) => None,
}
})
.collect()
}
fn gradient_items_to_color_stops(
style: &ComputedValues,
items: &[GradientItem<Color, LengthPercentage>],
gradient_line_length: Au,
) -> Vec<ColorStop<ColorF, f32>> {
// Remove color transititon hints, which are not supported yet.
// https://drafts.csswg.org/css-images-4/#color-transition-hint
//
// This gives an approximation of the gradient that might be visibly wrong,
// but maybe better than not parsing that value at all?
// Its debatble whether thats better or worse
// than not parsing and allowing authors to set a fallback.
// Either way, the best outcome is to add support.
// Gecko does so by approximating the non-linear interpolation
// by up to 10 piece-wise linear segments (9 intermediate color stops)
items
.iter()
.filter_map(|item| {
match item {
GradientItem::SimpleColorStop(color) => Some(ColorStop {
color: super::rgba(style.resolve_color(color)),
position: None,
}),
GradientItem::ComplexColorStop { color, position } => Some(ColorStop {
color: super::rgba(style.resolve_color(color)),
position: Some(if gradient_line_length.is_zero() {
0.
} else {
position
.to_used_value(gradient_line_length)
.scale_by(1. / gradient_line_length.to_f32_px())
.to_f32_px()
}),
}),
// FIXME: approximate like in:
// https://searchfox.org/mozilla-central/rev/f98dad153b59a985efd4505912588d4651033395/layout/painting/nsCSSRenderingGradients.cpp#315-391
GradientItem::InterpolationHint(_) => None,
}
})
.collect()
}
/// <https://drafts.csswg.org/css-images-4/#color-stop-fixup>
fn fixup_stops(stops: &mut [ColorStop<ColorF, f32>]) -> Vec<wr::GradientStop> {
assert!(!stops.is_empty());
// https://drafts.csswg.org/css-images-4/#color-stop-fixup
if let first_position @ None = &mut stops.first_mut().unwrap().position {
*first_position = Some(0.);
}
if let last_position @ None = &mut stops.last_mut().unwrap().position {
*last_position = Some(1.);
}
let mut iter = stops.iter_mut();
let mut max_so_far = iter.next().unwrap().position.unwrap();
for stop in iter {
if let Some(position) = &mut stop.position {
if *position < max_so_far {
*position = max_so_far
} else {
max_so_far = *position
}
}
}
let mut wr_stops = Vec::with_capacity(stops.len());
let mut iter = stops.iter().enumerate();
let (_, first) = iter.next().unwrap();
let first_stop_position = first.position.unwrap();
wr_stops.push(wr::GradientStop {
offset: first_stop_position,
color: first.color,
});
if stops.len() == 1 {
wr_stops.push(wr_stops[0]);
}
let mut last_positioned_stop_index = 0;
let mut last_positioned_stop_position = first_stop_position;
for (i, stop) in iter {
if let Some(position) = stop.position {
let step_count = i - last_positioned_stop_index;
if step_count > 1 {
let step = (position - last_positioned_stop_position) / step_count as f32;
for j in 1..step_count {
let color = stops[last_positioned_stop_index + j].color;
let offset = last_positioned_stop_position + j as f32 * step;
wr_stops.push(wr::GradientStop { offset, color })
}
}
last_positioned_stop_index = i;
last_positioned_stop_position = position;
wr_stops.push(wr::GradientStop {
offset: position,
color: stop.color,
})
}
}
wr_stops
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

328
components/layout/dom.rs Normal file
View file

@ -0,0 +1,328 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 std::marker::PhantomData;
use std::sync::Arc;
use atomic_refcell::{AtomicRef, AtomicRefCell, AtomicRefMut};
use base::id::{BrowsingContextId, PipelineId};
use html5ever::{local_name, namespace_url, ns};
use pixels::Image;
use script_layout_interface::wrapper_traits::{
LayoutDataTrait, LayoutNode, ThreadSafeLayoutElement, ThreadSafeLayoutNode,
};
use script_layout_interface::{
HTMLCanvasDataSource, LayoutElementType, LayoutNodeType as ScriptLayoutNodeType,
};
use servo_arc::Arc as ServoArc;
use style::properties::ComputedValues;
use style::selector_parser::PseudoElement;
use crate::cell::ArcRefCell;
use crate::context::LayoutContext;
use crate::flexbox::FlexLevelBox;
use crate::flow::BlockLevelBox;
use crate::flow::inline::InlineItem;
use crate::fragment_tree::Fragment;
use crate::geom::PhysicalSize;
use crate::replaced::{CanvasInfo, CanvasSource};
use crate::table::TableLevelBox;
use crate::taffy::TaffyItemBox;
/// The data that is stored in each DOM node that is used by layout.
#[derive(Default)]
pub struct InnerDOMLayoutData {
pub(super) self_box: ArcRefCell<Option<LayoutBox>>,
pub(super) pseudo_before_box: ArcRefCell<Option<LayoutBox>>,
pub(super) pseudo_after_box: ArcRefCell<Option<LayoutBox>>,
pub(super) pseudo_marker_box: ArcRefCell<Option<LayoutBox>>,
}
impl InnerDOMLayoutData {
pub(crate) fn for_pseudo(
&self,
pseudo_element: Option<PseudoElement>,
) -> AtomicRef<Option<LayoutBox>> {
match pseudo_element {
Some(PseudoElement::Before) => self.pseudo_before_box.borrow(),
Some(PseudoElement::After) => self.pseudo_after_box.borrow(),
Some(PseudoElement::Marker) => self.pseudo_marker_box.borrow(),
_ => self.self_box.borrow(),
}
}
}
/// A box that is stored in one of the `DOMLayoutData` slots.
pub(super) enum LayoutBox {
DisplayContents,
BlockLevel(ArcRefCell<BlockLevelBox>),
InlineLevel(ArcRefCell<InlineItem>),
FlexLevel(ArcRefCell<FlexLevelBox>),
TableLevelBox(TableLevelBox),
TaffyItemBox(ArcRefCell<TaffyItemBox>),
}
impl LayoutBox {
fn invalidate_cached_fragment(&self) {
match self {
LayoutBox::DisplayContents => {},
LayoutBox::BlockLevel(block_level_box) => {
block_level_box.borrow().invalidate_cached_fragment()
},
LayoutBox::InlineLevel(inline_item) => {
inline_item.borrow().invalidate_cached_fragment()
},
LayoutBox::FlexLevel(flex_level_box) => {
flex_level_box.borrow().invalidate_cached_fragment()
},
LayoutBox::TaffyItemBox(taffy_item_box) => {
taffy_item_box.borrow_mut().invalidate_cached_fragment()
},
LayoutBox::TableLevelBox(table_box) => table_box.invalidate_cached_fragment(),
}
}
pub(crate) fn fragments(&self) -> Vec<Fragment> {
match self {
LayoutBox::DisplayContents => vec![],
LayoutBox::BlockLevel(block_level_box) => block_level_box.borrow().fragments(),
LayoutBox::InlineLevel(inline_item) => inline_item.borrow().fragments(),
LayoutBox::FlexLevel(flex_level_box) => flex_level_box.borrow().fragments(),
LayoutBox::TaffyItemBox(taffy_item_box) => taffy_item_box.borrow().fragments(),
LayoutBox::TableLevelBox(table_box) => table_box.fragments(),
}
}
}
/// A wrapper for [`InnerDOMLayoutData`]. This is necessary to give the entire data
/// structure interior mutability, as we will need to mutate the layout data of
/// non-mutable DOM nodes.
#[derive(Default)]
pub struct DOMLayoutData(AtomicRefCell<InnerDOMLayoutData>);
// The implementation of this trait allows the data to be stored in the DOM.
impl LayoutDataTrait for DOMLayoutData {}
pub struct BoxSlot<'dom> {
pub(crate) slot: Option<ArcRefCell<Option<LayoutBox>>>,
pub(crate) marker: PhantomData<&'dom ()>,
}
/// A mutable reference to a `LayoutBox` stored in a DOM element.
impl BoxSlot<'_> {
pub(crate) fn new(slot: ArcRefCell<Option<LayoutBox>>) -> Self {
*slot.borrow_mut() = None;
let slot = Some(slot);
Self {
slot,
marker: PhantomData,
}
}
pub(crate) fn dummy() -> Self {
let slot = None;
Self {
slot,
marker: PhantomData,
}
}
pub(crate) fn set(mut self, box_: LayoutBox) {
if let Some(slot) = &mut self.slot {
*slot.borrow_mut() = Some(box_);
}
}
}
impl Drop for BoxSlot<'_> {
fn drop(&mut self) {
if !std::thread::panicking() {
if let Some(slot) = &mut self.slot {
assert!(slot.borrow().is_some(), "failed to set a layout box");
}
}
}
}
pub(crate) trait NodeExt<'dom>: 'dom + LayoutNode<'dom> {
/// Returns the image if its loaded, and its size in image pixels
/// adjusted for `image_density`.
fn as_image(self) -> Option<(Option<Arc<Image>>, PhysicalSize<f64>)>;
fn as_canvas(self) -> Option<(CanvasInfo, PhysicalSize<f64>)>;
fn as_iframe(self) -> Option<(PipelineId, BrowsingContextId)>;
fn as_video(self) -> Option<(Option<webrender_api::ImageKey>, Option<PhysicalSize<f64>>)>;
fn as_typeless_object_with_data_attribute(self) -> Option<String>;
fn style(self, context: &LayoutContext) -> ServoArc<ComputedValues>;
fn layout_data_mut(self) -> AtomicRefMut<'dom, InnerDOMLayoutData>;
fn layout_data(self) -> Option<AtomicRef<'dom, InnerDOMLayoutData>>;
fn element_box_slot(&self) -> BoxSlot<'dom>;
fn pseudo_element_box_slot(&self, which: PseudoElement) -> BoxSlot<'dom>;
fn unset_pseudo_element_box(self, which: PseudoElement);
/// Remove boxes for the element itself, and its `:before` and `:after` if any.
fn unset_all_boxes(self);
fn fragments_for_pseudo(&self, pseudo_element: Option<PseudoElement>) -> Vec<Fragment>;
fn invalidate_cached_fragment(self);
}
impl<'dom, LayoutNodeType> NodeExt<'dom> for LayoutNodeType
where
LayoutNodeType: 'dom + LayoutNode<'dom>,
{
fn as_image(self) -> Option<(Option<Arc<Image>>, PhysicalSize<f64>)> {
let node = self.to_threadsafe();
let (resource, metadata) = node.image_data()?;
let (width, height) = resource
.as_ref()
.map(|image| (image.width, image.height))
.or_else(|| metadata.map(|metadata| (metadata.width, metadata.height)))
.unwrap_or((0, 0));
let (mut width, mut height) = (width as f64, height as f64);
if let Some(density) = node.image_density().filter(|density| *density != 1.) {
width /= density;
height /= density;
}
Some((resource, PhysicalSize::new(width, height)))
}
fn as_video(self) -> Option<(Option<webrender_api::ImageKey>, Option<PhysicalSize<f64>>)> {
let node = self.to_threadsafe();
let data = node.media_data()?;
let natural_size = if let Some(frame) = data.current_frame {
Some(PhysicalSize::new(frame.width.into(), frame.height.into()))
} else {
data.metadata
.map(|meta| PhysicalSize::new(meta.width.into(), meta.height.into()))
};
Some((
data.current_frame.map(|frame| frame.image_key),
natural_size,
))
}
fn as_canvas(self) -> Option<(CanvasInfo, PhysicalSize<f64>)> {
let node = self.to_threadsafe();
let canvas_data = node.canvas_data()?;
let source = match canvas_data.source {
HTMLCanvasDataSource::WebGL(texture_id) => CanvasSource::WebGL(texture_id),
HTMLCanvasDataSource::Image(image_key) => CanvasSource::Image(image_key),
HTMLCanvasDataSource::WebGPU(image_key) => CanvasSource::WebGPU(image_key),
HTMLCanvasDataSource::Empty => CanvasSource::Empty,
};
Some((
CanvasInfo { source },
PhysicalSize::new(canvas_data.width.into(), canvas_data.height.into()),
))
}
fn as_iframe(self) -> Option<(PipelineId, BrowsingContextId)> {
let node = self.to_threadsafe();
match (node.iframe_pipeline_id(), node.iframe_browsing_context_id()) {
(Some(pipeline_id), Some(browsing_context_id)) => {
Some((pipeline_id, browsing_context_id))
},
_ => None,
}
}
fn as_typeless_object_with_data_attribute(self) -> Option<String> {
if self.type_id() != ScriptLayoutNodeType::Element(LayoutElementType::HTMLObjectElement) {
return None;
}
// TODO: This is the what the legacy layout system did, but really if Servo
// supports any `<object>` that's an image, it should support those with URLs
// and `type` attributes with image mime types.
let element = self.to_threadsafe().as_element()?;
if element.get_attr(&ns!(), &local_name!("type")).is_some() {
return None;
}
element
.get_attr(&ns!(), &local_name!("data"))
.map(|string| string.to_owned())
}
fn style(self, context: &LayoutContext) -> ServoArc<ComputedValues> {
self.to_threadsafe().style(context.shared_context())
}
fn layout_data_mut(self) -> AtomicRefMut<'dom, InnerDOMLayoutData> {
if LayoutNode::layout_data(&self).is_none() {
self.initialize_layout_data::<DOMLayoutData>();
}
LayoutNode::layout_data(&self)
.unwrap()
.downcast_ref::<DOMLayoutData>()
.unwrap()
.0
.borrow_mut()
}
fn layout_data(self) -> Option<AtomicRef<'dom, InnerDOMLayoutData>> {
LayoutNode::layout_data(&self)
.map(|data| data.downcast_ref::<DOMLayoutData>().unwrap().0.borrow())
}
fn element_box_slot(&self) -> BoxSlot<'dom> {
BoxSlot::new(self.layout_data_mut().self_box.clone())
}
fn pseudo_element_box_slot(&self, pseudo_element_type: PseudoElement) -> BoxSlot<'dom> {
let data = self.layout_data_mut();
let cell = match pseudo_element_type {
PseudoElement::Before => &data.pseudo_before_box,
PseudoElement::After => &data.pseudo_after_box,
PseudoElement::Marker => &data.pseudo_marker_box,
_ => unreachable!(
"Asked for box slot for unsupported pseudo-element: {:?}",
pseudo_element_type
),
};
BoxSlot::new(cell.clone())
}
fn unset_pseudo_element_box(self, pseudo_element_type: PseudoElement) {
let data = self.layout_data_mut();
let cell = match pseudo_element_type {
PseudoElement::Before => &data.pseudo_before_box,
PseudoElement::After => &data.pseudo_after_box,
PseudoElement::Marker => &data.pseudo_marker_box,
_ => unreachable!(
"Asked for box slot for unsupported pseudo-element: {:?}",
pseudo_element_type
),
};
*cell.borrow_mut() = None;
}
fn unset_all_boxes(self) {
let data = self.layout_data_mut();
*data.self_box.borrow_mut() = None;
*data.pseudo_before_box.borrow_mut() = None;
*data.pseudo_after_box.borrow_mut() = None;
*data.pseudo_marker_box.borrow_mut() = None;
// Stylo already takes care of removing all layout data
// for DOM descendants of elements with `display: none`.
}
fn invalidate_cached_fragment(self) {
let data = self.layout_data_mut();
if let Some(data) = data.self_box.borrow_mut().as_mut() {
data.invalidate_cached_fragment();
}
}
fn fragments_for_pseudo(&self, pseudo_element: Option<PseudoElement>) -> Vec<Fragment> {
NodeExt::layout_data(*self)
.and_then(|layout_data| {
layout_data
.for_pseudo(pseudo_element)
.as_ref()
.map(LayoutBox::fragments)
})
.unwrap_or_default()
}
}

View file

@ -0,0 +1,565 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 std::borrow::Cow;
use std::iter::FusedIterator;
use fonts::ByteIndex;
use html5ever::{LocalName, local_name};
use range::Range;
use script_layout_interface::wrapper_traits::{ThreadSafeLayoutElement, ThreadSafeLayoutNode};
use script_layout_interface::{LayoutElementType, LayoutNodeType};
use selectors::Element as SelectorsElement;
use servo_arc::Arc as ServoArc;
use style::dom::{TElement, TShadowRoot};
use style::properties::ComputedValues;
use style::selector_parser::PseudoElement;
use style::values::generics::counters::{Content, ContentItem};
use style::values::specified::Quotes;
use crate::context::LayoutContext;
use crate::dom::{BoxSlot, LayoutBox, NodeExt};
use crate::fragment_tree::{BaseFragmentInfo, FragmentFlags, Tag};
use crate::quotes::quotes_for_lang;
use crate::replaced::ReplacedContents;
use crate::style_ext::{Display, DisplayGeneratingBox, DisplayInside, DisplayOutside};
/// A data structure used to pass and store related layout information together to
/// avoid having to repeat the same arguments in argument lists.
#[derive(Clone)]
pub(crate) struct NodeAndStyleInfo<Node> {
pub node: Node,
pub pseudo_element_type: Option<PseudoElement>,
pub style: ServoArc<ComputedValues>,
}
impl<'dom, Node: NodeExt<'dom>> NodeAndStyleInfo<Node> {
fn new_with_pseudo(
node: Node,
pseudo_element_type: PseudoElement,
style: ServoArc<ComputedValues>,
) -> Self {
Self {
node,
pseudo_element_type: Some(pseudo_element_type),
style,
}
}
pub(crate) fn new(node: Node, style: ServoArc<ComputedValues>) -> Self {
Self {
node,
pseudo_element_type: None,
style,
}
}
pub(crate) fn is_single_line_text_input(&self) -> bool {
self.node.type_id() == LayoutNodeType::Element(LayoutElementType::HTMLInputElement)
}
pub(crate) fn pseudo(
&self,
context: &LayoutContext,
pseudo_element_type: PseudoElement,
) -> Option<Self> {
let style = self
.node
.to_threadsafe()
.as_element()?
.with_pseudo(pseudo_element_type)?
.style(context.shared_context());
Some(NodeAndStyleInfo {
node: self.node,
pseudo_element_type: Some(pseudo_element_type),
style,
})
}
pub(crate) fn get_selected_style(&self) -> ServoArc<ComputedValues> {
self.node.to_threadsafe().selected_style()
}
pub(crate) fn get_selection_range(&self) -> Option<Range<ByteIndex>> {
self.node.to_threadsafe().selection()
}
}
impl<'dom, Node> From<&NodeAndStyleInfo<Node>> for BaseFragmentInfo
where
Node: NodeExt<'dom>,
{
fn from(info: &NodeAndStyleInfo<Node>) -> Self {
let node = info.node;
let pseudo = info.pseudo_element_type;
let threadsafe_node = node.to_threadsafe();
let mut flags = FragmentFlags::empty();
// Anonymous boxes should not have a tag, because they should not take part in hit testing.
//
// TODO(mrobinson): It seems that anonymous boxes should take part in hit testing in some
// cases, but currently this means that the order of hit test results isn't as expected for
// some WPT tests. This needs more investigation.
if matches!(
pseudo,
Some(PseudoElement::ServoAnonymousBox) |
Some(PseudoElement::ServoAnonymousTable) |
Some(PseudoElement::ServoAnonymousTableCell) |
Some(PseudoElement::ServoAnonymousTableRow)
) {
return Self::anonymous();
}
if let Some(element) = threadsafe_node.as_html_element() {
if element.is_body_element_of_html_element_root() {
flags.insert(FragmentFlags::IS_BODY_ELEMENT_OF_HTML_ELEMENT_ROOT);
}
match element.get_local_name() {
&local_name!("br") => {
flags.insert(FragmentFlags::IS_BR_ELEMENT);
},
&local_name!("table") | &local_name!("th") | &local_name!("td") => {
flags.insert(FragmentFlags::IS_TABLE_TH_OR_TD_ELEMENT);
},
_ => {},
}
if matches!(
element.type_id(),
Some(LayoutNodeType::Element(
LayoutElementType::HTMLInputElement | LayoutElementType::HTMLTextAreaElement
))
) {
flags.insert(FragmentFlags::IS_TEXT_CONTROL);
}
if ThreadSafeLayoutElement::is_root(&element) {
flags.insert(FragmentFlags::IS_ROOT_ELEMENT);
}
};
Self {
tag: Some(Tag::new_pseudo(threadsafe_node.opaque(), pseudo)),
flags,
}
}
}
#[derive(Debug)]
pub(super) enum Contents {
/// Any kind of content that is not replaced, including the contents of pseudo-elements.
NonReplaced(NonReplacedContents),
/// Example: an `<img src=…>` element.
/// <https://drafts.csswg.org/css2/conform.html#replaced-element>
Replaced(ReplacedContents),
}
#[derive(Debug)]
#[allow(clippy::enum_variant_names)]
pub(super) enum NonReplacedContents {
/// Refers to a DOM subtree, plus `::before` and `::after` pseudo-elements.
OfElement,
/// Content of a `::before` or `::after` pseudo-element that is being generated.
/// <https://drafts.csswg.org/css2/generate.html#content>
OfPseudoElement(Vec<PseudoElementContentItem>),
/// Workaround for input and textarea element until we properly implement `display-inside`.
OfTextControl,
}
#[derive(Debug)]
pub(super) enum PseudoElementContentItem {
Text(String),
Replaced(ReplacedContents),
}
pub(super) trait TraversalHandler<'dom, Node>
where
Node: 'dom,
{
fn handle_text(&mut self, info: &NodeAndStyleInfo<Node>, text: Cow<'dom, str>);
/// Or pseudo-element
fn handle_element(
&mut self,
info: &NodeAndStyleInfo<Node>,
display: DisplayGeneratingBox,
contents: Contents,
box_slot: BoxSlot<'dom>,
);
}
fn traverse_children_of<'dom, Node>(
parent_element: Node,
context: &LayoutContext,
handler: &mut impl TraversalHandler<'dom, Node>,
) where
Node: NodeExt<'dom>,
{
traverse_eager_pseudo_element(PseudoElement::Before, parent_element, context, handler);
let is_text_input_element = matches!(
parent_element.type_id(),
LayoutNodeType::Element(LayoutElementType::HTMLInputElement)
);
let is_textarea_element = matches!(
parent_element.type_id(),
LayoutNodeType::Element(LayoutElementType::HTMLTextAreaElement)
);
if is_text_input_element || is_textarea_element {
let info = NodeAndStyleInfo::new(parent_element, parent_element.style(context));
let node_text_content = parent_element.to_threadsafe().node_text_content();
if node_text_content.is_empty() {
// The addition of zero-width space here forces the text input to have an inline formatting
// context that might otherwise be trimmed if there's no text. This is important to ensure
// that the input element is at least as tall as the line gap of the caret:
// <https://drafts.csswg.org/css-ui/#element-with-default-preferred-size>.
//
// This is also used to ensure that the caret will still be rendered when the input is empty.
// TODO: Is there a less hacky way to do this?
handler.handle_text(&info, "\u{200B}".into());
} else {
handler.handle_text(&info, node_text_content);
}
}
if !is_text_input_element && !is_textarea_element {
for child in iter_child_nodes(parent_element) {
if child.is_text_node() {
let info = NodeAndStyleInfo::new(child, child.style(context));
handler.handle_text(&info, child.to_threadsafe().node_text_content());
} else if child.is_element() {
traverse_element(child, context, handler);
}
}
}
traverse_eager_pseudo_element(PseudoElement::After, parent_element, context, handler);
}
fn traverse_element<'dom, Node>(
element: Node,
context: &LayoutContext,
handler: &mut impl TraversalHandler<'dom, Node>,
) where
Node: NodeExt<'dom>,
{
// Clear any existing pseudo-element box slot, because markers are not handled like
// `::before`` and `::after`. They are processed during box tree creation.
element.unset_pseudo_element_box(PseudoElement::Marker);
let replaced = ReplacedContents::for_element(element, context);
let style = element.style(context);
match Display::from(style.get_box().display) {
Display::None => element.unset_all_boxes(),
Display::Contents => {
if replaced.is_some() {
// `display: content` on a replaced element computes to `display: none`
// <https://drafts.csswg.org/css-display-3/#valdef-display-contents>
element.unset_all_boxes()
} else {
element.element_box_slot().set(LayoutBox::DisplayContents);
traverse_children_of(element, context, handler)
}
},
Display::GeneratingBox(display) => {
let contents = if let Some(replaced) = replaced {
Contents::Replaced(replaced)
} else if matches!(
element.type_id(),
LayoutNodeType::Element(
LayoutElementType::HTMLInputElement | LayoutElementType::HTMLTextAreaElement
)
) {
NonReplacedContents::OfTextControl.into()
} else {
NonReplacedContents::OfElement.into()
};
let display = display.used_value_for_contents(&contents);
let box_slot = element.element_box_slot();
let info = NodeAndStyleInfo::new(element, style);
handler.handle_element(&info, display, contents, box_slot);
},
}
}
fn traverse_eager_pseudo_element<'dom, Node>(
pseudo_element_type: PseudoElement,
node: Node,
context: &LayoutContext,
handler: &mut impl TraversalHandler<'dom, Node>,
) where
Node: NodeExt<'dom>,
{
assert!(pseudo_element_type.is_eager());
// First clear any old contents from the node.
node.unset_pseudo_element_box(pseudo_element_type);
let Some(element) = node.to_threadsafe().as_element() else {
return;
};
let Some(pseudo_element) = element.with_pseudo(pseudo_element_type) else {
return;
};
let style = pseudo_element.style(context.shared_context());
if style.ineffective_content_property() {
return;
}
let info = NodeAndStyleInfo::new_with_pseudo(node, pseudo_element_type, style);
match Display::from(info.style.get_box().display) {
Display::None => {},
Display::Contents => {
let items = generate_pseudo_element_content(&info.style, node, context);
let box_slot = node.pseudo_element_box_slot(pseudo_element_type);
box_slot.set(LayoutBox::DisplayContents);
traverse_pseudo_element_contents(&info, context, handler, items);
},
Display::GeneratingBox(display) => {
let items = generate_pseudo_element_content(&info.style, node, context);
let box_slot = node.pseudo_element_box_slot(pseudo_element_type);
let contents = NonReplacedContents::OfPseudoElement(items).into();
handler.handle_element(&info, display, contents, box_slot);
},
}
}
fn traverse_pseudo_element_contents<'dom, Node>(
info: &NodeAndStyleInfo<Node>,
context: &LayoutContext,
handler: &mut impl TraversalHandler<'dom, Node>,
items: Vec<PseudoElementContentItem>,
) where
Node: NodeExt<'dom>,
{
let mut anonymous_info = None;
for item in items {
match item {
PseudoElementContentItem::Text(text) => handler.handle_text(info, text.into()),
PseudoElementContentItem::Replaced(contents) => {
let anonymous_info = anonymous_info.get_or_insert_with(|| {
info.pseudo(context, PseudoElement::ServoAnonymousBox)
.unwrap_or_else(|| info.clone())
});
let display_inline = DisplayGeneratingBox::OutsideInside {
outside: DisplayOutside::Inline,
inside: DisplayInside::Flow {
is_list_item: false,
},
};
// `display` is not inherited, so we get the initial value
debug_assert!(
Display::from(anonymous_info.style.get_box().display) ==
Display::GeneratingBox(display_inline)
);
handler.handle_element(
anonymous_info,
display_inline,
Contents::Replaced(contents),
// We dont keep pointers to boxes generated by contents of pseudo-elements
BoxSlot::dummy(),
)
},
}
}
}
impl Contents {
/// Returns true iff the `try_from` impl below would return `Err(_)`
pub fn is_replaced(&self) -> bool {
matches!(self, Contents::Replaced(_))
}
}
impl From<NonReplacedContents> for Contents {
fn from(non_replaced_contents: NonReplacedContents) -> Self {
Contents::NonReplaced(non_replaced_contents)
}
}
impl std::convert::TryFrom<Contents> for NonReplacedContents {
type Error = &'static str;
fn try_from(contents: Contents) -> Result<Self, Self::Error> {
match contents {
Contents::NonReplaced(non_replaced_contents) => Ok(non_replaced_contents),
Contents::Replaced(_) => {
Err("Tried to covnert a `Contents::Replaced` into `NonReplacedContent`")
},
}
}
}
impl NonReplacedContents {
pub(crate) fn traverse<'dom, Node>(
self,
context: &LayoutContext,
info: &NodeAndStyleInfo<Node>,
handler: &mut impl TraversalHandler<'dom, Node>,
) where
Node: NodeExt<'dom>,
{
match self {
NonReplacedContents::OfElement | NonReplacedContents::OfTextControl => {
traverse_children_of(info.node, context, handler)
},
NonReplacedContents::OfPseudoElement(items) => {
traverse_pseudo_element_contents(info, context, handler, items)
},
}
}
}
fn get_quote_from_pair<I, S>(item: &ContentItem<I>, opening: &S, closing: &S) -> String
where
S: ToString + ?Sized,
{
match item {
ContentItem::OpenQuote => opening.to_string(),
ContentItem::CloseQuote => closing.to_string(),
_ => unreachable!("Got an unexpected ContentItem type when processing quotes."),
}
}
/// <https://www.w3.org/TR/CSS2/generate.html#propdef-content>
fn generate_pseudo_element_content<'dom, Node>(
pseudo_element_style: &ComputedValues,
element: Node,
context: &LayoutContext,
) -> Vec<PseudoElementContentItem>
where
Node: NodeExt<'dom>,
{
match &pseudo_element_style.get_counters().content {
Content::Items(items) => {
let mut vec = vec![];
for item in items.items.iter() {
match item {
ContentItem::String(s) => {
vec.push(PseudoElementContentItem::Text(s.to_string()));
},
ContentItem::Attr(attr) => {
let element = element
.to_threadsafe()
.as_element()
.expect("Expected an element");
// From
// <https://html.spec.whatwg.org/multipage/#case-sensitivity-of-the-css-%27attr%28%29%27-function>
//
// > CSS Values and Units leaves the case-sensitivity of attribute names for
// > the purpose of the `attr()` function to be defined by the host language.
// > [[CSSVALUES]].
// >
// > When comparing the attribute name part of a CSS `attr()`function to the
// > names of namespace-less attributes on HTML elements in HTML documents,
// > the name part of the CSS `attr()` function must first be converted to
// > ASCII lowercase. The same function when compared to other attributes must
// > be compared according to its original case. In both cases, to match the
// > values must be identical to each other (and therefore the comparison is
// > case sensitive).
let attr_name = match element.is_html_element_in_html_document() {
true => &*attr.attribute.to_ascii_lowercase(),
false => &*attr.attribute,
};
let attr_val =
element.get_attr(&attr.namespace_url, &LocalName::from(attr_name));
vec.push(PseudoElementContentItem::Text(
attr_val.map_or("".to_string(), |s| s.to_string()),
));
},
ContentItem::Image(image) => {
if let Some(replaced_content) =
ReplacedContents::from_image(element, context, image)
{
vec.push(PseudoElementContentItem::Replaced(replaced_content));
}
},
ContentItem::OpenQuote | ContentItem::CloseQuote => {
// TODO(xiaochengh): calculate quote depth
let maybe_quote = match &pseudo_element_style.get_list().quotes {
Quotes::QuoteList(quote_list) => {
quote_list.0.first().map(|quote_pair| {
get_quote_from_pair(
item,
&*quote_pair.opening,
&*quote_pair.closing,
)
})
},
Quotes::Auto => {
let lang = &pseudo_element_style.get_font()._x_lang;
let quotes = quotes_for_lang(lang.0.as_ref(), 0);
Some(get_quote_from_pair(item, &quotes.opening, &quotes.closing))
},
};
if let Some(quote) = maybe_quote {
vec.push(PseudoElementContentItem::Text(quote));
}
},
ContentItem::Counter(_, _) |
ContentItem::Counters(_, _, _) |
ContentItem::NoOpenQuote |
ContentItem::NoCloseQuote => {
// TODO: Add support for counters and quotes.
},
}
}
vec
},
Content::Normal | Content::None => unreachable!(),
}
}
pub enum ChildNodeIterator<Node> {
/// Iterating over the children of a node
Node(Option<Node>),
/// Iterating over the assigned nodes of a `HTMLSlotElement`
Slottables(<Vec<Node> as IntoIterator>::IntoIter),
}
#[allow(clippy::unnecessary_to_owned)] // Clippy is wrong.
pub(crate) fn iter_child_nodes<'dom, Node>(parent: Node) -> ChildNodeIterator<Node>
where
Node: NodeExt<'dom>,
{
if let Some(element) = parent.as_element() {
if let Some(shadow) = element.shadow_root() {
return iter_child_nodes(shadow.as_node());
};
let slotted_nodes = element.slotted_nodes();
if !slotted_nodes.is_empty() {
return ChildNodeIterator::Slottables(slotted_nodes.to_owned().into_iter());
}
}
let first = parent.first_child();
ChildNodeIterator::Node(first)
}
impl<'dom, Node> Iterator for ChildNodeIterator<Node>
where
Node: NodeExt<'dom>,
{
type Item = Node;
fn next(&mut self) -> Option<Self::Item> {
match self {
Self::Node(node) => {
let old = *node;
*node = old?.next_sibling();
old
},
Self::Slottables(slots) => slots.next(),
}
}
}
impl<'dom, Node> FusedIterator for ChildNodeIterator<Node> where Node: NodeExt<'dom> {}

View file

@ -0,0 +1,272 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
//! <https://drafts.csswg.org/css-flexbox/#box-model>
use malloc_size_of_derive::MallocSizeOf;
use style::properties::longhands::flex_direction::computed_value::T as FlexDirection;
use crate::geom::{LogicalRect, LogicalSides, LogicalVec2};
#[derive(Clone, Copy, Debug, Default)]
pub(super) struct FlexRelativeVec2<T> {
pub main: T,
pub cross: T,
}
#[derive(Clone, Copy, Debug)]
pub(super) struct FlexRelativeSides<T> {
pub cross_start: T,
pub main_start: T,
pub cross_end: T,
pub main_end: T,
}
pub(super) struct FlexRelativeRect<T> {
pub start_corner: FlexRelativeVec2<T>,
pub size: FlexRelativeVec2<T>,
}
impl<T> std::ops::Add for FlexRelativeVec2<T>
where
T: std::ops::Add,
{
type Output = FlexRelativeVec2<T::Output>;
fn add(self, rhs: Self) -> Self::Output {
FlexRelativeVec2 {
main: self.main + rhs.main,
cross: self.cross + rhs.cross,
}
}
}
impl<T> std::ops::Sub for FlexRelativeVec2<T>
where
T: std::ops::Sub,
{
type Output = FlexRelativeVec2<T::Output>;
fn sub(self, rhs: Self) -> Self::Output {
FlexRelativeVec2 {
main: self.main - rhs.main,
cross: self.cross - rhs.cross,
}
}
}
impl<T> FlexRelativeSides<T> {
pub fn sum_by_axis(self) -> FlexRelativeVec2<T::Output>
where
T: std::ops::Add,
{
FlexRelativeVec2 {
main: self.main_start + self.main_end,
cross: self.cross_start + self.cross_end,
}
}
}
/// One of the two bits set by the `flex-direction` property
/// (The other is "forward" v.s. reverse.)
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq)]
pub(super) enum FlexAxis {
/// The main axis is the inline axis of the container (not necessarily of flex items!),
/// cross is block.
Row,
/// The main axis is the block axis, cross is inline.
Column,
}
/// Which flow-relative sides map to the main-start and cross-start sides, respectively.
/// See <https://drafts.csswg.org/css-flexbox/#box-model>
#[derive(Clone, Copy, Debug, MallocSizeOf)]
pub(super) enum MainStartCrossStart {
InlineStartBlockStart,
InlineStartBlockEnd,
BlockStartInlineStart,
BlockStartInlineEnd,
InlineEndBlockStart,
InlineEndBlockEnd,
BlockEndInlineStart,
BlockEndInlineEnd,
}
impl FlexAxis {
pub fn from(flex_direction: FlexDirection) -> Self {
match flex_direction {
FlexDirection::Row | FlexDirection::RowReverse => FlexAxis::Row,
FlexDirection::Column | FlexDirection::ColumnReverse => FlexAxis::Column,
}
}
pub fn vec2_to_flex_relative<T>(self, flow_relative: LogicalVec2<T>) -> FlexRelativeVec2<T> {
let LogicalVec2 { inline, block } = flow_relative;
match self {
FlexAxis::Row => FlexRelativeVec2 {
main: inline,
cross: block,
},
FlexAxis::Column => FlexRelativeVec2 {
main: block,
cross: inline,
},
}
}
pub fn vec2_to_flow_relative<T>(self, flex_relative: FlexRelativeVec2<T>) -> LogicalVec2<T> {
let FlexRelativeVec2 { main, cross } = flex_relative;
match self {
FlexAxis::Row => LogicalVec2 {
inline: main,
block: cross,
},
FlexAxis::Column => LogicalVec2 {
block: main,
inline: cross,
},
}
}
}
macro_rules! sides_mapping_methods {
(
$(
$variant: path => {
$( $flex_relative_side: ident <=> $flow_relative_side: ident, )+
},
)+
) => {
pub fn sides_to_flex_relative<T>(self, flow_relative: LogicalSides<T>) -> FlexRelativeSides<T> {
match self {
$(
$variant => FlexRelativeSides {
$( $flex_relative_side: flow_relative.$flow_relative_side, )+
},
)+
}
}
pub fn sides_to_flow_relative<T>(self, flex_relative: FlexRelativeSides<T>) -> LogicalSides<T> {
match self {
$(
$variant => LogicalSides {
$( $flow_relative_side: flex_relative.$flex_relative_side, )+
},
)+
}
}
}
}
impl MainStartCrossStart {
pub fn from(flex_direction: FlexDirection, flex_wrap_reverse: bool) -> Self {
match (flex_direction, flex_wrap_reverse) {
// See definition of each keyword in
// https://drafts.csswg.org/css-flexbox/#flex-direction-property and
// https://drafts.csswg.org/css-flexbox/#flex-wrap-property,
// or the tables (though they map to physical rather than flow-relative) at
// https://drafts.csswg.org/css-flexbox/#axis-mapping
(FlexDirection::Row, true) => MainStartCrossStart::InlineStartBlockEnd,
(FlexDirection::Row, false) => MainStartCrossStart::InlineStartBlockStart,
(FlexDirection::Column, true) => MainStartCrossStart::BlockStartInlineEnd,
(FlexDirection::Column, false) => MainStartCrossStart::BlockStartInlineStart,
(FlexDirection::RowReverse, true) => MainStartCrossStart::InlineEndBlockEnd,
(FlexDirection::RowReverse, false) => MainStartCrossStart::InlineEndBlockStart,
(FlexDirection::ColumnReverse, true) => MainStartCrossStart::BlockEndInlineEnd,
(FlexDirection::ColumnReverse, false) => MainStartCrossStart::BlockEndInlineStart,
}
}
sides_mapping_methods! {
MainStartCrossStart::InlineStartBlockStart => {
main_start <=> inline_start,
cross_start <=> block_start,
main_end <=> inline_end,
cross_end <=> block_end,
},
MainStartCrossStart::InlineStartBlockEnd => {
main_start <=> inline_start,
cross_start <=> block_end,
main_end <=> inline_end,
cross_end <=> block_start,
},
MainStartCrossStart::BlockStartInlineStart => {
main_start <=> block_start,
cross_start <=> inline_start,
main_end <=> block_end,
cross_end <=> inline_end,
},
MainStartCrossStart::BlockStartInlineEnd => {
main_start <=> block_start,
cross_start <=> inline_end,
main_end <=> block_end,
cross_end <=> inline_start,
},
MainStartCrossStart::InlineEndBlockStart => {
main_start <=> inline_end,
cross_start <=> block_start,
main_end <=> inline_start,
cross_end <=> block_end,
},
MainStartCrossStart::InlineEndBlockEnd => {
main_start <=> inline_end,
cross_start <=> block_end,
main_end <=> inline_start,
cross_end <=> block_start,
},
MainStartCrossStart::BlockEndInlineStart => {
main_start <=> block_end,
cross_start <=> inline_start,
main_end <=> block_start,
cross_end <=> inline_end,
},
MainStartCrossStart::BlockEndInlineEnd => {
main_start <=> block_end,
cross_start <=> inline_end,
main_end <=> block_start,
cross_end <=> inline_start,
},
}
}
/// The start corner coordinates in both the input rectangle and output rectangle
/// are relative to some “base rectangle” whose size is passed here.
pub(super) fn rect_to_flow_relative<T>(
flex_axis: FlexAxis,
main_start_cross_start_sides_are: MainStartCrossStart,
base_rect_size: FlexRelativeVec2<T>,
rect: FlexRelativeRect<T>,
) -> LogicalRect<T>
where
T: Copy + std::ops::Add<Output = T> + std::ops::Sub<Output = T>,
{
// First, convert from (start corner, size) to offsets from the edges of the base rectangle
let end_corner_position = rect.start_corner + rect.size;
let end_corner_offsets = base_rect_size - end_corner_position;
// No-ops, but hopefully clarifies to human readers:
let start_corner_position = rect.start_corner;
let start_corner_offsets = start_corner_position;
// Then, convert to flow-relative using methods above
let flow_relative_offsets =
main_start_cross_start_sides_are.sides_to_flow_relative(FlexRelativeSides {
main_start: start_corner_offsets.main,
cross_start: start_corner_offsets.cross,
main_end: end_corner_offsets.main,
cross_end: end_corner_offsets.cross,
});
let flow_relative_base_rect_size = flex_axis.vec2_to_flow_relative(base_rect_size);
// Finally, convert back to (start corner, size)
let start_corner = LogicalVec2 {
inline: flow_relative_offsets.inline_start,
block: flow_relative_offsets.block_start,
};
let end_corner_position = LogicalVec2 {
inline: flow_relative_base_rect_size.inline - flow_relative_offsets.inline_end,
block: flow_relative_base_rect_size.block - flow_relative_offsets.block_end,
};
let size = end_corner_position - start_corner;
LogicalRect { start_corner, size }
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,201 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 geom::{FlexAxis, MainStartCrossStart};
use malloc_size_of_derive::MallocSizeOf;
use servo_arc::Arc as ServoArc;
use style::logical_geometry::WritingMode;
use style::properties::ComputedValues;
use style::properties::longhands::align_items::computed_value::T as AlignItems;
use style::properties::longhands::flex_direction::computed_value::T as FlexDirection;
use style::properties::longhands::flex_wrap::computed_value::T as FlexWrap;
use style::values::computed::{AlignContent, JustifyContent};
use style::values::specified::align::AlignFlags;
use crate::PropagatedBoxTreeData;
use crate::cell::ArcRefCell;
use crate::construct_modern::{ModernContainerBuilder, ModernItemKind};
use crate::context::LayoutContext;
use crate::dom::{LayoutBox, NodeExt};
use crate::dom_traversal::{NodeAndStyleInfo, NonReplacedContents};
use crate::formatting_contexts::IndependentFormattingContext;
use crate::fragment_tree::{BaseFragmentInfo, Fragment};
use crate::positioned::AbsolutelyPositionedBox;
mod geom;
mod layout;
/// A structure to hold the configuration of a flex container for use during layout
/// and preferred width calculation.
#[derive(Clone, Debug, MallocSizeOf)]
pub(crate) struct FlexContainerConfig {
container_is_single_line: bool,
writing_mode: WritingMode,
flex_axis: FlexAxis,
flex_direction: FlexDirection,
flex_direction_is_reversed: bool,
flex_wrap: FlexWrap,
flex_wrap_is_reversed: bool,
main_start_cross_start_sides_are: MainStartCrossStart,
align_content: AlignContent,
align_items: AlignItems,
justify_content: JustifyContent,
}
impl FlexContainerConfig {
fn new(container_style: &ComputedValues) -> FlexContainerConfig {
let flex_direction = container_style.clone_flex_direction();
let flex_axis = FlexAxis::from(flex_direction);
let flex_wrap = container_style.get_position().flex_wrap;
let container_is_single_line = match flex_wrap {
FlexWrap::Nowrap => true,
FlexWrap::Wrap | FlexWrap::WrapReverse => false,
};
let flex_direction_is_reversed = match flex_direction {
FlexDirection::Row | FlexDirection::Column => false,
FlexDirection::RowReverse | FlexDirection::ColumnReverse => true,
};
let flex_wrap_reverse = match flex_wrap {
FlexWrap::Nowrap | FlexWrap::Wrap => false,
FlexWrap::WrapReverse => true,
};
let align_content = container_style.clone_align_content();
let align_items = AlignItems(match container_style.clone_align_items().0 {
AlignFlags::AUTO | AlignFlags::NORMAL => AlignFlags::STRETCH,
align => align,
});
let justify_content = container_style.clone_justify_content();
let main_start_cross_start_sides_are =
MainStartCrossStart::from(flex_direction, flex_wrap_reverse);
FlexContainerConfig {
container_is_single_line,
writing_mode: container_style.writing_mode,
flex_axis,
flex_direction,
flex_direction_is_reversed,
flex_wrap,
flex_wrap_is_reversed: flex_wrap_reverse,
main_start_cross_start_sides_are,
align_content,
align_items,
justify_content,
}
}
}
#[derive(Debug, MallocSizeOf)]
pub(crate) struct FlexContainer {
children: Vec<ArcRefCell<FlexLevelBox>>,
#[conditional_malloc_size_of]
style: ServoArc<ComputedValues>,
/// The configuration of this [`FlexContainer`].
config: FlexContainerConfig,
}
impl FlexContainer {
pub fn construct<'dom>(
context: &LayoutContext,
info: &NodeAndStyleInfo<impl NodeExt<'dom>>,
contents: NonReplacedContents,
propagated_data: PropagatedBoxTreeData,
) -> Self {
let mut builder =
ModernContainerBuilder::new(context, info, propagated_data.union(&info.style));
contents.traverse(context, info, &mut builder);
let items = builder.finish();
let children = items
.into_iter()
.map(|item| {
let box_ = match item.kind {
ModernItemKind::InFlow => ArcRefCell::new(FlexLevelBox::FlexItem(
FlexItemBox::new(item.formatting_context),
)),
ModernItemKind::OutOfFlow => {
let abs_pos_box =
ArcRefCell::new(AbsolutelyPositionedBox::new(item.formatting_context));
ArcRefCell::new(FlexLevelBox::OutOfFlowAbsolutelyPositionedBox(abs_pos_box))
},
};
if let Some(box_slot) = item.box_slot {
box_slot.set(LayoutBox::FlexLevel(box_.clone()));
}
box_
})
.collect();
Self {
children,
style: info.style.clone(),
config: FlexContainerConfig::new(&info.style),
}
}
}
#[derive(Debug, MallocSizeOf)]
pub(crate) enum FlexLevelBox {
FlexItem(FlexItemBox),
OutOfFlowAbsolutelyPositionedBox(ArcRefCell<AbsolutelyPositionedBox>),
}
impl FlexLevelBox {
pub(crate) fn invalidate_cached_fragment(&self) {
match self {
FlexLevelBox::FlexItem(flex_item_box) => flex_item_box
.independent_formatting_context
.base
.invalidate_cached_fragment(),
FlexLevelBox::OutOfFlowAbsolutelyPositionedBox(positioned_box) => positioned_box
.borrow()
.context
.base
.invalidate_cached_fragment(),
}
}
pub(crate) fn fragments(&self) -> Vec<Fragment> {
match self {
FlexLevelBox::FlexItem(flex_item_box) => flex_item_box
.independent_formatting_context
.base
.fragments(),
FlexLevelBox::OutOfFlowAbsolutelyPositionedBox(positioned_box) => {
positioned_box.borrow().context.base.fragments()
},
}
}
}
#[derive(MallocSizeOf)]
pub(crate) struct FlexItemBox {
independent_formatting_context: IndependentFormattingContext,
}
impl std::fmt::Debug for FlexItemBox {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("FlexItemBox")
}
}
impl FlexItemBox {
fn new(independent_formatting_context: IndependentFormattingContext) -> Self {
Self {
independent_formatting_context,
}
}
fn style(&self) -> &ServoArc<ComputedValues> {
self.independent_formatting_context.style()
}
fn base_fragment_info(&self) -> BaseFragmentInfo {
self.independent_formatting_context.base_fragment_info()
}
}

View file

@ -0,0 +1,762 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 std::borrow::Cow;
use std::convert::TryFrom;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use servo_arc::Arc;
use style::properties::ComputedValues;
use style::properties::longhands::list_style_position::computed_value::T as ListStylePosition;
use style::selector_parser::PseudoElement;
use style::str::char_is_whitespace;
use super::OutsideMarker;
use super::inline::InlineFormattingContext;
use super::inline::construct::InlineFormattingContextBuilder;
use super::inline::inline_box::InlineBox;
use crate::PropagatedBoxTreeData;
use crate::cell::ArcRefCell;
use crate::context::LayoutContext;
use crate::dom::{BoxSlot, LayoutBox, NodeExt};
use crate::dom_traversal::{
Contents, NodeAndStyleInfo, NonReplacedContents, PseudoElementContentItem, TraversalHandler,
};
use crate::flow::float::FloatBox;
use crate::flow::{BlockContainer, BlockFormattingContext, BlockLevelBox};
use crate::formatting_contexts::IndependentFormattingContext;
use crate::fragment_tree::FragmentFlags;
use crate::layout_box_base::LayoutBoxBase;
use crate::positioned::AbsolutelyPositionedBox;
use crate::style_ext::{ComputedValuesExt, DisplayGeneratingBox, DisplayInside, DisplayOutside};
use crate::table::{AnonymousTableContent, Table};
impl BlockFormattingContext {
pub(crate) fn construct<'dom, Node>(
context: &LayoutContext,
info: &NodeAndStyleInfo<Node>,
contents: NonReplacedContents,
propagated_data: PropagatedBoxTreeData,
is_list_item: bool,
) -> Self
where
Node: NodeExt<'dom>,
{
Self::from_block_container(BlockContainer::construct(
context,
info,
contents,
propagated_data,
is_list_item,
))
}
pub(crate) fn from_block_container(contents: BlockContainer) -> Self {
let contains_floats = contents.contains_floats();
Self {
contents,
contains_floats,
}
}
}
struct BlockLevelJob<'dom, Node> {
info: NodeAndStyleInfo<Node>,
box_slot: BoxSlot<'dom>,
propagated_data: PropagatedBoxTreeData,
kind: BlockLevelCreator,
}
enum BlockLevelCreator {
SameFormattingContextBlock(IntermediateBlockContainer),
Independent {
display_inside: DisplayInside,
contents: Contents,
},
OutOfFlowAbsolutelyPositionedBox {
display_inside: DisplayInside,
contents: Contents,
},
OutOfFlowFloatBox {
display_inside: DisplayInside,
contents: Contents,
},
OutsideMarker {
list_item_style: Arc<ComputedValues>,
contents: Vec<PseudoElementContentItem>,
},
AnonymousTable {
table_block: ArcRefCell<BlockLevelBox>,
},
}
/// A block container that may still have to be constructed.
///
/// Represents either the inline formatting context of an anonymous block
/// box or the yet-to-be-computed block container generated from the children
/// of a given element.
///
/// Deferring allows using rayons `into_par_iter`.
enum IntermediateBlockContainer {
InlineFormattingContext(BlockContainer),
Deferred {
contents: NonReplacedContents,
propagated_data: PropagatedBoxTreeData,
is_list_item: bool,
},
}
/// A builder for a block container.
///
/// This builder starts from the first child of a given DOM node
/// and does a preorder traversal of all of its inclusive siblings.
pub(crate) struct BlockContainerBuilder<'dom, 'style, Node> {
context: &'style LayoutContext<'style>,
/// This NodeAndStyleInfo contains the root node, the corresponding pseudo
/// content designator, and the block container style.
info: &'style NodeAndStyleInfo<Node>,
/// The list of block-level boxes to be built for the final block container.
///
/// Contains all the block-level jobs we found traversing the tree
/// so far, if this is empty at the end of the traversal and the ongoing
/// inline formatting context is not empty, the block container establishes
/// an inline formatting context (see end of `build`).
///
/// DOM nodes which represent block-level boxes are immediately pushed
/// to this list with their style without ever being traversed at this
/// point, instead we just move to their next sibling. If the DOM node
/// doesn't have a next sibling, we either reached the end of the container
/// root or there are ongoing inline-level boxes
/// (see `handle_block_level_element`).
block_level_boxes: Vec<BlockLevelJob<'dom, Node>>,
/// Whether or not this builder has yet produced a block which would be
/// be considered the first line for the purposes of `text-indent`.
have_already_seen_first_line_for_text_indent: bool,
/// The propagated data to use for BoxTree construction.
propagated_data: PropagatedBoxTreeData,
inline_formatting_context_builder: InlineFormattingContextBuilder,
/// The [`NodeAndStyleInfo`] to use for anonymous block boxes pushed to the list of
/// block-level boxes, lazily initialized (see `end_ongoing_inline_formatting_context`).
anonymous_box_info: Option<NodeAndStyleInfo<Node>>,
/// A collection of content that is being added to an anonymous table. This is
/// composed of any sequence of internal table elements or table captions that
/// are found outside of a table.
anonymous_table_content: Vec<AnonymousTableContent<'dom, Node>>,
}
impl BlockContainer {
pub fn construct<'dom, Node>(
context: &LayoutContext,
info: &NodeAndStyleInfo<Node>,
contents: NonReplacedContents,
propagated_data: PropagatedBoxTreeData,
is_list_item: bool,
) -> BlockContainer
where
Node: NodeExt<'dom>,
{
let mut builder = BlockContainerBuilder::new(context, info, propagated_data);
if is_list_item {
if let Some((marker_info, marker_contents)) = crate::lists::make_marker(context, info) {
match marker_info.style.clone_list_style_position() {
ListStylePosition::Inside => {
builder.handle_list_item_marker_inside(&marker_info, info, marker_contents)
},
ListStylePosition::Outside => builder.handle_list_item_marker_outside(
&marker_info,
info,
marker_contents,
info.style.clone(),
),
}
}
}
contents.traverse(context, info, &mut builder);
builder.finish()
}
}
impl<'dom, 'style, Node> BlockContainerBuilder<'dom, 'style, Node>
where
Node: NodeExt<'dom>,
{
pub(crate) fn new(
context: &'style LayoutContext,
info: &'style NodeAndStyleInfo<Node>,
propagated_data: PropagatedBoxTreeData,
) -> Self {
BlockContainerBuilder {
context,
info,
block_level_boxes: Vec::new(),
propagated_data: propagated_data.union(&info.style),
have_already_seen_first_line_for_text_indent: false,
anonymous_box_info: None,
anonymous_table_content: Vec::new(),
inline_formatting_context_builder: InlineFormattingContextBuilder::new(),
}
}
pub(crate) fn finish(mut self) -> BlockContainer {
debug_assert!(
!self
.inline_formatting_context_builder
.currently_processing_inline_box()
);
self.finish_anonymous_table_if_needed();
if let Some(inline_formatting_context) = self.inline_formatting_context_builder.finish(
self.context,
self.propagated_data,
!self.have_already_seen_first_line_for_text_indent,
self.info.is_single_line_text_input(),
self.info.style.writing_mode.to_bidi_level(),
) {
// There are two options here. This block was composed of both one or more inline formatting contexts
// and child blocks OR this block was a single inline formatting context. In the latter case, we
// just return the inline formatting context as the block itself.
if self.block_level_boxes.is_empty() {
return BlockContainer::InlineFormattingContext(inline_formatting_context);
}
self.push_block_level_job_for_inline_formatting_context(inline_formatting_context);
}
let context = self.context;
let block_level_boxes = if self.context.use_rayon {
self.block_level_boxes
.into_par_iter()
.map(|block_level_job| block_level_job.finish(context))
.collect()
} else {
self.block_level_boxes
.into_iter()
.map(|block_level_job| block_level_job.finish(context))
.collect()
};
BlockContainer::BlockLevelBoxes(block_level_boxes)
}
fn finish_anonymous_table_if_needed(&mut self) {
if self.anonymous_table_content.is_empty() {
return;
}
// From https://drafts.csswg.org/css-tables/#fixup-algorithm:
// > If the boxs parent is an inline, run-in, or ruby box (or any box that would perform
// > inlinification of its children), then an inline-table box must be generated; otherwise
// > it must be a table box.
//
// Note that text content in the inline formatting context isn't enough to force the
// creation of an inline table. It requires the parent to be an inline box.
let inline_table = self
.inline_formatting_context_builder
.currently_processing_inline_box();
// Text decorations are not propagated to atomic inline-level descendants.
// From https://drafts.csswg.org/css2/#lining-striking-props:
// > Note that text decorations are not propagated to floating and absolutely
// > positioned descendants, nor to the contents of atomic inline-level descendants
// > such as inline blocks and inline tables.
let propagated_data = match inline_table {
true => self.propagated_data.without_text_decorations(),
false => self.propagated_data,
};
let contents: Vec<AnonymousTableContent<'dom, Node>> =
self.anonymous_table_content.drain(..).collect();
let last_text = match contents.last() {
Some(AnonymousTableContent::Text(info, text)) => Some((info.clone(), text.clone())),
_ => None,
};
let (table_info, ifc) =
Table::construct_anonymous(self.context, self.info, contents, propagated_data);
if inline_table {
self.inline_formatting_context_builder.push_atomic(ifc);
} else {
let table_block = ArcRefCell::new(BlockLevelBox::Independent(ifc));
self.end_ongoing_inline_formatting_context();
self.block_level_boxes.push(BlockLevelJob {
info: table_info,
box_slot: BoxSlot::dummy(),
kind: BlockLevelCreator::AnonymousTable { table_block },
propagated_data,
});
}
// If the last element in the anonymous table content is whitespace, that
// whitespace doesn't actually belong to the table. It should be processed outside
// ie become a space between the anonymous table and the rest of the block
// content. Anonymous tables are really only constructed around internal table
// elements and the whitespace between them, so this trailing whitespace should
// not be included.
//
// See https://drafts.csswg.org/css-tables/#fixup-algorithm sections "Remove
// irrelevant boxes" and "Generate missing parents."
if let Some((info, text)) = last_text {
self.handle_text(&info, text);
}
}
}
impl<'dom, Node> TraversalHandler<'dom, Node> for BlockContainerBuilder<'dom, '_, Node>
where
Node: NodeExt<'dom>,
{
fn handle_element(
&mut self,
info: &NodeAndStyleInfo<Node>,
display: DisplayGeneratingBox,
contents: Contents,
box_slot: BoxSlot<'dom>,
) {
match display {
DisplayGeneratingBox::OutsideInside { outside, inside } => {
self.finish_anonymous_table_if_needed();
match outside {
DisplayOutside::Inline => {
self.handle_inline_level_element(info, inside, contents, box_slot)
},
DisplayOutside::Block => {
let box_style = info.style.get_box();
// Floats and abspos cause blockification, so they only happen in this case.
// https://drafts.csswg.org/css2/visuren.html#dis-pos-flo
if box_style.position.is_absolutely_positioned() {
self.handle_absolutely_positioned_element(
info, inside, contents, box_slot,
)
} else if box_style.float.is_floating() {
self.handle_float_element(info, inside, contents, box_slot)
} else {
self.handle_block_level_element(info, inside, contents, box_slot)
}
},
};
},
DisplayGeneratingBox::LayoutInternal(_) => {
self.anonymous_table_content
.push(AnonymousTableContent::Element {
info: info.clone(),
display,
contents,
box_slot,
});
},
}
}
fn handle_text(&mut self, info: &NodeAndStyleInfo<Node>, text: Cow<'dom, str>) {
if text.is_empty() {
return;
}
// If we are building an anonymous table ie this text directly followed internal
// table elements that did not have a `<table>` ancestor, then we forward all
// whitespace to the table builder.
if !self.anonymous_table_content.is_empty() && text.chars().all(char_is_whitespace) {
self.anonymous_table_content
.push(AnonymousTableContent::Text(info.clone(), text));
return;
} else {
self.finish_anonymous_table_if_needed();
}
self.inline_formatting_context_builder.push_text(text, info);
}
}
impl<'dom, Node> BlockContainerBuilder<'dom, '_, Node>
where
Node: NodeExt<'dom>,
{
fn handle_list_item_marker_inside(
&mut self,
marker_info: &NodeAndStyleInfo<Node>,
container_info: &NodeAndStyleInfo<Node>,
contents: Vec<crate::dom_traversal::PseudoElementContentItem>,
) {
// TODO: We do not currently support saving box slots for ::marker pseudo-elements
// that are part nested in ::before and ::after pseudo elements. For now, just
// forget about them once they are built.
let box_slot = match container_info.pseudo_element_type {
Some(_) => BoxSlot::dummy(),
None => marker_info
.node
.pseudo_element_box_slot(PseudoElement::Marker),
};
self.handle_inline_level_element(
marker_info,
DisplayInside::Flow {
is_list_item: false,
},
NonReplacedContents::OfPseudoElement(contents).into(),
box_slot,
);
}
fn handle_list_item_marker_outside(
&mut self,
marker_info: &NodeAndStyleInfo<Node>,
container_info: &NodeAndStyleInfo<Node>,
contents: Vec<crate::dom_traversal::PseudoElementContentItem>,
list_item_style: Arc<ComputedValues>,
) {
// TODO: We do not currently support saving box slots for ::marker pseudo-elements
// that are part nested in ::before and ::after pseudo elements. For now, just
// forget about them once they are built.
let box_slot = match container_info.pseudo_element_type {
Some(_) => BoxSlot::dummy(),
None => marker_info
.node
.pseudo_element_box_slot(PseudoElement::Marker),
};
self.block_level_boxes.push(BlockLevelJob {
info: marker_info.clone(),
box_slot,
kind: BlockLevelCreator::OutsideMarker {
contents,
list_item_style,
},
propagated_data: self.propagated_data.without_text_decorations(),
});
}
fn handle_inline_level_element(
&mut self,
info: &NodeAndStyleInfo<Node>,
display_inside: DisplayInside,
contents: Contents,
box_slot: BoxSlot<'dom>,
) {
let (DisplayInside::Flow { is_list_item }, false) =
(display_inside, contents.is_replaced())
else {
// If this inline element is an atomic, handle it and return.
let atomic = self.inline_formatting_context_builder.push_atomic(
IndependentFormattingContext::construct(
self.context,
info,
display_inside,
contents,
// Text decorations are not propagated to atomic inline-level descendants.
self.propagated_data.without_text_decorations(),
),
);
box_slot.set(LayoutBox::InlineLevel(atomic));
return;
};
// Otherwise, this is just a normal inline box. Whatever happened before, all we need to do
// before recurring is to remember this ongoing inline level box.
let inline_item = self
.inline_formatting_context_builder
.start_inline_box(InlineBox::new(info));
if is_list_item {
if let Some((marker_info, marker_contents)) =
crate::lists::make_marker(self.context, info)
{
// Ignore `list-style-position` here:
// “If the list item is an inline box: this value is equivalent to `inside`.”
// https://drafts.csswg.org/css-lists/#list-style-position-outside
self.handle_list_item_marker_inside(&marker_info, info, marker_contents)
}
}
// `unwrap` doesnt panic here because `is_replaced` returned `false`.
NonReplacedContents::try_from(contents)
.unwrap()
.traverse(self.context, info, self);
self.finish_anonymous_table_if_needed();
self.inline_formatting_context_builder.end_inline_box();
box_slot.set(LayoutBox::InlineLevel(inline_item));
}
fn handle_block_level_element(
&mut self,
info: &NodeAndStyleInfo<Node>,
display_inside: DisplayInside,
contents: Contents,
box_slot: BoxSlot<'dom>,
) {
// We just found a block level element, all ongoing inline level boxes
// need to be split around it.
//
// After calling `split_around_block_and_finish`,
// `self.inline_formatting_context_builder` is set up with the state
// that we want to have after we push the block below.
if let Some(inline_formatting_context) = self
.inline_formatting_context_builder
.split_around_block_and_finish(
self.context,
self.propagated_data,
!self.have_already_seen_first_line_for_text_indent,
self.info.style.writing_mode.to_bidi_level(),
)
{
self.push_block_level_job_for_inline_formatting_context(inline_formatting_context);
}
let propagated_data = self.propagated_data;
let kind = match contents {
Contents::NonReplaced(contents) => match display_inside {
DisplayInside::Flow { is_list_item }
// Fragment flags are just used to indicate that the element is not replaced, so empty
// flags are okay here.
if !info.style.establishes_block_formatting_context(
FragmentFlags::empty()
) =>
{
BlockLevelCreator::SameFormattingContextBlock(
IntermediateBlockContainer::Deferred {
contents,
propagated_data,
is_list_item,
},
)
},
_ => BlockLevelCreator::Independent {
display_inside,
contents: contents.into(),
},
},
Contents::Replaced(contents) => {
let contents = Contents::Replaced(contents);
BlockLevelCreator::Independent {
display_inside,
contents,
}
},
};
self.block_level_boxes.push(BlockLevelJob {
info: info.clone(),
box_slot,
kind,
propagated_data,
});
// Any block also counts as the first line for the purposes of text indent. Even if
// they don't actually indent.
self.have_already_seen_first_line_for_text_indent = true;
}
fn handle_absolutely_positioned_element(
&mut self,
info: &NodeAndStyleInfo<Node>,
display_inside: DisplayInside,
contents: Contents,
box_slot: BoxSlot<'dom>,
) {
if !self.inline_formatting_context_builder.is_empty() {
let inline_level_box = self
.inline_formatting_context_builder
.push_absolutely_positioned_box(AbsolutelyPositionedBox::construct(
self.context,
info,
display_inside,
contents,
));
box_slot.set(LayoutBox::InlineLevel(inline_level_box));
return;
}
let kind = BlockLevelCreator::OutOfFlowAbsolutelyPositionedBox {
contents,
display_inside,
};
self.block_level_boxes.push(BlockLevelJob {
info: info.clone(),
box_slot,
kind,
propagated_data: self.propagated_data.without_text_decorations(),
});
}
fn handle_float_element(
&mut self,
info: &NodeAndStyleInfo<Node>,
display_inside: DisplayInside,
contents: Contents,
box_slot: BoxSlot<'dom>,
) {
if !self.inline_formatting_context_builder.is_empty() {
let inline_level_box =
self.inline_formatting_context_builder
.push_float_box(FloatBox::construct(
self.context,
info,
display_inside,
contents,
self.propagated_data,
));
box_slot.set(LayoutBox::InlineLevel(inline_level_box));
return;
}
let kind = BlockLevelCreator::OutOfFlowFloatBox {
contents,
display_inside,
};
self.block_level_boxes.push(BlockLevelJob {
info: info.clone(),
box_slot,
kind,
propagated_data: self.propagated_data.without_text_decorations(),
});
}
fn end_ongoing_inline_formatting_context(&mut self) {
if let Some(inline_formatting_context) = self.inline_formatting_context_builder.finish(
self.context,
self.propagated_data,
!self.have_already_seen_first_line_for_text_indent,
self.info.is_single_line_text_input(),
self.info.style.writing_mode.to_bidi_level(),
) {
self.push_block_level_job_for_inline_formatting_context(inline_formatting_context);
}
}
fn push_block_level_job_for_inline_formatting_context(
&mut self,
inline_formatting_context: InlineFormattingContext,
) {
let layout_context = self.context;
let info = self
.anonymous_box_info
.get_or_insert_with(|| {
self.info
.pseudo(layout_context, PseudoElement::ServoAnonymousBox)
.expect("Should never fail to create anonymous box")
})
.clone();
self.block_level_boxes.push(BlockLevelJob {
info,
// FIXME(nox): We should be storing this somewhere.
box_slot: BoxSlot::dummy(),
kind: BlockLevelCreator::SameFormattingContextBlock(
IntermediateBlockContainer::InlineFormattingContext(
BlockContainer::InlineFormattingContext(inline_formatting_context),
),
),
propagated_data: self.propagated_data,
});
self.have_already_seen_first_line_for_text_indent = true;
}
}
impl<'dom, Node> BlockLevelJob<'dom, Node>
where
Node: NodeExt<'dom>,
{
fn finish(self, context: &LayoutContext) -> ArcRefCell<BlockLevelBox> {
let info = &self.info;
let block_level_box = match self.kind {
BlockLevelCreator::SameFormattingContextBlock(intermediate_block_container) => {
let contents = intermediate_block_container.finish(context, info);
let contains_floats = contents.contains_floats();
ArcRefCell::new(BlockLevelBox::SameFormattingContextBlock {
base: LayoutBoxBase::new(info.into(), info.style.clone()),
contents,
contains_floats,
})
},
BlockLevelCreator::Independent {
display_inside,
contents,
} => {
let context = IndependentFormattingContext::construct(
context,
info,
display_inside,
contents,
self.propagated_data,
);
ArcRefCell::new(BlockLevelBox::Independent(context))
},
BlockLevelCreator::OutOfFlowAbsolutelyPositionedBox {
display_inside,
contents,
} => ArcRefCell::new(BlockLevelBox::OutOfFlowAbsolutelyPositionedBox(
ArcRefCell::new(AbsolutelyPositionedBox::construct(
context,
info,
display_inside,
contents,
)),
)),
BlockLevelCreator::OutOfFlowFloatBox {
display_inside,
contents,
} => ArcRefCell::new(BlockLevelBox::OutOfFlowFloatBox(FloatBox::construct(
context,
info,
display_inside,
contents,
self.propagated_data,
))),
BlockLevelCreator::OutsideMarker {
contents,
list_item_style,
} => {
let contents = NonReplacedContents::OfPseudoElement(contents);
let block_container = BlockContainer::construct(
context,
info,
contents,
self.propagated_data.without_text_decorations(),
false, /* is_list_item */
);
ArcRefCell::new(BlockLevelBox::OutsideMarker(OutsideMarker {
base: LayoutBoxBase::new(info.into(), info.style.clone()),
block_container,
list_item_style,
}))
},
BlockLevelCreator::AnonymousTable { table_block } => table_block,
};
self.box_slot
.set(LayoutBox::BlockLevel(block_level_box.clone()));
block_level_box
}
}
impl IntermediateBlockContainer {
fn finish<'dom, Node>(
self,
context: &LayoutContext,
info: &NodeAndStyleInfo<Node>,
) -> BlockContainer
where
Node: NodeExt<'dom>,
{
match self {
IntermediateBlockContainer::Deferred {
contents,
propagated_data,
is_list_item,
} => BlockContainer::construct(context, info, contents, propagated_data, is_list_item),
IntermediateBlockContainer::InlineFormattingContext(block_container) => block_container,
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,636 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 std::borrow::Cow;
use std::char::{ToLowercase, ToUppercase};
use icu_segmenter::WordSegmenter;
use servo_arc::Arc;
use style::computed_values::white_space_collapse::T as WhiteSpaceCollapse;
use style::values::specified::text::TextTransformCase;
use unicode_bidi::Level;
use super::text_run::TextRun;
use super::{InlineBox, InlineBoxIdentifier, InlineBoxes, InlineFormattingContext, InlineItem};
use crate::PropagatedBoxTreeData;
use crate::cell::ArcRefCell;
use crate::context::LayoutContext;
use crate::dom::NodeExt;
use crate::dom_traversal::NodeAndStyleInfo;
use crate::flow::float::FloatBox;
use crate::formatting_contexts::IndependentFormattingContext;
use crate::positioned::AbsolutelyPositionedBox;
use crate::style_ext::ComputedValuesExt;
#[derive(Default)]
pub(crate) struct InlineFormattingContextBuilder {
/// The collection of text strings that make up this [`InlineFormattingContext`] under
/// construction.
pub text_segments: Vec<String>,
/// The current offset in the final text string of this [`InlineFormattingContext`],
/// used to properly set the text range of new [`InlineItem::TextRun`]s.
current_text_offset: usize,
/// Whether the last processed node ended with whitespace. This is used to
/// implement rule 4 of <https://www.w3.org/TR/css-text-3/#collapse>:
///
/// > Any collapsible space immediately following another collapsible space—even one
/// > outside the boundary of the inline containing that space, provided both spaces are
/// > within the same inline formatting context—is collapsed to have zero advance width.
/// > (It is invisible, but retains its soft wrap opportunity, if any.)
last_inline_box_ended_with_collapsible_white_space: bool,
/// Whether or not the current state of the inline formatting context is on a word boundary
/// for the purposes of `text-transform: capitalize`.
on_word_boundary: bool,
/// Whether or not this inline formatting context will contain floats.
pub contains_floats: bool,
/// The current list of [`InlineItem`]s in this [`InlineFormattingContext`] under
/// construction. This is stored in a flat list to make it easy to access the last
/// item.
pub inline_items: Vec<ArcRefCell<InlineItem>>,
/// The current [`InlineBox`] tree of this [`InlineFormattingContext`] under construction.
pub inline_boxes: InlineBoxes,
/// The ongoing stack of inline boxes stack of the builder.
///
/// Contains all the currently ongoing inline boxes we entered so far.
/// The traversal is at all times as deep in the tree as this stack is,
/// which is why the code doesn't need to keep track of the actual
/// container root (see `handle_inline_level_element`).
///
/// When an inline box ends, it's removed from this stack.
inline_box_stack: Vec<InlineBoxIdentifier>,
/// Whether or not the inline formatting context under construction has any
/// uncollapsible text content.
pub has_uncollapsible_text_content: bool,
}
impl InlineFormattingContextBuilder {
pub(crate) fn new() -> Self {
// For the purposes of `text-transform: capitalize` the start of the IFC is a word boundary.
Self {
on_word_boundary: true,
..Default::default()
}
}
pub(crate) fn currently_processing_inline_box(&self) -> bool {
!self.inline_box_stack.is_empty()
}
fn push_control_character_string(&mut self, string_to_push: &str) {
self.text_segments.push(string_to_push.to_owned());
self.current_text_offset += string_to_push.len();
}
/// Return true if this [`InlineFormattingContextBuilder`] is empty for the purposes of ignoring
/// during box tree construction. An IFC is empty if it only contains TextRuns with
/// completely collapsible whitespace. When that happens it can be ignored completely.
pub(crate) fn is_empty(&self) -> bool {
if self.has_uncollapsible_text_content {
return false;
}
if !self.inline_box_stack.is_empty() {
return false;
}
fn inline_level_box_is_empty(inline_level_box: &InlineItem) -> bool {
match inline_level_box {
InlineItem::StartInlineBox(_) => false,
InlineItem::EndInlineBox => false,
// Text content is handled by `self.has_uncollapsible_text` content above in order
// to avoid having to iterate through the character once again.
InlineItem::TextRun(_) => true,
InlineItem::OutOfFlowAbsolutelyPositionedBox(..) => false,
InlineItem::OutOfFlowFloatBox(_) => false,
InlineItem::Atomic(..) => false,
}
}
self.inline_items
.iter()
.all(|inline_level_box| inline_level_box_is_empty(&inline_level_box.borrow()))
}
pub(crate) fn push_atomic(
&mut self,
independent_formatting_context: IndependentFormattingContext,
) -> ArcRefCell<InlineItem> {
let inline_level_box = ArcRefCell::new(InlineItem::Atomic(
Arc::new(independent_formatting_context),
self.current_text_offset,
Level::ltr(), /* This will be assigned later if necessary. */
));
self.inline_items.push(inline_level_box.clone());
// Push an object replacement character for this atomic, which will ensure that the line breaker
// inserts a line breaking opportunity here.
self.push_control_character_string("\u{fffc}");
self.last_inline_box_ended_with_collapsible_white_space = false;
self.on_word_boundary = true;
inline_level_box
}
pub(crate) fn push_absolutely_positioned_box(
&mut self,
absolutely_positioned_box: AbsolutelyPositionedBox,
) -> ArcRefCell<InlineItem> {
let absolutely_positioned_box = ArcRefCell::new(absolutely_positioned_box);
let inline_level_box = ArcRefCell::new(InlineItem::OutOfFlowAbsolutelyPositionedBox(
absolutely_positioned_box,
self.current_text_offset,
));
self.inline_items.push(inline_level_box.clone());
inline_level_box
}
pub(crate) fn push_float_box(&mut self, float_box: FloatBox) -> ArcRefCell<InlineItem> {
let inline_level_box = ArcRefCell::new(InlineItem::OutOfFlowFloatBox(Arc::new(float_box)));
self.inline_items.push(inline_level_box.clone());
self.contains_floats = true;
inline_level_box
}
pub(crate) fn start_inline_box(&mut self, inline_box: InlineBox) -> ArcRefCell<InlineItem> {
self.push_control_character_string(inline_box.base.style.bidi_control_chars().0);
let (identifier, inline_box) = self.inline_boxes.start_inline_box(inline_box);
let inline_level_box = ArcRefCell::new(InlineItem::StartInlineBox(inline_box));
self.inline_items.push(inline_level_box.clone());
self.inline_box_stack.push(identifier);
inline_level_box
}
pub(crate) fn end_inline_box(&mut self) -> ArcRefCell<InlineBox> {
let identifier = self.end_inline_box_internal();
let inline_level_box = self.inline_boxes.get(&identifier);
inline_level_box.borrow_mut().is_last_fragment = true;
self.push_control_character_string(
inline_level_box.borrow().base.style.bidi_control_chars().1,
);
inline_level_box
}
fn end_inline_box_internal(&mut self) -> InlineBoxIdentifier {
let identifier = self
.inline_box_stack
.pop()
.expect("Ended non-existent inline box");
self.inline_items
.push(ArcRefCell::new(InlineItem::EndInlineBox));
self.inline_boxes.end_inline_box(identifier);
identifier
}
pub(crate) fn push_text<'dom, Node: NodeExt<'dom>>(
&mut self,
text: Cow<'dom, str>,
info: &NodeAndStyleInfo<Node>,
) {
let white_space_collapse = info.style.clone_white_space_collapse();
let collapsed = WhitespaceCollapse::new(
text.chars(),
white_space_collapse,
self.last_inline_box_ended_with_collapsible_white_space,
);
// TODO: Not all text transforms are about case, this logic should stop ignoring
// TextTransform::FULL_WIDTH and TextTransform::FULL_SIZE_KANA.
let text_transform = info.style.clone_text_transform().case();
let capitalized_text: String;
let char_iterator: Box<dyn Iterator<Item = char>> = match text_transform {
TextTransformCase::None => Box::new(collapsed),
TextTransformCase::Capitalize => {
// `TextTransformation` doesn't support capitalization, so we must capitalize the whole
// string at once and make a copy. Here `on_word_boundary` indicates whether or not the
// inline formatting context as a whole is on a word boundary. This is different from
// `last_inline_box_ended_with_collapsible_white_space` because the word boundaries are
// between atomic inlines and at the start of the IFC, and because preserved spaces
// are a word boundary.
let collapsed_string: String = collapsed.collect();
capitalized_text = capitalize_string(&collapsed_string, self.on_word_boundary);
Box::new(capitalized_text.chars())
},
_ => {
// If `text-transform` is active, wrap the `WhitespaceCollapse` iterator in
// a `TextTransformation` iterator.
Box::new(TextTransformation::new(collapsed, text_transform))
},
};
let white_space_collapse = info.style.clone_white_space_collapse();
let new_text: String = char_iterator
.inspect(|&character| {
self.has_uncollapsible_text_content |= matches!(
white_space_collapse,
WhiteSpaceCollapse::Preserve | WhiteSpaceCollapse::BreakSpaces
) || !character.is_ascii_whitespace() ||
(character == '\n' && white_space_collapse != WhiteSpaceCollapse::Collapse);
})
.collect();
if new_text.is_empty() {
return;
}
let selection_range = info.get_selection_range();
let selected_style = info.get_selected_style();
if let Some(last_character) = new_text.chars().next_back() {
self.on_word_boundary = last_character.is_whitespace();
self.last_inline_box_ended_with_collapsible_white_space =
self.on_word_boundary && white_space_collapse != WhiteSpaceCollapse::Preserve;
}
let new_range = self.current_text_offset..self.current_text_offset + new_text.len();
self.current_text_offset = new_range.end;
self.text_segments.push(new_text);
if let Some(inline_item) = self.inline_items.last() {
if let InlineItem::TextRun(text_run) = &mut *inline_item.borrow_mut() {
text_run.borrow_mut().text_range.end = new_range.end;
return;
}
}
self.inline_items
.push(ArcRefCell::new(InlineItem::TextRun(ArcRefCell::new(
TextRun::new(
info.into(),
info.style.clone(),
new_range,
selection_range,
selected_style,
),
))));
}
pub(crate) fn split_around_block_and_finish(
&mut self,
layout_context: &LayoutContext,
propagated_data: PropagatedBoxTreeData,
has_first_formatted_line: bool,
default_bidi_level: Level,
) -> Option<InlineFormattingContext> {
if self.is_empty() {
return None;
}
// Create a new inline builder which will be active after the block splits this inline formatting
// context. It has the same inline box structure as this builder, except the boxes are
// marked as not being the first fragment. No inline content is carried over to this new
// builder.
let mut new_builder = InlineFormattingContextBuilder::new();
for identifier in self.inline_box_stack.iter() {
new_builder.start_inline_box(
self.inline_boxes
.get(identifier)
.borrow()
.split_around_block(),
);
}
let mut inline_builder_from_before_split = std::mem::replace(self, new_builder);
// End all ongoing inline boxes in the first builder, but ensure that they are not
// marked as the final fragments, so that they do not get inline end margin, borders,
// and padding.
while !inline_builder_from_before_split.inline_box_stack.is_empty() {
inline_builder_from_before_split.end_inline_box_internal();
}
inline_builder_from_before_split.finish(
layout_context,
propagated_data,
has_first_formatted_line,
/* is_single_line_text_input = */ false,
default_bidi_level,
)
}
/// Finish the current inline formatting context, returning [`None`] if the context was empty.
pub(crate) fn finish(
&mut self,
layout_context: &LayoutContext,
propagated_data: PropagatedBoxTreeData,
has_first_formatted_line: bool,
is_single_line_text_input: bool,
default_bidi_level: Level,
) -> Option<InlineFormattingContext> {
if self.is_empty() {
return None;
}
let old_builder = std::mem::replace(self, InlineFormattingContextBuilder::new());
assert!(old_builder.inline_box_stack.is_empty());
Some(InlineFormattingContext::new_with_builder(
old_builder,
layout_context,
propagated_data,
has_first_formatted_line,
is_single_line_text_input,
default_bidi_level,
))
}
}
fn preserve_segment_break() -> bool {
true
}
pub struct WhitespaceCollapse<InputIterator> {
char_iterator: InputIterator,
white_space_collapse: WhiteSpaceCollapse,
/// Whether or not we should collapse white space completely at the start of the string.
/// This is true when the last character handled in our owning [`super::InlineFormattingContext`]
/// was collapsible white space.
remove_collapsible_white_space_at_start: bool,
/// Whether or not the last character produced was newline. There is special behavior
/// we do after each newline.
following_newline: bool,
/// Whether or not we have seen any non-white space characters, indicating that we are not
/// in a collapsible white space section at the beginning of the string.
have_seen_non_white_space_characters: bool,
/// Whether the last character that we processed was a non-newline white space character. When
/// collapsing white space we need to wait until the next non-white space character or the end
/// of the string to push a single white space.
inside_white_space: bool,
/// When we enter a collapsible white space region, we may need to wait to produce a single
/// white space character as soon as we encounter a non-white space character. When that
/// happens we queue up the non-white space character for the next iterator call.
character_pending_to_return: Option<char>,
}
impl<InputIterator> WhitespaceCollapse<InputIterator> {
pub fn new(
char_iterator: InputIterator,
white_space_collapse: WhiteSpaceCollapse,
trim_beginning_white_space: bool,
) -> Self {
Self {
char_iterator,
white_space_collapse,
remove_collapsible_white_space_at_start: trim_beginning_white_space,
inside_white_space: false,
following_newline: false,
have_seen_non_white_space_characters: false,
character_pending_to_return: None,
}
}
fn is_leading_trimmed_white_space(&self) -> bool {
!self.have_seen_non_white_space_characters && self.remove_collapsible_white_space_at_start
}
/// Whether or not we need to produce a space character if the next character is not a newline
/// and not white space. This happens when we are exiting a section of white space and we
/// waited to produce a single space character for the entire section of white space (but
/// not following or preceding a newline).
fn need_to_produce_space_character_after_white_space(&self) -> bool {
self.inside_white_space && !self.following_newline && !self.is_leading_trimmed_white_space()
}
}
impl<InputIterator> Iterator for WhitespaceCollapse<InputIterator>
where
InputIterator: Iterator<Item = char>,
{
type Item = char;
fn next(&mut self) -> Option<Self::Item> {
// Point 4.1.1 first bullet:
// > If white-space is set to normal, nowrap, or pre-line, whitespace
// > characters are considered collapsible
// If whitespace is not considered collapsible, it is preserved entirely, which
// means that we can simply return the input string exactly.
if self.white_space_collapse == WhiteSpaceCollapse::Preserve ||
self.white_space_collapse == WhiteSpaceCollapse::BreakSpaces
{
// From <https://drafts.csswg.org/css-text-3/#white-space-processing>:
// > Carriage returns (U+000D) are treated identically to spaces (U+0020) in all respects.
//
// In the non-preserved case these are converted to space below.
return match self.char_iterator.next() {
Some('\r') => Some(' '),
next => next,
};
}
if let Some(character) = self.character_pending_to_return.take() {
self.inside_white_space = false;
self.have_seen_non_white_space_characters = true;
self.following_newline = false;
return Some(character);
}
while let Some(character) = self.char_iterator.next() {
// Don't push non-newline whitespace immediately. Instead wait to push it until we
// know that it isn't followed by a newline. See `push_pending_whitespace_if_needed`
// above.
if character.is_ascii_whitespace() && character != '\n' {
self.inside_white_space = true;
continue;
}
// Point 4.1.1:
// > 2. Collapsible segment breaks are transformed for rendering according to the
// > segment break transformation rules.
if character == '\n' {
// From <https://drafts.csswg.org/css-text-3/#line-break-transform>
// (4.1.3 -- the segment break transformation rules):
//
// > When white-space is pre, pre-wrap, or pre-line, segment breaks are not
// > collapsible and are instead transformed into a preserved line feed"
if self.white_space_collapse != WhiteSpaceCollapse::Collapse {
self.inside_white_space = false;
self.following_newline = true;
return Some(character);
// Point 4.1.3:
// > 1. First, any collapsible segment break immediately following another
// > collapsible segment break is removed.
// > 2. Then any remaining segment break is either transformed into a space (U+0020)
// > or removed depending on the context before and after the break.
} else if !self.following_newline &&
preserve_segment_break() &&
!self.is_leading_trimmed_white_space()
{
self.inside_white_space = false;
self.following_newline = true;
return Some(' ');
} else {
self.following_newline = true;
continue;
}
}
// Point 4.1.1:
// > 2. Any sequence of collapsible spaces and tabs immediately preceding or
// > following a segment break is removed.
// > 3. Every collapsible tab is converted to a collapsible space (U+0020).
// > 4. Any collapsible space immediately following another collapsible space—even
// > one outside the boundary of the inline containing that space, provided both
// > spaces are within the same inline formatting context—is collapsed to have zero
// > advance width.
if self.need_to_produce_space_character_after_white_space() {
self.inside_white_space = false;
self.character_pending_to_return = Some(character);
return Some(' ');
}
self.inside_white_space = false;
self.have_seen_non_white_space_characters = true;
self.following_newline = false;
return Some(character);
}
if self.need_to_produce_space_character_after_white_space() {
self.inside_white_space = false;
return Some(' ');
}
None
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.char_iterator.size_hint()
}
fn count(self) -> usize
where
Self: Sized,
{
self.char_iterator.count()
}
}
enum PendingCaseConversionResult {
Uppercase(ToUppercase),
Lowercase(ToLowercase),
}
impl PendingCaseConversionResult {
fn next(&mut self) -> Option<char> {
match self {
PendingCaseConversionResult::Uppercase(to_uppercase) => to_uppercase.next(),
PendingCaseConversionResult::Lowercase(to_lowercase) => to_lowercase.next(),
}
}
}
/// This is an interator that consumes a char iterator and produces character transformed
/// by the given CSS `text-transform` value. It currently does not support
/// `text-transform: capitalize` because Unicode segmentation libraries do not support
/// streaming input one character at a time.
pub struct TextTransformation<InputIterator> {
/// The input character iterator.
char_iterator: InputIterator,
/// The `text-transform` value to use.
text_transform: TextTransformCase,
/// If an uppercasing or lowercasing produces more than one character, this
/// caches them so that they can be returned in subsequent iterator calls.
pending_case_conversion_result: Option<PendingCaseConversionResult>,
}
impl<InputIterator> TextTransformation<InputIterator> {
pub fn new(char_iterator: InputIterator, text_transform: TextTransformCase) -> Self {
Self {
char_iterator,
text_transform,
pending_case_conversion_result: None,
}
}
}
impl<InputIterator> Iterator for TextTransformation<InputIterator>
where
InputIterator: Iterator<Item = char>,
{
type Item = char;
fn next(&mut self) -> Option<Self::Item> {
if let Some(character) = self
.pending_case_conversion_result
.as_mut()
.and_then(|result| result.next())
{
return Some(character);
}
self.pending_case_conversion_result = None;
for character in self.char_iterator.by_ref() {
match self.text_transform {
TextTransformCase::None => return Some(character),
TextTransformCase::Uppercase => {
let mut pending_result =
PendingCaseConversionResult::Uppercase(character.to_uppercase());
if let Some(character) = pending_result.next() {
self.pending_case_conversion_result = Some(pending_result);
return Some(character);
}
},
TextTransformCase::Lowercase => {
let mut pending_result =
PendingCaseConversionResult::Lowercase(character.to_lowercase());
if let Some(character) = pending_result.next() {
self.pending_case_conversion_result = Some(pending_result);
return Some(character);
}
},
// `text-transform: capitalize` currently cannot work on a per-character basis,
// so must be handled outside of this iterator.
TextTransformCase::Capitalize => return Some(character),
}
}
None
}
}
/// Given a string and whether the start of the string represents a word boundary, create a copy of
/// the string with letters after word boundaries capitalized.
fn capitalize_string(string: &str, allow_word_at_start: bool) -> String {
let mut output_string = String::new();
output_string.reserve(string.len());
let word_segmenter = WordSegmenter::new_auto();
let mut bounds = word_segmenter.segment_str(string).peekable();
let mut byte_index = 0;
for character in string.chars() {
let current_byte_index = byte_index;
byte_index += character.len_utf8();
if let Some(next_index) = bounds.peek() {
if *next_index == current_byte_index {
bounds.next();
if current_byte_index != 0 || allow_word_at_start {
output_string.extend(character.to_uppercase());
continue;
}
}
}
output_string.push(character);
}
output_string
}

View file

@ -0,0 +1,257 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 std::vec::IntoIter;
use app_units::Au;
use fonts::FontMetrics;
use malloc_size_of_derive::MallocSizeOf;
use super::{InlineContainerState, InlineContainerStateFlags, inline_container_needs_strut};
use crate::ContainingBlock;
use crate::cell::ArcRefCell;
use crate::context::LayoutContext;
use crate::dom::NodeExt;
use crate::dom_traversal::NodeAndStyleInfo;
use crate::fragment_tree::BaseFragmentInfo;
use crate::layout_box_base::LayoutBoxBase;
use crate::style_ext::{LayoutStyle, PaddingBorderMargin};
#[derive(Debug, MallocSizeOf)]
pub(crate) struct InlineBox {
pub base: LayoutBoxBase,
/// The identifier of this inline box in the containing [`super::InlineFormattingContext`].
pub(super) identifier: InlineBoxIdentifier,
pub is_first_fragment: bool,
pub is_last_fragment: bool,
/// The index of the default font in the [`super::InlineFormattingContext`]'s font metrics store.
/// This is initialized during IFC shaping.
pub default_font_index: Option<usize>,
}
impl InlineBox {
pub(crate) fn new<'dom, Node: NodeExt<'dom>>(info: &NodeAndStyleInfo<Node>) -> Self {
Self {
base: LayoutBoxBase::new(info.into(), info.style.clone()),
// This will be assigned later, when the box is actually added to the IFC.
identifier: InlineBoxIdentifier::default(),
is_first_fragment: true,
is_last_fragment: false,
default_font_index: None,
}
}
pub(crate) fn split_around_block(&self) -> Self {
Self {
base: LayoutBoxBase::new(self.base.base_fragment_info, self.base.style.clone()),
is_first_fragment: false,
is_last_fragment: false,
..*self
}
}
#[inline]
pub(crate) fn layout_style(&self) -> LayoutStyle {
LayoutStyle::Default(&self.base.style)
}
}
#[derive(Debug, Default, MallocSizeOf)]
pub(crate) struct InlineBoxes {
/// A collection of all inline boxes in a particular [`super::InlineFormattingContext`].
inline_boxes: Vec<ArcRefCell<InlineBox>>,
/// A list of tokens that represent the actual tree of inline boxes, while allowing
/// easy traversal forward and backwards through the tree. This structure is also
/// stored in the [`super::InlineFormattingContext::inline_items`], but this version is
/// faster to iterate.
inline_box_tree: Vec<InlineBoxTreePathToken>,
}
impl InlineBoxes {
pub(super) fn len(&self) -> usize {
self.inline_boxes.len()
}
pub(super) fn iter(&self) -> impl Iterator<Item = &ArcRefCell<InlineBox>> {
self.inline_boxes.iter()
}
pub(super) fn get(&self, identifier: &InlineBoxIdentifier) -> ArcRefCell<InlineBox> {
self.inline_boxes[identifier.index_in_inline_boxes as usize].clone()
}
pub(super) fn end_inline_box(&mut self, identifier: InlineBoxIdentifier) {
self.inline_box_tree
.push(InlineBoxTreePathToken::End(identifier));
}
pub(super) fn start_inline_box(
&mut self,
mut inline_box: InlineBox,
) -> (InlineBoxIdentifier, ArcRefCell<InlineBox>) {
assert!(self.inline_boxes.len() <= u32::MAX as usize);
assert!(self.inline_box_tree.len() <= u32::MAX as usize);
let index_in_inline_boxes = self.inline_boxes.len() as u32;
let index_of_start_in_tree = self.inline_box_tree.len() as u32;
let identifier = InlineBoxIdentifier {
index_of_start_in_tree,
index_in_inline_boxes,
};
inline_box.identifier = identifier;
let inline_box = ArcRefCell::new(inline_box);
self.inline_boxes.push(inline_box.clone());
self.inline_box_tree
.push(InlineBoxTreePathToken::Start(identifier));
(identifier, inline_box)
}
pub(super) fn get_path(
&self,
from: Option<InlineBoxIdentifier>,
to: InlineBoxIdentifier,
) -> IntoIter<InlineBoxTreePathToken> {
if from == Some(to) {
return Vec::new().into_iter();
}
let mut from_index = match from {
Some(InlineBoxIdentifier {
index_of_start_in_tree,
..
}) => index_of_start_in_tree as usize,
None => 0,
};
let mut to_index = to.index_of_start_in_tree as usize;
let is_reversed = to_index < from_index;
// Do not include the first or final token, depending on direction. These can be equal
// if we are starting or going to the the root of the inline formatting context, in which
// case we don't want to adjust.
if to_index > from_index && from.is_some() {
from_index += 1;
} else if to_index < from_index {
to_index += 1;
}
let mut path = Vec::with_capacity(from_index.abs_diff(to_index));
let min = from_index.min(to_index);
let max = from_index.max(to_index);
for token in &self.inline_box_tree[min..=max] {
// Skip useless recursion into inline boxes; we are looking for a direct path.
if Some(&token.reverse()) == path.last() {
path.pop();
} else {
path.push(*token);
}
}
if is_reversed {
path.reverse();
for token in path.iter_mut() {
*token = token.reverse();
}
}
path.into_iter()
}
}
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq)]
pub(super) enum InlineBoxTreePathToken {
Start(InlineBoxIdentifier),
End(InlineBoxIdentifier),
}
impl InlineBoxTreePathToken {
fn reverse(&self) -> Self {
match self {
Self::Start(index) => Self::End(*index),
Self::End(index) => Self::Start(*index),
}
}
}
/// An identifier for a particular [`InlineBox`] to be used to fetch it from an [`InlineBoxes`]
/// store of inline boxes.
///
/// [`u32`] is used for the index, in order to save space. The value refers to the token
/// in the start tree data structure which can be fetched to find the actual index of
/// of the [`InlineBox`] in [`InlineBoxes::inline_boxes`].
#[derive(Clone, Copy, Debug, Default, Eq, Hash, MallocSizeOf, PartialEq)]
pub(crate) struct InlineBoxIdentifier {
pub index_of_start_in_tree: u32,
pub index_in_inline_boxes: u32,
}
pub(super) struct InlineBoxContainerState {
/// The container state common to both [`InlineBox`] and the root of the
/// [`super::InlineFormattingContext`].
pub base: InlineContainerState,
/// The [`InlineBoxIdentifier`] of this inline container state. If this is the root
/// the identifier is [`None`].
pub identifier: InlineBoxIdentifier,
/// The [`BaseFragmentInfo`] of the [`InlineBox`] that this state tracks.
pub base_fragment_info: BaseFragmentInfo,
/// The [`PaddingBorderMargin`] of the [`InlineBox`] that this state tracks.
pub pbm: PaddingBorderMargin,
/// Whether this is the last fragment of this InlineBox. This may not be the case if
/// the InlineBox is split due to an block-in-inline-split and this is not the last of
/// that split.
pub is_last_fragment: bool,
}
impl InlineBoxContainerState {
pub(super) fn new(
inline_box: &InlineBox,
containing_block: &ContainingBlock,
layout_context: &LayoutContext,
parent_container: &InlineContainerState,
is_last_fragment: bool,
font_metrics: Option<&FontMetrics>,
) -> Self {
let style = inline_box.base.style.clone();
let pbm = inline_box
.layout_style()
.padding_border_margin(containing_block);
let mut flags = InlineContainerStateFlags::empty();
if inline_container_needs_strut(&style, layout_context, Some(&pbm)) {
flags.insert(InlineContainerStateFlags::CREATE_STRUT);
}
Self {
base: InlineContainerState::new(
style,
flags,
Some(parent_container),
parent_container.text_decoration_line,
font_metrics,
),
identifier: inline_box.identifier,
base_fragment_info: inline_box.base.base_fragment_info,
pbm,
is_last_fragment,
}
}
pub(super) fn calculate_space_above_baseline(&self) -> Au {
let (ascent, descent, line_gap) = (
self.base.font_metrics.ascent,
self.base.font_metrics.descent,
self.base.font_metrics.line_gap,
);
let leading = line_gap - (ascent + descent);
leading.scale_by(0.5) + ascent
}
}

View file

@ -0,0 +1,911 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 app_units::Au;
use bitflags::bitflags;
use fonts::{ByteIndex, FontMetrics, GlyphStore};
use itertools::Either;
use range::Range;
use servo_arc::Arc;
use style::Zero;
use style::computed_values::position::T as Position;
use style::computed_values::white_space_collapse::T as WhiteSpaceCollapse;
use style::properties::ComputedValues;
use style::values::generics::box_::{GenericVerticalAlign, VerticalAlignKeyword};
use style::values::generics::font::LineHeight;
use style::values::specified::align::AlignFlags;
use style::values::specified::box_::DisplayOutside;
use style::values::specified::text::TextDecorationLine;
use unicode_bidi::{BidiInfo, Level};
use webrender_api::FontInstanceKey;
use super::inline_box::{InlineBoxContainerState, InlineBoxIdentifier, InlineBoxTreePathToken};
use super::{InlineFormattingContextLayout, LineBlockSizes};
use crate::cell::ArcRefCell;
use crate::fragment_tree::{BaseFragmentInfo, BoxFragment, Fragment, TextFragment};
use crate::geom::{LogicalRect, LogicalVec2, PhysicalRect, ToLogical};
use crate::positioned::{
AbsolutelyPositionedBox, PositioningContext, PositioningContextLength, relative_adjustement,
};
use crate::{ContainingBlock, ContainingBlockSize};
pub(super) struct LineMetrics {
/// The block offset of the line start in the containing
/// [`crate::flow::InlineFormattingContext`].
pub block_offset: Au,
/// The block size of this line.
pub block_size: Au,
/// The block offset of this line's baseline from [`Self::block_offset`].
pub baseline_block_offset: Au,
}
bitflags! {
struct LineLayoutInlineContainerFlags: u8 {
/// Whether or not any line items were processed for this inline box, this includes
/// any child inline boxes.
const HAD_ANY_LINE_ITEMS = 1 << 0;
/// Whether or not the starting inline border, padding, or margin of the inline box
/// was encountered.
const HAD_INLINE_START_PBM = 1 << 2;
/// Whether or not the ending inline border, padding, or margin of the inline box
/// was encountered.
const HAD_INLINE_END_PBM = 1 << 3;
/// Whether or not any floats were encountered while laying out this inline box.
const HAD_ANY_FLOATS = 1 << 4;
}
}
/// The state used when laying out a collection of [`LineItem`]s into a line. This state is stored
/// per-inline container. For instance, when laying out the conents of a `<span>` a fresh
/// [`LineItemLayoutInlineContainerState`] is pushed onto [`LineItemLayout`]'s stack of states.
pub(super) struct LineItemLayoutInlineContainerState {
/// If this inline container is not the root inline container, the identifier of the [`super::InlineBox`]
/// that is currently being laid out.
pub identifier: Option<InlineBoxIdentifier>,
/// The fragments and their logical rectangle relative within the current inline box (or
/// line). These logical rectangles will be converted into physical ones and the Fragment's
/// `content_rect` will be updated once the inline box's final size is known in
/// [`LineItemLayout::end_inline_box`].
pub fragments: Vec<(Fragment, LogicalRect<Au>)>,
/// The current inline advance of the layout in the coordinates of this inline box.
pub inline_advance: Au,
/// Flags which track various features during layout.
flags: LineLayoutInlineContainerFlags,
/// The offset of the parent, relative to the start position of the line, not including
/// any inline start and end borders which are only processed when the inline box is
/// finished.
pub parent_offset: LogicalVec2<Au>,
/// The block offset of the parent's baseline relative to the block start of the line. This
/// is often the same as [`Self::parent_offset`], but can be different for the root
/// element.
pub baseline_offset: Au,
/// If this inline box establishes a containing block for positioned elements, this
/// is a fresh positioning context to contain them. Otherwise, this holds the starting
/// offset in the *parent* positioning context so that static positions can be updated
/// at the end of layout.
pub positioning_context_or_start_offset_in_parent:
Either<PositioningContext, PositioningContextLength>,
}
impl LineItemLayoutInlineContainerState {
fn new(
identifier: Option<InlineBoxIdentifier>,
parent_offset: LogicalVec2<Au>,
baseline_offset: Au,
positioning_context_or_start_offset_in_parent: Either<
PositioningContext,
PositioningContextLength,
>,
) -> Self {
Self {
identifier,
fragments: Vec::new(),
inline_advance: Au::zero(),
flags: LineLayoutInlineContainerFlags::empty(),
parent_offset,
baseline_offset,
positioning_context_or_start_offset_in_parent,
}
}
fn root(starting_inline_advance: Au, baseline_offset: Au) -> Self {
let mut state = Self::new(
None,
LogicalVec2::zero(),
baseline_offset,
Either::Right(PositioningContextLength::zero()),
);
state.inline_advance = starting_inline_advance;
state
}
}
/// The second phase of [`super::InlineFormattingContext`] layout: once items are gathered
/// for a line, we must lay them out and create fragments for them, properly positioning them
/// according to their baselines and also handling absolutely positioned children.
pub(super) struct LineItemLayout<'layout_data, 'layout> {
/// The state of the overall [`super::InlineFormattingContext`] layout.
layout: &'layout mut InlineFormattingContextLayout<'layout_data>,
/// The set of [`LineItemLayoutInlineContainerState`] created while laying out items
/// on this line. This does not include the current level of recursion.
pub state_stack: Vec<LineItemLayoutInlineContainerState>,
/// The current [`LineItemLayoutInlineContainerState`].
pub current_state: LineItemLayoutInlineContainerState,
/// The metrics of this line, which should remain constant throughout the
/// layout process.
pub line_metrics: LineMetrics,
/// The amount of space to add to each justification opportunity in order to implement
/// `text-align: justify`.
pub justification_adjustment: Au,
}
impl LineItemLayout<'_, '_> {
pub(super) fn layout_line_items(
layout: &mut InlineFormattingContextLayout,
line_items: Vec<LineItem>,
start_position: LogicalVec2<Au>,
effective_block_advance: &LineBlockSizes,
justification_adjustment: Au,
) -> Vec<Fragment> {
let baseline_offset = effective_block_advance.find_baseline_offset();
LineItemLayout {
layout,
state_stack: Vec::new(),
current_state: LineItemLayoutInlineContainerState::root(
start_position.inline,
baseline_offset,
),
line_metrics: LineMetrics {
block_offset: start_position.block,
block_size: effective_block_advance.resolve(),
baseline_block_offset: baseline_offset,
},
justification_adjustment,
}
.layout(line_items)
}
/// Start and end inline boxes in tree order, so that it reflects the given inline box.
fn prepare_layout_for_inline_box(&mut self, new_inline_box: Option<InlineBoxIdentifier>) {
// Optimize the case where we are moving to the root of the inline box stack.
let Some(new_inline_box) = new_inline_box else {
while !self.state_stack.is_empty() {
self.end_inline_box();
}
return;
};
// Otherwise, follow the path given to us by our collection of inline boxes, so we know which
// inline boxes to start and end.
let path = self
.layout
.ifc
.inline_boxes
.get_path(self.current_state.identifier, new_inline_box);
for token in path {
match token {
InlineBoxTreePathToken::Start(ref identifier) => self.start_inline_box(identifier),
InlineBoxTreePathToken::End(_) => self.end_inline_box(),
}
}
}
pub(super) fn layout(&mut self, mut line_items: Vec<LineItem>) -> Vec<Fragment> {
let mut last_level = Level::ltr();
let levels: Vec<_> = line_items
.iter()
.map(|item| {
let level = match item {
LineItem::TextRun(_, text_run) => text_run.bidi_level,
// TODO: This level needs either to be last_level, or if there were
// unicode characters inserted for the inline box, we need to get the
// level from them.
LineItem::InlineStartBoxPaddingBorderMargin(_) => last_level,
LineItem::InlineEndBoxPaddingBorderMargin(_) => last_level,
LineItem::Atomic(_, atomic) => atomic.bidi_level,
LineItem::AbsolutelyPositioned(..) => last_level,
LineItem::Float(..) => {
// At this point the float is already positioned, so it doesn't really matter what
// position it's fragment has in the order of line items.
last_level
},
};
last_level = level;
level
})
.collect();
if self.layout.ifc.has_right_to_left_content {
sort_by_indices_in_place(&mut line_items, BidiInfo::reorder_visual(&levels));
}
// `BidiInfo::reorder_visual` will reorder the contents of the line so that they
// are in the correct order as if one was looking at the line from left-to-right.
// During this layout we do not lay out from left to right. Instead we lay out
// from inline-start to inline-end. If the overall line contents have been flipped
// for BiDi, flip them again so that they are in line start-to-end order rather
// than left-to-right order.
let line_item_iterator = if self
.layout
.containing_block
.style
.writing_mode
.is_bidi_ltr()
{
Either::Left(line_items.into_iter())
} else {
Either::Right(line_items.into_iter().rev())
};
for item in line_item_iterator.into_iter().by_ref() {
// When preparing to lay out a new line item, start and end inline boxes, so that the current
// inline box state reflects the item's parent. Items in the line are not necessarily in tree
// order due to BiDi and other reordering so the inline box of the item could potentially be
// any in the inline formatting context.
self.prepare_layout_for_inline_box(item.inline_box_identifier());
self.current_state
.flags
.insert(LineLayoutInlineContainerFlags::HAD_ANY_LINE_ITEMS);
match item {
LineItem::InlineStartBoxPaddingBorderMargin(_) => {
self.current_state
.flags
.insert(LineLayoutInlineContainerFlags::HAD_INLINE_START_PBM);
},
LineItem::InlineEndBoxPaddingBorderMargin(_) => {
self.current_state
.flags
.insert(LineLayoutInlineContainerFlags::HAD_INLINE_END_PBM);
},
LineItem::TextRun(_, text_run) => self.layout_text_run(text_run),
LineItem::Atomic(_, atomic) => self.layout_atomic(atomic),
LineItem::AbsolutelyPositioned(_, absolute) => self.layout_absolute(absolute),
LineItem::Float(_, float) => self.layout_float(float),
}
}
// Move back to the root of the inline box tree, so that all boxes are ended.
self.prepare_layout_for_inline_box(None);
let fragments_and_rectangles = std::mem::take(&mut self.current_state.fragments);
fragments_and_rectangles
.into_iter()
.map(|(mut fragment, logical_rect)| {
if matches!(fragment, Fragment::Float(_)) {
return fragment;
}
// We do not know the actual physical position of a logically laid out inline element, until
// we know the width of the containing inline block. This step converts the logical rectangle
// into a physical one based on the inline formatting context width.
fragment.mutate_content_rect(|content_rect| {
*content_rect = logical_rect.as_physical(Some(self.layout.containing_block))
});
fragment
})
.collect()
}
fn current_positioning_context_mut(&mut self) -> &mut PositioningContext {
if let Either::Left(ref mut positioning_context) = self
.current_state
.positioning_context_or_start_offset_in_parent
{
return positioning_context;
}
self.state_stack
.iter_mut()
.rev()
.find_map(
|state| match state.positioning_context_or_start_offset_in_parent {
Either::Left(ref mut positioning_context) => Some(positioning_context),
Either::Right(_) => None,
},
)
.unwrap_or(self.layout.positioning_context)
}
fn start_inline_box(&mut self, identifier: &InlineBoxIdentifier) {
let inline_box_state =
&*self.layout.inline_box_states[identifier.index_in_inline_boxes as usize];
let inline_box = self.layout.ifc.inline_boxes.get(identifier);
let inline_box = &*(inline_box.borrow());
let style = &inline_box.base.style;
let space_above_baseline = inline_box_state.calculate_space_above_baseline();
let block_start_offset =
self.calculate_inline_box_block_start(inline_box_state, space_above_baseline);
let positioning_context_or_start_offset_in_parent =
match PositioningContext::new_for_style(style) {
Some(positioning_context) => Either::Left(positioning_context),
None => Either::Right(self.current_positioning_context_mut().len()),
};
let parent_offset = LogicalVec2 {
inline: self.current_state.inline_advance + self.current_state.parent_offset.inline,
block: block_start_offset,
};
let outer_state = std::mem::replace(
&mut self.current_state,
LineItemLayoutInlineContainerState::new(
Some(*identifier),
parent_offset,
block_start_offset + space_above_baseline,
positioning_context_or_start_offset_in_parent,
),
);
self.state_stack.push(outer_state);
}
fn end_inline_box(&mut self) {
let outer_state = self.state_stack.pop().expect("Ended unknown inline box");
let inner_state = std::mem::replace(&mut self.current_state, outer_state);
let identifier = inner_state.identifier.expect("Ended unknown inline box");
let inline_box_state =
&*self.layout.inline_box_states[identifier.index_in_inline_boxes as usize];
let inline_box = self.layout.ifc.inline_boxes.get(&identifier);
let inline_box = &*(inline_box.borrow());
let mut padding = inline_box_state.pbm.padding;
let mut border = inline_box_state.pbm.border;
let mut margin = inline_box_state.pbm.margin.auto_is(Au::zero);
let mut had_start = inner_state
.flags
.contains(LineLayoutInlineContainerFlags::HAD_INLINE_START_PBM);
let mut had_end = inner_state
.flags
.contains(LineLayoutInlineContainerFlags::HAD_INLINE_END_PBM);
let containing_block_writing_mode = self.layout.containing_block.style.writing_mode;
if containing_block_writing_mode.is_bidi_ltr() !=
inline_box.base.style.writing_mode.is_bidi_ltr()
{
std::mem::swap(&mut had_start, &mut had_end)
}
if !had_start {
padding.inline_start = Au::zero();
border.inline_start = Au::zero();
margin.inline_start = Au::zero();
}
if !had_end {
padding.inline_end = Au::zero();
border.inline_end = Au::zero();
margin.inline_end = Au::zero();
}
// If the inline box didn't have any content at all and it isn't the first fragment for
// an element (needed for layout queries currently) and it didn't have any padding, border,
// or margin do not make a fragment for it.
//
// Note: This is an optimization, but also has side effects. Any fragments on a line will
// force the baseline to advance in the parent IFC.
let pbm_sums = padding + border + margin;
if inner_state.fragments.is_empty() && !had_start && pbm_sums.inline_sum().is_zero() {
return;
}
// Make `content_rect` relative to the parent Fragment.
let mut content_rect = LogicalRect {
start_corner: LogicalVec2 {
inline: self.current_state.inline_advance + pbm_sums.inline_start,
block: inner_state.parent_offset.block - self.current_state.parent_offset.block,
},
size: LogicalVec2 {
inline: inner_state.inline_advance,
block: inline_box_state.base.font_metrics.line_gap,
},
};
// Relative adjustment should not affect the rest of line layout, so we can
// do it right before creating the Fragment.
let style = &inline_box.base.style;
if style.get_box().position == Position::Relative {
content_rect.start_corner += relative_adjustement(style, self.layout.containing_block);
}
let ifc_writing_mode = self.layout.containing_block.style.writing_mode;
let inline_box_containing_block = ContainingBlock {
size: ContainingBlockSize {
inline: content_rect.size.inline,
block: Default::default(),
},
style: self.layout.containing_block.style,
};
let fragments = inner_state
.fragments
.into_iter()
.map(|(mut fragment, logical_rect)| {
let is_float = matches!(fragment, Fragment::Float(_));
fragment.mutate_content_rect(|content_rect| {
if is_float {
content_rect.origin -=
pbm_sums.start_offset().to_physical_size(ifc_writing_mode);
} else {
// We do not know the actual physical position of a logically laid out inline element, until
// we know the width of the containing inline block. This step converts the logical rectangle
// into a physical one now that we've computed inline size of the containing inline block above.
*content_rect = logical_rect.as_physical(Some(&inline_box_containing_block))
}
});
fragment
})
.collect();
// Previously all the fragment's children were positioned relative to the linebox,
// but they need to be made relative to this fragment.
let physical_content_rect = content_rect.as_physical(Some(self.layout.containing_block));
let mut fragment = BoxFragment::new(
inline_box.base.base_fragment_info,
style.clone(),
fragments,
physical_content_rect,
padding.to_physical(ifc_writing_mode),
border.to_physical(ifc_writing_mode),
margin.to_physical(ifc_writing_mode),
None, /* clearance */
);
let offset_from_parent_ifc = LogicalVec2 {
inline: pbm_sums.inline_start + self.current_state.inline_advance,
block: content_rect.start_corner.block,
}
.to_physical_vector(self.layout.containing_block.style.writing_mode);
match inner_state.positioning_context_or_start_offset_in_parent {
Either::Left(mut positioning_context) => {
positioning_context
.layout_collected_children(self.layout.layout_context, &mut fragment);
positioning_context.adjust_static_position_of_hoisted_fragments_with_offset(
&offset_from_parent_ifc,
PositioningContextLength::zero(),
);
self.current_positioning_context_mut()
.append(positioning_context);
},
Either::Right(start_offset) => {
self.current_positioning_context_mut()
.adjust_static_position_of_hoisted_fragments_with_offset(
&offset_from_parent_ifc,
start_offset,
);
},
}
self.current_state.inline_advance += inner_state.inline_advance + pbm_sums.inline_sum();
let fragment = Fragment::Box(ArcRefCell::new(fragment));
inline_box.base.add_fragment(fragment.clone());
self.current_state.fragments.push((fragment, content_rect));
}
fn calculate_inline_box_block_start(
&self,
inline_box_state: &InlineBoxContainerState,
space_above_baseline: Au,
) -> Au {
let font_metrics = &inline_box_state.base.font_metrics;
let style = &inline_box_state.base.style;
let line_gap = font_metrics.line_gap;
// The baseline offset that we have in `Self::baseline_offset` is relative to the line
// baseline, so we need to make it relative to the line block start.
match inline_box_state.base.style.clone_vertical_align() {
GenericVerticalAlign::Keyword(VerticalAlignKeyword::Top) => {
let line_height: Au = line_height(style, font_metrics);
(line_height - line_gap).scale_by(0.5)
},
GenericVerticalAlign::Keyword(VerticalAlignKeyword::Bottom) => {
let line_height: Au = line_height(style, font_metrics);
let half_leading = (line_height - line_gap).scale_by(0.5);
self.line_metrics.block_size - line_height + half_leading
},
_ => {
self.line_metrics.baseline_block_offset + inline_box_state.base.baseline_offset -
space_above_baseline
},
}
}
fn layout_text_run(&mut self, text_item: TextRunLineItem) {
if text_item.text.is_empty() {
return;
}
let mut number_of_justification_opportunities = 0;
let mut inline_advance = text_item
.text
.iter()
.map(|glyph_store| {
number_of_justification_opportunities += glyph_store.total_word_separators();
glyph_store.total_advance()
})
.sum();
if !self.justification_adjustment.is_zero() {
inline_advance += self
.justification_adjustment
.scale_by(number_of_justification_opportunities as f32);
}
// The block start of the TextRun is often zero (meaning it has the same font metrics as the
// inline box's strut), but for children of the inline formatting context root or for
// fallback fonts that use baseline relative alignment, it might be different.
let start_corner = LogicalVec2 {
inline: self.current_state.inline_advance,
block: self.current_state.baseline_offset -
text_item.font_metrics.ascent -
self.current_state.parent_offset.block,
};
let content_rect = LogicalRect {
start_corner,
size: LogicalVec2 {
block: text_item.font_metrics.line_gap,
inline: inline_advance,
},
};
self.current_state.inline_advance += inline_advance;
self.current_state.fragments.push((
Fragment::Text(ArcRefCell::new(TextFragment {
base: text_item.base_fragment_info.into(),
parent_style: text_item.parent_style,
rect: PhysicalRect::zero(),
font_metrics: text_item.font_metrics,
font_key: text_item.font_key,
glyphs: text_item.text,
text_decoration_line: text_item.text_decoration_line,
justification_adjustment: self.justification_adjustment,
selection_range: text_item.selection_range,
selected_style: text_item.selected_style,
})),
content_rect,
));
}
fn layout_atomic(&mut self, atomic: AtomicLineItem) {
// The initial `start_corner` of the Fragment is only the PaddingBorderMargin sum start
// offset, which is the sum of the start component of the padding, border, and margin.
// This needs to be added to the calculated block and inline positions.
// Make the final result relative to the parent box.
let ifc_writing_mode = self.layout.containing_block.style.writing_mode;
let content_rect = {
let block_start = atomic.calculate_block_start(&self.line_metrics);
let atomic_fragment = atomic.fragment.borrow_mut();
let padding_border_margin_sides = atomic_fragment
.padding_border_margin()
.to_logical(ifc_writing_mode);
let mut atomic_offset = LogicalVec2 {
inline: self.current_state.inline_advance +
padding_border_margin_sides.inline_start,
block: block_start - self.current_state.parent_offset.block +
padding_border_margin_sides.block_start,
};
if atomic_fragment.style.get_box().position == Position::Relative {
atomic_offset +=
relative_adjustement(&atomic_fragment.style, self.layout.containing_block);
}
// Reconstruct a logical rectangle relative to the inline box container that will be used
// after the inline box is procesed to find a final physical rectangle.
LogicalRect {
start_corner: atomic_offset,
size: atomic_fragment
.content_rect
.size
.to_logical(ifc_writing_mode),
}
};
if let Some(mut positioning_context) = atomic.positioning_context {
let physical_rect_as_if_in_root =
content_rect.as_physical(Some(self.layout.containing_block));
positioning_context.adjust_static_position_of_hoisted_fragments_with_offset(
&physical_rect_as_if_in_root.origin.to_vector(),
PositioningContextLength::zero(),
);
self.current_positioning_context_mut()
.append(positioning_context);
}
self.current_state.inline_advance += atomic.size.inline;
self.current_state
.fragments
.push((Fragment::Box(atomic.fragment), content_rect));
}
fn layout_absolute(&mut self, absolute: AbsolutelyPositionedLineItem) {
let absolutely_positioned_box = (*absolute.absolutely_positioned_box).borrow();
let style = absolutely_positioned_box.context.style();
// From https://drafts.csswg.org/css2/#abs-non-replaced-width
// > The static-position containing block is the containing block of a
// > hypothetical box that would have been the first box of the element if its
// > specified position value had been static and its specified float had been
// > none. (Note that due to the rules in section 9.7 this hypothetical
// > calculation might require also assuming a different computed value for
// > display.)
//
// This box is different based on the original `display` value of the
// absolutely positioned element. If it's `inline` it would be placed inline
// at the top of the line, but if it's block it would be placed in a new
// block position after the linebox established by this line.
let initial_start_corner =
if style.get_box().original_display.outside() == DisplayOutside::Inline {
// Top of the line at the current inline position.
LogicalVec2 {
inline: self.current_state.inline_advance,
block: -self.current_state.parent_offset.block,
}
} else {
// After the bottom of the line at the start of the inline formatting context.
LogicalVec2 {
inline: -self.current_state.parent_offset.inline,
block: self.line_metrics.block_size - self.current_state.parent_offset.block,
}
};
// Since alignment of absolutes in inlines is currently always `start`, the size of
// of the static position rectangle does not matter.
let static_position_rect = LogicalRect {
start_corner: initial_start_corner,
size: LogicalVec2::zero(),
}
.as_physical(Some(self.layout.containing_block));
let hoisted_box = AbsolutelyPositionedBox::to_hoisted(
absolute.absolutely_positioned_box.clone(),
static_position_rect,
LogicalVec2 {
inline: AlignFlags::START,
block: AlignFlags::START,
},
self.layout.containing_block.style.writing_mode,
);
let hoisted_fragment = hoisted_box.fragment.clone();
self.current_positioning_context_mut().push(hoisted_box);
self.current_state.fragments.push((
Fragment::AbsoluteOrFixedPositioned(hoisted_fragment),
LogicalRect::zero(),
));
}
fn layout_float(&mut self, float: FloatLineItem) {
self.current_state
.flags
.insert(LineLayoutInlineContainerFlags::HAD_ANY_FLOATS);
// The `BoxFragment` for this float is positioned relative to the IFC, so we need
// to move it to be positioned relative to our parent InlineBox line item. Float
// fragments are children of these InlineBoxes and not children of the inline
// formatting context, so that they are parented properly for StackingContext
// properties such as opacity & filters.
let distance_from_parent_to_ifc = LogicalVec2 {
inline: self.current_state.parent_offset.inline,
block: self.line_metrics.block_offset + self.current_state.parent_offset.block,
};
float.fragment.borrow_mut().content_rect.origin -= distance_from_parent_to_ifc
.to_physical_size(self.layout.containing_block.style.writing_mode);
self.current_state
.fragments
.push((Fragment::Float(float.fragment), LogicalRect::zero()));
}
}
pub(super) enum LineItem {
InlineStartBoxPaddingBorderMargin(InlineBoxIdentifier),
InlineEndBoxPaddingBorderMargin(InlineBoxIdentifier),
TextRun(Option<InlineBoxIdentifier>, TextRunLineItem),
Atomic(Option<InlineBoxIdentifier>, AtomicLineItem),
AbsolutelyPositioned(Option<InlineBoxIdentifier>, AbsolutelyPositionedLineItem),
Float(Option<InlineBoxIdentifier>, FloatLineItem),
}
impl LineItem {
fn inline_box_identifier(&self) -> Option<InlineBoxIdentifier> {
match self {
LineItem::InlineStartBoxPaddingBorderMargin(identifier) => Some(*identifier),
LineItem::InlineEndBoxPaddingBorderMargin(identifier) => Some(*identifier),
LineItem::TextRun(identifier, _) => *identifier,
LineItem::Atomic(identifier, _) => *identifier,
LineItem::AbsolutelyPositioned(identifier, _) => *identifier,
LineItem::Float(identifier, _) => *identifier,
}
}
pub(super) fn trim_whitespace_at_end(&mut self, whitespace_trimmed: &mut Au) -> bool {
match self {
LineItem::InlineStartBoxPaddingBorderMargin(_) => true,
LineItem::InlineEndBoxPaddingBorderMargin(_) => true,
LineItem::TextRun(_, item) => item.trim_whitespace_at_end(whitespace_trimmed),
LineItem::Atomic(..) => false,
LineItem::AbsolutelyPositioned(..) => true,
LineItem::Float(..) => true,
}
}
pub(super) fn trim_whitespace_at_start(&mut self, whitespace_trimmed: &mut Au) -> bool {
match self {
LineItem::InlineStartBoxPaddingBorderMargin(_) => true,
LineItem::InlineEndBoxPaddingBorderMargin(_) => true,
LineItem::TextRun(_, item) => item.trim_whitespace_at_start(whitespace_trimmed),
LineItem::Atomic(..) => false,
LineItem::AbsolutelyPositioned(..) => true,
LineItem::Float(..) => true,
}
}
}
pub(super) struct TextRunLineItem {
pub base_fragment_info: BaseFragmentInfo,
pub parent_style: Arc<ComputedValues>,
pub text: Vec<std::sync::Arc<GlyphStore>>,
pub font_metrics: FontMetrics,
pub font_key: FontInstanceKey,
pub text_decoration_line: TextDecorationLine,
/// The BiDi level of this [`TextRunLineItem`] to enable reordering.
pub bidi_level: Level,
pub selection_range: Option<Range<ByteIndex>>,
pub selected_style: Arc<ComputedValues>,
}
impl TextRunLineItem {
fn trim_whitespace_at_end(&mut self, whitespace_trimmed: &mut Au) -> bool {
if matches!(
self.parent_style.get_inherited_text().white_space_collapse,
WhiteSpaceCollapse::Preserve | WhiteSpaceCollapse::BreakSpaces
) {
return false;
}
let index_of_last_non_whitespace = self
.text
.iter()
.rev()
.position(|glyph| !glyph.is_whitespace())
.map(|offset_from_end| self.text.len() - offset_from_end);
let first_whitespace_index = index_of_last_non_whitespace.unwrap_or(0);
*whitespace_trimmed += self
.text
.drain(first_whitespace_index..)
.map(|glyph| glyph.total_advance())
.sum();
// Only keep going if we only encountered whitespace.
index_of_last_non_whitespace.is_none()
}
fn trim_whitespace_at_start(&mut self, whitespace_trimmed: &mut Au) -> bool {
if matches!(
self.parent_style.get_inherited_text().white_space_collapse,
WhiteSpaceCollapse::Preserve | WhiteSpaceCollapse::BreakSpaces
) {
return false;
}
let index_of_first_non_whitespace = self
.text
.iter()
.position(|glyph| !glyph.is_whitespace())
.unwrap_or(self.text.len());
*whitespace_trimmed += self
.text
.drain(0..index_of_first_non_whitespace)
.map(|glyph| glyph.total_advance())
.sum();
// Only keep going if we only encountered whitespace.
self.text.is_empty()
}
pub(crate) fn can_merge(&self, font_key: FontInstanceKey, bidi_level: Level) -> bool {
self.font_key == font_key && self.bidi_level == bidi_level
}
}
pub(super) struct AtomicLineItem {
pub fragment: ArcRefCell<BoxFragment>,
pub size: LogicalVec2<Au>,
pub positioning_context: Option<PositioningContext>,
/// The block offset of this items' baseline relative to the baseline of the line.
/// This will be zero for boxes with `vertical-align: top` and `vertical-align:
/// bottom` since their baselines are calculated late in layout.
pub baseline_offset_in_parent: Au,
/// The offset of the baseline inside this item.
pub baseline_offset_in_item: Au,
/// The BiDi level of this [`AtomicLineItem`] to enable reordering.
pub bidi_level: Level,
}
impl AtomicLineItem {
/// Given the metrics for a line, our vertical alignment, and our block size, find a block start
/// position relative to the top of the line.
fn calculate_block_start(&self, line_metrics: &LineMetrics) -> Au {
match self.fragment.borrow().style.clone_vertical_align() {
GenericVerticalAlign::Keyword(VerticalAlignKeyword::Top) => Au::zero(),
GenericVerticalAlign::Keyword(VerticalAlignKeyword::Bottom) => {
line_metrics.block_size - self.size.block
},
// This covers all baseline-relative vertical alignment.
_ => {
let baseline = line_metrics.baseline_block_offset + self.baseline_offset_in_parent;
baseline - self.baseline_offset_in_item
},
}
}
}
pub(super) struct AbsolutelyPositionedLineItem {
pub absolutely_positioned_box: ArcRefCell<AbsolutelyPositionedBox>,
}
pub(super) struct FloatLineItem {
pub fragment: ArcRefCell<BoxFragment>,
/// Whether or not this float Fragment has been placed yet. Fragments that
/// do not fit on a line need to be placed after the hypothetical block start
/// of the next line.
pub needs_placement: bool,
}
fn line_height(parent_style: &ComputedValues, font_metrics: &FontMetrics) -> Au {
let font = parent_style.get_font();
let font_size = font.font_size.computed_size();
match font.line_height {
LineHeight::Normal => font_metrics.line_gap,
LineHeight::Number(number) => (font_size * number.0).into(),
LineHeight::Length(length) => length.0.into(),
}
}
/// Sort a mutable slice by the the given indices array in place, reording the slice so that final
/// value of `slice[x]` is `slice[indices[x]]`.
fn sort_by_indices_in_place<T>(data: &mut [T], mut indices: Vec<usize>) {
for idx in 0..data.len() {
if indices[idx] == idx {
continue;
}
let mut current_idx = idx;
loop {
let target_idx = indices[current_idx];
indices[current_idx] = current_idx;
if indices[target_idx] == target_idx {
break;
}
data.swap(current_idx, target_idx);
current_idx = target_idx;
}
}
}

View file

@ -0,0 +1,120 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 std::ops::Range;
use icu_segmenter::LineSegmenter;
pub(crate) struct LineBreaker {
linebreaks: Vec<usize>,
current_offset: usize,
}
impl LineBreaker {
pub(crate) fn new(string: &str) -> Self {
let line_segmenter = LineSegmenter::new_auto();
Self {
// From https://docs.rs/icu_segmenter/1.5.0/icu_segmenter/struct.LineSegmenter.html
// > For consistency with the grapheme, word, and sentence segmenters, there is always a
// > breakpoint returned at index 0, but this breakpoint is not a meaningful line break
// > opportunity.
//
// Skip this first line break opportunity, as it isn't interesting to us.
linebreaks: line_segmenter.segment_str(string).skip(1).collect(),
current_offset: 0,
}
}
pub(crate) fn advance_to_linebreaks_in_range(&mut self, text_range: Range<usize>) -> &[usize] {
let linebreaks_in_range = self.linebreaks_in_range_after_current_offset(text_range);
self.current_offset = linebreaks_in_range.end;
&self.linebreaks[linebreaks_in_range]
}
fn linebreaks_in_range_after_current_offset(&self, text_range: Range<usize>) -> Range<usize> {
assert!(text_range.start <= text_range.end);
let mut linebreaks_range = self.current_offset..self.linebreaks.len();
while self.linebreaks[linebreaks_range.start] < text_range.start &&
linebreaks_range.len() > 1
{
linebreaks_range.start += 1;
}
let mut ending_linebreak_index = linebreaks_range.start;
while self.linebreaks[ending_linebreak_index] < text_range.end &&
ending_linebreak_index < self.linebreaks.len() - 1
{
ending_linebreak_index += 1;
}
linebreaks_range.end = ending_linebreak_index;
linebreaks_range
}
}
#[test]
fn test_linebreaker_ranges() {
let linebreaker = LineBreaker::new("abc def");
assert_eq!(linebreaker.linebreaks, [4, 7]);
assert_eq!(
linebreaker.linebreaks_in_range_after_current_offset(0..5),
0..1
);
// The last linebreak should not be included for the text range we are interested in.
assert_eq!(
linebreaker.linebreaks_in_range_after_current_offset(0..7),
0..1
);
let linebreaker = LineBreaker::new("abc d def");
assert_eq!(linebreaker.linebreaks, [4, 6, 9]);
assert_eq!(
linebreaker.linebreaks_in_range_after_current_offset(0..5),
0..1
);
assert_eq!(
linebreaker.linebreaks_in_range_after_current_offset(0..7),
0..2
);
assert_eq!(
linebreaker.linebreaks_in_range_after_current_offset(0..9),
0..2
);
assert_eq!(
linebreaker.linebreaks_in_range_after_current_offset(4..9),
0..2
);
std::panic::catch_unwind(|| {
let linebreaker = LineBreaker::new("abc def");
linebreaker.linebreaks_in_range_after_current_offset(5..2);
})
.expect_err("Reversed range should cause an assertion failure.");
}
#[test]
fn test_linebreaker_stateful_advance() {
let mut linebreaker = LineBreaker::new("abc d def");
assert_eq!(linebreaker.linebreaks, [4, 6, 9]);
assert!(linebreaker.advance_to_linebreaks_in_range(0..7) == &[4, 6]);
assert!(linebreaker.advance_to_linebreaks_in_range(8..9).is_empty());
// We've already advanced, so a range from the beginning shouldn't affect things.
assert!(linebreaker.advance_to_linebreaks_in_range(0..9).is_empty());
linebreaker.current_offset = 0;
// Sending a value out of range shoudn't break things.
assert!(linebreaker.advance_to_linebreaks_in_range(0..999) == &[4, 6]);
linebreaker.current_offset = 0;
std::panic::catch_unwind(|| {
let mut linebreaker = LineBreaker::new("abc d def");
linebreaker.advance_to_linebreaks_in_range(2..0);
})
.expect_err("Reversed range should cause an assertion failure.");
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,640 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 std::mem;
use std::ops::Range;
use app_units::Au;
use base::text::is_bidi_control;
use fonts::{
FontContext, FontRef, GlyphRun, LAST_RESORT_GLYPH_ADVANCE, ShapingFlags, ShapingOptions,
};
use fonts_traits::ByteIndex;
use log::warn;
use malloc_size_of_derive::MallocSizeOf;
use range::Range as ServoRange;
use servo_arc::Arc;
use style::computed_values::text_rendering::T as TextRendering;
use style::computed_values::white_space_collapse::T as WhiteSpaceCollapse;
use style::computed_values::word_break::T as WordBreak;
use style::properties::ComputedValues;
use style::str::char_is_whitespace;
use style::values::computed::OverflowWrap;
use unicode_bidi::{BidiInfo, Level};
use unicode_script::Script;
use xi_unicode::linebreak_property;
use super::line_breaker::LineBreaker;
use super::{FontKeyAndMetrics, InlineFormattingContextLayout};
use crate::fragment_tree::BaseFragmentInfo;
// These constants are the xi-unicode line breaking classes that are defined in
// `table.rs`. Unfortunately, they are only identified by number.
pub(crate) const XI_LINE_BREAKING_CLASS_CM: u8 = 9;
pub(crate) const XI_LINE_BREAKING_CLASS_GL: u8 = 12;
pub(crate) const XI_LINE_BREAKING_CLASS_ZW: u8 = 28;
pub(crate) const XI_LINE_BREAKING_CLASS_WJ: u8 = 30;
pub(crate) const XI_LINE_BREAKING_CLASS_ZWJ: u8 = 42;
/// <https://www.w3.org/TR/css-display-3/#css-text-run>
#[derive(Debug, MallocSizeOf)]
pub(crate) struct TextRun {
pub base_fragment_info: BaseFragmentInfo,
#[conditional_malloc_size_of]
pub parent_style: Arc<ComputedValues>,
pub text_range: Range<usize>,
/// The text of this [`TextRun`] with a font selected, broken into unbreakable
/// segments, and shaped.
pub shaped_text: Vec<TextRunSegment>,
pub selection_range: Option<ServoRange<ByteIndex>>,
#[conditional_malloc_size_of]
pub selected_style: Arc<ComputedValues>,
}
// There are two reasons why we might want to break at the start:
//
// 1. The line breaker told us that a break was necessary between two separate
// instances of sending text to it.
// 2. We are following replaced content ie `have_deferred_soft_wrap_opportunity`.
//
// In both cases, we don't want to do this if the first character prevents a
// soft wrap opportunity.
#[derive(PartialEq)]
enum SegmentStartSoftWrapPolicy {
Force,
FollowLinebreaker,
}
#[derive(Debug, MallocSizeOf)]
pub(crate) struct TextRunSegment {
/// The index of this font in the parent [`super::InlineFormattingContext`]'s collection of font
/// information.
pub font_index: usize,
/// The [`Script`] of this segment.
pub script: Script,
/// The bidi Level of this segment.
pub bidi_level: Level,
/// The range of bytes in the parent [`super::InlineFormattingContext`]'s text content.
pub range: Range<usize>,
/// Whether or not the linebreaker said that we should allow a line break at the start of this
/// segment.
pub break_at_start: bool,
/// The shaped runs within this segment.
pub runs: Vec<GlyphRun>,
}
impl TextRunSegment {
fn new(font_index: usize, script: Script, bidi_level: Level, start_offset: usize) -> Self {
Self {
font_index,
script,
bidi_level,
range: start_offset..start_offset,
runs: Vec::new(),
break_at_start: false,
}
}
/// Update this segment if the Font and Script are compatible. The update will only
/// ever make the Script specific. Returns true if the new Font and Script are
/// compatible with this segment or false otherwise.
fn update_if_compatible(
&mut self,
new_font: &FontRef,
script: Script,
bidi_level: Level,
fonts: &[FontKeyAndMetrics],
font_context: &FontContext,
) -> bool {
fn is_specific(script: Script) -> bool {
script != Script::Common && script != Script::Inherited
}
if bidi_level != self.bidi_level {
return false;
}
let current_font_key_and_metrics = &fonts[self.font_index];
if new_font.key(font_context) != current_font_key_and_metrics.key ||
new_font.descriptor.pt_size != current_font_key_and_metrics.pt_size
{
return false;
}
if !is_specific(self.script) && is_specific(script) {
self.script = script;
}
script == self.script || !is_specific(script)
}
fn layout_into_line_items(
&self,
text_run: &TextRun,
mut soft_wrap_policy: SegmentStartSoftWrapPolicy,
ifc: &mut InlineFormattingContextLayout,
) {
if self.break_at_start && soft_wrap_policy == SegmentStartSoftWrapPolicy::FollowLinebreaker
{
soft_wrap_policy = SegmentStartSoftWrapPolicy::Force;
}
let mut byte_processed = ByteIndex(0);
for (run_index, run) in self.runs.iter().enumerate() {
ifc.possibly_flush_deferred_forced_line_break();
// If this whitespace forces a line break, queue up a hard line break the next time we
// see any content. We don't line break immediately, because we'd like to finish processing
// any ongoing inline boxes before ending the line.
if run.is_single_preserved_newline() {
byte_processed = byte_processed + run.range.length();
ifc.defer_forced_line_break();
continue;
}
// Break before each unbreakable run in this TextRun, except the first unless the
// linebreaker was set to break before the first run.
if run_index != 0 || soft_wrap_policy == SegmentStartSoftWrapPolicy::Force {
ifc.process_soft_wrap_opportunity();
}
ifc.push_glyph_store_to_unbreakable_segment(
run.glyph_store.clone(),
text_run,
self.font_index,
self.bidi_level,
ServoRange::<ByteIndex>::new(
byte_processed + ByteIndex(self.range.start as isize),
ByteIndex(self.range.len() as isize) - byte_processed,
),
);
byte_processed = byte_processed + run.range.length();
}
}
fn shape_and_push_range(
&mut self,
range: &Range<usize>,
formatting_context_text: &str,
segment_font: &FontRef,
options: &ShapingOptions,
) {
self.runs.push(GlyphRun {
glyph_store: segment_font.shape_text(&formatting_context_text[range.clone()], options),
range: ServoRange::new(
ByteIndex(range.start as isize),
ByteIndex(range.len() as isize),
),
});
}
/// Shape the text of this [`TextRunSegment`], first finding "words" for the shaper by processing
/// the linebreaks found in the owning [`super::InlineFormattingContext`]. Linebreaks are filtered,
/// based on the style of the parent inline box.
fn shape_text(
&mut self,
parent_style: &ComputedValues,
formatting_context_text: &str,
linebreaker: &mut LineBreaker,
shaping_options: &ShapingOptions,
font: FontRef,
) {
// Gather the linebreaks that apply to this segment from the inline formatting context's collection
// of line breaks. Also add a simulated break at the end of the segment in order to ensure the final
// piece of text is processed.
let range = self.range.clone();
let linebreaks = linebreaker.advance_to_linebreaks_in_range(self.range.clone());
let linebreak_iter = linebreaks.iter().chain(std::iter::once(&range.end));
self.runs.clear();
self.runs.reserve(linebreaks.len());
self.break_at_start = false;
let text_style = parent_style.get_inherited_text().clone();
let can_break_anywhere = text_style.word_break == WordBreak::BreakAll ||
text_style.overflow_wrap == OverflowWrap::Anywhere ||
text_style.overflow_wrap == OverflowWrap::BreakWord;
let mut last_slice = self.range.start..self.range.start;
for break_index in linebreak_iter {
if *break_index == self.range.start {
self.break_at_start = true;
continue;
}
let mut options = *shaping_options;
// Extend the slice to the next UAX#14 line break opportunity.
let mut slice = last_slice.end..*break_index;
let word = &formatting_context_text[slice.clone()];
// Split off any trailing whitespace into a separate glyph run.
let mut whitespace = slice.end..slice.end;
let mut rev_char_indices = word.char_indices().rev().peekable();
let mut ends_with_whitespace = false;
let ends_with_newline = rev_char_indices
.peek()
.is_some_and(|&(_, character)| character == '\n');
if let Some((first_white_space_index, first_white_space_character)) = rev_char_indices
.take_while(|&(_, character)| char_is_whitespace(character))
.last()
{
ends_with_whitespace = true;
whitespace.start = slice.start + first_white_space_index;
// If line breaking for a piece of text that has `white-space-collapse: break-spaces` there
// is a line break opportunity *after* every preserved space, but not before. This means
// that we should not split off the first whitespace, unless that white-space is a preserved
// newline.
//
// An exception to this is if the style tells us that we can break in the middle of words.
if text_style.white_space_collapse == WhiteSpaceCollapse::BreakSpaces &&
first_white_space_character != '\n' &&
!can_break_anywhere
{
whitespace.start += first_white_space_character.len_utf8();
options
.flags
.insert(ShapingFlags::ENDS_WITH_WHITESPACE_SHAPING_FLAG);
}
slice.end = whitespace.start;
}
// If there's no whitespace and `word-break` is set to `keep-all`, try increasing the slice.
// TODO: This should only happen for CJK text.
if !ends_with_whitespace &&
*break_index != self.range.end &&
text_style.word_break == WordBreak::KeepAll &&
!can_break_anywhere
{
continue;
}
// Only advance the last slice if we are not going to try to expand the slice.
last_slice = slice.start..*break_index;
// Push the non-whitespace part of the range.
if !slice.is_empty() {
self.shape_and_push_range(&slice, formatting_context_text, &font, &options);
}
if whitespace.is_empty() {
continue;
}
options.flags.insert(
ShapingFlags::IS_WHITESPACE_SHAPING_FLAG |
ShapingFlags::ENDS_WITH_WHITESPACE_SHAPING_FLAG,
);
// If `white-space-collapse: break-spaces` is active, insert a line breaking opportunity
// between each white space character in the white space that we trimmed off.
if text_style.white_space_collapse == WhiteSpaceCollapse::BreakSpaces {
let start_index = whitespace.start;
for (index, character) in formatting_context_text[whitespace].char_indices() {
let index = start_index + index;
self.shape_and_push_range(
&(index..index + character.len_utf8()),
formatting_context_text,
&font,
&options,
);
}
continue;
}
// The breaker breaks after every newline, so either there is none,
// or there is exactly one at the very end. In the latter case,
// split it into a different run. That's because shaping considers
// a newline to have the same advance as a space, but during layout
// we want to treat the newline as having no advance.
if ends_with_newline && whitespace.len() > 1 {
self.shape_and_push_range(
&(whitespace.start..whitespace.end - 1),
formatting_context_text,
&font,
&options,
);
self.shape_and_push_range(
&(whitespace.end - 1..whitespace.end),
formatting_context_text,
&font,
&options,
);
} else {
self.shape_and_push_range(&whitespace, formatting_context_text, &font, &options);
}
}
}
}
impl TextRun {
pub(crate) fn new(
base_fragment_info: BaseFragmentInfo,
parent_style: Arc<ComputedValues>,
text_range: Range<usize>,
selection_range: Option<ServoRange<ByteIndex>>,
selected_style: Arc<ComputedValues>,
) -> Self {
Self {
base_fragment_info,
parent_style,
text_range,
shaped_text: Vec::new(),
selection_range,
selected_style,
}
}
pub(super) fn segment_and_shape(
&mut self,
formatting_context_text: &str,
font_context: &FontContext,
linebreaker: &mut LineBreaker,
font_cache: &mut Vec<FontKeyAndMetrics>,
bidi_info: &BidiInfo,
) {
let inherited_text_style = self.parent_style.get_inherited_text().clone();
let letter_spacing = inherited_text_style
.letter_spacing
.0
.resolve(self.parent_style.clone_font().font_size.computed_size());
let letter_spacing = if letter_spacing.px() != 0. {
Some(app_units::Au::from(letter_spacing))
} else {
None
};
let mut flags = ShapingFlags::empty();
if letter_spacing.is_some() {
flags.insert(ShapingFlags::IGNORE_LIGATURES_SHAPING_FLAG);
}
if inherited_text_style.text_rendering == TextRendering::Optimizespeed {
flags.insert(ShapingFlags::IGNORE_LIGATURES_SHAPING_FLAG);
flags.insert(ShapingFlags::DISABLE_KERNING_SHAPING_FLAG)
}
let specified_word_spacing = &inherited_text_style.word_spacing;
let style_word_spacing: Option<Au> = specified_word_spacing.to_length().map(|l| l.into());
let segments = self
.segment_text_by_font(formatting_context_text, font_context, font_cache, bidi_info)
.into_iter()
.map(|(mut segment, font)| {
let word_spacing = style_word_spacing.unwrap_or_else(|| {
let space_width = font
.glyph_index(' ')
.map(|glyph_id| font.glyph_h_advance(glyph_id))
.unwrap_or(LAST_RESORT_GLYPH_ADVANCE);
specified_word_spacing.to_used_value(Au::from_f64_px(space_width))
});
let mut flags = flags;
if segment.bidi_level.is_rtl() {
flags.insert(ShapingFlags::RTL_FLAG);
}
let shaping_options = ShapingOptions {
letter_spacing,
word_spacing,
script: segment.script,
flags,
};
segment.shape_text(
&self.parent_style,
formatting_context_text,
linebreaker,
&shaping_options,
font,
);
segment
})
.collect();
let _ = std::mem::replace(&mut self.shaped_text, segments);
}
/// Take the [`TextRun`]'s text and turn it into [`TextRunSegment`]s. Each segment has a matched
/// font and script. Fonts may differ when glyphs are found in fallback fonts. Fonts are stored
/// in the `font_cache` which is a cache of all font keys and metrics used in this
/// [`super::InlineFormattingContext`].
fn segment_text_by_font(
&mut self,
formatting_context_text: &str,
font_context: &FontContext,
font_cache: &mut Vec<FontKeyAndMetrics>,
bidi_info: &BidiInfo,
) -> Vec<(TextRunSegment, FontRef)> {
let font_group = font_context.font_group(self.parent_style.clone_font());
let mut current: Option<(TextRunSegment, FontRef)> = None;
let mut results = Vec::new();
let text_run_text = &formatting_context_text[self.text_range.clone()];
let char_iterator = TwoCharsAtATimeIterator::new(text_run_text.chars());
let mut next_byte_index = self.text_range.start;
for (character, next_character) in char_iterator {
let current_byte_index = next_byte_index;
next_byte_index += character.len_utf8();
if char_does_not_change_font(character) {
continue;
}
// If the script and BiDi level do not change, use the current font as the first fallback. This
// can potentially speed up fallback on long font lists or with uncommon scripts which might be
// at the bottom of the list.
let script = Script::from(character);
let bidi_level = bidi_info.levels[current_byte_index];
let current_font = current.as_ref().and_then(|(text_run_segment, font)| {
if text_run_segment.bidi_level == bidi_level && text_run_segment.script == script {
Some(font.clone())
} else {
None
}
});
let Some(font) = font_group.write().find_by_codepoint(
font_context,
character,
next_character,
current_font,
) else {
continue;
};
// If the existing segment is compatible with the character, keep going.
if let Some(current) = current.as_mut() {
if current.0.update_if_compatible(
&font,
script,
bidi_level,
font_cache,
font_context,
) {
continue;
}
}
let font_index = add_or_get_font(&font, font_cache, font_context);
// Add the new segment and finish the existing one, if we had one. If the first
// characters in the run were control characters we may be creating the first
// segment in the middle of the run (ie the start should be the start of this
// text run's text).
let start_byte_index = match current {
Some(_) => current_byte_index,
None => self.text_range.start,
};
let new = (
TextRunSegment::new(font_index, script, bidi_level, start_byte_index),
font,
);
if let Some(mut finished) = current.replace(new) {
// The end of the previous segment is the start of the next one.
finished.0.range.end = current_byte_index;
results.push(finished);
}
}
// Either we have a current segment or we only had control character and whitespace. In both
// of those cases, just use the first font.
if current.is_none() {
current = font_group.write().first(font_context).map(|font| {
let font_index = add_or_get_font(&font, font_cache, font_context);
(
TextRunSegment::new(
font_index,
Script::Common,
Level::ltr(),
self.text_range.start,
),
font,
)
})
}
// Extend the last segment to the end of the string and add it to the results.
if let Some(mut last_segment) = current.take() {
last_segment.0.range.end = self.text_range.end;
results.push(last_segment);
}
results
}
pub(super) fn layout_into_line_items(&self, ifc: &mut InlineFormattingContextLayout) {
if self.text_range.is_empty() {
return;
}
// If we are following replaced content, we should have a soft wrap opportunity, unless the
// first character of this `TextRun` prevents that soft wrap opportunity. If we see such a
// character it should also override the LineBreaker's indication to break at the start.
let have_deferred_soft_wrap_opportunity =
mem::replace(&mut ifc.have_deferred_soft_wrap_opportunity, false);
let mut soft_wrap_policy = match have_deferred_soft_wrap_opportunity {
true => SegmentStartSoftWrapPolicy::Force,
false => SegmentStartSoftWrapPolicy::FollowLinebreaker,
};
for segment in self.shaped_text.iter() {
segment.layout_into_line_items(self, soft_wrap_policy, ifc);
soft_wrap_policy = SegmentStartSoftWrapPolicy::FollowLinebreaker;
}
}
}
/// Whether or not this character should be able to change the font during segmentation. Certain
/// character are not rendered at all, so it doesn't matter what font we use to render them. They
/// should just be added to the current segment.
fn char_does_not_change_font(character: char) -> bool {
if character.is_control() {
return true;
}
if character == '\u{00A0}' {
return true;
}
if is_bidi_control(character) {
return false;
}
let class = linebreak_property(character);
class == XI_LINE_BREAKING_CLASS_CM ||
class == XI_LINE_BREAKING_CLASS_GL ||
class == XI_LINE_BREAKING_CLASS_ZW ||
class == XI_LINE_BREAKING_CLASS_WJ ||
class == XI_LINE_BREAKING_CLASS_ZWJ
}
pub(super) fn add_or_get_font(
font: &FontRef,
ifc_fonts: &mut Vec<FontKeyAndMetrics>,
font_context: &FontContext,
) -> usize {
let font_instance_key = font.key(font_context);
for (index, ifc_font_info) in ifc_fonts.iter().enumerate() {
if ifc_font_info.key == font_instance_key &&
ifc_font_info.pt_size == font.descriptor.pt_size
{
return index;
}
}
ifc_fonts.push(FontKeyAndMetrics {
metrics: font.metrics.clone(),
key: font_instance_key,
pt_size: font.descriptor.pt_size,
});
ifc_fonts.len() - 1
}
pub(super) fn get_font_for_first_font_for_style(
style: &ComputedValues,
font_context: &FontContext,
) -> Option<FontRef> {
let font = font_context
.font_group(style.clone_font())
.write()
.first(font_context);
if font.is_none() {
warn!("Could not find font for style: {:?}", style.clone_font());
}
font
}
pub(crate) struct TwoCharsAtATimeIterator<InputIterator> {
/// The input character iterator.
iterator: InputIterator,
/// The first character to produce in the next run of the iterator.
next_character: Option<char>,
}
impl<InputIterator> TwoCharsAtATimeIterator<InputIterator> {
fn new(iterator: InputIterator) -> Self {
Self {
iterator,
next_character: None,
}
}
}
impl<InputIterator> Iterator for TwoCharsAtATimeIterator<InputIterator>
where
InputIterator: Iterator<Item = char>,
{
type Item = (char, Option<char>);
fn next(&mut self) -> Option<Self::Item> {
// If the iterator isn't initialized do that now.
if self.next_character.is_none() {
self.next_character = self.iterator.next();
}
let character = self.next_character?;
self.next_character = self.iterator.next();
Some((character, self.next_character))
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,500 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 app_units::Au;
use atomic_refcell::AtomicRef;
use compositing_traits::display_list::AxesScrollSensitivity;
use malloc_size_of_derive::MallocSizeOf;
use script_layout_interface::wrapper_traits::{
LayoutNode, ThreadSafeLayoutElement, ThreadSafeLayoutNode,
};
use script_layout_interface::{LayoutElementType, LayoutNodeType};
use servo_arc::Arc;
use style::dom::OpaqueNode;
use style::properties::ComputedValues;
use style::values::computed::Overflow;
use style_traits::CSSPixel;
use crate::cell::ArcRefCell;
use crate::context::LayoutContext;
use crate::dom::{LayoutBox, NodeExt};
use crate::dom_traversal::{Contents, NodeAndStyleInfo, NonReplacedContents, iter_child_nodes};
use crate::flexbox::FlexLevelBox;
use crate::flow::float::FloatBox;
use crate::flow::inline::InlineItem;
use crate::flow::{BlockContainer, BlockFormattingContext, BlockLevelBox};
use crate::formatting_contexts::IndependentFormattingContext;
use crate::fragment_tree::FragmentTree;
use crate::geom::{LogicalVec2, PhysicalPoint, PhysicalRect, PhysicalSize};
use crate::positioned::{AbsolutelyPositionedBox, PositioningContext};
use crate::replaced::ReplacedContents;
use crate::style_ext::{ComputedValuesExt, Display, DisplayGeneratingBox, DisplayInside};
use crate::taffy::{TaffyItemBox, TaffyItemBoxInner};
use crate::{DefiniteContainingBlock, PropagatedBoxTreeData};
#[derive(MallocSizeOf)]
pub struct BoxTree {
/// Contains typically exactly one block-level box, which was generated by the root element.
/// There may be zero if that element has `display: none`.
root: BlockFormattingContext,
/// <https://drafts.csswg.org/css-backgrounds/#special-backgrounds>
canvas_background: CanvasBackground,
/// Whether or not the viewport should be sensitive to scrolling input events in two axes
viewport_scroll_sensitivity: AxesScrollSensitivity,
}
impl BoxTree {
pub fn construct<'dom, Node>(context: &LayoutContext, root_element: Node) -> Self
where
Node: 'dom + Copy + LayoutNode<'dom> + Send + Sync,
{
let boxes = construct_for_root_element(context, root_element);
// Zero box for `:root { display: none }`, one for the root element otherwise.
assert!(boxes.len() <= 1);
// From https://www.w3.org/TR/css-overflow-3/#overflow-propagation:
// > UAs must apply the overflow-* values set on the root element to the viewport when the
// > root elements display value is not none. However, when the root element is an [HTML]
// > html element (including XML syntax for HTML) whose overflow value is visible (in both
// > axes), and that element has as a child a body element whose display value is also not
// > none, user agents must instead apply the overflow-* values of the first such child
// > element to the viewport. The element from which the value is propagated must then have a
// > used overflow value of visible.
let root_style = root_element.style(context);
let mut viewport_overflow_x = root_style.clone_overflow_x();
let mut viewport_overflow_y = root_style.clone_overflow_y();
if viewport_overflow_x == Overflow::Visible &&
viewport_overflow_y == Overflow::Visible &&
!root_style.get_box().display.is_none()
{
for child in iter_child_nodes(root_element) {
if !child
.to_threadsafe()
.as_element()
.is_some_and(|element| element.is_body_element_of_html_element_root())
{
continue;
}
let style = child.style(context);
if !style.get_box().display.is_none() {
viewport_overflow_x = style.clone_overflow_x();
viewport_overflow_y = style.clone_overflow_y();
break;
}
}
}
let contents = BlockContainer::BlockLevelBoxes(boxes);
let contains_floats = contents.contains_floats();
Self {
root: BlockFormattingContext {
contents,
contains_floats,
},
canvas_background: CanvasBackground::for_root_element(context, root_element),
// From https://www.w3.org/TR/css-overflow-3/#overflow-propagation:
// > If visible is applied to the viewport, it must be interpreted as auto.
// > If clip is applied to the viewport, it must be interpreted as hidden.
viewport_scroll_sensitivity: AxesScrollSensitivity {
x: viewport_overflow_x.to_scrollable().into(),
y: viewport_overflow_y.to_scrollable().into(),
},
}
}
/// This method attempts to incrementally update the box tree from an
/// arbitrary node that is not necessarily the document's root element.
///
/// If the node is not a valid candidate for incremental update, the method
/// loops over its parent. The only valid candidates for now are absolutely
/// positioned boxes which don't change their outside display mode (i.e. it
/// will not attempt to update from an absolutely positioned inline element
/// which became an absolutely positioned block element). The value `true`
/// is returned if an incremental update could be done, and `false`
/// otherwise.
///
/// There are various pain points that need to be taken care of to extend
/// the set of valid candidates:
/// * it is not obvious how to incrementally check whether a block
/// formatting context still contains floats or not;
/// * the propagation of text decorations towards node descendants is
/// hard to do incrementally with our current representation of boxes
/// * how intrinsic content sizes are computed eagerly makes it hard
/// to update those sizes for ancestors of the node from which we
/// made an incremental update.
pub fn update<'dom, Node>(context: &LayoutContext, mut dirty_node: Node) -> bool
where
Node: 'dom + Copy + LayoutNode<'dom> + Send + Sync,
{
#[allow(clippy::enum_variant_names)]
enum UpdatePoint {
AbsolutelyPositionedBlockLevelBox(ArcRefCell<BlockLevelBox>),
AbsolutelyPositionedInlineLevelBox(ArcRefCell<InlineItem>, usize),
AbsolutelyPositionedFlexLevelBox(ArcRefCell<FlexLevelBox>),
AbsolutelyPositionedTaffyLevelBox(ArcRefCell<TaffyItemBox>),
}
fn update_point<'dom, Node>(
node: Node,
) -> Option<(Arc<ComputedValues>, DisplayInside, UpdatePoint)>
where
Node: NodeExt<'dom>,
{
if !node.is_element() {
return None;
}
if node.type_id() == LayoutNodeType::Element(LayoutElementType::HTMLBodyElement) {
// This can require changes to the canvas background.
return None;
}
// Don't update unstyled nodes or nodes that have pseudo-elements.
let element_data = node.style_data()?.element_data.borrow();
if !element_data.styles.pseudos.is_empty() {
return None;
}
let layout_data = node.layout_data()?;
if layout_data.pseudo_before_box.borrow().is_some() {
return None;
}
if layout_data.pseudo_after_box.borrow().is_some() {
return None;
}
let primary_style = element_data.styles.primary();
let box_style = primary_style.get_box();
if !box_style.position.is_absolutely_positioned() {
return None;
}
let display_inside = match Display::from(box_style.display) {
Display::GeneratingBox(DisplayGeneratingBox::OutsideInside { inside, .. }) => {
inside
},
_ => return None,
};
let update_point =
match &*AtomicRef::filter_map(layout_data.self_box.borrow(), Option::as_ref)? {
LayoutBox::DisplayContents => return None,
LayoutBox::BlockLevel(block_level_box) => match &*block_level_box.borrow() {
BlockLevelBox::OutOfFlowAbsolutelyPositionedBox(_)
if box_style.position.is_absolutely_positioned() =>
{
UpdatePoint::AbsolutelyPositionedBlockLevelBox(block_level_box.clone())
},
_ => return None,
},
LayoutBox::InlineLevel(inline_level_box) => match &*inline_level_box.borrow() {
InlineItem::OutOfFlowAbsolutelyPositionedBox(_, text_offset_index)
if box_style.position.is_absolutely_positioned() =>
{
UpdatePoint::AbsolutelyPositionedInlineLevelBox(
inline_level_box.clone(),
*text_offset_index,
)
},
_ => return None,
},
LayoutBox::FlexLevel(flex_level_box) => match &*flex_level_box.borrow() {
FlexLevelBox::OutOfFlowAbsolutelyPositionedBox(_)
if box_style.position.is_absolutely_positioned() =>
{
UpdatePoint::AbsolutelyPositionedFlexLevelBox(flex_level_box.clone())
},
_ => return None,
},
LayoutBox::TableLevelBox(..) => return None,
LayoutBox::TaffyItemBox(taffy_level_box) => match &taffy_level_box
.borrow()
.taffy_level_box
{
TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(_)
if box_style.position.is_absolutely_positioned() =>
{
UpdatePoint::AbsolutelyPositionedTaffyLevelBox(taffy_level_box.clone())
},
_ => return None,
},
};
Some((primary_style.clone(), display_inside, update_point))
}
loop {
let Some((primary_style, display_inside, update_point)) = update_point(dirty_node)
else {
dirty_node = match dirty_node.parent_node() {
Some(parent) => parent,
None => return false,
};
continue;
};
let contents = ReplacedContents::for_element(dirty_node, context)
.map_or_else(|| NonReplacedContents::OfElement.into(), Contents::Replaced);
let info = NodeAndStyleInfo::new(dirty_node, Arc::clone(&primary_style));
let out_of_flow_absolutely_positioned_box = ArcRefCell::new(
AbsolutelyPositionedBox::construct(context, &info, display_inside, contents),
);
match update_point {
UpdatePoint::AbsolutelyPositionedBlockLevelBox(block_level_box) => {
*block_level_box.borrow_mut() = BlockLevelBox::OutOfFlowAbsolutelyPositionedBox(
out_of_flow_absolutely_positioned_box,
);
},
UpdatePoint::AbsolutelyPositionedInlineLevelBox(
inline_level_box,
text_offset_index,
) => {
*inline_level_box.borrow_mut() = InlineItem::OutOfFlowAbsolutelyPositionedBox(
out_of_flow_absolutely_positioned_box,
text_offset_index,
);
},
UpdatePoint::AbsolutelyPositionedFlexLevelBox(flex_level_box) => {
*flex_level_box.borrow_mut() = FlexLevelBox::OutOfFlowAbsolutelyPositionedBox(
out_of_flow_absolutely_positioned_box,
);
},
UpdatePoint::AbsolutelyPositionedTaffyLevelBox(taffy_level_box) => {
taffy_level_box.borrow_mut().taffy_level_box =
TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(
out_of_flow_absolutely_positioned_box,
);
},
}
break;
}
// We are going to rebuild the box tree from the update point downward, but this update
// point is an absolute, which means that it needs to be laid out again in the containing
// block for absolutes, which is established by one of its ancestors. In addition,
// absolutes, when laid out, can produce more absolutes (either fixed or absolutely
// positioned) elements, so there may be yet more layout that has to happen in this
// ancestor.
//
// We do not know which ancestor is the one that established the containing block for this
// update point, so just invalidate the fragment cache of all ancestors, meaning that even
// though the box tree is preserved, the fragment tree from the root to the update point and
// all of its descendants will need to be rebuilt. This isn't as bad as it seems, because
// siblings and siblings of ancestors of this path through the tree will still have cached
// fragments.
//
// TODO: Do better. This is still a very crude way to do incremental layout.
while let Some(parent_node) = dirty_node.parent_node() {
parent_node.invalidate_cached_fragment();
dirty_node = parent_node;
}
true
}
}
fn construct_for_root_element<'dom>(
context: &LayoutContext,
root_element: impl NodeExt<'dom>,
) -> Vec<ArcRefCell<BlockLevelBox>> {
let info = NodeAndStyleInfo::new(root_element, root_element.style(context));
let box_style = info.style.get_box();
let display_inside = match Display::from(box_style.display) {
Display::None => {
root_element.unset_all_boxes();
return Vec::new();
},
Display::Contents => {
// Unreachable because the style crate adjusts the computed values:
// https://drafts.csswg.org/css-display-3/#transformations
// “'display' of 'contents' computes to 'block' on the root element”
unreachable!()
},
// The root element is blockified, ignore DisplayOutside
Display::GeneratingBox(display_generating_box) => display_generating_box.display_inside(),
};
let contents = ReplacedContents::for_element(root_element, context)
.map_or_else(|| NonReplacedContents::OfElement.into(), Contents::Replaced);
let propagated_data = PropagatedBoxTreeData::default().union(&info.style);
let root_box = if box_style.position.is_absolutely_positioned() {
BlockLevelBox::OutOfFlowAbsolutelyPositionedBox(ArcRefCell::new(
AbsolutelyPositionedBox::construct(context, &info, display_inside, contents),
))
} else if box_style.float.is_floating() {
BlockLevelBox::OutOfFlowFloatBox(FloatBox::construct(
context,
&info,
display_inside,
contents,
propagated_data,
))
} else {
BlockLevelBox::Independent(IndependentFormattingContext::construct(
context,
&info,
display_inside,
contents,
propagated_data,
))
};
let root_box = ArcRefCell::new(root_box);
root_element
.element_box_slot()
.set(LayoutBox::BlockLevel(root_box.clone()));
vec![root_box]
}
impl BoxTree {
pub fn layout(
&self,
layout_context: &LayoutContext,
viewport: euclid::Size2D<f32, CSSPixel>,
) -> FragmentTree {
let style = layout_context
.style_context
.stylist
.device()
.default_computed_values();
// FIXME: use the documents mode:
// https://drafts.csswg.org/css-writing-modes/#principal-flow
let physical_containing_block = PhysicalRect::new(
PhysicalPoint::zero(),
PhysicalSize::new(
Au::from_f32_px(viewport.width),
Au::from_f32_px(viewport.height),
),
);
let initial_containing_block = DefiniteContainingBlock {
size: LogicalVec2 {
inline: physical_containing_block.size.width,
block: physical_containing_block.size.height,
},
style,
};
let mut positioning_context =
PositioningContext::new_for_containing_block_for_all_descendants();
let independent_layout = self.root.layout(
layout_context,
&mut positioning_context,
&(&initial_containing_block).into(),
false, /* depends_on_block_constraints */
);
let mut root_fragments = independent_layout.fragments.into_iter().collect::<Vec<_>>();
// Zero box for `:root { display: none }`, one for the root element otherwise.
assert!(root_fragments.len() <= 1);
// There may be more fragments at the top-level
// (for positioned boxes whose containing is the initial containing block)
// but only if there was one fragment for the root element.
positioning_context.layout_initial_containing_block_children(
layout_context,
&initial_containing_block,
&mut root_fragments,
);
let scrollable_overflow = root_fragments
.iter()
.fold(PhysicalRect::zero(), |acc, child| {
let child_overflow = child.scrollable_overflow();
// https://drafts.csswg.org/css-overflow/#scrolling-direction
// We want to clip scrollable overflow on box-start and inline-start
// sides of the scroll container.
//
// FIXME(mrobinson, bug 25564): This should take into account writing
// mode.
let child_overflow = PhysicalRect::new(
euclid::Point2D::zero(),
euclid::Size2D::new(
child_overflow.size.width + child_overflow.origin.x,
child_overflow.size.height + child_overflow.origin.y,
),
);
acc.union(&child_overflow)
});
FragmentTree {
root_fragments,
scrollable_overflow,
initial_containing_block: physical_containing_block,
canvas_background: self.canvas_background.clone(),
viewport_scroll_sensitivity: self.viewport_scroll_sensitivity,
}
}
}
/// <https://drafts.csswg.org/css-backgrounds/#root-background>
#[derive(Clone, MallocSizeOf)]
pub struct CanvasBackground {
/// DOM node for the root element
pub root_element: OpaqueNode,
/// The element whose style the canvas takes background properties from (see next field).
/// This can be the root element (same as the previous field), or the HTML `<body>` element.
/// See <https://drafts.csswg.org/css-backgrounds/#body-background>
pub from_element: OpaqueNode,
/// The computed styles to take background properties from.
#[conditional_malloc_size_of]
pub style: Option<Arc<ComputedValues>>,
}
impl CanvasBackground {
fn for_root_element<'dom>(context: &LayoutContext, root_element: impl NodeExt<'dom>) -> Self {
let root_style = root_element.style(context);
let mut style = root_style;
let mut from_element = root_element;
// https://drafts.csswg.org/css-backgrounds/#body-background
// “if the computed value of background-image on the root element is none
// and its background-color is transparent”
if style.background_is_transparent() &&
// “For documents whose root element is an HTML `HTML` element
// or an XHTML `html` element”
root_element.type_id() == LayoutNodeType::Element(LayoutElementType::HTMLHtmlElement) &&
// Dont try to access styles for an unstyled subtree
!matches!(style.clone_display().into(), Display::None)
{
// “that elements first HTML `BODY` or XHTML `body` child element”
if let Some(body) = iter_child_nodes(root_element).find(|child| {
child.is_element() &&
child.type_id() ==
LayoutNodeType::Element(LayoutElementType::HTMLBodyElement)
}) {
style = body.style(context);
from_element = body;
}
}
Self {
root_element: root_element.opaque(),
from_element: from_element.opaque(),
// “However, if no boxes are generated for the element
// whose background would be used for the canvas
// (for example, if the root element has display: none),
// then the canvas background is transparent.”
style: if let Display::GeneratingBox(_) = style.clone_display().into() {
Some(style)
} else {
None
},
}
}
}

View file

@ -0,0 +1,363 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 app_units::Au;
use malloc_size_of_derive::MallocSizeOf;
use servo_arc::Arc;
use style::properties::ComputedValues;
use style::selector_parser::PseudoElement;
use crate::context::LayoutContext;
use crate::dom::NodeExt;
use crate::dom_traversal::{Contents, NodeAndStyleInfo};
use crate::flexbox::FlexContainer;
use crate::flow::BlockFormattingContext;
use crate::fragment_tree::{BaseFragmentInfo, FragmentFlags};
use crate::layout_box_base::{
CacheableLayoutResult, CacheableLayoutResultAndInputs, LayoutBoxBase,
};
use crate::positioned::PositioningContext;
use crate::replaced::ReplacedContents;
use crate::sizing::{self, ComputeInlineContentSizes, InlineContentSizesResult};
use crate::style_ext::{AspectRatio, DisplayInside, LayoutStyle};
use crate::table::Table;
use crate::taffy::TaffyContainer;
use crate::{
ConstraintSpace, ContainingBlock, IndefiniteContainingBlock, LogicalVec2, PropagatedBoxTreeData,
};
/// <https://drafts.csswg.org/css-display/#independent-formatting-context>
#[derive(Debug, MallocSizeOf)]
pub(crate) struct IndependentFormattingContext {
pub base: LayoutBoxBase,
pub contents: IndependentFormattingContextContents,
}
#[derive(Debug, MallocSizeOf)]
pub(crate) enum IndependentFormattingContextContents {
NonReplaced(IndependentNonReplacedContents),
Replaced(ReplacedContents),
}
// Private so that code outside of this module cannot match variants.
// It should got through methods instead.
#[derive(Debug, MallocSizeOf)]
pub(crate) enum IndependentNonReplacedContents {
Flow(BlockFormattingContext),
Flex(FlexContainer),
Grid(TaffyContainer),
Table(Table),
// Other layout modes go here
}
/// The baselines of a layout or a [`crate::fragment_tree::BoxFragment`]. Some layout
/// uses the first and some layout uses the last.
#[derive(Clone, Copy, Debug, Default, MallocSizeOf)]
pub(crate) struct Baselines {
pub first: Option<Au>,
pub last: Option<Au>,
}
impl Baselines {
pub(crate) fn offset(&self, block_offset: Au) -> Baselines {
Self {
first: self.first.map(|first| first + block_offset),
last: self.last.map(|last| last + block_offset),
}
}
}
impl IndependentFormattingContext {
pub fn construct<'dom, Node: NodeExt<'dom>>(
context: &LayoutContext,
node_and_style_info: &NodeAndStyleInfo<Node>,
display_inside: DisplayInside,
contents: Contents,
propagated_data: PropagatedBoxTreeData,
) -> Self {
let mut base_fragment_info: BaseFragmentInfo = node_and_style_info.into();
match contents {
Contents::NonReplaced(non_replaced_contents) => {
let contents = match display_inside {
DisplayInside::Flow { is_list_item } |
DisplayInside::FlowRoot { is_list_item } => {
IndependentNonReplacedContents::Flow(BlockFormattingContext::construct(
context,
node_and_style_info,
non_replaced_contents,
propagated_data,
is_list_item,
))
},
DisplayInside::Grid => {
IndependentNonReplacedContents::Grid(TaffyContainer::construct(
context,
node_and_style_info,
non_replaced_contents,
propagated_data,
))
},
DisplayInside::Flex => {
IndependentNonReplacedContents::Flex(FlexContainer::construct(
context,
node_and_style_info,
non_replaced_contents,
propagated_data,
))
},
DisplayInside::Table => {
let table_grid_style = context
.shared_context()
.stylist
.style_for_anonymous::<Node::ConcreteElement>(
&context.shared_context().guards,
&PseudoElement::ServoTableGrid,
&node_and_style_info.style,
);
base_fragment_info.flags.insert(FragmentFlags::DO_NOT_PAINT);
IndependentNonReplacedContents::Table(Table::construct(
context,
node_and_style_info,
table_grid_style,
non_replaced_contents,
propagated_data,
))
},
};
Self {
base: LayoutBoxBase::new(base_fragment_info, node_and_style_info.style.clone()),
contents: IndependentFormattingContextContents::NonReplaced(contents),
}
},
Contents::Replaced(contents) => {
base_fragment_info.flags.insert(FragmentFlags::IS_REPLACED);
Self {
base: LayoutBoxBase::new(base_fragment_info, node_and_style_info.style.clone()),
contents: IndependentFormattingContextContents::Replaced(contents),
}
},
}
}
pub fn is_replaced(&self) -> bool {
matches!(
self.contents,
IndependentFormattingContextContents::Replaced(_)
)
}
#[inline]
pub fn style(&self) -> &Arc<ComputedValues> {
&self.base.style
}
#[inline]
pub fn base_fragment_info(&self) -> BaseFragmentInfo {
self.base.base_fragment_info
}
pub(crate) fn inline_content_sizes(
&self,
layout_context: &LayoutContext,
constraint_space: &ConstraintSpace,
) -> InlineContentSizesResult {
match &self.contents {
IndependentFormattingContextContents::NonReplaced(contents) => self
.base
.inline_content_sizes(layout_context, constraint_space, contents),
IndependentFormattingContextContents::Replaced(contents) => self
.base
.inline_content_sizes(layout_context, constraint_space, contents),
}
}
pub(crate) fn outer_inline_content_sizes(
&self,
layout_context: &LayoutContext,
containing_block: &IndefiniteContainingBlock,
auto_minimum: &LogicalVec2<Au>,
auto_block_size_stretches_to_containing_block: bool,
) -> InlineContentSizesResult {
sizing::outer_inline(
&self.layout_style(),
containing_block,
auto_minimum,
auto_block_size_stretches_to_containing_block,
self.is_replaced(),
true, /* establishes_containing_block */
|padding_border_sums| self.preferred_aspect_ratio(padding_border_sums),
|constraint_space| self.inline_content_sizes(layout_context, constraint_space),
)
}
pub(crate) fn preferred_aspect_ratio(
&self,
padding_border_sums: &LogicalVec2<Au>,
) -> Option<AspectRatio> {
match &self.contents {
IndependentFormattingContextContents::NonReplaced(content) => {
content.preferred_aspect_ratio()
},
IndependentFormattingContextContents::Replaced(content) => {
content.preferred_aspect_ratio(self.style(), padding_border_sums)
},
}
}
#[inline]
pub(crate) fn layout_style(&self) -> LayoutStyle {
match &self.contents {
IndependentFormattingContextContents::NonReplaced(content) => {
content.layout_style(&self.base)
},
IndependentFormattingContextContents::Replaced(content) => {
content.layout_style(&self.base)
},
}
}
}
impl IndependentNonReplacedContents {
pub(crate) fn layout_without_caching(
&self,
layout_context: &LayoutContext,
positioning_context: &mut PositioningContext,
containing_block_for_children: &ContainingBlock,
containing_block: &ContainingBlock,
depends_on_block_constraints: bool,
) -> CacheableLayoutResult {
match self {
IndependentNonReplacedContents::Flow(bfc) => bfc.layout(
layout_context,
positioning_context,
containing_block_for_children,
depends_on_block_constraints,
),
IndependentNonReplacedContents::Flex(fc) => fc.layout(
layout_context,
positioning_context,
containing_block_for_children,
depends_on_block_constraints,
),
IndependentNonReplacedContents::Grid(fc) => fc.layout(
layout_context,
positioning_context,
containing_block_for_children,
containing_block,
),
IndependentNonReplacedContents::Table(table) => table.layout(
layout_context,
positioning_context,
containing_block_for_children,
containing_block,
depends_on_block_constraints,
),
}
}
#[cfg_attr(
feature = "tracing",
tracing::instrument(
name = "IndependentNonReplacedContents::layout_with_caching",
skip_all,
fields(servo_profiling = true),
level = "trace",
)
)]
pub fn layout(
&self,
layout_context: &LayoutContext,
positioning_context: &mut PositioningContext,
containing_block_for_children: &ContainingBlock,
containing_block: &ContainingBlock,
base: &LayoutBoxBase,
depends_on_block_constraints: bool,
) -> CacheableLayoutResult {
if let Some(cache) = base.cached_layout_result.borrow().as_ref() {
let cache = &**cache;
if cache.containing_block_for_children_size.inline ==
containing_block_for_children.size.inline &&
(cache.containing_block_for_children_size.block ==
containing_block_for_children.size.block ||
!(cache.result.depends_on_block_constraints ||
depends_on_block_constraints))
{
positioning_context.append(cache.positioning_context.clone());
return cache.result.clone();
}
#[cfg(feature = "tracing")]
tracing::debug!(
name: "NonReplaced cache miss",
cached = ?cache.containing_block_for_children_size,
required = ?containing_block_for_children.size,
);
}
let mut child_positioning_context = PositioningContext::new_for_subtree(
positioning_context.collects_for_nearest_positioned_ancestor(),
);
let result = self.layout_without_caching(
layout_context,
&mut child_positioning_context,
containing_block_for_children,
containing_block,
depends_on_block_constraints,
);
*base.cached_layout_result.borrow_mut() = Some(Box::new(CacheableLayoutResultAndInputs {
result: result.clone(),
positioning_context: child_positioning_context.clone(),
containing_block_for_children_size: containing_block_for_children.size.clone(),
}));
positioning_context.append(child_positioning_context);
result
}
#[inline]
pub(crate) fn layout_style<'a>(&'a self, base: &'a LayoutBoxBase) -> LayoutStyle<'a> {
match self {
IndependentNonReplacedContents::Flow(fc) => fc.layout_style(base),
IndependentNonReplacedContents::Flex(fc) => fc.layout_style(),
IndependentNonReplacedContents::Grid(fc) => fc.layout_style(),
IndependentNonReplacedContents::Table(fc) => fc.layout_style(None),
}
}
#[inline]
pub(crate) fn preferred_aspect_ratio(&self) -> Option<AspectRatio> {
// TODO: support preferred aspect ratios on non-replaced boxes.
None
}
#[inline]
pub(crate) fn is_table(&self) -> bool {
matches!(self, Self::Table(_))
}
}
impl ComputeInlineContentSizes for IndependentNonReplacedContents {
fn compute_inline_content_sizes(
&self,
layout_context: &LayoutContext,
constraint_space: &ConstraintSpace,
) -> InlineContentSizesResult {
match self {
Self::Flow(inner) => inner
.contents
.compute_inline_content_sizes(layout_context, constraint_space),
Self::Flex(inner) => {
inner.compute_inline_content_sizes(layout_context, constraint_space)
},
Self::Grid(inner) => {
inner.compute_inline_content_sizes(layout_context, constraint_space)
},
Self::Table(inner) => {
inner.compute_inline_content_sizes(layout_context, constraint_space)
},
}
}
}

View file

@ -0,0 +1,143 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 bitflags::bitflags;
use malloc_size_of::malloc_size_of_is_0;
use malloc_size_of_derive::MallocSizeOf;
use script_layout_interface::combine_id_with_fragment_type;
use style::dom::OpaqueNode;
use style::selector_parser::PseudoElement;
/// This data structure stores fields that are common to all non-base
/// Fragment types and should generally be the first member of all
/// concrete fragments.
#[derive(Clone, Debug, MallocSizeOf)]
pub(crate) struct BaseFragment {
/// A tag which identifies the DOM node and pseudo element of this
/// Fragment's content. If this fragment is for an anonymous box,
/// the tag will be None.
pub tag: Option<Tag>,
/// Flags which various information about this fragment used during
/// layout.
pub flags: FragmentFlags,
}
impl BaseFragment {
pub(crate) fn anonymous() -> Self {
BaseFragment {
tag: None,
flags: FragmentFlags::empty(),
}
}
/// Returns true if this fragment is non-anonymous and it is for the given
/// OpaqueNode, regardless of the pseudo element.
pub(crate) fn is_for_node(&self, node: OpaqueNode) -> bool {
self.tag.map(|tag| tag.node == node).unwrap_or(false)
}
}
/// Information necessary to construct a new BaseFragment.
#[derive(Clone, Copy, Debug, MallocSizeOf)]
pub(crate) struct BaseFragmentInfo {
/// The tag to use for the new BaseFragment, if it is not an anonymous Fragment.
pub tag: Option<Tag>,
/// The flags to use for the new BaseFragment.
pub flags: FragmentFlags,
}
impl BaseFragmentInfo {
pub(crate) fn new_for_node(node: OpaqueNode) -> Self {
Self {
tag: Some(Tag::new(node)),
flags: FragmentFlags::empty(),
}
}
pub(crate) fn anonymous() -> Self {
Self {
tag: None,
flags: FragmentFlags::empty(),
}
}
}
impl From<BaseFragmentInfo> for BaseFragment {
fn from(info: BaseFragmentInfo) -> Self {
Self {
tag: info.tag,
flags: info.flags,
}
}
}
bitflags! {
/// Flags used to track various information about a DOM node during layout.
#[derive(Clone, Copy, Debug)]
pub(crate) struct FragmentFlags: u16 {
/// Whether or not the node that created this fragment is a `<body>` element on an HTML document.
const IS_BODY_ELEMENT_OF_HTML_ELEMENT_ROOT = 1 << 0;
/// Whether or not the node that created this Fragment is a `<br>` element.
const IS_BR_ELEMENT = 1 << 1;
/// Whether or not the node that created this Fragment is a `<input>` or `<textarea>` element.
const IS_TEXT_CONTROL = 1 << 2;
/// Whether or not this Fragment is a flex item or a grid item.
const IS_FLEX_OR_GRID_ITEM = 1 << 3;
/// Whether or not this Fragment was created to contain a replaced element or is
/// a replaced element.
const IS_REPLACED = 1 << 4;
/// Whether or not the node that created was a `<table>`, `<th>` or
/// `<td>` element. Note that this does *not* include elements with
/// `display: table` or `display: table-cell`.
const IS_TABLE_TH_OR_TD_ELEMENT = 1 << 5;
/// Whether or not this Fragment was created to contain a list item marker
/// with a used value of `list-style-position: outside`.
const IS_OUTSIDE_LIST_ITEM_MARKER = 1 << 6;
/// Avoid painting the borders, backgrounds, and drop shadow for this fragment, this is used
/// for empty table cells when 'empty-cells' is 'hide' and also table wrappers. This flag
/// doesn't avoid hit-testing nor does it prevent the painting outlines.
const DO_NOT_PAINT = 1 << 7;
/// Whether or not the size of this fragment depends on the block size of its container
/// and the fragment can be a flex item. This flag is used to cache items during flex
/// layout.
const SIZE_DEPENDS_ON_BLOCK_CONSTRAINTS_AND_CAN_BE_CHILD_OF_FLEX_ITEM = 1 << 8;
/// Whether or not the node that created this fragment is the root element.
const IS_ROOT_ELEMENT = 1 << 9;
}
}
malloc_size_of_is_0!(FragmentFlags);
/// A data structure used to hold DOM and pseudo-element information about
/// a particular layout object.
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq)]
pub(crate) struct Tag {
pub(crate) node: OpaqueNode,
pub(crate) pseudo: Option<PseudoElement>,
}
impl Tag {
/// Create a new Tag for a non-pseudo element. This is mainly used for
/// matching existing tags, since it does not accept an `info` argument.
pub(crate) fn new(node: OpaqueNode) -> Self {
Tag { node, pseudo: None }
}
/// Create a new Tag for a pseudo element. This is mainly used for
/// matching existing tags, since it does not accept an `info` argument.
pub(crate) fn new_pseudo(node: OpaqueNode, pseudo: Option<PseudoElement>) -> Self {
Tag { node, pseudo }
}
/// Returns true if this tag is for a pseudo element.
pub(crate) fn is_pseudo(&self) -> bool {
self.pseudo.is_some()
}
pub(crate) fn to_display_list_fragment_id(self) -> u64 {
combine_id_with_fragment_type(self.node.id(), self.pseudo.into())
}
}

View file

@ -0,0 +1,397 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 app_units::Au;
use atomic_refcell::AtomicRefCell;
use base::print_tree::PrintTree;
use malloc_size_of_derive::MallocSizeOf;
use servo_arc::Arc as ServoArc;
use style::Zero;
use style::computed_values::border_collapse::T as BorderCollapse;
use style::computed_values::overflow_x::T as ComputedOverflow;
use style::computed_values::position::T as ComputedPosition;
use style::logical_geometry::WritingMode;
use style::properties::ComputedValues;
use style::values::specified::box_::DisplayOutside;
use super::{BaseFragment, BaseFragmentInfo, CollapsedBlockMargins, Fragment};
use crate::formatting_contexts::Baselines;
use crate::geom::{
AuOrAuto, LengthPercentageOrAuto, PhysicalPoint, PhysicalRect, PhysicalSides, ToLogical,
};
use crate::style_ext::ComputedValuesExt;
use crate::table::SpecificTableGridInfo;
use crate::taffy::SpecificTaffyGridInfo;
/// Describes how a [`BoxFragment`] paints its background.
#[derive(MallocSizeOf)]
pub(crate) enum BackgroundMode {
/// Draw the normal [`BoxFragment`] background as well as the extra backgrounds
/// based on the style and positioning rectangles in this data structure.
Extra(Vec<ExtraBackground>),
/// Do not draw a background for this Fragment. This is used for elements like
/// table tracks and table track groups, which rely on cells to paint their
/// backgrounds.
None,
/// Draw the background normally, getting information from the Fragment style.
Normal,
}
#[derive(MallocSizeOf)]
pub(crate) struct ExtraBackground {
#[conditional_malloc_size_of]
pub style: ServoArc<ComputedValues>,
pub rect: PhysicalRect<Au>,
}
#[derive(Clone, Debug, MallocSizeOf)]
pub(crate) enum SpecificLayoutInfo {
Grid(Box<SpecificTaffyGridInfo>),
TableCellWithCollapsedBorders,
TableGridWithCollapsedBorders(Box<SpecificTableGridInfo>),
TableWrapper,
}
#[derive(MallocSizeOf)]
pub(crate) struct BoxFragment {
pub base: BaseFragment,
#[conditional_malloc_size_of]
pub style: ServoArc<ComputedValues>,
pub children: Vec<Fragment>,
/// The content rect of this fragment in the parent fragment's content rectangle. This
/// does not include padding, border, or margin -- it only includes content.
pub content_rect: PhysicalRect<Au>,
pub padding: PhysicalSides<Au>,
pub border: PhysicalSides<Au>,
pub margin: PhysicalSides<Au>,
/// When the `clear` property is not set to `none`, it may introduce clearance.
/// Clearance is some extra spacing that is added above the top margin,
/// so that the element doesn't overlap earlier floats in the same BFC.
/// The presence of clearance prevents the top margin from collapsing with
/// earlier margins or with the bottom margin of the parent block.
/// <https://drafts.csswg.org/css2/#clearance>
pub clearance: Option<Au>,
/// When this [`BoxFragment`] is for content that has a baseline, this tracks
/// the first and last baselines of that content. This is used to propagate baselines
/// to things such as tables and inline formatting contexts.
baselines: Baselines,
block_margins_collapsed_with_children: Option<Box<CollapsedBlockMargins>>,
/// The scrollable overflow of this box fragment.
pub scrollable_overflow_from_children: PhysicalRect<Au>,
/// The resolved box insets if this box is `position: sticky`. These are calculated
/// during stacking context tree construction because they rely on the size of the
/// scroll container.
pub(crate) resolved_sticky_insets: AtomicRefCell<Option<PhysicalSides<AuOrAuto>>>,
pub background_mode: BackgroundMode,
/// Additional information of from layout that could be used by Javascripts and devtools.
pub specific_layout_info: Option<SpecificLayoutInfo>,
}
impl BoxFragment {
#[allow(clippy::too_many_arguments)]
pub fn new(
base_fragment_info: BaseFragmentInfo,
style: ServoArc<ComputedValues>,
children: Vec<Fragment>,
content_rect: PhysicalRect<Au>,
padding: PhysicalSides<Au>,
border: PhysicalSides<Au>,
margin: PhysicalSides<Au>,
clearance: Option<Au>,
) -> BoxFragment {
let scrollable_overflow_from_children =
children.iter().fold(PhysicalRect::zero(), |acc, child| {
acc.union(&child.scrollable_overflow())
});
BoxFragment {
base: base_fragment_info.into(),
style,
children,
content_rect,
padding,
border,
margin,
clearance,
baselines: Baselines::default(),
block_margins_collapsed_with_children: None,
scrollable_overflow_from_children,
resolved_sticky_insets: AtomicRefCell::default(),
background_mode: BackgroundMode::Normal,
specific_layout_info: None,
}
}
pub fn with_baselines(mut self, baselines: Baselines) -> Self {
self.baselines = baselines;
self
}
/// Get the baselines for this [`BoxFragment`] if they are compatible with the given [`WritingMode`].
/// If they are not compatible, [`Baselines::default()`] is returned.
pub fn baselines(&self, writing_mode: WritingMode) -> Baselines {
let mut baselines =
if writing_mode.is_horizontal() == self.style.writing_mode.is_horizontal() {
self.baselines
} else {
// If the writing mode of the container requesting baselines is not
// compatible, ensure that the baselines established by this fragment are
// not used.
Baselines::default()
};
// From the https://drafts.csswg.org/css-align-3/#baseline-export section on "block containers":
// > However, for legacy reasons if its baseline-source is auto (the initial
// > value) a block-level or inline-level block container that is a scroll container
// > always has a last baseline set, whose baselines all correspond to its block-end
// > margin edge.
//
// This applies even if there is no baseline set, so we unconditionally set the value here
// and ignore anything that is set via [`Self::with_baselines`].
if self.style.establishes_scroll_container(self.base.flags) {
let content_rect_size = self.content_rect.size.to_logical(writing_mode);
let padding = self.padding.to_logical(writing_mode);
let border = self.border.to_logical(writing_mode);
let margin = self.margin.to_logical(writing_mode);
baselines.last = Some(
content_rect_size.block + padding.block_end + border.block_end + margin.block_end,
)
}
baselines
}
pub fn add_extra_background(&mut self, extra_background: ExtraBackground) {
match self.background_mode {
BackgroundMode::Extra(ref mut backgrounds) => backgrounds.push(extra_background),
_ => self.background_mode = BackgroundMode::Extra(vec![extra_background]),
}
}
pub fn set_does_not_paint_background(&mut self) {
self.background_mode = BackgroundMode::None;
}
pub fn with_specific_layout_info(mut self, info: Option<SpecificLayoutInfo>) -> Self {
self.specific_layout_info = info;
self
}
pub fn with_block_margins_collapsed_with_children(
mut self,
collapsed_margins: CollapsedBlockMargins,
) -> Self {
self.block_margins_collapsed_with_children = Some(collapsed_margins.into());
self
}
pub fn scrollable_overflow(&self) -> PhysicalRect<Au> {
let physical_padding_rect = self.padding_rect();
let content_origin = self.content_rect.origin.to_vector();
physical_padding_rect.union(
&self
.scrollable_overflow_from_children
.translate(content_origin),
)
}
pub(crate) fn padding_rect(&self) -> PhysicalRect<Au> {
self.content_rect.outer_rect(self.padding)
}
pub(crate) fn border_rect(&self) -> PhysicalRect<Au> {
self.padding_rect().outer_rect(self.border)
}
pub(crate) fn margin_rect(&self) -> PhysicalRect<Au> {
self.border_rect().outer_rect(self.margin)
}
pub(crate) fn padding_border_margin(&self) -> PhysicalSides<Au> {
self.margin + self.border + self.padding
}
pub fn print(&self, tree: &mut PrintTree) {
tree.new_level(format!(
"Box\
\nbase={:?}\
\ncontent={:?}\
\npadding rect={:?}\
\nborder rect={:?}\
\nmargin={:?}\
\nclearance={:?}\
\nscrollable_overflow={:?}\
\nbaselines={:?}\
\noverflow={:?}",
self.base,
self.content_rect,
self.padding_rect(),
self.border_rect(),
self.margin,
self.clearance,
self.scrollable_overflow(),
self.baselines,
self.style.effective_overflow(self.base.flags),
));
for child in &self.children {
child.print(tree);
}
tree.end_level();
}
pub fn scrollable_overflow_for_parent(&self) -> PhysicalRect<Au> {
let mut overflow = self.border_rect();
if self.style.establishes_scroll_container(self.base.flags) {
return overflow;
}
// https://www.w3.org/TR/css-overflow-3/#scrollable
// Only include the scrollable overflow of a child box if it has overflow: visible.
let scrollable_overflow = self.scrollable_overflow();
let bottom_right = PhysicalPoint::new(
overflow.max_x().max(scrollable_overflow.max_x()),
overflow.max_y().max(scrollable_overflow.max_y()),
);
let overflow_style = self.style.effective_overflow(self.base.flags);
if overflow_style.y == ComputedOverflow::Visible {
overflow.origin.y = overflow.origin.y.min(scrollable_overflow.origin.y);
overflow.size.height = bottom_right.y - overflow.origin.y;
}
if overflow_style.x == ComputedOverflow::Visible {
overflow.origin.x = overflow.origin.x.min(scrollable_overflow.origin.x);
overflow.size.width = bottom_right.x - overflow.origin.x;
}
overflow
}
pub(crate) fn calculate_resolved_insets_if_positioned(
&self,
containing_block: &PhysicalRect<Au>,
) -> PhysicalSides<AuOrAuto> {
let position = self.style.get_box().position;
debug_assert_ne!(
position,
ComputedPosition::Static,
"Should not call this method on statically positioned box."
);
if let Some(resolved_sticky_insets) = *self.resolved_sticky_insets.borrow() {
return resolved_sticky_insets;
}
let convert_to_au_or_auto = |sides: PhysicalSides<Au>| {
PhysicalSides::new(
AuOrAuto::LengthPercentage(sides.top),
AuOrAuto::LengthPercentage(sides.right),
AuOrAuto::LengthPercentage(sides.bottom),
AuOrAuto::LengthPercentage(sides.left),
)
};
// "A resolved value special case property like top defined in another
// specification If the property applies to a positioned element and the
// resolved value of the display property is not none or contents, and
// the property is not over-constrained, then the resolved value is the
// used value. Otherwise the resolved value is the computed value."
// https://drafts.csswg.org/cssom/#resolved-values
let insets = self.style.physical_box_offsets();
let (cb_width, cb_height) = (containing_block.width(), containing_block.height());
if position == ComputedPosition::Relative {
let get_resolved_axis = |start: &LengthPercentageOrAuto,
end: &LengthPercentageOrAuto,
container_length: Au| {
let start = start.map(|value| value.to_used_value(container_length));
let end = end.map(|value| value.to_used_value(container_length));
match (start.non_auto(), end.non_auto()) {
(None, None) => (Au::zero(), Au::zero()),
(None, Some(end)) => (-end, end),
(Some(start), None) => (start, -start),
// This is the overconstrained case, for which the resolved insets will
// simply be the computed insets.
(Some(start), Some(end)) => (start, end),
}
};
let (left, right) = get_resolved_axis(&insets.left, &insets.right, cb_width);
let (top, bottom) = get_resolved_axis(&insets.top, &insets.bottom, cb_height);
return convert_to_au_or_auto(PhysicalSides::new(top, right, bottom, left));
}
debug_assert!(
position == ComputedPosition::Fixed || position == ComputedPosition::Absolute
);
let margin_rect = self.margin_rect();
let (top, bottom) = match (&insets.top, &insets.bottom) {
(
LengthPercentageOrAuto::LengthPercentage(top),
LengthPercentageOrAuto::LengthPercentage(bottom),
) => (
top.to_used_value(cb_height),
bottom.to_used_value(cb_height),
),
_ => (margin_rect.origin.y, cb_height - margin_rect.max_y()),
};
let (left, right) = match (&insets.left, &insets.right) {
(
LengthPercentageOrAuto::LengthPercentage(left),
LengthPercentageOrAuto::LengthPercentage(right),
) => (left.to_used_value(cb_width), right.to_used_value(cb_width)),
_ => (margin_rect.origin.x, cb_width - margin_rect.max_x()),
};
convert_to_au_or_auto(PhysicalSides::new(top, right, bottom, left))
}
/// Whether this is a non-replaced inline-level box whose inner display type is `flow`.
/// <https://drafts.csswg.org/css-display-3/#inline-box>
pub(crate) fn is_inline_box(&self) -> bool {
self.style.is_inline_box(self.base.flags)
}
/// Whether this is an atomic inline-level box.
/// <https://drafts.csswg.org/css-display-3/#atomic-inline>
pub(crate) fn is_atomic_inline_level(&self) -> bool {
self.style.get_box().display.outside() == DisplayOutside::Inline && !self.is_inline_box()
}
/// Whether this is a table wrapper box.
/// <https://www.w3.org/TR/css-tables-3/#table-wrapper-box>
pub(crate) fn is_table_wrapper(&self) -> bool {
matches!(
self.specific_layout_info,
Some(SpecificLayoutInfo::TableWrapper)
)
}
pub(crate) fn has_collapsed_borders(&self) -> bool {
match &self.specific_layout_info {
Some(SpecificLayoutInfo::TableCellWithCollapsedBorders) => true,
Some(SpecificLayoutInfo::TableGridWithCollapsedBorders(_)) => true,
Some(SpecificLayoutInfo::TableWrapper) => {
self.style.get_inherited_table().border_collapse == BorderCollapse::Collapse
},
_ => false,
}
}
pub(crate) fn block_margins_collapsed_with_children(&self) -> CollapsedBlockMargins {
match self.block_margins_collapsed_with_children.as_ref() {
Some(collapsed_block_margins) => *(collapsed_block_margins).clone(),
_ => CollapsedBlockMargins::zero(),
}
}
}

View file

@ -0,0 +1,101 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 style::computed_values::position::T as ComputedPosition;
use crate::fragment_tree::Fragment;
/// A data structure used to track the containing block when recursing
/// through the Fragment tree. It tracks the three types of containing
/// blocks (for all descendants, for absolute and fixed position
/// descendants, and for fixed position descendants).
pub(crate) struct ContainingBlockManager<'a, T> {
/// The containing block for all non-absolute descendants. "...if the element's
/// position is 'relative' or 'static', the containing block is formed by the
/// content edge of the nearest block container ancestor box." This is also
/// the case for 'position: sticky' elements.
/// <https://www.w3.org/TR/CSS2/visudet.html#containing-block-details>
pub for_non_absolute_descendants: &'a T,
/// The containing block for absolute descendants. "If the element has
/// 'position: absolute', the containing block is
/// established by the nearest ancestor with a 'position' of 'absolute',
/// 'relative' or 'fixed', in the following way:
/// 1. In the case that the ancestor is an inline element, the containing
/// block is the bounding box around the padding boxes of the first and the
/// last inline boxes generated for that element. In CSS 2.1, if the inline
/// element is split across multiple lines, the containing block is
/// undefined.
/// 2. Otherwise, the containing block is formed by the padding edge of the
/// ancestor.
///
/// <https://www.w3.org/TR/CSS2/visudet.html#containing-block-details>
/// If the ancestor forms a containing block for all descendants (see below),
/// this value will be None and absolute descendants will use the containing
/// block for fixed descendants.
pub for_absolute_descendants: Option<&'a T>,
/// The containing block for fixed and absolute descendants.
/// "For elements whose layout is governed by the CSS box model, any value
/// other than none for the transform property also causes the element to
/// establish a containing block for all descendants. Its padding box will be
/// used to layout for all of its absolute-position descendants,
/// fixed-position descendants, and descendant fixed background attachments."
/// <https://w3c.github.io/csswg-drafts/css-transforms-1/#containing-block-for-all-descendants>
/// See `ComputedValues::establishes_containing_block_for_all_descendants`
/// for a list of conditions where an element forms a containing block for
/// all descendants.
pub for_absolute_and_fixed_descendants: &'a T,
}
impl<'a, T> ContainingBlockManager<'a, T> {
pub(crate) fn get_containing_block_for_fragment(&self, fragment: &Fragment) -> &T {
if let Fragment::Box(box_fragment) = fragment {
match box_fragment.borrow().style.clone_position() {
ComputedPosition::Fixed => self.for_absolute_and_fixed_descendants,
ComputedPosition::Absolute => self
.for_absolute_descendants
.unwrap_or(self.for_absolute_and_fixed_descendants),
_ => self.for_non_absolute_descendants,
}
} else {
self.for_non_absolute_descendants
}
}
pub(crate) fn new_for_non_absolute_descendants(
&self,
for_non_absolute_descendants: &'a T,
) -> Self {
ContainingBlockManager {
for_non_absolute_descendants,
for_absolute_descendants: self.for_absolute_descendants,
for_absolute_and_fixed_descendants: self.for_absolute_and_fixed_descendants,
}
}
pub(crate) fn new_for_absolute_descendants(
&self,
for_non_absolute_descendants: &'a T,
for_absolute_descendants: &'a T,
) -> Self {
ContainingBlockManager {
for_non_absolute_descendants,
for_absolute_descendants: Some(for_absolute_descendants),
for_absolute_and_fixed_descendants: self.for_absolute_and_fixed_descendants,
}
}
pub(crate) fn new_for_absolute_and_fixed_descendants(
&self,
for_non_absolute_descendants: &'a T,
for_absolute_and_fixed_descendants: &'a T,
) -> Self {
ContainingBlockManager {
for_non_absolute_descendants,
for_absolute_descendants: None,
for_absolute_and_fixed_descendants,
}
}
}

View file

@ -0,0 +1,309 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 std::sync::Arc;
use app_units::Au;
use base::id::PipelineId;
use base::print_tree::PrintTree;
use fonts::{ByteIndex, FontMetrics, GlyphStore};
use malloc_size_of_derive::MallocSizeOf;
use range::Range as ServoRange;
use servo_arc::Arc as ServoArc;
use style::Zero;
use style::properties::ComputedValues;
use style::values::specified::text::TextDecorationLine;
use webrender_api::{FontInstanceKey, ImageKey};
use super::{
BaseFragment, BoxFragment, ContainingBlockManager, HoistedSharedFragment, PositioningFragment,
Tag,
};
use crate::cell::ArcRefCell;
use crate::geom::{LogicalSides, PhysicalRect};
use crate::style_ext::ComputedValuesExt;
#[derive(Clone, MallocSizeOf)]
pub(crate) enum Fragment {
Box(ArcRefCell<BoxFragment>),
/// Floating content. A floated fragment is very similar to a normal
/// [BoxFragment] but it isn't positioned using normal in block flow
/// positioning rules (margin collapse, etc). Instead, they are laid
/// out by the [crate::flow::float::SequentialLayoutState] of their
/// float containing block formatting context.
Float(ArcRefCell<BoxFragment>),
Positioning(ArcRefCell<PositioningFragment>),
/// Absolute and fixed position fragments are hoisted up so that they
/// are children of the BoxFragment that establishes their containing
/// blocks, so that they can be laid out properly. When this happens
/// an `AbsoluteOrFixedPositioned` fragment is left at the original tree
/// position. This allows these hoisted fragments to be painted with
/// regard to their original tree order during stacking context tree /
/// display list construction.
AbsoluteOrFixedPositioned(ArcRefCell<HoistedSharedFragment>),
Text(ArcRefCell<TextFragment>),
Image(ArcRefCell<ImageFragment>),
IFrame(ArcRefCell<IFrameFragment>),
}
#[derive(Clone, MallocSizeOf)]
pub(crate) struct CollapsedBlockMargins {
pub collapsed_through: bool,
pub start: CollapsedMargin,
pub end: CollapsedMargin,
}
#[derive(Clone, Copy, Debug, MallocSizeOf)]
pub(crate) struct CollapsedMargin {
max_positive: Au,
min_negative: Au,
}
#[derive(MallocSizeOf)]
pub(crate) struct TextFragment {
pub base: BaseFragment,
#[conditional_malloc_size_of]
pub parent_style: ServoArc<ComputedValues>,
pub rect: PhysicalRect<Au>,
pub font_metrics: FontMetrics,
pub font_key: FontInstanceKey,
#[conditional_malloc_size_of]
pub glyphs: Vec<Arc<GlyphStore>>,
/// A flag that represents the _used_ value of the text-decoration property.
pub text_decoration_line: TextDecorationLine,
/// Extra space to add for each justification opportunity.
pub justification_adjustment: Au,
pub selection_range: Option<ServoRange<ByteIndex>>,
#[conditional_malloc_size_of]
pub selected_style: ServoArc<ComputedValues>,
}
#[derive(MallocSizeOf)]
pub(crate) struct ImageFragment {
pub base: BaseFragment,
#[conditional_malloc_size_of]
pub style: ServoArc<ComputedValues>,
pub rect: PhysicalRect<Au>,
pub clip: PhysicalRect<Au>,
pub image_key: Option<ImageKey>,
}
#[derive(MallocSizeOf)]
pub(crate) struct IFrameFragment {
pub base: BaseFragment,
pub pipeline_id: PipelineId,
pub rect: PhysicalRect<Au>,
#[conditional_malloc_size_of]
pub style: ServoArc<ComputedValues>,
}
impl Fragment {
pub fn base(&self) -> Option<BaseFragment> {
Some(match self {
Fragment::Box(fragment) => fragment.borrow().base.clone(),
Fragment::Text(fragment) => fragment.borrow().base.clone(),
Fragment::AbsoluteOrFixedPositioned(_) => return None,
Fragment::Positioning(fragment) => fragment.borrow().base.clone(),
Fragment::Image(fragment) => fragment.borrow().base.clone(),
Fragment::IFrame(fragment) => fragment.borrow().base.clone(),
Fragment::Float(fragment) => fragment.borrow().base.clone(),
})
}
pub(crate) fn mutate_content_rect(&mut self, callback: impl FnOnce(&mut PhysicalRect<Au>)) {
match self {
Fragment::Box(box_fragment) | Fragment::Float(box_fragment) => {
callback(&mut box_fragment.borrow_mut().content_rect)
},
Fragment::Positioning(_) | Fragment::AbsoluteOrFixedPositioned(_) => {},
Fragment::Text(text_fragment) => callback(&mut text_fragment.borrow_mut().rect),
Fragment::Image(image_fragment) => callback(&mut image_fragment.borrow_mut().rect),
Fragment::IFrame(iframe_fragment) => callback(&mut iframe_fragment.borrow_mut().rect),
}
}
pub fn tag(&self) -> Option<Tag> {
self.base().and_then(|base| base.tag)
}
pub fn print(&self, tree: &mut PrintTree) {
match self {
Fragment::Box(fragment) => fragment.borrow().print(tree),
Fragment::Float(fragment) => {
tree.new_level("Float".to_string());
fragment.borrow().print(tree);
tree.end_level();
},
Fragment::AbsoluteOrFixedPositioned(_) => {
tree.add_item("AbsoluteOrFixedPositioned".to_string());
},
Fragment::Positioning(fragment) => fragment.borrow().print(tree),
Fragment::Text(fragment) => fragment.borrow().print(tree),
Fragment::Image(fragment) => fragment.borrow().print(tree),
Fragment::IFrame(fragment) => fragment.borrow().print(tree),
}
}
pub fn scrolling_area(&self, containing_block: &PhysicalRect<Au>) -> PhysicalRect<Au> {
match self {
Fragment::Box(fragment) | Fragment::Float(fragment) => fragment
.borrow()
.scrollable_overflow()
.translate(containing_block.origin.to_vector()),
_ => self.scrollable_overflow(),
}
}
pub fn scrollable_overflow(&self) -> PhysicalRect<Au> {
match self {
Fragment::Box(fragment) | Fragment::Float(fragment) => {
fragment.borrow().scrollable_overflow_for_parent()
},
Fragment::AbsoluteOrFixedPositioned(_) => PhysicalRect::zero(),
Fragment::Positioning(fragment) => fragment.borrow().scrollable_overflow,
Fragment::Text(fragment) => fragment.borrow().rect,
Fragment::Image(fragment) => fragment.borrow().rect,
Fragment::IFrame(fragment) => fragment.borrow().rect,
}
}
pub(crate) fn find<T>(
&self,
manager: &ContainingBlockManager<PhysicalRect<Au>>,
level: usize,
process_func: &mut impl FnMut(&Fragment, usize, &PhysicalRect<Au>) -> Option<T>,
) -> Option<T> {
let containing_block = manager.get_containing_block_for_fragment(self);
if let Some(result) = process_func(self, level, containing_block) {
return Some(result);
}
match self {
Fragment::Box(fragment) | Fragment::Float(fragment) => {
let fragment = fragment.borrow();
let content_rect = fragment
.content_rect
.translate(containing_block.origin.to_vector());
let padding_rect = fragment
.padding_rect()
.translate(containing_block.origin.to_vector());
let new_manager = if fragment
.style
.establishes_containing_block_for_all_descendants(fragment.base.flags)
{
manager.new_for_absolute_and_fixed_descendants(&content_rect, &padding_rect)
} else if fragment
.style
.establishes_containing_block_for_absolute_descendants(fragment.base.flags)
{
manager.new_for_absolute_descendants(&content_rect, &padding_rect)
} else {
manager.new_for_non_absolute_descendants(&content_rect)
};
fragment
.children
.iter()
.find_map(|child| child.find(&new_manager, level + 1, process_func))
},
Fragment::Positioning(fragment) => {
let fragment = fragment.borrow();
let content_rect = fragment.rect.translate(containing_block.origin.to_vector());
let new_manager = manager.new_for_non_absolute_descendants(&content_rect);
fragment
.children
.iter()
.find_map(|child| child.find(&new_manager, level + 1, process_func))
},
_ => None,
}
}
}
impl TextFragment {
pub fn print(&self, tree: &mut PrintTree) {
tree.add_item(format!(
"Text num_glyphs={} box={:?}",
self.glyphs
.iter()
.map(|glyph_store| glyph_store.len().0)
.sum::<isize>(),
self.rect,
));
}
pub fn has_selection(&self) -> bool {
self.selection_range.is_some()
}
}
impl ImageFragment {
pub fn print(&self, tree: &mut PrintTree) {
tree.add_item(format!(
"Image\
\nrect={:?}",
self.rect
));
}
}
impl IFrameFragment {
pub fn print(&self, tree: &mut PrintTree) {
tree.add_item(format!(
"IFrame\
\npipeline={:?} rect={:?}",
self.pipeline_id, self.rect
));
}
}
impl CollapsedBlockMargins {
pub fn from_margin(margin: &LogicalSides<Au>) -> Self {
Self {
collapsed_through: false,
start: CollapsedMargin::new(margin.block_start),
end: CollapsedMargin::new(margin.block_end),
}
}
pub fn zero() -> Self {
Self {
collapsed_through: false,
start: CollapsedMargin::zero(),
end: CollapsedMargin::zero(),
}
}
}
impl CollapsedMargin {
pub fn zero() -> Self {
Self {
max_positive: Au::zero(),
min_negative: Au::zero(),
}
}
pub fn new(margin: Au) -> Self {
Self {
max_positive: margin.max(Au::zero()),
min_negative: margin.min(Au::zero()),
}
}
pub fn adjoin(&self, other: &Self) -> Self {
Self {
max_positive: self.max_positive.max(other.max_positive),
min_negative: self.min_negative.min(other.min_negative),
}
}
pub fn adjoin_assign(&mut self, other: &Self) {
*self = self.adjoin(other);
}
pub fn solve(&self) -> Au {
self.max_positive + self.min_negative
}
}

View file

@ -0,0 +1,194 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 app_units::Au;
use base::print_tree::PrintTree;
use compositing_traits::display_list::AxesScrollSensitivity;
use euclid::default::{Point2D, Rect, Size2D};
use fxhash::FxHashSet;
use malloc_size_of_derive::MallocSizeOf;
use style::animation::AnimationSetKey;
use style::dom::OpaqueNode;
use webrender_api::units;
use super::{ContainingBlockManager, Fragment, Tag};
use crate::display_list::StackingContext;
use crate::flow::CanvasBackground;
use crate::geom::{PhysicalPoint, PhysicalRect};
#[derive(MallocSizeOf)]
pub struct FragmentTree {
/// Fragments at the top-level of the tree.
///
/// If the root element has `display: none`, there are zero fragments.
/// Otherwise, there is at least one:
///
/// * The first fragment is generated by the root element.
/// * There may be additional fragments generated by positioned boxes
/// that have the initial containing block.
pub(crate) root_fragments: Vec<Fragment>,
/// The scrollable overflow rectangle for the entire tree
/// <https://drafts.csswg.org/css-overflow/#scrollable>
pub(crate) scrollable_overflow: PhysicalRect<Au>,
/// The containing block used in the layout of this fragment tree.
pub(crate) initial_containing_block: PhysicalRect<Au>,
/// <https://drafts.csswg.org/css-backgrounds/#special-backgrounds>
pub(crate) canvas_background: CanvasBackground,
/// Whether or not the viewport is sensitive to scroll input events.
pub viewport_scroll_sensitivity: AxesScrollSensitivity,
}
impl FragmentTree {
pub(crate) fn build_display_list(
&self,
builder: &mut crate::display_list::DisplayListBuilder,
root_stacking_context: &StackingContext,
) {
// Paint the canvas background (if any) before/under everything else
root_stacking_context.build_canvas_background_display_list(
builder,
self,
&self.initial_containing_block,
);
root_stacking_context.build_display_list(builder);
}
pub fn print(&self) {
let mut print_tree = PrintTree::new("Fragment Tree".to_string());
for fragment in &self.root_fragments {
fragment.print(&mut print_tree);
}
}
pub fn scrollable_overflow(&self) -> units::LayoutSize {
units::LayoutSize::from_untyped(Size2D::new(
self.scrollable_overflow.size.width.to_f32_px(),
self.scrollable_overflow.size.height.to_f32_px(),
))
}
pub(crate) fn find<T>(
&self,
mut process_func: impl FnMut(&Fragment, usize, &PhysicalRect<Au>) -> Option<T>,
) -> Option<T> {
let info = ContainingBlockManager {
for_non_absolute_descendants: &self.initial_containing_block,
for_absolute_descendants: None,
for_absolute_and_fixed_descendants: &self.initial_containing_block,
};
self.root_fragments
.iter()
.find_map(|child| child.find(&info, 0, &mut process_func))
}
pub fn remove_nodes_in_fragment_tree_from_set(&self, set: &mut FxHashSet<AnimationSetKey>) {
self.find(|fragment, _, _| {
let tag = fragment.tag()?;
set.remove(&AnimationSetKey::new(tag.node, tag.pseudo));
None::<()>
});
}
/// Get the vector of rectangles that surrounds the fragments of the node with the given address.
/// This function answers the `getClientRects()` query and the union of the rectangles answers
/// the `getBoundingClientRect()` query.
///
/// TODO: This function is supposed to handle scroll offsets, but that isn't happening at all.
pub fn get_content_boxes_for_node(&self, requested_node: OpaqueNode) -> Vec<Rect<Au>> {
let mut content_boxes = Vec::new();
let tag_to_find = Tag::new(requested_node);
self.find(|fragment, _, containing_block| {
if fragment.tag() != Some(tag_to_find) {
return None::<()>;
}
let fragment_relative_rect = match fragment {
Fragment::Box(fragment) | Fragment::Float(fragment) => {
fragment.borrow().border_rect()
},
Fragment::Positioning(fragment) => fragment.borrow().rect,
Fragment::Text(fragment) => fragment.borrow().rect,
Fragment::AbsoluteOrFixedPositioned(_) |
Fragment::Image(_) |
Fragment::IFrame(_) => return None,
};
let rect = fragment_relative_rect.translate(containing_block.origin.to_vector());
content_boxes.push(rect.to_untyped());
None::<()>
});
content_boxes
}
pub fn get_border_dimensions_for_node(&self, requested_node: OpaqueNode) -> Rect<i32> {
let tag_to_find = Tag::new(requested_node);
self.find(|fragment, _, _containing_block| {
if fragment.tag() != Some(tag_to_find) {
return None;
}
let rect = match fragment {
Fragment::Box(fragment) | Fragment::Float(fragment) => {
// https://drafts.csswg.org/cssom-view/#dom-element-clienttop
// " If the element has no associated CSS layout box or if the
// CSS layout box is inline, return zero." For this check we
// also explicitly ignore the list item portion of the display
// style.
let fragment = fragment.borrow();
if fragment.is_inline_box() {
return Some(Rect::zero());
}
if fragment.is_table_wrapper() {
// For tables the border actually belongs to the table grid box,
// so we need to include it in the dimension of the table wrapper box.
let mut rect = fragment.border_rect();
rect.origin = PhysicalPoint::zero();
rect
} else {
let mut rect = fragment.padding_rect();
rect.origin = PhysicalPoint::new(fragment.border.left, fragment.border.top);
rect
}
},
Fragment::Positioning(fragment) => fragment.borrow().rect.cast_unit(),
Fragment::Text(text_fragment) => text_fragment.borrow().rect,
_ => return None,
};
let rect = Rect::new(
Point2D::new(rect.origin.x.to_f32_px(), rect.origin.y.to_f32_px()),
Size2D::new(rect.size.width.to_f32_px(), rect.size.height.to_f32_px()),
);
Some(rect.round().to_i32().to_untyped())
})
.unwrap_or_else(Rect::zero)
}
pub fn get_scrolling_area_for_viewport(&self) -> PhysicalRect<Au> {
let mut scroll_area = self.initial_containing_block;
for fragment in self.root_fragments.iter() {
scroll_area = fragment
.scrolling_area(&self.initial_containing_block)
.union(&scroll_area);
}
scroll_area
}
pub fn get_scrolling_area_for_node(&self, requested_node: OpaqueNode) -> PhysicalRect<Au> {
let tag_to_find = Tag::new(requested_node);
let scroll_area = self.find(|fragment, _, containing_block| {
if fragment.tag() == Some(tag_to_find) {
Some(fragment.scrolling_area(containing_block))
} else {
None
}
});
scroll_area.unwrap_or_else(PhysicalRect::<Au>::zero)
}
}

View file

@ -0,0 +1,57 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 app_units::Au;
use malloc_size_of_derive::MallocSizeOf;
use style::logical_geometry::WritingMode;
use style::values::specified::align::AlignFlags;
use super::Fragment;
use crate::geom::{LogicalVec2, PhysicalRect, PhysicalVec};
/// A reference to a Fragment which is shared between `HoistedAbsolutelyPositionedBox`
/// and its placeholder `AbsoluteOrFixedPositionedFragment` in the original tree position.
/// This will be used later in order to paint this hoisted box in tree order.
#[derive(MallocSizeOf)]
pub(crate) struct HoistedSharedFragment {
pub fragment: Option<Fragment>,
/// The "static-position rect" of this absolutely positioned box. This is defined by the
/// layout mode from which the box originates.
///
/// See <https://drafts.csswg.org/css-position-3/#staticpos-rect>
pub static_position_rect: PhysicalRect<Au>,
/// The resolved alignment values used for aligning this absolutely positioned element
/// if the "static-position rect" ends up being the "inset-modified containing block".
/// These values are dependent on the layout mode (currently only interesting for
/// flexbox).
pub resolved_alignment: LogicalVec2<AlignFlags>,
/// This is the [`WritingMode`] of the original parent of the element that created this
/// hoisted absolutely-positioned fragment. This helps to interpret the offset for
/// static positioning. If the writing mode is right-to-left or bottom-to-top, the static
/// offset needs to be adjusted by the absolutely positioned element's inline size.
pub original_parent_writing_mode: WritingMode,
}
impl HoistedSharedFragment {
pub(crate) fn new(
static_position_rect: PhysicalRect<Au>,
resolved_alignment: LogicalVec2<AlignFlags>,
original_parent_writing_mode: WritingMode,
) -> Self {
HoistedSharedFragment {
fragment: None,
static_position_rect,
resolved_alignment,
original_parent_writing_mode,
}
}
}
impl HoistedSharedFragment {
/// `inset: auto`-positioned elements do not know their precise position until after
/// they're hoisted. This lets us adjust auto values after the fact.
pub(crate) fn adjust_offsets(&mut self, offset: &PhysicalVec<Au>) {
self.static_position_rect = self.static_position_rect.translate(*offset);
}
}

View file

@ -0,0 +1,20 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
mod base_fragment;
mod box_fragment;
mod containing_block;
mod fragment;
#[allow(clippy::module_inception)]
mod fragment_tree;
mod hoisted_shared_fragment;
mod positioning_fragment;
pub(crate) use base_fragment::*;
pub(crate) use box_fragment::*;
pub(crate) use containing_block::*;
pub(crate) use fragment::*;
pub use fragment_tree::*;
pub(crate) use hoisted_shared_fragment::*;
pub(crate) use positioning_fragment::*;

View file

@ -0,0 +1,81 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 app_units::Au;
use base::print_tree::PrintTree;
use malloc_size_of_derive::MallocSizeOf;
use servo_arc::Arc as ServoArc;
use style::properties::ComputedValues;
use super::{BaseFragment, BaseFragmentInfo, Fragment};
use crate::cell::ArcRefCell;
use crate::geom::PhysicalRect;
/// Can contain child fragments with relative coordinates, but does not contribute to painting
/// itself. [`PositioningFragment`]s may be completely anonymous, or just non-painting Fragments
/// generated by boxes.
#[derive(MallocSizeOf)]
pub(crate) struct PositioningFragment {
pub base: BaseFragment,
pub rect: PhysicalRect<Au>,
pub children: Vec<Fragment>,
/// The scrollable overflow of this anonymous fragment's children.
pub scrollable_overflow: PhysicalRect<Au>,
/// If this fragment was created with a style, the style of the fragment.
#[conditional_malloc_size_of]
pub style: Option<ServoArc<ComputedValues>>,
}
impl PositioningFragment {
pub fn new_anonymous(rect: PhysicalRect<Au>, children: Vec<Fragment>) -> ArcRefCell<Self> {
Self::new_with_base_fragment(BaseFragment::anonymous(), None, rect, children)
}
pub fn new_empty(
base_fragment_info: BaseFragmentInfo,
rect: PhysicalRect<Au>,
style: ServoArc<ComputedValues>,
) -> ArcRefCell<Self> {
Self::new_with_base_fragment(base_fragment_info.into(), Some(style), rect, Vec::new())
}
fn new_with_base_fragment(
base: BaseFragment,
style: Option<ServoArc<ComputedValues>>,
rect: PhysicalRect<Au>,
children: Vec<Fragment>,
) -> ArcRefCell<Self> {
let content_origin = rect.origin;
let scrollable_overflow = children.iter().fold(PhysicalRect::zero(), |acc, child| {
acc.union(
&child
.scrollable_overflow()
.translate(content_origin.to_vector()),
)
});
ArcRefCell::new(PositioningFragment {
base,
style,
rect,
children,
scrollable_overflow,
})
}
pub fn print(&self, tree: &mut PrintTree) {
tree.new_level(format!(
"PositioningFragment\
\nbase={:?}\
\nrect={:?}\
\nscrollable_overflow={:?}",
self.base, self.rect, self.scrollable_overflow
));
for child in &self.children {
child.print(tree);
}
tree.end_level();
}
}

1100
components/layout/geom.rs Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,142 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 std::fmt::{Debug, Formatter};
use app_units::Au;
use atomic_refcell::AtomicRefCell;
use malloc_size_of_derive::MallocSizeOf;
use servo_arc::Arc;
use style::properties::ComputedValues;
use crate::context::LayoutContext;
use crate::formatting_contexts::Baselines;
use crate::fragment_tree::{BaseFragmentInfo, CollapsedBlockMargins, Fragment, SpecificLayoutInfo};
use crate::geom::SizeConstraint;
use crate::positioned::PositioningContext;
use crate::sizing::{ComputeInlineContentSizes, InlineContentSizesResult};
use crate::{ConstraintSpace, ContainingBlockSize};
/// A box tree node that handles containing information about style and the original DOM
/// node or pseudo-element that it is based on. This also handles caching of layout values
/// such as the inline content sizes to avoid recalculating these values during layout
/// passes.
///
/// In the future, this will hold layout results to support incremental layout.
#[derive(MallocSizeOf)]
pub(crate) struct LayoutBoxBase {
pub base_fragment_info: BaseFragmentInfo,
#[conditional_malloc_size_of]
pub style: Arc<ComputedValues>,
pub cached_inline_content_size:
AtomicRefCell<Option<Box<(SizeConstraint, InlineContentSizesResult)>>>,
pub cached_layout_result: AtomicRefCell<Option<Box<CacheableLayoutResultAndInputs>>>,
pub fragments: AtomicRefCell<Vec<Fragment>>,
}
impl LayoutBoxBase {
pub(crate) fn new(base_fragment_info: BaseFragmentInfo, style: Arc<ComputedValues>) -> Self {
Self {
base_fragment_info,
style,
cached_inline_content_size: AtomicRefCell::default(),
cached_layout_result: AtomicRefCell::default(),
fragments: AtomicRefCell::default(),
}
}
/// Get the inline content sizes of a box tree node that extends this [`LayoutBoxBase`], fetch
/// the result from a cache when possible.
pub(crate) fn inline_content_sizes(
&self,
layout_context: &LayoutContext,
constraint_space: &ConstraintSpace,
layout_box: &impl ComputeInlineContentSizes,
) -> InlineContentSizesResult {
let mut cache = self.cached_inline_content_size.borrow_mut();
if let Some(cached_inline_content_size) = cache.as_ref() {
let (previous_cb_block_size, result) = **cached_inline_content_size;
if !result.depends_on_block_constraints ||
previous_cb_block_size == constraint_space.block_size
{
return result;
}
// TODO: Should we keep multiple caches for various block sizes?
}
let result =
layout_box.compute_inline_content_sizes_with_fixup(layout_context, constraint_space);
*cache = Some(Box::new((constraint_space.block_size, result)));
result
}
pub(crate) fn invalidate_cached_fragment(&self) {
let _ = self.cached_layout_result.borrow_mut().take();
}
pub(crate) fn fragments(&self) -> Vec<Fragment> {
self.fragments.borrow().clone()
}
pub(crate) fn add_fragment(&self, fragment: Fragment) {
self.fragments.borrow_mut().push(fragment);
}
pub(crate) fn set_fragment(&self, fragment: Fragment) {
*self.fragments.borrow_mut() = vec![fragment];
}
pub(crate) fn clear_fragments(&self) {
self.fragments.borrow_mut().clear();
}
}
impl Debug for LayoutBoxBase {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
f.debug_struct("LayoutBoxBase").finish()
}
}
#[derive(Clone, MallocSizeOf)]
pub(crate) struct CacheableLayoutResult {
pub fragments: Vec<Fragment>,
/// <https://drafts.csswg.org/css2/visudet.html#root-height>
pub content_block_size: Au,
/// If this layout is for a block container, this tracks the collapsable size
/// of start and end margins and whether or not the block container collapsed through.
pub collapsible_margins_in_children: CollapsedBlockMargins,
/// The contents of a table may force it to become wider than what we would expect
/// from 'width' and 'min-width'. This is the resulting inline content size,
/// or None for non-table layouts.
pub content_inline_size_for_table: Option<Au>,
/// The offset of the last inflow baseline of this layout in the content area, if
/// there was one. This is used to propagate baselines to the ancestors of `display:
/// inline-block`.
pub baselines: Baselines,
/// Whether or not this layout depends on the containing block size.
pub depends_on_block_constraints: bool,
/// Additional information of this layout that could be used by Javascripts and devtools.
pub specific_layout_info: Option<SpecificLayoutInfo>,
}
/// A collection of layout inputs and a cached layout result for a [`LayoutBoxBase`].
#[derive(MallocSizeOf)]
pub(crate) struct CacheableLayoutResultAndInputs {
/// The [`CacheableLayoutResult`] for this layout.
pub result: CacheableLayoutResult,
/// The [`ContainingBlockSize`] to use for this box's contents, but not
/// for the box itself.
pub containing_block_for_children_size: ContainingBlockSize,
/// A [`PositioningContext`] holding absolutely-positioned descendants
/// collected during the layout of this box.
pub positioning_context: PositioningContext,
}

File diff suppressed because it is too large Load diff

191
components/layout/lib.rs Normal file
View file

@ -0,0 +1,191 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
#![deny(unsafe_code)]
//! Layout. Performs layout on the DOM, builds display lists and sends them to be
//! painted.
mod cell;
pub mod context;
pub mod display_list;
pub mod dom;
mod dom_traversal;
mod flexbox;
pub mod flow;
mod formatting_contexts;
mod fragment_tree;
pub mod geom;
mod layout_box_base;
mod layout_impl;
mod taffy;
#[macro_use]
mod construct_modern;
mod lists;
mod positioned;
pub mod query;
mod quotes;
mod replaced;
mod sizing;
mod style_ext;
pub mod table;
pub mod traversal;
use app_units::Au;
pub use cell::ArcRefCell;
pub use flow::BoxTree;
pub use fragment_tree::FragmentTree;
pub use layout_impl::LayoutFactoryImpl;
use malloc_size_of_derive::MallocSizeOf;
use style::logical_geometry::WritingMode;
use style::properties::ComputedValues;
use style::values::computed::TextDecorationLine;
use crate::geom::{LogicalVec2, SizeConstraint};
use crate::style_ext::AspectRatio;
/// Represents the set of constraints that we use when computing the min-content
/// and max-content inline sizes of an element.
pub(crate) struct ConstraintSpace {
pub block_size: SizeConstraint,
pub writing_mode: WritingMode,
pub preferred_aspect_ratio: Option<AspectRatio>,
}
impl ConstraintSpace {
fn new(
block_size: SizeConstraint,
writing_mode: WritingMode,
preferred_aspect_ratio: Option<AspectRatio>,
) -> Self {
Self {
block_size,
writing_mode,
preferred_aspect_ratio,
}
}
fn new_for_style_and_ratio(
style: &ComputedValues,
preferred_aspect_ratio: Option<AspectRatio>,
) -> Self {
Self::new(
SizeConstraint::default(),
style.writing_mode,
preferred_aspect_ratio,
)
}
}
/// A variant of [`ContainingBlock`] that allows an indefinite inline size.
/// Useful for code that is shared for both layout (where we know the inline size
/// of the containing block) and intrinsic sizing (where we don't know it).
pub(crate) struct IndefiniteContainingBlock {
pub size: LogicalVec2<Option<Au>>,
pub writing_mode: WritingMode,
}
impl From<&ConstraintSpace> for IndefiniteContainingBlock {
fn from(constraint_space: &ConstraintSpace) -> Self {
Self {
size: LogicalVec2 {
inline: None,
block: constraint_space.block_size.to_definite(),
},
writing_mode: constraint_space.writing_mode,
}
}
}
impl<'a> From<&'_ ContainingBlock<'a>> for IndefiniteContainingBlock {
fn from(containing_block: &ContainingBlock<'a>) -> Self {
Self {
size: LogicalVec2 {
inline: Some(containing_block.size.inline),
block: containing_block.size.block.to_definite(),
},
writing_mode: containing_block.style.writing_mode,
}
}
}
impl<'a> From<&'_ DefiniteContainingBlock<'a>> for IndefiniteContainingBlock {
fn from(containing_block: &DefiniteContainingBlock<'a>) -> Self {
Self {
size: containing_block.size.map(|v| Some(*v)),
writing_mode: containing_block.style.writing_mode,
}
}
}
#[derive(Clone, Debug, MallocSizeOf)]
pub(crate) struct ContainingBlockSize {
inline: Au,
block: SizeConstraint,
}
pub(crate) struct ContainingBlock<'a> {
size: ContainingBlockSize,
style: &'a ComputedValues,
}
struct DefiniteContainingBlock<'a> {
size: LogicalVec2<Au>,
style: &'a ComputedValues,
}
impl<'a> From<&'_ DefiniteContainingBlock<'a>> for ContainingBlock<'a> {
fn from(definite: &DefiniteContainingBlock<'a>) -> Self {
ContainingBlock {
size: ContainingBlockSize {
inline: definite.size.inline,
block: SizeConstraint::Definite(definite.size.block),
},
style: definite.style,
}
}
}
/// Data that is propagated from ancestors to descendants during [`crate::flow::BoxTree`]
/// construction. This allows data to flow in the reverse direction of the typical layout
/// propoagation, but only during `BoxTree` construction.
#[derive(Clone, Copy, Debug)]
struct PropagatedBoxTreeData {
text_decoration: TextDecorationLine,
allow_percentage_column_in_tables: bool,
}
impl Default for PropagatedBoxTreeData {
fn default() -> Self {
Self {
text_decoration: Default::default(),
allow_percentage_column_in_tables: true,
}
}
}
impl PropagatedBoxTreeData {
pub(crate) fn union(&self, style: &ComputedValues) -> Self {
Self {
// FIXME(#31736): This is only taking into account the line style and not the decoration
// color. This should collect information about both so that they can be rendered properly.
text_decoration: self.text_decoration | style.clone_text_decoration_line(),
allow_percentage_column_in_tables: self.allow_percentage_column_in_tables,
}
}
pub(crate) fn without_text_decorations(&self) -> Self {
Self {
text_decoration: TextDecorationLine::NONE,
allow_percentage_column_in_tables: self.allow_percentage_column_in_tables,
}
}
fn disallowing_percentage_table_columns(&self) -> PropagatedBoxTreeData {
Self {
text_decoration: self.text_decoration,
allow_percentage_column_in_tables: false,
}
}
}

View file

@ -0,0 +1,96 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 style::properties::longhands::list_style_type::computed_value::T as ListStyleType;
use style::properties::style_structs;
use style::values::computed::Image;
use crate::context::LayoutContext;
use crate::dom::NodeExt;
use crate::dom_traversal::{NodeAndStyleInfo, PseudoElementContentItem};
use crate::replaced::ReplacedContents;
/// <https://drafts.csswg.org/css-lists/#content-property>
pub(crate) fn make_marker<'dom, Node>(
context: &LayoutContext,
info: &NodeAndStyleInfo<Node>,
) -> Option<(NodeAndStyleInfo<Node>, Vec<PseudoElementContentItem>)>
where
Node: NodeExt<'dom>,
{
let marker_info = info.pseudo(context, style::selector_parser::PseudoElement::Marker)?;
let style = &marker_info.style;
let list_style = style.get_list();
// https://drafts.csswg.org/css-lists/#marker-image
let marker_image = || match &list_style.list_style_image {
Image::Url(url) => Some(vec![
PseudoElementContentItem::Replaced(ReplacedContents::from_image_url(
marker_info.node,
context,
url,
)?),
PseudoElementContentItem::Text(" ".into()),
]),
// XXX: Non-None image types unimplemented.
Image::ImageSet(..) |
Image::Gradient(..) |
Image::CrossFade(..) |
Image::PaintWorklet(..) |
Image::None => None,
};
let content = marker_image().or_else(|| {
Some(vec![PseudoElementContentItem::Text(
marker_string(list_style)?.into(),
)])
})?;
Some((marker_info, content))
}
/// <https://drafts.csswg.org/css-lists/#marker-string>
fn marker_string(style: &style_structs::List) -> Option<&'static str> {
match style.list_style_type {
ListStyleType::None => None,
// TODO: Using non-breaking space here is a bit of a hack to give a bit of margin to outside
// markers, but really we should be setting `white-space: pre` on them instead.
// See https://github.com/w3c/csswg-drafts/issues/4891.
ListStyleType::Disc => Some("\u{00a0}"),
ListStyleType::Circle => Some("\u{00a0}"),
ListStyleType::Square => Some("\u{00a0}"),
ListStyleType::DisclosureOpen => Some("\u{00a0}"),
ListStyleType::DisclosureClosed => Some("\u{00a0}"),
ListStyleType::Decimal |
ListStyleType::LowerAlpha |
ListStyleType::UpperAlpha |
ListStyleType::ArabicIndic |
ListStyleType::Bengali |
ListStyleType::Cambodian |
ListStyleType::CjkDecimal |
ListStyleType::Devanagari |
ListStyleType::Gujarati |
ListStyleType::Gurmukhi |
ListStyleType::Kannada |
ListStyleType::Khmer |
ListStyleType::Lao |
ListStyleType::Malayalam |
ListStyleType::Mongolian |
ListStyleType::Myanmar |
ListStyleType::Oriya |
ListStyleType::Persian |
ListStyleType::Telugu |
ListStyleType::Thai |
ListStyleType::Tibetan |
ListStyleType::CjkEarthlyBranch |
ListStyleType::CjkHeavenlyStem |
ListStyleType::LowerGreek |
ListStyleType::Hiragana |
ListStyleType::HiraganaIroha |
ListStyleType::Katakana |
ListStyleType::KatakanaIroha => {
// TODO: Implement support for counters.
None
},
}
}

File diff suppressed because it is too large Load diff

1200
components/layout/query.rs Normal file

File diff suppressed because it is too large Load diff

427
components/layout/quotes.rs Normal file
View file

@ -0,0 +1,427 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
// Quotes data is obtained from ICU CLDR data file /tmp/cldr-common-46.0.zip.
// TODO(xiaochengh): This file should better be moved to elsewhere and maintained automatically.
// Or even better, extend the icu create to provide the data directly.
use std::collections::HashMap;
use std::sync::OnceLock;
use icu_locid::Locale;
#[derive(Clone, Copy, Debug)]
pub struct QuotePair {
pub opening: char,
pub closing: char,
}
#[derive(Clone, Copy, Debug)]
struct QuotesData {
quotes: QuotePair,
alternative_quotes: QuotePair,
}
impl QuotesData {
const fn from(chars: (char, char, char, char)) -> Self {
QuotesData {
quotes: QuotePair {
opening: chars.0,
closing: chars.1,
},
alternative_quotes: QuotePair {
opening: chars.2,
closing: chars.3,
},
}
}
}
static DEFAULT_QUOTES: QuotesData =
QuotesData::from(('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}'));
static QUOTES_MAP: OnceLock<HashMap<&'static str, QuotesData>> = OnceLock::new();
fn create_quotes_map() -> HashMap<&'static str, QuotesData> {
let input = [
("aa", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ab", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("af", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ak", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("an", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ann", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("apc", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("arn", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("as", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("asa", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("az", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ba", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("bal", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("bem", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("bew", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("bez", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("bgc", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("bgn", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("bho", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("blt", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("bn", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("bo", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("brx", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("bss", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("byn", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("cad", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("cch", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ccp", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ce", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ceb", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("cgg", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("cho", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("chr", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("cic", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ckb", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("co", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("csw", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("cu", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("cy", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("da", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("dav", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("dje", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("doi", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("dv", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("dz", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ebu", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ee", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("en", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("eo", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("es", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("fil", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("fo", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("frr", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("fur", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("fy", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ga", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("gaa", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("gd", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("gez", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("gl", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("gn", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("gu", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("guz", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("gv", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ha", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("haw", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("hi", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("hnj", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("id", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ig", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ii", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("io", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("iu", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("jbo", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("jmc", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("jv", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("kaa", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("kaj", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("kam", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("kcg", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("kde", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("kea", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ken", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("kgp", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("khq", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ki", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("kl", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("kln", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("km", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("kn", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ko", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("kok", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("kpe", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ks", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ksb", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ksh", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ku", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("kw", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("kxv", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("la", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("lg", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("lkt", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("lmo", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ln", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("lo", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("lrc", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ltg", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("lu", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("luo", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("lv", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("mai", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("mas", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("mdf", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("mer", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("mfe", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("mgh", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("mgo", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("mhn", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("mi", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("mic", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ml", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("mn", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("mni", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("moh", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("mr", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ms", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("mt", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("mus", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("my", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("myv", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("naq", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("nb", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("nd", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("nds", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ne", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("nn", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("nqo", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("nr", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("nus", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("nv", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ny", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("nyn", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("oc", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("om", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("or", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("os", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("osa", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("pa", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("pap", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("pcm", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("pis", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("prg", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ps", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("pt", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("qu", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("quc", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("raj", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("rhg", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("rif", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("rm", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("rof", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("rwk", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("sa", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("saq", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("sat", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("sbp", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("scn", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("sd", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("se", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("seh", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ses", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("shn", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("si", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("sid", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("skr", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("sma", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("smj", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("smn", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("sms", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("so", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ss", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ssy", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("su", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("sw", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("szl", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ta", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("te", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("teo", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("tg", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("th", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("tig", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("to", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("tok", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("tpi", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("tr", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("trv", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("trw", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ts", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("tt", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("twq", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("tyv", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("tzm", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ug", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("vai", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("ve", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("vec", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("vi", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("vmw", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("vo", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("vun", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("wa", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("wae", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("wal", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("wbp", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("wo", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("xh", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("xnr", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("xog", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("yi", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("yo", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("yrl", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("za", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("zh", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("zu", ('\u{201c}', '\u{201d}', '\u{2018}', '\u{2019}')),
("agq", ('\u{201e}', '\u{201d}', '\u{201a}', '\u{2019}')),
("ff", ('\u{201e}', '\u{201d}', '\u{201a}', '\u{2019}')),
("am", ('\u{ab}', '\u{bb}', '\u{2039}', '\u{203a}')),
("az-Arab", ('\u{ab}', '\u{bb}', '\u{2039}', '\u{203a}')),
("az-Cyrl", ('\u{ab}', '\u{bb}', '\u{2039}', '\u{203a}')),
("fa", ('\u{ab}', '\u{bb}', '\u{2039}', '\u{203a}')),
("fr-CH", ('\u{ab}', '\u{bb}', '\u{2039}', '\u{203a}')),
("gsw", ('\u{ab}', '\u{bb}', '\u{2039}', '\u{203a}')),
("jgo", ('\u{ab}', '\u{bb}', '\u{2039}', '\u{203a}')),
("kkj", ('\u{ab}', '\u{bb}', '\u{2039}', '\u{203a}')),
("mzn", ('\u{ab}', '\u{bb}', '\u{2039}', '\u{203a}')),
("sdh", ('\u{ab}', '\u{bb}', '\u{2039}', '\u{203a}')),
("ar", ('\u{201d}', '\u{201c}', '\u{2019}', '\u{2018}')),
("lld", ('\u{201d}', '\u{201c}', '\u{2019}', '\u{2018}')),
("ms-Arab", ('\u{201d}', '\u{201c}', '\u{2019}', '\u{2018}')),
("syr", ('\u{201d}', '\u{201c}', '\u{2019}', '\u{2018}')),
("ur", ('\u{201d}', '\u{201c}', '\u{2019}', '\u{2018}')),
("ast", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("blo", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("bm", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("br", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("ca", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("dyo", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("el", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("es-US", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("eu", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("ewo", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("ie", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("it", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("kab", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("kk", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("lij", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("mg", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("mua", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("nnh", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("pt-PT", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("sc", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("sg", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("sq", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("ti", ('\u{ab}', '\u{bb}', '\u{201c}', '\u{201d}')),
("bas", ('\u{ab}', '\u{bb}', '\u{201e}', '\u{201c}')),
("be", ('\u{ab}', '\u{bb}', '\u{201e}', '\u{201c}')),
("cv", ('\u{ab}', '\u{bb}', '\u{201e}', '\u{201c}')),
("ky", ('\u{ab}', '\u{bb}', '\u{201e}', '\u{201c}')),
("ru", ('\u{ab}', '\u{bb}', '\u{201e}', '\u{201c}')),
("sah", ('\u{ab}', '\u{bb}', '\u{201e}', '\u{201c}')),
("uk", ('\u{ab}', '\u{bb}', '\u{201e}', '\u{201c}')),
("bg", ('\u{201e}', '\u{201c}', '\u{201e}', '\u{201c}')),
("lt", ('\u{201e}', '\u{201c}', '\u{201e}', '\u{201c}')),
("bs-Cyrl", ('\u{201e}', '\u{201c}', '\u{201a}', '\u{2018}')),
("cs", ('\u{201e}', '\u{201c}', '\u{201a}', '\u{2018}')),
("de", ('\u{201e}', '\u{201c}', '\u{201a}', '\u{2018}')),
("dsb", ('\u{201e}', '\u{201c}', '\u{201a}', '\u{2018}')),
("et", ('\u{201e}', '\u{201c}', '\u{201a}', '\u{2018}')),
("hr", ('\u{201e}', '\u{201c}', '\u{201a}', '\u{2018}')),
("hsb", ('\u{201e}', '\u{201c}', '\u{201a}', '\u{2018}')),
("is", ('\u{201e}', '\u{201c}', '\u{201a}', '\u{2018}')),
("lb", ('\u{201e}', '\u{201c}', '\u{201a}', '\u{2018}')),
("luy", ('\u{201e}', '\u{201c}', '\u{201a}', '\u{2018}')),
("mk", ('\u{201e}', '\u{201c}', '\u{201a}', '\u{2018}')),
("sk", ('\u{201e}', '\u{201c}', '\u{201a}', '\u{2018}')),
("sl", ('\u{201e}', '\u{201c}', '\u{201a}', '\u{2018}')),
("bs", ('\u{201e}', '\u{201d}', '\u{2018}', '\u{2019}')),
("dua", ('\u{ab}', '\u{bb}', '\u{2018}', '\u{2019}')),
("el-POLYTON", ('\u{ab}', '\u{bb}', '\u{2018}', '\u{2019}')),
("ksf", ('\u{ab}', '\u{bb}', '\u{2018}', '\u{2019}')),
("no", ('\u{ab}', '\u{bb}', '\u{2018}', '\u{2019}')),
("rw", ('\u{ab}', '\u{bb}', '\u{2018}', '\u{2019}')),
("fi", ('\u{201d}', '\u{201d}', '\u{2019}', '\u{2019}')),
("he", ('\u{201d}', '\u{201d}', '\u{2019}', '\u{2019}')),
("lag", ('\u{201d}', '\u{201d}', '\u{2019}', '\u{2019}')),
("rn", ('\u{201d}', '\u{201d}', '\u{2019}', '\u{2019}')),
("sn", ('\u{201d}', '\u{201d}', '\u{2019}', '\u{2019}')),
("sv", ('\u{201d}', '\u{201d}', '\u{2019}', '\u{2019}')),
("fr-CA", ('\u{ab}', '\u{bb}', '\u{201d}', '\u{201c}')),
("fr", ('\u{ab}', '\u{bb}', '\u{ab}', '\u{bb}')),
("hy", ('\u{ab}', '\u{bb}', '\u{ab}', '\u{bb}')),
("yav", ('\u{ab}', '\u{bb}', '\u{ab}', '\u{bb}')),
("hu", ('\u{201e}', '\u{201d}', '\u{bb}', '\u{ab}')),
("ia", ('\u{2018}', '\u{2019}', '\u{201c}', '\u{201d}')),
("nso", ('\u{2018}', '\u{2019}', '\u{201c}', '\u{201d}')),
("ti-ER", ('\u{2018}', '\u{2019}', '\u{201c}', '\u{201d}')),
("tn", ('\u{2018}', '\u{2019}', '\u{201c}', '\u{201d}')),
("ja", ('\u{300c}', '\u{300d}', '\u{300e}', '\u{300f}')),
("yue", ('\u{300c}', '\u{300d}', '\u{300e}', '\u{300f}')),
("zh-Hant", ('\u{300c}', '\u{300d}', '\u{300e}', '\u{300f}')),
("ka", ('\u{201e}', '\u{201c}', '\u{ab}', '\u{bb}')),
("nl", ('\u{2018}', '\u{2019}', '\u{2018}', '\u{2019}')),
("nmg", ('\u{201e}', '\u{201d}', '\u{ab}', '\u{bb}')),
("pl", ('\u{201e}', '\u{201d}', '\u{ab}', '\u{bb}')),
("ro", ('\u{201e}', '\u{201d}', '\u{ab}', '\u{bb}')),
("shi", ('\u{ab}', '\u{bb}', '\u{201e}', '\u{201d}')),
("zgh", ('\u{ab}', '\u{bb}', '\u{201e}', '\u{201d}')),
("sr", ('\u{201e}', '\u{201d}', '\u{2019}', '\u{2019}')),
("st", ('\u{201c}', '\u{2019}', '\u{201c}', '\u{201d}')),
("tk", ('\u{201c}', '\u{201d}', '\u{201c}', '\u{201d}')),
("uz", ('\u{201c}', '\u{201d}', '\u{2019}', '\u{2018}')),
]
.map(|(lang, chars)| (lang, QuotesData::from(chars)));
HashMap::from(input)
}
fn quotes_data_for_lang(lang: &str) -> QuotesData {
// All valid language codes are at least two bytes long.
if lang.len() < 2 {
return DEFAULT_QUOTES;
}
let quotes_map = QUOTES_MAP.get_or_init(create_quotes_map);
// Found an exact match for the requested lang.
if let Some(quotes_data) = quotes_map.get(lang) {
return *quotes_data;
}
// Try parsing lang as a Locale and canonicalizing the subtags, then see if
// we can match it with region or script subtags, if present, or just the
// primary language tag.
let locale = match lang.parse::<Locale>() {
Err(_) => return DEFAULT_QUOTES,
Ok(locale) => locale,
};
let lang = locale.id.language.to_string();
if let Some(quotes_data) = quotes_map.get(lang.as_str()) {
return *quotes_data;
}
if let Some(quotes_data) = locale
.id
.region
.and_then(|region| quotes_map.get(format!("{lang}-{region}").as_str()))
{
return *quotes_data;
}
if let Some(quotes_data) = locale
.id
.script
.and_then(|script| quotes_map.get(format!("{lang}-{script}").as_str()))
{
return *quotes_data;
}
DEFAULT_QUOTES
}
pub fn quotes_for_lang(lang: &str, depth: usize) -> QuotePair {
let quotes_data = quotes_data_for_lang(lang);
match depth {
0 => quotes_data.quotes,
_ => quotes_data.alternative_quotes,
}
}

View file

@ -0,0 +1,623 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 std::cell::LazyCell;
use std::fmt;
use std::sync::Arc;
use app_units::Au;
use base::id::{BrowsingContextId, PipelineId};
use data_url::DataUrl;
use embedder_traits::ViewportDetails;
use euclid::{Scale, Size2D};
use malloc_size_of_derive::MallocSizeOf;
use net_traits::image_cache::{ImageOrMetadataAvailable, UsePlaceholder};
use pixels::Image;
use script_layout_interface::IFrameSize;
use servo_arc::Arc as ServoArc;
use style::Zero;
use style::computed_values::object_fit::T as ObjectFit;
use style::logical_geometry::{Direction, WritingMode};
use style::properties::ComputedValues;
use style::servo::url::ComputedUrl;
use style::values::CSSFloat;
use style::values::computed::image::Image as ComputedImage;
use url::Url;
use webrender_api::ImageKey;
use crate::cell::ArcRefCell;
use crate::context::LayoutContext;
use crate::dom::NodeExt;
use crate::fragment_tree::{BaseFragmentInfo, Fragment, IFrameFragment, ImageFragment};
use crate::geom::{
LogicalSides1D, LogicalVec2, PhysicalPoint, PhysicalRect, PhysicalSize, Size, Sizes,
};
use crate::layout_box_base::LayoutBoxBase;
use crate::sizing::{ComputeInlineContentSizes, ContentSizes, InlineContentSizesResult};
use crate::style_ext::{AspectRatio, Clamp, ComputedValuesExt, ContentBoxSizesAndPBM, LayoutStyle};
use crate::{ConstraintSpace, ContainingBlock, SizeConstraint};
#[derive(Debug, MallocSizeOf)]
pub(crate) struct ReplacedContents {
pub kind: ReplacedContentKind,
natural_size: NaturalSizes,
base_fragment_info: BaseFragmentInfo,
}
/// The natural dimensions of a replaced element, including a height, width, and
/// aspect ratio.
///
/// * Raster images always have an natural width and height, with 1 image pixel = 1px.
/// The natural 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 natural width and height **but no natural ratio**.
/// See <https://github.com/w3c/csswg-drafts/issues/1044> and
/// <https://drafts.csswg.org/css-images/#natural-dimensions> “In general, […]”
///
/// * For SVG, see <https://svgwg.org/svg2-draft/coords.html#SizingSVGInCSS>
/// and again <https://github.com/w3c/csswg-drafts/issues/4572>.
///
/// * IFrames do not have natural width and height or natural ratio according
/// to <https://drafts.csswg.org/css-images/#intrinsic-dimensions>.
#[derive(Debug, MallocSizeOf)]
pub(crate) struct NaturalSizes {
pub width: Option<Au>,
pub height: Option<Au>,
pub ratio: Option<CSSFloat>,
}
impl NaturalSizes {
pub(crate) fn from_width_and_height(width: f32, height: f32) -> Self {
// https://drafts.csswg.org/css-images/#natural-aspect-ratio:
// "If an object has a degenerate natural aspect ratio (at least one part being
// zero or infinity), it is treated as having no natural aspect ratio.""
let ratio = if width.is_normal() && height.is_normal() {
Some(width / height)
} else {
None
};
Self {
width: Some(Au::from_f32_px(width)),
height: Some(Au::from_f32_px(height)),
ratio,
}
}
pub(crate) fn empty() -> Self {
Self {
width: None,
height: None,
ratio: None,
}
}
}
#[derive(MallocSizeOf)]
pub(crate) enum CanvasSource {
WebGL(ImageKey),
Image(ImageKey),
WebGPU(ImageKey),
/// transparent black
Empty,
}
impl fmt::Debug for CanvasSource {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
match *self {
CanvasSource::WebGL(_) => "WebGL",
CanvasSource::Image(_) => "Image",
CanvasSource::WebGPU(_) => "WebGPU",
CanvasSource::Empty => "Empty",
}
)
}
}
#[derive(Debug, MallocSizeOf)]
pub(crate) struct CanvasInfo {
pub source: CanvasSource,
}
#[derive(Debug, MallocSizeOf)]
pub(crate) struct IFrameInfo {
pub pipeline_id: PipelineId,
pub browsing_context_id: BrowsingContextId,
}
#[derive(Debug, MallocSizeOf)]
pub(crate) struct VideoInfo {
pub image_key: webrender_api::ImageKey,
}
#[derive(Debug, MallocSizeOf)]
pub(crate) enum ReplacedContentKind {
Image(#[conditional_malloc_size_of] Option<Arc<Image>>),
IFrame(IFrameInfo),
Canvas(CanvasInfo),
Video(Option<VideoInfo>),
}
impl ReplacedContents {
pub fn for_element<'dom>(element: impl NodeExt<'dom>, context: &LayoutContext) -> Option<Self> {
if let Some(ref data_attribute_string) = element.as_typeless_object_with_data_attribute() {
if let Some(url) = try_to_parse_image_data_url(data_attribute_string) {
return Self::from_image_url(
element,
context,
&ComputedUrl::Valid(ServoArc::new(url)),
);
}
}
let (kind, natural_size_in_dots) = {
if let Some((image, natural_size_in_dots)) = element.as_image() {
(
ReplacedContentKind::Image(image),
Some(natural_size_in_dots),
)
} else if let Some((canvas_info, natural_size_in_dots)) = element.as_canvas() {
(
ReplacedContentKind::Canvas(canvas_info),
Some(natural_size_in_dots),
)
} else if let Some((pipeline_id, browsing_context_id)) = element.as_iframe() {
(
ReplacedContentKind::IFrame(IFrameInfo {
pipeline_id,
browsing_context_id,
}),
None,
)
} else if let Some((image_key, natural_size_in_dots)) = element.as_video() {
(
ReplacedContentKind::Video(image_key.map(|key| VideoInfo { image_key: key })),
natural_size_in_dots,
)
} else {
return None;
}
};
if let ReplacedContentKind::Image(Some(ref image)) = kind {
context.handle_animated_image(element.opaque(), image.clone());
}
let natural_size = if let Some(naturalc_size_in_dots) = natural_size_in_dots {
// FIXME: should 'image-resolution' (when implemented) be used *instead* of
// `script::dom::htmlimageelement::ImageRequest::current_pixel_density`?
// https://drafts.csswg.org/css-images-4/#the-image-resolution
let dppx = 1.0;
let width = (naturalc_size_in_dots.width as CSSFloat) / dppx;
let height = (naturalc_size_in_dots.height as CSSFloat) / dppx;
NaturalSizes::from_width_and_height(width, height)
} else {
NaturalSizes::empty()
};
let base_fragment_info = BaseFragmentInfo::new_for_node(element.opaque());
Some(Self {
kind,
natural_size,
base_fragment_info,
})
}
pub fn from_image_url<'dom>(
element: impl NodeExt<'dom>,
context: &LayoutContext,
image_url: &ComputedUrl,
) -> Option<Self> {
if let ComputedUrl::Valid(image_url) = image_url {
let (image, width, height) = match context.get_or_request_image_or_meta(
element.opaque(),
image_url.clone().into(),
UsePlaceholder::No,
) {
Some(ImageOrMetadataAvailable::ImageAvailable { image, .. }) => {
(Some(image.clone()), image.width as f32, image.height as f32)
},
Some(ImageOrMetadataAvailable::MetadataAvailable(metadata, _id)) => {
(None, metadata.width as f32, metadata.height as f32)
},
None => return None,
};
return Some(Self {
kind: ReplacedContentKind::Image(image),
natural_size: NaturalSizes::from_width_and_height(width, height),
base_fragment_info: BaseFragmentInfo::new_for_node(element.opaque()),
});
}
None
}
pub fn from_image<'dom>(
element: impl NodeExt<'dom>,
context: &LayoutContext,
image: &ComputedImage,
) -> Option<Self> {
match image {
ComputedImage::Url(image_url) => Self::from_image_url(element, context, image_url),
_ => None, // TODO
}
}
fn flow_relative_natural_size(&self, writing_mode: WritingMode) -> LogicalVec2<Option<Au>> {
let natural_size = PhysicalSize::new(self.natural_size.width, self.natural_size.height);
LogicalVec2::from_physical_size(&natural_size, writing_mode)
}
fn inline_size_over_block_size_intrinsic_ratio(
&self,
style: &ComputedValues,
) -> Option<CSSFloat> {
self.natural_size.ratio.map(|width_over_height| {
if style.writing_mode.is_vertical() {
1. / width_over_height
} else {
width_over_height
}
})
}
#[inline]
fn content_size(
&self,
axis: Direction,
preferred_aspect_ratio: Option<AspectRatio>,
get_size_in_opposite_axis: &dyn Fn() -> SizeConstraint,
get_fallback_size: &dyn Fn() -> Au,
) -> Au {
let Some(ratio) = preferred_aspect_ratio else {
return get_fallback_size();
};
let transfer = |size| ratio.compute_dependent_size(axis, size);
match get_size_in_opposite_axis() {
SizeConstraint::Definite(size) => transfer(size),
SizeConstraint::MinMax(min_size, max_size) => get_fallback_size()
.clamp_between_extremums(transfer(min_size), max_size.map(transfer)),
}
}
pub fn make_fragments(
&self,
layout_context: &LayoutContext,
style: &ServoArc<ComputedValues>,
size: PhysicalSize<Au>,
) -> Vec<Fragment> {
let natural_size = PhysicalSize::new(
self.natural_size.width.unwrap_or(size.width),
self.natural_size.height.unwrap_or(size.height),
);
let object_fit_size = self.natural_size.ratio.map_or(size, |width_over_height| {
let preserve_aspect_ratio_with_comparison =
|size: PhysicalSize<Au>, comparison: fn(&Au, &Au) -> bool| {
let candidate_width = size.height.scale_by(width_over_height);
if comparison(&candidate_width, &size.width) {
return PhysicalSize::new(candidate_width, size.height);
}
let candidate_height = size.width.scale_by(1. / width_over_height);
debug_assert!(comparison(&candidate_height, &size.height));
PhysicalSize::new(size.width, candidate_height)
};
match style.clone_object_fit() {
ObjectFit::Fill => size,
ObjectFit::Contain => preserve_aspect_ratio_with_comparison(size, PartialOrd::le),
ObjectFit::Cover => preserve_aspect_ratio_with_comparison(size, PartialOrd::ge),
ObjectFit::None => natural_size,
ObjectFit::ScaleDown => {
preserve_aspect_ratio_with_comparison(size.min(natural_size), PartialOrd::le)
},
}
});
let object_position = style.clone_object_position();
let horizontal_position = object_position
.horizontal
.to_used_value(size.width - object_fit_size.width);
let vertical_position = object_position
.vertical
.to_used_value(size.height - object_fit_size.height);
let rect = PhysicalRect::new(
PhysicalPoint::new(horizontal_position, vertical_position),
object_fit_size,
);
let clip = PhysicalRect::new(PhysicalPoint::origin(), size);
match &self.kind {
ReplacedContentKind::Image(image) => image
.as_ref()
.and_then(|image| image.id)
.map(|image_key| {
Fragment::Image(ArcRefCell::new(ImageFragment {
base: self.base_fragment_info.into(),
style: style.clone(),
rect,
clip,
image_key: Some(image_key),
}))
})
.into_iter()
.collect(),
ReplacedContentKind::Video(video) => {
vec![Fragment::Image(ArcRefCell::new(ImageFragment {
base: self.base_fragment_info.into(),
style: style.clone(),
rect,
clip,
image_key: video.as_ref().map(|video| video.image_key),
}))]
},
ReplacedContentKind::IFrame(iframe) => {
let size = Size2D::new(rect.size.width.to_f32_px(), rect.size.height.to_f32_px());
let hidpi_scale_factor = layout_context.shared_context().device_pixel_ratio();
layout_context.iframe_sizes.lock().insert(
iframe.browsing_context_id,
IFrameSize {
browsing_context_id: iframe.browsing_context_id,
pipeline_id: iframe.pipeline_id,
viewport_details: ViewportDetails {
size,
hidpi_scale_factor: Scale::new(hidpi_scale_factor.0),
},
},
);
vec![Fragment::IFrame(ArcRefCell::new(IFrameFragment {
base: self.base_fragment_info.into(),
style: style.clone(),
pipeline_id: iframe.pipeline_id,
rect,
}))]
},
ReplacedContentKind::Canvas(canvas_info) => {
if self.natural_size.width == Some(Au::zero()) ||
self.natural_size.height == Some(Au::zero())
{
return vec![];
}
let image_key = match canvas_info.source {
CanvasSource::WebGL(image_key) => image_key,
CanvasSource::WebGPU(image_key) => image_key,
CanvasSource::Image(image_key) => image_key,
CanvasSource::Empty => return vec![],
};
vec![Fragment::Image(ArcRefCell::new(ImageFragment {
base: self.base_fragment_info.into(),
style: style.clone(),
rect,
clip,
image_key: Some(image_key),
}))]
},
}
}
pub(crate) fn preferred_aspect_ratio(
&self,
style: &ComputedValues,
padding_border_sums: &LogicalVec2<Au>,
) -> Option<AspectRatio> {
style
.preferred_aspect_ratio(
self.inline_size_over_block_size_intrinsic_ratio(style),
padding_border_sums,
)
.or_else(|| {
matches!(self.kind, ReplacedContentKind::Video(_)).then(|| {
let size = Self::default_object_size();
AspectRatio::from_content_ratio(
size.width.to_f32_px() / size.height.to_f32_px(),
)
})
})
}
/// <https://drafts.csswg.org/css2/visudet.html#inline-replaced-width>
/// <https://drafts.csswg.org/css2/visudet.html#inline-replaced-height>
///
/// Also used in other cases, for example
/// <https://drafts.csswg.org/css2/visudet.html#block-replaced-width>
pub(crate) fn used_size_as_if_inline_element(
&self,
containing_block: &ContainingBlock,
style: &ComputedValues,
content_box_sizes_and_pbm: &ContentBoxSizesAndPBM,
ignore_block_margins_for_stretch: LogicalSides1D<bool>,
) -> LogicalVec2<Au> {
let pbm = &content_box_sizes_and_pbm.pbm;
self.used_size_as_if_inline_element_from_content_box_sizes(
containing_block,
style,
self.preferred_aspect_ratio(style, &pbm.padding_border_sums),
content_box_sizes_and_pbm.content_box_sizes.as_ref(),
Size::FitContent.into(),
pbm.sums_auto_is_zero(ignore_block_margins_for_stretch),
)
}
pub(crate) fn default_object_size() -> PhysicalSize<Au> {
// FIXME:
// https://drafts.csswg.org/css-images/#default-object-size
// “If 300px is too wide to fit the device, UAs should use the width of
// the largest rectangle that has a 2:1 ratio and fits the device instead.”
// “height of the largest rectangle that has a 2:1 ratio, has a height not greater
// than 150px, and has a width not greater than the device width.”
PhysicalSize::new(Au::from_px(300), Au::from_px(150))
}
pub(crate) fn flow_relative_default_object_size(writing_mode: WritingMode) -> LogicalVec2<Au> {
LogicalVec2::from_physical_size(&Self::default_object_size(), writing_mode)
}
/// <https://drafts.csswg.org/css2/visudet.html#inline-replaced-width>
/// <https://drafts.csswg.org/css2/visudet.html#inline-replaced-height>
///
/// Also used in other cases, for example
/// <https://drafts.csswg.org/css2/visudet.html#block-replaced-width>
///
/// The logic differs from CSS2 in order to properly handle `aspect-ratio` and keyword sizes.
/// Each axis can have preferred, min and max sizing constraints, plus constraints transferred
/// from the other axis if there is an aspect ratio, plus a natural and default size.
/// In case of conflict, the order of precedence (from highest to lowest) is:
/// 1. Non-transferred min constraint
/// 2. Non-transferred max constraint
/// 3. Non-transferred preferred constraint
/// 4. Transferred min constraint
/// 5. Transferred max constraint
/// 6. Transferred preferred constraint
/// 7. Natural size
/// 8. Default object size
///
/// <https://drafts.csswg.org/css-sizing-4/#aspect-ratio-size-transfers>
/// <https://github.com/w3c/csswg-drafts/issues/6071#issuecomment-2243986313>
pub(crate) fn used_size_as_if_inline_element_from_content_box_sizes(
&self,
containing_block: &ContainingBlock,
style: &ComputedValues,
preferred_aspect_ratio: Option<AspectRatio>,
sizes: LogicalVec2<&Sizes>,
automatic_size: LogicalVec2<Size<Au>>,
pbm_sums: LogicalVec2<Au>,
) -> LogicalVec2<Au> {
// <https://drafts.csswg.org/css-images-3/#natural-dimensions>
// <https://drafts.csswg.org/css-images-3/#default-object-size>
let writing_mode = style.writing_mode;
let natural_size = LazyCell::new(|| self.flow_relative_natural_size(writing_mode));
let default_object_size =
LazyCell::new(|| Self::flow_relative_default_object_size(writing_mode));
let get_inline_fallback_size = || {
natural_size
.inline
.unwrap_or_else(|| default_object_size.inline)
};
let get_block_fallback_size = || {
natural_size
.block
.unwrap_or_else(|| default_object_size.block)
};
// <https://drafts.csswg.org/css-sizing-4/#stretch-fit-sizing>
let inline_stretch_size = Au::zero().max(containing_block.size.inline - pbm_sums.inline);
let block_stretch_size = containing_block
.size
.block
.to_definite()
.map(|block_size| Au::zero().max(block_size - pbm_sums.block));
// First, compute the inline size. Intrinsic values depend on the block sizing properties
// through the aspect ratio, but these can also be intrinsic and depend on the inline size.
// Therefore, we tentatively treat intrinsic block sizing properties as their initial value.
let get_inline_content_size = || {
let get_block_size = || {
sizes
.block
.resolve_extrinsic(automatic_size.block, Au::zero(), block_stretch_size)
};
self.content_size(
Direction::Inline,
preferred_aspect_ratio,
&get_block_size,
&get_inline_fallback_size,
)
.into()
};
let inline_size = sizes.inline.resolve(
Direction::Inline,
automatic_size.inline,
Au::zero,
Some(inline_stretch_size),
get_inline_content_size,
false, /* is_table */
);
// Now we can compute the block size, using the inline size from above.
let block_content_size = LazyCell::new(|| -> ContentSizes {
let get_inline_size = || SizeConstraint::Definite(inline_size);
self.content_size(
Direction::Block,
preferred_aspect_ratio,
&get_inline_size,
&get_block_fallback_size,
)
.into()
});
let block_size = sizes.block.resolve(
Direction::Block,
automatic_size.block,
Au::zero,
block_stretch_size,
|| *block_content_size,
false, /* is_table */
);
LogicalVec2 {
inline: inline_size,
block: block_size,
}
}
#[inline]
pub(crate) fn layout_style<'a>(&self, base: &'a LayoutBoxBase) -> LayoutStyle<'a> {
LayoutStyle::Default(&base.style)
}
}
impl ComputeInlineContentSizes for ReplacedContents {
fn compute_inline_content_sizes(
&self,
_: &LayoutContext,
constraint_space: &ConstraintSpace,
) -> InlineContentSizesResult {
let get_inline_fallback_size = || {
let writing_mode = constraint_space.writing_mode;
self.flow_relative_natural_size(writing_mode)
.inline
.unwrap_or_else(|| Self::flow_relative_default_object_size(writing_mode).inline)
};
let inline_content_size = self.content_size(
Direction::Inline,
constraint_space.preferred_aspect_ratio,
&|| constraint_space.block_size,
&get_inline_fallback_size,
);
InlineContentSizesResult {
sizes: inline_content_size.into(),
depends_on_block_constraints: constraint_space.preferred_aspect_ratio.is_some(),
}
}
}
fn try_to_parse_image_data_url(string: &str) -> Option<Url> {
if !string.starts_with("data:") {
return None;
}
let data_url = DataUrl::process(string).ok()?;
let mime_type = data_url.mime_type();
if mime_type.type_ != "image" {
return None;
}
// TODO: Find a better way to test for supported image formats. Currently this type of check is
// repeated several places in Servo, but should be centralized somehow.
if !matches!(
mime_type.subtype.as_str(),
"png" | "jpeg" | "gif" | "webp" | "bmp" | "ico"
) {
return None;
}
Url::parse(string).ok()
}

297
components/layout/sizing.rs Normal file
View file

@ -0,0 +1,297 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
//! <https://drafts.csswg.org/css-sizing/>
use std::cell::LazyCell;
use std::ops::{Add, AddAssign};
use app_units::Au;
use malloc_size_of_derive::MallocSizeOf;
use style::Zero;
use style::values::computed::LengthPercentage;
use crate::context::LayoutContext;
use crate::geom::Size;
use crate::style_ext::{AspectRatio, Clamp, ComputedValuesExt, ContentBoxSizesAndPBM, LayoutStyle};
use crate::{ConstraintSpace, IndefiniteContainingBlock, LogicalVec2};
#[derive(PartialEq)]
pub(crate) enum IntrinsicSizingMode {
/// Used to refer to a min-content contribution or max-content contribution.
/// This is the size that a box contributes to its containing blocks min-content
/// or max-content size. Note this is based on the outer size of the box,
/// and takes into account the relevant sizing properties of the element.
/// <https://drafts.csswg.org/css-sizing-3/#contributions>
Contribution,
/// Used to refer to a min-content size or max-content size.
/// This is the size based on the contents of an element, without regard for its context.
/// Note this is usually based on the inner (content-box) size of the box,
/// and ignores the relevant sizing properties of the element.
/// <https://drafts.csswg.org/css-sizing-3/#intrinsic>
Size,
}
#[derive(Clone, Copy, Debug, Default, MallocSizeOf)]
pub(crate) struct ContentSizes {
pub min_content: Au,
pub max_content: Au,
}
/// <https://drafts.csswg.org/css-sizing/#intrinsic-sizes>
impl ContentSizes {
pub fn max(&self, other: Self) -> Self {
Self {
min_content: self.min_content.max(other.min_content),
max_content: self.max_content.max(other.max_content),
}
}
pub fn max_assign(&mut self, other: Self) {
*self = self.max(other);
}
pub fn union(&self, other: &Self) -> Self {
Self {
min_content: self.min_content.max(other.min_content),
max_content: self.max_content + other.max_content,
}
}
pub fn map(&self, f: impl Fn(Au) -> Au) -> Self {
Self {
min_content: f(self.min_content),
max_content: f(self.max_content),
}
}
}
impl Zero for ContentSizes {
fn zero() -> Self {
Au::zero().into()
}
fn is_zero(&self) -> bool {
self.min_content.is_zero() && self.max_content.is_zero()
}
}
impl Add for ContentSizes {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self {
min_content: self.min_content + rhs.min_content,
max_content: self.max_content + rhs.max_content,
}
}
}
impl AddAssign for ContentSizes {
fn add_assign(&mut self, rhs: Self) {
*self = self.add(rhs)
}
}
impl ContentSizes {
/// Clamps the provided amount to be between the min-content and the max-content.
/// This is called "shrink-to-fit" in CSS2, and "fit-content" in CSS Sizing.
/// <https://drafts.csswg.org/css2/visudet.html#shrink-to-fit-float>
/// <https://drafts.csswg.org/css-sizing/#funcdef-width-fit-content>
pub fn shrink_to_fit(&self, available_size: Au) -> Au {
// This formula is slightly different than what the spec says,
// to ensure that the minimum wins for a malformed ContentSize
// whose min_content is larger than its max_content.
available_size.min(self.max_content).max(self.min_content)
}
}
impl From<Au> for ContentSizes {
fn from(size: Au) -> Self {
Self {
min_content: size,
max_content: size,
}
}
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn outer_inline(
layout_style: &LayoutStyle,
containing_block: &IndefiniteContainingBlock,
auto_minimum: &LogicalVec2<Au>,
auto_block_size_stretches_to_containing_block: bool,
is_replaced: bool,
establishes_containing_block: bool,
get_preferred_aspect_ratio: impl FnOnce(&LogicalVec2<Au>) -> Option<AspectRatio>,
get_content_size: impl FnOnce(&ConstraintSpace) -> InlineContentSizesResult,
) -> InlineContentSizesResult {
let ContentBoxSizesAndPBM {
content_box_sizes,
pbm,
mut depends_on_block_constraints,
preferred_size_computes_to_auto,
} = layout_style.content_box_sizes_and_padding_border_margin(containing_block);
let margin = pbm.margin.map(|v| v.auto_is(Au::zero));
let pbm_sums = LogicalVec2 {
block: pbm.padding_border_sums.block + margin.block_sum(),
inline: pbm.padding_border_sums.inline + margin.inline_sum(),
};
let style = layout_style.style();
let content_size = LazyCell::new(|| {
let constraint_space = if establishes_containing_block {
let available_block_size = containing_block
.size
.block
.map(|v| Au::zero().max(v - pbm_sums.block));
let automatic_size = if preferred_size_computes_to_auto.block &&
auto_block_size_stretches_to_containing_block
{
depends_on_block_constraints = true;
Size::Stretch
} else {
Size::FitContent
};
ConstraintSpace::new(
content_box_sizes.block.resolve_extrinsic(
automatic_size,
auto_minimum.block,
available_block_size,
),
style.writing_mode,
get_preferred_aspect_ratio(&pbm.padding_border_sums),
)
} else {
// This assumes that there is no preferred aspect ratio, or that there is no
// block size constraint to be transferred so the ratio is irrelevant.
// We only get into here for anonymous blocks, for which the assumption holds.
ConstraintSpace::new(
containing_block.size.block.into(),
containing_block.writing_mode,
None,
)
};
get_content_size(&constraint_space)
});
let resolve_non_initial = |inline_size, stretch_values| {
Some(match inline_size {
Size::Initial => return None,
Size::Numeric(numeric) => (numeric, numeric, false),
Size::MinContent => (
content_size.sizes.min_content,
content_size.sizes.min_content,
content_size.depends_on_block_constraints,
),
Size::MaxContent => (
content_size.sizes.max_content,
content_size.sizes.max_content,
content_size.depends_on_block_constraints,
),
Size::FitContent => (
content_size.sizes.min_content,
content_size.sizes.max_content,
content_size.depends_on_block_constraints,
),
Size::FitContentFunction(size) => {
let size = content_size.sizes.shrink_to_fit(size);
(size, size, content_size.depends_on_block_constraints)
},
Size::Stretch => return stretch_values,
})
};
let (mut preferred_min_content, preferred_max_content, preferred_depends_on_block_constraints) =
resolve_non_initial(content_box_sizes.inline.preferred, None)
.unwrap_or_else(|| resolve_non_initial(Size::FitContent, None).unwrap());
let (mut min_min_content, mut min_max_content, mut min_depends_on_block_constraints) =
resolve_non_initial(
content_box_sizes.inline.min,
Some((Au::zero(), Au::zero(), false)),
)
.unwrap_or((auto_minimum.inline, auto_minimum.inline, false));
let (mut max_min_content, max_max_content, max_depends_on_block_constraints) =
resolve_non_initial(content_box_sizes.inline.max, None)
.map(|(min_content, max_content, depends_on_block_constraints)| {
(
Some(min_content),
Some(max_content),
depends_on_block_constraints,
)
})
.unwrap_or_default();
// https://drafts.csswg.org/css-sizing-3/#replaced-percentage-min-contribution
// > If the box is replaced, a cyclic percentage in the value of any max size property
// > or preferred size property (width/max-width/height/max-height), is resolved against
// > zero when calculating the min-content contribution in the corresponding axis.
//
// This means that e.g. the min-content contribution of `width: calc(100% + 100px)`
// should be 100px, but it's just zero on other browsers, so we do the same.
if is_replaced {
let has_percentage = |size: Size<LengthPercentage>| {
// We need a comment here to avoid breaking `./mach test-tidy`.
matches!(size, Size::Numeric(numeric) if numeric.has_percentage())
};
if content_box_sizes.inline.preferred.is_initial() &&
has_percentage(style.box_size(containing_block.writing_mode).inline)
{
preferred_min_content = Au::zero();
}
if content_box_sizes.inline.max.is_initial() &&
has_percentage(style.max_box_size(containing_block.writing_mode).inline)
{
max_min_content = Some(Au::zero());
}
}
// Regardless of their sizing properties, tables are always forced to be at least
// as big as their min-content size, so floor the minimums.
if layout_style.is_table() {
min_min_content.max_assign(content_size.sizes.min_content);
min_max_content.max_assign(content_size.sizes.min_content);
min_depends_on_block_constraints |= content_size.depends_on_block_constraints;
}
InlineContentSizesResult {
sizes: ContentSizes {
min_content: preferred_min_content
.clamp_between_extremums(min_min_content, max_min_content) +
pbm_sums.inline,
max_content: preferred_max_content
.clamp_between_extremums(min_max_content, max_max_content) +
pbm_sums.inline,
},
depends_on_block_constraints: depends_on_block_constraints &&
(preferred_depends_on_block_constraints ||
min_depends_on_block_constraints ||
max_depends_on_block_constraints),
}
}
#[derive(Clone, Copy, Debug, MallocSizeOf)]
pub(crate) struct InlineContentSizesResult {
pub sizes: ContentSizes,
pub depends_on_block_constraints: bool,
}
pub(crate) trait ComputeInlineContentSizes {
fn compute_inline_content_sizes(
&self,
layout_context: &LayoutContext,
constraint_space: &ConstraintSpace,
) -> InlineContentSizesResult;
/// Returns the same result as [`Self::compute_inline_content_sizes()`], but adjusted
/// to floor the max-content size by the min-content size.
/// This is being discussed in <https://github.com/w3c/csswg-drafts/issues/12076>.
fn compute_inline_content_sizes_with_fixup(
&self,
layout_context: &LayoutContext,
constraint_space: &ConstraintSpace,
) -> InlineContentSizesResult {
let mut result = self.compute_inline_content_sizes(layout_context, constraint_space);
let sizes = &mut result.sizes;
sizes.max_content.max_assign(sizes.min_content);
result
}
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,382 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
#![allow(rustdoc::private_intra_doc_links)]
//! # HTML Tables (╯°□°)╯︵ ┻━┻
//!
//! This implementation is based on the [table section of the HTML 5 Specification][1],
//! the draft [CSS Table Module Level! 3][2] and the [LayoutNG implementation of tables][3] in Blink.
//! In general, the draft specification differs greatly from what other browsers do, so we
//! generally follow LayoutNG when in question.
//!
//! [1]: https://html.spec.whatwg.org/multipage/#tables
//! [2]: https://drafts.csswg.org/css-tables
//! [3]: https://source.chromium.org/chromium/chromium/src/third_party/+/main:blink/renderer/core/layout/table
//!
//! Table layout is divided into two phases:
//!
//! 1. Box Tree Construction
//! 2. Fragment Tree Construction
//!
//! ## Box Tree Construction
//!
//! During box tree construction, table layout (`construct.rs`) will traverse the DOM and construct
//! the basic box tree representation of a table, using the structs defined in this file ([`Table`],
//! [`TableTrackGroup`], [`TableTrack`], etc). When processing the DOM, elements are handled
//! differently depending on their `display` value. For instance, an element with `display:
//! table-cell` is treated as a table cell. HTML table elements like `<table>` and `<td>` are
//! assigned the corresponding display value from the user agent stylesheet.
//!
//! Every [`Table`] holds an array of [`TableSlot`]. A [`TableSlot`] represents either a cell, a cell
//! location occupied by a cell originating from another place in the table, or is empty. In
//! addition, when there are table model errors, a slot may spanned by more than one cell.
//!
//! During processing, the box tree construction agorithm will also fix up table structure, for
//! instance, creating anonymous rows for lone table cells and putting non-table content into
//! anonymous cells. In addition, flow layout will collect table elements outside of tables and create
//! anonymous tables for them.
//!
//! After processing, box tree construction does a fix up pass on tables, converting rowspan=0 into
//! definite rowspan values and making sure that rowspan and celspan values are not larger than the
//! table itself. Finally, row groups may be reordered to enforce the fact that the first `<thead>`
//! comes before `<tbody>` which comes before the first `<tfoot>`.
//!
//! ## Fragment Tree Construction
//!
//! Fragment tree construction involves calculating the size and positioning of all table elements,
//! given their style, content, and cell and row spans. This happens both during intrinsic inline
//! size computation as well as layout into Fragments. In both of these cases, measurement and
//! layout is done by [`layout::TableLayout`], though for intrinsic size computation only a partial
//! layout is done.
//!
//! In general, we follow the following steps when laying out table content:
//!
//! 1. Compute track constrainedness and has originating cells
//! 2. Compute cell measures
//! 3. Compute column measures
//! 4. Compute intrinsic inline sizes for columns and the table
//! 5. Compute the final table inline size
//! 6. Distribute size to columns
//! 7. Do first pass cell layout
//! 8. Do row layout
//! 9. Compute table height and final row sizes
//! 10. Create fragments for table elements (columns, column groups, rows, row groups, cells)
//!
//! For intrinsic size computation this process stops at step 4.
mod construct;
mod layout;
use std::ops::Range;
use app_units::Au;
use atomic_refcell::AtomicRef;
pub(crate) use construct::AnonymousTableContent;
pub use construct::TableBuilder;
use euclid::{Point2D, Size2D, UnknownUnit, Vector2D};
use malloc_size_of_derive::MallocSizeOf;
use servo_arc::Arc;
use style::properties::ComputedValues;
use style::properties::style_structs::Font;
use style_traits::dom::OpaqueNode;
use super::flow::BlockFormattingContext;
use crate::cell::ArcRefCell;
use crate::flow::BlockContainer;
use crate::formatting_contexts::IndependentFormattingContext;
use crate::fragment_tree::{BaseFragmentInfo, Fragment};
use crate::geom::PhysicalVec;
use crate::layout_box_base::LayoutBoxBase;
use crate::style_ext::BorderStyleColor;
use crate::table::layout::TableLayout;
pub type TableSize = Size2D<usize, UnknownUnit>;
#[derive(Debug, MallocSizeOf)]
pub struct Table {
/// The style of this table. These are the properties that apply to the "wrapper" ie the element
/// that contains both the grid and the captions. Not all properties are actually used on the
/// wrapper though, such as background and borders, which apply to the grid.
#[conditional_malloc_size_of]
style: Arc<ComputedValues>,
/// The style of this table's grid. This is an anonymous style based on the table's style, but
/// eliminating all the properties handled by the "wrapper."
#[conditional_malloc_size_of]
grid_style: Arc<ComputedValues>,
/// The [`BaseFragmentInfo`] for this table's grid. This is necessary so that when the
/// grid has a background image, it can be associated with the table's node.
grid_base_fragment_info: BaseFragmentInfo,
/// The captions for this table.
pub captions: Vec<ArcRefCell<TableCaption>>,
/// The column groups for this table.
pub column_groups: Vec<ArcRefCell<TableTrackGroup>>,
/// The columns of this table defined by `<colgroup> | display: table-column-group`
/// and `<col> | display: table-column` elements as well as `display: table-column`.
pub columns: Vec<ArcRefCell<TableTrack>>,
/// The rows groups for this table defined by `<tbody>`, `<thead>`, and `<tfoot>`.
pub row_groups: Vec<ArcRefCell<TableTrackGroup>>,
/// The rows of this table defined by `<tr>` or `display: table-row` elements.
pub rows: Vec<ArcRefCell<TableTrack>>,
/// The content of the slots of this table.
pub slots: Vec<Vec<TableSlot>>,
/// The size of this table.
pub size: TableSize,
/// Whether or not this Table is anonymous.
anonymous: bool,
/// Whether percentage columns are taken into account during inline content sizes calculation.
percentage_columns_allowed_for_inline_content_sizes: bool,
}
impl Table {
pub(crate) fn new(
style: Arc<ComputedValues>,
grid_style: Arc<ComputedValues>,
base_fragment_info: BaseFragmentInfo,
percentage_columns_allowed_for_inline_content_sizes: bool,
) -> Self {
Self {
style,
grid_style,
grid_base_fragment_info: base_fragment_info,
captions: Vec::new(),
column_groups: Vec::new(),
columns: Vec::new(),
row_groups: Vec::new(),
rows: Vec::new(),
slots: Vec::new(),
size: TableSize::zero(),
anonymous: false,
percentage_columns_allowed_for_inline_content_sizes,
}
}
/// Return the slot at the given coordinates, if it exists in the table, otherwise
/// return None.
fn get_slot(&self, coords: TableSlotCoordinates) -> Option<&TableSlot> {
self.slots.get(coords.y)?.get(coords.x)
}
fn resolve_first_cell_coords(
&self,
coords: TableSlotCoordinates,
) -> Option<TableSlotCoordinates> {
match self.get_slot(coords) {
Some(&TableSlot::Cell(_)) => Some(coords),
Some(TableSlot::Spanned(offsets)) => Some(coords - offsets[0]),
_ => None,
}
}
fn resolve_first_cell(
&self,
coords: TableSlotCoordinates,
) -> Option<AtomicRef<'_, TableSlotCell>> {
let resolved_coords = self.resolve_first_cell_coords(coords)?;
let slot = self.get_slot(resolved_coords);
match slot {
Some(TableSlot::Cell(cell)) => Some(cell.borrow()),
_ => unreachable!(
"Spanned slot should not point to an empty cell or another spanned slot."
),
}
}
}
type TableSlotCoordinates = Point2D<usize, UnknownUnit>;
pub type TableSlotOffset = Vector2D<usize, UnknownUnit>;
#[derive(Debug, MallocSizeOf)]
pub struct TableSlotCell {
/// The [`LayoutBoxBase`] of this table cell.
base: LayoutBoxBase,
/// The contents of this cell, with its own layout.
contents: BlockFormattingContext,
/// Number of columns that the cell is to span. Must be greater than zero.
colspan: usize,
/// Number of rows that the cell is to span. Zero means that the cell is to span all
/// the remaining rows in the row group.
rowspan: usize,
}
impl TableSlotCell {
pub fn mock_for_testing(id: usize, colspan: usize, rowspan: usize) -> Self {
Self {
base: LayoutBoxBase::new(
BaseFragmentInfo::new_for_node(OpaqueNode(id)),
ComputedValues::initial_values_with_font_override(Font::initial_values()).to_arc(),
),
contents: BlockFormattingContext {
contents: BlockContainer::BlockLevelBoxes(Vec::new()),
contains_floats: false,
},
colspan,
rowspan,
}
}
/// Get the node id of this cell's [`BaseFragmentInfo`]. This is used for unit tests.
pub fn node_id(&self) -> usize {
self.base.base_fragment_info.tag.map_or(0, |tag| tag.node.0)
}
}
/// A single table slot. It may be an actual cell, or a reference
/// to a previous cell that is spanned here
///
/// In case of table model errors, it may be multiple references
#[derive(MallocSizeOf)]
pub enum TableSlot {
/// A table cell, with a colspan and a rowspan.
Cell(ArcRefCell<TableSlotCell>),
/// This slot is spanned by one or more multiple cells earlier in the table, which are
/// found at the given negative coordinate offsets. The vector is in the order of most
/// recent to earliest cell.
///
/// If there is more than one cell that spans a slot, this is a table model error, but
/// we still keep track of it. See
/// <https://html.spec.whatwg.org/multipage/#table-model-error>
Spanned(Vec<TableSlotOffset>),
/// An empty spot in the table. This can happen when there is a gap in columns between
/// cells that are defined and one which should exist because of cell with a rowspan
/// from a previous row.
Empty,
}
impl std::fmt::Debug for TableSlot {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Cell(_) => f.debug_tuple("Cell").finish(),
Self::Spanned(spanned) => f.debug_tuple("Spanned").field(spanned).finish(),
Self::Empty => write!(f, "Empty"),
}
}
}
impl TableSlot {
fn new_spanned(offset: TableSlotOffset) -> Self {
Self::Spanned(vec![offset])
}
}
/// A row or column of a table.
#[derive(Debug, MallocSizeOf)]
pub struct TableTrack {
/// The [`LayoutBoxBase`] of this [`TableTrack`].
base: LayoutBoxBase,
/// The index of the table row or column group parent in the table's list of row or column
/// groups.
group_index: Option<usize>,
/// Whether or not this [`TableTrack`] was anonymous, for instance created due to
/// a `span` attribute set on a parent `<colgroup>`.
is_anonymous: bool,
}
#[derive(Debug, MallocSizeOf, PartialEq)]
pub enum TableTrackGroupType {
HeaderGroup,
FooterGroup,
RowGroup,
ColumnGroup,
}
#[derive(Debug, MallocSizeOf)]
pub struct TableTrackGroup {
/// The [`LayoutBoxBase`] of this [`TableTrackGroup`].
base: LayoutBoxBase,
/// The type of this [`TableTrackGroup`].
group_type: TableTrackGroupType,
/// The range of tracks in this [`TableTrackGroup`].
track_range: Range<usize>,
}
impl TableTrackGroup {
pub(super) fn is_empty(&self) -> bool {
self.track_range.is_empty()
}
}
#[derive(Debug, MallocSizeOf)]
pub struct TableCaption {
/// The contents of this cell, with its own layout.
context: IndependentFormattingContext,
}
/// A calculated collapsed border.
#[derive(Clone, Debug, Default, MallocSizeOf, PartialEq)]
pub(crate) struct CollapsedBorder {
pub style_color: BorderStyleColor,
pub width: Au,
}
/// Represents a piecewise sequence of collapsed borders along a line.
pub(crate) type CollapsedBorderLine = Vec<CollapsedBorder>;
#[derive(Clone, Debug, MallocSizeOf)]
pub(crate) struct SpecificTableGridInfo {
pub collapsed_borders: PhysicalVec<Vec<CollapsedBorderLine>>,
pub track_sizes: PhysicalVec<Vec<Au>>,
}
pub(crate) struct TableLayoutStyle<'a> {
table: &'a Table,
layout: Option<&'a TableLayout<'a>>,
}
/// Table parts that are stored in the DOM. This is used in order to map from
/// the DOM to the box tree and will eventually be important for incremental
/// layout.
pub(crate) enum TableLevelBox {
Caption(ArcRefCell<TableCaption>),
Cell(ArcRefCell<TableSlotCell>),
#[allow(dead_code)]
TrackGroup(ArcRefCell<TableTrackGroup>),
#[allow(dead_code)]
Track(ArcRefCell<TableTrack>),
}
impl TableLevelBox {
pub(crate) fn invalidate_cached_fragment(&self) {
match self {
TableLevelBox::Caption(caption) => {
caption.borrow().context.base.invalidate_cached_fragment();
},
TableLevelBox::Cell(cell) => {
cell.borrow().base.invalidate_cached_fragment();
},
TableLevelBox::TrackGroup(track_group) => {
track_group.borrow().base.invalidate_cached_fragment()
},
TableLevelBox::Track(track) => track.borrow().base.invalidate_cached_fragment(),
}
}
pub(crate) fn fragments(&self) -> Vec<Fragment> {
match self {
TableLevelBox::Caption(caption) => caption.borrow().context.base.fragments(),
TableLevelBox::Cell(cell) => cell.borrow().base.fragments(),
TableLevelBox::TrackGroup(track_group) => track_group.borrow().base.fragments(),
TableLevelBox::Track(track) => track.borrow().base.fragments(),
}
}
}

View file

@ -0,0 +1,663 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 app_units::Au;
use atomic_refcell::{AtomicRef, AtomicRefCell};
use style::Zero;
use style::properties::ComputedValues;
use style::values::specified::align::AlignFlags;
use style::values::specified::box_::DisplayInside;
use taffy::style_helpers::{TaffyMaxContent, TaffyMinContent};
use taffy::{AvailableSpace, MaybeMath, RequestedAxis, RunMode};
use super::{
SpecificTaffyGridInfo, TaffyContainer, TaffyItemBox, TaffyItemBoxInner, TaffyStyloStyle,
};
use crate::cell::ArcRefCell;
use crate::context::LayoutContext;
use crate::formatting_contexts::{
Baselines, IndependentFormattingContext, IndependentFormattingContextContents,
};
use crate::fragment_tree::{
BoxFragment, CollapsedBlockMargins, Fragment, FragmentFlags, SpecificLayoutInfo,
};
use crate::geom::{
LogicalVec2, PhysicalPoint, PhysicalRect, PhysicalSides, PhysicalSize, Size, SizeConstraint,
Sizes,
};
use crate::layout_box_base::CacheableLayoutResult;
use crate::positioned::{AbsolutelyPositionedBox, PositioningContext, PositioningContextLength};
use crate::sizing::{ComputeInlineContentSizes, ContentSizes, InlineContentSizesResult};
use crate::style_ext::{ComputedValuesExt, LayoutStyle};
use crate::{ConstraintSpace, ContainingBlock, ContainingBlockSize};
const DUMMY_NODE_ID: taffy::NodeId = taffy::NodeId::new(u64::MAX);
fn resolve_content_size(constraint: AvailableSpace, content_sizes: ContentSizes) -> f32 {
match constraint {
AvailableSpace::Definite(limit) => {
let min = content_sizes.min_content.to_f32_px();
let max = content_sizes.max_content.to_f32_px();
limit.min(max).max(min)
},
AvailableSpace::MinContent => content_sizes.min_content.to_f32_px(),
AvailableSpace::MaxContent => content_sizes.max_content.to_f32_px(),
}
}
#[inline(always)]
fn with_independant_formatting_context<T>(
item: &mut TaffyItemBoxInner,
cb: impl FnOnce(&IndependentFormattingContext) -> T,
) -> T {
match item {
TaffyItemBoxInner::InFlowBox(context) => cb(context),
TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(abspos_box) => {
cb(&AtomicRefCell::borrow(abspos_box).context)
},
}
}
/// Layout parameters and intermediate results about a taffy container,
/// grouped to avoid passing around many parameters
struct TaffyContainerContext<'a> {
source_child_nodes: &'a [ArcRefCell<TaffyItemBox>],
layout_context: &'a LayoutContext<'a>,
positioning_context: &'a mut PositioningContext,
content_box_size_override: &'a ContainingBlock<'a>,
style: &'a ComputedValues,
specific_layout_info: Option<SpecificLayoutInfo>,
/// Temporary location for children specific info, which will be moved into child fragments
child_specific_layout_infos: Vec<Option<SpecificLayoutInfo>>,
}
struct ChildIter(std::ops::Range<usize>);
impl Iterator for ChildIter {
type Item = taffy::NodeId;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(taffy::NodeId::from)
}
}
impl taffy::TraversePartialTree for TaffyContainerContext<'_> {
type ChildIter<'a>
= ChildIter
where
Self: 'a;
fn child_ids(&self, _node_id: taffy::NodeId) -> Self::ChildIter<'_> {
ChildIter(0..self.source_child_nodes.len())
}
fn child_count(&self, _node_id: taffy::NodeId) -> usize {
self.source_child_nodes.len()
}
fn get_child_id(&self, _node_id: taffy::NodeId, index: usize) -> taffy::NodeId {
taffy::NodeId::from(index)
}
}
impl taffy::LayoutPartialTree for TaffyContainerContext<'_> {
type CoreContainerStyle<'a>
= TaffyStyloStyle<&'a ComputedValues>
where
Self: 'a;
fn get_core_container_style(&self, _node_id: taffy::NodeId) -> Self::CoreContainerStyle<'_> {
TaffyStyloStyle(self.style)
}
fn set_unrounded_layout(&mut self, node_id: taffy::NodeId, layout: &taffy::Layout) {
let id = usize::from(node_id);
(*self.source_child_nodes[id]).borrow_mut().taffy_layout = *layout;
}
fn compute_child_layout(
&mut self,
node_id: taffy::NodeId,
inputs: taffy::LayoutInput,
) -> taffy::LayoutOutput {
let mut child = (*self.source_child_nodes[usize::from(node_id)]).borrow_mut();
let child = &mut *child;
fn option_f32_to_size(input: Option<f32>) -> Size<Au> {
match input {
None => Size::Initial,
Some(length) => Size::Numeric(Au::from_f32_px(length)),
}
}
with_independant_formatting_context(
&mut child.taffy_level_box,
|independent_context| -> taffy::LayoutOutput {
// TODO: re-evaluate sizing constraint conversions in light of recent layout changes
let containing_block = &self.content_box_size_override;
let style = independent_context.style();
// Adjust known_dimensions from border box to content box
let pbm = independent_context
.layout_style()
.padding_border_margin(containing_block);
let pb_sum = pbm.padding_border_sums.map(|v| v.to_f32_px());
let margin_sum = pbm.margin.auto_is(Au::zero).sum().map(|v| v.to_f32_px());
let content_box_inset = pb_sum + margin_sum;
let content_box_known_dimensions = taffy::Size {
width: inputs
.known_dimensions
.width
.map(|width| width - pb_sum.inline),
height: inputs
.known_dimensions
.height
.map(|height| height - pb_sum.block),
};
match &independent_context.contents {
IndependentFormattingContextContents::Replaced(replaced) => {
let content_box_size = replaced
.used_size_as_if_inline_element_from_content_box_sizes(
containing_block,
style,
independent_context
.preferred_aspect_ratio(&pbm.padding_border_sums),
LogicalVec2 {
block: &Sizes::new(
option_f32_to_size(content_box_known_dimensions.height),
Size::Initial,
Size::Initial,
),
inline: &Sizes::new(
option_f32_to_size(content_box_known_dimensions.width),
Size::Initial,
Size::Initial,
),
},
Size::FitContent.into(),
pbm.padding_border_sums + pbm.margin.auto_is(Au::zero).sum(),
)
.to_physical_size(self.style.writing_mode);
// Create fragments if the RunMode if PerformLayout
// If the RunMode is ComputeSize then only the returned size will be used
if inputs.run_mode == RunMode::PerformLayout {
child.child_fragments = replaced.make_fragments(
self.layout_context,
style,
content_box_size,
);
}
let computed_size = taffy::Size {
width: inputs.known_dimensions.width.unwrap_or_else(|| {
content_box_size.width.to_f32_px() + pb_sum.inline
}),
height: inputs.known_dimensions.height.unwrap_or_else(|| {
content_box_size.height.to_f32_px() + pb_sum.block
}),
};
let size = inputs.known_dimensions.unwrap_or(computed_size);
taffy::LayoutOutput {
size,
..taffy::LayoutOutput::DEFAULT
}
},
IndependentFormattingContextContents::NonReplaced(non_replaced) => {
// Compute inline size
let inline_size = content_box_known_dimensions.width.unwrap_or_else(|| {
let constraint_space = ConstraintSpace {
// TODO: pass min- and max- size
block_size: SizeConstraint::new(
inputs.parent_size.height.map(Au::from_f32_px),
Au::zero(),
None,
),
writing_mode: self.style.writing_mode,
preferred_aspect_ratio: non_replaced.preferred_aspect_ratio(),
};
let result = independent_context
.inline_content_sizes(self.layout_context, &constraint_space);
let adjusted_available_space = inputs
.available_space
.width
.map_definite_value(|width| width - content_box_inset.inline);
resolve_content_size(adjusted_available_space, result.sizes)
});
// Return early if only inline content sizes are requested
if inputs.run_mode == RunMode::ComputeSize &&
inputs.axis == RequestedAxis::Horizontal
{
return taffy::LayoutOutput::from_outer_size(taffy::Size {
width: inline_size + pb_sum.inline,
// If RequestedAxis is Horizontal then height will be ignored.
height: 0.0,
});
}
let content_box_size_override = ContainingBlock {
size: ContainingBlockSize {
inline: Au::from_f32_px(inline_size),
block: content_box_known_dimensions
.height
.map(Au::from_f32_px)
.map_or_else(SizeConstraint::default, SizeConstraint::Definite),
},
style,
};
let layout = {
let mut child_positioning_context =
PositioningContext::new_for_style(style).unwrap_or_else(|| {
PositioningContext::new_for_subtree(
self.positioning_context
.collects_for_nearest_positioned_ancestor(),
)
});
let layout = non_replaced.layout_without_caching(
self.layout_context,
&mut child_positioning_context,
&content_box_size_override,
containing_block,
false, /* depends_on_block_constraints */
);
// Store layout data on child for later access
child.positioning_context = child_positioning_context;
layout
};
child.child_fragments = layout.fragments;
self.child_specific_layout_infos[usize::from(node_id)] =
layout.specific_layout_info;
let block_size = layout.content_block_size.to_f32_px();
let computed_size = taffy::Size {
width: inline_size + pb_sum.inline,
height: block_size + pb_sum.block,
};
let size = inputs.known_dimensions.unwrap_or(computed_size);
taffy::LayoutOutput {
size,
first_baselines: taffy::Point {
x: None,
y: layout.baselines.first.map(|au| au.to_f32_px()),
},
..taffy::LayoutOutput::DEFAULT
}
},
}
},
)
}
}
impl taffy::LayoutGridContainer for TaffyContainerContext<'_> {
type GridContainerStyle<'a>
= TaffyStyloStyle<&'a ComputedValues>
where
Self: 'a;
type GridItemStyle<'a>
= TaffyStyloStyle<AtomicRef<'a, ComputedValues>>
where
Self: 'a;
fn get_grid_container_style(
&self,
_node_id: taffy::prelude::NodeId,
) -> Self::GridContainerStyle<'_> {
TaffyStyloStyle(self.style)
}
fn get_grid_child_style(
&self,
child_node_id: taffy::prelude::NodeId,
) -> Self::GridItemStyle<'_> {
let id = usize::from(child_node_id);
let child = (*self.source_child_nodes[id]).borrow();
TaffyStyloStyle(AtomicRef::map(child, |c| &*c.style))
}
fn set_detailed_grid_info(
&mut self,
_node_id: taffy::NodeId,
specific_layout_info: taffy::DetailedGridInfo,
) {
self.specific_layout_info = Some(SpecificLayoutInfo::Grid(Box::new(
SpecificTaffyGridInfo::from_detailed_grid_layout(specific_layout_info),
)));
}
}
impl ComputeInlineContentSizes for TaffyContainer {
fn compute_inline_content_sizes(
&self,
layout_context: &LayoutContext,
_constraint_space: &ConstraintSpace,
) -> InlineContentSizesResult {
let style = &self.style;
let max_content_inputs = taffy::LayoutInput {
run_mode: taffy::RunMode::ComputeSize,
sizing_mode: taffy::SizingMode::InherentSize,
axis: taffy::RequestedAxis::Horizontal,
vertical_margins_are_collapsible: taffy::Line::FALSE,
known_dimensions: taffy::Size::NONE,
parent_size: taffy::Size::NONE,
available_space: taffy::Size::MAX_CONTENT,
};
let min_content_inputs = taffy::LayoutInput {
available_space: taffy::Size::MIN_CONTENT,
..max_content_inputs
};
let containing_block = &ContainingBlock {
size: ContainingBlockSize {
inline: Au::zero(),
block: SizeConstraint::default(),
},
style,
};
let mut grid_context = TaffyContainerContext {
layout_context,
positioning_context:
&mut PositioningContext::new_for_containing_block_for_all_descendants(),
content_box_size_override: containing_block,
style,
source_child_nodes: &self.children,
specific_layout_info: None,
child_specific_layout_infos: vec![None; self.children.len()],
};
let (max_content_output, min_content_output) = match style.clone_display().inside() {
DisplayInside::Grid => {
let max_content_output = taffy::compute_grid_layout(
&mut grid_context,
DUMMY_NODE_ID,
max_content_inputs,
);
let min_content_output = taffy::compute_grid_layout(
&mut grid_context,
DUMMY_NODE_ID,
min_content_inputs,
);
(max_content_output, min_content_output)
},
_ => panic!("Servo is only configured to use Taffy for CSS Grid layout"),
};
let pb_sums = self
.layout_style()
.padding_border_margin(containing_block)
.padding_border_sums;
InlineContentSizesResult {
sizes: ContentSizes {
max_content: Au::from_f32_px(max_content_output.size.width) - pb_sums.inline,
min_content: Au::from_f32_px(min_content_output.size.width) - pb_sums.inline,
},
// TODO: determine this accurately
//
// "true" is a safe default as it will prevent Servo from performing optimizations based
// on the assumption that the node's size does not depend on block constraints.
depends_on_block_constraints: true,
}
}
}
impl TaffyContainer {
/// <https://drafts.csswg.org/css-grid/#layout-algorithm>
pub(crate) fn layout(
&self,
layout_context: &LayoutContext,
positioning_context: &mut PositioningContext,
content_box_size_override: &ContainingBlock,
containing_block: &ContainingBlock,
) -> CacheableLayoutResult {
let mut container_ctx = TaffyContainerContext {
layout_context,
positioning_context,
content_box_size_override,
style: content_box_size_override.style,
source_child_nodes: &self.children,
specific_layout_info: None,
child_specific_layout_infos: vec![None; self.children.len()],
};
let container_style = &content_box_size_override.style;
let align_items = container_style.clone_align_items();
let justify_items = container_style.clone_justify_items();
let pbm = self.layout_style().padding_border_margin(containing_block);
let known_dimensions = taffy::Size {
width: Some(
(content_box_size_override.size.inline + pbm.padding_border_sums.inline)
.to_f32_px(),
),
height: content_box_size_override
.size
.block
.to_definite()
.map(Au::to_f32_px)
.maybe_add(pbm.padding_border_sums.block.to_f32_px()),
};
let taffy_containing_block = taffy::Size {
width: Some(containing_block.size.inline.to_f32_px()),
height: containing_block.size.block.to_definite().map(Au::to_f32_px),
};
let layout_input = taffy::LayoutInput {
run_mode: taffy::RunMode::PerformLayout,
sizing_mode: taffy::SizingMode::InherentSize,
axis: taffy::RequestedAxis::Vertical,
vertical_margins_are_collapsible: taffy::Line::FALSE,
known_dimensions,
parent_size: taffy_containing_block,
available_space: taffy_containing_block.map(AvailableSpace::from),
};
let output = match container_ctx.style.clone_display().inside() {
DisplayInside::Grid => {
taffy::compute_grid_layout(&mut container_ctx, DUMMY_NODE_ID, layout_input)
},
_ => panic!("Servo is only configured to use Taffy for CSS Grid layout"),
};
// Convert `taffy::Layout` into Servo `Fragment`s
// with container_ctx.child_specific_layout_infos will also moved to the corresponding `Fragment`s
let fragments: Vec<Fragment> = self
.children
.iter()
.map(|child| (**child).borrow_mut())
.enumerate()
.map(|(child_id, mut child)| {
fn rect_to_physical_sides<T>(rect: taffy::Rect<T>) -> PhysicalSides<T> {
PhysicalSides::new(rect.top, rect.right, rect.bottom, rect.left)
}
fn size_and_pos_to_logical_rect<T: Default>(
position: taffy::Point<T>,
size: taffy::Size<T>,
) -> PhysicalRect<T> {
PhysicalRect::new(
PhysicalPoint::new(position.x, position.y),
PhysicalSize::new(size.width, size.height),
)
}
let layout = &child.taffy_layout;
let padding = rect_to_physical_sides(layout.padding.map(Au::from_f32_px));
let border = rect_to_physical_sides(layout.border.map(Au::from_f32_px));
let margin = rect_to_physical_sides(layout.margin.map(Au::from_f32_px));
// Compute content box size and position.
//
// For the x/y position we have to correct for the difference between the
// content box and the border box for both the parent and the child.
let content_size = size_and_pos_to_logical_rect(
taffy::Point {
x: Au::from_f32_px(
layout.location.x + layout.padding.left + layout.border.left,
) - pbm.padding.inline_start -
pbm.border.inline_start,
y: Au::from_f32_px(
layout.location.y + layout.padding.top + layout.border.top,
) - pbm.padding.block_start -
pbm.border.block_start,
},
taffy::Size {
width: layout.size.width -
layout.padding.left -
layout.padding.right -
layout.border.left -
layout.border.right,
height: layout.size.height -
layout.padding.top -
layout.padding.bottom -
layout.border.top -
layout.border.bottom,
}
.map(Au::from_f32_px),
);
let child_specific_layout_info: Option<SpecificLayoutInfo> =
std::mem::take(&mut container_ctx.child_specific_layout_infos[child_id]);
let establishes_containing_block_for_absolute_descendants =
if let TaffyItemBoxInner::InFlowBox(independent_box) = &child.taffy_level_box {
child
.style
.establishes_containing_block_for_absolute_descendants(
independent_box.base_fragment_info().flags,
)
} else {
false
};
let fragment = match &mut child.taffy_level_box {
TaffyItemBoxInner::InFlowBox(independent_box) => {
let mut fragment_info = independent_box.base_fragment_info();
fragment_info
.flags
.insert(FragmentFlags::IS_FLEX_OR_GRID_ITEM);
let mut box_fragment = BoxFragment::new(
fragment_info,
independent_box.style().clone(),
std::mem::take(&mut child.child_fragments),
content_size,
padding,
border,
margin,
None, /* clearance */
)
.with_baselines(Baselines {
first: output.first_baselines.y.map(Au::from_f32_px),
last: None,
})
.with_specific_layout_info(child_specific_layout_info);
if establishes_containing_block_for_absolute_descendants {
child.positioning_context.layout_collected_children(
container_ctx.layout_context,
&mut box_fragment,
);
}
let fragment = Fragment::Box(ArcRefCell::new(box_fragment));
child
.positioning_context
.adjust_static_position_of_hoisted_fragments(
&fragment,
PositioningContextLength::zero(),
);
let child_positioning_context = std::mem::replace(
&mut child.positioning_context,
PositioningContext::new_for_containing_block_for_all_descendants(),
);
container_ctx
.positioning_context
.append(child_positioning_context);
fragment
},
TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(abs_pos_box) => {
fn resolve_alignment(value: AlignFlags, auto: AlignFlags) -> AlignFlags {
match value {
AlignFlags::AUTO => auto,
AlignFlags::NORMAL => AlignFlags::STRETCH,
value => value,
}
}
let hoisted_box = AbsolutelyPositionedBox::to_hoisted(
abs_pos_box.clone(),
PhysicalRect::from_size(PhysicalSize::new(
Au::from_f32_px(output.size.width),
Au::from_f32_px(output.size.height),
)),
LogicalVec2 {
inline: resolve_alignment(
child.style.clone_align_self().0.0,
align_items.0,
),
block: resolve_alignment(
child.style.clone_justify_self().0.0,
justify_items.computed.0,
),
},
container_ctx.style.writing_mode,
);
let hoisted_fragment = hoisted_box.fragment.clone();
container_ctx.positioning_context.push(hoisted_box);
Fragment::AbsoluteOrFixedPositioned(hoisted_fragment)
},
};
if let TaffyItemBoxInner::InFlowBox(independent_formatting_context) =
&child.taffy_level_box
{
independent_formatting_context
.base
.set_fragment(fragment.clone());
}
fragment
})
.collect();
CacheableLayoutResult {
fragments,
content_block_size: Au::from_f32_px(output.size.height) - pbm.padding_border_sums.block,
content_inline_size_for_table: None,
baselines: Baselines::default(),
// TODO: determine this accurately
//
// "true" is a safe default as it will prevent Servo from performing optimizations based
// on the assumption that the node's size does not depend on block constraints.
depends_on_block_constraints: true,
specific_layout_info: container_ctx.specific_layout_info,
collapsible_margins_in_children: CollapsedBlockMargins::zero(),
}
}
#[inline]
pub(crate) fn layout_style(&self) -> LayoutStyle {
LayoutStyle::Default(&self.style)
}
}

View file

@ -0,0 +1,184 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
mod layout;
mod stylo_taffy;
use std::fmt;
use app_units::Au;
use malloc_size_of_derive::MallocSizeOf;
use servo_arc::Arc;
use style::properties::ComputedValues;
use stylo_taffy::TaffyStyloStyle;
use crate::PropagatedBoxTreeData;
use crate::cell::ArcRefCell;
use crate::construct_modern::{ModernContainerBuilder, ModernItemKind};
use crate::context::LayoutContext;
use crate::dom::{LayoutBox, NodeExt};
use crate::dom_traversal::{NodeAndStyleInfo, NonReplacedContents};
use crate::formatting_contexts::IndependentFormattingContext;
use crate::fragment_tree::Fragment;
use crate::positioned::{AbsolutelyPositionedBox, PositioningContext};
#[derive(Debug, MallocSizeOf)]
pub(crate) struct TaffyContainer {
children: Vec<ArcRefCell<TaffyItemBox>>,
#[conditional_malloc_size_of]
style: Arc<ComputedValues>,
}
impl TaffyContainer {
pub fn construct<'dom>(
context: &LayoutContext,
info: &NodeAndStyleInfo<impl NodeExt<'dom>>,
contents: NonReplacedContents,
propagated_data: PropagatedBoxTreeData,
) -> Self {
let mut builder =
ModernContainerBuilder::new(context, info, propagated_data.union(&info.style));
contents.traverse(context, info, &mut builder);
let items = builder.finish();
let children = items
.into_iter()
.map(|item| {
let box_ = match item.kind {
ModernItemKind::InFlow => ArcRefCell::new(TaffyItemBox::new(
TaffyItemBoxInner::InFlowBox(item.formatting_context),
)),
ModernItemKind::OutOfFlow => {
let abs_pos_box =
ArcRefCell::new(AbsolutelyPositionedBox::new(item.formatting_context));
ArcRefCell::new(TaffyItemBox::new(
TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(abs_pos_box),
))
},
};
if let Some(box_slot) = item.box_slot {
box_slot.set(LayoutBox::TaffyItemBox(box_.clone()));
}
box_
})
.collect();
Self {
children,
style: info.style.clone(),
}
}
}
#[derive(MallocSizeOf)]
pub(crate) struct TaffyItemBox {
pub(crate) taffy_layout: taffy::Layout,
pub(crate) child_fragments: Vec<Fragment>,
pub(crate) positioning_context: PositioningContext,
#[conditional_malloc_size_of]
pub(crate) style: Arc<ComputedValues>,
pub(crate) taffy_level_box: TaffyItemBoxInner,
}
#[derive(Debug, MallocSizeOf)]
pub(crate) enum TaffyItemBoxInner {
InFlowBox(IndependentFormattingContext),
OutOfFlowAbsolutelyPositionedBox(ArcRefCell<AbsolutelyPositionedBox>),
}
impl fmt::Debug for TaffyItemBox {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TaffyItemBox")
.field("taffy_layout", &self.taffy_layout)
.field("child_fragments", &self.child_fragments.len())
.field("style", &self.style)
.field("taffy_level_box", &self.taffy_level_box)
.finish()
}
}
impl TaffyItemBox {
fn new(inner: TaffyItemBoxInner) -> Self {
let style: Arc<ComputedValues> = match &inner {
TaffyItemBoxInner::InFlowBox(item) => item.style().clone(),
TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(absbox) => {
(*absbox).borrow().context.style().clone()
},
};
Self {
taffy_layout: Default::default(),
child_fragments: Vec::new(),
positioning_context: PositioningContext::new_for_containing_block_for_all_descendants(),
style,
taffy_level_box: inner,
}
}
pub(crate) fn invalidate_cached_fragment(&mut self) {
self.taffy_layout = Default::default();
self.positioning_context =
PositioningContext::new_for_containing_block_for_all_descendants();
match self.taffy_level_box {
TaffyItemBoxInner::InFlowBox(ref independent_formatting_context) => {
independent_formatting_context
.base
.invalidate_cached_fragment()
},
TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(ref positioned_box) => {
positioned_box
.borrow()
.context
.base
.invalidate_cached_fragment()
},
}
}
pub(crate) fn fragments(&self) -> Vec<Fragment> {
match self.taffy_level_box {
TaffyItemBoxInner::InFlowBox(ref independent_formatting_context) => {
independent_formatting_context.base.fragments()
},
TaffyItemBoxInner::OutOfFlowAbsolutelyPositionedBox(ref positioned_box) => {
positioned_box.borrow().context.base.fragments()
},
}
}
}
/// Details from Taffy grid layout that will be stored
#[derive(Clone, Debug, MallocSizeOf)]
pub(crate) struct SpecificTaffyGridInfo {
pub rows: SpecificTaffyGridTrackInfo,
pub columns: SpecificTaffyGridTrackInfo,
}
impl SpecificTaffyGridInfo {
fn from_detailed_grid_layout(grid_info: taffy::DetailedGridInfo) -> Self {
Self {
rows: SpecificTaffyGridTrackInfo {
sizes: grid_info
.rows
.sizes
.iter()
.map(|size| Au::from_f32_px(*size))
.collect(),
},
columns: SpecificTaffyGridTrackInfo {
sizes: grid_info
.columns
.sizes
.iter()
.map(|size| Au::from_f32_px(*size))
.collect(),
},
}
}
}
#[derive(Clone, Debug, MallocSizeOf)]
pub(crate) struct SpecificTaffyGridTrackInfo {
pub sizes: Box<[Au]>,
}

View file

@ -0,0 +1,347 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
/// Private module of type aliases so we can refer to stylo types with nicer names
mod stylo {
pub(crate) use style::properties::generated::longhands::box_sizing::computed_value::T as BoxSizing;
pub(crate) use style::properties::longhands::aspect_ratio::computed_value::T as AspectRatio;
pub(crate) use style::properties::longhands::position::computed_value::T as Position;
pub(crate) use style::values::computed::{LengthPercentage, Percentage};
pub(crate) use style::values::generics::NonNegative;
pub(crate) use style::values::generics::length::{
GenericLengthPercentageOrNormal, GenericMargin, GenericMaxSize, GenericSize,
};
pub(crate) use style::values::generics::position::{Inset as GenericInset, PreferredRatio};
pub(crate) use style::values::specified::align::{AlignFlags, ContentDistribution};
pub(crate) use style::values::specified::box_::{
Display, DisplayInside, DisplayOutside, Overflow,
};
pub(crate) type MarginVal = GenericMargin<LengthPercentage>;
pub(crate) type InsetVal = GenericInset<Percentage, LengthPercentage>;
pub(crate) type Size = GenericSize<NonNegative<LengthPercentage>>;
pub(crate) type MaxSize = GenericMaxSize<NonNegative<LengthPercentage>>;
pub(crate) type Gap = GenericLengthPercentageOrNormal<NonNegative<LengthPercentage>>;
pub(crate) use style::computed_values::grid_auto_flow::T as GridAutoFlow;
pub(crate) use style::values::computed::{GridLine, GridTemplateComponent, ImplicitGridTracks};
pub(crate) use style::values::generics::grid::{
RepeatCount, TrackBreadth, TrackListValue, TrackSize,
};
pub(crate) use style::values::specified::GenericGridTemplateComponent;
}
#[inline]
pub fn length_percentage(val: &stylo::LengthPercentage) -> taffy::LengthPercentage {
if let Some(length) = val.to_length() {
taffy::LengthPercentage::Length(length.px())
} else if let Some(val) = val.to_percentage() {
taffy::LengthPercentage::Percent(val.0)
} else {
// TODO: Support calc
taffy::LengthPercentage::Percent(0.0)
}
}
#[inline]
pub fn dimension(val: &stylo::Size) -> taffy::Dimension {
match val {
stylo::Size::LengthPercentage(val) => length_percentage(&val.0).into(),
stylo::Size::Auto => taffy::Dimension::Auto,
// TODO: implement other values in Taffy
stylo::Size::MaxContent => taffy::Dimension::Auto,
stylo::Size::MinContent => taffy::Dimension::Auto,
stylo::Size::FitContent => taffy::Dimension::Auto,
stylo::Size::FitContentFunction(_) => taffy::Dimension::Auto,
stylo::Size::Stretch => taffy::Dimension::Auto,
// Anchor positioning will be flagged off for time being
stylo::Size::AnchorSizeFunction(_) => unreachable!(),
}
}
#[inline]
pub fn max_size_dimension(val: &stylo::MaxSize) -> taffy::Dimension {
match val {
stylo::MaxSize::LengthPercentage(val) => length_percentage(&val.0).into(),
stylo::MaxSize::None => taffy::Dimension::Auto,
// TODO: implement other values in Taffy
stylo::MaxSize::MaxContent => taffy::Dimension::Auto,
stylo::MaxSize::MinContent => taffy::Dimension::Auto,
stylo::MaxSize::FitContent => taffy::Dimension::Auto,
stylo::MaxSize::FitContentFunction(_) => taffy::Dimension::Auto,
stylo::MaxSize::Stretch => taffy::Dimension::Auto,
// Anchor positioning will be flagged off for time being
stylo::MaxSize::AnchorSizeFunction(_) => unreachable!(),
}
}
#[inline]
pub fn margin(val: &stylo::MarginVal) -> taffy::LengthPercentageAuto {
match val {
stylo::MarginVal::Auto => taffy::LengthPercentageAuto::Auto,
stylo::MarginVal::LengthPercentage(val) => length_percentage(val).into(),
// Anchor positioning will be flagged off for time being
stylo::MarginVal::AnchorSizeFunction(_) => unreachable!(),
}
}
#[inline]
pub fn inset(val: &stylo::InsetVal) -> taffy::LengthPercentageAuto {
match val {
stylo::InsetVal::Auto => taffy::LengthPercentageAuto::Auto,
stylo::InsetVal::LengthPercentage(val) => length_percentage(val).into(),
// Anchor positioning will be flagged off for time being
stylo::InsetVal::AnchorSizeFunction(_) => unreachable!(),
stylo::InsetVal::AnchorFunction(_) => unreachable!(),
}
}
#[inline]
pub fn is_block(input: stylo::Display) -> bool {
matches!(input.outside(), stylo::DisplayOutside::Block) &&
matches!(
input.inside(),
stylo::DisplayInside::Flow | stylo::DisplayInside::FlowRoot
)
}
#[inline]
pub fn box_generation_mode(input: stylo::Display) -> taffy::BoxGenerationMode {
match input.inside() {
stylo::DisplayInside::None => taffy::BoxGenerationMode::None,
_ => taffy::BoxGenerationMode::Normal,
}
}
#[inline]
pub fn box_sizing(input: stylo::BoxSizing) -> taffy::BoxSizing {
match input {
stylo::BoxSizing::BorderBox => taffy::BoxSizing::BorderBox,
stylo::BoxSizing::ContentBox => taffy::BoxSizing::ContentBox,
}
}
#[inline]
pub fn position(input: stylo::Position) -> taffy::Position {
match input {
// TODO: support position:static
stylo::Position::Relative => taffy::Position::Relative,
stylo::Position::Static => taffy::Position::Relative,
// TODO: support position:fixed and sticky
stylo::Position::Absolute => taffy::Position::Absolute,
stylo::Position::Fixed => taffy::Position::Absolute,
stylo::Position::Sticky => taffy::Position::Relative,
}
}
#[inline]
pub fn overflow(input: stylo::Overflow) -> taffy::Overflow {
// TODO: Enable Overflow::Clip in servo configuration of stylo
match input {
stylo::Overflow::Visible => taffy::Overflow::Visible,
stylo::Overflow::Hidden => taffy::Overflow::Hidden,
stylo::Overflow::Scroll => taffy::Overflow::Scroll,
stylo::Overflow::Clip => taffy::Overflow::Clip,
// TODO: Support Overflow::Auto in Taffy
stylo::Overflow::Auto => taffy::Overflow::Scroll,
}
}
#[inline]
pub fn aspect_ratio(input: stylo::AspectRatio) -> Option<f32> {
match input.ratio {
stylo::PreferredRatio::None => None,
stylo::PreferredRatio::Ratio(val) => Some(val.0.0 / val.1.0),
}
}
#[inline]
pub fn content_alignment(input: stylo::ContentDistribution) -> Option<taffy::AlignContent> {
match input.primary().value() {
stylo::AlignFlags::NORMAL => None,
stylo::AlignFlags::AUTO => None,
stylo::AlignFlags::START => Some(taffy::AlignContent::Start),
stylo::AlignFlags::END => Some(taffy::AlignContent::End),
stylo::AlignFlags::LEFT => Some(taffy::AlignContent::Start),
stylo::AlignFlags::RIGHT => Some(taffy::AlignContent::End),
stylo::AlignFlags::FLEX_START => Some(taffy::AlignContent::FlexStart),
stylo::AlignFlags::STRETCH => Some(taffy::AlignContent::Stretch),
stylo::AlignFlags::FLEX_END => Some(taffy::AlignContent::FlexEnd),
stylo::AlignFlags::CENTER => Some(taffy::AlignContent::Center),
stylo::AlignFlags::SPACE_BETWEEN => Some(taffy::AlignContent::SpaceBetween),
stylo::AlignFlags::SPACE_AROUND => Some(taffy::AlignContent::SpaceAround),
stylo::AlignFlags::SPACE_EVENLY => Some(taffy::AlignContent::SpaceEvenly),
// Should never be hit. But no real reason to panic here.
_ => None,
}
}
#[inline]
pub fn item_alignment(input: stylo::AlignFlags) -> Option<taffy::AlignItems> {
match input.value() {
stylo::AlignFlags::AUTO => None,
stylo::AlignFlags::NORMAL => Some(taffy::AlignItems::Stretch),
stylo::AlignFlags::STRETCH => Some(taffy::AlignItems::Stretch),
stylo::AlignFlags::FLEX_START => Some(taffy::AlignItems::FlexStart),
stylo::AlignFlags::FLEX_END => Some(taffy::AlignItems::FlexEnd),
stylo::AlignFlags::SELF_START => Some(taffy::AlignItems::Start),
stylo::AlignFlags::SELF_END => Some(taffy::AlignItems::End),
stylo::AlignFlags::START => Some(taffy::AlignItems::Start),
stylo::AlignFlags::END => Some(taffy::AlignItems::End),
stylo::AlignFlags::LEFT => Some(taffy::AlignItems::Start),
stylo::AlignFlags::RIGHT => Some(taffy::AlignItems::End),
stylo::AlignFlags::CENTER => Some(taffy::AlignItems::Center),
stylo::AlignFlags::BASELINE => Some(taffy::AlignItems::Baseline),
// Should never be hit. But no real reason to panic here.
_ => None,
}
}
#[inline]
pub fn gap(input: &stylo::Gap) -> taffy::LengthPercentage {
match input {
// For Flexbox and CSS Grid the "normal" value is 0px. This may need to be updated
// if we ever implement multi-column layout.
stylo::Gap::Normal => taffy::LengthPercentage::Length(0.0),
stylo::Gap::LengthPercentage(val) => length_percentage(&val.0),
}
}
// CSS Grid styles
// ===============
#[inline]
pub fn grid_auto_flow(input: stylo::GridAutoFlow) -> taffy::GridAutoFlow {
let is_row = input.contains(stylo::GridAutoFlow::ROW);
let is_dense = input.contains(stylo::GridAutoFlow::DENSE);
match (is_row, is_dense) {
(true, false) => taffy::GridAutoFlow::Row,
(true, true) => taffy::GridAutoFlow::RowDense,
(false, false) => taffy::GridAutoFlow::Column,
(false, true) => taffy::GridAutoFlow::ColumnDense,
}
}
#[inline]
pub fn grid_line(input: &stylo::GridLine) -> taffy::GridPlacement {
if input.is_auto() {
taffy::GridPlacement::Auto
} else if input.is_span {
taffy::style_helpers::span(input.line_num.try_into().unwrap())
} else if input.line_num == 0 {
taffy::GridPlacement::Auto
} else {
taffy::style_helpers::line(input.line_num.try_into().unwrap())
}
}
#[inline]
pub fn grid_template_tracks(
input: &stylo::GridTemplateComponent,
) -> Vec<taffy::TrackSizingFunction> {
match input {
stylo::GenericGridTemplateComponent::None => Vec::new(),
stylo::GenericGridTemplateComponent::TrackList(list) => list
.values
.iter()
.map(|track| match track {
stylo::TrackListValue::TrackSize(size) => {
taffy::TrackSizingFunction::Single(track_size(size))
},
stylo::TrackListValue::TrackRepeat(repeat) => taffy::TrackSizingFunction::Repeat(
track_repeat(repeat.count),
repeat.track_sizes.iter().map(track_size).collect(),
),
})
.collect(),
// TODO: Implement subgrid and masonry
stylo::GenericGridTemplateComponent::Subgrid(_) => Vec::new(),
stylo::GenericGridTemplateComponent::Masonry => Vec::new(),
}
}
#[inline]
pub fn grid_auto_tracks(
input: &stylo::ImplicitGridTracks,
) -> Vec<taffy::NonRepeatedTrackSizingFunction> {
input.0.iter().map(track_size).collect()
}
#[inline]
pub fn track_repeat(input: stylo::RepeatCount<i32>) -> taffy::GridTrackRepetition {
match input {
stylo::RepeatCount::Number(val) => {
taffy::GridTrackRepetition::Count(val.try_into().unwrap())
},
stylo::RepeatCount::AutoFill => taffy::GridTrackRepetition::AutoFill,
stylo::RepeatCount::AutoFit => taffy::GridTrackRepetition::AutoFit,
}
}
#[inline]
pub fn track_size(
input: &stylo::TrackSize<stylo::LengthPercentage>,
) -> taffy::NonRepeatedTrackSizingFunction {
match input {
stylo::TrackSize::Breadth(breadth) => taffy::MinMax {
min: min_track(breadth),
max: max_track(breadth),
},
stylo::TrackSize::Minmax(min, max) => taffy::MinMax {
min: min_track(min),
max: max_track(max),
},
stylo::TrackSize::FitContent(limit) => taffy::MinMax {
min: taffy::MinTrackSizingFunction::Auto,
max: taffy::MaxTrackSizingFunction::FitContent(match limit {
stylo::TrackBreadth::Breadth(lp) => length_percentage(lp),
// Are these valid? Taffy doesn't support this in any case
stylo::TrackBreadth::Fr(_) => unreachable!(),
stylo::TrackBreadth::Auto => unreachable!(),
stylo::TrackBreadth::MinContent => unreachable!(),
stylo::TrackBreadth::MaxContent => unreachable!(),
}),
},
}
}
#[inline]
pub fn min_track(
input: &stylo::TrackBreadth<stylo::LengthPercentage>,
) -> taffy::MinTrackSizingFunction {
match input {
stylo::TrackBreadth::Breadth(lp) => {
taffy::MinTrackSizingFunction::Fixed(length_percentage(lp))
},
stylo::TrackBreadth::Fr(_) => taffy::MinTrackSizingFunction::Auto,
stylo::TrackBreadth::Auto => taffy::MinTrackSizingFunction::Auto,
stylo::TrackBreadth::MinContent => taffy::MinTrackSizingFunction::MinContent,
stylo::TrackBreadth::MaxContent => taffy::MinTrackSizingFunction::MaxContent,
}
}
#[inline]
pub fn max_track(
input: &stylo::TrackBreadth<stylo::LengthPercentage>,
) -> taffy::MaxTrackSizingFunction {
match input {
stylo::TrackBreadth::Breadth(lp) => {
taffy::MaxTrackSizingFunction::Fixed(length_percentage(lp))
},
stylo::TrackBreadth::Fr(val) => taffy::MaxTrackSizingFunction::Fraction(*val),
stylo::TrackBreadth::Auto => taffy::MaxTrackSizingFunction::Auto,
stylo::TrackBreadth::MinContent => taffy::MaxTrackSizingFunction::MinContent,
stylo::TrackBreadth::MaxContent => taffy::MaxTrackSizingFunction::MaxContent,
}
}

View file

@ -0,0 +1,9 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
//! Conversion functions from Stylo types to Taffy types
mod convert;
mod wrapper;
pub use wrapper::TaffyStyloStyle;

View file

@ -0,0 +1,226 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 std::ops::Deref;
use style::properties::ComputedValues;
use super::convert;
/// A wrapper struct for anything that Deref's to a [`ComputedValues`], which
/// implements Taffy's layout traits and can used with Taffy's layout algorithms.
pub struct TaffyStyloStyle<T: Deref<Target = ComputedValues>>(pub T);
impl<T: Deref<Target = ComputedValues>> From<T> for TaffyStyloStyle<T> {
fn from(value: T) -> Self {
Self(value)
}
}
impl<T: Deref<Target = ComputedValues>> taffy::CoreStyle for TaffyStyloStyle<T> {
#[inline]
fn box_generation_mode(&self) -> taffy::BoxGenerationMode {
convert::box_generation_mode(self.0.get_box().display)
}
#[inline]
fn is_block(&self) -> bool {
convert::is_block(self.0.get_box().display)
}
#[inline]
fn box_sizing(&self) -> taffy::BoxSizing {
convert::box_sizing(self.0.get_position().box_sizing)
}
#[inline]
fn overflow(&self) -> taffy::Point<taffy::Overflow> {
let box_styles = self.0.get_box();
taffy::Point {
x: convert::overflow(box_styles.overflow_x),
y: convert::overflow(box_styles.overflow_y),
}
}
#[inline]
fn scrollbar_width(&self) -> f32 {
0.0
}
#[inline]
fn position(&self) -> taffy::Position {
convert::position(self.0.get_box().position)
}
#[inline]
fn inset(&self) -> taffy::Rect<taffy::LengthPercentageAuto> {
let position_styles = self.0.get_position();
taffy::Rect {
left: convert::inset(&position_styles.left),
right: convert::inset(&position_styles.right),
top: convert::inset(&position_styles.top),
bottom: convert::inset(&position_styles.bottom),
}
}
#[inline]
fn size(&self) -> taffy::Size<taffy::Dimension> {
let position_styles = self.0.get_position();
taffy::Size {
width: convert::dimension(&position_styles.width),
height: convert::dimension(&position_styles.height),
}
}
#[inline]
fn min_size(&self) -> taffy::Size<taffy::Dimension> {
let position_styles = self.0.get_position();
taffy::Size {
width: convert::dimension(&position_styles.min_width),
height: convert::dimension(&position_styles.min_height),
}
}
#[inline]
fn max_size(&self) -> taffy::Size<taffy::Dimension> {
let position_styles = self.0.get_position();
taffy::Size {
width: convert::max_size_dimension(&position_styles.max_width),
height: convert::max_size_dimension(&position_styles.max_height),
}
}
#[inline]
fn aspect_ratio(&self) -> Option<f32> {
convert::aspect_ratio(self.0.get_position().aspect_ratio)
}
#[inline]
fn margin(&self) -> taffy::Rect<taffy::LengthPercentageAuto> {
let margin_styles = self.0.get_margin();
taffy::Rect {
left: convert::margin(&margin_styles.margin_left),
right: convert::margin(&margin_styles.margin_right),
top: convert::margin(&margin_styles.margin_top),
bottom: convert::margin(&margin_styles.margin_bottom),
}
}
#[inline]
fn padding(&self) -> taffy::Rect<taffy::LengthPercentage> {
let padding_styles = self.0.get_padding();
taffy::Rect {
left: convert::length_percentage(&padding_styles.padding_left.0),
right: convert::length_percentage(&padding_styles.padding_right.0),
top: convert::length_percentage(&padding_styles.padding_top.0),
bottom: convert::length_percentage(&padding_styles.padding_bottom.0),
}
}
#[inline]
fn border(&self) -> taffy::Rect<taffy::LengthPercentage> {
let border_styles = self.0.get_border();
taffy::Rect {
left: taffy::LengthPercentage::Length(border_styles.border_left_width.to_f32_px()),
right: taffy::LengthPercentage::Length(border_styles.border_right_width.to_f32_px()),
top: taffy::LengthPercentage::Length(border_styles.border_top_width.to_f32_px()),
bottom: taffy::LengthPercentage::Length(border_styles.border_bottom_width.to_f32_px()),
}
}
}
impl<T: Deref<Target = ComputedValues>> taffy::GridContainerStyle for TaffyStyloStyle<T> {
type TemplateTrackList<'a>
= Vec<taffy::TrackSizingFunction>
where
Self: 'a;
type AutoTrackList<'a>
= Vec<taffy::NonRepeatedTrackSizingFunction>
where
Self: 'a;
#[inline]
fn grid_template_rows(&self) -> Self::TemplateTrackList<'_> {
convert::grid_template_tracks(&self.0.get_position().grid_template_rows)
}
#[inline]
fn grid_template_columns(&self) -> Self::TemplateTrackList<'_> {
convert::grid_template_tracks(&self.0.get_position().grid_template_columns)
}
#[inline]
fn grid_auto_rows(&self) -> Self::AutoTrackList<'_> {
convert::grid_auto_tracks(&self.0.get_position().grid_auto_rows)
}
#[inline]
fn grid_auto_columns(&self) -> Self::AutoTrackList<'_> {
convert::grid_auto_tracks(&self.0.get_position().grid_auto_columns)
}
#[inline]
fn grid_auto_flow(&self) -> taffy::GridAutoFlow {
convert::grid_auto_flow(self.0.get_position().grid_auto_flow)
}
#[inline]
fn gap(&self) -> taffy::Size<taffy::LengthPercentage> {
let position_styles = self.0.get_position();
taffy::Size {
width: convert::gap(&position_styles.column_gap),
height: convert::gap(&position_styles.row_gap),
}
}
#[inline]
fn align_content(&self) -> Option<taffy::AlignContent> {
convert::content_alignment(self.0.get_position().align_content.0)
}
#[inline]
fn justify_content(&self) -> Option<taffy::JustifyContent> {
convert::content_alignment(self.0.get_position().justify_content.0)
}
#[inline]
fn align_items(&self) -> Option<taffy::AlignItems> {
convert::item_alignment(self.0.get_position().align_items.0)
}
#[inline]
fn justify_items(&self) -> Option<taffy::AlignItems> {
convert::item_alignment(self.0.get_position().justify_items.computed.0)
}
}
impl<T: Deref<Target = ComputedValues>> taffy::GridItemStyle for TaffyStyloStyle<T> {
#[inline]
fn grid_row(&self) -> taffy::Line<taffy::GridPlacement> {
let position_styles = self.0.get_position();
taffy::Line {
start: convert::grid_line(&position_styles.grid_row_start),
end: convert::grid_line(&position_styles.grid_row_end),
}
}
#[inline]
fn grid_column(&self) -> taffy::Line<taffy::GridPlacement> {
let position_styles = self.0.get_position();
taffy::Line {
start: convert::grid_line(&position_styles.grid_column_start),
end: convert::grid_line(&position_styles.grid_column_end),
}
}
#[inline]
fn align_self(&self) -> Option<taffy::AlignSelf> {
convert::item_alignment(self.0.get_position().align_self.0.0)
}
#[inline]
fn justify_self(&self) -> Option<taffy::AlignSelf> {
convert::item_alignment(self.0.get_position().justify_self.0.0)
}
}

View file

@ -0,0 +1,854 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
//! Property-based randomized testing for the core float layout algorithm.
use std::f32::INFINITY;
use std::ops::Range;
use std::panic::{self, PanicHookInfo};
use std::sync::{Mutex, MutexGuard};
use std::{thread, u32};
use app_units::Au;
use layout::flow::float::{
Clear, ContainingBlockPositionInfo, FloatBand, FloatBandNode, FloatBandTree, FloatContext,
FloatSide, PlacementInfo,
};
use layout::geom::{LogicalRect, LogicalVec2};
use num_traits::identities::Zero;
use quickcheck::{Arbitrary, Gen};
static PANIC_HOOK_MUTEX: Mutex<()> = Mutex::new(());
// Suppresses panic messages. Some tests need to fail and we don't want them to spam the console.
// Note that, because the panic hook is process-wide, tests that are expected to fail might
// suppress panic messages from other failing tests. To work around this, run failing tests one at
// a time or use only a single test thread.
struct PanicMsgSuppressor<'a> {
#[allow(dead_code)]
mutex_guard: MutexGuard<'a, ()>,
prev_hook: Option<Box<dyn Fn(&PanicHookInfo<'_>) + 'static + Sync + Send>>,
}
impl<'a> PanicMsgSuppressor<'a> {
fn new(mutex_guard: MutexGuard<'a, ()>) -> PanicMsgSuppressor<'a> {
let prev_hook = panic::take_hook();
panic::set_hook(Box::new(|_| ()));
PanicMsgSuppressor {
mutex_guard,
prev_hook: Some(prev_hook),
}
}
}
impl<'a> Drop for PanicMsgSuppressor<'a> {
fn drop(&mut self) {
panic::set_hook(self.prev_hook.take().unwrap())
}
}
// AA tree helpers
#[derive(Clone, Debug)]
struct FloatBandWrapper(FloatBand);
impl Arbitrary for FloatBandWrapper {
fn arbitrary(generator: &mut Gen) -> FloatBandWrapper {
let top: u32 = u32::arbitrary(generator);
let inline_start: Option<u32> = Some(u32::arbitrary(generator));
let inline_end: Option<u32> = Some(u32::arbitrary(generator));
FloatBandWrapper(FloatBand {
top: Au::from_f32_px(top as f32),
inline_start: inline_start.map(|value| Au::from_f32_px(value as f32)),
inline_end: inline_end.map(|value| Au::from_f32_px(value as f32)),
})
}
}
#[derive(Clone, Debug)]
struct FloatRangeInput {
start_index: u32,
side: FloatSide,
length: u32,
}
impl Arbitrary for FloatRangeInput {
fn arbitrary(generator: &mut Gen) -> FloatRangeInput {
let start_index: u32 = Arbitrary::arbitrary(generator);
let is_left: bool = Arbitrary::arbitrary(generator);
let length: u32 = Arbitrary::arbitrary(generator);
FloatRangeInput {
start_index,
side: if is_left {
FloatSide::InlineStart
} else {
FloatSide::InlineEnd
},
length,
}
}
}
// AA tree predicates
fn check_node_ordering(node: &FloatBandNode) {
let mid = node.band.top;
if let Some(ref left) = node.left.0 {
assert!(left.band.top < mid);
}
if let Some(ref right) = node.right.0 {
assert!(right.band.top > mid);
}
if let Some(ref left) = node.left.0 {
check_node_ordering(left);
}
if let Some(ref right) = node.right.0 {
check_node_ordering(right);
}
}
// https://en.wikipedia.org/wiki/AA_tree#Balancing_rotations
fn check_node_balance(node: &FloatBandNode) {
// 1. The level of every leaf node is one.
if node.left.0.is_none() && node.right.0.is_none() {
assert_eq!(node.level, 1);
}
// 2. The level of every left child is exactly one less than that of its parent.
if let Some(ref left) = node.left.0 {
assert_eq!(left.level, node.level - 1);
}
// 3. The level of every right child is equal to or one less than that of its parent.
if let Some(ref right) = node.right.0 {
assert!(right.level == node.level || right.level == node.level - 1);
}
// 4. The level of every right grandchild is strictly less than that of its grandparent.
if let Some(ref right) = node.right.0 {
if let Some(ref right_right) = right.right.0 {
assert!(right_right.level < node.level);
}
}
// 5. Every node of level greater than one has two children.
if node.level > 1 {
assert!(node.left.0.is_some() && node.right.0.is_some());
}
}
fn check_tree_ordering(tree: FloatBandTree) {
if let Some(ref root) = tree.root.0 {
check_node_ordering(root);
}
}
fn check_tree_balance(tree: FloatBandTree) {
if let Some(ref root) = tree.root.0 {
check_node_balance(root);
}
}
fn check_tree_find(tree: &FloatBandTree, block_position: Au, sorted_bands: &[FloatBand]) {
let found_band = tree
.find(block_position)
.expect("Couldn't find the band in the tree!");
let reference_band_index = sorted_bands
.iter()
.position(|band| band.top > block_position)
.expect("Couldn't find the reference band!") -
1;
let reference_band = &sorted_bands[reference_band_index];
assert_eq!(found_band.top, reference_band.top);
assert_eq!(found_band.inline_start, reference_band.inline_start);
assert_eq!(found_band.inline_end, reference_band.inline_end);
}
fn check_tree_find_next(tree: &FloatBandTree, block_position: Au, sorted_bands: &[FloatBand]) {
let found_band = tree
.find_next(block_position)
.expect("Couldn't find the band in the tree!");
let reference_band_index = sorted_bands
.iter()
.position(|band| band.top > block_position)
.expect("Couldn't find the reference band!");
let reference_band = &sorted_bands[reference_band_index];
assert_eq!(found_band.top, reference_band.top);
assert_eq!(found_band.inline_start, reference_band.inline_start);
assert_eq!(found_band.inline_end, reference_band.inline_end);
}
fn check_node_range_setting(
node: &FloatBandNode,
block_range: &Range<Au>,
side: FloatSide,
value: Au,
) {
if node.band.top >= block_range.start && node.band.top < block_range.end {
match side {
FloatSide::InlineStart => assert!(node.band.inline_start.unwrap() >= value),
FloatSide::InlineEnd => assert!(node.band.inline_end.unwrap() <= value),
}
}
if let Some(ref left) = node.left.0 {
check_node_range_setting(left, block_range, side, value)
}
if let Some(ref right) = node.right.0 {
check_node_range_setting(right, block_range, side, value)
}
}
fn check_tree_range_setting(
tree: &FloatBandTree,
block_range: &Range<Au>,
side: FloatSide,
value: Au,
) {
if let Some(ref root) = tree.root.0 {
check_node_range_setting(root, block_range, side, value)
}
}
// AA tree unit tests
// Tests that the tree is a properly-ordered binary tree.
#[test]
fn test_tree_ordering() {
let f: fn(Vec<FloatBandWrapper>) = check;
quickcheck::quickcheck(f);
fn check(bands: Vec<FloatBandWrapper>) {
let mut tree = FloatBandTree::new();
for FloatBandWrapper(band) in bands {
tree = tree.insert(band);
}
check_tree_ordering(tree);
}
}
// Tests that the tree is balanced (i.e. AA tree invariants are maintained).
#[test]
fn test_tree_balance() {
let f: fn(Vec<FloatBandWrapper>) = check;
quickcheck::quickcheck(f);
fn check(bands: Vec<FloatBandWrapper>) {
let mut tree = FloatBandTree::new();
for FloatBandWrapper(band) in bands {
tree = tree.insert(band);
}
check_tree_balance(tree);
}
}
// Tests that the `find()` method works.
#[test]
fn test_tree_find() {
let f: fn(Vec<FloatBandWrapper>, Vec<u16>) = check;
quickcheck::quickcheck(f);
fn check(bands: Vec<FloatBandWrapper>, lookups: Vec<u16>) {
let mut bands: Vec<FloatBand> = bands.into_iter().map(|band| band.0).collect();
bands.push(FloatBand {
top: Au::zero(),
inline_start: None,
inline_end: None,
});
bands.push(FloatBand {
top: Au::from_f32_px(INFINITY),
inline_start: None,
inline_end: None,
});
let mut tree = FloatBandTree::new();
for band in &bands {
tree = tree.insert(*band);
}
bands.sort_by(|a, b| a.top.partial_cmp(&b.top).unwrap());
for lookup in lookups {
check_tree_find(&tree, Au::from_f32_px(lookup as f32), &bands);
}
}
}
// Tests that the `find_next()` method works.
#[test]
fn test_tree_find_next() {
let f: fn(Vec<FloatBandWrapper>, Vec<u16>) = check;
quickcheck::quickcheck(f);
fn check(bands: Vec<FloatBandWrapper>, lookups: Vec<u16>) {
let mut bands: Vec<FloatBand> = bands.into_iter().map(|band| band.0).collect();
bands.push(FloatBand {
top: Au::zero(),
inline_start: None,
inline_end: None,
});
bands.push(FloatBand {
top: Au::from_f32_px(INFINITY),
inline_start: None,
inline_end: None,
});
bands.sort_by(|a, b| a.top.partial_cmp(&b.top).unwrap());
bands.dedup_by(|a, b| a.top == b.top);
let mut tree = FloatBandTree::new();
for band in &bands {
tree = tree.insert(*band);
}
for lookup in lookups {
check_tree_find_next(&tree, Au::from_f32_px(lookup as f32), &bands);
}
}
}
// Tests that `set_range()` works.
#[test]
fn test_tree_range_setting() {
let f: fn(Vec<FloatBandWrapper>, Vec<FloatRangeInput>) = check;
quickcheck::quickcheck(f);
fn check(bands: Vec<FloatBandWrapper>, ranges: Vec<FloatRangeInput>) {
let mut tree = FloatBandTree::new();
for FloatBandWrapper(band) in &bands {
tree = tree.insert(*band);
}
let mut tops: Vec<Au> = bands.iter().map(|band| band.0.top).collect();
tops.push(Au::from_f32_px(INFINITY));
tops.sort_by(|a, b| a.to_px().partial_cmp(&b.to_px()).unwrap());
for range in ranges {
let start = range.start_index.min(tops.len() as u32 - 1);
let end = (range.start_index as u64 + range.length as u64).min(tops.len() as u64 - 1);
let block_range = tops[start as usize]..tops[end as usize];
let length = Au::from_px(range.length as i32);
let new_tree = tree.set_range(&block_range, range.side, length);
check_tree_range_setting(&new_tree, &block_range, range.side, length);
}
}
}
// Float predicates
#[derive(Clone, Debug)]
struct FloatInput {
// Information needed to place the float.
info: PlacementInfo,
// The float may be placed no higher than this line. This simulates the effect of line boxes
// per CSS 2.1 § 9.5.1 rule 6.
ceiling: Au,
/// Containing block positioning information, which is used to track the current offsets
/// from the float containing block formatting context to the current containing block.
containing_block_info: ContainingBlockPositionInfo,
}
impl Arbitrary for FloatInput {
fn arbitrary(generator: &mut Gen) -> FloatInput {
// See #29819: Limit the maximum size of all f32 values here because
// massive float values will start to introduce very bad floating point
// errors.
// TODO: This should be be addressed in a better way. Perhaps we should
// reintroduce the use of app_units in Layout 2020.
let width = u32::arbitrary(generator) % 12345;
let height = u32::arbitrary(generator) % 12345;
let is_left = bool::arbitrary(generator);
let ceiling = u32::arbitrary(generator) % 12345;
let left = u32::arbitrary(generator) % 12345;
let containing_block_width = u32::arbitrary(generator) % 12345;
let clear = u8::arbitrary(generator);
FloatInput {
info: PlacementInfo {
size: LogicalVec2 {
inline: Au::from_f32_px(width as f32),
block: Au::from_f32_px(height as f32),
},
side: if is_left {
FloatSide::InlineStart
} else {
FloatSide::InlineEnd
},
clear: new_clear(clear),
},
ceiling: Au::from_f32_px(ceiling as f32),
containing_block_info: ContainingBlockPositionInfo::new_with_inline_offsets(
Au::from_f32_px(left as f32),
Au::from_f32_px(left as f32 + containing_block_width as f32),
),
}
}
fn shrink(&self) -> Box<dyn Iterator<Item = FloatInput>> {
let mut this = (*self).clone();
let mut shrunk = false;
if let Some(inline_size) = self.info.size.inline.to_px().shrink().next() {
this.info.size.inline = Au::from_px(inline_size);
shrunk = true;
}
if let Some(block_size) = self.info.size.block.to_px().shrink().next() {
this.info.size.block = Au::from_px(block_size);
shrunk = true;
}
if let Some(clear) = (self.info.clear as u8).shrink().next() {
this.info.clear = new_clear(clear);
shrunk = true;
}
if let Some(left) = self
.containing_block_info
.inline_start
.to_px()
.shrink()
.next()
{
this.containing_block_info.inline_start = Au::from_px(left);
shrunk = true;
}
if let Some(right) = self
.containing_block_info
.inline_end
.to_px()
.shrink()
.next()
{
this.containing_block_info.inline_end = Au::from_px(right);
shrunk = true;
}
if let Some(ceiling) = self.ceiling.to_px().shrink().next() {
this.ceiling = Au::from_px(ceiling);
shrunk = true;
}
if shrunk {
quickcheck::single_shrinker(this)
} else {
quickcheck::empty_shrinker()
}
}
}
fn new_clear(value: u8) -> Clear {
match value & 3 {
0 => Clear::None,
1 => Clear::InlineStart,
2 => Clear::InlineEnd,
_ => Clear::Both,
}
}
#[derive(Clone)]
struct FloatPlacement {
float_context: FloatContext,
placed_floats: Vec<PlacedFloat>,
}
// Information about the placement of a float.
#[derive(Clone)]
struct PlacedFloat {
origin: LogicalVec2<Au>,
info: PlacementInfo,
ceiling: Au,
containing_block_info: ContainingBlockPositionInfo,
}
impl Drop for FloatPlacement {
fn drop(&mut self) {
if !thread::panicking() {
return;
}
// Dump the float context for debugging.
eprintln!("Failing float placement:");
for placed_float in &self.placed_floats {
eprintln!(
" * {:?} @ {:?}, T {:?} L {:?} R {:?}",
placed_float.info,
placed_float.origin,
placed_float.ceiling,
placed_float.containing_block_info.inline_start,
placed_float.containing_block_info.inline_end,
);
}
eprintln!("Bands:\n{:?}\n", self.float_context.bands);
}
}
impl PlacedFloat {
fn rect(&self) -> LogicalRect<Au> {
LogicalRect {
start_corner: self.origin,
size: self.info.size,
}
}
}
impl FloatPlacement {
fn place(floats: Vec<FloatInput>) -> FloatPlacement {
let mut float_context = FloatContext::new(Au::from_f32_px(INFINITY));
let mut placed_floats = vec![];
for float in floats {
let ceiling = float.ceiling;
float_context.set_ceiling_from_non_floats(ceiling);
float_context.containing_block_info = float.containing_block_info;
placed_floats.push(PlacedFloat {
origin: float_context.add_float(&float.info),
info: float.info,
ceiling,
containing_block_info: float.containing_block_info,
})
}
FloatPlacement {
float_context,
placed_floats,
}
}
}
// From CSS 2.1 § 9.5.1 [1].
//
// [1]: https://www.w3.org/TR/CSS2/visuren.html#float-position
// 1. The left outer edge of a left-floating box may not be to the left of the left edge of its
// containing block. An analogous rule holds for right-floating elements.
fn check_floats_rule_1(placement: &FloatPlacement) {
for placed_float in &placement.placed_floats {
match placed_float.info.side {
FloatSide::InlineStart => assert!(
placed_float.origin.inline >= placed_float.containing_block_info.inline_start
),
FloatSide::InlineEnd => {
assert!(
placed_float.rect().max_inline_position() <=
placed_float.containing_block_info.inline_end
)
},
}
}
}
// 2. If the current box is left-floating, and there are any left-floating boxes generated by
// elements earlier in the source document, then for each such earlier box, either the left
// outer edge of the current box must be to the right of the right outer edge of the earlier
// box, or its top must be lower than the bottom of the earlier box. Analogous rules hold for
// right-floating boxes.
fn check_floats_rule_2(placement: &FloatPlacement) {
for (this_float_index, this_float) in placement.placed_floats.iter().enumerate() {
for prev_float in &placement.placed_floats[0..this_float_index] {
match (this_float.info.side, prev_float.info.side) {
(FloatSide::InlineStart, FloatSide::InlineStart) => {
assert!(
this_float.origin.inline >= prev_float.rect().max_inline_position() ||
this_float.origin.block >= prev_float.rect().max_block_position()
);
},
(FloatSide::InlineEnd, FloatSide::InlineEnd) => {
assert!(
this_float.rect().max_inline_position() <= prev_float.origin.inline ||
this_float.origin.block >= prev_float.rect().max_block_position()
);
},
(FloatSide::InlineStart, FloatSide::InlineEnd) |
(FloatSide::InlineEnd, FloatSide::InlineStart) => {},
}
}
}
}
// 3. The right outer edge of a left-floating box may not be to the right of the left outer edge of
// any right-floating box that is next to it. Analogous rules hold for right-floating elements.
fn check_floats_rule_3(placement: &FloatPlacement) {
for (this_float_index, this_float) in placement.placed_floats.iter().enumerate() {
for other_float in &placement.placed_floats[0..this_float_index] {
// This logic to check intersection is complicated by the fact that we need to treat
// zero-height floats later in the document as "next to" floats earlier in the
// document. Otherwise we might end up with a situation like:
//
// <div id="a" style="float: left; width: 32px; height: 32px"></div>
// <div id="b" style="float: right; width: 0px; height: 0px"></div>
//
// Where the top of `b` should probably be 32px per Rule 3, but unless this distinction
// is made the top of `b` could legally be 0px.
if this_float.origin.block >= other_float.rect().max_block_position() ||
(this_float.info.size.block.is_zero() &&
this_float.rect().max_block_position() < other_float.origin.block) ||
(this_float.info.size.block > Au::zero() &&
this_float.rect().max_block_position() <= other_float.origin.block)
{
continue;
}
match (this_float.info.side, other_float.info.side) {
(FloatSide::InlineStart, FloatSide::InlineEnd) => {
assert!(this_float.rect().max_inline_position() <= other_float.origin.inline);
},
(FloatSide::InlineEnd, FloatSide::InlineStart) => {
assert!(this_float.origin.inline >= other_float.rect().max_inline_position());
},
(FloatSide::InlineStart, FloatSide::InlineStart) |
(FloatSide::InlineEnd, FloatSide::InlineEnd) => {},
}
}
}
}
// 4. A floating box's outer top may not be higher than the top of its containing block. When the
// float occurs between two collapsing margins, the float is positioned as if it had an
// otherwise empty anonymous block parent taking part in the flow. The position of such a parent
// is defined by the rules in the section on margin collapsing.
fn check_floats_rule_4(placement: &FloatPlacement) {
for placed_float in &placement.placed_floats {
assert!(placed_float.origin.block >= Au::zero());
}
}
// 5. The outer top of a floating box may not be higher than the outer top of any block or floated
// box generated by an element earlier in the source document.
fn check_floats_rule_5(placement: &FloatPlacement) {
let mut block_position = Au::zero();
for placed_float in &placement.placed_floats {
assert!(placed_float.origin.block >= block_position);
block_position = placed_float.origin.block;
}
}
// 6. The outer top of an element's floating box may not be higher than the top of any line-box
// containing a box generated by an element earlier in the source document.
fn check_floats_rule_6(placement: &FloatPlacement) {
for placed_float in &placement.placed_floats {
assert!(placed_float.origin.block >= placed_float.ceiling);
}
}
// 7. A left-floating box that has another left-floating box to its left may not have its right
// outer edge to the right of its containing block's right edge. (Loosely: a left float may not
// stick out at the right edge, unless it is already as far to the left as possible.) An
// analogous rule holds for right-floating elements.
fn check_floats_rule_7(placement: &FloatPlacement) {
for (placed_float_index, placed_float) in placement.placed_floats.iter().enumerate() {
// Only consider floats that stick out.
match placed_float.info.side {
FloatSide::InlineStart => {
if placed_float.rect().max_inline_position() <=
placed_float.containing_block_info.inline_end
{
continue;
}
},
FloatSide::InlineEnd => {
if placed_float.origin.inline >= placed_float.containing_block_info.inline_start {
continue;
}
},
}
// Make sure there are no previous floats to the left or right.
for prev_float in &placement.placed_floats[0..placed_float_index] {
assert!(
prev_float.info.side != placed_float.info.side ||
prev_float.rect().max_block_position() <= placed_float.origin.block ||
prev_float.origin.block >= placed_float.rect().max_block_position()
);
}
}
}
// 8. A floating box must be placed as high as possible.
fn check_floats_rule_8(floats_and_perturbations: Vec<(FloatInput, u32)>) {
let floats = floats_and_perturbations
.iter()
.map(|(float, _)| (*float).clone())
.collect();
let placement = FloatPlacement::place(floats);
for (float_index, &(_, perturbation)) in floats_and_perturbations.iter().enumerate() {
if perturbation == 0 {
continue;
}
let mut placement = placement.clone();
placement.placed_floats[float_index].origin.block -= Au::from_f32_px(perturbation as f32);
let result = {
let mutex_guard = PANIC_HOOK_MUTEX.lock().unwrap();
let _suppressor = PanicMsgSuppressor::new(mutex_guard);
panic::catch_unwind(|| check_basic_float_rules(&placement))
};
assert!(result.is_err());
}
}
// 9. A left-floating box must be put as far to the left as possible, a right-floating box as far
// to the right as possible. A higher position is preferred over one that is further to the
// left/right.
fn check_floats_rule_9(floats_and_perturbations: Vec<(FloatInput, u32)>) {
let floats = floats_and_perturbations
.iter()
.map(|(float, _)| (*float).clone())
.collect();
let placement = FloatPlacement::place(floats);
for (float_index, &(_, perturbation)) in floats_and_perturbations.iter().enumerate() {
if perturbation == 0 {
continue;
}
let mut placement = placement.clone();
{
let placed_float = &mut placement.placed_floats[float_index];
let perturbation = Au::from_f32_px(perturbation as f32);
match placed_float.info.side {
FloatSide::InlineStart => placed_float.origin.inline -= perturbation,
FloatSide::InlineEnd => placed_float.origin.inline += perturbation,
}
}
let result = {
let mutex_guard = PANIC_HOOK_MUTEX.lock().unwrap();
let _suppressor = PanicMsgSuppressor::new(mutex_guard);
panic::catch_unwind(|| check_basic_float_rules(&placement))
};
assert!(result.is_err());
}
}
// From CSS 2.1 § 9.5.2 (https://www.w3.org/TR/CSS2/visuren.html#propdef-clear):
//
// 10. The top outer edge of the float must be below the bottom outer edge of all earlier
// left-floating boxes (in the case of 'clear: left'), or all earlier right-floating boxes (in
// the case of 'clear: right'), or both ('clear: both').
fn check_floats_rule_10(placement: &FloatPlacement) {
let mut block_position = Au::zero();
for placed_float in &placement.placed_floats {
assert!(placed_float.origin.block >= block_position);
block_position = placed_float.origin.block;
}
for (this_float_index, this_float) in placement.placed_floats.iter().enumerate() {
if this_float.info.clear == Clear::None {
continue;
}
for other_float in &placement.placed_floats[0..this_float_index] {
// This logic to check intersection is complicated by the fact that we need to treat
// zero-height floats later in the document as "next to" floats earlier in the
// document. Otherwise we might end up with a situation like:
//
// <div id="a" style="float: left; width: 32px; height: 32px"></div>
// <div id="b" style="float: right; width: 0px; height: 0px"></div>
//
// Where the top of `b` should probably be 32px per Rule 3, but unless this distinction
// is made the top of `b` could legally be 0px.
if this_float.origin.block >= other_float.rect().max_block_position() ||
(this_float.info.size.block.is_zero() &&
this_float.rect().max_block_position() < other_float.origin.block) ||
(this_float.info.size.block > Au::zero() &&
this_float.rect().max_block_position() <= other_float.origin.block)
{
continue;
}
match this_float.info.clear {
Clear::InlineStart => assert_ne!(other_float.info.side, FloatSide::InlineStart),
Clear::InlineEnd => assert_ne!(other_float.info.side, FloatSide::InlineEnd),
Clear::Both => assert!(false),
Clear::None => unreachable!(),
}
}
}
}
// Checks that rule 1-7 and rule 10 hold (i.e. all rules that don't specify that floats are placed
// "as far as possible" in some direction).
fn check_basic_float_rules(placement: &FloatPlacement) {
check_floats_rule_1(placement);
check_floats_rule_2(placement);
check_floats_rule_3(placement);
check_floats_rule_4(placement);
check_floats_rule_5(placement);
check_floats_rule_6(placement);
check_floats_rule_7(placement);
check_floats_rule_10(placement);
}
// Float unit tests
#[test]
fn test_floats_rule_1() {
let f: fn(Vec<FloatInput>) = check;
quickcheck::quickcheck(f);
fn check(floats: Vec<FloatInput>) {
check_floats_rule_1(&FloatPlacement::place(floats));
}
}
#[test]
fn test_floats_rule_2() {
let f: fn(Vec<FloatInput>) = check;
quickcheck::quickcheck(f);
fn check(floats: Vec<FloatInput>) {
check_floats_rule_2(&FloatPlacement::place(floats));
}
}
#[test]
fn test_floats_rule_3() {
let f: fn(Vec<FloatInput>) = check;
quickcheck::quickcheck(f);
fn check(floats: Vec<FloatInput>) {
check_floats_rule_3(&FloatPlacement::place(floats));
}
}
#[test]
fn test_floats_rule_4() {
let f: fn(Vec<FloatInput>) = check;
quickcheck::quickcheck(f);
fn check(floats: Vec<FloatInput>) {
check_floats_rule_4(&FloatPlacement::place(floats));
}
}
#[test]
fn test_floats_rule_5() {
let f: fn(Vec<FloatInput>) = check;
quickcheck::quickcheck(f);
fn check(floats: Vec<FloatInput>) {
check_floats_rule_5(&FloatPlacement::place(floats));
}
}
#[test]
fn test_floats_rule_6() {
let f: fn(Vec<FloatInput>) = check;
quickcheck::quickcheck(f);
fn check(floats: Vec<FloatInput>) {
check_floats_rule_6(&FloatPlacement::place(floats));
}
}
#[test]
fn test_floats_rule_7() {
let f: fn(Vec<FloatInput>) = check;
quickcheck::quickcheck(f);
fn check(floats: Vec<FloatInput>) {
check_floats_rule_7(&FloatPlacement::place(floats));
}
}
#[test]
fn test_floats_rule_8() {
let f: fn(Vec<(FloatInput, u32)>) = check;
quickcheck::quickcheck(f);
fn check(floats: Vec<(FloatInput, u32)>) {
check_floats_rule_8(floats);
}
}
#[test]
fn test_floats_rule_9() {
let f: fn(Vec<(FloatInput, u32)>) = check;
quickcheck::quickcheck(f);
fn check(floats: Vec<(FloatInput, u32)>) {
check_floats_rule_9(floats);
}
}
#[test]
fn test_floats_rule_10() {
let f: fn(Vec<FloatInput>) = check;
quickcheck::quickcheck(f);
fn check(floats: Vec<FloatInput>) {
check_floats_rule_10(&FloatPlacement::place(floats));
}
}

View file

@ -0,0 +1,252 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
//! Tests for proper table box tree construction.
mod tables {
use euclid::Vector2D;
use layout::ArcRefCell;
use layout::table::{Table, TableBuilder, TableSlot, TableSlotCell, TableSlotOffset};
fn row_lengths(table: &Table) -> Vec<usize> {
table.slots.iter().map(|row| row.len()).collect()
}
fn slot_is_cell_with_id(slot: &TableSlot, id: usize) -> bool {
match slot {
TableSlot::Cell(cell) if cell.borrow().node_id() == id => true,
_ => false,
}
}
fn slot_is_empty(slot: &TableSlot) -> bool {
match slot {
TableSlot::Empty => true,
_ => false,
}
}
fn slot_is_spanned_with_offsets(slot: &TableSlot, offsets: Vec<(usize, usize)>) -> bool {
match slot {
TableSlot::Spanned(slot_offsets) => {
let offsets: Vec<TableSlotOffset> = offsets
.iter()
.map(|offset| Vector2D::new(offset.0, offset.1))
.collect();
offsets == *slot_offsets
},
_ => false,
}
}
#[test]
fn test_empty_table() {
let table_builder = TableBuilder::new_for_tests();
let table = table_builder.finish();
assert!(table.slots.is_empty())
}
#[test]
fn test_simple_table() {
let mut table_builder = TableBuilder::new_for_tests();
table_builder.start_row();
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(1, 1, 1)));
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(2, 1, 1)));
table_builder.end_row();
table_builder.start_row();
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(3, 1, 1)));
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(4, 1, 1)));
table_builder.end_row();
let table = table_builder.finish();
assert_eq!(row_lengths(&table), vec![2, 2]);
assert!(slot_is_cell_with_id(&table.slots[0][0], 1));
assert!(slot_is_cell_with_id(&table.slots[0][1], 2));
assert!(slot_is_cell_with_id(&table.slots[1][0], 3));
assert!(slot_is_cell_with_id(&table.slots[1][1], 4));
}
#[test]
fn test_simple_rowspan() {
let mut table_builder = TableBuilder::new_for_tests();
table_builder.start_row();
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(1, 1, 1)));
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(2, 1, 1)));
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(3, 1, 2)));
table_builder.end_row();
table_builder.start_row();
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(4, 1, 1)));
table_builder.end_row();
let table = table_builder.finish();
assert_eq!(row_lengths(&table), vec![3, 3]);
assert!(slot_is_cell_with_id(&table.slots[0][0], 1));
assert!(slot_is_cell_with_id(&table.slots[0][1], 2));
assert!(slot_is_cell_with_id(&table.slots[0][2], 3));
assert!(slot_is_cell_with_id(&table.slots[1][0], 4));
assert!(slot_is_empty(&table.slots[1][1]));
assert!(slot_is_spanned_with_offsets(
&table.slots[1][2],
vec![(0, 1)]
));
}
#[test]
fn test_simple_colspan() {
let mut table_builder = TableBuilder::new_for_tests();
table_builder.start_row();
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(1, 3, 1)));
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(2, 1, 1)));
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(3, 1, 1)));
table_builder.end_row();
table_builder.start_row();
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(4, 1, 1)));
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(5, 3, 1)));
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(6, 1, 1)));
table_builder.end_row();
table_builder.start_row();
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(7, 1, 1)));
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(8, 1, 1)));
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(9, 3, 1)));
table_builder.end_row();
let table = table_builder.finish();
assert_eq!(row_lengths(&table), vec![5, 5, 5]);
assert!(slot_is_cell_with_id(&table.slots[0][0], 1));
assert!(slot_is_spanned_with_offsets(
&table.slots[0][1],
vec![(1, 0)]
));
assert!(slot_is_spanned_with_offsets(
&table.slots[0][2],
vec![(2, 0)]
));
assert!(slot_is_cell_with_id(&table.slots[0][3], 2));
assert!(slot_is_cell_with_id(&table.slots[0][4], 3));
assert!(slot_is_cell_with_id(&table.slots[1][0], 4));
assert!(slot_is_cell_with_id(&table.slots[1][1], 5));
assert!(slot_is_spanned_with_offsets(
&table.slots[1][2],
vec![(1, 0)]
));
assert!(slot_is_spanned_with_offsets(
&table.slots[1][3],
vec![(2, 0)]
));
assert!(slot_is_cell_with_id(&table.slots[1][4], 6));
assert!(slot_is_cell_with_id(&table.slots[2][0], 7));
assert!(slot_is_cell_with_id(&table.slots[2][1], 8));
assert!(slot_is_cell_with_id(&table.slots[2][2], 9));
assert!(slot_is_spanned_with_offsets(
&table.slots[2][3],
vec![(1, 0)]
));
assert!(slot_is_spanned_with_offsets(
&table.slots[2][4],
vec![(2, 0)]
));
}
#[test]
fn test_simple_table_model_error() {
let mut table_builder = TableBuilder::new_for_tests();
table_builder.start_row();
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(1, 1, 1)));
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(2, 1, 1)));
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(3, 1, 2)));
table_builder.end_row();
table_builder.start_row();
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(4, 3, 1)));
table_builder.end_row();
let table = table_builder.finish();
assert_eq!(row_lengths(&table), vec![3, 3]);
assert!(slot_is_cell_with_id(&table.slots[0][0], 1));
assert!(slot_is_cell_with_id(&table.slots[0][1], 2));
assert!(slot_is_cell_with_id(&table.slots[0][2], 3));
assert!(slot_is_cell_with_id(&table.slots[1][0], 4));
assert!(slot_is_spanned_with_offsets(
&table.slots[1][1],
vec![(1, 0)]
));
assert!(slot_is_spanned_with_offsets(
&table.slots[1][2],
vec![(2, 0), (0, 1)]
));
}
#[test]
fn test_simple_rowspan_0() {
let mut table_builder = TableBuilder::new_for_tests();
table_builder.start_row();
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(1, 1, 1)));
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(2, 1, 1)));
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(3, 1, 0)));
table_builder.end_row();
table_builder.start_row();
table_builder.end_row();
table_builder.start_row();
table_builder.end_row();
table_builder.start_row();
table_builder.end_row();
let table = table_builder.finish();
assert_eq!(row_lengths(&table), vec![3, 3, 3, 3]);
assert!(slot_is_empty(&table.slots[1][0]));
assert!(slot_is_empty(&table.slots[1][1]));
assert!(slot_is_spanned_with_offsets(
&table.slots[1][2],
vec![(0, 1)]
));
assert!(slot_is_spanned_with_offsets(
&table.slots[2][2],
vec![(0, 2)]
));
assert!(slot_is_spanned_with_offsets(
&table.slots[3][2],
vec![(0, 3)]
));
}
#[test]
fn test_incoming_rowspans() {
let mut table_builder = TableBuilder::new_for_tests();
table_builder.start_row();
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(1, 1, 1)));
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(2, 1, 30)));
table_builder.end_row();
table_builder.start_row();
table_builder.add_cell(ArcRefCell::new(TableSlotCell::mock_for_testing(3, 2, 1)));
table_builder.end_row();
assert_eq!(table_builder.incoming_rowspans, vec![0, 28]);
let table = table_builder.finish();
assert_eq!(row_lengths(&table), vec![2, 2]);
}
}

View file

@ -0,0 +1,63 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
mod text {
use layout::flow::inline::construct::WhitespaceCollapse;
use style::computed_values::white_space_collapse::T as WhiteSpaceCollapse;
#[test]
fn test_collapse_whitespace() {
let collapse = |input: &str, white_space_collapse, trim_beginning_white_space| {
WhitespaceCollapse::new(
input.chars(),
white_space_collapse,
trim_beginning_white_space,
)
.collect::<String>()
};
let output = collapse("H ", WhiteSpaceCollapse::Collapse, false);
assert_eq!(output, "H ");
let output = collapse(" W", WhiteSpaceCollapse::Collapse, true);
assert_eq!(output, "W");
let output = collapse(" W", WhiteSpaceCollapse::Collapse, false);
assert_eq!(output, " W");
let output = collapse(" H W", WhiteSpaceCollapse::Collapse, false);
assert_eq!(output, " H W");
let output = collapse("\n H \n \t W", WhiteSpaceCollapse::Collapse, false);
assert_eq!(output, " H W");
let output = collapse("\n H \n \t W \n", WhiteSpaceCollapse::Preserve, false);
assert_eq!(output, "\n H \n \t W \n");
let output = collapse(
"\n H \n \t W \n ",
WhiteSpaceCollapse::PreserveBreaks,
false,
);
assert_eq!(output, "\nH\nW\n");
let output = collapse("Hello \n World", WhiteSpaceCollapse::PreserveBreaks, true);
assert_eq!(output, "Hello\nWorld");
let output = collapse(" \n World", WhiteSpaceCollapse::PreserveBreaks, true);
assert_eq!(output, "\nWorld");
let output = collapse(" ", WhiteSpaceCollapse::Collapse, true);
assert_eq!(output, "");
let output = collapse(" ", WhiteSpaceCollapse::Collapse, false);
assert_eq!(output, " ");
let output = collapse("\n ", WhiteSpaceCollapse::Collapse, true);
assert_eq!(output, "");
let output = collapse("\n ", WhiteSpaceCollapse::Collapse, false);
assert_eq!(output, " ");
}
}

View file

@ -0,0 +1,74 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 script_layout_interface::wrapper_traits::LayoutNode;
use style::context::{SharedStyleContext, StyleContext};
use style::data::ElementData;
use style::dom::{NodeInfo, TElement, TNode};
use style::traversal::{DomTraversal, PerLevelTraversalData, recalc_style_at};
use crate::context::LayoutContext;
use crate::dom::DOMLayoutData;
pub struct RecalcStyle<'a> {
context: LayoutContext<'a>,
}
impl<'a> RecalcStyle<'a> {
pub fn new(context: LayoutContext<'a>) -> Self {
RecalcStyle { context }
}
pub fn context(&self) -> &LayoutContext<'a> {
&self.context
}
pub fn destroy(self) -> LayoutContext<'a> {
self.context
}
}
#[allow(unsafe_code)]
impl<'dom, E> DomTraversal<E> for RecalcStyle<'_>
where
E: TElement,
E::ConcreteNode: 'dom + LayoutNode<'dom>,
{
fn process_preorder<F>(
&self,
traversal_data: &PerLevelTraversalData,
context: &mut StyleContext<E>,
node: E::ConcreteNode,
note_child: F,
) where
F: FnMut(E::ConcreteNode),
{
unsafe {
node.initialize_style_and_layout_data::<DOMLayoutData>();
if !node.is_text_node() {
let el = node.as_element().unwrap();
let mut data = el.mutate_data().unwrap();
recalc_style_at(self, traversal_data, context, el, &mut data, note_child);
el.unset_dirty_descendants();
}
}
}
#[inline]
fn needs_postorder_traversal() -> bool {
false
}
fn process_postorder(&self, _style_context: &mut StyleContext<E>, _node: E::ConcreteNode) {
panic!("this should never be called")
}
fn text_node_needs_traversal(node: E::ConcreteNode, parent_data: &ElementData) -> bool {
node.layout_data().is_none() || !parent_data.damage.is_empty()
}
fn shared_context(&self) -> &SharedStyleContext {
&self.context.style_context
}
}