mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 13:33:51 +00:00
Merge pull request #8542 from Hassan1305/feat-3644-pdf-lang
PDF export: write the document language as the catalog's /Lang
This commit is contained in:
@@ -220,6 +220,13 @@ pub fn language_quotes(language: &str) -> Option<Quotes> {
|
||||
LANGUAGES.iter().find(|(l, _, _)| l.eq_ignore_ascii_case(language)).map(|(_, _, q)| *q)
|
||||
}
|
||||
|
||||
/// The BCP 47 tag of the [`LANGUAGES`] entry named or tagged `language` (case-insensitive), the
|
||||
/// default's included (`en-US`): what a document's language ([`DocSetup::language`]) is written
|
||||
/// as in an exported file. `None` for a language not in the list.
|
||||
pub fn language_tag(language: &str) -> Option<&'static str> {
|
||||
LANGUAGES.iter().find(|(n, t, _)| n.eq_ignore_ascii_case(language) || t.eq_ignore_ascii_case(language)).map(|(_, t, _)| *t)
|
||||
}
|
||||
|
||||
/// The [`LANGUAGES`] entry named or tagged `language` (case-insensitive) as a character's
|
||||
/// language ([`crate::CharStyle::language`]) stores it: its tag, or `None` for the default (the
|
||||
/// first entry); `Err` for a language not in the list.
|
||||
@@ -404,6 +411,16 @@ impl DocSetup {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A document's language is written by its tag, the default's included (#3644).
|
||||
#[test]
|
||||
fn document_languages_have_their_tags() {
|
||||
assert_eq!(language_tag(&DocSetup::default().language), Some("en-US"));
|
||||
assert_eq!(language_tag("French"), Some("fr"));
|
||||
assert_eq!(language_tag("german: swiss"), Some("de-CH"));
|
||||
assert_eq!(language_tag("sr-latn"), Some("sr-Latn"));
|
||||
assert_eq!((language_tag("Klingon"), language_tag("")), (None, None));
|
||||
}
|
||||
|
||||
/// Characters store a language of the list by its tag, the default as none.
|
||||
#[test]
|
||||
fn char_languages_are_stored_by_tag() {
|
||||
|
||||
@@ -64,8 +64,8 @@ pub(crate) struct Sheet<'a> {
|
||||
|
||||
impl Writer {
|
||||
/// A PDF with `set`'s version, standard, compression and colour output, and `doc`'s metadata
|
||||
/// under `title`, created at `created` (Unix seconds). With `cmyk`, transparency blends in
|
||||
/// CMYK (unless the colours are converted to RGB).
|
||||
/// and language under `title`, created at `created` (Unix seconds). With `cmyk`, transparency
|
||||
/// blends in CMYK (unless the colours are converted to RGB).
|
||||
pub(crate) fn new(doc: &Document, set: &crate::PdfSettings, title: &str, created: Option<i64>, cmyk: bool) -> Result<Self, PdfError> {
|
||||
let out = ColorOut::new(doc, set)?;
|
||||
let mut warnings = out.profile_notes.clone();
|
||||
@@ -111,7 +111,11 @@ impl Writer {
|
||||
created: created.map(date_time),
|
||||
modified: None,
|
||||
};
|
||||
let pdf = write::Writer::new(settings, metadata);
|
||||
// The document language (Document Setup) as the catalog's `Lang` and the XMP packet's
|
||||
// `dc:language` (ISO 32000-1, 14.9.2): its BCP 47 tag. A language without a tag writes
|
||||
// none rather than a guess.
|
||||
let language = vectorcraft_doc::setup::language_tag(&doc.setup.language).map(str::to_string);
|
||||
let pdf = write::Writer::new_with_language(settings, metadata, language);
|
||||
Ok(Self {
|
||||
pdf,
|
||||
cmyk: out.blends_cmyk(cmyk),
|
||||
|
||||
@@ -1449,6 +1449,41 @@ fn empty_and_populated_file_info_preserve_key_presence() {
|
||||
assert!(s.contains("/ViewerPreferences<</DisplayDocTitle true>>"), "viewers should show the document title (#3646)");
|
||||
}
|
||||
|
||||
/// The text of the `/Lang` entry of the catalog of `pdf` (a literal string), if any.
|
||||
fn catalog_lang(pdf: &[u8]) -> Option<String> {
|
||||
let xref = crate::patch::Xref::read(pdf).expect("a cross-reference table");
|
||||
let root = xref.trailer_ref(pdf, b"/Root").expect("a catalog");
|
||||
let (start, end) = xref.dict(pdf, root).expect("the catalog's dictionary");
|
||||
let catalog = String::from_utf8_lossy(&pdf[start..end]).into_owned();
|
||||
let at = catalog.find("/Lang")? + "/Lang".len();
|
||||
let rest = catalog[at..].trim_start().strip_prefix('(').expect("a literal string");
|
||||
Some(rest[..rest.find(')').expect("a closed string")].to_string())
|
||||
}
|
||||
|
||||
/// The catalog gives the document language chosen in Document Setup as its BCP 47 tag, in
|
||||
/// exported and printed PDFs, and the XMP packet says it too (#3644).
|
||||
#[test]
|
||||
fn the_catalog_gives_the_document_language() {
|
||||
let mut d = doc(100.0, 100.0);
|
||||
for (language, tag) in [(None, "en-US"), (Some("French"), "fr"), (Some("German: Swiss"), "de-CH")] {
|
||||
if let Some(language) = language {
|
||||
d.setup.language = language.into();
|
||||
}
|
||||
for opts in [PdfOptions::uncompressed(), PdfOptions::default()] {
|
||||
let bytes = export(&d, &opts).unwrap();
|
||||
assert_eq!(catalog_lang(&bytes).as_deref(), Some(tag), "{language:?}");
|
||||
}
|
||||
// Print to PDF writes its file with the same writer.
|
||||
let printed = print(&d, &PrintOptions { created: Some(1_700_000_000), ..Default::default() }).unwrap();
|
||||
assert_eq!(catalog_lang(&printed.bytes).as_deref(), Some(tag), "{language:?}: printed");
|
||||
let s = uncompressed(&d);
|
||||
assert!(s.contains(&format!("<dc:language><rdf:Bag><rdf:li>{tag}</rdf:li></rdf:Bag></dc:language>")), "{language:?}: {s}");
|
||||
}
|
||||
// A language without a tag (from a file) writes none rather than a guess.
|
||||
d.setup.language = "Klingon".into();
|
||||
assert_eq!(catalog_lang(&export(&d, &PdfOptions::uncompressed()).unwrap()), None);
|
||||
}
|
||||
|
||||
/// A function or colour space built on itself is refused before the interpreter builds it (by
|
||||
/// recursion, with no cap: a stack overflow), and those of real files are read.
|
||||
#[test]
|
||||
|
||||
File diff suppressed because one or more lines are too long
+3
-2
File diff suppressed because one or more lines are too long
+3
-2
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user