|
|
|
@ -1,10 +1,16 @@
|
|
|
|
|
use super::SVGUnit;
|
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
|
use std::fmt;
|
|
|
|
|
use std::num::ParseFloatError;
|
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
use std::{
|
|
|
|
|
fmt::Display,
|
|
|
|
|
ops::{Add, Div, Mul, Sub},
|
|
|
|
|
};
|
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
use regex::Regex;
|
|
|
|
|
use derive_more::From;
|
|
|
|
|
use strum::ParseError;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
|
pub struct SVGMeasure {
|
|
|
|
@ -29,6 +35,32 @@ impl SVGMeasure {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(From, Debug)]
|
|
|
|
|
pub enum SVGUnitParseError {
|
|
|
|
|
ParseMeasure(ParseFloatError),
|
|
|
|
|
ParseUnit(ParseError),
|
|
|
|
|
Invalid,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FromStr for SVGMeasure {
|
|
|
|
|
type Err = SVGUnitParseError;
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
|
if s == "0" {
|
|
|
|
|
return Ok(SVGMeasure::new(0.0, SVGUnit::Pixel));
|
|
|
|
|
}
|
|
|
|
|
lazy_static! {
|
|
|
|
|
static ref RE: Regex = Regex::new(r"^([\d.]+)([a-zA-Z]+)$").unwrap();
|
|
|
|
|
}
|
|
|
|
|
if let Some(captures) = RE.captures(s) {
|
|
|
|
|
let measure = captures[1].parse::<f64>()?;
|
|
|
|
|
let unit = captures[2].parse::<SVGUnit>()?;
|
|
|
|
|
Ok(SVGMeasure::new(measure, unit))
|
|
|
|
|
} else {
|
|
|
|
|
Err(SVGUnitParseError::Invalid)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const EQ_TOLERANCE: f64 = 0.00001;
|
|
|
|
|
|
|
|
|
|
impl PartialEq for SVGMeasure {
|
|
|
|
|