mirror of
https://github.com/servo/servo.git
synced 2025-08-04 05:00:08 +01:00
Support addition of font-stretch
Although there are no specific tests for this yet if any code does try to perform addition or accumulation with a font-stretch value it will likely fail the bounds check in the Into trait. To avoid that this patch provides an basic implementation of add_weighted for font-stretch that should work correctly with portions whose sum is outside the [0.0, 1.0] range.
This commit is contained in:
parent
d45ee9d662
commit
4d187524c6
1 changed files with 11 additions and 6 deletions
|
@ -1373,11 +1373,16 @@ impl Animatable for FontWeight {
|
||||||
/// https://drafts.csswg.org/css-fonts/#font-stretch-prop
|
/// https://drafts.csswg.org/css-fonts/#font-stretch-prop
|
||||||
impl Animatable for FontStretch {
|
impl Animatable for FontStretch {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn add_weighted(&self, other: &Self, self_portion: f64, other_portion: f64) -> Result<Self, ()> {
|
fn add_weighted(&self, other: &Self, self_portion: f64, other_portion: f64)
|
||||||
|
-> Result<Self, ()>
|
||||||
|
{
|
||||||
let from = f64::from(*self);
|
let from = f64::from(*self);
|
||||||
let to = f64::from(*other);
|
let to = f64::from(*other);
|
||||||
let interpolated_mapped_index = ((from * self_portion + to * other_portion) + 0.5).floor();
|
// FIXME: When `const fn` is available in release rust, make |normal|, below, const.
|
||||||
Ok(interpolated_mapped_index.into())
|
let normal = f64::from(FontStretch::normal);
|
||||||
|
let result = (from - normal) * self_portion + (to - normal) * other_portion + normal;
|
||||||
|
|
||||||
|
Ok(result.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -1410,11 +1415,11 @@ impl From<FontStretch> for f64 {
|
||||||
impl Into<FontStretch> for f64 {
|
impl Into<FontStretch> for f64 {
|
||||||
fn into(self) -> FontStretch {
|
fn into(self) -> FontStretch {
|
||||||
use properties::longhands::font_stretch::computed_value::T::*;
|
use properties::longhands::font_stretch::computed_value::T::*;
|
||||||
debug_assert!(self >= 1.0 && self <= 9.0);
|
let index = (self + 0.5).floor().min(9.0).max(1.0);
|
||||||
static FONT_STRETCH_ENUM_MAP: [FontStretch; 9] =
|
static FONT_STRETCH_ENUM_MAP: [FontStretch; 9] =
|
||||||
[ ultra_condensed, extra_condensed, condensed, semi_condensed, normal,
|
[ ultra_condensed, extra_condensed, condensed, semi_condensed, normal,
|
||||||
semi_expanded, expanded, extra_expanded, ultra_expanded ];
|
semi_expanded, expanded, extra_expanded, ultra_expanded ];
|
||||||
FONT_STRETCH_ENUM_MAP[(self - 1.0) as usize]
|
FONT_STRETCH_ENUM_MAP[(index - 1.0) as usize]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue