1#[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 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#[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 pub fn version(&self) -> u16 {
50 let range = self.version_byte_range();
51 self.data.read_at(range.start).ok().unwrap()
52 }
53
54 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 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 pub range_max_ppem: BigEndian<u16>,
133 pub range_gasp_behavior: BigEndian<GaspRangeBehavior>,
135}
136
137impl GaspRange {
138 pub fn range_max_ppem(&self) -> u16 {
140 self.range_max_ppem.get()
141 }
142
143 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 pub const GASP_GRIDFIT: Self = Self { bits: 0x0001 };
181
182 pub const GASP_DOGRAY: Self = Self { bits: 0x0002 };
184
185 pub const GASP_SYMMETRIC_GRIDFIT: Self = Self { bits: 0x0004 };
188
189 pub const GASP_SYMMETRIC_SMOOTHING: Self = Self { bits: 0x0008 };
192}
193
194impl GaspRangeBehavior {
195 #[inline]
197 pub const fn empty() -> Self {
198 Self { bits: 0 }
199 }
200
201 #[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 #[inline]
214 pub const fn bits(&self) -> u16 {
215 self.bits
216 }
217
218 #[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 #[inline]
232 pub const fn from_bits_truncate(bits: u16) -> Self {
233 Self {
234 bits: bits & Self::all().bits,
235 }
236 }
237
238 #[inline]
240 pub const fn is_empty(&self) -> bool {
241 self.bits() == Self::empty().bits()
242 }
243
244 #[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 #[inline]
255 pub const fn contains(&self, other: Self) -> bool {
256 (self.bits & other.bits) == other.bits
257 }
258
259 #[inline]
261 pub fn insert(&mut self, other: Self) {
262 self.bits |= other.bits;
263 }
264
265 #[inline]
267 pub fn remove(&mut self, other: Self) {
268 self.bits &= !other.bits;
269 }
270
271 #[inline]
273 pub fn toggle(&mut self, other: Self) {
274 self.bits ^= other.bits;
275 }
276
277 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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}