mirror of
https://github.com/typst/typst
synced 2025-05-22 04:55:29 +08:00
Fix figure detection
This commit is contained in:
parent
88cb8c2626
commit
d14d1e867f
@ -319,8 +319,4 @@ impl LocalName for TableElem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Figurable for TableElem {
|
impl Figurable for TableElem {}
|
||||||
fn priority(&self, _styles: StyleChain) -> isize {
|
|
||||||
-1000
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -178,7 +178,7 @@ impl Synthesize for FigureElem {
|
|||||||
// Determine the figure's kind.
|
// Determine the figure's kind.
|
||||||
let kind = match self.kind(styles) {
|
let kind = match self.kind(styles) {
|
||||||
Smart::Auto => self
|
Smart::Auto => self
|
||||||
.find_figurable(styles)
|
.find_figurable()
|
||||||
.map(|elem| FigureKind::Elem(elem.func()))
|
.map(|elem| FigureKind::Elem(elem.func()))
|
||||||
.unwrap_or_else(|| FigureKind::Elem(ImageElem::func())),
|
.unwrap_or_else(|| FigureKind::Elem(ImageElem::func())),
|
||||||
Smart::Custom(kind) => kind,
|
Smart::Custom(kind) => kind,
|
||||||
@ -324,22 +324,14 @@ impl Refable for FigureElem {
|
|||||||
impl FigureElem {
|
impl FigureElem {
|
||||||
/// Determines the type of the figure by looking at the content, finding all
|
/// Determines the type of the figure by looking at the content, finding all
|
||||||
/// [`Figurable`] elements and sorting them by priority then returning the highest.
|
/// [`Figurable`] elements and sorting them by priority then returning the highest.
|
||||||
pub fn find_figurable(&self, styles: StyleChain) -> Option<Content> {
|
pub fn find_figurable(&self) -> Option<Content> {
|
||||||
self.body()
|
self.body().query_first(Selector::can::<dyn Figurable>()).cloned()
|
||||||
.query(Selector::can::<dyn Figurable>())
|
|
||||||
.into_iter()
|
|
||||||
.max_by_key(|elem| elem.with::<dyn Figurable>().unwrap().priority(styles))
|
|
||||||
.cloned()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Finds the element with the given function in the figure's content.
|
/// Finds the element with the given function in the figure's content.
|
||||||
/// Returns `None` if no element with the given function is found.
|
/// Returns `None` if no element with the given function is found.
|
||||||
pub fn find_of_elem(&self, func: ElemFunc) -> Option<Content> {
|
pub fn find_of_elem(&self, func: ElemFunc) -> Option<Content> {
|
||||||
self.body()
|
self.body().query_first(Selector::Elem(func, None)).cloned()
|
||||||
.query(Selector::Elem(func, None))
|
|
||||||
.into_iter()
|
|
||||||
.next()
|
|
||||||
.cloned()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Builds the supplement and numbering of the figure. Returns [`None`] if
|
/// 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.
|
/// 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
|
/// This trait is used to determine the type of a figure.
|
||||||
/// the figure's content is the figurable descendant with the highest priority.
|
pub trait Figurable: LocalName {}
|
||||||
pub trait Figurable: LocalName {
|
|
||||||
/// The priority of this element.
|
|
||||||
fn priority(&self, styles: StyleChain) -> isize;
|
|
||||||
}
|
|
||||||
|
@ -244,11 +244,7 @@ impl LocalName for RawElem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Figurable for RawElem {
|
impl Figurable for RawElem {}
|
||||||
fn priority(&self, _styles: StyleChain) -> isize {
|
|
||||||
500
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Highlight a syntax node in a theme by calling `f` with ranges and their
|
/// Highlight a syntax node in a theme by calling `f` with ranges and their
|
||||||
/// styles.
|
/// styles.
|
||||||
|
@ -144,11 +144,7 @@ impl LocalName for ImageElem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Figurable for ImageElem {
|
impl Figurable for ImageElem {}
|
||||||
fn priority(&self, _styles: StyleChain) -> isize {
|
|
||||||
1000
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// How an image should adjust itself to a given area.
|
/// How an image should adjust itself to a given area.
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)]
|
||||||
|
@ -373,6 +373,21 @@ impl Content {
|
|||||||
results
|
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.
|
/// Extracts the plain text of this content.
|
||||||
pub fn plain_text(&self) -> EcoString {
|
pub fn plain_text(&self) -> EcoString {
|
||||||
let mut text = EcoString::new();
|
let mut text = EcoString::new();
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 70 KiB After Width: | Height: | Size: 58 KiB |
@ -13,7 +13,7 @@ We can clearly see that @fig-cylinder and
|
|||||||
) <tab-basic>
|
) <tab-basic>
|
||||||
|
|
||||||
#figure(
|
#figure(
|
||||||
pad(y: -11pt, image("/cylinder.svg", height: 3cm)),
|
pad(y: -6pt, image("/cylinder.svg", height: 2cm)),
|
||||||
caption: [The basic shapes.],
|
caption: [The basic shapes.],
|
||||||
numbering: "I",
|
numbering: "I",
|
||||||
) <fig-cylinder>
|
) <fig-cylinder>
|
||||||
@ -25,20 +25,12 @@ We can clearly see that @fig-cylinder and
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
// Testing figures with and without caption
|
// Testing figures with tables.
|
||||||
#figure(
|
|
||||||
table(
|
|
||||||
columns: 2,
|
|
||||||
[First cylinder],
|
|
||||||
image("/cylinder.svg", height: 3cm),
|
|
||||||
)
|
|
||||||
) <fig-image-in-table-no-caption>
|
|
||||||
|
|
||||||
#figure(
|
#figure(
|
||||||
table(
|
table(
|
||||||
columns: 2,
|
columns: 2,
|
||||||
[Second cylinder],
|
[Second cylinder],
|
||||||
image("/cylinder.svg", height: 3cm),
|
image("/cylinder.svg"),
|
||||||
),
|
),
|
||||||
caption: "A table containing images."
|
caption: "A table containing images."
|
||||||
) <fig-image-in-table>
|
) <fig-image-in-table>
|
||||||
@ -76,6 +68,7 @@ We can clearly see that @fig-cylinder and
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#set page(width: 150pt)
|
||||||
#figure(
|
#figure(
|
||||||
$a^2 + b^2 = c^2$,
|
$a^2 + b^2 = c^2$,
|
||||||
supplement: "Theorem",
|
supplement: "Theorem",
|
||||||
@ -93,19 +86,17 @@ We can clearly see that @fig-cylinder and
|
|||||||
) <fig-formula>
|
) <fig-formula>
|
||||||
|
|
||||||
#figure(
|
#figure(
|
||||||
caption: [Hello world in #emph[rust].],
|
|
||||||
)[
|
|
||||||
#show raw: set align(left)
|
|
||||||
```rust
|
```rust
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello!");
|
||||||
}
|
}
|
||||||
```
|
```,
|
||||||
]
|
caption: [Hello world in _rust_],
|
||||||
|
)
|
||||||
|
|
||||||
---
|
---
|
||||||
// Test breakable figures
|
// Test breakable figures
|
||||||
#set page(width: 200pt, height: 6em)
|
#set page(height: 6em)
|
||||||
#show figure: set block(breakable: true)
|
#show figure: set block(breakable: true)
|
||||||
|
|
||||||
#figure(table[a][b][c][d][e], caption: [A table])
|
#figure(table[a][b][c][d][e], caption: [A table])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user