Skip to content
Permalink
Browse files

style: Remove nsStyleCoord.

And move the useful bits of it somewhere else (ServoStyleConstInlines.h for the
inline function definitions, and nsFrame.cpp for the static assertions).

Differential Revision: https://phabricator.services.mozilla.com/D36120
  • Loading branch information...
emilio committed Jun 28, 2019
1 parent 8d12701 commit 31b166fb1b677a1cdc3bee6828a94c1e30a7fa02
@@ -10,49 +10,15 @@

#![allow(unsafe_code)]

use crate::gecko::values::GeckoStyleCoordConvertible;
use crate::gecko_bindings::bindings;
use crate::gecko_bindings::structs::{self, nsStyleCoord_CalcValue, Matrix4x4Components};
use crate::gecko_bindings::structs::{self, Matrix4x4Components};
use crate::gecko_bindings::structs::{nsStyleImage, nsresult};
use crate::stylesheets::RulesMutateError;
use crate::values::computed::transform::Matrix3D;
use crate::values::computed::url::ComputedImageUrl;
use crate::values::computed::{Gradient, Image, LengthPercentage};
use crate::values::computed::{Length, Percentage, TextAlign};
use crate::values::computed::{Gradient, Image, TextAlign};
use crate::values::generics::image::GenericImage;
use crate::values::generics::rect::Rect;
use crate::Zero;
use app_units::Au;
use style_traits::values::specified::AllowedNumericType;

impl From<LengthPercentage> for nsStyleCoord_CalcValue {
fn from(other: LengthPercentage) -> nsStyleCoord_CalcValue {
debug_assert!(
other.was_calc || !other.has_percentage || other.unclamped_length() == Length::zero()
);
nsStyleCoord_CalcValue {
mLength: other.unclamped_length().to_i32_au(),
mPercent: other.percentage(),
mHasPercent: other.has_percentage,
}
}
}

impl From<nsStyleCoord_CalcValue> for LengthPercentage {
fn from(other: nsStyleCoord_CalcValue) -> LengthPercentage {
let percentage = if other.mHasPercent {
Some(Percentage(other.mPercent))
} else {
None
};
Self::with_clamping_mode(
Au(other.mLength).into(),
percentage,
AllowedNumericType::All,
/* was_calc = */ true,
)
}
}

impl nsStyleImage {
/// Set a given Servo `Image` value into this `nsStyleImage`.
@@ -296,31 +262,6 @@ impl From<RulesMutateError> for nsresult {
}
}

impl<T> Rect<T>
where
T: GeckoStyleCoordConvertible,
{
/// Convert this generic Rect to given Gecko fields.
pub fn to_gecko_rect(&self, sides: &mut crate::gecko_bindings::structs::nsStyleSides) {
self.0.to_gecko_style_coord(&mut sides.data_at_mut(0));
self.1.to_gecko_style_coord(&mut sides.data_at_mut(1));
self.2.to_gecko_style_coord(&mut sides.data_at_mut(2));
self.3.to_gecko_style_coord(&mut sides.data_at_mut(3));
}

/// Convert from given Gecko data to generic Rect.
pub fn from_gecko_rect(
sides: &crate::gecko_bindings::structs::nsStyleSides,
) -> Option<crate::values::generics::rect::Rect<T>> {
Some(Rect::new(
T::from_gecko_style_coord(&sides.data_at(0)).expect("coord[0] cound not convert"),
T::from_gecko_style_coord(&sides.data_at(1)).expect("coord[1] cound not convert"),
T::from_gecko_style_coord(&sides.data_at(2)).expect("coord[2] cound not convert"),
T::from_gecko_style_coord(&sides.data_at(3)).expect("coord[3] cound not convert"),
))
}
}

impl TextAlign {
/// Obtain a specified value from a Gecko keyword value
///
@@ -7,165 +7,15 @@
//! Different kind of helpers to interact with Gecko values.

use crate::counter_style::{Symbol, Symbols};
use crate::gecko_bindings::structs::{nsStyleCoord, CounterStylePtr};
use crate::gecko_bindings::sugar::ns_style_coord::{CoordData, CoordDataMut, CoordDataValue};
use crate::values::computed::{Length, LengthPercentage};
use crate::values::computed::{Number, NumberOrPercentage, Percentage};
use crate::values::generics::length::LengthPercentageOrAuto;
use crate::values::generics::{CounterStyleOrNone, NonNegative};
use crate::gecko_bindings::structs::CounterStylePtr;
use crate::values::generics::CounterStyleOrNone;
use crate::values::Either;
use crate::{Atom, Zero};
use crate::Atom;
use app_units::Au;
use cssparser::RGBA;
use nsstring::{nsACString, nsCStr};
use std::cmp::max;

/// A trait that defines an interface to convert from and to `nsStyleCoord`s.
///
/// TODO(emilio): Almost everything that is in this file should be somehow
/// switched to cbindgen.
pub trait GeckoStyleCoordConvertible: Sized {
/// Convert this to a `nsStyleCoord`.
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T);
/// Given a `nsStyleCoord`, try to get a value of this type..
fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self>;
}

