read_fonts/tables/layout/lookup_flag.rs
1//! The lookup flag type.
2//!
3//! This is kind-of-but-not-quite-exactly a bit enumeration, and so we implement
4//! it manually.
5
6use core::ops::{BitOr, BitOrAssign, Sub};
7
8/// The [LookupFlag](https://learn.microsoft.com/en-us/typography/opentype/spec/chapter2#lookupFlag) bit enumeration.
9#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct LookupFlag(u16);
12
13//NOTE: this impl has the potential to make garbage if used on two lookupflag
14//instances which have different mark attachment masks set, but as that field
15//is not really used in compilation, which is where this impl will be helpful,
16//the risk that this is the source of an actual bug seems very low,
17impl BitOr for LookupFlag {
18 type Output = Self;
19
20 fn bitor(self, rhs: Self) -> Self::Output {
21 Self(self.0 | rhs.0)
22 }
23}
24
25impl BitOrAssign for LookupFlag {
26 fn bitor_assign(&mut self, rhs: Self) {
27 self.0 |= rhs.0
28 }
29}
30
31impl Sub for LookupFlag {
32 type Output = Self;
33 /// Returns the difference between the flags in `self` and `other`.
34 #[inline]
35 fn sub(self, rhs: Self) -> Self {
36 Self(self.0 & !rhs.0)
37 }
38}
39
40impl LookupFlag {
41 /// This bit relates only to the correct processing of GPOS type 3 (cursive attachment) lookups
42 ///
43 /// When this bit is set, the last glyph in a given sequence to which the cursive
44 /// attachment lookup is applied, will be positioned on the baseline.
45 pub const RIGHT_TO_LEFT: Self = LookupFlag(0x0001);
46 /// If set, skips over base glyphs
47 pub const IGNORE_BASE_GLYPHS: Self = LookupFlag(0x002);
48 /// If set, skips over ligatures
49 pub const IGNORE_LIGATURES: Self = LookupFlag(0x004);
50 /// If set, skips over all combining marks
51 pub const IGNORE_MARKS: Self = LookupFlag(0x008);
52 /// If set, indicates that the lookup table structure is followed by a MarkFilteringSet field.
53 ///
54 /// The layout engine skips over all mark glyphs not in the mark filtering set indicated.
55 pub const USE_MARK_FILTERING_SET: Self = LookupFlag(0x010);
56
57 // union of all flags, above
58 const FLAG_MASK: Self = LookupFlag(0x1F);
59
60 /// Return new, empty flags
61 pub fn empty() -> Self {
62 Self(0)
63 }
64
65 /// Construct a LookupFlag from a raw value, discarding invalid bits
66 pub fn from_bits_truncate(bits: u16) -> Self {
67 const VALID_BITS: u16 = !0x00E0;
68 Self(bits & VALID_BITS)
69 }
70
71 /// Raw transmutation to u16.
72 pub fn to_bits(self) -> u16 {
73 self.0
74 }
75
76 /// Returns `true` if all of the flags in `other` are contained within `self`.
77 #[inline]
78 pub const fn contains(&self, other: Self) -> bool {
79 // only count flag bits
80 let other = other.0 & Self::FLAG_MASK.0;
81 (self.0 & other) == other
82 }
83
84 /// If not zero, skips over all marks of attachment type different from specified.
85 pub fn mark_attachment_class(self) -> Option<u16> {
86 let val = self.0 & 0xff00;
87 if val == 0 {
88 None
89 } else {
90 Some(val >> 8)
91 }
92 }
93
94 /// If not zero, skips over all marks of attachment type different from specified.
95 pub fn set_mark_attachment_class(&mut self, val: u16) {
96 let val = (val & 0xff) << 8;
97 self.0 = (self.0 & 0xff) | val;
98 }
99}
100
101impl types::Scalar for LookupFlag {
102 type Raw = <u16 as types::Scalar>::Raw;
103 fn to_raw(self) -> Self::Raw {
104 self.0.to_raw()
105 }
106 fn from_raw(raw: Self::Raw) -> Self {
107 let t = <u16>::from_raw(raw);
108 Self(t)
109 }
110}