Compare commits

...

5 Commits

Author SHA1 Message Date
Tobias Schmitz
1780a28c70
docs: [no ci] fixup some comments 2025-06-25 17:47:36 +02:00
Tobias Schmitz
19697b3cad
feat: [no ci] mark RepeatElem as artifact 2025-06-25 17:46:36 +02:00
Tobias Schmitz
652119163e
fix: [no ci] mark table gutter and fill as artifacts 2025-06-25 17:39:50 +02:00
Tobias Schmitz
cdd1a0a732
feat: always write alt text in marked content sequence for images 2025-06-25 16:43:31 +02:00
Tobias Schmitz
43f9ee3960
feat: [no ci] add cli args for PDF/UA-1 standard and to disable tagging 2025-06-25 16:22:08 +02:00
8 changed files with 76 additions and 32 deletions

View File

@ -246,6 +246,13 @@ pub struct CompileArgs {
#[arg(long = "pdf-standard", value_delimiter = ',')]
pub pdf_standard: Vec<PdfStandard>,
/// By default, even when not producing a `PDF/UA-1` document, a tagged PDF
/// document is written to provide a baseline of accessibility. In some
/// circumstances (for example when trying to reduce the size of a document)
/// it can be desirable to disable tagged PDF.
#[arg(long = "disable-pdf-tags")]
pub disable_pdf_tags: bool,
/// The PPI (pixels per inch) to use for PNG export.
#[arg(long = "ppi", default_value_t = 144.0)]
pub ppi: f32,
@ -506,6 +513,9 @@ pub enum PdfStandard {
/// PDF/A-4e.
#[value(name = "a-4e")]
A_4e,
/// PDF/UA-1.
#[value(name = "ua-1")]
Ua_1,
}
display_possible_values!(PdfStandard);

View File

@ -65,6 +65,8 @@ pub struct CompileConfig {
pub open: Option<Option<String>>,
/// A list of standards the PDF should conform to.
pub pdf_standards: PdfStandards,
/// Whether to write PDF (accessibility) tags.
pub disable_pdf_tags: bool,
/// A path to write a Makefile rule describing the current compilation.
pub make_deps: Option<PathBuf>,
/// The PPI (pixels per inch) to use for PNG export.
@ -150,6 +152,7 @@ impl CompileConfig {
output_format,
pages,
pdf_standards,
disable_pdf_tags: args.disable_pdf_tags,
creation_timestamp: args.world.creation_timestamp,
make_deps: args.make_deps.clone(),
ppi: args.ppi,
@ -291,6 +294,7 @@ fn export_pdf(document: &PagedDocument, config: &CompileConfig) -> SourceResult<
timestamp,
page_ranges: config.pages.clone(),
standards: config.pdf_standards.clone(),
disable_tags: config.disable_pdf_tags,
};
let buffer = typst_pdf::pdf(document, &options)?;
config
@ -773,6 +777,7 @@ impl From<PdfStandard> for typst_pdf::PdfStandard {
PdfStandard::A_4 => typst_pdf::PdfStandard::A_4,
PdfStandard::A_4f => typst_pdf::PdfStandard::A_4f,
PdfStandard::A_4e => typst_pdf::PdfStandard::A_4e,
PdfStandard::Ua_1 => typst_pdf::PdfStandard::Ua_1,
}
}
}

View File

