summaryrefslogtreecommitdiff
path: root/library/src/layout
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-12-15 22:51:55 +0100
committerLaurenz <laurmaedje@gmail.com>2022-12-15 23:11:20 +0100
commitb6202b646a0d5ecced301d9bac8bfcaf977d7ee4 (patch)
tree7d42cb50f9e66153e7e8b2217009684e25f54f42 /library/src/layout
parentf3980c704544a464f9729cc8bc9f97d3a7454769 (diff)
Reflection for castables
Diffstat (limited to 'library/src/layout')
-rw-r--r--library/src/layout/align.rs2
-rw-r--r--library/src/layout/columns.rs4
-rw-r--r--library/src/layout/container.rs8
-rw-r--r--library/src/layout/grid.rs25
-rw-r--r--library/src/layout/hide.rs2
-rw-r--r--library/src/layout/pad.rs2
-rw-r--r--library/src/layout/page.rs55
-rw-r--r--library/src/layout/par.rs19
-rw-r--r--library/src/layout/place.rs2
-rw-r--r--library/src/layout/repeat.rs2
-rw-r--r--library/src/layout/spacing.rs16
-rw-r--r--library/src/layout/stack.rs10
-rw-r--r--library/src/layout/transform.rs4
13 files changed, 93 insertions, 58 deletions
diff --git a/library/src/layout/align.rs b/library/src/layout/align.rs
index 4fae3c3c..f00aeaf2 100644
--- a/library/src/layout/align.rs
+++ b/library/src/layout/align.rs
@@ -1,6 +1,8 @@
use crate::prelude::*;
/// Align content horizontally and vertically.
+///
+/// Tags: layout.
#[func]
#[capable]
#[derive(Debug, Hash)]
diff --git a/library/src/layout/columns.rs b/library/src/layout/columns.rs
index 0e29bc00..3bbf56e4 100644
--- a/library/src/layout/columns.rs
+++ b/library/src/layout/columns.rs
@@ -2,6 +2,8 @@ use crate::prelude::*;
use crate::text::TextNode;
/// Separate a region into multiple equally sized columns.
+///
+/// Tags: layout.
#[func]
#[capable(Layout)]
#[derive(Debug, Hash)]
@@ -103,6 +105,8 @@ impl Layout for ColumnsNode {
}
/// A column break.
+///
+/// Tags: layout.
#[func]
#[capable(Behave)]
#[derive(Debug, Hash)]
diff --git a/library/src/layout/container.rs b/library/src/layout/container.rs
index 85257715..d451bccf 100644
--- a/library/src/layout/container.rs
+++ b/library/src/layout/container.rs
@@ -2,6 +2,8 @@ use super::VNode;
use crate::prelude::*;
/// An inline-level container that sizes content.
+///
+/// Tags: layout.
#[func]
#[capable(Layout, Inline)]
#[derive(Debug, Hash)]
@@ -62,6 +64,8 @@ impl Layout for BoxNode {
impl Inline for BoxNode {}
/// A block-level container that places content into a separate flow.
+///
+/// Tags: layout.
#[func]
#[capable(Layout)]
#[derive(Debug, Hash)]
@@ -70,10 +74,10 @@ pub struct BlockNode(pub Content);
#[node]
impl BlockNode {
/// The spacing between the previous and this block.
- #[property(skip)]
+ #[property(reflect, skip)]
pub const ABOVE: VNode = VNode::block_spacing(Em::new(1.2).into());
/// The spacing between this and the following block.
- #[property(skip)]
+ #[property(reflect, skip)]
pub const BELOW: VNode = VNode::block_spacing(Em::new(1.2).into());
/// Whether this block must stick to the following one.
#[property(skip)]
diff --git a/library/src/layout/grid.rs b/library/src/layout/grid.rs
index 2a6bd4ff..e70210c0 100644
--- a/library/src/layout/grid.rs
+++ b/library/src/layout/grid.rs
@@ -3,6 +3,8 @@ use crate::prelude::*;
use super::Spacing;
/// Arrange content in a grid.
+///
+/// Tags: layout.
#[func]
#[capable(Layout)]
#[derive(Debug, Hash)]
@@ -85,17 +87,9 @@ pub struct TrackSizings(pub Vec<TrackSizing>);
castable! {
TrackSizings,
- Expected: "integer, auto, relative length, fraction, or array of the latter three",
- Value::Auto => Self(vec![TrackSizing::Auto]),
- Value::Length(v) => Self(vec![TrackSizing::Relative(v.into())]),
- Value::Ratio(v) => Self(vec![TrackSizing::Relative(v.into())]),
- Value::Relative(v) => Self(vec![TrackSizing::Relative(v)]),
- Value::Fraction(v) => Self(vec![TrackSizing::Fractional(v)]),
- Value::Int(v) => Self(vec![
- TrackSizing::Auto;
- Value::Int(v).cast::<NonZeroUsize>()?.get()
- ]),
- Value::Array(values) => Self(values
+ sizing: TrackSizing => Self(vec![sizing]),
+ count: NonZeroUsize => Self(vec![TrackSizing::Auto; count.get()]),
+ values: Array => Self(values
.into_iter()
.filter_map(|v| v.cast().ok())
.collect()),
@@ -103,12 +97,9 @@ castable! {
castable! {
TrackSizing,
- Expected: "auto, relative length, or fraction",
- Value::Auto => Self::Auto,
- Value::Length(v) => Self::Relative(v.into()),
- Value::Ratio(v) => Self::Relative(v.into()),
- Value::Relative(v) => Self::Relative(v),
- Value::Fraction(v) => Self::Fractional(v),
+ _: AutoValue => Self::Auto,
+ v: Rel<Length> => Self::Relative(v),
+ v: Fr => Self::Fractional(v),
}
/// Performs grid layout.
diff --git a/library/src/layout/hide.rs b/library/src/layout/hide.rs
index 1318b7ed..4e70dca9 100644
--- a/library/src/layout/hide.rs
+++ b/library/src/layout/hide.rs
@@ -1,6 +1,8 @@
use crate::prelude::*;
/// Hide content without affecting layout.
+///
+/// Tags: layout.
#[func]
#[capable(Layout, Inline)]
#[derive(Debug, Hash)]
diff --git a/library/src/layout/pad.rs b/library/src/layout/pad.rs
index 9c44919d..9d882ba4 100644
--- a/library/src/layout/pad.rs
+++ b/library/src/layout/pad.rs
@@ -1,6 +1,8 @@
use crate::prelude::*;
/// Pad content at the sides.
+///
+/// Tags: layout.
#[func]
#[capable(Layout)]
#[derive(Debug, Hash)]
diff --git a/library/src/layout/page.rs b/library/src/layout/page.rs
index 5a23b27b..fe83137e 100644
--- a/library/src/layout/page.rs
+++ b/library/src/layout/page.rs
@@ -5,6 +5,8 @@ use crate::prelude::*;
use crate::text::TextNode;
/// Layouts its child onto one or multiple pages.
+///
+/// Tags: layout.
#[func]
#[capable]
#[derive(Clone, Hash)]
@@ -12,6 +14,9 @@ pub struct PageNode(pub Content);
#[node]
impl PageNode {
+ /// The paper size.
+ #[property(reflect, skip, shorthand)]
+ pub const PAPER: Paper = Paper::A4;
/// The unflipped width of the page.
#[property(resolve)]
pub const WIDTH: Smart<Length> = Smart::Custom(Paper::A4.width().into());
@@ -145,6 +150,8 @@ impl Debug for PageNode {
}
/// A page break.
+///
+/// Tags: layout.
#[func]
#[capable]
#[derive(Debug, Copy, Clone, Hash)]
@@ -187,7 +194,10 @@ impl Marginal {
impl Cast<Spanned<Value>> for Marginal {
fn is(value: &Spanned<Value>) -> bool {
- matches!(&value.v, Value::Content(_) | Value::Func(_))
+ matches!(
+ &value.v,
+ Value::None | Value::Str(_) | Value::Content(_) | Value::Func(_)
+ )
}
fn cast(value: Spanned<Value>) -> StrResult<Self> {
@@ -196,43 +206,51 @@ impl Cast<Spanned<Value>> for Marginal {
Value::Str(v) => Ok(Self::Content(TextNode::packed(v))),
Value::Content(v) => Ok(Self::Content(v)),
Value::Func(v) => Ok(Self::Func(v, value.span)),
- v => Err(format_eco!(
- "expected none, content or function, found {}",
- v.type_name(),
- )),
+ v => Self::error(v),
}
}
+
+ fn describe() -> CastInfo {
+ CastInfo::Union(vec![
+ CastInfo::Type("none"),
+ CastInfo::Type("content"),
+ CastInfo::Type("function"),
+ ])
+ }
}
/// Specification of a paper.
-#[derive(Debug, Copy, Clone)]
+#[derive(Debug, Copy, Clone, Hash)]
pub struct Paper {
/// The width of the paper in millimeters.
- width: f64,
+ width: Scalar,
/// The height of the paper in millimeters.
- height: f64,
+ height: Scalar,
}
impl Paper {
/// The width of the paper.
pub fn width(self) -> Abs {
- Abs::mm(self.width)
+ Abs::mm(self.width.0)
}
/// The height of the paper.
pub fn height(self) -> Abs {
- Abs::mm(self.height)
+ Abs::mm(self.height.0)
}
}
/// Defines paper constants and a paper parsing implementation.
macro_rules! papers {
- ($(($var:ident: $width:expr, $height: expr, $($pats:tt)*))*) => {
+ ($(($var:ident: $width:expr, $height: expr, $pat:literal))*) => {
/// Predefined papers.
///
/// Each paper is parsable from its name in kebab-case.
impl Paper {
- $(pub const $var: Self = Self { width: $width, height: $height };)*
+ $(pub const $var: Self = Self {
+ width: Scalar($width),
+ height: Scalar($height),
+ };)*
}
impl FromStr for Paper {
@@ -240,18 +258,17 @@ macro_rules! papers {
fn from_str(name: &str) -> Result<Self, Self::Err> {
match name.to_lowercase().as_str() {
- $($($pats)* => Ok(Self::$var),)*
+ $($pat => Ok(Self::$var),)*
_ => Err("invalid paper name"),
}
}
}
- };
-}
-castable! {
- Paper,
- Expected: "string",
- Value::Str(string) => Self::from_str(&string)?,
+ castable! {
+ Paper,
+ $($pat => Self::$var,)*
+ }
+ };
}
// All paper sizes in mm.
diff --git a/library/src/layout/par.rs b/library/src/layout/par.rs
index 925eea54..eed0dbb1 100644
--- a/library/src/layout/par.rs
+++ b/library/src/layout/par.rs
@@ -12,6 +12,8 @@ use crate::text::{
};
/// Arrange text, spacing and inline-level nodes into a paragraph.
+///
+/// Tags: layout.
#[func]
#[capable]
#[derive(Hash)]
@@ -109,9 +111,8 @@ pub struct HorizontalAlign(pub GenAlign);
castable! {
HorizontalAlign,
- Expected: "alignment",
- @align: GenAlign => match align.axis() {
- Axis::X => Self(*align),
+ align: GenAlign => match align.axis() {
+ Axis::X => Self(align),
Axis::Y => Err("must be horizontal")?,
},
}
@@ -135,15 +136,15 @@ pub enum Linebreaks {
castable! {
Linebreaks,
- Expected: "string",
- Value::Str(string) => match string.as_str() {
- "simple" => Self::Simple,
- "optimized" => Self::Optimized,
- _ => Err(r#"expected "simple" or "optimized""#)?,
- },
+ /// Determine the linebreaks in a simple first-fit style.
+ "simple" => Self::Simple,
+ /// Optimize the linebreaks for the whole paragraph.
+ "optimized" => Self::Optimized,
}
/// A paragraph break.
+///
+/// Tags: layout.
#[func]
#[capable(Unlabellable)]
#[derive(Debug, Hash)]
diff --git a/library/src/layout/place.rs b/library/src/layout/place.rs
index 4c9c0a46..c3fcd0d5 100644
--- a/library/src/layout/place.rs
+++ b/library/src/layout/place.rs
@@ -1,6 +1,8 @@
use crate::prelude::*;
/// Place content at an absolute position.
+///
+/// Tags: layout.
#[func]
#[capable(Layout, Behave)]
#[derive(Debug, Hash)]
diff --git a/library/src/layout/repeat.rs b/library/src/layout/repeat.rs
index 196f19de..a47dbb3e 100644
--- a/library/src/layout/repeat.rs
+++ b/library/src/layout/repeat.rs
@@ -1,6 +1,8 @@
use crate::prelude::*;
/// Repeats content to fill a line.
+///
+/// Tags: layout.
#[func]
#[capable(Layout, Inline)]
#[derive(Debug, Hash)]
diff --git a/library/src/layout/spacing.rs b/library/src/layout/spacing.rs
index 91e45b03..e961c0cf 100644
--- a/library/src/layout/spacing.rs
+++ b/library/src/layout/spacing.rs
@@ -3,6 +3,8 @@ use std::cmp::Ordering;
use crate::prelude::*;
/// Horizontal spacing.
+///
+/// Tags: layout.
#[func]
#[capable(Behave)]
#[derive(Debug, Copy, Clone, Hash)]
@@ -52,6 +54,8 @@ impl Behave for HNode {
}
/// Vertical spacing.
+///
+/// Tags: layout.
#[func]
#[capable(Behave)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, PartialOrd)]
@@ -119,6 +123,11 @@ impl Behave for VNode {
}
}
+castable! {
+ VNode,
+ spacing: Spacing => VNode::block_around(spacing),
+}
+
/// Kinds of spacing.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Spacing {
@@ -160,9 +169,6 @@ impl PartialOrd for Spacing {
castable! {
Spacing,
- Expected: "relative length or fraction",
- Value::Length(v) => Self::Relative(v.into()),
- Value::Ratio(v) => Self::Relative(v.into()),
- Value::Relative(v) => Self::Relative(v),
- Value::Fraction(v) => Self::Fractional(v),
+ v: Rel<Length> => Self::Relative(v),
+ v: Fr => Self::Fractional(v),
}
diff --git a/library/src/layout/stack.rs b/library/src/layout/stack.rs
index 1e956669..111e3433 100644
--- a/library/src/layout/stack.rs
+++ b/library/src/layout/stack.rs
@@ -4,6 +4,8 @@ use super::{AlignNode, Spacing};
use crate::prelude::*;
/// Arrange content and spacing along an axis.
+///
+/// Tags: layout.
#[func]
#[capable(Layout)]
#[derive(Debug, Hash)]
@@ -81,12 +83,8 @@ impl Debug for StackChild {
castable! {
StackChild,
- Expected: "relative length, fraction, or content",
- Value::Length(v) => Self::Spacing(Spacing::Relative(v.into())),
- Value::Ratio(v) => Self::Spacing(Spacing::Relative(v.into())),
- Value::Relative(v) => Self::Spacing(Spacing::Relative(v)),
- Value::Fraction(v) => Self::Spacing(Spacing::Fractional(v)),
- Value::Content(v) => Self::Block(v),
+ spacing: Spacing => Self::Spacing(spacing),
+ content: Content => Self::Block(content),
}
/// Performs stack layout.
diff --git a/library/src/layout/transform.rs b/library/src/layout/transform.rs
index 35b6709a..f1a89d4c 100644
--- a/library/src/layout/transform.rs
+++ b/library/src/layout/transform.rs
@@ -3,6 +3,8 @@ use typst::geom::Transform;
use crate::prelude::*;
/// Move content without affecting layout.
+///
+/// Tags: layout.
#[func]
#[capable(Layout, Inline)]
#[derive(Debug, Hash)]
@@ -46,6 +48,8 @@ impl Layout for MoveNode {
impl Inline for MoveNode {}
/// Transform content without affecting layout.
+///
+/// Tags: layout.
#[func]
#[capable(Layout, Inline)]
#[derive(Debug, Hash)]