diff --git a/Cargo.lock b/Cargo.lock index 600f25155..569b4d67d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3326,7 +3326,6 @@ dependencies = [ "clap", "comemo 0.4.0", "ecow", - "krilla", "oxipng", "parking_lot", "rayon", diff --git a/crates/typst-pdf/src/convert.rs b/crates/typst-pdf/src/convert.rs index f4f79205f..843786430 100644 --- a/crates/typst-pdf/src/convert.rs +++ b/crates/typst-pdf/src/convert.rs @@ -9,7 +9,8 @@ use krilla::geom::PathBuilder; use krilla::page::{PageLabel, PageSettings}; use krilla::pdf::PdfError; use krilla::surface::Surface; -use krilla::tagging::{TagId, TagTree}; +use krilla::tagging::TagId; +use krilla::tagging::fmt::Output; use krilla::{Document, SerializeSettings}; use krilla_svg::render_svg_glyph; use typst_library::diag::{SourceDiagnostic, SourceResult, bail, error}; @@ -19,7 +20,7 @@ use typst_library::layout::{ Abs, Frame, FrameItem, GroupItem, PagedDocument, Size, Transform, }; use typst_library::model::HeadingElem; -use typst_library::text::Font; +use typst_library::text::{Font, Lang}; use typst_library::visualize::{Geometry, Paint}; use typst_syntax::Span; @@ -55,10 +56,18 @@ pub fn convert( pub fn tag_tree( typst_document: &PagedDocument, options: &PdfOptions, -) -> SourceResult { +) -> SourceResult { let (mut document, mut gc) = setup(typst_document, options); convert_pages(&mut gc, &mut document)?; - Ok(gc.tags.build_tree()) + let mut tree = if let Some(lang) = gc.tags.doc_lang + && lang != Lang::ENGLISH + { + format!("lang: \"{}\"\n---\n", lang.as_str()) + } else { + String::new() + }; + gc.tags.build_tree().output(&mut tree).unwrap(); + Ok(tree) } fn setup<'a>( diff --git a/crates/typst-pdf/src/lib.rs b/crates/typst-pdf/src/lib.rs index acf2ad909..9b745ca07 100644 --- a/crates/typst-pdf/src/lib.rs +++ b/crates/typst-pdf/src/lib.rs @@ -18,7 +18,6 @@ pub use self::metadata::{Timestamp, Timezone}; use std::fmt::{self, Debug, Formatter}; use ecow::eco_format; -use krilla::tagging::TagTree; use serde::{Deserialize, Serialize}; use typst_library::diag::{SourceResult, StrResult, bail}; use typst_library::foundations::Smart; @@ -34,7 +33,10 @@ pub fn pdf(document: &PagedDocument, options: &PdfOptions) -> SourceResult SourceResult { +pub fn pdf_tags( + document: &PagedDocument, + options: &PdfOptions, +) -> SourceResult { convert::tag_tree(document, options) } diff --git a/tests/Cargo.toml b/tests/Cargo.toml index a3cd81030..cb979b6c2 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -25,7 +25,6 @@ default = [ "typst-render", "typst-svg", "typst-svg", - "krilla", ] [dependencies] @@ -40,7 +39,6 @@ typst-library = { workspace = true, optional = true } typst-pdf = { workspace = true, optional = true } typst-render = { workspace = true, optional = true } typst-svg = { workspace = true, optional = true } -krilla = { workspace = true, optional = true } clap = { workspace = true } comemo = { workspace = true } ecow = { workspace = true } diff --git a/tests/ref/pdftags/lang-tags-pars-basic.yml b/tests/ref/pdftags/lang-tags-pars-basic.yml new file mode 100644 index 000000000..a11c265bf --- /dev/null +++ b/tests/ref/pdftags/lang-tags-pars-basic.yml @@ -0,0 +1,13 @@ +lang: "uk" +--- +- Tag: P + /K: + - Content: page=0 mcid=0 +- Tag: P + /Lang: "sr" + /K: + - Content: page=0 mcid=1 +- Tag: P + /Lang: "be" + /K: + - Content: page=0 mcid=2 diff --git a/tests/ref/pdftags/lang-tags-propagation.yml b/tests/ref/pdftags/lang-tags-propagation.yml new file mode 100644 index 000000000..adf1b63a3 --- /dev/null +++ b/tests/ref/pdftags/lang-tags-propagation.yml @@ -0,0 +1,46 @@ +lang: "nl" +--- +- Tag: P + /K: + - Content: page=0 mcid=0 +- Tag: L + /Lang: "de" + /Numbering: Circle + /K: + - Tag: LI + /K: + - Tag: Lbl + /K: + - Content: page=0 mcid=1 + - Tag: LBody + /K: + - Tag: P + /K: + - Content: page=0 mcid=2 + - Tag: L + /Numbering: Circle + /K: + - Tag: LI + /K: + - Tag: Lbl + /K: + - Content: page=0 mcid=3 + - Tag: LBody + /K: + - Content: page=0 mcid=4 + - Tag: LI + /K: + - Tag: Lbl + /K: + - Content: page=0 mcid=5 + - Tag: LBody + /K: + - Content: page=0 mcid=6 + - Tag: LI + /K: + - Tag: Lbl + /K: + - Content: page=0 mcid=7 + - Tag: LBody + /K: + - Content: page=0 mcid=8 diff --git a/tests/ref/pdftags/text-lang.yml b/tests/ref/pdftags/text-lang.yml new file mode 100644 index 000000000..2b4793e20 --- /dev/null +++ b/tests/ref/pdftags/text-lang.yml @@ -0,0 +1,8 @@ +lang: "zh" +--- +- Tag: H1 + /T: "目录" + /K: + - Content: page=0 mcid=0 + - Content: page=0 mcid=1 +- Tag: TOC diff --git a/tests/src/run.rs b/tests/src/run.rs index a1e64c990..5069dcb07 100644 --- a/tests/src/run.rs +++ b/tests/src/run.rs @@ -3,8 +3,6 @@ use std::ops::Range; use std::path::PathBuf; use ecow::eco_vec; -use krilla::tagging::TagTree; -use krilla::tagging::fmt::Output; use tiny_skia as sk; use typst::diag::{SourceDiagnostic, SourceResult, Warned}; use typst::layout::{Abs, Frame, FrameItem, PagedDocument, Transform}; @@ -76,7 +74,7 @@ impl<'a> Runner<'a> { self.run_test::(); } if pdftags { - self.run_test::(); + self.run_test::(); } self.handle_not_emitted(); @@ -518,7 +516,9 @@ impl OutputType for HtmlDocument { } } -impl OutputType for TagTree { +struct Pdftags(String); + +impl OutputType for Pdftags { type Live = String; fn live_path(name: &str) -> PathBuf { @@ -537,16 +537,16 @@ impl OutputType for TagTree { }; let mut options = PdfOptions::default(); options.standards = PdfStandards::new(&[PdfStandard::Ua_1]).unwrap(); - let output = typst_pdf::pdf_tags(&doc, &options); + let output = typst_pdf::pdf_tags(&doc, &options).map(Pdftags); Warned { warnings, output } } fn is_skippable(&self) -> Result { - Ok(self.children.is_empty()) + Ok(self.0.is_empty()) } fn make_live(&self) -> SourceResult { - Ok(self.display().to_string()) + Ok(self.0.clone()) } fn save_live(&self, name: &str, live: &Self::Live) { diff --git a/tests/suite/pdftags/lang.typ b/tests/suite/pdftags/lang.typ new file mode 100644 index 000000000..8a1313fe3 --- /dev/null +++ b/tests/suite/pdftags/lang.typ @@ -0,0 +1,19 @@ +--- lang-tags-pars-basic pdftags --- +#set text(lang: "uk") +Par 1. + +#set text(lang: "sr") +Par 2. + +#set text(lang: "be") +Par 3. + +--- lang-tags-propagation pdftags --- +#set text(lang: "nl") +A paragraph. + +// language attributes are propagated to the parent (L) tag +- #text(lang: "de", "a") + - #text(lang: "de", "b") + - #text(lang: "de", "c") +- #text(lang: "de", "d") diff --git a/tests/suite/text/lang.typ b/tests/suite/text/lang.typ index b4f87310c..96fc41555 100644 --- a/tests/suite/text/lang.typ +++ b/tests/suite/text/lang.typ @@ -1,6 +1,6 @@ // Test setting the document language. ---- text-lang --- +--- text-lang render pdftags --- // without any region #set text(font: "Noto Serif CJK TC", lang: "zh") #outline()