Skip to content

Commit 8418066

Browse files
committed
Keys added in PDF 2 for structure tree dicts
1 parent 03f48b3 commit 8418066

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

src/structure.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,22 @@ impl StructTreeRoot<'_> {
392392
pub fn namespaces(&mut self) -> TypedArray<'_, Ref> {
393393
self.dict.insert(Name(b"Namespaces")).array().typed()
394394
}
395+
396+
/// Start writing the `PronunciationLexicon` attribute to specify one or
397+
/// multiple pronunciation lexicons for the document. PDF 2.0+.
398+
///
399+
/// The lexicons shall be XML files conforming to the Pronunciation Lexicon
400+
/// Specification (PLS) Version 1.0. Each entry in the array is an indirect
401+
/// reference to a [`FileSpec`] dictionary for a lexicon file.
402+
pub fn pronunciation_lexicon(&mut self) -> TypedArray<'_, Ref> {
403+
self.dict.insert(Name(b"PronunciationLexicon")).array().typed()
404+
}
405+
406+
/// Start writing the `/AF` attribute to specify one or multiple files
407+
/// associated with the entire structure tree. PDF 2.0+.
408+
pub fn associated_files(&mut self) -> TypedArray<'_, FileSpec> {
409+
self.dict.insert(Name(b"AF")).array().typed()
410+
}
395411
}
396412

397413
deref!('a, StructTreeRoot<'a> => Dict<'a>, dict);
@@ -443,6 +459,16 @@ impl StructElement<'_> {
443459
self
444460
}
445461

462+
/// Write the `/Ref` attribute to specify to which structure element this
463+
/// element refers. Used e.g. for footnotes. PDF 2.0+
464+
///
465+
/// The parameter `refs` shall be indirect object references to other
466+
/// structure elements.
467+
pub fn refs(&mut self, refs: impl IntoIterator<Item = Ref>) -> &mut Self {
468+
self.dict.insert(Name(b"Ref")).array().typed().items(refs);
469+
self
470+
}
471+
446472
/// Write the `/Pg` attribute to specify the page some or all of this
447473
/// structure element is located on.
448474
pub fn page(&mut self, page: Ref) -> &mut Self {
@@ -538,6 +564,20 @@ impl StructElement<'_> {
538564
self.dict.pair(Name(b"NS"), ns);
539565
self
540566
}
567+
568+
/// Write the `/PhoneticAlphabet` attribute to specify the phonetic alphabet
569+
/// used in the [StructElement::phoneme] attribute. PDF 2.0+
570+
pub fn phonetic_alphabet(&mut self, alphabet: PhoneticAlphabet) -> &mut Self {
571+
self.dict.pair(Name(b"PhoneticAlphabet"), alphabet.to_name());
572+
self
573+
}
574+
575+
/// Write the `/Phoneme` attribute to specify the phonetic pronunciation of
576+
/// the text in the structure element. PDF 2.0+
577+
pub fn phoneme(&mut self, phoneme: TextStr) -> &mut Self {
578+
self.dict.pair(Name(b"Phoneme"), phoneme);
579+
self
580+
}
541581
}
542582

543583
deref!('a, StructElement<'a> => Dict<'a>, dict);
@@ -859,6 +899,34 @@ impl StructRole {
859899
}
860900
}
861901

902+
/// Which phonetic alphabet to use for the `/Phonetic` key in the
903+
/// [`StructElement`] dictionary.
904+
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
905+
pub enum PhoneticAlphabet<'a> {
906+
/// The International Phonetic Alphabet.
907+
Ipa,
908+
/// The Extended Speech Assessment Methods Phonetic Alphabet (X-SAMPA).
909+
XSampa,
910+
/// The Pinyin romanization system for Chinese.
911+
Pinyin,
912+
/// The Wade-Giles romanization system for Chinese.
913+
WadeGiles,
914+
/// A custom phonetic alphabet.
915+
Custom(Name<'a>),
916+
}
917+
918+
impl<'a> PhoneticAlphabet<'a> {
919+
pub(crate) fn to_name(self) -> Name<'a> {
920+
match self {
921+
Self::Ipa => Name(b"ipa"),
922+
Self::XSampa => Name(b"x-sampa"),
923+
Self::Pinyin => Name(b"zh-Latn-pinyin"),
924+
Self::WadeGiles => Name(b"zh-Latn-wadegile"),
925+
Self::Custom(name) => name,
926+
}
927+
}
928+
}
929+
862930
/// Writer for a _namespace dictionary_. PDF 2.0+
863931
///
864932
/// This struct is created by [`Chunk::namespace`].

0 commit comments

Comments
 (0)