summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--library/src/layout/table.rs6
-rw-r--r--library/src/meta/figure.rs24
-rw-r--r--library/src/text/raw.rs6
-rw-r--r--library/src/visualize/image.rs6
-rw-r--r--src/model/content.rs15
-rw-r--r--tests/ref/meta/figure.pngbin71757 -> 59579 bytes
-rw-r--r--tests/typ/meta/figure.typ27
7 files changed, 33 insertions, 51 deletions
diff --git a/library/src/layout/table.rs b/library/src/layout/table.rs
index 29e2edb8..9cd18b56 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 2082701b..fe492238 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<Content> {
- self.body()
- .query(Selector::can::<dyn Figurable>())
- .into_iter()
- .max_by_key(|elem| elem.with::<dyn Figurable>().unwrap().priority(styles))
- .cloned()
+ pub fn find_figurable(&self) -> Option<Content> {
+ self.body().query_first(Selector::can::<dyn Figurable>()).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<Content> {
- 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 4cc26fa6..db437481 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 917f483e..fc6aa651 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 4eeecbd6..71a5cb84 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 593c6438..524f6cfd 100644
--- a/tests/ref/meta/figure.png
+++ b/tests/ref/meta/figure.png
Binary files differ
diff --git a/tests/typ/meta/figure.typ b/tests/typ/meta/figure.typ
index 81db3edf..fcf92740 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
) <tab-basic>
#figure(
- pad(y: -11pt, image("/cylinder.svg", height: 3cm)),
+ pad(y: -6pt, image("/cylinder.svg", height: 2cm)),
caption: [The basic shapes.],
numbering: "I",
) <fig-cylinder>
@@ -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),
- )
-) <fig-image-in-table-no-caption>
-
+// Testing figures with tables.
#figure(
table(
columns: 2,
[Second cylinder],
- image("/cylinder.svg", height: 3cm),
+ image("/cylinder.svg"),
),
caption: "A table containing images."
) <fig-image-in-table>
@@ -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
) <fig-formula>
#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])