Fix figure detection

This commit is contained in:
Laurenz 2023-05-17 12:55:25 +02:00
parent 88cb8c2626
commit d14d1e867f
7 changed files with 33 additions and 51 deletions

View File

@ -319,8 +319,4 @@ impl LocalName for TableElem {
}
}
impl Figurable for TableElem {
fn priority(&self, _styles: StyleChain) -> isize {
-1000
}
}
impl Figurable for TableElem {}

View File

@ -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 {}

View File

@ -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.

View File

@ -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)]

View File

@ -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();

Binary file not shown.

Before

Width:  |  Height:  |  Size: 70 KiB

After

Width:  |  Height:  |  Size: 58 KiB

View File

@ -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])