1#[allow(unused_imports)]
6use crate::codegen_prelude::*;
7
8#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11#[repr(transparent)]
12pub struct MacStyle {
13 bits: u16,
14}
15
16impl MacStyle {
17 pub const BOLD: Self = Self { bits: 0x0001 };
19
20 pub const ITALIC: Self = Self { bits: 0x0002 };
22
23 pub const UNDERLINE: Self = Self { bits: 0x0004 };
25
26 pub const OUTLINE: Self = Self { bits: 0x0008 };
28
29 pub const SHADOW: Self = Self { bits: 0x0010 };
31
32 pub const CONDENSED: Self = Self { bits: 0x0020 };
34
35 pub const EXTENDED: Self = Self { bits: 0x0040 };
37}
38
39impl MacStyle {
40 #[inline]
42 pub const fn empty() -> Self {
43 Self { bits: 0 }
44 }
45
46 #[inline]
48 pub const fn all() -> Self {
49 Self {
50 bits: Self::BOLD.bits
51 | Self::ITALIC.bits
52 | Self::UNDERLINE.bits
53 | Self::OUTLINE.bits
54 | Self::SHADOW.bits
55 | Self::CONDENSED.bits
56 | Self::EXTENDED.bits,
57 }
58 }
59
60 #[inline]
62 pub const fn bits(&self) -> u16 {
63 self.bits
64 }
65
66 #[inline]
69 pub const fn from_bits(bits: u16) -> Option<Self> {
70 if (bits & !Self::all().bits()) == 0 {
71 Some(Self { bits })
72 } else {
73 None
74 }
75 }
76
77 #[inline]
80 pub const fn from_bits_truncate(bits: u16) -> Self {
81 Self {
82 bits: bits & Self::all().bits,
83 }
84 }
85
86 #[inline]
88 pub const fn is_empty(&self) -> bool {
89 self.bits() == Self::empty().bits()
90 }
91
92 #[inline]
94 pub const fn intersects(&self, other: Self) -> bool {
95 !(Self {
96 bits: self.bits & other.bits,
97 })
98 .is_empty()
99 }
100
101 #[inline]
103 pub const fn contains(&self, other: Self) -> bool {
104 (self.bits & other.bits) == other.bits
105 }
106
107 #[inline]
109 pub fn insert(&mut self, other: Self) {
110 self.bits |= other.bits;
111 }
112
113 #[inline]
115 pub fn remove(&mut self, other: Self) {
116 self.bits &= !other.bits;
117 }
118
119 #[inline]
121 pub fn toggle(&mut self, other: Self) {
122 self.bits ^= other.bits;
123 }
124
125 #[inline]
136 #[must_use]
137 pub const fn intersection(self, other: Self) -> Self {
138 Self {
139 bits: self.bits & other.bits,
140 }
141 }
142
143 #[inline]
154 #[must_use]
155 pub const fn union(self, other: Self) -> Self {
156 Self {
157 bits: self.bits | other.bits,
158 }
159 }
160
161 #[inline]
174 #[must_use]
175 pub const fn difference(self, other: Self) -> Self {
176 Self {
177 bits: self.bits & !other.bits,
178 }
179 }
180}
181
182impl std::ops::BitOr for MacStyle {
183 type Output = Self;
184
185 #[inline]
187 fn bitor(self, other: MacStyle) -> Self {
188 Self {
189 bits: self.bits | other.bits,
190 }
191 }
192}
193
194impl std::ops::BitOrAssign for MacStyle {
195 #[inline]
197 fn bitor_assign(&mut self, other: Self) {
198 self.bits |= other.bits;
199 }
200}
201
202impl std::ops::BitXor for MacStyle {
203 type Output = Self;
204
205 #[inline]
207 fn bitxor(self, other: Self) -> Self {
208 Self {
209 bits: self.bits ^ other.bits,
210 }
211 }
212}
213
214impl std::ops::BitXorAssign for MacStyle {
215 #[inline]
217 fn bitxor_assign(&mut self, other: Self) {
218 self.bits ^= other.bits;
219 }
220}
221
222impl std::ops::BitAnd for MacStyle {
223 type Output = Self;
224
225 #[inline]
227 fn bitand(self, other: Self) -> Self {
228 Self {
229 bits: self.bits & other.bits,
230 }
231 }
232}
233
234impl std::ops::BitAndAssign for MacStyle {
235 #[inline]
237 fn bitand_assign(&mut self, other: Self) {
238 self.bits &= other.bits;
239 }
240}
241
242impl std::ops::Sub for MacStyle {
243 type Output = Self;
244
245 #[inline]
247 fn sub(self, other: Self) -> Self {
248 Self {
249 bits: self.bits & !other.bits,
250 }
251 }
252}
253
254impl std::ops::SubAssign for MacStyle {
255 #[inline]
257 fn sub_assign(&mut self, other: Self) {
258 self.bits &= !other.bits;
259 }
260}
261
262impl std::ops::Not for MacStyle {
263 type Output = Self;
264
265 #[inline]
267 fn not(self) -> Self {
268 Self { bits: !self.bits } & Self::all()
269 }
270}
271
272impl std::fmt::Debug for MacStyle {
273 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
274 let members: &[(&str, Self)] = &[
275 ("BOLD", Self::BOLD),
276 ("ITALIC", Self::ITALIC),
277 ("UNDERLINE", Self::UNDERLINE),
278 ("OUTLINE", Self::OUTLINE),
279 ("SHADOW", Self::SHADOW),
280 ("CONDENSED", Self::CONDENSED),
281 ("EXTENDED", Self::EXTENDED),
282 ];
283 let mut first = true;
284 for (name, value) in members {
285 if self.contains(*value) {
286 if !first {
287 f.write_str(" | ")?;
288 }
289 first = false;
290 f.write_str(name)?;
291 }
292 }
293 if first {
294 f.write_str("(empty)")?;
295 }
296 Ok(())
297 }
298}
299
300impl std::fmt::Binary for MacStyle {
301 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
302 std::fmt::Binary::fmt(&self.bits, f)
303 }
304}
305
306impl std::fmt::Octal for MacStyle {
307 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
308 std::fmt::Octal::fmt(&self.bits, f)
309 }
310}
311
312impl std::fmt::LowerHex for MacStyle {
313 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
314 std::fmt::LowerHex::fmt(&self.bits, f)
315 }
316}
317
318impl std::fmt::UpperHex for MacStyle {
319 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
320 std::fmt::UpperHex::fmt(&self.bits, f)
321 }
322}
323
324impl font_types::Scalar for MacStyle {
325 type Raw = <u16 as font_types::Scalar>::Raw;
326 fn to_raw(self) -> Self::Raw {
327 self.bits().to_raw()
328 }
329 fn from_raw(raw: Self::Raw) -> Self {
330 let t = <u16>::from_raw(raw);
331 Self::from_bits_truncate(t)
332 }
333}
334
335#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)]
337#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
338#[repr(transparent)]
339pub struct Flags {
340 bits: u16,
341}
342
343impl Flags {
344 pub const BASELINE_AT_Y_0: Self = Self { bits: 0x0001 };
346
347 pub const LSB_AT_X_0: Self = Self { bits: 0x0002 };
349
350 pub const INSTRUCTIONS_MAY_DEPEND_ON_POINT_SIZE: Self = Self { bits: 0x0004 };
352
353 pub const FORCE_INTEGER_PPEM: Self = Self { bits: 0x0008 };
355
356 pub const INSTRUCTIONS_MAY_ALTER_ADVANCE_WIDTH: Self = Self { bits: 0x0010 };
358
359 pub const LOSSLESS_TRANSFORMED_FONT_DATA: Self = Self { bits: 0x0800 };
361
362 pub const CONVERTED_FONT: Self = Self { bits: 0x1000 };
364
365 pub const OPTIMIZED_FOR_CLEARTYPE: Self = Self { bits: 0x2000 };
367
368 pub const LAST_RESORT_FONT: Self = Self { bits: 0x4000 };
370}
371
372impl Flags {
373 #[inline]
375 pub const fn empty() -> Self {
376 Self { bits: 0 }
377 }
378
379 #[inline]
381 pub const fn all() -> Self {
382 Self {
383 bits: Self::BASELINE_AT_Y_0.bits
384 | Self::LSB_AT_X_0.bits
385 | Self::INSTRUCTIONS_MAY_DEPEND_ON_POINT_SIZE.bits
386 | Self::FORCE_INTEGER_PPEM.bits
387 | Self::INSTRUCTIONS_MAY_ALTER_ADVANCE_WIDTH.bits
388 | Self::LOSSLESS_TRANSFORMED_FONT_DATA.bits
389 | Self::CONVERTED_FONT.bits
390 | Self::OPTIMIZED_FOR_CLEARTYPE.bits
391 | Self::LAST_RESORT_FONT.bits,
392 }
393 }
394
395 #[inline]
397 pub const fn bits(&self) -> u16 {
398 self.bits
399 }
400
401 #[inline]
404 pub const fn from_bits(bits: u16) -> Option<Self> {
405 if (bits & !Self::all().bits()) == 0 {
406 Some(Self { bits })
407 } else {
408 None
409 }
410 }
411
412 #[inline]
415 pub const fn from_bits_truncate(bits: u16) -> Self {
416 Self {
417 bits: bits & Self::all().bits,
418 }
419 }
420
421 #[inline]
423 pub const fn is_empty(&self) -> bool {
424 self.bits() == Self::empty().bits()
425 }
426
427 #[inline]
429 pub const fn intersects(&self, other: Self) -> bool {
430 !(Self {
431 bits: self.bits & other.bits,
432 })
433 .is_empty()
434 }
435
436 #[inline]
438 pub const fn contains(&self, other: Self) -> bool {
439 (self.bits & other.bits) == other.bits
440 }
441
442 #[inline]
444 pub fn insert(&mut self, other: Self) {
445 self.bits |= other.bits;
446 }
447
448 #[inline]
450 pub fn remove(&mut self, other: Self) {
451 self.bits &= !other.bits;
452 }
453
454 #[inline]
456 pub fn toggle(&mut self, other: Self) {
457 self.bits ^= other.bits;
458 }
459
460 #[inline]
471 #[must_use]
472 pub const fn intersection(self, other: Self) -> Self {
473 Self {
474 bits: self.bits & other.bits,
475 }
476 }
477
478 #[inline]
489 #[must_use]
490 pub const fn union(self, other: Self) -> Self {
491 Self {
492 bits: self.bits | other.bits,
493 }
494 }
495
496 #[inline]
509 #[must_use]
510 pub const fn difference(self, other: Self) -> Self {
511 Self {
512 bits: self.bits & !other.bits,
513 }
514 }
515}
516
517impl std::ops::BitOr for Flags {
518 type Output = Self;
519
520 #[inline]
522 fn bitor(self, other: Flags) -> Self {
523 Self {
524 bits: self.bits | other.bits,
525 }
526 }
527}
528
529impl std::ops::BitOrAssign for Flags {
530 #[inline]
532 fn bitor_assign(&mut self, other: Self) {
533 self.bits |= other.bits;
534 }
535}
536
537impl std::ops::BitXor for Flags {
538 type Output = Self;
539
540 #[inline]
542 fn bitxor(self, other: Self) -> Self {
543 Self {
544 bits: self.bits ^ other.bits,
545 }
546 }
547}
548
549impl std::ops::BitXorAssign for Flags {
550 #[inline]
552 fn bitxor_assign(&mut self, other: Self) {
553 self.bits ^= other.bits;
554 }
555}
556
557impl std::ops::BitAnd for Flags {
558 type Output = Self;
559
560 #[inline]
562 fn bitand(self, other: Self) -> Self {
563 Self {
564 bits: self.bits & other.bits,
565 }
566 }
567}
568
569impl std::ops::BitAndAssign for Flags {
570 #[inline]
572 fn bitand_assign(&mut self, other: Self) {
573 self.bits &= other.bits;
574 }
575}
576
577impl std::ops::Sub for Flags {
578 type Output = Self;
579
580 #[inline]
582 fn sub(self, other: Self) -> Self {
583 Self {
584 bits: self.bits & !other.bits,
585 }
586 }
587}
588
589impl std::ops::SubAssign for Flags {
590 #[inline]
592 fn sub_assign(&mut self, other: Self) {
593 self.bits &= !other.bits;
594 }
595}
596
597impl std::ops::Not for Flags {
598 type Output = Self;
599
600 #[inline]
602 fn not(self) -> Self {
603 Self { bits: !self.bits } & Self::all()
604 }
605}
606
607impl std::fmt::Debug for Flags {
608 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
609 let members: &[(&str, Self)] = &[
610 ("BASELINE_AT_Y_0", Self::BASELINE_AT_Y_0),
611 ("LSB_AT_X_0", Self::LSB_AT_X_0),
612 (
613 "INSTRUCTIONS_MAY_DEPEND_ON_POINT_SIZE",
614 Self::INSTRUCTIONS_MAY_DEPEND_ON_POINT_SIZE,
615 ),
616 ("FORCE_INTEGER_PPEM", Self::FORCE_INTEGER_PPEM),
617 (
618 "INSTRUCTIONS_MAY_ALTER_ADVANCE_WIDTH",
619 Self::INSTRUCTIONS_MAY_ALTER_ADVANCE_WIDTH,
620 ),
621 (
622 "LOSSLESS_TRANSFORMED_FONT_DATA",
623 Self::LOSSLESS_TRANSFORMED_FONT_DATA,
624 ),
625 ("CONVERTED_FONT", Self::CONVERTED_FONT),
626 ("OPTIMIZED_FOR_CLEARTYPE", Self::OPTIMIZED_FOR_CLEARTYPE),
627 ("LAST_RESORT_FONT", Self::LAST_RESORT_FONT),
628 ];
629 let mut first = true;
630 for (name, value) in members {
631 if self.contains(*value) {
632 if !first {
633 f.write_str(" | ")?;
634 }
635 first = false;
636 f.write_str(name)?;
637 }
638 }
639 if first {
640 f.write_str("(empty)")?;
641 }
642 Ok(())
643 }
644}
645
646impl std::fmt::Binary for Flags {
647 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
648 std::fmt::Binary::fmt(&self.bits, f)
649 }
650}
651
652impl std::fmt::Octal for Flags {
653 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
654 std::fmt::Octal::fmt(&self.bits, f)
655 }
656}
657
658impl std::fmt::LowerHex for Flags {
659 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
660 std::fmt::LowerHex::fmt(&self.bits, f)
661 }
662}
663
664impl std::fmt::UpperHex for Flags {
665 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
666 std::fmt::UpperHex::fmt(&self.bits, f)
667 }
668}
669
670impl font_types::Scalar for Flags {
671 type Raw = <u16 as font_types::Scalar>::Raw;
672 fn to_raw(self) -> Self::Raw {
673 self.bits().to_raw()
674 }
675 fn from_raw(raw: Self::Raw) -> Self {
676 let t = <u16>::from_raw(raw);
677 Self::from_bits_truncate(t)
678 }
679}
680
681impl<'a> MinByteRange<'a> for Head<'a> {
682 fn min_byte_range(&self) -> Range<usize> {
683 0..self.glyph_data_format_byte_range().end
684 }
685 fn min_table_bytes(&self) -> &'a [u8] {
686 let range = self.min_byte_range();
687 self.data.as_bytes().get(range).unwrap_or_default()
688 }
689}
690
691impl TopLevelTable for Head<'_> {
692 const TAG: Tag = Tag::new(b"head");
694}
695
696impl ReadArgs for Head<'_> {
697 type Args = ();
698}
699
700impl<'a> FontRead<'a> for Head<'a> {
701 fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
702 #[allow(clippy::absurd_extreme_comparisons)]
703 if data.len() < Self::MIN_SIZE {
704 return Err(ReadError::OutOfBounds);
705 }
706 Ok(Self { data })
707 }
708}
709
710#[derive(Clone)]
713pub struct Head<'a> {
714 data: FontData<'a>,
715}
716
717#[allow(clippy::needless_lifetimes)]
718impl<'a> Head<'a> {
719 pub const MIN_SIZE: usize = (MajorMinor::RAW_BYTE_LEN
720 + Fixed::RAW_BYTE_LEN
721 + u32::RAW_BYTE_LEN
722 + u32::RAW_BYTE_LEN
723 + Flags::RAW_BYTE_LEN
724 + u16::RAW_BYTE_LEN
725 + LongDateTime::RAW_BYTE_LEN
726 + LongDateTime::RAW_BYTE_LEN
727 + i16::RAW_BYTE_LEN
728 + i16::RAW_BYTE_LEN
729 + i16::RAW_BYTE_LEN
730 + i16::RAW_BYTE_LEN
731 + MacStyle::RAW_BYTE_LEN
732 + u16::RAW_BYTE_LEN
733 + i16::RAW_BYTE_LEN
734 + i16::RAW_BYTE_LEN
735 + i16::RAW_BYTE_LEN);
736 basic_table_impls!(impl_the_methods);
737
738 pub fn version(&self) -> MajorMinor {
740 let range = self.version_byte_range();
741 self.data.read_at(range.start).ok().unwrap()
742 }
743
744 pub fn font_revision(&self) -> Fixed {
746 let range = self.font_revision_byte_range();
747 self.data.read_at(range.start).ok().unwrap()
748 }
749
750 pub fn checksum_adjustment(&self) -> u32 {
756 let range = self.checksum_adjustment_byte_range();
757 self.data.read_at(range.start).ok().unwrap()
758 }
759
760 pub fn magic_number(&self) -> u32 {
762 let range = self.magic_number_byte_range();
763 self.data.read_at(range.start).ok().unwrap()
764 }
765
766 pub fn flags(&self) -> Flags {
768 let range = self.flags_byte_range();
769 self.data.read_at(range.start).ok().unwrap()
770 }
771
772 pub fn units_per_em(&self) -> u16 {
777 let range = self.units_per_em_byte_range();
778 self.data.read_at(range.start).ok().unwrap()
779 }
780
781 pub fn created(&self) -> LongDateTime {
784 let range = self.created_byte_range();
785 self.data.read_at(range.start).ok().unwrap()
786 }
787
788 pub fn modified(&self) -> LongDateTime {
791 let range = self.modified_byte_range();
792 self.data.read_at(range.start).ok().unwrap()
793 }
794
795 pub fn x_min(&self) -> i16 {
797 let range = self.x_min_byte_range();
798 self.data.read_at(range.start).ok().unwrap()
799 }
800
801 pub fn y_min(&self) -> i16 {
803 let range = self.y_min_byte_range();
804 self.data.read_at(range.start).ok().unwrap()
805 }
806
807 pub fn x_max(&self) -> i16 {
809 let range = self.x_max_byte_range();
810 self.data.read_at(range.start).ok().unwrap()
811 }
812
813 pub fn y_max(&self) -> i16 {
815 let range = self.y_max_byte_range();
816 self.data.read_at(range.start).ok().unwrap()
817 }
818
819 pub fn mac_style(&self) -> MacStyle {
821 let range = self.mac_style_byte_range();
822 self.data.read_at(range.start).ok().unwrap()
823 }
824
825 pub fn lowest_rec_ppem(&self) -> u16 {
827 let range = self.lowest_rec_ppem_byte_range();
828 self.data.read_at(range.start).ok().unwrap()
829 }
830
831 pub fn font_direction_hint(&self) -> i16 {
833 let range = self.font_direction_hint_byte_range();
834 self.data.read_at(range.start).ok().unwrap()
835 }
836
837 pub fn index_to_loc_format(&self) -> i16 {
839 let range = self.index_to_loc_format_byte_range();
840 self.data.read_at(range.start).ok().unwrap()
841 }
842
843 pub fn glyph_data_format(&self) -> i16 {
845 let range = self.glyph_data_format_byte_range();
846 self.data.read_at(range.start).ok().unwrap()
847 }
848
849 pub fn version_byte_range(&self) -> Range<usize> {
850 let start = 0;
851 let end = start + MajorMinor::RAW_BYTE_LEN;
852 start..end
853 }
854
855 pub fn font_revision_byte_range(&self) -> Range<usize> {
856 let start = self.version_byte_range().end;
857 let end = start + Fixed::RAW_BYTE_LEN;
858 start..end
859 }
860
861 pub fn checksum_adjustment_byte_range(&self) -> Range<usize> {
862 let start = self.font_revision_byte_range().end;
863 let end = start + u32::RAW_BYTE_LEN;
864 start..end
865 }
866
867 pub fn magic_number_byte_range(&self) -> Range<usize> {
868 let start = self.checksum_adjustment_byte_range().end;
869 let end = start + u32::RAW_BYTE_LEN;
870 start..end
871 }
872
873 pub fn flags_byte_range(&self) -> Range<usize> {
874 let start = self.magic_number_byte_range().end;
875 let end = start + Flags::RAW_BYTE_LEN;
876 start..end
877 }
878
879 pub fn units_per_em_byte_range(&self) -> Range<usize> {
880 let start = self.flags_byte_range().end;
881 let end = start + u16::RAW_BYTE_LEN;
882 start..end
883 }
884
885 pub fn created_byte_range(&self) -> Range<usize> {
886 let start = self.units_per_em_byte_range().end;
887 let end = start + LongDateTime::RAW_BYTE_LEN;
888 start..end
889 }
890
891 pub fn modified_byte_range(&self) -> Range<usize> {
892 let start = self.created_byte_range().end;
893 let end = start + LongDateTime::RAW_BYTE_LEN;
894 start..end
895 }
896
897 pub fn x_min_byte_range(&self) -> Range<usize> {
898 let start = self.modified_byte_range().end;
899 let end = start + i16::RAW_BYTE_LEN;
900 start..end
901 }
902
903 pub fn y_min_byte_range(&self) -> Range<usize> {
904 let start = self.x_min_byte_range().end;
905 let end = start + i16::RAW_BYTE_LEN;
906 start..end
907 }
908
909 pub fn x_max_byte_range(&self) -> Range<usize> {
910 let start = self.y_min_byte_range().end;
911 let end = start + i16::RAW_BYTE_LEN;
912 start..end
913 }
914
915 pub fn y_max_byte_range(&self) -> Range<usize> {
916 let start = self.x_max_byte_range().end;
917 let end = start + i16::RAW_BYTE_LEN;
918 start..end
919 }
920
921 pub fn mac_style_byte_range(&self) -> Range<usize> {
922 let start = self.y_max_byte_range().end;
923 let end = start + MacStyle::RAW_BYTE_LEN;
924 start..end
925 }
926
927 pub fn lowest_rec_ppem_byte_range(&self) -> Range<usize> {
928 let start = self.mac_style_byte_range().end;
929 let end = start + u16::RAW_BYTE_LEN;
930 start..end
931 }
932
933 pub fn font_direction_hint_byte_range(&self) -> Range<usize> {
934 let start = self.lowest_rec_ppem_byte_range().end;
935 let end = start + i16::RAW_BYTE_LEN;
936 start..end
937 }
938
939 pub fn index_to_loc_format_byte_range(&self) -> Range<usize> {
940 let start = self.font_direction_hint_byte_range().end;
941 let end = start + i16::RAW_BYTE_LEN;
942 start..end
943 }
944
945 pub fn glyph_data_format_byte_range(&self) -> Range<usize> {
946 let start = self.index_to_loc_format_byte_range().end;
947 let end = start + i16::RAW_BYTE_LEN;
948 start..end
949 }
950}
951
952const _: () = assert!(FontData::default_data_long_enough(Head::MIN_SIZE));
953
954impl Default for Head<'_> {
955 fn default() -> Self {
956 Self {
957 data: FontData::default_table_data(),
958 }
959 }
960}