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