Introduce CalcLengthOrPercentage::unclamped_length

This is necessary for some operations, notably converting this to something
suitable for gecko.
This commit is contained in:
Anthony Ramine 2017-05-18 19:18:00 +02:00
parent bcf1a6c5e5
commit 307dd74ecf
9 changed files with 32 additions and 20 deletions

View file

@ -2803,12 +2803,13 @@ struct StopRun {
stop_count: usize, stop_count: usize,
} }
fn position_to_offset(position: LengthOrPercentage, Au(total_length): Au) -> f32 { fn position_to_offset(position: LengthOrPercentage, total_length: Au) -> f32 {
match position { match position {
LengthOrPercentage::Length(Au(length)) => length as f32 / total_length as f32, LengthOrPercentage::Length(Au(length)) => length as f32 / total_length.0 as f32,
LengthOrPercentage::Percentage(percentage) => percentage as f32, LengthOrPercentage::Percentage(percentage) => percentage as f32,
LengthOrPercentage::Calc(calc) => LengthOrPercentage::Calc(calc) => {
calc.percentage() + (calc.length().0 as f32) / (total_length as f32), calc.to_used_value(Some(total_length)).unwrap().0 as f32 / total_length.0 as f32
},
} }
} }

View file

@ -1504,7 +1504,11 @@ impl Fragment {
result_inline result_inline
} }
LengthOrPercentageOrAuto::Length(length) => length, LengthOrPercentageOrAuto::Length(length) => length,
LengthOrPercentageOrAuto::Calc(calc) => calc.length(), LengthOrPercentageOrAuto::Calc(calc) => {
// TODO(nox): This is probably wrong, because it accounts neither for
// clamping (not sure if necessary here) nor percentage.
calc.unclamped_length()
},
}; };
let size_constraint = self.size_constraint(None, Direction::Inline); let size_constraint = self.size_constraint(None, Direction::Inline);
@ -2233,8 +2237,7 @@ impl Fragment {
offset -= minimum_line_metrics.space_needed().scale_by(percentage) offset -= minimum_line_metrics.space_needed().scale_by(percentage)
} }
vertical_align::T::LengthOrPercentage(LengthOrPercentage::Calc(formula)) => { vertical_align::T::LengthOrPercentage(LengthOrPercentage::Calc(formula)) => {
offset -= minimum_line_metrics.space_needed().scale_by(formula.percentage()) + offset -= formula.to_used_value(Some(minimum_line_metrics.space_needed())).unwrap()
formula.length()
} }
} }
} }

View file

@ -24,7 +24,7 @@ impl From<CalcLengthOrPercentage> for nsStyleCoord_CalcValue {
fn from(other: CalcLengthOrPercentage) -> nsStyleCoord_CalcValue { fn from(other: CalcLengthOrPercentage) -> nsStyleCoord_CalcValue {
let has_percentage = other.percentage.is_some(); let has_percentage = other.percentage.is_some();
nsStyleCoord_CalcValue { nsStyleCoord_CalcValue {
mLength: other.length().0, mLength: other.unclamped_length().0,
mPercent: other.percentage.unwrap_or(0.0), mPercent: other.percentage.unwrap_or(0.0),
mHasPercent: has_percentage, mHasPercent: has_percentage,
} }

View file

@ -1035,7 +1035,7 @@ impl Animatable for CalcLengthOrPercentage {
} }
} }
let length = self.length().add_weighted(&other.length(), self_portion, other_portion)?; let length = self.unclamped_length().add_weighted(&other.unclamped_length(), self_portion, other_portion)?;
let percentage = add_weighted_half(self.percentage, other.percentage, self_portion, other_portion)?; let percentage = add_weighted_half(self.percentage, other.percentage, self_portion, other_portion)?;
Ok(CalcLengthOrPercentage::with_clamping_mode(length, percentage, self.clamping_mode)) Ok(CalcLengthOrPercentage::with_clamping_mode(length, percentage, self.clamping_mode))
} }
@ -1047,7 +1047,7 @@ impl Animatable for CalcLengthOrPercentage {
#[inline] #[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<f64, ()> { fn compute_squared_distance(&self, other: &Self) -> Result<f64, ()> {
let length_diff = (self.length().0 - other.length().0) as f64; let length_diff = (self.unclamped_length().0 - other.unclamped_length().0) as f64;
let percentage_diff = (self.percentage() - other.percentage()) as f64; let percentage_diff = (self.percentage() - other.percentage()) as f64;
Ok(length_diff * length_diff + percentage_diff * percentage_diff) Ok(length_diff * length_diff + percentage_diff * percentage_diff)
} }
@ -1112,7 +1112,7 @@ impl Animatable for LengthOrPercentage {
(this, other) => { (this, other) => {
let this: CalcLengthOrPercentage = From::from(this); let this: CalcLengthOrPercentage = From::from(this);
let other: CalcLengthOrPercentage = From::from(other); let other: CalcLengthOrPercentage = From::from(other);
let length_diff = (this.length().0 - other.length().0) as f64; let length_diff = (this.unclamped_length().0 - other.unclamped_length().0) as f64;
let percentage_diff = (this.percentage() - other.percentage()) as f64; let percentage_diff = (this.percentage() - other.percentage()) as f64;
Ok(length_diff * length_diff + percentage_diff * percentage_diff) Ok(length_diff * length_diff + percentage_diff * percentage_diff)
} }
@ -1186,7 +1186,7 @@ impl Animatable for LengthOrPercentageOrAuto {
let this: Option<CalcLengthOrPercentage> = From::from(this); let this: Option<CalcLengthOrPercentage> = From::from(this);
let other: Option<CalcLengthOrPercentage> = From::from(other); let other: Option<CalcLengthOrPercentage> = From::from(other);
if let (Some(this), Some(other)) = (this, other) { if let (Some(this), Some(other)) = (this, other) {
let length_diff = (this.length().0 - other.length().0) as f64; let length_diff = (this.unclamped_length().0 - other.unclamped_length().0) as f64;
let percentage_diff = (this.percentage() - other.percentage()) as f64; let percentage_diff = (this.percentage() - other.percentage()) as f64;
Ok(length_diff * length_diff + percentage_diff * percentage_diff) Ok(length_diff * length_diff + percentage_diff * percentage_diff)
} else { } else {

View file

@ -800,8 +800,7 @@ ${helpers.single_keyword_system("font-variant-caps",
} }
SpecifiedValue::Length(LengthOrPercentage::Calc(ref calc)) => { SpecifiedValue::Length(LengthOrPercentage::Calc(ref calc)) => {
let calc = calc.to_computed_value(context); let calc = calc.to_computed_value(context);
calc.length() + base_size.resolve(context) calc.to_used_value(Some(base_size.resolve(context))).unwrap()
.scale_by(calc.percentage())
} }
SpecifiedValue::Keyword(ref key, fraction) => { SpecifiedValue::Keyword(ref key, fraction) => {
key.to_computed_value(context).scale_by(fraction) key.to_computed_value(context).scale_by(fraction)

View file

@ -132,7 +132,8 @@
let calc = calc.to_computed_value(context); let calc = calc.to_computed_value(context);
let fr = specified::FontRelativeLength::Em(calc.percentage()); let fr = specified::FontRelativeLength::Em(calc.percentage());
let fr = specified::Length::NoCalc(specified::NoCalcLength::FontRelative(fr)); let fr = specified::Length::NoCalc(specified::NoCalcLength::FontRelative(fr));
computed_value::T::Length(calc.length() + fr.to_computed_value(context)) let length = calc.unclamped_length();
computed_value::T::Length(calc.clamping_mode.clamp(length + fr.to_computed_value(context)))
} }
} }
} }

