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 HeaderFlags {
13 bits: u16,
14}
15
16impl HeaderFlags {
17 pub const ALWAYS_SET: Self = Self { bits: 0x0001 };
19
20 pub const DRAW_OUTLINES: Self = Self { bits: 0x0002 };
22}
23
24impl HeaderFlags {
25 #[inline]
27 pub const fn empty() -> Self {
28 Self { bits: 0 }
29 }
30
31 #[inline]
33 pub const fn all() -> Self {
34 Self {
35 bits: Self::ALWAYS_SET.bits | Self::DRAW_OUTLINES.bits,
36 }
37 }
38
39 #[inline]
41 pub const fn bits(&self) -> u16 {
42 self.bits
43 }
44
45 #[inline]
48 pub const fn from_bits(bits: u16) -> Option<Self> {
49 if (bits & !Self::all().bits()) == 0 {
50 Some(Self { bits })
51 } else {
52 None
53 }
54 }
55
56 #[inline]
59 pub const fn from_bits_truncate(bits: u16) -> Self {
60 Self {
61 bits: bits & Self::all().bits,
62 }
63 }
64
65 #[inline]
67 pub const fn is_empty(&self) -> bool {
68 self.bits() == Self::empty().bits()
69 }
70
71 #[inline]
73 pub const fn intersects(&self, other: Self) -> bool {
74 !(Self {
75 bits: self.bits & other.bits,
76 })
77 .is_empty()
78 }
79
80 #[inline]
82 pub const fn contains(&self, other: Self) -> bool {
83 (self.bits & other.bits) == other.bits
84 }
85
86 #[inline]
88 pub fn insert(&mut self, other: Self) {
89 self.bits |= other.bits;
90 }
91
92 #[inline]
94 pub fn remove(&mut self, other: Self) {
95 self.bits &= !other.bits;
96 }
97
98 #[inline]
100 pub fn toggle(&mut self, other: Self) {
101 self.bits ^= other.bits;
102 }
103
104 #[inline]
115 #[must_use]
116 pub const fn intersection(self, other: Self) -> Self {
117 Self {
118 bits: self.bits & other.bits,
119 }
120 }
121
122 #[inline]
133 #[must_use]
134 pub const fn union(self, other: Self) -> Self {
135 Self {
136 bits: self.bits | other.bits,
137 }
138 }
139
140 #[inline]
153 #[must_use]
154 pub const fn difference(self, other: Self) -> Self {
155 Self {
156 bits: self.bits & !other.bits,
157 }
158 }
159}
160
161impl std::ops::BitOr for HeaderFlags {
162 type Output = Self;
163
164 #[inline]
166 fn bitor(self, other: HeaderFlags) -> Self {
167 Self {
168 bits: self.bits | other.bits,
169 }
170 }
171}
172
173impl std::ops::BitOrAssign for HeaderFlags {
174 #[inline]
176 fn bitor_assign(&mut self, other: Self) {
177 self.bits |= other.bits;
178 }
179}
180
181impl std::ops::BitXor for HeaderFlags {
182 type Output = Self;
183
184 #[inline]
186 fn bitxor(self, other: Self) -> Self {
187 Self {
188 bits: self.bits ^ other.bits,
189 }
190 }
191}
192
193impl std::ops::BitXorAssign for HeaderFlags {
194 #[inline]
196 fn bitxor_assign(&mut self, other: Self) {
197 self.bits ^= other.bits;
198 }
199}
200
201impl std::ops::BitAnd for HeaderFlags {
202 type Output = Self;
203
204 #[inline]
206 fn bitand(self, other: Self) -> Self {
207 Self {
208 bits: self.bits & other.bits,
209 }
210 }
211}
212
213impl std::ops::BitAndAssign for HeaderFlags {
214 #[inline]
216 fn bitand_assign(&mut self, other: Self) {
217 self.bits &= other.bits;
218 }
219}
220
221impl std::ops::Sub for HeaderFlags {
222 type Output = Self;
223
224 #[inline]
226 fn sub(self, other: Self) -> Self {
227 Self {
228 bits: self.bits & !other.bits,
229 }
230 }
231}
232
233impl std::ops::SubAssign for HeaderFlags {
234 #[inline]
236 fn sub_assign(&mut self, other: Self) {
237 self.bits &= !other.bits;
238 }
239}
240
241impl std::ops::Not for HeaderFlags {
242 type Output = Self;
243
244 #[inline]
246 fn not(self) -> Self {
247 Self { bits: !self.bits } & Self::all()
248 }
249}
250
251impl std::fmt::Debug for HeaderFlags {
252 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
253 let members: &[(&str, Self)] = &[
254 ("ALWAYS_SET", Self::ALWAYS_SET),
255 ("DRAW_OUTLINES", Self::DRAW_OUTLINES),
256 ];
257 let mut first = true;
258 for (name, value) in members {
259 if self.contains(*value) {
260 if !first {
261 f.write_str(" | ")?;
262 }
263 first = false;
264 f.write_str(name)?;
265 }
266 }
267 if first {
268 f.write_str("(empty)")?;
269 }
270 Ok(())
271 }
272}
273
274impl std::fmt::Binary for HeaderFlags {
275 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
276 std::fmt::Binary::fmt(&self.bits, f)
277 }
278}
279
280impl std::fmt::Octal for HeaderFlags {
281 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
282 std::fmt::Octal::fmt(&self.bits, f)
283 }
284}
285
286impl std::fmt::LowerHex for HeaderFlags {
287 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
288 std::fmt::LowerHex::fmt(&self.bits, f)
289 }
290}
291
292impl std::fmt::UpperHex for HeaderFlags {
293 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
294 std::fmt::UpperHex::fmt(&self.bits, f)
295 }
296}
297
298impl font_types::Scalar for HeaderFlags {
299 type Raw = <u16 as font_types::Scalar>::Raw;
300 fn to_raw(self) -> Self::Raw {
301 self.bits().to_raw()
302 }
303 fn from_raw(raw: Self::Raw) -> Self {
304 let t = <u16>::from_raw(raw);
305 Self::from_bits_truncate(t)
306 }
307}
308
309impl<'a> MinByteRange<'a> for Sbix<'a> {
310 fn min_byte_range(&self) -> Range<usize> {
311 0..self.strike_offsets_byte_range().end
312 }
313 fn min_table_bytes(&self) -> &'a [u8] {
314 let range = self.min_byte_range();
315 self.offset_data().as_bytes().get(range).unwrap_or_default()
316 }
317}
318
319impl TopLevelTable for Sbix<'_> {
320 const TAG: Tag = Tag::new(b"sbix");
322}
323
324impl ReadArgs for Sbix<'_> {
325 type Args = u32;
326}
327
328impl<'a> FontRead<'a> for Sbix<'a> {
329 fn read_with_args(data: FontData<'a>, args: u32) -> Result<Self, ReadError> {
330 let num_glyphs = args;
331
332 #[allow(clippy::absurd_extreme_comparisons)]
333 if data.len() < Self::MIN_SIZE {
334 return Err(ReadError::OutOfBounds);
335 }
336 let Some(data) = TableDataRef::new(data.as_bytes()) else {
337 return Err(ReadError::OutOfBounds);
338 };
339 Ok(Self { data, num_glyphs })
340 }
341}
342
343impl<'a> Sbix<'a> {
344 pub fn read(data: FontData<'a>, num_glyphs: u32) -> Result<Self, ReadError> {
349 let args = num_glyphs;
350 Self::read_with_args(data, args)
351 }
352}
353
354#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
356#[repr(C, packed)]
357#[doc(hidden)]
358pub struct SbixFixedHeader {
359 pub version: BigEndian<u16>,
360 pub flags: BigEndian<HeaderFlags>,
361 pub num_strikes: BigEndian<u32>,
362}
363
364#[allow(clippy::absurd_extreme_comparisons)]
365const _: () = assert!(std::mem::size_of::<SbixFixedHeader>() <= Sbix::MIN_SIZE);
366
367#[derive(Clone)]
369pub struct Sbix<'a> {
370 data: TableDataRef<'a, SbixFixedHeader>,
371 num_glyphs: u32,
372}
373
374#[allow(clippy::needless_lifetimes)]
375impl<'a> Sbix<'a> {
376 pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + HeaderFlags::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
377 basic_table_impls!(impl_the_methods);
378
379 pub fn version(&self) -> u16 {
381 self.data.header().version.get()
382 }
383
384 pub fn flags(&self) -> HeaderFlags {
388 self.data.header().flags.get()
389 }
390
391 pub fn num_strikes(&self) -> u32 {
393 self.data.header().num_strikes.get()
394 }
395
396 pub fn strike_offsets(&self) -> &'a [BigEndian<Offset32>] {
398 let range = self.strike_offsets_byte_range();
399 self.offset_data()
400 .read_array(range)
401 .ok()
402 .unwrap_or_default()
403 }
404
405 pub fn strikes(&self) -> ArrayOfOffsets<'a, Strike<'a>, Offset32> {
407 let data = self.offset_data();
408 let offsets = self.strike_offsets();
409 let args = self.num_glyphs();
410 ArrayOfOffsets::new(offsets, data, args)
411 }
412
413 pub(crate) fn num_glyphs(&self) -> u32 {
414 self.num_glyphs
415 }
416
417 pub fn version_byte_range(&self) -> Range<usize> {
418 let start = 0usize;
419 let end = start.saturating_add(u16::RAW_BYTE_LEN);
420 start..end
421 }
422
423 pub fn flags_byte_range(&self) -> Range<usize> {
424 let start = self.version_byte_range().end;
425 let end = start.saturating_add(HeaderFlags::RAW_BYTE_LEN);
426 start..end
427 }
428
429 pub fn num_strikes_byte_range(&self) -> Range<usize> {
430 let start = self.flags_byte_range().end;
431 let end = start.saturating_add(u32::RAW_BYTE_LEN);
432 start..end
433 }
434
435 pub fn strike_offsets_byte_range(&self) -> Range<usize> {
436 let num_strikes = self.num_strikes();
437 let start = self.num_strikes_byte_range().end;
438 let end = start.saturating_add(
439 (transforms::to_usize(num_strikes)).saturating_mul(Offset32::RAW_BYTE_LEN),
440 );
441 start..end
442 }
443}
444
445const _: () = assert!(FontData::default_data_long_enough(Sbix::MIN_SIZE));
446
447impl Default for Sbix<'_> {
448 fn default() -> Self {
449 Self {
450 data: TableDataRef::new(FontData::default_table_data().as_bytes())
451 .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
452 num_glyphs: Default::default(),
453 }
454 }
455}
456
457impl<'a> MinByteRange<'a> for Strike<'a> {
458 fn min_byte_range(&self) -> Range<usize> {
459 0..self.glyph_data_offsets_byte_range().end
460 }
461 fn min_table_bytes(&self) -> &'a [u8] {
462 let range = self.min_byte_range();
463 self.offset_data().as_bytes().get(range).unwrap_or_default()
464 }
465}
466
467impl ReadArgs for Strike<'_> {
468 type Args = u32;
469}
470
471impl<'a> FontRead<'a> for Strike<'a> {
472 fn read_with_args(data: FontData<'a>, args: u32) -> Result<Self, ReadError> {
473 let num_glyphs = args;
474
475 #[allow(clippy::absurd_extreme_comparisons)]
476 if data.len() < Self::MIN_SIZE {
477 return Err(ReadError::OutOfBounds);
478 }
479 let Some(data) = TableDataRef::new(data.as_bytes()) else {
480 return Err(ReadError::OutOfBounds);
481 };
482 Ok(Self { data, num_glyphs })
483 }
484}
485
486impl<'a> Strike<'a> {
487 pub fn read(data: FontData<'a>, num_glyphs: u32) -> Result<Self, ReadError> {
492 let args = num_glyphs;
493 Self::read_with_args(data, args)
494 }
495}
496
497#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
499#[repr(C, packed)]
500#[doc(hidden)]
501pub struct StrikeFixedHeader {
502 pub ppem: BigEndian<u16>,
503 pub ppi: BigEndian<u16>,
504}
505
506#[allow(clippy::absurd_extreme_comparisons)]
507const _: () = assert!(std::mem::size_of::<StrikeFixedHeader>() <= Strike::MIN_SIZE);
508
509#[derive(Clone)]
511pub struct Strike<'a> {
512 data: TableDataRef<'a, StrikeFixedHeader>,
513 num_glyphs: u32,
514}
515
516#[allow(clippy::needless_lifetimes)]
517impl<'a> Strike<'a> {
518 pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
519 basic_table_impls!(impl_the_methods);
520
521 pub fn ppem(&self) -> u16 {
523 self.data.header().ppem.get()
524 }
525
526 pub fn ppi(&self) -> u16 {
528 self.data.header().ppi.get()
529 }
530
531 pub fn glyph_data_offsets(&self) -> &'a [BigEndian<u32>] {
533 let range = self.glyph_data_offsets_byte_range();
534 self.offset_data()
535 .read_array(range)
536 .ok()
537 .unwrap_or_default()
538 }
539
540 pub(crate) fn num_glyphs(&self) -> u32 {
541 self.num_glyphs
542 }
543
544 pub fn ppem_byte_range(&self) -> Range<usize> {
545 let start = 0usize;
546 let end = start.saturating_add(u16::RAW_BYTE_LEN);
547 start..end
548 }
549
550 pub fn ppi_byte_range(&self) -> Range<usize> {
551 let start = self.ppem_byte_range().end;
552 let end = start.saturating_add(u16::RAW_BYTE_LEN);
553 start..end
554 }
555
556 pub fn glyph_data_offsets_byte_range(&self) -> Range<usize> {
557 let num_glyphs = self.num_glyphs();
558 let start = self.ppi_byte_range().end;
559 let end = start.saturating_add(
560 (transforms::add(num_glyphs, 1_usize)).saturating_mul(u32::RAW_BYTE_LEN),
561 );
562 start..end
563 }
564}
565
566const _: () = assert!(FontData::default_data_long_enough(Strike::MIN_SIZE));
567
568impl Default for Strike<'_> {
569 fn default() -> Self {
570 Self {
571 data: TableDataRef::new(FontData::default_table_data().as_bytes())
572 .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
573 num_glyphs: Default::default(),
574 }
575 }
576}
577
578impl<'a> MinByteRange<'a> for GlyphData<'a> {
579 fn min_byte_range(&self) -> Range<usize> {
580 0..self.data_byte_range().end
581 }
582 fn min_table_bytes(&self) -> &'a [u8] {
583 let range = self.min_byte_range();
584 self.offset_data().as_bytes().get(range).unwrap_or_default()
585 }
586}
587
588impl ReadArgs for GlyphData<'_> {
589 type Args = ();
590}
591
592impl<'a> FontRead<'a> for GlyphData<'a> {
593 fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
594 #[allow(clippy::absurd_extreme_comparisons)]
595 if data.len() < Self::MIN_SIZE {
596 return Err(ReadError::OutOfBounds);
597 }
598 let Some(data) = TableDataRef::new(data.as_bytes()) else {
599 return Err(ReadError::OutOfBounds);
600 };
601 Ok(Self { data })
602 }
603}
604
605#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
607#[repr(C, packed)]
608#[doc(hidden)]
609pub struct GlyphDataFixedHeader {
610 pub origin_offset_x: BigEndian<i16>,
611 pub origin_offset_y: BigEndian<i16>,
612 pub graphic_type: BigEndian<Tag>,
613}
614
615#[allow(clippy::absurd_extreme_comparisons)]
616const _: () = assert!(std::mem::size_of::<GlyphDataFixedHeader>() <= GlyphData::MIN_SIZE);
617
618#[derive(Clone)]
620pub struct GlyphData<'a> {
621 data: TableDataRef<'a, GlyphDataFixedHeader>,
622}
623
624#[allow(clippy::needless_lifetimes)]
625impl<'a> GlyphData<'a> {
626 pub const MIN_SIZE: usize = (i16::RAW_BYTE_LEN + i16::RAW_BYTE_LEN + Tag::RAW_BYTE_LEN);
627 basic_table_impls!(impl_the_methods);
628
629 pub fn origin_offset_x(&self) -> i16 {
631 self.data.header().origin_offset_x.get()
632 }
633
634 pub fn origin_offset_y(&self) -> i16 {
636 self.data.header().origin_offset_y.get()
637 }
638
639 pub fn graphic_type(&self) -> Tag {
641 self.data.header().graphic_type.get()
642 }
643
644 pub fn data(&self) -> &'a [u8] {
646 let range = self.data_byte_range();
647 self.offset_data()
648 .read_array(range)
649 .ok()
650 .unwrap_or_default()
651 }
652
653 pub fn origin_offset_x_byte_range(&self) -> Range<usize> {
654 let start = 0usize;
655 let end = start.saturating_add(i16::RAW_BYTE_LEN);
656 start..end
657 }
658
659 pub fn origin_offset_y_byte_range(&self) -> Range<usize> {
660 let start = self.origin_offset_x_byte_range().end;
661 let end = start.saturating_add(i16::RAW_BYTE_LEN);
662 start..end
663 }
664
665 pub fn graphic_type_byte_range(&self) -> Range<usize> {
666 let start = self.origin_offset_y_byte_range().end;
667 let end = start.saturating_add(Tag::RAW_BYTE_LEN);
668 start..end
669 }
670
671 pub fn data_byte_range(&self) -> Range<usize> {
672 let start = self.graphic_type_byte_range().end;
673 let end = start.saturating_add(
674 self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN,
675 );
676 start..end
677 }
678}
679
680const _: () = assert!(FontData::default_data_long_enough(GlyphData::MIN_SIZE));
681
682impl Default for GlyphData<'_> {
683 fn default() -> Self {
684 Self {
685 data: TableDataRef::new(FontData::default_table_data().as_bytes())
686 .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
687 }
688 }
689}