summaryrefslogtreecommitdiff
path: root/crates/typst-library/src
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-12-17 10:25:27 +0100
committerGitHub <noreply@github.com>2024-12-17 09:25:27 +0000
commit134638525516995d5947c5b3f98ffbc13784a143 (patch)
tree549d3ed3b5a4eac66b585314e772fda092058021 /crates/typst-library/src
parented67220e4b5ae6b3a1bc50f59bd52b5b1dea3a6b (diff)
Rename `pattern` to `tiling` (#5590)
Diffstat (limited to 'crates/typst-library/src')
-rw-r--r--crates/typst-library/src/foundations/ops.rs4
-rw-r--r--crates/typst-library/src/foundations/value.rs16
-rw-r--r--crates/typst-library/src/introspection/locator.rs2
-rw-r--r--crates/typst-library/src/text/mod.rs2
-rw-r--r--crates/typst-library/src/visualize/mod.rs11
-rw-r--r--crates/typst-library/src/visualize/paint.rs28
-rw-r--r--crates/typst-library/src/visualize/stroke.rs26
-rw-r--r--crates/typst-library/src/visualize/tiling.rs (renamed from crates/typst-library/src/visualize/pattern.rs)102
8 files changed, 97 insertions, 94 deletions
diff --git a/crates/typst-library/src/foundations/ops.rs b/crates/typst-library/src/foundations/ops.rs
index b41fce8d..8d12966b 100644
--- a/crates/typst-library/src/foundations/ops.rs
+++ b/crates/typst-library/src/foundations/ops.rs
@@ -144,8 +144,8 @@ pub fn add(lhs: Value, rhs: Value) -> HintedStrResult<Value> {
| (Length(thickness), Gradient(gradient)) => {
Stroke::from_pair(gradient, thickness).into_value()
}
- (Pattern(pattern), Length(thickness)) | (Length(thickness), Pattern(pattern)) => {
- Stroke::from_pair(pattern, thickness).into_value()
+ (Tiling(tiling), Length(thickness)) | (Length(thickness), Tiling(tiling)) => {
+ Stroke::from_pair(tiling, thickness).into_value()
}
(Duration(a), Duration(b)) => Duration(a + b),
diff --git a/crates/typst-library/src/foundations/value.rs b/crates/typst-library/src/foundations/value.rs
index 538b4f1b..eb0d6eed 100644
--- a/crates/typst-library/src/foundations/value.rs
+++ b/crates/typst-library/src/foundations/value.rs
@@ -20,7 +20,7 @@ use crate::foundations::{
};
use crate::layout::{Abs, Angle, Em, Fr, Length, Ratio, Rel};
use crate::text::{RawContent, RawElem, TextElem};
-use crate::visualize::{Color, Gradient, Pattern};
+use crate::visualize::{Color, Gradient, Tiling};
/// A computational value.
#[derive(Default, Clone)]
@@ -50,8 +50,8 @@ pub enum Value {
Color(Color),
/// A gradient value: `gradient.linear(...)`.
Gradient(Gradient),
- /// A pattern fill: `pattern(...)`.
- Pattern(Pattern),
+ /// A tiling fill: `tiling(...)`.
+ Tiling(Tiling),
/// A symbol: `arrow.l`.
Symbol(Symbol),
/// A version.
@@ -130,7 +130,7 @@ impl Value {
Self::Fraction(_) => Type::of::<Fr>(),
Self::Color(_) => Type::of::<Color>(),
Self::Gradient(_) => Type::of::<Gradient>(),
- Self::Pattern(_) => Type::of::<Pattern>(),
+ Self::Tiling(_) => Type::of::<Tiling>(),
Self::Symbol(_) => Type::of::<Symbol>(),
Self::Version(_) => Type::of::<Version>(),
Self::Str(_) => Type::of::<Str>(),
@@ -244,7 +244,7 @@ impl Debug for Value {
Self::Fraction(v) => Debug::fmt(v, f),
Self::Color(v) => Debug::fmt(v, f),
Self::Gradient(v) => Debug::fmt(v, f),
- Self::Pattern(v) => Debug::fmt(v, f),
+ Self::Tiling(v) => Debug::fmt(v, f),
Self::Symbol(v) => Debug::fmt(v, f),
Self::Version(v) => Debug::fmt(v, f),
Self::Str(v) => Debug::fmt(v, f),
@@ -282,7 +282,7 @@ impl Repr for Value {
Self::Fraction(v) => v.repr(),
Self::Color(v) => v.repr(),
Self::Gradient(v) => v.repr(),
- Self::Pattern(v) => v.repr(),
+ Self::Tiling(v) => v.repr(),
Self::Symbol(v) => v.repr(),
Self::Version(v) => v.repr(),
Self::Str(v) => v.repr(),
@@ -333,7 +333,7 @@ impl Hash for Value {
Self::Fraction(v) => v.hash(state),
Self::Color(v) => v.hash(state),
Self::Gradient(v) => v.hash(state),
- Self::Pattern(v) => v.hash(state),
+ Self::Tiling(v) => v.hash(state),
Self::Symbol(v) => v.hash(state),
Self::Version(v) => v.hash(state),
Self::Str(v) => v.hash(state),
@@ -640,7 +640,7 @@ primitive! { Rel<Length>: "relative length",
primitive! { Fr: "fraction", Fraction }
primitive! { Color: "color", Color }
primitive! { Gradient: "gradient", Gradient }
-primitive! { Pattern: "pattern", Pattern }
+primitive! { Tiling: "tiling", Tiling }
primitive! { Symbol: "symbol", Symbol }
primitive! { Version: "version", Version }
primitive! {
diff --git a/crates/typst-library/src/introspection/locator.rs b/crates/typst-library/src/introspection/locator.rs
index a84cf163..3ba3d648 100644
--- a/crates/typst-library/src/introspection/locator.rs
+++ b/crates/typst-library/src/introspection/locator.rs
@@ -161,7 +161,7 @@ impl<'a> Locator<'a> {
///
/// Should typically only be created at the document level, though there
/// are a few places where we use it as well that just don't support
- /// introspection (e.g. drawable patterns).
+ /// introspection (e.g. tilings).
pub fn root() -> Self {
Self { local: 0, outer: None }
}
diff --git a/crates/typst-library/src/text/mod.rs b/crates/typst-library/src/text/mod.rs
index 62c87051..91927b57 100644
--- a/crates/typst-library/src/text/mod.rs
+++ b/crates/typst-library/src/text/mod.rs
@@ -249,7 +249,7 @@ pub struct TextElem {
if paint.v.relative() == Smart::Custom(RelativeTo::Self_) {
bail!(
paint.span,
- "gradients and patterns on text must be relative to the parent";
+ "gradients and tilings on text must be relative to the parent";
hint: "make sure to set `relative: auto` on your text fill"
);
}
diff --git a/crates/typst-library/src/visualize/mod.rs b/crates/typst-library/src/visualize/mod.rs
index 5c8bf646..61c56a61 100644
--- a/crates/typst-library/src/visualize/mod.rs
+++ b/crates/typst-library/src/visualize/mod.rs
@@ -6,10 +6,10 @@ mod image;
mod line;
mod paint;
mod path;
-mod pattern;
mod polygon;
mod shape;
mod stroke;
+mod tiling;
pub use self::color::*;
pub use self::gradient::*;
@@ -17,12 +17,12 @@ pub use self::image::*;
pub use self::line::*;
pub use self::paint::*;
pub use self::path::*;
-pub use self::pattern::*;
pub use self::polygon::*;
pub use self::shape::*;
pub use self::stroke::*;
+pub use self::tiling::*;
-use crate::foundations::{category, Category, Scope};
+use crate::foundations::{category, Category, Scope, Type};
/// Drawing and data visualization.
///
@@ -37,7 +37,7 @@ pub(super) fn define(global: &mut Scope) {
global.category(VISUALIZE);
global.define_type::<Color>();
global.define_type::<Gradient>();
- global.define_type::<Pattern>();
+ global.define_type::<Tiling>();
global.define_type::<Stroke>();
global.define_elem::<ImageElem>();
global.define_elem::<LineElem>();
@@ -47,4 +47,7 @@ pub(super) fn define(global: &mut Scope) {
global.define_elem::<CircleElem>();
global.define_elem::<PolygonElem>();
global.define_elem::<PathElem>();
+
+ // Compatibility.
+ global.define("pattern", Type::of::<Tiling>());
}
diff --git a/crates/typst-library/src/visualize/paint.rs b/crates/typst-library/src/visualize/paint.rs
index cd1006aa..a618e515 100644
--- a/crates/typst-library/src/visualize/paint.rs
+++ b/crates/typst-library/src/visualize/paint.rs
@@ -3,7 +3,7 @@ use std::fmt::{self, Debug, Formatter};
use ecow::EcoString;
use crate::foundations::{cast, Repr, Smart};
-use crate::visualize::{Color, Gradient, Pattern, RelativeTo};
+use crate::visualize::{Color, Gradient, RelativeTo, Tiling};
/// How a fill or stroke should be painted.
#[derive(Clone, Eq, PartialEq, Hash)]
@@ -12,8 +12,8 @@ pub enum Paint {
Solid(Color),
/// A gradient.
Gradient(Gradient),
- /// A pattern.
- Pattern(Pattern),
+ /// A tiling.
+ Tiling(Tiling),
}
impl Paint {
@@ -21,7 +21,7 @@ impl Paint {
pub fn unwrap_solid(&self) -> Color {
match self {
Self::Solid(color) => *color,
- Self::Gradient(_) | Self::Pattern(_) => panic!("expected solid color"),
+ Self::Gradient(_) | Self::Tiling(_) => panic!("expected solid color"),
}
}
@@ -30,7 +30,7 @@ impl Paint {
match self {
Self::Solid(_) => Smart::Auto,
Self::Gradient(gradient) => gradient.relative(),
- Self::Pattern(pattern) => pattern.relative(),
+ Self::Tiling(tiling) => tiling.relative(),
}
}
@@ -44,8 +44,8 @@ impl Paint {
Self::Gradient(gradient) => {
Self::Gradient(gradient.clone().with_relative(RelativeTo::Parent))
}
- Self::Pattern(pattern) => {
- Self::Pattern(pattern.clone().with_relative(RelativeTo::Parent))
+ Self::Tiling(tiling) => {
+ Self::Tiling(tiling.clone().with_relative(RelativeTo::Parent))
}
}
}
@@ -56,14 +56,14 @@ impl Debug for Paint {
match self {
Self::Solid(v) => v.fmt(f),
Self::Gradient(v) => v.fmt(f),
- Self::Pattern(v) => v.fmt(f),
+ Self::Tiling(v) => v.fmt(f),
}
}
}
-impl From<Pattern> for Paint {
- fn from(pattern: Pattern) -> Self {
- Self::Pattern(pattern)
+impl From<Tiling> for Paint {
+ fn from(tiling: Tiling) -> Self {
+ Self::Tiling(tiling)
}
}
@@ -72,7 +72,7 @@ impl Repr for Paint {
match self {
Self::Solid(color) => color.repr(),
Self::Gradient(gradient) => gradient.repr(),
- Self::Pattern(pattern) => pattern.repr(),
+ Self::Tiling(tiling) => tiling.repr(),
}
}
}
@@ -94,9 +94,9 @@ cast! {
self => match self {
Self::Solid(color) => color.into_value(),
Self::Gradient(gradient) => gradient.into_value(),
- Self::Pattern(pattern) => pattern.into_value(),
+ Self::Tiling(tiling) => tiling.into_value(),
},
color: Color => Self::Solid(color),
gradient: Gradient => Self::Gradient(gradient),
- pattern: Pattern => Self::Pattern(pattern),
+ tiling: Tiling => Self::Tiling(tiling),
}
diff --git a/crates/typst-library/src/visualize/stroke.rs b/crates/typst-library/src/visualize/stroke.rs
index 4ca10920..2ab493a5 100644
--- a/crates/typst-library/src/visualize/stroke.rs
+++ b/crates/typst-library/src/visualize/stroke.rs
@@ -7,7 +7,7 @@ use crate::foundations::{
Resolve, Smart, StyleChain, Value,
};
use crate::layout::{Abs, Length};
-use crate::visualize::{Color, Gradient, Paint, Pattern};
+use crate::visualize::{Color, Gradient, Paint, Tiling};
/// Defines how to draw a line.
///
@@ -213,9 +213,9 @@ impl<T: Numeric> Stroke<T> {
thickness: self.thickness.map(&f),
cap: self.cap,
join: self.join,
- dash: self.dash.map(|pattern| {
- pattern.map(|pattern| DashPattern {
- array: pattern
+ dash: self.dash.map(|dash| {
+ dash.map(|dash| DashPattern {
+ array: dash
.array
.into_iter()
.map(|l| match l {
@@ -223,7 +223,7 @@ impl<T: Numeric> Stroke<T> {
DashLength::LineWidth => DashLength::LineWidth,
})
.collect(),
- phase: f(pattern.phase),
+ phase: f(dash.phase),
})
}),
miter_limit: self.miter_limit,
@@ -237,14 +237,10 @@ impl Stroke<Abs> {
let thickness = self.thickness.unwrap_or(default.thickness);
let dash = self
.dash
- .map(|pattern| {
- pattern.map(|pattern| DashPattern {
- array: pattern
- .array
- .into_iter()
- .map(|l| l.finish(thickness))
- .collect(),
- phase: pattern.phase,
+ .map(|dash| {
+ dash.map(|dash| DashPattern {
+ array: dash.array.into_iter().map(|l| l.finish(thickness)).collect(),
+ phase: dash.phase,
})
})
.unwrap_or(default.dash);
@@ -372,8 +368,8 @@ cast! {
paint: Smart::Custom(gradient.into()),
..Default::default()
},
- pattern: Pattern => Self {
- paint: Smart::Custom(pattern.into()),
+ tiling: Tiling => Self {
+ paint: Smart::Custom(tiling.into()),
..Default::default()
},
mut dict: Dict => {
diff --git a/crates/typst-library/src/visualize/pattern.rs b/crates/typst-library/src/visualize/tiling.rs
index 2017ea65..d699d3b6 100644
--- a/crates/typst-library/src/visualize/pattern.rs
+++ b/crates/typst-library/src/visualize/tiling.rs
@@ -13,18 +13,18 @@ use crate::layout::{Abs, Axes, Frame, Length, Region, Size};
use crate::visualize::RelativeTo;
use crate::World;
-/// A repeating pattern fill.
+/// A repeating tiling fill.
///
-/// Typst supports the most common pattern type of tiled patterns, where a
-/// pattern is repeated in a grid-like fashion, covering the entire area of an
-/// element that is filled or stroked. The pattern is defined by a tile size and
-/// a body defining the content of each cell. You can also add horizontal or
-/// vertical spacing between the cells of the pattern.
+/// Typst supports the most common type of tilings, where a pattern is repeated
+/// in a grid-like fashion, covering the entire area of an element that is
+/// filled or stroked. The pattern is defined by a tile size and a body defining
+/// the content of each cell. You can also add horizontal or vertical spacing
+/// between the cells of the tiling.
///
/// # Examples
///
/// ```example
-/// #let pat = pattern(size: (30pt, 30pt))[
+/// #let pat = tiling(size: (30pt, 30pt))[
/// #place(line(start: (0%, 0%), end: (100%, 100%)))
/// #place(line(start: (0%, 100%), end: (100%, 0%)))
/// ]
@@ -32,14 +32,14 @@ use crate::World;
/// #rect(fill: pat, width: 100%, height: 60pt, stroke: 1pt)
/// ```
///
-/// Patterns are also supported on text, but only when setting the
-/// [relativeness]($pattern.relative) to either `{auto}` (the default value) or
-/// `{"parent"}`. To create word-by-word or glyph-by-glyph patterns, you can
+/// Tilings are also supported on text, but only when setting the
+/// [relativeness]($tiling.relative) to either `{auto}` (the default value) or
+/// `{"parent"}`. To create word-by-word or glyph-by-glyph tilings, you can
/// wrap the words or characters of your text in [boxes]($box) manually or
/// through a [show rule]($styling/#show-rules).
///
/// ```example
-/// #let pat = pattern(
+/// #let pat = tiling(
/// size: (30pt, 30pt),
/// relative: "parent",
/// square(
@@ -54,13 +54,13 @@ use crate::World;
/// ```
///
/// You can also space the elements further or closer apart using the
-/// [`spacing`]($pattern.spacing) feature of the pattern. If the spacing
-/// is lower than the size of the pattern, the pattern will overlap.
-/// If it is higher, the pattern will have gaps of the same color as the
-/// background of the pattern.
+/// [`spacing`]($tiling.spacing) feature of the tiling. If the spacing
+/// is lower than the size of the tiling, the tiling will overlap.
+/// If it is higher, the tiling will have gaps of the same color as the
+/// background of the tiling.
///
/// ```example
-/// #let pat = pattern(
+/// #let pat = tiling(
/// size: (30pt, 30pt),
/// spacing: (10pt, 10pt),
/// relative: "parent",
@@ -79,11 +79,11 @@ use crate::World;
/// ```
///
/// # Relativeness
-/// The location of the starting point of the pattern is dependent on the
+/// The location of the starting point of the tiling is dependent on the
/// dimensions of a container. This container can either be the shape that it is
/// being painted on, or the closest surrounding container. This is controlled
-/// by the `relative` argument of a pattern constructor. By default, patterns
-/// are relative to the shape they are being painted on, unless the pattern is
+/// by the `relative` argument of a tiling constructor. By default, tilings
+/// are relative to the shape they are being painted on, unless the tiling is
/// applied on text, in which case they are relative to the closest ancestor
/// container.
///
@@ -94,29 +94,33 @@ use crate::World;
/// contains the shape. This includes the boxes and blocks that are implicitly
/// created by show rules and elements. For example, a [`rotate`] will not
/// affect the parent of a gradient, but a [`grid`] will.
-#[ty(scope, cast)]
+///
+/// # Compatibility
+/// This type used to be called `pattern`. The name remains as an alias, but is
+/// deprecated since Typst 0.13.
+#[ty(scope, cast, keywords = ["pattern"])]
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
-pub struct Pattern(Arc<Repr>);
+pub struct Tiling(Arc<Repr>);
-/// Internal representation of [`Pattern`].
+/// Internal representation of [`Tiling`].
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
struct Repr {
- /// The pattern's rendered content.
+ /// The tiling's rendered content.
frame: LazyHash<Frame>,
- /// The pattern's tile size.
+ /// The tiling's tile size.
size: Size,
- /// The pattern's tile spacing.
+ /// The tiling's tile spacing.
spacing: Size,
- /// The pattern's relative transform.
+ /// The tiling's relative transform.
relative: Smart<RelativeTo>,
}
#[scope]
-impl Pattern {
- /// Construct a new pattern.
+impl Tiling {
+ /// Construct a new tiling.
///
/// ```example
- /// #let pat = pattern(
+ /// #let pat = tiling(
/// size: (20pt, 20pt),
/// relative: "parent",
/// place(
@@ -136,15 +140,15 @@ impl Pattern {
engine: &mut Engine,
/// The callsite span.
span: Span,
- /// The bounding box of each cell of the pattern.
+ /// The bounding box of each cell of the tiling.
#[named]
#[default(Spanned::new(Smart::Auto, Span::detached()))]
size: Spanned<Smart<Axes<Length>>>,
- /// The spacing between cells of the pattern.
+ /// The spacing between cells of the tiling.
#[named]
#[default(Spanned::new(Axes::splat(Length::zero()), Span::detached()))]
spacing: Spanned<Axes<Length>>,
- /// The [relative placement](#relativeness) of the pattern.
+ /// The [relative placement](#relativeness) of the tiling.
///
/// For an element placed at the root/top level of the document, the
/// parent is the page itself. For other elements, the parent is the
@@ -153,14 +157,14 @@ impl Pattern {
#[named]
#[default(Smart::Auto)]
relative: Smart<RelativeTo>,
- /// The content of each cell of the pattern.
+ /// The content of each cell of the tiling.
body: Content,
- ) -> SourceResult<Pattern> {
+ ) -> SourceResult<Tiling> {
let size_span = size.span;
if let Smart::Custom(size) = size.v {
// Ensure that sizes are absolute.
if !size.x.em.is_zero() || !size.y.em.is_zero() {
- bail!(size_span, "pattern tile size must be absolute");
+ bail!(size_span, "tile size must be absolute");
}
// Ensure that sizes are non-zero and finite.
@@ -169,25 +173,25 @@ impl Pattern {
|| !size.x.is_finite()
|| !size.y.is_finite()
{
- bail!(size_span, "pattern tile size must be non-zero and non-infinite");
+ bail!(size_span, "tile size must be non-zero and non-infinite");
}
}
// Ensure that spacing is absolute.
if !spacing.v.x.em.is_zero() || !spacing.v.y.em.is_zero() {
- bail!(spacing.span, "pattern tile spacing must be absolute");
+ bail!(spacing.span, "tile spacing must be absolute");
}
// Ensure that spacing is finite.
if !spacing.v.x.is_finite() || !spacing.v.y.is_finite() {
- bail!(spacing.span, "pattern tile spacing must be finite");
+ bail!(spacing.span, "tile spacing must be finite");
}
// The size of the frame
let size = size.v.map(|l| l.map(|a| a.abs));
let region = size.unwrap_or_else(|| Axes::splat(Abs::inf()));
- // Layout the pattern.
+ // Layout the tiling.
let world = engine.world;
let library = world.library();
let locator = Locator::root();
@@ -204,7 +208,7 @@ impl Pattern {
// Check that the frame is non-zero.
if frame.width().is_zero() || frame.height().is_zero() {
bail!(
- span, "pattern tile size must be non-zero";
+ span, "tile size must be non-zero";
hint: "try setting the size manually"
);
}
@@ -218,8 +222,8 @@ impl Pattern {
}
}
-impl Pattern {
- /// Set the relative placement of the pattern.
+impl Tiling {
+ /// Set the relative placement of the tiling.
pub fn with_relative(mut self, relative: RelativeTo) -> Self {
if let Some(this) = Arc::get_mut(&mut self.0) {
this.relative = Smart::Custom(relative);
@@ -233,27 +237,27 @@ impl Pattern {
self
}
- /// Return the frame of the pattern.
+ /// Return the frame of the tiling.
pub fn frame(&self) -> &Frame {
&self.0.frame
}
- /// Return the size of the pattern in absolute units.
+ /// Return the size of the tiling in absolute units.
pub fn size(&self) -> Size {
self.0.size
}
- /// Return the spacing of the pattern in absolute units.
+ /// Return the spacing of the tiling in absolute units.
pub fn spacing(&self) -> Size {
self.0.spacing
}
- /// Returns the relative placement of the pattern.
+ /// Returns the relative placement of the tiling.
pub fn relative(&self) -> Smart<RelativeTo> {
self.0.relative
}
- /// Returns the relative placement of the pattern.
+ /// Returns the relative placement of the tiling.
pub fn unwrap_relative(&self, on_text: bool) -> RelativeTo {
self.0.relative.unwrap_or_else(|| {
if on_text {
@@ -265,10 +269,10 @@ impl Pattern {
}
}
-impl repr::Repr for Pattern {
+impl repr::Repr for Tiling {
fn repr(&self) -> EcoString {
let mut out =
- eco_format!("pattern(({}, {})", self.0.size.x.repr(), self.0.size.y.repr());
+ eco_format!("tiling(({}, {})", self.0.size.x.repr(), self.0.size.y.repr());
if self.0.spacing.is_zero() {
out.push_str(", spacing: (");