Skip to content

Commit

Permalink
Make text-shadow and box-shadow use SimpleShadow
Browse files Browse the repository at this point in the history
  • Loading branch information
nox committed Jun 28, 2017
1 parent 65ff4a3 commit 201d7e7
Show file tree
Hide file tree
Showing 14 changed files with 414 additions and 434 deletions.
40 changes: 23 additions & 17 deletions components/layout/display_list_builder.rs
Expand Up @@ -57,7 +57,8 @@ use style::properties::style_structs;
use style::servo::restyle_damage::REPAINT;
use style::values::{Either, RGBA};
use style::values::computed::{Gradient, GradientItem, LengthOrPercentage};
use style::values::computed::{LengthOrPercentageOrAuto, NumberOrPercentage, Position, Shadow};
use style::values::computed::{LengthOrPercentageOrAuto, NumberOrPercentage, Position};
use style::values::computed::effects::SimpleShadow;
use style::values::computed::image::{EndingShape, LineDirection};
use style::values::generics::background::BackgroundSize;
use style::values::generics::effects::Filter;
Expand Down Expand Up @@ -525,7 +526,7 @@ pub trait FragmentDisplayListBuilding {
state: &mut DisplayListBuildState,
text_fragment: &ScannedTextFragmentInfo,
stacking_relative_content_box: &Rect<Au>,
text_shadow: Option<&Shadow>,
text_shadow: Option<&SimpleShadow>,
clip: &Rect<Au>);

/// Creates the display item for a text decoration: underline, overline, or line-through.
Expand All @@ -534,7 +535,7 @@ pub trait FragmentDisplayListBuilding {
color: &RGBA,
stacking_relative_box: &LogicalRect<Au>,
clip: &Rect<Au>,
blur_radius: Au);
blur: Au);