@ -25,7 +25,6 @@ use crate::layout::{BlockElem, Length};
/// Berlin, the 22nd of December, 2022
/// ]
/// ```
// TODO: should this be a PDF artifact by deafult?
#[elem(Locatable, Show)]
pub struct RepeatElem {
/// The content to repeat.

View File

@ -192,10 +192,11 @@ pub enum TableHeaderScope {
/// TODO: maybe generalize this and use it to mark html elements with `aria-hidden="true"`?
#[elem(Locatable, Show)]
pub struct ArtifactElem {
/// The artifact kind.
#[default(ArtifactKind::Other)]
pub kind: ArtifactKind,
/// The content to underline.
/// The content that is an artifact.
#[required]
pub body: Content,
}

View File

@ -39,17 +39,14 @@ pub fn convert(
typst_document: &PagedDocument,
options: &PdfOptions,
) -> SourceResult<Vec<u8>> {
// HACK
let config = Configuration::new_with_validator(Validator::UA1);
let settings = SerializeSettings {
compress_content_streams: false, // true,
compress_content_streams: true,
no_device_cs: true,
ascii_compatible: true, // false,
ascii_compatible: false,
xmp_metadata: true,
cmyk_profile: None,
configuration: config, // options.standards.config,
// TODO: allow opting out of tagging PDFs
enable_tagging: true,
configuration: options.standards.config,
enable_tagging: options.disable_tags,
render_svg_glyph_fn: render_svg_glyph,
};

View File

@ -4,6 +4,7 @@ use std::sync::{Arc, OnceLock};
use image::{DynamicImage, EncodableLayout, GenericImageView, Rgba};
use krilla::image::{BitsPerComponent, CustomImage, ImageColorspace};
use krilla::surface::Surface;
use krilla::tagging::SpanTag;
use krilla_svg::{SurfaceExt, SvgSettings};
use typst_library::diag::{bail, SourceResult};
use typst_library::foundations::Smart;
@ -33,7 +34,8 @@ pub(crate) fn handle_image(
gc.image_spans.insert(span);
let mut handle = tags::start_marked(gc, surface);
let mut handle =
tags::start_span(gc, surface, SpanTag::empty().with_alt_text(image.alt()));
let surface = handle.surface();
match image.kind() {
ImageKind::Raster(raster) => {

View File

@ -54,6 +54,11 @@ pub struct PdfOptions<'a> {
pub page_ranges: Option<PageRanges>,
/// A list of PDF standards that Typst will enforce conformance with.
pub standards: PdfStandards,
/// By default, even when not producing a `PDF/UA-1` document, a tagged PDF
/// document is written to provide a baseline of accessibility. In some
/// circumstances, for example when trying to reduce the size of a document,
/// it can be desirable to disable tagged PDF.
pub disable_tags: bool,
}
/// Encapsulates a list of compatible PDF standards.
@ -105,6 +110,7 @@ impl PdfStandards {
PdfStandard::A_4 => set_validator(Validator::A4)?,
PdfStandard::A_4f => set_validator(Validator::A4F)?,
PdfStandard::A_4e => set_validator(Validator::A4E)?,
PdfStandard::Ua_1 => set_validator(Validator::UA1)?,
}
}
@ -188,4 +194,7 @@ pub enum PdfStandard {
/// PDF/A-4e.
#[serde(rename = "a-4e")]
A_4e,
/// PDF/UA-1.
#[serde(rename = "ua-1")]
Ua_1,
}

View File

@ -5,14 +5,15 @@ use ecow::EcoString;
use krilla::page::Page;
use krilla::surface::Surface;
use krilla::tagging::{
ArtifactType, ContentTag, Identifier, Node, TableCellSpan, TableDataCell,
ArtifactType, ContentTag, Identifier, Node, SpanTag, TableCellSpan, TableDataCell,
TableHeaderCell, TableHeaderScope, Tag, TagBuilder, TagGroup, TagKind, TagTree,
};
use typst_library::foundations::{Content, LinkMarker, Packed, StyleChain};
use typst_library::introspection::Location;
use typst_library::layout::RepeatElem;
use typst_library::model::{
Destination, FigureCaption, FigureElem, HeadingElem, Outlinable, OutlineElem,
OutlineEntry, TableCell, TableElem, TableHLine, TableVLine,
OutlineEntry, TableCell, TableElem,
};
use typst_library::pdf::{ArtifactElem, ArtifactKind, PdfTagElem, PdfTagKind};
use typst_library::visualize::ImageElem;
@ -156,16 +157,16 @@ impl Tags {
/// Returns the current parent's list of children and the structure type ([Tag]).
/// In case of the document root the structure type will be `None`.
pub(crate) fn parent(&mut self) -> (Option<&mut StackEntryKind>, &mut Vec<TagNode>) {
if let Some(entry) = self.stack.last_mut() {
(Some(&mut entry.kind), &mut entry.nodes)
} else {
(None, &mut self.tree)
}
pub(crate) fn parent(&mut self) -> Option<&mut StackEntryKind> {
self.stack.last_mut().map(|e| &mut e.kind)
}
pub(crate) fn push(&mut self, node: TagNode) {
self.parent().1.push(node);
if let Some(entry) = self.stack.last_mut() {
entry.nodes.push(node);
} else {
self.tree.push(node);
}
}
pub(crate) fn build_tree(&mut self) -> TagTree {
@ -224,12 +225,34 @@ impl<'a> TagHandle<'a, '_> {
pub(crate) fn start_marked<'a, 'b>(
gc: &mut GlobalContext,
surface: &'b mut Surface<'a>,
) -> TagHandle<'a, 'b> {
start_content(gc, surface, ContentTag::Other)
}
/// Returns a [`TagHandle`] that automatically calls [`Surface::end_tagged`]
/// when dropped.
pub(crate) fn start_span<'a, 'b>(
gc: &mut GlobalContext,
surface: &'b mut Surface<'a>,
span: SpanTag,
) -> TagHandle<'a, 'b> {
start_content(gc, surface, ContentTag::Span(span))
}
fn start_content<'a, 'b>(
gc: &mut GlobalContext,
surface: &'b mut Surface<'a>,
content: ContentTag,
) -> TagHandle<'a, 'b> {
let content = if let Some((_, kind)) = gc.tags.in_artifact {
let ty = artifact_type(kind);
ContentTag::Artifact(ty)
} else if let Some(StackEntryKind::Table(_)) = gc.tags.stack.last().map(|e| &e.kind) {
// Mark any direct child of a table as an aritfact. Any real content
// will be wrapped inside a `TableCell`.
ContentTag::Artifact(ArtifactType::Other)
} else {
ContentTag::Other
content
};
let id = surface.start_tagged(content);
gc.tags.push(TagNode::Leaf(id));
@ -265,6 +288,9 @@ pub(crate) fn handle_start(gc: &mut GlobalContext, elem: &Content) {
let kind = artifact.kind(StyleChain::default());
start_artifact(gc, loc, kind);
return;
} else if let Some(_) = elem.to_packed::<RepeatElem>() {
start_artifact(gc, loc, ArtifactKind::Other);
return;
}
let tag: Tag = if let Some(pdf_tag) = elem.to_packed::<PdfTagElem>() {
@ -295,13 +321,14 @@ pub(crate) fn handle_start(gc: &mut GlobalContext, elem: &Content) {
} else if let Some(image) = elem.to_packed::<ImageElem>() {
let alt = image.alt(StyleChain::default()).map(|s| s.to_string());
let figure_tag = (gc.tags.parent().0)
.and_then(|parent| parent.as_standard_mut())
.filter(|tag| tag.kind == TagKind::Figure && tag.alt_text.is_none());
let figure_tag = (gc.tags.parent())
.and_then(StackEntryKind::as_standard_mut)
.filter(|tag| tag.kind == TagKind::Figure);
if let Some(figure_tag) = figure_tag {
// HACK: set alt text of outer figure tag, if the contained image
// has alt text specified
figure_tag.alt_text = alt;
// Set alt text of outer figure tag, if not present.
if figure_tag.alt_text.is_none() {
figure_tag.alt_text = alt;
}
return;
} else {
TagKind::Figure.with_alt_text(alt)
@ -319,12 +346,6 @@ pub(crate) fn handle_start(gc: &mut GlobalContext, elem: &Content) {
} else if let Some(cell) = elem.to_packed::<TableCell>() {
push_stack(gc, loc, StackEntryKind::TableCell(cell.clone()));
return;
} else if let Some(_) = elem.to_packed::<TableHLine>() {
start_artifact(gc, loc, ArtifactKind::Other);
return;
} else if let Some(_) = elem.to_packed::<TableVLine>() {
start_artifact(gc, loc, ArtifactKind::Other);
return;
} else {
return;
};