/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
//! `<length>` computed values, and related ones.
usesuper::{Number, ToComputedValue}; usecrate::derives::*; usecrate::logical_geometry::PhysicalSide; usecrate::typed_om::{NumericValue, ToTyped, TypedValue, UnitValue}; usecrate::values::animated::{Context as AnimatedContext, ToAnimatedValue}; usecrate::values::computed::position::TryTacticAdjustment; usecrate::values::computed::{NonNegativeNumber, Percentage, Zoom}; usecrate::values::generics::length::{
GenericLengthOrNumber, GenericLengthPercentageOrNormal, GenericMaxSize, GenericSize,
}; #[cfg(feature = "gecko")] usecrate::values::generics::position::TreeScoped; usecrate::values::generics::NonNegative; usecrate::values::generics::{length as generics, ClampToNonNegative}; usecrate::values::resolved::{Context as ResolvedContext, ToResolvedValue}; usecrate::values::CSSFloat; #[cfg(feature = "gecko")] usecrate::values::DashedIdent; usecrate::Zero; use app_units::Au; use std::fmt::{self, Write}; use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign}; use style_traits::{CSSPixel, CssString, CssWriter, ToCss}; use thin_vec::ThinVec;
/// Some boilerplate to share between negative and non-negative /// length-percentage or auto.
macro_rules! computed_length_percentage_or_auto {
($inner:ty) => { /// Returns the used value. #[inline] pubfn to_used_value(&self, percentage_basis: Au) -> Option<Au> { match *self { Self::Auto => None, Self::LengthPercentage(ref lp) => Some(lp.to_used_value(percentage_basis)),
}
}
};
}
/// A computed type for `<length-percentage> | auto`. pubtype LengthPercentageOrAuto = generics::GenericLengthPercentageOrAuto<LengthPercentage>;
impl LengthPercentageOrAuto { /// Clamps the value to a non-negative value. pubfn clamp_to_non_negative(self) -> Self { usecrate::values::generics::length::LengthPercentageOrAuto::*; matchself {
LengthPercentage(l) => LengthPercentage(l.clamp_to_non_negative()),
Auto => Auto,
}
}
/// Convert to have a borrow inside the enum pubfn as_ref(&self) -> generics::GenericLengthPercentageOrAuto<&LengthPercentage> { usecrate::values::generics::length::LengthPercentageOrAuto::*; match *self {
LengthPercentage(ref lp) => LengthPercentage(lp),
Auto => Auto,
}
}
/// A wrapper of LengthPercentageOrAuto, whose value must be >= 0. pubtype NonNegativeLengthPercentageOrAuto =
generics::GenericLengthPercentageOrAuto<NonNegativeLengthPercentage>;
impl CSSPixelLength { /// Return a new CSSPixelLength. #[inline] pubfn new(px: CSSFloat) -> Self {
CSSPixelLength(px)
}
/// Returns a normalized (NaN turned to zero) version of this length. #[inline] pubfn normalized(self) -> Self { Self::new(crate::values::normalize(self.0))
}
/// Returns a finite (normalized and clamped to float min and max) version of this length. #[inline] pubfn finite(self) -> Self { Self::new(crate::values::normalize(self.0).min(f32::MAX).max(f32::MIN))
}
/// Scale the length by a given amount. #[inline] pubfn scale_by(self, scale: CSSFloat) -> Self {
CSSPixelLength(self.0 * scale)
}
/// Return the length with app_unit i32 type. #[inline] pubfn to_i32_au(self) -> i32 {
Au::from(self).0
}
/// Return the absolute value of this length. #[inline] pubfn abs(self) -> Self {
CSSPixelLength::new(self.0.abs())
}
/// Return the clamped value of this length. #[inline] pubfn clamp_to_non_negative(self) -> Self {
CSSPixelLength::new(self.0.max(0.))
}
/// Returns the minimum between `self` and `other`. #[inline] pubfn min(self, other: Self) -> Self {
CSSPixelLength::new(self.0.min(other.0))
}
/// Returns the maximum between `self` and `other`. #[inline] pubfn max(self, other: Self) -> Self {
CSSPixelLength::new(self.0.max(other.0))
}
/// Sets `self` to the maximum between `self` and `other`. #[inline] pubfn max_assign(&mutself, other: Self) {
*self = self.max(other);
}
/// Clamp the value to a lower bound and an optional upper bound. /// /// Can be used for example with `min-width` and `max-width`. #[inline] pubfn clamp_between_extremums(self, min_size: Self, max_size: Option<Self>) -> Self { self.clamp_below_max(max_size).max(min_size)
}
/// Clamp the value to an optional upper bound. /// /// Can be used for example with `max-width`. #[inline] pubfn clamp_below_max(self, max_size: Option<Self>) -> Self { match max_size {
None => self,
Some(max_size) => self.min(max_size),
}
}
}
/// An alias of computed `<length>` value. pubtype Length = CSSPixelLength;
/// Either a computed `<length>` or the `auto` keyword. pubtype LengthOrAuto = generics::GenericLengthPercentageOrAuto<Length>;
/// Either a non-negative `<length>` or the `auto` keyword. pubtype NonNegativeLengthOrAuto = generics::GenericLengthPercentageOrAuto<NonNegativeLength>;
/// Either a computed `<length>` or a `<number>` value. pubtype LengthOrNumber = GenericLengthOrNumber<Length, Number>;
/// A wrapper of Length, whose value must be >= 0. pubtype NonNegativeLength = NonNegative<Length>;
impl From<NonNegativeLength> for Au { #[inline] fn from(non_negative_len: NonNegativeLength) -> Self {
Au::from(non_negative_len.0)
}
}
/// Either a computed NonNegativeLengthPercentage or the `normal` keyword. pubtype NonNegativeLengthPercentageOrNormal =
GenericLengthPercentageOrNormal<NonNegativeLengthPercentage>;
/// Either a non-negative `<length>` or a `<number>`. pubtype NonNegativeLengthOrNumber = GenericLengthOrNumber<NonNegativeLength, NonNegativeNumber>;
/// A computed value for `min-width`, `min-height`, `width` or `height` property. pubtype Size = GenericSize<NonNegativeLengthPercentage>;
/// A computed value for `max-width` or `max-height` property. pubtype MaxSize = GenericMaxSize<NonNegativeLengthPercentage>;
/// Resolve the anchor function with the given resolver. Returns `Err()` if no anchor is found. /// `prop_axis`, axis of the property (e.g. `margin-left` -> Horizontal axis), is used if the /// anchor size keyword is not specified. #[cfg(feature = "gecko")] pubfn resolve_anchor_size(
anchor_name: &TreeScoped<DashedIdent>,
prop_axis: PhysicalAxis,
anchor_size_keyword: AnchorSizeKeyword,
params: &AnchorPosResolutionParams,
) -> Result<Length, ()> { usecrate::gecko_bindings::structs::Gecko_GetAnchorPosSize;
letmut offset = Length::zero(); let valid = unsafe {
Gecko_GetAnchorPosSize(
params,
anchor_name.value.0.as_ptr(),
&anchor_name.scope,
prop_axis as u8,
anchor_size_keyword as u8,
&mut offset,
)
};
if !valid { return Err(());
}
Ok(offset)
}
/// A computed type for `margin` properties. pubtype Margin = generics::GenericMargin<LengthPercentage>;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.