Skip to main content

read_fonts/generated/
generated_dsig.rs

1// THIS FILE IS AUTOGENERATED.
2// Any changes to this file will be overwritten.
3// For more information about how codegen works, see font-codegen/README.md
4
5#[allow(unused_imports)]
6use crate::codegen_prelude::*;
7
8impl<'a> MinByteRange<'a> for Dsig<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.signature_records_byte_range().end
11    }
12    fn min_table_bytes(&self) -> &'a [u8] {
13        let range = self.min_byte_range();
14        self.data.as_bytes().get(range).unwrap_or_default()
15    }
16}
17
18impl TopLevelTable for Dsig<'_> {
19    /// `DSIG`
20    const TAG: Tag = Tag::new(b"DSIG");
21}
22
23impl ReadArgs for Dsig<'_> {
24    type Args = ();
25}
26
27impl<'a> FontRead<'a> for Dsig<'a> {
28    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
29        #[allow(clippy::absurd_extreme_comparisons)]
30        if data.len() < Self::MIN_SIZE {
31            return Err(ReadError::OutOfBounds);
32        }
33        Ok(Self { data })
34    }
35}
36
37/// [DSIG (Digital Signature Table)](https://docs.microsoft.com/en-us/typography/opentype/spec/dsig#table-structure) table
38#[derive(Clone)]
39pub struct Dsig<'a> {
40    data: FontData<'a>,
41}
42
43#[allow(clippy::needless_lifetimes)]
44impl<'a> Dsig<'a> {
45    pub const MIN_SIZE: usize =
46        (u32::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + PermissionFlags::RAW_BYTE_LEN);
47    basic_table_impls!(impl_the_methods);
48
49    /// Version number of the DSIG table (0x00000001)
50    pub fn version(&self) -> u32 {
51        let range = self.version_byte_range();
52        self.data.read_at(range.start).ok().unwrap()
53    }
54
55    /// Number of signatures in the table
56    pub fn num_signatures(&self) -> u16 {
57        let range = self.num_signatures_byte_range();
58        self.data.read_at(range.start).ok().unwrap()
59    }
60
61    /// Permission flags
62    pub fn flags(&self) -> PermissionFlags {
63        let range = self.flags_byte_range();
64        self.data.read_at(range.start).ok().unwrap()
65    }
66
67    /// Array of signature records
68    pub fn signature_records(&self) -> &'a [SignatureRecord] {
69        let range = self.signature_records_byte_range();
70        self.data.read_array(range).ok().unwrap_or_default()
71    }
72
73    pub fn version_byte_range(&self) -> Range<usize> {
74        let start = 0;
75        let end = start + u32::RAW_BYTE_LEN;
76        start..end
77    }
78
79    pub fn num_signatures_byte_range(&self) -> Range<usize> {
80        let start = self.version_byte_range().end;
81        let end = start + u16::RAW_BYTE_LEN;
82        start..end
83    }
84
85    pub fn flags_byte_range(&self) -> Range<usize> {
86        let start = self.num_signatures_byte_range().end;
87        let end = start + PermissionFlags::RAW_BYTE_LEN;
88        start..end
89    }
90
91    pub fn signature_records_byte_range(&self) -> Range<usize> {
92        let num_signatures = self.num_signatures();
93        let start = self.flags_byte_range().end;
94        let end = start
95            + (transforms::to_usize(num_signatures)).saturating_mul(SignatureRecord::RAW_BYTE_LEN);
96        start..end
97    }
98}
99
100const _: () = assert!(FontData::default_data_long_enough(Dsig::MIN_SIZE));
101
102impl Default for Dsig<'_> {
103    fn default() -> Self {
104        Self {
105            data: FontData::default_table_data(),
106        }
107    }
108}
109
110/// [Permission flags](https://learn.microsoft.com/en-us/typography/opentype/spec/dsig#table-structure)
111#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)]
112#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
113#[repr(transparent)]
114pub struct PermissionFlags {
115    bits: u16,
116}
117
118impl PermissionFlags {
119    /// Bit 0: Cannot be resigned
120    pub const CANNOT_BE_RESIGNED: Self = Self {
121        bits: 0b0000_0000_0000_0001,
122    };
123}
124
125impl PermissionFlags {
126    ///  Returns an empty set of flags.
127    #[inline]
128    pub const fn empty() -> Self {
129        Self { bits: 0 }
130    }
131
132    /// Returns the set containing all flags.
133    #[inline]
134    pub const fn all() -> Self {
135        Self {
136            bits: Self::CANNOT_BE_RESIGNED.bits,
137        }
138    }
139
140    /// Returns the raw value of the flags currently stored.
141    #[inline]
142    pub const fn bits(&self) -> u16 {
143        self.bits
144    }
145
146    /// Convert from underlying bit representation, unless that
147    /// representation contains bits that do not correspond to a flag.
148    #[inline]
149    pub const fn from_bits(bits: u16) -> Option<Self> {
150        if (bits & !Self::all().bits()) == 0 {
151            Some(Self { bits })
152        } else {
153            None
154        }
155    }
156
157    /// Convert from underlying bit representation, dropping any bits
158    /// that do not correspond to flags.
159    #[inline]
160    pub const fn from_bits_truncate(bits: u16) -> Self {
161        Self {
162            bits: bits & Self::all().bits,
163        }
164    }
165
166    /// Returns `true` if no flags are currently stored.
167    #[inline]
168    pub const fn is_empty(&self) -> bool {
169        self.bits() == Self::empty().bits()
170    }
171
172    /// Returns `true` if there are flags common to both `self` and `other`.
173    #[inline]
174    pub const fn intersects(&self, other: Self) -> bool {
175        !(Self {
176            bits: self.bits & other.bits,
177        })
178        .is_empty()
179    }
180
181    /// Returns `true` if all of the flags in `other` are contained within `self`.
182    #[inline]
183    pub const fn contains(&self, other: Self) -> bool {
184        (self.bits & other.bits) == other.bits
185    }
186
187    /// Inserts the specified flags in-place.
188    #[inline]
189    pub fn insert(&mut self, other: Self) {
190        self.bits |= other.bits;
191    }
192
193    /// Removes the specified flags in-place.
194    #[inline]
195    pub fn remove(&mut self, other: Self) {
196        self.bits &= !other.bits;
197    }
198
199    /// Toggles the specified flags in-place.
200    #[inline]
201    pub fn toggle(&mut self, other: Self) {
202        self.bits ^= other.bits;
203    }
204
205    /// Returns the intersection between the flags in `self` and
206    /// `other`.
207    ///
208    /// Specifically, the returned set contains only the flags which are
209    /// present in *both* `self` *and* `other`.
210    ///
211    /// This is equivalent to using the `&` operator (e.g.
212    /// [`ops::BitAnd`]), as in `flags & other`.
213    ///
214    /// [`ops::BitAnd`]: https://doc.rust-lang.org/std/ops/trait.BitAnd.html
215    #[inline]
216    #[must_use]
217    pub const fn intersection(self, other: Self) -> Self {
218        Self {
219            bits: self.bits & other.bits,
220        }
221    }
222
223    /// Returns the union of between the flags in `self` and `other`.
224    ///
225    /// Specifically, the returned set contains all flags which are
226    /// present in *either* `self` *or* `other`, including any which are
227    /// present in both.
228    ///
229    /// This is equivalent to using the `|` operator (e.g.
230    /// [`ops::BitOr`]), as in `flags | other`.
231    ///
232    /// [`ops::BitOr`]: https://doc.rust-lang.org/std/ops/trait.BitOr.html
233    #[inline]
234    #[must_use]
235    pub const fn union(self, other: Self) -> Self {
236        Self {
237            bits: self.bits | other.bits,
238        }
239    }
240
241    /// Returns the difference between the flags in `self` and `other`.
242    ///
243    /// Specifically, the returned set contains all flags present in
244    /// `self`, except for the ones present in `other`.
245    ///
246    /// It is also conceptually equivalent to the "bit-clear" operation:
247    /// `flags & !other` (and this syntax is also supported).
248    ///
249    /// This is equivalent to using the `-` operator (e.g.
250    /// [`ops::Sub`]), as in `flags - other`.
251    ///
252    /// [`ops::Sub`]: https://doc.rust-lang.org/std/ops/trait.Sub.html
253    #[inline]
254    #[must_use]
255    pub const fn difference(self, other: Self) -> Self {
256        Self {
257            bits: self.bits & !other.bits,
258        }
259    }
260}
261
262impl std::ops::BitOr for PermissionFlags {
263    type Output = Self;
264
265    /// Returns the union of the two sets of flags.
266    #[inline]
267    fn bitor(self, other: PermissionFlags) -> Self {
268        Self {
269            bits: self.bits | other.bits,
270        }
271    }
272}
273
274impl std::ops::BitOrAssign for PermissionFlags {
275    /// Adds the set of flags.
276    #[inline]
277    fn bitor_assign(&mut self, other: Self) {
278        self.bits |= other.bits;
279    }
280}
281
282impl std::ops::BitXor for PermissionFlags {
283    type Output = Self;
284
285    /// Returns the left flags, but with all the right flags toggled.
286    #[inline]
287    fn bitxor(self, other: Self) -> Self {
288        Self {
289            bits: self.bits ^ other.bits,
290        }
291    }
292}
293
294impl std::ops::BitXorAssign for PermissionFlags {
295    /// Toggles the set of flags.
296    #[inline]
297    fn bitxor_assign(&mut self, other: Self) {
298        self.bits ^= other.bits;
299    }
300}
301
302impl std::ops::BitAnd for PermissionFlags {
303    type Output = Self;
304
305    /// Returns the intersection between the two sets of flags.
306    #[inline]
307    fn bitand(self, other: Self) -> Self {
308        Self {
309            bits: self.bits & other.bits,
310        }
311    }
312}
313
314impl std::ops::BitAndAssign for PermissionFlags {
315    /// Disables all flags disabled in the set.
316    #[inline]
317    fn bitand_assign(&mut self, other: Self) {
318        self.bits &= other.bits;
319    }
320}
321
322impl std::ops::Sub for PermissionFlags {
323    type Output = Self;
324
325    /// Returns the set difference of the two sets of flags.
326    #[inline]
327    fn sub(self, other: Self) -> Self {
328        Self {
329            bits: self.bits & !other.bits,
330        }
331    }
332}
333
334impl std::ops::SubAssign for PermissionFlags {
335    /// Disables all flags enabled in the set.
336    #[inline]
337    fn sub_assign(&mut self, other: Self) {
338        self.bits &= !other.bits;
339    }
340}
341
342impl std::ops::Not for PermissionFlags {
343    type Output = Self;
344
345    /// Returns the complement of this set of flags.
346    #[inline]
347    fn not(self) -> Self {
348        Self { bits: !self.bits } & Self::all()
349    }
350}
351
352impl std::fmt::Debug for PermissionFlags {
353    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
354        let members: &[(&str, Self)] = &[("CANNOT_BE_RESIGNED", Self::CANNOT_BE_RESIGNED)];
355        let mut first = true;
356        for (name, value) in members {
357            if self.contains(*value) {
358                if !first {
359                    f.write_str(" | ")?;
360                }
361                first = false;
362                f.write_str(name)?;
363            }
364        }
365        if first {
366            f.write_str("(empty)")?;
367        }
368        Ok(())
369    }
370}
371
372impl std::fmt::Binary for PermissionFlags {
373    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
374        std::fmt::Binary::fmt(&self.bits, f)
375    }
376}
377
378impl std::fmt::Octal for PermissionFlags {
379    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
380        std::fmt::Octal::fmt(&self.bits, f)
381    }
382}
383
384impl std::fmt::LowerHex for PermissionFlags {
385    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
386        std::fmt::LowerHex::fmt(&self.bits, f)
387    }
388}
389
390impl std::fmt::UpperHex for PermissionFlags {
391    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
392        std::fmt::UpperHex::fmt(&self.bits, f)
393    }
394}
395
396impl font_types::Scalar for PermissionFlags {
397    type Raw = <u16 as font_types::Scalar>::Raw;
398    fn to_raw(self) -> Self::Raw {
399        self.bits().to_raw()
400    }
401    fn from_raw(raw: Self::Raw) -> Self {
402        let t = <u16>::from_raw(raw);
403        Self::from_bits_truncate(t)
404    }
405}
406
407/// [Signature Record](https://learn.microsoft.com/en-us/typography/opentype/spec/dsig#table-structure)
408#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
409#[repr(C)]
410#[repr(packed)]
411pub struct SignatureRecord {
412    /// Format of the signature
413    pub format: BigEndian<u32>,
414    /// Length of signature in bytes
415    pub length: BigEndian<u32>,
416    /// Offset to the signature block from the beginning of the table
417    pub signature_block_offset: BigEndian<Offset32>,
418}
419
420impl SignatureRecord {
421    /// Format of the signature
422    pub fn format(&self) -> u32 {
423        self.format.get()
424    }
425
426    /// Length of signature in bytes
427    pub fn length(&self) -> u32 {
428        self.length.get()
429    }
430
431    /// Offset to the signature block from the beginning of the table
432    pub fn signature_block_offset(&self) -> Offset32 {
433        self.signature_block_offset.get()
434    }
435}
436
437impl FixedSize for SignatureRecord {
438    const RAW_BYTE_LEN: usize = u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + Offset32::RAW_BYTE_LEN;
439}
440
441impl<'a> MinByteRange<'a> for SignatureBlockFormat1<'a> {
442    fn min_byte_range(&self) -> Range<usize> {
443        0..self.signature_byte_range().end
444    }
445    fn min_table_bytes(&self) -> &'a [u8] {
446        let range = self.min_byte_range();
447        self.data.as_bytes().get(range).unwrap_or_default()
448    }
449}
450
451impl ReadArgs for SignatureBlockFormat1<'_> {
452    type Args = ();
453}
454
455impl<'a> FontRead<'a> for SignatureBlockFormat1<'a> {
456    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
457        #[allow(clippy::absurd_extreme_comparisons)]
458        if data.len() < Self::MIN_SIZE {
459            return Err(ReadError::OutOfBounds);
460        }
461        Ok(Self { data })
462    }
463}
464
465/// [Signature Block Format 1](https://learn.microsoft.com/en-us/typography/opentype/spec/dsig#table-structure)
466#[derive(Clone)]
467pub struct SignatureBlockFormat1<'a> {
468    data: FontData<'a>,
469}
470
471#[allow(clippy::needless_lifetimes)]
472impl<'a> SignatureBlockFormat1<'a> {
473    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
474    basic_table_impls!(impl_the_methods);
475
476    /// Length (in bytes) of the PKCS#7 packet in the signature field.
477    pub fn signature_length(&self) -> u32 {
478        let range = self.signature_length_byte_range();
479        self.data.read_at(range.start).ok().unwrap()
480    }
481
482    /// PKCS#7 packet
483    pub fn signature(&self) -> &'a [u8] {
484        let range = self.signature_byte_range();
485        self.data.read_array(range).ok().unwrap_or_default()
486    }
487
488    pub fn _reserved1_byte_range(&self) -> Range<usize> {
489        let start = 0;
490        let end = start + u16::RAW_BYTE_LEN;
491        start..end
492    }
493
494    pub fn _reserved2_byte_range(&self) -> Range<usize> {
495        let start = self._reserved1_byte_range().end;
496        let end = start + u16::RAW_BYTE_LEN;
497        start..end
498    }
499
500    pub fn signature_length_byte_range(&self) -> Range<usize> {
501        let start = self._reserved2_byte_range().end;
502        let end = start + u32::RAW_BYTE_LEN;
503        start..end
504    }
505
506    pub fn signature_byte_range(&self) -> Range<usize> {
507        let signature_length = self.signature_length();
508        let start = self.signature_length_byte_range().end;
509        let end = start + (transforms::to_usize(signature_length)).saturating_mul(u8::RAW_BYTE_LEN);
510        start..end
511    }
512}
513
514const _: () = assert!(FontData::default_data_long_enough(
515    SignatureBlockFormat1::MIN_SIZE
516));
517
518impl Default for SignatureBlockFormat1<'_> {
519    fn default() -> Self {
520        Self {
521            data: FontData::default_table_data(),
522        }
523    }
524}