rspack_lightningcss/values/
shape.rs1use super::length::LengthPercentage;
4use super::position::Position;
5use super::rect::Rect;
6use crate::error::{ParserError, PrinterError};
7use crate::macros::enum_property;
8use crate::properties::border_radius::BorderRadius;
9use crate::traits::{Parse, ToCss};
10use crate::values::number::NonNegative;
11#[cfg(feature = "visitor")]
12use crate::visitor::Visit;
13use cssparser::*;
14
15#[derive(Debug, Clone, PartialEq)]
17#[cfg_attr(feature = "visitor", derive(Visit))]
18#[cfg_attr(
19 feature = "serde",
20 derive(serde::Serialize, serde::Deserialize),
21 serde(tag = "type", content = "value", rename_all = "kebab-case")
22)]
23#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
24#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
25pub enum BasicShape {
26 Inset(InsetRect),
28 Circle(Circle),
30 Ellipse(Ellipse),
32 Polygon(Polygon),
34}
35
36#[derive(Debug, Clone, PartialEq)]
38#[cfg_attr(feature = "visitor", derive(Visit))]
39#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
40#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
41pub struct InsetRect {
42 pub rect: Rect<LengthPercentage>,
44 pub radius: BorderRadius,
46}
47
48#[derive(Debug, Clone, PartialEq)]
50#[cfg_attr(feature = "visitor", derive(Visit))]
51#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
52#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
53pub struct Circle {
54 pub radius: ShapeRadius,
56 pub position: Position,
58}
59
60#[derive(Debug, Clone, PartialEq, Parse, ToCss)]
63#[cfg_attr(feature = "visitor", derive(Visit))]
64#[cfg_attr(
65 feature = "serde",
66 derive(serde::Serialize, serde::Deserialize),
67 serde(tag = "type", content = "value", rename_all = "kebab-case")
68)]
69#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
70pub enum ShapeRadius {
71 LengthPercentage(NonNegative<LengthPercentage>),
73 ClosestSide,
75 FarthestSide,
77}
78
79#[derive(Debug, Clone, PartialEq)]
81#[cfg_attr(feature = "visitor", derive(Visit))]
82#[cfg_attr(
83 feature = "serde",
84 derive(serde::Serialize, serde::Deserialize),
85 serde(rename_all = "camelCase")
86)]
87#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
88pub struct Ellipse {
89 pub radius_x: ShapeRadius,
91 pub radius_y: ShapeRadius,
93 pub position: Position,
95}
96
97#[derive(Debug, Clone, PartialEq)]
99#[cfg_attr(feature = "visitor", derive(Visit))]
100#[cfg_attr(
101 feature = "serde",
102 derive(serde::Serialize, serde::Deserialize),
103 serde(rename_all = "camelCase")
104)]
105#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
106pub struct Polygon {
107 pub fill_rule: FillRule,
109 pub points: Vec<Point>,
111}
112
113#[derive(Debug, Clone, PartialEq)]
117#[cfg_attr(feature = "visitor", derive(Visit))]
118#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
119#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
120pub struct Point {
121 x: LengthPercentage,
123 y: LengthPercentage,
125}
126
127enum_property! {
128 pub enum FillRule {
133 Nonzero,
135 Evenodd,
137 }
138}
139
140impl Default for FillRule {
141 fn default() -> FillRule {
142 FillRule::Nonzero
143 }
144}
145
146impl<'i> Parse<'i> for BasicShape {
147 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
148 let location = input.current_source_location();
149 let f = input.expect_function()?;
150 match_ignore_ascii_case! { &f,
151 "inset" => Ok(BasicShape::Inset(input.parse_nested_block(InsetRect::parse)?)),
152 "circle" => Ok(BasicShape::Circle(input.parse_nested_block(Circle::parse)?)),
153 "ellipse" => Ok(BasicShape::Ellipse(input.parse_nested_block(Ellipse::parse)?)),
154 "polygon" => Ok(BasicShape::Polygon(input.parse_nested_block(Polygon::parse)?)),
155 _ => Err(location.new_unexpected_token_error(Token::Ident(f.clone()))),
156 }
157 }
158}
159
160impl<'i> Parse<'i> for InsetRect {
161 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
162 let rect = Rect::parse(input)?;
163 let radius = if input.try_parse(|input| input.expect_ident_matching("round")).is_ok() {
164 BorderRadius::parse(input)?
165 } else {
166 BorderRadius::default()
167 };
168 Ok(InsetRect { rect, radius })
169 }
170}
171
172impl<'i> Parse<'i> for Circle {
173 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
174 let radius = input.try_parse(ShapeRadius::parse).unwrap_or_default();
175 let position = if input.try_parse(|input| input.expect_ident_matching("at")).is_ok() {
176 Position::parse(input)?
177 } else {
178 Position::center()
179 };
180
181 Ok(Circle { radius, position })
182 }
183}
184
185impl Default for ShapeRadius {
186 fn default() -> ShapeRadius {
187 ShapeRadius::ClosestSide
188 }
189}
190
191impl<'i> Parse<'i> for Ellipse {
192 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
193 let (x, y) = input
194 .try_parse(|input| -> Result<_, ParseError<'i, ParserError<'i>>> {
195 Ok((ShapeRadius::parse(input)?, ShapeRadius::parse(input)?))
196 })
197 .unwrap_or_default();
198
199 let position = if input.try_parse(|input| input.expect_ident_matching("at")).is_ok() {
200 Position::parse(input)?
201 } else {
202 Position::center()
203 };
204
205 Ok(Ellipse {
206 radius_x: x,
207 radius_y: y,
208 position,
209 })
210 }
211}
212
213impl<'i> Parse<'i> for Polygon {
214 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
215 let fill_rule = input.try_parse(FillRule::parse);
216 if fill_rule.is_ok() {
217 input.expect_comma()?;
218 }
219
220 let points = input.parse_comma_separated(Point::parse)?;
221 Ok(Polygon {
222 fill_rule: fill_rule.unwrap_or_default(),
223 points,
224 })
225 }
226}
227
228impl<'i> Parse<'i> for Point {
229 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
230 let x = LengthPercentage::parse(input)?;
231 let y = LengthPercentage::parse(input)?;
232 Ok(Point { x, y })
233 }
234}
235
236impl ToCss for BasicShape {
237 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
238 match self {
239 BasicShape::Inset(rect) => {
240 dest.write_str("inset(")?;
241 rect.to_css(dest)?;
242 dest.write_char(')')?;
243 Ok(())
244 }
245 BasicShape::Circle(circle) => {
246 dest.write_str("circle(")?;
247 circle.to_css(dest)?;
248 dest.write_char(')')?;
249 Ok(())
250 }
251 BasicShape::Ellipse(ellipse) => {
252 dest.write_str("ellipse(")?;
253 ellipse.to_css(dest)?;
254 dest.write_char(')')?;
255 Ok(())
256 }
257 BasicShape::Polygon(poly) => {
258 dest.write_str("polygon(")?;
259 poly.to_css(dest)?;
260 dest.write_char(')')?;
261 Ok(())
262 }
263 }
264 }
265}
266
267impl ToCss for InsetRect {
268 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
269 self.rect.to_css(dest)?;
270 if self.radius != BorderRadius::default() {
271 dest.write_str(" round ")?;
272 self.radius.to_css(dest)?;
273 }
274 Ok(())
275 }
276}
277
278impl ToCss for Circle {
279 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
280 let mut has_output = false;
281 if self.radius != ShapeRadius::default() {
282 self.radius.to_css(dest)?;
283 has_output = true;
284 }
285
286 if !self.position.is_center() {
287 if has_output {
288 dest.write_char(' ')?;
289 }
290 dest.write_str("at ")?;
291 self.position.to_css(dest)?;
292 }
293
294 Ok(())
295 }
296}
297
298impl ToCss for Ellipse {
299 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
300 let mut has_output = false;
301 if self.radius_x != ShapeRadius::default() || self.radius_y != ShapeRadius::default() {
302 self.radius_x.to_css(dest)?;
303 dest.write_char(' ')?;
304 self.radius_y.to_css(dest)?;
305 has_output = true;
306 }
307
308 if !self.position.is_center() {
309 if has_output {
310 dest.write_char(' ')?;
311 }
312 dest.write_str("at ")?;
313 self.position.to_css(dest)?;
314 }
315
316 Ok(())
317 }
318}
319
320impl ToCss for Polygon {
321 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
322 if self.fill_rule != FillRule::default() {
323 self.fill_rule.to_css(dest)?;
324 dest.delim(',', false)?;
325 }
326
327 let mut first = true;
328 for point in &self.points {
329 if first {
330 first = false;
331 } else {
332 dest.delim(',', false)?;
333 }
334 point.to_css(dest)?;
335 }
336
337 Ok(())
338 }
339}
340
341impl ToCss for Point {
342 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
343 self.x.to_css(dest)?;
344 dest.write_char(' ')?;
345 self.y.to_css(dest)
346 }
347}