1use super::{Number, ToComputedValue};
8use crate::Zero;
9use crate::derives::*;
10use crate::logical_geometry::PhysicalSide;
11use crate::typed_om::{NumericType, NumericValue, ToTyped, TypedValue, UnitValue};
12use crate::values::CSSFloat;
13#[cfg(feature = "gecko")]
14use crate::values::DashedIdent;
15use crate::values::animated::{Context as AnimatedContext, ToAnimatedValue};
16use crate::values::computed::position::TryTacticAdjustment;
17use crate::values::computed::{NonNegativeNumber, Percentage, Zoom};
18use crate::values::generics::NonNegative;
19use crate::values::generics::length::{
20 GenericFitTolerance, GenericLengthOrNumber, GenericLengthPercentageOrNormal, GenericMaxSize,
21 GenericSize,
22};
23#[cfg(feature = "gecko")]
24use crate::values::generics::position::TreeScoped;
25use crate::values::generics::{ClampToNonNegative, length as generics};
26use crate::values::resolved::{Context as ResolvedContext, ToResolvedValue};
27use app_units::Au;
28use std::fmt::{self, Write};
29use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign};
30use style_traits::{CSSPixel, CssString, CssWriter, ToCss};
31use thin_vec::ThinVec;
32
33pub use super::image::Image;
34pub use super::length_percentage::{LengthPercentage, NonNegativeLengthPercentage};
35pub use crate::values::specified::url::UrlOrNone;
36pub use crate::values::specified::{Angle, BorderStyle, NoCalcPercentage, Time};
37
38macro_rules! computed_length_percentage_or_auto {
41 ($inner:ty) => {
42 #[inline]
44 pub fn to_used_value(&self, percentage_basis: Au) -> Option<Au> {
45 match *self {
46 Self::Auto => None,
47 Self::LengthPercentage(ref lp) => Some(lp.to_used_value(percentage_basis)),
48 }
49 }
50 };
51}
52
53pub type LengthPercentageOrAuto = generics::GenericLengthPercentageOrAuto<LengthPercentage>;
55
56impl LengthPercentageOrAuto {
57 pub fn clamp_to_non_negative(self) -> Self {
59 use crate::values::generics::length::LengthPercentageOrAuto::*;
60 match self {
61 LengthPercentage(l) => LengthPercentage(l.clamp_to_non_negative()),
62 Auto => Auto,
63 }
64 }
65
66 pub fn as_ref(&self) -> generics::GenericLengthPercentageOrAuto<&LengthPercentage> {
68 use crate::values::generics::length::LengthPercentageOrAuto::*;
69 match *self {
70 LengthPercentage(ref lp) => LengthPercentage(lp),
71 Auto => Auto,
72 }
73 }
74
75 computed_length_percentage_or_auto!(LengthPercentage);
76}
77
78impl generics::GenericLengthPercentageOrAuto<&LengthPercentage> {
79 #[inline]
81 pub fn percentage_relative_to(&self, basis: Length) -> LengthOrAuto {
82 use crate::values::generics::length::LengthPercentageOrAuto::*;
83 match self {
84 LengthPercentage(length_percentage) => {
85 LengthPercentage(length_percentage.percentage_relative_to(basis))
86 },
87 Auto => Auto,
88 }
89 }
90
91 #[inline]
93 pub fn maybe_percentage_relative_to(&self, basis: Option<Length>) -> LengthOrAuto {
94 use crate::values::generics::length::LengthPercentageOrAuto::*;
95 match self {
96 LengthPercentage(length_percentage) => length_percentage
97 .maybe_percentage_relative_to(basis)
98 .map_or(Auto, LengthPercentage),
99 Auto => Auto,
100 }
101 }
102}
103
104pub type NonNegativeLengthPercentageOrAuto =
106 generics::GenericLengthPercentageOrAuto<NonNegativeLengthPercentage>;
107
108impl NonNegativeLengthPercentageOrAuto {
109 computed_length_percentage_or_auto!(NonNegativeLengthPercentage);
110}
111
112#[derive(
114 Animate,
115 Clone,
116 ComputeSquaredDistance,
117 Copy,
118 Deserialize,
119 MallocSizeOf,
120 PartialEq,
121 PartialOrd,
122 Serialize,
123 ToAnimatedZero,
124 ToComputedValue,
125 ToShmem,
126)]
127#[repr(C)]
128pub struct CSSPixelLength(CSSFloat);
129
130impl ToResolvedValue for CSSPixelLength {
131 type ResolvedValue = Self;
132
133 fn to_resolved_value(self, context: &ResolvedContext) -> Self::ResolvedValue {
134 Self(context.style.effective_zoom.unzoom(self.0))
135 }
136
137 #[inline]
138 fn from_resolved_value(value: Self::ResolvedValue) -> Self {
139 value
140 }
141}
142
143impl ToAnimatedValue for CSSPixelLength {
144 type AnimatedValue = Self;
145
146 fn to_animated_value(self, context: &AnimatedContext) -> Self::AnimatedValue {
147 Self(context.style.effective_zoom.unzoom(self.0))
148 }
149
150 #[inline]
151 fn from_animated_value(value: Self::AnimatedValue) -> Self {
152 value
153 }
154}
155
156impl fmt::Debug for CSSPixelLength {
157 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
158 self.0.fmt(f)?;
159 f.write_str(" px")
160 }
161}
162
163impl CSSPixelLength {
164 #[inline]
166 pub fn new(px: CSSFloat) -> Self {
167 CSSPixelLength(px)
168 }
169
170 #[inline]
172 pub fn normalized(self) -> Self {
173 Self::new(crate::values::normalize(self.0))
174 }
175
176 #[inline]
178 pub fn finite(self) -> Self {
179 Self::new(crate::values::normalize(self.0).min(f32::MAX).max(f32::MIN))
180 }
181
182 #[inline]
184 pub fn scale_by(self, scale: CSSFloat) -> Self {
185 CSSPixelLength(self.0 * scale)
186 }
187
188 #[inline]
190 pub fn px(self) -> CSSFloat {
191 self.0
192 }
193
194 #[inline]
196 pub fn zoom(self, zoom: Zoom) -> Self {
197 Self::new(zoom.zoom(self.px()))
198 }
199
200 #[inline]
202 pub fn to_i32_au(self) -> i32 {
203 Au::from(self).0
204 }
205
206 #[inline]
208 pub fn abs(self) -> Self {
209 CSSPixelLength::new(self.0.abs())
210 }
211
212 #[inline]
214 pub fn clamp_to_non_negative(self) -> Self {
215 CSSPixelLength::new(self.0.max(0.))
216 }
217
218 #[inline]
220 pub fn min(self, other: Self) -> Self {
221 CSSPixelLength::new(self.0.min(other.0))
222 }
223
224 #[inline]
226 pub fn max(self, other: Self) -> Self {
227 CSSPixelLength::new(self.0.max(other.0))
228 }
229
230 #[inline]
232 pub fn max_assign(&mut self, other: Self) {
233 *self = self.max(other);
234 }
235
236 #[inline]
240 pub fn clamp_between_extremums(self, min_size: Self, max_size: Option<Self>) -> Self {
241 self.clamp_below_max(max_size).max(min_size)
242 }
243
244 #[inline]
248 pub fn clamp_below_max(self, max_size: Option<Self>) -> Self {
249 match max_size {
250 None => self,
251 Some(max_size) => self.min(max_size),
252 }
253 }
254}
255
256impl num_traits::Zero for CSSPixelLength {
257 fn zero() -> Self {
258 CSSPixelLength::new(0.)
259 }
260
261 fn is_zero(&self) -> bool {
262 self.px() == 0.
263 }
264}
265
266impl ToCss for CSSPixelLength {
267 #[inline]
268 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
269 where
270 W: Write,
271 {
272 self.0.to_css(dest)?;
273 dest.write_str("px")
274 }
275}
276
277impl ToTyped for CSSPixelLength {
278 fn to_typed(&self, dest: &mut ThinVec<TypedValue>) -> Result<(), ()> {
279 dest.push(TypedValue::Numeric(NumericValue::Unit(UnitValue {
280 numeric_type: NumericType::length(),
281 value: self.0,
282 unit: CssString::from("px"),
283 })));
284 Ok(())
285 }
286}
287
288impl std::iter::Sum for CSSPixelLength {
289 fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
290 iter.fold(Length::zero(), Add::add)
291 }
292}
293
294impl Add for CSSPixelLength {
295 type Output = Self;
296
297 #[inline]
298 fn add(self, other: Self) -> Self {
299 Self::new(self.px() + other.px())
300 }
301}
302
303impl AddAssign for CSSPixelLength {
304 #[inline]
305 fn add_assign(&mut self, other: Self) {
306 self.0 += other.0;
307 }
308}
309
310impl Div for CSSPixelLength {
311 type Output = CSSFloat;
312
313 #[inline]
314 fn div(self, other: Self) -> CSSFloat {
315 self.px() / other.px()
316 }
317}
318
319impl Div<CSSFloat> for CSSPixelLength {
320 type Output = Self;
321
322 #[inline]
323 fn div(self, other: CSSFloat) -> Self {
324 Self::new(self.px() / other)
325 }
326}
327
328impl MulAssign<CSSFloat> for CSSPixelLength {
329 #[inline]
330 fn mul_assign(&mut self, other: CSSFloat) {
331 self.0 *= other;
332 }
333}
334
335impl Mul<CSSFloat> for CSSPixelLength {
336 type Output = Self;
337
338 #[inline]
339 fn mul(self, other: CSSFloat) -> Self {
340 Self::new(self.px() * other)
341 }
342}
343
344impl Neg for CSSPixelLength {
345 type Output = Self;
346
347 #[inline]
348 fn neg(self) -> Self {
349 CSSPixelLength::new(-self.0)
350 }
351}
352
353impl Sub for CSSPixelLength {
354 type Output = Self;
355
356 #[inline]
357 fn sub(self, other: Self) -> Self {
358 Self::new(self.px() - other.px())
359 }
360}
361
362impl SubAssign for CSSPixelLength {
363 #[inline]
364 fn sub_assign(&mut self, other: Self) {
365 self.0 -= other.0;
366 }
367}
368
369impl From<CSSPixelLength> for Au {
370 #[inline]
371 fn from(len: CSSPixelLength) -> Self {
372 Au::from_f32_px(len.0)
373 }
374}
375
376impl From<Au> for CSSPixelLength {
377 #[inline]
378 fn from(len: Au) -> Self {
379 CSSPixelLength::new(len.to_f32_px())
380 }
381}
382
383impl From<CSSPixelLength> for euclid::Length<CSSFloat, CSSPixel> {
384 #[inline]
385 fn from(length: CSSPixelLength) -> Self {
386 Self::new(length.0)
387 }
388}
389
390pub type Length = CSSPixelLength;
392
393pub type LengthOrAuto = generics::GenericLengthPercentageOrAuto<Length>;
395
396pub type NonNegativeLengthOrAuto = generics::GenericLengthPercentageOrAuto<NonNegativeLength>;
398
399pub type LengthOrNumber = GenericLengthOrNumber<Length, Number>;
401
402pub type NonNegativeLength = NonNegative<Length>;
404
405impl ClampToNonNegative for Length {
406 fn clamp_to_non_negative(self) -> Self {
407 Self::new(self.px().max(0.))
408 }
409}
410
411impl NonNegativeLength {
412 #[inline]
414 pub fn new(px: CSSFloat) -> Self {
415 NonNegative(Length::new(px.max(0.)))
416 }
417
418 #[inline]
420 pub fn px(&self) -> CSSFloat {
421 self.0.px()
422 }
423
424 #[inline]
425 pub fn clamp(self) -> Self {
427 if (self.0).0 < 0. { Self::zero() } else { self }
428 }
429}
430
431impl From<Length> for NonNegativeLength {
432 #[inline]
433 fn from(len: Length) -> Self {
434 NonNegative(len)
435 }
436}
437
438impl From<Au> for NonNegativeLength {
439 #[inline]
440 fn from(au: Au) -> Self {
441 NonNegative(au.into())
442 }
443}
444
445impl From<NonNegativeLength> for Au {
446 #[inline]
447 fn from(non_negative_len: NonNegativeLength) -> Self {
448 Au::from(non_negative_len.0)
449 }
450}
451
452pub type NonNegativeLengthPercentageOrNormal =
454 GenericLengthPercentageOrNormal<NonNegativeLengthPercentage>;
455
456pub type FitTolerance = GenericFitTolerance<NonNegativeLengthPercentage>;
458
459pub type NonNegativeLengthOrNumber = GenericLengthOrNumber<NonNegativeLength, NonNegativeNumber>;
461
462pub type Size = GenericSize<NonNegativeLengthPercentage>;
464
465pub type MaxSize = GenericMaxSize<NonNegativeLengthPercentage>;
467
468#[cfg(feature = "gecko")]
469use crate::{
470 gecko_bindings::structs::AnchorPosResolutionParams, logical_geometry::PhysicalAxis,
471 values::generics::length::AnchorSizeKeyword,
472};
473
474#[cfg(feature = "gecko")]
478pub fn resolve_anchor_size(
479 anchor_name: &TreeScoped<DashedIdent>,
480 prop_axis: PhysicalAxis,
481 anchor_size_keyword: AnchorSizeKeyword,
482 params: &AnchorPosResolutionParams,
483) -> Result<Length, ()> {
484 use crate::gecko_bindings::structs::Gecko_GetAnchorPosSize;
485
486 let mut offset = Length::zero();
487 let valid = unsafe {
488 Gecko_GetAnchorPosSize(
489 params,
490 anchor_name.value.0.as_ptr(),
491 &anchor_name.scope,
492 prop_axis as u8,
493 anchor_size_keyword as u8,
494 &mut offset,
495 )
496 };
497
498 if !valid {
499 return Err(());
500 }
501
502 Ok(offset)
503}
504
505pub type Margin = generics::GenericMargin<LengthPercentage>;
507
508impl TryTacticAdjustment for MaxSize {
509 fn try_tactic_adjustment(&mut self, old_side: PhysicalSide, new_side: PhysicalSide) {
510 debug_assert!(
511 old_side.orthogonal_to(new_side),
512 "Sizes should only change axes"
513 );
514 match self {
515 Self::FitContentFunction(lp)
516 | Self::LengthPercentage(lp)
517 | Self::AnchorContainingCalcFunction(lp) => {
518 lp.try_tactic_adjustment(old_side, new_side);
519 },
520 Self::AnchorSizeFunction(s) => s.try_tactic_adjustment(old_side, new_side),
521 Self::None
522 | Self::MaxContent
523 | Self::MinContent
524 | Self::FitContent
525 | Self::WebkitFillAvailable
526 | Self::Stretch => {},
527 #[cfg(feature = "gecko")]
528 Self::MozAvailable => {},
529 }
530 }
531}
532
533impl TryTacticAdjustment for Size {
534 fn try_tactic_adjustment(&mut self, old_side: PhysicalSide, new_side: PhysicalSide) {
535 debug_assert!(
536 old_side.orthogonal_to(new_side),
537 "Sizes should only change axes"
538 );
539 match self {
540 Self::FitContentFunction(lp)
541 | Self::LengthPercentage(lp)
542 | Self::AnchorContainingCalcFunction(lp) => {
543 lp.try_tactic_adjustment(old_side, new_side);
544 },
545 Self::AnchorSizeFunction(s) => s.try_tactic_adjustment(old_side, new_side),
546 Self::Auto
547 | Self::MaxContent
548 | Self::MinContent
549 | Self::FitContent
550 | Self::WebkitFillAvailable
551 | Self::Stretch => {},
552 #[cfg(feature = "gecko")]
553 Self::MozAvailable => {},
554 }
555 }
556}
557
558impl TryTacticAdjustment for NoCalcPercentage {
559 fn try_tactic_adjustment(&mut self, old_side: PhysicalSide, new_side: PhysicalSide) {
560 let mut percentage = Percentage(self.get());
561 percentage.try_tactic_adjustment(old_side, new_side);
562 *self = NoCalcPercentage::new(percentage.0);
563 }
564}
565
566impl TryTacticAdjustment for Percentage {
567 fn try_tactic_adjustment(&mut self, old_side: PhysicalSide, new_side: PhysicalSide) {
568 if old_side.parallel_to(new_side) {
569 self.0 = 1.0 - self.0;
570 }
571 }
572}
573
574impl TryTacticAdjustment for Margin {
575 fn try_tactic_adjustment(&mut self, old_side: PhysicalSide, new_side: PhysicalSide) {
576 match self {
577 Self::Auto => {},
578 Self::LengthPercentage(lp) | Self::AnchorContainingCalcFunction(lp) => {
579 lp.try_tactic_adjustment(old_side, new_side)
580 },
581 Self::AnchorSizeFunction(anchor) => anchor.try_tactic_adjustment(old_side, new_side),
582 }
583 }
584}