diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-04-07 18:04:29 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-04-07 18:04:29 +0200 |
| commit | 4bb6240b401605ef6d905273db07545e14f9a21f (patch) | |
| tree | b01163a5fce3fe62d16abcbdabf37bc373617ff1 /src/library | |
| parent | 1192132dc0a9e991953fd29e93f87c8437a53ea0 (diff) | |
Make `Relative` generic
Diffstat (limited to 'src/library')
| -rw-r--r-- | src/library/graphics/line.rs | 17 | ||||
| -rw-r--r-- | src/library/graphics/shape.rs | 2 | ||||
| -rw-r--r-- | src/library/graphics/transform.rs | 8 | ||||
| -rw-r--r-- | src/library/layout/columns.rs | 4 | ||||
| -rw-r--r-- | src/library/layout/grid.rs | 4 | ||||
| -rw-r--r-- | src/library/layout/pad.rs | 6 | ||||
| -rw-r--r-- | src/library/layout/page.rs | 14 | ||||
| -rw-r--r-- | src/library/layout/spacing.rs | 2 | ||||
| -rw-r--r-- | src/library/mod.rs | 13 | ||||
| -rw-r--r-- | src/library/structure/list.rs | 6 | ||||
| -rw-r--r-- | src/library/structure/table.rs | 2 | ||||
| -rw-r--r-- | src/library/text/deco.rs | 12 | ||||
| -rw-r--r-- | src/library/text/mod.rs | 2 | ||||
| -rw-r--r-- | src/library/text/par.rs | 6 | ||||
| -rw-r--r-- | src/library/utility/color.rs | 2 |
15 files changed, 50 insertions, 50 deletions
diff --git a/src/library/graphics/line.rs b/src/library/graphics/line.rs index 1ca25bd9..571506c1 100644 --- a/src/library/graphics/line.rs +++ b/src/library/graphics/line.rs @@ -3,8 +3,10 @@ use crate::library::prelude::*; /// Display a line without affecting the layout. #[derive(Debug, Hash)] pub struct LineNode { - origin: Spec<Relative>, - delta: Spec<Relative>, + /// Where the line starts. + origin: Spec<Relative<Length>>, + /// The offset from the `origin` where the line ends. + delta: Spec<Relative<Length>>, } #[node] @@ -15,14 +17,15 @@ impl LineNode { pub const THICKNESS: Length = Length::pt(1.0); fn construct(_: &mut Context, args: &mut Args) -> TypResult<Content> { - let origin = args.named::<Spec<Relative>>("origin")?.unwrap_or_default(); - let delta = match args.named::<Spec<Relative>>("to")? { + let origin = args.named("origin")?.unwrap_or_default(); + let delta = match args.named::<Spec<Relative<Length>>>("to")? { Some(to) => to.zip(origin).map(|(to, from)| to - from), None => { - let length = - args.named::<Relative>("length")?.unwrap_or(Length::cm(1.0).into()); - let angle = args.named::<Angle>("angle")?.unwrap_or_default(); + let length = args + .named::<Relative<Length>>("length")? + .unwrap_or(Length::cm(1.0).into()); + let angle = args.named::<Angle>("angle")?.unwrap_or_default(); let x = angle.cos() * length; let y = angle.sin() * length; diff --git a/src/library/graphics/shape.rs b/src/library/graphics/shape.rs index 3f338d1a..9faa4c52 100644 --- a/src/library/graphics/shape.rs +++ b/src/library/graphics/shape.rs @@ -28,7 +28,7 @@ impl<const S: ShapeKind> ShapeNode<S> { /// The stroke's thickness. pub const THICKNESS: Length = Length::pt(1.0); /// How much to pad the shape's content. - pub const PADDING: Relative = Relative::zero(); + pub const PADDING: Relative<Length> = Relative::zero(); fn construct(_: &mut Context, args: &mut Args) -> TypResult<Content> { let size = match S { diff --git a/src/library/graphics/transform.rs b/src/library/graphics/transform.rs index 1ce91f28..eb419a7e 100644 --- a/src/library/graphics/transform.rs +++ b/src/library/graphics/transform.rs @@ -29,11 +29,11 @@ impl<const T: TransformKind> TransformNode<T> { MOVE => { let tx = args.named("x")?.unwrap_or_default(); let ty = args.named("y")?.unwrap_or_default(); - Transform::translation(tx, ty) + Transform::translate(tx, ty) } ROTATE => { let angle = args.named_or_find("angle")?.unwrap_or_default(); - Transform::rotation(angle) + Transform::rotate(angle) } SCALE | _ => { let all = args.find()?; @@ -62,9 +62,9 @@ impl<const T: TransformKind> Layout for TransformNode<T> { for frame in &mut frames { let Spec { x, y } = origin.zip(frame.size).map(|(o, s)| o.resolve(s)); - let transform = Transform::translation(x, y) + let transform = Transform::translate(x, y) .pre_concat(self.transform) - .pre_concat(Transform::translation(-x, -y)); + .pre_concat(Transform::translate(-x, -y)); Arc::make_mut(frame).transform(transform); } diff --git a/src/library/layout/columns.rs b/src/library/layout/columns.rs index 56e55d57..1cb45c37 100644 --- a/src/library/layout/columns.rs +++ b/src/library/layout/columns.rs @@ -14,7 +14,7 @@ pub struct ColumnsNode { #[node] impl ColumnsNode { /// The size of the gutter space between each column. - pub const GUTTER: Relative = Ratio::new(0.04).into(); + pub const GUTTER: Relative<Length> = Ratio::new(0.04).into(); fn construct(_: &mut Context, args: &mut Args) -> TypResult<Content> { Ok(Content::block(Self { @@ -33,7 +33,7 @@ impl Layout for ColumnsNode { ) -> TypResult<Vec<Arc<Frame>>> { // Separating the infinite space into infinite columns does not make // much sense. - if regions.first.x.is_infinite() { + if !regions.first.x.is_finite() { return self.child.layout(ctx, regions, styles); } diff --git a/src/library/layout/grid.rs b/src/library/layout/grid.rs index ee485bd2..b1e5e54c 100644 --- a/src/library/layout/grid.rs +++ b/src/library/layout/grid.rs @@ -58,7 +58,7 @@ pub enum TrackSizing { Auto, /// A track size specified in absolute terms and relative to the parent's /// size. - Relative(Relative), + Relative(Relative<Length>), /// A track size specified as a fraction of the remaining free space in the /// parent. Fractional(Fraction), @@ -422,7 +422,7 @@ impl<'a> GridLayouter<'a> { fn layout_relative_row( &mut self, ctx: &mut Context, - v: Relative, + v: Relative<Length>, y: usize, ) -> TypResult<()> { let resolved = v.resolve(self.regions.base.y); diff --git a/src/library/layout/pad.rs b/src/library/layout/pad.rs index 1ec5f124..b7470540 100644 --- a/src/library/layout/pad.rs +++ b/src/library/layout/pad.rs @@ -4,7 +4,7 @@ use crate::library::prelude::*; #[derive(Debug, Hash)] pub struct PadNode { /// The amount of padding. - pub padding: Sides<Relative>, + pub padding: Sides<Relative<Length>>, /// The child node whose sides to pad. pub child: LayoutNode, } @@ -54,7 +54,7 @@ impl Layout for PadNode { } /// Shrink a size by padding relative to the size itself. -fn shrink(size: Size, padding: Sides<Relative>) -> Size { +fn shrink(size: Size, padding: Sides<Relative<Length>>) -> Size { size - padding.resolve(size).sum_by_axis() } @@ -77,7 +77,7 @@ fn shrink(size: Size, padding: Sides<Relative>) -> Size { /// <=> w - p.rel * w - p.abs = s /// <=> (1 - p.rel) * w = s + p.abs /// <=> w = (s + p.abs) / (1 - p.rel) -fn grow(size: Size, padding: Sides<Relative>) -> Size { +fn grow(size: Size, padding: Sides<Relative<Length>>) -> Size { size.zip(padding.sum_by_axis()) .map(|(s, p)| (s + p.abs).safe_div(1.0 - p.rel.get())) } diff --git a/src/library/layout/page.rs b/src/library/layout/page.rs index abe1786f..37a87ae2 100644 --- a/src/library/layout/page.rs +++ b/src/library/layout/page.rs @@ -16,13 +16,13 @@ impl PageNode { /// Whether the page is flipped into landscape orientation. pub const FLIPPED: bool = false; /// The left margin. - pub const LEFT: Smart<Relative> = Smart::Auto; + pub const LEFT: Smart<Relative<Length>> = Smart::Auto; /// The right margin. - pub const RIGHT: Smart<Relative> = Smart::Auto; + pub const RIGHT: Smart<Relative<Length>> = Smart::Auto; /// The top margin. - pub const TOP: Smart<Relative> = Smart::Auto; + pub const TOP: Smart<Relative<Length>> = Smart::Auto; /// The bottom margin. - pub const BOTTOM: Smart<Relative> = Smart::Auto; + pub const BOTTOM: Smart<Relative<Length>> = Smart::Auto; /// The page's background color. pub const FILL: Option<Paint> = None; /// How many columns the page has. @@ -85,7 +85,7 @@ impl PageNode { } let mut min = width.min(height); - if min.is_infinite() { + if !min.is_finite() { min = Paper::A4.width(); } @@ -115,7 +115,7 @@ impl PageNode { } // Layout the child. - let regions = Regions::repeat(size, size, size.map(Length::is_finite)); + let regions = Regions::repeat(size, size, size.map(Numeric::is_finite)); let mut frames = child.layout(ctx, ®ions, styles)?; let header = styles.get(Self::HEADER); @@ -133,7 +133,7 @@ impl PageNode { let pos = Point::new(padding.left, y); let w = size.x - padding.left - padding.right; let area = Size::new(w, h); - let pod = Regions::one(area, area, area.map(Length::is_finite)); + let pod = Regions::one(area, area, area.map(Numeric::is_finite)); let sub = Layout::layout(&content, ctx, &pod, styles)?.remove(0); Arc::make_mut(frame).push_frame(pos, sub); } diff --git a/src/library/layout/spacing.rs b/src/library/layout/spacing.rs index 633093e9..e9837ef5 100644 --- a/src/library/layout/spacing.rs +++ b/src/library/layout/spacing.rs @@ -24,7 +24,7 @@ impl VNode { #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub enum Spacing { /// Spacing specified in absolute terms and relative to the parent's size. - Relative(Relative), + Relative(Relative<Length>), /// Spacing specified as a fraction of the remaining free space in the parent. Fractional(Fraction), } diff --git a/src/library/mod.rs b/src/library/mod.rs index af9ab575..7c5a519f 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -167,16 +167,13 @@ castable! { } castable! { - Spec<Relative>, + Spec<Relative<Length>>, Expected: "array of two relative lengths", Value::Array(array) => { - match array.as_slice() { - [a, b] => { - let a = a.clone().cast::<Relative>()?; - let b = b.clone().cast::<Relative>()?; - Spec::new(a, b) - }, - _ => return Err("point array must contain exactly two entries".to_string()), + let mut iter = array.into_iter(); + match (iter.next(), iter.next(), iter.next()) { + (Some(a), Some(b), None) => Spec::new(a.cast()?, b.cast()?), + _ => Err("point array must contain exactly two entries")?, } }, } diff --git a/src/library/structure/list.rs b/src/library/structure/list.rs index 532ec625..c58e8648 100644 --- a/src/library/structure/list.rs +++ b/src/library/structure/list.rs @@ -34,11 +34,11 @@ impl<const L: ListKind> ListNode<L> { #[property(referenced)] pub const LABEL: Label = Label::Default; /// The spacing between the list items of a non-wide list. - pub const SPACING: Relative = Relative::zero(); + pub const SPACING: Relative<Length> = Relative::zero(); /// The indentation of each item's label. - pub const INDENT: Relative = Ratio::new(0.0).into(); + pub const INDENT: Relative<Length> = Ratio::new(0.0).into(); /// The space between the label and the body of each item. - pub const BODY_INDENT: Relative = Ratio::new(0.5).into(); + pub const BODY_INDENT: Relative<Length> = Ratio::new(0.5).into(); /// The extra padding above the list. pub const ABOVE: Length = Length::zero(); /// The extra padding below the list. diff --git a/src/library/structure/table.rs b/src/library/structure/table.rs index 9317e43f..e01ae908 100644 --- a/src/library/structure/table.rs +++ b/src/library/structure/table.rs @@ -23,7 +23,7 @@ impl TableNode { /// The stroke's thickness. pub const THICKNESS: Length = Length::pt(1.0); /// How much to pad the cells's content. - pub const PADDING: Relative = Length::pt(5.0).into(); + pub const PADDING: Relative<Length> = Length::pt(5.0).into(); fn construct(_: &mut Context, args: &mut Args) -> TypResult<Content> { let columns = args.named("columns")?.unwrap_or_default(); diff --git a/src/library/text/deco.rs b/src/library/text/deco.rs index a81f0374..da1a1141 100644 --- a/src/library/text/deco.rs +++ b/src/library/text/deco.rs @@ -26,13 +26,13 @@ impl<const L: DecoLine> DecoNode<L> { /// Thickness of the line's strokes (dependent on scaled font size), read /// from the font tables if `None`. #[property(shorthand)] - pub const THICKNESS: Option<Relative> = None; + pub const THICKNESS: Option<Relative<Length>> = None; /// Position of the line relative to the baseline (dependent on scaled font /// size), read from the font tables if `None`. - pub const OFFSET: Option<Relative> = None; + pub const OFFSET: Option<Relative<Length>> = None; /// Amount that the line will be longer or shorter than its associated text /// (dependent on scaled font size). - pub const EXTENT: Relative = Relative::zero(); + pub const EXTENT: Relative<Length> = Relative::zero(); /// Whether the line skips sections in which it would collide /// with the glyphs. Does not apply to strikethrough. pub const EVADE: bool = true; @@ -66,9 +66,9 @@ impl<const L: DecoLine> Show for DecoNode<L> { pub struct Decoration { pub line: DecoLine, pub stroke: Option<Paint>, - pub thickness: Option<Relative>, - pub offset: Option<Relative>, - pub extent: Relative, + pub thickness: Option<Relative<Length>>, + pub offset: Option<Relative<Length>>, + pub extent: Relative<Length>, pub evade: bool, } diff --git a/src/library/text/mod.rs b/src/library/text/mod.rs index 975a4805..4a139fb3 100644 --- a/src/library/text/mod.rs +++ b/src/library/text/mod.rs @@ -188,7 +188,7 @@ castable! { /// The size of text. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] -pub struct FontSize(pub Relative); +pub struct FontSize(pub Relative<Length>); impl Fold for FontSize { type Output = Length; diff --git a/src/library/text/par.rs b/src/library/text/par.rs index a05aff44..2d31cd11 100644 --- a/src/library/text/par.rs +++ b/src/library/text/par.rs @@ -42,11 +42,11 @@ impl ParNode { /// will will be hyphenated if and only if justification is enabled. pub const HYPHENATE: Smart<bool> = Smart::Auto; /// The spacing between lines (dependent on scaled font size). - pub const LEADING: Relative = Ratio::new(0.65).into(); + pub const LEADING: Relative<Length> = Ratio::new(0.65).into(); /// The extra spacing between paragraphs (dependent on scaled font size). - pub const SPACING: Relative = Ratio::new(0.55).into(); + pub const SPACING: Relative<Length> = Ratio::new(0.55).into(); /// The indent the first line of a consecutive paragraph should have. - pub const INDENT: Relative = Relative::zero(); + pub const INDENT: Relative<Length> = Relative::zero(); fn construct(_: &mut Context, args: &mut Args) -> TypResult<Content> { // The paragraph constructor is special: It doesn't create a paragraph diff --git a/src/library/utility/color.rs b/src/library/utility/color.rs index 409af177..75410380 100644 --- a/src/library/utility/color.rs +++ b/src/library/utility/color.rs @@ -8,7 +8,7 @@ pub fn rgb(_: &mut Context, args: &mut Args) -> TypResult<Value> { if let Some(string) = args.find::<Spanned<EcoString>>()? { match RgbaColor::from_str(&string.v) { Ok(color) => color, - Err(_) => bail!(string.span, "invalid hex string"), + Err(msg) => bail!(string.span, msg), } } else { struct Component(u8); |