View file

@ -88,12 +88,21 @@ impl CalcLengthOrPercentage {
} }
} }
/// Returns this `calc()` as a `<length>`.
///
/// Panics in debug mode if a percentage is present in the expression.
#[inline] #[inline]
#[allow(missing_docs)]
pub fn length(&self) -> Au { pub fn length(&self) -> Au {
debug_assert!(self.percentage.is_none());
self.clamping_mode.clamp(self.length) self.clamping_mode.clamp(self.length)
} }
/// Returns the `<length>` component of this `calc()`, unclamped.
#[inline]
pub fn unclamped_length(&self) -> Au {
self.length
}
#[inline] #[inline]
#[allow(missing_docs)] #[allow(missing_docs)]
pub fn percentage(&self) -> CSSFloat { pub fn percentage(&self) -> CSSFloat {
@ -245,7 +254,7 @@ impl LengthOrPercentage {
match *self { match *self {
Length(l) => (l, NotNaN::new(0.0).unwrap()), Length(l) => (l, NotNaN::new(0.0).unwrap()),
Percentage(p) => (Au(0), NotNaN::new(p).unwrap()), Percentage(p) => (Au(0), NotNaN::new(p).unwrap()),
Calc(c) => (c.length(), NotNaN::new(c.percentage()).unwrap()), Calc(c) => (c.unclamped_length(), NotNaN::new(c.percentage()).unwrap()),
} }
} }

View file

@ -241,7 +241,7 @@ impl<S: Side> ToComputedValue for PositionComponent<S> {
}, },
ComputedLengthOrPercentage::Calc(calc) => { ComputedLengthOrPercentage::Calc(calc) => {
let p = 1. - calc.percentage.unwrap_or(0.); let p = 1. - calc.percentage.unwrap_or(0.);
ComputedLengthOrPercentage::Calc(CalcLengthOrPercentage::new(-calc.length(), Some(p))) ComputedLengthOrPercentage::Calc(CalcLengthOrPercentage::new(-calc.unclamped_length(), Some(p)))
}, },
} }
}, },

View file

@ -718,8 +718,7 @@ impl MaybeNew for ViewportConstraints {
Some(initial_viewport.$dimension.scale_by(value.0)), Some(initial_viewport.$dimension.scale_by(value.0)),
LengthOrPercentageOrAuto::Auto => None, LengthOrPercentageOrAuto::Auto => None,
LengthOrPercentageOrAuto::Calc(ref calc) => { LengthOrPercentageOrAuto::Calc(ref calc) => {
let calc = calc.to_computed_value(&context); calc.to_computed_value(&context).to_used_value(Some(initial_viewport.$dimension))
Some(initial_viewport.$dimension.scale_by(calc.percentage()) + calc.length())
} }
}, },
ViewportLength::ExtendToZoom => { ViewportLength::ExtendToZoom => {