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