Skip to content

Commit

Permalink
Add path parsing to BasicShape
Browse files Browse the repository at this point in the history
  • Loading branch information
noahbald committed Oct 7, 2024
1 parent eb49015 commit 93c7f7e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
27 changes: 19 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const-str = "0.3.1"
pathdiff = "0.2.1"
ahash = "0.8.7"
paste = "1.0.12"
oxvg_path = "0.0.1-beta.2"
# CLI deps
atty = { version = "0.2", optional = true }
clap = { version = "3.0.6", features = ["derive"], optional = true }
Expand Down
39 changes: 39 additions & 0 deletions src/values/shape.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! CSS shape values for masking and clipping.
use std::fmt::Write;

use super::length::LengthPercentage;
use super::position::Position;
use super::rect::Rect;
Expand All @@ -11,6 +13,7 @@ use crate::traits::{Parse, ToCss};
#[cfg(feature = "visitor")]
use crate::visitor::Visit;
use cssparser::*;
use oxvg_path;

/// A CSS [`<basic-shape>`](https://www.w3.org/TR/css-shapes-1/#basic-shape-functions) value.
#[derive(Debug, Clone, PartialEq)]
Expand All @@ -31,6 +34,8 @@ pub enum BasicShape {
Ellipse(Ellipse),
/// A polygon.
Polygon(Polygon),
/// A path
Path(Path),
}

/// An [`inset()`](https://www.w3.org/TR/css-shapes-1/#funcdef-inset) rectangle shape.
Expand Down Expand Up @@ -124,6 +129,15 @@ pub struct Point {
y: LengthPercentage,
}

/// A path within a `path()` shape.
///
/// See [Path](oxvg_path::Path).
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub struct Path(pub oxvg_path::Path);

enum_property! {
/// A [`<fill-rule>`](https://www.w3.org/TR/css-shapes-1/#typedef-fill-rule) used to
/// determine the interior of a `polygon()` shape.
Expand Down Expand Up @@ -152,6 +166,7 @@ impl<'i> Parse<'i> for BasicShape {
"circle" => Ok(BasicShape::Circle(input.parse_nested_block(Circle::parse)?)),
"ellipse" => Ok(BasicShape::Ellipse(input.parse_nested_block(Ellipse::parse)?)),
"polygon" => Ok(BasicShape::Polygon(input.parse_nested_block(Polygon::parse)?)),
"path" => Ok(BasicShape::Path(input.parse_nested_block(Path::parse)?)),
_ => Err(location.new_unexpected_token_error(Token::Ident(f.clone()))),
}
}
Expand Down Expand Up @@ -233,6 +248,16 @@ impl<'i> Parse<'i> for Point {
}
}

impl<'i> Parse<'i> for Path {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let string = input.expect_string()?.to_string();
match oxvg_path::Path::parse(string) {
Ok(path) => Ok(Path(path)),
Err(_) => Err(input.new_custom_error(ParserError::InvalidValue)),
}
}
}

impl ToCss for BasicShape {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
Expand All @@ -259,6 +284,11 @@ impl ToCss for BasicShape {
poly.to_css(dest)?;
dest.write_char(')')
}
BasicShape::Path(path) => {
dest.write_str("polygon(")?;
path.to_css(dest)?;
dest.write_char(')')
}
}
}
}
Expand Down Expand Up @@ -359,3 +389,12 @@ impl ToCss for Point {
self.y.to_css(dest)
}
}

impl ToCss for Path {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
Ok(write!(dest, "{}", self.0)?)
}
}

0 comments on commit 93c7f7e

Please sign in to comment.