impl nsStyleCoord {
#[inline]
/// Set this `nsStyleCoord` value to `val`.
pub fn set<T: GeckoStyleCoordConvertible>(&mut self, val: T) {
val.to_gecko_style_coord(self);
}
}

impl<Inner> GeckoStyleCoordConvertible for NonNegative<Inner>
where
Inner: GeckoStyleCoordConvertible,
{
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
self.0.to_gecko_style_coord(coord)
}

fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
Some(NonNegative(Inner::from_gecko_style_coord(coord)?))
}
}

impl GeckoStyleCoordConvertible for Number {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
coord.set_value(CoordDataValue::Factor(*self));
}

fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
match coord.as_value() {
CoordDataValue::Factor(f) => Some(f),
_ => None,
}
}
}

impl GeckoStyleCoordConvertible for Percentage {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
coord.set_value(CoordDataValue::Percent(self.0));
}

fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
match coord.as_value() {
CoordDataValue::Percent(p) => Some(Percentage(p)),
_ => None,
}
}
}

impl GeckoStyleCoordConvertible for NumberOrPercentage {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
match *self {
NumberOrPercentage::Number(ref n) => n.to_gecko_style_coord(coord),
NumberOrPercentage::Percentage(ref p) => p.to_gecko_style_coord(coord),
}
}

fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
match coord.as_value() {
CoordDataValue::Factor(f) => Some(NumberOrPercentage::Number(f)),
CoordDataValue::Percent(p) => Some(NumberOrPercentage::Percentage(Percentage(p))),
_ => None,
}
}
}

impl GeckoStyleCoordConvertible for LengthPercentage {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
if self.was_calc {
return coord.set_value(CoordDataValue::Calc((*self).into()));
}
debug_assert!(!self.has_percentage || self.unclamped_length() == Length::zero());
if self.has_percentage {
return coord.set_value(CoordDataValue::Percent(self.percentage()));
}
coord.set_value(CoordDataValue::Coord(self.unclamped_length().to_i32_au()))
}

fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
match coord.as_value() {
CoordDataValue::Coord(coord) => Some(LengthPercentage::new(Au(coord).into(), None)),
CoordDataValue::Percent(p) => {
Some(LengthPercentage::new(Au(0).into(), Some(Percentage(p))))
},
CoordDataValue::Calc(calc) => Some(calc.into()),
_ => None,
}
}
}

impl GeckoStyleCoordConvertible for Length {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
coord.set_value(CoordDataValue::Coord(self.to_i32_au()));
}

fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
match coord.as_value() {
CoordDataValue::Coord(coord) => Some(Au(coord).into()),
_ => None,
}
}
}

impl<LengthPercentage> GeckoStyleCoordConvertible for LengthPercentageOrAuto<LengthPercentage>
where
LengthPercentage: GeckoStyleCoordConvertible,
{
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
match *self {
LengthPercentageOrAuto::Auto => coord.set_value(CoordDataValue::Auto),
LengthPercentageOrAuto::LengthPercentage(ref lp) => lp.to_gecko_style_coord(coord),
}
}

fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
match coord.as_value() {
CoordDataValue::Auto => Some(LengthPercentageOrAuto::Auto),
_ => LengthPercentage::from_gecko_style_coord(coord)
.map(LengthPercentageOrAuto::LengthPercentage),
}
}
}

impl<T: GeckoStyleCoordConvertible> GeckoStyleCoordConvertible for Option<T> {
fn to_gecko_style_coord<U: CoordDataMut>(&self, coord: &mut U) {
if let Some(ref me) = *self {
me.to_gecko_style_coord(coord);
} else {
coord.set_value(CoordDataValue::None);
}
}

fn from_gecko_style_coord<U: CoordData>(coord: &U) -> Option<Self> {
Some(T::from_gecko_style_coord(coord))
}
}

/// Convert a given RGBA value to `nscolor`.
pub fn convert_rgba_to_nscolor(rgba: &RGBA) -> u32 {
((rgba.alpha as u32) << 24) |
@@ -7,7 +7,6 @@
mod ns_com_ptr;
mod ns_compatibility;
mod ns_style_auto_array;
pub mod ns_style_coord;
mod ns_t_array;
pub mod origin_flags;
pub mod ownership;

0 comments on commit 31b166f

Please sign in to comment.
You can’t perform that action at this time.