Skip to content

Commit

Permalink
use app unit in box_fragement
Browse files Browse the repository at this point in the history
  • Loading branch information
atbrakhi committed Jun 4, 2024
1 parent 6451b1a commit 16a746c
Show file tree
Hide file tree
Showing 20 changed files with 287 additions and 270 deletions.
39 changes: 20 additions & 19 deletions components/layout_2020/display_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use std::cell::{OnceCell, RefCell};
use std::sync::Arc;

use app_units::Au;
use base::id::BrowsingContextId;
use base::WebRenderEpochToU16;
use embedder_traits::Cursor;
Expand Down Expand Up @@ -230,7 +231,7 @@ impl Fragment {
pub(crate) fn build_display_list(
&self,
builder: &mut DisplayListBuilder,
containing_block: &PhysicalRect<Length>,
containing_block: &PhysicalRect<Au>,
section: StackingContextSection,
) {
match self {
Expand Down Expand Up @@ -288,7 +289,7 @@ impl Fragment {

builder.iframe_sizes.insert(
iframe.browsing_context_id,
Size2D::new(rect.size.width.px(), rect.size.height.px()),
Size2D::new(rect.size.width.to_f32_px(), rect.size.height.to_f32_px()),
);

let common = builder.common_properties(rect.to_webrender(), &iframe.style);
Expand Down Expand Up @@ -321,7 +322,7 @@ impl Fragment {
builder: &mut DisplayListBuilder,
style: &ComputedValues,
tag: Option<Tag>,
rect: PhysicalRect<Length>,
rect: PhysicalRect<Au>,
cursor: Cursor,
) {
let hit_info = builder.hit_info(style, tag, cursor);
Expand All @@ -345,7 +346,7 @@ impl Fragment {
&self,
fragment: &TextFragment,
builder: &mut DisplayListBuilder,
containing_block: &PhysicalRect<Length>,
containing_block: &PhysicalRect<Au>,
) {
// NB: The order of painting text components (CSS Text Decoration Module Level 3) is:
// shadows, underline, overline, text, text-emphasis, and then line-through.
Expand All @@ -357,7 +358,7 @@ impl Fragment {
.to_physical(fragment.parent_style.writing_mode, containing_block)
.translate(containing_block.origin.to_vector());
let mut baseline_origin = rect.origin;
baseline_origin.y += Length::from(fragment.font_metrics.ascent);
baseline_origin.y += fragment.font_metrics.ascent;
let glyphs = glyphs(
&fragment.glyphs,
baseline_origin,
Expand Down Expand Up @@ -404,8 +405,8 @@ impl Fragment {
.contains(TextDecorationLine::UNDERLINE)
{
let mut rect = rect;
rect.origin.y += Length::from(font_metrics.ascent - font_metrics.underline_offset);
rect.size.height = Length::new(font_metrics.underline_size.to_nearest_pixel(dppx));
rect.origin.y += font_metrics.ascent - font_metrics.underline_offset;
rect.size.height = Au::from_f32_px(font_metrics.underline_size.to_nearest_pixel(dppx));
self.build_display_list_for_text_decoration(fragment, builder, &rect, &color);
}

Expand All @@ -415,7 +416,7 @@ impl Fragment {
.contains(TextDecorationLine::OVERLINE)
{
let mut rect = rect;
rect.size.height = Length::new(font_metrics.underline_size.to_nearest_pixel(dppx));
rect.size.height = Au::from_f32_px(font_metrics.underline_size.to_nearest_pixel(dppx));
self.build_display_list_for_text_decoration(fragment, builder, &rect, &color);
}

Expand All @@ -435,8 +436,8 @@ impl Fragment {
.contains(TextDecorationLine::LINE_THROUGH)
{
let mut rect = rect;
rect.origin.y += Length::from(font_metrics.ascent - font_metrics.strikeout_offset);
rect.size.height = Length::new(font_metrics.strikeout_size.to_nearest_pixel(dppx));
rect.origin.y += font_metrics.ascent - font_metrics.strikeout_offset;
rect.size.height = Au::from_f32_px(font_metrics.strikeout_size.to_nearest_pixel(dppx));
self.build_display_list_for_text_decoration(fragment, builder, &rect, &color);
}

Expand All @@ -449,7 +450,7 @@ impl Fragment {
&self,
fragment: &TextFragment,
builder: &mut DisplayListBuilder,
rect: &PhysicalRect<Length>,
rect: &PhysicalRect<Au>,
color: &AbsoluteColor,
) {
let rect = rect.to_webrender();
Expand All @@ -476,7 +477,7 @@ impl Fragment {

struct BuilderForBoxFragment<'a> {
fragment: &'a BoxFragment,
containing_block: &'a PhysicalRect<Length>,
containing_block: &'a PhysicalRect<Au>,
border_rect: units::LayoutRect,
padding_rect: OnceCell<units::LayoutRect>,
content_rect: OnceCell<units::LayoutRect>,
Expand All @@ -487,7 +488,7 @@ struct BuilderForBoxFragment<'a> {
}

impl<'a> BuilderForBoxFragment<'a> {
fn new(fragment: &'a BoxFragment, containing_block: &'a PhysicalRect<Length>) -> Self {
fn new(fragment: &'a BoxFragment, containing_block: &'a PhysicalRect<Au>) -> Self {
let border_rect: units::LayoutRect = fragment
.border_rect()
.to_physical(fragment.style.writing_mode, containing_block)
Expand Down Expand Up @@ -711,7 +712,7 @@ impl<'a> BuilderForBoxFragment<'a> {
// is used).
if let BackgroundMode::Extra(ref extra_backgrounds) = self.fragment.background_mode {
for extra_background in extra_backgrounds {
let positioning_area: LogicalRect<Length> = extra_background.rect.clone().into();
let positioning_area: LogicalRect<Au> = extra_background.rect.clone();
let painter = BackgroundPainter {
style: &extra_background.style,
painting_area_override: None,
Expand Down Expand Up @@ -959,7 +960,7 @@ fn rgba(color: AbsoluteColor) -> wr::ColorF {

fn glyphs(
glyph_runs: &[Arc<GlyphStore>],
mut baseline_origin: PhysicalPoint<Length>,
mut baseline_origin: PhysicalPoint<Au>,
justification_adjustment: Length,
) -> Vec<wr::GlyphInstance> {
use gfx_traits::ByteIndex;
Expand All @@ -971,8 +972,8 @@ fn glyphs(
if !run.is_whitespace() {
let glyph_offset = glyph.offset().unwrap_or(Point2D::zero());
let point = units::LayoutPoint::new(
baseline_origin.x.px() + glyph_offset.x.to_f32_px(),
baseline_origin.y.px() + glyph_offset.y.to_f32_px(),
baseline_origin.x.to_f32_px() + glyph_offset.x.to_f32_px(),
baseline_origin.y.to_f32_px() + glyph_offset.y.to_f32_px(),
);
let glyph = wr::GlyphInstance {
index: glyph.id(),
Expand All @@ -982,9 +983,9 @@ fn glyphs(
}

if glyph.char_is_word_separator() {
baseline_origin.x += justification_adjustment;
baseline_origin.x += justification_adjustment.into();
}
baseline_origin.x += Length::from(glyph.advance());
baseline_origin.x += glyph.advance();
}
}
glyphs
Expand Down
101 changes: 56 additions & 45 deletions components/layout_2020/display_list/stacking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use std::cell::RefCell;
use std::mem;

use app_units::Au;
use base::print_tree::PrintTree;
use euclid::default::Rect;
use euclid::default::{Point2D, Rect, Size2D};
use euclid::SideOffsets2D;
use log::warn;
use servo_arc::Arc as ServoArc;
Expand Down Expand Up @@ -34,7 +35,7 @@ use crate::display_list::DisplayListBuilder;
use crate::fragment_tree::{
BoxFragment, ContainingBlockManager, Fragment, FragmentFlags, FragmentTree, PositioningFragment,
};
use crate::geom::{PhysicalRect, PhysicalSides};
use crate::geom::{AuOrAuto, PhysicalRect, PhysicalSides};
use crate::style_ext::ComputedValuesExt;

#[derive(Clone)]
Expand All @@ -53,12 +54,12 @@ pub(crate) struct ContainingBlock {
clip_chain_id: wr::ClipChainId,

/// The physical rect of this containing block.
rect: PhysicalRect<Length>,
rect: PhysicalRect<Au>,
}

impl ContainingBlock {
pub(crate) fn new(
rect: PhysicalRect<Length>,
rect: PhysicalRect<Au>,
scroll_node_id: ScrollTreeNodeId,
scroll_frame_size: Option<LayoutSize>,
clip_chain_id: wr::ClipChainId,
Expand All @@ -71,7 +72,7 @@ impl ContainingBlock {
}
}

pub(crate) fn new_replacing_rect(&self, rect: &PhysicalRect<Length>) -> Self {
pub(crate) fn new_replacing_rect(&self, rect: &PhysicalRect<Au>) -> Self {
ContainingBlock {
rect: *rect,
..*self
Expand Down Expand Up @@ -246,7 +247,7 @@ pub(crate) enum StackingContextContent {
reference_frame_scroll_node_id: ScrollTreeNodeId,
clip_chain_id: wr::ClipChainId,
section: StackingContextSection,
containing_block: PhysicalRect<Length>,
containing_block: PhysicalRect<Au>,
fragment: ArcRefCell<Fragment>,
},

Expand Down Expand Up @@ -528,7 +529,7 @@ impl StackingContext {
&self,
builder: &mut DisplayListBuilder,
fragment_tree: &crate::FragmentTree,
containing_block_rect: &PhysicalRect<Length>,
containing_block_rect: &PhysicalRect<Au>,
) {
let style = if let Some(style) = &fragment_tree.canvas_background.style {
style
Expand Down Expand Up @@ -896,7 +897,7 @@ impl Fragment {
}

struct ReferenceFrameData {
origin: crate::geom::PhysicalPoint<Length>,
origin: crate::geom::PhysicalPoint<Au>,
transform: LayoutTransform,
kind: wr::ReferenceFrameKind,
}
Expand Down Expand Up @@ -1220,7 +1221,7 @@ impl BoxFragment {
display_list: &mut DisplayList,
parent_scroll_node_id: &ScrollTreeNodeId,
parent_clip_chain_id: &wr::ClipChainId,
containing_block_rect: &PhysicalRect<Length>,
containing_block_rect: &PhysicalRect<Au>,
) -> Option<wr::ClipChainId> {
let position = self.style.get_box().position;
// https://drafts.csswg.org/css2/#clipping
Expand Down Expand Up @@ -1254,7 +1255,7 @@ impl BoxFragment {
display_list: &mut DisplayList,
parent_scroll_node_id: &ScrollTreeNodeId,
parent_clip_id: &wr::ClipChainId,
containing_block_rect: &PhysicalRect<Length>,
containing_block_rect: &PhysicalRect<Au>,
) -> Option<(ScrollTreeNodeId, wr::ClipChainId, LayoutSize)> {
let overflow_x = self.style.get_box().overflow_x;
let overflow_y = self.style.get_box().overflow_y;
Expand Down Expand Up @@ -1316,7 +1317,7 @@ impl BoxFragment {
&mut self,
display_list: &mut DisplayList,
parent_scroll_node_id: &ScrollTreeNodeId,
containing_block_rect: &PhysicalRect<Length>,
containing_block_rect: &PhysicalRect<Au>,
scroll_frame_size: &Option<LayoutSize>,
) -> Option<ScrollTreeNodeId> {
if self.style.get_box().position != ComputedPosition::Sticky {
Expand All @@ -1335,19 +1336,23 @@ impl BoxFragment {
// nearest scroll frame instead of the containing block like for other types
// of positioning.
let position = self.style.get_position();
let offsets = PhysicalSides::new(
position
.top
.map(|v| v.resolve(Length::new(scroll_frame_size_for_resolve.height))),
position
.right
.map(|v| v.resolve(Length::new(scroll_frame_size_for_resolve.width))),
position
.bottom
.map(|v| v.resolve(Length::new(scroll_frame_size_for_resolve.height))),
position
.left
.map(|v| v.resolve(Length::new(scroll_frame_size_for_resolve.width))),
let offsets = PhysicalSides::<AuOrAuto>::new(
position.top.map(|v| {
v.resolve(Length::new(scroll_frame_size_for_resolve.height))
.into()
}),
position.right.map(|v| {
v.resolve(Length::new(scroll_frame_size_for_resolve.width))
.into()
}),
position.bottom.map(|v| {
v.resolve(Length::new(scroll_frame_size_for_resolve.height))
.into()
}),
position.left.map(|v| {
v.resolve(Length::new(scroll_frame_size_for_resolve.width))
.into()
}),
);
self.resolved_sticky_insets = Some(offsets);

Expand Down Expand Up @@ -1385,10 +1390,10 @@ impl BoxFragment {
);

let margins = SideOffsets2D::new(
offsets.top.non_auto().map(|v| v.px()),
offsets.right.non_auto().map(|v| v.px()),
offsets.bottom.non_auto().map(|v| v.px()),
offsets.left.non_auto().map(|v| v.px()),
offsets.top.non_auto().map(|v| v.to_f32_px()),
offsets.right.non_auto().map(|v| v.to_f32_px()),
offsets.bottom.non_auto().map(|v| v.to_f32_px()),
offsets.left.non_auto().map(|v| v.to_f32_px()),
);

let sticky_node_id = display_list.define_sticky_frame(
Expand All @@ -1405,7 +1410,7 @@ impl BoxFragment {
/// Optionally returns the data for building a reference frame, without yet building it.
fn reference_frame_data_if_necessary(
&self,
containing_block_rect: &PhysicalRect<Length>,
containing_block_rect: &PhysicalRect<Au>,
) -> Option<ReferenceFrameData> {
if !self.style.has_transform_or_perspective(self.base.flags) {
return None;
Expand Down Expand Up @@ -1451,22 +1456,24 @@ impl BoxFragment {
}

/// Returns true if the given style contains a transform that is not invertible.
fn has_non_invertible_transform_or_zero_scale(&self, containing_block: &Rect<Length>) -> bool {
fn has_non_invertible_transform_or_zero_scale(&self, containing_block: &Rect<Au>) -> bool {
let list = &self.style.get_box().transform;
match list.to_transform_3d_matrix(Some(containing_block)) {
match list.to_transform_3d_matrix(Some(&au_rect_to_length_rect(containing_block))) {
Ok(t) => !t.0.is_invertible() || t.0.m11 == 0. || t.0.m22 == 0.,
Err(_) => false,
}
}

/// Returns the 4D matrix representing this fragment's transform.
pub fn calculate_transform_matrix(
&self,
border_rect: &Rect<Length>,
) -> Option<LayoutTransform> {
pub fn calculate_transform_matrix(&self, border_rect: &Rect<Au>) -> Option<LayoutTransform> {
let list = &self.style.get_box().transform;
let transform =
LayoutTransform::from_untyped(&list.to_transform_3d_matrix(Some(border_rect)).ok()?.0);

let transform = LayoutTransform::from_untyped(
&list
.to_transform_3d_matrix(Some(&au_rect_to_length_rect(border_rect)))
.ok()?
.0,
);
// WebRender will end up dividing by the scale value of this transform, so we
// want to ensure we don't feed it a divisor of 0.
assert_ne!(transform.m11, 0.);
Expand All @@ -1475,11 +1482,11 @@ impl BoxFragment {
let transform_origin = &self.style.get_box().transform_origin;
let transform_origin_x = transform_origin
.horizontal
.percentage_relative_to(border_rect.size.width)
.percentage_relative_to(border_rect.size.width.into())
.px();
let transform_origin_y = transform_origin
.vertical
.percentage_relative_to(border_rect.size.height)
.percentage_relative_to(border_rect.size.height.into())
.px();
let transform_origin_z = transform_origin.depth.px();

Expand All @@ -1498,21 +1505,18 @@ impl BoxFragment {
}

/// Returns the 4D matrix representing this fragment's perspective.
pub fn calculate_perspective_matrix(
&self,
border_rect: &Rect<Length>,
) -> Option<LayoutTransform> {
pub fn calculate_perspective_matrix(&self, border_rect: &Rect<Au>) -> Option<LayoutTransform> {
match self.style.get_box().perspective {
Perspective::Length(length) => {
let perspective_origin = &self.style.get_box().perspective_origin;
let perspective_origin = LayoutPoint::new(
perspective_origin
.horizontal
.percentage_relative_to(border_rect.size.width)
.percentage_relative_to(border_rect.size.width.into())
.px(),
perspective_origin
.vertical
.percentage_relative_to(border_rect.size.height)
.percentage_relative_to(border_rect.size.height.into())
.px(),
);

Expand Down Expand Up @@ -1563,3 +1567,10 @@ impl PositioningFragment {
}
}
}

pub fn au_rect_to_length_rect(rect: &Rect<Au>) -> Rect<Length> {
Rect::new(
Point2D::new(rect.origin.x.into(), rect.origin.y.into()),
Size2D::new(rect.size.width.into(), rect.size.height.into()),
)
}
Loading

0 comments on commit 16a746c

Please sign in to comment.