diff --git a/library/src/layout/table.rs b/library/src/layout/table.rs index 29e2edb88..9cd18b562 100644 --- a/library/src/layout/table.rs +++ b/library/src/layout/table.rs @@ -319,8 +319,4 @@ impl LocalName for TableElem { } } -impl Figurable for TableElem { - fn priority(&self, _styles: StyleChain) -> isize { - -1000 - } -} +impl Figurable for TableElem {} diff --git a/library/src/meta/figure.rs b/library/src/meta/figure.rs index 2082701bd..fe4922388 100644 --- a/library/src/meta/figure.rs +++ b/library/src/meta/figure.rs @@ -178,7 +178,7 @@ impl Synthesize for FigureElem { // Determine the figure's kind. let kind = match self.kind(styles) { Smart::Auto => self - .find_figurable(styles) + .find_figurable() .map(|elem| FigureKind::Elem(elem.func())) .unwrap_or_else(|| FigureKind::Elem(ImageElem::func())), Smart::Custom(kind) => kind, @@ -324,22 +324,14 @@ impl Refable for FigureElem { impl FigureElem { /// Determines the type of the figure by looking at the content, finding all /// [`Figurable`] elements and sorting them by priority then returning the highest. - pub fn find_figurable(&self, styles: StyleChain) -> Option { - self.body() - .query(Selector::can::()) - .into_iter() - .max_by_key(|elem| elem.with::().unwrap().priority(styles)) - .cloned() + pub fn find_figurable(&self) -> Option { + self.body().query_first(Selector::can::()).cloned() } /// Finds the element with the given function in the figure's content. /// Returns `None` if no element with the given function is found. pub fn find_of_elem(&self, func: ElemFunc) -> Option { - self.body() - .query(Selector::Elem(func, None)) - .into_iter() - .next() - .cloned() + self.body().query_first(Selector::Elem(func, None)).cloned() } /// Builds the supplement and numbering of the figure. Returns [`None`] if @@ -411,9 +403,5 @@ cast_to_value! { /// An element that can be auto-detected in a figure. /// -/// This trait is used to determine the type of a figure. The element chosen as -/// the figure's content is the figurable descendant with the highest priority. -pub trait Figurable: LocalName { - /// The priority of this element. - fn priority(&self, styles: StyleChain) -> isize; -} +/// This trait is used to determine the type of a figure. +pub trait Figurable: LocalName {} diff --git a/library/src/text/raw.rs b/library/src/text/raw.rs index 4cc26fa67..db4374815 100644 --- a/library/src/text/raw.rs +++ b/library/src/text/raw.rs @@ -244,11 +244,7 @@ impl LocalName for RawElem { } } -impl Figurable for RawElem { - fn priority(&self, _styles: StyleChain) -> isize { - 500 - } -} +impl Figurable for RawElem {} /// Highlight a syntax node in a theme by calling `f` with ranges and their /// styles. diff --git a/library/src/visualize/image.rs b/library/src/visualize/image.rs index 917f483ec..fc6aa6518 100644 --- a/library/src/visualize/image.rs +++ b/library/src/visualize/image.rs @@ -144,11 +144,7 @@ impl LocalName for ImageElem { } } -impl Figurable for ImageElem { - fn priority(&self, _styles: StyleChain) -> isize { - 1000 - } -} +impl Figurable for ImageElem {} /// How an image should adjust itself to a given area. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)] diff --git a/src/model/content.rs b/src/model/content.rs index 4eeecbd6b..71a5cb84b 100644 --- a/src/model/content.rs +++ b/src/model/content.rs @@ -373,6 +373,21 @@ impl Content { results } + /// Queries the content tree for the first element that match the given + /// selector. + /// + /// Elements produced in `show` rules will not be included in the results. + #[tracing::instrument(skip_all)] + pub fn query_first(&self, selector: Selector) -> Option<&Content> { + let mut result = None; + self.traverse(&mut |element| { + if result.is_none() && selector.matches(element) { + result = Some(element); + } + }); + result + } + /// Extracts the plain text of this content. pub fn plain_text(&self) -> EcoString { let mut text = EcoString::new(); diff --git a/tests/ref/meta/figure.png b/tests/ref/meta/figure.png index 593c6438a..524f6cfd7 100644 Binary files a/tests/ref/meta/figure.png and b/tests/ref/meta/figure.png differ diff --git a/tests/typ/meta/figure.typ b/tests/typ/meta/figure.typ index 81db3edf0..fcf927403 100644 --- a/tests/typ/meta/figure.typ +++ b/tests/typ/meta/figure.typ @@ -13,7 +13,7 @@ We can clearly see that @fig-cylinder and ) #figure( - pad(y: -11pt, image("/cylinder.svg", height: 3cm)), + pad(y: -6pt, image("/cylinder.svg", height: 2cm)), caption: [The basic shapes.], numbering: "I", ) @@ -25,20 +25,12 @@ We can clearly see that @fig-cylinder and --- -// Testing figures with and without caption -#figure( - table( - columns: 2, - [First cylinder], - image("/cylinder.svg", height: 3cm), - ) -) - +// Testing figures with tables. #figure( table( columns: 2, [Second cylinder], - image("/cylinder.svg", height: 3cm), + image("/cylinder.svg"), ), caption: "A table containing images." ) @@ -76,6 +68,7 @@ We can clearly see that @fig-cylinder and ) } +#set page(width: 150pt) #figure( $a^2 + b^2 = c^2$, supplement: "Theorem", @@ -93,19 +86,17 @@ We can clearly see that @fig-cylinder and ) #figure( - caption: [Hello world in #emph[rust].], -)[ - #show raw: set align(left) ```rust fn main() { - println!("Hello, world!"); + println!("Hello!"); } - ``` -] + ```, + caption: [Hello world in _rust_], +) --- // Test breakable figures -#set page(width: 200pt, height: 6em) +#set page(height: 6em) #show figure: set block(breakable: true) #figure(table[a][b][c][d][e], caption: [A table])