summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-01-24 15:55:13 +0100
committerLaurenz <laurmaedje@gmail.com>2024-01-24 16:29:20 +0100
commit6ab04d80f355e763b70d8d8998d18d589bad2e94 (patch)
tree3c993d9a245535262eddfbf6cc161f7e1de9f3fe
parenta3684352ea3a09f5dd2587305f202d530d0b892f (diff)
Remove guards for built-in elements
The only recursive built-in show rule was the one for equations and that one was unnecessary.
-rw-r--r--crates/typst/src/foundations/content.rs5
-rw-r--r--crates/typst/src/foundations/element.rs7
-rw-r--r--crates/typst/src/math/equation.rs16
-rw-r--r--crates/typst/src/realize/mod.rs19
4 files changed, 9 insertions, 38 deletions
diff --git a/crates/typst/src/foundations/content.rs b/crates/typst/src/foundations/content.rs
index 582fd9c5..16ab0c04 100644
--- a/crates/typst/src/foundations/content.rs
+++ b/crates/typst/src/foundations/content.rs
@@ -159,11 +159,6 @@ impl Content {
self.inner.guards.contains(&guard)
}
- /// Whether no show rule was executed for this content so far.
- pub fn is_pristine(&self) -> bool {
- self.inner.guards.is_empty()
- }
-
/// Whether this content has already been prepared.
pub fn is_prepared(&self) -> bool {
self.inner.prepared
diff --git a/crates/typst/src/foundations/element.rs b/crates/typst/src/foundations/element.rs
index 665aa8ef..dfe8ddb5 100644
--- a/crates/typst/src/foundations/element.rs
+++ b/crates/typst/src/foundations/element.rs
@@ -331,9 +331,4 @@ pub enum Behaviour {
/// Guards content against being affected by the same show rule multiple times.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
-pub enum Guard {
- /// The nth recipe from the top of the chain.
- Nth(usize),
- /// The [base recipe](Show) for a kind of element.
- Base(Element),
-}
+pub struct Guard(pub usize);
diff --git a/crates/typst/src/math/equation.rs b/crates/typst/src/math/equation.rs
index 33f8a23a..227fdc3f 100644
--- a/crates/typst/src/math/equation.rs
+++ b/crates/typst/src/math/equation.rs
@@ -3,8 +3,8 @@ use std::num::NonZeroUsize;
use crate::diag::{bail, SourceResult};
use crate::engine::Engine;
use crate::foundations::{
- elem, Content, Finalize, Guard, NativeElement, Packed, Resolve, Show, Smart,
- StyleChain, Synthesize,
+ elem, Content, Finalize, NativeElement, Packed, Resolve, Smart, StyleChain,
+ Synthesize,
};
use crate::introspection::{Count, Counter, CounterUpdate, Locatable};
use crate::layout::{
@@ -47,7 +47,6 @@ use crate::World;
#[elem(
Locatable,
Synthesize,
- Show,
Finalize,
LayoutSingle,
LayoutMath,
@@ -120,17 +119,6 @@ impl Synthesize for Packed<EquationElem> {
}
}
-impl Show for Packed<EquationElem> {
- #[typst_macros::time(name = "math.equation", span = self.span())]
- fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- let mut realized = self.clone().pack().guarded(Guard::Base(EquationElem::elem()));
- if self.block(styles) {
- realized = AlignElem::new(realized).pack().spanned(self.span());
- }
- Ok(realized)
- }
-}
-
impl Finalize for Packed<EquationElem> {
fn finalize(&self, realized: Content, style: StyleChain) -> Content {
let mut realized = realized;
diff --git a/crates/typst/src/realize/mod.rs b/crates/typst/src/realize/mod.rs
index ff191d80..cd40ac86 100644
--- a/crates/typst/src/realize/mod.rs
+++ b/crates/typst/src/realize/mod.rs
@@ -71,11 +71,7 @@ pub fn realize_block<'a>(
/// Whether the target is affected by show rules in the given style chain.
pub fn applicable(target: &Content, styles: StyleChain) -> bool {
- if target.needs_preparation() {
- return true;
- }
-
- if target.can::<dyn Show>() && target.is_pristine() {
+ if target.needs_preparation() || target.can::<dyn Show>() {
return true;
}
@@ -84,7 +80,7 @@ pub fn applicable(target: &Content, styles: StyleChain) -> bool {
// Find out whether any recipe matches and is unguarded.
for recipe in styles.recipes() {
- if recipe.applicable(target) && !target.is_guarded(Guard::Nth(n)) {
+ if !target.is_guarded(Guard(n)) && recipe.applicable(target) {
return true;
}
n -= 1;
@@ -136,8 +132,8 @@ pub fn realize(
// Find an applicable show rule recipe.
for recipe in styles.recipes() {
- let guard = Guard::Nth(n);
- if recipe.applicable(target) && !target.is_guarded(guard) {
+ let guard = Guard(n);
+ if !target.is_guarded(guard) && recipe.applicable(target) {
if let Some(content) = try_apply(engine, target, recipe, guard)? {
return Ok(Some(content));
}
@@ -146,11 +142,8 @@ pub fn realize(
}
// Apply the built-in show rule if there was no matching recipe.
- let guard = Guard::Base(target.func());
- if !target.is_guarded(guard) {
- if let Some(showable) = target.with::<dyn Show>() {
- return Ok(Some(showable.show(engine, styles)?));
- }
+ if let Some(showable) = target.with::<dyn Show>() {
+ return Ok(Some(showable.show(engine, styles)?));
}
Ok(None)