diff --git a/gfx/src/nsFont.h b/gfx/src/nsFont.h index 3e8bffa5c1a1..47419407c451 100644 --- a/gfx/src/nsFont.h +++ b/gfx/src/nsFont.h @@ -41,11 +41,14 @@ struct nsFont final { mozilla::StyleFontSizeAdjust sizeAdjust = mozilla::StyleFontSizeAdjust::None(); +#pragma push_macro("TRANSPARENT") +#undef TRANSPARENT // The estimated background color behind the text. Enables a special // rendering mode when the alpha component > 0. Only used for text in the // chrome. mozilla::StyleAbsoluteColor fontSmoothingBackgroundColor = - mozilla::StyleAbsoluteColor::Transparent(); + mozilla::StyleAbsoluteColor::TRANSPARENT; +#pragma pop_macro("TRANSPARENT") // Language system tag, to override document language; // this is an OpenType "language system" tag represented as a 32-bit integer diff --git a/layout/generic/nsImageFrame.cpp b/layout/generic/nsImageFrame.cpp index e2f72b3a76ae..f98964457c1d 100644 --- a/layout/generic/nsImageFrame.cpp +++ b/layout/generic/nsImageFrame.cpp @@ -1809,7 +1809,7 @@ void nsImageFrame::DisplayAltText(nsPresContext* aPresContext, struct nsRecessedBorder : public nsStyleBorder { explicit nsRecessedBorder(nscoord aBorderWidth) { for (const auto side : AllPhysicalSides()) { - BorderColorFor(side) = StyleColor::Black(); + BorderColorFor(side) = StyleColor::BLACK; mBorder.Side(side) = aBorderWidth; // Note: use SetBorderStyle here because we want to affect // mComputedBorder diff --git a/layout/painting/nsCSSRenderingGradients.cpp b/layout/painting/nsCSSRenderingGradients.cpp index f4a859cf34e5..e7ca461653b4 100644 --- a/layout/painting/nsCSSRenderingGradients.cpp +++ b/layout/painting/nsCSSRenderingGradients.cpp @@ -552,7 +552,7 @@ static StyleAbsoluteColor GetSpecifiedColor( const StyleGenericGradientItem& aItem, const ComputedStyle& aStyle) { if (aItem.IsInterpolationHint()) { - return StyleAbsoluteColor::Transparent(); + return StyleAbsoluteColor::TRANSPARENT; } const StyleColor& c = aItem.IsSimpleColorStop() ? aItem.AsSimpleColorStop() @@ -1084,7 +1084,7 @@ void nsCSSGradientRenderer::Paint(gfxContext& aContext, const nsRect& aDest, gfxRect dirtyFillRect = fillRect.Intersect(dirtyAreaToFill); gfxRect fillRectRelativeToTile = dirtyFillRect - tileRect.TopLeft(); - auto edgeColor = StyleAbsoluteColor::Transparent(); + auto edgeColor = StyleAbsoluteColor::TRANSPARENT; if (mGradient->IsLinear() && !isRepeat && RectIsBeyondLinearGradientEdge(fillRectRelativeToTile, matrix, mStops, gradientStart, gradientEnd, diff --git a/layout/style/StyleColorInlines.h b/layout/style/StyleColorInlines.h index 72b4dbae8adb..e591c0e14d57 100644 --- a/layout/style/StyleColorInlines.h +++ b/layout/style/StyleColorInlines.h @@ -28,37 +28,31 @@ inline StyleAbsoluteColor StyleAbsoluteColor::Srgb(float red, float green, StyleColorSpace::Srgb, StyleColorFlags{0}}; } -inline StyleAbsoluteColor StyleAbsoluteColor::Transparent() { - return StyleAbsoluteColor::Srgb(0.0f, 0.0f, 0.0f, 0.0f); -} - -inline StyleAbsoluteColor StyleAbsoluteColor::Black() { - return StyleAbsoluteColor::Srgb(0.0f, 0.0f, 0.0f, 1.0f); -} - -inline StyleAbsoluteColor StyleAbsoluteColor::White() { - return StyleAbsoluteColor::Srgb(1.0f, 1.0f, 1.0f, 1.0f); -} - template <> inline StyleColor StyleColor::FromColor(nscolor aColor) { return StyleColor::Absolute(StyleAbsoluteColor::FromColor(aColor)); } -template <> -inline StyleColor StyleColor::Black() { - return FromColor(NS_RGB(0, 0, 0)); -} +// Workaround for window.h conflict. +#pragma push_macro("TRANSPARENT") +#undef TRANSPARENT +// static template <> -inline StyleColor StyleColor::White() { - return FromColor(NS_RGB(255, 255, 255)); -} +inline const StyleColor StyleColor::TRANSPARENT = + StyleColor::Absolute(StyleAbsoluteColor::TRANSPARENT); +#pragma pop_macro("TRANSPARENT") + +// static template <> -inline StyleColor StyleColor::Transparent() { - return FromColor(NS_RGBA(0, 0, 0, 0)); -} +inline const StyleColor StyleColor::BLACK = + StyleColor::Absolute(StyleAbsoluteColor::BLACK); + +// static +template <> +inline const StyleColor StyleColor::WHITE = + StyleColor::Absolute(StyleAbsoluteColor::WHITE); template <> StyleAbsoluteColor StyleColor::ResolveColor(const StyleAbsoluteColor&) const; diff --git a/layout/style/nsStyleStruct.cpp b/layout/style/nsStyleStruct.cpp index 019347ae88a5..2536de5c91b5 100644 --- a/layout/style/nsStyleStruct.cpp +++ b/layout/style/nsStyleStruct.cpp @@ -752,7 +752,7 @@ using SVGPaintFallback = StyleGenericSVGPaintFallback; // nsStyleSVG // nsStyleSVG::nsStyleSVG() - : mFill{StyleSVGPaintKind::Color(StyleColor::Black()), + : mFill{StyleSVGPaintKind::Color(StyleColor::BLACK), SVGPaintFallback::Unset()}, mStroke{StyleSVGPaintKind::None(), SVGPaintFallback::Unset()}, mMarkerEnd(StyleUrlOrNone::None()), @@ -899,9 +899,9 @@ nsStyleSVGReset::nsStyleSVGReset() mR(NonNegativeLengthPercentage::Zero()), mMask(nsStyleImageLayers::LayerType::Mask), mClipPath(StyleClipPath::None()), - mStopColor(StyleColor::Black()), - mFloodColor(StyleColor::Black()), - mLightingColor(StyleColor::White()), + mStopColor(StyleColor::BLACK), + mFloodColor(StyleColor::BLACK), + mLightingColor(StyleColor::WHITE), mStopOpacity(1.0f), mFloodOpacity(1.0f), mVectorEffect(StyleVectorEffect::None), @@ -2037,7 +2037,7 @@ nsChangeHint nsStyleImageLayers::Layer::CalcDifference( nsStyleBackground::nsStyleBackground() : mImage(nsStyleImageLayers::LayerType::Background), - mBackgroundColor(StyleColor::Transparent()) { + mBackgroundColor(StyleColor::TRANSPARENT) { MOZ_COUNT_CTOR(nsStyleBackground); } diff --git a/servo/components/style/color/mod.rs b/servo/components/style/color/mod.rs index 8731a4b14b1c..39355684f0e2 100644 --- a/servo/components/style/color/mod.rs +++ b/servo/components/style/color/mod.rs @@ -185,6 +185,30 @@ macro_rules! color_components_as { } impl AbsoluteColor { + /// A fully transparent color in the legacy syntax. + pub const TRANSPARENT: Self = Self { + components: ColorComponents(0.0, 0.0, 0.0), + alpha: 0.0, + color_space: ColorSpace::Srgb, + flags: ColorFlags { bits: 0 }, // cbindgen does not like ColorFlags::empty(). + }; + + /// An opaque black color in the legacy syntax. + pub const BLACK: Self = Self { + components: ColorComponents(0.0, 0.0, 0.0), + alpha: 1.0, + color_space: ColorSpace::Srgb, + flags: ColorFlags { bits: 0 }, // cbindgen does not like ColorFlags::empty(). + }; + + /// An opaque white color in the legacy syntax. + pub const WHITE: Self = Self { + components: ColorComponents(1.0, 1.0, 1.0), + alpha: 1.0, + color_space: ColorSpace::Srgb, + flags: ColorFlags { bits: 0 }, // cbindgen does not like ColorFlags::empty(). + }; + /// Create a new [`AbsoluteColor`] with the given [`ColorSpace`] and /// components. pub fn new(color_space: ColorSpace, components: ColorComponents, alpha: f32) -> Self { @@ -223,21 +247,6 @@ impl AbsoluteColor { Self::new(ColorSpace::Srgb, ColorComponents(red, green, blue), alpha) } - /// Create a new transparent color. - pub fn transparent() -> Self { - Self::srgb(0.0, 0.0, 0.0, 0.0) - } - - /// Create a new opaque black color. - pub fn black() -> Self { - Self::srgb(0.0, 0.0, 0.0, 1.0) - } - - /// Create a new opaque white color. - pub fn white() -> Self { - Self::srgb(1.0, 1.0, 1.0, 1.0) - } - /// Return all the components of the color in an array. (Includes alpha) #[inline] pub fn raw_components(&self) -> &[f32; 4] { diff --git a/servo/components/style/properties/cascade.rs b/servo/components/style/properties/cascade.rs index 883cb74d6b09..2e6092db08a0 100644 --- a/servo/components/style/properties/cascade.rs +++ b/servo/components/style/properties/cascade.rs @@ -445,7 +445,7 @@ fn tweak_when_ignoring_colors( // We assume here currentColor is opaque. color .to_computed_value(context) - .resolve_to_absolute(&AbsoluteColor::black()) + .resolve_to_absolute(&AbsoluteColor::BLACK) .alpha } diff --git a/servo/components/style/properties/longhands/background.mako.rs b/servo/components/style/properties/longhands/background.mako.rs index 76b71b12cd01..e01c2a4f8570 100644 --- a/servo/components/style/properties/longhands/background.mako.rs +++ b/servo/components/style/properties/longhands/background.mako.rs @@ -9,7 +9,7 @@ ${helpers.predefined_type( "background-color", "Color", - "computed::Color::transparent()", + "computed::Color::TRANSPARENT", engines="gecko servo-2013 servo-2020", initial_specified_value="SpecifiedValue::transparent()", spec="https://drafts.csswg.org/css-backgrounds/#background-color", diff --git a/servo/components/style/properties/longhands/font.mako.rs b/servo/components/style/properties/longhands/font.mako.rs index 7650fc2886ab..cd63d4562fa5 100644 --- a/servo/components/style/properties/longhands/font.mako.rs +++ b/servo/components/style/properties/longhands/font.mako.rs @@ -445,7 +445,7 @@ ${helpers.single_keyword( ${helpers.predefined_type( "-moz-font-smoothing-background-color", "color::MozFontSmoothingBackgroundColor", - "computed::color::MozFontSmoothingBackgroundColor::transparent()", + "computed::color::MozFontSmoothingBackgroundColor::TRANSPARENT", engines="gecko", animation_value_type="none", gecko_ffi_name="mFont.fontSmoothingBackgroundColor", diff --git a/servo/components/style/properties/longhands/inherited_svg.mako.rs b/servo/components/style/properties/longhands/inherited_svg.mako.rs index b9d55c01b20e..84b484660e52 100644 --- a/servo/components/style/properties/longhands/inherited_svg.mako.rs +++ b/servo/components/style/properties/longhands/inherited_svg.mako.rs @@ -51,7 +51,7 @@ ${helpers.single_keyword( ${helpers.predefined_type( "fill", "SVGPaint", - "crate::values::computed::SVGPaint::black()", + "crate::values::computed::SVGPaint::BLACK", engines="gecko", animation_value_type="IntermediateSVGPaint", boxed=True, diff --git a/servo/components/style/properties/longhands/inherited_text.mako.rs b/servo/components/style/properties/longhands/inherited_text.mako.rs index c1b110c08656..7b2108e2307a 100644 --- a/servo/components/style/properties/longhands/inherited_text.mako.rs +++ b/servo/components/style/properties/longhands/inherited_text.mako.rs @@ -9,7 +9,7 @@ ${helpers.predefined_type( "color", "ColorPropertyValue", - "crate::color::AbsoluteColor::black()", + "crate::color::AbsoluteColor::BLACK", engines="gecko servo-2013 servo-2020", animation_value_type="AbsoluteColor", ignored_when_colors_disabled="True", diff --git a/servo/components/style/properties/longhands/svg.mako.rs b/servo/components/style/properties/longhands/svg.mako.rs index d579473a0230..165687992288 100644 --- a/servo/components/style/properties/longhands/svg.mako.rs +++ b/servo/components/style/properties/longhands/svg.mako.rs @@ -20,7 +20,7 @@ ${helpers.single_keyword( ${helpers.predefined_type( "stop-color", "Color", - "computed::Color::black()", + "computed::Color::BLACK", engines="gecko", animation_value_type="AnimatedRGBA", spec="https://www.w3.org/TR/SVGTiny12/painting.html#StopColorProperty", @@ -40,7 +40,7 @@ ${helpers.predefined_type( ${helpers.predefined_type( "flood-color", "Color", - "computed::Color::black()", + "computed::Color::BLACK", engines="gecko", animation_value_type="AnimatedColor", spec="https://www.w3.org/TR/SVG/filters.html#FloodColorProperty", @@ -58,7 +58,7 @@ ${helpers.predefined_type( ${helpers.predefined_type( "lighting-color", "Color", - "computed::Color::white()", + "computed::Color::WHITE", engines="gecko", animation_value_type="AnimatedColor", spec="https://www.w3.org/TR/SVG/filters.html#LightingColorProperty", diff --git a/servo/components/style/values/animated/color.rs b/servo/components/style/values/animated/color.rs index d03eb9b3a855..12d865b463fe 100644 --- a/servo/components/style/values/animated/color.rs +++ b/servo/components/style/values/animated/color.rs @@ -74,7 +74,7 @@ impl Animate for Color { impl ComputeSquaredDistance for Color { #[inline] fn compute_squared_distance(&self, other: &Self) -> Result { - let current_color = AbsoluteColor::transparent(); + let current_color = AbsoluteColor::TRANSPARENT; self.resolve_to_absolute(¤t_color) .compute_squared_distance(&other.resolve_to_absolute(¤t_color)) } @@ -83,6 +83,6 @@ impl ComputeSquaredDistance for Color { impl ToAnimatedZero for Color { #[inline] fn to_animated_zero(&self) -> Result { - Ok(Color::Absolute(AbsoluteColor::transparent())) + Ok(Color::Absolute(AbsoluteColor::TRANSPARENT)) } } diff --git a/servo/components/style/values/computed/color.rs b/servo/components/style/values/computed/color.rs index 820bcfe5b1b3..035ad43dada6 100644 --- a/servo/components/style/values/computed/color.rs +++ b/servo/components/style/values/computed/color.rs @@ -42,6 +42,15 @@ impl ToCss for Color { } impl Color { + /// A fully transparent color. + pub const TRANSPARENT: Self = Self::Absolute(AbsoluteColor::TRANSPARENT); + + /// An opaque black color. + pub const BLACK: Self = Self::Absolute(AbsoluteColor::BLACK); + + /// An opaque white color. + pub const WHITE: Self = Self::Absolute(AbsoluteColor::WHITE); + /// Create a new computed [`Color`] from a given color-mix, simplifying it to an absolute color /// if possible. pub fn from_color_mix(color_mix: ColorMix) -> Self { @@ -52,21 +61,6 @@ impl Color { } } - /// Returns a complex color value representing transparent. - pub fn transparent() -> Color { - Color::Absolute(AbsoluteColor::transparent()) - } - - /// Returns opaque black. - pub fn black() -> Color { - Color::Absolute(AbsoluteColor::black()) - } - - /// Returns opaque white. - pub fn white() -> Color { - Color::Absolute(AbsoluteColor::white()) - } - /// Combine this complex color with the given foreground color into an /// absolute color. pub fn resolve_to_absolute(&self, current_color: &AbsoluteColor) -> AbsoluteColor { @@ -93,7 +87,7 @@ impl Color { impl ToAnimatedZero for AbsoluteColor { fn to_animated_zero(&self) -> Result { - Ok(Self::transparent()) + Ok(Self::TRANSPARENT) } } diff --git a/servo/components/style/values/computed/svg.rs b/servo/components/style/values/computed/svg.rs index 640c3bfda708..6efdfca36b7b 100644 --- a/servo/components/style/values/computed/svg.rs +++ b/servo/components/style/values/computed/svg.rs @@ -20,12 +20,10 @@ pub type SVGPaintKind = generic::GenericSVGPaintKind; impl SVGPaint { /// Opaque black color - pub fn black() -> Self { - SVGPaint { - kind: generic::SVGPaintKind::Color(Color::black()), - fallback: generic::SVGPaintFallback::Unset, - } - } + pub const BLACK: Self = Self { + kind: generic::SVGPaintKind::Color(Color::BLACK), + fallback: generic::SVGPaintFallback::Unset, + }; } /// | | | context-value diff --git a/servo/components/style/values/specified/color.rs b/servo/components/style/values/specified/color.rs index 3a745d488bd2..18c747dc0c96 100644 --- a/servo/components/style/values/specified/color.rs +++ b/servo/components/style/values/specified/color.rs @@ -801,7 +801,7 @@ impl Color { #[inline] pub fn transparent() -> Self { // We should probably set authored to "transparent", but maybe it doesn't matter. - Self::from_absolute_color(AbsoluteColor::transparent()) + Self::from_absolute_color(AbsoluteColor::TRANSPARENT) } /// Create a color from an [`AbsoluteColor`]. @@ -974,7 +974,7 @@ impl ToComputedValue for MozFontSmoothingBackgroundColor { fn to_computed_value(&self, context: &Context) -> Self::ComputedValue { self.0 .to_computed_value(context) - .resolve_to_absolute(&AbsoluteColor::transparent()) + .resolve_to_absolute(&AbsoluteColor::TRANSPARENT) } fn from_computed_value(computed: &Self::ComputedValue) -> Self { diff --git a/servo/ports/geckolib/cbindgen.toml b/servo/ports/geckolib/cbindgen.toml index b72759f2741d..88e74666f377 100644 --- a/servo/ports/geckolib/cbindgen.toml +++ b/servo/ports/geckolib/cbindgen.toml @@ -10,9 +10,12 @@ header = """/* This Source Code Form is subject to the terms of the Mozilla Publ // Work-around silly windows.h define. #pragma push_macro("STRICT") #undef STRICT +#pragma push_macro("TRANSPARENT") +#undef TRANSPARENT """ trailer = """ #pragma pop_macro("STRICT") +#pragma pop_macro("TRANSPARENT") #pragma GCC diagnostic pop #include "mozilla/ServoStyleConstsInlines.h" """ @@ -546,10 +549,12 @@ renaming_overrides_prefixing = true """ "GenericColor" = """ + static const StyleGenericColor TRANSPARENT; + static const StyleGenericColor BLACK; + static const StyleGenericColor WHITE; + static inline StyleGenericColor FromColor(nscolor); - static inline StyleGenericColor Black(); - static inline StyleGenericColor White(); - static inline StyleGenericColor Transparent(); + bool MaybeTransparent() const; /** @@ -580,10 +585,6 @@ renaming_overrides_prefixing = true */ static inline StyleAbsoluteColor Srgb(float red, float green, float blue, float alpha); - static inline StyleAbsoluteColor Transparent(); - static inline StyleAbsoluteColor Black(); - static inline StyleAbsoluteColor White(); - static inline StyleAbsoluteColor FromColor(nscolor); /**