Skip to main content

read_fonts/generated/
generated_gasp.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 Gasp<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.gasp_ranges_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 Gasp<'_> {
19    /// `gasp`
20    const TAG: Tag = Tag::new(b"gasp");
21}
22
23impl ReadArgs for Gasp<'_> {
24    type Args = ();
25}
26
27impl<'a> FontRead<'a> for Gasp<'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/// [gasp](https://learn.microsoft.com/en-us/typography/opentype/spec/gasp#gasp-table-formats)
38#[derive(Clone)]
39pub struct Gasp<'a> {
40    data: FontData<'a>,
41}
42
43#[allow(clippy::needless_lifetimes)]
44impl<'a> Gasp<'a> {
45    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
46    basic_table_impls!(impl_the_methods);
47
48    /// Version number (set to 1)
49    pub fn version(&self) -> u16 {
50        let range = self.version_byte_range();
51        self.data.read_at(range.start).ok().unwrap()
52    }
53
54    /// Number of records to follow
55    pub fn num_ranges(&self) -> u16 {
56        let range = self.num_ranges_byte_range();
57        self.data.read_at(range.start).ok().unwrap()
58    }
59
60    /// Sorted by ppem
61    pub fn gasp_ranges(&self) -> &'a [GaspRange] {
62        let range = self.gasp_ranges_byte_range();
63        self.data.read_array(range).ok().unwrap_or_default()
64    }
65
66    pub fn version_byte_range(&self) -> Range<usize> {
67        let start = 0;
68        let end = start + u16::RAW_BYTE_LEN;
69        start..end
70    }
71
72    pub fn num_ranges_byte_range(&self) -> Range<usize> {
73        let start = self.version_byte_range().end;
74        let end = start + u16::RAW_BYTE_LEN;
75        start..end
76    }
77
78    pub fn gasp_ranges_byte_range(&self) -> Range<usize> {
79        let num_ranges = self.num_ranges();
80        let start = self.num_ranges_byte_range().end;
81        let end =
82            start + (transforms::to_usize(num_ranges)).saturating_mul(GaspRange::RAW_BYTE_LEN);
83        start..end
84    }
85}
86
87const _: () = assert!(FontData::default_data_long_enough(Gasp::MIN_SIZE));
88
89impl Default for Gasp<'_> {
90    fn default() -> Self {
91        Self {
92            data: FontData::default_table_data(),
93        }
94    }
95}
96
97#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
98#[repr(C)]
99#[repr(packed)]
100pub struct GaspRange {
101    /// Upper limit of range, in PPEM
102    pub range_max_ppem: BigEndian<u16>,
103    /// Flags describing desired rasterizer behavior.
104    pub range_gasp_behavior: BigEndian<GaspRangeBehavior>,
105}
106
107impl GaspRange {
108    /// Upper limit of range, in PPEM
109    pub fn range_max_ppem(&self) -> u16 {
110        self.range_max_ppem.get()
111    }
112
113    /// Flags describing desired rasterizer behavior.
114    pub fn range_gasp_behavior(&self) -> GaspRangeBehavior {
115        self.range_gasp_behavior.get()
116    }
117}
118
119impl FixedSize for GaspRange {
120    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + GaspRangeBehavior::RAW_BYTE_LEN;
121}
122
123#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)]
124#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
125#[repr(transparent)]
126pub struct GaspRangeBehavior {
127    bits: u16,
128}
129
130impl GaspRangeBehavior {
131    /// Use gridfitting
132    pub const GASP_GRIDFIT: Self = Self { bits: 0x0001 };
133
134    /// Use grayscale rendering
135    pub const GASP_DOGRAY: Self = Self { bits: 0x0002 };
136
137    /// Use gridfitting with ClearType symmetric smoothing Only
138    /// supported in version 1 'gasp'
139    pub const GASP_SYMMETRIC_GRIDFIT: Self = Self { bits: 0x0004 };
140
141    /// Use smoothing along multiple axes with ClearType® Only
142    /// supported in version 1 'gasp'
143    pub const GASP_SYMMETRIC_SMOOTHING: Self = Self { bits: 0x0008 };
144}
145
146impl GaspRangeBehavior {
147    ///  Returns an empty set of flags.
148    #[inline]
149    pub const fn empty() -> Self {
150        Self { bits: 0 }
151    }
152
153    /// Returns the set containing all flags.
154    #[inline]
155    pub const fn all() -> Self {
156        Self {
157            bits: Self::GASP_GRIDFIT.bits
158                | Self::GASP_DOGRAY.bits
159                | Self::GASP_SYMMETRIC_GRIDFIT.bits
160                | Self::GASP_SYMMETRIC_SMOOTHING.bits,
161        }
162    }
163
164    /// Returns the raw value of the flags currently stored.
165    #[inline]
166    pub const fn bits(&self) -> u16 {
167        self.bits
168    }
169
170    /// Convert from underlying bit representation, unless that
171    /// representation contains bits that do not correspond to a flag.
172    #[inline]
173    pub const fn from_bits(bits: u16) -> Option<Self> {
174        if (bits & !Self::all().bits()) == 0 {
175            Some(Self { bits })
176        } else {
177            None
178        }
179    }
180
181    /// Convert from underlying bit representation, dropping any bits
182    /// that do not correspond to flags.
183    #[inline]
184    pub const fn from_bits_truncate(bits: u16) -> Self {
185        Self {
186            bits: bits & Self::all().bits,
187        }
188    }
189
190    /// Returns `true` if no flags are currently stored.
191    #[inline]
192    pub const fn is_empty(&self) -> bool {
193        self.bits() == Self::empty().bits()
194    }
195
196    /// Returns `true` if there are flags common to both `self` and `other`.
197    #[inline]
198    pub const fn intersects(&self, other: Self) -> bool {
199        !(Self {
200            bits: self.bits & other.bits,
201        })
202        .is_empty()
203    }
204
205    /// Returns `true` if all of the flags in `other` are contained within `self`.
206    #[inline]
207    pub const fn contains(&self, other: Self) -> bool {
208        (self.bits & other.bits) == other.bits
209    }
210
211    /// Inserts the specified flags in-place.
212    #[inline]
213    pub fn insert(&mut self, other: Self) {
214        self.bits |= other.bits;
215    }
216
217    /// Removes the specified flags in-place.
218    #[inline]
219    pub fn remove(&mut self, other: Self) {
220        self.bits &= !other.bits;
221    }
222
223    /// Toggles the specified flags in-place.
224    #[inline]
225    pub fn toggle(&mut self, other: Self) {
226        self.bits ^= other.bits;
227    }
228
229    /// Returns the intersection between the flags in `self` and
230    /// `other`.
231    ///
232    /// Specifically, the returned set contains only the flags which are
233    /// present in *both* `self` *and* `other`.
234    ///
235    /// This is equivalent to using the `&` operator (e.g.
236    /// [`ops::BitAnd`]), as in `flags & other`.
237    ///
238    /// [`ops::BitAnd`]: https://doc.rust-lang.org/std/ops/trait.BitAnd.html
239    #[inline]
240    #[must_use]
241    pub const fn intersection(self, other: Self) -> Self {
242        Self {
243            bits: self.bits & other.bits,
244        }
245    }
246
247    /// Returns the union of between the flags in `self` and `other`.
248    ///
249    /// Specifically, the returned set contains all flags which are
250    /// present in *either* `self` *or* `other`, including any which are
251    /// present in both.
252    ///
253    /// This is equivalent to using the `|` operator (e.g.
254    /// [`ops::BitOr`]), as in `flags | other`.
255    ///
256    /// [`ops::BitOr`]: https://doc.rust-lang.org/std/ops/trait.BitOr.html
257    #[inline]
258    #[must_use]
259    pub const fn union(self, other: Self) -> Self {
260        Self {
261            bits: self.bits | other.bits,
262        }
263    }
264
265    /// Returns the difference between the flags in `self` and `other`.
266    ///
267    /// Specifically, the returned set contains all flags present in
268    /// `self`, except for the ones present in `other`.
269    ///
270    /// It is also conceptually equivalent to the "bit-clear" operation:
271    /// `flags & !other` (and this syntax is also supported).
272    ///
273    /// This is equivalent to using the `-` operator (e.g.
274    /// [`ops::Sub`]), as in `flags - other`.
275    ///
276    /// [`ops::Sub`]: https://doc.rust-lang.org/std/ops/trait.Sub.html
277    #[inline]
278    #[must_use]
279    pub const fn difference(self, other: Self) -> Self {
280        Self {
281            bits: self.bits & !other.bits,
282        }
283    }
284}
285
286impl std::ops::BitOr for GaspRangeBehavior {
287    type Output = Self;
288
289    /// Returns the union of the two sets of flags.
290    #[inline]
291    fn bitor(self, other: GaspRangeBehavior) -> Self {
292        Self {
293            bits: self.bits | other.bits,
294        }
295    }
296}
297
298impl std::ops::BitOrAssign for GaspRangeBehavior {
299    /// Adds the set of flags.
300    #[inline]
301    fn bitor_assign(&mut self, other: Self) {
302        self.bits |= other.bits;
303    }
304}
305
306impl std::ops::BitXor for GaspRangeBehavior {
307    type Output = Self;
308
309    /// Returns the left flags, but with all the right flags toggled.
310    #[inline]
311    fn bitxor(self, other: Self) -> Self {
312        Self {
313            bits: self.bits ^ other.bits,
314        }
315    }
316}
317
318impl std::ops::BitXorAssign for GaspRangeBehavior {
319    /// Toggles the set of flags.
320    #[inline]
321    fn bitxor_assign(&mut self, other: Self) {
322        self.bits ^= other.bits;
323    }
324}
325
326impl std::ops::BitAnd for GaspRangeBehavior {
327    type Output = Self;
328
329    /// Returns the intersection between the two sets of flags.
330    #[inline]
331    fn bitand(self, other: Self) -> Self {
332        Self {
333            bits: self.bits & other.bits,
334        }
335    }
336}
337
338impl std::ops::BitAndAssign for GaspRangeBehavior {
339    /// Disables all flags disabled in the set.
340    #[inline]
341    fn bitand_assign(&mut self, other: Self) {
342        self.bits &= other.bits;
343    }
344}
345
346impl std::ops::Sub for GaspRangeBehavior {
347    type Output = Self;
348
349    /// Returns the set difference of the two sets of flags.
350    #[inline]
351    fn sub(self, other: Self) -> Self {
352        Self {
353            bits: self.bits & !other.bits,
354        }
355    }
356}
357
358impl std::ops::SubAssign for GaspRangeBehavior {
359    /// Disables all flags enabled in the set.
360    #[inline]
361    fn sub_assign(&mut self, other: Self) {
362        self.bits &= !other.bits;
363    }
364}
365
366impl std::ops::Not for GaspRangeBehavior {
367    type Output = Self;
368
369    /// Returns the complement of this set of flags.
370    #[inline]
371    fn not(self) -> Self {
372        Self { bits: !self.bits } & Self::all()
373    }
374}
375
376impl std::fmt::Debug for GaspRangeBehavior {
377    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
378        let members: &[(&str, Self)] = &[
379            ("GASP_GRIDFIT", Self::GASP_GRIDFIT),
380            ("GASP_DOGRAY", Self::GASP_DOGRAY),
381            ("GASP_SYMMETRIC_GRIDFIT", Self::GASP_SYMMETRIC_GRIDFIT),
382            ("GASP_SYMMETRIC_SMOOTHING", Self::GASP_SYMMETRIC_SMOOTHING),
383        ];
384        let mut first = true;
385        for (name, value) in members {
386            if self.contains(*value) {
387                if !first {
388                    f.write_str(" | ")?;
389                }
390                first = false;
391                f.write_str(name)?;
392            }
393        }
394        if first {
395            f.write_str("(empty)")?;
396        }
397        Ok(())
398    }
399}
400
401impl std::fmt::Binary for GaspRangeBehavior {
402    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
403        std::fmt::Binary::fmt(&self.bits, f)
404    }
405}
406
407impl std::fmt::Octal for GaspRangeBehavior {
408    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
409        std::fmt::Octal::fmt(&self.bits, f)
410    }
411}
412
413impl std::fmt::LowerHex for GaspRangeBehavior {
414    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
415        std::fmt::LowerHex::fmt(&self.bits, f)
416    }
417}
418
419impl std::fmt::UpperHex for GaspRangeBehavior {
420    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
421        std::fmt::UpperHex::fmt(&self.bits, f)
422    }
423}
424
425impl font_types::Scalar for GaspRangeBehavior {
426    type Raw = <u16 as font_types::Scalar>::Raw;
427    fn to_raw(self) -> Self::Raw {
428        self.bits().to_raw()
429    }
430    fn from_raw(raw: Self::Raw) -> Self {
431        let t = <u16>::from_raw(raw);
432        Self::from_bits_truncate(t)
433    }
434}