summaryrefslogtreecommitdiff
path: root/src/eval
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-04-07 18:04:29 +0200
committerLaurenz <laurmaedje@gmail.com>2022-04-07 18:04:29 +0200
commit4bb6240b401605ef6d905273db07545e14f9a21f (patch)
treeb01163a5fce3fe62d16abcbdabf37bc373617ff1 /src/eval
parent1192132dc0a9e991953fd29e93f87c8437a53ea0 (diff)
Make `Relative` generic
Diffstat (limited to 'src/eval')
-rw-r--r--src/eval/layout.rs12
-rw-r--r--src/eval/ops.rs20
-rw-r--r--src/eval/value.rs4
3 files changed, 19 insertions, 17 deletions
diff --git a/src/eval/layout.rs b/src/eval/layout.rs
index f541694c..9bf44194 100644
--- a/src/eval/layout.rs
+++ b/src/eval/layout.rs
@@ -8,7 +8,9 @@ use std::sync::Arc;
use super::{Barrier, StyleChain};
use crate::diag::TypResult;
use crate::frame::{Element, Frame, Geometry, Shape, Stroke};
-use crate::geom::{Align, Length, Paint, Point, Relative, Sides, Size, Spec, Transform};
+use crate::geom::{
+ Align, Length, Numeric, Paint, Point, Relative, Sides, Size, Spec, Transform,
+};
use crate::library::graphics::MoveNode;
use crate::library::layout::{AlignNode, PadNode};
use crate::util::Prehashed;
@@ -161,7 +163,7 @@ impl LayoutNode {
}
/// Force a size for this node.
- pub fn sized(self, sizing: Spec<Option<Relative>>) -> Self {
+ pub fn sized(self, sizing: Spec<Option<Relative<Length>>>) -> Self {
if sizing.any(Option::is_some) {
SizedNode { sizing, child: self }.pack()
} else {
@@ -189,7 +191,7 @@ impl LayoutNode {
}
/// Pad this node at the sides.
- pub fn padded(self, padding: Sides<Relative>) -> Self {
+ pub fn padded(self, padding: Sides<Relative<Length>>) -> Self {
if !padding.left.is_zero()
|| !padding.top.is_zero()
|| !padding.right.is_zero()
@@ -205,7 +207,7 @@ impl LayoutNode {
pub fn moved(self, offset: Point) -> Self {
if !offset.is_zero() {
MoveNode {
- transform: Transform::translation(offset.x, offset.y),
+ transform: Transform::translate(offset.x, offset.y),
child: self,
}
.pack()
@@ -292,7 +294,7 @@ impl Layout for EmptyNode {
#[derive(Debug, Hash)]
struct SizedNode {
/// How to size the node horizontally and vertically.
- sizing: Spec<Option<Relative>>,
+ sizing: Spec<Option<Relative<Length>>>,
/// The node to be sized.
child: LayoutNode,
}
diff --git a/src/eval/ops.rs b/src/eval/ops.rs
index 024de433..ff21d93f 100644
--- a/src/eval/ops.rs
+++ b/src/eval/ops.rs
@@ -2,7 +2,7 @@ use std::cmp::Ordering;
use super::{Dynamic, StrExt, Value};
use crate::diag::StrResult;
-use crate::geom::{Align, Spec, SpecAxis};
+use crate::geom::{Align, Numeric, Spec, SpecAxis};
use Value::*;
/// Bail with a type mismatch error.
@@ -66,12 +66,12 @@ pub fn add(lhs: Value, rhs: Value) -> StrResult<Value> {
(Angle(a), Angle(b)) => Angle(a + b),
(Length(a), Length(b)) => Length(a + b),
- (Length(a), Ratio(b)) => Relative(a + b),
- (Length(a), Relative(b)) => Relative(a + b),
+ (Length(a), Ratio(b)) => Relative(b + a),
+ (Length(a), Relative(b)) => Relative(b + a),
(Ratio(a), Length(b)) => Relative(a + b),
(Ratio(a), Ratio(b)) => Ratio(a + b),
- (Ratio(a), Relative(b)) => Relative(a + b),
+ (Ratio(a), Relative(b)) => Relative(b + a),
(Relative(a), Length(b)) => Relative(a + b),
(Relative(a), Ratio(b)) => Relative(a + b),
@@ -123,15 +123,15 @@ pub fn sub(lhs: Value, rhs: Value) -> StrResult<Value> {
(Angle(a), Angle(b)) => Angle(a - b),
(Length(a), Length(b)) => Length(a - b),
- (Length(a), Ratio(b)) => Relative(a - b),
- (Length(a), Relative(b)) => Relative(a - b),
+ (Length(a), Ratio(b)) => Relative(-b + a),
+ (Length(a), Relative(b)) => Relative(-b + a),
- (Ratio(a), Length(b)) => Relative(a - b),
+ (Ratio(a), Length(b)) => Relative(a + -b),
(Ratio(a), Ratio(b)) => Ratio(a - b),
- (Ratio(a), Relative(b)) => Relative(a - b),
+ (Ratio(a), Relative(b)) => Relative(-b + a),
- (Relative(a), Length(b)) => Relative(a - b),
- (Relative(a), Ratio(b)) => Relative(a - b),
+ (Relative(a), Length(b)) => Relative(a + -b),
+ (Relative(a), Ratio(b)) => Relative(a + -b),
(Relative(a), Relative(b)) => Relative(a - b),
(Fraction(a), Fraction(b)) => Fraction(a - b),
diff --git a/src/eval/value.rs b/src/eval/value.rs
index 774ae0b6..44df89e2 100644
--- a/src/eval/value.rs
+++ b/src/eval/value.rs
@@ -31,7 +31,7 @@ pub enum Value {
/// A ratio: `50%`.
Ratio(Ratio),
/// A relative length, combination of a ratio and a length: `20% + 5cm`.
- Relative(Relative),
+ Relative(Relative<Length>),
/// A fraction: `1fr`.
Fraction(Fraction),
/// A color value: `#f79143ff`.
@@ -549,7 +549,7 @@ primitive! { f64: "float", Float, Int(v) => v as f64 }
primitive! { Length: "length", Length }
primitive! { Angle: "angle", Angle }
primitive! { Ratio: "ratio", Ratio }
-primitive! { Relative: "relative length", Relative, Length(v) => v.into(), Ratio(v) => v.into() }
+primitive! { Relative<Length>: "relative length", Relative, Length(v) => v.into(), Ratio(v) => v.into() }
primitive! { Fraction: "fraction", Fraction }
primitive! { Color: "color", Color }
primitive! { EcoString: "string", Str }