hb_subset/subset/
flags.rs

1use crate::{sys, SubsetInput};
2use std::ops::Deref;
3use std::ops::DerefMut;
4
5/// Flags for [`SubsetInput`].
6///
7/// These flags can be used to instruct which tables the subsetter should touch, and how.
8///
9/// # Default flags
10/// ```
11/// # use hb_subset::Flags;
12/// assert_eq!(
13///     *Flags::default()
14///         .retain_hinting()
15///         .remap_glyph_indices()
16///         .retain_subroutines()
17///         .retain_subroutines()
18///         .remove_legacy_names()
19///         .remove_overlap_simple_flag()
20///         .remove_unrecognized_tables()
21///         .remove_notdef_outline()
22///         .remove_glyph_names()
23///         .recompute_unicode_ranges()
24///         .retain_layout_closure(),
25///     Flags::default()
26/// );
27/// ```
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29pub struct Flags(pub sys::hb_subset_flags_t);
30
31impl Flags {
32    fn add_flag(&mut self, flag: sys::hb_subset_flags_t) -> &mut Self {
33        self.0 |= flag;
34        self
35    }
36
37    fn remove_flag(&mut self, flag: sys::hb_subset_flags_t) -> &mut Self {
38        self.0 .0 &= !flag.0;
39        self
40    }
41
42    /// Instructs subsetter to remove hinting instructions.
43    pub fn remove_hinting(&mut self) -> &mut Self {
44        self.add_flag(sys::hb_subset_flags_t::NO_HINTING)
45    }
46
47    /// Instructs subsetter to retain hinting instructions.
48    pub fn retain_hinting(&mut self) -> &mut Self {
49        self.remove_flag(sys::hb_subset_flags_t::NO_HINTING)
50    }
51
52    /// Instructs subsetter to glyph indices.
53    ///
54    /// If a glyph gets dropped, its index will still be retained as an empty glyph.
55    pub fn retain_glyph_indices(&mut self) -> &mut Self {
56        self.add_flag(sys::hb_subset_flags_t::RETAIN_GIDS)
57    }
58
59    /// Instructs subsetter to map old glyph indices to new ones.
60    pub fn remap_glyph_indices(&mut self) -> &mut Self {
61        self.remove_flag(sys::hb_subset_flags_t::RETAIN_GIDS)
62    }
63
64    /// Instructs subsetter to remove subroutines from the CFF glyphs.
65    ///
66    /// This has only effect when subsetting a CFF font.
67    pub fn remove_subroutines(&mut self) -> &mut Self {
68        self.add_flag(sys::hb_subset_flags_t::DESUBROUTINIZE)
69    }
70
71    /// Instructs subsetter to retain subroutines for CFF glyphs.
72    pub fn retain_subroutines(&mut self) -> &mut Self {
73        self.remove_flag(sys::hb_subset_flags_t::DESUBROUTINIZE)
74    }
75
76    /// Instructs subsetter to keep non-unicode name records.
77    pub fn retain_legacy_names(&mut self) -> &mut Self {
78        self.add_flag(sys::hb_subset_flags_t::NAME_LEGACY)
79    }
80
81    /// Instructs subsetter to remove non-unicode name records.
82    pub fn remove_legacy_names(&mut self) -> &mut Self {
83        self.remove_flag(sys::hb_subset_flags_t::NAME_LEGACY)
84    }
85
86    /// Instructs subsetter to set `OVERLAP_SIMPLE` flag for simple glyphs.
87    ///
88    /// This is not required for OpenType, but may affect rendering in some platforms.
89    pub fn set_overlap_simple_flag(&mut self) -> &mut Self {
90        self.add_flag(sys::hb_subset_flags_t::SET_OVERLAPS_FLAG)
91    }
92
93    /// Instructs subsetter to not emit `OVERLAP_SIMPLE` flag.
94    pub fn remove_overlap_simple_flag(&mut self) -> &mut Self {
95        self.remove_flag(sys::hb_subset_flags_t::SET_OVERLAPS_FLAG)
96    }
97
98    /// Instructs subsetter to keep unrecognized tables.
99    ///
100    /// The subsetter wil just pass them trough without touching them.
101    pub fn retain_unrecognized_tables(&mut self) -> &mut Self {
102        self.add_flag(sys::hb_subset_flags_t::PASSTHROUGH_UNRECOGNIZED)
103    }
104
105    /// Instructs subsetter to remove unrecognized tables.
106    pub fn remove_unrecognized_tables(&mut self) -> &mut Self {
107        self.remove_flag(sys::hb_subset_flags_t::PASSTHROUGH_UNRECOGNIZED)
108    }
109
110    /// Instructs subsetter to keep glyph outline for `notdef``.
111    pub fn retain_notdef_outline(&mut self) -> &mut Self {
112        self.add_flag(sys::hb_subset_flags_t::NOTDEF_OUTLINE)
113    }
114
115    /// Instructs subsetter to remove glyph outline for `notdef``.
116    pub fn remove_notdef_outline(&mut self) -> &mut Self {
117        self.remove_flag(sys::hb_subset_flags_t::NOTDEF_OUTLINE)
118    }
119
120    /// Instructs subsetter to keep glyph name information.
121    pub fn retain_glyph_names(&mut self) -> &mut Self {
122        self.add_flag(sys::hb_subset_flags_t::GLYPH_NAMES)
123    }
124
125    /// Instructs subsetter to remove glyph name information.
126    pub fn remove_glyph_names(&mut self) -> &mut Self {
127        self.remove_flag(sys::hb_subset_flags_t::GLYPH_NAMES)
128    }
129
130    /// Instructs subsetter to recompute unicode ranges in `OS/2` table.
131    pub fn recompute_unicode_ranges(&mut self) -> &mut Self {
132        self.remove_flag(sys::hb_subset_flags_t::NO_PRUNE_UNICODE_RANGES)
133    }
134
135    /// Instructs subsetter to keep original unicode ranges in `OS/2` table.
136    pub fn retain_unicode_ranges(&mut self) -> &mut Self {
137        self.add_flag(sys::hb_subset_flags_t::NO_PRUNE_UNICODE_RANGES)
138    }
139
140    /// Instructs subsetter to keep glyphs for all possible combinations of already retained glyphs.
141    ///
142    /// For example, if glyphs corresponding to `f` and `i` are retained, then also glyphs corresponding to `ff`, `fi` and
143    /// `ffi` are retained.
144    pub fn retain_layout_closure(&mut self) -> &mut Self {
145        self.remove_flag(sys::hb_subset_flags_t::NO_LAYOUT_CLOSURE)
146    }
147
148    /// Instructs subsetter to keep only minimum set of glyphs disregarding layout closure.
149    pub fn no_layout_closure(&mut self) -> &mut Self {
150        self.add_flag(sys::hb_subset_flags_t::NO_LAYOUT_CLOSURE)
151    }
152}
153
154impl Default for Flags {
155    fn default() -> Self {
156        Self(sys::hb_subset_flags_t::DEFAULT)
157    }
158}
159
160/// Helper which sets the flags on associated [`SubsetInput`] on drop.
161///
162/// See [`SubsetInput::flags`].
163pub struct FlagRef<'s>(pub(crate) &'s mut SubsetInput, pub(crate) Flags);
164
165impl<'s> Deref for FlagRef<'s> {
166    type Target = Flags;
167
168    fn deref(&self) -> &Self::Target {
169        &self.1
170    }
171}
172
173impl<'s> DerefMut for FlagRef<'s> {
174    fn deref_mut(&mut self) -> &mut Self::Target {
175        &mut self.1
176    }
177}
178
179impl<'s> Drop for FlagRef<'s> {
180    fn drop(&mut self) {
181        unsafe { sys::hb_subset_input_set_flags(self.0.as_raw(), self.1 .0 .0) }
182    }
183}