feat: add alt field to figure

This commit is contained in:
Tobias Schmitz 2025-07-23 15:21:35 +02:00
parent 3909bdae6b
commit 042330cd66
No known key found for this signature in database
4 changed files with 15 additions and 7 deletions

View File

@ -282,7 +282,8 @@ const HEADING_RULE: ShowFn<HeadingElem> = |elem, engine, styles| {
const FIGURE_RULE: ShowFn<FigureElem> = |elem, _, styles| {
let span = elem.span();
let mut realized = PdfMarkerTag::FigureBody(elem.body.clone());
let mut realized =
PdfMarkerTag::FigureBody(elem.alt.get_cloned(styles), elem.body.clone());
// Build the caption, if any.
if let Some(caption) = elem.caption.get_cloned(styles) {

View File

@ -103,6 +103,9 @@ use crate::visualize::ImageElem;
/// ```
#[elem(scope, Locatable, Synthesize, Count, ShowSet, Refable, Outlinable)]
pub struct FigureElem {
/// An alternative description of the figure.
pub alt: Option<EcoString>,
/// The content of the figure. Often, an [image].
#[required]
pub body: Content,

View File

@ -1,5 +1,6 @@
use std::num::NonZeroU32;
use ecow::EcoString;
use typst_macros::{Cast, elem, func};
use typst_utils::NonZeroExt;
@ -118,8 +119,8 @@ impl Construct for PdfMarkerTag {
}
macro_rules! pdf_marker_tag {
($(#[doc = $doc:expr] $variant:ident$(($($name:ident: $ty:ident)+))?,)+) => {
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
($(#[doc = $doc:expr] $variant:ident$(($($name:ident: $ty:ty)+))?,)+) => {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PdfMarkerTagKind {
$(
#[doc = $doc]
@ -147,7 +148,7 @@ pdf_marker_tag! {
/// `TOC`
OutlineBody,
/// `Figure`
FigureBody,
FigureBody(alt: Option<EcoString>),
/// `L` bibliography list
Bibliography(numbered: bool),
/// `LBody` wrapping `BibEntry`

View File

@ -59,15 +59,18 @@ pub(crate) fn handle_start(
}
let mut tag: TagKind = if let Some(tag) = elem.to_packed::<PdfMarkerTag>() {
match tag.kind {
match &tag.kind {
PdfMarkerTagKind::OutlineBody => {
push_stack(gc, loc, StackEntryKind::Outline(OutlineCtx::new()))?;
return Ok(());
}
PdfMarkerTagKind::FigureBody => Tag::Figure(None).into(),
PdfMarkerTagKind::FigureBody(alt) => {
let alt = alt.as_ref().map(|s| s.to_string());
Tag::Figure(alt).into()
}
PdfMarkerTagKind::Bibliography(numbered) => {
let numbering =
if numbered { ListNumbering::Decimal } else { ListNumbering::None };
if *numbered { ListNumbering::Decimal } else { ListNumbering::None };
push_stack(gc, loc, StackEntryKind::List(ListCtx::new(numbering)))?;
return Ok(());
}