use crate::parser::{Parse, ParserContext};
use crate::values::computed::motion::OffsetRotate as ComputedOffsetRotate;
use crate::values::computed::{Context, ToComputedValue};
use crate::values::generics::motion as generics;
use crate::values::specified::basic_shape::BasicShape;
use crate::values::specified::position::{HorizontalPosition, VerticalPosition};
use crate::values::specified::url::SpecifiedUrl;
use crate::values::specified::{Angle, Position};
use crate::Zero;
use cssparser::Parser;
use style_traits::{ParseError, StyleParseErrorKind};
pub type RayFunction = generics::GenericRayFunction<Angle, Position>;
pub type OffsetPathFunction =
generics::GenericOffsetPathFunction<BasicShape, RayFunction, SpecifiedUrl>;
pub type OffsetPath = generics::GenericOffsetPath<OffsetPathFunction>;
pub type OffsetPosition = generics::GenericOffsetPosition<HorizontalPosition, VerticalPosition>;
#[allow(missing_docs)]
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
Deserialize,
MallocSizeOf,
Parse,
PartialEq,
Serialize,
SpecifiedValueInfo,
ToAnimatedValue,
ToComputedValue,
ToCss,
ToResolvedValue,
ToShmem,
)]
#[repr(u8)]
pub enum CoordBox {
ContentBox,
PaddingBox,
BorderBox,
FillBox,
StrokeBox,
ViewBox,
}
impl CoordBox {
#[inline]
pub fn is_default(&self) -> bool {
matches!(*self, Self::BorderBox)
}
}
impl Parse for RayFunction {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
input.expect_function_matching("ray")?;
input.parse_nested_block(|i| Self::parse_function_arguments(context, i))
}
}
impl RayFunction {
fn parse_function_arguments<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
use crate::values::specified::PositionOrAuto;
let mut angle = None;
let mut size = None;
let mut contain = false;
let mut position = None;
loop {
if angle.is_none() {
angle = input.try_parse(|i| Angle::parse(context, i)).ok();
}
if size.is_none() {
size = input.try_parse(generics::RaySize::parse).ok();
if size.is_some() {
continue;
}
}
if !contain {
contain = input
.try_parse(|i| i.expect_ident_matching("contain"))
.is_ok();
if contain {
continue;
}
}
if position.is_none() {
if input.try_parse(|i| i.expect_ident_matching("at")).is_ok() {
let pos = Position::parse(context, input)?;
position = Some(PositionOrAuto::Position(pos));
}
if position.is_some() {
continue;
}
}
break;
}
if angle.is_none() {
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
}
Ok(RayFunction {
angle: angle.unwrap(),
size: size.unwrap_or(generics::RaySize::ClosestSide),
contain,
position: position.unwrap_or(PositionOrAuto::auto()),
})
}
}
impl Parse for OffsetPathFunction {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
use crate::values::specified::basic_shape::{AllowedBasicShapes, ShapeType};
if let Ok(ray) = input.try_parse(|i| RayFunction::parse(context, i)) {
return Ok(OffsetPathFunction::Ray(ray));
}
if static_prefs::pref!("layout.css.motion-path-url.enabled") {
if let Ok(url) = input.try_parse(|i| SpecifiedUrl::parse(context, i)) {
return Ok(OffsetPathFunction::Url(url));
}
}
BasicShape::parse(context, input, AllowedBasicShapes::ALL, ShapeType::Outline)
.map(OffsetPathFunction::Shape)
}
}
impl Parse for OffsetPath {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
if input.try_parse(|i| i.expect_ident_matching("none")).is_ok() {
return Ok(OffsetPath::none());
}
let mut path = None;
let mut coord_box = None;
loop {
if path.is_none() {
path = input
.try_parse(|i| OffsetPathFunction::parse(context, i))
.ok();
}
if coord_box.is_none() {
coord_box = input.try_parse(CoordBox::parse).ok();
if coord_box.is_some() {
continue;
}
}
break;
}
if let Some(p) = path {
return Ok(OffsetPath::OffsetPath {
path: Box::new(p),
coord_box: coord_box.unwrap_or(CoordBox::BorderBox),
});
}
match coord_box {
Some(c) => Ok(OffsetPath::CoordBox(c)),
None => Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)),
}
}
}
#[derive(
Clone, Copy, Debug, MallocSizeOf, Parse, PartialEq, SpecifiedValueInfo, ToCss, ToShmem,
)]
#[repr(u8)]
pub enum OffsetRotateDirection {
#[css(skip)]
None,
Auto,
Reverse,
}
impl OffsetRotateDirection {
#[inline]
fn is_none(&self) -> bool {
*self == OffsetRotateDirection::None
}
}
#[inline]
fn direction_specified_and_angle_is_zero(direction: &OffsetRotateDirection, angle: &Angle) -> bool {
!direction.is_none() && angle.is_zero()
}
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToCss, ToShmem)]
pub struct OffsetRotate {
#[css(skip_if = "OffsetRotateDirection::is_none")]
direction: OffsetRotateDirection,
#[css(contextual_skip_if = "direction_specified_and_angle_is_zero")]
angle: Angle,
}
impl OffsetRotate {
#[inline]
pub fn auto() -> Self {
OffsetRotate {
direction: OffsetRotateDirection::Auto,
angle: Angle::zero(),
}
}
#[inline]
pub fn is_auto(&self) -> bool {
self.direction == OffsetRotateDirection::Auto && self.angle.is_zero()
}
}
impl Parse for OffsetRotate {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let location = input.current_source_location();
let mut direction = input.try_parse(OffsetRotateDirection::parse);
let angle = input.try_parse(|i| Angle::parse(context, i));
if direction.is_err() {
direction = input.try_parse(OffsetRotateDirection::parse);
}
if direction.is_err() && angle.is_err() {
return Err(location.new_custom_error(StyleParseErrorKind::UnspecifiedError));
}
Ok(OffsetRotate {
direction: direction.unwrap_or(OffsetRotateDirection::None),
angle: angle.unwrap_or(Zero::zero()),
})
}
}
impl ToComputedValue for OffsetRotate {
type ComputedValue = ComputedOffsetRotate;
#[inline]
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
use crate::values::computed::Angle as ComputedAngle;
ComputedOffsetRotate {
auto: !self.direction.is_none(),
angle: if self.direction == OffsetRotateDirection::Reverse {
self.angle.to_computed_value(context) + ComputedAngle::from_degrees(180.0)
} else {
self.angle.to_computed_value(context)
},
}
}
#[inline]
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
OffsetRotate {
direction: if computed.auto {
OffsetRotateDirection::Auto
} else {
OffsetRotateDirection::None
},
angle: ToComputedValue::from_computed_value(&computed.angle),
}
}
}