mirror of
https://github.com/typst/typst
synced 2025-08-03 01:37:54 +08:00
test: add tests for language tag attributes
This commit is contained in:
parent
e816af11f7
commit
71433334b4
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -3326,7 +3326,6 @@ dependencies = [
|
|||||||
"clap",
|
"clap",
|
||||||
"comemo 0.4.0",
|
"comemo 0.4.0",
|
||||||
"ecow",
|
"ecow",
|
||||||
"krilla",
|
|
||||||
"oxipng",
|
"oxipng",
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
"rayon",
|
"rayon",
|
||||||
|
@ -9,7 +9,8 @@ use krilla::geom::PathBuilder;
|
|||||||
use krilla::page::{PageLabel, PageSettings};
|
use krilla::page::{PageLabel, PageSettings};
|
||||||
use krilla::pdf::PdfError;
|
use krilla::pdf::PdfError;
|
||||||
use krilla::surface::Surface;
|
use krilla::surface::Surface;
|
||||||
use krilla::tagging::{TagId, TagTree};
|
use krilla::tagging::TagId;
|
||||||
|
use krilla::tagging::fmt::Output;
|
||||||
use krilla::{Document, SerializeSettings};
|
use krilla::{Document, SerializeSettings};
|
||||||
use krilla_svg::render_svg_glyph;
|
use krilla_svg::render_svg_glyph;
|
||||||
use typst_library::diag::{SourceDiagnostic, SourceResult, bail, error};
|
use typst_library::diag::{SourceDiagnostic, SourceResult, bail, error};
|
||||||
@ -19,7 +20,7 @@ use typst_library::layout::{
|
|||||||
Abs, Frame, FrameItem, GroupItem, PagedDocument, Size, Transform,
|
Abs, Frame, FrameItem, GroupItem, PagedDocument, Size, Transform,
|
||||||
};
|
};
|
||||||
use typst_library::model::HeadingElem;
|
use typst_library::model::HeadingElem;
|
||||||
use typst_library::text::Font;
|
use typst_library::text::{Font, Lang};
|
||||||
use typst_library::visualize::{Geometry, Paint};
|
use typst_library::visualize::{Geometry, Paint};
|
||||||
use typst_syntax::Span;
|
use typst_syntax::Span;
|
||||||
|
|
||||||
@ -55,10 +56,18 @@ pub fn convert(
|
|||||||
pub fn tag_tree(
|
pub fn tag_tree(
|
||||||
typst_document: &PagedDocument,
|
typst_document: &PagedDocument,
|
||||||
options: &PdfOptions,
|
options: &PdfOptions,
|
||||||
) -> SourceResult<TagTree> {
|
) -> SourceResult<String> {
|
||||||
let (mut document, mut gc) = setup(typst_document, options);
|
let (mut document, mut gc) = setup(typst_document, options);
|
||||||
convert_pages(&mut gc, &mut document)?;
|
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>(
|
fn setup<'a>(
|
||||||
|
@ -18,7 +18,6 @@ pub use self::metadata::{Timestamp, Timezone};
|
|||||||
use std::fmt::{self, Debug, Formatter};
|
use std::fmt::{self, Debug, Formatter};
|
||||||
|
|
||||||
use ecow::eco_format;
|
use ecow::eco_format;
|
||||||
use krilla::tagging::TagTree;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use typst_library::diag::{SourceResult, StrResult, bail};
|
use typst_library::diag::{SourceResult, StrResult, bail};
|
||||||
use typst_library::foundations::Smart;
|
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.
|
/// Generate the document tag tree and display it in a human readable form.
|
||||||
#[doc(hidden)]
|
#[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)
|
convert::tag_tree(document, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,7 +25,6 @@ default = [
|
|||||||
"typst-render",
|
"typst-render",
|
||||||
"typst-svg",
|
"typst-svg",
|
||||||
"typst-svg",
|
"typst-svg",
|
||||||
"krilla",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
@ -40,7 +39,6 @@ typst-library = { workspace = true, optional = true }
|
|||||||
typst-pdf = { workspace = true, optional = true }
|
typst-pdf = { workspace = true, optional = true }
|
||||||
typst-render = { workspace = true, optional = true }
|
typst-render = { workspace = true, optional = true }
|
||||||
typst-svg = { workspace = true, optional = true }
|
typst-svg = { workspace = true, optional = true }
|
||||||
krilla = { workspace = true, optional = true }
|
|
||||||
clap = { workspace = true }
|
clap = { workspace = true }
|
||||||
comemo = { workspace = true }
|
comemo = { workspace = true }
|
||||||
ecow = { workspace = true }
|
ecow = { workspace = true }
|
||||||
|
13
tests/ref/pdftags/lang-tags-pars-basic.yml
Normal file
13
tests/ref/pdftags/lang-tags-pars-basic.yml
Normal 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
|
46
tests/ref/pdftags/lang-tags-propagation.yml
Normal file
46
tests/ref/pdftags/lang-tags-propagation.yml
Normal 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
|
8
tests/ref/pdftags/text-lang.yml
Normal file
8
tests/ref/pdftags/text-lang.yml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
lang: "zh"
|
||||||
|
---
|
||||||
|
- Tag: H1
|
||||||
|
/T: "目录"
|
||||||
|
/K:
|
||||||
|
- Content: page=0 mcid=0
|
||||||
|
- Content: page=0 mcid=1
|
||||||
|
- Tag: TOC
|
@ -3,8 +3,6 @@ use std::ops::Range;
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use ecow::eco_vec;
|
use ecow::eco_vec;
|
||||||
use krilla::tagging::TagTree;
|
|
||||||
use krilla::tagging::fmt::Output;
|
|
||||||
use tiny_skia as sk;
|
use tiny_skia as sk;
|
||||||
use typst::diag::{SourceDiagnostic, SourceResult, Warned};
|
use typst::diag::{SourceDiagnostic, SourceResult, Warned};
|
||||||
use typst::layout::{Abs, Frame, FrameItem, PagedDocument, Transform};
|
use typst::layout::{Abs, Frame, FrameItem, PagedDocument, Transform};
|
||||||
@ -76,7 +74,7 @@ impl<'a> Runner<'a> {
|
|||||||
self.run_test::<HtmlDocument>();
|
self.run_test::<HtmlDocument>();
|
||||||
}
|
}
|
||||||
if pdftags {
|
if pdftags {
|
||||||
self.run_test::<TagTree>();
|
self.run_test::<Pdftags>();
|
||||||
}
|
}
|
||||||
|
|
||||||
self.handle_not_emitted();
|
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;
|
type Live = String;
|
||||||
|
|
||||||
fn live_path(name: &str) -> PathBuf {
|
fn live_path(name: &str) -> PathBuf {
|
||||||
@ -537,16 +537,16 @@ impl OutputType for TagTree {
|
|||||||
};
|
};
|
||||||
let mut options = PdfOptions::default();
|
let mut options = PdfOptions::default();
|
||||||
options.standards = PdfStandards::new(&[PdfStandard::Ua_1]).unwrap();
|
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 }
|
Warned { warnings, output }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_skippable(&self) -> Result<bool, ()> {
|
fn is_skippable(&self) -> Result<bool, ()> {
|
||||||
Ok(self.children.is_empty())
|
Ok(self.0.is_empty())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_live(&self) -> SourceResult<Self::Live> {
|
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) {
|
fn save_live(&self, name: &str, live: &Self::Live) {
|
||||||
|
19
tests/suite/pdftags/lang.typ
Normal file
19
tests/suite/pdftags/lang.typ
Normal 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")
|
@ -1,6 +1,6 @@
|
|||||||
// Test setting the document language.
|
// Test setting the document language.
|
||||||
|
|
||||||
--- text-lang ---
|
--- text-lang render pdftags ---
|
||||||
// without any region
|
// without any region
|
||||||
#set text(font: "Noto Serif CJK TC", lang: "zh")
|
#set text(font: "Noto Serif CJK TC", lang: "zh")
|
||||||
#outline()
|
#outline()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user