test: add tests for language tag attributes

This commit is contained in:
Tobias Schmitz 2025-08-01 11:14:59 +02:00
parent e816af11f7
commit 71433334b4
No known key found for this signature in database
10 changed files with 111 additions and 17 deletions

1
Cargo.lock generated
View File

@ -3326,7 +3326,6 @@ dependencies = [
"clap",
"comemo 0.4.0",
"ecow",
"krilla",
"oxipng",
"parking_lot",
"rayon",

View File

@ -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<TagTree> {
) -> SourceResult<String> {
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>(

View File

@ -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<Vec<u
/// Generate the document tag tree and display it in a human readable form.
#[doc(hidden)]
pub fn pdf_tags(document: &PagedDocument, options: &PdfOptions) -> SourceResult<TagTree> {
pub fn pdf_tags(
document: &PagedDocument,
options: &PdfOptions,
) -> SourceResult<String> {
convert::tag_tree(document, options)
}

View File

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

View File

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

View File

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

View File

@ -0,0 +1,8 @@
lang: "zh"
---
- Tag: H1
/T: "目录"
/K:
- Content: page=0 mcid=0
- Content: page=0 mcid=1
- Tag: TOC

View File

@ -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::<HtmlDocument>();
}
if pdftags {
self.run_test::<TagTree>();
self.run_test::<Pdftags>();
}
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<bool, ()> {
Ok(self.children.is_empty())
Ok(self.0.is_empty())
}
fn make_live(&self) -> SourceResult<Self::Live> {
Ok(self.display().to_string())
Ok(self.0.clone())
}
fn save_live(&self, name: &str, live: &Self::Live) {

View File

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

View File

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