Skip to main content

kashida/
builtin.rs

1//! The built-in pattern sets, compiled on first use.
2
3use crate::pattern::{compile_pattern_text, PatternSet};
4use std::sync::OnceLock;
5
6const ARABIC_SIMPLE_TEXT: &str = include_str!("../data/arabic-simple.pat");
7const ARABIC_NASKH_TEXT: &str = include_str!("../data/arabic-naskh.pat");
8const SYRIAC_TEXT: &str = include_str!("../data/syriac.pat");
9
10fn arabic_simple_set() -> &'static PatternSet {
11    static SET: OnceLock<PatternSet> = OnceLock::new();
12    SET.get_or_init(|| {
13        compile_pattern_text(ARABIC_SIMPLE_TEXT).expect("built-in \"arabic-simple\" compiles")
14    })
15}
16
17fn arabic_naskh_set() -> &'static PatternSet {
18    static SET: OnceLock<PatternSet> = OnceLock::new();
19    SET.get_or_init(|| {
20        compile_pattern_text(ARABIC_NASKH_TEXT).expect("built-in \"arabic-naskh\" compiles")
21    })
22}
23
24fn syriac_set() -> &'static PatternSet {
25    static SET: OnceLock<PatternSet> = OnceLock::new();
26    SET.get_or_init(|| compile_pattern_text(SYRIAC_TEXT).expect("built-in \"syriac\" compiles"))
27}
28
29/// The names of the built-in pattern sets:
30///
31/// - `"arabic-simple"`: Arabic _kashida_ rules suitable for _simple_
32///   typefaces, i.e. those where letters have only the basic forms with no or
33///   very limited relations between letters (no or very few ligatures,
34///   contextual alternates, and so on).
35///   It is also suitable for _Kufic_ styles of Arabic in general.
36///   The rules are based on [Microsoft justification rules], which are rooted
37///   in Arabic newspaper typesetting.
38/// - `"arabic-naskh"`: Classical Arabic _kashida_ insertion rules suitable for
39///   classical _Naskh_ and _Naskh_-like typefaces (e.g. Thuluth) that follow
40///   the classical rules of Arabic calligraphy and have advanced relations
41///   between letters.
42///   The rules are derived mainly from the publications of [Benatia et al.]
43///   on the subject, but overridden where they disagree with Fawzi Salim
44///   Afifi’s _Taallum al-khatt al-arabi_, part 3 (تعلم الخط العربي، الجزء
45///   الثالث).
46/// - `"syriac"`: Syriac, following the [guidelines proposed for justified
47///   Syriac].
48///
49/// See the `.pat` sources for the rules themselves and their citations.
50///
51/// [Microsoft justification rules]: https://web.archive.org/web/20130308140133/microsoft.com/middleeast/msdn/JustifyingText-CSS.aspx
52/// [Benatia et al.]: https://www.tug.org/tugboat/tb27-2/tb87benatia.pdf
53/// [guidelines proposed for justified Syriac]: https://bugs.documentfoundation.org/show_bug.cgi?id=140767
54pub fn builtin_pattern_set_names() -> &'static [&'static str] {
55    &["arabic-simple", "arabic-naskh", "syriac"]
56}
57
58/// Whether `name` refers to a built-in pattern set, without compiling it,
59/// unlike [`builtin_pattern_set`].
60pub fn is_builtin_pattern_set(name: &str) -> bool {
61    builtin_pattern_set_names().contains(&name)
62}
63
64/// The built-in pattern set of that name, compiled on first use, or `None`
65/// if there is none. [`builtin_pattern_set_names`] lists and describes them.
66pub fn builtin_pattern_set(name: &str) -> Option<&'static PatternSet> {
67    match name {
68        "arabic-simple" => Some(arabic_simple_set()),
69        "arabic-naskh" => Some(arabic_naskh_set()),
70        "syriac" => Some(syriac_set()),
71        _ => None,
72    }
73}