[librsvg: 2/4] Move Angle to its own file
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 2/4] Move Angle to its own file
- Date: Tue, 11 Dec 2018 00:39:56 +0000 (UTC)
commit e48ee942b5ae726f7a3778b487a684f06097a4ca
Author: Paolo Borelli <pborelli gnome org>
Date: Mon Dec 10 22:40:00 2018 +0100
Move Angle to its own file
Makefile.am | 1 +
rsvg_internals/src/angle.rs | 124 +++++++++++++++++++++++++++++++++++++++++++
rsvg_internals/src/lib.rs | 1 +
rsvg_internals/src/marker.rs | 104 +-----------------------------------
4 files changed, 127 insertions(+), 103 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index f1ddc052..312a51fb 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -42,6 +42,7 @@ RUST_SRC = \
rsvg_internals/Cargo.toml \
rsvg_internals/build.rs \
rsvg_internals/src/allowed_url.rs \
+ rsvg_internals/src/angle.rs \
rsvg_internals/src/aspect_ratio.rs \
rsvg_internals/src/attributes.rs \
rsvg_internals/src/bbox.rs \
diff --git a/rsvg_internals/src/angle.rs b/rsvg_internals/src/angle.rs
new file mode 100644
index 00000000..4872880f
--- /dev/null
+++ b/rsvg_internals/src/angle.rs
@@ -0,0 +1,124 @@
+use std::f64::consts::*;
+
+use cssparser::{Parser, Token};
+
+use error::ValueErrorKind;
+use parsers::{Parse, ParseError};
+
+#[derive(Debug, Copy, Clone, PartialEq)]
+pub struct Angle(f64);
+
+impl Angle {
+ pub fn new(rad: f64) -> Angle {
+ Angle(Angle::normalize(rad))
+ }
+
+ pub fn from_degrees(deg: f64) -> Angle {
+ Angle(Angle::normalize(deg.to_radians()))
+ }
+
+ pub fn from_vector(vx: f64, vy: f64) -> Angle {
+ let rad = vy.atan2(vx);
+
+ if rad.is_nan() {
+ Angle(0.0)
+ } else {
+ Angle(Angle::normalize(rad))
+ }
+ }
+
+ pub fn radians(&self) -> f64 {
+ self.0
+ }
+
+ pub fn bisect(&self, other: Angle) -> Angle {
+ let half_delta = (other.0 - self.0) * 0.5;
+
+ if FRAC_PI_2 < half_delta.abs() {
+ Angle(Angle::normalize(self.0 + half_delta - PI))
+ } else {
+ Angle(Angle::normalize(self.0 + half_delta))
+ }
+ }
+
+ // Normalizes an angle to [0.0, 2*PI)
+ fn normalize(rad: f64) -> f64 {
+ let res = rad % (PI * 2.0);
+ if res < 0.0 {
+ res + PI * 2.0
+ } else {
+ res
+ }
+ }
+}
+
+// angle:
+// https://www.w3.org/TR/SVG/types.html#DataTypeAngle
+//
+// angle ::= number ("deg" | "grad" | "rad")?
+//
+impl Parse for Angle {
+ type Data = ();
+ type Err = ValueErrorKind;
+
+ fn parse(parser: &mut Parser<'_, '_>, _: ()) -> Result<Angle, ValueErrorKind> {
+ let angle = {
+ let token = parser
+ .next()
+ .map_err(|_| ParseError::new("expected angle"))?;
+
+ match *token {
+ Token::Number { value, .. } => Angle::from_degrees(value as f64),
+
+ Token::Dimension {
+ value, ref unit, ..
+ } => {
+ let value = f64::from(value);
+
+ match unit.as_ref() {
+ "deg" => Angle::from_degrees(value),
+ "grad" => Angle::from_degrees(value * 360.0 / 400.0),
+ "rad" => Angle::new(value),
+ _ => {
+ return Err(ValueErrorKind::Parse(ParseError::new(
+ "expected 'deg' | 'grad' | 'rad'",
+ )))
+ }
+ }
+ }
+
+ _ => return Err(ValueErrorKind::Parse(ParseError::new("expected angle"))),
+ }
+ };
+
+ parser
+ .expect_exhausted()
+ .map_err(|_| ValueErrorKind::Parse(ParseError::new("expected angle")))?;
+
+ Ok(angle)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn parses_angle() {
+ assert_eq!(Angle::parse_str("0", ()), Ok(Angle::new(0.0)));
+ assert_eq!(Angle::parse_str("15", ()), Ok(Angle::from_degrees(15.0)));
+ assert_eq!(
+ Angle::parse_str("180.5deg", ()),
+ Ok(Angle::from_degrees(180.5))
+ );
+ assert_eq!(Angle::parse_str("1rad", ()), Ok(Angle::new(1.0)));
+ assert_eq!(
+ Angle::parse_str("-400grad", ()),
+ Ok(Angle::from_degrees(-360.0))
+ );
+
+ assert!(Angle::parse_str("", ()).is_err());
+ assert!(Angle::parse_str("foo", ()).is_err());
+ assert!(Angle::parse_str("300foo", ()).is_err());
+ }
+}
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index 8e2d507b..8ff34e01 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -89,6 +89,7 @@ mod float_eq_cairo;
mod property_macros;
mod allowed_url;
+mod angle;
mod aspect_ratio;
mod attributes;
mod bbox;
diff --git a/rsvg_internals/src/marker.rs b/rsvg_internals/src/marker.rs
index be28eb9f..d8e012b7 100644
--- a/rsvg_internals/src/marker.rs
+++ b/rsvg_internals/src/marker.rs
@@ -4,6 +4,7 @@ use std::f64::consts::*;
use cairo::MatrixTrait;
use cssparser::{CowRcStr, Parser, Token};
+use angle::Angle;
use aspect_ratio::*;
use attributes::Attribute;
use defs::Fragment;
@@ -586,96 +587,6 @@ fn find_outgoing_directionality_forwards(
(false, 0.0, 0.0)
}
-#[derive(Debug, Copy, Clone, PartialEq)]
-struct Angle(f64);
-
-impl Angle {
- pub fn new(rad: f64) -> Angle {
- Angle(Angle::normalize(rad))
- }
-
- pub fn from_degrees(deg: f64) -> Angle {
- Angle(Angle::normalize(deg.to_radians()))
- }
-
- pub fn from_vector(vx: f64, vy: f64) -> Angle {
- let rad = vy.atan2(vx);
-
- if rad.is_nan() {
- Angle(0.0)
- } else {
- Angle(Angle::normalize(rad))
- }
- }
-
- pub fn radians(&self) -> f64 {
- self.0
- }
-
- pub fn bisect(&self, other: Angle) -> Angle {
- let half_delta = (other.0 - self.0) * 0.5;
-
- if FRAC_PI_2 < half_delta.abs() {
- Angle(Angle::normalize(self.0 + half_delta - PI))
- } else {
- Angle(Angle::normalize(self.0 + half_delta))
- }
- }
-
- // Normalizes an angle to [0.0, 2*PI)
- fn normalize(rad: f64) -> f64 {
- let res = rad % (PI * 2.0);
- if res < 0.0 {
- res + PI * 2.0
- } else {
- res
- }
- }
-}
-
-// angle:
-// https://www.w3.org/TR/SVG/types.html#DataTypeAngle
-//
-// angle ::= number ("deg" | "grad" | "rad")?
-//
-impl Parse for Angle {
- type Data = ();
- type Err = ValueErrorKind;
-
- fn parse(parser: &mut Parser<'_, '_>, _: ()) -> Result<Angle, ValueErrorKind> {
- let angle = {
- let token = parser
- .next()
- .map_err(|_| ParseError::new("expected angle"))?;
-
- match *token {
- Token::Number { value, .. } => Angle::from_degrees(value as f64),
-
- Token::Dimension {
- value, ref unit, ..
- } => {
- let value = f64::from(value);
-
- match unit.as_ref() {
- "deg" => Angle::from_degrees(value),
- "grad" => Angle::from_degrees(value * 360.0 / 400.0),
- "rad" => Angle::new(value),
- _ => return Err(ValueErrorKind::Parse(ParseError::new("expected 'deg' | 'grad' |
'rad'"))),
- }
- }
-
- _ => return Err(ValueErrorKind::Parse(ParseError::new("expected angle"))),
- }
- };
-
- parser
- .expect_exhausted()
- .map_err(|_| ValueErrorKind::Parse(ParseError::new("expected angle")))?;
-
- Ok(angle)
- }
-}
-
// From SVG's marker-start, marker-mid, marker-end properties
#[derive(Debug, Copy, Clone, PartialEq)]
enum MarkerType {
@@ -918,19 +829,6 @@ where
mod parser_tests {
use super::*;
- #[test]
- fn parses_angle() {
- assert_eq!(Angle::parse_str("0", ()), Ok(Angle::new(0.0)));
- assert_eq!(Angle::parse_str("15", ()), Ok(Angle::from_degrees(15.0)));
- assert_eq!(Angle::parse_str("180.5deg", ()), Ok(Angle::from_degrees(180.5)));
- assert_eq!(Angle::parse_str("1rad", ()), Ok(Angle::new(1.0)));
- assert_eq!(Angle::parse_str("-400grad", ()), Ok(Angle::from_degrees(-360.0)));
-
- assert!(Angle::parse_str("", ()).is_err());
- assert!(Angle::parse_str("foo", ()).is_err());
- assert!(Angle::parse_str("300foo", ()).is_err());
- }
-
#[test]
fn parsing_invalid_marker_units_yields_error() {
assert!(is_parse_error(
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]