servo/components/style/values/computed/transform.rs
author Anthony Ramine <n.oxyde@gmail.com>
Wed, 31 May 2017 11:08:56 -0500
changeset 409705 46391a33a8f9efb4b29efd58087500ab63ca3fee
child 410538 0cf5121dfb27b707912e80851c93091dd00418ac
permissions -rw-r--r--
servo: Merge #17095 - Refactor a few CSS properties (from servo:derive-all-the-things); r=emilio Source-Repo: https://github.com/servo/servo Source-Revision: cd22fc6b902496d105f8003beaee5c8eb79e9669

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! Computed types for CSS values that are related to transformations.

use properties::animated_properties::Animatable;
use values::computed::{Length, LengthOrPercentage};
use values::generics::transform::TransformOrigin as GenericTransformOrigin;

/// The computed value of a CSS `<transform-origin>`
pub type TransformOrigin = GenericTransformOrigin<LengthOrPercentage, LengthOrPercentage, Length>;

impl TransformOrigin {
    /// Returns the initial computed value for `transform-origin`.
    #[inline]
    pub fn initial_value() -> Self {
        Self::new(
            LengthOrPercentage::Percentage(0.5),
            LengthOrPercentage::Percentage(0.5),
            Length::from_px(0)
        )
    }
}

impl Animatable for TransformOrigin {
    #[inline]
    fn add_weighted(&self, other: &Self, self_portion: f64, other_portion: f64) -> Result<Self, ()> {
        Ok(Self::new(
            self.horizontal.add_weighted(&other.horizontal, self_portion, other_portion)?,
            self.vertical.add_weighted(&other.vertical, self_portion, other_portion)?,
            self.depth.add_weighted(&other.depth, self_portion, other_portion)?,
        ))
    }

    #[inline]
    fn compute_distance(&self, other: &Self) -> Result<f64, ()> {
        self.compute_squared_distance(other).map(f64::sqrt)
    }

    #[inline]
    fn compute_squared_distance(&self, other: &Self) -> Result<f64, ()> {
        Ok(
            self.horizontal.compute_squared_distance(&other.horizontal)? +
            self.vertical.compute_squared_distance(&other.vertical)? +
            self.depth.compute_squared_distance(&other.depth)?
        )
    }
}