hb_subset/subset/
flags.rs

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