generated from ElnuDev/rust-project
Compare commits
No commits in common. "d4c7f529558344c04848da6bccd404bc67534478" and "63f6b61f9199a3ba43058840c8f344b2d76d1f2f" have entirely different histories.
d4c7f52955
...
63f6b61f91
4 changed files with 32 additions and 100 deletions
|
@ -5,7 +5,6 @@ use std::{
|
||||||
ops::{Add, Div, Mul, Sub},
|
ops::{Add, Div, Mul, Sub},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
pub struct SVGMeasure {
|
pub struct SVGMeasure {
|
||||||
pub measure: f64,
|
pub measure: f64,
|
||||||
pub unit: SVGUnit,
|
pub unit: SVGUnit,
|
||||||
|
@ -15,29 +14,6 @@ impl SVGMeasure {
|
||||||
pub fn new(measure: f64, unit: SVGUnit) -> Self {
|
pub fn new(measure: f64, unit: SVGUnit) -> Self {
|
||||||
Self { measure, unit }
|
Self { measure, unit }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_user_units(&self) -> f64 {
|
|
||||||
self.measure * self.unit.to_user_units()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn to_unit(&self, unit: SVGUnit) -> Self {
|
|
||||||
SVGMeasure {
|
|
||||||
measure: self.to_user_units() / unit.to_user_units(),
|
|
||||||
unit,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const EQ_TOLERANCE: f64 = 0.00001;
|
|
||||||
|
|
||||||
impl PartialEq for SVGMeasure {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
(self.to_user_units() - other.to_user_units()).abs() < EQ_TOLERANCE
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ne(&self, other: &Self) -> bool {
|
|
||||||
(self.to_user_units() - other.to_user_units()).abs() > EQ_TOLERANCE
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for SVGMeasure {
|
impl Display for SVGMeasure {
|
||||||
|
@ -49,35 +25,31 @@ impl Display for SVGMeasure {
|
||||||
impl Add for SVGMeasure {
|
impl Add for SVGMeasure {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn add(mut self, other: Self) -> Self {
|
fn add(self, other: Self) -> Self {
|
||||||
self.measure += other.to_unit(self.unit).measure;
|
SVGMeasure::new(self.measure + other.measure, self.unit)
|
||||||
self
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sub for SVGMeasure {
|
impl Sub for SVGMeasure {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn sub(mut self, other: Self) -> Self {
|
fn sub(self, other: Self) -> Self {
|
||||||
self.measure -= other.to_unit(self.unit).measure;
|
SVGMeasure::new(self.measure - other.measure, self.unit)
|
||||||
self
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Mul<f64> for SVGMeasure {
|
impl Mul<f64> for SVGMeasure {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn mul(mut self, scalar: f64) -> Self {
|
fn mul(self, scalar: f64) -> Self {
|
||||||
self.measure *= scalar;
|
SVGMeasure::new(self.measure * scalar, self.unit)
|
||||||
self
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Div<f64> for SVGMeasure {
|
impl Div<f64> for SVGMeasure {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn div(mut self, scalar: f64) -> Self {
|
fn div(self, scalar: f64) -> Self {
|
||||||
self.measure /= scalar;
|
SVGMeasure::new(self.measure / scalar, self.unit)
|
||||||
self
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,3 @@ pub use unit::SVGUnit;
|
||||||
|
|
||||||
mod measure;
|
mod measure;
|
||||||
pub use measure::SVGMeasure;
|
pub use measure::SVGMeasure;
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests;
|
|
||||||
|
|
|
@ -1,46 +0,0 @@
|
||||||
use crate::svg::{SVGMeasure, SVGUnit};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn add() {
|
|
||||||
let m_10cm = SVGMeasure::new(10.0, SVGUnit::Centimeter);
|
|
||||||
let m_5mm = SVGMeasure::new(5.0, SVGUnit::Millimeter);
|
|
||||||
assert_eq!(m_10cm + m_5mm, SVGMeasure::new(10.5, SVGUnit::Centimeter));
|
|
||||||
assert_eq!(m_5mm + m_10cm, SVGMeasure::new(105.0, SVGUnit::Millimeter));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn sub() {
|
|
||||||
let m_10cm = SVGMeasure::new(10.0, SVGUnit::Centimeter);
|
|
||||||
let m_5mm = SVGMeasure::new(5.0, SVGUnit::Millimeter);
|
|
||||||
assert_eq!(m_10cm - m_5mm, SVGMeasure::new(9.5, SVGUnit::Centimeter));
|
|
||||||
assert_eq!(m_5mm - m_10cm, SVGMeasure::new(-95.0, SVGUnit::Millimeter));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn mul() {
|
|
||||||
let m_10cm = SVGMeasure::new(10.0, SVGUnit::Centimeter);
|
|
||||||
assert_eq!(m_10cm * 5.0, SVGMeasure::new(50.0, SVGUnit::Centimeter));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn div() {
|
|
||||||
let m_10cm = SVGMeasure::new(10.0, SVGUnit::Centimeter);
|
|
||||||
assert_eq!(m_10cm / 5.0, SVGMeasure::new(2.0, SVGUnit::Centimeter));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn eq() {
|
|
||||||
let m_1in = SVGMeasure::new(1.0, SVGUnit::Inch);
|
|
||||||
assert_eq!(m_1in, m_1in);
|
|
||||||
let m_1cm = SVGMeasure::new(1.0, SVGUnit::Centimeter);
|
|
||||||
let m_10mm = SVGMeasure::new(10.0, SVGUnit::Millimeter);
|
|
||||||
assert_eq!(m_1cm, m_10mm);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn ne() {
|
|
||||||
// Testing negative difference, there should be absolute value in PartialEq impl
|
|
||||||
let a = SVGMeasure::new(1.0, SVGUnit::Pixel);
|
|
||||||
let b = SVGMeasure::new(10.0, SVGUnit::Pixel);
|
|
||||||
assert_ne!(a, b);
|
|
||||||
}
|
|
|
@ -1,7 +1,6 @@
|
||||||
use std::fmt::{self, Display};
|
use std::fmt::{self, Display};
|
||||||
|
|
||||||
/// https://oreillymedia.github.io/Using_SVG/guide/units.html
|
/// https://oreillymedia.github.io/Using_SVG/guide/units.html
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
||||||
pub enum SVGUnit {
|
pub enum SVGUnit {
|
||||||
/// Pixel units, directly equivalent to SVG user units.
|
/// Pixel units, directly equivalent to SVG user units.
|
||||||
Pixel,
|
Pixel,
|
||||||
|
@ -15,20 +14,22 @@ pub enum SVGUnit {
|
||||||
Point,
|
Point,
|
||||||
// Picas.
|
// Picas.
|
||||||
Pica,
|
Pica,
|
||||||
}
|
/// Em units. Equivalent to the computed font-size in effect for an element.
|
||||||
|
Em,
|
||||||
impl SVGUnit {
|
/// Ex units. Equivalent to the height of a lower-case letter in the font (and font-size) in effect for an element. If the font doesn’t include lower-case letters, or doesn’t include the metadata about the ex-height, then 1ex = 0.5em.
|
||||||
pub fn to_user_units(&self) -> f64 {
|
Ex,
|
||||||
use SVGUnit::*;
|
/// Character units. Equivalent to the width of the `0` (zero) character in the font and font-size in effect for an element.
|
||||||
match self {
|
Character,
|
||||||
Pixel => 1.0,
|
/// Root-em units. The font size of the root element in the document, regardless of any settings for the current element.
|
||||||
Inch => 96.0,
|
Rem,
|
||||||
Centimeter => 37.795,
|
/// Viewport width units. 1% of the viewport width.
|
||||||
Millimeter => 3.7795,
|
ViewportWidth,
|
||||||
Point => 4.0 / 3.0, // 1.3333
|
/// Viewport height units. 1% of the viewport height.
|
||||||
Pica => 16.0,
|
ViewportHeight,
|
||||||
}
|
/// Viewport minimum units. 1% of the viewport width or height, whichever is smaller.
|
||||||
}
|
ViewportMinimum,
|
||||||
|
/// Viewport maximum units. 1% of the viewport width or height, whichever is larger.
|
||||||
|
ViewportMaximum,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for SVGUnit {
|
impl Display for SVGUnit {
|
||||||
|
@ -44,6 +45,14 @@ impl Display for SVGUnit {
|
||||||
Millimeter => "mm",
|
Millimeter => "mm",
|
||||||
Point => "pt",
|
Point => "pt",
|
||||||
Pica => "pc",
|
Pica => "pc",
|
||||||
|
Em => "em",
|
||||||
|
Ex => "ex",
|
||||||
|
Character => "ch",
|
||||||
|
Rem => "rem",
|
||||||
|
ViewportWidth => "vw",
|
||||||
|
ViewportHeight => "vh",
|
||||||
|
ViewportMinimum => "vmin",
|
||||||
|
ViewportMaximum => "vmax",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue