mirror of
https://github.com/typst/typst
synced 2025-05-18 02:55:28 +08:00
Remove guards for built-in elements
The only recursive built-in show rule was the one for equations and that one was unnecessary.
This commit is contained in:
parent
a3684352ea
commit
6ab04d80f3
@ -159,11 +159,6 @@ impl Content {
|
|||||||
self.inner.guards.contains(&guard)
|
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.
|
/// Whether this content has already been prepared.
|
||||||
pub fn is_prepared(&self) -> bool {
|
pub fn is_prepared(&self) -> bool {
|
||||||
self.inner.prepared
|
self.inner.prepared
|
||||||
|
@ -331,9 +331,4 @@ pub enum Behaviour {
|
|||||||
|
|
||||||
/// Guards content against being affected by the same show rule multiple times.
|
/// Guards content against being affected by the same show rule multiple times.
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
||||||
pub enum Guard {
|
pub struct Guard(pub usize);
|
||||||
/// The nth recipe from the top of the chain.
|
|
||||||
Nth(usize),
|
|
||||||
/// The [base recipe](Show) for a kind of element.
|
|
||||||
Base(Element),
|
|
||||||
}
|
|
||||||
|
@ -3,8 +3,8 @@ use std::num::NonZeroUsize;
|
|||||||
use crate::diag::{bail, SourceResult};
|
use crate::diag::{bail, SourceResult};
|
||||||
use crate::engine::Engine;
|
use crate::engine::Engine;
|
||||||
use crate::foundations::{
|
use crate::foundations::{
|
||||||
elem, Content, Finalize, Guard, NativeElement, Packed, Resolve, Show, Smart,
|
elem, Content, Finalize, NativeElement, Packed, Resolve, Smart, StyleChain,
|
||||||
StyleChain, Synthesize,
|
Synthesize,
|
||||||
};
|
};
|
||||||
use crate::introspection::{Count, Counter, CounterUpdate, Locatable};
|
use crate::introspection::{Count, Counter, CounterUpdate, Locatable};
|
||||||
use crate::layout::{
|
use crate::layout::{
|
||||||
@ -47,7 +47,6 @@ use crate::World;
|
|||||||
#[elem(
|
#[elem(
|
||||||
Locatable,
|
Locatable,
|
||||||
Synthesize,
|
Synthesize,
|
||||||
Show,
|
|
||||||
Finalize,
|
Finalize,
|
||||||
LayoutSingle,
|
LayoutSingle,
|
||||||
LayoutMath,
|
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> {
|
impl Finalize for Packed<EquationElem> {
|
||||||
fn finalize(&self, realized: Content, style: StyleChain) -> Content {
|
fn finalize(&self, realized: Content, style: StyleChain) -> Content {
|
||||||
let mut realized = realized;
|
let mut realized = realized;
|
||||||
|
@ -71,11 +71,7 @@ pub fn realize_block<'a>(
|
|||||||
|
|
||||||
/// Whether the target is affected by show rules in the given style chain.
|
/// Whether the target is affected by show rules in the given style chain.
|
||||||
pub fn applicable(target: &Content, styles: StyleChain) -> bool {
|
pub fn applicable(target: &Content, styles: StyleChain) -> bool {
|
||||||
if target.needs_preparation() {
|
if target.needs_preparation() || target.can::<dyn Show>() {
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if target.can::<dyn Show>() && target.is_pristine() {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +80,7 @@ pub fn applicable(target: &Content, styles: StyleChain) -> bool {
|
|||||||
|
|
||||||
// Find out whether any recipe matches and is unguarded.
|
// Find out whether any recipe matches and is unguarded.
|
||||||
for recipe in styles.recipes() {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
n -= 1;
|
n -= 1;
|
||||||
@ -136,8 +132,8 @@ pub fn realize(
|
|||||||
|
|
||||||
// Find an applicable show rule recipe.
|
// Find an applicable show rule recipe.
|
||||||
for recipe in styles.recipes() {
|
for recipe in styles.recipes() {
|
||||||
let guard = Guard::Nth(n);
|
let guard = Guard(n);
|
||||||
if recipe.applicable(target) && !target.is_guarded(guard) {
|
if !target.is_guarded(guard) && recipe.applicable(target) {
|
||||||
if let Some(content) = try_apply(engine, target, recipe, guard)? {
|
if let Some(content) = try_apply(engine, target, recipe, guard)? {
|
||||||
return Ok(Some(content));
|
return Ok(Some(content));
|
||||||
}
|
}
|
||||||
@ -146,11 +142,8 @@ pub fn realize(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Apply the built-in show rule if there was no matching recipe.
|
// Apply the built-in show rule if there was no matching recipe.
|
||||||
let guard = Guard::Base(target.func());
|
if let Some(showable) = target.with::<dyn Show>() {
|
||||||
if !target.is_guarded(guard) {
|
return Ok(Some(showable.show(engine, styles)?));
|
||||||
if let Some(showable) = target.with::<dyn Show>() {
|
|
||||||
return Ok(Some(showable.show(engine, styles)?));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(None)
|
Ok(None)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user