Skip to main content

harfrust/
direction.rs

1use core::str::FromStr;
2
3use crate::Script;
4
5/// Defines the direction in which text is to be read.
6#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
7pub enum Direction {
8    /// Initial, unset direction.
9    Invalid,
10    /// Text is set horizontally from left to right.
11    LeftToRight,
12    /// Text is set horizontally from right to left.
13    RightToLeft,
14    /// Text is set vertically from top to bottom.
15    TopToBottom,
16    /// Text is set vertically from bottom to top.
17    BottomToTop,
18}
19
20impl Direction {
21    #[inline]
22    pub(crate) fn is_horizontal(self) -> bool {
23        match self {
24            Direction::Invalid => false,
25            Direction::LeftToRight => true,
26            Direction::RightToLeft => true,
27            Direction::TopToBottom => false,
28            Direction::BottomToTop => false,
29        }
30    }
31
32    #[inline]
33    pub(crate) fn is_vertical(self) -> bool {
34        !self.is_horizontal()
35    }
36
37    #[inline]
38    pub(crate) fn is_forward(self) -> bool {
39        match self {
40            Direction::Invalid => false,
41            Direction::LeftToRight => true,
42            Direction::RightToLeft => false,
43            Direction::TopToBottom => true,
44            Direction::BottomToTop => false,
45        }
46    }
47
48    #[inline]
49    pub(crate) fn is_backward(self) -> bool {
50        !self.is_forward()
51    }
52
53    #[inline]
54    pub(crate) fn reverse(self) -> Self {
55        match self {
56            Direction::Invalid => Direction::Invalid,
57            Direction::LeftToRight => Direction::RightToLeft,
58            Direction::RightToLeft => Direction::LeftToRight,
59            Direction::TopToBottom => Direction::BottomToTop,
60            Direction::BottomToTop => Direction::TopToBottom,
61        }
62    }
63
64    pub(crate) fn from_script(script: Script) -> Option<Self> {
65        // https://docs.google.com/spreadsheets/d/1Y90M0Ie3MUJ6UVCRDOypOtijlMDLNNyyLk36T6iMu0o
66
67        match script {
68            // Unicode-1.1 additions
69            Script::ARABIC |
70            Script::HEBREW |
71
72            // Unicode-3.0 additions
73            Script::SYRIAC |
74            Script::THAANA |
75
76            // Unicode-4.0 additions
77            Script::CYPRIOT |
78
79            // Unicode-4.1 additions
80            Script::KHAROSHTHI |
81
82            // Unicode-5.0 additions
83            Script::PHOENICIAN |
84            Script::NKO |
85
86            // Unicode-5.1 additions
87            Script::LYDIAN |
88
89            // Unicode-5.2 additions
90            Script::AVESTAN |
91            Script::IMPERIAL_ARAMAIC |
92            Script::INSCRIPTIONAL_PAHLAVI |
93            Script::INSCRIPTIONAL_PARTHIAN |
94            Script::OLD_SOUTH_ARABIAN |
95            Script::OLD_TURKIC |
96            Script::SAMARITAN |
97
98            // Unicode-6.0 additions
99            Script::MANDAIC |
100
101            // Unicode-6.1 additions
102            Script::MEROITIC_CURSIVE |
103            Script::MEROITIC_HIEROGLYPHS |
104
105            // Unicode-7.0 additions
106            Script::MANICHAEAN |
107            Script::MENDE_KIKAKUI |
108            Script::NABATAEAN |
109            Script::OLD_NORTH_ARABIAN |
110            Script::PALMYRENE |
111            Script::PSALTER_PAHLAVI |
112
113            // Unicode-8.0 additions
114            Script::HATRAN |
115
116            // Unicode-9.0 additions
117            Script::ADLAM |
118
119            // Unicode-11.0 additions
120            Script::HANIFI_ROHINGYA |
121            Script::OLD_SOGDIAN |
122            Script::SOGDIAN |
123
124            // Unicode-12.0 additions
125            Script::ELYMAIC |
126
127            // Unicode-13.0 additions
128            Script::CHORASMIAN |
129            Script::YEZIDI |
130
131            // Unicode-14.0 additions
132            Script::OLD_UYGHUR |
133
134            // Unicode-16.0 additions
135            Script::GARAY |
136
137            // Unicode-17.0 additions
138            Script::SIDETIC => {
139                Some(Direction::RightToLeft)
140            }
141
142            // https://github.com/harfbuzz/harfbuzz/issues/1000
143            Script::OLD_HUNGARIAN |
144            Script::OLD_ITALIC |
145            Script::RUNIC |
146            Script::TIFINAGH => {
147                None
148            }
149
150            _ => Some(Direction::LeftToRight),
151        }
152    }
153}
154
155impl Default for Direction {
156    #[inline]
157    fn default() -> Self {
158        Direction::Invalid
159    }
160}
161
162impl FromStr for Direction {
163    type Err = &'static str;
164
165    fn from_str(s: &str) -> Result<Self, Self::Err> {
166        if s.is_empty() {
167            return Err("invalid direction");
168        }
169
170        // harfbuzz also matches only the first letter.
171        match s.as_bytes()[0].to_ascii_lowercase() {
172            b'l' => Ok(Direction::LeftToRight),
173            b'r' => Ok(Direction::RightToLeft),
174            b't' => Ok(Direction::TopToBottom),
175            b'b' => Ok(Direction::BottomToTop),
176            _ => Err("invalid direction"),
177        }
178    }
179}