mirror of
https://github.com/servo/servo.git
synced 2025-08-10 16:05:43 +01:00
gfx: Implement box-shadow
per CSS-BACKGROUNDS.
This commit is contained in:
parent
1bc2c8a639
commit
3ba0abd8ff
21 changed files with 766 additions and 31 deletions
|
@ -5,13 +5,16 @@
|
|||
//! Painting of display lists using Moz2D/Azure.
|
||||
|
||||
use azure::azure::AzIntSize;
|
||||
use azure::azure_hl::{B8G8R8A8, A8, Color, ColorPattern, ColorPatternRef, DrawOptions};
|
||||
use azure::azure_hl::{DrawSurfaceOptions, DrawTarget, ExtendClamp, GradientStop, Linear};
|
||||
use azure::azure_hl::{LinearGradientPattern, LinearGradientPatternRef, SourceOp, StrokeOptions};
|
||||
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::scaled_font::ScaledFont;
|
||||
use azure::{AZ_CAP_BUTT, AzFloat, struct__AzDrawOptions, struct__AzGlyph};
|
||||
use azure::{struct__AzGlyphBuffer, struct__AzPoint, AzDrawTargetFillGlyphs};
|
||||
use display_list::{SidewaysLeft, SidewaysRight, TextDisplayItem, Upright, BorderRadii};
|
||||
use display_list::{BOX_SHADOW_INFLATION_FACTOR, BorderRadii, SidewaysLeft, SidewaysRight};
|
||||
use display_list::{TextDisplayItem, Upright};
|
||||
use font_context::FontContext;
|
||||
use geom::matrix2d::Matrix2D;
|
||||
use geom::point::Point2D;
|
||||
|
@ -22,7 +25,7 @@ use libc::size_t;
|
|||
use libc::types::common::c99::{uint16_t, uint32_t};
|
||||
use png::{RGB8, RGBA8, K8, KA8};
|
||||
use servo_net::image::base::Image;
|
||||
use servo_util::geometry::Au;
|
||||
use servo_util::geometry::{Au, MAX_RECT};
|
||||
use servo_util::opts;
|
||||
use servo_util::range::Range;
|
||||
use std::default::Default;
|
||||
|
@ -40,6 +43,8 @@ pub struct PaintContext<'a> {
|
|||
pub page_rect: Rect<f32>,
|
||||
/// The rectangle that this context encompasses in screen coordinates (pixels).
|
||||
pub screen_rect: Rect<uint>,
|
||||
/// The clipping rect for the stacking context as a whole.
|
||||
pub clip_rect: Option<Rect<Au>>,
|
||||
/// 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.
|
||||
|
@ -58,7 +63,7 @@ enum DashSize {
|
|||
DashedBorder = 3
|
||||
}
|
||||
|
||||
impl<'a> PaintContext<'a> {
|
||||
impl<'a> PaintContext<'a> {
|
||||
pub fn get_draw_target(&self) -> &DrawTarget {
|
||||
&self.draw_target
|
||||
}
|
||||
|
@ -751,6 +756,108 @@ impl<'a> PaintContext<'a> {
|
|||
draw_options);
|
||||
self.draw_target.set_transform(&old_transform);
|
||||
}
|
||||
|
||||
/// Draws a box shadow with the given boundaries, color, offset, blur radius, and spread
|
||||
/// radius. `box_bounds` represents the boundaries of the box.
|
||||
pub fn draw_box_shadow(&mut self,
|
||||
box_bounds: &Rect<Au>,
|
||||
offset: &Point2D<Au>,
|
||||
color: Color,
|
||||
blur_radius: Au,
|
||||
spread_radius: Au,
|
||||
inset: bool) {
|
||||
// Remove both the transient clip and the stacking context clip, because we may need to
|
||||
// draw outside the stacking context's clip.
|
||||
self.remove_transient_clip_if_applicable();
|
||||
self.pop_clip_if_applicable();
|
||||
|
||||
// If we have blur, create a new draw target that's the same size as this tile, but with
|
||||
// enough space around the edges to hold the entire blur. (If we don't do the latter, then
|
||||
// there will be seams between tiles.)
|
||||
//
|
||||
// FIXME(pcwalton): This draw target might be larger than necessary and waste memory.
|
||||
let side_inflation = (blur_radius * BOX_SHADOW_INFLATION_FACTOR).to_subpx().ceil() as i32;
|
||||
let draw_target_transform = self.draw_target.get_transform();
|
||||
let temporary_draw_target;
|
||||
if blur_radius > Au(0) {
|
||||
let draw_target_size = self.draw_target.get_size();
|
||||
let draw_target_size = Size2D(draw_target_size.width, draw_target_size.height);
|
||||
let inflated_draw_target_size = Size2D(draw_target_size.width + side_inflation * 2,
|
||||
draw_target_size.height + side_inflation * 2);
|
||||
temporary_draw_target =
|
||||
self.draw_target.create_similar_draw_target(&inflated_draw_target_size,
|
||||
self.draw_target.get_format());
|
||||
temporary_draw_target.set_transform(
|
||||
&Matrix2D::identity().translate(side_inflation as AzFloat,
|
||||
side_inflation as AzFloat)
|
||||
.mul(&draw_target_transform));
|
||||
} else {
|
||||
temporary_draw_target = self.draw_target.clone();
|
||||
}
|
||||
|
||||
let shadow_bounds = box_bounds.translate(offset).inflate(spread_radius, spread_radius);
|
||||
let path;
|
||||
if inset {
|
||||
path = temporary_draw_target.create_rectangular_border_path(&MAX_RECT, &shadow_bounds);
|
||||
self.draw_target.push_clip(&self.draw_target.create_rectangular_path(box_bounds))
|
||||
} else {
|
||||
path = temporary_draw_target.create_rectangular_path(&shadow_bounds);
|
||||
self.draw_target.push_clip(&self.draw_target
|
||||
.create_rectangular_border_path(&MAX_RECT, box_bounds))
|
||||
}
|
||||
|
||||
temporary_draw_target.fill(&path, &ColorPattern::new(color), &DrawOptions::new(1.0, 0));
|
||||
|
||||
// Blur, if we need to.
|
||||
if blur_radius > Au(0) {
|
||||
// Go ahead and create the blur now. Despite the name, Azure's notion of `StdDeviation`
|
||||
// describes the blur radius, not the sigma for the Gaussian blur.
|
||||
let blur_filter = self.draw_target.create_filter(GaussianBlurFilterType);
|
||||
blur_filter.set_attribute(StdDeviationGaussianBlurAttribute(blur_radius.to_subpx() as
|
||||
AzFloat));
|
||||
blur_filter.set_input(GaussianBlurInput, &temporary_draw_target.snapshot());
|
||||
|
||||
// Blit the blur onto the tile. We undo the transforms here because we want to directly
|
||||
// stack the temporary draw target onto the tile.
|
||||
temporary_draw_target.set_transform(&Matrix2D::identity());
|
||||
self.draw_target.set_transform(&Matrix2D::identity());
|
||||
let temporary_draw_target_size = temporary_draw_target.get_size();
|
||||
self.draw_target
|
||||
.draw_filter(blur_filter,
|
||||
&Rect(Point2D(0.0, 0.0),
|
||||
Size2D(temporary_draw_target_size.width as AzFloat,
|
||||
temporary_draw_target_size.height as AzFloat)),
|
||||
&Point2D(-side_inflation as AzFloat, -side_inflation as AzFloat),
|
||||
DrawOptions::new(1.0, 0));
|
||||
self.draw_target.set_transform(&draw_target_transform);
|
||||
}
|
||||
|
||||
// Undo the draw target's clip.
|
||||
self.draw_target.pop_clip();
|
||||
|
||||
// Push back the stacking context clip.
|
||||
self.push_clip_if_applicable();
|
||||
}
|
||||
|
||||
pub fn push_clip_if_applicable(&self) {
|
||||
match self.clip_rect {
|
||||
None => {}
|
||||
Some(ref clip_rect) => self.draw_push_clip(clip_rect),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop_clip_if_applicable(&self) {
|
||||
if self.clip_rect.is_some() {
|
||||
self.draw_pop_clip()
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ToAzurePoint {
|
||||
|
@ -785,6 +892,28 @@ impl ToAzureSize for AzIntSize {
|
|||
}
|
||||
}
|
||||
|
||||
trait ToAzureIntSize {
|
||||
fn to_azure_int_size(&self) -> Size2D<i32>;
|
||||
}
|
||||
|
||||
impl ToAzureIntSize for Size2D<Au> {
|
||||
fn to_azure_int_size(&self) -> Size2D<i32> {
|
||||
Size2D(self.width.to_nearest_px() as i32, self.height.to_nearest_px() as i32)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToAzureIntSize for Size2D<AzFloat> {
|
||||
fn to_azure_int_size(&self) -> Size2D<i32> {
|
||||
Size2D(self.width as i32, self.height as i32)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToAzureIntSize for Size2D<i32> {
|
||||
fn to_azure_int_size(&self) -> Size2D<i32> {
|
||||
Size2D(self.width, self.height)
|
||||
}
|
||||
}
|
||||
|
||||
trait ToSideOffsetsPx {
|
||||
fn to_float_px(&self) -> SideOffsets2D<AzFloat>;
|
||||
}
|
||||
|
@ -890,3 +1019,65 @@ impl ScaledFontExtensionMethods for ScaledFont {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
trait DrawTargetExtensions {
|
||||
/// Creates and returns a path that represents a rectangular border. Like this:
|
||||
///
|
||||
/// ```text
|
||||
/// +--------------------------------+
|
||||
/// |################################|
|
||||
/// |#######+---------------------+##|
|
||||
/// |#######| |##|
|
||||
/// |#######+---------------------+##|
|
||||
/// |################################|
|
||||
/// +--------------------------------+
|
||||
/// ```
|
||||
fn create_rectangular_border_path<T>(&self, outer_rect: &T, inner_rect: &T)
|
||||
-> Path
|
||||
where T: ToAzureRect;
|
||||
|
||||
/// Creates and returns a path that represents a rectangle.
|
||||
fn create_rectangular_path(&self, rect: &Rect<Au>) -> Path;
|
||||
}
|
||||
|
||||
impl DrawTargetExtensions for DrawTarget {
|
||||
fn create_rectangular_border_path<T>(&self, outer_rect: &T, inner_rect: &T)
|
||||
-> Path
|
||||
where T: ToAzureRect {
|
||||
// +-----------+
|
||||
// |2 |1
|
||||
// | |
|
||||
// | +---+---+
|
||||
// | |9 |6 |5, 10
|
||||
// | | | |
|
||||
// | +---+ |
|
||||
// | 8 7 |
|
||||
// | |
|
||||
// +-----------+
|
||||
// 3 4
|
||||
|
||||
let (outer_rect, inner_rect) = (outer_rect.to_azure_rect(), inner_rect.to_azure_rect());
|
||||
let path_builder = self.create_path_builder();
|
||||
path_builder.move_to(Point2D(outer_rect.max_x(), outer_rect.origin.y)); // 1
|
||||
path_builder.line_to(Point2D(outer_rect.origin.x, outer_rect.origin.y)); // 2
|
||||
path_builder.line_to(Point2D(outer_rect.origin.x, outer_rect.max_y())); // 3
|
||||
path_builder.line_to(Point2D(outer_rect.max_x(), outer_rect.max_y())); // 4
|
||||
path_builder.line_to(Point2D(outer_rect.max_x(), inner_rect.origin.y)); // 5
|
||||
path_builder.line_to(Point2D(inner_rect.max_x(), inner_rect.origin.y)); // 6
|
||||
path_builder.line_to(Point2D(inner_rect.max_x(), inner_rect.max_y())); // 7
|
||||
path_builder.line_to(Point2D(inner_rect.origin.x, inner_rect.max_y())); // 8
|
||||
path_builder.line_to(inner_rect.origin); // 9
|
||||
path_builder.line_to(Point2D(outer_rect.max_x(), inner_rect.origin.y)); // 10
|
||||
path_builder.finish()
|
||||
}
|
||||
|
||||
fn create_rectangular_path(&self, rect: &Rect<Au>) -> Path {
|
||||
let path_builder = self.create_path_builder();
|
||||
path_builder.move_to(rect.origin.to_azure_point());
|
||||
path_builder.line_to(Point2D(rect.max_x(), rect.origin.y).to_azure_point());
|
||||
path_builder.line_to(Point2D(rect.max_x(), rect.max_y()).to_azure_point());
|
||||
path_builder.line_to(Point2D(rect.origin.x, rect.max_y()).to_azure_point());
|
||||
path_builder.finish()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue