summaryrefslogtreecommitdiff
path: root/src/geom
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-08-21 19:08:47 +0200
committerLaurenz <laurmaedje@gmail.com>2021-08-21 19:08:47 +0200
commitc0377de653ed7c0ae0e253724cbbb622125fbd3f (patch)
treed69237f632084f07ce04e6d877cdea451a03f295 /src/geom
parent0dd4ae0a7ac0c247078df492469ff20b8a90c886 (diff)
Shorter/clearer field name for geometry types
Size { width, height } => Size { w, h } Spec { horizontal, vertical } => Spec { x, y } Gen { cross, main } => Gen { inline, block }
Diffstat (limited to 'src/geom')
-rw-r--r--src/geom/gen.rs71
-rw-r--r--src/geom/path.rs4
-rw-r--r--src/geom/point.rs4
-rw-r--r--src/geom/sides.rs8
-rw-r--r--src/geom/size.rs66
-rw-r--r--src/geom/spec.rs63
6 files changed, 99 insertions, 117 deletions
diff --git a/src/geom/gen.rs b/src/geom/gen.rs
index 075b8620..1b42968a 100644
--- a/src/geom/gen.rs
+++ b/src/geom/gen.rs
@@ -1,18 +1,18 @@
use super::*;
-/// A container with a main and cross component.
+/// A container with an inline and a block component.
#[derive(Default, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Gen<T> {
- /// The cross component.
- pub cross: T,
- /// The main component.
- pub main: T,
+ /// The inline component.
+ pub inline: T,
+ /// The block component.
+ pub block: T,
}
impl<T> Gen<T> {
/// Create a new instance from the two components.
- pub fn new(cross: T, main: T) -> Self {
- Self { cross, main }
+ pub fn new(inline: T, block: T) -> Self {
+ Self { inline, block }
}
/// Create a new instance with two equal components.
@@ -20,7 +20,7 @@ impl<T> Gen<T> {
where
T: Clone,
{
- Self { cross: value.clone(), main: value }
+ Self { inline: value.clone(), block: value }
}
/// Maps the individual fields with `f`.
@@ -28,14 +28,17 @@ impl<T> Gen<T> {
where
F: FnMut(T) -> U,
{
- Gen { cross: f(self.cross), main: f(self.main) }
+ Gen {
+ inline: f(self.inline),
+ block: f(self.block),
+ }
}
- /// Convert to the specific representation.
- pub fn to_spec(self, main: SpecAxis) -> Spec<T> {
- match main {
- SpecAxis::Horizontal => Spec::new(self.main, self.cross),
- SpecAxis::Vertical => Spec::new(self.cross, self.main),
+ /// Convert to the specific representation, given the current block axis.
+ pub fn to_spec(self, block: SpecAxis) -> Spec<T> {
+ match block {
+ SpecAxis::Horizontal => Spec::new(self.block, self.inline),
+ SpecAxis::Vertical => Spec::new(self.inline, self.block),
}
}
}
@@ -44,19 +47,19 @@ impl Gen<Length> {
/// The zero value.
pub fn zero() -> Self {
Self {
- main: Length::zero(),
- cross: Length::zero(),
+ inline: Length::zero(),
+ block: Length::zero(),
}
}
/// Convert to a point.
- pub fn to_point(self, main: SpecAxis) -> Point {
- self.to_spec(main).to_point()
+ pub fn to_point(self, block: SpecAxis) -> Point {
+ self.to_spec(block).to_point()
}
/// Convert to a size.
- pub fn to_size(self, main: SpecAxis) -> Size {
- self.to_spec(main).to_size()
+ pub fn to_size(self, block: SpecAxis) -> Size {
+ self.to_spec(block).to_size()
}
}
@@ -64,8 +67,8 @@ impl<T> Gen<Option<T>> {
/// Unwrap the individual fields.
pub fn unwrap_or(self, other: Gen<T>) -> Gen<T> {
Gen {
- cross: self.cross.unwrap_or(other.cross),
- main: self.main.unwrap_or(other.main),
+ inline: self.inline.unwrap_or(other.inline),
+ block: self.block.unwrap_or(other.block),
}
}
}
@@ -75,40 +78,40 @@ impl<T> Get<GenAxis> for Gen<T> {
fn get(self, axis: GenAxis) -> T {
match axis {
- GenAxis::Main => self.main,
- GenAxis::Cross => self.cross,
+ GenAxis::Inline => self.inline,
+ GenAxis::Block => self.block,
}
}
fn get_mut(&mut self, axis: GenAxis) -> &mut T {
match axis {
- GenAxis::Main => &mut self.main,
- GenAxis::Cross => &mut self.cross,
+ GenAxis::Inline => &mut self.inline,
+ GenAxis::Block => &mut self.block,
}
}
}
impl<T: Debug> Debug for Gen<T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- write!(f, "Gen({:?}, {:?})", self.main, self.cross)
+ write!(f, "Gen({:?}, {:?})", self.inline, self.block)
}
}
/// The two generic layouting axes.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum GenAxis {
- /// The axis pages and paragraphs are set along.
- Main,
/// The axis words and lines are set along.
- Cross,
+ Inline,
+ /// The axis paragraphs and pages are set along.
+ Block,
}
impl GenAxis {
/// The other axis.
pub fn other(self) -> Self {
match self {
- Self::Main => Self::Cross,
- Self::Cross => Self::Main,
+ Self::Inline => Self::Block,
+ Self::Block => Self::Inline,
}
}
}
@@ -116,8 +119,8 @@ impl GenAxis {
impl Display for GenAxis {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad(match self {
- Self::Main => "main",
- Self::Cross => "cross",
+ Self::Inline => "inline",
+ Self::Block => "block",
})
}
}
diff --git a/src/geom/path.rs b/src/geom/path.rs
index bb2832fd..f4e4a8b2 100644
--- a/src/geom/path.rs
+++ b/src/geom/path.rs
@@ -25,8 +25,8 @@ impl Path {
/// Create a path that approximates an axis-aligned ellipse.
pub fn ellipse(size: Size) -> Self {
// https://stackoverflow.com/a/2007782
- let rx = size.width / 2.0;
- let ry = size.height / 2.0;
+ let rx = size.w / 2.0;
+ let ry = size.h / 2.0;
let m = 0.551784;
let mx = m * rx;
let my = m * ry;
diff --git a/src/geom/point.rs b/src/geom/point.rs
index 13bced78..02f1d277 100644
--- a/src/geom/point.rs
+++ b/src/geom/point.rs
@@ -28,8 +28,8 @@ impl Point {
}
/// Convert to the generic representation.
- pub fn to_gen(self, main: SpecAxis) -> Gen<Length> {
- match main {
+ pub fn to_gen(self, block: SpecAxis) -> Gen<Length> {
+ match block {
SpecAxis::Horizontal => Gen::new(self.y, self.x),
SpecAxis::Vertical => Gen::new(self.x, self.y),
}
diff --git a/src/geom/sides.rs b/src/geom/sides.rs
index d4fdf247..fc7fb3f4 100644
--- a/src/geom/sides.rs
+++ b/src/geom/sides.rs
@@ -45,10 +45,10 @@ impl Sides<Linear> {
/// Resolve the linear sides relative to the given `size`.
pub fn resolve(self, size: Size) -> Sides<Length> {
Sides {
- left: self.left.resolve(size.width),
- top: self.top.resolve(size.height),
- right: self.right.resolve(size.width),
- bottom: self.bottom.resolve(size.height),
+ left: self.left.resolve(size.w),
+ top: self.top.resolve(size.h),
+ right: self.right.resolve(size.w),
+ bottom: self.bottom.resolve(size.h),
}
}
}
diff --git a/src/geom/size.rs b/src/geom/size.rs
index c191a80c..44ceea36 100644
--- a/src/geom/size.rs
+++ b/src/geom/size.rs
@@ -6,68 +6,65 @@ use serde::{Deserialize, Serialize};
#[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct Size {
/// The width.
- pub width: Length,
+ pub w: Length,
/// The height.
- pub height: Length,
+ pub h: Length,
}
impl Size {
/// The zero size.
pub fn zero() -> Self {
- Self {
- width: Length::zero(),
- height: Length::zero(),
- }
+ Self { w: Length::zero(), h: Length::zero() }
}
/// Create a new size from width and height.
- pub fn new(width: Length, height: Length) -> Self {
- Self { width, height }
+ pub fn new(w: Length, h: Length) -> Self {
+ Self { w, h }
}
/// Create an instance with two equal components.
- pub fn splat(value: Length) -> Self {
- Self { width: value, height: value }
+ pub fn splat(v: Length) -> Self {
+ Self { w: v, h: v }
}
/// Limit width and height at that of another size.
pub fn cap(self, limit: Self) -> Self {
Self {
- width: self.width.min(limit.width),
- height: self.height.min(limit.height),
+ w: self.w.min(limit.w),
+ h: self.h.min(limit.h),
}
}
/// Whether the other size fits into this one (smaller width and height).
pub fn fits(self, other: Self) -> bool {
- self.width.fits(other.width) && self.height.fits(other.height)
+ self.w.fits(other.w) && self.h.fits(other.h)
}
/// Whether both components are finite.
pub fn is_finite(self) -> bool {
- self.width.is_finite() && self.height.is_finite()
+ self.w.is_finite() && self.h.is_finite()
}
/// Whether any of the two components is infinite.
pub fn is_infinite(self) -> bool {
- self.width.is_infinite() || self.height.is_infinite()
+ self.w.is_infinite() || self.h.is_infinite()
}
/// Convert to a point.
pub fn to_point(self) -> Point {
- Point::new(self.width, self.height)
+ Point::new(self.w, self.h)
}
/// Convert to a Spec.
pub fn to_spec(self) -> Spec<Length> {
- Spec::new(self.width, self.height)
+ Spec::new(self.w, self.h)
}
/// Convert to the generic representation.
- pub fn to_gen(self, main: SpecAxis) -> Gen<Length> {
- match main {
- SpecAxis::Horizontal => Gen::new(self.height, self.width),
- SpecAxis::Vertical => Gen::new(self.width, self.height),
+ pub fn to_gen(self, block: SpecAxis) -> Gen<Length> {
+ match block {
+ SpecAxis::Horizontal => Gen::new(self.h, self.w),
+ SpecAxis::Vertical => Gen::new(self.w, self.h),
}
}
}
@@ -77,22 +74,22 @@ impl Get<SpecAxis> for Size {
fn get(self, axis: SpecAxis) -> Length {
match axis {
- SpecAxis::Horizontal => self.width,
- SpecAxis::Vertical => self.height,
+ SpecAxis::Horizontal => self.w,
+ SpecAxis::Vertical => self.h,
}
}
fn get_mut(&mut self, axis: SpecAxis) -> &mut Length {
match axis {
- SpecAxis::Horizontal => &mut self.width,
- SpecAxis::Vertical => &mut self.height,
+ SpecAxis::Horizontal => &mut self.w,
+ SpecAxis::Vertical => &mut self.h,
}
}
}
impl Debug for Size {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- write!(f, "Size({:?}, {:?})", self.width, self.height)
+ write!(f, "Size({:?}, {:?})", self.w, self.h)
}
}
@@ -100,7 +97,7 @@ impl Neg for Size {
type Output = Self;
fn neg(self) -> Self {
- Self { width: -self.width, height: -self.height }
+ Self { w: -self.w, h: -self.h }
}
}
@@ -108,10 +105,7 @@ impl Add for Size {
type Output = Self;
fn add(self, other: Self) -> Self {
- Self {
- width: self.width + other.width,
- height: self.height + other.height,
- }
+ Self { w: self.w + other.w, h: self.h + other.h }
}
}
@@ -121,10 +115,7 @@ impl Mul<f64> for Size {
type Output = Self;
fn mul(self, other: f64) -> Self {
- Self {
- width: self.width * other,
- height: self.height * other,
- }
+ Self { w: self.w * other, h: self.h * other }
}
}
@@ -140,10 +131,7 @@ impl Div<f64> for Size {
type Output = Self;
fn div(self, other: f64) -> Self {
- Self {
- width: self.width / other,
- height: self.height / other,
- }
+ Self { w: self.w / other, h: self.h / other }
}
}
diff --git a/src/geom/spec.rs b/src/geom/spec.rs
index 6d669d19..3b8b4dd6 100644
--- a/src/geom/spec.rs
+++ b/src/geom/spec.rs
@@ -4,26 +4,23 @@ use super::*;
#[derive(Default, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Spec<T> {
/// The horizontal component.
- pub horizontal: T,
+ pub x: T,
/// The vertical component.
- pub vertical: T,
+ pub y: T,
}
impl<T> Spec<T> {
/// Create a new instance from the two components.
- pub fn new(horizontal: T, vertical: T) -> Self {
- Self { horizontal, vertical }
+ pub fn new(x: T, y: T) -> Self {
+ Self { x, y }
}
/// Create a new instance with two equal components.
- pub fn splat(value: T) -> Self
+ pub fn splat(v: T) -> Self
where
T: Clone,
{
- Self {
- horizontal: value.clone(),
- vertical: value,
- }
+ Self { x: v.clone(), y: v }
}
/// Maps the individual fields with `f`.
@@ -31,17 +28,14 @@ impl<T> Spec<T> {
where
F: FnMut(T) -> U,
{
- Spec {
- horizontal: f(self.horizontal),
- vertical: f(self.vertical),
- }
+ Spec { x: f(self.x), y: f(self.y) }
}
/// Convert to the generic representation.
- pub fn to_gen(self, main: SpecAxis) -> Gen<T> {
- match main {
- SpecAxis::Horizontal => Gen::new(self.vertical, self.horizontal),
- SpecAxis::Vertical => Gen::new(self.horizontal, self.vertical),
+ pub fn to_gen(self, block: SpecAxis) -> Gen<T> {
+ match block {
+ SpecAxis::Horizontal => Gen::new(self.y, self.x),
+ SpecAxis::Vertical => Gen::new(self.x, self.y),
}
}
@@ -51,27 +45,24 @@ impl<T> Spec<T> {
where
F: Fn(&T, &U) -> bool,
{
- eq(&self.vertical, &other.vertical) && eq(&self.horizontal, &other.horizontal)
+ eq(&self.x, &other.x) && eq(&self.y, &other.y)
}
}
impl Spec<Length> {
/// The zero value.
pub fn zero() -> Self {
- Self {
- horizontal: Length::zero(),
- vertical: Length::zero(),
- }
+ Self { x: Length::zero(), y: Length::zero() }
}
/// Convert to a point.
pub fn to_point(self) -> Point {
- Point::new(self.horizontal, self.vertical)
+ Point::new(self.x, self.y)
}
/// Convert to a size.
pub fn to_size(self) -> Size {
- Size::new(self.horizontal, self.vertical)
+ Size::new(self.x, self.y)
}
}
@@ -79,8 +70,8 @@ impl<T> Spec<Option<T>> {
/// Unwrap the individual fields.
pub fn unwrap_or(self, other: Spec<T>) -> Spec<T> {
Spec {
- horizontal: self.horizontal.unwrap_or(other.horizontal),
- vertical: self.vertical.unwrap_or(other.vertical),
+ x: self.x.unwrap_or(other.x),
+ y: self.y.unwrap_or(other.y),
}
}
}
@@ -90,42 +81,42 @@ impl<T> Get<SpecAxis> for Spec<T> {
fn get(self, axis: SpecAxis) -> T {
match axis {
- SpecAxis::Horizontal => self.horizontal,
- SpecAxis::Vertical => self.vertical,
+ SpecAxis::Horizontal => self.x,
+ SpecAxis::Vertical => self.y,
}
}
fn get_mut(&mut self, axis: SpecAxis) -> &mut T {
match axis {
- SpecAxis::Horizontal => &mut self.horizontal,
- SpecAxis::Vertical => &mut self.vertical,
+ SpecAxis::Horizontal => &mut self.x,
+ SpecAxis::Vertical => &mut self.y,
}
}
}
impl<T: Debug> Debug for Spec<T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- write!(f, "Spec({:?}, {:?})", self.horizontal, self.vertical)
+ write!(f, "Spec({:?}, {:?})", self.x, self.y)
}
}
/// The two specific layouting axes.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum SpecAxis {
- /// The vertical layouting axis.
- Vertical,
/// The horizontal layouting axis.
Horizontal,
+ /// The vertical layouting axis.
+ Vertical,
}
impl SpecAxis {
/// The direction with the given positivity for this axis.
pub fn dir(self, positive: bool) -> Dir {
match (self, positive) {
- (Self::Vertical, true) => Dir::TTB,
- (Self::Vertical, false) => Dir::BTT,
(Self::Horizontal, true) => Dir::LTR,
(Self::Horizontal, false) => Dir::RTL,
+ (Self::Vertical, true) => Dir::TTB,
+ (Self::Vertical, false) => Dir::BTT,
}
}
@@ -141,8 +132,8 @@ impl SpecAxis {
impl Display for SpecAxis {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad(match self {
- Self::Vertical => "vertical",
Self::Horizontal => "horizontal",
+ Self::Vertical => "vertical",
})
}
}