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#[cfg(feature = "experimental_traverse")]
98impl<'a> SomeTable<'a> for Gasp<'a> {
99    fn type_name(&self) -> &str {
100        "Gasp"
101    }
102    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
103        match idx {
104            0usize => Some(Field::new("version", self.version())),
105            1usize => Some(Field::new("num_ranges", self.num_ranges())),
106            2usize => Some(Field::new(
107                "gasp_ranges",
108                traversal::FieldType::array_of_records(
109                    stringify!(GaspRange),
110                    self.gasp_ranges(),
111                    self.offset_data(),
112                ),
113            )),
114            _ => None,
115        }
116    }
117}
118
119#[cfg(feature = "experimental_traverse")]
120#[allow(clippy::needless_lifetimes)]
121impl<'a> std::fmt::Debug for Gasp<'a> {
122    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
123        (self as &dyn SomeTable<'a>).fmt(f)
124    }
125}
126
127#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
128#[repr(C)]
129#[repr(packed)]
130pub struct GaspRange {
131    /// Upper limit of range, in PPEM
132    pub range_max_ppem: BigEndian<u16>,
133    /// Flags describing desired rasterizer behavior.
134    pub range_gasp_behavior: BigEndian<GaspRangeBehavior>,
135}
136
137impl GaspRange {
138    /// Upper limit of range, in PPEM
139    pub fn range_max_ppem(&self) -> u16 {
140        self.range_max_ppem.get()
141    }
142
143    /// Flags describing desired rasterizer behavior.
144    pub fn range_gasp_behavior(&self) -> GaspRangeBehavior {
145        self.range_gasp_behavior.get()
146    }
147}
148
149impl FixedSize for GaspRange {
150    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + GaspRangeBehavior::RAW_BYTE_LEN;
151}
152
153#[cfg(feature = "experimental_traverse")]
154impl<'a> SomeRecord<'a> for GaspRange {
155    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
156        RecordResolver {
157            name: "GaspRange",
158            get_field: Box::new(move |idx, _data| match idx {
159                0usize => Some(Field::new("range_max_ppem", self.range_max_ppem())),
160                1usize => Some(Field::new(
161                    "range_gasp_behavior",
162                    self.range_gasp_behavior(),
163                )),
164                _ => None,
165            }),
166            data,
167        }
168    }
169}
170
171#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)]
172#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
173#[repr(transparent)]
174pub struct GaspRangeBehavior {
175    bits: u16,
176}
177
178impl GaspRangeBehavior {
179    /// Use gridfitting
180    pub const GASP_GRIDFIT: Self = Self { bits: 0x0001 };
181
182    /// Use grayscale rendering
183    pub const GASP_DOGRAY: Self = Self { bits: 0x0002 };
184
185    /// Use gridfitting with ClearType symmetric smoothing Only
186    /// supported in version 1 'gasp'
187    pub const GASP_SYMMETRIC_GRIDFIT: Self = Self { bits: 0x0004 };
188
189    /// Use smoothing along multiple axes with ClearType® Only
190    /// supported in version 1 'gasp'
191    pub const GASP_SYMMETRIC_SMOOTHING: Self = Self { bits: 0x0008 };
192}
193
194impl GaspRangeBehavior {
195    ///  Returns an empty set of flags.
196    #[inline]
197    pub const fn empty() -> Self {
198        Self { bits: 0 }
199    }
200
201    /// Returns the set containing all flags.
202    #[inline]
203    pub const fn all() -> Self {
204        Self {
205            bits: Self::GASP_GRIDFIT.bits
206                | Self::GASP_DOGRAY.bits
207                | Self::GASP_SYMMETRIC_GRIDFIT.bits
208                | Self::GASP_SYMMETRIC_SMOOTHING.bits,
209        }
210    }
211
212    /// Returns the raw value of the flags currently stored.
213    #[inline]
214    pub const fn bits(&self) -> u16 {
215        self.bits
216    }
217
218    /// Convert from underlying bit representation, unless that
219    /// representation contains bits that do not correspond to a flag.
220    #[inline]
221    pub const fn from_bits(bits: u16) -> Option<Self> {
222        if (bits & !Self::all().bits()) == 0 {
223            Some(Self { bits })
224        } else {
225            None
226        }
227    }
228
229    /// Convert from underlying bit representation, dropping any bits
230    /// that do not correspond to flags.
231    #[inline]
232    pub const fn from_bits_truncate(bits: u16) -> Self {
233        Self {
234            bits: bits & Self::all().bits,
235        }
236    }
237
238    /// Returns `true` if no flags are currently stored.
239    #[inline]
240    pub const fn is_empty(&self) -> bool {
241        self.bits() == Self::empty().bits()
242    }
243
244    /// Returns `true` if there are flags common to both `self` and `other`.
245    #[inline]
246    pub const fn intersects(&self, other: Self) -> bool {
247        !(Self {
248            bits: self.bits & other.bits,
249        })
250        .is_empty()
251    }
252
253    /// Returns `true` if all of the flags in `other` are contained within `self`.
254    #[inline]
255    pub const fn contains(&self, other: Self) -> bool {
256        (self.bits & other.bits) == other.bits
257    }
258
259    /// Inserts the specified flags in-place.
260    #[inline]
261    pub fn insert(&mut self, other: Self) {
262        self.bits |= other.bits;
263    }
264
265    /// Removes the specified flags in-place.
266    #[inline]
267    pub fn remove(&mut self, other: Self) {
268        self.bits &= !other.bits;
269    }
270
271    /// Toggles the specified flags in-place.
272    #[inline]
273    pub fn toggle(&mut self, other: Self) {
274        self.bits ^= other.bits;
275    }
276
277    /// Returns the intersection between the flags in `self` and
278    /// `other`.
279    ///
280    /// Specifically, the returned set contains only the flags which are
281    /// present in *both* `self` *and* `other`.
282    ///
283    /// This is equivalent to using the `&` operator (e.g.
284    /// [`ops::BitAnd`]), as in `flags & other`.
285    ///
286    /// [`ops::BitAnd`]: https://doc.rust-lang.org/std/ops/trait.BitAnd.html
287    #[inline]
288    #[must_use]
289    pub const fn intersection(self, other: Self) -> Self {
290        Self {
291            bits: self.bits & other.bits,
292        }
293    }
294
295    /// Returns the union of between the flags in `self` and `other`.
296    ///
297    /// Specifically, the returned set contains all flags which are
298    /// present in *either* `self` *or* `other`, including any which are
299    /// present in both.
300    ///
301    /// This is equivalent to using the `|` operator (e.g.
302    /// [`ops::BitOr`]), as in `flags | other`.
303    ///
304    /// [`ops::BitOr`]: https://doc.rust-lang.org/std/ops/trait.BitOr.html
305    #[inline]
306    #[must_use]
307    pub const fn union(self, other: Self) -> Self {
308        Self {
309            bits: self.bits | other.bits,
310        }
311    }
312
313    /// Returns the difference between the flags in `self` and `other`.
314    ///
315    /// Specifically, the returned set contains all flags present in
316    /// `self`, except for the ones present in `other`.
317    ///
318    /// It is also conceptually equivalent to the "bit-clear" operation:
319    /// `flags & !other` (and this syntax is also supported).
320    ///
321    /// This is equivalent to using the `-` operator (e.g.
322    /// [`ops::Sub`]), as in `flags - other`.
323    ///
324    /// [`ops::Sub`]: https://doc.rust-lang.org/std/ops/trait.Sub.html
325    #[inline]
326    #[must_use]
327    pub const fn difference(self, other: Self) -> Self {
328        Self {
329            bits: self.bits & !other.bits,
330        }
331    }
332}
333
334impl std::ops::BitOr for GaspRangeBehavior {
335    type Output = Self;
336
337    /// Returns the union of the two sets of flags.
338    #[inline]
339    fn bitor(self, other: GaspRangeBehavior) -> Self {
340        Self {
341            bits: self.bits | other.bits,
342        }
343    }
344}
345
346impl std::ops::BitOrAssign for GaspRangeBehavior {
347    /// Adds the set of flags.
348    #[inline]
349    fn bitor_assign(&mut self, other: Self) {
350        self.bits |= other.bits;
351    }
352}
353
354impl std::ops::BitXor for GaspRangeBehavior {
355    type Output = Self;
356
357    /// Returns the left flags, but with all the right flags toggled.
358    #[inline]
359    fn bitxor(self, other: Self) -> Self {
360        Self {
361            bits: self.bits ^ other.bits,
362        }
363    }
364}
365
366impl std::ops::BitXorAssign for GaspRangeBehavior {
367    /// Toggles the set of flags.
368    #[inline]
369    fn bitxor_assign(&mut self, other: Self) {
370        self.bits ^= other.bits;
371    }
372}
373
374impl std::ops::BitAnd for GaspRangeBehavior {
375    type Output = Self;
376
377    /// Returns the intersection between the two sets of flags.
378    #[inline]
379    fn bitand(self, other: Self) -> Self {
380        Self {
381            bits: self.bits & other.bits,
382        }
383    }
384}
385
386impl std::ops::BitAndAssign for GaspRangeBehavior {
387    /// Disables all flags disabled in the set.
388    #[inline]
389    fn bitand_assign(&mut self, other: Self) {
390        self.bits &= other.bits;
391    }
392}
393
394impl std::ops::Sub for GaspRangeBehavior {
395    type Output = Self;
396
397    /// Returns the set difference of the two sets of flags.
398    #[inline]
399    fn sub(self, other: Self) -> Self {
400        Self {
401            bits: self.bits & !other.bits,
402        }
403    }
404}
405
406impl std::ops::SubAssign for GaspRangeBehavior {
407    /// Disables all flags enabled in the set.
408    #[inline]
409    fn sub_assign(&mut self, other: Self) {
410        self.bits &= !other.bits;
411    }
412}
413
414impl std::ops::Not for GaspRangeBehavior {
415    type Output = Self;
416
417    /// Returns the complement of this set of flags.
418    #[inline]
419    fn not(self) -> Self {
420        Self { bits: !self.bits } & Self::all()
421    }
422}
423
424impl std::fmt::Debug for GaspRangeBehavior {
425    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
426        let members: &[(&str, Self)] = &[
427            ("GASP_GRIDFIT", Self::GASP_GRIDFIT),
428            ("GASP_DOGRAY", Self::GASP_DOGRAY),
429            ("GASP_SYMMETRIC_GRIDFIT", Self::GASP_SYMMETRIC_GRIDFIT),
430            ("GASP_SYMMETRIC_SMOOTHING", Self::GASP_SYMMETRIC_SMOOTHING),
431        ];
432        let mut first = true;
433        for (name, value) in members {
434            if self.contains(*value) {
435                if !first {
436                    f.write_str(" | ")?;
437                }
438                first = false;
439                f.write_str(name)?;
440            }
441        }
442        if first {
443            f.write_str("(empty)")?;
444        }
445        Ok(())
446    }
447}
448
449impl std::fmt::Binary for GaspRangeBehavior {
450    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
451        std::fmt::Binary::fmt(&self.bits, f)
452    }
453}
454
455impl std::fmt::Octal for GaspRangeBehavior {
456    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
457        std::fmt::Octal::fmt(&self.bits, f)
458    }
459}
460
461impl std::fmt::LowerHex for GaspRangeBehavior {
462    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
463        std::fmt::LowerHex::fmt(&self.bits, f)
464    }
465}
466
467impl std::fmt::UpperHex for GaspRangeBehavior {
468    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
469        std::fmt::UpperHex::fmt(&self.bits, f)
470    }
471}
472
473impl font_types::Scalar for GaspRangeBehavior {
474    type Raw = <u16 as font_types::Scalar>::Raw;
475    fn to_raw(self) -> Self::Raw {
476        self.bits().to_raw()
477    }
478    fn from_raw(raw: Self::Raw) -> Self {
479        let t = <u16>::from_raw(raw);
480        Self::from_bits_truncate(t)
481    }
482}
483
484#[cfg(feature = "experimental_traverse")]
485impl<'a> From<GaspRangeBehavior> for FieldType<'a> {
486    fn from(src: GaspRangeBehavior) -> FieldType<'a> {
487        src.bits().into()
488    }
489}