diff --git a/Cargo.toml b/Cargo.toml index 2bd4282..3ef007d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ttf-parser" -version = "0.24.1" +version = "0.25.0" authors = ["Yevhenii Reizner "] keywords = ["ttf", "truetype", "opentype"] categories = ["parser-implementations"] diff --git a/src/tables/cmap/format12.rs b/src/tables/cmap/format12.rs index 8613495..450684d 100644 --- a/src/tables/cmap/format12.rs +++ b/src/tables/cmap/format12.rs @@ -65,13 +65,16 @@ impl<'a> Subtable12<'a> { u16::try_from(id).ok().map(GlyphId) } + /// Iterate over each codepoint defined in this table. + pub fn codepoints_iter(&'a self) -> impl Iterator + 'a { + self.groups + .into_iter() + .flat_map(|group| group.start_char_code..=group.end_char_code) + } + /// Calls `f` for each codepoint defined in this table. - pub fn codepoints(&self, mut f: impl FnMut(u32)) { - for group in self.groups { - for code_point in group.start_char_code..=group.end_char_code { - f(code_point); - } - } + pub fn codepoints(&self, f: impl FnMut(u32)) { + self.codepoints_iter().for_each(f) } } diff --git a/src/tables/cmap/format4.rs b/src/tables/cmap/format4.rs index d1d432b..92d92ac 100644 --- a/src/tables/cmap/format4.rs +++ b/src/tables/cmap/format4.rs @@ -100,18 +100,19 @@ impl<'a> Subtable4<'a> { None } - /// Calls `f` for each codepoint defined in this table. - pub fn codepoints(&self, mut f: impl FnMut(u32)) { - for (start, end) in self.start_codes.into_iter().zip(self.end_codes) { - // OxFFFF value is special and indicates codes end. - if start == end && start == 0xFFFF { - break; - } + /// Iterate over each codepoint defined in this table. + pub fn codepoints_iter(&'a self) -> impl Iterator + 'a { + self.start_codes + .into_iter() + .zip(self.end_codes) + .take_while(|&(start, end)| !(start == end && start == 0xFFFF)) + .flat_map(|(start, end)| start..=end) + .map(u32::from) + } - for code_point in start..=end { - f(u32::from(code_point)); - } - } + /// Calls `f` for each codepoint defined in this table. + pub fn codepoints(&self, f: impl FnMut(u32)) { + self.codepoints_iter().for_each(f) } }