Skip to content

Commit

Permalink
[color] (de)serialize for RGBA
Browse files Browse the repository at this point in the history
  • Loading branch information
aslpavel committed Mar 30, 2024
1 parent 671c9a4 commit 951ec95
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rasterize"
version = "0.3.10"
version = "0.3.11"
authors = ["Pavel Aslanov <[email protected]>"]
description = "Simple and small 2D rendering library"
edition = "2021"
Expand Down
25 changes: 24 additions & 1 deletion src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -224,12 +225,35 @@ impl FromStr for RGBA {
}
}

#[cfg(feature = "serde")]
impl Serialize for RGBA {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.collect_str(self)
}
}

#[cfg(feature = "serde")]
#[derive(Clone)]
pub struct RGBADeserializer<'a> {
pub colors: &'a HashMap<String, RGBA>,
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for RGBA {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
RGBADeserializer {
colors: &SVG_COLORS,
}
.deserialize(deserializer)
}
}

#[cfg(feature = "serde")]
impl<'de, 'a> DeserializeSeed<'de> for RGBADeserializer<'a> {
type Value = RGBA;
Expand All @@ -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)
}
Expand Down
10 changes: 10 additions & 0 deletions src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,15 @@ impl<I: Read> SvgPathParser<I> {
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;
Expand All @@ -323,6 +326,7 @@ impl<I: Read> SvgPathParser<I> {
};
SvgPathCmd::LineTo(p1)
}
// horizontal line
b'H' | b'h' => {
let x = self.parser.parse_scalar()?;
let p0 = self.position;
Expand All @@ -333,7 +337,9 @@ impl<I: Read> SvgPathParser<I> {
};
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,
Expand All @@ -342,11 +348,13 @@ impl<I: Read> SvgPathParser<I> {
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,
Expand All @@ -356,6 +364,7 @@ impl<I: Read> SvgPathParser<I> {
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()?;
Expand All @@ -371,6 +380,7 @@ impl<I: Read> SvgPathParser<I> {
dst,
}
}
// close path
b'Z' | b'z' => SvgPathCmd::Close(self.subpath_start),
_ => unreachable!(),
};
Expand Down

0 comments on commit 951ec95

Please sign in to comment.