Skip to main content

pdfluent_forms/
flags.rs

1//! Field flags (/Ff) bitfield wrapper (B.1).
2
3/// Wrapper around a PDF field-flags integer (/Ff).
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
5pub struct FieldFlags(u32);
6
7impl FieldFlags {
8    /// Create from raw bits.
9    pub fn from_bits(bits: u32) -> Self {
10        Self(bits)
11    }
12    /// Empty (no flags set).
13    pub fn empty() -> Self {
14        Self(0)
15    }
16    /// Raw bits.
17    pub fn bits(self) -> u32 {
18        self.0
19    }
20    fn has(self, bit: u32) -> bool {
21        self.0 & (1 << bit) != 0
22    }
23
24    // Common flags
25    /// Bit 1: read-only.
26    pub fn read_only(self) -> bool {
27        self.has(0)
28    }
29    /// Bit 2: required.
30    pub fn required(self) -> bool {
31        self.has(1)
32    }
33    /// Bit 3: no-export.
34    pub fn no_export(self) -> bool {
35        self.has(2)
36    }
37
38    // Text field flags
39    /// Bit 13: multiline.
40    pub fn multiline(self) -> bool {
41        self.has(12)
42    }
43    /// Bit 14: password.
44    pub fn password(self) -> bool {
45        self.has(13)
46    }
47    /// Bit 21: file-select.
48    pub fn file_select(self) -> bool {
49        self.has(20)
50    }
51    /// Bit 23: do-not-spell-check.
52    pub fn do_not_spell_check(self) -> bool {
53        self.has(22)
54    }
55    /// Bit 24: do-not-scroll.
56    pub fn do_not_scroll(self) -> bool {
57        self.has(23)
58    }
59    /// Bit 25: comb.
60    pub fn comb(self) -> bool {
61        self.has(24)
62    }
63    /// Bit 26: rich-text.
64    pub fn rich_text(self) -> bool {
65        self.has(25)
66    }
67
68    // Button flags
69    /// Bit 15: no-toggle-to-off (radio buttons).
70    pub fn no_toggle_to_off(self) -> bool {
71        self.has(14)
72    }
73    /// Bit 16: radio.
74    pub fn radio(self) -> bool {
75        self.has(15)
76    }
77    /// Bit 17: push-button.
78    pub fn push_button(self) -> bool {
79        self.has(16)
80    }
81    /// Bit 26: radios-in-unison.
82    pub fn radios_in_unison(self) -> bool {
83        self.has(25)
84    }
85
86    // Choice flags
87    /// Bit 18: combo.
88    pub fn combo(self) -> bool {
89        self.has(17)
90    }
91    /// Bit 19: edit (editable combo).
92    pub fn edit(self) -> bool {
93        self.has(18)
94    }
95    /// Bit 20: sort.
96    pub fn sort(self) -> bool {
97        self.has(19)
98    }
99    /// Bit 22: multi-select.
100    pub fn multi_select(self) -> bool {
101        self.has(21)
102    }
103    /// Bit 27: commit-on-sel-change.
104    pub fn commit_on_sel_change(self) -> bool {
105        self.has(26)
106    }
107}
108
109#[cfg(test)]
110mod tests {
111    use super::*;
112    #[test]
113    fn empty_flags() {
114        assert!(!FieldFlags::empty().read_only());
115    }
116    #[test]
117    fn read_only() {
118        assert!(FieldFlags::from_bits(1).read_only());
119    }
120    #[test]
121    fn multiline() {
122        assert!(FieldFlags::from_bits(1 << 12).multiline());
123    }
124    #[test]
125    fn combo() {
126        assert!(FieldFlags::from_bits(1 << 17).combo());
127    }
128    #[test]
129    fn push_button() {
130        assert!(FieldFlags::from_bits(1 << 16).push_button());
131    }
132    #[test]
133    fn radio() {
134        assert!(FieldFlags::from_bits(1 << 15).radio());
135    }
136}