takumi 0.73.0

Render your React components to images.
Documentation
use cssparser::Parser;
use std::borrow::Cow;
use taffy::{LengthPercentage, Point, Size};

use crate::{
  layout::style::{
    CssToken, FromCss, Length, LengthDefaultsToZero, MakeComputed, Overflow, ParseResult,
    merge_enum_values,
  },
  rendering::Sizing,
};

/// A pair of values for horizontal and vertical axes.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SpacePair<T: Copy> {
  /// The horizontal value.
  pub x: T,
  /// The vertical value.
  pub y: T,
}

impl<T: Copy + Default> Default for SpacePair<T> {
  fn default() -> Self {
    Self::from_single(T::default())
  }
}

impl<'i, T: Copy + FromCss<'i>> FromCss<'i> for SpacePair<T> {
  fn from_css(input: &mut Parser<'i, '_>) -> ParseResult<'i, Self> {
    let first = T::from_css(input)?;
    if let Ok(second) = T::from_css(input) {
      Ok(Self::from_pair(first, second))
    } else {
      Ok(Self::from_single(first))
    }
  }

  fn expect_message() -> Cow<'static, str> {
    Cow::Owned(format!(
      "1 ~ 2 values of {}",
      merge_enum_values(T::valid_tokens())
    ))
  }

  fn valid_tokens() -> &'static [CssToken] {
    T::valid_tokens()
  }
}

impl<T: Copy> SpacePair<T> {
  /// Create a new [`SpacePair`] from a single value.
  #[inline]
  pub const fn from_single(value: T) -> Self {
    Self::from_pair(value, value)
  }

  /// Create a new [`SpacePair`] from a pair of values.
  #[inline]
  pub const fn from_pair(x: T, y: T) -> Self {
    Self { x, y }
  }

  /// Create a new [`SpacePair`] for reversed properties like `gap: row column`.
  #[inline]
  pub const fn from_reversed_pair(y: T, x: T) -> Self {
    Self { x, y }
  }
}

impl<T: Copy + MakeComputed> MakeComputed for SpacePair<T> {
  fn make_computed(&mut self, sizing: &Sizing) {
    self.x.make_computed(sizing);
    self.y.make_computed(sizing);
  }
}

impl<const DEFAULT_AUTO: bool> SpacePair<Length<DEFAULT_AUTO>> {
  pub(crate) fn resolve_to_size(self, sizing: &Sizing) -> Size<LengthPercentage> {
    Size {
      width: self.x.resolve_to_length_percentage(sizing),
      height: self.y.resolve_to_length_percentage(sizing),
    }
  }
}

impl<T: Copy> From<SpacePair<T>> for Point<T> {
  fn from(value: SpacePair<T>) -> Self {
    Point {
      x: value.x,
      y: value.y,
    }
  }
}

impl SpacePair<Overflow> {
  pub(crate) fn should_clip_content(&self) -> bool {
    self.x != Overflow::Visible || self.y != Overflow::Visible
  }
}

/// A pair of values for horizontal and vertical border radii.
pub type BorderRadiusPair = SpacePair<LengthDefaultsToZero>;

impl BorderRadiusPair {
  pub(crate) fn to_px(self, sizing: &Sizing, border_box: Size<f32>) -> SpacePair<f32> {
    SpacePair::from_pair(
      self.x.to_px(sizing, border_box.width).max(0.0),
      self.y.to_px(sizing, border_box.height).max(0.0),
    )
  }
}