style: Use cbindgen instead of nsStyleImage.

The trickier part is that we represent -moz-image-rect as a Rect() type instead
of image with non-null clip-rect. So we need to add a bit of code to
distinguish "image request types" from other types of images.

But it's not too annoying, and we need to do the same for fancier images like
image-set and such whenever we implement it, so seems nice to get rid of
most explicit usages of nsStyleImage::GetType().

Differential Revision: https://phabricator.services.mozilla.com/D62164
This commit is contained in:
Emilio Cobos Álvarez 2020-02-11 20:56:20 +00:00
parent 4dadcf71a5
commit 558cc59288
2 changed files with 15 additions and 119 deletions

View file

@ -10,87 +10,10 @@
#![allow(unsafe_code)]
use crate::gecko_bindings::bindings;
use crate::gecko_bindings::structs::{self, Matrix4x4Components};
use crate::gecko_bindings::structs::{nsStyleImage, nsresult};
use crate::gecko_bindings::structs::{self, Matrix4x4Components, nsresult};
use crate::stylesheets::RulesMutateError;
use crate::values::computed::transform::Matrix3D;
use crate::values::computed::{Gradient, Image, TextAlign};
use crate::values::generics::image::GenericImage;
use crate::values::generics::rect::Rect;
impl nsStyleImage {
/// Set a given Servo `Image` value into this `nsStyleImage`.
pub fn set(&mut self, image: Image) {
match image {
GenericImage::None => unsafe {
bindings::Gecko_SetNullImageValue(self);
},
GenericImage::Gradient(boxed_gradient) => self.set_gradient(boxed_gradient),
GenericImage::Url(ref url) => unsafe {
bindings::Gecko_SetLayerImageImageValue(self, url);
},
GenericImage::Rect(ref image_rect) => {
unsafe {
bindings::Gecko_SetLayerImageImageValue(self, &image_rect.url);
bindings::Gecko_InitializeImageCropRect(self);
// Set CropRect
let ref mut rect = *self.mCropRect.mPtr;
*rect = Rect(
image_rect.top,
image_rect.right,
image_rect.bottom,
image_rect.left,
);
}
},
GenericImage::Element(ref element) => unsafe {
bindings::Gecko_SetImageElement(self, element.as_ptr());
},
}
}
fn set_gradient(&mut self, gradient: Box<Gradient>) {
unsafe {
bindings::Gecko_SetGradientImageValue(self, Box::into_raw(gradient));
}
}
/// Converts into Image.
pub unsafe fn to_image(&self) -> Image {
use crate::gecko_bindings::structs::nsStyleImageType;
use crate::values::computed::MozImageRect;
match self.mType {
nsStyleImageType::eStyleImageType_Null => GenericImage::None,
nsStyleImageType::eStyleImageType_Image => {
let url = self.__bindgen_anon_1.mImage.as_ref().clone();
if self.mCropRect.mPtr.is_null() {
GenericImage::Url(url)
} else {
let rect = &*self.mCropRect.mPtr;
GenericImage::Rect(Box::new(MozImageRect {
url,
top: rect.0,
right: rect.1,
bottom: rect.2,
left: rect.3,
}))
}
},
nsStyleImageType::eStyleImageType_Gradient => {
let gradient: &Gradient = &**self.__bindgen_anon_1.mGradient.as_ref();
GenericImage::Gradient(Box::new(gradient.clone()))
},
nsStyleImageType::eStyleImageType_Element => {
use crate::gecko_string_cache::Atom;
let atom = bindings::Gecko_GetImageElement(self);
GenericImage::Element(Atom::from_raw(atom))
},
}
}
}
use crate::values::computed::TextAlign;
pub mod basic_shape {
//! Conversions from and to CSS shape representations.
@ -148,9 +71,8 @@ pub mod basic_shape {
use crate::values::generics::image::Image as GenericImage;
let shape_image = &*other.__bindgen_anon_1.mShapeImage.as_ref().mPtr;
let image = shape_image.to_image();
match image {
GenericImage::Url(url) => ShapeSource::ImageOrUrl(url.0),
match *shape_image {
GenericImage::Url(ref url) => ShapeSource::ImageOrUrl(url.0.clone()),
_ => panic!("ClippingShape doesn't support non-url images"),
}
},
@ -166,8 +88,7 @@ pub mod basic_shape {
match other.mType {
StyleShapeSourceType::Image => unsafe {
let shape_image = &*other.__bindgen_anon_1.mShapeImage.as_ref().mPtr;
let image = shape_image.to_image();
ShapeSource::ImageOrUrl(image)
ShapeSource::ImageOrUrl(shape_image.clone())
},
_ => other
.to_shape_source()

View file

@ -24,11 +24,9 @@ use crate::gecko_bindings::bindings::Gecko_Destroy_${style_struct.gecko_ffi_name
use crate::gecko_bindings::bindings::Gecko_CopyCounterStyle;
use crate::gecko_bindings::bindings::Gecko_CopyCursorArrayFrom;
use crate::gecko_bindings::bindings::Gecko_CopyFontFamilyFrom;
use crate::gecko_bindings::bindings::Gecko_CopyImageValueFrom;
use crate::gecko_bindings::bindings::Gecko_EnsureImageLayersLength;
use crate::gecko_bindings::bindings::Gecko_nsStyleFont_SetLang;
use crate::gecko_bindings::bindings::Gecko_nsStyleFont_CopyLangFrom;
use crate::gecko_bindings::bindings::Gecko_SetNullImageValue;
use crate::gecko_bindings::structs;
use crate::gecko_bindings::structs::nsCSSPropertyID;
use crate::gecko_bindings::structs::mozilla::PseudoStyleType;
@ -777,8 +775,7 @@ fn static_assert() {
for x in CORNERS]) %>
<%self:impl_trait style_struct_name="Border"
skip_longhands="${skip_border_longhands} border-image-source
border-image-repeat">
skip_longhands="${skip_border_longhands} border-image-repeat">
% for side in SIDES:
pub fn set_border_${side.ident}_style(&mut self, v: BorderStyle) {
self.gecko.mBorderStyle[${side.index}] = v;
@ -847,25 +844,6 @@ fn static_assert() {
corner) %>
% endfor
pub fn set_border_image_source(&mut self, image: longhands::border_image_source::computed_value::T) {
self.gecko.mBorderImageSource.set(image);
}
pub fn copy_border_image_source_from(&mut self, other: &Self) {
unsafe {
Gecko_CopyImageValueFrom(&mut self.gecko.mBorderImageSource,
&other.gecko.mBorderImageSource);
}
}
pub fn reset_border_image_source(&mut self, other: &Self) {
self.copy_border_image_source_from(other)
}
pub fn clone_border_image_source(&self) -> longhands::border_image_source::computed_value::T {
unsafe { self.gecko.mBorderImageSource.to_image() }
}
<%
border_image_repeat_keywords = ["Stretch", "Repeat", "Round", "Space"]
%>
@ -1998,7 +1976,7 @@ fn static_assert() {
for (layer, other) in self.gecko.${image_layers_field}.mLayers.iter_mut()
.zip(other.gecko.${image_layers_field}.mLayers.iter())
.take(count as usize) {
Gecko_CopyImageValueFrom(&mut layer.mImage, &other.mImage);
layer.mImage = other.mImage.clone();
}
self.gecko.${image_layers_field}.mImageCount = count;
}
@ -2018,20 +1996,17 @@ fn static_assert() {
let images = images.into_iter();
unsafe {
// Prevent leaking of the last elements we did set
for image in &mut self.gecko.${image_layers_field}.mLayers {
Gecko_SetNullImageValue(&mut image.mImage)
}
// XXXManishearth clear mSourceURI for masks
Gecko_EnsureImageLayersLength(&mut self.gecko.${image_layers_field}, images.len(),
LayerType::${shorthand.title()});
Gecko_EnsureImageLayersLength(
&mut self.gecko.${image_layers_field},
images.len(),
LayerType::${shorthand.title()},
);
}
self.gecko.${image_layers_field}.mImageCount = images.len() as u32;
for (image, geckoimage) in images.zip(self.gecko.${image_layers_field}
.mLayers.iter_mut()) {
geckoimage.mImage.set(image)
geckoimage.mImage = image;
}
}
@ -2039,7 +2014,7 @@ fn static_assert() {
longhands::${shorthand}_image::computed_value::List(
self.gecko.${image_layers_field}.mLayers.iter()
.take(self.gecko.${image_layers_field}.mImageCount as usize)
.map(|layer| unsafe { layer.mImage.to_image() })
.map(|layer| layer.mImage.clone())
.collect()
)
}
@ -2266,7 +2241,7 @@ fn set_style_svg_path(
unsafe {
bindings::Gecko_NewShapeImage(${ident});
let style_image = &mut *${ident}.__bindgen_anon_1.mShapeImage.as_mut().mPtr;
style_image.set(image);
*style_image = image;
}
}
ShapeSource::Box(reference) => {