summaryrefslogtreecommitdiff
path: root/src/geom
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-08-23 13:18:20 +0200
committerLaurenz <laurmaedje@gmail.com>2021-08-23 13:21:44 +0200
commit0806af4aecc9414962b13894a2a3c4befd2ca3c8 (patch)
tree0267109894f9cf4b74fe2663a2e29cf589d75b44 /src/geom
parentc0377de653ed7c0ae0e253724cbbb622125fbd3f (diff)
Kerned PDF output
Diffstat (limited to 'src/geom')
-rw-r--r--src/geom/angle.rs3
-rw-r--r--src/geom/em.rs112
-rw-r--r--src/geom/fr.rs2
-rw-r--r--src/geom/length.rs3
-rw-r--r--src/geom/mod.rs5
-rw-r--r--src/geom/path.rs2
-rw-r--r--src/geom/point.rs2
-rw-r--r--src/geom/relative.rs2
-rw-r--r--src/geom/size.rs2
9 files changed, 117 insertions, 16 deletions
diff --git a/src/geom/angle.rs b/src/geom/angle.rs
index cf3e49a0..023b1d98 100644
--- a/src/geom/angle.rs
+++ b/src/geom/angle.rs
@@ -1,6 +1,3 @@
-use decorum::N64;
-use serde::{Deserialize, Serialize};
-
use super::*;
/// An angle.
diff --git a/src/geom/em.rs b/src/geom/em.rs
new file mode 100644
index 00000000..45e5ba60
--- /dev/null
+++ b/src/geom/em.rs
@@ -0,0 +1,112 @@
+use super::*;
+
+/// A length that is relative to the font size.
+///
+/// `1em` is the same as the font size.
+#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
+#[derive(Serialize, Deserialize)]
+pub struct Em(N64);
+
+impl Em {
+ /// The zero length.
+ pub fn zero() -> Self {
+ Self(N64::from(0.0))
+ }
+
+ /// The font size.
+ pub fn one() -> Self {
+ Self(N64::from(1.0))
+ }
+
+ /// Create an font-relative length.
+ pub fn new(em: f64) -> Self {
+ Self(N64::from(em))
+ }
+
+ /// Create font units at the given units per em.
+ pub fn from_units(units: impl Into<f64>, units_per_em: f64) -> Self {
+ Self(N64::from(units.into() / units_per_em))
+ }
+
+ /// Convert to a length at the given font size.
+ pub fn to_length(self, font_size: Length) -> Length {
+ self.get() * font_size
+ }
+
+ /// The number of em units.
+ pub fn get(self) -> f64 {
+ self.0.into()
+ }
+
+ /// Whether the length is zero.
+ pub fn is_zero(self) -> bool {
+ self.0 == 0.0
+ }
+}
+
+impl Debug for Em {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ Display::fmt(self, f)
+ }
+}
+
+impl Display for Em {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ write!(f, "{}em", self.get())
+ }
+}
+
+impl Neg for Em {
+ type Output = Self;
+
+ fn neg(self) -> Self {
+ Self(-self.0)
+ }
+}
+
+impl Add for Em {
+ type Output = Self;
+
+ fn add(self, other: Self) -> Self {
+ Self(self.0 + other.0)
+ }
+}
+
+sub_impl!(Em - Em -> Em);
+
+impl Mul<f64> for Em {
+ type Output = Self;
+
+ fn mul(self, other: f64) -> Self {
+ Self(self.0 * other)
+ }
+}
+
+impl Mul<Em> for f64 {
+ type Output = Em;
+
+ fn mul(self, other: Em) -> Em {
+ other * self
+ }
+}
+
+impl Div<f64> for Em {
+ type Output = Self;
+
+ fn div(self, other: f64) -> Self {
+ Self(self.0 / other)
+ }
+}
+
+impl Div for Em {
+ type Output = f64;
+
+ fn div(self, other: Self) -> f64 {
+ self.get() / other.get()
+ }
+}
+
+assign_impl!(Em += Em);
+assign_impl!(Em -= Em);
+assign_impl!(Em *= f64);
+assign_impl!(Em /= f64);
diff --git a/src/geom/fr.rs b/src/geom/fr.rs
index eaf539d9..ed0c8329 100644
--- a/src/geom/fr.rs
+++ b/src/geom/fr.rs
@@ -1,5 +1,3 @@
-use decorum::N64;
-
use super::*;
/// A fractional length.
diff --git a/src/geom/length.rs b/src/geom/length.rs
index 3510fa6f..f8484f75 100644
--- a/src/geom/length.rs
+++ b/src/geom/length.rs
@@ -1,6 +1,3 @@
-use decorum::N64;
-use serde::{Deserialize, Serialize};
-
use super::*;
/// An absolute length.
diff --git a/src/geom/mod.rs b/src/geom/mod.rs
index 6f48d7d9..11e082b8 100644
--- a/src/geom/mod.rs
+++ b/src/geom/mod.rs
@@ -5,6 +5,7 @@ mod macros;
mod align;
mod angle;
mod dir;
+mod em;
mod fr;
mod gen;
mod length;
@@ -19,6 +20,7 @@ mod spec;
pub use align::*;
pub use angle::*;
pub use dir::*;
+pub use em::*;
pub use fr::*;
pub use gen::*;
pub use length::*;
@@ -35,6 +37,9 @@ use std::fmt::{self, Debug, Display, Formatter};
use std::iter::Sum;
use std::ops::*;
+use decorum::N64;
+use serde::{Deserialize, Serialize};
+
/// Generic access to a structure's components.
pub trait Get<Index> {
/// The structure's component type.
diff --git a/src/geom/path.rs b/src/geom/path.rs
index f4e4a8b2..bc0d3f2d 100644
--- a/src/geom/path.rs
+++ b/src/geom/path.rs
@@ -1,7 +1,5 @@
use super::*;
-use serde::{Deserialize, Serialize};
-
/// A bezier path.
#[derive(Debug, Default, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(transparent)]
diff --git a/src/geom/point.rs b/src/geom/point.rs
index 02f1d277..8ab043c3 100644
--- a/src/geom/point.rs
+++ b/src/geom/point.rs
@@ -1,7 +1,5 @@
use super::*;
-use serde::{Deserialize, Serialize};
-
/// A point in 2D.
#[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct Point {
diff --git a/src/geom/relative.rs b/src/geom/relative.rs
index 7785788c..056af206 100644
--- a/src/geom/relative.rs
+++ b/src/geom/relative.rs
@@ -1,5 +1,3 @@
-use decorum::N64;
-
use super::*;
/// A relative length.
diff --git a/src/geom/size.rs b/src/geom/size.rs
index 44ceea36..506e1c8f 100644
--- a/src/geom/size.rs
+++ b/src/geom/size.rs
@@ -1,7 +1,5 @@
use super::*;
-use serde::{Deserialize, Serialize};
-
/// A size in 2D.
#[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct Size {