summaryrefslogtreecommitdiff
path: root/crates/typst-library/src/pdf/accessibility.rs
blob: 9399c1c60b9ff787590f12a2e1d1a968c3eec6fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use std::num::NonZeroU32;

use ecow::EcoString;
use typst_macros::{cast, elem, func, Cast};
use typst_utils::NonZeroExt;

use crate::diag::SourceResult;
use crate::engine::Engine;
use crate::foundations::{Content, NativeElement, Packed, Show, Smart, StyleChain};
use crate::introspection::Locatable;
use crate::model::TableCell;

// TODO: docs
#[elem(Locatable, Show)]
pub struct PdfTagElem {
    #[default(PdfTagKind::NonStruct)]
    pub kind: PdfTagKind,

    /// An alternate description.
    pub alt: Option<EcoString>,
    /// Exact replacement for this structure element and its children.
    pub actual_text: Option<EcoString>,
    /// The expanded form of an abbreviation/acronym.
    pub expansion: Option<EcoString>,

    /// The content to underline.
    #[required]
    pub body: Content,
}

impl Show for Packed<PdfTagElem> {
    #[typst_macros::time(name = "pdf.tag", span = self.span())]
    fn show(&self, _: &mut Engine, _: StyleChain) -> SourceResult<Content> {
        Ok(self.body.clone())
    }
}

// TODO: docs
/// PDF structure elements
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum PdfTagKind {
    // grouping elements
    /// (Part)
    Part,
    /// (Article)
    Art,
    /// (Section)
    Sect,
    /// (Division)
    Div,
    /// (Block quotation)
    BlockQuote,
    /// (Caption)
    Caption,
    /// (Table of contents)
    TOC,
    /// (Table of contents item)
    TOCI,
    /// (Index)
    Index,
    /// (Nonstructural element)
    NonStruct,
    /// (Private element)
    Private,

    // paragraph like elements
    /// (Heading)
    H { title: Option<EcoString> },
    /// (Heading level 1)
    H1 { title: Option<EcoString> },
    /// (Heading level 2)
    H2 { title: Option<EcoString> },
    /// (Heading level 3)
    H4 { title: Option<EcoString> },
    /// (Heading level 4)
    H3 { title: Option<EcoString> },
    /// (Heading level 5)
    H5 { title: Option<EcoString> },
    /// (Heading level 6)
    H6 { title: Option<EcoString> },
    /// (Paragraph)
    P,

    // list elements
    /// (List)
    L { numbering: ListNumbering },
    /// (List item)
    LI,
    /// (Label)
    Lbl,
    /// (List body)
    LBody,

    // table elements
    /// (Table)
    Table,
    /// (Table row)
    TR,
    /// (Table header)
    TH { scope: TableHeaderScope },
    /// (Table data cell)
    TD,
    /// (Table header row group)
    THead,
    /// (Table body row group)
    TBody,
    /// (Table footer row group)
    TFoot,

    // inline elements
    /// (Span)
    Span,
    /// (Quotation)
    Quote,
    /// (Note)
    Note,
    /// (Reference)
    Reference,
    /// (Bibliography Entry)
    BibEntry,
    /// (Code)
    Code,
    /// (Link)
    Link,
    /// (Annotation)
    Annot,

    /// (Ruby)
    Ruby,
    /// (Ruby base text)
    RB,
    /// (Ruby annotation text)
    RT,
    /// (Ruby punctuation)
    RP,

    /// (Warichu)
    Warichu,
    /// (Warichu text)
    WT,
    /// (Warichu punctuation)
    WP,

    /// (Figure)
    Figure,
    /// (Formula)
    Formula,
    /// (Form)
    Form,
}

cast! {
    PdfTagKind,
    self => match self {
        PdfTagKind::Part => "part".into_value(),
        _ => todo!(),
    },
    "part" => Self::Part,
    // TODO
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ListNumbering {
    /// No numbering.
    None,
    /// Solid circular bullets.
    Disc,
    /// Open circular bullets.
    Circle,
    /// Solid square bullets.
    Square,
    /// Decimal numbers.
    Decimal,
    /// Lowercase Roman numerals.
    LowerRoman,
    /// Uppercase Roman numerals.
    UpperRoman,
    /// Lowercase letters.
    LowerAlpha,
    /// Uppercase letters.
    UpperAlpha,
}

/// Mark content as a PDF artifact.
/// TODO: maybe generalize this and use it to mark html elements with `aria-hidden="true"`?
#[elem(Locatable, Show)]
pub struct ArtifactElem {
    /// The artifact kind.
    #[default(ArtifactKind::Other)]
    pub kind: ArtifactKind,

    /// The content that is an artifact.
    #[required]
    pub body: Content,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Cast)]
pub enum ArtifactKind {
    /// Page header artifacts.
    Header,
    /// Page footer artifacts.
    Footer,
    /// Other page artifacts.
    Page,
    /// Other artifacts.
    #[default]
    Other,
}

impl Show for Packed<ArtifactElem> {
    #[typst_macros::time(name = "pdf.artifact", span = self.span())]
    fn show(&self, _: &mut Engine, _: StyleChain) -> SourceResult<Content> {
        Ok(self.body.clone())
    }
}

// TODO: feature gate
/// Explicity define this cell as a header cell.
#[func]
pub fn header_cell(
    #[named]
    #[default(NonZeroU32::ONE)]
    level: NonZeroU32,
    #[named]
    #[default]
    scope: TableHeaderScope,
    /// The table cell.
    cell: TableCell,
) -> Content {
    cell.with_kind(Smart::Custom(TableCellKind::Header(level, scope)))
        .pack()
}

// TODO: feature gate
/// Explicity define this cell as a data cell.
#[func]
pub fn data_cell(
    /// The table cell.
    cell: TableCell,
) -> Content {
    cell.with_kind(Smart::Custom(TableCellKind::Data)).pack()
}

#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
pub enum TableCellKind {
    Header(NonZeroU32, TableHeaderScope),
    Footer,
    #[default]
    Data,
}

/// The scope of a table header cell.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash, Cast)]
pub enum TableHeaderScope {
    /// The header cell refers to both the row and the column.
    Both,
    /// The header cell refers to the column.
    #[default]
    Column,
    /// The header cell refers to the row.
    Row,
}

impl TableHeaderScope {
    pub fn refers_to_column(&self) -> bool {
        match self {
            TableHeaderScope::Both => true,
            TableHeaderScope::Column => true,
            TableHeaderScope::Row => false,
        }
    }

    pub fn refers_to_row(&self) -> bool {
        match self {
            TableHeaderScope::Both => true,
            TableHeaderScope::Column => false,
            TableHeaderScope::Row => true,
        }
    }
}