diff --git a/Cargo.toml b/Cargo.toml index 66e42e5..dadc2c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rasterize" -version = "0.3.10" +version = "0.3.11" authors = ["Pavel Aslanov "] description = "Simple and small 2D rendering library" edition = "2021" diff --git a/src/color.rs b/src/color.rs index 8e6d5b1..cd71658 100644 --- a/src/color.rs +++ b/src/color.rs @@ -2,6 +2,7 @@ use crate::{simd::f32x4, Paint, Point, Scalar, Transform, Units}; use bytemuck::{Pod, Zeroable}; #[cfg(feature = "serde")] use serde::{de::DeserializeSeed, Deserializer}; +use serde::{Deserialize, Serialize}; use std::{ collections::HashMap, fmt, @@ -224,12 +225,35 @@ impl FromStr for RGBA { } } +#[cfg(feature = "serde")] +impl Serialize for RGBA { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.collect_str(self) + } +} + #[cfg(feature = "serde")] #[derive(Clone)] pub struct RGBADeserializer<'a> { pub colors: &'a HashMap, } +#[cfg(feature = "serde")] +impl<'de> Deserialize<'de> for RGBA { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + RGBADeserializer { + colors: &SVG_COLORS, + } + .deserialize(deserializer) + } +} + #[cfg(feature = "serde")] impl<'de, 'a> DeserializeSeed<'de> for RGBADeserializer<'a> { type Value = RGBA; @@ -238,7 +262,6 @@ impl<'de, 'a> DeserializeSeed<'de> for RGBADeserializer<'a> { where D: Deserializer<'de>, { - use serde::Deserialize; let color = std::borrow::Cow::<'de, str>::deserialize(deserializer)?; RGBA::from_str_named(color.as_ref(), self.colors).map_err(serde::de::Error::custom) } diff --git a/src/svg.rs b/src/svg.rs index e296973..c9ec5b4 100644 --- a/src/svg.rs +++ b/src/svg.rs @@ -307,12 +307,15 @@ impl SvgPathParser { Some(op) => op, }; let cmd = match op { + // move b'M' | b'm' => { let dst = self.parse_point()?; self.subpath_start = dst; SvgPathCmd::MoveTo(dst) } + // line b'L' | b'l' => SvgPathCmd::LineTo(self.parse_point()?), + // vertical line b'V' | b'v' => { let y = self.parser.parse_scalar()?; let p0 = self.position; @@ -323,6 +326,7 @@ impl SvgPathParser { }; SvgPathCmd::LineTo(p1) } + // horizontal line b'H' | b'h' => { let x = self.parser.parse_scalar()?; let p0 = self.position; @@ -333,7 +337,9 @@ impl SvgPathParser { }; SvgPathCmd::LineTo(p1) } + // quadratic bezier curve b'Q' | b'q' => SvgPathCmd::QuadTo(self.parse_point()?, self.parse_point()?), + // smooth quadratic bezier curve b'T' | b't' => { let p1 = match self.prev_cmd { Some(SvgPathCmd::QuadTo(p1, p2)) => 2.0 * p2 - p1, @@ -342,11 +348,13 @@ impl SvgPathParser { let p2 = self.parse_point()?; SvgPathCmd::QuadTo(p1, p2) } + // cubic bezier curve b'C' | b'c' => SvgPathCmd::CubicTo( self.parse_point()?, self.parse_point()?, self.parse_point()?, ), + // smooth cubic bezier curve b'S' | b's' => { let p1 = match self.prev_cmd { Some(SvgPathCmd::CubicTo(_, p2, p3)) => 2.0 * p3 - p2, @@ -356,6 +364,7 @@ impl SvgPathParser { let p3 = self.parse_point()?; SvgPathCmd::CubicTo(p1, p2, p3) } + // elliptical arc b'A' | b'a' => { let rx = self.parser.parse_scalar()?; let ry = self.parser.parse_scalar()?; @@ -371,6 +380,7 @@ impl SvgPathParser { dst, } } + // close path b'Z' | b'z' => SvgPathCmd::Close(self.subpath_start), _ => unreachable!(), };