From 219d4c76838932e9eebc0d59fea8a9b7c6e5a380 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Mon, 22 Dec 2014 15:51:48 -0700 Subject: [PATCH] servo: Merge #4459 - gfx: Clip the background properly when `border-radius` is used, and clean up some painting stuff (from pcwalton:border-radius-clipping); r=glennw Together these improve a large number of sites: GitHub, Reddit, Wikipedia, etc. r? @glennw Source-Repo: https://github.com/servo/servo Source-Revision: 63a7742d834e9ed44421baa3ce218a5eabce58bf --- servo/components/gfx/display_list/mod.rs | 183 ++++- .../components/gfx/display_list/optimizer.rs | 2 +- servo/components/gfx/lib.rs | 2 +- servo/components/gfx/paint_context.rs | 413 ++++++++---- servo/components/gfx/paint_task.rs | 2 +- servo/components/layout/block.rs | 12 +- .../components/layout/display_list_builder.rs | 636 ++++++++++-------- servo/components/layout/flow.rs | 25 +- servo/components/layout/fragment.rs | 2 +- servo/components/layout/inline.rs | 10 +- servo/components/layout/layout_task.rs | 9 +- servo/components/util/geometry.rs | 2 +- 12 files changed, 822 insertions(+), 476 deletions(-) diff --git a/servo/components/gfx/display_list/mod.rs b/servo/components/gfx/display_list/mod.rs index 62c19a03c6a8..c6ad40c64d98 100644 --- a/servo/components/gfx/display_list/mod.rs +++ b/servo/components/gfx/display_list/mod.rs @@ -6,8 +6,8 @@ //! perform. Using a list instead of painting elements in immediate mode allows transforms, hit //! testing, and invalidation to be performed using the same primitives as painting. It also allows //! Servo to aggressively cull invisible and out-of-bounds painting elements, to reduce overdraw. -//! Finally, display lists allow tiles to be farmed out onto multiple CPUs and painted in -//! parallel (although this benefit does not apply to GPU-based painting). +//! Finally, display lists allow tiles to be farmed out onto multiple CPUs and painted in parallel +//! (although this benefit does not apply to GPU-based painting). //! //! Display items describe relatively high-level drawing operations (for example, entire borders //! and shadows instead of lines and blur operations), to reduce the amount of allocation required. @@ -33,10 +33,11 @@ use servo_msg::compositor_msg::LayerId; use servo_net::image::base::Image; use servo_util::cursor::Cursor; use servo_util::dlist as servo_dlist; -use servo_util::geometry::{mod, Au, ZERO_POINT}; +use servo_util::geometry::{mod, Au, MAX_RECT, ZERO_POINT, ZERO_RECT}; use servo_util::range::Range; use servo_util::smallvec::{SmallVec, SmallVec8}; use std::fmt; +use std::num::Zero; use std::slice::Items; use style::ComputedValues; use style::computed_values::border_style; @@ -205,7 +206,7 @@ impl StackingContext { page_rect: paint_context.page_rect, screen_rect: paint_context.screen_rect, clip_rect: clip_rect, - transient_clip_rect: None, + transient_clip: None, }; // Optimize the display list to throw out out-of-bounds display items and so forth. @@ -348,7 +349,9 @@ impl StackingContext { mut iterator: I) where I: Iterator<&'a DisplayItem> { for item in iterator { - if !geometry::rect_contains_point(item.base().clip_rect, point) { + // TODO(pcwalton): Use a precise algorithm here. This will allow us to properly hit + // test elements with `border-radius`, for example. + if !item.base().clip.might_intersect_point(&point) { // Clipped out. continue } @@ -477,25 +480,133 @@ pub struct BaseDisplayItem { /// Metadata attached to this display item. pub metadata: DisplayItemMetadata, - /// The rectangle to clip to. - /// - /// TODO(pcwalton): Eventually, to handle `border-radius`, this will (at least) need to grow - /// the ability to describe rounded rectangles. - pub clip_rect: Rect, + /// The region to clip to. + pub clip: ClippingRegion, } impl BaseDisplayItem { #[inline(always)] - pub fn new(bounds: Rect, metadata: DisplayItemMetadata, clip_rect: Rect) + pub fn new(bounds: Rect, metadata: DisplayItemMetadata, clip: ClippingRegion) -> BaseDisplayItem { BaseDisplayItem { bounds: bounds, metadata: metadata, - clip_rect: clip_rect, + clip: clip, } } } +/// A clipping region for a display item. Currently, this can describe rectangles, rounded +/// rectangles (for `border-radius`), or arbitrary intersections of the two. Arbitrary transforms +/// are not supported because those are handled by the higher-level `StackingContext` abstraction. +#[deriving(Clone, PartialEq, Show)] +pub struct ClippingRegion { + /// The main rectangular region. This does not include any corners. + pub main: Rect, + /// Any complex regions. + /// + /// TODO(pcwalton): Atomically reference count these? Not sure if it's worth the trouble. + /// Measure and follow up. + pub complex: Vec, +} + +/// A complex clipping region. These don't as easily admit arbitrary intersection operations, so +/// they're stored in a list over to the side. Currently a complex clipping region is just a +/// rounded rectangle, but the CSS WGs will probably make us throw more stuff in here eventually. +#[deriving(Clone, PartialEq, Show)] +pub struct ComplexClippingRegion { + /// The boundaries of the rectangle. + pub rect: Rect, + /// Border radii of this rectangle. + pub radii: BorderRadii, +} + +impl ClippingRegion { + /// Returns an empty clipping region that, if set, will result in no pixels being visible. + #[inline] + pub fn empty() -> ClippingRegion { + ClippingRegion { + main: ZERO_RECT, + complex: Vec::new(), + } + } + + /// Returns an all-encompassing clipping region that clips no pixels out. + #[inline] + pub fn max() -> ClippingRegion { + ClippingRegion { + main: MAX_RECT, + complex: Vec::new(), + } + } + + /// Returns a clipping region that represents the given rectangle. + #[inline] + pub fn from_rect(rect: &Rect) -> ClippingRegion { + ClippingRegion { + main: *rect, + complex: Vec::new(), + } + } + + /// Returns the intersection of this clipping region and the given rectangle. + /// + /// TODO(pcwalton): This could more eagerly eliminate complex clipping regions, at the cost of + /// complexity. + #[inline] + pub fn intersect_rect(self, rect: &Rect) -> ClippingRegion { + ClippingRegion { + main: self.main.intersection(rect).unwrap_or(ZERO_RECT), + complex: self.complex, + } + } + + /// Returns true if this clipping region might be nonempty. This can return false positives, + /// but never false negatives. + #[inline] + pub fn might_be_nonempty(&self) -> bool { + !self.main.is_empty() + } + + /// Returns true if this clipping region might contain the given point and false otherwise. + /// This is a quick, not a precise, test; it can yield false positives. + #[inline] + pub fn might_intersect_point(&self, point: &Point2D) -> bool { + geometry::rect_contains_point(self.main, *point) && + self.complex.iter().all(|complex| geometry::rect_contains_point(complex.rect, *point)) + } + + /// Returns true if this clipping region might intersect the given rectangle and false + /// otherwise. This is a quick, not a precise, test; it can yield false positives. + #[inline] + pub fn might_intersect_rect(&self, rect: &Rect) -> bool { + self.main.intersects(rect) && + self.complex.iter().all(|complex| complex.rect.intersects(rect)) + } + + + /// Returns a bounding rect that surrounds this entire clipping region. + #[inline] + pub fn bounding_rect(&self) -> Rect { + let mut rect = self.main; + for complex in self.complex.iter() { + rect = rect.union(&complex.rect) + } + rect + } + + /// Intersects this clipping region with the given rounded rectangle. + #[inline] + pub fn intersect_with_rounded_rect(mut self, rect: &Rect, radii: &BorderRadii) + -> ClippingRegion { + self.complex.push(ComplexClippingRegion { + rect: *rect, + radii: *radii, + }); + self + } +} + /// Metadata attached to each display item. This is useful for performing auxiliary tasks with /// the display list involving hit testing: finding the originating DOM node and determining the /// cursor to use when the element is hovered over. @@ -610,12 +721,21 @@ pub struct BorderDisplayItem { /// Information about the border radii. /// /// TODO(pcwalton): Elliptical radii. -#[deriving(Clone, Default, Show)] +#[deriving(Clone, Default, PartialEq, Show)] pub struct BorderRadii { - pub top_left: T, - pub top_right: T, + pub top_left: T, + pub top_right: T, pub bottom_right: T, - pub bottom_left: T, + pub bottom_left: T, +} + +impl BorderRadii where T: PartialEq + Zero { + /// Returns true if all the radii are zero. + pub fn is_square(&self) -> bool { + let zero = Zero::zero(); + self.top_left == zero && self.top_right == zero && self.bottom_right == zero && + self.bottom_left == zero + } } /// Paints a line segment. @@ -673,13 +793,12 @@ impl<'a> Iterator<&'a DisplayItem> for DisplayItemIterator<'a> { impl DisplayItem { /// Paints this display item into the given painting context. fn draw_into_context(&self, paint_context: &mut PaintContext) { - let this_clip_rect = self.base().clip_rect; - if paint_context.transient_clip_rect != Some(this_clip_rect) { - if paint_context.transient_clip_rect.is_some() { - paint_context.draw_pop_clip(); + { + let this_clip = &self.base().clip; + match paint_context.transient_clip { + Some(ref transient_clip) if transient_clip == this_clip => {} + Some(_) | None => paint_context.push_transient_clip((*this_clip).clone()), } - paint_context.draw_push_clip(&this_clip_rect); - paint_context.transient_clip_rect = Some(this_clip_rect) } match *self { @@ -693,6 +812,8 @@ impl DisplayItem { } DisplayItem::ImageClass(ref image_item) => { + // FIXME(pcwalton): This is a really inefficient way to draw a tiled image; use a + // brush instead. debug!("Drawing image at {}.", image_item.base.bounds); let mut y_offset = Au(0); @@ -715,23 +836,21 @@ impl DisplayItem { DisplayItem::BorderClass(ref border) => { paint_context.draw_border(&border.base.bounds, - border.border_widths, - &border.radius, - border.color, - border.style) + &border.border_widths, + &border.radius, + &border.color, + &border.style) } DisplayItem::GradientClass(ref gradient) => { paint_context.draw_linear_gradient(&gradient.base.bounds, - &gradient.start_point, - &gradient.end_point, - gradient.stops.as_slice()); + &gradient.start_point, + &gradient.end_point, + gradient.stops.as_slice()); } DisplayItem::LineClass(ref line) => { - paint_context.draw_line(&line.base.bounds, - line.color, - line.style) + paint_context.draw_line(&line.base.bounds, line.color, line.style) } DisplayItem::BoxShadowClass(ref box_shadow) => { diff --git a/servo/components/gfx/display_list/optimizer.rs b/servo/components/gfx/display_list/optimizer.rs index 6cf59632aef1..c0dac718c51a 100644 --- a/servo/components/gfx/display_list/optimizer.rs +++ b/servo/components/gfx/display_list/optimizer.rs @@ -47,7 +47,7 @@ impl DisplayListOptimizer { where I: Iterator<&'a DisplayItem> { for display_item in display_items { if self.visible_rect.intersects(&display_item.base().bounds) && - self.visible_rect.intersects(&display_item.base().clip_rect) { + display_item.base().clip.might_intersect_rect(&self.visible_rect) { result_list.push_back((*display_item).clone()) } } diff --git a/servo/components/gfx/lib.rs b/servo/components/gfx/lib.rs index c680233f7acd..4d54758288db 100644 --- a/servo/components/gfx/lib.rs +++ b/servo/components/gfx/lib.rs @@ -2,7 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#![feature(globs, macro_rules, phase, unsafe_destructor, default_type_params)] +#![feature(globs, macro_rules, phase, unsafe_destructor, default_type_params, if_let)] #![deny(unused_imports)] #![deny(unused_variables)] diff --git a/servo/components/gfx/paint_context.rs b/servo/components/gfx/paint_context.rs index 7dea4896d6b6..62f41745922e 100644 --- a/servo/components/gfx/paint_context.rs +++ b/servo/components/gfx/paint_context.rs @@ -8,13 +8,13 @@ use azure::azure::AzIntSize; use azure::azure_hl::{A8, B8G8R8A8, Color, ColorPattern, ColorPatternRef, DrawOptions}; use azure::azure_hl::{DrawSurfaceOptions, DrawTarget, ExtendClamp, GaussianBlurFilterType}; use azure::azure_hl::{GaussianBlurInput, GradientStop, Linear, LinearGradientPattern}; -use azure::azure_hl::{LinearGradientPatternRef, Path, SourceOp, StdDeviationGaussianBlurAttribute}; -use azure::azure_hl::{StrokeOptions}; +use azure::azure_hl::{LinearGradientPatternRef, Path, PathBuilder, SourceOp}; +use azure::azure_hl::{StdDeviationGaussianBlurAttribute, StrokeOptions}; use azure::scaled_font::ScaledFont; use azure::{AZ_CAP_BUTT, AzFloat, struct__AzDrawOptions, struct__AzGlyph}; use azure::{struct__AzGlyphBuffer, struct__AzPoint, AzDrawTargetFillGlyphs}; -use display_list::{BOX_SHADOW_INFLATION_FACTOR, TextDisplayItem, BorderRadii}; use display_list::TextOrientation::{SidewaysLeft, SidewaysRight, Upright}; +use display_list::{BOX_SHADOW_INFLATION_FACTOR, BorderRadii, ClippingRegion, TextDisplayItem}; use font_context::FontContext; use geom::matrix2d::Matrix2D; use geom::point::Point2D; @@ -29,6 +29,7 @@ use servo_util::geometry::{Au, MAX_RECT}; use servo_util::opts; use servo_util::range::Range; use std::default::Default; +use std::mem; use std::num::{Float, FloatMath}; use std::ptr; use style::computed_values::border_style; @@ -45,10 +46,10 @@ pub struct PaintContext<'a> { pub screen_rect: Rect, /// The clipping rect for the stacking context as a whole. pub clip_rect: Option>, - /// The current transient clipping rect, if any. A "transient clipping rect" is the clipping - /// rect used by the last display item. We cache the last value so that we avoid pushing and - /// popping clip rects unnecessarily. - pub transient_clip_rect: Option>, + /// The current transient clipping region, if any. A "transient clipping region" is the + /// clipping region used by the last display item. We cache the last value so that we avoid + /// pushing and popping clipping regions unnecessarily. + pub transient_clip: Option, } enum Direction { @@ -77,25 +78,20 @@ impl<'a> PaintContext<'a> { pub fn draw_border(&self, bounds: &Rect, - border: SideOffsets2D, + border: &SideOffsets2D, radius: &BorderRadii, - color: SideOffsets2D, - style: SideOffsets2D) { + color: &SideOffsets2D, + style: &SideOffsets2D) { let border = border.to_float_px(); let radius = radius.to_radii_px(); - self.draw_target.make_current(); - - self.draw_border_segment(Direction::Top, bounds, border, &radius, color, style); - self.draw_border_segment(Direction::Right, bounds, border, &radius, color, style); - self.draw_border_segment(Direction::Bottom, bounds, border, &radius, color, style); - self.draw_border_segment(Direction::Left, bounds, border, &radius, color, style); + self.draw_border_segment(Direction::Top, bounds, &border, &radius, color, style); + self.draw_border_segment(Direction::Right, bounds, &border, &radius, color, style); + self.draw_border_segment(Direction::Bottom, bounds, &border, &radius, color, style); + self.draw_border_segment(Direction::Left, bounds, &border, &radius, color, style); } - pub fn draw_line(&self, - bounds: &Rect, - color: Color, - style: border_style::T) { + pub fn draw_line(&self, bounds: &Rect, color: Color, style: border_style::T) { self.draw_target.make_current(); self.draw_line_segment(bounds, &Default::default(), color, style); @@ -108,7 +104,8 @@ impl<'a> PaintContext<'a> { let left_top = Point2D(rect.origin.x, rect.origin.y); let right_top = Point2D(rect.origin.x + rect.size.width, rect.origin.y); let left_bottom = Point2D(rect.origin.x, rect.origin.y + rect.size.height); - let right_bottom = Point2D(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height); + let right_bottom = Point2D(rect.origin.x + rect.size.width, + rect.origin.y + rect.size.height); path_builder.move_to(left_top); path_builder.line_to(right_top); @@ -166,10 +163,10 @@ impl<'a> PaintContext<'a> { fn draw_border_segment(&self, direction: Direction, bounds: &Rect, - border: SideOffsets2D, + border: &SideOffsets2D, radius: &BorderRadii, - color: SideOffsets2D, - style: SideOffsets2D) { + color: &SideOffsets2D, + style: &SideOffsets2D) { let (style_select, color_select) = match direction { Direction::Top => (style.top, color.top), Direction::Left => (style.left, color.left), @@ -177,59 +174,117 @@ impl<'a> PaintContext<'a> { Direction::Bottom => (style.bottom, color.bottom) }; - match style_select{ - border_style::none => { + match style_select { + border_style::none | border_style::hidden => {} + border_style::dotted => { + // FIXME(sammykim): This doesn't work well with dash_pattern and cap_style. + self.draw_dashed_border_segment(direction, + bounds, + border, + color_select, + DashSize::DottedBorder); } - border_style::hidden => { + border_style::dashed => { + self.draw_dashed_border_segment(direction, + bounds, + border, + color_select, + DashSize::DashedBorder); } - //FIXME(sammykim): This doesn't work with dash_pattern and cap_style well. I referred firefox code. - border_style::dotted => { - self.draw_dashed_border_segment(direction, bounds, border, color_select, DashSize::DottedBorder); + border_style::solid => { + self.draw_solid_border_segment(direction, bounds, border, radius, color_select); } - border_style::dashed => { - self.draw_dashed_border_segment(direction, bounds, border, color_select, DashSize::DashedBorder); - } - border_style::solid => { - self.draw_solid_border_segment(direction,bounds,border,radius,color_select); - } - border_style::double => { + border_style::double => { self.draw_double_border_segment(direction, bounds, border, radius, color_select); } border_style::groove | border_style::ridge => { - self.draw_groove_ridge_border_segment(direction, bounds, border, radius, color_select, style_select); + self.draw_groove_ridge_border_segment(direction, + bounds, + border, + radius, + color_select, + style_select); } border_style::inset | border_style::outset => { - self.draw_inset_outset_border_segment(direction, bounds, border, radius, color_select, style_select); + self.draw_inset_outset_border_segment(direction, + bounds, + border, + radius, + color_select, + style_select); } } } - fn draw_line_segment(&self, bounds: &Rect, radius: &BorderRadii, color: Color, style: border_style::T) { + fn draw_line_segment(&self, + bounds: &Rect, + radius: &BorderRadii, + color: Color, + style: border_style::T) { let border = SideOffsets2D::new_all_same(bounds.size.width).to_float_px(); - match style { - border_style::none | border_style::hidden => {} - border_style::dotted => { - self.draw_dashed_border_segment(Direction::Right, bounds, border, color, DashSize::DottedBorder); + border_style::none | border_style::hidden => {} + border_style::dotted => { + self.draw_dashed_border_segment(Direction::Right, + bounds, + &border, + color, + DashSize::DottedBorder); } - border_style::dashed => { - self.draw_dashed_border_segment(Direction::Right, bounds, border, color, DashSize::DashedBorder); + border_style::dashed => { + self.draw_dashed_border_segment(Direction::Right, + bounds, + &border, + color, + DashSize::DashedBorder); } - border_style::solid => { - self.draw_solid_border_segment(Direction::Right, bounds, border, radius, color); + border_style::solid => { + self.draw_solid_border_segment(Direction::Right, bounds, &border, radius, color) } - border_style::double => { - self.draw_double_border_segment(Direction::Right, bounds, border, radius, color); + border_style::double => { + self.draw_double_border_segment(Direction::Right, bounds, &border, radius, color) } border_style::groove | border_style::ridge => { - self.draw_groove_ridge_border_segment(Direction::Right, bounds, border, radius, color, style); + self.draw_groove_ridge_border_segment(Direction::Right, + bounds, + &border, + radius, + color, + style); } border_style::inset | border_style::outset => { - self.draw_inset_outset_border_segment(Direction::Right, bounds, border, radius, color, style); + self.draw_inset_outset_border_segment(Direction::Right, + bounds, + &border, + radius, + color, + style); } } } + fn draw_border_path(&self, + bounds: &Rect, + direction: Direction, + border: &SideOffsets2D, + radii: &BorderRadii, + color: Color) { + let mut path_builder = self.draw_target.create_path_builder(); + self.create_border_path_segment(&mut path_builder, + bounds, + direction, + border, + radii); + let draw_options = DrawOptions::new(1.0, 0); + self.draw_target.fill(&path_builder.finish(), &ColorPattern::new(color), &draw_options); + } + + fn push_rounded_rect_clip(&self, bounds: &Rect, radii: &BorderRadii) { + let mut path_builder = self.draw_target.create_path_builder(); + self.create_rounded_rect_path(&mut path_builder, bounds, radii); + self.draw_target.push_clip(&path_builder.finish()); + } + // The following comment is wonderful, and stolen from // gecko:gfx/thebes/gfxContext.cpp:RoundedRectangle for reference. // @@ -310,14 +365,13 @@ impl<'a> PaintContext<'a> { // For the various corners and for each axis, the sign of this // constant changes, or it might be 0 -- it's multiplied by the // appropriate multiplier from the list before using. - #[allow(non_snake_case)] - fn draw_border_path(&self, - bounds: &Rect, - direction: Direction, - border: SideOffsets2D, - radius: &BorderRadii, - color: Color) { + fn create_border_path_segment(&self, + path_builder: &mut PathBuilder, + bounds: &Rect, + direction: Direction, + border: &SideOffsets2D, + radius: &BorderRadii) { // T = top, B = bottom, L = left, R = right let box_TL = bounds.origin; @@ -325,9 +379,6 @@ impl<'a> PaintContext<'a> { let box_BL = box_TL + Point2D(0.0, bounds.size.height); let box_BR = box_TL + Point2D(bounds.size.width, bounds.size.height); - let draw_opts = DrawOptions::new(1.0, 0); - let path_builder = self.draw_target.create_path_builder(); - let rad_R: AzFloat = 0.; let rad_BR = rad_R + Float::frac_pi_4(); let rad_B = rad_BR + Float::frac_pi_4(); @@ -354,9 +405,9 @@ impl<'a> PaintContext<'a> { } match direction { - Direction::Top => { - let edge_TL = box_TL + dx(radius.top_left.max(border.left)); - let edge_TR = box_TR + dx(-radius.top_right.max(border.right)); + Direction::Top => { + let edge_TL = box_TL + dx(radius.top_left.max(border.left)); + let edge_TR = box_TR + dx(-radius.top_right.max(border.right)); let edge_BR = edge_TR + dy(border.top); let edge_BL = edge_TL + dy(border.top); @@ -368,7 +419,8 @@ impl<'a> PaintContext<'a> { if radius.top_right != 0. { // the origin is the center of the arcs we're about to draw. - let origin = edge_TR + Point2D((border.right - radius.top_right).max(0.), radius.top_right); + let origin = edge_TR + Point2D((border.right - radius.top_right).max(0.), + radius.top_right); // the elbow is the inside of the border's curve. let distance_to_elbow = (radius.top_right - border.top).max(0.); @@ -380,16 +432,17 @@ impl<'a> PaintContext<'a> { path_builder.line_to(edge_BL); if radius.top_left != 0. { - let origin = edge_TL + Point2D(-(border.left - radius.top_left).max(0.), radius.top_left); + let origin = edge_TL + Point2D(-(border.left - radius.top_left).max(0.), + radius.top_left); let distance_to_elbow = (radius.top_left - border.top).max(0.); path_builder.arc(origin, distance_to_elbow, rad_T, rad_TL, true); path_builder.arc(origin, radius.top_left, rad_TL, rad_T, false); } } - Direction::Left => { - let edge_TL = box_TL + dy(radius.top_left.max(border.top)); - let edge_BL = box_BL + dy(-radius.bottom_left.max(border.bottom)); + Direction::Left => { + let edge_TL = box_TL + dy(radius.top_left.max(border.top)); + let edge_BL = box_BL + dy(-radius.bottom_left.max(border.bottom)); let edge_TR = edge_TL + dx(border.left); let edge_BR = edge_BL + dx(border.left); @@ -400,7 +453,8 @@ impl<'a> PaintContext<'a> { path_builder.line_to(corner_TL); if radius.top_left != 0. { - let origin = edge_TL + Point2D(radius.top_left, -(border.top - radius.top_left).max(0.)); + let origin = edge_TL + Point2D(radius.top_left, + -(border.top - radius.top_left).max(0.)); let distance_to_elbow = (radius.top_left - border.left).max(0.); path_builder.arc(origin, radius.top_left, rad_L, rad_TL, false); @@ -411,16 +465,18 @@ impl<'a> PaintContext<'a> { path_builder.line_to(edge_BR); if radius.bottom_left != 0. { - let origin = edge_BL + Point2D(radius.bottom_left, (border.bottom - radius.bottom_left).max(0.)); + let origin = edge_BL + + Point2D(radius.bottom_left, + (border.bottom - radius.bottom_left).max(0.)); let distance_to_elbow = (radius.bottom_left - border.left).max(0.); path_builder.arc(origin, distance_to_elbow, rad_L, rad_BL, true); path_builder.arc(origin, radius.bottom_left, rad_BL, rad_L, false); } } - Direction::Right => { - let edge_TR = box_TR + dy(radius.top_right.max(border.top)); - let edge_BR = box_BR + dy(-radius.bottom_right.max(border.bottom)); + Direction::Right => { + let edge_TR = box_TR + dy(radius.top_right.max(border.top)); + let edge_BR = box_BR + dy(-radius.bottom_right.max(border.bottom)); let edge_TL = edge_TR + dx(-border.right); let edge_BL = edge_BR + dx(-border.right); @@ -431,7 +487,8 @@ impl<'a> PaintContext<'a> { path_builder.line_to(edge_TL); if radius.top_right != 0. { - let origin = edge_TR + Point2D(-radius.top_right, -(border.top - radius.top_right).max(0.)); + let origin = edge_TR + Point2D(-radius.top_right, + -(border.top - radius.top_right).max(0.)); let distance_to_elbow = (radius.top_right - border.right).max(0.); path_builder.arc(origin, distance_to_elbow, rad_R, rad_TR, true); @@ -442,7 +499,9 @@ impl<'a> PaintContext<'a> { path_builder.line_to(corner_BR); if radius.bottom_right != 0. { - let origin = edge_BR + Point2D(-radius.bottom_right, (border.bottom - radius.bottom_right).max(0.)); + let origin = edge_BR + + Point2D(-radius.bottom_right, + (border.bottom - radius.bottom_right).max(0.)); let distance_to_elbow = (radius.bottom_right - border.right).max(0.); path_builder.arc(origin, radius.bottom_right, rad_R, rad_BR, false); @@ -450,8 +509,8 @@ impl<'a> PaintContext<'a> { } } Direction::Bottom => { - let edge_BL = box_BL + dx(radius.bottom_left.max(border.left)); - let edge_BR = box_BR + dx(-radius.bottom_right.max(border.right)); + let edge_BL = box_BL + dx(radius.bottom_left.max(border.left)); + let edge_BR = box_BR + dx(-radius.bottom_right.max(border.right)); let edge_TL = edge_BL + dy(-border.bottom); let edge_TR = edge_BR + dy(-border.bottom); @@ -462,7 +521,8 @@ impl<'a> PaintContext<'a> { path_builder.line_to(edge_TR); if radius.bottom_right != 0. { - let origin = edge_BR + Point2D((border.right - radius.bottom_right).max(0.), -radius.bottom_right); + let origin = edge_BR + Point2D((border.right - radius.bottom_right).max(0.), + -radius.bottom_right); let distance_to_elbow = (radius.bottom_right - border.bottom).max(0.); path_builder.arc(origin, distance_to_elbow, rad_B, rad_BR, true); @@ -473,7 +533,8 @@ impl<'a> PaintContext<'a> { path_builder.line_to(corner_BL); if radius.bottom_left != 0. { - let origin = edge_BL - Point2D((border.left - radius.bottom_left).max(0.), radius.bottom_left); + let origin = edge_BL - Point2D((border.left - radius.bottom_left).max(0.), + radius.bottom_left); let distance_to_elbow = (radius.bottom_left - border.bottom).max(0.); path_builder.arc(origin, radius.bottom_left, rad_B, rad_BL, false); @@ -481,16 +542,64 @@ impl<'a> PaintContext<'a> { } } } + } - let path = path_builder.finish(); - self.draw_target.fill(&path, &ColorPattern::new(color), &draw_opts); + /// Creates a path representing the given rounded rectangle. + /// + /// TODO(pcwalton): Should we unify with the code above? It doesn't seem immediately obvious + /// how to do that (especially without regressing performance) unless we have some way to + /// efficiently intersect or union paths, since different border styles/colors can force us to + /// slice through the rounded corners. My first attempt to unify with the above code resulted + /// in making a mess of it, and the simplicity of this code path is appealing, so it may not + /// be worth it… In any case, revisit this decision when we support elliptical radii. + fn create_rounded_rect_path(&self, + path_builder: &mut PathBuilder, + bounds: &Rect, + radii: &BorderRadii) { + // +----------+ + // / 1 2 \ + // + 8 3 + + // | | + // + 7 4 + + // \ 6 5 / + // +----------+ + + path_builder.move_to(Point2D(bounds.origin.x + radii.top_left, bounds.origin.y)); // 1 + path_builder.line_to(Point2D(bounds.max_x() - radii.top_right, bounds.origin.y)); // 2 + path_builder.arc(Point2D(bounds.max_x() - radii.top_right, + bounds.origin.y + radii.top_right), + radii.top_right, + 1.5f32 * Float::frac_pi_2(), + Float::two_pi(), + false); // 3 + path_builder.line_to(Point2D(bounds.max_x(), bounds.max_y() - radii.bottom_right)); // 4 + path_builder.arc(Point2D(bounds.max_x() - radii.bottom_right, + bounds.max_y() - radii.bottom_right), + radii.bottom_right, + 0.0, + Float::frac_pi_2(), + false); // 5 + path_builder.line_to(Point2D(bounds.origin.x + radii.bottom_left, bounds.max_y())); // 6 + path_builder.arc(Point2D(bounds.origin.x + radii.bottom_left, + bounds.max_y() - radii.bottom_left), + radii.bottom_left, + Float::frac_pi_2(), + Float::pi(), + false); // 7 + path_builder.line_to(Point2D(bounds.origin.x, bounds.origin.y + radii.top_left)); // 8 + path_builder.arc(Point2D(bounds.origin.x + radii.top_left, + bounds.origin.y + radii.top_left), + radii.top_left, + Float::pi(), + 1.5f32 * Float::frac_pi_2(), + false); // 1 } fn draw_dashed_border_segment(&self, direction: Direction, - bounds: &Rect, - border: SideOffsets2D, - color: Color, + bounds: &Rect, + border: &SideOffsets2D, + color: Color, dash_size: DashSize) { let rect = bounds.to_azure_rect(); let draw_opts = DrawOptions::new(1u as AzFloat, 0 as uint16_t); @@ -546,14 +655,19 @@ impl<'a> PaintContext<'a> { &draw_opts); } - fn draw_solid_border_segment(&self, direction: Direction, bounds: &Rect, border: SideOffsets2D, radius: &BorderRadii, color: Color) { + fn draw_solid_border_segment(&self, + direction: Direction, + bounds: &Rect, + border: &SideOffsets2D, + radius: &BorderRadii, + color: Color) { let rect = bounds.to_azure_rect(); self.draw_border_path(&rect, direction, border, radius, color); } fn get_scaled_bounds(&self, - bounds: &Rect, - border: SideOffsets2D, + bounds: &Rect, + border: &SideOffsets2D, shrink_factor: f32) -> Rect { let rect = bounds.to_azure_rect(); let scaled_border = SideOffsets2D::new(shrink_factor * border.top, @@ -571,25 +685,30 @@ impl<'a> PaintContext<'a> { return Color::new(color.r * scale_factor, color.g * scale_factor, color.b * scale_factor, color.a); } - fn draw_double_border_segment(&self, direction: Direction, bounds: &Rect, border: SideOffsets2D, radius: &BorderRadii, color: Color) { - let scaled_border = SideOffsets2D::new((1.0/3.0) * border.top, - (1.0/3.0) * border.right, - (1.0/3.0) * border.bottom, - (1.0/3.0) * border.left); + fn draw_double_border_segment(&self, + direction: Direction, + bounds: &Rect, + border: &SideOffsets2D, + radius: &BorderRadii, + color: Color) { + let scaled_border = SideOffsets2D::new((1.0/3.0) * border.top, + (1.0/3.0) * border.right, + (1.0/3.0) * border.bottom, + (1.0/3.0) * border.left); let inner_scaled_bounds = self.get_scaled_bounds(bounds, border, 2.0/3.0); // draw the outer portion of the double border. - self.draw_solid_border_segment(direction, bounds, scaled_border, radius, color); + self.draw_solid_border_segment(direction, bounds, &scaled_border, radius, color); // draw the inner portion of the double border. - self.draw_border_path(&inner_scaled_bounds, direction, scaled_border, radius, color); + self.draw_border_path(&inner_scaled_bounds, direction, &scaled_border, radius, color); } fn draw_groove_ridge_border_segment(&self, direction: Direction, - bounds: &Rect, - border: SideOffsets2D, - radius: &BorderRadii, - color: Color, - style: border_style::T) { + bounds: &Rect, + border: &SideOffsets2D, + radius: &BorderRadii, + color: Color, + style: border_style::T) { // original bounds as a Rect, with no scaling. let original_bounds = self.get_scaled_bounds(bounds, border, 0.0); // shrink the bounds by 1/2 of the border, leaving the innermost 1/2 of the border @@ -617,44 +736,65 @@ impl<'a> PaintContext<'a> { } let (outer_color, inner_color) = match (direction, is_groove) { - (Direction::Top, true) | (Direction::Left, true) | - (Direction::Right, false) | (Direction::Bottom, false) => (darker_color, lighter_color), + (Direction::Top, true) | (Direction::Left, true) | + (Direction::Right, false) | (Direction::Bottom, false) => { + (darker_color, lighter_color) + } (Direction::Top, false) | (Direction::Left, false) | - (Direction::Right, true) | (Direction::Bottom, true) => (lighter_color, darker_color) + (Direction::Right, true) | (Direction::Bottom, true) => (lighter_color, darker_color), }; // outer portion of the border - self.draw_border_path(&original_bounds, direction, scaled_border, radius, outer_color); + self.draw_border_path(&original_bounds, direction, &scaled_border, radius, outer_color); // inner portion of the border - self.draw_border_path(&inner_scaled_bounds, direction, scaled_border, radius, inner_color); + self.draw_border_path(&inner_scaled_bounds, + direction, + &scaled_border, + radius, + inner_color); } fn draw_inset_outset_border_segment(&self, direction: Direction, - bounds: &Rect, - border: SideOffsets2D, - radius: &BorderRadii, - color: Color, - style: border_style::T) { + bounds: &Rect, + border: &SideOffsets2D, + radius: &BorderRadii, + color: Color, + style: border_style::T) { let is_inset = match style { - border_style::inset => true, - border_style::outset => false, - _ => panic!("invalid border style") + border_style::inset => true, + border_style::outset => false, + _ => panic!("invalid border style") }; // original bounds as a Rect let original_bounds = self.get_scaled_bounds(bounds, border, 0.0); - let mut scaled_color; - // You can't scale black color (i.e. 'scaled = 0 * scale', equals black). + let mut scaled_color; if color.r != 0.0 || color.g != 0.0 || color.b != 0.0 { scaled_color = match direction { - Direction::Top | Direction::Left => self.scale_color(color, if is_inset { 2.0/3.0 } else { 1.0 }), - Direction::Right | Direction::Bottom => self.scale_color(color, if is_inset { 1.0 } else { 2.0/3.0 }) + Direction::Top | Direction::Left => { + self.scale_color(color, if is_inset { 2.0/3.0 } else { 1.0 }) + } + Direction::Right | Direction::Bottom => { + self.scale_color(color, if is_inset { 1.0 } else { 2.0/3.0 }) + } }; } else { scaled_color = match direction { - Direction::Top | Direction::Left => if is_inset { Color::new(0.3, 0.3, 0.3, color.a) } else { Color::new(0.7, 0.7, 0.7, color.a) }, - Direction::Right | Direction::Bottom => if is_inset { Color::new(0.7, 0.7, 0.7, color.a) } else { Color::new(0.3, 0.3, 0.3, color.a) } + Direction::Top | Direction::Left => { + if is_inset { + Color::new(0.3, 0.3, 0.3, color.a) + } else { + Color::new(0.7, 0.7, 0.7, color.a) + } + } + Direction::Right | Direction::Bottom => { + if is_inset { + Color::new(0.7, 0.7, 0.7, color.a) + } else { + Color::new(0.3, 0.3, 0.3, color.a) + } + } }; } @@ -728,6 +868,12 @@ impl<'a> PaintContext<'a> { pub fn get_or_create_temporary_draw_target(&mut self, opacity: AzFloat) -> DrawTarget { if opacity == 1.0 { + // Reuse the draw target, but remove the transient clip. If we don't do the latter, + // we'll be in a state whereby the paint subcontext thinks it has no transient clip + // (see `StackingContext::optimize_and_draw_into_context`) but it actually does, + // resulting in a situation whereby display items are seemingly randomly clipped out. + self.remove_transient_clip_if_applicable(); + return self.draw_target.clone() } @@ -852,9 +998,8 @@ impl<'a> PaintContext<'a> { } pub fn push_clip_if_applicable(&self) { - match self.clip_rect { - None => {} - Some(ref clip_rect) => self.draw_push_clip(clip_rect), + if let Some(ref clip_rect) = self.clip_rect { + self.draw_push_clip(clip_rect) } } @@ -865,11 +1010,27 @@ impl<'a> PaintContext<'a> { } pub fn remove_transient_clip_if_applicable(&mut self) { - if self.transient_clip_rect.is_some() { - self.draw_pop_clip(); - self.transient_clip_rect = None + if let Some(old_transient_clip) = mem::replace(&mut self.transient_clip, None) { + for _ in old_transient_clip.complex.iter() { + self.draw_pop_clip() + } + self.draw_pop_clip() } } + + /// Sets a new transient clipping region. Automatically calls + /// `remove_transient_clip_if_applicable()` first. + pub fn push_transient_clip(&mut self, clip_region: ClippingRegion) { + self.remove_transient_clip_if_applicable(); + + self.draw_push_clip(&clip_region.main); + for complex_region in clip_region.complex.iter() { + // FIXME(pcwalton): Actually draw a rounded rect. + self.push_rounded_rect_clip(&complex_region.rect.to_azure_rect(), + &complex_region.radii.to_radii_px()) + } + self.transient_clip = Some(clip_region) + } } pub trait ToAzurePoint { diff --git a/servo/components/gfx/paint_task.rs b/servo/components/gfx/paint_task.rs index fee1b4a382e7..d5008bf355d1 100644 --- a/servo/components/gfx/paint_task.rs +++ b/servo/components/gfx/paint_task.rs @@ -510,7 +510,7 @@ impl WorkerThread { page_rect: tile.page_rect, screen_rect: tile.screen_rect, clip_rect: None, - transient_clip_rect: None, + transient_clip: None, }; // Apply the translation to paint the tile we want. diff --git a/servo/components/layout/block.rs b/servo/components/layout/block.rs index 122fd0a823e2..02b58ce463ea 100644 --- a/servo/components/layout/block.rs +++ b/servo/components/layout/block.rs @@ -50,10 +50,10 @@ use table::ColumnComputedInlineSize; use wrapper::ThreadSafeLayoutNode; use geom::Size2D; -use gfx::display_list::DisplayList; +use gfx::display_list::{ClippingRegion, DisplayList}; use serialize::{Encoder, Encodable}; use servo_msg::compositor_msg::LayerId; -use servo_util::geometry::{Au, MAX_AU, MAX_RECT, ZERO_POINT}; +use servo_util::geometry::{Au, MAX_AU, ZERO_POINT}; use servo_util::logical_geometry::{LogicalPoint, LogicalRect, LogicalSize}; use servo_util::opts; use std::cmp::{max, min}; @@ -1699,7 +1699,7 @@ impl Flow for BlockFlow { let container_size = Size2D::zero(); if self.is_root() { - self.base.clip_rect = MAX_RECT + self.base.clip = ClippingRegion::max() } if self.base.flags.contains(IS_ABSOLUTELY_POSITIONED) { @@ -1774,8 +1774,8 @@ impl Flow for BlockFlow { } else { self.base.stacking_relative_position }; - let clip_rect = self.fragment.clip_rect_for_children(&self.base.clip_rect, - &origin_for_children); + let clip = self.fragment.clipping_region_for_children(&self.base.clip, + &origin_for_children); // Process children. let writing_mode = self.base.writing_mode; @@ -1789,7 +1789,7 @@ impl Flow for BlockFlow { } flow::mut_base(kid).absolute_position_info = absolute_position_info_for_children; - flow::mut_base(kid).clip_rect = clip_rect + flow::mut_base(kid).clip = clip.clone() } } diff --git a/servo/components/layout/display_list_builder.rs b/servo/components/layout/display_list_builder.rs index 25e567d4e3a3..662e79d08462 100644 --- a/servo/components/layout/display_list_builder.rs +++ b/servo/components/layout/display_list_builder.rs @@ -23,7 +23,7 @@ use geom::approxeq::ApproxEq; use geom::{Point2D, Rect, Size2D, SideOffsets2D}; use gfx::color; use gfx::display_list::{BOX_SHADOW_INFLATION_FACTOR, BaseDisplayItem, BorderDisplayItem}; -use gfx::display_list::{BorderRadii, BoxShadowDisplayItem}; +use gfx::display_list::{BorderRadii, BoxShadowDisplayItem, ClippingRegion}; use gfx::display_list::{DisplayItem, DisplayList, DisplayItemMetadata}; use gfx::display_list::{GradientDisplayItem}; use gfx::display_list::{GradientStop, ImageDisplayItem, LineDisplayItem}; @@ -35,7 +35,7 @@ use servo_msg::compositor_msg::{FixedPosition, Scrollable}; use servo_msg::constellation_msg::{ConstellationChan, FrameRectMsg}; use servo_net::image::holder::ImageHolder; use servo_util::cursor::{DefaultCursor, TextCursor, VerticalTextCursor}; -use servo_util::geometry::{mod, Au, ZERO_POINT, ZERO_RECT}; +use servo_util::geometry::{mod, Au, ZERO_POINT}; use servo_util::logical_geometry::{LogicalRect, WritingMode}; use servo_util::opts; use std::default::Default; @@ -82,7 +82,7 @@ pub trait FragmentDisplayListBuilding { layout_context: &LayoutContext, level: StackingLevel, absolute_bounds: &Rect, - clip_rect: &Rect); + clip: &ClippingRegion); /// Adds the display items necessary to paint the background image of this fragment to the /// display list at the appropriate stacking level. @@ -92,7 +92,7 @@ pub trait FragmentDisplayListBuilding { layout_context: &LayoutContext, level: StackingLevel, absolute_bounds: &Rect, - clip_rect: &Rect, + clip: &ClippingRegion, image_url: &Url); /// Adds the display items necessary to paint the background linear gradient of this fragment @@ -101,7 +101,7 @@ pub trait FragmentDisplayListBuilding { display_list: &mut DisplayList, level: StackingLevel, absolute_bounds: &Rect, - clip_rect: &Rect, + clip: &ClippingRegion, gradient: &LinearGradient, style: &ComputedValues); @@ -112,7 +112,7 @@ pub trait FragmentDisplayListBuilding { display_list: &mut DisplayList, abs_bounds: &Rect, level: StackingLevel, - clip_rect: &Rect); + clip: &ClippingRegion); /// Adds the display items necessary to paint the outline of this fragment to the display list /// if necessary. @@ -120,7 +120,7 @@ pub trait FragmentDisplayListBuilding { style: &ComputedValues, display_list: &mut DisplayList, bounds: &Rect, - clip_rect: &Rect); + clip: &ClippingRegion); /// Adds the display items necessary to paint the box shadow of this fragment to the display /// list if necessary. @@ -130,19 +130,19 @@ pub trait FragmentDisplayListBuilding { layout_context: &LayoutContext, level: StackingLevel, absolute_bounds: &Rect, - clip_rect: &Rect); + clip: &ClippingRegion); fn build_debug_borders_around_text_fragments(&self, style: &ComputedValues, display_list: &mut DisplayList, flow_origin: Point2D, text_fragment: &ScannedTextFragmentInfo, - clip_rect: &Rect); + clip: &ClippingRegion); fn build_debug_borders_around_fragment(&self, display_list: &mut DisplayList, flow_origin: Point2D, - clip_rect: &Rect); + clip: &ClippingRegion); /// Adds the display items for this fragment to the given display list. /// @@ -152,13 +152,13 @@ pub trait FragmentDisplayListBuilding { /// * `layout_context`: The layout context. /// * `dirty`: The dirty rectangle in the coordinate system of the owning flow. /// * `flow_origin`: Position of the origin of the owning flow wrt the display list root flow. - /// * `clip_rect`: The rectangle to clip the display items to. + /// * `clip`: The region to clip the display items to. fn build_display_list(&mut self, display_list: &mut DisplayList, layout_context: &LayoutContext, flow_origin: Point2D, background_and_border_level: BackgroundAndBorderLevel, - clip_rect: &Rect); + clip: &ClippingRegion); /// Sends the size and position of this iframe fragment to the constellation. This is out of /// line to guide inlining. @@ -167,25 +167,53 @@ pub trait FragmentDisplayListBuilding { offset: Point2D, layout_context: &LayoutContext); - fn clip_rect_for_children(&self, current_clip_rect: &Rect, flow_origin: &Point2D) - -> Rect; + fn clipping_region_for_children(&self, current_clip: &ClippingRegion, flow_origin: &Point2D) + -> ClippingRegion; /// Calculates the clipping rectangle for a fragment, taking the `clip` property into account /// per CSS 2.1 § 11.1.2. - fn calculate_style_specified_clip(&self, parent_clip_rect: &Rect, origin: &Point2D) - -> Rect; + fn calculate_style_specified_clip(&self, parent_clip: &ClippingRegion, origin: &Point2D) + -> ClippingRegion; + + /// Creates the text display item for one text fragment. + fn build_display_list_for_text_fragment(&self, + display_list: &mut DisplayList, + text_fragment: &ScannedTextFragmentInfo, + text_color: RGBA, + offset: &Point2D, + flow_origin: &Point2D, + clip: &ClippingRegion); + + /// Creates the display item for a text decoration: underline, overline, or line-through. + fn build_display_list_for_text_decoration(&self, + display_list: &mut DisplayList, + color: RGBA, + flow_origin: &Point2D, + clip: &ClippingRegion, + logical_bounds: &LogicalRect, + offset: &Point2D); + + /// A helper method that `build_display_list` calls to create per-fragment-type display items. + fn build_fragment_type_specific_display_items(&mut self, + display_list: &mut DisplayList, + flow_origin: Point2D, + clip: &ClippingRegion); } fn build_border_radius(abs_bounds: &Rect, border_style: &Border) -> BorderRadii { // TODO(cgaebel): Support border radii even in the case of multiple border widths. - // This is an extennsion of supporting elliptical radii. For now, all percentage + // This is an extension of supporting elliptical radii. For now, all percentage // radii will be relative to the width. BorderRadii { - top_left: model::specified(border_style.border_top_left_radius.radius, abs_bounds.size.width), - top_right: model::specified(border_style.border_top_right_radius.radius, abs_bounds.size.width), - bottom_right: model::specified(border_style.border_bottom_right_radius.radius, abs_bounds.size.width), - bottom_left: model::specified(border_style.border_bottom_left_radius.radius, abs_bounds.size.width), + top_left: model::specified(border_style.border_top_left_radius.radius, + abs_bounds.size.width), + top_right: model::specified(border_style.border_top_right_radius.radius, + abs_bounds.size.width), + bottom_right: model::specified(border_style.border_bottom_right_radius.radius, + abs_bounds.size.width), + bottom_left: model::specified(border_style.border_bottom_left_radius.radius, + abs_bounds.size.width), } } @@ -196,7 +224,14 @@ impl FragmentDisplayListBuilding for Fragment { layout_context: &LayoutContext, level: StackingLevel, absolute_bounds: &Rect, - clip_rect: &Rect) { + clip: &ClippingRegion) { + // Adjust the clipping region as necessary to account for `border-radius`. + let border_radii = build_border_radius(absolute_bounds, style.get_border()); + let mut clip = (*clip).clone(); + if !border_radii.is_square() { + clip = clip.intersect_with_rounded_rect(absolute_bounds, &border_radii) + } + // FIXME: This causes a lot of background colors to be displayed when they are clearly not // needed. We could use display list optimization to clean this up, but it still seems // inefficient. What we really want is something like "nearest ancestor element that @@ -208,7 +243,7 @@ impl FragmentDisplayListBuilding for Fragment { DisplayItemMetadata::new(self.node, style, DefaultCursor), - *clip_rect), + clip.clone()), color: background_color.to_gfx_color(), }), level); } @@ -223,7 +258,7 @@ impl FragmentDisplayListBuilding for Fragment { self.build_display_list_for_background_linear_gradient(display_list, level, absolute_bounds, - clip_rect, + &clip, gradient, style) } @@ -233,7 +268,7 @@ impl FragmentDisplayListBuilding for Fragment { layout_context, level, absolute_bounds, - clip_rect, + &clip, image_url) } } @@ -245,7 +280,7 @@ impl FragmentDisplayListBuilding for Fragment { layout_context: &LayoutContext, level: StackingLevel, absolute_bounds: &Rect, - clip_rect: &Rect, + clip: &ClippingRegion, image_url: &Url) { let background = style.get_background(); let mut holder = ImageHolder::new(image_url.clone(), @@ -269,7 +304,7 @@ impl FragmentDisplayListBuilding for Fragment { // Clip. // // TODO: Check the bounds to see if a clip item is actually required. - let clip_rect = clip_rect.intersection(&bounds).unwrap_or(ZERO_RECT); + let clip = clip.clone().intersect_rect(&bounds); // Use background-attachment to get the initial virtual origin let (virtual_origin_x, virtual_origin_y) = match background.background_attachment { @@ -322,7 +357,7 @@ impl FragmentDisplayListBuilding for Fragment { display_list.push(DisplayItem::ImageClass(box ImageDisplayItem { base: BaseDisplayItem::new(bounds, DisplayItemMetadata::new(self.node, style, DefaultCursor), - clip_rect), + clip), image: image.clone(), stretch_size: Size2D(Au::from_px(image.width as int), Au::from_px(image.height as int)), @@ -333,10 +368,10 @@ impl FragmentDisplayListBuilding for Fragment { display_list: &mut DisplayList, level: StackingLevel, absolute_bounds: &Rect, - clip_rect: &Rect, + clip: &ClippingRegion, gradient: &LinearGradient, style: &ComputedValues) { - let clip_rect = clip_rect.intersection(absolute_bounds).unwrap_or(ZERO_RECT); + let clip = clip.clone().intersect_rect(absolute_bounds); // This is the distance between the center and the ending point; i.e. half of the distance // between the starting point and the ending point. @@ -432,7 +467,7 @@ impl FragmentDisplayListBuilding for Fragment { let gradient_display_item = DisplayItem::GradientClass(box GradientDisplayItem { base: BaseDisplayItem::new(*absolute_bounds, DisplayItemMetadata::new(self.node, style, DefaultCursor), - clip_rect), + clip), start_point: center - delta, end_point: center + delta, stops: stops, @@ -447,7 +482,7 @@ impl FragmentDisplayListBuilding for Fragment { _layout_context: &LayoutContext, level: StackingLevel, absolute_bounds: &Rect, - clip_rect: &Rect) { + clip: &ClippingRegion) { // NB: According to CSS-BACKGROUNDS, box shadows render in *reverse* order (front to back). for box_shadow in style.get_effects().box_shadow.iter().rev() { let inflation = box_shadow.spread_radius + box_shadow.blur_radius * @@ -460,7 +495,7 @@ impl FragmentDisplayListBuilding for Fragment { DisplayItemMetadata::new(self.node, style, DefaultCursor), - *clip_rect), + (*clip).clone()), box_bounds: *absolute_bounds, color: style.resolve_color(box_shadow.color).to_gfx_color(), offset: Point2D(box_shadow.offset_x, box_shadow.offset_y), @@ -476,7 +511,7 @@ impl FragmentDisplayListBuilding for Fragment { display_list: &mut DisplayList, abs_bounds: &Rect, level: StackingLevel, - clip_rect: &Rect) { + clip: &ClippingRegion) { let border = style.logical_border_width(); if border.is_zero() { return @@ -491,7 +526,7 @@ impl FragmentDisplayListBuilding for Fragment { display_list.push(DisplayItem::BorderClass(box BorderDisplayItem { base: BaseDisplayItem::new(*abs_bounds, DisplayItemMetadata::new(self.node, style, DefaultCursor), - *clip_rect), + (*clip).clone()), border_widths: border.to_physical(style.writing_mode), color: SideOffsets2D::new(top_color.to_gfx_color(), right_color.to_gfx_color(), @@ -509,7 +544,7 @@ impl FragmentDisplayListBuilding for Fragment { style: &ComputedValues, display_list: &mut DisplayList, bounds: &Rect, - clip_rect: &Rect) { + clip: &ClippingRegion) { let width = style.get_outline().outline_width; if width == Au(0) { return @@ -533,7 +568,7 @@ impl FragmentDisplayListBuilding for Fragment { display_list.outlines.push_back(DisplayItem::BorderClass(box BorderDisplayItem { base: BaseDisplayItem::new(bounds, DisplayItemMetadata::new(self.node, style, DefaultCursor), - *clip_rect), + (*clip).clone()), border_widths: SideOffsets2D::new_all_same(width), color: SideOffsets2D::new_all_same(color), style: SideOffsets2D::new_all_same(outline_style), @@ -546,7 +581,7 @@ impl FragmentDisplayListBuilding for Fragment { display_list: &mut DisplayList, flow_origin: Point2D, text_fragment: &ScannedTextFragmentInfo, - clip_rect: &Rect) { + clip: &ClippingRegion) { // FIXME(#2795): Get the real container size let container_size = Size2D::zero(); // Fragment position wrt to the owning flow. @@ -559,7 +594,7 @@ impl FragmentDisplayListBuilding for Fragment { display_list.content.push_back(DisplayItem::BorderClass(box BorderDisplayItem { base: BaseDisplayItem::new(absolute_fragment_bounds, DisplayItemMetadata::new(self.node, style, DefaultCursor), - *clip_rect), + (*clip).clone()), border_widths: SideOffsets2D::new_all_same(Au::from_px(1)), color: SideOffsets2D::new_all_same(color::rgb(0, 0, 200)), style: SideOffsets2D::new_all_same(border_style::solid), @@ -577,7 +612,7 @@ impl FragmentDisplayListBuilding for Fragment { let line_display_item = box LineDisplayItem { base: BaseDisplayItem::new(baseline, DisplayItemMetadata::new(self.node, style, DefaultCursor), - *clip_rect), + (*clip).clone()), color: color::rgb(0, 200, 0), style: border_style::dashed, }; @@ -587,7 +622,7 @@ impl FragmentDisplayListBuilding for Fragment { fn build_debug_borders_around_fragment(&self, display_list: &mut DisplayList, flow_origin: Point2D, - clip_rect: &Rect) { + clip: &ClippingRegion) { // FIXME(#2795): Get the real container size let container_size = Size2D::zero(); // Fragment position wrt to the owning flow. @@ -602,7 +637,7 @@ impl FragmentDisplayListBuilding for Fragment { DisplayItemMetadata::new(self.node, &*self.style, DefaultCursor), - *clip_rect), + (*clip).clone()), border_widths: SideOffsets2D::new_all_same(Au::from_px(1)), color: SideOffsets2D::new_all_same(color::rgb(0, 0, 200)), style: SideOffsets2D::new_all_same(border_style::solid), @@ -610,22 +645,24 @@ impl FragmentDisplayListBuilding for Fragment { })); } - fn calculate_style_specified_clip(&self, parent_clip_rect: &Rect, origin: &Point2D) - -> Rect { + fn calculate_style_specified_clip(&self, parent_clip: &ClippingRegion, origin: &Point2D) + -> ClippingRegion { // Account for `clip` per CSS 2.1 § 11.1.2. let style_clip_rect = match (self.style().get_box().position, self.style().get_effects().clip) { (position::absolute, Some(style_clip_rect)) => style_clip_rect, - _ => return *parent_clip_rect, + _ => return (*parent_clip).clone(), }; // FIXME(pcwalton, #2795): Get the real container size. let border_box = self.border_box.to_physical(self.style.writing_mode, Size2D::zero()); let clip_origin = Point2D(border_box.origin.x + style_clip_rect.left, border_box.origin.y + style_clip_rect.top); - Rect(clip_origin + *origin, - Size2D(style_clip_rect.right.unwrap_or(border_box.size.width) - clip_origin.x, - style_clip_rect.bottom.unwrap_or(border_box.size.height) - clip_origin.y)) + let new_clip_rect = + Rect(clip_origin + *origin, + Size2D(style_clip_rect.right.unwrap_or(border_box.size.width) - clip_origin.x, + style_clip_rect.bottom.unwrap_or(border_box.size.height) - clip_origin.y)); + (*parent_clip).clone().intersect_rect(&new_clip_rect) } fn build_display_list(&mut self, @@ -633,7 +670,7 @@ impl FragmentDisplayListBuilding for Fragment { layout_context: &LayoutContext, flow_origin: Point2D, background_and_border_level: BackgroundAndBorderLevel, - clip_rect: &Rect) { + clip: &ClippingRegion) { // Compute the fragment position relative to the parent stacking context. If the fragment // itself establishes a stacking context, then the origin of its position will be (0, 0) // for the purposes of this computation. @@ -645,13 +682,6 @@ impl FragmentDisplayListBuilding for Fragment { let absolute_fragment_bounds = self.stacking_relative_bounds(&stacking_relative_flow_origin); - // FIXME(#2795): Get the real container size - let container_size = Size2D::zero(); - let rect_to_absolute = |writing_mode: WritingMode, logical_rect: LogicalRect| { - let physical_rect = logical_rect.to_physical(writing_mode, container_size); - Rect(physical_rect.origin + stacking_relative_flow_origin, physical_rect.size) - }; - debug!("Fragment::build_display_list at rel={}, abs={}: {}", self.border_box, absolute_fragment_bounds, @@ -671,9 +701,8 @@ impl FragmentDisplayListBuilding for Fragment { // Calculate the clip rect. If there's nothing to render at all, don't even construct // display list items. - let clip_rect = self.calculate_style_specified_clip(clip_rect, - &absolute_fragment_bounds.origin); - if !absolute_fragment_bounds.intersects(&clip_rect) { + let clip = self.calculate_style_specified_clip(clip, &absolute_fragment_bounds.origin); + if !clip.might_intersect_rect(&absolute_fragment_bounds) { return; } @@ -684,230 +713,77 @@ impl FragmentDisplayListBuilding for Fragment { StackingLevel::from_background_and_border_level(background_and_border_level); // Add a shadow to the list, if applicable. - match self.inline_context { - Some(ref inline_context) => { - for style in inline_context.styles.iter().rev() { - self.build_display_list_for_box_shadow_if_applicable( - &**style, - display_list, - layout_context, - level, - &absolute_fragment_bounds, - &clip_rect); - } + if let Some(ref inline_context) = self.inline_context { + for style in inline_context.styles.iter().rev() { + self.build_display_list_for_box_shadow_if_applicable(&**style, + display_list, + layout_context, + level, + &absolute_fragment_bounds, + &clip); } - None => {} } - match self.specific { - SpecificFragmentInfo::ScannedText(_) => {}, - _ => { - self.build_display_list_for_box_shadow_if_applicable( - &*self.style, - display_list, - layout_context, - level, - &absolute_fragment_bounds, - &clip_rect); - } + if !self.is_scanned_text_fragment() { + self.build_display_list_for_box_shadow_if_applicable(&*self.style, + display_list, + layout_context, + level, + &absolute_fragment_bounds, + &clip); } // Add the background to the list, if applicable. - match self.inline_context { - Some(ref inline_context) => { - for style in inline_context.styles.iter().rev() { - self.build_display_list_for_background_if_applicable( - &**style, - display_list, - layout_context, - level, - &absolute_fragment_bounds, - &clip_rect); - } + if let Some(ref inline_context) = self.inline_context { + for style in inline_context.styles.iter().rev() { + self.build_display_list_for_background_if_applicable(&**style, + display_list, + layout_context, + level, + &absolute_fragment_bounds, + &clip); } - None => {} } - match self.specific { - SpecificFragmentInfo::ScannedText(_) => {}, - _ => { - self.build_display_list_for_background_if_applicable( - &*self.style, - display_list, - layout_context, - level, - &absolute_fragment_bounds, - &clip_rect); - } + if !self.is_scanned_text_fragment() { + self.build_display_list_for_background_if_applicable(&*self.style, + display_list, + layout_context, + level, + &absolute_fragment_bounds, + &clip); } // Add a border and outlines, if applicable. - match self.inline_context { - Some(ref inline_context) => { - for style in inline_context.styles.iter().rev() { - self.build_display_list_for_borders_if_applicable( - &**style, - display_list, - &absolute_fragment_bounds, - level, - &clip_rect); - self.build_display_list_for_outline_if_applicable( - &**style, - display_list, - &absolute_fragment_bounds, - &clip_rect); - } + if let Some(ref inline_context) = self.inline_context { + for style in inline_context.styles.iter().rev() { + self.build_display_list_for_borders_if_applicable(&**style, + display_list, + &absolute_fragment_bounds, + level, + &clip); + self.build_display_list_for_outline_if_applicable(&**style, + display_list, + &absolute_fragment_bounds, + &clip); } - None => {} } - match self.specific { - SpecificFragmentInfo::ScannedText(_) => {}, - _ => { - self.build_display_list_for_borders_if_applicable( - &*self.style, - display_list, - &absolute_fragment_bounds, - level, - &clip_rect); - self.build_display_list_for_outline_if_applicable( - &*self.style, - display_list, - &absolute_fragment_bounds, - &clip_rect); - } + if !self.is_scanned_text_fragment() { + self.build_display_list_for_borders_if_applicable(&*self.style, + display_list, + &absolute_fragment_bounds, + level, + &clip); + self.build_display_list_for_outline_if_applicable(&*self.style, + display_list, + &absolute_fragment_bounds, + &clip); } } - let content_box = self.content_box(); - let absolute_content_box = rect_to_absolute(self.style.writing_mode, content_box); - // Create special per-fragment-type display items. - match self.specific { - SpecificFragmentInfo::UnscannedText(_) => panic!("Shouldn't see unscanned fragments here."), - SpecificFragmentInfo::TableColumn(_) => panic!("Shouldn't see table column fragments here."), - SpecificFragmentInfo::ScannedText(ref text_fragment) => { - // Create the text display item. - let (orientation, cursor) = if self.style.writing_mode.is_vertical() { - if self.style.writing_mode.is_sideways_left() { - (SidewaysLeft, VerticalTextCursor) - } else { - (SidewaysRight, VerticalTextCursor) - } - } else { - (Upright, TextCursor) - }; - - let metrics = &text_fragment.run.font_metrics; - let baseline_origin = { - let mut content_box_start = content_box.start; - content_box_start.b = content_box_start.b + metrics.ascent; - content_box_start.to_physical(self.style.writing_mode, container_size) - + flow_origin - }; - - display_list.content.push_back(DisplayItem::TextClass(box TextDisplayItem { - base: BaseDisplayItem::new(absolute_content_box, - DisplayItemMetadata::new(self.node, - self.style(), - cursor), - clip_rect), - text_run: text_fragment.run.clone(), - range: text_fragment.range, - text_color: self.style().get_color().color.to_gfx_color(), - orientation: orientation, - baseline_origin: baseline_origin, - })); - - // Create display items for text decoration - { - let line = |maybe_color: Option, - style: &ComputedValues, - rect: || -> LogicalRect| { - match maybe_color { - None => {} - Some(color) => { - let bounds = rect_to_absolute(self.style.writing_mode, rect()); - display_list.content.push_back(DisplayItem::SolidColorClass( - box SolidColorDisplayItem { - base: BaseDisplayItem::new( - bounds, - DisplayItemMetadata::new(self.node, - style, - DefaultCursor), - clip_rect), - color: color.to_gfx_color(), - })) - } - } - }; - - let text_decorations = - self.style().get_inheritedtext()._servo_text_decorations_in_effect; - line(text_decorations.underline, self.style(), || { - let mut rect = content_box.clone(); - rect.start.b = rect.start.b + metrics.ascent - metrics.underline_offset; - rect.size.block = metrics.underline_size; - rect - }); - - line(text_decorations.overline, self.style(), || { - let mut rect = content_box.clone(); - rect.size.block = metrics.underline_size; - rect - }); - - line(text_decorations.line_through, self.style(), || { - let mut rect = content_box.clone(); - rect.start.b = rect.start.b + metrics.ascent - metrics.strikeout_offset; - rect.size.block = metrics.strikeout_size; - rect - }); - } - - if opts::get().show_debug_fragment_borders { - self.build_debug_borders_around_text_fragments(self.style(), - display_list, - flow_origin, - &**text_fragment, - &clip_rect); - } - } - SpecificFragmentInfo::Generic | SpecificFragmentInfo::Iframe(..) | SpecificFragmentInfo::Table | SpecificFragmentInfo::TableCell | - SpecificFragmentInfo::TableRow | SpecificFragmentInfo::TableWrapper | SpecificFragmentInfo::InlineBlock(_) | - SpecificFragmentInfo::InlineAbsoluteHypothetical(_) => { - if opts::get().show_debug_fragment_borders { - self.build_debug_borders_around_fragment(display_list, - flow_origin, - &clip_rect); - } - } - SpecificFragmentInfo::Image(ref mut image_fragment) => { - let image_ref = &mut image_fragment.image; - match image_ref.get_image(self.node.to_untrusted_node_address()) { - Some(image) => { - debug!("(building display list) building image fragment"); - - // Place the image into the display list. - display_list.content.push_back(DisplayItem::ImageClass(box ImageDisplayItem { - base: BaseDisplayItem::new(absolute_content_box, - DisplayItemMetadata::new(self.node, - &*self.style, - DefaultCursor), - clip_rect), - image: image.clone(), - stretch_size: absolute_content_box.size, - })); - } - None => { - // No image data at all? Do nothing. - // - // TODO: Add some kind of placeholder image. - debug!("(building display list) no image :("); - } - } - } - } + self.build_fragment_type_specific_display_items(display_list, flow_origin, &clip); if opts::get().show_debug_fragment_borders { - self.build_debug_borders_around_fragment(display_list, flow_origin, &clip_rect) + self.build_debug_borders_around_fragment(display_list, flow_origin, &clip) } // If this is an iframe, then send its position and size up to the constellation. @@ -920,13 +796,95 @@ impl FragmentDisplayListBuilding for Fragment { // origin to the constellation here during display list construction. This should work // because layout for the iframe only needs to know size, and origin is only relevant if // the iframe is actually going to be displayed. + if let SpecificFragmentInfo::Iframe(ref iframe_fragment) = self.specific { + self.finalize_position_and_size_of_iframe(&**iframe_fragment, + absolute_fragment_bounds.origin, + layout_context) + } + } + + fn build_fragment_type_specific_display_items(&mut self, + display_list: &mut DisplayList, + flow_origin: Point2D, + clip: &ClippingRegion) { + // Compute the fragment position relative to the parent stacking context. If the fragment + // itself establishes a stacking context, then the origin of its position will be (0, 0) + // for the purposes of this computation. + let stacking_relative_flow_origin = if self.establishes_stacking_context() { + ZERO_POINT + } else { + flow_origin + }; + + // FIXME(#2795): Get the real container size. + let content_box = self.content_box(); + let container_size = Size2D::zero(); + let rect_to_absolute = |writing_mode: WritingMode, logical_rect: LogicalRect| { + let physical_rect = logical_rect.to_physical(writing_mode, container_size); + Rect(physical_rect.origin + stacking_relative_flow_origin, physical_rect.size) + }; + match self.specific { - SpecificFragmentInfo::Iframe(ref iframe_fragment) => { - self.finalize_position_and_size_of_iframe(&**iframe_fragment, - absolute_fragment_bounds.origin, - layout_context) + SpecificFragmentInfo::UnscannedText(_) => { + panic!("Shouldn't see unscanned fragments here.") + } + SpecificFragmentInfo::TableColumn(_) => { + panic!("Shouldn't see table column fragments here.") + } + SpecificFragmentInfo::ScannedText(ref text_fragment) => { + // Create the main text display item. + let text_color = self.style().get_color().color; + self.build_display_list_for_text_fragment(display_list, + &**text_fragment, + text_color, + &flow_origin, + &Point2D(Au(0), Au(0)), + clip); + + if opts::get().show_debug_fragment_borders { + self.build_debug_borders_around_text_fragments(self.style(), + display_list, + flow_origin, + &**text_fragment, + clip); + } + } + SpecificFragmentInfo::Generic | + SpecificFragmentInfo::Iframe(..) | + SpecificFragmentInfo::Table | + SpecificFragmentInfo::TableCell | + SpecificFragmentInfo::TableRow | + SpecificFragmentInfo::TableWrapper | + SpecificFragmentInfo::InlineBlock(_) | + SpecificFragmentInfo::InlineAbsoluteHypothetical(_) => { + if opts::get().show_debug_fragment_borders { + self.build_debug_borders_around_fragment(display_list, flow_origin, clip); + } + } + SpecificFragmentInfo::Image(ref mut image_fragment) => { + let image_ref = &mut image_fragment.image; + if let Some(image) = image_ref.get_image(self.node.to_untrusted_node_address()) { + debug!("(building display list) building image fragment"); + let absolute_content_box = rect_to_absolute(self.style.writing_mode, + content_box); + + // Place the image into the display list. + display_list.content.push_back(DisplayItem::ImageClass(box ImageDisplayItem { + base: BaseDisplayItem::new(absolute_content_box, + DisplayItemMetadata::new(self.node, + &*self.style, + DefaultCursor), + (*clip).clone()), + image: image.clone(), + stretch_size: absolute_content_box.size, + })); + } else { + // No image data at all? Do nothing. + // + // TODO: Add some kind of placeholder image. + debug!("(building display list) no image :("); + } } - _ => {} } } @@ -951,30 +909,144 @@ impl FragmentDisplayListBuilding for Fragment { iframe_rect)); } - fn clip_rect_for_children(&self, current_clip_rect: &Rect, origin: &Point2D) - -> Rect { + fn clipping_region_for_children(&self, current_clip: &ClippingRegion, flow_origin: &Point2D) + -> ClippingRegion { // Don't clip if we're text. - match self.specific { - SpecificFragmentInfo::ScannedText(_) => return *current_clip_rect, - _ => {} + if self.is_scanned_text_fragment() { + return (*current_clip).clone() } // Account for style-specified `clip`. - let current_clip_rect = self.calculate_style_specified_clip(current_clip_rect, origin); + let current_clip = self.calculate_style_specified_clip(current_clip, flow_origin); // Only clip if `overflow` tells us to. match self.style.get_box().overflow { overflow::hidden | overflow::auto | overflow::scroll => {} - _ => return current_clip_rect, + _ => return current_clip, } // Create a new clip rect. // // FIXME(#2795): Get the real container size. let physical_rect = self.border_box.to_physical(self.style.writing_mode, Size2D::zero()); - current_clip_rect.intersection(&Rect(Point2D(physical_rect.origin.x + origin.x, - physical_rect.origin.y + origin.y), - physical_rect.size)).unwrap_or(ZERO_RECT) + current_clip.intersect_rect(&Rect(physical_rect.origin + *flow_origin, physical_rect.size)) + } + + fn build_display_list_for_text_fragment(&self, + display_list: &mut DisplayList, + text_fragment: &ScannedTextFragmentInfo, + text_color: RGBA, + flow_origin: &Point2D, + offset: &Point2D, + clip: &ClippingRegion) { + // Determine the orientation and cursor to use. + let (orientation, cursor) = if self.style.writing_mode.is_vertical() { + if self.style.writing_mode.is_sideways_left() { + (SidewaysLeft, VerticalTextCursor) + } else { + (SidewaysRight, VerticalTextCursor) + } + } else { + (Upright, TextCursor) + }; + + // Compute location of the baseline. + // + // FIXME(pcwalton): Get the real container size. + let container_size = Size2D::zero(); + let content_box = self.content_box(); + let metrics = &text_fragment.run.font_metrics; + let baseline_origin = { + let mut content_box_start = content_box.start; + content_box_start.b = content_box_start.b + metrics.ascent; + content_box_start.to_physical(self.style.writing_mode, container_size) + *flow_origin + + *offset + }; + let stacking_relative_flow_origin = if self.establishes_stacking_context() { + ZERO_POINT + } else { + *flow_origin + }; + let rect_to_absolute = |writing_mode: WritingMode, logical_rect: LogicalRect| { + let physical_rect = logical_rect.to_physical(writing_mode, container_size); + Rect(physical_rect.origin + stacking_relative_flow_origin, physical_rect.size) + }; + let content_rect = rect_to_absolute(self.style.writing_mode, + content_box).translate(offset); + + // Create the text display item. + display_list.content.push_back(DisplayItem::TextClass(box TextDisplayItem { + base: BaseDisplayItem::new(content_rect, + DisplayItemMetadata::new(self.node, self.style(), cursor), + (*clip).clone()), + text_run: text_fragment.run.clone(), + range: text_fragment.range, + text_color: text_color.to_gfx_color(), + orientation: orientation, + baseline_origin: baseline_origin, + })); + + // Create display items for text decorations. + let text_decorations = self.style().get_inheritedtext()._servo_text_decorations_in_effect; + if let Some(underline_color) = text_decorations.underline { + let mut rect = content_box.clone(); + rect.start.b = rect.start.b + metrics.ascent - metrics.underline_offset; + rect.size.block = metrics.underline_size; + self.build_display_list_for_text_decoration(display_list, + underline_color, + flow_origin, + clip, + &rect, + offset) + } + + if let Some(overline_color) = text_decorations.overline { + let mut rect = content_box.clone(); + rect.size.block = metrics.underline_size; + self.build_display_list_for_text_decoration(display_list, + overline_color, + flow_origin, + clip, + &rect, + offset) + } + + if let Some(line_through_color) = text_decorations.line_through { + let mut rect = content_box.clone(); + rect.start.b = rect.start.b + metrics.ascent - metrics.strikeout_offset; + rect.size.block = metrics.strikeout_size; + self.build_display_list_for_text_decoration(display_list, + line_through_color, + flow_origin, + clip, + &rect, + offset) + } + } + + fn build_display_list_for_text_decoration(&self, + display_list: &mut DisplayList, + color: RGBA, + flow_origin: &Point2D, + clip: &ClippingRegion, + logical_bounds: &LogicalRect, + offset: &Point2D) { + // FIXME(pcwalton): Get the real container size. + let container_size = Size2D::zero(); + let stacking_relative_flow_origin = if self.establishes_stacking_context() { + ZERO_POINT + } else { + *flow_origin + }; + let physical_rect = logical_bounds.to_physical(self.style.writing_mode, container_size); + + let bounds = Rect(physical_rect.origin + stacking_relative_flow_origin, + physical_rect.size).translate(offset); + let metadata = DisplayItemMetadata::new(self.node, &*self.style, DefaultCursor); + display_list.content.push_back(DisplayItem::SolidColorClass(box SolidColorDisplayItem { + base: BaseDisplayItem::new(bounds, metadata, (*clip).clone()), + color: color.to_gfx_color(), + })) } } @@ -1014,7 +1086,7 @@ impl BlockFlowDisplayListBuilding for BlockFlow { layout_context, stacking_relative_fragment_origin, background_border_level, - &self.base.clip_rect); + &self.base.clip); for kid in self.base.children.iter_mut() { flow::mut_base(kid).display_list_building_result.add_to(display_list); @@ -1128,7 +1200,7 @@ impl ListItemFlowDisplayListBuilding for ListItemFlow { layout_context, stacking_relative_fragment_origin, BackgroundAndBorderLevel::Content, - &self.block_flow.base.clip_rect); + &self.block_flow.base.clip); } } diff --git a/servo/components/layout/flow.rs b/servo/components/layout/flow.rs index 873f6268fd7b..a36fca0df0aa 100644 --- a/servo/components/layout/flow.rs +++ b/servo/components/layout/flow.rs @@ -47,11 +47,11 @@ use table_wrapper::TableWrapperFlow; use wrapper::ThreadSafeLayoutNode; use geom::{Point2D, Rect, Size2D}; +use gfx::display_list::ClippingRegion; use serialize::{Encoder, Encodable}; use servo_msg::compositor_msg::LayerId; use servo_util::geometry::Au; -use servo_util::logical_geometry::WritingMode; -use servo_util::logical_geometry::{LogicalRect, LogicalSize}; +use servo_util::logical_geometry::{LogicalRect, LogicalSize, WritingMode}; use std::mem; use std::fmt; use std::iter::Zip; @@ -290,7 +290,7 @@ pub trait Flow: fmt::Show + ToString + Sync { /// NB: Do not change this `&self` to `&mut self` under any circumstances! It has security /// implications because this can be called on parents concurrently from descendants! fn generated_containing_block_rect(&self) -> LogicalRect { - panic!("generated_containing_block_position not yet implemented for this flow") + panic!("generated_containing_block_rect not yet implemented for this flow") } /// Returns a layer ID for the given fragment. @@ -763,11 +763,8 @@ pub struct BaseFlow { /// FIXME(pcwalton): Merge with `absolute_static_i_offset` and `fixed_static_i_offset` above? pub absolute_position_info: AbsolutePositionInfo, - /// The clipping rectangle for this flow and its descendants, in layer coordinates. - /// - /// TODO(pcwalton): When we have `border-radius` this will need to at least support rounded - /// rectangles. - pub clip_rect: Rect, + /// The clipping region for this flow and its descendants, in layer coordinates. + pub clip: ClippingRegion, /// The results of display list building for this flow. pub display_list_building_result: DisplayListBuildingResult, @@ -910,7 +907,7 @@ impl BaseFlow { absolute_cb: ContainingBlockLink::new(), display_list_building_result: DisplayListBuildingResult::None, absolute_position_info: AbsolutePositionInfo::new(writing_mode), - clip_rect: Rect(Point2D::zero(), Size2D::zero()), + clip: ClippingRegion::max(), flags: flags, writing_mode: writing_mode, } @@ -946,16 +943,12 @@ impl BaseFlow { }; for item in all_items.iter() { - let paint_bounds = match item.base().bounds.intersection(&item.base().clip_rect) { - None => continue, - Some(rect) => rect, - }; - - if paint_bounds.is_empty() { + let paint_bounds = item.base().clip.clone().intersect_rect(&item.base().bounds); + if !paint_bounds.might_be_nonempty() { continue; } - if bounds.union(&paint_bounds) != bounds { + if bounds.union(&paint_bounds.bounding_rect()) != bounds { error!("DisplayList item {} outside of Flow overflow ({})", item, paint_bounds); } } diff --git a/servo/components/layout/fragment.rs b/servo/components/layout/fragment.rs index bfc4286ae0e7..b50f484f8d92 100644 --- a/servo/components/layout/fragment.rs +++ b/servo/components/layout/fragment.rs @@ -954,7 +954,7 @@ impl Fragment { } /// Returns true if and only if this is a scanned text fragment. - fn is_scanned_text_fragment(&self) -> bool { + pub fn is_scanned_text_fragment(&self) -> bool { match self.specific { SpecificFragmentInfo::ScannedText(..) => true, _ => false, diff --git a/servo/components/layout/inline.rs b/servo/components/layout/inline.rs index d8c113771f96..fdea1dcdcfa0 100644 --- a/servo/components/layout/inline.rs +++ b/servo/components/layout/inline.rs @@ -1215,15 +1215,15 @@ impl Flow for InlineFlow { _ => continue, }; - let clip_rect = fragment.clip_rect_for_children(&self.base.clip_rect, - &stacking_relative_position); + let clip = fragment.clipping_region_for_children(&self.base.clip, + &stacking_relative_position); match fragment.specific { SpecificFragmentInfo::InlineBlock(ref mut info) => { - flow::mut_base(info.flow_ref.deref_mut()).clip_rect = clip_rect + flow::mut_base(info.flow_ref.deref_mut()).clip = clip } SpecificFragmentInfo::InlineAbsoluteHypothetical(ref mut info) => { - flow::mut_base(info.flow_ref.deref_mut()).clip_rect = clip_rect + flow::mut_base(info.flow_ref.deref_mut()).clip = clip } _ => {} } @@ -1246,7 +1246,7 @@ impl Flow for InlineFlow { layout_context, fragment_origin, BackgroundAndBorderLevel::Content, - &self.base.clip_rect); + &self.base.clip); match fragment.specific { SpecificFragmentInfo::InlineBlock(ref mut block_flow) => { let block_flow = block_flow.flow_ref.deref_mut(); diff --git a/servo/components/layout/layout_task.rs b/servo/components/layout/layout_task.rs index 94073cd4e09a..8c0890dac80a 100644 --- a/servo/components/layout/layout_task.rs +++ b/servo/components/layout/layout_task.rs @@ -25,11 +25,11 @@ use geom::rect::Rect; use geom::size::Size2D; use geom::scale_factor::ScaleFactor; use gfx::color; -use gfx::display_list::{DisplayItemMetadata, DisplayList, OpaqueNode, StackingContext}; +use gfx::display_list::{ClippingRegion, DisplayItemMetadata, DisplayList, OpaqueNode}; +use gfx::display_list::{StackingContext}; use gfx::font_cache_task::FontCacheTask; use gfx::paint_task::{mod, PaintInitMsg, PaintChan, PaintLayer}; -use layout_traits; -use layout_traits::{LayoutControlMsg, LayoutTaskFactory}; +use layout_traits::{mod, LayoutControlMsg, LayoutTaskFactory}; use log; use script::dom::bindings::js::JS; use script::dom::node::{LayoutDataRef, Node, NodeTypeId}; @@ -634,7 +634,8 @@ impl LayoutTask { LogicalPoint::zero(writing_mode).to_physical(writing_mode, rw_data.screen_size); - flow::mut_base(&mut **layout_root).clip_rect = data.page_clip_rect; + flow::mut_base(&mut **layout_root).clip = + ClippingRegion::from_rect(&data.page_clip_rect); let rw_data = rw_data.deref_mut(); match rw_data.parallel_traversal { diff --git a/servo/components/util/geometry.rs b/servo/components/util/geometry.rs index 1e28a3a56f4f..1191edba02a3 100644 --- a/servo/components/util/geometry.rs +++ b/servo/components/util/geometry.rs @@ -65,7 +65,7 @@ pub enum PagePx {} // See https://bugzilla.mozilla.org/show_bug.cgi?id=177805 for more info. // // FIXME: Implement Au using Length and ScaleFactor instead of a custom type. -#[deriving(Clone, Hash, PartialEq, PartialOrd, Eq, Ord)] +#[deriving(Clone, Hash, PartialEq, PartialOrd, Eq, Ord, Zero)] pub struct Au(pub i32); impl Default for Au {