1#[allow(unused_imports)]
6use crate::codegen_prelude::*;
7
8#[derive(Debug, Clone, Copy)]
10#[doc(hidden)]
11pub struct GvarMarker {
12 glyph_variation_data_offsets_byte_len: usize,
13}
14
15impl GvarMarker {
16 pub fn version_byte_range(&self) -> Range<usize> {
17 let start = 0;
18 start..start + MajorMinor::RAW_BYTE_LEN
19 }
20
21 pub fn axis_count_byte_range(&self) -> Range<usize> {
22 let start = self.version_byte_range().end;
23 start..start + u16::RAW_BYTE_LEN
24 }
25
26 pub fn shared_tuple_count_byte_range(&self) -> Range<usize> {
27 let start = self.axis_count_byte_range().end;
28 start..start + u16::RAW_BYTE_LEN
29 }
30
31 pub fn shared_tuples_offset_byte_range(&self) -> Range<usize> {
32 let start = self.shared_tuple_count_byte_range().end;
33 start..start + Offset32::RAW_BYTE_LEN
34 }
35
36 pub fn glyph_count_byte_range(&self) -> Range<usize> {
37 let start = self.shared_tuples_offset_byte_range().end;
38 start..start + u16::RAW_BYTE_LEN
39 }
40
41 pub fn flags_byte_range(&self) -> Range<usize> {
42 let start = self.glyph_count_byte_range().end;
43 start..start + GvarFlags::RAW_BYTE_LEN
44 }
45
46 pub fn glyph_variation_data_array_offset_byte_range(&self) -> Range<usize> {
47 let start = self.flags_byte_range().end;
48 start..start + u32::RAW_BYTE_LEN
49 }
50
51 pub fn glyph_variation_data_offsets_byte_range(&self) -> Range<usize> {
52 let start = self.glyph_variation_data_array_offset_byte_range().end;
53 start..start + self.glyph_variation_data_offsets_byte_len
54 }
55}
56
57impl MinByteRange for GvarMarker {
58 fn min_byte_range(&self) -> Range<usize> {
59 0..self.glyph_variation_data_offsets_byte_range().end
60 }
61}
62
63impl TopLevelTable for Gvar<'_> {
64 const TAG: Tag = Tag::new(b"gvar");
66}
67
68impl<'a> FontRead<'a> for Gvar<'a> {
69 fn read(data: FontData<'a>) -> Result<Self, ReadError> {
70 let mut cursor = data.cursor();
71 cursor.advance::<MajorMinor>();
72 cursor.advance::<u16>();
73 cursor.advance::<u16>();
74 cursor.advance::<Offset32>();
75 let glyph_count: u16 = cursor.read()?;
76 let flags: GvarFlags = cursor.read()?;
77 cursor.advance::<u32>();
78 let glyph_variation_data_offsets_byte_len = (transforms::add(glyph_count, 1_usize))
79 .checked_mul(<U16Or32 as ComputeSize>::compute_size(&flags)?)
80 .ok_or(ReadError::OutOfBounds)?;
81 cursor.advance_by(glyph_variation_data_offsets_byte_len);
82 cursor.finish(GvarMarker {
83 glyph_variation_data_offsets_byte_len,
84 })
85 }
86}
87
88pub type Gvar<'a> = TableRef<'a, GvarMarker>;
90
91#[allow(clippy::needless_lifetimes)]
92impl<'a> Gvar<'a> {
93 pub fn version(&self) -> MajorMinor {
95 let range = self.shape.version_byte_range();
96 self.data.read_at(range.start).unwrap()
97 }
98
99 pub fn axis_count(&self) -> u16 {
102 let range = self.shape.axis_count_byte_range();
103 self.data.read_at(range.start).unwrap()
104 }
105
106 pub fn shared_tuple_count(&self) -> u16 {
111 let range = self.shape.shared_tuple_count_byte_range();
112 self.data.read_at(range.start).unwrap()
113 }
114
115 pub fn shared_tuples_offset(&self) -> Offset32 {
117 let range = self.shape.shared_tuples_offset_byte_range();
118 self.data.read_at(range.start).unwrap()
119 }
120
121 pub fn shared_tuples(&self) -> Result<SharedTuples<'a>, ReadError> {
123 let data = self.data;
124 let args = (self.shared_tuple_count(), self.axis_count());
125 self.shared_tuples_offset().resolve_with_args(data, &args)
126 }
127
128 pub fn glyph_count(&self) -> u16 {
131 let range = self.shape.glyph_count_byte_range();
132 self.data.read_at(range.start).unwrap()
133 }
134
135 pub fn flags(&self) -> GvarFlags {
139 let range = self.shape.flags_byte_range();
140 self.data.read_at(range.start).unwrap()
141 }
142
143 pub fn glyph_variation_data_array_offset(&self) -> u32 {
146 let range = self.shape.glyph_variation_data_array_offset_byte_range();
147 self.data.read_at(range.start).unwrap()
148 }
149
150 pub fn glyph_variation_data_offsets(&self) -> ComputedArray<'a, U16Or32> {
153 let range = self.shape.glyph_variation_data_offsets_byte_range();
154 self.data.read_with_args(range, &self.flags()).unwrap()
155 }
156}
157
158#[cfg(feature = "experimental_traverse")]
159impl<'a> SomeTable<'a> for Gvar<'a> {
160 fn type_name(&self) -> &str {
161 "Gvar"
162 }
163 fn get_field(&self, idx: usize) -> Option<Field<'a>> {
164 match idx {
165 0usize => Some(Field::new("version", self.version())),
166 1usize => Some(Field::new("axis_count", self.axis_count())),
167 2usize => Some(Field::new("shared_tuple_count", self.shared_tuple_count())),
168 3usize => Some(Field::new(
169 "shared_tuples_offset",
170 FieldType::offset(self.shared_tuples_offset(), self.shared_tuples()),
171 )),
172 4usize => Some(Field::new("glyph_count", self.glyph_count())),
173 5usize => Some(Field::new("flags", self.flags())),
174 6usize => Some(Field::new(
175 "glyph_variation_data_array_offset",
176 self.glyph_variation_data_array_offset(),
177 )),
178 7usize => Some(Field::new(
179 "glyph_variation_data_offsets",
180 traversal::FieldType::Unknown,
181 )),
182 _ => None,
183 }
184 }
185}
186
187#[cfg(feature = "experimental_traverse")]
188#[allow(clippy::needless_lifetimes)]
189impl<'a> std::fmt::Debug for Gvar<'a> {
190 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
191 (self as &dyn SomeTable<'a>).fmt(f)
192 }
193}
194
195#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)]
196#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
197#[repr(transparent)]
198pub struct GvarFlags {
199 bits: u16,
200}
201
202impl GvarFlags {
203 pub const LONG_OFFSETS: Self = Self { bits: 1 };
205}
206
207impl GvarFlags {
208 #[inline]
210 pub const fn empty() -> Self {
211 Self { bits: 0 }
212 }
213
214 #[inline]
216 pub const fn all() -> Self {
217 Self {
218 bits: Self::LONG_OFFSETS.bits,
219 }
220 }
221
222 #[inline]
224 pub const fn bits(&self) -> u16 {
225 self.bits
226 }
227
228 #[inline]
231 pub const fn from_bits(bits: u16) -> Option<Self> {
232 if (bits & !Self::all().bits()) == 0 {
233 Some(Self { bits })
234 } else {
235 None
236 }
237 }
238
239 #[inline]
242 pub const fn from_bits_truncate(bits: u16) -> Self {
243 Self {
244 bits: bits & Self::all().bits,
245 }
246 }
247
248 #[inline]
250 pub const fn is_empty(&self) -> bool {
251 self.bits() == Self::empty().bits()
252 }
253
254 #[inline]
256 pub const fn intersects(&self, other: Self) -> bool {
257 !(Self {
258 bits: self.bits & other.bits,
259 })
260 .is_empty()
261 }
262
263 #[inline]
265 pub const fn contains(&self, other: Self) -> bool {
266 (self.bits & other.bits) == other.bits
267 }
268
269 #[inline]
271 pub fn insert(&mut self, other: Self) {
272 self.bits |= other.bits;
273 }
274
275 #[inline]
277 pub fn remove(&mut self, other: Self) {
278 self.bits &= !other.bits;
279 }
280
281 #[inline]
283 pub fn toggle(&mut self, other: Self) {
284 self.bits ^= other.bits;
285 }
286
287 #[inline]
298 #[must_use]
299 pub const fn intersection(self, other: Self) -> Self {
300 Self {
301 bits: self.bits & other.bits,
302 }
303 }
304
305 #[inline]
316 #[must_use]
317 pub const fn union(self, other: Self) -> Self {
318 Self {
319 bits: self.bits | other.bits,
320 }
321 }
322
323 #[inline]
336 #[must_use]
337 pub const fn difference(self, other: Self) -> Self {
338 Self {
339 bits: self.bits & !other.bits,
340 }
341 }
342}
343
344impl std::ops::BitOr for GvarFlags {
345 type Output = Self;
346
347 #[inline]
349 fn bitor(self, other: GvarFlags) -> Self {
350 Self {
351 bits: self.bits | other.bits,
352 }
353 }
354}
355
356impl std::ops::BitOrAssign for GvarFlags {
357 #[inline]
359 fn bitor_assign(&mut self, other: Self) {
360 self.bits |= other.bits;
361 }
362}
363
364impl std::ops::BitXor for GvarFlags {
365 type Output = Self;
366
367 #[inline]
369 fn bitxor(self, other: Self) -> Self {
370 Self {
371 bits: self.bits ^ other.bits,
372 }
373 }
374}
375
376impl std::ops::BitXorAssign for GvarFlags {
377 #[inline]
379 fn bitxor_assign(&mut self, other: Self) {
380 self.bits ^= other.bits;
381 }
382}
383
384impl std::ops::BitAnd for GvarFlags {
385 type Output = Self;
386
387 #[inline]
389 fn bitand(self, other: Self) -> Self {
390 Self {
391 bits: self.bits & other.bits,
392 }
393 }
394}
395
396impl std::ops::BitAndAssign for GvarFlags {
397 #[inline]
399 fn bitand_assign(&mut self, other: Self) {
400 self.bits &= other.bits;
401 }
402}
403
404impl std::ops::Sub for GvarFlags {
405 type Output = Self;
406
407 #[inline]
409 fn sub(self, other: Self) -> Self {
410 Self {
411 bits: self.bits & !other.bits,
412 }
413 }
414}
415
416impl std::ops::SubAssign for GvarFlags {
417 #[inline]
419 fn sub_assign(&mut self, other: Self) {
420 self.bits &= !other.bits;
421 }
422}
423
424impl std::ops::Not for GvarFlags {
425 type Output = Self;
426
427 #[inline]
429 fn not(self) -> Self {
430 Self { bits: !self.bits } & Self::all()
431 }
432}
433
434impl std::fmt::Debug for GvarFlags {
435 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
436 let members: &[(&str, Self)] = &[("LONG_OFFSETS", Self::LONG_OFFSETS)];
437 let mut first = true;
438 for (name, value) in members {
439 if self.contains(*value) {
440 if !first {
441 f.write_str(" | ")?;
442 }
443 first = false;
444 f.write_str(name)?;
445 }
446 }
447 if first {
448 f.write_str("(empty)")?;
449 }
450 Ok(())
451 }
452}
453
454impl std::fmt::Binary for GvarFlags {
455 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
456 std::fmt::Binary::fmt(&self.bits, f)
457 }
458}
459
460impl std::fmt::Octal for GvarFlags {
461 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
462 std::fmt::Octal::fmt(&self.bits, f)
463 }
464}
465
466impl std::fmt::LowerHex for GvarFlags {
467 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
468 std::fmt::LowerHex::fmt(&self.bits, f)
469 }
470}
471
472impl std::fmt::UpperHex for GvarFlags {
473 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
474 std::fmt::UpperHex::fmt(&self.bits, f)
475 }
476}
477
478impl font_types::Scalar for GvarFlags {
479 type Raw = <u16 as font_types::Scalar>::Raw;
480 fn to_raw(self) -> Self::Raw {
481 self.bits().to_raw()
482 }
483 fn from_raw(raw: Self::Raw) -> Self {
484 let t = <u16>::from_raw(raw);
485 Self::from_bits_truncate(t)
486 }
487}
488
489#[cfg(feature = "experimental_traverse")]
490impl<'a> From<GvarFlags> for FieldType<'a> {
491 fn from(src: GvarFlags) -> FieldType<'a> {
492 src.bits().into()
493 }
494}
495
496#[derive(Debug, Clone, Copy)]
498#[doc(hidden)]
499pub struct SharedTuplesMarker {
500 axis_count: u16,
501 tuples_byte_len: usize,
502}
503
504impl SharedTuplesMarker {
505 pub fn tuples_byte_range(&self) -> Range<usize> {
506 let start = 0;
507 start..start + self.tuples_byte_len
508 }
509}
510
511impl MinByteRange for SharedTuplesMarker {
512 fn min_byte_range(&self) -> Range<usize> {
513 0..self.tuples_byte_range().end
514 }
515}
516
517impl ReadArgs for SharedTuples<'_> {
518 type Args = (u16, u16);
519}
520
521impl<'a> FontReadWithArgs<'a> for SharedTuples<'a> {
522 fn read_with_args(data: FontData<'a>, args: &(u16, u16)) -> Result<Self, ReadError> {
523 let (shared_tuple_count, axis_count) = *args;
524 let mut cursor = data.cursor();
525 let tuples_byte_len = (shared_tuple_count as usize)
526 .checked_mul(<Tuple as ComputeSize>::compute_size(&axis_count)?)
527 .ok_or(ReadError::OutOfBounds)?;
528 cursor.advance_by(tuples_byte_len);
529 cursor.finish(SharedTuplesMarker {
530 axis_count,
531 tuples_byte_len,
532 })
533 }
534}
535
536impl<'a> SharedTuples<'a> {
537 pub fn read(
542 data: FontData<'a>,
543 shared_tuple_count: u16,
544 axis_count: u16,
545 ) -> Result<Self, ReadError> {
546 let args = (shared_tuple_count, axis_count);
547 Self::read_with_args(data, &args)
548 }
549}
550
551pub type SharedTuples<'a> = TableRef<'a, SharedTuplesMarker>;
553
554#[allow(clippy::needless_lifetimes)]
555impl<'a> SharedTuples<'a> {
556 pub fn tuples(&self) -> ComputedArray<'a, Tuple<'a>> {
557 let range = self.shape.tuples_byte_range();
558 self.data.read_with_args(range, &self.axis_count()).unwrap()
559 }
560
561 pub(crate) fn axis_count(&self) -> u16 {
562 self.shape.axis_count
563 }
564}
565
566#[cfg(feature = "experimental_traverse")]
567impl<'a> SomeTable<'a> for SharedTuples<'a> {
568 fn type_name(&self) -> &str {
569 "SharedTuples"
570 }
571 fn get_field(&self, idx: usize) -> Option<Field<'a>> {
572 match idx {
573 0usize => Some(Field::new(
574 "tuples",
575 traversal::FieldType::computed_array("Tuple", self.tuples(), self.offset_data()),
576 )),
577 _ => None,
578 }
579 }
580}
581
582#[cfg(feature = "experimental_traverse")]
583#[allow(clippy::needless_lifetimes)]
584impl<'a> std::fmt::Debug for SharedTuples<'a> {
585 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
586 (self as &dyn SomeTable<'a>).fmt(f)
587 }
588}
589
590#[derive(Debug, Clone, Copy)]
592#[doc(hidden)]
593pub struct GlyphVariationDataHeaderMarker {
594 tuple_variation_headers_byte_len: usize,
595}
596
597impl GlyphVariationDataHeaderMarker {
598 pub fn tuple_variation_count_byte_range(&self) -> Range<usize> {
599 let start = 0;
600 start..start + TupleVariationCount::RAW_BYTE_LEN
601 }
602
603 pub fn serialized_data_offset_byte_range(&self) -> Range<usize> {
604 let start = self.tuple_variation_count_byte_range().end;
605 start..start + Offset16::RAW_BYTE_LEN
606 }
607
608 pub fn tuple_variation_headers_byte_range(&self) -> Range<usize> {
609 let start = self.serialized_data_offset_byte_range().end;
610 start..start + self.tuple_variation_headers_byte_len
611 }
612}
613
614impl MinByteRange for GlyphVariationDataHeaderMarker {
615 fn min_byte_range(&self) -> Range<usize> {
616 0..self.tuple_variation_headers_byte_range().end
617 }
618}
619
620impl<'a> FontRead<'a> for GlyphVariationDataHeader<'a> {
621 fn read(data: FontData<'a>) -> Result<Self, ReadError> {
622 let mut cursor = data.cursor();
623 cursor.advance::<TupleVariationCount>();
624 cursor.advance::<Offset16>();
625 let tuple_variation_headers_byte_len = cursor.remaining_bytes();
626 cursor.advance_by(tuple_variation_headers_byte_len);
627 cursor.finish(GlyphVariationDataHeaderMarker {
628 tuple_variation_headers_byte_len,
629 })
630 }
631}
632
633pub type GlyphVariationDataHeader<'a> = TableRef<'a, GlyphVariationDataHeaderMarker>;
635
636#[allow(clippy::needless_lifetimes)]
637impl<'a> GlyphVariationDataHeader<'a> {
638 pub fn tuple_variation_count(&self) -> TupleVariationCount {
643 let range = self.shape.tuple_variation_count_byte_range();
644 self.data.read_at(range.start).unwrap()
645 }
646
647 pub fn serialized_data_offset(&self) -> Offset16 {
650 let range = self.shape.serialized_data_offset_byte_range();
651 self.data.read_at(range.start).unwrap()
652 }
653
654 pub fn serialized_data(&self) -> Result<FontData<'a>, ReadError> {
656 let data = self.data;
657 self.serialized_data_offset().resolve(data)
658 }
659
660 pub fn tuple_variation_headers(&self) -> VarLenArray<'a, TupleVariationHeader> {
662 let range = self.shape.tuple_variation_headers_byte_range();
663 VarLenArray::read(self.data.split_off(range.start).unwrap()).unwrap()
664 }
665}
666
667#[cfg(feature = "experimental_traverse")]
668impl<'a> SomeTable<'a> for GlyphVariationDataHeader<'a> {
669 fn type_name(&self) -> &str {
670 "GlyphVariationDataHeader"
671 }
672 fn get_field(&self, idx: usize) -> Option<Field<'a>> {
673 match idx {
674 0usize => Some(Field::new(
675 "tuple_variation_count",
676 traversal::FieldType::Unknown,
677 )),
678 1usize => Some(Field::new(
679 "serialized_data_offset",
680 traversal::FieldType::Unknown,
681 )),
682 2usize => Some(Field::new(
683 "tuple_variation_headers",
684 traversal::FieldType::Unknown,
685 )),
686 _ => None,
687 }
688 }
689}
690
691#[cfg(feature = "experimental_traverse")]
692#[allow(clippy::needless_lifetimes)]
693impl<'a> std::fmt::Debug for GlyphVariationDataHeader<'a> {
694 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
695 (self as &dyn SomeTable<'a>).fmt(f)
696 }
697}