/// A helper method that `build_display_list` calls to create per-fragment-type display items.
fn build_fragment_type_specific_display_items(&mut self,
Expand Down Expand Up @@ -1351,11 +1352,14 @@ impl FragmentDisplayListBuilding for Fragment {
clip: &Rect<Au>) {
// NB: According to CSS-BACKGROUNDS, box shadows render in *reverse* order (front to back).
for box_shadow in style.get_effects().box_shadow.0.iter().rev() {
let bounds =
shadow_bounds(&absolute_bounds.translate(&Vector2D::new(box_shadow.offset_x,
box_shadow.offset_y)),
box_shadow.blur_radius,
box_shadow.spread_radius);
let bounds = shadow_bounds(
&absolute_bounds.translate(&Vector2D::new(
box_shadow.base.horizontal,
box_shadow.base.vertical,
)),
box_shadow.base.blur,
box_shadow.spread,
);

// TODO(pcwalton): Multiple border radii; elliptical border radii.
let base = state.create_base_display_item(&bounds,
Expand All @@ -1366,10 +1370,10 @@ impl FragmentDisplayListBuilding for Fragment {
state.add_display_item(DisplayItem::BoxShadow(box BoxShadowDisplayItem {
base: base,
box_bounds: *absolute_bounds,
color: style.resolve_color(box_shadow.color).to_gfx_color(),
offset: Vector2D::new(box_shadow.offset_x, box_shadow.offset_y),
blur_radius: box_shadow.blur_radius,
spread_radius: box_shadow.spread_radius,
color: style.resolve_color(box_shadow.base.color).to_gfx_color(),
offset: Vector2D::new(box_shadow.base.horizontal, box_shadow.base.vertical),
blur_radius: box_shadow.base.blur,
spread_radius: box_shadow.spread,
border_radius: model::specified_border_radius(style.get_border()
.border_top_left_radius,
absolute_bounds.size).width,
Expand Down Expand Up @@ -2039,7 +2043,7 @@ impl FragmentDisplayListBuilding for Fragment {
state: &mut DisplayListBuildState,
text_fragment: &ScannedTextFragmentInfo,
stacking_relative_content_box: &Rect<Au>,
text_shadow: Option<&Shadow>,
text_shadow: Option<&SimpleShadow>,
clip: &Rect<Au>) {
// TODO(emilio): Allow changing more properties by ::selection
let text_color = if let Some(shadow) = text_shadow {
Expand All @@ -2051,8 +2055,10 @@ impl FragmentDisplayListBuilding for Fragment {
} else {
self.style().get_color().color
};
let offset = text_shadow.map(|s| Vector2D::new(s.offset_x, s.offset_y)).unwrap_or_else(Vector2D::zero);
let shadow_blur_radius = text_shadow.map(|s| s.blur_radius).unwrap_or(Au(0));
let offset = text_shadow.map_or(Vector2D::zero(), |s| {
Vector2D::new(s.horizontal, s.vertical)
});
let shadow_blur_radius = text_shadow.map(|s| s.blur).unwrap_or(Au(0));

// Determine the orientation and cursor to use.
let (orientation, cursor) = if self.style.writing_mode.is_vertical() {
Expand Down Expand Up @@ -2885,8 +2891,8 @@ fn position_to_offset(position: LengthOrPercentage, total_length: Au) -> f32 {

/// Adjusts `content_rect` as necessary for the given spread, and blur so that the resulting
/// bounding rect contains all of a shadow's ink.
fn shadow_bounds(content_rect: &Rect<Au>, blur_radius: Au, spread_radius: Au) -> Rect<Au> {
let inflation = spread_radius + blur_radius * BLUR_INFLATION_FACTOR;
fn shadow_bounds(content_rect: &Rect<Au>, blur: Au, spread: Au) -> Rect<Au> {
let inflation = spread + blur * BLUR_INFLATION_FACTOR;
content_rect.inflate(inflation, inflation)
}

Expand Down
5 changes: 2 additions & 3 deletions components/layout/fragment.rs
Expand Up @@ -2551,9 +2551,8 @@ impl Fragment {

// Box shadows cause us to draw outside our border box.
for box_shadow in &self.style().get_effects().box_shadow.0 {
let offset = Vector2D::new(box_shadow.offset_x, box_shadow.offset_y);
let inflation = box_shadow.spread_radius + box_shadow.blur_radius *
BLUR_INFLATION_FACTOR;
let offset = Vector2D::new(box_shadow.base.horizontal, box_shadow.base.vertical);
let inflation = box_shadow.spread + box_shadow.base.blur * BLUR_INFLATION_FACTOR;
overflow.paint = overflow.paint.union(&border_box.translate(&offset)
.inflate(inflation, inflation))
}
Expand Down
45 changes: 19 additions & 26 deletions components/style/gecko_bindings/sugar/ns_css_shadow_item.rs
Expand Up @@ -7,37 +7,30 @@
use app_units::Au;
use gecko::values::{convert_rgba_to_nscolor, convert_nscolor_to_rgba};
use gecko_bindings::structs::nsCSSShadowItem;
use values::computed::{Color, Shadow};
use values::computed::effects::SimpleShadow;
use values::computed::Color;
use values::computed::effects::{BoxShadow, SimpleShadow};

impl nsCSSShadowItem {
/// Set this item to the given shadow value.
pub fn set_from_shadow(&mut self, other: Shadow) {
self.mXOffset = other.offset_x.0;
self.mYOffset = other.offset_y.0;
self.mRadius = other.blur_radius.0;
self.mSpread = other.spread_radius.0;
self.mInset = other.inset;
if other.color.is_currentcolor() {
// TODO handle currentColor
// https://bugzilla.mozilla.org/show_bug.cgi?id=760345
self.mHasColor = false;
self.mColor = 0;
} else {
self.mHasColor = true;
self.mColor = convert_rgba_to_nscolor(&other.color.color);
}
/// Sets this item from the given box shadow.
#[inline]
pub fn set_from_box_shadow(&mut self, shadow: BoxShadow) {
self.set_from_simple_shadow(shadow.base);
self.mSpread = shadow.spread.0;
self.mInset = shadow.inset;
}

/// Generate shadow value from this shadow item.
pub fn to_shadow(&self) -> Shadow {
Shadow {
offset_x: Au(self.mXOffset),
offset_y: Au(self.mYOffset),
blur_radius: Au(self.mRadius),
spread_radius: Au(self.mSpread),
/// Returns this item as a box shadow.
#[inline]
pub fn to_box_shadow(&self) -> BoxShadow {
BoxShadow {
base: SimpleShadow {
color: Color::rgba(convert_nscolor_to_rgba(self.mColor)),
horizontal: Au(self.mXOffset),
vertical: Au(self.mYOffset),
blur: Au(self.mRadius),
},
spread: Au(self.mSpread),
inset: self.mInset,
color: Color::rgba(convert_nscolor_to_rgba(self.mColor)),
}
}

Expand Down
14 changes: 7 additions & 7 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -60,7 +60,7 @@ use std::ptr;
use stylearc::Arc;
use std::cmp;
use values::{Auto, CustomIdent, Either, KeyframesName};
use values::computed::{Filter, Shadow};
use values::computed::effects::{BoxShadow, Filter, SimpleShadow};
use values::specified::length::Percentage;
use computed_values::border_style;

Expand Down Expand Up @@ -3173,13 +3173,13 @@ fn static_assert() {
<%self:impl_trait style_struct_name="Effects"
skip_longhands="box-shadow clip filter">
pub fn set_box_shadow<I>(&mut self, v: I)
where I: IntoIterator<Item = Shadow>,
where I: IntoIterator<Item = BoxShadow>,
I::IntoIter: ExactSizeIterator
{
let v = v.into_iter();
self.gecko.mBoxShadow.replace_with_new(v.len() as u32);
for (servo, gecko_shadow) in v.zip(self.gecko.mBoxShadow.iter_mut()) {
gecko_shadow.set_from_shadow(servo);
gecko_shadow.set_from_box_shadow(servo);
}
}

Expand All @@ -3188,7 +3188,7 @@ fn static_assert() {
}

pub fn clone_box_shadow(&self) -> longhands::box_shadow::computed_value::T {
let buf = self.gecko.mBoxShadow.iter().map(|v| v.to_shadow()).collect();
let buf = self.gecko.mBoxShadow.iter().map(|v| v.to_box_shadow()).collect();
longhands::box_shadow::computed_value::T(buf)
}

Expand Down Expand Up @@ -3494,13 +3494,13 @@ fn static_assert() {
${impl_keyword_clone('text_align', 'mTextAlign', text_align_keyword)}

pub fn set_text_shadow<I>(&mut self, v: I)
where I: IntoIterator<Item = Shadow>,
where I: IntoIterator<Item = SimpleShadow>,
I::IntoIter: ExactSizeIterator
{
let v = v.into_iter();
self.gecko.mTextShadow.replace_with_new(v.len() as u32);
for (servo, gecko_shadow) in v.zip(self.gecko.mTextShadow.iter_mut()) {
gecko_shadow.set_from_shadow(servo);
gecko_shadow.set_from_simple_shadow(servo);
}
}

Expand All @@ -3509,7 +3509,7 @@ fn static_assert() {
}

pub fn clone_text_shadow(&self) -> longhands::text_shadow::computed_value::T {
let buf = self.gecko.mTextShadow.iter().map(|v| v.to_shadow()).collect();
let buf = self.gecko.mTextShadow.iter().map(|v| v.to_simple_shadow()).collect();
longhands::text_shadow::computed_value::T(buf)
}

Expand Down

0 comments on commit 201d7e7

Please sign in to comment.