summaryrefslogtreecommitdiff
path: root/crates/typst-library/src/visualize
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2025-07-09 10:16:36 +0200
committerGitHub <noreply@github.com>2025-07-09 08:16:36 +0000
commite5e1dcd9c01341d2cd3473ac94a70223d5966086 (patch)
treed648efad9463cb10270d55ba35210eeb1e91ee22 /crates/typst-library/src/visualize
parent0a3c6939dd274f40672484695d909c2cc0d0d755 (diff)
Target-specific native show rules (#6569)
Diffstat (limited to 'crates/typst-library/src/visualize')
-rw-r--r--crates/typst-library/src/visualize/curve.rs19
-rw-r--r--crates/typst-library/src/visualize/image/mod.rs20
-rw-r--r--crates/typst-library/src/visualize/line.rs16
-rw-r--r--crates/typst-library/src/visualize/path.rs20
-rw-r--r--crates/typst-library/src/visualize/polygon.rs18
-rw-r--r--crates/typst-library/src/visualize/shape.rs56
6 files changed, 24 insertions, 125 deletions
diff --git a/crates/typst-library/src/visualize/curve.rs b/crates/typst-library/src/visualize/curve.rs
index 587f0d4a..15ae48c6 100644
--- a/crates/typst-library/src/visualize/curve.rs
+++ b/crates/typst-library/src/visualize/curve.rs
@@ -2,12 +2,9 @@ use kurbo::ParamCurveExtrema;
use typst_macros::{scope, Cast};
use typst_utils::Numeric;
-use crate::diag::{bail, HintedStrResult, HintedString, SourceResult};
-use crate::engine::Engine;
-use crate::foundations::{
- cast, elem, Content, NativeElement, Packed, Show, Smart, StyleChain,
-};
-use crate::layout::{Abs, Axes, BlockElem, Length, Point, Rel, Size};
+use crate::diag::{bail, HintedStrResult, HintedString};
+use crate::foundations::{cast, elem, Content, Packed, Smart};
+use crate::layout::{Abs, Axes, Length, Point, Rel, Size};
use crate::visualize::{FillRule, Paint, Stroke};
use super::FixedStroke;
@@ -42,7 +39,7 @@ use super::FixedStroke;
/// curve.close(),
/// )
/// ```
-#[elem(scope, Show)]
+#[elem(scope)]
pub struct CurveElem {
/// How to fill the curve.
///
@@ -95,14 +92,6 @@ pub struct CurveElem {
pub components: Vec<CurveComponent>,
}
-impl Show for Packed<CurveElem> {
- fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> {
- Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_curve)
- .pack()
- .spanned(self.span()))
- }
-}
-
#[scope]
impl CurveElem {
#[elem]
diff --git a/crates/typst-library/src/visualize/image/mod.rs b/crates/typst-library/src/visualize/image/mod.rs
index 48a14f0e..95021b81 100644
--- a/crates/typst-library/src/visualize/image/mod.rs
+++ b/crates/typst-library/src/visualize/image/mod.rs
@@ -15,13 +15,11 @@ use ecow::EcoString;
use typst_syntax::{Span, Spanned};
use typst_utils::LazyHash;
-use crate::diag::{SourceResult, StrResult};
-use crate::engine::Engine;
+use crate::diag::StrResult;
use crate::foundations::{
- cast, elem, func, scope, Bytes, Cast, Content, Derived, NativeElement, Packed, Show,
- Smart, StyleChain,
+ cast, elem, func, scope, Bytes, Cast, Content, Derived, NativeElement, Packed, Smart,
};
-use crate::layout::{BlockElem, Length, Rel, Sizing};
+use crate::layout::{Length, Rel, Sizing};
use crate::loading::{DataSource, Load, LoadSource, Loaded, Readable};
use crate::model::Figurable;
use crate::text::LocalName;
@@ -44,7 +42,7 @@ use crate::text::LocalName;
/// ],
/// )
/// ```
-#[elem(scope, Show, LocalName, Figurable)]
+#[elem(scope, LocalName, Figurable)]
pub struct ImageElem {
/// A [path]($syntax/#paths) to an image file or raw bytes making up an
/// image in one of the supported [formats]($image.format).
@@ -219,16 +217,6 @@ impl ImageElem {
}
}
-impl Show for Packed<ImageElem> {
- fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_image)
- .with_width(self.width.get(styles))
- .with_height(self.height.get(styles))
- .pack()
- .spanned(self.span()))
- }
-}
-
impl LocalName for Packed<ImageElem> {
const KEY: &'static str = "figure";
}
diff --git a/crates/typst-library/src/visualize/line.rs b/crates/typst-library/src/visualize/line.rs
index d058b926..7eecfc91 100644
--- a/crates/typst-library/src/visualize/line.rs
+++ b/crates/typst-library/src/visualize/line.rs
@@ -1,7 +1,5 @@
-use crate::diag::SourceResult;
-use crate::engine::Engine;
-use crate::foundations::{elem, Content, NativeElement, Packed, Show, StyleChain};
-use crate::layout::{Abs, Angle, Axes, BlockElem, Length, Rel};
+use crate::foundations::elem;
+use crate::layout::{Abs, Angle, Axes, Length, Rel};
use crate::visualize::Stroke;
/// A line from one point to another.
@@ -17,7 +15,7 @@ use crate::visualize::Stroke;
/// stroke: 2pt + maroon,
/// )
/// ```
-#[elem(Show)]
+#[elem]
pub struct LineElem {
/// The start point of the line.
///
@@ -50,11 +48,3 @@ pub struct LineElem {
#[fold]
pub stroke: Stroke,
}
-
-impl Show for Packed<LineElem> {
- fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> {
- Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_line)
- .pack()
- .spanned(self.span()))
- }
-}
diff --git a/crates/typst-library/src/visualize/path.rs b/crates/typst-library/src/visualize/path.rs
index e19e091d..bd8aea02 100644
--- a/crates/typst-library/src/visualize/path.rs
+++ b/crates/typst-library/src/visualize/path.rs
@@ -1,11 +1,7 @@
use self::PathVertex::{AllControlPoints, MirroredControlPoint, Vertex};
-use crate::diag::{bail, SourceResult};
-use crate::engine::Engine;
-use crate::foundations::{
- array, cast, elem, Array, Content, NativeElement, Packed, Reflect, Show, Smart,
- StyleChain,
-};
-use crate::layout::{Axes, BlockElem, Length, Rel};
+use crate::diag::bail;
+use crate::foundations::{array, cast, elem, Array, Reflect, Smart};
+use crate::layout::{Axes, Length, Rel};
use crate::visualize::{FillRule, Paint, Stroke};
/// A path through a list of points, connected by Bézier curves.
@@ -21,7 +17,7 @@ use crate::visualize::{FillRule, Paint, Stroke};
/// ((50%, 0pt), (40pt, 0pt)),
/// )
/// ```
-#[elem(Show)]
+#[elem]
pub struct PathElem {
/// How to fill the path.
///
@@ -83,14 +79,6 @@ pub struct PathElem {
pub vertices: Vec<PathVertex>,
}
-impl Show for Packed<PathElem> {
- fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> {
- Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_path)
- .pack()
- .spanned(self.span()))
- }
-}
-
/// A component used for path creation.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum PathVertex {
diff --git a/crates/typst-library/src/visualize/polygon.rs b/crates/typst-library/src/visualize/polygon.rs
index d75e1a65..db75a267 100644
--- a/crates/typst-library/src/visualize/polygon.rs
+++ b/crates/typst-library/src/visualize/polygon.rs
@@ -2,12 +2,8 @@ use std::f64::consts::PI;
use typst_syntax::Span;
-use crate::diag::SourceResult;
-use crate::engine::Engine;
-use crate::foundations::{
- elem, func, scope, Content, NativeElement, Packed, Show, Smart, StyleChain,
-};
-use crate::layout::{Axes, BlockElem, Em, Length, Rel};
+use crate::foundations::{elem, func, scope, Content, NativeElement, Smart};
+use crate::layout::{Axes, Em, Length, Rel};
use crate::visualize::{FillRule, Paint, Stroke};
/// A closed polygon.
@@ -25,7 +21,7 @@ use crate::visualize::{FillRule, Paint, Stroke};
/// (0%, 2cm),
/// )
/// ```
-#[elem(scope, Show)]
+#[elem(scope)]
pub struct PolygonElem {
/// How to fill the polygon.
///
@@ -124,11 +120,3 @@ impl PolygonElem {
elem.pack().spanned(span)
}
}
-
-impl Show for Packed<PolygonElem> {
- fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> {
- Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_polygon)
- .pack()
- .spanned(self.span()))
- }
-}
diff --git a/crates/typst-library/src/visualize/shape.rs b/crates/typst-library/src/visualize/shape.rs
index f21bf93e..fc7b8748 100644
--- a/crates/typst-library/src/visualize/shape.rs
+++ b/crates/typst-library/src/visualize/shape.rs
@@ -1,9 +1,5 @@
-use crate::diag::SourceResult;
-use crate::engine::Engine;
-use crate::foundations::{
- elem, Cast, Content, NativeElement, Packed, Show, Smart, StyleChain,
-};
-use crate::layout::{Abs, BlockElem, Corners, Length, Point, Rel, Sides, Size, Sizing};
+use crate::foundations::{elem, Cast, Content, Smart};
+use crate::layout::{Abs, Corners, Length, Point, Rel, Sides, Size, Sizing};
use crate::visualize::{Curve, FixedStroke, Paint, Stroke};
/// A rectangle with optional content.
@@ -19,7 +15,7 @@ use crate::visualize::{Curve, FixedStroke, Paint, Stroke};
/// to fit the content.
/// ]
/// ```
-#[elem(title = "Rectangle", Show)]
+#[elem(title = "Rectangle")]
pub struct RectElem {
/// The rectangle's width, relative to its parent container.
pub width: Smart<Rel<Length>>,
@@ -122,16 +118,6 @@ pub struct RectElem {
pub body: Option<Content>,
}
-impl Show for Packed<RectElem> {
- fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_rect)
- .with_width(self.width.get(styles))
- .with_height(self.height.get(styles))
- .pack()
- .spanned(self.span()))
- }
-}
-
/// A square with optional content.
///
/// # Example
@@ -145,7 +131,7 @@ impl Show for Packed<RectElem> {
/// sized to fit.
/// ]
/// ```
-#[elem(Show)]
+#[elem]
pub struct SquareElem {
/// The square's side length. This is mutually exclusive with `width` and
/// `height`.
@@ -209,16 +195,6 @@ pub struct SquareElem {
pub body: Option<Content>,
}
-impl Show for Packed<SquareElem> {
- fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_square)
- .with_width(self.width.get(styles))
- .with_height(self.height.get(styles))
- .pack()
- .spanned(self.span()))
- }
-}
-
/// An ellipse with optional content.
///
/// # Example
@@ -233,7 +209,7 @@ impl Show for Packed<SquareElem> {
/// to fit the content.
/// ]
/// ```
-#[elem(Show)]
+#[elem]
pub struct EllipseElem {
/// The ellipse's width, relative to its parent container.
pub width: Smart<Rel<Length>>,
@@ -269,16 +245,6 @@ pub struct EllipseElem {
pub body: Option<Content>,
}
-impl Show for Packed<EllipseElem> {
- fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_ellipse)
- .with_width(self.width.get(styles))
- .with_height(self.height.get(styles))
- .pack()
- .spanned(self.span()))
- }
-}
-
/// A circle with optional content.
///
/// # Example
@@ -293,7 +259,7 @@ impl Show for Packed<EllipseElem> {
/// sized to fit.
/// ]
/// ```
-#[elem(Show)]
+#[elem]
pub struct CircleElem {
/// The circle's radius. This is mutually exclusive with `width` and
/// `height`.
@@ -354,16 +320,6 @@ pub struct CircleElem {
pub body: Option<Content>,
}
-impl Show for Packed<CircleElem> {
- fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_circle)
- .with_width(self.width.get(styles))
- .with_height(self.height.get(styles))
- .pack()
- .spanned(self.span()))
- }
-}
-
/// A geometric shape with optional fill and stroke.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Shape {